|
@@ -6,9 +6,11 @@ import (
|
|
|
"box-cost/log"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
+ "github.com/xuri/excelize/v2"
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
)
|
|
@@ -85,131 +87,160 @@ type SupplierPlanCost struct {
|
|
|
|
|
|
// }
|
|
|
|
|
|
-// func DownLoadPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
-// _planId := c.Query("id")
|
|
|
-// compId := c.Query("compId")
|
|
|
-// planId, err := primitive.ObjectIDFromHex(_planId)
|
|
|
-// if err != nil {
|
|
|
-// return nil, errors.New("planId错误")
|
|
|
-// }
|
|
|
-// plan := model.ProductPlan{}
|
|
|
-// found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
-// CollectName: repo.CollectionProductPlan,
|
|
|
-// Query: repo.Map{"_id": planId},
|
|
|
-// }, &plan)
|
|
|
-// if !found || err != nil {
|
|
|
-// return nil, errors.New("数据未找到")
|
|
|
-// }
|
|
|
-// // 获取部件单据
|
|
|
-// curComp := &model.PackComponent{}
|
|
|
-// for _, comp := range plan.Pack.Components {
|
|
|
-// if comp.Id == compId {
|
|
|
-// curComp = comp
|
|
|
-// }
|
|
|
-// }
|
|
|
-// if curComp.Id == "" {
|
|
|
-// return nil, errors.New("该组件不存在")
|
|
|
-// }
|
|
|
+func DownLoadCompBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ _planId := c.Query("id")
|
|
|
+ compId := c.Query("compId")
|
|
|
+ planId, err := primitive.ObjectIDFromHex(_planId)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New("planId错误")
|
|
|
+ }
|
|
|
+ plan := model.ProductPlan{}
|
|
|
+ found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ CollectName: repo.CollectionProductPlan,
|
|
|
+ Query: repo.Map{"_id": planId},
|
|
|
+ }, &plan)
|
|
|
+ if !found || err != nil {
|
|
|
+ return nil, errors.New("数据未找到")
|
|
|
+ }
|
|
|
+ // 获取部件单据
|
|
|
+ curComp := &model.PackComponent{}
|
|
|
+ for _, comp := range plan.Pack.Components {
|
|
|
+ if comp.Id == compId {
|
|
|
+ curComp = comp
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if curComp.Id == "" {
|
|
|
+ return nil, errors.New("该组件不存在")
|
|
|
+ }
|
|
|
|
|
|
-// // 获取bill
|
|
|
-// if len(curComp.Stages) == 0 {
|
|
|
-// return nil, errors.New("该组件数据不存在")
|
|
|
-// }
|
|
|
-// f := excelize.NewFile()
|
|
|
-// index := f.NewSheet("Sheet1")
|
|
|
-// f.SetActiveSheet(index)
|
|
|
-// f.SetDefaultFont("宋体")
|
|
|
-// companyName := getCompanyName(apictx)
|
|
|
-
|
|
|
-// // 获取不同类型的单据id
|
|
|
-// set := make(map[int]map[primitive.ObjectID]struct{})
|
|
|
-// for _, stage := range curComp.Stages {
|
|
|
-// _billId := stage.BillId
|
|
|
-// billId, _ := primitive.ObjectIDFromHex(_billId)
|
|
|
-// if !billId.IsZero() {
|
|
|
-// set[stage.Type] = map[primitive.ObjectID]struct{}{billId: struct{}{}}
|
|
|
-// }
|
|
|
-// }
|
|
|
+ // 获取bill
|
|
|
+ if len(curComp.Stages) == 0 {
|
|
|
+ return nil, errors.New("该组件数据不存在")
|
|
|
+ }
|
|
|
|
|
|
-// // row := 0
|
|
|
-// // for k, bill := range set {
|
|
|
-// // for billId, _ := range bill {
|
|
|
-// // var billExcel IExcel
|
|
|
-// // collectName := ""
|
|
|
-// // if k == 1 {
|
|
|
-// // collectName = repo.CollectionBillPurchase
|
|
|
-// // billExcel = NewPurchaseBill(f)
|
|
|
-// // }
|
|
|
-// // if k == 2 {
|
|
|
-// // collectName = repo.CollectionBillProduce
|
|
|
-// // billExcel = NewProduceBill(f)
|
|
|
-// // }
|
|
|
-// // if k == 2 {
|
|
|
-// // collectName = repo.CollectionBillProduct
|
|
|
-// // billExcel = NewProductBill(f)
|
|
|
-// // }
|
|
|
-// // billExcel.SetRow(row)
|
|
|
-
|
|
|
-// // purchase := model.PurchaseBill{}
|
|
|
-// // found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
-// // CollectName: repo.CollectionBillPurchase,
|
|
|
-// // Query: repo.Map{"_id": billId},
|
|
|
-// // }, &purchase)
|
|
|
-// // fmt.Println(purchase)
|
|
|
-// // if found {
|
|
|
-// // var billExcel IPurchBill
|
|
|
-// // if purchase.Process != nil {
|
|
|
-// // billExcel = NewProcessBill(f)
|
|
|
-// // }
|
|
|
-
|
|
|
-// // if len(purchase.Paper) > 0 {
|
|
|
-// // billExcel = NewPurchaseBill(f)
|
|
|
-// // }
|
|
|
-// // // purchaseExcel := NewPurchaseBill(f)
|
|
|
-// // billExcel.SetContent(&purchase)
|
|
|
-// // billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
|
|
|
-// // billExcel.SetOffset(offset)
|
|
|
-// // // billExcel.Title = fmt.Sprintf("%s原材料采购单", companyName)
|
|
|
-// // //设置对应的数据
|
|
|
-// // // purchaseExcel.Offset = offset
|
|
|
-// // billExcel.Draws()
|
|
|
-
|
|
|
-// // offset += 15
|
|
|
-// // }
|
|
|
-// //
|
|
|
-// // }
|
|
|
-
|
|
|
-// // 采购单
|
|
|
-// // if k == 1 {
|
|
|
-
|
|
|
-// // }
|
|
|
-// // // 加工单
|
|
|
-// // if k == 2 {
|
|
|
-// // for billId, _ := range bill {
|
|
|
-
|
|
|
-// // }
|
|
|
-
|
|
|
-// // }
|
|
|
-// // // 成品采购单
|
|
|
-// // if k == 3 {
|
|
|
-// // for billId, _ := range bill {
|
|
|
-
|
|
|
-// // }
|
|
|
-
|
|
|
-// // }
|
|
|
-// }
|
|
|
+ // 获取不同类型的单据id
|
|
|
+ // set := make(map[int]map[primitive.ObjectID]struct{})
|
|
|
+ billIds := make([]string, 0)
|
|
|
+ for _, stage := range curComp.Stages {
|
|
|
+ _billId := stage.BillId
|
|
|
|
|
|
-// c.Header("Content-Type", "application/octet-stream")
|
|
|
-// c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
|
|
|
-// c.Header("Content-Transfer-Encoding", "binary")
|
|
|
+ billId, _ := primitive.ObjectIDFromHex(_billId)
|
|
|
+ if !billId.IsZero() {
|
|
|
+ // set[stage.Type] = map[primitive.ObjectID]struct{}{billId: struct{}{}}
|
|
|
+ billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
|
|
|
|
|
|
-// err = f.Write(c.Writer)
|
|
|
-// if err != nil {
|
|
|
-// return nil, err
|
|
|
-// }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ typeBillIds := removeDuplicationSort(billIds)
|
|
|
+ if len(typeBillIds) < 1 {
|
|
|
+ return nil, errors.New("未找到单据信息")
|
|
|
+ }
|
|
|
+ f := excelize.NewFile()
|
|
|
+ index := f.NewSheet("Sheet1")
|
|
|
+ f.SetActiveSheet(index)
|
|
|
+ f.SetDefaultFont("宋体")
|
|
|
+ companyName := getCompanyName(apictx)
|
|
|
+
|
|
|
+ row := 0
|
|
|
+ for _, tId := range typeBillIds {
|
|
|
+ tidArr := strings.Split(tId, "_")
|
|
|
+
|
|
|
+ var billExcel IExcel
|
|
|
+
|
|
|
+ // 采购
|
|
|
+ billId, _ := primitive.ObjectIDFromHex(tidArr[1])
|
|
|
+ if tidArr[0] == "1" {
|
|
|
+ purchase := model.PurchaseBill{}
|
|
|
+ found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ CollectName: repo.CollectionBillPurchase,
|
|
|
+ Query: repo.Map{"_id": billId},
|
|
|
+ }, &purchase)
|
|
|
+ if found {
|
|
|
+ billExcel = NewPurchaseBill(f)
|
|
|
+ if purchase.Reviewed == 1 {
|
|
|
+ if len(purchase.SignUsers) > 0 {
|
|
|
+ signs := []*model.Signature{}
|
|
|
+ repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
+ CollectName: repo.CollectionSignature,
|
|
|
+ Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
|
|
|
+ Sort: bson.M{"sort": 1}, // 升序
|
|
|
+ }, &signs)
|
|
|
+ billExcel.SetSignatures(signs)
|
|
|
+ }
|
|
|
|
|
|
-// return nil, nil
|
|
|
-// }
|
|
|
+ }
|
|
|
+ billExcel.SetContent(&purchase)
|
|
|
+ billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ // 工艺
|
|
|
+ if tidArr[0] == "2" {
|
|
|
+ produce := model.ProduceBill{}
|
|
|
+ found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ CollectName: repo.CollectionBillProduce,
|
|
|
+ Query: repo.Map{"_id": billId},
|
|
|
+ }, &produce)
|
|
|
+ if found {
|
|
|
+ billExcel = NewProduceBill(f)
|
|
|
+ if produce.Reviewed == 1 {
|
|
|
+ if len(produce.SignUsers) > 0 {
|
|
|
+ signs := []*model.Signature{}
|
|
|
+ repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
+ CollectName: repo.CollectionSignature,
|
|
|
+ Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
|
|
|
+ Sort: bson.M{"sort": 1}, // 升序
|
|
|
+ }, &signs)
|
|
|
+ billExcel.SetSignatures(signs)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ billExcel.SetContent(&produce)
|
|
|
+ billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ // 成品采购
|
|
|
+ if tidArr[0] == "3" {
|
|
|
+ product := model.ProductBill{}
|
|
|
+ found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ CollectName: repo.CollectionBillProduct,
|
|
|
+ Query: repo.Map{"_id": billId},
|
|
|
+ }, &product)
|
|
|
+ if found {
|
|
|
+ billExcel = NewProduceBill(f)
|
|
|
+ if product.Reviewed == 1 {
|
|
|
+ if len(product.SignUsers) > 0 {
|
|
|
+ signs := []*model.Signature{}
|
|
|
+ repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
+ CollectName: repo.CollectionSignature,
|
|
|
+ Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
|
|
|
+ Sort: bson.M{"sort": 1}, // 升序
|
|
|
+ }, &signs)
|
|
|
+ billExcel.SetSignatures(signs)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ billExcel.SetContent(&product)
|
|
|
+ billExcel.SetTitle(fmt.Sprintf("%s成品采购单", companyName))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ billExcel.SetRow(row)
|
|
|
+ billExcel.Draws()
|
|
|
+ row = billExcel.GetRow() + 3
|
|
|
+ }
|
|
|
+
|
|
|
+ c.Header("Content-Type", "application/octet-stream")
|
|
|
+ c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
|
|
|
+ c.Header("Content-Transfer-Encoding", "binary")
|
|
|
+
|
|
|
+ err = f.Write(c.Writer)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil, nil
|
|
|
+}
|
|
|
|
|
|
// 创建生产计划
|
|
|
func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|