diff --git a/app/admin/apis/purchasemanage/purchase.go b/app/admin/apis/purchasemanage/purchase.go index af450dd..6af9f37 100644 --- a/app/admin/apis/purchasemanage/purchase.go +++ b/app/admin/apis/purchasemanage/purchase.go @@ -285,6 +285,7 @@ func ErpPurchaseDetail(c *gin.Context) { v.EffectiveCount = nCount // 有效数量 v.ErpCategoryID = erpCommodity.ErpCategoryId v.ErpCategoryName = erpCommodity.ErpCategoryName + v.Price = math.Round(v.Price*1000) / 1000 commodityList = append(commodityList, v) } diff --git a/app/admin/models/commodity.go b/app/admin/models/commodity.go index f1a848b..943b28c 100644 --- a/app/admin/models/commodity.go +++ b/app/admin/models/commodity.go @@ -509,6 +509,10 @@ func (m *ErpCommodityListReq) List() (*ErpCommodityListResp, error) { // 计算分页所需的切片索引 startIndex := page * m.PageSize + if (len(commodities)/m.PageSize + 1) < startIndex { + startIndex = 0 + page = 0 + } endIndex := (page + 1) * m.PageSize if endIndex > len(commodities) { endIndex = len(commodities) diff --git a/app/admin/models/purchase.go b/app/admin/models/purchase.go index 2dd697b..dbb9d2b 100644 --- a/app/admin/models/purchase.go +++ b/app/admin/models/purchase.go @@ -782,29 +782,63 @@ func (m *ErpPurchaseOrder) IdInit() error { return nil } -// GetPurchaseInventorySn 生成入库编号 +//// GetPurchaseInventorySn 生成入库编号 有重复的情况 +//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:", logger.Field("err", err)) +// count++ +// continue +// } +// if err == nil && !exist { +// return sn +// } +// return "" +// } +//} + +var mu sync.Mutex + +// GetPurchaseInventorySn generates a unique inventory serial number func GetPurchaseInventorySn() string { - count := 0 - for { - if count > 5 { - return "" - } + const maxRetries = 5 + + mu.Lock() + defer mu.Unlock() + + for retryCount := 0; retryCount < maxRetries; retryCount++ { 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)) + // 使用日期格式精确到天 + datePart := nowTime.Format("060102") // 格式为 YYMMDD + // 生成10位随机数 + rand.Seed(nowTime.UnixNano() + int64(retryCount)) // 为了确保每次生成不同的随机数 + randomNum := rand.Int63n(1e10) // 10位随机数,范围从0到9999999999 + randomPart := fmt.Sprintf("%010d", randomNum) // 确保随机数是10位的,前面补零 + + // 拼接日期部分和随机数部分 + sn := fmt.Sprintf("%s%s", datePart, randomPart) + exist, err := QueryRecordExist(fmt.Sprintf("SELECT * FROM erp_purchase_inventory WHERE serial_number='%s'", sn)) if err != nil { logger.Error("sn err:", logger.Field("err", err)) - count++ continue } - if err == nil && !exist { + if !exist { return sn } - return "" } + + return "" // 返回空字符串,如果在最大重试次数后仍未找到唯一编号 } // CheckCreateErpPurchaseOrderParam 新增采购订单-检查参数 @@ -1296,8 +1330,8 @@ func InventoryErpPurchaseUpdateStock(gdb *gorm.DB, list []ErpPurchaseInventory, ErpCategoryName: commodityInfo.ErpCategoryName, ErpSupplierId: purchaseOrder.ErpSupplierId, ErpSupplierName: purchaseOrder.ErpSupplierName, - StaffCostPrice: tools.RoundToTwoDecimalPlaces(v.EmployeePrice - v.ImplementationPrice), - WholesalePrice: tools.RoundToTwoDecimalPlaces(v.ImplementationPrice), + StaffCostPrice: tools.RoundToThreeDecimalPlaces(v.EmployeePrice - v.ImplementationPrice), + WholesalePrice: tools.RoundToThreeDecimalPlaces(v.ImplementationPrice), State: InStock, StorageType: PurchaseInventory, // 采购入库 FirstStockTime: nowTime, @@ -5740,6 +5774,9 @@ func CancelAuditUpdateStock(begin *gorm.DB, req ErpPurchaseOrder) error { // Inventories: inventoryList, //} + // 赋值采购退货单号 + purchaseOrder.RejectedSerialNumber = purchaseOrder.SerialNumber + // 按照采购退货更新库存 err = InventoryErpPurchaseUpdateRejectStock(begin, inventoryList, purchaseOrder) if err != nil { diff --git a/tools/utils.go b/tools/utils.go index dc7fa11..dc8de87 100644 --- a/tools/utils.go +++ b/tools/utils.go @@ -116,6 +116,11 @@ func RoundToTwoDecimalPlaces(num float64) float64 { return math.Round(num*100) / 100 } +// RoundToThreeDecimalPlaces 将float64类型四舍五入保留3位小数 +func RoundToThreeDecimalPlaces(num float64) float64 { + return math.Round(num*1000) / 1000 +} + // Uint32SliceContains 检查 uint32 切片中是否包含特定的值 func Uint32SliceContains(slice []uint32, val uint32) bool { for _, item := range slice {