material_checklist.js 41 KB

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