123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- /**
- * Created by Tony on 2017/4/28.
- */
- var rationGLJOprObj = {
- sheet: null,
- currentRationItem: null,
- distTypeTree: null,
- cache: {},
- setting: {
- header:[
- {headerName:"编码",headerWidth:120,dataCode:"code", dataType: "String", formatter: "@"},
- {headerName:"名称",headerWidth:400,dataCode:"name", dataType: "String"},
- {headerName:"单位",headerWidth:160,dataCode:"unit", dataType: "String"},
- {headerName:"基价单位",headerWidth:160, dataCode:"basePrice", dataType: "Number", formatter:"0.00", precision: 2},
- {headerName:"定额消耗",headerWidth:160, dataCode:"consumeAmt", dataType: "Number", formatter: "0.000", precision: 3},
- {headerName:"类型",headerWidth:160,dataCode:"gljType", dataType: "String"}
- ],
- view:{
- comboBox:[],
- lockColumns:[1,2,3,5,6]
- }
- },
- getDistTypeTree: function (gljDistType) {
- let me = this;
- let distType;
- let distTypeTree = {
- prefix : 'gljDistType',
- distTypes: {},
- comboDatas: [],
- distTypesArr: []
- };
- gljDistType.forEach(function (typeData) {
- let typeObj = {
- data: typeData,
- children: [],
- parent: null
- }
- distTypeTree.distTypes[distTypeTree.prefix + typeData.ID] = typeObj;
- distTypeTree.distTypesArr.push(typeObj);
- });
- gljDistType.forEach(function (typeData) {
- distType = distTypeTree.distTypes[distTypeTree.prefix + typeData.ID];
- let parent = distTypeTree.distTypes[distTypeTree.prefix + typeData.ParentID];
- if(parent){
- distType.parent = parent;
- parent.children.push(distType);
- }
- });
- distTypeTree.distTypesArr.forEach(function (distTypeObj) {
- if(distTypeObj.children.length === 0 && distTypeObj.data.fullName !== '普通机械' &&distTypeObj.data.fullName !== '机械组成物'
- && distTypeObj.data.fullName !== '机上人工'){
- distTypeTree.comboDatas.push({text: distTypeObj.data.fullName, value: distTypeObj.data.ID});
- }
- if(distTypeObj.data.fullName === '机械'){
- distTypeTree.comboDatas.push({text: distTypeObj.data.fullName, value: distTypeObj.data.ID});
- }
- });
- //me.distTypeTree = distTypeTree;
- return distTypeTree;
- //return distTypeTree.comboDatas;
- },
- getGljDistType: function (callback) {
- let me = this;
- $.ajax({
- type: 'post',
- url: "api/getGljDistType",
- dataType: 'json',
- success: function (result) {
- if(!result.error && callback){
- me.distTypeTree = me.getDistTypeTree(result.data);
- console.log(`me.distTypeTree`);
- console.log(me.distTypeTree);
- callback();
- }
- }
- })
- },
- buildSheet: function(sheet) {
- var me = this;
- me.sheet = sheet;
- me.getGljDistType(function () {
- me.onContextmenuOpr();
- sheetCommonObj.initSheet(me.sheet, me.setting, 30);
- me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
- me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
- me.sheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
- me.sheet.bind(GC.Spread.Sheets.Events.RangeChanged, me.onRangeChanged);
- });
- },
- onRangeChanged: function(sender, args) {
- if (args.action == GC.Spread.Sheets.RangeChangedAction.clear) {
- var me = rationGLJOprObj, updateArr = [], removeArr = [];
- if (args.col == 0) {
- if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
- var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
- for (var i = args.rowCount - 1; i >= 0; i--) {
- if (args.row + i < cacheArr.length) {
- cacheArr.splice(args.row + i, 1);
- }
- }
- me.updateRationItem();
- sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
- me.showGljItems(me.currentRationItem.ID);
- }
- }
- }
- },
- onClipboardPasting: function(sender, args) {
- var me = rationGLJOprObj;
- if (args.cellRange.colCount != 1 || args.cellRange.col != 0 || !(me.currentRationItem)) {
- args.cancel = true;
- }
- },
- onClipboardPasted: function(e, info) {
- var me = rationGLJOprObj, repId = storageUtil.getSessionCache("RationGrp","repositoryID");
- if (repId) {
- if (info.cellRange.col == 0) {
- var tmpCodes = sheetCommonObj.analyzePasteData({header:[{dataCode: "code"}] }, info);
- var codes = [];
- for (var i = 0; i < tmpCodes.length; i++) {
- codes.push(tmpCodes[i].code);
- }
- me.addGljItems(codes, repId);
- } else {
- //修改用量
- }
- }
- },
- onCellEditEnd: function(sender, args){
- var me = rationGLJOprObj;
- if (args.col != 0) {
- var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
- if (args.row < cacheArr.length) {
- var editGlj = cacheArr[args.row];
- if (editGlj["consumeAmt"] != args.editingText) {
- let parseNum = parseFloat(args.editingText);
- if(isNaN(parseFloat(args.editingText))){
- $('#alertModalBtn').click();
- $('#alertText').text("定额消耗只能输入数值!");
- args.sheet.options.isProtected = true;
- $('#alertModalCls').click(function () {
- args.sheet.options.isProtected = false;
- args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
- });
- $('#alertModalCof').click(function () {
- args.sheet.options.isProtected = false;
- args.sheet.setValue(args.row, args.col, editGlj['consumeAmt']);
- })
- }
- else{
- args.sheet.setValue(args.row, args.col, parseNum);
- editGlj["consumeAmt"] = parseNum;
- me.updateRationItem();
- }
- }
- }
- } else {
- //重新更新工料机
- if (args.editingText == null || args.editingText.trim() == "") {
- //delete
- if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
- var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
- if (args.row < cacheArr.length) {
- cacheArr.splice(args.row, 1);
- me.updateRationItem();
- sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
- me.showGljItems(me.currentRationItem.ID);
- }
- }
- } else {
- var repId = storageUtil.getSessionCache("RationGrp","repositoryID");
- if (repId) {
- var codes = [];
- codes.push(args.editingText.trim());
- me.addGljItems(codes, repId);
- }
- }
- }
- },
- onContextmenuOpr: function () {
- $.contextMenu({
- selector: '#rdSpread',
- callback: function(key, options) {
- var m = "clicked: " + key;
- window.console && console.log(m) || alert(m);
- },
- items: {
- "insert": {name: "插入", callback: function (key, opt) {
- }},
- "delete": {name: "删除"}
- }
- });
- },
- addGljItems: function(codes, repId) {
- var me = this;
- $.ajax({
- type:"POST",
- url:"api/getGljItemsByCodes",
- data:{"gljCodes": JSON.stringify(codes), repId: repId},
- dataType:"json",
- cache:false,
- timeout:5000,
- success:function(result){
- sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
- if (result) {
- var rstArr = [], dummyR = {gljId: 0, consumeAmt:0}, newAddArr = [];
- for (var i = 0; i < result.data.length; i++) {
- dummyR.gljId = result.data[i].ID;
- rstArr.push(me.createRationGljDisplayItem(dummyR, result.data[i]));
- }
- if (me.cache["_GLJ_" + me.currentRationItem.ID]) {
- var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
- for (var i = 0; i < rstArr.length; i++) {
- var hasDup = false;
- for (var j = 0; j < cacheArr.length; j++) {
- if (cacheArr[j].gljId == rstArr[i].gljId) {
- hasDup = true;
- break;
- }
- }
- if (!hasDup) {
- newAddArr.push(rstArr[i]);
- }
- }
- cacheArr.sort(function(a, b) {
- var rst = 0;
- if (a.code > b.code) rst = 1
- else if (a.code < b.code) rst = -1;
- return rst;
- });
- me.cache["_GLJ_" + me.currentRationItem.ID] = cacheArr.concat(newAddArr);
- }
- me.showGljItems(me.currentRationItem.ID);
- if (newAddArr.length > 0) {
- me.updateRationItem();
- }
- }
- sheetCommonObj.lockCells(me.sheet, me.setting);
- },
- error:function(err){
- alert(err);
- }
- })
- },
- rationCal: function () {
- let me = this;
- let price = {gljType1: [], gljType2: [], gljType3: []}, rst = {labourPrice: 0, materialPrice: 0, machinePrice: 0}, rationBasePrc = 0;
- function round(v,e){
- var t=1;
- for(;e>0;t*=10,e--);
- for(;e<0;t/=10,e++);
- return Math.round(v*t)/t;
- }
- if(me.currentRationItem && me.cache['_GLJ_' + me.currentRationItem.ID]){
- let cacheArr = me.cache['_GLJ_' + me.currentRationItem.ID];
- cacheArr.forEach(function (gljData) {
- if(gljData.gljType && gljData.basePrice && gljData.consumeAmt){
- let parent = me.distTypeTree.distTypes[me.distTypeTree.prefix + gljData.gljType].parent;
- if(parent && parent.data.ID <= 3){
- price['gljType' + parent.data.ID].push(round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
- }
- if(!parent && gljData.gljType <= 3){
- price['gljType' + gljData.gljType].push(round( gljData.basePrice * gljData.consumeAmt, 3));//取三位
- }
- }
- });
- if(price.gljType1.length > 0){
- let labourPrice = 0;
- price.gljType1.forEach(function (singlePrc) {
- labourPrice += singlePrc;
- });
- let roundPrice = round(labourPrice, 2);
- rst.labourPrice = roundPrice;
- rationBasePrc += roundPrice;
- }
- if(price.gljType2.length > 0){
- let materialPrice = 0;
- price.gljType2.forEach(function (singlePrc) {
- materialPrice += singlePrc;
- });
- let roundPrice = round(materialPrice, 2);
- rst.materialPrice = roundPrice;
- rationBasePrc += roundPrice;
- }
- if(price.gljType3.length > 0){
- let machinePrice = 0;
- price.gljType3.forEach(function (singlePrc) {
- machinePrice += singlePrc;
- });
- let roundPrice = round(machinePrice, 2);
- rst.machinePrice = roundPrice;
- rationBasePrc += roundPrice;
- }
- rst.rationBasePrc = rationBasePrc;
- }
- return rst;
- },
- updateRationItem: function() {
- var me = this, updateArr = [];
- if (me.currentRationItem) {
- me.currentRationItem.rationGljList = me.buildRationItemGlj();
- //recalculate ration basePrice
- let price = me.rationCal();
- me.currentRationItem.labourPrice = price.labourPrice;
- me.currentRationItem.materialPrice = price.materialPrice;
- me.currentRationItem.machinePrice = price.machinePrice;
- me.currentRationItem.basePrice = price.rationBasePrc;
- updateArr.push(me.currentRationItem);
- rationOprObj.mixUpdateRequest(updateArr, [], []);
- }
- },
- buildRationItemGlj: function(){
- var me = this, rst = [];
- if (me.currentRationItem && me.cache["_GLJ_" + me.currentRationItem.ID]) {
- var cacheArr = me.cache["_GLJ_" + me.currentRationItem.ID];
- for (var i = 0; i < cacheArr.length; i++) {
- rst.push({gljId: cacheArr[i].gljId, consumeAmt: cacheArr[i].consumeAmt, proportion: 0});
- }
- }
- return rst;
- },
- createRationGljDisplayItem: function(rItem, repGlj) {
- var rst = {};
- rst.gljId = rItem.gljId;
- rst.consumeAmt = rItem.consumeAmt;
- rst.code = repGlj.code;
- rst.name = repGlj.name;
- rst.specs = repGlj.specs;
- rst.unit = repGlj.unit;
- rst.basePrice = repGlj.basePrice;
- rst.gljType = repGlj.gljType;
- return rst;
- },
- getGljItems: function(rationItem) {
- var me = this, rationID = rationItem.ID, rationGljList = rationItem.rationGljList;
- me.currentRationItem = rationItem;
- if (me.cache["_GLJ_" + rationID]) {
- me.showGljItems(rationID);
- sheetCommonObj.lockCells(me.sheet, me.setting);
- } else {
- var gljIds = [];
- for (var i = 0; i < rationGljList.length; i++) {
- gljIds.push(rationGljList[i].gljId);
- }
- $.ajax({
- type:"POST",
- url:"api/getGljItemsByIds",
- data:{"gljIds": JSON.stringify(gljIds)},
- dataType:"json",
- cache:false,
- timeout:5000,
- success:function(result){
- sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
- if (result) {
- var cacheArr = [];
- for (var i = 0; i < result.data.length; i++) {
- for (var j = 0; j < rationGljList.length; j++) {
- if (rationGljList[j].gljId == result.data[i].ID) {
- cacheArr.push(me.createRationGljDisplayItem(rationGljList[j], result.data[i]));
- break;
- }
- }
- }
- me.cache["_GLJ_" + rationID] = cacheArr;
- me.showGljItems(rationID);
- }
- sheetCommonObj.lockCells(me.sheet, me.setting);
- },
- error:function(err){
- alert(err);
- }
- })
- }
- },
- showGljItems: function(rationID) {
- var me = this;
- if (me.cache["_GLJ_" + rationID]) {
- sheetCommonObj.showData(me.sheet, me.setting, me.cache["_GLJ_" + rationID], me.distTypeTree);
- }
- }
- }
|