Skip to content

Commit

Permalink
[sprint-1][EA-3] get detail customer: add field order, paid, debt
Browse files Browse the repository at this point in the history
  • Loading branch information
Duchieuctk41 committed Jan 24, 2024
1 parent a780571 commit 7e2c2f8
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 13 deletions.
1 change: 1 addition & 0 deletions api_errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ var MapErrorCodeMessage = map[string]MessageAndStatus{
ErrNotFound: {"Status Not Found", http.StatusNotFound},
ErrDateNotBetween: {"Date Not Between", http.StatusBadRequest},
ErrWalletNameAlreadyExist: {"Wallet name already exists", http.StatusBadRequest},
ErrQuantityIsNotEnough: {"Quantity is not enough", http.StatusBadRequest},
}
13 changes: 13 additions & 0 deletions models/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ type Customer struct {
Email string `json:"email" gorm:"column:email;type:varchar(100);not null"`
}

type CustomerDetailResponse struct {
BaseModel
FullName string `json:"full_name" gorm:"column:full_name;type:varchar(100);not null"`
Gender string `json:"gender" gorm:"column:gender;type:varchar(12)"`
Age int `json:"age" gorm:"column:age;type:int"`
Address string `json:"address" gorm:"column:address;type:text"`
PhoneNumber string `json:"phone_number" gorm:"column:phone_number;type:varchar(15)"`
Email string `json:"email" gorm:"column:email;type:varchar(100);not null"`
TotalOrder int `json:"total_order"`
TotalPaid float64 `json:"total_paid"`
TotalDebt float64 `json:"total_debt"`
}

func (Customer) TableName() string {
return "customers"
}
19 changes: 19 additions & 0 deletions models/debt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package models

import (
"time"
)

type Debt struct {
BaseModel
Amount float64 `json:"amount"`
Note string `json:"note"`
StartTime *time.Time `json:"start_time"`
EndTime *time.Time `json:"end_time"`
Repeat bool `json:"repeat"`
Spent float64 `json:"spent" migration:"-"`
}

func (p *Debt) TableName() string {
return "budgets"
}
5 changes: 5 additions & 0 deletions models/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ type OrderOverview struct {
Complete int `json:"complete"`
Cancel int `json:"cancel"`
}

type CustomerDetail struct {
TotalOrder int `json:"total_order"`
TotalPaid float64 `json:"total_paid"`
}
8 changes: 8 additions & 0 deletions repository/cashbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type CashbookRepository interface {
GetListDebt(ctx context.Context, req erpdto.ListCashbookRequest) (res []*models.Cashbook, total int64, err error)
GetTotalTransactionByCategoryIdAndTime(ctx context.Context, categoryId uuid.UUID, startTime, endTime *time.Time) (total float64, err error)
GetListTotalTransactionByCategoryIdAndTime(ctx context.Context, categoryId uuid.UUID, startTime, endTime *time.Time) (output []*erpdto.TotalTransactionByCategoryResponse, err error)
GetTotalDebtByCustomerID(ctx context.Context, customerId uuid.UUID) (total float64, err error)
}

