Skip to content

Commit

Permalink
Merge pull request #142 from kimthu09/mai
Browse files Browse the repository at this point in the history
Fix create invoice
  • Loading branch information
kimthu09 authored Jan 13, 2024
2 parents e1b329f + e2b510d commit 2d04cd1
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 72 deletions.
13 changes: 7 additions & 6 deletions backend/common/round_handler.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package common

import "math"
import (
"math"
)

const maxDecimalNumber = 2
const maxDecimalNumber = 3

func roundToDecimal(num float32, decimalPlaces int) float32 {
func RoundToDecimal(num float32, decimalPlaces int) float32 {
shift := math.Pow10(decimalPlaces)
return float32(math.Round(float64(num)*shift)) / float32(shift)
}

func CustomRound(num *float32) {
roundedNum := roundToDecimal(*num, maxDecimalNumber)
*num = roundedNum
func RoundToInt(num float32) int {
return int(RoundToDecimal(num, 0))
}
44 changes: 36 additions & 8 deletions backend/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5038,6 +5038,22 @@ const docTemplate = `{
}
}
},
"invoicedetailmodel.InvoiceDetail": {
"type": "object",
"properties": {
"book": {
"$ref": "#/definitions/bookmodel.SimpleBook"
},
"qty": {
"type": "integer",
"example": 2
},
"unitPrice": {
"type": "integer",
"example": 60000
}
}
},
"invoicedetailmodel.ReqCreateInvoiceDetail": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -5073,6 +5089,14 @@ const docTemplate = `{
"invoicemodel.Invoice": {
"type": "object",
"properties": {
"amountPriceUsePoint": {
"type": "integer",
"example": 20000
},
"amountReceived": {
"type": "integer",
"example": 100000
},
"createdAt": {
"type": "string",
"example": "2023-12-03T15:02:19.62113565Z"
Expand All @@ -5083,6 +5107,12 @@ const docTemplate = `{
"customer": {
"$ref": "#/definitions/invoicemodel.SimpleCustomer"
},
"details": {
"type": "array",
"items": {
"$ref": "#/definitions/invoicedetailmodel.InvoiceDetail"
}
},
"id": {
"type": "string",
"example": "123"
Expand All @@ -5095,14 +5125,6 @@ const docTemplate = `{
"type": "integer",
"example": 20000
},
"qtyPriceUsePoint": {
"type": "integer",
"example": 20000
},
"qtyReceived": {
"type": "integer",
"example": 100000
},
"totalPrice": {
"type": "integer",
"example": 120000
Expand All @@ -5112,11 +5134,17 @@ const docTemplate = `{
"invoicemodel.ReqCreateInvoice": {
"type": "object",
"properties": {
"customerId": {
"type": "string"
},
"details": {
"type": "array",
"items": {
"$ref": "#/definitions/invoicedetailmodel.ReqCreateInvoiceDetail"
}
},
"isUsePoint": {
"type": "boolean"
}
}
},
Expand Down
44 changes: 36 additions & 8 deletions backend/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -5032,6 +5032,22 @@
}
}
},
"invoicedetailmodel.InvoiceDetail": {
"type": "object",
"properties": {
"book": {
"$ref": "#/definitions/bookmodel.SimpleBook"
},
"qty": {
"type": "integer",
"example": 2
},
"unitPrice": {
"type": "integer",
"example": 60000
}
}
},
"invoicedetailmodel.ReqCreateInvoiceDetail": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -5067,6 +5083,14 @@
"invoicemodel.Invoice": {
"type": "object",
"properties": {
"amountPriceUsePoint": {
"type": "integer",
"example": 20000
},
"amountReceived": {
"type": "integer",
"example": 100000
},
"createdAt": {
"type": "string",
"example": "2023-12-03T15:02:19.62113565Z"
Expand All @@ -5077,6 +5101,12 @@
"customer": {
"$ref": "#/definitions/invoicemodel.SimpleCustomer"
},
"details": {
"type": "array",
"items": {
"$ref": "#/definitions/invoicedetailmodel.InvoiceDetail"
}
},
"id": {
"type": "string",
"example": "123"
Expand All @@ -5089,14 +5119,6 @@
"type": "integer",
"example": 20000
},
"qtyPriceUsePoint": {
"type": "integer",
"example": 20000
},
"qtyReceived": {
"type": "integer",
"example": 100000
},
"totalPrice": {
"type": "integer",
"example": 120000
Expand All @@ -5106,11 +5128,17 @@
"invoicemodel.ReqCreateInvoice": {
"type": "object",
"properties": {
"customerId": {
"type": "string"
},
"details": {
"type": "array",
"items": {
"$ref": "#/definitions/invoicedetailmodel.ReqCreateInvoiceDetail"
}
},
"isUsePoint": {
"type": "boolean"
}
}
},
Expand Down
31 changes: 25 additions & 6 deletions backend/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,17 @@ definitions:
description: Data contains the detailed information about inventory check
note details.
type: object
invoicedetailmodel.InvoiceDetail:
properties:
book:
$ref: '#/definitions/bookmodel.SimpleBook'
qty:
example: 2
type: integer
unitPrice:
example: 60000
type: integer
type: object
invoicedetailmodel.ReqCreateInvoiceDetail:
properties:
bookId:
Expand All @@ -1023,13 +1034,23 @@ definitions:
type: object
invoicemodel.Invoice:
properties:
amountPriceUsePoint:
example: 20000
type: integer
amountReceived:
example: 100000
type: integer
createdAt:
example: "2023-12-03T15:02:19.62113565Z"
type: string
createdBy:
$ref: '#/definitions/usermodel.SimpleUser'
customer:
$ref: '#/definitions/invoicemodel.SimpleCustomer'
details:
items:
$ref: '#/definitions/invoicedetailmodel.InvoiceDetail'
type: array
id:
example: "123"
type: string
Expand All @@ -1039,22 +1060,20 @@ definitions:
pointUse:
example: 20000
type: integer
qtyPriceUsePoint:
example: 20000
type: integer
qtyReceived:
example: 100000
type: integer
totalPrice:
example: 120000
type: integer
type: object
invoicemodel.ReqCreateInvoice:
properties:
customerId:
type: string
details:
items:
$ref: '#/definitions/invoicedetailmodel.ReqCreateInvoiceDetail'
type: array
isUsePoint:
type: boolean
type: object
invoicemodel.ResCreateInvoice:
properties:
Expand Down
2 changes: 1 addition & 1 deletion backend/module/dashboard/dashboardrepo/see_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (repo *seeDashboardRepo) SeeDashboard(
for _, invoice := range allInvoices {
chartSale := dashboardmodel.ChartComponent{
Time: *invoice.CreatedAt,
Value: invoice.QuantityReceived - invoice.TotalImportPrice,
Value: invoice.AmountReceived - invoice.TotalImportPrice,
}
listProfit = append(listProfit, chartSale)

Expand Down
59 changes: 59 additions & 0 deletions backend/module/invoice/invoicebiz/create_invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"book-store-management-backend/common"
"book-store-management-backend/component/generator"
"book-store-management-backend/middleware"
"book-store-management-backend/module/customer/customermodel"
"book-store-management-backend/module/invoice/invoicemodel"
"book-store-management-backend/module/shopgeneral/shopgeneralmodel"
"context"
Expand All @@ -17,6 +18,15 @@ type CreateInvoiceRepo interface {
ctx context.Context,
data *invoicemodel.ReqCreateInvoice,
) error
FindCustomer(
ctx context.Context,
customerId string,
) (*customermodel.Customer, error)
UpdateCustomerPoint(
ctx context.Context,
customerId string,
data customermodel.CustomerUpdatePoint,
) error
HandleInvoice(
ctx context.Context,
data *invoicemodel.ReqCreateInvoice,
Expand Down Expand Up @@ -59,6 +69,55 @@ func (biz *createInvoiceBiz) CreateInvoice(
return err
}

general, errGetShopGeneral := biz.repo.GetShopGeneral(ctx)
if errGetShopGeneral != nil {
return errGetShopGeneral
}
if data.CustomerId != "" {
customer, errGetCustomer := biz.repo.FindCustomer(ctx, data.CustomerId)
if errGetCustomer != nil {
return errGetCustomer
}

priceUseForPoint := float32(0)
pointUse := 0
if data.IsUsePoint {
if float32(data.TotalPrice) >= float32(customer.Point)*general.UsePointPercent {
pointUse = customer.Point
priceUseForPoint = float32(customer.Point) * general.UsePointPercent
} else {
pointUse = common.RoundToInt(float32(data.TotalPrice) / general.UsePointPercent)
priceUseForPoint = float32(data.TotalPrice)
}
}

priceUseForPointInt := common.RoundToInt(priceUseForPoint)
data.AmountReceived = data.TotalPrice - priceUseForPointInt
data.AmountPriceUseForPoint = priceUseForPointInt

pointReceive := common.RoundToInt(float32(data.AmountReceived) * general.AccumulatePointPercent)

data.PointUse = pointUse
data.PointReceive = pointReceive

amountPointNeedUpdate :=
pointReceive - pointUse

customerUpdatePoint := customermodel.CustomerUpdatePoint{
Amount: &amountPointNeedUpdate,
}
if err := biz.repo.UpdateCustomerPoint(
ctx, data.CustomerId, customerUpdatePoint); err != nil {
return err
}

} else {
data.AmountReceived = data.TotalPrice
if data.IsUsePoint {
return invoicemodel.ErrInvoiceNotHaveCustomerToUsePoint
}
}

if err := biz.repo.HandleInvoice(ctx, data); err != nil {
return err
}
Expand Down
31 changes: 18 additions & 13 deletions backend/module/invoice/invoicemodel/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (
)

type Invoice struct {
Id string `json:"id" gorm:"column:id;" example:"123"`
CustomerId string `json:"-" gorm:"column:customerId;"`
Customer *SimpleCustomer `json:"customer" gorm:"foreignKey:CustomerId;references:Id"`
TotalPrice int `json:"totalPrice" gorm:"column:totalPrice;" example:"120000"`
TotalImportPrice int `json:"-" gorm:"column:totalImportPrice;" example:"100000"`
QuantityReceived int `json:"qtyReceived" gorm:"column:qtyReceived" example:"100000"`
QuantityPriceUseForPoint int `json:"qtyPriceUsePoint" gorm:"column:qtyPriceUsePoint" example:"20000"`
PointUse int `json:"pointUse" gorm:"column:pointUse;" example:"20000"`
PointReceive int `json:"pointReceive" gorm:"column:pointReceive;" example:"10000"`
CreatedBy string `json:"-" gorm:"column:createdBy;" example:"admin"`
CreatedByUser usermodel.SimpleUser `json:"createdBy" gorm:"foreignKey:CreatedBy"`
CreatedAt *time.Time `json:"createdAt" gorm:"column:createdAt;" example:"2023-12-03T15:02:19.62113565Z"`
Details []invoicedetailmodel.InvoiceDetail `json:"details"`
Id string `json:"id" gorm:"column:id;" example:"123"`
CustomerId string `json:"-" gorm:"column:customerId;"`
Customer *SimpleCustomer `json:"customer" gorm:"foreignKey:CustomerId;references:Id"`
TotalPrice int `json:"totalPrice" gorm:"column:totalPrice;" example:"120000"`
TotalImportPrice int `json:"-" gorm:"column:totalImportPrice;" example:"100000"`
AmountReceived int `json:"amountReceived" gorm:"column:amountReceived" example:"100000"`
AmountPriceUsePoint int `json:"amountPriceUsePoint" gorm:"column:amountPriceUsePoint" example:"20000"`
PointUse int `json:"pointUse" gorm:"column:pointUse;" example:"20000"`
PointReceive int `json:"pointReceive" gorm:"column:pointReceive;" example:"10000"`
CreatedBy string `json:"-" gorm:"column:createdBy;" example:"admin"`
CreatedByUser usermodel.SimpleUser `json:"createdBy" gorm:"foreignKey:CreatedBy"`
CreatedAt *time.Time `json:"createdAt" gorm:"column:createdAt;" example:"2023-12-03T15:02:19.62113565Z"`
Details []invoicedetailmodel.InvoiceDetail `json:"details"`
}

func (*Invoice) TableName() string {
Expand All @@ -34,6 +34,11 @@ var (
"Danh sách sản phẩm cần thanh toán đang trống",
"ErrInvoiceDetailsEmpty",
)
ErrInvoiceNotHaveCustomerToUsePoint = common.NewCustomError(
errors.New("customer is required for this invoice"),
"Khách hàng bắt buộc phải có cho hóa đơn này",
"ErrInvoiceNotHaveCustomerToUsePoint",
)
ErrInvoiceBookIsNotEnough = common.NewCustomError(
errors.New("exist book in the stock is not enough for the invoice"),
"Tồn tại 1 sách có số lượng trong kho không đủ để bán",
Expand Down
Loading

0 comments on commit 2d04cd1

Please sign in to comment.