material.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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;
  137. if (materials[0].order === material_order) {
  138. const material = materials[0];
  139. if (material.is_stage_self) {
  140. const sql = 'SELECT msb.id, msb.tid, msb.mid, msb.ms_id, 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. ` WHERE msb.mid = ?` +
  148. ' ORDER By msb.ms_id, mb.order';
  149. result = await this.ctx.app.mysql.query(sql, [material.id]);
  150. } else {
  151. result = await this.ctx.service.materialBills.getAllDataByCondition({
  152. where: {tid: tender_id}
  153. });
  154. }
  155. } else {
  156. const material = this.ctx.helper._.find(materials, {order: material_order});
  157. if (!material) return [];
  158. if (material.is_stage_self) {
  159. const sql = 'SELECT msb.id, msb.tid, msb.mid, msb.ms_id, mb.order, mb.t_type, mb.code, mb.name, mb.unit, mb.spec, mb.m_type,' +
  160. ' msb.quantity, mbh.expr,' +
  161. ' mb.basic_price, mb.basic_times, ' +
  162. ' 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, ' +
  163. ' msb.remark, msb.is_summary, mbh.m_tax, mb.in_time' +
  164. ` FROM ${this.ctx.service.materialStageBills.tableName} msb` +
  165. ' LEFT JOIN ' + this.ctx.service.materialBillsHistory.tableName + ' mbh ON msb.mb_id = mbh.mb_id' +
  166. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' mb ON msb.mb_id = mb.id ' +
  167. ' WHERE msb.mid = ?'+
  168. ' ORDER By msb.ms_id, mb.order';
  169. result = await this.ctx.app.mysql.query(sql, [material.id]);
  170. } else {
  171. 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,' +
  172. ' mbh.quantity, mbh.expr,' +
  173. ' mb.basic_price, mb.basic_times, ' +
  174. ' 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, ' +
  175. ' mb.remark, mb.is_summary, mbh.m_tax, mb.in_time' +
  176. ' FROM ' + this.ctx.service.materialBillsHistory.tableName + ' mbh ' +
  177. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' mb ON mbh.mb_id = mb.id ' +
  178. ' WHERE mbh.tid = ? And mbh.mid = ?'+
  179. ' ORDER By mb.order';
  180. result = await this.ctx.app.mysql.query(sql, [tender_id, material.id]);
  181. }
  182. }
  183. this._completeMaterialGl(result);
  184. return result;
  185. } else {
  186. return [];
  187. }
  188. }
  189. async getMaterialGlDetail(tender_id, material_order, fields) {
  190. const material = await this.ctx.service.material.getDataByCondition({tid: tender_id, order: material_order});
  191. return material ? await this.ctx.service.materialList.getMaterialData(tender_id, material.id) : [];
  192. }
  193. async getMaterialBills(tender_id, material_order, fields) {
  194. const material = await this.ctx.service.material.getDataByCondition({tid: tender_id, order: material_order});
  195. try {
  196. const billsData = await this.ctx.service.ledger.getData(tender_id);
  197. if (this._checkFieldsExist(fields, billsFields.stage)) {
  198. const curStage = await this.ctx.service.stageBills.getStagesData(tender_id, material.stage_id);
  199. this.ctx.helper.assignRelaData(billsData, [
  200. {data: curStage, fields: ['contract_qty', 'contract_tp', 'contract_expr', 'qc_qty', 'qc_tp', 'postil'], prefix: '', relaId: 'lid'}
  201. ]);
  202. }
  203. const billsTree = this._getNewBillsTree();
  204. billsTree.loadDatas(billsData);
  205. billsTree.calculateAll();
  206. return billsTree.getDatas([
  207. 'id', 'tender_id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf',
  208. 'code', 'b_code', 'name', 'unit', 'unit_price',
  209. 'deal_qty', 'deal_tp',
  210. 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'quantity', 'total_price',
  211. 'dgn_qty1', 'dgn_qty2',
  212. 'drawing_code', 'memo', 'node_type', 'is_tp',
  213. 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp', 'postil',
  214. 'sgfh_expr', 'sjcl_expr', 'qtcl_expr', 'contract_expr',
  215. ]);
  216. } catch(err) {
  217. this.ctx.helper.log(err);
  218. return [];
  219. }
  220. }
  221. async getMaterialPos(tender_id, material_order, fields) {
  222. const material = await this.ctx.service.material.getDataByCondition({tid: tender_id, order: material_order});
  223. try {
  224. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: tender_id }});
  225. if (this._checkFieldsExist(fields, posFields.stage)) {
  226. const curPosStage = await this.ctx.service.stagePos.getStagesData(tender_id, material.stage_id);
  227. this.ctx.helper.assignRelaData(posData, [
  228. {data: curPosStage, fields: ['contract_qty', 'qc_qty', 'contract_expr', 'postil'], prefix: '', relaId: 'pid'}
  229. ]);
  230. }
  231. const pos = this._getNewPos();
  232. pos.loadDatas(posData);
  233. pos.calculateAll();
  234. return pos.getDatas();
  235. } catch (err) {
  236. this.ctx.helper.log(err);
  237. return [];
  238. }
  239. }
  240. async _getMaterialStageGatherBills(tender_id, stage_id, stage_order) {
  241. const decimal = this.materialGatherBase.decimal;
  242. const billsData = this.ctx.helper.clone(this.materialGatherBase.billsData);
  243. const curStageBills = await this.ctx.service.stageBills.getStagesData(tender_id, stage_id);
  244. this.ctx.helper.assignRelaData(billsData, [
  245. { data: curStageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
  246. ]);
  247. const billsTree = this._getNewBillsTree();
  248. billsTree.loadDatas(billsData);
  249. billsTree.calculateAll();
  250. const posData = this.ctx.helper.clone(this.materialGatherBase.posData);
  251. const curStage = await this.ctx.service.stagePos.getStagesData(tender_id, stage_id);
  252. this.ctx.helper.assignRelaData(posData, [
  253. { data: curStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
  254. ]);
  255. const pos = this._getNewPos();
  256. pos.loadDatas(posData);
  257. pos.calculateAll();
  258. const gclGatherModel = require('../gcl_gather').gclGather;
  259. const gatherUtil = new gclGatherModel(this.ctx);
  260. gatherUtil.gatherObj(billsTree, pos);
  261. const materialGl = this.materialGatherBase.materialGl;
  262. const materialNotJoin = this.materialGatherBase.materialNotJoin;
  263. const helper = this.ctx.helper;
  264. for (const g of gatherUtil.gclList) {
  265. g.sid = stage_id;
  266. g.sorder = stage_order;
  267. g.jiacha = 0;
  268. for (const x of g.leafXmjs) {
  269. x.sid = stage_id;
  270. x.sorder = stage_order;
  271. x.jiacha = 0;
  272. const mnj = materialNotJoin.find(m => {
  273. 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);
  274. });
  275. x.is_join = !mnj;
  276. if (mnj) continue;
  277. const list = materialGl.filter(g => {
  278. return g.gcl_id === x.org_gcl_id && g.xmj_id === x.id && (x.mx_id && x.mx_id !== x.id ? x.mx_id === g.mx_id : true);
  279. });
  280. for (const l of list) {
  281. x.jiacha = helper.add(x.jiacha, helper.mul(helper.mul(x.gather_qty, l.quantity), l.m_spread));
  282. }
  283. x.jiacha = helper.round(x.jiacha, decimal.tp);
  284. g.jiacha = helper.add(g.jiacha, x.jiacha);
  285. }
  286. }
  287. return [gatherUtil.gclList, gatherUtil.leafXmjs ];
  288. }
  289. async getMaterialGatherBills(tender_id, material_order) {
  290. const materials = await this.ctx.service.material.getAllDataByCondition({
  291. where: { tid: tender_id },
  292. orders: [['order', 'desc']],
  293. });
  294. if (materials.length === 0) return {};
  295. const material = await this.ctx.service.material.getDataByCondition({ tid: tender_id, order: material_order });
  296. this.materialGatherBase = {};
  297. this.materialGatherBase.decimal = material.decimal ? JSON.parse(material.decimal) : materialConst.decimal;
  298. try {
  299. // 获取基础数据
  300. this.materialGatherBase.billsData = await this.ctx.service.ledger.getData(tender_id);
  301. this.materialGatherBase.posData = await this.ctx.service.pos.getPosData({ tid: tender_id });
  302. this.materialGatherBase.materialGl = material_order === materials[0].order
  303. ? await this.ctx.service.materialList.getMaterialData(tender_id, material.id)
  304. : await this.ctx.service.materialList.getPreMaterialData(tender_id, material.id);
  305. this.materialGatherBase.materialNotJoin = await this.ctx.service.materialListNotjoin.getAllDataByCondition({ where: { mid: material.id } });
  306. const mem_material_gather_bills = [], mem_material_gather_xmj = [];
  307. if (material.is_stage_self) {
  308. const stageIds = material.stage_id.split(',');
  309. const stageOrders = material.s_order.split(',');
  310. for (const [i, sid] of stageIds.entries()) {
  311. const [gclList, leafXmjs] = await this._getMaterialStageGatherBills(tender_id, sid, stageOrders[i]);
  312. mem_material_gather_bills.push(...gclList);
  313. mem_material_gather_xmj.push(...leafXmjs);
  314. }
  315. } else {
  316. const [gclList, leafXmjs] = await this._getMaterialStageGatherBills(tender_id, material.stage_id, material.stage_order);
  317. mem_material_gather_bills.push(...gclList);
  318. mem_material_gather_xmj.push(...leafXmjs);
  319. }
  320. return {mem_material_gather_bills, mem_material_gather_xmj, mem_material_gather_gl: this.materialGatherBase.materialGl};
  321. } catch (err) {
  322. this.ctx.log(err);
  323. return {};
  324. }
  325. }
  326. async getMaterialStage(tender_id, material_order, fields) {
  327. const material = await this.ctx.service.material.getDataByCondition({tid: tender_id, order: material_order});
  328. if (material.is_stage_self) {
  329. return await this.ctx.service.materialStage.getAllDataByCondition({ where: { mid: material.id } });
  330. } else {
  331. return [{
  332. id: -1, tid: material.id, mid: material.id, sid: material.stage_id, order: material.stage_order,
  333. m_tp: material.m_tp, m_tax_tp: material.m_tax_tp,
  334. }];
  335. }
  336. }
  337. }
  338. module.exports = ReportMemoryMaterial;