type transactionRepo struct {
Expand Down Expand Up @@ -149,3 +150,10 @@ func (r *transactionRepo) GetListTotalTransactionByCategoryIdAndTime(ctx context
err = query.Find(&output).Error
return output, err
}

func (r *transactionRepo) GetTotalDebtByCustomerID(ctx context.Context, customerId uuid.UUID) (total float64, err error) {
total = float64(0)
query := r.db.Model(&models.Cashbook{}).Where("customer_id = ? and is_pay = false", customerId)
err = query.Pluck("coalesce(sum(amount),0)", &total).Error
return total, err
}
9 changes: 9 additions & 0 deletions repository/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type OrderRepo interface {
GetList(ctx context.Context, req erpdto.GetListOrderRequest) (res []*models.Order, total int64, err error)
GetOverview(ctx context.Context, req erpdto.GetListOrderRequest) (res []*models.OrderOverview, err error)
GetBestSeller(ctx context.Context, req erpdto.GetListOrderRequest) (res []*models.ProductBestSellerResponse, err error)
GetDetailCustomer(ctx context.Context, customerId string) (res *models.CustomerDetail, err error)
}

type orderRepo struct {
Expand Down Expand Up @@ -108,3 +109,11 @@ func (r *orderRepo) GetBestSeller(ctx context.Context, req erpdto.GetListOrderRe
Limit(10).Group("products.id").Find(&res).Error
return res, err
}

func (r *orderRepo) GetDetailCustomer(ctx context.Context, customerId string) (res *models.CustomerDetail, err error) {
queryString := `SELECT count(id) as total_order, coalesce(sum(payment), 0) as total_paid
FROM orders WHERE customer_id = ?`

err = r.db.Debug().Raw(queryString, customerId).Find(&res).Error
return res, err
}
50 changes: 37 additions & 13 deletions service/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,77 +13,101 @@ import (

type ERPCustomerService interface {
ListCustomer(ctx context.Context, req erpdto.ListCustomerRequest) ([]*models.Customer, *int64, error)
GetOneById(ctx context.Context, id string) (*models.Customer, error)
GetOneById(ctx context.Context, id string) (*models.CustomerDetailResponse, error)
CreateCustomer(ctx context.Context, req erpdto.CreateCustomerRequest) (*models.Customer, error)
UpdateCustomer(ctx context.Context, req erpdto.UpdateCustomerRequest) (*models.Customer, error)
DeleteCustomer(ctx context.Context, customerId string) error
}

type erpCustomerService struct {
erpCustomerRepo repository.ERPCustomerRepository
orderRepo repository.OrderRepo
cashbookRepo repository.CashbookRepository
db *infrastructure.Database
logger *zap.Logger
}

func NewCustomerService(erpCustomerRepo repository.ERPCustomerRepository, db *infrastructure.Database, logger *zap.Logger) ERPCustomerService {
func NewCustomerService(erpCustomerRepo repository.ERPCustomerRepository, db *infrastructure.Database, logger *zap.Logger, orderRepo repository.OrderRepo, cashbookRepo repository.CashbookRepository) ERPCustomerService {
return &erpCustomerService{
erpCustomerRepo: erpCustomerRepo,
orderRepo: orderRepo,
cashbookRepo: cashbookRepo,
db: db,
logger: logger,
}
}

func (p *erpCustomerService) ListCustomer(ctx context.Context, req erpdto.ListCustomerRequest) ([]*models.Customer, *int64, error) {
customers, total, err := p.erpCustomerRepo.List(ctx, req)
func (s *erpCustomerService) ListCustomer(ctx context.Context, req erpdto.ListCustomerRequest) ([]*models.Customer, *int64, error) {
customers, total, err := s.erpCustomerRepo.List(ctx, req)
if err != nil {
return nil, nil, err
}

return customers, total, nil
}

func (p *erpCustomerService) GetOneById(ctx context.Context, id string) (*models.Customer, error) {
customers, err := p.erpCustomerRepo.FindOneByID(ctx, id)
func (s *erpCustomerService) GetOneById(ctx context.Context, id string) (*models.CustomerDetailResponse, error) {
customer, err := s.erpCustomerRepo.FindOneByID(ctx, id)
if err != nil {
return nil, err
}

return customers, nil
orderDetail, err := s.orderRepo.GetDetailCustomer(ctx, customer.ID.String())
if err != nil {
return nil, err
}

totalDebt, err := s.cashbookRepo.GetTotalDebtByCustomerID(ctx, customer.ID)
if err != nil {
return nil, err
}

output := &models.CustomerDetailResponse{}

if err = copier.Copy(&output, &customer); err != nil {
log.Println("Copy struct failed!")
return nil, err
}
output.TotalOrder = orderDetail.TotalOrder
output.TotalDebt = totalDebt
output.TotalPaid = orderDetail.TotalPaid

return output, nil
}

func (p *erpCustomerService) CreateCustomer(ctx context.Context, req erpdto.CreateCustomerRequest) (*models.Customer, error) {
func (s *erpCustomerService) CreateCustomer(ctx context.Context, req erpdto.CreateCustomerRequest) (*models.Customer, error) {
customer := &models.Customer{}

if err := copier.Copy(&customer, &req); err != nil {
log.Println("Copy struct failed!")
return nil, err
}

customer, err := p.erpCustomerRepo.Create(ctx, customer)
customer, err := s.erpCustomerRepo.Create(ctx, customer)
if err != nil {
return nil, err
}

return customer, nil
}

func (p *erpCustomerService) UpdateCustomer(ctx context.Context, req erpdto.UpdateCustomerRequest) (*models.Customer, error) {
func (s *erpCustomerService) UpdateCustomer(ctx context.Context, req erpdto.UpdateCustomerRequest) (*models.Customer, error) {
customer := &models.Customer{}

if err := copier.Copy(&customer, &req); err != nil {
log.Println("Copy struct failed!")
return nil, err
}

_, err := p.erpCustomerRepo.Update(ctx, customer)
_, err := s.erpCustomerRepo.Update(ctx, customer)
if err != nil {
return nil, err
}

return customer, nil
}

func (p *erpCustomerService) DeleteCustomer(ctx context.Context, customerId string) error {
err := p.erpCustomerRepo.Delete(ctx, customerId)
func (s *erpCustomerService) DeleteCustomer(ctx context.Context, customerId string) error {
err := s.erpCustomerRepo.Delete(ctx, customerId)
return err
}

0 comments on commit 7e2c2f8

Please sign in to comment.