material.js 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212
  1. 'use strict';
  2. /**
  3. * 材料调差 - 调差工料
  4. *
  5. * @author Mai
  6. * @date 2019/1/16
  7. * @version
  8. */
  9. // 编号排序,多重判断
  10. function sortByCode(a, b) {
  11. let code1 = a.code.split('-');
  12. let code2 = b.code.split('-');
  13. let code1length = code1.length;
  14. let code2length = code2.length;
  15. for (let i = 0; i < code1length; i ++) {
  16. if (i+1 <= code2length) {
  17. if (code1[i] != code2[i]) {
  18. if (/^\d+$/.test(code1[i]) && /^\d+$/.test(code2[i])) {
  19. return parseInt(code1[i]) - parseInt(code2[i]);
  20. } else if (!/^\d+$/.test(code1[i]) && /^\d+$/.test(code2[i])) {
  21. return 1;
  22. } else if (/^\d+$/.test(code1[i]) && !/^\d+$/.test(code2[i])) {
  23. return -1;
  24. } else {
  25. const str1length = code1[i].length;
  26. const str2length = code2[i].length;
  27. for (let j = 0; j < str1length; j++) {
  28. if (j+1 <= str2length) {
  29. if (code1[i].charAt(j) != code2[i].charAt(j)) {
  30. return code1[i].charAt(j).charCodeAt() - code2[i].charAt(j).charCodeAt();
  31. } else if (j+1 == str1length && code1[i].charAt(j) == code2[i].charAt(j)) {
  32. if (str1length == str2length) {
  33. return 0;
  34. } else {
  35. return str1length - str2length;
  36. }
  37. }
  38. } else {
  39. if (j+1 >= str1length) {
  40. return 1;
  41. } else {
  42. return -1;
  43. }
  44. }
  45. }
  46. }
  47. } else if (i+1 == code1length && code1[i] == code2[i]) {
  48. if (code1length == code2length) {
  49. return 0;
  50. } else {
  51. return code1length - code2length;
  52. }
  53. }
  54. } else {
  55. if (i+1 >= code1length) {
  56. return 1;
  57. } else {
  58. return -1;
  59. }
  60. }
  61. }
  62. }
  63. function loadUpdateMaterials(newMaterial, fields) {
  64. const newMaterials = newMaterial instanceof Array ? newMaterial : [newMaterial];
  65. for (const nm of newMaterials) {
  66. const om = _.find(materialBillsData, {id: nm.id});
  67. for (const prop in nm) {
  68. if (!fields || fields.indexOf(prop) >= 0) {
  69. om[prop] = nm[prop];
  70. }
  71. }
  72. }
  73. }
  74. function DatePickerCellType() {
  75. }
  76. DatePickerCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  77. DatePickerCellType.prototype.createEditorElement = function () {
  78. //Create input presenter.
  79. var input = document.createElement("input");
  80. return input;
  81. };
  82. DatePickerCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect) {
  83. //Initialize input editor.
  84. if (editorContext) {
  85. const $editor = $(editorContext);
  86. GC.Spread.Sheets.CellTypes.Base.prototype.activateEditor.apply(this, arguments);
  87. $editor.datepicker({});
  88. $editor.css("position", "absolute");
  89. $editor.attr("gcUIElement", "gcEditingInput");
  90. $(".ui-datepicker").attr("gcUIElement", "gcEditingInput");
  91. }
  92. }
  93. DatePickerCellType.prototype.deactivateEditor = function (editorContext) {
  94. //Remove input editor when end editor status.
  95. if (editorContext) {
  96. var element = editorContext;
  97. // $(element).datepicker().data('datepicker').hide();
  98. // $(element).datepicker().data('datepicker').destroy();
  99. $(element).datepicker("hide");
  100. $(element).datepicker("destroy");
  101. }
  102. GC.Spread.Sheets.CellTypes.Base.prototype.deactivateEditor.apply(this, arguments)
  103. };
  104. DatePickerCellType.prototype.setEditorValue = function (editor, value) {
  105. //Sync value from Cell value to editor value.
  106. // console.log($(editor), value);
  107. $(editor).datepicker("setDate", value);
  108. // $(editor).datepicker().data('datepicker').selectDate(value ? new Date(value) : new Date());
  109. };
  110. DatePickerCellType.prototype.getEditorValue = function (editor, context) {
  111. // console.log($(editor).datepicker().data('datepicker'), context);
  112. //Sync value from editor value to cell value.
  113. // console.log($(editor).datepicker("getDate"));
  114. // return $(editor).datepicker("getDate");
  115. return $(editor).val();
  116. };
  117. DatePickerCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect) {
  118. if (editorContext) {
  119. const $editor = $(editorContext);
  120. $editor.css("width", cellRect.width - 1);
  121. $editor.css("height", cellRect.height - 3);
  122. }
  123. };
  124. function resetTpTable() {
  125. $('#tp_set').find('td').eq(1).text(ZhCalc.round(m_tp, materialDecimal.tp));
  126. $('#tp_set').find('td').eq(2).text(ZhCalc.add(pre_tp, ZhCalc.round(m_tp, materialDecimal.tp)));
  127. if (materialTax) {
  128. $('#tax_rate_set').find('td').eq(1).text(ZhCalc.round(m_tax_tp, materialDecimal.tp));
  129. $('#tax_rate_set').find('td').eq(2).text(ZhCalc.add(m_tax_pre_tp, ZhCalc.round(m_tax_tp, materialDecimal.tp)));
  130. } else {
  131. const rate = $('#rateInput').val();
  132. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), materialDecimal.tp);
  133. const jzbqhs = ZhCalc.add(pre_tp_hs, bqhs);
  134. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  135. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  136. }
  137. if (isStageSelf) {
  138. let html = '';
  139. for (const ms of materialStageData) {
  140. const taxHtml = materialTax ? ' <td class="text-center">' + (ms.m_tax_tp !== null ? ms.m_tax_tp : '') + '</td>\n' : '';
  141. html += '<tr><td>第' + ms.order + '期</td><td class="text-center">' + (ms.m_tp !== null ? ms.m_tp : '') + '</td>\n' +
  142. taxHtml +
  143. ' </tr>';
  144. }
  145. const allTaxHtml = materialTax ? ' <td class="text-center">' + m_tax_tp + '</td>\n' : '';
  146. html += '<tr><td>合计</td><td class="text-center">' + m_tp + '</td>\n' +
  147. allTaxHtml +
  148. ' </tr>';
  149. $('#materialStageTable').html(html);
  150. }
  151. }
  152. function getPasteHint (str, row = '') {
  153. let returnObj = str;
  154. if (row) {
  155. returnObj.msg = '工料第' + (row+1) + '行' + str.msg;
  156. }
  157. return returnObj;
  158. }
  159. const is_numeric = (value) => {
  160. if (typeof(value) === 'object') {
  161. return false;
  162. } else {
  163. return !Number.isNaN(Number(value)) && value.toString().trim() !== '';
  164. }
  165. };
  166. $(document).ready(() => {
  167. autoFlashHeight();
  168. const materialSpread = SpreadJsObj.createNewSpread($('#material-spread')[0]);
  169. const materialSpreadSetting = {
  170. emptyRows: 0,
  171. headRows: 2,
  172. headRowHeight: [25, 32],
  173. defaultRowHeight: 21,
  174. headerFont: '12px 微软雅黑',
  175. font: '12px 微软雅黑',
  176. readOnly: readOnly,
  177. };
  178. let materialSpreadSettingCols = [
  179. {title: '调差类型', colSpan: '1', rowSpan: '2', field: 't_type', hAlign: 1, width: 60, formatter: '@', readOnly: 'readOnly.isUsed', cellType: 'customizeCombo', comboItems: materialType.t_type, cellTypeKey: 1},
  180. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 60, formatter: '@', readOnly: 'readOnly.isUsed'},
  181. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: 'readOnly.isEdit'},
  182. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: 'readOnly.isEdit'},
  183. {title: '规格', colSpan: '1', rowSpan: '2', field: 'spec', hAlign: 0, width: 150, formatter: '@', readOnly: 'readOnly.isEdit'},
  184. {title: '工料分类', colSpan: '1', rowSpan: '2', field: 'm_type', hAlign: 1, width: 60, readOnly: 'readOnly.isEdit', cellType: 'customizeCombo', comboItems: materialType.m_type, cellTypeKey: 2},
  185. ];
  186. if (materialTax) {
  187. materialSpreadSettingCols.push({title: '税率(%)', colSpan: '1', rowSpan: '2', field: 'm_tax', hAlign: 2, width: 50, type: 'Number', readOnly: editTaxPermission ? 'readOnly.remark' : 'readOnly.isEdit'});
  188. }
  189. materialSpreadSettingCols = _.concat(materialSpreadSettingCols, [
  190. {title: '上涨 幅度(%)', colSpan: '1', rowSpan: '2', field: 'm_up_risk', hAlign: 2, width: 50, type: 'Number', readOnly: 'readOnly.isEdit'},
  191. {title: '下跌 幅度(%)', colSpan: '1', rowSpan: '2', field: 'm_down_risk', hAlign: 2, width: 50, type: 'Number', readOnly: 'readOnly.isEdit'},
  192. {title: '基准价', colSpan: '1', rowSpan: '2', field: 'basic_price', hAlign: 2, width: 60, type: 'Number', readOnly: 'readOnly.isEdit'},
  193. {title: '基准时间', colSpan: '1', rowSpan: '2', field: 'basic_times', hAlign: 0, width: 70, formatter: '@', readOnly: 'readOnly.isEdit'},
  194. {title: '本期信息价|单价', colSpan: '3|1', rowSpan: '1|1', field: 'msg_tp', hAlign: 2, width: 60, type: 'Number', readOnly: 'readOnly.msg_tp'},
  195. {title: '|时间', colSpan: '|1', rowSpan: '|1', field: 'msg_times', hAlign: 0, width: 80, formatter: '@', readOnly: 'readOnly.remark'},
  196. {title: '|单价差', colSpan: '|1', rowSpan: '|1', field: 'msg_spread', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue: 'getValue.msg_spread'},]
  197. );
  198. if (materialTax) {
  199. materialSpreadSettingCols = _.concat(materialSpreadSettingCols, [
  200. {title: '本期材料调差|有效价差', colSpan: '4|1', rowSpan: '1|1', field: 'm_spread', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue: 'getValue.m_spread'},
  201. {title: '|本期应耗数量', colSpan: '|1', rowSpan: '|1', field: 'quantity', hAlign: 2, width: 90, type: 'Number', readOnly: true},
  202. {title: '|调差金额', colSpan: '|1', rowSpan: '|1', field: 'm_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true, getValue: 'getValue.m_tp'},
  203. {title: '|调差金额(材料税)', colSpan: '|1', rowSpan: '|1', field: 'm_tax_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true, getValue: 'getValue.m_tax_tp'},
  204. {title: '截止上期|调差金额', colSpan: '2|1', rowSpan: '1|1', field: 'pre_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true},
  205. {title: '|调差金额(材料税)', colSpan: '|1', rowSpan: '|1', field: 'tax_pre_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true},
  206. {title: '备注', colSpan: '1', rowSpan: '2', field: 'remark', hAlign: 0, width: 50, formatter: '@', cellType: 'ellipsisAutoTip', readOnly: 'readOnly.remark'},
  207. {title: '是否汇总', colSpan: '1', rowSpan: '2', field: 'is_summary', hAlign: 1, width: 60, cellType: 'checkbox', readOnly: 'readOnly.isEdit'}
  208. ])
  209. } else {
  210. materialSpreadSettingCols = _.concat(materialSpreadSettingCols, [
  211. {title: '本期材料调差|有效价差', colSpan: '3|1', rowSpan: '1|1', field: 'm_spread', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue: 'getValue.m_spread'},
  212. {title: '|本期应耗数量', colSpan: '|1', rowSpan: '|1', field: 'quantity', hAlign: 2, width: 90, type: 'Number', readOnly: true},
  213. {title: '|调差金额', colSpan: '|1', rowSpan: '|1', field: 'm_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true, getValue: 'getValue.m_tp'},
  214. {title: '截止上期|调差金额', colSpan: '1|1', rowSpan: '1|1', field: 'pre_tp', hAlign: 2, width: 100, type: 'Number', readOnly: true},
  215. {title: '备注', colSpan: '1', rowSpan: '2', field: 'remark', hAlign: 0, width: 50, formatter: '@', cellType: 'ellipsisAutoTip', readOnly: 'readOnly.remark'},
  216. {title: '是否汇总', colSpan: '1', rowSpan: '2', field: 'is_summary', hAlign: 1, width: 60, cellType: 'checkbox', readOnly: 'readOnly.isEdit'}
  217. ])
  218. }
  219. materialSpreadSetting.cols = materialSpreadSettingCols;
  220. const spCol = _.find(materialSpreadSetting.cols, {field: 'quantity'});
  221. spCol.readOnly = true;
  222. spCol.cellType = 'activeImageBtn';
  223. spCol.normalImg = '#ellipsis-icon';
  224. spCol.indent = 5;
  225. spCol.showImage = function (data) {
  226. // return !readOnly && data.t_type === 2 && data.mid === materialID;
  227. return data && data.t_type === 2;
  228. };
  229. materialSpreadSetting.imageClick = function (data) {
  230. if (data.t_type === 2) {
  231. $('#bcyy').modal('show');
  232. $('#materialbillsId').val(data.id);
  233. $('#expr').val(data.expr);
  234. if (!readOnly) {
  235. $('#expr').attr('readOnly', false);
  236. $('#expr_btn').show();
  237. $('#expr_select').show();
  238. } else {
  239. $('#expr').attr('readOnly', true);
  240. $('#expr_btn').hide();
  241. $('#expr_select').hide();
  242. }
  243. }
  244. };
  245. const materialBase = {
  246. isUsed: function (data) {
  247. if (data.t_type === 2) {
  248. return data.mid === materialID;
  249. } else {
  250. const mlInfo = _.find(materialListData, { mb_id: data.id });
  251. return data.mid === materialID && mlInfo === undefined;
  252. }
  253. },
  254. isEdit: function (data) {
  255. // if (data.t_type === 2) {
  256. // return data.mid === materialID;
  257. // } else {
  258. // const mlInfo = _.find(materialListData, { mb_id: data.id });
  259. // return data.mid === materialID && mlInfo === undefined;
  260. // }
  261. return data.mid === materialID;
  262. },
  263. // isStage: function (data) {
  264. // return data.mid === materialID;
  265. // }
  266. }
  267. const materialCol = {
  268. getValue: {
  269. msg_spread: function (data) {
  270. return ZhCalc.round(ZhCalc.sub(data.msg_tp, data.basic_price), materialDecimal.up);
  271. },
  272. m_spread : function (data) {
  273. const msg_spread = materialCol.getValue.msg_spread(data);
  274. const cor = msg_spread >= 0 ? ZhCalc.mul(data.basic_price, ZhCalc.div(data.m_up_risk, 100)) : ZhCalc.mul(data.basic_price, ZhCalc.div(data.m_down_risk, 100));
  275. return Math.abs(msg_spread) > Math.abs(cor) ? (msg_spread > 0 ? ZhCalc.round(ZhCalc.sub(msg_spread, cor), materialDecimal.up) : ZhCalc.round(ZhCalc.add(msg_spread, cor), materialDecimal.up)) : 0;
  276. },
  277. m_tp: function (data) {
  278. return ZhCalc.round(ZhCalc.mul(materialCol.getValue.m_spread(data), data.quantity), materialDecimal.tp);
  279. },
  280. m_tax_tp: function (data) {
  281. const m_tp = ZhCalc.round(ZhCalc.mul(materialCol.getValue.m_spread(data), data.quantity), materialDecimal.tp);
  282. return data.m_tax ? ZhCalc.round(ZhCalc.mul(m_tp, (1+ZhCalc.div(data.m_tax, 100))), materialDecimal.tp) : m_tp;
  283. }
  284. },
  285. readOnly: {
  286. isEdit: function (data) {
  287. return !(!readOnly && materialBase.isEdit(data));
  288. },
  289. isUsed: function (data) {
  290. return !(!readOnly && materialBase.isUsed(data));
  291. },
  292. remark: function () {
  293. return readOnly;
  294. },
  295. msg_tp: function () {
  296. return !(!readOnly && months.length === 0);
  297. }
  298. },
  299. };
  300. const needUpdateArray = ['quantity', 'expr', 'msg_tp', 'msg_times', 'msg_spread', 'm_spread', 'm_tp', 'm_tax_tp', 'is_summary', 'remark'];
  301. const materialSpreadObj = {
  302. getMaterialBillsData: function () {
  303. if (isStageSelf) {
  304. // 当前选中nav id
  305. const ms_id = $('#myTab').find('.active').data('msid');
  306. const msbList = _.filter(materialStageBillsData, { ms_id });
  307. for (const mb of materialBillsData) {
  308. mb.ms_id = ms_id;
  309. const msbInfo = _.find(msbList, { mb_id: mb.mb_id ? mb.mb_id : mb.id });
  310. if (msbInfo) {
  311. for (const nu of needUpdateArray) {
  312. mb[nu] = msbInfo[nu];
  313. }
  314. }
  315. }
  316. }
  317. return materialBillsData;
  318. },
  319. updateOneMaterialBill: function (data) {
  320. const ms_id = $('#myTab').find('.active').data('msid');
  321. const msbInfo = _.find(materialStageBillsData, { ms_id, mb_id: data.id });
  322. console.log(msbInfo);
  323. data.ms_id = ms_id;
  324. for (const nu of needUpdateArray) {
  325. data[nu] = msbInfo[nu];
  326. }
  327. return data;
  328. },
  329. updateMaterialData: function (datas) {
  330. if (datas.stageData && datas.stageData.length > 0) {
  331. if (datas.stageData.length === materialStageData.length) {
  332. // 全体替换
  333. materialStageData = datas.stageData;
  334. } else {
  335. // 替换对应的stage数据
  336. for (const s of datas.stageData) {
  337. const index = _.findIndex(materialStageData, { id: s.id });
  338. materialStageData.splice(index, 1, s);
  339. }
  340. }
  341. }
  342. if (datas.pushStageBillsData && datas.pushStageBillsData.length > 0) {
  343. materialStageBillsData = _.concat(materialStageBillsData, datas.pushStageBillsData);
  344. }
  345. if (datas.removeStageBillsData && datas.removeStageBillsData.length > 0) {
  346. materialStageBillsData = _.remove(materialStageBillsData, function (item) {
  347. return _.findIndex(datas.removeStageBillsData, { id: item.mb_id }) === -1
  348. })
  349. }
  350. if (datas.stageBillsData && datas.stageBillsData.length > 0) {
  351. if (datas.stageBillsData.length === materialStageBillsData.length) {
  352. // 全体替换
  353. materialStageBillsData = datas.stageBillsData;
  354. } else {
  355. // 替换对应的stage数据
  356. for (const s of datas.stageBillsData) {
  357. const index = _.findIndex(materialStageBillsData, { id: s.id });
  358. materialStageBillsData.splice(index, 1, s);
  359. }
  360. }
  361. }
  362. if (datas.billsData && datas.billsData.length > 0) {
  363. if (datas.billsData.length === materialBillsData.length) {
  364. // 全体替换
  365. materialBillsData = datas.billsData;
  366. materialSpreadObj.getMaterialBillsData();
  367. } else {
  368. for (const b of datas.billsData) {
  369. const index = _.findIndex(materialBillsData, {id: b.id});
  370. materialBillsData.splice(index, 1, b);
  371. materialSpreadObj.updateOneMaterialBill(materialBillsData[index]);
  372. }
  373. }
  374. }
  375. },
  376. materialSheetReset: function (redo = false) {
  377. let newMaterialBillsData = _.cloneDeep(materialBillsData);
  378. if($('#bills0_list').is(':checked')) {
  379. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  380. return item.quantity !== null && item.quantity !== 0;
  381. });
  382. }
  383. if (redo) {
  384. materialSpread.getActiveSheet().reset();
  385. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  386. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  387. }
  388. console.log(newMaterialBillsData);
  389. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  390. materialSpreadObj.refreshActn();
  391. },
  392. refreshActn: function (rowCount = 1) {
  393. const setObjEnable = function (obj, enable) {
  394. if (enable) {
  395. obj.removeClass('disabled');
  396. } else {
  397. obj.addClass('disabled');
  398. }
  399. };
  400. const sheet = materialSpread.getActiveSheet();
  401. const select = SpreadJsObj.getSelectObject(sheet);
  402. const selection = sheet.getSelections();
  403. const sel = selection ? selection[0] : sheet.getSelections()[0];
  404. const row = sel ? sel.row : -1;
  405. const first = materialBillsData[row];
  406. let last = first;
  407. if (sel.rowCount > 1 && first) {
  408. for (let r = 1; r < sel.rowCount; r++) {
  409. const rNode = materialBillsData[sel.row + r];
  410. if (!rNode) break;
  411. last = rNode;
  412. }
  413. }
  414. const preNode = materialBillsData[row - 1];
  415. // 还需判断是否已被调差清单调用
  416. setObjEnable($('#del'), !readOnly && select && materialBase.isUsed(select) && rowCount === 1);
  417. setObjEnable($('#up-move'), !readOnly && first && preNode && materialBillsData.indexOf(last) > 0 && sel.row + sel.rowCount <= materialBillsData.length && !$('#bills0_list').is(':checked'));
  418. setObjEnable($('#down-move'), !readOnly && first && materialBillsData.indexOf(last) < materialBillsData.length - 1 && sel.row + sel.rowCount <= materialBillsData.length && !$('#bills0_list').is(':checked'));
  419. },
  420. add: function () {
  421. const sheet = materialSpread.getActiveSheet();
  422. postData(window.location.pathname + '/save', {type: 'add'}, function (result) {
  423. if (result) {
  424. materialBillsData.push(result.info);
  425. if (isStageSelf) {
  426. materialSpreadObj.updateMaterialData(result);
  427. materialSpreadObj.getMaterialBillsData();
  428. }
  429. // sheet.addRows(materialBillsData.length - 1, 1);
  430. // SpreadJsObj.reLoadRowData(sheet, materialBillsData.length - 1);
  431. let newMaterialBillsData = materialBillsData;
  432. if($('#bills0_list').is(':checked')) {
  433. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  434. return item.quantity !== null && item.quantity !== 0;
  435. });
  436. }
  437. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  438. // materialSpreadObj.materialSheetReset();
  439. sheet.setSelection(newMaterialBillsData.length - 1, 0, 1, 1);
  440. console.log(sheet);
  441. materialSpreadObj.refreshActn();
  442. // 月信息价需要同时添加空白的list
  443. if (months.length > 0) {
  444. const one_month ={ mb_id: result.info.id, code: '', name: null, origin: null, unit: null, order: result.info.order };
  445. for (const m of months) {
  446. one_month[m] = null;
  447. }
  448. monthsList.push(one_month);
  449. // const newMonthsList = _.filter(monthsList, function (item) {
  450. // return _.findIndex(newMaterialBillsData, { code: item.code }) !== -1;
  451. // });
  452. // materialMonthSpread.getActiveSheet().addRows(newMonthsList.length - 1, 1);
  453. // SpreadJsObj.reLoadRowData(materialMonthSpread.getActiveSheet(), newMonthsList.length - 1);
  454. monthFunGather.monthsListSet();
  455. // materialMonthSpreadObj.monthsListSet();
  456. }
  457. }
  458. });
  459. },
  460. del: function () {
  461. const sheet = materialSpread.getActiveSheet();
  462. const select = SpreadJsObj.getSelectObject(sheet);
  463. const sel = sheet.getSelections();
  464. postData(window.location.pathname + '/save', {type: 'del', id: select.id}, function (result) {
  465. m_tp = result.m_tp;
  466. if (materialTax) {
  467. m_tax_tp = result.m_tax_tp;
  468. }
  469. const index = _.findIndex(materialBillsData, { id: select.id });
  470. materialBillsData.splice(index, 1);
  471. if (isStageSelf) {
  472. result.removeStageBillsData = [{ id: select.id }];
  473. materialSpreadObj.updateMaterialData(result);
  474. materialSpreadObj.getMaterialBillsData();
  475. }
  476. let newMaterialBillsData = materialBillsData;
  477. let newIndex = index;
  478. if($('#bills0_list').is(':checked')) {
  479. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  480. return item.quantity !== null && item.quantity !== 0;
  481. });
  482. newIndex = _.findIndex(newMaterialBillsData, { id: select.id });
  483. }
  484. // sheet.deleteRows(newIndex, 1);
  485. // sheet.zh_data.splice(newIndex, 1);
  486. materialSpreadObj.materialSheetReset(true);
  487. resetTpTable();
  488. // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  489. sheet.setSelection(newIndex > 0 ? newIndex - 1 : 0, sel.length > 0 ? sel[0].col : 0, 1, 1);
  490. materialSpreadObj.refreshActn();
  491. // 月信息价需要同时删除
  492. if (months.length > 0) {
  493. monthsList.splice(index, 1);
  494. materialMonthSpread.getActiveSheet().deleteRows(newIndex, 1);
  495. // SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  496. }
  497. });
  498. },
  499. upMove: function () {
  500. if($('#bills0_list').is(':checked')) {
  501. return;
  502. }
  503. const data = {
  504. type: 'changeOrder',
  505. postData: [],
  506. };
  507. const sheet = materialSpread.getActiveSheet();
  508. const selection = sheet.getSelections();
  509. const row = selection[0].row, count = selection[0].rowCount;
  510. const first = sheet.zh_data[row];
  511. if (!first) {
  512. materialSpreadObj.refreshActn();
  513. return false;
  514. }
  515. const pre = sheet.zh_data[row - 1], preUpdate = {id: pre.id};
  516. for (let iRow = 0; iRow < count; iRow++) {
  517. const posData = sheet.zh_data[iRow + row];
  518. if (posData) {
  519. data.postData.push({id: posData.id, order: sheet.zh_data[iRow + row - 1].order});
  520. preUpdate.order = posData.order;
  521. }
  522. }
  523. console.log(monthsList);
  524. data.postData.push(preUpdate);
  525. console.log(data);
  526. if (data.postData.length > 0) {
  527. postData(window.location.pathname + '/save', data, function () {
  528. _.forEach(data.postData, function (item) {
  529. const cl = _.find(materialBillsData, { id: item.id });
  530. cl.order = item.order;
  531. });
  532. materialBillsData.sort(function (a, b) {
  533. return a.order - b.order
  534. });
  535. SpreadJsObj.reLoadSheetData(sheet);
  536. const sel = selection[0];
  537. if (sel) {
  538. sheet.setSelection(sheet.zh_data.indexOf(first), sel.col, sel.rowCount, sel.colCount);
  539. }
  540. if (months.length > 0) {
  541. _.forEach(data.postData, function (item) {
  542. const cl = _.find(monthsList, { mb_id: item.id });
  543. cl.order = item.order;
  544. });
  545. monthsList.sort(function (a, b) {
  546. return a.order - b.order
  547. });
  548. const materialMonthSheet = materialMonthSpread.getActiveSheet();
  549. SpreadJsObj.reLoadSheetData(materialMonthSheet);
  550. }
  551. materialSpreadObj.refreshActn();
  552. });
  553. }
  554. },
  555. downMove: function () {
  556. if($('#bills0_list').is(':checked')) {
  557. return;
  558. }
  559. const data = {
  560. type: 'changeOrder',
  561. postData: [],
  562. };
  563. const sheet = materialSpread.getActiveSheet();
  564. const selection = sheet.getSelections();
  565. const row = selection[0].row, count = selection[0].rowCount;
  566. const first = sheet.zh_data[row];
  567. if (!first) {
  568. materialSpreadObj.refreshActn();
  569. return false;
  570. }
  571. const next = sheet.zh_data[row + count], nextUpdate = {id: next.id};
  572. for (let iRow = count - 1; iRow >= 0; iRow--) {
  573. const posData = sheet.zh_data[iRow + row];
  574. if (posData) {
  575. data.postData.push({id: posData.id, order: sheet.zh_data[iRow + row + 1].order});
  576. nextUpdate.order = posData.order;
  577. }
  578. }
  579. data.postData.push(nextUpdate);
  580. console.log(data);
  581. if (data.postData.length > 0) {
  582. postData(window.location.pathname + '/save', data, function () {
  583. _.forEach(data.postData, function (item) {
  584. const cl = _.find(materialBillsData, { id: item.id });
  585. cl.order = item.order;
  586. });
  587. materialBillsData.sort(function (a, b) {
  588. return a.order - b.order
  589. });
  590. SpreadJsObj.reLoadSheetData(sheet);
  591. const sel = selection[0];
  592. if (sel) {
  593. sheet.setSelection(sheet.zh_data.indexOf(first), sel.col, sel.rowCount, sel.colCount);
  594. }
  595. if (months.length > 0) {
  596. _.forEach(data.postData, function (item) {
  597. const cl = _.find(monthsList, { mb_id: item.id });
  598. cl.order = item.order;
  599. });
  600. monthsList.sort(function (a, b) {
  601. return a.order - b.order
  602. });
  603. const materialMonthSheet = materialMonthSpread.getActiveSheet();
  604. SpreadJsObj.reLoadSheetData(materialMonthSheet);
  605. }
  606. materialSpreadObj.refreshActn();
  607. });
  608. }
  609. },
  610. selectionChanged: function (e, info) {
  611. const sel = info.sheet.getSelections()[0];
  612. const col = info.sheet.zh_setting.cols[sel.col];
  613. materialSpreadObj.refreshActn(sel.rowCount);
  614. const data = SpreadJsObj.getSelectObject(info.sheet);
  615. materialSpreadObj.setReadOnly(true);
  616. // console.log(data);
  617. setCurBillSourceList(data ? (data.mb_id || data.id) : null, $('#myTab').find('.active').data('msid') || null);
  618. },
  619. editEnded: function (e, info) {
  620. if (info.sheet.zh_setting) {
  621. const select = SpreadJsObj.getSelectObject(info.sheet);
  622. const col = info.sheet.zh_setting.cols[info.col];
  623. if (col.field === 'is_summary') {
  624. return;
  625. }
  626. // 未改变值则不提交
  627. let validText = (col.type === 'Number' || col.field === 'm_type' || col.field === 't_type') && is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  628. const orgValue = select[col.field];
  629. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  630. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  631. return;
  632. }
  633. // 判断部分值是否输入的是数字判断和数据计算
  634. if (col.field === 'basic_price') {
  635. if (isNaN(validText)) {
  636. toastr.error('不能输入其它非数字类型字符');
  637. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  638. return;
  639. }
  640. let num = parseFloat(validText);
  641. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  642. if (validText !== null && (num < 0 || !reg.test(num))) {
  643. toastr.warning('已保留'+ materialDecimal.up +'位小数');
  644. validText = ZhCalc.round(num, materialDecimal.up);
  645. }
  646. }
  647. if (col.field === 'msg_tp') {
  648. if (isNaN(validText)) {
  649. toastr.error('不能输入其它非数字类型字符');
  650. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  651. return;
  652. }
  653. const num = parseFloat(validText);
  654. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  655. if (validText !== null && (num < 0 || !reg.test(num))) {
  656. toastr.warning('已保留'+ materialDecimal.up +'位小数');
  657. validText = ZhCalc.round(num, materialDecimal.up);
  658. }
  659. }
  660. if (col.field === 'm_up_risk' || col.field === 'm_down_risk' || col.field === 'm_tax') {
  661. // 只能输入正整数
  662. if (isNaN(validText)) {
  663. toastr.error('不能输入其它非数字类型字符');
  664. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  665. return;
  666. }
  667. const num = parseFloat(validText);
  668. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  669. toastr.error('只能输入0-100的正整数');
  670. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  671. return;
  672. }
  673. }
  674. if (col.field === 't_type') {
  675. if (validText === 1) {
  676. select.quantity = null;
  677. select.expr = null;
  678. select.m_tp = null;
  679. }
  680. }
  681. select[col.field] = validText;
  682. select.msg_spread = materialCol.getValue.msg_spread(select);
  683. select.m_spread = materialCol.getValue.m_spread(select);
  684. select.m_tp = materialCol.getValue.m_tp(select);
  685. select.m_tax_tp = materialCol.getValue.m_tax_tp(select);
  686. delete select.waitingLoading;
  687. // console.log(select);
  688. // 更新至服务器
  689. postData(window.location.pathname + '/save', { type:'update', updateData: select, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  690. m_tp = result.m_tp;
  691. if (materialTax) {
  692. m_tax_tp = result.m_tax_tp;
  693. }
  694. if (isStageSelf) {
  695. materialSpreadObj.updateMaterialData(result);
  696. }
  697. resetTpTable();
  698. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  699. // 判断如果是更改了编号,名称,单位,月信息价需要跟着改变值
  700. if (months.length > 0 && (col.field === 'code' || col.field === 'name' || col.field === 'unit')) {
  701. const index = _.findIndex(materialBillsData, { id: select.id });
  702. monthsList[index][col.field] = validText;
  703. monthFunGather.monthsListSet();
  704. }
  705. }, function () {
  706. select[col.field] = orgValue;
  707. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  708. });
  709. }
  710. },
  711. buttonClicked: function (e, info) {
  712. if (info.sheet.zh_setting) {
  713. const select = SpreadJsObj.getSelectObject(info.sheet);
  714. const col = info.sheet.zh_setting.cols[info.col];
  715. if (materialCol.readOnly.isEdit(select)) {
  716. return;
  717. }
  718. if (col.field === 'is_summary') {
  719. if (info.sheet.isEditing()) {
  720. info.sheet.endEdit(true);
  721. }
  722. select.is_summary = info.sheet.getValue(info.row, info.col) ? 1 : 0;
  723. delete select.waitingLoading;
  724. // 更新至服务器
  725. postData(window.location.pathname + '/save', { type:'update', updateData: select, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  726. m_tp = result.m_tp;
  727. if (materialTax) {
  728. m_tax_tp = result.m_tax_tp;
  729. }
  730. if (isStageSelf) {
  731. materialSpreadObj.updateMaterialData(result);
  732. }
  733. resetTpTable();
  734. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  735. }, function () {
  736. select.is_summary = info.sheet.getValue(info.row, info.col) ? 0 : 1;
  737. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  738. });
  739. }
  740. }
  741. },
  742. deletePress: function (sheet) {
  743. return;
  744. },
  745. clipboardPasted(e, info) {
  746. const hint = {
  747. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  748. codeEmpty: {type: 'error', msg: '请先输入编号,并且不超过15个字符'},
  749. codeUsed: {type: 'error', msg: '该编号已存在,请重新输入。'},
  750. codeError: {type: 'error', msg: '编号为纯数字时,不能为小数'},
  751. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  752. riskCan: {type: 'error', msg: '只能粘贴0-100的正整数'},
  753. numberCan: {type: 'warning', msg: '已保留'+ materialDecimal.up +'位小数'},
  754. };
  755. const range = info.cellRange;
  756. const sortData = info.sheet.zh_data || [];
  757. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  758. toastMessageUniq(hint.cellError);
  759. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  760. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  761. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  762. return;
  763. }
  764. if (sortData.length > 0 && range.col + range.colCount > 18) {
  765. toastMessageUniq(hint.cellError);
  766. SpreadJsObj.reLoadSheetHeader(materialSpread.getActiveSheet());
  767. SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  768. return;
  769. }
  770. const data = [];
  771. // const rowData = [];
  772. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  773. let bPaste = true;
  774. const curRow = range.row + iRow;
  775. // const materialData = JSON.parse(JSON.stringify(sortData[curRow]));
  776. const materialData = { id: sortData[curRow].id };
  777. const hintRow = range.rowCount > 1 ? curRow : '';
  778. let sameCol = 0;
  779. for (let iCol = 0; iCol < range.colCount; iCol++) {
  780. const curCol = range.col + iCol;
  781. const colSetting = info.sheet.zh_setting.cols[curCol];
  782. if (!colSetting) continue;
  783. let validText = info.sheet.getText(curRow, curCol);
  784. validText = (colSetting.type === 'Number' || colSetting.field === 'm_type' || colSetting.field === 't_type') && is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  785. const orgValue = sortData[curRow][colSetting.field];
  786. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  787. sameCol++;
  788. if (range.colCount === sameCol) {
  789. bPaste = false;
  790. }
  791. continue;
  792. }
  793. if (colSetting.field === 'code' && (validText === '' || validText.length > 15)) {
  794. toastMessageUniq(getPasteHint(hint.codeEmpty, hintRow));
  795. bPaste = false;
  796. continue;
  797. }
  798. if (colSetting.field === 'code' && validText.toString().indexOf('.') !== -1) {
  799. toastMessageUniq(getPasteHint(hint.codeError, hintRow));
  800. bPaste = false;
  801. continue;
  802. }
  803. if (colSetting.field === 'code' && _.find(sortData, { code: validText.toString() })) {
  804. toastMessageUniq(getPasteHint(hint.codeUsed, hintRow));
  805. bPaste = false;
  806. continue;
  807. }
  808. if (colSetting.field === 't_type' || colSetting.field === 'm_type') {
  809. continue;
  810. }
  811. if (colSetting.field === 'basic_price' || colSetting.field === 'msg_tp'
  812. || colSetting.field === 'm_up_risk' || colSetting.field === 'm_down_risk') {
  813. if (isNaN(validText)) {
  814. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  815. bPaste = false;
  816. continue;
  817. }
  818. const num = parseFloat(validText);
  819. if (colSetting.field === 'basic_price' || colSetting.field === 'msg_tp') {
  820. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  821. if (validText !== null && (num < 0 || !reg.test(num))) {
  822. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  823. validText = ZhCalc.round(num, materialDecimal.up);
  824. }
  825. } else if (colSetting.field === 'm_up_risk' || colSetting.field === 'm_down_risk' || colSetting.field === 'm_tax') {
  826. if (validText !== null && (num < 0 || num > 100 || !/^\d+$/.test(num))) {
  827. toastMessageUniq(getPasteHint(hint.riskCan, hintRow));
  828. bPaste = false;
  829. continue;
  830. }
  831. }
  832. }
  833. materialData[colSetting.field] = validText;
  834. sortData[curRow][colSetting.field] = validText;
  835. }
  836. if (materialData.code === '' || materialData.code === null) {
  837. toastMessageUniq(getPasteHint(hint.codeEmpty, hintRow));
  838. bPaste = false;
  839. }
  840. if (bPaste) {
  841. materialData.msg_spread = materialCol.getValue.msg_spread(sortData[curRow]);
  842. materialData.m_spread = materialCol.getValue.m_spread(sortData[curRow]);
  843. materialData.m_tp = materialCol.getValue.m_tp(sortData[curRow]);
  844. materialData.m_tax_tp = materialCol.getValue.m_tax_tp(sortData[curRow]);
  845. data.push(materialData);
  846. // rowData.push(curRow);
  847. } else {
  848. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  849. }
  850. }
  851. if (data.length === 0) {
  852. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  853. return;
  854. }
  855. // console.log(data);
  856. // 更新至服务器
  857. postData(window.location.pathname + '/save', { type:'paste', updateData: data, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  858. materialBillsData = result.info;
  859. m_tp = result.m_tp;
  860. if (materialTax) {
  861. m_tax_tp = result.m_tax_tp;
  862. }
  863. if (isStageSelf) {
  864. materialSpreadObj.updateMaterialData(result);
  865. materialSpreadObj.getMaterialBillsData();
  866. }
  867. // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  868. materialSpreadObj.materialSheetReset();
  869. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  870. // for (const row in rowData) {
  871. // materialBillsData.splice(index, 1, result.info[row]);
  872. // SpreadJsObj.reLoadRowData(info.sheet, row);
  873. // }
  874. if (months.length > 0) {
  875. for (const [i,m] of monthsList.entries()) {
  876. m.code = materialBillsData[i].code;
  877. m.name = materialBillsData[i].name;
  878. m.unit = materialBillsData[i].unit;
  879. m.origin = materialBillsData[i].origin;
  880. }
  881. // SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, monthsList);
  882. monthFunGather.monthsListSet();
  883. // SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  884. }
  885. resetTpTable();
  886. // materialSpreadObj.refreshActn();
  887. }, function () {
  888. // materialSpreadObj.refreshActn();
  889. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  890. return;
  891. });
  892. },
  893. setReadOnly: function(readOnly) {
  894. // SpreadJsObj.resetFieldReadOnly(materialSpread.getActiveSheet(), 'msg_spread', 'm_spread', 'm_tp', 'pre_tp', readOnly);
  895. }
  896. };
  897. materialSpread.bind(spreadNS.Events.SelectionChanged, materialSpreadObj.selectionChanged);
  898. materialSpread.bind(spreadNS.Events.ClipboardPasted, materialSpreadObj.clipboardPasted);
  899. SpreadJsObj.addDeleteBind(materialSpread, materialSpreadObj.deletePress);
  900. materialSpread.bind(spreadNS.Events.EditEnded, materialSpreadObj.editEnded);
  901. materialSpread.bind(spreadNS.Events.ButtonClicked, materialSpreadObj.buttonClicked);
  902. const sheet = materialSpread.getActiveSheet();
  903. sheet.suspendPaint();
  904. // materialSpreadObj.refreshActn();
  905. // const basic_range = sheet.getRange(-1, 8, -1, 1);
  906. // basic_range.cellType(new DatePickerCellType());
  907. // basic_range.formatter("yyyy-MM-dd");
  908. // const msg_range = sheet.getRange(-1, 10, -1, 1);
  909. // msg_range.cellType(new DatePickerCellType());
  910. // msg_range.formatter("yyyy-MM-dd");
  911. sheet.resumePaint();
  912. const static_cols = [
  913. {title: '编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 60, formatter: '@', readOnly: true},
  914. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 100, formatter: '@', readOnly: true},
  915. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  916. {title: '来源地', colSpan: '1', rowSpan: '2', field: 'origin', hAlign: 0, width: 70, formatter: '@', readOnly: 'readOnly.isEdit'},
  917. {title: '平均单价', colSpan: '1', rowSpan: '2', field: 'average_msg_tp', hAlign: 2, width: 60, type: 'Number', readOnly: true, getValue:'getValue.average_msg_tp'},
  918. ];
  919. // 月信息价方法集合
  920. const monthFunGather = {
  921. _setMonthHtml: function() {
  922. let html = '';
  923. let qihtml = '';
  924. for (const m of months) {
  925. html += '<div class="custom-control custom-checkbox mb-2">\n' +
  926. ' <input type="checkbox" name="del_month" value="' + m + '" class="custom-control-input" id="month_' + m + '">\n' +
  927. ' <label class="custom-control-label" for="month_' + m + '">' + m + '月</label>\n' +
  928. ' </div>';
  929. qihtml += parseInt(m.split('-')[1]) + '月,';
  930. }
  931. if (months.length > 0) {
  932. qihtml = '<span class="mx-2 text-muted">/</span>本期月信息价:' + qihtml;
  933. qihtml = qihtml.substring(0, qihtml.length-1);
  934. }
  935. $('#show_month').html(html);
  936. $('#qi-month').html(qihtml);
  937. },
  938. _monthHeaderSet: function() {
  939. const newMonths = [];
  940. for (const m of months) {
  941. const year = m.split('-')[0];
  942. const month = parseInt(m.split('-')[1]);
  943. let one = _.find(newMonths, { 'year': year });
  944. let oneIndex = _.findIndex(newMonths, { 'year': year });
  945. if (one) {
  946. one.month.push(month);
  947. newMonths.splice(oneIndex, 1, one);
  948. } else {
  949. one = {
  950. year: year,
  951. month: [ month ],
  952. };
  953. newMonths.push(one);
  954. }
  955. }
  956. const pushMonth = [];
  957. for(const mo of newMonths) {
  958. for (let i in mo.month) {
  959. i = parseInt(i);
  960. const newCols = {
  961. title: (i === 0 ? mo.year: '') + '|' + mo.month[i] + '月',
  962. colSpan: (i === 0 ? (mo.month.length === 1 ? '0' : mo.month.length) : '') + '|1',
  963. rowSpan: i === 0 ? '1|1' : '|1',
  964. field: mo.year + '-' + (mo.month[i] < 10 ? '0' + mo.month[i] : mo.month[i]),
  965. hAlign: 2, width: 60, type: '@', readOnly: 'readOnly.isEdit'};
  966. pushMonth.push(newCols);
  967. }
  968. }
  969. return pushMonth;
  970. },
  971. monthSheetReset: function (ml = monthsList) {
  972. const monthCols = monthFunGather._monthHeaderSet();
  973. const newMonthSpreadHeaderCols = static_cols.concat(monthCols);
  974. materialMonthSpreadSetting.cols = newMonthSpreadHeaderCols;
  975. // 表头变化需要重新绘制,不然报错;
  976. materialMonthSpread.getActiveSheet().reset();
  977. SpreadJsObj.initSpreadSettingEvents(materialMonthSpreadSetting, materialMonthCol);
  978. SpreadJsObj.initSheet(materialMonthSpread.getActiveSheet(), materialMonthSpreadSetting);
  979. SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, ml);
  980. monthFunGather._setMonthHtml();
  981. },
  982. monthsListSet: function (ml = materialBillsData, checked = true) {
  983. if($('#bills0_list').is(':checked') && checked) {
  984. ml = _.filter(materialBillsData, function (item) {
  985. return item.quantity === null || item.quantity === 0;
  986. });
  987. }
  988. const newMonthsList = _.filter(monthsList, function (item) {
  989. return _.findIndex(ml, { code: item.code }) !== -1;
  990. });
  991. monthFunGather.monthSheetReset(newMonthsList);
  992. }
  993. };
  994. const materialMonthSpread = SpreadJsObj.createNewSpread($('#material-month-spread')[0]);
  995. const materialMonthSpreadSetting = {
  996. cols: static_cols,
  997. emptyRows: 0,
  998. headRows: 2,
  999. headRowHeight: [25, 25],
  1000. defaultRowHeight: 21,
  1001. headerFont: '12px 微软雅黑',
  1002. font: '12px 微软雅黑',
  1003. readOnly: readOnly,
  1004. };
  1005. if (months.length > 0) {
  1006. const monthCols = monthFunGather._monthHeaderSet();
  1007. const monthSpreadHeaderCols = static_cols.concat(monthCols);
  1008. materialMonthSpreadSetting.cols = monthSpreadHeaderCols;
  1009. }
  1010. const materialMonthCol = {
  1011. getValue: {
  1012. average_msg_tp: function (data) {
  1013. let msg_tp = 0;
  1014. let hadnum = 0;
  1015. for (const m of months) {
  1016. msg_tp += data[m];
  1017. if (data[m] !== null && data[m] !== '' && data[m] !== 0) {
  1018. hadnum++;
  1019. }
  1020. }
  1021. const average_tp = hadnum !== 0 ? ZhCalc.round(ZhCalc.div(msg_tp, hadnum), materialDecimal.up) : ZhCalc.round(ZhCalc.div(msg_tp, months.length), materialDecimal.up);
  1022. return average_tp;
  1023. },
  1024. },
  1025. readOnly: {
  1026. isEdit: function (data) {
  1027. return readOnly;
  1028. },
  1029. },
  1030. };
  1031. materialMonthSpread.getActiveSheet().frozenColumnCount(5);
  1032. materialMonthSpread.getActiveSheet().options.frozenlineColor = '#f1f1f1';
  1033. SpreadJsObj.initSpreadSettingEvents(materialMonthSpreadSetting, materialMonthCol);
  1034. SpreadJsObj.initSheet(materialMonthSpread.getActiveSheet(), materialMonthSpreadSetting);
  1035. SpreadJsObj.loadSheetData(materialMonthSpread.getActiveSheet(), SpreadJsObj.DataType.Data, monthsList);
  1036. const materialMonthSpreadObj = {
  1037. editEnded: function (e, info) {
  1038. if (info.sheet.zh_setting) {
  1039. const select = SpreadJsObj.getSelectObject(info.sheet);
  1040. const col = info.sheet.zh_setting.cols[info.col];
  1041. // 未改变值则不提交
  1042. let validText = is_numeric(info.editingText) ? parseFloat(info.editingText) : (info.editingText ? trimInvalidChar(info.editingText) : null);
  1043. const orgValue = select[col.field];
  1044. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  1045. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1046. return;
  1047. }
  1048. if (col.field === 'origin') {
  1049. select[col.field] = validText;
  1050. postData(window.location.pathname + '/month/save', { type:'updateOrigin', updateData: { mb_id: select.mb_id, value: validText } }, function (result) {
  1051. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1052. materialBillsData = result.materialBillsData;
  1053. materialSpreadObj.materialSheetReset();
  1054. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1055. }, function () {
  1056. select[col.field] = orgValue;
  1057. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1058. });
  1059. } else {
  1060. // 判断部分值是否输入的是数字判断和数据计算
  1061. if (isNaN(validText)) {
  1062. toastr.error('不能输入其它非数字类型字符');
  1063. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1064. return;
  1065. }
  1066. const num = parseFloat(validText);
  1067. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  1068. if (validText !== null && (num < 0 || !reg.test(num))) {
  1069. toastr.warning('已保留'+ materialDecimal.up +'位小数');
  1070. validText = ZhCalc.round(num, materialDecimal.up);
  1071. }
  1072. select[col.field] = validText;
  1073. // 更新至服务器
  1074. postData(window.location.pathname + '/month/save', { type:'update', updateData: { mb_id: select.mb_id, yearmonth: col.field, value: validText } }, function (result) {
  1075. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1076. materialBillsData = result.materialBillsData;
  1077. materialSpreadObj.materialSheetReset();
  1078. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1079. m_tp = result.m_tp;
  1080. if (materialTax) {
  1081. m_tax_tp = result.m_tax_tp;
  1082. }
  1083. resetTpTable();
  1084. }, function () {
  1085. select[col.field] = orgValue;
  1086. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  1087. });
  1088. }
  1089. }
  1090. },
  1091. deletePress: function (sheet) {
  1092. // if (sheet.zh_setting && sheet.zh_data) {
  1093. // const sel = sheet.getSelections()[0];
  1094. // if (!sel) return;
  1095. //
  1096. // const col = sheet.zh_setting.cols[sel.col];
  1097. // const select = SpreadJsObj.getSelectObject(sheet);
  1098. // const orgValue = select[col.field];
  1099. // if (sel.colCount > 1 || sel.rowCount > 1) {
  1100. // toastr.warning('请勿同时删除多行或多列数据');
  1101. // }
  1102. // console.log(sel, select, col);
  1103. // if (orgValue === null || col.field === 'type' || col.field === 'is_summary') {
  1104. // return;
  1105. // }
  1106. // select[col.field] = null;
  1107. // select.calc_num = materialExponentCol.getValue.calc_num(select);
  1108. // console.log(select);
  1109. // 更新至服务器
  1110. // postData(window.location.pathname + '/save', { type:'update', updateData: select }, function (result) {
  1111. // ex_tp = result.ex_tp;
  1112. // ex_expr = result.ex_expr;
  1113. // resetExTpTable();
  1114. // SpreadJsObj.reLoadRowData(sheet, sel.row);
  1115. // materialExponentData.splice(sel.row, 1, select);
  1116. // }, function () {
  1117. // select[col.field] = orgValue;
  1118. // SpreadJsObj.reLoadRowData(sheet, sel.row);
  1119. // });
  1120. // }
  1121. return;
  1122. },
  1123. clipboardPasted(e, info) {
  1124. const hint = {
  1125. cellError: {type: 'error', msg: '粘贴内容超出了表格范围'},
  1126. numberExpr: {type: 'error', msg: '不能粘贴其它非数字类型字符'},
  1127. numberCan: {type: 'warning', msg: '已保留'+ materialDecimal.up +'位小数'},
  1128. };
  1129. const range = info.cellRange;
  1130. const sortData = info.sheet.zh_data || [];
  1131. if (info.cellRange.row + info.cellRange.rowCount > sortData.length) {
  1132. toastMessageUniq(hint.cellError);
  1133. SpreadJsObj.reLoadSheetHeader(materialMonthSpread.getActiveSheet());
  1134. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1135. return;
  1136. }
  1137. if (sortData.length > 0 && range.col + range.colCount > 5 + months.length) {
  1138. toastMessageUniq(hint.cellError);
  1139. SpreadJsObj.reLoadSheetHeader(materialMonthSpread.getActiveSheet());
  1140. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1141. return;
  1142. }
  1143. const data = [];
  1144. let pasteOrigin = false;
  1145. for (let iRow = 0; iRow < range.rowCount; iRow++) {
  1146. let bPaste = true;
  1147. const curRow = range.row + iRow;
  1148. const materialMonthData = sortData[curRow];
  1149. const hintRow = range.rowCount > 1 ? curRow : '';
  1150. let sameCol = 0;
  1151. for (let iCol = 0; iCol < range.colCount; iCol++) {
  1152. const curCol = range.col + iCol;
  1153. const colSetting = info.sheet.zh_setting.cols[curCol];
  1154. if (!colSetting) continue;
  1155. let validText = info.sheet.getText(curRow, curCol);
  1156. validText = is_numeric(validText) ? parseFloat(validText) : (validText ? trimInvalidChar(validText) : null);
  1157. const orgValue = sortData[curRow][colSetting.field];
  1158. if (orgValue == validText || ((!orgValue || orgValue === '') && (validText === ''))) {
  1159. sameCol++;
  1160. if (range.colCount === sameCol) {
  1161. bPaste = false;
  1162. }
  1163. continue;
  1164. }
  1165. if (colSetting.field === 'origin') {
  1166. pasteOrigin = true;
  1167. } else {
  1168. const num = parseFloat(validText);
  1169. if (isNaN(validText)) {
  1170. toastMessageUniq(getPasteHint(hint.numberExpr, hintRow));
  1171. bPaste = false;
  1172. continue;
  1173. }
  1174. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  1175. if (validText !== null && (num < 0 || !reg.test(num))) {
  1176. toastMessageUniq(getPasteHint(hint.numberCan, hintRow));
  1177. validText = ZhCalc.round(num, materialDecimal.up);
  1178. }
  1179. }
  1180. materialMonthData[colSetting.field] = validText;
  1181. sortData[curRow][colSetting.field] = validText;
  1182. }
  1183. if (bPaste) {
  1184. data.push(materialMonthData);
  1185. } else {
  1186. SpreadJsObj.reLoadRowData(info.sheet, curRow);
  1187. }
  1188. }
  1189. if (data.length === 0) {
  1190. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1191. return;
  1192. }
  1193. // console.log(data);
  1194. if (pasteOrigin) {
  1195. // 更新至服务器
  1196. postData(window.location.pathname + '/month/save', { type:'pasteOrigin', updateData: data }, function (result) {
  1197. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1198. materialBillsData = result.materialBillsData;
  1199. }, function () {
  1200. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1201. return;
  1202. });
  1203. } else {
  1204. // 更新至服务器
  1205. postData(window.location.pathname + '/month/save', { type:'paste', updateData: data }, function (result) {
  1206. SpreadJsObj.reLoadSheetData(materialMonthSpread.getActiveSheet());
  1207. materialBillsData = result.materialBillsData;
  1208. materialSpreadObj.materialSheetReset();
  1209. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1210. m_tp = result.m_tp;
  1211. if (materialTax) {
  1212. m_tax_tp = result.m_tax_tp;
  1213. }
  1214. resetTpTable();
  1215. }, function () {
  1216. SpreadJsObj.reLoadRowData(info.sheet, info.cellRange.row, info.cellRange.rowCount);
  1217. return;
  1218. });
  1219. }
  1220. },
  1221. };
  1222. // 应耗来源sjs
  1223. const material_source_cols = [
  1224. {title: '清单编号', colSpan: '1', rowSpan: '2', field: 'code', hAlign: 0, width: 100, formatter: '@'},
  1225. {title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 150, formatter: '@'},
  1226. {title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 70, formatter: '@'},
  1227. {title: '本期应耗', colSpan: '1', rowSpan: '2', field: 'quantity', hAlign: 2, width: 100, formatter: '@'},
  1228. ];
  1229. const materialSourceSpread = SpreadJsObj.createNewSpread($('#material-source-spread')[0]);
  1230. const materialSourceSpreadSetting = {
  1231. cols: material_source_cols,
  1232. emptyRows: 0,
  1233. headRows: 1,
  1234. headRowHeight: [32],
  1235. defaultRowHeight: 21,
  1236. headerFont: '12px 微软雅黑',
  1237. font: '12px 微软雅黑',
  1238. readOnly: true,
  1239. };
  1240. SpreadJsObj.initSheet(materialSourceSpread.getActiveSheet(), materialSourceSpreadSetting);
  1241. SpreadJsObj.loadSheetData(materialSourceSpread.getActiveSheet(), SpreadJsObj.DataType.Data, []);
  1242. let first = true;
  1243. function setListsData(sid = null, ms_id = null) {
  1244. if (first) {
  1245. // 加载清单数据 - 暂时统一加载,如有需要,切换成动态加载并缓存
  1246. postData(window.location.pathname + '/list/load', {sid}, async function (result) {
  1247. ledger = result.ledger;
  1248. curLedgerData = result.curLedgerData;
  1249. pos = result.pos;
  1250. curPosData = result.curPosData;
  1251. materialListData2 = result.materialListData;
  1252. gclList = result.gclList;
  1253. notJoinList = result.materialNotJoinListData;
  1254. selfList = result.materialSelfListData;
  1255. materialChecklistData = result.materialChecklistData;
  1256. if (isStageSelf) {
  1257. const newGclGatherListData = [];
  1258. for (const [index, s] of result.ledgerListData.entries()) {
  1259. gclGatherModel.loadLedgerData(_.cloneDeep(ledger), s);
  1260. gclGatherModel.loadPosData(_.cloneDeep(pos), result.posListData[index]);
  1261. const oneGclGatherData = gclGatherModel.gatherGclData().filter(item => {
  1262. return item.qc_qty || item.contract_qty || item.qc_minus_qty
  1263. });
  1264. newGclGatherListData.push(oneGclGatherData);
  1265. }
  1266. gclGatherListData = newGclGatherListData;
  1267. }
  1268. // 解析清单汇总数据
  1269. gclGatherModel.loadLedgerData(_.cloneDeep(ledger), curLedgerData);
  1270. gclGatherModel.loadPosData(_.cloneDeep(pos), curPosData);
  1271. gclGatherData = gclGatherModel.gatherGclData();
  1272. gclGatherData = gclGatherData.filter(item => {
  1273. return item.qc_qty || item.contract_qty || item.qc_minus_qty
  1274. });
  1275. if (openMaterialChecklist) {
  1276. // 取交集
  1277. const selfListGcl = _.uniq(_.map(selfList, 'gcl_id'));
  1278. gclGatherData = _.filter(gclGatherData, function (item) {
  1279. if (_.find(materialChecklistData, {
  1280. b_code: item.b_code,
  1281. name: item.name,
  1282. unit: item.unit,
  1283. unit_price: item.unit_price
  1284. })) {
  1285. return true;
  1286. } else {
  1287. const gcl_ids = item.leafXmjs ? _.uniq(_.map(item.leafXmjs, 'gcl_id')) : [];
  1288. const jiaoji = _.intersection(gcl_ids, selfListGcl);
  1289. if (jiaoji.length > 0) {
  1290. return true;
  1291. }
  1292. }
  1293. });
  1294. }
  1295. // calculateJiaCha(gclGatherData);
  1296. console.log(gclGatherData);
  1297. setCurBillSourceList(materialBillsData[0] ? materialBillsData[0].mb_id || materialBillsData[0].id : null, ms_id);
  1298. first = false;
  1299. // 判断是否需要更新contract_qty、qc_qty和qc_minus_qty
  1300. if (!materialIsNewQty && !readOnly && !editForAudit) {
  1301. let needUpdateList = [];
  1302. if (materialListData2.length > 0) {
  1303. if (isStageSelf) {
  1304. for (const msIndex in materialStageData) {
  1305. const materialStageList = _.filter(materialListData2, { ms_id: materialStageData[msIndex].id });
  1306. const gclIdList = _.uniq(_.map(materialListData2, 'gcl_id'));
  1307. let leafXmjList = [];
  1308. for (const id of gclIdList) {
  1309. const gcl = _.find(gclGatherListData[msIndex], function (item) {
  1310. return item.leafXmjs && item.leafXmjs.length > 0 && _.findIndex(item.leafXmjs, {gcl_id: id}) !== -1;
  1311. });
  1312. if (gcl) leafXmjList = [...leafXmjList, ...gcl.leafXmjs];
  1313. }
  1314. for (const ml of materialStageList) {
  1315. const leafXmjInfo = _.find(leafXmjList, function (item) {
  1316. return item.gcl_id === ml.gcl_id && item.id === ml.xmj_id && (item.mx_id === undefined || (item.mx_id !== undefined && item.mx_id === ml.mx_id));
  1317. });
  1318. if (leafXmjInfo) {
  1319. needUpdateList.push({ id: ml.id, contract_qty: leafXmjInfo.contract_qty, qc_qty: leafXmjInfo.qc_qty, qc_minus_qty: leafXmjInfo.qc_minus_qty });
  1320. }
  1321. }
  1322. }
  1323. } else {
  1324. // 找出所有gcl相同的,方便搜索gclGatherData值
  1325. const gclIdList = _.uniq(_.map(materialListData2, 'gcl_id'));
  1326. let leafXmjList = [];
  1327. for (const id of gclIdList) {
  1328. const gcl = _.find(gclGatherData, function (item) {
  1329. return item.leafXmjs && item.leafXmjs.length > 0 && _.findIndex(item.leafXmjs, {gcl_id: id}) !== -1;
  1330. });
  1331. if (gcl) leafXmjList = [...leafXmjList, ...gcl.leafXmjs];
  1332. }
  1333. for (const ml of materialListData2) {
  1334. const leafXmjInfo = _.find(leafXmjList, function (item) {
  1335. return item.gcl_id === ml.gcl_id && item.id === ml.xmj_id && (item.mx_id === undefined || (item.mx_id !== undefined && item.mx_id === ml.mx_id));
  1336. });
  1337. if (leafXmjInfo) {
  1338. needUpdateList.push({ id: ml.id, contract_qty: leafXmjInfo.contract_qty, qc_qty: leafXmjInfo.qc_qty, qc_minus_qty: leafXmjInfo.qc_minus_qty });
  1339. }
  1340. }
  1341. }
  1342. }
  1343. console.log(needUpdateList);
  1344. postData(window.location.pathname + '/save', { type: 'qty_update', needUpdateList }, function (result) {
  1345. materialListData2 = result.materialListData;
  1346. });
  1347. }
  1348. });
  1349. } else {
  1350. const i = _.findIndex(materialStageData, { sid });
  1351. gclGatherData = gclGatherListData[i];
  1352. gclGatherData = gclGatherData.filter(item => {
  1353. return item.qc_qty || item.contract_qty || item.qc_minus_qty
  1354. });
  1355. if (openMaterialChecklist) {
  1356. // 取交集
  1357. const selfListGcl = _.uniq(_.map(selfList, 'gcl_id'));
  1358. gclGatherData = _.filter(gclGatherData, function (item) {
  1359. if (_.find(materialChecklistData, {
  1360. b_code: item.b_code,
  1361. name: item.name,
  1362. unit: item.unit,
  1363. unit_price: item.unit_price
  1364. })) {
  1365. return true;
  1366. } else {
  1367. const gcl_ids = item.leafXmjs ? _.uniq(_.map(item.leafXmjs, 'gcl_id')) : [];
  1368. const jiaoji = _.intersection(gcl_ids, selfListGcl);
  1369. if (jiaoji.length > 0) {
  1370. return true;
  1371. }
  1372. }
  1373. });
  1374. }
  1375. // calculateJiaCha(gclGatherData);
  1376. console.log(gclGatherData);
  1377. }
  1378. }
  1379. setListsData(isStageSelf ? materialStageData[0].sid : null, isStageSelf ? materialStageData[0].id : null);
  1380. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  1381. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  1382. materialSpreadObj.getMaterialBillsData();
  1383. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1384. materialSpreadObj.refreshActn();
  1385. materialMonthSpread.bind(spreadNS.Events.ClipboardPasted, materialMonthSpreadObj.clipboardPasted);
  1386. SpreadJsObj.addDeleteBind(materialMonthSpread, materialMonthSpreadObj.deletePress);
  1387. materialMonthSpread.bind(spreadNS.Events.EditEnded, materialMonthSpreadObj.editEnded);
  1388. // 期切换
  1389. $('#myTab a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  1390. e.preventDefault();
  1391. showWaitingView();
  1392. setTimeout(function () {
  1393. materialSpreadObj.getMaterialBillsData();
  1394. materialSpreadObj.materialSheetReset(true);
  1395. // SpreadJsObj.reLoadSheetData(materialSpread.getActiveSheet());
  1396. // 消耗量表格更新
  1397. let html = '';
  1398. calcBase = _.find(calcBaseList, { ms_id: $('#myTab').find('.active').data('msid') }).calcBase;
  1399. for (let iBase = 0; iBase < calcBase.length; iBase++) {
  1400. if (!calcBase[iBase].cType || (calcBase[iBase].cType && calcBase[iBase].cType === 1 && calcBase[iBase].value)) {
  1401. html += '<tr><td>' + calcBase[iBase].name + '</td><td>' + calcBase[iBase].code + '</td><td>' + calcBase[iBase].value + '</td></tr>';
  1402. }
  1403. }
  1404. $('#calcBase').html(html);
  1405. const ms_id = parseInt($('#myTab').find('.active').data('msid'));
  1406. const msInfo = _.find(materialStageData, { id: ms_id });
  1407. setListsData(msInfo.sid, ms_id);
  1408. const data = SpreadJsObj.getSelectObject(materialSpread.getActiveSheet());
  1409. setCurBillSourceList(data ? (data.mb_id || data.id) : null, ms_id);
  1410. closeWaitingView();
  1411. }, 1000);
  1412. });
  1413. // 本条工料应耗来源数据信息
  1414. function setCurBillSourceList(mb_id, ms_id = null) {
  1415. const showSourceList = [];
  1416. if (mb_id) {
  1417. const list = _.filter(materialListData2, function (item) {
  1418. return item.mb_id === mb_id && item.ms_id === ms_id && (item.is_join === 1 || item.is_join === 2);
  1419. });
  1420. console.log(list);
  1421. if (list.length > 0) {
  1422. for (const l of list) {
  1423. const gcl = _.find(gclGatherData, function (item) {
  1424. return item.leafXmjs && item.leafXmjs.length > 0 && _.findIndex(item.leafXmjs, { gcl_id: l.gcl_id }) !== -1;
  1425. });
  1426. if (gcl) {
  1427. const index = _.findIndex(showSourceList, { code: gcl.b_code, name: gcl.name, unit: gcl.unit });
  1428. if (index === -1) {
  1429. showSourceList.push({
  1430. code: gcl.b_code,
  1431. name: gcl.name,
  1432. unit: gcl.unit,
  1433. quantity: ZhCalc.mul(calcQty(l), l.quantity),
  1434. });
  1435. } else {
  1436. showSourceList[index].quantity = ZhCalc.add(showSourceList[index].quantity, ZhCalc.mul(calcQty(l), l.quantity));
  1437. }
  1438. }
  1439. }
  1440. }
  1441. }
  1442. // 按清单编号排序
  1443. SpreadJsObj.loadSheetData(materialSourceSpread.getActiveSheet(), SpreadJsObj.DataType.Data, showSourceList.sort(sortByCode));
  1444. }
  1445. function calcQty(info) {
  1446. let qty = '';
  1447. switch (qtySource) {
  1448. case qtySourceValueConst.gather_qty: qty = info.gather_qty; break;
  1449. case qtySourceValueConst.contract_qty: qty = info.contract_qty; break;
  1450. case qtySourceValueConst.gather_minus_qty: qty = ZhCalc.add(info.gather_qty, info.qc_minus_qty); break;
  1451. default: throw '未配置计量来源出错';
  1452. }
  1453. if (qtySource !== qtySourceValueConst.contract_qty && info.is_join === 2) {
  1454. qty = info.contract_qty;
  1455. }
  1456. return qty;
  1457. }
  1458. if (!readOnly || editForAudit) {
  1459. $('#expr_select button').on('click', function () {
  1460. const code = $(this).text();
  1461. // $('#expr').val($('#expr').val() + code);
  1462. $('#expr').insertAtCaret(code);
  1463. });
  1464. const ExprObj = {
  1465. _checkExprValid(expr, invalidParam) {
  1466. if (!expr) return [true, null];
  1467. const param = [];
  1468. let num = '', base = '';
  1469. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  1470. if (/^[\d\.%]+/.test(expr[i])) {
  1471. if (base !== '') {
  1472. param.push({type: 'base', value: base});
  1473. base = '';
  1474. }
  1475. num = num + expr[i];
  1476. } else if (/^[a-z]/.test(expr[i])) {
  1477. if (num !== '') {
  1478. param.push({type: 'num', value: num});
  1479. num = '';
  1480. }
  1481. base = base + expr[i];
  1482. } else if (expr[i] === '(') {
  1483. if (num !== '') {
  1484. param.push({type: 'num', value: num});
  1485. num = '';
  1486. }
  1487. if (base !== '') {
  1488. param.push({type: 'base', value: base});
  1489. base = '';
  1490. }
  1491. param.push({type: 'left', value: '('});
  1492. } else if (expr[i] === ')') {
  1493. if (num !== '') {
  1494. param.push({type: 'num', value: num});
  1495. num = '';
  1496. }
  1497. if (base !== '') {
  1498. param.push({type: 'base', value: base});
  1499. base = '';
  1500. }
  1501. param.push({type: 'right', value: ')'});
  1502. } else if (/^[\+\-*\/]/.test(expr[i])) {
  1503. if (num !== '') {
  1504. param.push({type: 'num', value: num});
  1505. num = '';
  1506. }
  1507. if (base !== '') {
  1508. param.push({type: 'base', value: base});
  1509. base = '';
  1510. }
  1511. param.push({type: 'calc', value: expr[i]});
  1512. } else {
  1513. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  1514. }
  1515. }
  1516. if (num !== '') {
  1517. param.push({type: 'num', value: num});
  1518. num = '';
  1519. }
  1520. if (base !== '') {
  1521. param.push({type: 'base', value: base});
  1522. base = '';
  1523. }
  1524. if (param.length === 0) return true;
  1525. if (param.length > 1) {
  1526. if (param[0].value === '-') {
  1527. param[1].value = '-' + param[1];
  1528. }
  1529. param.unshift();
  1530. }
  1531. const iLen = param.length;
  1532. let iLeftCount = 0, iRightCount = 0;
  1533. for (const [i, p] of param.entries()) {
  1534. if (p.type === 'calc') {
  1535. if (i === 0 || i === iLen - 1)
  1536. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
  1537. }
  1538. if (p.type === 'num') {
  1539. num = p.value.replace('%', '');
  1540. if (p.value.length - num.length > 1)
  1541. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  1542. num = _.toNumber(num);
  1543. if (num === undefined || num === null || _.isNaN(num))
  1544. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  1545. if (i > 0) {
  1546. if (param[i - 1].type !== 'calc') {
  1547. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  1548. } else if (param[i - 1].value === '/' && num === 0) {
  1549. return [false, '输入的表达式非法:请勿除0'];
  1550. }
  1551. }
  1552. }
  1553. if (p.type === 'base') {
  1554. const baseParam = _.find(calcBase, {code: p.value});
  1555. if (!baseParam)
  1556. return [false, '输入的表达式非法:不存在计算基数' + p.value];
  1557. if (invalidParam && invalidParam.indexOf(p.value) >= 0)
  1558. return [false, '不可使用计算基数' + p.value];
  1559. if (i > 0 && (param[i - 1].type === 'num' || param[i - 1].type === 'right'))
  1560. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  1561. }
  1562. if (p.type === 'left') {
  1563. iLeftCount += 1;
  1564. if (i !== 0 && param[i-1].type !== 'calc')
  1565. return [false, '输入的表达式非法:(前应有运算符'];
  1566. }
  1567. if (p.type === 'right') {
  1568. iRightCount += 1;
  1569. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  1570. return [false, '输入的表达式非法:)后应有运算符'];
  1571. if (iRightCount > iLeftCount)
  1572. return [false, '输入的表达式非法:")"前无对应的"("'];
  1573. }
  1574. }
  1575. if (iLeftCount > iRightCount)
  1576. return [false, '输入的表达式非法:"("后无对应的")"'];
  1577. return [true, ''];
  1578. },
  1579. _checkExpr: function (text) {
  1580. if (text) {
  1581. const num = _.toNumber(text);
  1582. if (num) {
  1583. console.log(num);
  1584. } else {
  1585. const expr = text.replace('=', '').toLowerCase();
  1586. const [valid, msg] = this._checkExprValid(expr);
  1587. if (!valid) return [valid, msg];
  1588. }
  1589. }
  1590. return [true, ''];
  1591. },
  1592. };
  1593. $('#expr_btn').click(function () {
  1594. const expr = $('#expr').val();
  1595. // 判断表达式格式
  1596. const [valid, msg] = ExprObj._checkExpr(expr);
  1597. if (!valid) {
  1598. toastr.error(msg);
  1599. return false;
  1600. }
  1601. postData(window.location.pathname + '/save', { type:'expr', id: $('#materialbillsId').val(), expr: expr, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  1602. m_tp = result.m_tp;
  1603. if (materialTax) {
  1604. m_tax_tp = result.m_tax_tp;
  1605. }
  1606. const sheet = materialSpread.getActiveSheet();
  1607. const select = SpreadJsObj.getSelectObject(sheet);
  1608. const index = _.findIndex(materialBillsData, { id: select.id });
  1609. let newIndex = index;
  1610. if (isStageSelf) {
  1611. materialSpreadObj.updateMaterialData(result);
  1612. result.info = materialSpreadObj.updateOneMaterialBill(result.info);
  1613. }
  1614. if($('#bills0_list').is(':checked')) {
  1615. const newMaterialBillsData = _.filter(materialBillsData, function (item) {
  1616. return item.quantity !== null && item.quantity !== 0;
  1617. });
  1618. newIndex = _.findIndex(newMaterialBillsData, { id: select.id });
  1619. // newIndex = newMaterialBillsData.indexOf(select);
  1620. newMaterialBillsData.splice(newIndex, 1, result.info);
  1621. sheet.zh_data[newIndex] = result.info;
  1622. }
  1623. materialBillsData.splice(index, 1, result.info);
  1624. sheet.zh_data.splice(newIndex, 1, result.info);
  1625. SpreadJsObj.reLoadRowData(sheet, newIndex);
  1626. resetTpTable();
  1627. $('#bcyy').modal('hide');
  1628. });
  1629. });
  1630. }
  1631. if (!readOnly && !editForAudit) {
  1632. $('#add').click(materialSpreadObj.add);
  1633. $('#del').click(materialSpreadObj.del);
  1634. $('#up-move').click(materialSpreadObj.upMove);
  1635. $('#down-move').click(materialSpreadObj.downMove);
  1636. // 右键菜单
  1637. $.contextMenu({
  1638. selector: '#material-spread',
  1639. build: function ($trigger, e) {
  1640. const target = SpreadJsObj.safeRightClickSelection($trigger, e, materialSpread);
  1641. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  1642. },
  1643. items: {
  1644. 'create': {
  1645. name: '新增材料',
  1646. icon: 'fa-sign-in',
  1647. callback: function (key, opt) {
  1648. materialSpreadObj.add(materialSpread.getActiveSheet());
  1649. },
  1650. },
  1651. 'delete': {
  1652. name: '删除材料',
  1653. icon: 'fa-remove',
  1654. callback: function (key, opt) {
  1655. materialSpreadObj.del(materialSpread.getActiveSheet());
  1656. },
  1657. disabled: function (key, opt) {
  1658. const sheet = materialSpread.getActiveSheet();
  1659. const select = SpreadJsObj.getSelectObject(sheet);
  1660. const sel = sheet.getSelections()[0];
  1661. materialSpreadObj.refreshActn(sel.rowCount);
  1662. if (!readOnly && select && materialBase.isUsed(select) && sel.rowCount === 1) {
  1663. return false;
  1664. } else {
  1665. return true;
  1666. }
  1667. }
  1668. },
  1669. }
  1670. });
  1671. $('.changeRate').click(function () {
  1672. $('#rateInput').val(parseInt($(this).data('value')));
  1673. $('#rateInput').siblings('.dropdown-menu').hide();
  1674. });
  1675. $('#rateInput').click(function () {
  1676. $(this).siblings('.dropdown-menu').show();
  1677. })
  1678. // 回车提交
  1679. $('#rateInput').on('keypress', function () {
  1680. if(window.event.keyCode === 13) {
  1681. $(this).blur();
  1682. }
  1683. });
  1684. $('#rateInput').blur(function () {
  1685. const _self = $(this);
  1686. setTimeout(function () {
  1687. let rate = parseFloat(_self.val());
  1688. if (_.isNaN(rate)) {
  1689. toastr.error('请输入0-100之前的整数值');
  1690. $('#rateInput').val(materialRate);
  1691. return;
  1692. }
  1693. rate = _.round(rate);
  1694. if(rate < 0 || rate > 100) {
  1695. toastr.error('请输入0-100之前的整数值');
  1696. $('#rateInput').val(materialRate);
  1697. return;
  1698. }
  1699. $('#rateInput').siblings('.dropdown-menu').hide();
  1700. console.log(rate, materialRate);
  1701. if (rate !== materialRate) {
  1702. postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  1703. const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), materialDecimal.tp);
  1704. // const exbqhs = ZhCalc.round(ZhCalc.mul(ex_tp, 1+rate/100), materialDecimal.tp);
  1705. const jzbqhs = ZhCalc.round(ZhCalc.add(pre_tp_hs, bqhs), materialDecimal.tp);
  1706. // const exjzbqhs = ZhCalc.round(ZhCalc.add(ex_pre_tp_hs, exbqhs), materialDecimal.tp);
  1707. $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  1708. $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  1709. // $('#rate_set').find('td').eq(3).text(exbqhs !== 0 ? exbqhs : '');
  1710. // $('#rate_set').find('td').eq(4).text(exjzbqhs !== 0 ? exjzbqhs : '');
  1711. materialRate = rate;
  1712. $('#rateInput').val(rate);
  1713. });
  1714. } else {
  1715. $('#rateInput').val(rate);
  1716. }
  1717. }, 500);
  1718. });
  1719. // $('#changeRate').change(function () {
  1720. // const rate = parseInt($(this).val());
  1721. // postData(window.location.pathname + '/save', { type:'rate', rate: rate }, function (result) {
  1722. // const bqhs = ZhCalc.round(ZhCalc.mul(m_tp, 1+rate/100), materialDecimal.tp);
  1723. // const exbqhs = ZhCalc.round(ZhCalc.mul(ex_tp, 1+rate/100), materialDecimal.tp);
  1724. // const jzbqhs = ZhCalc.round(ZhCalc.add(pre_tp_hs, bqhs), materialDecimal.tp);
  1725. // const exjzbqhs = ZhCalc.round(ZhCalc.add(ex_pre_tp_hs, exbqhs), materialDecimal.tp);
  1726. // $('#rate_set').find('td').eq(1).text(bqhs !== 0 ? bqhs : '');
  1727. // $('#rate_set').find('td').eq(2).text(jzbqhs !== 0 ? jzbqhs : '');
  1728. // $('#rate_set').find('td').eq(3).text(exbqhs !== 0 ? exbqhs : '');
  1729. // $('#rate_set').find('td').eq(4).text(exjzbqhs !== 0 ? exjzbqhs : '');
  1730. // });
  1731. // });
  1732. // 创建月信息价
  1733. $('#make-month').click(function() {
  1734. const yearmonth = $('#months').val();
  1735. const result = yearmonth.match(/^(\d{1,4})(-|\/)(\d{1,2})$/);
  1736. if (result === null) {
  1737. toastr.error('请选择正确的信息价月份');
  1738. return false;
  1739. }
  1740. //判断是否已存在当前月份
  1741. if (months.indexOf(yearmonth) !== -1) {
  1742. toastr.error('调差期已创建过本月的信息价');
  1743. return false;
  1744. }
  1745. postData(window.location.pathname + '/month/save', { type: 'add', updateData: { yearmonth: yearmonth } }, function (data) {
  1746. months.push(yearmonth);
  1747. months.sort();
  1748. monthsList = data.monthsList;
  1749. monthFunGather.monthsListSet();
  1750. // 工料表单价显示也要更新
  1751. materialBillsData = data.materialBillsData;
  1752. materialSpreadObj.materialSheetReset();
  1753. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1754. m_tp = data.m_tp;
  1755. if (materialTax) {
  1756. m_tax_tp = data.m_tax_tp;
  1757. }
  1758. resetTpTable();
  1759. $('#add-month').modal('hide');
  1760. });
  1761. });
  1762. // 删除月信息价
  1763. $('#del-month').click(function () {
  1764. const del_month_array = [];
  1765. $("input[name='del_month']:checked").each(function () {
  1766. del_month_array.push($(this).val());
  1767. });
  1768. if (del_month_array.length === 0) {
  1769. toastr.error('请选择要移除的月信息价');
  1770. return false;
  1771. }
  1772. postData(window.location.pathname + '/month/save', { type: 'del', updateData: { del_yearmonth: del_month_array } }, function (data) {
  1773. for (const dm of del_month_array) {
  1774. // _.remove(materialMonthSpreadSetting.cols, function (n) {
  1775. // return n.field === dm;
  1776. // });
  1777. _.remove(months, function (n) {
  1778. return n === dm;
  1779. });
  1780. }
  1781. monthsList = data.monthsList;
  1782. monthFunGather.monthsListSet();
  1783. // 工料表单价显示也要更新
  1784. materialBillsData = data.materialBillsData;
  1785. materialSpreadObj.materialSheetReset();
  1786. // SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, materialBillsData);
  1787. m_tp = data.m_tp;
  1788. if (materialTax) {
  1789. m_tax_tp = data.m_tax_tp;
  1790. }
  1791. resetTpTable();
  1792. $('#remove-month').modal('hide');
  1793. });
  1794. });
  1795. // 导入功能
  1796. let importFile = null;
  1797. $('#upload-xls-file').change(function () {
  1798. const file = this.files[0];
  1799. importFile = file;
  1800. });
  1801. $('#import-bills-btn').click(function() {
  1802. if (!importFile) {
  1803. toastr.error('请选择excel文件再确定');
  1804. return;
  1805. }
  1806. const ext = importFile.name.toLowerCase().split('.').splice(-1)[0];
  1807. const imgStr = /(xls|xlsx|XLS|XLSX)$/;
  1808. if (!imgStr.test(ext)) {
  1809. toastr.error('请导入正确格式的excel文件。');
  1810. return
  1811. }
  1812. const fileReader = new FileReader();
  1813. fileReader.onload = async function(ev) {
  1814. try{
  1815. const data = ev.target.result;
  1816. let tree = [];
  1817. const includeSpec = $('#xls-spec').is(':checked');
  1818. if (/(xls|xlsx|XLS|XLSX)$/.test(ext)) {
  1819. const workbook = XLSX.read(data, {type: 'binary'}); // 以二进制流方式读取得到整份excel表格对象
  1820. const jsonData = transExcel(XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], { defval: null }));
  1821. console.log(jsonData);
  1822. if (!(jsonData[0] && jsonData[0].code !== undefined && jsonData[0].code !== null && jsonData[0].unit !== undefined && jsonData[0].name !== undefined &&
  1823. jsonData[0].spec !== undefined && jsonData[0].msg_tp !== undefined && jsonData[0].msg_times !== undefined)) {
  1824. throw 'excel必须按指定格式内容上传';
  1825. }
  1826. // 去重
  1827. tree = _.unionWith(jsonData, function (item1, item2) {
  1828. return item1.code === item2.code && item1.name === item2.name && item1.unit === item2.unit && (!includeSpec || (includeSpec && item1.spec === item2.spec));
  1829. });
  1830. }
  1831. if (tree.length === 0) {
  1832. throw '该excel不存在导入的数据';
  1833. }
  1834. // 判断msg_tp格式,不符合则无法导入,还有excel日期转换问题
  1835. for (const t of tree) {
  1836. t.code = t.code.toString();
  1837. if (t.msg_tp) {
  1838. if (_.isNumber(t.msg_tp)) {
  1839. const num = parseFloat(t.msg_tp);
  1840. const reg = materialDecimal.up ? new RegExp("^\\d+(\\.\\d{1,"+ materialDecimal.up +"})?$") : new RegExp("^\\d+?$");
  1841. if (num < 0 || !reg.test(num)) {
  1842. t.msg_tp = ZhCalc.round(num, materialDecimal.up);
  1843. }
  1844. } else {
  1845. throw '信息价数据存在非数字字符,无法导入';
  1846. }
  1847. }
  1848. if (_.isNumber(t.msg_times)) {
  1849. t.msg_times = formatDate(t.msg_times, '/');
  1850. }
  1851. t.spec = t.spec === null ? '' : t.spec;
  1852. }
  1853. console.log(tree);
  1854. postData(window.location.pathname + '/save', { type:'export', postData: tree, includeSpec, ms_id: $('#myTab').find('.active').data('msid') || null }, function (result) {
  1855. m_tp = result.m_tp;
  1856. if (materialTax) {
  1857. m_tax_tp = result.m_tax_tp;
  1858. }
  1859. materialBillsData = result.billsData;
  1860. if (isStageSelf) {
  1861. materialStageData = result.stageData;
  1862. materialStageBillsData = result.stageBillsData;
  1863. }
  1864. materialSpreadObj.getMaterialBillsData();
  1865. materialSpreadObj.materialSheetReset(true);
  1866. if (months.length > 0) {
  1867. monthsList = result.monthsList;
  1868. monthFunGather.monthsListSet();
  1869. }
  1870. resetTpTable();
  1871. if (result.addNum || result.updateNum) {
  1872. let msg = [];
  1873. if (result.addNum !== undefined && result.addNum !== 0) {
  1874. msg.push('成功导入工料 ' + result.addNum + ' 条');
  1875. }
  1876. if (result.updateNum !== undefined && result.updateNum !== 0) {
  1877. msg.push('成功更新工料 ' + result.updateNum + ' 条');
  1878. }
  1879. toastr.success(msg.length > 1 ? msg.join(',并') : msg[0]);
  1880. } else {
  1881. toastr.warning('不存在可导入的信息价数据');
  1882. }
  1883. $('#import-info').modal('hide');
  1884. });
  1885. } catch (error) {
  1886. console.log(error);
  1887. toastr.error(error);
  1888. return
  1889. }
  1890. };
  1891. // 以二进制方式打开文件
  1892. fileReader.readAsBinaryString(importFile);
  1893. $('#upload-xls-file').val('');
  1894. importFile = null;
  1895. });
  1896. } else {
  1897. // SpreadJsObj.forbiddenSpreadContextMenu('#material-spread', materialSpread);
  1898. }
  1899. // 筛选调差工料本期应耗数量为0
  1900. $('#bills0_list').click(function () {
  1901. const isCheck = $(this).is(':checked');
  1902. let newMaterialBillsData = materialBillsData;
  1903. if (isCheck) {
  1904. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  1905. return item.quantity !== null && item.quantity !== 0;
  1906. });
  1907. SpreadJsObj.initSpreadSettingEvents(materialSpreadSetting, materialCol);
  1908. SpreadJsObj.initSheet(materialSpread.getActiveSheet(), materialSpreadSetting);
  1909. }
  1910. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  1911. monthFunGather.monthsListSet(newMaterialBillsData, false);
  1912. materialSpreadObj.refreshActn();
  1913. });
  1914. let gljLib;
  1915. const gljLibCellDoubleClick = function (e, info) {
  1916. if (readOnly || editForAudit) return;
  1917. const gljSheet = info.sheet;
  1918. if (!gljSheet.zh_setting || !gljSheet.zh_tree) { return; }
  1919. const gljTree = gljSheet.zh_tree;
  1920. const gljNode = gljTree.nodes[info.row];
  1921. const sheet = materialSpread.getActiveSheet();
  1922. const select = SpreadJsObj.getSelectObject(sheet);
  1923. if (!gljNode) return;
  1924. if (gljNode && gljNode.is_leaf === 0) {
  1925. toastr.warning('非最底层项目下,不应添加');
  1926. return;
  1927. }
  1928. postData(window.location.pathname + '/save', { type:'add-glj', postData: gljNode, order: select ? select.order : null }, function (result) {
  1929. if (result) {
  1930. // 比order大的+1
  1931. const maxData = select ? _.filter(materialBillsData, (item) => { return item.order > select.order }) : [];
  1932. _.forEach(maxData, function (item) {
  1933. const cl = _.find(materialBillsData, { id: item.id });
  1934. cl.order = item.order + 1;
  1935. });
  1936. materialBillsData.push(result.info);
  1937. materialBillsData.sort(function (a, b) {
  1938. return a.order - b.order
  1939. });
  1940. console.log(materialBillsData);
  1941. if (isStageSelf) {
  1942. materialSpreadObj.updateMaterialData(result);
  1943. materialSpreadObj.getMaterialBillsData();
  1944. }
  1945. // sheet.addRows(materialBillsData.length - 1, 1);
  1946. // SpreadJsObj.reLoadRowData(sheet, materialBillsData.length - 1);
  1947. let newMaterialBillsData = materialBillsData;
  1948. if($('#bills0_list').is(':checked')) {
  1949. newMaterialBillsData = _.filter(materialBillsData, function (item) {
  1950. return item.quantity !== null && item.quantity !== 0;
  1951. });
  1952. }
  1953. SpreadJsObj.loadSheetData(materialSpread.getActiveSheet(), SpreadJsObj.DataType.Data, newMaterialBillsData);
  1954. // materialSpreadObj.materialSheetReset();
  1955. sheet.setSelection(sheet.zh_data.indexOf(select) + 1, 0, 1, 1);
  1956. materialSpreadObj.refreshActn();
  1957. // 月信息价需要同时添加空白的list
  1958. if (months.length > 0) {
  1959. const one_month ={ mb_id: result.info.id, code: result.info.code, name: result.info.name, origin: result.info.origin, unit: result.info.unit, order: result.info.order };
  1960. for (const m of months) {
  1961. one_month[m] = null;
  1962. }
  1963. _.forEach(maxData, function (item) {
  1964. const cl = _.find(monthsList, { mb_id: item.id });
  1965. cl.order = item.order + 1;
  1966. });
  1967. monthsList.push(one_month);
  1968. monthsList.sort(function (a, b) {
  1969. return a.order - b.order
  1970. });
  1971. console.log(monthsList);
  1972. // const newMonthsList = _.filter(monthsList, function (item) {
  1973. // return _.findIndex(newMaterialBillsData, { code: item.code }) !== -1;
  1974. // });
  1975. // materialMonthSpread.getActiveSheet().addRows(newMonthsList.length - 1, 1);
  1976. // SpreadJsObj.reLoadRowData(materialMonthSpread.getActiveSheet(), newMonthsList.length - 1);
  1977. monthFunGather.monthsListSet();
  1978. // materialMonthSpreadObj.monthsListSet();
  1979. }
  1980. }
  1981. });
  1982. };
  1983. const gljLibSetting = {
  1984. selector: '#material-tab',
  1985. stdType: 'glj',
  1986. libs: gljLists,
  1987. treeSetting: {
  1988. id: 'glj_id',
  1989. pid: 'glj_pid',
  1990. order: 'order',
  1991. level: 'level',
  1992. rootId: -1,
  1993. keys: ['id', 'list_id', 'glj_id'],
  1994. },
  1995. spreadSetting: {
  1996. cols: [
  1997. {title: '编号', field: 'code', hAlign: 0, width: 120, formatter: '@', readOnly: true, cellType: 'tree'},
  1998. {title: '名称', field: 'name', hAlign: 0, width: 150, formatter: '@', readOnly: true},
  1999. {title: '单位', field: 'unit', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  2000. {title: '参考上涨幅度', field: 'rise_range', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  2001. {title: '参考下跌幅度', field: 'fall_range', hAlign: 1, width: 50, formatter: '@', readOnly: true},
  2002. ],
  2003. treeCol: 0,
  2004. emptyRows: 0,
  2005. headRows: 1,
  2006. headRowHeight: [32],
  2007. defaultRowHeight: 21,
  2008. headerFont: '12px 微软雅黑',
  2009. font: '12px 微软雅黑',
  2010. headColWidth: [30],
  2011. selectedBackColor: '#fffacd',
  2012. },
  2013. spreadClassCss: 'sjs-material',
  2014. cellDoubleClick: gljLibCellDoubleClick,
  2015. page: 'material',
  2016. tid: window.location.pathname.split('/')[2],
  2017. };
  2018. $.subMenu({
  2019. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  2020. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  2021. key: 'menu.1.0.0',
  2022. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  2023. callback: function (info) {
  2024. if (info.mini) {
  2025. $('.panel-title').addClass('fluid');
  2026. $('#sub-menu').removeClass('panel-sidebar');
  2027. } else {
  2028. $('.panel-title').removeClass('fluid');
  2029. $('#sub-menu').addClass('panel-sidebar');
  2030. }
  2031. autoFlashHeight();
  2032. materialSpread.refresh();
  2033. }
  2034. });
  2035. $.divResizer({
  2036. select: '#right-spr',
  2037. callback: function () {
  2038. materialSpread.refresh();
  2039. materialMonthSpread.refresh();
  2040. if (gljLib) gljLib.spread.refresh();
  2041. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  2042. setLocalCache('material_month_' + materialID, width);
  2043. }
  2044. });
  2045. $.divResizer({
  2046. select: '#main-resize',
  2047. callback: function () {
  2048. let bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  2049. $(".sp-wrap").height(bcontent-30);
  2050. const height = $('.bcontent-wrap').height();
  2051. const cutHeight = isStageSelf ? getObjHeight($('#myTab')) : 0;
  2052. $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight);
  2053. updateSjsHeight();
  2054. materialSpread.refresh();
  2055. materialMonthSpread.refresh();
  2056. if (gljLib) gljLib.spread.refresh();
  2057. setLocalCache('material_bills_' + materialID, height);
  2058. }
  2059. });
  2060. // 展开收起月信息价并浏览器记住本期展开收起
  2061. $('a', '.right-nav').bind('click', function () {
  2062. //const main = $('#main-view'), tool = $('#tools-view');
  2063. const tab = $(this), tabPanel = $(tab.attr('content'));
  2064. if (!tab.hasClass('active')) {
  2065. $('a', '.side-menu').removeClass('active');
  2066. $('#right-view .tab-content .tab-pane').removeClass('active');
  2067. tab.addClass('active');
  2068. tabPanel.addClass('active');
  2069. showSideTools(tab.hasClass('active'));
  2070. if (tab.attr('content') === '#month-tab') {
  2071. const width = (($('#right-view').width()/$('#right-view').parent('div').width())*100).toFixed();
  2072. setLocalCache('material_month_' + materialID, width);
  2073. materialMonthSpread.refresh();
  2074. } else if (tab.attr('content') === '#material-tab') {
  2075. if (!gljLib) {
  2076. gljLib = $.gljLib(gljLibSetting);
  2077. }
  2078. updateSjsHeight();
  2079. gljLib.spread.refresh();
  2080. }
  2081. } else {
  2082. removeLocalCache('material_month_' + materialID);
  2083. tab.removeClass('active');
  2084. tabPanel.removeClass('active');
  2085. showSideTools(tab.hasClass('active'));
  2086. }
  2087. materialSpread.refresh();
  2088. // materialMonthSpread.refresh();
  2089. });
  2090. // 根据浏览器记录展开收起
  2091. if (getLocalCache('material_month_' + materialID) && !isStageSelf) {
  2092. const tab = $('.right-nav a[content="#month-tab"]'), tabPanel = $(tab.attr('content'));
  2093. $('a', '.side-menu').removeClass('active');
  2094. $('.tab-content .tab-pane').removeClass('active');
  2095. tab.addClass('active');
  2096. tabPanel.addClass('active');
  2097. $('#right-view').width(getLocalCache('material_month_' + materialID) + '%');
  2098. showSideTools(tab.hasClass('active'));
  2099. materialSpread.refresh();
  2100. materialMonthSpread.refresh();
  2101. }
  2102. const cutHeight = isStageSelf ? getObjHeight($('#myTab')) : 0;
  2103. if (getLocalCache('material_bills_' + materialID)) {
  2104. $('.bcontent-wrap').height(getLocalCache('material_bills_' + materialID));
  2105. const cHeader = getObjHeight($(".c-header"));
  2106. const bcontent = $(".bcontent-wrap") ? $(".bcontent-wrap").height() : 0;
  2107. $(".sp-wrap").height(bcontent-30);
  2108. $('.sjs-height-1').height($(window).height()-cHeader-bcontent-90+53);
  2109. $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight);
  2110. updateSjsHeight();
  2111. materialSpread.refresh();
  2112. materialMonthSpread.refresh();
  2113. if (gljLib) gljLib.spread.refresh();
  2114. } else {
  2115. $('.sjs-height-material').height($('.sjs-height-1').height() - cutHeight);
  2116. updateSjsHeight();
  2117. materialSpread.refresh();
  2118. materialMonthSpread.refresh();
  2119. if (gljLib) gljLib.spread.refresh();
  2120. }
  2121. function updateSjsHeight() {
  2122. $('.sjs-height-material-month').height($('.sjs-height-1').height() - getObjHeight($('.sjs-bar-1')));
  2123. $('.material-table-height').height($('.bcontent-wrap').height() - getObjHeight($('.bc-bar')));
  2124. $('.sjs-material').height($('.sjs-height-1').height() - getObjHeight($('.sjs-bar')));
  2125. }
  2126. function getObjHeight(select) {
  2127. return select.length > 0 ? select.height() : 0;
  2128. }
  2129. function formatDate(numb, format) {
  2130. const old = numb - 1;
  2131. const t = Math.round((old - Math.floor(old)) * 24 * 60 * 60);
  2132. const time = new Date(1900, 0, old, 0, 0, t);
  2133. const year = time.getFullYear();
  2134. const month = time.getMonth() + 1;
  2135. const date = time.getDate();
  2136. return year + format + (month < 10 ? '0' + month : month) + format + (date < 10 ? '0' + date : date);
  2137. }
  2138. function transExcel(results) {
  2139. const mapInfo = {
  2140. '编号': 'code',
  2141. '名称': 'name',
  2142. '单位': 'unit',
  2143. '规格': 'spec',
  2144. '信息价': 'msg_tp',
  2145. '时间': 'msg_times',
  2146. };
  2147. return results.map(zhObj => {
  2148. const enObj = {}
  2149. const zhKeys = Object.keys(zhObj);
  2150. zhKeys.forEach(zhKey => {
  2151. const enKey = mapInfo[zhKey];
  2152. enObj[enKey] = zhObj[zhKey] && _.isString(zhObj[zhKey]) ? _.trim(zhObj[zhKey]) : zhObj[zhKey];
  2153. });
  2154. return enObj
  2155. })
  2156. }
  2157. });