476 lines
16 KiB
Go
476 lines
16 KiB
Go
|
package models
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
orm "go-admin/common/global"
|
||
|
"go-admin/logger"
|
||
|
"math/rand"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
PurchaseTypeProcure = "procure"
|
||
|
PurchaseTypeReject = "reject"
|
||
|
)
|
||
|
|
||
|
type ErpPurchaseOrder struct {
|
||
|
Model
|
||
|
SerialNumber string `json:"serial_number" gorm:"index"`
|
||
|
PurchaseType string `json:"purchase_type"` // 类型:procure-采购 reject-退货
|
||
|
StoreId uint32 `json:"store_id" gorm:"index"` // 门店id
|
||
|
StoreName string `json:"store_name"`
|
||
|
ErpSupplierId uint32 `json:"erp_supplier_id" gorm:"index"`
|
||
|
ErpSupplierName string `json:"erp_supplier_name"`
|
||
|
MakerTime time.Time `json:"maker_time"`
|
||
|
MakerId uint32 `json:"maker_id" gorm:"index"`
|
||
|
MakerName string `json:"maker_name"`
|
||
|
AuditTime time.Time `json:"audit_time"`
|
||
|
AuditorId uint32 `json:"auditor_id" gorm:"index"`
|
||
|
AuditorName string `json:"auditor_name"`
|
||
|
State uint32 `json:"state"` // 1-待审核 2-待入库 3-待退货 4-已完成
|
||
|
RejectedPurchaseOrderId uint32 `json:"rejected_purchase_order_id"`
|
||
|
Commodities []ErpPurchaseCommodity `json:"commodities" gorm:"-"`
|
||
|
// erp_purchase_order
|
||
|
}
|
||
|
|
||
|
type ErpPurchaseCommodity struct {
|
||
|
Model
|
||
|
ErpPurchaseOrderId uint32 `json:"erp_purchase_order_id" gorm:"index"`
|
||
|
ErpCommodityId uint32 `json:"erp_commodity_id" gorm:"index"`
|
||
|
ErpCommodityName string `json:"erp_commodity_name"`
|
||
|
CommoditySerialNumber string `json:"commodity_serial_number" gorm:"index"`
|
||
|
IMEIType uint32 `json:"imei_type"` // 1-无串码 2-串码
|
||
|
IMEI string `json:"imei"`
|
||
|
Count uint32 `json:"count"`
|
||
|
Price uint32 `json:"price"`
|
||
|
Amount uint32 `json:"amount"`
|
||
|
Remark string `json:"remark"`
|
||
|
InventoryCount int32 `json:"inventory_count"` // 入库
|
||
|
RejectedPrice uint32 `json:"rejected_price"`
|
||
|
RejectedCount uint32 `json:"rejected_count"`
|
||
|
RejectedAmount uint32 `json:"rejected_amount"`
|
||
|
|
||
|
//PriceString string `json:"price_string" gorm:"index"`
|
||
|
// erp_purchase_commodity
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
InventoryTypeProcure = "procure"
|
||
|
InventoryTypeReject = "reject"
|
||
|
)
|
||
|
|
||
|
type ErpPurchaseInventory struct {
|
||
|
Model
|
||
|
SerialNumber string `json:"serial_number" gorm:"index"` // 入库编号
|
||
|
InventoryType string `json:"inventory_type"` //
|
||
|
ErpPurchaseOrderId uint32 `json:"erp_purchase_order_id" gorm:"index"` //
|
||
|
ErpCommodityId uint32 `json:"erp_commodity_id" gorm:"index"`
|
||
|
ErpCommodityName string `json:"erp_commodity_name"`
|
||
|
CommoditySerialNumber string `json:"commodity_serial_number" gorm:"index"`
|
||
|
IMEIType uint32 `json:"imei_type"` // 1-无串码 2-串码
|
||
|
Count uint32 `json:"count"`
|
||
|
Price uint32 `json:"price"`
|
||
|
ImplementationPrice uint32 `json:"implementation_price"`
|
||
|
EmployeePrice uint32 `json:"employee_price"` // 员工成本
|
||
|
InventoryCount int32 `json:"inventory_count"` // 入库
|
||
|
Amount uint32 `json:"amount"`
|
||
|
Remark string `json:"remark"`
|
||
|
//IMEIList []string `json:"imei_list" gorm:"-"`
|
||
|
|
||
|
ErpPurchaseCommodity *ErpPurchaseCommodity `json:"erp_purchase_commodity" gorm:"-"`
|
||
|
// erp_purchase_inventory
|
||
|
}
|
||
|
|
||
|
type ErpInventoryCommodity struct {
|
||
|
Model
|
||
|
SerialNumber string `json:"serial_number" gorm:"index"` // 入库编号
|
||
|
InventoryType string `json:"inventory_type"`
|
||
|
ErpPurchaseInventoryId uint32 `json:"erp_purchase_inventory_id"`
|
||
|
ErpPurchaseOrderId uint32 `json:"erp_purchase_order_id"` //
|
||
|
ErpCommodityId uint32 `json:"erp_commodity_id"`
|
||
|
ErpCommodityName string `json:"erp_commodity_name"`
|
||
|
CommoditySerialNumber string `json:"commodity_serial_number"`
|
||
|
IMEIType uint32 `json:"imei_type"` // 1-无串码 2-串码
|
||
|
IMEI string `json:"imei"`
|
||
|
Count uint32 `json:"count"`
|
||
|
Price uint32 `json:"price"`
|
||
|
EmployeePrice uint32 `json:"employee_price"` // 员工成本
|
||
|
ImplementationPrice uint32 `json:"implementation_price"`
|
||
|
Amount uint32 `json:"amount"`
|
||
|
Remark string `json:"remark"`
|
||
|
}
|
||
|
|
||
|
//type ErpPurchaseInventoryRecord struct {
|
||
|
// Model
|
||
|
// ErpPurchaseOrderId uint32 `json:"erp_purchase_order_id"` //
|
||
|
// ErpCommodityId uint32 `json:"erp_commodity_id"`
|
||
|
// ErpCommodityName string `json:"erp_commodity_name"`
|
||
|
// CommoditySerialNumber string `json:"commodity_serial_number"`
|
||
|
// IMEIType uint32 `json:"imei_type"` // 1-无串码 2-串码
|
||
|
// IMEI string `json:"imei"`
|
||
|
// CommodityName string `json:"commodity_name"`
|
||
|
// Count uint32 `json:"count"`
|
||
|
// Price uint32 `json:"price"`
|
||
|
// Amount uint32 `json:"amount"`
|
||
|
// Remark string `json:"remark"`
|
||
|
// RejectedPrice uint32 `json:"rejected_price"`
|
||
|
// RejectedCount uint32 `json:"rejected_count"`
|
||
|
// RejectedAmount uint32 `json:"rejected_amount"`
|
||
|
//
|
||
|
// //PriceString string `json:"price_string" gorm:"index"`
|
||
|
//}
|
||
|
|
||
|
//type ErpInventoryRecordCommodity struct {
|
||
|
// Model
|
||
|
// ErpPurchaseOrderId uint32 `json:"erp_purchase_order_id"` //
|
||
|
//
|
||
|
//}
|
||
|
|
||
|
func GetPurchaseInventorySn() string {
|
||
|
count := 0
|
||
|
for {
|
||
|
if count > 5 {
|
||
|
return ""
|
||
|
}
|
||
|
nowTime := time.Now()
|
||
|
sn := nowTime.Format("060102")
|
||
|
sn += fmt.Sprintf("%d", nowTime.Unix()%100)
|
||
|
rand.Seed(nowTime.UnixNano())
|
||
|
sn += fmt.Sprintf("%d", rand.Int31n(100))
|
||
|
exist, err := QueryRecordExist(fmt.Sprintf("SELECT * FROM erp_purchase_inventory WHERE serial_number='%s'", sn))
|
||
|
if err != nil {
|
||
|
logger.Error("sn err:", err)
|
||
|
count++
|
||
|
continue
|
||
|
}
|
||
|
if err == nil && !exist {
|
||
|
return sn
|
||
|
}
|
||
|
return ""
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *ErpPurchaseOrder) IdInit() error {
|
||
|
if m.StoreId != 0 {
|
||
|
store, err := GetStore(m.StoreId)
|
||
|
if err != nil {
|
||
|
logger.Error("get store err:", err)
|
||
|
return err
|
||
|
}
|
||
|
m.StoreName = store.Name
|
||
|
}
|
||
|
|
||
|
if m.ErpSupplierId != 0 {
|
||
|
supplier, err := GetErpSupplier(m.ErpSupplierId)
|
||
|
if err != nil {
|
||
|
logger.Error("get supplier err:", err)
|
||
|
return err
|
||
|
}
|
||
|
m.ErpSupplierName = supplier.Name
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func ErpPurchaseCommodityListPerfectInfo(purchaseCommodities []ErpPurchaseCommodity) error {
|
||
|
commodityIds := make([]uint32, 0, len(purchaseCommodities))
|
||
|
for i, _ := range purchaseCommodities {
|
||
|
commodityIds = append(commodityIds, purchaseCommodities[i].ID)
|
||
|
}
|
||
|
commodityMap, err := GetErpCommodityMap(commodityIds)
|
||
|
if err != nil {
|
||
|
logger.Error("purchase commodities err:", err)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
for i, _ := range purchaseCommodities {
|
||
|
v, ok := commodityMap[purchaseCommodities[i].ErpCommodityId]
|
||
|
if ok {
|
||
|
purchaseCommodities[i].CommoditySerialNumber = v.SerialNumber
|
||
|
purchaseCommodities[i].IMEIType = v.IMEIType
|
||
|
purchaseCommodities[i].IMEI = v.IMEI
|
||
|
purchaseCommodities[i].ErpCommodityName = v.Name
|
||
|
if purchaseCommodities[i].Count != 0 {
|
||
|
purchaseCommodities[i].Amount = purchaseCommodities[i].Count * purchaseCommodities[i].Price
|
||
|
}
|
||
|
if purchaseCommodities[i].RejectedCount != 0 {
|
||
|
purchaseCommodities[i].RejectedAmount = purchaseCommodities[i].RejectedCount * purchaseCommodities[i].RejectedPrice
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
type ErpPurchaseOrderListReq struct {
|
||
|
SerialNumber string `json:"serial_number"`
|
||
|
PurchaseType uint32 `json:"purchase_type"`
|
||
|
StoreId uint32 `json:"store_id"` // 门店id
|
||
|
ErpSupplierId uint32 `json:"erp_supplier_id"`
|
||
|
MakerTimeStart string `json:"maker_time_start"`
|
||
|
MakerTimeEnd string `json:"maker_time_end"`
|
||
|
State uint32 `json:"state"` // 1-待审核 2-待入库 3-待退货 4-已完成
|
||
|
PageNum int `json:"page_num"`
|
||
|
PageSize int `json:"page_size"`
|
||
|
IsExport uint32 `json:"is_export"` // 1-导出
|
||
|
}
|
||
|
type ErpPurchaseOrderListResp struct {
|
||
|
List []ErpPurchaseOrder `json:"list"`
|
||
|
Total int `json:"total"`
|
||
|
PageNum int `json:"page_num"`
|
||
|
PageSize int `json:"page_size"`
|
||
|
}
|
||
|
|
||
|
func (m *ErpPurchaseOrderListReq) List() (*ErpPurchaseOrderListResp, error) {
|
||
|
resp := &ErpPurchaseOrderListResp{
|
||
|
PageNum: m.PageNum,
|
||
|
PageSize: m.PageSize,
|
||
|
}
|
||
|
page := m.PageNum - 1
|
||
|
if page < 0 {
|
||
|
page = 0
|
||
|
}
|
||
|
if m.PageSize == 0 {
|
||
|
m.PageSize = 10
|
||
|
}
|
||
|
qs := orm.Eloquent.Table("erp_purchase_order")
|
||
|
if m.SerialNumber != "" {
|
||
|
qs = qs.Where("serial_number=?", m.SerialNumber)
|
||
|
}
|
||
|
if m.PurchaseType != 0 {
|
||
|
qs = qs.Where("purchase_type=?", m.PurchaseType)
|
||
|
}
|
||
|
if m.StoreId != 0 {
|
||
|
qs = qs.Where("store_id=?", m.StoreId)
|
||
|
}
|
||
|
if m.ErpSupplierId != 0 {
|
||
|
qs = qs.Where("erp_supplier_id=?", m.ErpSupplierId)
|
||
|
}
|
||
|
if m.State != 0 {
|
||
|
qs = qs.Where("state=?", m.State)
|
||
|
}
|
||
|
if m.MakerTimeStart != "" {
|
||
|
parse, err := time.Parse(DateTimeFormat, m.MakerTimeStart)
|
||
|
if err != nil {
|
||
|
logger.Errorf("err:", err)
|
||
|
//return users, 0, err
|
||
|
}
|
||
|
qs = qs.Where("maker_time > ?", parse)
|
||
|
}
|
||
|
if m.MakerTimeEnd != "" {
|
||
|
parse, err := time.Parse(DateTimeFormat, m.MakerTimeEnd)
|
||
|
if err != nil {
|
||
|
logger.Errorf("err:", err)
|
||
|
//return users, 0, err
|
||
|
}
|
||
|
parse = parse.AddDate(0, 0, 1)
|
||
|
qs = qs.Where("maker_time < ?", parse)
|
||
|
}
|
||
|
|
||
|
var count int64
|
||
|
err := qs.Count(&count).Error
|
||
|
if err != nil {
|
||
|
logger.Error("count err:", err)
|
||
|
return resp, err
|
||
|
}
|
||
|
resp.Total = int(count)/m.PageSize + 1
|
||
|
var orders []ErpPurchaseOrder
|
||
|
err = qs.Order("id DESC").Offset(page * m.PageSize).Limit(m.PageSize).Find(&orders).Error
|
||
|
if err != nil && err != RecordNotFound {
|
||
|
logger.Error("erp commodity list err:", err)
|
||
|
return resp, err
|
||
|
}
|
||
|
|
||
|
resp.List = orders
|
||
|
return resp, nil
|
||
|
}
|
||
|
|
||
|
func ErpPurchaseInventoryListIdInit(inventories []ErpPurchaseInventory, commodities []ErpInventoryCommodity, orderId uint32, inventoryType string) error {
|
||
|
inventorySn := GetPurchaseInventorySn()
|
||
|
ids := make([]uint32, 0, len(inventories))
|
||
|
for i, _ := range inventories {
|
||
|
ids = append(ids, inventories[i].ErpCommodityId)
|
||
|
}
|
||
|
commodityMap, err := GetErpCommodityMap(ids)
|
||
|
if err != nil {
|
||
|
logger.Error("commodity map err:", err)
|
||
|
return err
|
||
|
}
|
||
|
purchaseCommodityMap, err := GetErpPurchaseOrderPurchaseCommodityMap(orderId)
|
||
|
if err != nil {
|
||
|
logger.Error("purchase commodity map err:", err)
|
||
|
return err
|
||
|
}
|
||
|
commodityCountMap := make(map[uint32]uint32, 0)
|
||
|
for i, _ := range commodities {
|
||
|
purchaseCommodity, _ := purchaseCommodityMap[inventories[i].ErpCommodityId]
|
||
|
v, ok := commodityMap[commodities[i].ErpCommodityId]
|
||
|
if ok {
|
||
|
commodities[i].SerialNumber = inventorySn
|
||
|
commodities[i].InventoryType = inventoryType
|
||
|
commodities[i].ErpPurchaseOrderId = orderId
|
||
|
commodities[i].ErpCommodityName = v.Name
|
||
|
commodities[i].CommoditySerialNumber = v.SerialNumber
|
||
|
commodities[i].IMEIType = v.IMEIType
|
||
|
commodities[i].Price = purchaseCommodity.Price
|
||
|
commodities[i].Amount = commodities[i].Count * commodities[i].ImplementationPrice
|
||
|
if v.IMEIType == 1 {
|
||
|
commodityCountMap[v.ID] += commodities[i].Count
|
||
|
} else {
|
||
|
commodityCountMap[v.ID] += 1
|
||
|
}
|
||
|
|
||
|
//commodities[i].InventoryCount = int32(inventories[i].Count)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for i, _ := range inventories {
|
||
|
purchaseCommodity, _ := purchaseCommodityMap[inventories[i].ErpCommodityId]
|
||
|
commodityCount, _ := commodityCountMap[inventories[i].ErpCommodityId]
|
||
|
v, ok := commodityMap[inventories[i].ErpCommodityId]
|
||
|
if ok {
|
||
|
inventories[i].ErpPurchaseOrderId = orderId
|
||
|
inventories[i].InventoryType = inventoryType
|
||
|
inventories[i].ErpCommodityName = v.Name
|
||
|
inventories[i].CommoditySerialNumber = v.SerialNumber
|
||
|
inventories[i].IMEIType = v.IMEIType
|
||
|
inventories[i].Price = purchaseCommodity.Price
|
||
|
inventories[i].Count = commodityCount
|
||
|
inventories[i].Amount = inventories[i].Count * inventories[i].ImplementationPrice
|
||
|
inventories[i].InventoryCount = purchaseCommodity.InventoryCount + int32(inventories[i].Count)
|
||
|
if int32(purchaseCommodity.Count) < inventories[i].InventoryCount {
|
||
|
return errors.New(fmt.Sprintf("order id:%d purchase commodity id:%d inventory count err", orderId, purchaseCommodity.ID))
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
//func GetErpPurchaseInventoryMap(ids []uint32) (map[uint32]ErpPurchaseInventory, error) {
|
||
|
// inventoryMap := make(map[uint32]ErpPurchaseInventory, 0)
|
||
|
// if len(ids) == 0 {
|
||
|
// return inventoryMap, nil
|
||
|
// }
|
||
|
// var inventories []ErpPurchaseInventory
|
||
|
// err := orm.Eloquent.Table("erp_purchase_inventory").Where("id IN (?)", ids).Find(&inventories).Error
|
||
|
// if err != nil {
|
||
|
// logger.Error("inventories err:",err)
|
||
|
// return inventoryMap, err
|
||
|
// }
|
||
|
//
|
||
|
// for i, _ := range inventories {
|
||
|
// inventoryMap[inventories[i].ErpPurchaseOrderId]
|
||
|
// }
|
||
|
//}
|
||
|
|
||
|
func GetErpPurchaseOrderPurchaseCommodityMap(orderId uint32) (map[uint32]ErpPurchaseCommodity, error) {
|
||
|
commodityMap := make(map[uint32]ErpPurchaseCommodity, 0)
|
||
|
if orderId == 0 {
|
||
|
return commodityMap, nil
|
||
|
}
|
||
|
|
||
|
var commodities []ErpPurchaseCommodity
|
||
|
err := orm.Eloquent.Table("erp_purchase_commodity").Where("erp_purchase_order_id=?", orderId).Find(&commodities).Error
|
||
|
if err != nil {
|
||
|
logger.Error("commodities err:", err)
|
||
|
return commodityMap, err
|
||
|
}
|
||
|
|
||
|
for i, _ := range commodities {
|
||
|
commodityMap[commodities[i].ErpCommodityId] = commodities[i]
|
||
|
}
|
||
|
|
||
|
return commodityMap, nil
|
||
|
}
|
||
|
|
||
|
type ErpPurchaseQuotationListReq struct {
|
||
|
SerialNumber string `json:"serial_number"`
|
||
|
StoreId uint32 `json:"store_id"` // 门店id
|
||
|
MakerTimeStart string `json:"maker_time_start"`
|
||
|
MakerTimeEnd string `json:"maker_time_end"`
|
||
|
State uint32 `json:"state"` // 1-待审核 2-待入库 3-待退货 4-已完成
|
||
|
PageNum int `json:"page_num"`
|
||
|
PageSize int `json:"page_size"`
|
||
|
IsExport uint32 `json:"is_export"` // 1-导出
|
||
|
}
|
||
|
type ErpPurchaseQuotationListResp struct {
|
||
|
List []ErpPurchaseOrder `json:"list"`
|
||
|
Total int `json:"total"`
|
||
|
PageNum int `json:"page_num"`
|
||
|
PageSize int `json:"page_size"`
|
||
|
}
|
||
|
|
||
|
func (m *ErpPurchaseQuotationListReq) List() (*ErpPurchaseOrderListResp, error) {
|
||
|
resp := &ErpPurchaseOrderListResp{
|
||
|
PageNum: m.PageNum,
|
||
|
PageSize: m.PageSize,
|
||
|
}
|
||
|
page := m.PageNum - 1
|
||
|
if page < 0 {
|
||
|
page = 0
|
||
|
}
|
||
|
if m.PageSize == 0 {
|
||
|
m.PageSize = 10
|
||
|
}
|
||
|
qs := orm.Eloquent.Table("erp_purchase_order").Where("purchase_type=?", "procure")
|
||
|
if m.SerialNumber != "" {
|
||
|
qs = qs.Where("serial_number=?", m.SerialNumber)
|
||
|
}
|
||
|
if m.StoreId != 0 {
|
||
|
qs = qs.Where("store_id=?", m.StoreId)
|
||
|
}
|
||
|
if m.State != 0 {
|
||
|
qs = qs.Where("state=?", m.State)
|
||
|
}
|
||
|
if m.MakerTimeStart != "" {
|
||
|
parse, err := time.Parse(DateTimeFormat, m.MakerTimeStart)
|
||
|
if err != nil {
|
||
|
logger.Errorf("err:", err)
|
||
|
//return users, 0, err
|
||
|
}
|
||
|
qs = qs.Where("maker_time > ?", parse)
|
||
|
}
|
||
|
if m.MakerTimeEnd != "" {
|
||
|
parse, err := time.Parse(DateTimeFormat, m.MakerTimeEnd)
|
||
|
if err != nil {
|
||
|
logger.Errorf("err:", err)
|
||
|
//return users, 0, err
|
||
|
}
|
||
|
parse = parse.AddDate(0, 0, 1)
|
||
|
qs = qs.Where("maker_time < ?", parse)
|
||
|
}
|
||
|
|
||
|
var count int64
|
||
|
err := qs.Count(&count).Error
|
||
|
if err != nil {
|
||
|
logger.Error("count err:", err)
|
||
|
return resp, err
|
||
|
}
|
||
|
resp.Total = int(count)/m.PageSize + 1
|
||
|
var orders []ErpPurchaseOrder
|
||
|
err = qs.Order("id DESC").Offset(page * m.PageSize).Limit(m.PageSize).Find(&orders).Error
|
||
|
if err != nil && err != RecordNotFound {
|
||
|
logger.Error("erp commodity list err:", err)
|
||
|
return resp, err
|
||
|
}
|
||
|
|
||
|
resp.List = orders
|
||
|
return resp, nil
|
||
|
}
|
||
|
|
||
|
func GetErpPurchaseCommodityMap(ids []uint32, orderId uint32) (map[uint32]ErpPurchaseCommodity, error) {
|
||
|
commodityMap := make(map[uint32]ErpPurchaseCommodity, 0)
|
||
|
var commodities []ErpPurchaseCommodity
|
||
|
err := orm.Eloquent.Table("erp_purchase_commodity").Where("erp_purchase_order_id=?", orderId).
|
||
|
Where("erp_commodity_id IN (?)", ids).Find(&commodities).Error
|
||
|
if err != nil {
|
||
|
logger.Error("get supplier err:", err)
|
||
|
return commodityMap, err
|
||
|
}
|
||
|
for i, _ := range commodities {
|
||
|
commodityMap[commodities[i].ErpCommodityId] = commodities[i]
|
||
|
}
|
||
|
return commodityMap, nil
|
||
|
}
|