下面是本人的第一个关于mysql的第一个存储过程案例
创建过程
BEGIN DECLARE cr_stack_depth INTEGER DEFAULT cr_debug.ENTER_MODULE('report_QCY', 'mosa_erp', 7);
/*
进销存统计表
inDate VARCHAR(10),branchID INT(11),warehouseList VARCHAR(500)
这下面遇到的这几个列名都是程序中传过来的
实质是将扣减领料信息、销售信息,退货信息,欠货发货信息、报废信息、
增加验收信息、自购信息、赠品入库信息、
的显示。
将所有进货数量相加-所有出货数量 = 理论数量,
总的进货金额/数量=单价
系统中有一个特定的分部就是总部,因为领料是自动扣减总部的,所以要判断是不是这个特殊的总部
*/
-- 变量
DECLARE startDate VARCHAR(20);-- 开始时间变量
DECLARE endDate VARCHAR(20); -- 结束
DECLARE nYear VARCHAR(4); -- 开始年
DECLARE nMount VARCHAR(2); -- 开始月
DECLARE lastYear INT; -- 结束年
DECLARE lastMount INT; -- 结束月
DECLARE mWarehouse INT; -- 存储默认仓库的id
DECLARE rWarehouse INT; -- 存储退货仓库的id
DECLARE qtWarehouse INT; -- 其它仓库id
-- 初始化变量
CALL cr_debug.UPDATE_WATCH2('inDate', inDate, cr_stack_depth);CALL cr_debug.UPDATE_WATCH2('branchID', branchID, cr_stack_depth);CALL cr_debug.UPDATE_WATCH2('warehouseList', warehouseList, cr_stack_depth);CALL cr_debug.UPDATE_WATCH2('startDate', startDate, cr_stack_depth);CALL cr_debug.UPDATE_WATCH2('endDate', endDate, cr_stack_depth);CALL cr_debug.UPDATE_WATCH2('nYear', nYear, cr_stack_depth);CALL cr_debug.UPDATE_WATCH2('nMount', nMount, cr_stack_depth);CALL cr_debug.UPDATE_WATCH2('lastYear', lastYear, cr_stack_depth);CALL cr_debug.UPDATE_WATCH2('lastMount', lastMount, cr_stack_depth);CALL cr_debug.UPDATE_WATCH2('mWarehouse', mWarehouse, cr_stack_depth);CALL cr_debug.UPDATE_WATCH2('rWarehouse', rWarehouse, cr_stack_depth);CALL cr_debug.UPDATE_WATCH2('qtWarehouse', qtWarehouse, cr_stack_depth); CALL cr_debug.TRACE(2, 2, 0, 5, cr_stack_depth); CALL cr_debug.TRACE(27, 27, 0, 37, cr_stack_depth);SET startDate = CONCAT(inDate,'-01');CALL cr_debug.UPDATE_WATCH2('startDate', startDate, cr_stack_depth);-- 传来'2012-12' startDate = '2012-12-01'
-- CONCAT(pram1,pram2)返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL
CALL cr_debug.TRACE(29, 29, 0, 98, cr_stack_depth);SELECT date_format(LAST_DAY(STR_TO_DATE(CONCAT(inDate,'-1'),'%Y-%m-%d')),'%Y-%m-%d') INTO endDate;CALL cr_debug.UPDATE_SYSTEM_CALLS(101);CALL cr_debug.UPDATE_WATCH2('endDate', endDate, cr_stack_depth);
-- DATE_FORMAT(date,format) 根据format 字符串安排date 值的格式。
-- LAST_DAY(date) 获取一个日期或日期时间值,返回该月最后一天对应的值。若参数无效,则返回NULL。
-- STR_TO_DATE(str,format) 它获取一个字符串 str 和一个格式字符串format。若格式字符串包含日期和时间部分,则 STR_TO_DATE()返回一个 DATETIME 值, 若该字符串只包含日期部分或时间部分,则返回一个 DATE 或TIME值
CALL cr_debug.TRACE(33, 33, 0, 27, cr_stack_depth);SET nYear = LEFT(inDate,4);CALL cr_debug.UPDATE_WATCH2('nYear', nYear, cr_stack_depth);
-- `LEFT`(str,len) 返回从字符串str 开始的len 最左字符
CALL cr_debug.TRACE(35, 35, 0, 33, cr_stack_depth);SET nMount = RIGHT(inDate,2)+1-1;CALL cr_debug.UPDATE_WATCH2('nMount', nMount, cr_stack_depth);
-- `RIGHT`(str,len)返回从字符串str 开始的len 最右字符
CALL cr_debug.TRACE(39, 45, 0, 7, cr_stack_depth);if(nMount=12)
THEN
CALL cr_debug.TRACE(41, 41, 3, 21, cr_stack_depth);SET lastMount = 1;CALL cr_debug.UPDATE_WATCH2('lastMount', lastMount, cr_stack_depth);
CALL cr_debug.TRACE(42, 42, 3, 26, cr_stack_depth);SET lastYear = nYear-1;CALL cr_debug.UPDATE_WATCH2('lastYear', lastYear, cr_stack_depth);
ELSE
CALL cr_debug.TRACE(44, 44, 3, 28, cr_stack_depth);SET lastMount = nMount-1;CALL cr_debug.UPDATE_WATCH2('lastMount', lastMount, cr_stack_depth);
END IF;
CALL cr_debug.TRACE(47, 93, 0, 2, cr_stack_depth);CREATE TEMPORARY TABLE if not exists jxc_report -- 不存在则创建临时表
(
warehouseID INT(11) NOT NULL,-- 仓库IDNOT NULL UNIQUE
witemNO VARCHAR(20) NOT NULL, -- 商品编码
pmrAmount DECIMAL(18,4),-- 领料数量
salAmount DECIMAL(18,4),-- 销货量
salPrice DECIMAL(18,4),-- 销货单价
salSum DECIMAL(18,4),-- 销货金额
qrAmount DECIMAL(18,4), -- 退货数量
qrPrice DECIMAL(18,4), -- 退货单价
qrSum DECIMAL(18,4), -- 退货金额
qooAmount DECIMAL(18,4),-- 欠货发货量
qooPrice DECIMAL(18,4),-- 欠货单价
qooSum DECIMAL(18,4),-- 欠货金额
irlAmount DECIMAL(18,4),-- 报废量
irlPrice DECIMAL(18,4),-- 报废单价
irlSum DECIMAL(18,4),-- 报废金额
sumChuAmount DECIMAL(18,4),-- 总出货量
sumChuSum DECIMAL(18,4), -- 总出货金额
-- 以下进库量的字段
qaiAmount DECIMAL(18,4),-- 验收量
qaiPrice DECIMAL(18,4),-- 验收单价
qaiSum DECIMAL(18,4),-- 验收金额
qsbAmount DECIMAL(18,4),-- 自购入库
qsbPrice DECIMAL(18,4),-- 自购单据
qsbSum DECIMAL(18,4),-- 自购金额
imwAmount DECIMAL(18,4),-- 供应商赠品入库
sumJinAmount DECIMAL(18,4),-- 总的进货量
sumJinSum DECIMAL(18,4), -- 总进货金额
adjustmentAmount DECIMAL(18,4) NULL, -- 调整数量
adjustmentPrice DECIMAL(18,4) NULL, -- 调整单价
adjustmentSum DECIMAL(18,4) NULL, -- 调整金额
transactionAmount DECIMAL(18,4) NULL, -- 盘点量
profitlossSum DECIMAL(18,4) NULL, -- 盘盈亏金额
sumAmount DECIMAL(18,4),-- 理论结存数量
sumPrice DECIMAL(18,4),-- 理论结存价格
sumSum DECIMAL(18,4),-- 理论结存金额
iisAmount DECIMAL(18,4), -- 实际库存量
iisPrice DECIMAL(18,4), -- 实际单价
iisSum DECIMAL(18,4), -- 实际金额
UNIQUE KEY (warehouseID,witemNO)
);
CALL cr_debug.TRACE(94, 102, 0, 2, cr_stack_depth);CREATE TEMPORARY TABLE if not exists jxc -- 不存在则创建临时表
(
wID INT(11) NOT NULL,-- 仓库IDNOT NULL UNIQUE
witemNO VARCHAR(20) NOT NULL, -- 商品编码
wamount DECIMAL(18,4) NULL, -- 领出数量
wprice DECIMAL(18,4) NULL, -- 领出单价
wjsun DECIMAL(18,4) NULL, -- 领出金额
UNIQUE KEY (wID,witemNO)
);
CALL cr_debug.TRACE(103, 110, 0, 2, cr_stack_depth);CREATE TEMPORARY TABLE if not exists warehouse -- 不存在则创建临时表
(
`ID` INT(11) NOT NULL,
`NAME` VARCHAR(20) NULL,
`IS_DEFAULT` DECIMAL(18,4) NULL,
`BRANCH_ID` DECIMAL(18,4) NULL,
UNIQUE KEY (ID)
);
CALL cr_debug.TRACE(111, 111, 0, 25, cr_stack_depth);TRUNCATE TABLE warehouse; -- 使用前先清空临时表
-- 需要统计的仓库,如果自选择了分部没选择对应的仓库,则查询该分部下的所有仓库,
CALL cr_debug.TRACE(114, 142, 0, 7, cr_stack_depth);IF(warehouseList<=>'0') THEN -- 传过来的仓库集合字符串恒等于'0'
/*
"<=>"的案例:
NULL-safe equal.这个操作符和=操作符执行相同的比较操作,不过在两个操作码均为NULL时,其所得值为1而不为NULL,而当一个操作码为NULL时,其所得值为0而不为NULL。
mysql> SELECT 1 <=> 1, NULL <=> NULL, 1 <=> NULL;
-> 1, 1, 0
*/
CALL cr_debug.TRACE(121, 121, 4, 138, cr_stack_depth);INSERT INTO warehouse(ID,NAME,IS_DEFAULT,BRANCH_ID) SELECT ID,NAME,IS_DEFAULT,BRANCH_ID FROM ivt_warehouse WHERE BRANCH_ID = branchID;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
CALL cr_debug.TRACE(123, 123, 4, 64, cr_stack_depth);SELECT ID INTO mWarehouse FROM warehouse WHERE IS_DEFAULT=1;CALL cr_debug.UPDATE_SYSTEM_CALLS(101);CALL cr_debug.UPDATE_WATCH2('mWarehouse', mWarehouse, cr_stack_depth);
-- 这个SELECT语法把选定的列直接存储到变量。因此,只有单一的行可以被取回。 重要: SQL变量名不能和列名一样。如果SELECT ... INTO这样的SQL语句包含一个对列的参考,并包含一个与列相同名字的 局部变量,MySQL当前把参考解释为一个变量的名字。
CALL cr_debug.TRACE(125, 125, 4, 64, cr_stack_depth);SELECT ID INTO rWarehouse FROM warehouse WHERE IS_DEFAULT=2;CALL cr_debug.UPDATE_SYSTEM_CALLS(101);CALL cr_debug.UPDATE_WATCH2('rWarehouse', rWarehouse, cr_stack_depth);
ELSE
CALL cr_debug.TRACE(127, 133, 4, 14, cr_stack_depth);WHILE(LOCATE(',',warehouseList)<>0)-- 传过来的字符串有值
-- LOCATE(substr,str) , LOCATE(substr,str,pos) 第一个语法返回字符串 str中子字符串substr的第一个出现位置。第二个语法返回字符串 str中子字符串substr的第一个出现位置, 起始位置在pos。如若substr 不在str中,则返回值为0。
DO
CALL cr_debug.TRACE(130, 130, 6, 92, cr_stack_depth);INSERT INTO warehouse(ID) VALUES(SUBSTR(warehouseList,1,LOCATE(',',warehouseList)-1));CALL cr_debug.UPDATE_SYSTEM_CALLS(102);-- 截取第一个字符串
-- SUBSTR(str,pos,len) 截取指定位置上的字符串
CALL cr_debug.TRACE(132, 132, 6, 124, cr_stack_depth);SET warehouseList = SUBSTR(warehouseList,LOCATE(',',warehouseList)+1,LENGTH(warehouseList)-LOCATE(',',warehouseList));CALL cr_debug.UPDATE_WATCH2('warehouseList', warehouseList, cr_stack_depth);-- 截取第一个字符串后的字符
END WHILE;
-- 上面的循环是获取除最后一仓库id的功能
CALL cr_debug.TRACE(135, 137, 4, 11, cr_stack_depth);IF LENGTH(warehouseList)>0 THEN -- 为了将最后一位字符获取
CALL cr_debug.TRACE(136, 136, 8, 56, cr_stack_depth);INSERT INTO warehouse(ID) VALUES(warehouseList);CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
END IF;
CALL cr_debug.TRACE(139, 139, 4, 138, cr_stack_depth);UPDATE warehouse AS w,ivt_warehouse AS iw SET w.`NAME`=iw.`NAME`,w.IS_DEFAULT=iw.IS_DEFAULT,w.BRANCH_ID=iw.BRANCH_ID WHERE w.ID=iw.ID;CALL cr_debug.UPDATE_SYSTEM_CALLS(104);-- 跟据传来的仓库id修改临时表
CALL cr_debug.TRACE(140, 140, 4, 64, cr_stack_depth);SELECT ID INTO mWarehouse FROM warehouse WHERE IS_DEFAULT=1;CALL cr_debug.UPDATE_SYSTEM_CALLS(101);CALL cr_debug.UPDATE_WATCH2('mWarehouse', mWarehouse, cr_stack_depth);-- 设置默认仓库的值
CALL cr_debug.TRACE(141, 141, 4, 64, cr_stack_depth);SELECT ID INTO rWarehouse FROM warehouse WHERE IS_DEFAULT=2;CALL cr_debug.UPDATE_SYSTEM_CALLS(101);CALL cr_debug.UPDATE_WATCH2('rWarehouse', rWarehouse, cr_stack_depth);-- 设置退货仓库的值
END IF;
/*
进货(仅计算默认仓库),因为验收时只入默认仓库 START
(数量[验收数量].单价[验收价格].金额)
*/
-- 验收入库的信息 start
CALL cr_debug.TRACE(150, 166, 0, 7, cr_stack_depth);IF(mWarehouse IS NOT NULL) THEN
CALL cr_debug.TRACE(151, 151, 1, 20, cr_stack_depth);TRUNCATE TABLE jxc; -- 使用前先清空临时表
-- 仓库id , 商品No,领出数量,领出单价,领出金额,查询验收单那个对应分部近的商品信息
CALL cr_debug.TRACE(153, 159, 1, 26, cr_stack_depth);INSERT INTO jxc(wID,witemNO,wamount,wprice,wjsun)
SELECT mWarehouse AS wID,qaiitem.ITEM_NO AS witemNO,SUM(qaiitem.ACCEPT_AMOUNT ) AS wamount,SUM(qaiitem.ACCEPT_AMOUNT *qaiitem.PRICE)/SUM(qaiitem.ACCEPT_AMOUNT ) AS wprice,SUM(qaiitem.ACCEPT_AMOUNT *qaiitem.PRICE) AS wjsun
FROM qcy_acceptence_info_item AS qaiitem
LEFT JOIN qcy_acceptence_info AS qainfo ON qainfo.ID = qaiitem.HEADER_ID
LEFT JOIN bd_material AS bdp ON bdp.MATERIAL_NO = qaiitem.ITEM_NO
WHERE qaiitem.ACCEPT_AMOUNT >0 AND qainfo.ACCEPT_BRANCH_ID=branchID AND (qainfo.`STATUS`=4 OR qainfo.`STATUS`=2) AND qainfo.ACCEPT_DATE BETWEEN startDate AND endDate
GROUP BY qaiitem.ITEM_NO;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 将得到的结果放到总表中
CALL cr_debug.TRACE(161, 163, 1, 80, cr_stack_depth);INSERT INTO jxc_report(warehouseID,witemNO,qaiAmount,qaiPrice,qaiSum)
SELECT wID,witemNO,wamount,wprice,wjsun FROM jxc AS i
ON DUPLICATE KEY UPDATE qaiAmount = i.wamount,qaiPrice=i.wprice,qaiSum=i.wjsun;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 使用后清理临时表
CALL cr_debug.TRACE(165, 165, 1, 20, cr_stack_depth);TRUNCATE TABLE jxc;
END IF;
-- 验收入库信息 STOP
-- 自购入库信息 START------------------------------------
CALL cr_debug.TRACE(171, 171, 1, 20, cr_stack_depth);TRUNCATE TABLE jxc;
CALL cr_debug.TRACE(172, 177, 1, 41, cr_stack_depth);INSERT INTO jxc(wID,witemNO,wamount,wprice,wjsun)
SELECT self.WAREHOUSE_ID AS wID, item.ITEM_NO AS witemNO,sum(item.AMOUNT) AS wamount ,sum(item.AMOUNT*item.PRICE)/sum(item.AMOUNT) AS wprice, SUM(item.AMOUNT*item.PRICE) AS wjsun
from qcy_self_buy_item AS item
LEFT JOIN qcy_self_buy AS self ON self.ID=item.HEADER_ID
where item.AMOUNT>0 AND item.PRICE>0 AND self.IN_BRANCH_ID=branchID AND self.WAREHOUSE_ID in (SELECT ID from warehouse) and (self.`STATUS`=4 OR self.`STATUS`=2) AND self.PURCHASE_DATE BETWEEN startDate AND endDate
GROUP BY self.WAREHOUSE_ID,item.ITEM_NO;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 将得到的结果放到总表中
CALL cr_debug.TRACE(180, 182, 1, 80, cr_stack_depth);INSERT INTO jxc_report(warehouseID,witemNO,qsbAmount,qsbPrice,qsbSum)
SELECT wID,witemNO,wamount,wprice,wjsun FROM jxc AS i
ON DUPLICATE KEY UPDATE qsbAmount = i.wamount,qsbPrice=i.wprice,qsbSum=i.wjsun;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 使用后清理临时表
CALL cr_debug.TRACE(184, 184, 1, 20, cr_stack_depth);TRUNCATE TABLE jxc;
-- ---------- 自购入库信息 STOP--------------------------
-- ---------- 供应商赠品入库作业 START-------------------
CALL cr_debug.TRACE(188, 193, 1, 50, cr_stack_depth);INSERT INTO jxc(wID,witemNO,wamount)
SELECT imw.TARGET_WAREHOUSE_ID AS wID,imwItem.ITEM_NO AS witemNO,sum(imwItem.AMOUNT) AS wamount
FROM ivt_move_warehouse_info_item AS imwItem
LEFT JOIN `ivt_move_warehouse_info` AS imw ON imw.ID = imwItem.HEADER_ID
where imwItem.AMOUNT>0 AND imw.TARGET_BRANCH_ID =branchID AND imw.TARGET_WAREHOUSE_ID IN (SELECT ID from warehouse) AND imw.RECEIPT_DATE BETWEEN startDate AND endDate AND (imw.`STATUS`=4 OR imw.`STATUS`=2)
GROUP BY imw.TARGET_WAREHOUSE_ID,imwItem.ITEM_NO;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 将得到的结果放到总表中
CALL cr_debug.TRACE(197, 199, 1, 48, cr_stack_depth);INSERT INTO jxc_report(warehouseID,witemNO,imwAmount)
SELECT wID,witemNO,wamount FROM jxc AS i
ON DUPLICATE KEY UPDATE imwAmount = i.wamount ;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 使用后清理临时表
CALL cr_debug.TRACE(201, 201, 1, 20, cr_stack_depth);TRUNCATE TABLE jxc;
-- 供应商赠品入库作业 STOP-------------------------------
/**
进货(仅计算默认仓库) END
(数量[验收数量].单价[验收价格].金额)
*/
/*
出货(仅计算默认仓库) START
(数量.单价.金额)
*/
/**
原物料需求扣除 START,默认扣减的是总仓默认仓库
**/
CALL cr_debug.TRACE(217, 232, 0, 7, cr_stack_depth);IF(mWarehouse IS NOT NULL) THEN
CALL cr_debug.TRACE(218, 218, 1, 20, cr_stack_depth);TRUNCATE TABLE jxc;
CALL cr_debug.TRACE(219, 224, 2, 24, cr_stack_depth);INSERT INTO jxc(wID,witemNO,wamount)
SELECT mWarehouse AS warehouseID,pmri.ITEM_NO AS witemNO,sum(pmri.REQUIREMENT_AMOUNT) AS quitAmount
FROM prod_material_requirement_item AS pmri
LEFT JOIN `prod_material_requirement` AS pmr ON pmr.ID = pmri.HEADER_ID
where pmri.REQUIREMENT_AMOUNT>0 AND pmr.BRANCH_ID = branchID AND pmr.RECEIPT_DATE BETWEEN startDate AND endDate AND (pmr.`STATUS`=4 OR pmr.`STATUS`=2)
GROUP BY pmri.ITEM_NO;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 将得到的结果放到总表中
CALL cr_debug.TRACE(227, 229, 1, 48, cr_stack_depth);INSERT INTO jxc_report(warehouseID,witemNO,pmrAmount)
SELECT wID,witemNO,wamount FROM jxc AS i
ON DUPLICATE KEY UPDATE pmrAmount = i.wamount ;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
CALL cr_debug.TRACE(231, 231, 1, 20, cr_stack_depth);TRUNCATE TABLE jxc;
end IF;
/**
原物料需求扣除 STOP
**/
/*
销货单 start
*/
CALL cr_debug.TRACE(239, 253, 0, 7, cr_stack_depth);IF(mWarehouse IS NOT NULL) THEN
CALL cr_debug.TRACE(240, 240, 1, 20, cr_stack_depth);TRUNCATE TABLE jxc; -- 使用前先清空临时表
CALL cr_debug.TRACE(242, 246, 1, 42, cr_stack_depth);INSERT INTO jxc(wID,witemNO,wamount,wprice,wjsun)
SELECT qssi.WAREHOUSE_ID, qssi.ITEM_NO,sum(qssi.SALES_AMOUNT),sum(qssi.SALES_AMOUNT*qssi.PRICE)/sum(qssi.SALES_AMOUNT),sum(qssi.SALES_AMOUNT*qssi.PRICE) from qcy_sales_slip_item AS qssi
LEFT JOIN qcy_sales_slip AS qss ON qss.ID = qssi.HEADER_ID
where qss.BATCH_NO = branchID AND qssi.WAREHOUSE_ID =mWarehouse AND qssi.SALES_AMOUNT>0 AND qssi.PRICE >0 AND (qss.`STATUS`=2 OR qss.`STATUS`=4) AND qss.SALES_DATE BETWEEN startDate AND endDate
GROUP BY qssi.WAREHOUSE_ID ,qssi.ITEM_NO;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 将得到的结果放到总表中
CALL cr_debug.TRACE(249, 251, 1, 80, cr_stack_depth);INSERT INTO jxc_report(warehouseID,witemNO,salAmount,salPrice,salSum)
SELECT wID,witemNO,wamount,wprice,wjsun FROM jxc AS i
ON DUPLICATE KEY UPDATE salAmount = i.wamount,salPrice=i.wprice,salSum=i.wjsun;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
CALL cr_debug.TRACE(252, 252, 1, 20, cr_stack_depth);TRUNCATE TABLE jxc; -- 使用前先清空临时表
end IF;
/*
销货单 STOP
*/
/*
退货数量 START
*/
CALL cr_debug.TRACE(260, 274, 0, 7, cr_stack_depth);IF(rWarehouse IS NOT NULL) THEN
CALL cr_debug.TRACE(261, 261, 1, 20, cr_stack_depth);TRUNCATE TABLE jxc;
CALL cr_debug.TRACE(262, 267, 1, 25, cr_stack_depth);INSERT INTO jxc(wID,witemNO,wamount,wprice,wjsun)
SELECT rWarehouse AS wID, ritem.ITEM_NO AS witemNO,SUM(ritem.REJECT_AMOUNT ),SUM(ritem.REJECT_AMOUNT*ritem.PRICE)/ SUM(ritem.REJECT_AMOUNT ),SUM(ritem.REJECT_AMOUNT*ritem.PRICE)
FROM qcy_rejection_info_item AS ritem
LEFT JOIN qcy_rejection_info AS rinfo ON rinfo.ID = ritem.HEADER_ID
WHERE rinfo.ACCEPT_BRANCH_ID = branchID AND rinfo.WAREHOUSE_ID = rWarehouse AND ritem.REJECT_AMOUNT>0 AND ritem.PRICE>0 AND (rinfo.`STATUS`=4 OR rinfo.`STATUS`=2)
GROUP BY ritem.ITEM_NO;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 将得到的结果放到总表中
CALL cr_debug.TRACE(270, 272, 1, 77, cr_stack_depth);INSERT INTO jxc_report(warehouseID,witemNO,qrAmount,qrPrice,qrSum)
SELECT wID,witemNO,wamount,wprice,wjsun FROM jxc AS i
ON DUPLICATE KEY UPDATE qrAmount = i.wamount,qrPrice=i.wprice,qrSum=i.wjsun;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
CALL cr_debug.TRACE(273, 273, 1, 20, cr_stack_depth);TRUNCATE TABLE jxc; -- 使用前先清空临时表
END IF;
/*
退货数量 STOP
*/
/*
-- 欠货 start
*/
CALL cr_debug.TRACE(283, 297, 0, 7, cr_stack_depth);IF(rWarehouse IS NOT NULL) THEN
CALL cr_debug.TRACE(284, 289, 1, 49, cr_stack_depth);INSERT INTO jxc(wID,witemNO,wamount,wprice,wjsun)
SELECT qooitem.WAREHOUSE_ID AS wID , qooitem.ITEM_NO AS witemNO,SUM(qooitem.SALES_AMOUNT) AS wamount ,SUM(qooitem.PRICE*qooitem.SALES_AMOUNT)/SUM(qooitem.SALES_AMOUNT) AS wprice ,SUM(qooitem.PRICE*qooitem.SALES_AMOUNT) AS wjsin
FROM qcy_owe_orders_item AS qooitem
LEFT JOIN qcy_owe_orders AS qooinfo ON qooinfo.ID=qooitem.HEADER_ID
WHERE qooinfo.BRANCH_ID =branchID AND qooitem.WAREHOUSE_ID=rWarehouse AND (qooinfo.`STATUS`=2 OR qooinfo.`STATUS`=4) AND qooitem.SALES_AMOUNT>0 AND qooitem.PRICE>0
GROUP BY qooitem.WAREHOUSE_ID, qooitem.ITEM_NO;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 将得到的结果放到总表中
CALL cr_debug.TRACE(292, 294, 1, 80, cr_stack_depth);INSERT INTO jxc_report(warehouseID,witemNO,qooAmount,qooPrice,qooSum)
SELECT wID,witemNO,wamount,wprice,wjsun FROM jxc AS i
ON DUPLICATE KEY UPDATE qooAmount = i.wamount,qooPrice=i.wprice,qooSum=i.wjsun;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
CALL cr_debug.TRACE(296, 296, 1, 20, cr_stack_depth);TRUNCATE TABLE jxc;
END IF;
/*
-- 欠货 stop
*/
/*
--------------- 报废(区分仓库) ------------ START
*/
CALL cr_debug.TRACE(307, 307, 0, 19, cr_stack_depth);TRUNCATE TABLE jxc; -- 使用前先清空临时表
CALL cr_debug.TRACE(308, 312, 1, 42, cr_stack_depth);INSERT INTO jxc(wID,witemNO,wamount,wprice,wjsun)
SELECT irli.WAREHOUSE_ID, irli.ITEM_NO,SUM(irli.ITEM_AMOUNT),SUM(irli.ITEM_AMOUNT*irli.PRICE)/SUM(irli.ITEM_AMOUNT),SUM(irli.ITEM_AMOUNT*irli.PRICE) FROM ivt_resource_loss_item AS irli
LEFT JOIN ivt_resource_loss AS irl ON irli.HEADER_ID = irl.ID
WHERE irl.BRANCH_ID = branchID AND irli.WAREHOUSE_ID IN (SELECT ID FROM warehouse) AND irli.ITEM_AMOUNT>0 AND irli.PRICE>0 AND (irl.`STATUS`=4 OR irl.`STATUS`=2) AND irl.REPORT_DATE BETWEEN startDate AND endDate
GROUP BY irli.WAREHOUSE_ID,irli.ITEM_NO;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 将得到的结果放到总表中
CALL cr_debug.TRACE(314, 319, 2, 63, cr_stack_depth);INSERT INTO jxc_report (warehouseID, witemNO, irlAmount, irlPrice, irlSum)
SELECT wID, witemNO, wamount , wprice, wjsun
FROM
jxc AS j
ON DUPLICATE KEY UPDATE
irlAmount = j.wamount, irlPrice = j.wprice, irlSum = j.wjsun;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 使用后清理临时表
CALL cr_debug.TRACE(321, 321, 0, 19, cr_stack_depth);TRUNCATE TABLE jxc;
/*
--------------- 报废 ------------ END
*/
/*
--------------- 调整(区分仓库) ------------ START
*/
CALL cr_debug.TRACE(329, 329, 0, 19, cr_stack_depth);TRUNCATE TABLE jxc; -- 使用前先清空临时表
CALL cr_debug.TRACE(330, 336, 1, 41, cr_stack_depth);INSERT INTO jxc(wID,witemNO,wamount,wprice,wjsun)
SELECT ivti.warehouse_ID ,ivti.ITEM_NO,SUM(ivti.TARGET_AMOUNT),SUM(ivti.TARGET_AMOUNT*ivti.PRICE)/SUM(ivti.TARGET_AMOUNT),SUM(ivti.TARGET_AMOUNT*ivti.PRICE)
FROM ivt_adjustment_storage_item as ivti
LEFT JOIN ivt_adjustment_storage AS ivt ON ivti.HEADER_ID = ivt.ID
where ivt.BRANCH_ID = branchID and ivti.WAREHOUSE_ID IN (SELECT ID FROM warehouse) AND ivti.TARGET_AMOUNT>0 and ivti.PRICE>0 AND (ivt.`STATUS`=2 OR ivt.`STATUS`=4) AND ivt.ADJUSTMENT_DATE BETWEEN startDate AND endDate
GROUP BY ivti.WAREHOUSE_ID,ivti.ITEM_NO;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 将得到的结果放到总表中
CALL cr_debug.TRACE(338, 340, 1, 99, cr_stack_depth);INSERT INTO jxc_report(warehouseID,witemNO,adjustmentAmount,adjustmentPrice,adjustmentSum)
SELECT wID AS warehouseID,witemNO,wamount,wprice,wjsun FROM jxc AS j
ON DUPLICATE KEY UPDATE adjustmentAmount=j.wamount,adjustmentPrice=j.wprice,adjustmentSum=j.wjsun;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 使用后清理临时表
CALL cr_debug.TRACE(342, 342, 0, 19, cr_stack_depth);TRUNCATE TABLE jxc;
/*
--------------- 调整 -------------------- END
*/
/*
--------------- 盘盈亏(区分仓库) ------------ START
*/
CALL cr_debug.TRACE(349, 349, 0, 19, cr_stack_depth);TRUNCATE TABLE jxc; -- 使用前先清空临时表
CALL cr_debug.TRACE(350, 355, 1, 40, cr_stack_depth);INSERT INTO jxc(wID,witemNO,wamount,wjsun)
SELECT iit.WAREHOUSE_ID, iiti.ITEM_NO,SUM(iiti.PROFIT_LOSS),SUM(iiti.PROFIT_LOSS_COST) FROM
ivt_inventory_transaction_item AS iiti
LEFT JOIN ivt_inventory_transaction AS iit ON iit.ID = iiti.HEADER_ID
where iiti.PROFIT_LOSS IS NOT NULL AND (iit.`STATUS`=2 OR iit.`STATUS`=4) and iit.WAREHOUSE_ID IN(SELECT ID FROM warehouse) AND iit.TRANSACTION_DATE BETWEEN startDate AND endDate
GROUP BY iit.WAREHOUSE_ID,iiti.ITEM_NO;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
CALL cr_debug.TRACE(357, 359, 1, 75, cr_stack_depth);INSERT INTO jxc_report(warehouseID,witemNO,transactionAmount,profitlossSum)
SELECT wID AS warehouseID,witemNO,wamount,wjsun FROM jxc AS j
ON DUPLICATE KEY UPDATE transactionAmount=j.wamount,profitlossSum=j.wjsun;CALL cr_debug.UPDATE_SYSTEM_CALLS(102);
-- 使用后清理临时表
CALL cr_debug.TRACE(361, 361, 0, 19, cr_stack_depth);TRUNCATE TABLE jxc;
/*
--------------- 盘盈亏 -------------------- END
*/
/*
--------------- 期末结存 ------------ START
*/
/*
UPDATE jxc_report set finalAmount=is_null_return_zero(endTheoryAmount)+is_null_return_zero(adjustmentAmount)+is_null_return_zero(transactionAmount);
UPDATE jxc_report SET finalSum = endTheorySun/finalAmount;
*/
/*
--------------- 期末结存 -------------------- END
*/
CALL cr_debug.TRACE(375, 402, 0, 29, cr_stack_depth);SELECT w.`NAME` AS '仓库',bdp.MATERIAL_NO AS '商品编号',bdp.MATERIAL_NAME AS '商品名称' ,bdu.`NAME` AS '单位',
j.pmrAmount AS '原物料扣减量',
j.salAmount AS '销货量', j.salPrice AS '销货单价', j.salSum AS '销货金额',
j.qrAmount AS '退货量',j.qrPrice AS '退货单价', j.qrSum AS '退货金额',
j.qooAmount AS '欠货发货量' ,j.qooPrice AS '欠货发货单价', j.qooSum AS '欠货发货金额',
j.irlAmount AS '报废量', j.irlPrice AS '报废单价', j.irlSum AS '报废金额',
j.pmrAmount+j.salAmount+j.qrAmount+j.qooAmount+j.irlAmount AS '总出货量',j.salSum+j.qrSum+j.qooSum+j.irlSum AS '总出货金额',
j.qaiAmount AS '验收量' , j.qaiPrice AS '验收单价', j.qaiSum AS '验收金额',
j.qsbAmount AS '自购量' ,j.qsbPrice as '自购单价', j.qsbSum AS '自购金额',
j.imwAmount AS '供应商赠品入库量',
j.qaiAmount+j.qsbAmount+j.imwAmount AS '总进货量',j.qaiSum+j.qsbSum AS '总进货金额',
j.adjustmentAmount AS '调整量',j.adjustmentPrice AS '调整单价',j.adjustmentSum AS '调整金额',
j.transactionAmount AS '盘盈亏量',j.profitlossSum AS '盘盈亏金额',
(j.qaiAmount+j.qsbAmount+j.imwAmount) - (j.pmrAmount+j.salAmount+j.qrAmount+j.qooAmount+j.irlAmount) AS '理论结存量',
storagItem.ITEM_AMOUNT AS '实际结存量',storagItem.PRICE AS '实际结存单价', storagItem.ITEM_AMOUNT*storagItem.PRICE AS '实际结存金额'
FROM jxc_report AS j
LEFT JOIN bd_material AS bdp ON bdp.MATERIAL_NO = j.witemNO
LEFT JOIN warehouse AS w ON w.ID = j.warehouseID
LEFT JOIN ivt_item_storage AS storagItem ON storagItem.WAREHOUSE_ID = w.ID
LEFT JOIN bd_unit AS bdu ON bdu.ID = bdp.BASIC_UNIT
WHERE storagItem.ITEM_NO= j.witemNO
ORDER BY warehouseID,witemNO;CALL cr_debug.UPDATE_SYSTEM_CALLS(101);
CALL cr_debug.TRACE(404, 404, 0, 26, cr_stack_depth);TRUNCATE TABLE jxc_report; -- 使用后清空临时表
CALL cr_debug.TRACE(405, 405, 0, 25, cr_stack_depth);TRUNCATE TABLE warehouse;
CALL cr_debug.TRACE(406, 406, 0, 3, cr_stack_depth);END