//#region import
|
import { default as BaseController } from "../baseController";
|
import * as sql from "mssql";
|
import { Post } from "egg-shell-decorators";
|
import { SaleOrderPrint } from "../../entity/outbound/manufacture/saleOrderPrint";
|
import { vSaleOrderPrintListGroup } from "../../entity/outbound/manufacture/vSaleOrderPrintListGroup";
|
import { SaleOrder } from "../../entity/outbound/sale/saleOrder";
|
//#endregion
|
|
/**
|
* 闪电发货校验
|
*/
|
export default class OrderSendBatchController extends BaseController {
|
//#region GetData
|
/**
|
* 获取波次下的订单数据
|
*/
|
@Post()
|
public async getData() {
|
let userInfo = await this.userInfo;
|
let { ctx } = this;
|
let body = ctx.request.body;
|
|
let sql = `
|
SELECT L.order_Id, L.orderCode,L.expressCorpName, O.statusText,
|
SUM(L.quantityOrder) AS totalQuantityOrder,
|
SUM(O.weight) AS weight
|
FROM dbo.Sale_OrderPrint M INNER JOIN dbo.Sale_OrderPrintList L ON M.orderPrint_Id=L.orderPrint_Id
|
INNER JOIN dbo.Sale_Order O ON O.order_Id = L.order_Id
|
WHERE M.orderPrintCode=@0 And M.userProduct_Id=@1
|
AND O.statusText IN('波次完成', '拣货中','等待配货', '配货中', '等待打包', '部分打包', '部分发运')
|
GROUP BY L.order_Id, L.orderCode, L.expressCorpName, O.statusText;`;
|
|
var table = await this.dbRead.query(sql, [body.orderPrintCode, userInfo.userProduct_Id]);
|
this.info.data = table;
|
this.info.result = true;
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 保存闪电发货
|
/**
|
* 保存闪电发货
|
*/
|
@Post()
|
public async saveScan() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
let orderPrintCode = body.orderPrintCode;
|
let dataList = body.dataList;
|
|
try {
|
for (var model of dataList) {
|
let orderCode = model.orderCode;
|
//#region 验证
|
if (!orderPrintCode) {
|
this.info.msg = "波次不能为空!";
|
ctx.body = this.info;
|
return;
|
}
|
if (!orderCode) {
|
this.info.msg = "快递单号不能为空!";
|
ctx.body = this.info;
|
return;
|
}
|
let orderPrint = await this.dbRead.findOne(SaleOrderPrint, {
|
orderPrintCode: body.orderPrintCode,
|
userProduct_Id: userInfo.userProduct_Id
|
});
|
if (orderPrint == null) {
|
this.info.msg = "波次不存在!";
|
ctx.body = this.info;
|
return;
|
}
|
let where = "orderPrint_Id=:orderPrint_Id AND (expressCode=:expressCode OR orderCode=:expressCode)";
|
let printList = await this.dbRead
|
.createQueryBuilder(vSaleOrderPrintListGroup, "t")
|
.where(where, {
|
orderPrint_Id: orderPrint.orderPrint_Id,
|
expressCode: orderCode
|
})
|
.getMany();
|
if (printList == null) {
|
this.info.msg = "该波次下不存在该快递单!";
|
ctx.body = this.info;
|
return;
|
}
|
//#endregion
|
|
for (let printDetail of printList) {
|
if ((printDetail.quantityOuter || 0) - (printDetail.quantityOrder || 0) - (printDetail.freezeQuanity || 0) >= 0) {
|
this.info.msg = "该订单已经打包完成!";
|
ctx.body = this.info;
|
return;
|
}
|
if ((printDetail.quantityOuter || 0) > 0) {
|
this.info.msg = "该订单已经部分打包,不能使用完全出库,请继续使用部分打包!";
|
ctx.body = this.info;
|
return;
|
}
|
// let outerModel = await this.dbRead.findOne(SaleOuter, {
|
// expressCode: orderCode
|
// });
|
// if (outerModel != null) {
|
// this.info.msg = "该快递单号[" + orderCode + "]在[打包校验]已经使用过!";
|
// ctx.body = this.info;
|
// return;
|
// }
|
//验证耗材是否正确
|
let wrapperBarcode = model.wrapperBarcode;
|
if (wrapperBarcode) {
|
var wrapperBarcodeArray = wrapperBarcode.split("\n").filter(item => item);
|
if (wrapperBarcodeArray.Length > 0) {
|
let sql = `SELECT TOP 1 v.col FROM [dbo].[Func_SplitStrToTable]${wrapperBarcodeArray.Join(",")}) AS v
|
WHERE NOT EXISTS(
|
SELECT * FROM dbo.vBase_ProductInfo_SKU WHERE ProductModel=v.col AND userProduct_Id=${userInfo.userProduct_Id}
|
)`;
|
let notExistsSku = await this.dbRead.query(sql);
|
if (notExistsSku.length) {
|
this.info.msg = "包材[" + notExistsSku[0].col + "]在物料信息中不存在,请先维护好基础数据!";
|
ctx.body = this.info;
|
return;
|
}
|
}
|
}
|
let orderInfo = await this.dbRead.findOne(SaleOrder, printDetail.order_Id);
|
if (!orderInfo) {
|
this.info.msg = "波次中的明细订单在销售订单中已经不存在了,订单ID:" + printDetail.order_Id;
|
ctx.body = this.info;
|
return;
|
}
|
let weight = model.weight || orderInfo.weight || body.weight;
|
|
//#region 快速发货
|
let outerCode = await ctx.service.common.getCodeRegular(268);
|
|
const connection: any = await this.dbWrite.connection;
|
let request = new sql.Request(connection.driver.master);
|
request.input("outerCode", outerCode);
|
request.input("order_Id", orderInfo.order_Id);
|
request.input("wrapperBarcode", wrapperBarcode);
|
request.input("user_Id", userInfo.user_Id);
|
request.input("weight", weight);
|
request.input("orderPrint_Id", orderPrint.orderPrint_Id);
|
request.input("wayBillCode", await ctx.service.common.getCodeRegular(499));
|
request.output("outMsg", sql.NVarChar(2000));
|
let result = await request.execute("sp_Sale_SendFast");
|
let outMsg = result.output.outMsg;
|
|
var newOrderInfo = await this.dbRead.findOne(SaleOrder, {
|
select: ["statusID"],
|
where: {
|
order_Id: printDetail.order_Id
|
}
|
});
|
await ctx.service.outbound.orderHelper.setStatusHistory(
|
orderInfo.order_Id,
|
orderInfo.statusID,
|
newOrderInfo.statusID,
|
"订单状态",
|
"闪电发货"
|
);
|
if (body.driver) {
|
await this.dbWrite.update(SaleOrder, orderInfo.order_Id, {
|
driver: body.driver
|
});
|
}
|
|
if (outMsg) {
|
this.info.result = false;
|
this.info.msg = outMsg;
|
} else {
|
this.info.result = true;
|
this.info.msg = orderPrint.orderPrint_Id + "," + orderInfo.order_Id;
|
//扫描成功生成揽收信息
|
await ctx.service.tms.wayBillHelper.createWayBillReceive(orderCode, weight);
|
}
|
//#endregion
|
}
|
}
|
this.info.msg = "发货成功!";
|
this.info.result = true;
|
} catch (error) {
|
this.info.msg = "发货失败:" + error.message;
|
this.info.result = false;
|
}
|
|
ctx.body = this.info;
|
}
|
//#endregion
|
}
|