jiangshuai
2024-02-06 02a2176f7c5733a4e4c4429c2028bbb86a967ce7
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package purchase_wms
 
import (
    "context"
    "errors"
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "srm/global"
    "srm/model/purchase"
    "srm/model/test"
    "time"
)
 
type Server struct {
    UnimplementedPurchaseServiceServer
}
 
func (s *Server) UpdatePurchaseStatus(ctx context.Context, req *UpdatePurchaseStatusRequest) (*UpdatePurchaseStatusResponse, error) {
    if req.Number == "" {
        return nil, errors.New("采购编号不能为空")
    }
    err := global.GVA_DB.Model(&purchase.Purchase{}).Where("number = ?", req.Number).
        Updates(map[string]interface{}{"status": purchase.OrderStatusStored}).Error
    return new(UpdatePurchaseStatusResponse), err
}
 
func (s *Server) GetSupplierListByProductId(ctx context.Context, req *GetSupplierListByProductIdRequest) (*GetSupplierListByProductIdResponse, error) {
    if req.ProductId == "" {
        return nil, errors.New("产品编号不能为空")
    }
    var products []test.SupplierMaterial
    err := global.GVA_DB.Model(&test.SupplierMaterial{}).Where("number = ?", req.ProductId).Preload("Supplier").Find(&products).Error
    if err != nil {
        return nil, err
    }
    list := make([]*SupplierList, 0)
    for _, product := range products {
        if product.Supplier.Status != 1 {
            continue
        }
        var sl SupplierList
        sl.SupplierId = int64(product.SupplierId)
        sl.SupplierName = product.Supplier.Name
        sl.PurchasePrice = float32(product.PurchasePrice)
        list = append(list, &sl)
    }
    resp := new(GetSupplierListByProductIdResponse)
    resp.List = list
    return resp, nil
}
 
func (s *Server) CreatePurchaseByWms(ctx context.Context, req *CreatePurchaseByWmsRequest) (*CreatePurchaseByWmsResponse, error) {
    if req.ProductId == "" {
        return nil, errors.New("产品id为空")
    }
    var product test.SupplierMaterial
    err := global.GVA_DB.Model(&test.SupplierMaterial{}).Where("number = ?", req.ProductId).First(&product).Error
    if err != nil {
        if err == gorm.ErrRecordNotFound {
            var material test.Material
            err = global.GVA_DB.Model(&test.Material{}).Where("id = ?", req.ProductId).First(&material).Error
            if err != nil {
                return nil, err
            }
            product.Name = material.Name
            product.Number = material.ID
            product.Unit = material.Unit
            product.PurchasePrice = material.PurchasePrice.InexactFloat64()
            product.Specifications = material.Specs
            product.ModelNumber = material.Type
            product.Name = material.Name
            product.Name = material.Name
            err = global.GVA_DB.Create(&product).Error
            if err != nil {
                return nil, err
            }
        } else {
            return nil, err
        }
    }
 
    //采购单
    var purchaseRecord purchase.Purchase
 
    //purchaseRecord.SupplierId = int(req.SupplierId)
    if req.Source == "WMS" {
        purchaseRecord.OrderSource = "WMS推送"
        purchaseRecord.Name = "WMS补货"
    } else if req.Source == "APS" {
        purchaseRecord.OrderSource = "APS推送"
        purchaseRecord.Name = "APS采购"
    }
 
    purchaseRecord.ID = 0
    purchaseRecord.Status = purchase.OrderStatusConfirmed
    purchaseRecord.HandledBy = "admin"
    purchaseRecord.Creator = "admin"
    purchaseRecord.Number = fmt.Sprintf("CG%v", time.Now().Unix())
    purchaseRecord.Principal = "admin"
    purchaseRecord.OrderType = "采购订单"
    purchaseRecord.Quantity = decimal.NewFromInt(req.Amount)
    purchaseRecord.TotalPrice = purchaseRecord.Quantity.Mul(decimal.NewFromFloat(product.PurchasePrice))
    purchaseRecord.UnInvoiceAmount = purchaseRecord.TotalPrice
    purchaseRecord.ShouldPayAmount = purchaseRecord.TotalPrice
 
    //产品
    var pp purchase.PurchaseProducts
    pp.ProductId = int(product.ID)
    pp.Amount = purchaseRecord.Quantity
    pp.Price = decimal.NewFromFloat(product.PurchasePrice)
    pp.Total = purchaseRecord.TotalPrice
    if req.Source == "WMS" {
        pp.Remark = "WMS补货"
    } else if req.Source == "APS" {
        pp.Remark = "APS采购"
    }
 
    err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
        err = tx.Create(&purchaseRecord).Error
        if err != nil {
            return err
        }
        pp.PurchaseId = int(purchaseRecord.ID)
        return tx.Create(&pp).Error
    })
    if err != nil {
        return nil, err
    }
    resp := new(CreatePurchaseByWmsResponse)
    resp.PurchaseNumber = purchaseRecord.Number
    return resp, nil
}
 
func (s *Server) GetPurchaseInfo(ctx context.Context, req *GetPurchaseInfoRequest) (*GetPurchaseInfoResponse, error) {
    if len(req.PurchaseNumbers) == 0 {
        return nil, errors.New("采购单编码不能为空")
    }
    var ps []purchase.Purchase
    err := global.GVA_DB.Model(&purchase.Purchase{}).Where("number in (?)", req.PurchaseNumbers).Preload("Supplier").Find(&ps).Error
    if err != nil {
        return nil, err
    }
    infos := make([]*PurchaseInfo, 0)
    for _, p := range ps {
        var info PurchaseInfo
        info.PurchaseNumber = p.Number
        info.PurchaseName = p.Name
        info.SupplierName = p.Supplier.Name
        info.Amount = p.Quantity.IntPart()
        info.Status = int64(p.Status)
        infos = append(infos, &info)
    }
    resp := new(GetPurchaseInfoResponse)
    resp.Infos = infos
    return resp, nil
}
 
func (s *Server) ExistSupplier(ctx context.Context, req *ExistSupplierRequest) (*ExistSupplierResponse, error) {
    resp := new(ExistSupplierResponse)
    if len(req.ProductId) == 0 {
        resp.Exist = false
        return resp, nil
    }
    var products []test.SupplierMaterial
    err := global.GVA_DB.Model(&test.SupplierMaterial{}).Where("number in (?)", req.ProductId).Find(&products).Error
    if err != nil {
        return nil, err
    }
    for _, number := range req.ProductId {
        exit := false
        for _, product := range products {
            if number == product.Number && product.SupplierId > 0 {
                exit = true
                break
            }
        }
        if !exit {
            resp.Exist = exit
            return resp, nil
        }
    }
    resp.Exist = true
    return resp, nil
}