333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { default as BaseController } from "../baseController";
import { Post } from "egg-shell-decorators";
import { PurchaseDamagedOrder } from "../../entity/inbound/purchase/purchaseDamagedOrder";
 
/**
 * 收货 - 收货计划单
 */
export default class DamagedOrderController extends BaseController {
  //#region 残次品转入到预到货单
  @Post()
  public async toPurchaseOrder() {
    let { ctx } = this;
    let body = ctx.request.body;
    let userInfo = await ctx.helper.userInfo();
    let damagedOrder_Id = body.damagedOrder_Id;
 
    try {
      //如果预到货单存在  工单 则跳出
      let count = await this.dbRead.count(PurchaseDamagedOrder, {
        damagedOrder_Id: damagedOrder_Id,
        statusText: "已转预到货单"
      });
      if (count > 0) {
        this.info.msg = "残品已生成预到货订单,不允许重复操作!";
        this.info.result = false;
        ctx.body = this.info;
        return;
      }
      let orderCode = await ctx.service.common.getCodeRegular(102); //得到预到货单自动编号
      //#region 复制数据
      let sql = `
      --复制主表数据
      INSERT INTO Purchase_Order
      (OrderCode,Storage_Id,StorageName,Provider_Id, ProviderCode, ProviderShortName, TotalQuantity,TotalMoney,
        User_Id,UserTrueName,Dept_Id,DeptName,
        StatusText,StatusID,Enable, Remark, CreateID, Creator, CreateDate,Consignor_Id,ConsignorCode,ConsignorName,
        SourceId,TrackingNumber,OrderType,PlatUser_Id,PlatUserCode,PlatUserName,PlatCorpName,UserProduct_Id,UserProductCode,UserProductAlias)
     SELECT   '${orderCode}', Storage_Id, StorageName,
      Provider_Id, ProviderCode,ProviderShortName, TotalQuantity,TotalMoney, 
      User_Id,UserTrueName,Dept_Id,DeptName,
      '新建', 1, 1 AS Enable, Remark, ${userInfo.user_Id} as CreateID, '${userInfo.userTrueName}' as Creator, getdate(),
      Consignor_Id,ConsignorCode,ConsignorName,damagedOrder_Id as SourceId,DamagedOrderCode as TrackingNumber,'残品预到货',
                PlatUser_Id,PlatUserCode,PlatUserName,PlatCorpName,UserProduct_Id,UserProductCode,UserProductAlias
      FROM Purchase_DamagedOrder
      WHERE damagedOrder_Id=${damagedOrder_Id};`;
 
      sql += `
      --获取主表主键ID
      declare  @order_Id int;     
      SELECT @order_Id = IDENT_CURRENT('Purchase_Order');`;
 
      sql += `
      --复制明细数据
      INSERT INTO Purchase_OrderList
                (Order_Id, Product_Id,ProductCode, ProductName, ProductModel,ProductSpec,RelationCode,
                  Quantity,PurchasePrice,PurchaseMoney,BatchNumber,ProduceDate,SmallUnit,Enable,Remark, CreateID, Creator, CreateDate)
      SELECT     @order_Id AS Order_Id, Product_Id,ProductCode, ProductName, ProductModel,ProductSpec,
                 RelationCode,Quantity,PurchasePrice,
                 PurchaseMoney,BatchOrder,ProductDate,SmallUnit,1 AS Enable,Remark,
              ${userInfo.user_Id} as CreateID, '${userInfo.userTrueName}' as Creator, getdate()
      FROM Purchase_DamagedOrderList
      WHERE damagedOrder_Id=${damagedOrder_Id};`;
 
      sql += "select @order_Id as order_Id";
      sql = "Begin Tran\r\n" + sql + "\r\n";
      sql += " If @@Error <> 0 RollBack Tran Else Commit Tran";
      let resultData = await this.dbWrite.query(sql);
      //#endregion
      let newOrder_Id = resultData[0].order_Id;
      if (newOrder_Id && newOrder_Id > 0) {
        await this.dbWrite.update(PurchaseDamagedOrder, damagedOrder_Id, {
          statusID: 3,
          statusText: "已转预到货单"
        });
 
        this.info.msg = "转入到预到货单成功!";
        this.info.result = true;
      } else {
        this.info.msg = "转入到预到货单失败!";
        this.info.result = false;
      }
    } catch (error) {
      this.info.result = false;
      this.info.msg = error.message;
    }
    ctx.body = this.info;
  }
  //#endregion
}