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("采购编号不能为空")
|
}
|
m := make(map[string]interface{})
|
m["status"] = purchase.OrderStatusStored
|
if req.Status > 0 {
|
m["status"] = req.Status
|
}
|
err := global.GVA_DB.Model(&purchase.Purchase{}).Where("number = ?", req.Number).Updates(m).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).Order("id desc").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
|
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
|
}
|
purchaseIds := make([]uint, 0)
|
for _, p := range ps {
|
purchaseIds = append(purchaseIds, p.ID)
|
}
|
if len(purchaseIds) == 0 {
|
return nil, errors.New("没有查到采购单")
|
}
|
pps := make([]*purchase.PurchaseProducts, 0)
|
err = global.GVA_DB.Model(&purchase.PurchaseProducts{}).Where("purchase_id in (?)", purchaseIds).Preload("Product").Find(&pps).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.Status = int64(p.Status)
|
if p.Status == purchase.OrderStatusStored || p.Status == purchase.OrderStatusCompleted {
|
info.FinishAmount = info.Amount
|
}
|
for _, pp := range pps {
|
if int(p.ID) == pp.PurchaseId {
|
ni := info
|
ni.ProductId = pp.Product.Number
|
ni.ProductName = pp.Product.Name
|
ni.Specs = pp.Product.Specifications
|
ni.Unit = pp.Product.Unit
|
ni.Amount = pp.Amount.IntPart()
|
infos = append(infos, &ni)
|
}
|
}
|
}
|
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
|
}
|
|
func (s *Server) CreatePurchaseByAps(ctx context.Context, req *CreatePurchaseByApsRequest) (*CreatePurchaseByWmsResponse, error) {
|
if len(req.Req) == 0 {
|
return nil, errors.New("产品id为空")
|
}
|
productIds := make([]string, 0)
|
amount := int64(0)
|
numMap := make(map[string]int64)
|
for _, request := range req.Req {
|
productIds = append(productIds, request.ProductId)
|
numMap[request.ProductId] = request.Amount
|
amount += request.Amount
|
}
|
var products []test.SupplierMaterial
|
var newProducts []test.SupplierMaterial
|
err := global.GVA_DB.Model(&test.SupplierMaterial{}).Where("number in (?)", productIds).Order("id desc").Find(&products).Error
|
if err != nil {
|
return nil, err
|
}
|
for _, id := range productIds {
|
flag := false
|
for _, product := range products {
|
if id == product.Number {
|
newProducts = append(newProducts, product)
|
flag = true
|
break
|
}
|
}
|
if !flag {
|
var material test.Material
|
var product test.SupplierMaterial
|
err = global.GVA_DB.Model(&test.Material{}).Where("id = ?", id).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
|
err = global.GVA_DB.Create(&product).Error
|
if err != nil {
|
return nil, err
|
}
|
newProducts = append(newProducts, product)
|
}
|
}
|
|
//采购单
|
var purchaseRecord purchase.Purchase
|
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(amount)
|
purchaseRecord.SourceOrder = req.SourceOrder
|
|
price := float64(0)
|
var pps []purchase.PurchaseProducts
|
for _, product := range newProducts {
|
//产品
|
var pp purchase.PurchaseProducts
|
pp.ProductId = int(product.ID)
|
pp.Amount = decimal.NewFromInt(numMap[product.Number])
|
pp.Price = decimal.NewFromFloat(product.PurchasePrice)
|
price += product.PurchasePrice
|
pp.Total = pp.Amount.Mul(pp.Price)
|
pp.Remark = "APS采购"
|
pps = append(pps, pp)
|
}
|
purchaseRecord.TotalPrice = purchaseRecord.Quantity.Mul(decimal.NewFromFloat(price))
|
purchaseRecord.UnInvoiceAmount = purchaseRecord.TotalPrice
|
purchaseRecord.ShouldPayAmount = purchaseRecord.TotalPrice
|
|
err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
|
err = tx.Create(&purchaseRecord).Error
|
if err != nil {
|
return err
|
}
|
for i := 0; i < len(pps); i++ {
|
pps[i].PurchaseId = int(purchaseRecord.ID)
|
}
|
return tx.Create(&pps).Error
|
})
|
if err != nil {
|
return nil, err
|
}
|
resp := new(CreatePurchaseByWmsResponse)
|
resp.PurchaseNumber = purchaseRecord.Number
|
return resp, nil
|
}
|