|
@@ -35,6 +35,7 @@ func ProductPlan(r *GinRouter) {
|
|
|
|
|
|
// 下载部件单据
|
|
|
r.GET("/bill/plan/download", DownLoadCompBills)
|
|
|
+ r.GET("/bill/plan/download", DownLoadPlanBills)
|
|
|
|
|
|
// 生产成本表
|
|
|
r.GET("/plan/cost/download", DownLoadPlanCost)
|
|
@@ -87,6 +88,148 @@ func DownLoadPlanCost(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
|
|
}
|
|
|
|
|
|
+func DownLoadPlanBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ _planId := c.Query("id")
|
|
|
+ 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("数据未找到")
|
|
|
+ }
|
|
|
+ // 获取所有stages单据id
|
|
|
+ billIds := make([]string, 0)
|
|
|
+ curComp := &model.PackComponent{}
|
|
|
+ for _, comp := range plan.Pack.Components {
|
|
|
+ if comp.Id == "" || len(curComp.Stages) == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ for _, stage := range comp.Stages {
|
|
|
+ billId, _ := primitive.ObjectIDFromHex(stage.BillId)
|
|
|
+ if !billId.IsZero() {
|
|
|
+ billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ // 去重单据号
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ 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 = NewProductBill(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(companyName)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if billExcel == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ billExcel.SetRow(row)
|
|
|
+ billExcel.Draws()
|
|
|
+ row = billExcel.GetRow() + 5
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 DownLoadCompBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
_planId := c.Query("id")
|
|
|
compId := c.Query("compId")
|