62 lines
1.9 KiB
Go
62 lines
1.9 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"bj_power_mes/ent"
|
|
"bj_power_mes/ent/materialrequest"
|
|
)
|
|
|
|
// GenerateMaterialRequest 按日排产自动算料生成备料单
|
|
// 数量 = 该日所有工单计划数量 × BOM单台用量×(1+损耗率),材料来源=BOM
|
|
func (s *Service) GenerateMaterialRequest(ctx context.Context, planDate string, operator string) (int, error) {
|
|
plans, err := s.ListDailyPlans(ctx, "", planDate)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if len(plans) == 0 {
|
|
return 0, errors.New("该日期没有排产计划")
|
|
}
|
|
created := 0
|
|
for _, plan := range plans {
|
|
bom, err := s.ListBom(ctx, plan.OrderNo)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, item := range bom {
|
|
reqQty := float64(plan.PlanQty) * item.UnitQty * (1 + item.LossRate/100)
|
|
if reqQty <= 0 {
|
|
continue
|
|
}
|
|
reqNo := fmt.Sprintf("MR%s%03d", time.Now().Format("20060102150405"), len(bom))
|
|
_ = s.ctx.EntClient.MaterialRequest.Create().
|
|
SetRequestNo(reqNo).SetOrderNo(plan.OrderNo).SetPlanDate(plan.PlanDate).
|
|
SetMaterialCode(item.MaterialCode).SetMaterialName(item.MaterialName).
|
|
SetUnit(item.Unit).SetManageMode(item.ManageMode).SetReqQty(reqQty).
|
|
SetStatus("PENDING").SetOperator(operator).
|
|
SetTargetDock("").Exec(ctx)
|
|
created++
|
|
}
|
|
}
|
|
s.ctx.EventLog.Write(ctx, "material.request.generate", "", operator, "material_request", planDate, "按日排产自动生成备料单", map[string]any{"planDate": planDate, "count": created})
|
|
return created, nil
|
|
}
|
|
|
|
// ListMaterialRequests 查询备料单
|
|
func (s *Service) ListMaterialRequests(ctx context.Context, orderNo, planDate, status string) ([]*ent.MaterialRequest, error) {
|
|
q := s.ctx.EntClient.MaterialRequest.Query()
|
|
if orderNo != "" {
|
|
q = q.Where(materialrequest.OrderNo(orderNo))
|
|
}
|
|
if planDate != "" {
|
|
q = q.Where(materialrequest.PlanDate(planDate))
|
|
}
|
|
if status != "" {
|
|
q = q.Where(materialrequest.Status(status))
|
|
}
|
|
return q.Order(materialrequest.ByID()).All(ctx)
|
|
}
|