Skip to content

Commit

Permalink
fix: ignore throwing error when process empty emp commission
Browse files Browse the repository at this point in the history
  • Loading branch information
namnhce authored and lmquang committed Apr 12, 2023
1 parent 4ec7b76 commit 80d8cfc
Show file tree
Hide file tree
Showing 3 changed files with 28 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
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
1 change: 0 additions & 1 deletion pkg/handler/webhook/basecamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 80d8cfc

Please sign in to comment.