material.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const materialConst = require('../../const/material');
  10. const Ledger = require('../../lib/ledger');
  11. const billsFields = (function () {
  12. const cur = ['contract_qty', 'contract_tp', 'contract_expr', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp', 'postil'];
  13. const pre = ['pre_contract_qty', 'pre_contract_tp', 'pre_qc_qty', 'pre_qc_tp', 'pre_gather_qty', 'pre_gather_tp'];
  14. const end = ['end_contract_qty', 'end_contract_tp', 'end_qc_qty', 'end_qc_tp', 'end_gather_qty', 'end_gather_tp'];
  15. const final = ['final_tp', 'final_ratio'];
  16. const stageDgn = ['deal_dgn_qty1', 'deal_dgn_qty2', 'c_dgn_qty1', 'c_dgn_qty2'];
  17. const stage = cur.concat(pre, end, final);
  18. const stageEnd = pre.concat(end, final);
  19. const bgl = ['qc_bgl_code'];
  20. const leafXmj = ['leaf_xmj_id'];
  21. return {cur, pre, end, final, stageDgn, stage, stageEnd, bgl, leafXmj};
  22. })();
  23. const posFields = (function () {
  24. const cur = ['contract_qty', 'qc_qty', 'gather_qty', 'postil'];
  25. const pre = ['pre_contract_qty', 'pre_qc_qty', 'pre_gather_qty'];
  26. const end = ['end_contract_qty', 'end_qc_qty', 'end_gather_qty'];
  27. const final = ['final_ratio'];
  28. const stage = cur.concat(pre, end, final);
  29. const stageEnd = pre.concat(end, final);
  30. const bgl = ['qc_bgl_code'];
  31. return {cur, pre, end, final, stage, stageEnd, bgl};
  32. })();
  33. class ReportMemoryMaterial {
  34. constructor(ctx) {
  35. this.ctx = ctx;
  36. }
  37. _getNewPos(updateFields) {
  38. const helper = this.ctx.helper;
  39. return new Ledger.pos({
  40. id: 'id', ledgerId: 'lid',
  41. updateFields: ['contract_qty', 'qc_qty', 'postil'],
  42. calc: function (p) {
  43. p.pre_gather_qty = helper.add(p.pre_contract_qty, p.pre_qc_qty);
  44. p.gather_qty = helper.add(p.contract_qty, p.qc_qty);
  45. p.end_contract_qty = helper.add(p.pre_contract_qty, p.contract_qty);
  46. p.end_qc_qty = helper.add(p.pre_qc_qty, p.qc_qty);
  47. p.end_gather_qty = helper.add(p.pre_gather_qty, p.gather_qty);
  48. }
  49. });
  50. }
  51. _getNewBillsTree(calcFields) {
  52. return new Ledger.billsTree(this.ctx, {
  53. id: 'ledger_id',
  54. pid: 'ledger_pid',
  55. order: 'order',
  56. level: 'level',
  57. rootId: -1,
  58. keys: ['id', 'tender_id', 'ledger_id'],
  59. stageId: 'id',
  60. calcFields: calcFields || ['deal_tp', 'total_price', 'contract_tp', 'qc_tp', 'gather_tp', 'pre_contract_tp', 'pre_qc_tp', 'pre_gather_tp'],
  61. calc: function (node, helper) {
  62. if (node.children && node.children.length === 0) {
  63. node.pre_gather_qty = helper.add(node.pre_contract_qty, node.pre_qc_qty);
  64. node.gather_qty = helper.add(node.contract_qty, node.qc_qty);
  65. node.end_contract_qty = helper.add(node.pre_contract_qty, node.contract_qty);
  66. node.end_qc_qty = helper.add(node.pre_qc_qty, node.qc_qty);
  67. node.end_gather_qty = helper.add(node.pre_gather_qty, node.gather_qty);
  68. }
  69. node.pre_gather_tp = helper.add(node.pre_contract_tp, node.pre_qc_tp);
  70. node.gather_tp = helper.add(node.contract_tp, node.qc_tp);
  71. node.end_contract_tp = helper.add(node.pre_contract_tp, node.contract_tp);
  72. node.end_qc_tp = helper.add(node.pre_qc_tp, node.qc_tp);
  73. node.end_gather_tp = helper.add(node.pre_gather_tp, node.gather_tp);
  74. node.final_tp = helper.add(node.total_price, node.end_qc_tp);
  75. node.final_ratio = helper.mul(helper.div(node.end_gather_tp, node.final_tp, 4), 100);
  76. }
  77. });
  78. }
  79. _checkFieldsExist(source, check) {
  80. for (const s of source) {
  81. if (check.indexOf(s) >= 0) {
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. async getSelectMaterialAuditors(tid, material_order, fields) {
  88. await this.ctx.service.material.checkMaterial(tid, material_order);
  89. const auditors = await this.ctx.service.materialAudit.getFinalAuditGroup(this.ctx.material.id, this.ctx.material.curTimes);
  90. const user = await this.ctx.service.projectAccount.getDataById(this.ctx.material.user_id);
  91. const result = [{
  92. aid: user.id,
  93. name: user.name,
  94. company: user.company,
  95. role: user.role,
  96. mobile: user.mobile,
  97. telephone: user.telephone,
  98. sign_path: user.sign_path,
  99. opinion: user.opinion,
  100. end_time: auditors && auditors.length > 0 ? auditors[0].begin_time : null,
  101. sort: 0,
  102. }, ...auditors];
  103. return result;
  104. }
  105. async getMaterial(tender_id, material_order, fields) {
  106. const result = await this.ctx.service.material.getValidMaterials(tender_id);
  107. if (this._checkFieldsExist(fields, ['checked_time'])) {
  108. for (const r of result) {
  109. const auditors = await this.ctx.service.materialAudit.getFinalAuditGroup(r.id, r.curTimes || r.times);
  110. r.checked_time = !r.curTimes ? auditors[auditors.length - 1].end_time : null;
  111. }
  112. }
  113. return result;
  114. }
  115. _completeMaterialGl(materialGl) {
  116. const tTypeStr = [], mTypeStr = [];
  117. for (const t of materialConst.t_type) {
  118. tTypeStr[t.value] = t.text;
  119. }
  120. for (const m of materialConst.m_type) {
  121. mTypeStr[m.value] = m.text;
  122. }
  123. for (const gl of materialGl) {
  124. gl.tp = this.ctx.helper.mul(gl.quantity, gl.m_spread, 2);
  125. gl.t_type_str = tTypeStr[gl.t_type];
  126. gl.m_type_str = mTypeStr[gl.m_type];
  127. gl.end_tp = this.ctx.helper.add(gl.tp, gl.pre_tp);
  128. }
  129. }
  130. async getMaterialGl(tender_id, material_order, fields) {
  131. const materials = await this.ctx.service.material.getAllDataByCondition({
  132. where: {tid: tender_id},
  133. orders: [['order', 'desc']],
  134. });
  135. if (materials.length > 0) {
  136. let result, material;
  137. if (materials[0].order === material_order) {
  138. material = materials[0];
  139. if (material.is_stage_self) {
  140. const sql = 'SELECT msb.id, msb.tid, msb.mid, msb.ms_id, ms.sid, ms.`order` as s_order, mb.order, mb.t_type, mb.code, mb.name, mb.unit, mb.spec, mb.m_type,' +
  141. ' msb.quantity, mb.expr,' +
  142. ' mb.basic_price, mb.basic_times, ' +
  143. ' msb.msg_tp, msb.msg_times, msb.msg_spread, mb.m_up_risk, mb.m_down_risk, msb.m_spread, msb.m_tp, mb.pre_tp, msb.m_tax_tp, mb.tax_pre_tp, mb.origin, ' +
  144. ' msb.remark, msb.is_summary, mb.m_tax, mb.in_time' +
  145. ` FROM ${this.ctx.service.materialStageBills.tableName} msb` +
  146. ` LEFT JOIN ${this.ctx.service.materialBills.tableName} mb ON msb.mb_id = mb.id` +
  147. ' LEFT JOIN ' + this.ctx.service.materialStage.tableName + ' ms ON msb.ms_id = ms.id ' +
  148. ` WHERE msb.mid = ?` +
  149. ' ORDER By msb.ms_id, mb.order';
  150. result = await this.ctx.app.mysql.query(sql, [material.id]);
  151. } else {
  152. result = await this.ctx.service.materialBills.getAllDataByCondition({
  153. where: {tid: tender_id}
  154. });
  155. }
  156. } else {
  157. const material = this.ctx.helper._.find(materials, {order: material_order});
  158. if (!material) return [];
  159. if (material.is_stage_self) {
  160. const sql = 'SELECT msb.id, msb.tid, msb.mid, msb.ms_id, ms.sid, ms.`order` as s_order, mb.order, mb.t_type, mb.code, mb.name, mb.unit, mb.spec, mb.m_type,' +
  161. ' msb.quantity, mbh.expr,' +
  162. ' mb.basic_price, mb.basic_times, ' +
  163. ' msb.msg_tp, msb.msg_times, msb.msg_spread, mbh.m_up_risk, mbh.m_down_risk, msb.m_spread, msb.m_tp, mbh.pre_tp, msb.m_tax_tp, mbh.tax_pre_tp, mbh.origin, ' +
  164. ' msb.remark, msb.is_summary, mbh.m_tax, mb.in_time' +
  165. ` FROM ${this.ctx.service.materialStageBills.tableName} msb` +
  166. ' LEFT JOIN ' + this.ctx.service.materialBillsHistory.tableName + ' mbh ON msb.mb_id = mbh.mb_id' +
  167. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' mb ON msb.mb_id = mb.id ' +
  168. ' LEFT JOIN ' + this.ctx.service.materialStage.tableName + ' ms ON msb.ms_id = ms.id ' +
  169. ' WHERE msb.mid = ?'+
  170. ' ORDER By msb.ms_id, mb.order';
  171. result = await this.ctx.app.mysql.query(sql, [material.id]);
  172. } else {
  173. const sql = 'SELECT mb.id, mb.tid, mb.mid, mb.order, mb.t_type, mb.code, mb.name, mb.unit, mb.spec, mb.m_type,' +
  174. ' mbh.quantity, mbh.expr,' +
  175. ' mb.basic_price, mb.basic_times, ' +
  176. ' mbh.msg_tp, mbh.msg_times, mbh.msg_spread, mbh.m_up_risk, mbh.m_down_risk, mbh.m_spread, mbh.m_tp, mbh.pre_tp, mbh.m_tax_tp, mbh.tax_pre_tp, mbh.origin, ' +
  177. ' mb.remark, mb.is_summary, mbh.m_tax, mb.in_time' +
  178. ' FROM ' + this.ctx.service.materialBillsHistory.tableName + ' mbh ' +
  179. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' mb ON mbh.mb_id = mb.id ' +
  180. ' WHERE mbh.tid = ? And mbh.mid = ?'+
  181. ' ORDER By mb.order';
  182. result = await this.ctx.app.mysql.query(sql, [tender_id, material.id]);
  183. }
  184. }
  185. this._completeMaterialGl(result);
  186. return result;
  187. } else {
  188. return [];
  189. }
  190. }
  191. async getMaterialGlDetail(tender_id, material_order, fields) {
  192. const material = await this.ctx.service.material.getDataByCondition({tid: tender_id, order: material_order});
  193. return material ? await this.ctx.service.materialList.getMaterialData(tender_id, material.id) : [];
  194. }
  195. async getMaterialBills(tender_id, material_order, fields) {
  196. const material = await this.ctx.service.material.getDataByCondition({tid: tender_id, order: material_order});
  197. try {
  198. const billsData = await this.ctx.service.ledger.getData(tender_id);
  199. if (this._checkFieldsExist(fields, billsFields.stage)) {
  200. const curStage = await this.ctx.service.stageBills.getStagesData(tender_id, material.stage_id);
  201. this.ctx.helper.assignRelaData(billsData, [
  202. {data: curStage, fields: ['contract_qty', 'contract_tp', 'contract_expr', 'qc_qty', 'qc_tp', 'postil'], prefix: '', relaId: 'lid'}
  203. ]);
  204. }
  205. const billsTree = this._getNewBillsTree();
  206. billsTree.loadDatas(billsData);
  207. billsTree.calculateAll();
  208. return billsTree.getDatas([
  209. 'id', 'tender_id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf',
  210. 'code', 'b_code', 'name', 'unit', 'unit_price',
  211. 'deal_qty', 'deal_tp',
  212. 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'quantity', 'total_price',
  213. 'dgn_qty1', 'dgn_qty2',
  214. 'drawing_code', 'memo', 'node_type', 'is_tp',
  215. 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp', 'postil',
  216. 'sgfh_expr', 'sjcl_expr', 'qtcl_expr', 'contract_expr',
  217. ]);
  218. } catch(err) {
  219. this.ctx.helper.log(err);
  220. return [];
  221. }
  222. }
  223. async getMaterialPos(tender_id, material_order, fields) {
  224. const material = await this.ctx.service.material.getDataByCondition({tid: tender_id, order: material_order});
  225. try {
  226. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: tender_id }});
  227. if (this._checkFieldsExist(fields, posFields.stage)) {
  228. const curPosStage = await this.ctx.service.stagePos.getStagesData(tender_id, material.stage_id);
  229. this.ctx.helper.assignRelaData(posData, [
  230. {data: curPosStage, fields: ['contract_qty', 'qc_qty', 'contract_expr', 'postil'], prefix: '', relaId: 'pid'}
  231. ]);
  232. }
  233. const pos = this._getNewPos();
  234. pos.loadDatas(posData);
  235. pos.calculateAll();
  236. return pos.getDatas();
  237. } catch (err) {
  238. this.ctx.helper.log(err);
  239. return [];
  240. }
  241. }
  242. async _getMaterialStageGatherBills(tender_id, stage_id, stage_order, stageSelf) {
  243. const decimal = this.materialGatherBase.decimal;
  244. const billsData = this.ctx.helper.clone(this.materialGatherBase.billsData);
  245. const curStageBills = await this.ctx.service.stageBills.getStagesData(tender_id, stage_id);
  246. this.ctx.helper.assignRelaData(billsData, [
  247. { data: curStageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
  248. ]);
  249. const billsTree = this._getNewBillsTree();
  250. billsTree.loadDatas(billsData);
  251. billsTree.calculateAll();
  252. const posData = this.ctx.helper.clone(this.materialGatherBase.posData);
  253. const curStage = await this.ctx.service.stagePos.getStagesData(tender_id, stage_id);
  254. this.ctx.helper.assignRelaData(posData, [
  255. { data: curStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
  256. ]);
  257. const pos = this._getNewPos();
  258. pos.loadDatas(posData);
  259. pos.calculateAll();
  260. const gclGatherModel = require('../gcl_gather').gclGather;
  261. const gatherUtil = new gclGatherModel(this.ctx);
  262. gatherUtil.gatherObj(billsTree, pos);
  263. const materialGl = stageSelf
  264. ? this.materialGatherBase.materialGl.filter(x => { return x.sid === parseInt(stage_id); })
  265. : this.materialGatherBase.materialGl;
  266. const materialNotJoin = this.materialGatherBase.materialNotJoin;
  267. const helper = this.ctx.helper;
  268. for (const g of gatherUtil.gclList) {
  269. g.sid = stage_id;
  270. g.sorder = stage_order;
  271. g.jiacha = 0;
  272. for (const x of g.leafXmjs) {
  273. x.sid = stage_id;
  274. x.sorder = stage_order;
  275. x.jiacha = 0;
  276. const mnj = materialNotJoin.find(m => {
  277. return m.gcl_id === x.org_gcl_id && m.xmj_id === x.id && (x.mx_id && x.mx_id !== x.id ? x.mx_id === m.mx_id : true);
  278. });
  279. x.is_join = !mnj;
  280. if (mnj) continue;
  281. const list = materialGl.filter(gl => {
  282. return gl.gcl_id === x.org_gcl_id && gl.xmj_id === x.id && (x.mx_id && x.mx_id !== x.id ? x.mx_id === gl.mx_id : true);
  283. });
  284. for (const l of list) {
  285. x.jiacha = helper.add(x.jiacha, helper.mul(helper.mul(x.gather_qty, l.quantity), l.m_spread));
  286. }
  287. x.jiacha = helper.round(x.jiacha, decimal.tp);
  288. g.jiacha = helper.add(g.jiacha, x.jiacha);
  289. }
  290. }
  291. return [gatherUtil.gclList, gatherUtil.leafXmjs ];
  292. }
  293. async getMaterialGatherBills(tender_id, material_order) {
  294. const materials = await this.ctx.service.material.getAllDataByCondition({
  295. where: { tid: tender_id },
  296. orders: [['order', 'desc']],
  297. });
  298. if (materials.length === 0) return {};
  299. const material = await this.ctx.service.material.getDataByCondition({ tid: tender_id, order: material_order });
  300. this.materialGatherBase = {};
  301. this.materialGatherBase.decimal = material.decimal ? JSON.parse(material.decimal) : materialConst.decimal;
  302. try {
  303. // 获取基础数据
  304. this.materialGatherBase.billsData = await this.ctx.service.ledger.getData(tender_id);
  305. this.materialGatherBase.posData = await this.ctx.service.pos.getPosData({ tid: tender_id });
  306. if (material.is_stage_self) {
  307. this.materialGatherBase.materialGl = material_order === materials[0].order
  308. ? await this.ctx.service.materialList.getMaterialStageData(tender_id, material.id)
  309. : await this.ctx.service.materialList.getPreMaterialStageData(tender_id, material.id);
  310. } else {
  311. this.materialGatherBase.materialGl = material_order === materials[0].order
  312. ? await this.ctx.service.materialList.getMaterialData(tender_id, material.id)
  313. : await this.ctx.service.materialList.getPreMaterialData(tender_id, material.id);
  314. }
  315. this.materialGatherBase.materialNotJoin = await this.ctx.service.materialListNotjoin.getAllDataByCondition({ where: { mid: material.id } });
  316. const mem_material_gather_bills = [], mem_material_gather_xmj = [], mem_material_gather_gl = [];
  317. if (material.is_stage_self) {
  318. const stageIds = material.stage_id.split(',');
  319. const stageOrders = material.s_order.split(',');
  320. for (const [i, sid] of stageIds.entries()) {
  321. const [gclList, leafXmjs] = await this._getMaterialStageGatherBills(tender_id, sid, stageOrders[i], true);
  322. mem_material_gather_bills.push(...gclList);
  323. mem_material_gather_xmj.push(...leafXmjs);
  324. mem_material_gather_gl.push(this.materialGatherBase.materialGl);
  325. }
  326. } else {
  327. const [gclList, leafXmjs] = await this._getMaterialStageGatherBills(tender_id, material.stage_id, material.stage_order, false);
  328. mem_material_gather_bills.push(...gclList);
  329. mem_material_gather_xmj.push(...leafXmjs);
  330. mem_material_gather_gl.push(this.materialGatherBase.materialGl);
  331. }
  332. return {mem_material_gather_bills, mem_material_gather_xmj, mem_material_gather_gl};
  333. } catch (err) {
  334. this.ctx.log(err);
  335. return {};
  336. }
  337. }
  338. async getMaterialStage(tender_id, material_order, fields) {
  339. const material = await this.ctx.service.material.getDataByCondition({tid: tender_id, order: material_order});
  340. if (material.is_stage_self) {
  341. return await this.ctx.service.materialStage.getAllDataByCondition({ where: { mid: material.id } });
  342. } else {
  343. return [{
  344. id: -1, tid: material.id, mid: material.id, sid: material.stage_id, order: material.stage_order,
  345. m_tp: material.m_tp, m_tax_tp: material.m_tax_tp,
  346. }];
  347. }
  348. }
  349. }
  350. module.exports = ReportMemoryMaterial;