mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
fix(BE): edit customer, finance: bank optional, nominal minus, and filter
This commit is contained in:
@@ -82,6 +82,7 @@ func (s initialService) GetOne(c *fiber.Ctx, id uint) (*entity.Payment, error) {
|
||||
}
|
||||
|
||||
func (s *initialService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.Payment, error) {
|
||||
normalizeOptionalBankId(&req.BankId)
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -124,7 +125,7 @@ func (s *initialService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
PaymentDate: time.Now(),
|
||||
PaymentMethod: string(utils.PaymentMethodSaldo),
|
||||
BankId: req.BankId,
|
||||
Direction: directionForInitialType(balanceType),
|
||||
Direction: directionForInitialType(party, balanceType),
|
||||
Nominal: signedNominal(balanceType, req.Nominal),
|
||||
Notes: req.Note,
|
||||
CreatedBy: actorID,
|
||||
@@ -164,6 +165,7 @@ func (s *initialService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
}
|
||||
|
||||
func (s initialService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Payment, error) {
|
||||
normalizeOptionalBankId(&req.BankId)
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -186,6 +188,8 @@ func (s initialService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
requiresExisting := req.PartyType != nil || req.PartyId != nil || req.InitialBalanceType != nil || req.Nominal != nil
|
||||
requiresVerification := requiresExisting || req.ReferenceNumber != nil || req.Note != nil || req.BankId != nil
|
||||
var existing *entity.Payment
|
||||
var resolvedPartyType string
|
||||
var resolvedPartyId uint
|
||||
if requiresVerification {
|
||||
current, err := s.Repository.GetByID(c.Context(), id, nil)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -199,26 +203,25 @@ func (s initialService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Initial not found")
|
||||
}
|
||||
existing = current
|
||||
resolvedPartyType = existing.PartyType
|
||||
resolvedPartyId = existing.PartyId
|
||||
}
|
||||
|
||||
if req.PartyType != nil || req.PartyId != nil {
|
||||
partyType := existing.PartyType
|
||||
partyId := existing.PartyId
|
||||
|
||||
if req.PartyType != nil {
|
||||
normalized, err := normalizePartyType(*req.PartyType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
partyType = normalized
|
||||
updateBody["party_type"] = partyType
|
||||
resolvedPartyType = normalized
|
||||
updateBody["party_type"] = resolvedPartyType
|
||||
}
|
||||
if req.PartyId != nil {
|
||||
partyId = *req.PartyId
|
||||
updateBody["party_id"] = partyId
|
||||
resolvedPartyId = *req.PartyId
|
||||
updateBody["party_id"] = resolvedPartyId
|
||||
}
|
||||
|
||||
if err := s.ensurePartyExists(c.Context(), partyType, partyId); err != nil {
|
||||
if err := s.ensurePartyExists(c.Context(), resolvedPartyType, resolvedPartyId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -238,8 +241,11 @@ func (s initialService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
nominal = *req.Nominal
|
||||
}
|
||||
|
||||
updateBody["direction"] = directionForInitialType(balanceType)
|
||||
updateBody["direction"] = directionForInitialType(resolvedPartyType, balanceType)
|
||||
updateBody["nominal"] = signedNominal(balanceType, nominal)
|
||||
} else if req.PartyType != nil {
|
||||
balanceType := balanceTypeFromPayment(existing)
|
||||
updateBody["direction"] = directionForInitialType(resolvedPartyType, balanceType)
|
||||
}
|
||||
|
||||
if len(updateBody) == 0 {
|
||||
@@ -262,7 +268,7 @@ func isInitialTransaction(transactionType string) bool {
|
||||
}
|
||||
|
||||
func balanceTypeFromPayment(payment *entity.Payment) string {
|
||||
if strings.EqualFold(payment.Direction, "OUT") || payment.Nominal < 0 {
|
||||
if payment.Nominal < 0 {
|
||||
return "NEGATIVE"
|
||||
}
|
||||
return "POSITIVE"
|
||||
@@ -286,11 +292,24 @@ func normalizeInitialBalanceType(balanceType string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func directionForInitialType(balanceType string) string {
|
||||
if strings.EqualFold(balanceType, "NEGATIVE") {
|
||||
return "OUT"
|
||||
func directionForInitialType(partyType string, balanceType string) string {
|
||||
switch utils.PaymentParty(strings.ToUpper(strings.TrimSpace(partyType))) {
|
||||
case utils.PaymentPartySupplier:
|
||||
if strings.EqualFold(balanceType, "POSITIVE") {
|
||||
return "OUT"
|
||||
}
|
||||
return "IN"
|
||||
case utils.PaymentPartyCustomer:
|
||||
if strings.EqualFold(balanceType, "NEGATIVE") {
|
||||
return "OUT"
|
||||
}
|
||||
return "IN"
|
||||
default:
|
||||
if strings.EqualFold(balanceType, "NEGATIVE") {
|
||||
return "OUT"
|
||||
}
|
||||
return "IN"
|
||||
}
|
||||
return "IN"
|
||||
}
|
||||
|
||||
func signedNominal(balanceType string, nominal float64) float64 {
|
||||
@@ -335,3 +354,12 @@ func (s initialService) ensureBankExists(ctx context.Context, bankId *uint) erro
|
||||
commonSvc.RelationCheck{Name: "Bank", ID: bankId, Exists: s.Repository.BankExists},
|
||||
)
|
||||
}
|
||||
|
||||
func normalizeOptionalBankId(bankId **uint) {
|
||||
if bankId == nil || *bankId == nil {
|
||||
return
|
||||
}
|
||||
if **bankId == 0 {
|
||||
*bankId = nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user