material_checklist.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. if (gcl && gcl.leafXmjs[iLXmjRow]) {
  244. const xmj = gcl.leafXmjs[iLXmjRow];
  245. materialList = [];
  246. materialList = _.filter(materialListData, function (m) {
  247. 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);
  248. });
  249. // for (const m of materialListData) {
  250. // 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)) {
  251. // materialList.push(m);
  252. // }
  253. // }
  254. // 对清单调差工料table的单位数量进行改变
  255. materialSpreadSetting.cols[materialSpreadSetting.cols.length - 2].title = '|' + gcl.unit + '数量 �';
  256. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  257. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialList);
  258. } else {
  259. materialSpreadSetting.cols[materialSpreadSetting.cols.length - 2].title = '数量 �';
  260. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  261. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, []);
  262. }
  263. SpreadJsObj.resetTopAndSelect(materialSpread.getActiveSheet());
  264. }
  265. // 对添加工料表格赋值
  266. function changeMaterialTable() {
  267. $('#materialBills tr').removeClass('table-secondary');
  268. $('#materialBills').find('input').removeAttr('disabled');
  269. $('#materialBills').find('input').prop('checked', false);
  270. for (const ml of materialList) {
  271. const mbIndex = _.findIndex(materialBillsData, {id : ml.mb_id });
  272. if (mbIndex !== -1) {
  273. $('#materialBills tr').eq(mbIndex).addClass('table-secondary');
  274. $('#materialBills').find('input').eq(mbIndex).attr('disabled', true);
  275. $('#materialBills').find('input').eq(mbIndex).prop('checked', true);
  276. }
  277. }
  278. }
  279. // 选中清单并添加
  280. $('#set_checklist_btn').click(function () {
  281. const select_checklist = $('#lists_data').find('input:checked:not(:disabled)');
  282. const pushData = [];
  283. for (const sc of select_checklist) {
  284. const order = parseInt($(sc).val());
  285. const checklistInfo = _.find(materialChecklistData, { b_code: gclGatherData[order].b_code, name: gclGatherData[order].name, unit: gclGatherData[order].unit, unit_price: gclGatherData[order].unit_price });
  286. if (!checklistInfo) {
  287. pushData.push({
  288. b_code: gclGatherData[order].b_code,
  289. name: gclGatherData[order].name,
  290. unit: gclGatherData[order].unit,
  291. unit_price: gclGatherData[order].unit_price,
  292. quantity: gclGatherData[order].quantity ? gclGatherData[order].quantity : null,
  293. total_price: gclGatherData[order].total_price ? gclGatherData[order].total_price : null,
  294. had_bills: 0,
  295. })
  296. }
  297. }
  298. const notSelect_checklist = $('#lists_data').find('input:not(:checked):not(:disabled)');
  299. const removeData = [];
  300. for (const nsc of notSelect_checklist) {
  301. const order = parseInt($(nsc).val());
  302. // const order = parseInt($(nsc).attr('data-index'));
  303. const checklistInfo = _.find(materialChecklistData, { b_code: gclGatherData[order].b_code, name: gclGatherData[order].name, unit: gclGatherData[order].unit, unit_price: gclGatherData[order].unit_price });
  304. if (checklistInfo) {
  305. removeData.push(checklistInfo.id);
  306. }
  307. }
  308. setChecklistData(pushData, removeData);
  309. $('#addtclist').modal('hide');
  310. });
  311. // 筛选无调差工料清单
  312. $('#notBills_checkList').click(function () {
  313. const isCheck = $(this).is(':checked');
  314. let newMaterialChecklistData = materialChecklistData;
  315. if (isCheck) {
  316. $('#bills0_checkList').prop('checked', false);
  317. newMaterialChecklistData = _.filter(materialChecklistData, { had_bills: 0 });
  318. }
  319. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialChecklistData);
  320. SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  321. if (newMaterialChecklistData.length > 0) {
  322. const index = _.findIndex(gclGatherData, { b_code: newMaterialChecklistData[0].b_code, name: newMaterialChecklistData[0].name, unit: newMaterialChecklistData[0].unit, unit_price: newMaterialChecklistData[0].unit_price });
  323. loadMaterialData(index, 0);
  324. } else {
  325. loadMaterialData(-1, 0);
  326. }
  327. });
  328. // 筛选调差工料清单为0
  329. $('#bills0_checkList').click(function () {
  330. const isCheck = $(this).is(':checked');
  331. let newMaterialChecklistData = materialChecklistData;
  332. if (isCheck) {
  333. newMaterialChecklistData = [];
  334. $('#notBills_checkList').prop('checked', false);
  335. const materialList0 = _.uniq(_.map(_.filter(materialListData, { quantity: 0 }), 'gcl_id'));
  336. if (materialList0.length > 0) {
  337. const hadMaterialChecklistData = _.filter(materialChecklistData, { had_bills: 1 });
  338. for (const h of hadMaterialChecklistData) {
  339. const gcl = _.find(gclGatherData, { b_code: h.b_code, name: h.name, unit: h.unit, unit_price: h.unit_price });
  340. if (gcl && gcl.leafXmjs.length > 0 && _.indexOf(materialList0, gcl.leafXmjs[0].gcl_id) !== -1) {
  341. newMaterialChecklistData.push(h);
  342. }
  343. }
  344. }
  345. }
  346. SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialChecklistData);
  347. SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  348. if (newMaterialChecklistData.length > 0) {
  349. const index = _.findIndex(gclGatherData, { b_code: newMaterialChecklistData[0].b_code, name: newMaterialChecklistData[0].name, unit: newMaterialChecklistData[0].unit, unit_price: newMaterialChecklistData[0].unit_price });
  350. loadMaterialData(index, 0);
  351. } else {
  352. loadMaterialData(-1, 0);
  353. }
  354. });
  355. // 添加调差工料
  356. $('#add_material_bill').click(function () {
  357. // 获取已选工料
  358. $('#materialBills').find('input:disabled').prop('checked', false);
  359. const selectList = $('#materialBills').find('input:checked');
  360. if (selectList.length === 0) {
  361. toastr.warning('请选择调差工料');
  362. $('#materialBills').find('input:disabled').prop('checked', true);
  363. return false;
  364. }
  365. const mb_id = [];
  366. for (let s = 0; s < selectList.length; s++) {
  367. mb_id.push($('#materialBills').find('input:checked').eq(s).val());
  368. }
  369. // 获取当前项目节或部位明细id
  370. const sheet = ledgerSpread.getActiveSheet();
  371. const select = SpreadJsObj.getSelectObject(sheet);
  372. const gclIndex = _.findIndex(gclGatherData, { b_code: select.b_code, name: select.name, unit: select.unit, unit_price: select.unit_price });
  373. const gcl = gclGatherData[gclIndex].leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined);
  374. const index = materialChecklistData.indexOf(select);
  375. const datas = [];
  376. for (const xmj of gcl) {
  377. const notx = findNotJoinLeafXmj(xmj);
  378. const data = {
  379. xmj_id: xmj.id,
  380. gcl_id: xmj.gcl_id,
  381. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  382. gather_qty: xmj.gather_qty,
  383. is_join: notx === undefined ? 1 : 0,
  384. };
  385. datas.push(data);
  386. }
  387. // 上传到数据库
  388. console.log(datas, gcl);
  389. postData(window.location.pathname + '/save', {type: 'adds', checklist: { id: select.id, had_bills: 1 }, postData: {xmjs: datas, mbIds: mb_id}}, function (result) {
  390. materialListData = result;
  391. materialChecklistData[index].had_bills = 1;
  392. loadMaterialData(gclIndex, 0);
  393. // SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), index);
  394. $('#addgl').modal('hide');
  395. });
  396. $('#materialBills').find('input:disabled').prop('checked', true);
  397. });
  398. if (!readOnly) {
  399. // material-spread右键功能
  400. const materialSpreadObj = {
  401. del: function () {
  402. const materialSheet = materialSpread.getActiveSheet();
  403. const materialSelect = SpreadJsObj.getSelectObject(materialSheet);
  404. const sheet = ledgerSpread.getActiveSheet();
  405. const select = SpreadJsObj.getSelectObject(sheet);
  406. const index = materialChecklistData.indexOf(select);
  407. const gclIndex = _.findIndex(gclGatherData, { b_code: select.b_code, name: select.name, unit: select.unit, unit_price: select.unit_price });
  408. const gcl = gclGatherData[gclIndex].leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined);
  409. const datas = [];
  410. for (const xmj of gcl) {
  411. const data = {
  412. xmj_id: xmj.id,
  413. gcl_id: xmj.gcl_id,
  414. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  415. };
  416. datas.push(data);
  417. }
  418. const xmj = gcl[0];
  419. const materialCount = _.size(_.filter(materialListData, function (m) {
  420. 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);
  421. }));
  422. let checklist = false;
  423. if (materialCount === 1) {
  424. checklist = {
  425. id: select.id,
  426. had_bills: 0,
  427. }
  428. }
  429. console.log(datas, materialSelect.mb_id, checklist);
  430. postData(window.location.pathname + '/save', {type: 'dels', checklist, postData: { xmjs: datas, mb_id: materialSelect.mb_id }}, function (result) {
  431. materialListData = result;
  432. if (checklist) materialChecklistData[index].had_bills = checklist.had_bills;
  433. loadMaterialData(gclIndex, 0);
  434. // SpreadJsObj.reLoadRowData(ledgerSpread.getActiveSheet(), index);
  435. });
  436. },
  437. deletePress: function (sheet) {
  438. return;
  439. },
  440. editStarting: function (e, info) {
  441. const col = info.sheet.zh_setting.cols[info.col];
  442. const select = SpreadJsObj.getSelectObject(info.sheet);
  443. if (col.field === 'quantity') {
  444. if (select.expr && select.expr !== '') {
  445. info.sheet.getCell(info.row, info.col).text(select.expr);
  446. }
  447. }
  448. },
  449. editEnded: function (e, info) {
  450. if (info.sheet.zh_setting) {
  451. const select = SpreadJsObj.getSelectObject(info.sheet);
  452. const col = info.sheet.zh_setting.cols[info.col];
  453. const validText = info.editingText ? info.editingText.replace('\n', '') : null;
  454. let orgValue;
  455. if (col.field === 'quantity') {
  456. orgValue = validText && validText !== ''
  457. ? _.toNumber(validText) ? select.quantity : select.expr
  458. : (select.expr && select.expr !== '') ? select.expr : select.quantity;
  459. } else {
  460. orgValue = select[col.field];
  461. }
  462. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === '' || validText === null))) {
  463. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  464. return;
  465. }
  466. const exprQuantity = {
  467. expr: '',
  468. quantity: 0,
  469. };
  470. const [valid, msg] = materialSpreadObj._checkExpr(validText, exprQuantity);
  471. if (!valid) {
  472. toastr.error(msg);
  473. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  474. return;
  475. }
  476. if (isNaN(exprQuantity.quantity)) {
  477. toastr.error('不能输入其它非数字类型字符');
  478. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  479. return;
  480. }
  481. const num = parseFloat(exprQuantity.quantity);
  482. if (num < 0 || !/^\d+(\.\d{1,6})?$/.test(num)) {
  483. toastr.warning('已保留6位小数');
  484. exprQuantity.quantity = ZhCalc.round(num, 6);
  485. }
  486. // 更新至服务器
  487. const ledgerSheet = ledgerSpread.getActiveSheet();
  488. const ledgerSelect = SpreadJsObj.getSelectObject(ledgerSheet);
  489. const gclIndex = _.findIndex(gclGatherData, { b_code: ledgerSelect.b_code, name: ledgerSelect.name, unit: ledgerSelect.unit, unit_price: ledgerSelect.unit_price });
  490. const gcl = gclGatherData[gclIndex].leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined);
  491. const datas = [];
  492. for (const xmj of gcl) {
  493. const data = {
  494. xmj_id: xmj.id,
  495. gcl_id: xmj.gcl_id,
  496. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  497. };
  498. datas.push(data);
  499. }
  500. console.log(exprQuantity, datas, select.mb_id);
  501. postData(window.location.pathname + '/save', { type:'updates', updateData: { xmjs: datas, expr: exprQuantity.expr, quantity: exprQuantity.quantity, mb_id: select.mb_id } }, function (result) {
  502. materialListData = result;
  503. loadMaterialData(gclIndex, 0);
  504. materialSpread.getActiveSheet().setSelection(info.row + 1, info.col, 1, 1);
  505. }, function () {
  506. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  507. });
  508. }
  509. },
  510. clipboardPasted(e, info) {
  511. const hint = {
  512. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  513. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  514. numberCan: {type: 'warning', msg: '已保留6位小数'},
  515. };
  516. const range = info.cellRange;
  517. const sortData = info.sheet.zh_data || [];
  518. if (range.row + range.rowCount > sortData.length) {
  519. toastMessageUniq(hint.cellError);
  520. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  521. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  522. return;
  523. }
  524. if (sortData.length > 0 && range.col + range.colCount > 5) {
  525. toastMessageUniq(hint.cellError);
  526. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  527. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  528. return;
  529. }
  530. const data = [];
  531. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  532. let bPaste = true;
  533. const curRow = range.row + iRow;
  534. const materialData = { id: sortData[curRow].id, mb_id: sortData[curRow].mb_id };
  535. const hintRow = range.rowCount > 1 ? curRow : '';
  536. let sameCol = 0;
  537. for (let iCol = 0; iCol < range.colCount; iCol++) {
  538. const curCol = range.col + iCol;
  539. const colSetting = info.sheet.zh_setting.cols[curCol];
  540. if (!colSetting) continue;
  541. const validText = info.sheet.getText(curRow, curCol).replace('\n', '');
  542. const orgValue = sortData[curRow][colSetting.field];
  543. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  544. sameCol++;
  545. if (range.colCount === sameCol) {
  546. bPaste = false;
  547. }
  548. continue;
  549. }
  550. const exprQuantity = {
  551. expr: '',
  552. quantity: 0,
  553. };
  554. const [valid, msg] = materialSpreadObj._checkExpr(validText, exprQuantity);
  555. if (!valid) {
  556. toastMessageUniq(getPasteHint(msg, hintRow));
  557. bPaste = false;
  558. continue;
  559. }
  560. if (isNaN(exprQuantity.quantity)) {
  561. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  562. bPaste = false;
  563. continue;
  564. }
  565. const num = parseFloat(exprQuantity.quantity);
  566. if (num < 0 || !/^\d+(\.\d{1,6})?$/.test(num)) {
  567. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  568. exprQuantity.quantity = ZhCalc.round(num, 6);
  569. }
  570. materialData.expr = exprQuantity.expr;
  571. materialData.quantity = exprQuantity.quantity;
  572. }
  573. if (bPaste) {
  574. data.push(materialData);
  575. } else {
  576. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  577. }
  578. }
  579. if (data.length === 0) {
  580. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  581. return;
  582. }
  583. const ledgerSheet = ledgerSpread.getActiveSheet();
  584. const ledgerSelect = SpreadJsObj.getSelectObject(ledgerSheet);
  585. const gclIndex = _.findIndex(gclGatherData, { b_code: ledgerSelect.b_code, name: ledgerSelect.name, unit: ledgerSelect.unit, unit_price: ledgerSelect.unit_price });
  586. const gcl = gclGatherData[gclIndex].leafXmjs.filter(item => item.gather_qty !== null && item.gather_qty !== undefined);
  587. const datas = [];
  588. for (const xmj of gcl) {
  589. const data2 = {
  590. xmj_id: xmj.id,
  591. gcl_id: xmj.gcl_id,
  592. mx_id: xmj.mx_id !== undefined ? xmj.mx_id : '',
  593. };
  594. datas.push(data2);
  595. }
  596. console.log(data, datas);
  597. // 更新至服务器
  598. postData(window.location.pathname + '/save', { type:'pastes', updateData: { xmjs: datas, pasteData: data } }, function (result) {
  599. materialListData = result;
  600. loadMaterialData(gclIndex, 0);
  601. materialSpread.getActiveSheet().setSelection(info.cellRange.row, info.cellRange.col, info.cellRange.rowCount, info.cellRange.colCount);
  602. }, function () {
  603. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  604. });
  605. },
  606. _checkExprValid(expr) {
  607. if (!expr) return [true, null];
  608. const param = [];
  609. let num = '', base = '';
  610. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  611. if (/^[\d\.%]+/.test(expr[i])) {
  612. if (base !== '') {
  613. param.push({type: 'base', value: base});
  614. base = '';
  615. }
  616. num = num + expr[i];
  617. } else if (expr[i] === '(') {
  618. if (num !== '') {
  619. param.push({type: 'num', value: num});
  620. num = '';
  621. }
  622. if (base !== '') {
  623. param.push({type: 'base', value: base});
  624. base = '';
  625. }
  626. param.push({type: 'left', value: '('});
  627. } else if (expr[i] === ')') {
  628. if (num !== '') {
  629. param.push({type: 'num', value: num});
  630. num = '';
  631. }
  632. if (base !== '') {
  633. param.push({type: 'base', value: base});
  634. base = '';
  635. }
  636. param.push({type: 'right', value: ')'});
  637. } else if (/^[\+\-*\/]/.test(expr[i])) {
  638. if (num !== '') {
  639. param.push({type: 'num', value: num});
  640. num = '';
  641. }
  642. if (base !== '') {
  643. param.push({type: 'base', value: base});
  644. base = '';
  645. }
  646. param.push({type: 'calc', value: expr[i]});
  647. } else {
  648. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  649. }
  650. }
  651. if (num !== '') {
  652. param.push({type: 'num', value: num});
  653. num = '';
  654. }
  655. if (base !== '') {
  656. param.push({type: 'base', value: base});
  657. base = '';
  658. }
  659. if (param.length === 0) return true;
  660. if (param.length > 1) {
  661. if (param[0].value === '-') {
  662. param[1].value = '-' + param[1];
  663. }
  664. param.unshift();
  665. }
  666. const iLen = param.length;
  667. let iLeftCount = 0, iRightCount = 0;
  668. for (const [i, p] of param.entries()) {
  669. if (p.type === 'calc') {
  670. if (i === 0 || i === iLen - 1)
  671. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字'];
  672. }
  673. if (p.type === 'num') {
  674. num = p.value.replace('%', '');
  675. if (p.value.length - num.length > 1)
  676. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  677. num = _.toNumber(num);
  678. if (num === undefined || num === null || _.isNaN(num))
  679. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  680. if (i > 0) {
  681. if (param[i - 1].type !== 'calc' && param[i - 1].type !== 'left') {
  682. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  683. } else if (param[i - 1].value === '/' && num === 0) {
  684. return [false, '输入的表达式非法:请勿除0'];
  685. }
  686. }
  687. }
  688. if (p.type === 'base') {
  689. if (i > 0 && (param[i - 1].type === 'num' || param[i - 1].type === 'right'))
  690. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  691. }
  692. if (p.type === 'left') {
  693. iLeftCount += 1;
  694. if (i !== 0 && param[i-1].type !== 'calc')
  695. return [false, '输入的表达式非法:(前应有运算符'];
  696. }
  697. if (p.type === 'right') {
  698. iRightCount += 1;
  699. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  700. return [false, '输入的表达式非法:)后应有运算符'];
  701. if (iRightCount > iLeftCount)
  702. return [false, '输入的表达式非法:")"前无对应的"("'];
  703. }
  704. }
  705. if (iLeftCount > iRightCount)
  706. return [false, '输入的表达式非法:"("后无对应的")"'];
  707. return [true, ''];
  708. },
  709. _checkExpr: function (text, data) {
  710. if (text) {
  711. const num = _.toNumber(text);
  712. if (num) {
  713. data.quantity = num;
  714. data.expr = '';
  715. } else {
  716. const expr = $.trim(text).replace('\t', '').replace('=', '').toLowerCase();
  717. const [valid, msg] = this._checkExprValid(expr);
  718. if (!valid) return [valid, msg];
  719. data.expr = expr;
  720. data.quantity = ZhCalc.calcExpr.calcExprStrRpn(expr);
  721. }
  722. } else {
  723. data.quantity = 0;
  724. data.expr = '';
  725. }
  726. return [true, ''];
  727. },
  728. };
  729. materialSpread.bind(spreadNS.Events.EditStarting, materialSpreadObj.editStarting);
  730. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  731. materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted);
  732. SpreadJsObj.addDeleteBind(materialSpread, materialSpreadObj.deletePress);
  733. $.contextMenu({
  734. selector: '#material-spread',
  735. build: function ($trigger, e) {
  736. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  737. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  738. },
  739. items: {
  740. 'create': {
  741. name: '添加工料',
  742. icon: 'fa-sign-in',
  743. callback: function (key, opt) {
  744. // 获取已选清单
  745. changeMaterialTable();
  746. $('#addgl').modal('show');
  747. },
  748. disabled: function (key, opt) {
  749. const sheet = ledgerSpread.getActiveSheet();
  750. const select = SpreadJsObj.getSelectObject(sheet);
  751. if (!select) {
  752. return true;
  753. }
  754. return readOnly;
  755. }
  756. },
  757. 'delete': {
  758. name: '删除工料',
  759. icon: 'fa-remove',
  760. callback: function (key, opt) {
  761. materialSpreadObj.del(materialSpread.getActiveSheet());
  762. },
  763. disabled: function (key, opt) {
  764. const sheet = materialSpread.getActiveSheet();
  765. const selection = sheet.getSelections();
  766. const sel = selection ? selection[0] : sheet.getSelections()[0];
  767. const select = SpreadJsObj.getSelectObject(sheet);
  768. if (!select) {
  769. return true;
  770. }
  771. if (!readOnly && sel.rowCount === 1 && select && materialBase.isEdit(select)) {
  772. return false;
  773. } else {
  774. return true;
  775. }
  776. }
  777. },
  778. }
  779. });
  780. }
  781. // 切换清单行,读取所属项目节数据
  782. ledgerSpread.getActiveSheet().bind(spreadNS.Events.SelectionChanged, function (e, info) {
  783. if (info.oldSelections !== undefined) {
  784. const iOldRow = info.oldSelections[0].row, iNewRow = info.newSelections[0].row;
  785. if (iNewRow !== iOldRow) {
  786. const sheet = ledgerSpread.getActiveSheet();
  787. const select = SpreadJsObj.getSelectObject(sheet);
  788. const index = _.findIndex(gclGatherData, { b_code: select.b_code, name: select.name, unit: select.unit, unit_price: select.unit_price });
  789. loadMaterialData(index, 0);
  790. }
  791. }
  792. });
  793. $('#open_addtclist').click(function () {
  794. $('#tclist_search').val('');
  795. $('#tclist_search').siblings('a').hide();
  796. makeChecklistData(gclGatherData, materialChecklistData);
  797. $('#addtclist').modal('show');
  798. });
  799. // 回车提交
  800. $('#tclist_search').on('keypress', function () {
  801. if(window.event.keyCode === 13) {
  802. $(this).blur();
  803. }
  804. });
  805. $('#tclist_search').on('blur', function () {
  806. const value = _.trim($(this).val());
  807. let showListData = gclGatherData;
  808. if (value !== '') {
  809. $(this).siblings('a').show();
  810. showListData = _.filter(gclGatherData, function (c) {
  811. return (c.b_code && c.b_code.indexOf(value) !== -1) || (c.name && c.name.indexOf(value) !== -1);
  812. })
  813. } else {
  814. $(this).siblings('a').hide();
  815. }
  816. remakeChecklistData(gclGatherData, showListData);
  817. });
  818. $('.remove-btn').on('click', function () {
  819. $(this).hide();
  820. $(this).siblings('input').val('');
  821. remakeChecklistData(gclGatherData);
  822. });
  823. // 显示有调差工料清单
  824. // $('#show_material_gcl').click(function () {
  825. // if ($(this).is(':checked')) {
  826. // const hadMaterialGclGatherData = [];
  827. // const hadGclIdList = [];
  828. // for (const ml of materialListData) {
  829. // if (hadGclIdList.indexOf(ml.gcl_id) === -1) {
  830. // hadGclIdList.push(ml.gcl_id);
  831. // }
  832. // }
  833. // for (const gcl of gclGatherData) {
  834. // for (const index in gcl.leafXmjs) {
  835. // const gcl_id = gcl.leafXmjs[index].gcl_id;
  836. // if (hadGclIdList.indexOf(gcl_id) !== -1) {
  837. // hadMaterialGclGatherData.push(gcl);
  838. // break;
  839. // }
  840. // }
  841. // }
  842. // gclGatherData = hadMaterialGclGatherData;
  843. // } else {
  844. // gclGatherModel.loadLedgerData(ledger, curLedgerData);
  845. // gclGatherModel.loadPosData(pos, curPosData);
  846. // gclGatherData = gclGatherModel.gatherGclData().filter(item => {
  847. // return item.qc_qty || item.contract_qty
  848. // });
  849. // }
  850. // SpreadJsObj.loadSheetData(ledgerSpread.getActiveSheet(), SpreadJsObj.DataType.Data, gclGatherData);
  851. // loadLeafXmjData(0);
  852. // loadMaterialData(0, 0);
  853. // SpreadJsObj.resetTopAndSelect(ledgerSpread.getActiveSheet());
  854. // SpreadJsObj.resetTopAndSelect(leafXmjSpread.getActiveSheet());
  855. // SpreadJsObj.resetTopAndSelect(materialSpread.getActiveSheet());
  856. // });
  857. $.subMenu({
  858. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  859. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  860. key: 'menu.1.0.0',
  861. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  862. callback: function (info) {
  863. if (info.mini) {
  864. $('.panel-title').addClass('fluid');
  865. $('#sub-menu').removeClass('panel-sidebar');
  866. } else {
  867. $('.panel-title').removeClass('fluid');
  868. $('#sub-menu').addClass('panel-sidebar');
  869. }
  870. autoFlashHeight();
  871. ledgerSpread.refresh();
  872. materialSpread.refresh();
  873. }
  874. });
  875. $.divResizer({
  876. select: '#right-spr',
  877. callback: function () {
  878. ledgerSpread.refresh();
  879. materialSpread.refresh();
  880. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  881. setLocalCache('material_checklist_' + materialID, width);
  882. }
  883. });
  884. // 展开收起工料并浏览器记住本期展开收起
  885. $('a', '.right-nav').bind('click', function () {
  886. const tab = $(this), tabPanel = $(tab.attr('content'));
  887. if (!tab.hasClass('active')) {
  888. $('a', '.side-menu').removeClass('active');
  889. $('.tab-content .tab-select-show').removeClass('active');
  890. tab.addClass('active');
  891. tabPanel.addClass('active');
  892. showSideTools(tab.hasClass('active'));
  893. if (tab.attr('content') === '#material-tab') {
  894. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  895. setLocalCache('material_checklist_' + materialID, width);
  896. }
  897. } else {
  898. removeLocalCache('material_checklist_' + materialID);
  899. tab.removeClass('active');
  900. tabPanel.removeClass('active');
  901. showSideTools(tab.hasClass('active'));
  902. }
  903. ledgerSpread.refresh();
  904. materialSpread.refresh();
  905. });
  906. // 根据浏览器记录展开收起
  907. if (getLocalCache('material_checklist_' + materialID)) {
  908. const tab = $('.right-nav a[content="#material-tab"]'), tabPanel = $(tab.attr('content'));
  909. $('a', '.side-menu').removeClass('active');
  910. $('.tab-content .tab-select-show').removeClass('active');
  911. tab.addClass('active');
  912. tabPanel.addClass('active');
  913. $('#right-view').width(getLocalCache('material_checklist_' + materialID) + '%');
  914. showSideTools(tab.hasClass('active'));
  915. ledgerSpread.refresh();
  916. materialSpread.refresh();
  917. }
  918. });