liujiandao
2024-03-07 63558d60419edd772f8f8c8b413326d4550783f0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package purchase
 
import (
    "github.com/shopspring/decimal"
    "srm/global"
    "srm/model/test"
)
 
type Purchase struct {
    global.GVA_MODEL_INT
    PurchaseTypeId      int                 `json:"purchaseTypeId,string" form:"purchaseType" gorm:"type:int(11);not null;default 0;comment:采购类型id"` // 采购类型id
    PurchaseType        PurchaseType        `json:"purchaseType" gorm:"foreignKey:PurchaseTypeId"`
    OrderSource         string              `json:"orderSource" gorm:"type:varchar(255);not null;default '';comment:单据来源"`                    // 单据来源
    SupplierId          int                 `json:"supplierId,string" form:"supplierId" gorm:"type:int(11);not null;default 0;comment:供应商id"` // 供应商id
    Supplier            test.Supplier       `json:"supplier" gorm:"foreignKey:SupplierId"`
    Number              string              `json:"number" form:"number" gorm:"unique;type:varchar(255);not null;default '';comment:采购编号"`                  // 采购编号
    Name                string              `json:"name" form:"name" gorm:"type:varchar(255);not null;default '';comment:采购名称"`                             // 采购名称
    Contact             string              `json:"contact" form:"contact" gorm:"type:varchar(255);not null;default '';comment:联系人"`                        // 联系人
    Phone               string              `json:"phone" form:"phone" gorm:"type:varchar(255);not null;default '';comment:联系人电话"`                          // 联系人电话
    SigningDate         string              `json:"signingDate" form:"signingDate" gorm:"type:varchar(255);not null;default '';comment:签约日期"`               // 签约日期
    DeliveryDate        string              `json:"deliveryDate" form:"deliveryDate" gorm:"type:varchar(255);not null;default '';comment:交付日期"`             //交付日期
    Remark              string              `json:"remark" form:"remark" gorm:"type:varchar(1000);not null;default '';comment:备注"`                          //备注
    Status              OrderStatus         `json:"status" form:"status" gorm:"type:tinyint(1);not null;default 0;comment:状态"`                              //状态
    HandledBy           string              `json:"handledBy" form:"handledBy" gorm:"type:varchar(255);not null;default '';comment:经办人"`                    //经办人
    Creator             string              `json:"creator" form:"creator" gorm:"type:varchar(255);not null;default '';comment:制单人"`                        //制单人
    OrderType           string              `json:"orderType" form:"orderType" gorm:"type:varchar(255);not null;default '';comment:单据类型"`                   //单据类型
    Warehouse           string              `json:"warehouse" form:"warehouse" gorm:"type:varchar(255);not null;default '';comment:收货仓库"`                   //收货仓库
    WarehouseAddress    string              `json:"warehouseAddress" form:"warehouseAddress" gorm:"type:varchar(512);default '';comment:收货仓库地址"`            //收货仓库地址
    Principal           string              `json:"principal" form:"principal" gorm:"type:varchar(255);not null;default '';comment:仓库负责人"`                  //仓库负责人
    SourceOrder         string              `json:"sourceOrder" gorm:"type:varchar(255);comment:来源单据"`                                                      //来源单据
    Quantity            decimal.Decimal     `json:"quantity" form:"quantity" gorm:"type:decimal(12,4);not null;comment:采购数量"`                               // 采购数量
    TotalPrice          decimal.Decimal     `json:"totalPrice" form:"totalPrice" gorm:"type:decimal(12,2);not null;default '';comment:价税合计"`                //价税合计
    WholeDiscountType   WholeDiscountType   `json:"wholeDiscountType" form:"wholeDiscountType" gorm:"type:tinyint(1);not null;default '';comment:整单折扣"`     //整单折扣类型
    WholeDiscount       decimal.Decimal     `json:"wholeDiscount" form:"wholeDiscount" gorm:"type:decimal(12,2);not null;default '';comment:整单折扣"`          //整单折扣值
    PriceAdjustmentType PriceAdjustmentType `json:"priceAdjustmentType" form:"priceAdjustmentType" gorm:"type:tinyint(1);not null;default '';comment:价格调整"` //价格调整类型
    PriceAdjustment     decimal.Decimal     `json:"priceAdjustment" form:"priceAdjustment" gorm:"type:decimal(12,2);not null;default '';comment:价格调整"`      //价格调整值
    RealTotalPrice      decimal.Decimal     `json:"realTotalPrice" form:"realTotalPrice" gorm:"type:decimal(12,2);not null;default '';comment:最终价格"`        //最终价格
    InvoiceAmount       decimal.Decimal     `json:"invoiceAmount" form:"invoiceAmount" gorm:"type:decimal(12,2);not null;default '';comment:已收票金额"`         //已收票金额
    UnInvoiceAmount     decimal.Decimal     `json:"unInvoiceAmount" form:"unInvoiceAmount" gorm:"type:decimal(12,2);not null;default '';comment:未收票金额"`     //未收票金额
    ShouldPayAmount     decimal.Decimal     `json:"shouldPayAmount" form:"shouldPayAmount" gorm:"type:decimal(12,2);not null;default '';comment:应付金额"`      //应付金额
    PaidAmount          decimal.Decimal     `json:"paidAmount" form:"paidAmount" gorm:"type:decimal(12,2);not null;default '';comment:已付金额"`                //已付金额
}
 
