revise_price.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const showSideTools = function (show) {
  10. const left = $('#left-view'), right = $('#right-view'), parent = left.parent();
  11. if (show) {
  12. right.show();
  13. autoFlashHeight();
  14. const percent = 100 - right.outerWidth() /parent.width() * 100;
  15. left.css('width', percent + '%');
  16. } else {
  17. left.width(parent.width());
  18. right.hide();
  19. }
  20. };
  21. const setPriceHint = function (show) {
  22. const hinticon = show ? 'fa-bell' : undefined;
  23. subMiniMenu.$children[2].hinticon = hinticon;
  24. subMenu.$children[2].hinticon = hinticon;
  25. };
  26. $(document).ready(() => {
  27. const ledgerGclSpreadSetting = {
  28. cols: [
  29. { title: '清单编号', colSpan: '1', rowSpan: '2', field: 'b_code', hAlign: 0, width: 80, formatter: '@' },
  30. { title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 200, formatter: '@' },
  31. { title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 60, formatter: '@' },
  32. { title: '单价', colSpan: '1', rowSpan: '2', field: 'unit_price', hAlign: 2, width: 80, type: 'Number' },
  33. ],
  34. emptyRows: 0,
  35. headRows: 1,
  36. headRowHeight: [32],
  37. headColWidth: [30],
  38. defaultRowHeight: 21,
  39. headerFont: '12px 微软雅黑',
  40. font: '12px 微软雅黑',
  41. readOnly: true,
  42. };
  43. const priceSpreadSetting = {
  44. cols: [
  45. { title: '清单编号', colSpan: '1', rowSpan: '2', field: 'b_code', hAlign: 0, width: 100, formatter: '@', readOnly: true },
  46. { title: '名称', colSpan: '1', rowSpan: '2', field: 'name', hAlign: 0, width: 210, formatter: '@', readOnly: true },
  47. { title: '单位', colSpan: '1', rowSpan: '2', field: 'unit', hAlign: 1, width: 60, formatter: '@', readOnly: true },
  48. { title: '当前单价', colSpan: '1', rowSpan: '2', field: 'org_price', hAlign: 2, width: 80, type: 'Number', readOnly: true },
  49. { title: '调整后单价', colSpan: '1', rowSpan: '2', field: 'new_price', hAlign: 2, width: 80, type: 'Number' },
  50. { title: '备注', colSpan: '1', rowSpan: '2', field: 'memo', hAlign: 2, width: 150, formatter: '@' },
  51. ],
  52. emptyRows: 0,
  53. headRows: 1,
  54. headRowHeight: [32],
  55. headColWidth: [30],
  56. defaultRowHeight: 21,
  57. headerFont: '12px 微软雅黑',
  58. font: '12px 微软雅黑',
  59. readOnly,
  60. };
  61. autoFlashHeight();
  62. const priceSpread = SpreadJsObj.createNewSpread($('#price-spread')[0]);
  63. const priceSheet = priceSpread.getActiveSheet();
  64. SpreadJsObj.initSheet(priceSheet, priceSpreadSetting);
  65. class RevisePrice {
  66. constructor () {
  67. this.data = [];
  68. }
  69. resortData() {
  70. this.data.sort(function (a, b) {
  71. return a.order - b.order;
  72. });
  73. }
  74. loadDatas(datas) {
  75. this.data = datas;
  76. this.resortData();
  77. }
  78. loadUpdateData(updateData) {
  79. if (updateData.add) {
  80. for (const a of updateData.add) {
  81. this.data.push(a);
  82. }
  83. }
  84. if (updateData.update) {
  85. for (const u of updateData.update) {
  86. const d = this.data.find(function (x) {
  87. return u.id === x.id;
  88. });
  89. if (d) {
  90. _.assign(d, u);
  91. } else {
  92. this.data.push(d);
  93. }
  94. }
  95. }
  96. if (updateData.del) {
  97. _.remove(this.data, function (d) {
  98. return updateData.del.indexOf(d.id) >= 0;
  99. });
  100. }
  101. this.resortData();
  102. }
  103. }
  104. const revisePrice = new RevisePrice();
  105. const priceOprObj = {
  106. addRevisePrice(data) {
  107. const op = revisePrice.data.find(x => {
  108. return x.b_code === data.b_code && x.name === x.name && x.unit === x.unit && checkZero(ZhCalc.sub(x.org_price, data.unit_price));
  109. });
  110. if (op) {
  111. toastr.warning('已存在该单价调整');
  112. SpreadJsObj.locateData(priceSheet, op);
  113. return;
  114. }
  115. postData(window.location.pathname + '/update', { add: { b_code: data.b_code, name: data.name, unit: data.unit, unit_price: data.unit_price } }, result => {
  116. revisePrice.loadUpdateData(result);
  117. SpreadJsObj.reLoadSheetData(priceSheet);
  118. setPriceHint(revisePrice.data.length > 0);
  119. });
  120. },
  121. /**
  122. * 删除按钮响应事件
  123. * @param sheet
  124. */
  125. deletePress: function (sheet) {
  126. if (!sheet.zh_setting || readOnly) return;
  127. const sortData = sheet.zh_data;
  128. const datas = [];
  129. const sels = sheet.getSelections();
  130. if (!sels || !sels[0]) return;
  131. for (let iRow = sels[0].row; iRow < sels[0].row + sels[0].rowCount; iRow++) {
  132. let bDel = false;
  133. const node = sortData[iRow];
  134. if (node) {
  135. const data = {id: node.id};
  136. for (let iCol = sels[0].col; iCol < sels[0].col + sels[0].colCount; iCol++) {
  137. const style = sheet.getStyle(iRow, iCol);
  138. if (!style.locked) {
  139. const colSetting = sheet.zh_setting.cols[iCol];
  140. data[colSetting.field] = null;
  141. bDel = true;
  142. }
  143. }
  144. if (bDel) {
  145. datas.push(data);
  146. }
  147. }
  148. }
  149. if (datas.length > 0) {
  150. postData(window.location.pathname + '/update', {update: datas}, function (result) {
  151. revisePrice.loadUpdateData(result);
  152. SpreadJsObj.reLoadSheetData(priceSheet);
  153. }, function () {
  154. SpreadJsObj.reLoadSheetData(priceSheet);
  155. });
  156. }
  157. },
  158. delete: function (sheet) {
  159. if (!sheet.zh_setting || readOnly) return;
  160. const sortData = sheet.zh_data;
  161. const datas = [];
  162. const sels = sheet.getSelections();
  163. if (!sels || !sels[0]) return;
  164. for (let iRow = sels[0].row, iLen = sels[0].row + sels[0].rowCount; iRow < iLen; iRow++) {
  165. const node = sortData[iRow];
  166. datas.push(node.id);
  167. }
  168. if (datas.length > 0) {
  169. postData(window.location.pathname + '/update', {del: datas}, function (result) {
  170. revisePrice.loadUpdateData(result);
  171. SpreadJsObj.reLoadSheetData(priceSheet);
  172. setPriceHint(revisePrice.data.length > 0);
  173. }, function () {
  174. SpreadJsObj.reLoadSheetData(priceSheet);
  175. });
  176. }
  177. },
  178. editEnded: function (e, info) {
  179. if (!info.sheet.zh_setting || !info.sheet.zh_data) return;
  180. const node = info.sheet.zh_data[info.row];
  181. if (!node) return;
  182. const col = info.sheet.zh_setting.cols[info.col];
  183. const data = { update: { id: node.id, org_price: node.org_price } };
  184. const oldValue = node ? node[col.field] : null;
  185. const newValue = trimInvalidChar(info.editingText);
  186. if (oldValue == info.editingText || ((!oldValue || oldValue === '') && (newValue === ''))) {
  187. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  188. return;
  189. }
  190. if (col.type === 'Number') {
  191. const num = _.toNumber(newValue);
  192. if (num) data.update[col.field] = num;
  193. } else {
  194. data.update[col.field] = newValue;
  195. }
  196. postData(window.location.pathname + '/update', data, function (result) {
  197. revisePrice.loadUpdateData(result);
  198. SpreadJsObj.reLoadSheetData(info.sheet);
  199. }, function () {
  200. SpreadJsObj.reLoadRowData(info.sheet, info.row);
  201. });
  202. },
  203. clipboardPasting(e, info) {
  204. const setting = info.sheet.zh_setting, sortData = info.sheet.zh_data;
  205. info.cancel = true;
  206. if (!setting || !sortData) return;
  207. const pasteData = info.pasteData.html
  208. ? SpreadJsObj.analysisPasteHtml(info.pasteData.html)
  209. : (info.pasteData.text === ''
  210. ? SpreadJsObj.Clipboard.getAnalysisPasteText()
  211. : SpreadJsObj.analysisPasteText(info.pasteData.text));
  212. const uDatas = [];
  213. for (let iRow = 0; iRow < info.cellRange.rowCount; iRow++) {
  214. const curRow = info.cellRange.row + iRow;
  215. const node = sortData[curRow];
  216. let bPaste = false;
  217. const data = {};
  218. for (let iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  219. const curCol = info.cellRange.col + iCol;
  220. const colSetting = setting.cols[curCol];
  221. const value = trimInvalidChar(pasteData[iRow][iCol]);
  222. if (colSetting.type === 'Number') {
  223. const num = _.toNumber(value);
  224. if (num) {
  225. data[colSetting.field] = num;
  226. bPaste = true;
  227. }
  228. } else {
  229. data[colSetting.field] = value;
  230. bPaste = true;
  231. }
  232. }
  233. if (bPaste) {
  234. data.id = node.id;
  235. uDatas.push(data);
  236. }
  237. }
  238. const updateData = {};
  239. if (uDatas.length > 0) updateData.update = uDatas;
  240. if (uDatas.length > 0) {
  241. postData(window.location.pathname + '/update', updateData, function (result) {
  242. revisePrice.loadUpdateData(result);
  243. SpreadJsObj.reLoadSheetData(info.sheet);
  244. });
  245. } else {
  246. SpreadJsObj.reLoadSheetData(info.sheet);
  247. }
  248. },
  249. upMove: function () {
  250. const sels = priceSheet.getSelections(), sortData = priceSheet.zh_data;
  251. const node = sortData[sels[0].row];
  252. const preNode = sortData[sels[0].row - 1];
  253. const data = [
  254. {id: node.id, order: preNode.order},
  255. {id: preNode.id, order: node.order}
  256. ];
  257. postData(window.location.pathname + '/update', {update: data}, function (result) {
  258. revisePrice.loadUpdateData(result);
  259. SpreadJsObj.reLoadRowsData(priceSheet, [sels[0].row, sels[0].row - 1]);
  260. priceSheet.setSelection(sels[0].row - 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  261. });
  262. },
  263. downMove: function () {
  264. const sels = priceSheet.getSelections(), sortData = priceSheet.zh_data;
  265. const node = sortData[sels[0].row];
  266. const nextNode = sortData[sels[0].row + 1];
  267. const data = [
  268. {id: node.id, order: nextNode.order},
  269. {id: nextNode.id, order: node.order}
  270. ];
  271. postData(window.location.pathname + '/update', {update: data}, function (result) {
  272. revisePrice.loadUpdateData(result);
  273. SpreadJsObj.reLoadRowsData(priceSheet, [sels[0].row, sels[0].row + 1]);
  274. priceSheet.setSelection(sels[0].row + 1, sels[0].col, sels[0].rowCount, sels[0].colCount);
  275. });
  276. }
  277. };
  278. if (!readOnly) {
  279. priceSheet.bind(spreadNS.Events.EditEnded, priceOprObj.editEnded);
  280. priceSheet.bind(spreadNS.Events.ClipboardPasting, priceOprObj.clipboardPasting);
  281. SpreadJsObj.addDeleteBind(priceSpread, priceOprObj.deletePress);
  282. $.contextMenu({
  283. selector: '#price-spread',
  284. build: function ($trigger, e) {
  285. const target = SpreadJsObj.safeRightClickSelection($trigger, e, priceSpread);
  286. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  287. },
  288. items: {
  289. del: {
  290. name: '删除',
  291. icon: 'fa-remove',
  292. callback: function (key, opt) {
  293. priceOprObj.delete(priceSheet);
  294. },
  295. disabled: function (key, opt) {
  296. const node = SpreadJsObj.getSelectObject(priceSheet);
  297. return node === undefined || node === null;
  298. },
  299. visible: function (key, opt) {
  300. return !readOnly;
  301. }
  302. },
  303. sprDel: '------------',
  304. upMove: {
  305. name: '上移',
  306. icon: 'fa-arrow-up',
  307. callback: function (key, opt) {
  308. priceOprObj.upMove();
  309. },
  310. disabled: function (key, opt) {
  311. const sels = priceSheet.getSelections();
  312. if (!sels || !sels[0] || sels[0].row === 0) return true;
  313. const row = sels[0].row;
  314. const node = revisePrice.data[row];
  315. return node === undefined || node === null;
  316. },
  317. visible: function (key, opt) {
  318. return !readOnly;
  319. }
  320. },
  321. downMove: {
  322. name: '下移',
  323. icon: 'fa-arrow-down',
  324. callback: function (key, opt) {
  325. priceOprObj.downMove();
  326. },
  327. disabled: function (key, opt) {
  328. const sels = priceSheet.getSelections();
  329. if (!sels || !sels[0] || sels[0].row >= revisePrice.data.length - 1) return true;
  330. const row = sels[0].row;
  331. const node = revisePrice.data[row];
  332. return node === undefined || node === null;
  333. },
  334. visible: function (key, opt) {
  335. return !readOnly;
  336. }
  337. }
  338. },
  339. });
  340. }
  341. class LedgerGcl {
  342. constructor(setting) {
  343. this.setting = setting;
  344. this.spread = SpreadJsObj.createNewSpread($(this.setting.selector)[0]);
  345. this.sheet = this.spread.getActiveSheet();
  346. SpreadJsObj.initSheet(this.sheet, this.setting.spreadSetting);
  347. if (!readOnly) {
  348. this.spread.bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  349. const gcl = SpreadJsObj.getSelectObject(info.sheet);
  350. priceOprObj.addRevisePrice(gcl);
  351. });
  352. }
  353. }
  354. loadData(bills, pos) {
  355. gclGatherModel.loadLedgerData(bills);
  356. gclGatherModel.loadPosData(pos);
  357. this.gcl = gclGatherModel.gatherGclData();
  358. this.sheet && SpreadJsObj.loadSheetData(this.sheet, SpreadJsObj.DataType.Data, this.gcl);
  359. }
  360. }
  361. const ledgerGcl = new LedgerGcl({
  362. selector: '#ledger-gcl-spread',
  363. spreadSetting: ledgerGclSpreadSetting
  364. });
  365. $.subMenu({
  366. menu: '#sub-menu', miniMenu: '#sub-mini-menu', miniMenuList: '#mini-menu-list',
  367. toMenu: '#to-menu', toMiniMenu: '#to-mini-menu',
  368. key: 'menu.1.0.0',
  369. miniHint: '#sub-mini-hint', hintKey: 'menu.hint.1.0.1',
  370. callback: function (info) {
  371. if (info.mini) {
  372. $('.panel-title').addClass('fluid');
  373. $('#sub-menu').removeClass('panel-sidebar');
  374. } else {
  375. $('.panel-title').removeClass('fluid');
  376. $('#sub-menu').addClass('panel-sidebar');
  377. }
  378. autoFlashHeight();
  379. priceSpread.refresh();
  380. ledgerGcl.spread.refresh();
  381. }
  382. });
  383. $.divResizer({
  384. select: '#revise-right-spr',
  385. callback: function () {
  386. priceSpread.refresh();
  387. ledgerGcl.spread.refresh();
  388. }
  389. });
  390. postData('load', { filter: 'bills;pos;price' }, result => {
  391. revisePrice.loadDatas(result.price);
  392. SpreadJsObj.loadSheetData(priceSheet, SpreadJsObj.DataType.Data, revisePrice.data);
  393. ledgerGcl.loadData(result.bills, result.pos);
  394. $("[content='#ledgerGcl']").click();
  395. });
  396. $('a', '#side-menu').bind('click', function (e) {
  397. e.preventDefault();
  398. const tab = $(this), tabPanel = $(tab.attr('content'));
  399. // 展开工具栏、切换标签
  400. if (!tab.hasClass('active')) {
  401. $('a', '#side-menu').removeClass('active');
  402. tab.addClass('active');
  403. $('.tab-content .tab-pane').removeClass('active');
  404. tabPanel.addClass('active');
  405. showSideTools(tab.hasClass('active'));
  406. ledgerGcl.spread.refresh();
  407. } else {// 收起工具栏
  408. tab.removeClass('active');
  409. tabPanel.removeClass('active');
  410. showSideTools(tab.hasClass('active'));
  411. }
  412. priceSpread.refresh();
  413. });
  414. });