//#region import
|
import { default as BaseController } from "../baseController";
|
import * as sql from "mssql";
|
import { Post } from "egg-shell-decorators";
|
import { SaleOrder } from "../../entity/outbound/sale/saleOrder";
|
import { In } from "typeorm";
|
import { vBaseProductPlaceHolder } from "../../entity/storage/storage/vBaseProductPlaceHolder";
|
import { SaleOrderList } from "../../entity/outbound/sale/saleOrderList";
|
//#endregion
|
|
/**
|
* 出库 - 生成批次
|
*/
|
export default class OrderBatchController extends BaseController {
|
//#region 预览或者保存 Handle
|
/**
|
* 预览或者保存
|
*/
|
@Post()
|
public async createOrderPrint() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
|
try {
|
let finalWhere = "";
|
let opName = this.getOpName(body.op);
|
//#region 生成where条件
|
switch (body.op) {
|
case "order":
|
finalWhere = await this.getOrderWhere(body.where, body.selectIDs);
|
break;
|
}
|
//#endregion
|
|
//将数据保存到临时表
|
this.info = await this.previewTmp(finalWhere, body.op, opName, body.count);
|
if (this.info.result && body.type == "save") {
|
this.info = await this.save();
|
}
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region getOpName
|
private getOpName(op): string {
|
let opName = "";
|
switch (op) {
|
case "diqu":
|
opName = "地区";
|
break;
|
case "qudao":
|
opName = "渠道";
|
break;
|
case "kuqu":
|
opName = "库区";
|
break;
|
case "huozhu":
|
opName = "货主";
|
break;
|
case "danpin":
|
opName = "单品";
|
break;
|
case "danjian":
|
opName = "单件";
|
break;
|
case "duopin":
|
opName = "多品";
|
break;
|
case "kuaidi":
|
opName = "快递";
|
break;
|
case "order":
|
opName = "订单";
|
break;
|
default:
|
opName = "无";
|
break;
|
}
|
return opName;
|
}
|
//#endregion
|
|
//#region getOrderWhere
|
/**
|
* 将选中的订单生成波次
|
* 2016/3/1
|
*/
|
private async getOrderWhere(where, orderIds) {
|
let { ctx } = this;
|
let userInfo = await ctx.helper.userInfo();
|
|
if (!userInfo.isAdministrator) {
|
if (where) where += " And ";
|
where += " consignor_Id in(SELECT Node_Id FROM dbo.Sys_RoleAuthData where DataType_Id=1 AND user_Id=" + userInfo.user_Id + ")";
|
}
|
if (where) where += " And ";
|
where += " Order_Id IN(" + orderIds + ")";
|
return where;
|
}
|
//#endregion
|
|
//#region 根据条件把数据存入到临时表
|
/**
|
* 先根据条件把波次单数据创建到临时表(通用)
|
*/
|
private async previewTmp(where, op, opName, orderCount: number) {
|
let { ctx } = this;
|
let userInfo = await ctx.helper.userInfo();
|
let sessionId = userInfo.user_Id;
|
|
const connection: any = await this.dbWrite.connection;
|
let request = new sql.Request(connection.driver.master);
|
request.input("where", where);
|
request.input("sessionId", sessionId);
|
request.input("user_Id", userInfo.user_Id);
|
request.input("op", op);
|
request.input("opName", opName);
|
request.input("orderCount", orderCount);
|
request.output("msg", sql.NVarChar(2000));
|
let result = await request.execute("sp_SaleOrderBatch_SaveTmpData");
|
let msg = result.output.msg;
|
|
if (msg) {
|
this.info.result = false;
|
this.info.msg = msg;
|
} else {
|
this.info.result = true;
|
}
|
return this.info;
|
}
|
//#endregion
|
|
//#region 保存-通过sessionid读取临时表中的数据
|
private async save() {
|
let { ctx } = this;
|
let userInfo = await ctx.helper.userInfo();
|
let sessionId = userInfo.user_Id;
|
|
const connection: any = await this.dbWrite.connection;
|
let request = new sql.Request(connection.driver.master);
|
request.input("sessionId", sessionId);
|
request.input("user_Id", userInfo.user_Id);
|
request.input("subBatch", this.body.subBatch);
|
request.output("msg", sql.NVarChar(2000));
|
let result = await request.execute("sp_SaleOrderBatch_Save");
|
let msg = result.output.msg;
|
|
this.info.result = true;
|
this.info.msg = "生成成功,波次单号:[" + msg + "]";
|
return this.info;
|
}
|
//#endregion
|
|
//#region getDropdownList 获得下拉框数据
|
@Post()
|
public async getDropdownList() {
|
let userInfo = await this.userInfo;
|
let fixedWhere = this.body.fixedWhere;
|
let where = await this.ctx.service.common.parseWhere(fixedWhere);
|
where = Object.assign(where, {
|
userProduct_Id: userInfo.userProduct_Id,
|
sortingStatus: 2,
|
statusText: "审核成功"
|
});
|
let orderList = await this.dbRead.find(SaleOrder, {
|
select: [
|
"order_Id",
|
"storage_Id",
|
"storageName",
|
"consignor_Id",
|
"consignorCode",
|
"consignorName",
|
"clientShortName",
|
"expressCorp_Id",
|
"expressCode",
|
"expressCorpName",
|
"orderChannel",
|
"shippingName",
|
"mobile",
|
"provinceName",
|
"cityName",
|
"regionName"
|
],
|
where: where
|
});
|
// 仓库分组
|
let storageList = orderList.reduce(
|
(all: Array<SaleOrder>, next) => (all.some(item => item.storage_Id === next.storage_Id) ? all : [...all, next]),
|
[]
|
);
|
// 货主分组
|
let consignorList = orderList.reduce(
|
(all: Array<SaleOrder>, next) => (all.some(item => item.consignor_Id === next.consignor_Id) ? all : [...all, next]),
|
[]
|
);
|
let ids = orderList.map(item => item.order_Id);
|
if (!ids.length) {
|
ids = [0];
|
}
|
|
// 获得占位库区
|
let holderList = await this.dbRead.find(vBaseProductPlaceHolder, {
|
select: ["areaCode"],
|
where: {
|
userProduct_Id: userInfo.userProduct_Id,
|
mainID: In(ids)
|
}
|
});
|
// // 库区分组
|
let storageAreaList = holderList.reduce(
|
(all: Array<vBaseProductPlaceHolder>, next) => (all.some(item => item.areaCode === next.areaCode) ? all : [...all, next]),
|
[]
|
);
|
|
// 客户分组
|
let clientList = orderList.reduce(
|
(all: Array<SaleOrder>, next) => (all.some(item => item.clientShortName === next.clientShortName) ? all : [...all, next]),
|
[]
|
);
|
|
// 快递公司分组
|
let expressList = orderList.reduce(
|
(all: Array<SaleOrder>, next) => (all.some(item => item.expressCorp_Id === next.expressCorp_Id) ? all : [...all, next]),
|
[]
|
);
|
|
// 渠道分组
|
let channelList = orderList.reduce(
|
(all: Array<SaleOrder>, next) => (all.some(item => item.orderChannel === next.orderChannel) ? all : [...all, next]),
|
[]
|
);
|
|
// 收货人分组
|
let shippingNameList = orderList.reduce(
|
(all: Array<SaleOrder>, next) => (all.some(item => item.shippingName === next.shippingName) ? all : [...all, next]),
|
[]
|
);
|
|
// 电话分组
|
let mobileList = orderList.reduce((all: Array<SaleOrder>, next) => (all.some(item => item.mobile === next.mobile) ? all : [...all, next]), []);
|
|
// 省分组
|
let provinceList = orderList.reduce(
|
(all: Array<SaleOrder>, next) => (all.some(item => item.provinceName === next.provinceName) ? all : [...all, next]),
|
[]
|
);
|
|
// 市分组
|
let cityList = orderList.reduce((all: Array<SaleOrder>, next) => (all.some(item => item.cityName === next.cityName) ? all : [...all, next]), []);
|
|
// 区分组
|
let regionList = orderList.reduce(
|
(all: Array<SaleOrder>, next) => (all.some(item => item.regionName === next.regionName) ? all : [...all, next]),
|
[]
|
);
|
|
// 获得物料数据
|
let productList = await this.dbRead.find(SaleOrderList, {
|
select: ["product_Id", "productCode", "productModel", "productName", "order_Id", "quantityOrder"],
|
where: {
|
order_Id: In(ids)
|
}
|
});
|
|
// 物料名称
|
let productNameList = productList.reduce(
|
(all: Array<SaleOrderList>, next) => (all.some(item => item.productName === next.productName) ? all : [...all, next]),
|
[]
|
);
|
|
// 物料编号分组
|
let productCodeList = productList.reduce(
|
(all: Array<SaleOrderList>, next) => (all.some(item => item.productCode === next.productCode) ? all : [...all, next]),
|
[]
|
);
|
|
// 明细订单分组
|
let qtyList = productList.reduce(
|
(all: Array<SaleOrderList>, next) => (all.some(item => item.order_Id === next.order_Id) ? all : [...all, next]),
|
[]
|
);
|
|
// 物料条码分组
|
let productModelList = productList.reduce(
|
(all: Array<SaleOrderList>, next) => (all.some(item => item.productModel === next.productModel) ? all : [...all, next]),
|
[]
|
);
|
|
// 获得每个订单数量
|
let detailQtyList = qtyList.map(item => {
|
let total = productList
|
.filter(row => row.order_Id === item.order_Id)
|
.reduce(
|
(totalItem: any, currItem) => {
|
totalItem.totalQuantityOrder += currItem.quantityOrder;
|
return totalItem;
|
},
|
{ totalQuantityOrder: 0 }
|
);
|
return {
|
totalQuantityOrder: total.totalQuantityOrder
|
};
|
});
|
// 获得每个订单明细条数
|
let detailCountList = qtyList.map(item => {
|
let count = productList.filter(row => row.order_Id === item.order_Id).length;
|
return {
|
count: count
|
};
|
});
|
|
// 构建下拉框数据
|
let dropdownList: any = {
|
// 仓库
|
storageList: storageList.map(item => {
|
return {
|
value: item.storage_Id,
|
label: item.storageName
|
};
|
}),
|
// 货主
|
consignorList: consignorList.map((item: SaleOrder) => {
|
return {
|
value: item.consignor_Id,
|
label: item.consignorName
|
};
|
}),
|
// 库区
|
storageAreaList: storageAreaList.map(item => {
|
return {
|
value: item.areaCode,
|
label: item.areaCode
|
};
|
}),
|
// 客户
|
clientList: clientList.map(item => {
|
return {
|
value: item.clientShortName,
|
label: item.clientShortName
|
};
|
}),
|
// 快递公司
|
expressList: expressList.map(item => {
|
return {
|
value: item.expressCorp_Id,
|
label: item.expressCorpName
|
};
|
}),
|
// 快递公司
|
channelList: channelList.map(item => {
|
return {
|
value: item.orderChannel,
|
label: item.orderChannel
|
};
|
}),
|
// 收货人
|
shippingNameList: shippingNameList.map(item => {
|
return {
|
value: item.shippingName,
|
label: item.shippingName
|
};
|
}),
|
// 电话
|
mobileList: mobileList.map(item => {
|
return {
|
value: item.mobile,
|
label: item.mobile
|
};
|
}),
|
// 省
|
provinceList: provinceList.map(item => {
|
return {
|
value: item.provinceName,
|
label: item.provinceName
|
};
|
}),
|
// 市
|
cityList: cityList.map(item => {
|
return {
|
value: item.cityName,
|
label: item.cityName
|
};
|
}),
|
// 区
|
regionList: regionList.map(item => {
|
return {
|
value: item.regionName,
|
label: item.regionName
|
};
|
}),
|
// 物料名称
|
productNameList: productNameList.map(item => {
|
return {
|
value: item.productName,
|
label: item.productName
|
};
|
}),
|
// 物料编号
|
productCodeList: productCodeList.map(item => {
|
return {
|
value: item.productCode,
|
label: item.productCode
|
};
|
}),
|
// 物料条码
|
productModelList: productModelList.map(item => {
|
return {
|
value: item.productModel,
|
label: item.productModel
|
};
|
}),
|
// 物料数量
|
detailQtyList: detailQtyList
|
.reduce((all: Array<any>, next) => (all.some(item => item.totalQuantityOrder == next.totalQuantityOrder) ? all : [...all, next]), [])
|
.map(item => {
|
return {
|
value: item.totalQuantityOrder,
|
label: item.totalQuantityOrder
|
};
|
}),
|
// 物料条数
|
detailCountList: detailCountList
|
.reduce((all: Array<any>, next) => (all.some(item => item.count == next.count) ? all : [...all, next]), [])
|
.map(item => {
|
return {
|
value: item.count,
|
label: item.count
|
};
|
})
|
};
|
|
this.info.result = true;
|
this.info.data = dropdownList;
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 获得子波次数据
|
/**
|
* 获得子波次数据
|
*/
|
@Post()
|
public async getSubBatch() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
|
try {
|
let sql = `SELECT S.sub_Id, S.subOrderPrintCode, S.orderPrint_Id, S.orderPrintList_Id, S.Remark,
|
S.createID, S.creator, S.createDate, S.areaCode, L.allotPositionName,
|
L.positionName, L.product_Id, L.productCode, L.productName, L.productModel, L.productSpec, L.quantityOrder,
|
L.storeOrderCode, L.orderCode
|
FROM Sale_OrderPrintSub AS S INNER JOIN
|
Sale_OrderPrintList AS L ON S.OrderPrintList_Id = L.OrderPrintList_Id
|
where L.orderPrint_Id=@0`;
|
let dataList = await this.dbRead.query(sql, [body.orderPrint_Id]);
|
|
this.info.data = dataList;
|
this.info.result = true;
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
}
|