func (Purchase) TableName() string {
    return "srm_purchase"
}
 
type OrderStatus int
 
const (
    OrderStatusConfirmed OrderStatus = 1 //待确认
    OrderStatusReceived  OrderStatus = 2 //待入库
    OrderStatusStored    OrderStatus = 3 //已入库
    OrderStatusCompleted OrderStatus = 4 //已完成
    OrderStatusCanceled  OrderStatus = 5 //已取消
)
 
type WholeDiscountType int
 
const (
    WholeDiscountTypePercent  WholeDiscountType = 1 //百分比降价
    WholeDiscountTypeDiscount WholeDiscountType = 2 //直接降价
)
 
func (wdt WholeDiscountType) IsValid(totalPrice, value decimal.Decimal) bool {
    if wdt == 0 {
        return true
    }
    if wdt != WholeDiscountTypePercent && wdt != WholeDiscountTypeDiscount {
        return false
    }
    if wdt == WholeDiscountTypeDiscount && value.GreaterThan(totalPrice) {
        return false
    }
    if wdt == WholeDiscountTypePercent && totalPrice.Mul(value).Div(decimal.NewFromInt(100)).GreaterThan(totalPrice) {
        return false
    }
    return true
}
 
type PriceAdjustmentType int
 
const (
    PriceAdjustmentTypeAdd PriceAdjustmentType = 1 //增加
    PriceAdjustmentTypeSub PriceAdjustmentType = 2 //减少
)
 
func (pat PriceAdjustmentType) IsValid(totalPrice, value decimal.Decimal) bool {
    if pat == 0 {
        return true
    }
    if pat != PriceAdjustmentTypeAdd && pat != PriceAdjustmentTypeSub {
        return false
    }
    if pat == PriceAdjustmentTypeSub && value.GreaterThan(totalPrice) {
        return false
    }
    return true
}
 
func (slf Purchase) CalcRealTotalPrice() decimal.Decimal {
    totalPrice := slf.TotalPrice
    if slf.WholeDiscountType == WholeDiscountTypePercent {
        totalPrice = totalPrice.Mul(decimal.NewFromInt(1).Sub(slf.WholeDiscount.Div(decimal.NewFromInt(100))))
    } else if slf.WholeDiscountType == WholeDiscountTypeDiscount {
        totalPrice = totalPrice.Sub(slf.WholeDiscount)
    }
 
    if slf.PriceAdjustmentType == PriceAdjustmentTypeAdd {
        totalPrice = totalPrice.Add(slf.PriceAdjustment)
    } else if slf.PriceAdjustmentType == PriceAdjustmentTypeSub {
        totalPrice = totalPrice.Sub(slf.PriceAdjustment)
    }
    return totalPrice.RoundCeil(2)
}