Skip to content

Commit

Permalink
Merge branch 'release/v0.0.86'
Browse files Browse the repository at this point in the history
  • Loading branch information
lmquang committed Apr 13, 2023
2 parents bb8e742 + 80d8cfc commit c09b896
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 30 deletions.
6 changes: 4 additions & 2 deletions pkg/controller/invoice/commission.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (c *controller) storeCommission(db *gorm.DB, l logger.Logger, invoice *mode
return nil, err
}

if len(employeeCommissions) == 0 {
return []model.EmployeeCommission{}, nil
}

return c.store.EmployeeCommission.Create(db, employeeCommissions)
}

Expand All @@ -61,7 +65,6 @@ func (c *controller) calculateCommissionFromInvoice(db *gorm.DB, l logger.Logger

// Get list of project head who will get the commission from this invoice
pics := getPICs(invoice, projectMembers)

var res []model.EmployeeCommission
if len(pics.devLeads) > 0 {
commissionRate := commissionConfigMap[model.HeadPositionTechnicalLead.String()]
Expand Down Expand Up @@ -119,7 +122,6 @@ func (c *controller) calculateCommissionFromInvoice(db *gorm.DB, l logger.Logger
}
res = append(res, c...)
}

if len(pics.suppliers) > 0 {
c, err := c.calculateRefBonusCommission(pics.suppliers, invoice)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/invoice/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ type IController interface {
UpdateStatus(in UpdateStatusInput) (*model.Invoice, error)
MarkInvoiceAsError(invoice *model.Invoice) (*model.Invoice, error)
MarkInvoiceAsPaid(invoice *model.Invoice, sendThankYouEmail bool) (*model.Invoice, error)
MarkInvoiceAsPaidByBasecampWebhookMessage(invoice *model.Invoice, msg *model.BasecampWebhookMessage) (*model.Invoice, error)
}
18 changes: 18 additions & 0 deletions pkg/controller/invoice/update_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ func (c *controller) MarkInvoiceAsPaid(invoice *model.Invoice, sendThankYouEmail
return invoice, nil
}

func (c *controller) MarkInvoiceAsPaidByBasecampWebhookMessage(invoice *model.Invoice, msg *model.BasecampWebhookMessage) (*model.Invoice, error) {
l := c.logger.Fields(logger.Fields{
"controller": "invoice",
"method": "MarkInvoiceAsPaidByBasecampWebhookMessage",
"req": invoice,
})
invoice.Status = model.InvoiceStatusPaid

c.processPaidInvoice(l, &processPaidInvoiceRequest{
Invoice: invoice,
InvoiceTodoID: msg.Recording.ID,
InvoiceBucketID: msg.Recording.Bucket.ID,
SentThankYouMail: true,
})

return invoice, nil
}

func (c *controller) processPaidInvoice(l logger.Logger, req *processPaidInvoiceRequest) {
wg := &sync.WaitGroup{}
wg.Add(3)
Expand Down
51 changes: 24 additions & 27 deletions pkg/handler/invoice/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,35 +317,32 @@ func (h *handler) Send(c *gin.Context) {
iv.Status = model.InvoiceStatusDraft
}

errsCh := make(chan error)
var amountGr = 1

go func() {
temp, rate, err := h.service.Wise.Convert(float64(iv.Total), iv.Bank.Currency.Name, "VND")
if err != nil {
errsCh <- err
return
}
am := model.NewVietnamDong(int64(temp))
iv.ConversionAmount = int64(am)
iv.ConversionRate = rate
temp, rate, err := h.service.Wise.Convert(float64(iv.Total), iv.Bank.Currency.Name, "VND")
if err != nil {
l.Error(err, "failed to convert currency")
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, err, req, ""))
return
}
am := model.NewVietnamDong(int64(temp))
iv.ConversionAmount = int64(am)
iv.ConversionRate = rate

invrs, err := h.store.Invoice.Save(h.repo.DB(), iv)
if err != nil {
l.Errorf(err, "failed to create invoice", "invoice", iv.Number)
errsCh <- err
return
}
iv.ID = invrs.ID
invrs, err := h.store.Invoice.Save(h.repo.DB(), iv)
if err != nil {
l.Errorf(err, "failed to create invoice", "invoice", iv.Number)
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, err, req, ""))
return
}
iv.ID = invrs.ID

if err := h.store.InvoiceNumberCaching.UpdateInvoiceCachingNumber(h.repo.DB(), time.Now(), iv.Project.Code); err != nil {
l.Errorf(err, "failed to update invoice caching number", "project", iv.Project.Code)
errsCh <- err
return
}
errsCh <- nil
}()
if err := h.store.InvoiceNumberCaching.UpdateInvoiceCachingNumber(h.repo.DB(), time.Now(), iv.Project.Code); err != nil {
l.Errorf(err, "failed to update invoice caching number", "project", iv.Project.Code)
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, err, req, ""))
return
}

errsCh := make(chan error)
var amountGr = 0
if !req.IsDraft {
amountGr += 2
fn := strconv.FormatInt(rand.Int63(), 10) + "_" + iv.Number + ".pdf"
Expand All @@ -370,8 +367,8 @@ func (h *handler) Send(c *gin.Context) {
errsCh <- err
return
}
iv.ThreadID = threadID

iv.ThreadID = threadID
_, err = h.store.Invoice.UpdateSelectedFieldsByID(h.repo.DB(), iv.ID.String(), *iv, "thread_id")
if err != nil {
l.Errorf(err, "failed to update invoice thread id", "thread_id", threadID)
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/webhook/basecamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (h *handler) markInvoiceAsPaid(msg *model.BasecampWebhookMessage) error {
return nil
}

if _, err := h.controller.Invoice.MarkInvoiceAsPaid(invoice, true); err != nil {
if _, err := h.controller.Invoice.MarkInvoiceAsPaidByBasecampWebhookMessage(invoice, msg); err != nil {
h.service.Basecamp.CommentResult(msg.Recording.Bucket.ID, msg.Recording.ID, h.service.Basecamp.BuildFailedComment(err.Error()))
return err
}
Expand Down

0 comments on commit c09b896

Please sign in to comment.