material_checklist.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. 'use strict';
  2. /**
  3. * 材料调差 - 调差清单设置
  4. *
  5. * @author EllisRan
  6. * @date 2022/1/7
  7. * @version
  8. */
  9. function getStageId() {
  10. return window.location.pathname.split('/')[5];
  11. }
  12. function findNotJoinLeafXmj(x, type = '') {
  13. if (type === 'index') {
  14. return notJoinList.findIndex(function (item) {
  15. return item.gcl_id === x.gcl_id && item.xmj_id === x.id && (x.mx_id === undefined || (x.mx_id !== undefined && x.mx_id === item.mx_id));
  16. });
  17. }
  18. return notJoinList.find(function (item) {
  19. return item.gcl_id === x.gcl_id && item.xmj_id === x.id && (x.mx_id === undefined || (x.mx_id !== undefined && x.mx_id === item.mx_id));
  20. });
  21. }
  22. function getPasteHint (str, row = '') {
  23. let returnObj = str;
  24. if (row) {
  25. returnObj.msg = '清单第' + (row+1) + '行' + (str.msg ? str.msg : str);
  26. }
  27. return returnObj;
  28. }
  29. function makeChecklistData(lists, checklists) {
  30. let html = '';
  31. if (lists.length > 0) {
  32. for(const [i,l] of lists.entries()) {
  33. const checklistInfo = _.find(checklists, { b_code: l.b_code, name: l.name, unit: l.unit, unit_price: l.unit_price });
  34. const isChecked = checklistInfo ? ' checked' : '';
  35. const isDisabled = checklistInfo && checklistInfo.had_bills === 1 ? ' disabled' : '';
  36. html += '<tr>\n' +
  37. ' <td><div class="text-center custom-control custom-checkbox mb-2">\n' +
  38. ' <input type="checkbox" id="lists_'+ i +'" value="'+ i +'" name="customCheckbox" class="custom-control-input"'+ isChecked + isDisabled +'>\n' +
  39. ' <label class="custom-control-label" for="lists_'+ i +'"></label>\n' +
  40. ' </div></td>\n' +
  41. ' <td class="text-center">'+ (i+1) +'</td>\n' +
  42. ' <td>'+ l.b_code +'</td>\n' +
  43. ' <td>'+ l.name +'</td>\n' +
  44. ' <td class="text-center">'+ (l.unit ? l.unit : '') +'</td>\n' +
  45. ' <td class="text-right">'+ (l.unit_price ? l.unit_price : '') +'</td>\n' +
  46. ' <td class="text-right">'+ (l.quantity ? l.quantity : '') +'</td>\n' +
  47. ' <td class="text-right">'+ (l.total_price ? l.total_price : '') +'</td>\n' +
  48. ' </tr>';
  49. }
  50. }
  51. $('#lists_data').html(html);
  52. }
  53. // 清单搜索隐藏清单table部分值
  54. function remakeChecklistData(lists, showListData = lists) {
  55. // 先加载台账数据
  56. if (lists.length > 0) {
  57. for (const [index,gcl] of lists.entries()) {
  58. const isShow = _.find(showListData, gcl);
  59. $('#lists_data tr').eq(index).css('display', (isShow ? 'table-row' : 'none'));
  60. }
  61. }
  62. }
  63. $(document).ready(() => {
  64. function TipCellType()
  65. {
  66. }
  67. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.ColumnHeader();
  68. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  69. return { x: x, y: y, row: context.row, col: context.col, cellRect: cellRect, sheetArea: context.sheetArea, sheet: context.sheet };
  70. };
  71. TipCellType.prototype.processMouseEnter = function (hitInfo){
  72. if (!this._toolTipElement) {
  73. var div = document.createElement("div");
  74. $(div).css("position", "absolute")
  75. .css("border", "1px #C0C0C0 solid")
  76. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  77. .css("font", "9pt Arial")
  78. .css("background", "#fff")
  79. // .css("color", "#fff")
  80. .css("z-index", "1000")
  81. .css("padding", 5);
  82. this._toolTipElement = div;
  83. }
  84. $(this._toolTipElement).text("单位数量:每一单位清单下所需工料消耗量。")
  85. .css("top", hitInfo.y + 15)
  86. .css("left", hitInfo.x - 15);
  87. $(this._toolTipElement).hide();
  88. // document.body.insertBefore(this._toolTipElement, null);
  89. $('#material-spread-div').append(this._toolTipElement, null);
  90. $(this._toolTipElement).show("fast");
  91. };
  92. TipCellType.prototype.processMouseLeave = function (hitInfo) {
  93. if (this._toolTipElement) {
  94. this._toolTipElement.remove();
  95. this._toolTipElement = null;
  96. }
  97. };
  98. autoFlashHeight();
  99. // 清单table
  100. const ledgerSpread = SpreadJsObj.createNewSpread($('#ledger-spread')[0]);
  101. const ledgerSpreadSetting = {
  102. cols: [
  103. {title: '清单编号', colSpan: '1', rowSpan: '2', field: 'b_code', hAlign: 0, width: 80, formatter: '@'},
  104. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 220, formatter: '@'},
  105. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 60, formatter: '@'},
  106. {title: '单价', colSpan: '1', rowSpan: '2', field: 'unit_price', hAlign: 2, width: 70, type: 'Number'},
  107. {title: '工程量', colSpan: '1', rowSpan: '2', field: 'quantity', hAlign: 2, width: 90, type: 'Number'},
  108. {title: '台账金额', colSpan: '1', rowSpan: '2', field: 'total_price', hAlign: 2, width: 110, type: 'Number'},
  109. ],
  110. emptyRows: 0,
  111. headRows: 1,
  112. headRowHeight: [25, 25],
  113. defaultRowHeight: 21,
  114. headerFont: '12px 微软雅黑',
  115. font: '12px 微软雅黑',
  116. readOnly: true,
  117. };
  118. SpreadJsObj.initSheet(ledgerSpread.getActiveSheet(), ledgerSpreadSetting);
  119. // 加载清单数据 - 暂时统一加载,如有需要,切换成动态加载并缓存
  120. postData(window.location.pathname + '/load', {}, function (result) {
  121. ledger = result.ledger;
  122. curLedgerData = result.curLedgerData;
  123. pos = result.pos;
  124. curPosData = result.curPosData;
  125. materialListData = result.materialListData;
  126. notJoinList = result.materialNotJoinListData;
  127. materialChecklistData = result.materialChecklistData;
  128. // 解析清单汇总数据
  129. gclGatherModel.loadLedgerData(ledger, curLedgerData);
  130. gclGatherModel.loadPosData(pos, curPosData);
  131. gclGatherData = gclGatherModel.gatherGclData();
  132. console.log(gclGatherData);
  133. const hadBillsidList = _.uniq(_.map(materialListData, 'gcl_id'));
  134. console.log(hadBillsidList);
  135. // 对比清单设置和调差清单,还要和台账对比,显示已选清单列表 不同则更新到清单设置页中
  136. const pushData = [];
  137. const updateData = [];
  138. for (const hb of hadBillsidList) {
  139. const gcl = _.find(gclGatherData, function (item) {
  140. return item.leafXmjs && item.leafXmjs.length > 0 && _.findIndex(item.leafXmjs, { gcl_id : hb }) !== -1;
  141. });
  142. if (gcl) {
  143. const mc = _.find(materialChecklistData, { b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price });
  144. // const newOrder = _.indexOf(gclGatherData, gcl);
  145. // console.log(newOrder);
  146. if (!mc && _.findIndex(pushData, { b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price }) === -1) {
  147. pushData.push({ b_code: gcl.b_code, name: gcl.name, unit: gcl.unit, unit_price: gcl.unit_price, quantity: (gcl.quantity ? gcl.quantity : null), total_price: (gcl.total_price ? gcl.total_price : null), had_bills: 1 });
  148. }
  149. }
  150. }
  151. const removeData = [];
  152. for (const mc of materialChecklistData) {
  153. const gcl = _.find(gclGatherData, { b_code: mc.b_code, name: mc.name, unit: mc.unit, unit_price: mc.unit_price });
  154. // 判断是否已不存在工料清单,台账修改过后删除之
  155. if (!gcl) {
  156. removeData.push(mc.id);
  157. } else {
  158. // 更新had_bills值
  159. const updateObj = { id: mc.id };
  160. if (mc.had_bills === 1) {
  161. if (_.indexOf(hadBillsidList, gcl.leafXmjs ? gcl.leafXmjs[0].gcl_id : null) === -1) {
  162. updateObj.mid = materialID;
  163. updateObj.had_bills = 0;
  164. // updateData.push({ id: mc.id, mid: materialID, had_bills: 0 });
  165. }
  166. }
  167. // 更新工程量及台账金额
  168. if (mc.quantity !== (gcl.quantity ? gcl.quantity : null)) {
  169. updateObj.quantity = gcl.quantity ? gcl.quantity : null;
  170. updateObj.total_price = gcl.total_price ? gcl.total_price : null;
  171. }
  172. if(!_.isEqual(updateObj,{ id: mc.id })) updateData.push(updateObj);
  173. }
  174. }
  175. console.log(pushData, removeData, updateData);
  176. setChecklistData(pushData, removeData, updateData, true);
  177. });
  178. function setChecklistData(pushData, removeData, updateData = [], sendmsg = false) {
  179. if (pushData.length > 0 || removeData.length > 0 || updateData.length > 0) {
  180. postData(window.location.pathname + '/save', { type: 'resetChecklist', pushData, removeData, updateData }, function (result2) {
  181. if (sendmsg && pushData.length > 0) {
  182. toastr.success('已同步历史调差清单数据至本页中');
  183. }
  184. if (sendmsg && removeData.length > 0) {
  185. toastr.warning('已删除部分与台账清单不匹配的清单数据');
  186. }
  187. materialChecklistData = result2;
  188. showSjsData();
  189. })
  190. } else {
  191. showSjsData();
  192. }
  193. }
  194. function showSjsData() {
  195. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialChecklistData);
  196. SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  197. if (materialChecklistData.length > 0) {
  198. const index = _.findIndex(gclGatherData, { b_code: materialChecklistData[0].b_code, name: materialChecklistData[0].name, unit: materialChecklistData[0].unit, unit_price: materialChecklistData[0].unit_price });
  199. loadMaterialData(index, 0);
  200. } else {
  201. loadMaterialData(-1, 0);
  202. }
  203. const sheet = materialSpread.getActiveSheet();
  204. sheet.suspendPaint();
  205. sheet.setCellType(1, 3, new TipCellType(), spreadNS.SheetArea.colHeader);
  206. sheet.resumePaint();
  207. }
  208. // 调差清单工料table
  209. const materialSpread = SpreadJsObj.createNewSpread($('#material-spread')[0]);
  210. const materialSpreadSetting = {
  211. cols: [
  212. {title: '清单工料含量|编号', colSpan: '5|1', rowSpan: '1|1', field: 'code', hAlign: 0, width: 80, formatter: '@', readOnly: true},
  213. {title: '|名称', colSpan: '|1', rowSpan: '|1', field: 'name', hAlign: 0, width: 100, formatter: '@', readOnly: true},
  214. {title: '|单位', colSpan: '|1', rowSpan: '|1', field: 'unit', hAlign: 1, width: 60, formatter: '@', readOnly: true},
  215. {title: '|数量 �', colSpan: '|1', rowSpan: '|1', field: 'quantity', hAlign: 2, width: 80, type: 'Number', readOnly: 'readOnly.isEdit'},
  216. {title: '|计算式', colSpan: '1', rowSpan: '|1', field: 'expr', hAlign: 2, width: 120, formatter: '@', readOnly: 'readOnly.isEdit'},
  217. ],
  218. emptyRows: 0,
  219. headRows: 2,
  220. headRowHeight: [25, 25],
  221. defaultRowHeight: 21,
  222. headerFont: '12px 微软雅黑',
  223. font: '12px 微软雅黑',
  224. };
  225. const materialBase = {
  226. isEdit: function (data) {
  227. // 是否本期添加的工料
  228. return data.order === stage_order;
  229. }
  230. };
  231. const materialCol = {
  232. readOnly: {
  233. isEdit: function (data) {
  234. return !(!readOnly && materialBase.isEdit(data));
  235. },
  236. },
  237. };
  238. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  239. // 获取项目节数据
  240. let materialList = [];
  241. function loadMaterialData(iGclRow, iLXmjRow) {
  242. const gcl = gclGatherData[iGclRow];
  243. const leafXmjs = gcl && gcl.leafXmjs ? gcl.leafXmjs.filter(item => {
  244. return item.qc_qty || item.contract_qty
  245. }) : null;
  246. if (leafXmjs) {
  247. const xmj = leafXmjs[iLXmjRow];
  248. materialList = [];
  249. materialList = _.filter(materialListData, function (m) {
  250. return m.gcl_id === xmj.gcl_id && m.xmj_id === xmj.id && ((xmj.mx_id !==undefined && m.mx_id === xmj.mx_id) || xmj.mx_id === undefined);
  251. });
  252. // for (const m of materialListData) {
  253. // if (m.gcl_id === xmj.gcl_id && m.xmj_id === xmj.id && ((xmj.mx_id !==undefined && m.mx_id === xmj.mx_id) || xmj.mx_id === undefined)) {
  254. // materialList.push(m);
  255. // }
  256. // }
  257. // 对清单调差工料table的单位数量进行改变
  258. materialSpreadSetting.cols[materialSpreadSetting.cols.length - 2].title = '|' + gcl.unit + '数量 �';
  259. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  260. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialList);
  261. } else {
  262. materialSpreadSetting.cols[materialSpreadSetting.cols.length - 2].title = '数量 �';
  263. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  264. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, []);
  265. }
  266. SpreadJsObj.resetTopAndSelect(materialSpread.getActiveSheet());
  267. }
  268. // 对添加工料表格赋值
  269. function changeMaterialTable() {
  270. $('#materialBills tr').removeClass('table-secondary');
  271. $('#materialBills').find('input').removeAttr('disabled');
  272. $('#materialBills').find('input').prop('checked', false);
  273. for (const ml of materialList) {
  274. const mbIndex = _.findIndex(materialBillsData, {id : ml.mb_id });
  275. if (mbIndex !== -1) {
  276. $('#materialBills tr').eq(mbIndex).addClass('table-secondary');
  277. $('#materialBills').find('input').eq(mbIndex).attr('disabled', true);
  278. $('#materialBills').find('input').eq(mbIndex).prop('checked', true);
  279. }
  280. }
  281. }
  282. // 选中清单并添加
  283. $('#set_checklist_btn').click(function () {
  284. const select_checklist = $('#lists_data').find('input:checked:not(:disabled)');
  285. const pushData = [];
  286. for (const sc of select_checklist) {
  287. const order = parseInt($(sc).val());
  288. const checklistInfo = _.find(materialChecklistData, { b_code: gclGatherData[order].b_code, name: gclGatherData[order].name, unit: gclGatherData[order].unit, unit_price: gclGatherData[order].unit_price });
  289. if (!checklistInfo) {
  290. pushData.push({
  291. b_code: gclGatherData[order].b_code,
  292. name: gclGatherData[order].name,
  293. unit: gclGatherData[order].unit,
  294. unit_price: gclGatherData[order].unit_price,
  295. quantity: gclGatherData[order].quantity ? gclGatherData[order].quantity : null,
  296. total_price: gclGatherData[order].total_price ? gclGatherData[order].total_price : null,
  297. had_bills: 0,
  298. })
  299. }
  300. }
  301. const notSelect_checklist = $('#lists_data').find('input:not(:checked):not(:disabled)');
  302. const removeData = [];
  303. for (const nsc of notSelect_checklist) {
  304. const order = parseInt($(nsc).val());
  305. // const order = parseInt($(nsc).attr('data-index'));
  306. const checklistInfo = _.find(materialChecklistData, { b_code: gclGatherData[order].b_code, name: gclGatherData[order].name, unit: gclGatherData[order].unit, unit_price: gclGatherData[order].unit_price });
  307. if (checklistInfo) {
  308. removeData.push(checklistInfo.id);
  309. }
  310. }
  311. setChecklistData(pushData, removeData);
  312. $('#addtclist').modal('hide');
  313. });
  314. // 筛选无调差工料清单
  315. $('#notBills_checkList').click(function () {
  316. const isCheck = $(this).is(':checked');
  317. let newMaterialChecklistData = materialChecklistData;
  318. if (isCheck) {
  319. $('#bills0_checkList').prop('checked', false);
  320. newMaterialChecklistData = _.filter(materialChecklistData, { had_bills: 0 });
  321. }
  322. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialChecklistData);
  323. SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  324. if (newMaterialChecklistData.length > 0) {
  325. const index = _.findIndex(gclGatherData, { b_code: newMaterialChecklistData[0].b_code, name: newMaterialChecklistData[0].name, unit: newMaterialChecklistData[0].unit, unit_price: newMaterialChecklistData[0].unit_price });
  326. loadMaterialData(index, 0);
  327. } else {
  328. loadMaterialData(-1, 0);
  329. }
  330. });
  331. // 筛选调差工料清单为0
  332. $('#bills0_checkList').click(function () {
  333. const isCheck = $(this).is(':checked');
  334. let newMaterialChecklistData = materialChecklistData;
  335. if (isCheck) {
  336. newMaterialChecklistData = [];
  337. $('#notBills_checkList').prop('checked', false);
  338. const materialList0 = _.uniq(_.map(_.filter(materialListData, { quantity: 0 }), 'gcl_id'));
  339. if (materialList0.length > 0) {
  340. const hadMaterialChecklistData = _.filter(materialChecklistData, { had_bills: 1 });
  341. for (const h of hadMaterialChecklistData) {
  342. const gcl = _.find(gclGatherData, { b_code: h.b_code, name: h.name, unit: h.unit, unit_price: h.unit_price });
  343. if (gcl && gcl.leafXmjs.length > 0 && _.indexOf(materialList0, gcl.leafXmjs[0].gcl_id) !== -1) {
  344. newMaterialChecklistData.push(h);
  345. }
  346. }
  347. }
  348. }
  349. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialChecklistData);
  350. SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  351. if (newMaterialChecklistData.length > 0) {
  352. const index = _.findIndex(gclGatherData, { b_code: newMaterialChecklistData[0].b_code, name: newMaterialChecklistData[0].name, unit: newMaterialChecklistData[0].unit, unit_price: newMaterialChecklistData[0].unit_price });
  353. loadMaterialData(index, 0);
  354. } else {
  355. loadMaterialData(-1, 0);
  356. }
  357. });
  358. // 添加调差工料
  359. $('#add_material_bill').click(function () {
  360. // 获取已选工料
  361. $('#materialBills').find('input:disabled').prop('checked', false);
  362. const selectList = $('#materialBills').find('input:checked');
  363. if (selectList.length === 0) {
  364. toastr.warning('请选择调差工料');
  365. $('#materialBills').find('input:disabled').prop('checked', true);
  366. return false;
  367. }
  368. const mb_id = [];
  369. for (let s = 0; s < selectList.length; s++) {
  370. mb_id.push($('#materialBills').find('input:checked').eq(s).val());
  371. }
  372. // 获取当前项目节或部位明细id
  373. const sheet = ledgerSpread.getActiveSheet();
  374. const select = SpreadJsObj.getSelectObject(sheet);
  375. const gclIndex = _.findIndex(gclGatherData, { b_code: select.b_code, name: select.name, unit: select.unit, unit_price: select.unit_price });
  376. const gcl = gclGatherData[gclIndex].leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined);
  377. const index = materialChecklistData.indexOf(select);
  378. const datas = [];
  379. for (const xmj of gcl) {
  380. const notx = findNotJoinLeafXmj(xmj);
  381. const data = {
  382. xmj_id: xmj.id,
  383. gcl_id: xmj.gcl_id,
  384. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  385. gather_qty: xmj.gather_qty,
  386. is_join: notx === undefined ? 1 : 0,
  387. };
  388. datas.push(data);
  389. }
  390. // 上传到数据库
  391. console.log(datas, gcl);
  392. postData(window.location.pathname + '/save', {type: 'adds', checklist: { id: select.id, had_bills: 1 }, postData: {xmjs: datas, mbIds: mb_id}}, function (result) {
  393. materialListData = result;
  394. materialChecklistData[index].had_bills = 1;
  395. loadMaterialData(gclIndex, 0);
  396. // SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), index);
  397. $('#addgl').modal('hide');
  398. });
  399. $('#materialBills').find('input:disabled').prop('checked', true);
  400. });
  401. if (!readOnly) {
  402. // material-spread右键功能
  403. const materialSpreadObj = {
  404. del: function () {
  405. const materialSheet = materialSpread.getActiveSheet();
  406. const materialSelect = SpreadJsObj.getSelectObject(materialSheet);
  407. const sheet = ledgerSpread.getActiveSheet();
  408. const select = SpreadJsObj.getSelectObject(sheet);
  409. const index = materialChecklistData.indexOf(select);
  410. const gclIndex = _.findIndex(gclGatherData, { b_code: select.b_code, name: select.name, unit: select.unit, unit_price: select.unit_price });
  411. const gcl = gclGatherData[gclIndex].leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined);
  412. const datas = [];
  413. for (const xmj of gcl) {
  414. const data = {
  415. xmj_id: xmj.id,
  416. gcl_id: xmj.gcl_id,
  417. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  418. };
  419. datas.push(data);
  420. }
  421. const xmj = gcl[0];
  422. const materialCount = _.size(_.filter(materialListData, function (m) {
  423. return m.gcl_id === xmj.gcl_id && m.xmj_id === xmj.id && ((xmj.mx_id !==undefined && m.mx_id === xmj.mx_id) || xmj.mx_id === undefined);
  424. }));
  425. let checklist = false;
  426. if (materialCount === 1) {
  427. checklist = {
  428. id: select.id,
  429. had_bills: 0,
  430. }
  431. }
  432. console.log(datas, materialSelect.mb_id, checklist);
  433. postData(window.location.pathname + '/save', {type: 'dels', checklist, postData: { xmjs: datas, mb_id: materialSelect.mb_id }}, function (result) {
  434. materialListData = result;
  435. if (checklist) materialChecklistData[index].had_bills = checklist.had_bills;
  436. loadMaterialData(gclIndex, 0);
  437. // SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), index);
  438. });
  439. },
  440. deletePress: function (sheet) {
  441. return;
  442. },
  443. editStarting: function (e, info) {
  444. const col = info.sheet.zh_setting.cols[info.col];
  445. const select = SpreadJsObj.getSelectObject(info.sheet);
  446. if (col.field === 'quantity') {
  447. if (select.expr && select.expr !== '') {
  448. info.sheet.getCell(info.row, info.col).text(select.expr);
  449. }
  450. }
  451. },
  452. editEnded: function (e, info) {
  453. if (info.sheet.zh_setting) {
  454. const select = SpreadJsObj.getSelectObject(info.sheet);
  455. const col = info.sheet.zh_setting.cols[info.col];
  456. const validText = info.editingText ? info.editingText.replace('\n', '') : null;
  457. let orgValue;
  458. if (col.field === 'quantity') {
  459. orgValue = validText && validText !== ''
  460. ? _.toNumber(validText) ? select.quantity : select.expr
  461. : (select.expr && select.expr !== '') ? select.expr : select.quantity;
  462. } else {
  463. orgValue = select[col.field];
  464. }
  465. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === '' || validText === null))) {
  466. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  467. return;
  468. }
  469. const exprQuantity = {
  470. expr: '',
  471. quantity: 0,
  472. };
  473. const [valid, msg] = materialSpreadObj._checkExpr(validText, exprQuantity);
  474. if (!valid) {
  475. toastr.error(msg);
  476. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  477. return;
  478. }
  479. if (isNaN(exprQuantity.quantity)) {
  480. toastr.error('不能输入其它非数字类型字符');
  481. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  482. return;
  483. }
  484. const num = parseFloat(exprQuantity.quantity);
  485. if (num < 0 || !/^\d+(\.\d{1,6})?$/.test(num)) {
  486. toastr.warning('已保留6位小数');
  487. exprQuantity.quantity = ZhCalc.round(num, 6);
  488. }
  489. // 更新至服务器
  490. const ledgerSheet = ledgerSpread.getActiveSheet();
  491. const ledgerSelect = SpreadJsObj.getSelectObject(ledgerSheet);
  492. const gclIndex = _.findIndex(gclGatherData, { b_code: ledgerSelect.b_code, name: ledgerSelect.name, unit: ledgerSelect.unit, unit_price: ledgerSelect.unit_price });
  493. const gcl = gclGatherData[gclIndex].leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined);
  494. const datas = [];
  495. for (const xmj of gcl) {
  496. const data = {
  497. xmj_id: xmj.id,
  498. gcl_id: xmj.gcl_id,
  499. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  500. };
  501. datas.push(data);
  502. }
  503. console.log(exprQuantity, datas, select.mb_id);
  504. postData(window.location.pathname + '/save', { type:'updates', updateData: { xmjs: datas, expr: exprQuantity.expr, quantity: exprQuantity.quantity, mb_id: select.mb_id } }, function (result) {
  505. materialListData = result;
  506. loadMaterialData(gclIndex, 0);
  507. materialSpread.getActiveSheet().setSelection(info.row + 1, info.col, 1, 1);
  508. }, function () {
  509. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  510. });
  511. }
  512. },
  513. clipboardPasted(e, info) {
  514. const hint = {
  515. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  516. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  517. numberCan: {type: 'warning', msg: '已保留6位小数'},
  518. };
  519. const range = info.cellRange;
  520. const sortData = info.sheet.zh_data || [];
  521. if (range.row + range.rowCount > sortData.length) {
  522. toastMessageUniq(hint.cellError);
  523. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  524. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  525. return;
  526. }
  527. if (sortData.length > 0 && range.col + range.colCount > 5) {
  528. toastMessageUniq(hint.cellError);
  529. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  530. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  531. return;
  532. }
  533. const data = [];
  534. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  535. let bPaste = true;
  536. const curRow = range.row + iRow;
  537. const materialData = { id: sortData[curRow].id, mb_id: sortData[curRow].mb_id };
  538. const hintRow = range.rowCount > 1 ? curRow : '';
  539. let sameCol = 0;
  540. for (let iCol = 0; iCol < range.colCount; iCol++) {
  541. const curCol = range.col + iCol;
  542. const colSetting = info.sheet.zh_setting.cols[curCol];
  543. if (!colSetting) continue;
  544. const validText = info.sheet.getText(curRow, curCol).replace('\n', '');
  545. const orgValue = sortData[curRow][colSetting.field];
  546. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  547. sameCol++;
  548. if (range.colCount === sameCol) {
  549. bPaste = false;
  550. }
  551. continue;
  552. }
  553. const exprQuantity = {
  554. expr: '',
  555. quantity: 0,
  556. };
  557. const [valid, msg] = materialSpreadObj._checkExpr(validText, exprQuantity);
  558. if (!valid) {
  559. toastMessageUniq(getPasteHint(msg, hintRow));
  560. bPaste = false;
  561. continue;
  562. }
  563. if (isNaN(exprQuantity.quantity)) {
  564. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  565. bPaste = false;
  566. continue;
  567. }
  568. const num = parseFloat(exprQuantity.quantity);
  569. if (num < 0 || !/^\d+(\.\d{1,6})?$/.test(num)) {
  570. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  571. exprQuantity.quantity = ZhCalc.round(num, 6);
  572. }
  573. materialData.expr = exprQuantity.expr;
  574. materialData.quantity = exprQuantity.quantity;
  575. }
  576. if (bPaste) {
  577. data.push(materialData);
  578. } else {
  579. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  580. }
  581. }
  582. if (data.length === 0) {
  583. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  584. return;
  585. }
  586. const ledgerSheet = ledgerSpread.getActiveSheet();
  587. const ledgerSelect = SpreadJsObj.getSelectObject(ledgerSheet);
  588. const gclIndex = _.findIndex(gclGatherData, { b_code: ledgerSelect.b_code, name: ledgerSelect.name, unit: ledgerSelect.unit, unit_price: ledgerSelect.unit_price });
  589. const gcl = gclGatherData[gclIndex].leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined);
  590. const datas = [];
  591. for (const xmj of gcl) {
  592. const data2 = {
  593. xmj_id: xmj.id,
  594. gcl_id: xmj.gcl_id,
  595. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  596. };
  597. datas.push(data2);
  598. }
  599. console.log(data, datas);
  600. // 更新至服务器
  601. postData(window.location.pathname + '/save', { type:'pastes', updateData: { xmjs: datas, pasteData: data } }, function (result) {
  602. materialListData = result;
  603. loadMaterialData(gclIndex, 0);
  604. materialSpread.getActiveSheet().setSelection(info.cellRange.row, info.cellRange.col, info.cellRange.rowCount, info.cellRange.colCount);
  605. }, function () {
  606. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  607. });
  608. },
  609. _checkExprValid(expr) {
  610. if (!expr) return [true, null];
  611. const param = [];
  612. let num = '', base = '';
  613. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  614. if (/^[\d\.%]+/.test(expr[i])) {
  615. if (base !== '') {
  616. param.push({type: 'base', value: base});
  617. base = '';
  618. }
  619. num = num + expr[i];
  620. } else if (expr[i] === '(') {
  621. if (num !== '') {
  622. param.push({type: 'num', value: num});
  623. num = '';
  624. }
  625. if (base !== '') {
  626. param.push({type: 'base', value: base});
  627. base = '';
  628. }
  629. param.push({type: 'left', value: '('});
  630. } else if (expr[i] === ')') {
  631. if (num !== '') {
  632. param.push({type: 'num', value: num});
  633. num = '';
  634. }
  635. if (base !== '') {
  636. param.push({type: 'base', value: base});
  637. base = '';
  638. }
  639. param.push({type: 'right', value: ')'});
  640. } else if (/^[\+\-*\/]/.test(expr[i])) {
  641. if (num !== '') {
  642. param.push({type: 'num', value: num});
  643. num = '';
  644. }
  645. if (base !== '') {
  646. param.push({type: 'base', value: base});
  647. base = '';
  648. }
  649. param.push({type: 'calc', value: expr[i]});
  650. } else {
  651. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  652. }
  653. }
  654. if (num !== '') {
  655. param.push({type: 'num', value: num});
  656. num = '';
  657. }
  658. if (base !== '') {
  659. param.push({type: 'base', value: base});
  660. base = '';
  661. }
  662. if (param.length === 0) return true;
  663. if (param.length > 1) {
  664. if (param[0].value === '-') {
  665. param[1].value = '-' + param[1];
  666. }
  667. param.unshift();
  668. }
  669. const iLen = param.length;
  670. let iLeftCount = 0, iRightCount = 0;
  671. for (const [i, p] of param.entries()) {
  672. if (p.type === 'calc') {
  673. if (i === 0 || i === iLen - 1)
  674. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字'];
  675. }
  676. if (p.type === 'num') {
  677. num = p.value.replace('%', '');
  678. if (p.value.length - num.length > 1)
  679. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  680. num = _.toNumber(num);
  681. if (num === undefined || num === null || _.isNaN(num))
  682. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  683. if (i > 0) {
  684. if (param[i - 1].type !== 'calc' && param[i - 1].type !== 'left') {
  685. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  686. } else if (param[i - 1].value === '/' && num === 0) {
  687. return [false, '输入的表达式非法:请勿除0'];
  688. }
  689. }
  690. }
  691. if (p.type === 'base') {
  692. if (i > 0 && (param[i - 1].type === 'num' || param[i - 1].type === 'right'))
  693. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  694. }
  695. if (p.type === 'left') {
  696. iLeftCount += 1;
  697. if (i !== 0 && param[i-1].type !== 'calc')
  698. return [false, '输入的表达式非法:(前应有运算符'];
  699. }
  700. if (p.type === 'right') {
  701. iRightCount += 1;
  702. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  703. return [false, '输入的表达式非法:)后应有运算符'];
  704. if (iRightCount > iLeftCount)
  705. return [false, '输入的表达式非法:")"前无对应的"("'];
  706. }
  707. }
  708. if (iLeftCount > iRightCount)
  709. return [false, '输入的表达式非法:"("后无对应的")"'];
  710. return [true, ''];
  711. },
  712. _checkExpr: function (text, data) {
  713. if (text) {
  714. const num = _.toNumber(text);
  715. if (num) {
  716. data.quantity = num;
  717. data.expr = '';
  718. } else {
  719. const expr = $.trim(text).replace('\t', '').replace('=', '').toLowerCase();
  720. const [valid, msg] = this._checkExprValid(expr);
  721. if (!valid) return [valid, msg];
  722. data.expr = expr;
  723. data.quantity = ZhCalc.calcExpr.calcExprStrRpn(expr);
  724. }
  725. } else {
  726. data.quantity = 0;
  727. data.expr = '';
  728. }
  729. return [true, ''];
  730. },
  731. };
  732. materialSpread.bind(spreadNS.Events.EditStarting, materialSpreadObj.editStarting);
  733. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  734. materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted);
  735. SpreadJsObj.addDeleteBind(materialSpread, materialSpreadObj.deletePress);
  736. $.contextMenu({
  737. selector: '#material-spread',
  738. build: function ($trigger, e) {
  739. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  740. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  741. },
  742. items: {
  743. 'create': {
  744. name: '添加工料',
  745. icon: 'fa-sign-in',
  746. callback: function (key, opt) {
  747. // 获取已选清单
  748. changeMaterialTable();
  749. $('#addgl').modal('show');
  750. },
  751. disabled: function (key, opt) {
  752. const sheet = ledgerSpread.getActiveSheet();
  753. const select = SpreadJsObj.getSelectObject(sheet);
  754. if (!select) {
  755. return true;
  756. }
  757. return readOnly;
  758. }
  759. },
  760. 'delete': {
  761. name: '删除工料',
  762. icon: 'fa-remove',
  763. callback: function (key, opt) {
  764. materialSpreadObj.del(materialSpread.getActiveSheet());
  765. },
  766. disabled: function (key, opt) {
  767. const sheet = materialSpread.getActiveSheet();
  768. const selection = sheet.getSelections();
  769. const sel = selection ? selection[0] : sheet.getSelections()[0];
  770. const select = SpreadJsObj.getSelectObject(sheet);
  771. if (!select) {
  772. return true;
  773. }
  774. if (!readOnly && sel.rowCount === 1 && select && materialBase.isEdit(select)) {
  775. return false;
  776. } else {
  777. return true;
  778. }
  779. }
  780. },
  781. }
  782. });
  783. }
  784. // 切换清单行,读取所属项目节数据
  785. ledgerSpread.getActiveSheet().bind(spreadNS.Events.SelectionChanged, function (e, info) {
  786. if (info.oldSelections !== undefined) {
  787. const iOldRow = info.oldSelections[0].row, iNewRow = info.newSelections[0].row;
  788. if (iNewRow !== iOldRow) {
  789. const sheet = ledgerSpread.getActiveSheet();
  790. const select = SpreadJsObj.getSelectObject(sheet);
  791. const index = _.findIndex(gclGatherData, { b_code: select.b_code, name: select.name, unit: select.unit, unit_price: select.unit_price });
  792. loadMaterialData(index, 0);
  793. }
  794. }
  795. });
  796. $('#open_addtclist').click(function () {
  797. $('#tclist_search').val('');
  798. $('#tclist_search').siblings('a').hide();
  799. makeChecklistData(gclGatherData, materialChecklistData);
  800. $('#addtclist').modal('show');
  801. });
  802. // 回车提交
  803. $('#tclist_search').on('keypress', function () {
  804. if(window.event.keyCode === 13) {
  805. $(this).blur();
  806. }
  807. });
  808. $('#tclist_search').on('blur', function () {
  809. const value = _.trim($(this).val());
  810. let showListData = gclGatherData;
  811. if (value !== '') {
  812. $(this).siblings('a').show();
  813. showListData = _.filter(gclGatherData, function (c) {
  814. return (c.b_code && c.b_code.indexOf(value) !== -1) || (c.name && c.name.indexOf(value) !== -1);
  815. })
  816. } else {
  817. $(this).siblings('a').hide();
  818. }
  819. remakeChecklistData(gclGatherData, showListData);
  820. });
  821. $('.remove-btn').on('click', function () {
  822. $(this).hide();
  823. $(this).siblings('input').val('');
  824. remakeChecklistData(gclGatherData);
  825. });
  826. // 显示有调差工料清单
  827. // $('#show_material_gcl').click(function () {
  828. // if ($(this).is(':checked')) {
  829. // const hadMaterialGclGatherData = [];
  830. // const hadGclIdList = [];
  831. // for (const ml of materialListData) {
  832. // if (hadGclIdList.indexOf(ml.gcl_id) === -1) {
  833. // hadGclIdList.push(ml.gcl_id);
  834. // }
  835. // }
  836. // for (const gcl of gclGatherData) {
  837. // for (const index in gcl.leafXmjs) {
  838. // const gcl_id = gcl.leafXmjs[index].gcl_id;
  839. // if (hadGclIdList.indexOf(gcl_id) !== -1) {
  840. // hadMaterialGclGatherData.push(gcl);
  841. // break;
  842. // }
  843. // }
  844. // }
  845. // gclGatherData = hadMaterialGclGatherData;
  846. // } else {
  847. // gclGatherModel.loadLedgerData(ledger, curLedgerData);
  848. // gclGatherModel.loadPosData(pos, curPosData);
  849. // gclGatherData = gclGatherModel.gatherGclData().filter(item => {
  850. // return item.qc_qty || item.contract_qty
  851. // });
  852. // }
  853. // SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, gclGatherData);
  854. // loadLeafXmjData(0);
  855. // loadMaterialData(0, 0);
  856. // SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  857. // SpreadJsObj.resetTopAndSelect(leafXmjSpread.getActiveSheet());
  858. // SpreadJsObj.resetTopAndSelect(materialSpread.getActiveSheet());
  859. // });
  860. $.subMenu({
  861. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  862. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  863. key: 'menu.1.0.0',
  864. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  865. callback: function (info) {
  866. if (info.mini) {
  867. $('.panel-title').addClass('fluid');
  868. $('#sub-menu').removeClass('panel-sidebar');
  869. } else {
  870. $('.panel-title').removeClass('fluid');
  871. $('#sub-menu').addClass('panel-sidebar');
  872. }
  873. autoFlashHeight();
  874. ledgerSpread.refresh();
  875. materialSpread.refresh();
  876. }
  877. });
  878. $.divResizer({
  879. select: '#right-spr',
  880. callback: function () {
  881. ledgerSpread.refresh();
  882. materialSpread.refresh();
  883. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  884. setLocalCache('material_checklist_' + materialID, width);
  885. }
  886. });
  887. // 展开收起工料并浏览器记住本期展开收起
  888. $('a', '.right-nav').bind('click', function () {
  889. const tab = $(this), tabPanel = $(tab.attr('content'));
  890. if (!tab.hasClass('active')) {
  891. $('a', '.side-menu').removeClass('active');
  892. $('.tab-content .tab-select-show').removeClass('active');
  893. tab.addClass('active');
  894. tabPanel.addClass('active');
  895. showSideTools(tab.hasClass('active'));
  896. if (tab.attr('content') === '#material-tab') {
  897. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  898. setLocalCache('material_checklist_' + materialID, width);
  899. }
  900. } else {
  901. removeLocalCache('material_checklist_' + materialID);
  902. tab.removeClass('active');
  903. tabPanel.removeClass('active');
  904. showSideTools(tab.hasClass('active'));
  905. }
  906. ledgerSpread.refresh();
  907. materialSpread.refresh();
  908. });
  909. // 根据浏览器记录展开收起
  910. if (getLocalCache('material_checklist_' + materialID)) {
  911. const tab = $('.right-nav a[content="#material-tab"]'), tabPanel = $(tab.attr('content'));
  912. $('a', '.side-menu').removeClass('active');
  913. $('.tab-content .tab-select-show').removeClass('active');
  914. tab.addClass('active');
  915. tabPanel.addClass('active');
  916. $('#right-view').width(getLocalCache('material_checklist_' + materialID) + '%');
  917. showSideTools(tab.hasClass('active'));
  918. ledgerSpread.refresh();
  919. materialSpread.refresh();
  920. }
  921. });