Browse Source

结账修复

shenjingwei 3 weeks ago
parent
commit
f300c33728

+ 11 - 13
src/custom/restcontroller/webmanage/sale/period/SQL/本期结账库存查询.sql

@@ -1,18 +1,16 @@
 select t.itemid, t.stockid, sum(t.qty) as qty
-from (SELECT itemid, stockid, qty
-      FROM sa_inventoryear
+from (
+      SELECT itemid, stockid, qty FROM sa_inventoryear
       WHERE YEAR = $year$ AND MONTH = $month$ and siteid=$siteid$
       union all
-      SELECT itemid, stockid, sum (qty) qty
-      FROM (
-          SELECT t2.itemid, t2.stockid, t1.siteid, CASE WHEN type LIKE '%入%' AND rb = 1 THEN t2.qty WHEN type LIKE '%入%' AND rb = 0 THEN - t2.qty WHEN type LIKE '%出%' AND rb = 1 THEN - t2.qty WHEN type LIKE '%出%'
-          AND rb = 0 THEN t2.qty END qty FROM st_stockbill t1
-          JOIN st_stockbill_items t2 ON t1.st_stockbillid = t2.st_stockbillid AND t1.siteid = t2.siteid
-          WHERE t1.STATUS = '审核' AND ifnull(t1.typemx, '') != '正品入库' AND t1.billdate >= $begdate$ AND t1.billdate <= $enddate$ and t1.siteid=$siteid$
-          UNION ALL
-          SELECT t2.itemid, t2.stockid, t1.siteid, CASE WHEN type LIKE '%入%' AND rb = 1 THEN t2.qty WHEN type LIKE '%入%' AND rb = 0 THEN - t2.qty END qty FROM st_stockbill t1
-          JOIN st_stockbill_items t2 ON t1.st_stockbillid = t2.st_stockbillid AND t1.siteid = t2.siteid
-          WHERE t1.STATUS = '审核' AND ifnull(t1.typemx, '') = '正品入库' AND ifnull(t2.amount, 0) != 0 AND t1.billdate >= $begdate$ AND t1.billdate <= $enddate$ and t1.siteid=$siteid$) t
-      GROUP BY itemid, stockid, siteid) t
+      SELECT t2.itemid, t2.stockid, t1.siteid, CASE WHEN type LIKE '%入%' AND rb = 1 THEN t2.qty WHEN type LIKE '%入%' AND rb = 0 THEN - t2.qty WHEN type LIKE '%出%' AND rb = 1 THEN - t2.qty WHEN type LIKE '%出%'
+      AND rb = 0 THEN t2.qty END qty FROM st_stockbill t1
+      JOIN st_stockbill_items t2 ON t1.st_stockbillid = t2.st_stockbillid AND t1.siteid = t2.siteid
+      WHERE t1.STATUS = '审核' AND ifnull(t1.typemx, '') != '正品入库' AND t1.billdate >= $begdate$ AND t1.billdate <= $enddate$ and t1.siteid=$siteid$
+      UNION ALL
+      SELECT t2.itemid, t2.stockid, t1.siteid, CASE WHEN type LIKE '%入%' AND rb = 1 THEN t2.qty WHEN type LIKE '%入%' AND rb = 0 THEN - t2.qty END qty FROM st_stockbill t1
+      JOIN st_stockbill_items t2 ON t1.st_stockbillid = t2.st_stockbillid AND t1.siteid = t2.siteid
+      WHERE t1.STATUS = '审核' AND ifnull(t1.typemx, '') = '正品入库' AND ifnull(t2.amount, 0) != 0 AND t1.billdate >= $begdate$ AND t1.billdate <= $enddate$ and t1.siteid=$siteid$
+) t
 group by t.itemid, t.stockid
 having sum(t.qty)!=0