------------------
CREATE PROCEDURE proc_cb_lj AS
BEGIN
DECLARE @t TABLE(FORDERBILLNO NVARCHAR(255),FPRODUCTID_FNUMBER NVARCHAR(255),FPRODUCTID_FNAME NVARCHAR(255),
FMATERIALID INT,FSUMPRODUCTQTY DECIMAL(23,10),FSUMPRODUCTAMOUNT DECIMAL(23,10),UNITCOST DECIMAL(23,10))
CREATE TABLE #TM_CB_ORDERCOSTTRACKRPT (FMATERIALID INT ,FORDERBILLDATE DATETIME NULL, FORDERBILLNO NVARCHAR (255) NOT NULL DEFAULT ' ', FORDERBILLSEQ NVARCHAR (160) NOT NULL DEFAULT ' ', FORDERBILLNOSEQ NVARCHAR (160) NOT NULL DEFAULT ' ', FORDERENTRYID INT NOT NULL DEFAULT 0, FBILLENTRYID INT NOT NULL DEFAULT 0, FPRODUCTID_FNUMBER VARCHAR (255) NOT NULL DEFAULT ' ', FPRODUCTID_FNAME NVARCHAR (255) NOT NULL DEFAULT ' ', FSPECIFICATION NVARCHAR (510) NOT NULL DEFAULT ' ', FBOMNUMBERFIELD_FNUMBER VARCHAR (510) NOT NULL DEFAULT ' ', FBATCHFIELD_FNAME NVARCHAR (510) NOT NULL DEFAULT ' ', FAUXPROPID INT NOT NULL DEFAULT 0, FAIDPROPERTYFIELD_FNAME NVARCHAR (1024) NOT NULL DEFAULT ' ', FMTONO NVARCHAR (510) NOT NULL DEFAULT ' ', FBASICUNITFIELD_FNAME NVARCHAR (255) NOT NULL DEFAULT ' ', FBILLQTY DECIMAL (23, 10) NOT NULL DEFAULT 0, FNOPRODUCTQTY DECIMAL (23, 10) NOT NULL DEFAULT 0, FCOSTCENTERNUMBER VARCHAR (255) NOT NULL DEFAULT ' ', FCOSTCENTERNAME NVARCHAR (255) NOT NULL DEFAULT ' ', FPRODUCTEDTYPE NVARCHAR (80) NOT NULL DEFAULT ' ', FPRODUCTNO NVARCHAR (255) NOT NULL DEFAULT ' ', FBILLSEQ NVARCHAR (160) NOT NULL DEFAULT ' ', FSUMPRODUCTQTY DECIMAL (23, 10) NOT NULL DEFAULT 0, FSUMPRODUCTAMOUNT DECIMAL (23, 10) NOT NULL DEFAULT 0, FCOSTITEMID INT NOT NULL DEFAULT 0, FCOSTITEMID_FNUMBER VARCHAR (255) NOT NULL DEFAULT ' ', FCOSTITEMID_FNAME NVARCHAR (255) NOT NULL DEFAULT ' ', FQTYDIGITS INT NOT NULL DEFAULT 0, FAMOUNTDIGITS INT NOT NULL DEFAULT 0, FRATEDIGITS INT NOT NULL DEFAULT 0, FBEGINQTY DECIMAL (23, 10) NOT NULL DEFAULT 0, FBEGINAMOUNT DECIMAL (23, 10) NOT NULL DEFAULT 0, FSUMQUALIFIEDINQTY DECIMAL (23, 10) NOT NULL DEFAULT 0, FSUMQUALIFIEDINAMOUNT DECIMAL (23, 10) NOT NULL DEFAULT 0, FSUMDYSINQTY DECIMAL (23, 10) NOT NULL DEFAULT 0, FSUMDYSINAMOUNT DECIMAL (23, 10) NOT NULL DEFAULT 0, FSUMWASTEINQTY DECIMAL (23, 10) NOT NULL DEFAULT 0, FSUMWASTEINAMOUNT DECIMAL (23, 10) NOT NULL DEFAULT 0, FSUMCOMPLETEQTY DECIMAL (23, 10) NOT NULL DEFAULT 0, FSUMCOMPLETEAMOUNT DECIMAL (23, 10) NOT NULL DEFAULT 0, FENDQTY DECIMAL (23, 10) NOT NULL DEFAULT 0, FENDAMOUNT DECIMAL (23, 10) NOT NULL DEFAULT 0, FQUALIFIEDINRATE DECIMAL (23, 10) NOT NULL DEFAULT 0, FDYSINRATE DECIMAL (23, 10) NOT NULL DEFAULT 0, FWASTEINRTE DECIMAL (23, 10) NOT NULL DEFAULT 0)
exec sp_executesql N'INSERT INTO #TM_CB_ORDERCOSTTRACKRPT (FMATERIALID,FQTYDIGITS, FAMOUNTDIGITS, FRATEDIGITS, FOrderEntryId, FSumQualifiedInAmount, FSumDysInAmount, FSumWasteInAmount, FSumCompleteAmount, FBeginAmount, FEndAmount, FSumProductAmount, FOrderBillDate, FOrderBillNo, FOrderBillSeq, FPRODUCTID_FNUMBER, FPRODUCTID_FNAME, FSPECIFICATION, FBOMNUMBERFIELD_FNUMBER, FBATCHFIELD_FNAME, FAuxPropId, FMtoNo, FBASICUNITFIELD_FNAME, FBillQty, FNoProductQty, FOrderBillNoSeq, FSumQualifiedInQty, FSumDysInQty, FSumWasteInQty, FSumCompleteQty, FQualifiedInRate, FDysInRate, FWasteInRte, FBeginQty, FEndQty, FSumProductQty, FPRODUCTNO)
SELECT salentry.FMATERIALID, min(unit.FPrecision), @FAMOUNTDIGITS, 2, salentry.FENTRYID, sum(ISNULL(cost.FQUALIFIEDINAMOUNT, 0)), sum(ISNULL(cost.FDYSINAMOUNT, 0)), sum(ISNULL(cost.FWASTEINAMOUNT, 0)), sum(ISNULL((cost.FQUALIFIEDINAMOUNT + cost.FDYSINAMOUNT + cost.FWASTEINAMOUNT), 0)), sum(CASE WHEN (ISNULL(info.FACCTGID, 0) >= @FStartAcctgId1) THEN ISNULL(cost.FBEGINAMOUNT, 0) ELSE 0 END), sum(CASE WHEN (ISNULL(info.FACCTGID, 0) <= @FEndAcctgId1) THEN ISNULL(cost.FENDAMOUNT, 0) ELSE 0 END), sum(CASE WHEN (ISNULL(info.FACCTGID, 0) <= @FEndAcctgId2) THEN ISNULL(cost.FSUMCURRINPUTAMOUNT, 0) ELSE 0 END), min(sal.FDATE), min(sal.FBILLNO), min(salentry.FSEQ), min(mater.FNUMBER), min(ISNULL(mater_L.FNAME, '' '')), min(ISNULL(mater_L.FSPECIFICATION, '' '')), min(ISNULL(bom.FNUMBER, '' '')), min(ISNULL(lot.FNumber, '' '')), min(salentry.FAUXPROPID), min(salentry.FMTONO), min(ISNULL(unit_L.FNAME, '' '')), min(salentry.FBASEUNITQTY),
min(salentry.FBASEUNITQTY) - sum(CASE WHEN (ISNULL(info.FACCTGID, 0) <= @FEndAcctgId5) THEN ISNULL(cost.FSUMCURRINPUTQTY, 0) ELSE 0 END), '' '', sum(ISNULL(cost.FQUALIFIEDINQTY, 0)), sum(ISNULL(cost.FDYSINQTY, 0)), sum(ISNULL(cost.FWASTEINQTY, 0)), sum(ISNULL((cost.FQUALIFIEDINQTY + cost.FDYSINQTY +
cost.FWASTEINQTY), 0)), CASE WHEN sum(ISNULL((cost.FQUALIFIEDINQTY + cost.FDYSINQTY + cost.FWASTEINQTY), 0)) = 0 THEN 0 ELSE ROUND((sum(ISNULL(cost.FQUALIFIEDINQTY, 0)) / sum(ISNULL((cost.FQUALIFIEDINQTY + cost.FDYSINQTY + cost.FWASTEINQTY), 0))), 2) END, CASE WHEN sum(ISNULL((cost.FQUALIFIEDINQTY +
cost.FDYSINQTY + cost.FWASTEINQTY), 0)) = 0 THEN 0 ELSE ROUND((sum(ISNULL(cost.FDYSINQTY, 0)) / sum(ISNULL((cost.FQUALIFIEDINQTY + cost.FDYSINQTY + cost.FWASTEINQTY), 0))), 2) END, CASE WHEN sum(ISNULL((cost.FQUALIFIEDINQTY + cost.FDYSINQTY + cost.FWASTEINQTY), 0)) = 0 THEN 0 ELSE ROUND(((1 -
(sum(ISNULL(cost.FQUALIFIEDINQTY, 0)) / sum(ISNULL((cost.FQUALIFIEDINQTY + cost.FDYSINQTY + cost.FWASTEINQTY), 0)))) - (sum(ISNULL(cost.FDYSINQTY, 0)) / sum(ISNULL((cost.FQUALIFIEDINQTY + cost.FDYSINQTY + cost.FWASTEINQTY), 0)))), 2) END, sum(CASE WHEN (ISNULL(info.FACCTGID, 0) >= @FStartAcctgId2) THEN
ISNULL(cost.FBEGINQTY, 0) ELSE 0 END), sum(CASE WHEN (ISNULL(info.FACCTGID, 0) <= @FEndAcctgId3) THEN ISNULL(cost.FENDQTY, 0) ELSE 0 END), sum(CASE WHEN (ISNULL(info.FACCTGID, 0) <= @FEndAcctgId4) THEN ISNULL(cost.FSUMCURRINPUTQTY, 0) ELSE 0 END), N''小计''
FROM T_SAL_ORDER sal INNER JOIN T_SAL_ORDERENTRY salentry ON sal.FID = salentry.FID
INNER JOIN T_SAL_ORDERENTRY_F fin ON fin.FEntryId = salentry.FEntryId
INNER JOIN T_SAL_ORDERentry_E SOE_E ON (salentry.FENTRYID = SOE_E.FENTRYID AND (SOE_E.FROWTYPE <> ''Son''))
INNER JOIN T_BD_MATERIAL mater ON salentry.FMATERIALID = mater.FMATERIALID
INNER JOIN T_BD_UNIT unit ON salentry.FBaseUnitId = unit.FUNITID
LEFT OUTER JOIN (SELECT A.FACCTGID, A.FORDERBILLNO, A.FORDERBILLSEQ, A.FID, A.FPRODUCTDIMEID FROM V_CB_PROORDERINFO A
INNER JOIN T_HS_OUTACCTG B ON A.FACCTGID = B.FID WHERE (((B.FDIMENSIONID = @FSUMDIMENSIONID AND (B.FID >= @FSUMSTARTACCTGID)) AND (B.FID <= @FSUMENDACCTGID)) AND A.FENDINITKEY = ''1'')) info ON (sal.FBILLNO = info.FORDERBILLNO AND salentry.FSEQ = info.FORDERBILLSEQ) LEFT OUTER JOIN V_CB_COSTCALEXPENSE cost ON info.FID = cost.FID LEFT OUTER JOIN T_BD_MATERIAL_L mater_L ON (mater.FMATERIALID = mater_L.FMATERIALID AND mater_L.FLOCALEID = 2052) LEFT OUTER JOIN T_ENG_BOM bom ON salentry.FBOMID = bom.FID LEFT OUTER JOIN T_BD_LOTMASTER lot ON salentry.FLOT = lot.FLOTID LEFT OUTER JOIN T_BD_UNIT_L unit_L ON (unit.FUNITID = unit_L.FUNITID AND unit_L.FLOCALEID = 2052)
WHERE (
EXISTS (SELECT 1 FROM T_ORG_ACCOUNTSYSTEM acctSys INNER JOIN T_ORG_ACCTSYSENTRY acctOrg ON acctOrg.FACCTSYSTEMID = acctSys.FACCTSYSTEMID INNER JOIN T_ORG_ACCTSYSDETAIL acctSubOrg ON acctSubOrg.FEntryID = acctOrg.FEntryID WHERE ((fin.FSETTLEORGID = acctSubOrg.FSUBORGID AND acctSys.FACCTSYSTEMID = 1) AND acctOrg.FMAINORGID = 1))) GROUP BY salentry.FMATERIALID,salentry.FENTRYID',N'@FAMOUNTDIGITS int,@FStartAcctgId1 bigint,@FEndAcctgId1 bigint,@FEndAcctgId2 bigint,@FStartAcctgId2 bigint,@FEndAcctgId3 bigint,@FEndAcctgId4 bigint,@FEndAcctgId5 bigint,@FSUMDIMENSIONID bigint,@FSUMSTARTACCTGID bigint,@FSUMENDACCTGID bigint,@FStartDate datetime,@FEndDate datetime',@FAMOUNTDIGITS=2,@FStartAcctgId1=100010,@FEndAcctgId1=100011,@FEndAcctgId2=100011,@FStartAcctgId2=100010,@FEndAcctgId3=100011,@FEndAcctgId4=100011,@FEndAcctgId5=100011,@FSUMDIMENSIONID=1,@FSUMSTARTACCTGID=100010,@FSUMENDACCTGID=100011,@FStartDate='2019-09-01 00:00:00',@FEndDate='2019-10-31 00:00:00'
--SELECT * FROM #TM_CB_ORDERCOSTTRACKRPT WHERE FSUMPRODUCTAMOUNT <>0
INSERT INTO @t
SELECT FORDERBILLNO,FPRODUCTID_FNUMBER,FPRODUCTID_FNAME,FMATERIALID,FSUMPRODUCTQTY,FSUMPRODUCTAMOUNT,
ROUND(FSUMPRODUCTAMOUNT/FSUMPRODUCTQTY, 2) AS UNITCOST
FROM #TM_CB_ORDERCOSTTRACKRPT WHERE FSUMPRODUCTAMOUNT <>0
ORDER BY FORDERBILLNO
DROP TABLE #TM_CB_ORDERCOSTTRACKRPT
SELECT *FROM @t
end
EXEC proc_cb_lj