material.js 87 KB

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