diff --git a/pkg/controller/invoice/commission.go b/pkg/controller/invoice/commission.go index bb3765124..a8e3d3c1a 100644 --- a/pkg/controller/invoice/commission.go +++ b/pkg/controller/invoice/commission.go @@ -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) } @@ -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()] @@ -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 { diff --git a/pkg/handler/invoice/invoice.go b/pkg/handler/invoice/invoice.go index f459d5cea..05a42151d 100644 --- a/pkg/handler/invoice/invoice.go +++ b/pkg/handler/invoice/invoice.go @@ -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" @@ -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) diff --git a/pkg/handler/webhook/basecamp.go b/pkg/handler/webhook/basecamp.go index 52c54b606..3f96f99f3 100644 --- a/pkg/handler/webhook/basecamp.go +++ b/pkg/handler/webhook/basecamp.go @@ -113,7 +113,6 @@ 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