revise_price.js 17 KB

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