| 
					
				 | 
			
			
				@@ -223,12 +223,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 m_spread = msbInfo.m_spread; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 updateId = msbInfo.id; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const msSql = ms_id ? ' AND `ms_id` = ' + ms_id : ''; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const sql = 'SELECT SUM(' + this.ctx.helper.getQtySource(this.ctx.material.qty_source) + '*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=?' + msSql + ' AND `is_join`=1'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const sqlParam = [this.ctx.material.id, mb_id]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const mb_quantity = await transaction.queryOne(sql, sqlParam); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            console.log(mb_quantity); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const newQuantity = this.ctx.helper.round(mb_quantity.quantity, this.ctx.material.decimal.qty); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const newQuantity = await this.getMbQuantity(transaction, this.ctx.material.id, this.ctx.material.qty_source, this.ctx.material.decimal.qty, mb_id, ms_id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, m_spread), this.ctx.material.decimal.tp); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             const updateData = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 id: updateId, 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -245,10 +240,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     const msbList = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id } }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     for (const msb of msbList) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         if (msb.ms_id !== parseInt(ms_id)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            const sql4 = 'SELECT SUM(' + this.ctx.helper.getQtySource(this.ctx.material.qty_source) + '*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `ms_id`=? AND `is_join`=1'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            const sqlParam4 = [this.ctx.material.id, mb_id, msb.ms_id]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            const mb_quantity4 = await transaction.queryOne(sql4, sqlParam4); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                            const newQuantity4 = this.ctx.helper.round(mb_quantity4.quantity, this.ctx.material.decimal.qty); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            const newQuantity4 = await this.getMbQuantity(transaction, this.ctx.material.id, this.ctx.material.qty_source, this.ctx.material.decimal.qty, mb_id, msb.ms_id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                             const newTp4 = this.ctx.helper.round(this.ctx.helper.mul(newQuantity4, msb.m_spread), this.ctx.material.decimal.tp); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                             const updateData4 = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                 id: msb.id, 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -325,10 +317,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     const updateMsIds = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     const msbList = await transaction.select(this.ctx.service.materialStageBills.tableName, { where: { mid: this.ctx.material.id, mb_id: mbInfo.id } }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     for (const msb of msbList) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        const sql = 'SELECT SUM(' + this.ctx.helper.getQtySource(this.ctx.material.qty_source) + '*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `ms_id`=? AND `is_join`=1'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        const sqlParam = [this.ctx.material.id, mbInfo.id, msb.ms_id]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        const mb_quantity = await transaction.queryOne(sql, sqlParam); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        const newQuantity = this.ctx.helper.round(mb_quantity.quantity, this.ctx.material.decimal.qty); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        const newQuantity = await this.getMbQuantity(transaction, this.ctx.material.id, this.ctx.material.qty_source, this.ctx.material.decimal.qty, mbInfo.id, msb.ms_id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, msb.m_spread), this.ctx.material.decimal.tp); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         const updateData = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                             id: msb.id, 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -356,11 +345,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     await transaction.update(this.ctx.service.materialBills.tableName, updateBillsData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    const sql = 'SELECT SUM(' + this.ctx.helper.getQtySource(this.ctx.material.qty_source) + '*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    const sqlParam = [this.ctx.material.id, mbInfo.id]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    const mb_quantity = await transaction.queryOne(sql, sqlParam); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    console.log(mb_quantity); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    const newQuantity = this.ctx.helper.round(mb_quantity.quantity, this.ctx.material.decimal.qty); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    const newQuantity = await this.getMbQuantity(transaction, this.ctx.material.id, this.ctx.material.qty_source, this.ctx.material.decimal.qty, mbInfo.id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, mbInfo.m_spread), this.ctx.material.decimal.tp); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     const updateData = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         id: mbInfo.id, 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -499,7 +484,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         gather_qty: ml.gather_qty, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         quantity: ml.quantity ? ml.quantity : 0, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         expr: ml.expr ? ml.expr : '', 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        is_join: is_join ? 0 : 1, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        is_join: is_join ? 0 : is_change ? 2 : 1, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         in_time: new Date(), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     if (ml.sid) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -529,7 +514,7 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         gather_qty: ml.gather_qty, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         quantity: ml.quantity ? ml.quantity : 0, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         expr: ml.expr ? ml.expr : '', 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        is_join: is_join ? 0 : 1, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        is_join: is_join ? 0 : is_change ? 2 : 1, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         is_self: 1, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                         in_time: new Date(), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     }; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -847,6 +832,18 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 throw err; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        async getMbQuantity(transaction, mid, qty_source, qty_decimal, mb_id, ms_id = null, needRound = 1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const msSql = ms_id ? ' AND `ms_id` = ' + ms_id : ''; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const sql = 'SELECT SUM(' + this.ctx.helper.getQtySource(qty_source) + '*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=?' + msSql + ' AND `is_join`=1'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const sqlParam = [mid, mb_id]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const mb_quantity = await transaction.queryOne(sql, sqlParam); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const sql2 = 'SELECT SUM(' + this.ctx.helper.getQtySource(qty_source, 1) + '*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=?' + msSql + ' AND `is_join`=2'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const sqlParam2 = [mid, mb_id]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const mb_quantity2 = await transaction.queryOne(sql2, sqlParam2); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            const newQuantity = this.ctx.helper.add(mb_quantity.quantity, mb_quantity2.quantity); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return needRound ? this.ctx.helper.round(newQuantity, qty_decimal) : newQuantity; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     return MaterialList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 }; 
			 |