import * as sql from "mssql";
|
import { default as BaseController } from "../baseController";
|
import { Post } from "egg-shell-decorators";
|
import { PurchaseReturn } from "../../entity/inbound/purchase/purchaseReturn";
|
import { PurchaseReturnList } from "../../entity/inbound/purchase/purchaseReturnList";
|
import { PurchaseReturnStatusHistory } from "../../entity/inbound/purchase/purchaseReturnStatusHistory";
|
|
/**
|
* 收货 - 入库单
|
*/
|
export default class EnterController extends BaseController {
|
//#region getByCode
|
/**
|
* 根据采购退货单Code获取采购退货单信息,参数需要拼接在地址后,例如:/api/PurchaseReturn/GetByCode?returnCode=xxxxxx
|
*/
|
@Post()
|
public async getByCode() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
if (!body.orderCode) {
|
this.info.result = false;
|
this.info.msg = "查询单号不能为空";
|
return;
|
}
|
var model = await this.dbRead.findOne(PurchaseReturn, {
|
where: [{ returnCode: body.orderCode }, { orderCode: body.orderCode }],
|
relations: ["PurchaseReturnList"]
|
});
|
this.info.data = model;
|
this.info.result = true;
|
return this.info;
|
}
|
//#endregion
|
|
//#region 复制单据
|
@Post()
|
public async copySaleOrder() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
|
let return_Id = body.return_Id;
|
let returnCode = await ctx.service.common.getCodeRegular(104); //得到自动编号
|
|
try {
|
var returnInfo = await this.dbRead.findOne(PurchaseReturn, return_Id);
|
let returnNew = Object.assign({}, returnInfo);
|
|
returnNew.return_Id = 0;
|
returnNew.returnCode = returnCode;
|
returnNew.sortingStatus = 1;
|
returnNew.sortingDate = null;
|
returnNew.statusID = 1;
|
returnNew.statusText = "新建";
|
returnNew.auditing = 0;
|
returnNew.auditor = null;
|
returnNew.auditDate = null;
|
|
await this.dbWrite.insert(PurchaseReturn, returnNew);
|
|
//如果订单主表新增成功,在复制明细
|
if (returnNew.return_Id > 0) {
|
let returnDetails = await this.dbRead.find(PurchaseReturnList, {
|
return_Id: returnInfo.return_Id
|
});
|
for (var detail of returnDetails) {
|
let detailNew = Object.assign({}, detail);
|
detailNew.orderList_Id = 0;
|
detailNew.return_Id = returnNew.return_Id;
|
await this.dbWrite.insert(PurchaseReturnList, detailNew);
|
}
|
this.info.msg = "复制成功!";
|
this.info.result = true;
|
} else {
|
this.info.msg = "错误信息:复制失败!";
|
this.info.result = false;
|
}
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
|
ctx.body = this.info;
|
}
|
|
//#endregion
|
|
//#region 转到出库单
|
@Post()
|
public async toSaleOrder() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
|
try {
|
const connection: any = await this.dbWrite.connection;
|
let request = new sql.Request(connection.driver.master);
|
request.input("return_Id", body.return_Id);
|
request.input("user_Id", userInfo.user_Id);
|
request.input("userTrueName", userInfo.userTrueName);
|
request.output("outMsg", sql.NVarChar(2000));
|
let result = await request.execute("sp_Purchase_Return_ToSaleOrder");
|
let outMsg = result.output.outMsg;
|
|
if (outMsg != null && outMsg.ToString()) {
|
this.info.msg = outMsg.ToString();
|
this.info.result = false;
|
return this.info;
|
}
|
this.info.result = true;
|
this.info.msg = "转到出库单成功!";
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = error.message;
|
}
|
|
ctx.body = this.info;
|
}
|
|
//#endregion
|
|
//#region 退货货单审核
|
/**
|
* 退货货单审核
|
*/
|
@Post()
|
public async auditingReturn() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
let return_Ids = body.return_Ids;
|
if (!Array.isArray(return_Ids)) {
|
this.info.result = false;
|
this.info.msg = "没有可执行的数据";
|
ctx.body = this.info;
|
return;
|
}
|
|
try {
|
for (let return_Id of return_Ids) {
|
// 更新审核状态
|
let returnInfo = await this.dbRead.findOne(PurchaseReturn, return_Id);
|
await this.dbWrite.update(
|
PurchaseReturn,
|
{
|
return_Id: return_Id
|
},
|
{
|
statusID: 2,
|
auditor: userInfo.userTrueName,
|
auditing: 2,
|
auditDate: new Date(),
|
statusText: "审核成功"
|
}
|
);
|
|
// 记录审核退货单轨迹
|
await this.dbWrite.save(PurchaseReturnStatusHistory, {
|
return_Id: return_Id,
|
statusType: "单据状态",
|
operationType: "订单状态",
|
fromStatus: returnInfo.statusText,
|
toStatus: "审核成功",
|
createID: userInfo.user_Id,
|
creator: userInfo.userTrueName,
|
createDate: new Date()
|
});
|
|
this.info.result = true;
|
this.info.msg = "审核成功";
|
}
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
}
|