123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- /**
- * Created by Zhong on 2017/8/15.
- */
- let gljComponentOprObj = {
- workBook: null,
- setting: {
- owner: "gljComponent",
- header:[
- {headerName:"编码",headerWidth:80,dataCode:"code", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center"},
- {headerName:"名称",headerWidth:110,dataCode:"name", dataType: "String", hAlign: "left", vAlign: "center"},
- {headerName:"计量单位",headerWidth:80,dataCode:"unit", dataType: "String", hAlign: "center", vAlign: "center"},
- {headerName:"单价",headerWidth:80,dataCode:"basePrice", dataType: "Number", formatter: "0.00", hAlign: "right", vAlign: "center"},
- {headerName:"消耗量",headerWidth:80,dataCode:"consumeAmt", dataType: "Number", formatter: "0.000", hAlign: "right", vAlign: "center"}
- ],
- view: {
- lockedCells:[0, 1, 2, 3]
- }
- },
- buildSheet: function(container) {
- let me = gljComponentOprObj;
- me.workBook = sheetOpr.buildSheet(container, me.setting, 30);
- me.workBook.getSheet(0).setColumnWidth(0, 20, GC.Spread.Sheets.SheetArea.rowHeader);
- me.workBook.getSheet(0).setFormatter(-1, 0, "@", GC.Spread.Sheets.SheetArea.viewport);
- me.workBook.getSheet(0).options.isProtected = true;
- sheetOpr.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
- me.onContextmenuOpr();//右键菜单
- me.gljComponentDelOpr();
- me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditStarting, me.onCellEditStart);
- me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
- me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
- me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
- /*me.gljComponentDelOpr();
- me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
- me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
- me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditStarting, me.onCellEditStart);
- me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);*/
- },
- getRowData: function (sheet, row, setting) {
- let rst = {};
- for(let i = 0; i < setting.header.length; i++){
- rst[setting.header[i].dataCode] = sheet.getValue(row, i);
- }
- return rst;
- },
- getComponent: function (sheet, rowCount) {
- let component = [];
- for(let row = 0; row < rowCount; row++){
- let obj = {};
- obj.consumeAmt = sheet.getValue(row, 4);
- obj.ID = sheet.getTag(row, 0);
- component.push(obj);
- }
- return component;
- },
- onContextmenuOpr: function () {
- let me = gljComponentOprObj, that = repositoryGljObj, co = componentOprObj;
- $.contextMenu({
- selector: '#gljComponentSheet',
- build: function($triggerElement, e){
- //控制允许右键菜单在哪个位置出现
- let sheet = me.workBook.getSheet(0);
- let offset = $("#gljComponentSheet").offset(),
- x = e.pageX - offset.left,
- y = e.pageY - offset.top;
- let target = sheet.hitTest(x, y);
- if(target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){//在表格内
- sheet.setActiveCell(target.row, target.col);
- //控制按钮是否可用
- let insertDis = false,
- delDis = false;
- if(me.isLocked){
- insertDis = true;
- delDis = true;
- }
- if(typeof that.currentComponent !== 'undefined' && target.row >= that.currentComponent.length){//右键定位在有组成物的行,删除键才显示可用
- delDis = true;
- }
- return {
- callback: function(){},
- items: {
- "insert": {name: "插入", disabled: insertDis, icon: "fa-sign-in", callback: function (key, opt) {
- //默认radio所有工料机
- co.initRadio();
- co.gljCurTypeId = null;
- //默认点击树根节点
- if(co.rootNode){
- co.treeObj.selectNode(co.rootNode);
- componentTypeTreeOprObj.onClick(null, 'componentTree', co.rootNode);
- }
- //弹出窗口
- $('#componentBtn').click();
- }},
- "delete": {name: "删除", disabled: delDis, icon: "fa-remove", callback: function (key, opt) {
- //删除
- let deleteObj = that.currentComponent[target.row];
- let gljComponent = that.currentGlj.component;
- let updateArr = [];
- //更新当前工料机的组成物列表
- for(let i = 0, len = gljComponent.length; i < len; i++){
- if(gljComponent[i].ID === deleteObj.ID){
- gljComponent.splice(i, 1);
- break;
- }
- }
- //重新计算工料机
- let gljBasePrc = me.reCalGljBasePrc(that.getCurrentComponent(gljComponent));
- if(gljBasePrc !== that.currentGlj.basePrice){
- that.currentGlj.basePrice = gljBasePrc;
- that.reshowGljBasePrc(that.currentGlj);
- //updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
- }
- updateArr.push(that.currentGlj);
- me.updateComponent(updateArr);
- }}
- }
- };
- }
- else{
- return false;
- }
- }
- });
- },
- gljComponentDelOpr: function () {
- let me = gljComponentOprObj, that = repositoryGljObj, updateArr = [], removeArr = [], isUpdate = false, updateBasePrc= [];
- me.workBook.commandManager().register('gljComponentDel', function () {
- let sels = me.workBook.getSheet(0).getSelections();
- if(sels.length > 0 && that.currentComponent.length > 0){
- let component = that.currentGlj.component;
- for(let i = 0; i < sels.length > 0; i++){
- if(sels[i].colCount === me.setting.header.length){//可删除
- for(let j = 0; j < sels[i].rowCount; j++){
- if(sels[i].row + j < that.currentComponent.length){
- removeArr.push(that.currentComponent[sels[i].row + j].ID);
- }
- }
- }
- else if(sels[i].col === 0){
- //编码不可为空
- alert("编码不可为空!");
- }
- else if(sels[i].col === 4){//消耗量修改为0
- if(sels[i].row === -1){//全修改
- for(let j = 0; j < that.currentComponent.length; j++){
- isUpdate = true;
- that.currentComponent[j].consumeAmt = 0;
- for(let k = 0; k < component.length; k++){
- if(component[k].ID === that.currentComponent[j].ID){
- component[k].consumeAmt = 0;
- break;
- }
- }
- }
- }
- else{//部分修改
- for(let j = 0; j < sels[i].rowCount; j++){
- if(sels[i].row + j < that.currentComponent.length){
- isUpdate = true;
- that.currentComponent[sels[i].row + j].consumeAmt = 0;
- for(let k = 0; k < component.length; k++){
- if(component[k].ID === that.currentComponent[sels[i].row + j].ID){
- component[k].consumeAmt = 0;
- break;
- }
- }
- }
- }
- }
- }
- }
- if(removeArr.length > 0 || isUpdate){
- for(let i = 0; i < removeArr.length; i++){
- for(let j = 0; j < that.currentComponent.length; j++){
- if(that.currentComponent[j].ID === removeArr[i]){
- that.currentComponent.splice(j--, 1);
- }
- }
- for(let j = 0; j < component.length; j++){
- if(component[j].ID === removeArr[i]){
- component.splice(j--, 1);
- }
- }
- }
- //重新计算工料机
- let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
- if(gljBasePrc !== that.currentGlj.basePrice){
- that.currentGlj.basePrice = gljBasePrc;
- that.reshowGljBasePrc(that.currentGlj);
- updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
- }
- updateArr.push(that.currentGlj);
- me.updateComponent(updateArr);
- if(updateBasePrc.length > 0){
- that.updateRationBasePrcRq(updateBasePrc);
- }
- }
- }
- });
- me.workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
- //me.workBook.commandManager().setShortcutKey('gljComponentDel', GC.Spread.Commands.Key.del, false, false, false, false);
- },
- onCellEditStart: function(sender, args) {
- let me = gljComponentOprObj, that = repositoryGljObj;
- let rObj = me.getRowData(args.sheet, args.row, me.setting);
- me.currentEditingComponent = rObj;
- },
- onCellEditEnd: function (sender, args) {
- let me = gljComponentOprObj, that = repositoryGljObj, updateBasePrc = [];
- let gljList = that.gljList, updateArr = [], materialComponent = [202, 203, 204], machineComponent = [302, 303];
- // if(args.editingText !== me.currentEditingComponent.code){
- //编码
- /* if(args.col === 0 && args.editingText && args.editingText.trim().length > 0 &&args.editingText !== me.currentEditingComponent.code){
- let component = that.currentGlj.component, hasCode = false;
- for(let i = 0; i < gljList.length; i++){
- if(gljList[i].code === args.editingText){//有效的组成物
- hasCode = true;
- if((materialComponent.indexOf(that.currentGlj.gljType) !== -1 && gljList[i].gljType === 201)
- || (that.currentGlj.gljType === 301 && machineComponent.indexOf(gljList[i].gljType) !== -1 )){//普通材料
- //是否与原有组成物不同
- let isExist = false;
- for(let j = 0; j < component.length; j++){
- if(component[j].ID === gljList[i].ID){
- isExist = true;
- break;
- }
- }
- if(!isExist){
- let rObj = {};
- rObj.ID = gljList[i].ID;
- //rObj.basePrice = gljList[i].basePrice;
- if(typeof that.currentComponent[args.row] !== 'undefined'){
- rObj.consumeAmt = that.currentComponent[args.row].consumeAmt;
- let index;
- for(let j = 0; j < component.length; j++){
- if(component[j].ID === that.currentComponent[args.row].ID){
- index = j;
- break;
- }
- }
- component.splice(index, 1);
- component.splice(index, 0, rObj);
- //计算工料机单价
- let gljBasePrc = me.reCalGljBasePrc(that.getCurrentComponent(component));
- if(gljBasePrc !== that.currentGlj.basePrice){
- that.currentGlj.basePrice = gljBasePrc;
- that.reshowGljBasePrc(that.currentGlj);
- //工料机单价改变,重算引用了该工料机的定额单价
- updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
- }
- updateArr.push(that.currentGlj);
- }
- else{
- rObj.consumeAmt = 0;
- component.push(rObj);
- //计算工料机单价
- let gljBasePrc = me.reCalGljBasePrc(that.getCurrentComponent(component));
- if(gljBasePrc !== that.currentGlj.basePrice){
- that.currentGlj.basePrice = gljBasePrc;
- that.reshowGljBasePrc(that.currentGlj);
- //工料机单价改变,重算引用了该工料机的定额单价
- updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
- }
- updateArr.push(that.currentGlj);
- }
- break;
- }
- else{
- //已存在
- alert("已存在!");
- args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
- me.currentEditingComponent[me.setting.header[args.col].dataCode]: '');
- }
- }
- else{
- if(materialComponent.indexOf(that.currentGlj.gljType) === 1){
- alert("该组成物只能是普通材料!");
- }
- else if(that.currentGlj.gljType === 301){
- alert("该组成物只能是机械组成物或机上人工!")
- }
- args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
- me.currentEditingComponent[me.setting.header[args.col].dataCode]: '');
- //无效
- }
- }
- }
- if(!hasCode){
- alert("不存在此工料机,请先添加!");
- args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
- me.currentEditingComponent[me.setting.header[args.col].dataCode] : '');
- //不存在
- }
- }*/
- if(args.col === 4 && me.currentEditingComponent.code && args.editingText && args.editingText.trim().length > 0){//消耗量
- let consumeAmt = parseFloat(args.editingText);
- if(!isNaN(consumeAmt) && consumeAmt !== me.currentEditingComponent.consumeAmt){
- let roundCons = me.round(consumeAmt, 3);
- let component = that.currentGlj.component;
- for(let i = 0; i < component.length; i++){
- if(component[i].ID === that.currentComponent[args.row].ID){
- component[i].consumeAmt = roundCons;
- }
- }
- that.currentComponent[args.row].consumeAmt = roundCons;
- //计算工料机单价
- let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
- if(gljBasePrc !== that.currentGlj.basePrice){
- that.currentGlj.basePrice = gljBasePrc;
- that.reshowGljBasePrc(that.currentGlj);
- //工料机单价改变,重算引用了该工料机的定额单价
- //updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
- }
- updateArr.push(that.currentGlj);
- }
- else{
- //只能输入数值
- args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
- me.currentEditingComponent[me.setting.header[args.col].dataCode]: 0);
- }
- }
- else{
- args.sheet.setValue(args.row, args.col, me.currentEditingComponent.consumeAmt);
- }
- if(updateArr.length > 0){
- me.updateComponent(updateArr);
- /*if(updateBasePrc.length > 0){
- that.updateRationBasePrcRq(updateBasePrc)
- }*/
- }
- },
- onClipboardPasting: function (sender, info) {
- let me = gljComponentOprObj;
- let maxCol = info.cellRange.col + info.cellRange.colCount - 1;
- //复制的列数超过正确的列数,不可复制
- if(maxCol >= me.setting.header.length){
- args.cancel = true;
- }
- },
- onClipboardPasted: function (sender, info) {
- let me = gljComponentOprObj, that = repositoryGljObj, updateArr = [] ,materialComponent = [202, 203, 204], machineComponent = [302, 303],
- component = that.currentGlj.component, newComponent = [], concatComponent = [], isChange = false, updateBasePrc = [];
- let items = sheetOpr.analyzePasteData(me.setting, info);
- let gljCache = that.gljList;
- //编码
- /* if(info.cellRange.col === 0){
- for(let i = 0; i < items.length; i++){
- for(let j = 0; j < gljCache.length; j++){
- if(items[i].code === gljCache[j].code){
- if((materialComponent.indexOf(that.currentGlj.gljType) !== -1 && gljCache[j].gljType === 201)
- || (that.currentGlj.gljType === 301 && machineComponent.indexOf(gljCache[j].gljType) !== -1 )){
- //是否与原有组成物不同
- let isExist = false;
- for(let k = 0; k < component.length; k++){
- if(component[k].ID === gljCache[j].ID){
- isExist = true;
- me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
- typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
- break;
- }
- }
- if(!isExist){
- isChange = true;
- let obj = {};
- obj.ID = gljCache[j].ID;
- if(typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'){//更新
- obj.consumeAmt = that.currentComponent[info.cellRange.row + i].consumeAmt;
- let index;
- for(let k = 0; k < component.length; k++){
- if(that.currentComponent[info.cellRange.row + i].ID === component[k].ID){
- index = k;
- break;
- }
- }
- component.splice(index, 1);
- component.splice(index, 0, obj);
- }
- else{//新增
- obj.consumeAmt = 0;
- component.push(obj);
- }
- break;
- }
- }
- else{
- me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
- typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
- }
- }
- else{
- me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
- typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
- }
- }
- }
- if(isChange){
- //计算工料机单价
- let gljBasePrc = me.reCalGljBasePrc(that.getCurrentComponent(component));
- if(gljBasePrc !== that.currentGlj.basePrice){
- that.currentGlj.basePrice = gljBasePrc;
- that.reshowGljBasePrc(that.currentGlj);
- updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
- }
- updateArr.push(that.currentGlj);
- }
- }*/
- //消耗量
- if(info.cellRange.col === 4){
- let items = sheetOpr.analyzePasteData(me.setting, info);
- let row = info.cellRange.row;
- for(let i = 0; i < items.length; i++){
- if(row + i < that.currentComponent.length){
- let currentObj = that.currentComponent[row + i];
- if(items[i].consumeAmt.trim().length > 0 && items[i].consumeAmt !== currentObj.consumeAmt){
- let roundCons = me.round(items[i].consumeAmt, 3);
- isChange = true;
- currentObj.consumeAmt = roundCons;
- for(let j = 0; j < component.length; j++){
- if(component[j].ID === currentObj.ID){
- component[j].consumeAmt = currentObj.consumeAmt;
- break;
- }
- }
- }
- else{
- me.workBook.getSheet(0).setValue(row + i, info.cellRange.col, currentObj.consumeAmt);
- }
- }
- else{
- me.workBook.getSheet(0).setValue(row + i, info.cellRange.col, '');
- }
- }
- if(isChange){
- //计算工料机单价
- let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
- if(gljBasePrc !== that.currentGlj.basePrice){
- that.currentGlj.basePrice = gljBasePrc;
- that.reshowGljBasePrc(that.currentGlj);
- //updateBasePrc.push({gljId: that.currentGlj.ID, gljType: that.currentGlj.gljType, basePrice: that.currentGlj.basePrice});
- }
- updateArr.push(that.currentGlj);
- }
- }
- if(updateArr.length > 0){
- me.updateComponent(updateArr);
- /* if(updateBasePrc.length > 0){
- that.updateRationBasePrcRq(updateBasePrc);
- }*/
- }
- },
- updateComponent: function (updateArr) {
- let me = gljComponentOprObj, that = repositoryGljObj;
- $.ajax({
- type: 'post',
- url: 'complementartGlj/api/updateComponent',
- data: {"userId": pageOprObj.userId, "updateArr": JSON.stringify(updateArr)},
- dataType: 'json',
- success: function (result) {
- if(!result.error){
- that.currentComponent = that.getCurrentComponent(result.data[0].component);
- sheetOpr.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
- sheetOpr.showData(me.workBook.getSheet(0), me.setting, that.currentComponent);
- }
- else{
- sheetOpr.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
- }
- $('#componentsCacnel').click();
- }
- })
- },
- round: function (v, e) {
- let t=1;
- for(;e>0;t*=10,e--);
- for(;e<0;t/=10,e++);
- return Math.round(v*t)/t;
- },
- reCalGljBasePrc: function (component) {
- let me = gljComponentOprObj, gljBasePrc = 0;
- for(let i = 0; i < component.length; i++){
- let roundBasePrc = me.round(component[i].basePrice, 2);
- gljBasePrc += me.round(roundBasePrc * component[i].consumeAmt, 2);
- }
- return gljBasePrc;
- }
- };
|