| 
					
				 | 
			
			
				@@ -287,61 +287,40 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				          * @return {Promise<void>} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				          */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         async getUsedData(tid, cid) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const lastStage = await this.ctx.service.stage.getLastestStage(tid, true); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let filter; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (this.ctx.stage && lastStage.id === this.ctx.stage.id) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ? And sChange.`sorder` <= ?))', 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    [lastStage.order, lastStage.order, this.ctx.stage.curTimes, this.ctx.stage.curOrder]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (this.ctx.stage.status === audit.stage.status.checked) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const sql = 'SELECT scf.*, cal.unit_price ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  FROM ' + this.tableName + ' scf ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  WHERE scf.tid = ? And scf.cid = ? And s.order = ?'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const result = await this.db.query(sql, [tid, cid, this.ctx.stage.order]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                if (lastStage.status === audit.stage.status.uncheck) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    filter = 'And s.order < ' + lastStage.order; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } else if (lastStage.status === audit.stage.status.checked) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    filter = ''; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } else if (lastStage.status === audit.stage.status.checkNo) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ?))', 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        [lastStage.order, lastStage.order, lastStage.times]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(lastStage.id, lastStage.times); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    filter = this.db.format(' And (s.`order` < ? || (s.`order` = ? And sChange.`stimes` <= ? And sChange.`sorder` <= ?))', 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        [lastStage.order, lastStage.order, lastStage.times, curAuditor.order - 1]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const sql = 'SELECT * FROM ' + this.tableName + ' WHERE sid = ? AND (stimes * 100 + sorder) <= (? * 100 + ?)'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const curAll = await this.db.query(sql, [this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cid', 'cbid'], 'stimes', 'sorder'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return cur; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const sql = 'SELECT c.lid, c.pid, SUM(c.qty) as used_qty,' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        '    cb.tid, cb.cid, cb.id, cb.code, cb.name, cb.unit, cb.unit_price, cb.detail, cb.samount, cb.bwmx, cb.detail' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        '  FROM ' + this.ctx.service.changeAuditList.tableName + ' As cb' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        '  LEFT JOIN ' + this.tableName + ' As c ON cb.id = c.cbid ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        '  INNER JOIN (' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        '    SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, `lid`, `pid`, `cbid`, sChange.`sid`, `cid` ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        '      FROM ' + this.tableName + ' As sChange' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        '      LEFT JOIN ' + this.ctx.service.stage.tableName + ' As s ON sChange.sid = s.id' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        '      WHERE sChange.tid = ? AND cid = ?' + filter + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        '      GROUP By `lid`, `pid`, `cbid`, sChange.`sid`' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        '  ) As m' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        '  ON (c.stimes * ' + timesLen + ' + c.sorder) = m.flow And c.`cbid` = m.`cbid` AND c.`sid` = m.`sid` And c.`cid` = m.`cid` And c.`lid` = m.`lid` And c.`pid` = m.`pid`' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        '  WHERE cb.cid = ?' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        '  GROUP By c.`cbid`'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const sqlParam = [tid, cid, cid]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return await this.db.query(sql, sqlParam); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         async getFinalUsedData(tid, cid) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const sql = 'SELECT c.lid, c.pid, SUM(c.qty) as used_qty,' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                '    cb.tid, cb.cid, cb.id, cb.code, cb.name, cb.unit, cb.unit_price, cb.detail, cb.samount, cb.oamount, cb.bwmx, cb.gcl_id' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                '  FROM ' + this.ctx.service.changeAuditList.tableName + ' As cb' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                '  LEFT JOIN ' + this.tableName + ' As c ON cb.id = c.cbid ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                '  INNER JOIN (' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                '    SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, `lid`, `pid`, `cbid`, sChange.`sid`, `cid` ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                '      FROM ' + this.tableName + ' As sChange' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                '      LEFT JOIN ' + this.ctx.service.stage.tableName + ' As s ON sChange.sid = s.id' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                '      WHERE sChange.tid = ? AND cid = ?' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                '      GROUP By `lid`, `pid`, `cbid`, sChange.`sid`' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                '  ) As m' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                '  ON (c.stimes * ' + timesLen + ' + c.sorder) = m.flow And c.`cbid` = m.`cbid` AND c.`sid` = m.`sid` And c.`cid` = m.`cid` And c.`lid` = m.`lid` And c.`pid` = m.`pid`' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                '  WHERE cb.cid = ?' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                '  GROUP By c.`cbid`'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            const sqlParam = [tid, cid, cid]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return await this.db.query(sql, sqlParam); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (stage.status === audit.stage.status.checked) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const sql = 'SELECT scf.*, cal.unit_price ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  WHERE scf.tid = ? And scf.cid = ? And s.order <= ?'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const result = await this.db.query(sql, [tid, cid, this.ctx.stage.order]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const preSql = 'SELECT scf.* ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  WHERE scf.tid = ? And scf.cid = ? And s.order < ?'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const pre = await this.db.query(preSql, [tid, cid, stage.order]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const sql = 'SELECT * FROM ' + this.tableName + ' WHERE sid = ? AND (stimes * 100 + sorder) <= (? * 100 + ?)'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const curAll = await this.db.query(sql, [this.ctx.stage.id, this.ctx.stage.curTimes, this.ctx.stage.curOrder]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cid', 'cbid'], 'stimes', 'sorder'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return [...pre, ...cur]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         /** 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -432,18 +411,18 @@ module.exports = app => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         async _getChangeBillsWithUsedInfo(stage) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             if (stage.status === audit.stage.status.checked) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const sql = 'SELECT scf.*, cal.unit_price ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    '  FROM ' + this.tableName + ' scf ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     '  LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     '  WHERE scf.tid = ? And s.order <= ?'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const result = await this.db.query(sql, [stage.tid, stage.order]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const preSql = 'SELECT scf.* ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    '  FROM ' + this.tableName + ' scf ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    '  FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     '  LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                     '  WHERE scf.tid = ? And s.order < ?'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const pre = await this.db.query(preSql, [stage.tid, stage.order]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                const sql = 'SELECT * FROM ' + this.ctx.service.stageChange.tableName + ' WHERE sid = ? AND (stimes * 100 + sorder) <= (? * 100 + ?)'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                const sql = 'SELECT * FROM ' + this.tableName + ' WHERE sid = ? AND (stimes * 100 + sorder) <= (? * 100 + ?)'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const curAll = await this.db.query(sql, [stage.id, stage.curTimes, stage.curOrder]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cid', 'cbid'], 'stimes', 'sorder'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 return [...pre, ...cur]; 
			 |