import BaseService from "../../service/baseService";
|
import * as mssql from "mssql";
|
import { Post } from "egg-shell-decorators";
|
import { FeePayableBill } from "../../entity/finance/fee/feePayableBill";
|
import { In } from "typeorm";
|
import { FeeStatement } from "../../entity/finance/fee/feeStatement";
|
import { FeePayableBillDetail } from "../../entity/finance/fee/feePayableBillDetail";
|
import { FeeStatementDetail } from "../../entity/finance/fee/feeStatementDetail";
|
|
export default class PayableBillController extends BaseService {
|
//#region
|
@Post()
|
public async storageSettle() {
|
let { ctx } = this;
|
let userInfo = await ctx.helper.userInfo();
|
|
try {
|
const connection: any = await this.dbWrite.connection;
|
let request = new mssql.Request(connection.driver.master);
|
|
request.input("UserProduct_Id", userInfo.userProduct_Id);
|
request.output("outMsg", mssql.NVarChar(2000));
|
let result = await request.execute("sp_Fee_Base_Storagefee");
|
let outMsg = result.output.outMsg;
|
|
if (outMsg) {
|
this.info.msg = outMsg;
|
this.info.result = false;
|
} else {
|
this.info.msg = "执行完成";
|
this.info.result = true;
|
}
|
} catch (error) {
|
this.info.msg = error.message;
|
this.info.result = false;
|
}
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
//#region 生成对账单
|
@Post()
|
public async billGenerat() {
|
let { ctx } = this;
|
let userInfo = await this.userInfo;
|
let body = ctx.body;
|
let ids = body.ids;
|
if (!Array.isArray(ids)) {
|
this.info.result = false;
|
this.info.msg = "没有选择可执行的数据";
|
return;
|
}
|
try {
|
// 获取主表信息 Fee_PayableBill
|
let payableBillList = await this.dbRead.find(FeePayableBill, {
|
where: {
|
payableBill_Id: In(ids)
|
}
|
});
|
// 根据仓库和商家进行分组
|
let groupList: Array<FeePayableBill> = payableBillList.reduce(
|
(all: Array<any>, next) =>
|
all.some(item => item.storage_Id == next.storage_Id && item.consignor_Id == next.consignor_Id) ? all : [...all, next],
|
[]
|
);
|
if (groupList.length > 1) {
|
this.info.result = false;
|
this.info.msg = "仓库和商家必须一致才可以生成对账单";
|
ctx.body = this.info;
|
return;
|
}
|
|
// 分组后的数据进行保存
|
for (let groupInfo of groupList) {
|
var totalReceivable = 0;
|
for (let payList of payableBillList) {
|
totalReceivable += payList.totalReceivable;
|
}
|
// 保存主表
|
let code = await this.ctx.service.common.getCodeRegular(1786); // 获取对账单编号
|
let statementInfo = new FeeStatement();
|
statementInfo.statementCode = code;
|
statementInfo.billCompany = null;
|
statementInfo.consignorName = groupInfo.consignorName;
|
statementInfo.consignorCode = groupInfo.consignorCode;
|
statementInfo.consignor_Id = groupInfo.consignor_Id;
|
statementInfo.createDate = new Date();
|
statementInfo.createID = userInfo.user_Id;
|
statementInfo.creator = userInfo.userTrueName;
|
statementInfo.endData = groupInfo.storageName;
|
statementInfo.invoiceNo = groupInfo.storageName;
|
statementInfo.isBilling = 0;
|
statementInfo.isSettlement = 0;
|
statementInfo.remark = groupInfo.storageName;
|
statementInfo.settlementRemark = groupInfo.storageName;
|
statementInfo.starData = null;
|
statementInfo.statType = groupInfo.billType;
|
statementInfo.statement_Id = 0;
|
statementInfo.statusText = "新建";
|
statementInfo.storageName = groupInfo.storageName;
|
statementInfo.storage_Id = groupInfo.storage_Id;
|
statementInfo.totalMoney = totalReceivable;
|
// aaa = groupInfo.totalReceivable;
|
// statementInfo.totalMoney = groupInfo.totalReceivable + aaa;
|
|
// 设置账套信息
|
await this.setAccountInfo(statementInfo);
|
await this.dbWrite.save(statementInfo);
|
|
let groupIds = payableBillList
|
.filter(item => item.storage_Id == groupInfo.storage_Id && item.consignor_Id == groupInfo.consignor_Id)
|
.map(item => item.payableBill_Id); // [1,3,4]
|
// 处理明细
|
let detailList = await this.dbRead.find(FeePayableBillDetail, {
|
payableBill_Id: In(groupIds)
|
});
|
for (let detailInfo of detailList) {
|
let statementDetailInfo = new FeeStatementDetail();
|
statementDetailInfo.statement_Id = statementInfo.statement_Id;
|
statementDetailInfo.billType = groupInfo.billType;
|
statementDetailInfo.chargeMode = null;
|
statementDetailInfo.consignorCode = groupInfo.consignorCode;
|
statementDetailInfo.consignorName = groupInfo.consignorName;
|
statementDetailInfo.consignor_Id = groupInfo.consignor_Id;
|
|
statementDetailInfo.endTime = detailInfo.endDate;
|
statementDetailInfo.feeItemCode = detailInfo.feeItemCode;
|
statementDetailInfo.feeItemName = detailInfo.feeItemName;
|
statementDetailInfo.feeItem_Id = detailInfo.feeItem_Id;
|
statementDetailInfo.freeDays = detailInfo.freeDays;
|
statementDetailInfo.freeMoney = detailInfo.freeMoney;
|
|
statementDetailInfo.paid = null;
|
statementDetailInfo.payMode = detailInfo.payMode;
|
statementDetailInfo.payStatus = detailInfo.payStatus;
|
statementDetailInfo.payableBillCode = groupInfo.payableBillCode;
|
statementDetailInfo.payableBillCreateDate = new Date();
|
statementDetailInfo.payableBillDetail_Id = detailInfo.payableBillDetail_Id;
|
statementDetailInfo.payableBill_Id = detailInfo.payableBill_Id;
|
statementDetailInfo.receivable = detailInfo.receivable;
|
statementDetailInfo.remark = detailInfo.remark;
|
statementDetailInfo.settlementMode = detailInfo.payType;
|
statementDetailInfo.startTime = detailInfo.beginDate;
|
statementDetailInfo.storageName = groupInfo.storageName;
|
statementDetailInfo.storage_Id = groupInfo.storage_Id;
|
statementDetailInfo.storagefee_Id = null;
|
statementDetailInfo.tagWeight_T = null;
|
statementDetailInfo.typeName = groupInfo.typeName;
|
statementDetailInfo.type_Id = null;
|
statementDetailInfo.unitPrice = detailInfo.feeRate;
|
statementDetailInfo.weight = detailInfo.weight;
|
statementDetailInfo.qty = detailInfo.qty;
|
|
// 保存明细
|
await this.dbWrite.save(statementDetailInfo);
|
}
|
}
|
if (!payableBillList.length) {
|
this.info.result = false;
|
this.info.msg = "数据不存在";
|
ctx.body = this.info;
|
return;
|
}
|
this.info.result = true;
|
this.info.msg = `账单生成成功`;
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + error.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
}
|