ration_glj.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var rationGLJOprObj = {
  5. sheet: null,
  6. currentRationItem: null,
  7. distTypeTree: null,
  8. cache: {},
  9. setting: {
  10. header:[
  11. {headerName:"编码",headerWidth:120,dataCode:"code", dataType: "String", formatter: "@"},
  12. {headerName:"名称",headerWidth:400,dataCode:"name", dataType: "String"},
  13. {headerName:"单位",headerWidth:160,dataCode:"unit", dataType: "String"},
  14. {headerName:"基价单位",headerWidth:160, dataCode:"basePrice", dataType: "Number", formatter:"0.00", precision: 2},
  15. {headerName:"定额消耗",headerWidth:160, dataCode:"consumeAmt", dataType: "Number", formatter: "0.000", precision: 3},
  16. {headerName:"类型",headerWidth:160,dataCode:"gljType", dataType: "String"}
  17. ],
  18. view:{
  19. comboBox:[],
  20. lockColumns:[1,2,3,5,6]
  21. }
  22. },
  23. getDistTypeTree: function (gljDistType) {
  24. let me = this;
  25. let distType;
  26. let distTypeTree = {
  27. prefix : 'gljDistType',
  28. distTypes: {},
  29. comboDatas: [],
  30. distTypesArr: []
  31. };
  32. gljDistType.forEach(function (typeData) {
  33. let typeObj = {
  34. data: typeData,
  35. children: [],
  36. parent: null
  37. }
  38. distTypeTree.distTypes[distTypeTree.prefix + typeData.ID] = typeObj;
  39. distTypeTree.distTypesArr.push(typeObj);
  40. });
  41. gljDistType.forEach(function (typeData) {
  42. distType = distTypeTree.distTypes[distTypeTree.prefix + typeData.ID];
  43. let parent = distTypeTree.distTypes[distTypeTree.prefix + typeData.ParentID];
  44. if(parent){
  45. distType.parent = parent;
  46. parent.children.push(distType);
  47. }
  48. });
  49. distTypeTree.distTypesArr.forEach(function (distTypeObj) {
  50. if(distTypeObj.children.length === 0 && distTypeObj.data.fullName !== '普通机械' &&distTypeObj.data.fullName !== '机械组成物'
  51. && distTypeObj.data.fullName !== '机上人工'){
  52. distTypeTree.comboDatas.push({text: distTypeObj.data.fullName, value: distTypeObj.data.ID});
  53. }
  54. if(distTypeObj.data.fullName === '机械'){
  55. distTypeTree.comboDatas.push({text: distTypeObj.data.fullName, value: distTypeObj.data.ID});
  56. }
  57. });
  58. //me.distTypeTree = distTypeTree;
  59. return distTypeTree;
  60. //return distTypeTree.comboDatas;
  61. },
  62. getGljDistType: function (callback) {
  63. let me = this;
  64. $.ajax({
  65. type: 'post',
  66. url: "api/getGljDistType",
  67. dataType: 'json',
  68. success: function (result) {
  69. if(!result.error && callback){
  70. me.distTypeTree = me.getDistTypeTree(result.data);
  71. console.log(`me.distTypeTree`);
  72. console.log(me.distTypeTree);
  73. callback();
  74. }
  75. }
  76. })
  77. },
  78. buildSheet: function(sheet) {
  79. var me = this;
  80. me.sheet = sheet;
  81. me.getGljDistType(function () {
  82. me.onContextmenuOpr();
  83. sheetCommonObj.initSheet(me.sheet, me.setting, 30);
  84. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  85. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  86. me.sheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
  87. me.sheet.bind(GC.Spread.Sheets.Events.RangeChanged, me.onRangeChanged);
  88. });
  89. },
  90. onRangeChanged: function(sender, args) {
  91. if (args.action == GC.Spread.Sheets.RangeChangedAction.clear) {
  92. var me = rationGLJOprObj, updateArr = [], removeArr = [];
  93. if (args.col == 0) {
  94. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  95. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  96. for (var i = args.rowCount - 1; i >= 0; i--) {
  97. if (args.row + i < cacheArr.length) {
  98. cacheArr.splice(args.row + i, 1);
  99. }
  100. }
  101. me.updateRationItem();
  102. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  103. me.showGljItems(me.currentRationItem.ID);
  104. }
  105. }
  106. }
  107. },
  108. onClipboardPasting: function(sender, args) {
  109. var me = rationGLJOprObj;
  110. if (args.cellRange.colCount != 1 || args.cellRange.col != 0 || !(me.currentRationItem)) {
  111. args.cancel = true;
  112. }
  113. },
  114. onClipboardPasted: function(e, info) {
  115. var me = rationGLJOprObj, repId = storageUtil.getSessionCache("RationGrp","repositoryID");
  116. if (repId) {
  117. if (info.cellRange.col == 0) {
  118. var tmpCodes = sheetCommonObj.analyzePasteData({header:[{dataCode: "code"}] }, info);
  119. var codes = [];
  120. for (var i = 0; i < tmpCodes.length; i++) {
  121. codes.push(tmpCodes[i].code);
  122. }
  123. me.addGljItems(codes, repId);
  124. } else {
  125. //修改用量
  126. }
  127. }
  128. },
  129. onCellEditEnd: function(sender, args){
  130. var me = rationGLJOprObj;
  131. if (args.col != 0) {
  132. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  133. if (args.row < cacheArr.length) {
  134. var editGlj = cacheArr[args.row];
  135. if (editGlj["consumeAmt"] != args.editingText) {
  136. if(typeof args.editingText !== 'number'){
  137. $('#alertModalBtn').click();
  138. $('#alertText').text("定额消耗只能输入数值!");
  139. args.sheet.options.isProtected = true;
  140. $('#alertModalCls').click(function () {
  141. args.sheet.options.isProtected = false;
  142. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  143. });
  144. $('#alertModalCof').click(function () {
  145. args.sheet.options.isProtected = false;
  146. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  147. })
  148. }
  149. else{
  150. editGlj["consumeAmt"] = args.editingText;
  151. me.updateRationItem();
  152. }
  153. }
  154. }
  155. } else {
  156. //重新更新工料机
  157. if (args.editingText == null || args.editingText.trim() == "") {
  158. //delete
  159. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  160. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  161. if (args.row < cacheArr.length) {
  162. cacheArr.splice(args.row, 1);
  163. me.updateRationItem();
  164. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  165. me.showGljItems(me.currentRationItem.ID);
  166. }
  167. }
  168. } else {
  169. var repId = storageUtil.getSessionCache("RationGrp","repositoryID");
  170. if (repId) {
  171. var codes = [];
  172. codes.push(args.editingText.trim());
  173. me.addGljItems(codes, repId);
  174. }
  175. }
  176. }
  177. },
  178. onContextmenuOpr: function () {
  179. $.contextMenu({
  180. selector: '#rdSpread',
  181. callback: function(key, options) {
  182. var m = "clicked: " + key;
  183. console.log(`maincbkey`);
  184. console.log(key);
  185. console.log(`maincbOp`);
  186. console.log(options);
  187. window.console && console.log(m) || alert(m);
  188. },
  189. items: {
  190. "insert": {name: "插入", callback: function (key, opt) {
  191. console.log(`key`);
  192. console.log(key);
  193. console.log(`opt`);
  194. console.log(opt);
  195. console.log(opt.position());
  196. }},
  197. "delete": {name: "删除"}
  198. }
  199. });
  200. },
  201. addGljItems: function(codes, repId) {
  202. var me = this;
  203. $.ajax({
  204. type:"POST",
  205. url:"api/getGljItemsByCodes",
  206. data:{"gljCodes": JSON.stringify(codes), repId: repId},
  207. dataType:"json",
  208. cache:false,
  209. timeout:5000,
  210. success:function(result){
  211. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  212. if (result) {
  213. var rstArr = [], dummyR = {gljId: 0, consumeAmt:0}, newAddArr = [];
  214. for (var i = 0; i < result.data.length; i++) {
  215. dummyR.gljId = result.data[i].ID;
  216. rstArr.push(me.createRationGljDisplayItem(dummyR, result.data[i]));
  217. }
  218. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  219. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  220. for (var i = 0; i < rstArr.length; i++) {
  221. var hasDup = false;
  222. for (var j = 0; j < cacheArr.length; j++) {
  223. if (cacheArr[j].gljId == rstArr[i].gljId) {
  224. hasDup = true;
  225. break;
  226. }
  227. }
  228. if (!hasDup) {
  229. newAddArr.push(rstArr[i]);
  230. }
  231. }
  232. cacheArr.sort(function(a, b) {
  233. var rst = 0;
  234. if (a.code > b.code) rst = 1
  235. else if (a.code < b.code) rst = -1;
  236. return rst;
  237. });
  238. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
  239. }
  240. me.showGljItems(me.currentRationItem.ID);
  241. if (newAddArr.length > 0) {
  242. me.updateRationItem();
  243. }
  244. }
  245. sheetCommonObj.lockCells(me.sheet, me.setting);
  246. },
  247. error:function(err){
  248. alert(err);
  249. }
  250. })
  251. },
  252. rationCal: function () {
  253. let me = this;
  254. let price = {gljType1: [], gljType2: [], gljType3: []}, rst = {labourPrice: 0, materialPrice: 0, machinePrice: 0}, rationBasePrc = 0;
  255. function round(v,e){
  256. var t=1;
  257. for(;e>0;t*=10,e--);
  258. for(;e<0;t/=10,e++);
  259. return Math.round(v*t)/t;
  260. }
  261. if(me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]){
  262. let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
  263. cacheArr.forEach(function (gljData) {
  264. if(gljData.gljType && gljData.basePrice && gljData.consumeAmt){
  265. let parent = me.distTypeTree.distTypes[me.distTypeTree.prefix + gljData.gljType].parent;
  266. if(parent && parent.data.ID <= 3){
  267. price['gljType' + parent.data.ID].push(round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
  268. }
  269. if(!parent && gljData.gljType <= 3){
  270. price['gljType' + gljData.gljType].push(round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
  271. }
  272. }
  273. });
  274. if(price.gljType1.length > 0){
  275. let labourPrice = 0;
  276. price.gljType1.forEach(function (singlePrc) {
  277. labourPrice += singlePrc;
  278. });
  279. let roundPrice = round(labourPrice, 2);
  280. rst.labourPrice = roundPrice;
  281. rationBasePrc += roundPrice;
  282. }
  283. if(price.gljType2.length > 0){
  284. let materialPrice = 0;
  285. price.gljType2.forEach(function (singlePrc) {
  286. materialPrice += singlePrc;
  287. });
  288. let roundPrice = round(materialPrice, 2);
  289. rst.materialPrice = roundPrice;
  290. rationBasePrc += roundPrice;
  291. }
  292. if(price.gljType3.length > 0){
  293. let machinePrice = 0;
  294. price.gljType3.forEach(function (singlePrc) {
  295. machinePrice += singlePrc;
  296. });
  297. let roundPrice = round(machinePrice, 2);
  298. rst.machinePrice = roundPrice;
  299. rationBasePrc += roundPrice;
  300. }
  301. rst.rationBasePrc = rationBasePrc;
  302. }
  303. return rst;
  304. },
  305. updateRationItem: function() {
  306. var me = this, updateArr = [];
  307. if (me.currentRationItem) {
  308. me.currentRationItem.rationGljList = me.buildRationItemGlj();
  309. //recalculate ration basePrice
  310. let price = me.rationCal();
  311. me.currentRationItem.labourPrice = price.labourPrice;
  312. me.currentRationItem.materialPrice = price.materialPrice;
  313. me.currentRationItem.machinePrice = price.machinePrice;
  314. me.currentRationItem.basePrice = price.rationBasePrc;
  315. updateArr.push(me.currentRationItem);
  316. rationOprObj.mixUpdateRequest(updateArr, [], []);
  317. }
  318. },
  319. buildRationItemGlj: function(){
  320. var me = this, rst = [];
  321. if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
  322. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  323. for (var i = 0; i < cacheArr.length; i++) {
  324. rst.push({gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, proportion: 0});
  325. }
  326. }
  327. return rst;
  328. },
  329. createRationGljDisplayItem: function(rItem, repGlj) {
  330. var rst = {};
  331. rst.gljId = rItem.gljId;
  332. rst.consumeAmt = rItem.consumeAmt;
  333. rst.code = repGlj.code;
  334. rst.name = repGlj.name;
  335. rst.specs = repGlj.specs;
  336. rst.unit = repGlj.unit;
  337. rst.basePrice = repGlj.basePrice;
  338. rst.gljType = repGlj.gljType;
  339. return rst;
  340. },
  341. getGljItems: function(rationItem) {
  342. var me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList;
  343. me.currentRationItem = rationItem;
  344. if (me.cache["_GLJ_" + rationID]) {
  345. me.showGljItems(rationID);
  346. sheetCommonObj.lockCells(me.sheet, me.setting);
  347. } else {
  348. var gljIds = [];
  349. for (var i = 0; i < rationGljList.length; i++) {
  350. gljIds.push(rationGljList[i].gljId);
  351. }
  352. $.ajax({
  353. type:"POST",
  354. url:"api/getGljItemsByIds",
  355. data:{"gljIds": JSON.stringify(gljIds)},
  356. dataType:"json",
  357. cache:false,
  358. timeout:5000,
  359. success:function(result){
  360. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  361. if (result) {
  362. var cacheArr = [];
  363. for (var i = 0; i < result.data.length; i++) {
  364. for (var j = 0; j < rationGljList.length; j++) {
  365. if (rationGljList[j].gljId == result.data[i].ID) {
  366. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], result.data[i]));
  367. break;
  368. }
  369. }
  370. }
  371. me.cache["_GLJ_" + rationID] = cacheArr;
  372. me.showGljItems(rationID);
  373. }
  374. sheetCommonObj.lockCells(me.sheet, me.setting);
  375. },
  376. error:function(err){
  377. alert(err);
  378. }
  379. })
  380. }
  381. },
  382. showGljItems: function(rationID) {
  383. var me = this;
  384. if (me.cache["_GLJ_" + rationID]) {
  385. sheetCommonObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  386. }
  387. }
  388. }