|
@@ -7,6 +7,7 @@ import {complementaryGljModel, stdGljModel} from '../../complementary_glj_lib/mo
|
|
|
import async from 'async';
|
|
|
let stdRationModel = require ('../../ration_repository/models/ration_item').Model;
|
|
|
let counter = require('../../../public/counter/counter');
|
|
|
+const scMathUtil = require('../../../public/scMathUtil').getUtil();
|
|
|
|
|
|
class CompleRatoinDao {
|
|
|
async updateRation(userID, compilationId, updateData, callback){
|
|
@@ -112,9 +113,6 @@ class CompleRatoinDao {
|
|
|
|
|
|
async getGljItemsByCodes(userID, compilationId, rationRepId, codes, callback){
|
|
|
try{
|
|
|
- console.log(userID);
|
|
|
- console.log(compilationId);
|
|
|
- console.log(codes);
|
|
|
let rst = [];
|
|
|
for(let i = 0, len = codes.length; i < len; i++){
|
|
|
let stdGlj = await stdGljModel.find({repositoryId: rationRepId, code: codes[i]});
|
|
@@ -123,9 +121,7 @@ class CompleRatoinDao {
|
|
|
rst.push(stdGlj[0]);
|
|
|
}
|
|
|
else {
|
|
|
- console.log(codes[i]);
|
|
|
let compleGlj = await complementaryGljModel.find({userId: userID, compilationId: compilationId, code: codes[i]});
|
|
|
- console.log(compleGlj);
|
|
|
if(compleGlj.length > 0){
|
|
|
compleGlj[0]._doc.type = 'complementary';
|
|
|
rst.push(compleGlj[0]);
|
|
@@ -139,6 +135,164 @@ class CompleRatoinDao {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ updateRationBasePrc(userID, basePrcArr, callback){
|
|
|
+ let me = this;
|
|
|
+ async.each(basePrcArr, function (basePrcObj, finalCb) {
|
|
|
+ let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
|
|
|
+ async.waterfall([
|
|
|
+ function (cb) {
|
|
|
+ if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){
|
|
|
+ //补充定额
|
|
|
+ compleRationModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, compleRst) {
|
|
|
+ if(err){
|
|
|
+ cb(err);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ compleRationModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
|
|
|
+ if(err){
|
|
|
+ cb(err);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ cb(null, compleRst);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ compleRationModel.find({'rationGljList.gljId': adjGljId}, function (err, compleRst) {
|
|
|
+ if(err){
|
|
|
+ cb(err);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ cb(null, compleRst);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+ function (result, cb) {
|
|
|
+ async.each(result, function (rationItem, ecb) {
|
|
|
+ let rationGljList = rationItem.rationGljList,
|
|
|
+ gljIds = [];
|
|
|
+ rationGljList.forEach(function (rationGlj) {
|
|
|
+ let idObj = Object.create(null);
|
|
|
+ idObj.id = rationGlj.gljId;
|
|
|
+ idObj.type = rationGlj.type;
|
|
|
+ gljIds.push(idObj);
|
|
|
+ });
|
|
|
+ me.getGljItemsByIds(userID, gljIds, function(err, gljItems){
|
|
|
+ if(err){
|
|
|
+ ecb(err);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ let gljArr = [];
|
|
|
+ for(let i=0; i<gljItems.length; i++){
|
|
|
+ let gljParentType = -1;
|
|
|
+ if(gljItems[i].ID === adjGljId){
|
|
|
+ gljItems[i].gljType = adjGljType;
|
|
|
+ }
|
|
|
+ if(gljItems[i].gljType <= 3){
|
|
|
+ gljParentType = gljItems[i].gljType;
|
|
|
+ }
|
|
|
+ if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
|
|
|
+ gljParentType = 2;
|
|
|
+ }
|
|
|
+ if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
|
|
|
+ gljParentType = 3;
|
|
|
+ }
|
|
|
+ if(gljItems[i].ID === adjGljId){
|
|
|
+ gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ gljArr.push({gljId: gljItems[i].ID, basePrice: parseFloat(gljItems[i].basePrice), gljParentType: gljParentType});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ gljArr.forEach(function (gljItem) {
|
|
|
+ rationGljList.forEach(function (rationGlj) {
|
|
|
+ if(gljItem.gljId === rationGlj.gljId){
|
|
|
+ gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ });
|
|
|
+ //recalculate the price of ration
|
|
|
+ let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
|
|
|
+ gljArr.forEach(function (gljItem) {
|
|
|
+ if(gljItem.gljParentType !== -1){
|
|
|
+ singlePrc = scMathUtil.roundTo(gljItem.basePrice * gljItem.consumeAmt, -3);
|
|
|
+ if(gljItem.gljParentType === 1){
|
|
|
+ labourPrc.push(singlePrc);
|
|
|
+ }
|
|
|
+ else if(gljItem.gljParentType ===2){
|
|
|
+ materialPrc.push(singlePrc);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ machinePrc.push(singlePrc);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if(labourPrc.length > 0){
|
|
|
+ let sumLaP = 0;
|
|
|
+ for(let i=0; i<labourPrc.length; i++){
|
|
|
+ sumLaP += labourPrc[i];
|
|
|
+ }
|
|
|
+ updatePrc.labourPrice = scMathUtil.roundTo(sumLaP, -2);
|
|
|
+ }
|
|
|
+ if(materialPrc.length > 0){
|
|
|
+ let sumMtP = 0;
|
|
|
+ for(let i= 0; i<materialPrc.length; i++){
|
|
|
+ sumMtP += materialPrc[i];
|
|
|
+ }
|
|
|
+ updatePrc.materialPrice = scMathUtil.roundTo(sumMtP, -2);
|
|
|
+ }
|
|
|
+ if(machinePrc.length > 0){
|
|
|
+ let sumMaP = 0;
|
|
|
+ for(let i =0; i< machinePrc.length; i++){
|
|
|
+ sumMaP += machinePrc[i];
|
|
|
+ }
|
|
|
+ updatePrc.machinePrice = scMathUtil.roundTo(sumMaP, -2);
|
|
|
+ }
|
|
|
+ updatePrc.basePrice = scMathUtil.roundTo(updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice, -2);
|
|
|
+ //updateDataBase
|
|
|
+ compleRationModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice.toString(), materialPrice: updatePrc.materialPrice.toString(),
|
|
|
+ machinePrice: updatePrc.machinePrice.toString(), basePrice: updatePrc.basePrice.toString()}},
|
|
|
+ function (err, result) {
|
|
|
+ if(err){
|
|
|
+ ecb(err);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ ecb(null);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }, function(err){
|
|
|
+ if(err){
|
|
|
+ cb(err);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ cb(null);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ ], function (err) {
|
|
|
+ if(err){
|
|
|
+ finalCb(err);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ finalCb(null);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }, function (err) {
|
|
|
+ if(err){
|
|
|
+ callback(err, 'Error');
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ callback(0, '');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
mixUpdateRationItems (userID, compilationId, rationLibId, sectionId, updateItems, addItems, rIds, callback){
|
|
|
let me = this;
|
|
|
if (updateItems.length == 0 && rIds.length == 0) {
|