ration_glj.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. let parseNum = parseFloat(args.editingText);
  137. if(isNaN(parseFloat(args.editingText))){
  138. $('#alertModalBtn').click();
  139. $('#alertText').text("定额消耗只能输入数值!");
  140. args.sheet.options.isProtected = true;
  141. $('#alertModalCls').click(function () {
  142. args.sheet.options.isProtected = false;
  143. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  144. });
  145. $('#alertModalCof').click(function () {
  146. args.sheet.options.isProtected = false;
  147. args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
  148. })
  149. }
  150. else{
  151. editGlj["consumeAmt"] = parseNum;
  152. me.updateRationItem();
  153. }
  154. }
  155. }
  156. } else {
  157. //重新更新工料机
  158. if (args.editingText == null || args.editingText.trim() == "") {
  159. //delete
  160. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  161. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  162. if (args.row < cacheArr.length) {
  163. cacheArr.splice(args.row, 1);
  164. me.updateRationItem();
  165. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  166. me.showGljItems(me.currentRationItem.ID);
  167. }
  168. }
  169. } else {
  170. var repId = storageUtil.getSessionCache("RationGrp","repositoryID");
  171. if (repId) {
  172. var codes = [];
  173. codes.push(args.editingText.trim());
  174. me.addGljItems(codes, repId);
  175. }
  176. }
  177. }
  178. },
  179. onContextmenuOpr: function () {
  180. $.contextMenu({
  181. selector: '#rdSpread',
  182. callback: function(key, options) {
  183. var m = "clicked: " + key;
  184. console.log(`maincbkey`);
  185. console.log(key);
  186. console.log(`maincbOp`);
  187. console.log(options);
  188. window.console && console.log(m) || alert(m);
  189. },
  190. items: {
  191. "insert": {name: "插入", callback: function (key, opt) {
  192. console.log(`key`);
  193. console.log(key);
  194. console.log(`opt`);
  195. console.log(opt);
  196. console.log(opt.position());
  197. }},
  198. "delete": {name: "删除"}
  199. }
  200. });
  201. },
  202. addGljItems: function(codes, repId) {
  203. var me = this;
  204. $.ajax({
  205. type:"POST",
  206. url:"api/getGljItemsByCodes",
  207. data:{"gljCodes": JSON.stringify(codes), repId: repId},
  208. dataType:"json",
  209. cache:false,
  210. timeout:5000,
  211. success:function(result){
  212. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  213. if (result) {
  214. var rstArr = [], dummyR = {gljId: 0, consumeAmt:0}, newAddArr = [];
  215. for (var i = 0; i < result.data.length; i++) {
  216. dummyR.gljId = result.data[i].ID;
  217. rstArr.push(me.createRationGljDisplayItem(dummyR, result.data[i]));
  218. }
  219. if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
  220. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  221. for (var i = 0; i < rstArr.length; i++) {
  222. var hasDup = false;
  223. for (var j = 0; j < cacheArr.length; j++) {
  224. if (cacheArr[j].gljId == rstArr[i].gljId) {
  225. hasDup = true;
  226. break;
  227. }
  228. }
  229. if (!hasDup) {
  230. newAddArr.push(rstArr[i]);
  231. }
  232. }
  233. cacheArr.sort(function(a, b) {
  234. var rst = 0;
  235. if (a.code > b.code) rst = 1
  236. else if (a.code < b.code) rst = -1;
  237. return rst;
  238. });
  239. me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
  240. }
  241. me.showGljItems(me.currentRationItem.ID);
  242. if (newAddArr.length > 0) {
  243. me.updateRationItem();
  244. }
  245. }
  246. sheetCommonObj.lockCells(me.sheet, me.setting);
  247. },
  248. error:function(err){
  249. alert(err);
  250. }
  251. })
  252. },
  253. rationCal: function () {
  254. let me = this;
  255. let price = {gljType1: [], gljType2: [], gljType3: []}, rst = {labourPrice: 0, materialPrice: 0, machinePrice: 0}, rationBasePrc = 0;
  256. function round(v,e){
  257. var t=1;
  258. for(;e>0;t*=10,e--);
  259. for(;e<0;t/=10,e++);
  260. return Math.round(v*t)/t;
  261. }
  262. if(me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]){
  263. let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
  264. cacheArr.forEach(function (gljData) {
  265. if(gljData.gljType && gljData.basePrice && gljData.consumeAmt){
  266. let parent = me.distTypeTree.distTypes[me.distTypeTree.prefix + gljData.gljType].parent;
  267. if(parent && parent.data.ID <= 3){
  268. price['gljType' + parent.data.ID].push(round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
  269. }
  270. if(!parent && gljData.gljType <= 3){
  271. price['gljType' + gljData.gljType].push(round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
  272. }
  273. }
  274. });
  275. if(price.gljType1.length > 0){
  276. let labourPrice = 0;
  277. price.gljType1.forEach(function (singlePrc) {
  278. labourPrice += singlePrc;
  279. });
  280. let roundPrice = round(labourPrice, 2);
  281. rst.labourPrice = roundPrice;
  282. rationBasePrc += roundPrice;
  283. }
  284. if(price.gljType2.length > 0){
  285. let materialPrice = 0;
  286. price.gljType2.forEach(function (singlePrc) {
  287. materialPrice += singlePrc;
  288. });
  289. let roundPrice = round(materialPrice, 2);
  290. rst.materialPrice = roundPrice;
  291. rationBasePrc += roundPrice;
  292. }
  293. if(price.gljType3.length > 0){
  294. let machinePrice = 0;
  295. price.gljType3.forEach(function (singlePrc) {
  296. machinePrice += singlePrc;
  297. });
  298. let roundPrice = round(machinePrice, 2);
  299. rst.machinePrice = roundPrice;
  300. rationBasePrc += roundPrice;
  301. }
  302. rst.rationBasePrc = rationBasePrc;
  303. }
  304. return rst;
  305. },
  306. updateRationItem: function() {
  307. var me = this, updateArr = [];
  308. if (me.currentRationItem) {
  309. me.currentRationItem.rationGljList = me.buildRationItemGlj();
  310. //recalculate ration basePrice
  311. let price = me.rationCal();
  312. me.currentRationItem.labourPrice = price.labourPrice;
  313. me.currentRationItem.materialPrice = price.materialPrice;
  314. me.currentRationItem.machinePrice = price.machinePrice;
  315. me.currentRationItem.basePrice = price.rationBasePrc;
  316. updateArr.push(me.currentRationItem);
  317. rationOprObj.mixUpdateRequest(updateArr, [], []);
  318. }
  319. },
  320. buildRationItemGlj: function(){
  321. var me = this, rst = [];
  322. if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
  323. var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
  324. for (var i = 0; i < cacheArr.length; i++) {
  325. rst.push({gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, proportion: 0});
  326. }
  327. }
  328. return rst;
  329. },
  330. createRationGljDisplayItem: function(rItem, repGlj) {
  331. var rst = {};
  332. rst.gljId = rItem.gljId;
  333. rst.consumeAmt = rItem.consumeAmt;
  334. rst.code = repGlj.code;
  335. rst.name = repGlj.name;
  336. rst.specs = repGlj.specs;
  337. rst.unit = repGlj.unit;
  338. rst.basePrice = repGlj.basePrice;
  339. rst.gljType = repGlj.gljType;
  340. return rst;
  341. },
  342. getGljItems: function(rationItem) {
  343. var me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList;
  344. me.currentRationItem = rationItem;
  345. if (me.cache["_GLJ_" + rationID]) {
  346. me.showGljItems(rationID);
  347. sheetCommonObj.lockCells(me.sheet, me.setting);
  348. } else {
  349. var gljIds = [];
  350. for (var i = 0; i < rationGljList.length; i++) {
  351. gljIds.push(rationGljList[i].gljId);
  352. }
  353. $.ajax({
  354. type:"POST",
  355. url:"api/getGljItemsByIds",
  356. data:{"gljIds": JSON.stringify(gljIds)},
  357. dataType:"json",
  358. cache:false,
  359. timeout:5000,
  360. success:function(result){
  361. sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
  362. if (result) {
  363. var cacheArr = [];
  364. for (var i = 0; i < result.data.length; i++) {
  365. for (var j = 0; j < rationGljList.length; j++) {
  366. if (rationGljList[j].gljId == result.data[i].ID) {
  367. cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], result.data[i]));
  368. break;
  369. }
  370. }
  371. }
  372. me.cache["_GLJ_" + rationID] = cacheArr;
  373. me.showGljItems(rationID);
  374. }
  375. sheetCommonObj.lockCells(me.sheet, me.setting);
  376. },
  377. error:function(err){
  378. alert(err);
  379. }
  380. })
  381. }
  382. },
  383. showGljItems: function(rationID) {
  384. var me = this;
  385. if (me.cache["_GLJ_" + rationID]) {
  386. sheetCommonObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
  387. }
  388. }
  389. }