material.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. result = await this.ctx.service.materialBills.getAllDataByCondition({
  139. where: {tid: tender_id}
  140. });
  141. } else {
  142. const material = this.ctx.helper._.find(materials, {order: material_order});
  143. if (!material) return [];
  144. const sql = 'SELECT mb.id, mb.tid, mb.mid, mb.order, mb.order, mb.t_type, mb.code, mb.name, mb.unit, mb.spec, mb.m_type,' +
  145. ' mbh.quantity, mbh.expr,' +
  146. ' mb.basic_price, mb.basic_times, ' +
  147. ' 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, ' +
  148. ' mb.remark, mb.is_summary, mbh.m_tax, mb.in_time' +
  149. ' FROM ' + this.ctx.service.materialBillsHistory.tableName + ' mbh ' +
  150. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' mb ON mbh.mb_id = mb.id ' +
  151. ' WHERE mbh.tid = ? And mbh.mid = ?';
  152. result = await this.ctx.app.mysql.query(sql, [tender_id, material.id]);
  153. }
  154. this._completeMaterialGl(result);
  155. return result;
  156. } else {
  157. return [];
  158. }
  159. }
  160. async getMaterialGlDetail(tender_id, material_order, fields) {
  161. const material = await this.ctx.service.material.getDataByCondition({tid: tender_id, order: material_order});
  162. return material ? await this.ctx.service.materialList.getMaterialData(tender_id, material.id) : [];
  163. }
  164. async getMaterialBills(tender_id, material_order, fields) {
  165. const material = await this.ctx.service.material.getDataByCondition({tid: tender_id, order: material_order});
  166. try {
  167. const billsData = await this.ctx.service.ledger.getData(tender_id);
  168. if (this._checkFieldsExist(fields, billsFields.stage)) {
  169. const curStage = await this.ctx.service.stageBills.getStagesData(tender_id, material.stage_id);
  170. this.ctx.helper.assignRelaData(billsData, [
  171. {data: curStage, fields: ['contract_qty', 'contract_tp', 'contract_expr', 'qc_qty', 'qc_tp', 'postil'], prefix: '', relaId: 'lid'}
  172. ]);
  173. }
  174. const billsTree = this._getNewBillsTree();
  175. billsTree.loadDatas(billsData);
  176. billsTree.calculateAll();
  177. return billsTree.getDatas([
  178. 'id', 'tender_id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf',
  179. 'code', 'b_code', 'name', 'unit', 'unit_price',
  180. 'deal_qty', 'deal_tp',
  181. 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'quantity', 'total_price',
  182. 'dgn_qty1', 'dgn_qty2',
  183. 'drawing_code', 'memo', 'node_type', 'is_tp',
  184. 'contract_qty', 'contract_tp', 'qc_qty', 'qc_tp', 'gather_qty', 'gather_tp', 'postil',
  185. 'sgfh_expr', 'sjcl_expr', 'qtcl_expr', 'contract_expr',
  186. ]);
  187. } catch(err) {
  188. this.ctx.helper.log(err);
  189. return [];
  190. }
  191. }
  192. async getMaterialPos(tender_id, material_order, fields) {
  193. const material = await this.ctx.service.material.getDataByCondition({tid: tender_id, order: material_order});
  194. try {
  195. const posData = await this.ctx.service.pos.getAllDataByCondition({ where: {tid: tender_id }});
  196. if (this._checkFieldsExist(fields, posFields.stage)) {
  197. const curPosStage = await this.ctx.service.stagePos.getStagesData(tender_id, material.stage_id);
  198. this.ctx.helper.assignRelaData(posData, [
  199. {data: curPosStage, fields: ['contract_qty', 'qc_qty', 'contract_expr', 'postil'], prefix: '', relaId: 'pid'}
  200. ]);
  201. }
  202. const pos = this._getNewPos();
  203. pos.loadDatas(posData);
  204. pos.calculateAll();
  205. return pos.getDatas();
  206. } catch (err) {
  207. this.ctx.helper.log(err);
  208. return [];
  209. }
  210. }
  211. async getMaterialGatherBills(tender_id, material_order) {
  212. const materials = await this.ctx.service.material.getAllDataByCondition({
  213. where: { tid: tender_id },
  214. orders: [['order', 'desc']],
  215. });
  216. if (materials.length === 0) return {};
  217. const material = await this.ctx.service.material.getDataByCondition({ tid: tender_id, order: material_order });
  218. const decimal = material.decimal ? JSON.parse(material.decimal) : materialConst.decimal;
  219. try {
  220. const billsData = await this.ctx.service.ledger.getData(tender_id);
  221. const curStageBills = await this.ctx.service.stageBills.getStagesData(tender_id, material.stage_id);
  222. this.ctx.helper.assignRelaData(billsData, [
  223. { data: curStageBills, fields: ['contract_qty', 'contract_tp', 'qc_qty', 'qc_tp'], prefix: '', relaId: 'lid' },
  224. ]);
  225. const billsTree = this._getNewBillsTree();
  226. billsTree.loadDatas(billsData);
  227. billsTree.calculateAll();
  228. const posData = await this.ctx.service.pos.getPosData({ tid: tender_id });
  229. const curStage = await this.ctx.service.stagePos.getStagesData(tender_id, material.stage_id);
  230. this.ctx.helper.assignRelaData(posData, [
  231. { data: curStage, fields: ['contract_qty', 'qc_qty'], prefix: '', relaId: 'pid' },
  232. ]);
  233. const pos = this._getNewPos();
  234. pos.loadDatas(posData);
  235. pos.calculateAll();
  236. const gclGatherModel = require('../gcl_gather').gclGather;
  237. const gatherUtil = new gclGatherModel(this.ctx);
  238. gatherUtil.gatherObj(billsTree, pos);
  239. const materialGl = material_order === materials[0].order
  240. ? await this.ctx.service.materialList.getMaterialData(tender_id, material.id)
  241. : await this.ctx.service.materialList.getPreMaterialData(tender_id, material.id);
  242. const materialNotJoin = await this.ctx.service.materialListNotjoin.getAllDataByCondition({ where: { mid: material.id } });
  243. const helper = this.ctx.helper;
  244. for (const g of gatherUtil.gclList) {
  245. g.jiacha = 0;
  246. for (const x of g.leafXmjs) {
  247. x.jiacha = 0;
  248. const mnj = materialNotJoin.find(m => {
  249. 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);
  250. });
  251. if (mnj) continue;
  252. const list = materialGl.filter(g => {
  253. 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);
  254. });
  255. for (const l of list) {
  256. x.jiacha = helper.add(x.jiacha, helper.mul(helper.mul(x.gather_qty, l.quantity), l.m_spread));
  257. }
  258. x.jiacha = helper.round(x.jiacha, decimal.tp);
  259. g.jiacha = helper.add(g.jiacha, x.jiacha);
  260. }
  261. }
  262. return { mem_material_gather_bills: gatherUtil.gclList, mem_material_gather_xmj: gatherUtil.leafXmjs, mem_material_gather_gl: materialGl };
  263. } catch (err) {
  264. this.ctx.log(err);
  265. return {};
  266. }
  267. }
  268. }
  269. module.exports = ReportMemoryMaterial;