Advertisement
Guest User

UnitFinancialFigures

a guest
Sep 17th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 30.15 KB | None | 0 0
  1.  
  2.  
  3.     ALTER VIEW [dbo].[UnitFinancialFigures] AS
  4.  
  5.     WITH
  6.     ctePeriodWeekCount AS (
  7.         SELECT Count(Week.WeekNo) AS WeekCount, PrdNo AS Period, PrdYear AS Year
  8.         FROM Week
  9.         INNER JOIN Period on Week.WeekEndDate BETWEEN Period.StartDate AND Period.EndDate
  10.         GROUP BY PrdNo, PrdYear),
  11.  
  12.     cteLabourFigures AS (
  13.         SELECT Labour.LabUnitId, Labour.LabPeriod, Labour.LabYear,
  14.         SUM(CASE WHEN LabourTypes.LTypeIsPayrollLabour = 1 THEN LabourBreakdown.LBrkAmount ELSE 0 END) AS PayRollLabour,
  15.         SUM(CASE WHEN LabourTypes.LTypeIsPayrollLabour = 0 THEN LabourBreakdown.LBrkAmount ELSE 0 END) AS OtherLabour
  16.         FROM Labour
  17.         INNER JOIN LabourBreakdown on LabourBreakDown.LBrkLabId = Labour.LabId
  18.         INNER JOIN LabourTypes on LabourTypes.LTypeId = LabourBreakdown.LBrkLTypeId
  19.         GROUP BY Labour.LabUnitId, Labour.LabPeriod, Labour.LabYear),
  20.  
  21.     cteMobileRelief AS (
  22.         SELECT McDtUnitId, McHdPeriod, McHdYear, SUM(McDtAmount) AS MobileReliefTotal
  23.         FROM MobileCostHeader
  24.         INNER JOIN MobileCostDetail on MobileCostDetail.McDtHdId = MobileCostHeader.MCHdId
  25.         GROUP BY McDtUnitId, McHdPeriod, McHdYear),
  26.  
  27.     cteInvoices AS (
  28.         SELECT Period.PrdNo, Period.PrdYear, Invoice.AccountID, SUM(Invoice.InvTotalInclVat) AS TotalInvoices,
  29.         SUM(Invoice.InvVatAmount) AS TotalInvoicesVat
  30.         FROM [SOP].Invoice
  31.         INNER JOIN Period on Invoice.InvoiceDate BETWEEN Period.StartDate AND Period.EndDate
  32.         GROUP By Period.PrdNo, Period.PrdYear, Invoice.AccountID),
  33.  
  34.     cteSundryCosts AS (
  35.         SELECT ScDtAccountId, ScDtPeriodNo, ScDtYearNo, SUM(ScdtTotal) AS SundryTotal,
  36.         SUM(ScdtAmount) AS SundryNetTotal,
  37.         SUM(ScdtVatAmount) AS SundryVatAmount
  38.         FROM SundryCostDetail
  39.         WHERE ScdtRepeat = 0
  40.         GROUP BY ScDtAccountId, ScDtPeriodNo, ScDtYearNo),
  41.  
  42.     cteAdditionalProfit AS (
  43.         SELECT ScDtAccountId, ScDtPeriodNo, ScDtYearNo, SUM(ScdtTotal) AS AdditionalProfitTotal,
  44.         SUM(ScdtAmount) AS AdditionalProfitNetTotal,
  45.         SUM(ScdtVatAmount) AS AdditionalProfitVatAmount
  46.         FROM SundryCostDetail
  47.         WHERE ScdtRepeat = 0 AND SCdtExcludeFromGP = 0
  48.         GROUP BY ScDtAccountId, ScDtPeriodNo, ScDtYearNo),
  49.  
  50.     cteCashReceived AS (
  51.         SELECT CtUnitId, CtPeriod, CtYear,
  52.         SUM(CtAmount - CtAgentVatAmount) AS GoodsAmount, SUM(CtVatAmount + CtAgentVatAmount) AS VatTotal, SUM(CtTotal) AS Total,
  53.         SUM(VATFree.CanaAmount - VATFree.CanaAgentVatAmount) AS VATFreeGoodsAmount,
  54.         SUM(Catering.CanaAmount - Catering.CanaAgentVatAmount) AS CateringGoodsAmount,
  55.         SUM(Vending.CanaAmount - Vending.CanaAgentVatAmount) AS VendingGoodsAmount,
  56.         SUM(CansConf.CanaAmount - CansConf.CanaAgentVatAmount) AS CansConfGoodsAmount
  57.         FROM CashTran
  58.         LEFT JOIN CashAnalysis AS VATFree ON CashTran.CtId = VATFree.CanaTranId AND VATFree.CanaCode = 'G201'
  59.         LEFT JOIN CashAnalysis AS Catering ON CashTran.CtId = Catering.CanaTranId AND Catering.CanaCode = 'G205'
  60.         LEFT JOIN CashAnalysis AS Vending ON CashTran.CtId = Vending.CanaTranId AND Vending.CanaCode = 'G202'
  61.         LEFT JOIN CashAnalysis AS CansConf ON CashTran.CtId = CansConf.CanaTranId AND CansConf.CanaCode = 'G203'
  62.         WHERE CtCategoryId = 3 AND CtApproved = 1 ANd CtTranType = 'R'
  63.         GROUP BY CtUnitId, CtPeriod, CtYear),
  64.  
  65.     cteFreeIssues AS (
  66.         SELECT CtUnitId, CtPeriod, CtYear,
  67.         SUM(CtAmount - CtAgentVatAmount) AS GoodsAmount, SUM(CtVatAmount + CtAgentVatAmount) AS VatTotal, SUM(CtTotal) AS Total,
  68.         SUM(Catering.CanaAmount - Catering.CanaAgentVatAmount) AS CateringGoodsAmount,
  69.         SUM(Vending.CanaAmount - Vending.CanaAgentVatAmount) AS VendingGoodsAmount,
  70.         SUM(CansConf.CanaAmount - CansConf.CanaAgentVatAmount) AS CansConfGoodsAmount,
  71.         SUM(Labour.CanaAmount - Labour.CanaAgentVatAmount) AS LabourGoodsAmount
  72.         FROM CashTran
  73.         INNER JOIN [SOP].Accounts ON Accounts.AcUniqueId = CtUnitId
  74.         LEFT JOIN CashAnalysis AS Catering ON CashTran.CtId = Catering.CanaTranId AND Catering.CanaCode = 'G204'
  75.         LEFT JOIN CashAnalysis AS Vending ON CashTran.CtId = Vending.CanaTranId AND Vending.CanaCode = 'G202'
  76.         LEFT JOIN CashAnalysis AS CansConf ON CashTran.CtId = CansConf.CanaTranId AND CansConf.CanaCode = 'G203'
  77.         LEFT JOIN CashAnalysis AS Labour ON CashTran.CtId = Labour.CanaTranId AND Labour.CanaCode = 'H995'
  78.         WHERE CtCategoryId = 2
  79.         GROUP BY CtUnitId, CtPeriod, CtYear),
  80.  
  81.     cteExternalFreeIssues AS (
  82.         SELECT CtUnitId, CtPeriod, CtYear, SUM(CtAmount - CtAgentVatAmount) AS GoodsAmount, SUM(CtVatAmount + CtAgentVatAmount) AS VatTotal, SUM(CtTotal) AS Total
  83.         FROM CashTran
  84.         INNER JOIN [SOP].Accounts on Accounts.AcUniqueId = CtUnitId
  85.         WHERE CtCategoryId = 4
  86.         GROUP BY CtUnitId, CtPeriod, CtYear),
  87.  
  88.     cteAgencyLabour AS (
  89.         SELECT PtUnitId, PtYear, PtPeriod, SUM(CASE WHEN PtTranType = 'I' THEN PanaGoodsAmount - PanaAgentVatAmount ELSE -(PanaGoodsAmount - PanaAgentVatAmount) END) Total
  90.         FROM PurchaseTran
  91.         INNER Join PurchaseAnalysis on PurchaseAnalysis.PanaTranId = PurchaseTran.PtId
  92.         INNER JOIN [SOP].Accounts on Accounts.AcUniqueID = PtUnitId
  93.         INNER JOIN UnitAnalysisConfig on UnitAnalysisConfig.UACfgAnalysisCode = PurchaseAnalysis.PanaPCode AND UnitAnalysisConfig.UACfgType = 1
  94.         WHERE PurchaseAnalysis.PanaPCode = 'H995' AND (PtTranType = 'I' Or PtTranType = 'C') AND PtApproved =1
  95.         GROUP BY PtUnitId, PtPeriod, PtYear),
  96.  
  97.     ctePurchases AS (
  98.         SELECT PurchaseTran.PtUnitId, PurchaseTran.PtPeriod, PurchaseTran.PtYear,
  99.         SUM(CASE WHEN PtTranType = 'I' THEN PtTotal WHEN PtTranType = 'C' THEN - PtTotal ELSE 0 END) AS PurchaseTotal,
  100.         SUM(CASE WHEN PtTranType = 'I' THEN PtAmount - PtAgentVatAmount WHEN PtTranType = 'C' THEN -(PtAmount - PtAgentVatAmount) ELSE 0 END) AS PurchaseNetTotal,
  101.         SUM(CASE WHEN PtTranType = 'I' THEN PtVat + PtAgentVatAmount WHEN PtTranType = 'C' THEN -(PtVat + PtAgentVatAmount) ELSE 0 END) AS VatTotal,
  102.         SUM(CASE WHEN PtSupplierId = CmpCashAccount THEN PtTotal ELSE 0 END) AS TotalCashPaid,
  103.         SUM(CASE WHEN PtTranType = 'I' THEN Catering.PanaGoodsAmount - Catering.PanaAgentVatAmount WHEN PtTranType = 'C' THEN -(Catering.PanaGoodsAmount - Catering.PanaAgentVatAmount) ELSE 0 END) CateringTotal,
  104.         SUM(CASE WHEN PtTranType = 'I' THEN Catering.PanaAgentVatAmount + Catering.PanaVatAmount WHEN PtTranType = 'C' THEN -(Catering.PanaAgentVatAmount + Catering.PanaVatAmount) ELSE 0 END) AS CateringVatAmount,
  105.         SUM(CASE WHEN PtTranType = 'I' THEN Vending.PanaGoodsAmount - Vending.PanaAgentVatAmount WHEN PtTranType = 'C' THEN -(Vending.PanaGoodsAmount - Vending.PanaAgentVatAmount) ELSE 0 END) VendingTotal,
  106.         SUM(CASE WHEN PtTranType = 'I' THEN Vending.PanaAgentVatAmount + Vending.PanaVatAmount WHEN PtTranType = 'C' THEN -(Vending.PanaAgentVatAmount + Vending.PanaVatAmount) ELSE 0 END) AS VendingVatAmount,
  107.         SUM(CASE WHEN PtTranType = 'I' THEN CansConf.PanaGoodsAmount - CansConf.PanaAgentVatAmount WHEN PtTranType = 'C' THEN -(CansConf.PanaGoodsAmount - CansConf.PanaAgentVatAmount) ELSE 0 END) CansConfTotal,
  108.         SUM(CASE WHEN PtTranType = 'I' THEN CansConf.PanaAgentVatAmount + CansConf.PanaVatAmount WHEN PtTranType = 'C' THEN -(CansConf.PanaAgentVatAmount + CansConf.PanaVatAmount) ELSE 0 END) AS CansConfVatAmount
  109.         FROM PurchaseTran
  110.         INNER JOIN CompParam on Compparam.CmpDefault = 1
  111.         INNER JOIN [SOP].Accounts on Accounts.AcUniqueID = PurchaseTran.PtUnitId
  112.         LEFT JOIN PurchaseAnalysis AS Catering on Catering.PanaTranId = PurchaseTran.PtId AND (Catering.PanaPCode = 'H201' OR Catering.PanaPCode = 'H401')
  113.         LEFT JOIN PurchaseAnalysis AS Vending on Vending.PanaTranId = PurchaseTran.PtId AND Vending.PanaPCode = 'H202'
  114.         LEFT JOIN PurchaseAnalysis AS CansConf on CansConf.PanaTranId = PurchaseTran.PtId AND CansConf.PanaPCode = 'H211'
  115.         WHERE (PtTranType = 'I' OR PtTranType = 'C') AND PtApproved = 1
  116.         GROUP BY PtUnitId, PtPeriod, PtYear),
  117.  
  118.     cteSundryPurchases AS (
  119.         SELECT PtUnitId, PtYear, PtPeriod,
  120.         SUM(CASE WHEN PtTranType = 'I' THEN PanaGoodsAmount - PanaAgentVatAmount ELSE -(PanaGoodsAmount - PanaAgentVatAmount) END) Total,
  121.         SUM(CASE WHEN PtTranType = 'I' THEN PanaAgentVatAmount + PanaVatAmount WHEN PtTranType = 'C' THEN -(PanaAgentVatAmount + PanaVatAmount) ELSE 0 END) AS VatAmount
  122.         FROM PurchaseTran
  123.         INNER Join PurchaseAnalysis on PurchaseAnalysis.PanaTranId = PurchaseTran.PtId
  124.         INNER JOIN [SOP].Accounts on Accounts.AcUniqueID = PurchaseTran.PtUnitId
  125.         INNER JOIN UnitAnalysisConfig on UnitAnalysisConfig.UACfgAnalysisCode = PurchaseAnalysis.PanaPCode AND (UnitAnalysisConfig.UACfgType = 1 OR UnitAnalysisConfig.UACfgType = 3)
  126.         WHere UnitAnalysisConfig.UACfgTandOLabel = 'Sundry' AND (PtTranType = 'I' Or PtTranType = 'C') AND PtApproved = 1
  127.         GROUP BY PtUnitId, PtPeriod, PtYear),
  128.  
  129.     cteDiscount AS (
  130.         SELECT PurchaseTran.PtUnitId, SUM(CASE WHEN PtTRanType = 'I' THEN PtDiscountAmount ELSE -PtDiscountAmount END) AS DiscountTotal, PtPeriod, PtYear
  131.         FROM PurchaseTran
  132.         WHERE PurchaseTran.PtApproved = 1 And (PurchaseTran.PtTranType = 'I' OR PurchaseTran.PtTranType = 'C')
  133.         GROUP BY PurchaseTran.PtUnitId, PtPeriod, PtYear),
  134.  
  135.     cteUnitPeriodOpeningStock AS (
  136.         SELECT UStkId, UStkOpening, UnitStock.UStkUnitId, UnitStock.UStkPeriod, UnitStock.UStkYearNo, SUM(OpeningCatering.USAnaAmount) AS OpeningCateringStock, SUM(OpeningBeverage.USAnaAmount) AS OpeningBeverageStock, SUM(OpeningSundry.USAnaAmount) AS OpeningSundryStock, SUM(OpeningCansConf.USAnaAmount) AS OpeningCansConfStock
  137.         FROM UnitStock
  138.         LEFT JOIN UnitStockAnalysis AS OpeningCatering ON UnitStock.UStkId = OpeningCatering.USAnaTranId AND OpeningCatering.USanaType = 'O' AND OpeningCatering.USAnaCode = 'CAT001'
  139.         LEFT JOIN UnitStockAnalysis AS OpeningBeverage ON UnitStock.UStkId = OpeningBeverage.USAnaTranId AND OpeningBeverage.USanaType = 'O' AND OpeningBeverage.USAnaCode = 'BEV001'
  140.         LEFT JOIN UnitStockAnalysis AS OpeningSundry ON UnitStock.UStkId = OpeningSundry.USAnaTranId AND OpeningSundry.USanaType = 'O' AND OpeningSundry.USAnaCode = 'SUN001'
  141.         LEFT JOIN UnitStockAnalysis AS OpeningCansConf ON UnitStock.UStkId = OpeningCansConf.USAnaTranId AND OpeningCansConf.USanaType = 'O' AND OpeningCansConf.USAnaCode = 'CAN001'
  142.         INNER JOIN Week on Week.WeekNo = UnitStock.UStkWeeKNo AND Week.WeekYear = UnitStock.UStkYearNo
  143.         INNER JOIN (
  144.             SELECT UStkUnitId, UStkPeriod, MIN(Week.WeekEndDate) OpeningDate
  145.             FROM UnitStock
  146.             INNER JOIN Week on Week.WeekNo = UnitStock.UStkWeeKNo AND Week.WeekYear = UnitStock.UStkYearNo
  147.             GROUP BY UStkUnitId, UStkPeriod) OpeningData on OpeningData.OpeningDate = Week.WeekEndDate AND OpeningData.UStkUnitId = UnitStock.UStkUnitId
  148.         GROUP BY UStkId, UStkOpening, UnitStock.UStkUnitId, UnitStock.UStkPeriod, UnitStock.UStkYearNo),
  149.  
  150.     cteUnitPeriodClosingStock AS (
  151.         SELECT UStkId, UStkClosing, UnitStock.UStkUnitId, UnitStock.UStkPeriod, UnitStock.UStkYearNo, SUM(ClosingCatering.USAnaAmount) AS ClosingCateringStock, SUM(ClosingBeverage.USAnaAmount) AS ClosingBeverageStock, SUM(ClosingSundry.USAnaAmount) AS ClosingSundryStock, SUM(ClosingCansConf.USAnaAmount) AS ClosingCansConfStock
  152.         FROM UnitStock
  153.         LEFT JOIN UnitStockAnalysis AS ClosingCatering ON UnitStock.UStkId = ClosingCatering.USAnaTranId AND ClosingCatering.USanaType = 'C' AND ClosingCatering.USAnaCode = 'CAT001'
  154.         LEFT JOIN UnitStockAnalysis AS ClosingBeverage ON UnitStock.UStkId = ClosingBeverage.USAnaTranId AND ClosingBeverage.USanaType = 'C' AND ClosingBeverage.USAnaCode = 'BEV001'
  155.         LEFT JOIN UnitStockAnalysis AS ClosingSundry ON UnitStock.UStkId = ClosingSundry.USAnaTranId AND ClosingSundry.USanaType = 'C' AND ClosingSundry.USAnaCode = 'SUN001'
  156.         LEFT JOIN UnitStockAnalysis AS ClosingCansConf ON UnitStock.UStkId = ClosingCansConf.USAnaTranId AND ClosingCansConf.USanaType = 'C' AND ClosingCansConf.USAnaCode = 'CAN001'
  157.         INNER JOIN Week on Week.WeekNo = UnitStock.UStkWeeKNo AND Week.WeekYear = UnitStock.UStkYearNo
  158.         INNER JOIN (
  159.             SELECT UStkUnitId, UStkPeriod, MAX(Week.WeekEndDate) ClosingDate
  160.             FROM UnitStock
  161.             INNER JOIN Week on Week.WeekNo = UnitStock.UStkWeeKNo AND Week.WeekYear = UnitStock.UStkYearNo
  162.             GROUP BY UStkUnitId, UStkPeriod) ClosingData on ClosingData.ClosingDate = Week.WeekEndDate AND ClosingData.UStkUnitId = UnitStock.UStkUnitId
  163.         GROUP BY UStkId, UStkClosing, UnitStock.UStkUnitId, UnitStock.UStkPeriod, UnitStock.UStkYearNo)
  164.            
  165.     SELECT
  166.     ASubAccountId,
  167.     AcRef,
  168.     AcName,
  169.     Period.PrdNo,
  170.     Period.PrdYear,
  171.     Period.StartDate,
  172.     Period.EndDate,
  173.     CASE WHEN ASubSubmittedBy IS NULL THEN 0 ELSE 1 END AS Submitted,
  174.     ISNULL(cteDiscount.DiscountTotal ,0) AS DiscountTotal,
  175.     ISNULL(cteLabourFigures.PayRollLabour,0) AS PayrollLabour,
  176.     ISNULL(cteLabourFigures.OtherLabour, 0)  AS OtherLabour,
  177.     ((AcUnitTrainingLevy / 100) * ISNULL(cteLabourFigures.PayRollLabour,0)) AS TotalTrainingLevy,
  178.     ((CmpApprenticeshipLevyPercent / 100) * ISNULL(cteLabourFigures.PayRollLabour,0)) AS ApprenticeshipLevy,
  179.     ISNULL(cteMobileRelief.MobileReliefTotal,0) AS MobileReliefTotal,
  180.     ISNULL(cteAgencyLabour.Total, 0) AS AgencyLabour,
  181.     ISNULL(ctePurchases.PurchaseTotal,0) AS PurchaseTotal,
  182.     ISNULL(cteInvoices.TotalInvoices,0) AS TotalInvoices,
  183.     ISNULL(cteSundryCosts.SundryTotal,0) AS SundryTotal,
  184.     ISNULL(cteCashReceived.Total,0) AS CashReceivedTotal,
  185.     ISNULL(cteCashReceived.VatTotal,0) AS CashReceivedVAT,
  186.     ISNULL(ctePurchases.TotalCashPaid, 0) AS TotalCashPaid,
  187.     ISNULL(cteCashReceived.GoodsAmount,0) AS CashReceivedNetTotal,
  188.     ISNULL(cteInvoices.TotalInvoicesVat,0) AS TotalInvoicesVAT,
  189.     ISNULL(cteUnitPeriodOpeningStock.UStkOpening,0) AS OpeningStock,
  190.     ISNULL(cteUnitPeriodClosingStock.UStkClosing,0) AS ClosingStock,
  191.     ISNULL(cteUnitPeriodOpeningStock.UStkOpening,0) - ISNULL(cteUnitPeriodClosingStock.UStkClosing,0) AS StockBalance,
  192.     ISNULL(cteFreeIssues.GoodsAmount, 0) AS FreeIssuesTotal,
  193.     ISNULL(cteFreeIssues.GoodsAmount, 0) + ISNULL(cteCashReceived.Total,0) AS TotalSales,
  194.     ISNULL(cteFreeIssues.GoodsAmount, 0) + ISNULL(cteCashReceived.GoodsAmount,0) AS TotalNetSales,
  195.     ISNULL(ctePurchases.PurchaseNetTotal,0) + (ISNULL(cteUnitPeriodOpeningStock.UStkOpening,0) - ISNULL(cteUnitPeriodClosingStock.UStkClosing,0)) AS Consumption,
  196.     (ISNULL(cteFreeIssues.GoodsAmount, 0) + ISNULL(cteCashReceived.GoodsAmount,0)) - (ISNULL(ctePurchases.PurchaseNetTotal,0) + (ISNULL(cteUnitPeriodOpeningStock.UStkOpening,0) - ISNULL(cteUnitPeriodClosingStock.UStkClosing,0))) AS Profit,
  197.     ISNULL(cteLabourFigures.PayRollLabour, 0) + ISNULL(cteLabourFigures.OtherLabour, 0) + ISNULL(cteMobileRelief.MobileReliefTotal,0) + ((AcUnitTrainingLevy / 100) * ISNULL(cteLabourFigures.PayRollLabour,0)) + ((CmpApprenticeshipLevyPercent / 100) * ISNULL(cteLabourFigures.PayRollLabour,0)) AS TotalLabour,
  198.     ISNULL(UnitLabourFigures.TotalBacksheetLabour,0) AS TotalBacksheetLabour,
  199.     ISNULL(ctePurchases.CateringTotal,0) AS CateringPurchases, ISNULL(ctePurchases.VendingTotal,0) AS VendingPurchases,
  200.     ISNULL(cteSundryPurchases.Total, 0) AS SundryInvoicesOnly,
  201.     ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) AS SundryPurchases,
  202.     ISNULL(ctePurchases.CansConfTotal, 0) AS CansCOnfPurchases,
  203.     ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) + ISNULL(ctePurchases.CansConfTotal, 0) - ISNULL(cteExternalFreeIssues.GoodsAmount,0) AS TandONetPurchases,
  204.     ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) + ISNULL(ctePurchases.CansConfTotal, 0) AS GPPurchases,
  205.     ISNULL(ctePurchases.CateringVatAmount,0) + ISNULL(ctePurchases.VendingVatAmount,0) + ISNULL(cteSundryPurchases.VatAmount, 0) + ISNULL(cteSundryCosts.SundryVatAmount,0) + ISNULL(ctePurchases.CansConfVatAmount, 0) AS TandOVatPurchases,
  206.     ISNULL(ctePurchases.VatTotal,0) + ISNULL(cteSundryCosts.SundryVatAmount,0) AS PurchasesVat,
  207.     ((ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) + ISNULL(ctePurchases.CansConfTotal, 0)) + ISNULL(ctePurchases.VatTotal,0) + ISNULL(cteSundryCosts.SundryVatAmount,0)) AS GrossPurchases,
  208.     (ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) + ISNULL(ctePurchases.CansConfTotal, 0)  - ISNULL(cteExternalFreeIssues.GoodsAmount,0)) + (ISNULL(cteUnitPeriodOpeningStock.UStkOpening,0) - ISNULL(cteUnitPeriodClosingStock.UStkClosing,0) ) AS TandOConsumption,
  209.     (ISNULL(cteFreeIssues.GoodsAmount, 0) + ISNULL(cteCashReceived.GoodsAmount,0)) - ((ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) + ISNULL(ctePurchases.CansConfTotal, 0)) + (ISNULL(cteUnitPeriodOpeningStock.UStkOpening,0) - ISNULL(cteUnitPeriodClosingStock.UStkClosing,0) )) AS TandONetProfit,
  210.    
  211.     CASE WHEN Accounts.AcUnitTradingType = 'A' THEN ISNULL(cteCashReceived.Total,0) - ((ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) + ISNULL(ctePurchases.CansConfTotal, 0) - ISNULL(cteExternalFreeIssues.GoodsAmount,0)) + ISNULL(ctePurchases.VatTotal,0) + ISNULL(cteSundryCosts.SundryVatAmount,0))
  212.     ELSE (ISNULL(cteFreeIssues.GoodsAmount, 0) + ISNULL(cteCashReceived.GoodsAmount,0)) - ((ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) + ISNULL(ctePurchases.CansConfTotal, 0) - ISNULL(cteExternalFreeIssues.GoodsAmount,0)) + (ISNULL(cteUnitPeriodOpeningStock.UStkOpening,0) - ISNULL(cteUnitPeriodClosingStock.UStkClosing,0) )) END
  213.     AS TandOProfit,
  214.  
  215.     CASE WHEN Accounts.AcUnitTradingType = 'A' THEN ISNULL(UnitLabourFigures.TotalBacksheetLabour,0) -  ((ISNULL(cteFreeIssues.GoodsAmount, 0) + ISNULL(cteCashReceived.GoodsAmount,0)) - ((ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) + ISNULL(ctePurchases.CansConfTotal, 0)) + (ISNULL(cteUnitPeriodOpeningStock.UStkOpening,0) - ISNULL(cteUnitPeriodClosingStock.UStkClosing,0) ))) + ISNULL(Accounts.AcUnitSoftwareCharge,0)
  216.     ELSE ISNULL(UnitLabourFigures.TotalBacksheetLabour,0) - ((ISNULL(cteFreeIssues.GoodsAmount, 0) + ISNULL(cteCashReceived.GoodsAmount,0)) - ((ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) + ISNULL(ctePurchases.CansConfTotal, 0) - ISNULL(cteExternalFreeIssues.GoodsAmount,0)) + (ISNULL(cteUnitPeriodOpeningStock.UStkOpening,0) - ISNULL(cteUnitPeriodClosingStock.UStkClosing,0) ))) + ISNULL(Accounts.AcUnitSoftwareCharge,0) END
  217.     AS OperatingCost,
  218.  
  219.     CASE WHEN Accounts.AcUnitTradingType = 'A' THEN (((ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) + ISNULL(ctePurchases.CansConfTotal, 0)) + ISNULL(ctePurchases.VatTotal,0) + ISNULL(cteSundryCosts.SundryVatAmount,0))) - ISNULL(cteExternalFreeIssues.GoodsAmount,0) + ISNULL(UnitLabourFigures.TotalBacksheetLabour,0) - ((ISNULL(cteCashReceived.Total,0))) + ISNULL(Accounts.AcUnitSoftwareCharge,0) + (ISNULL(Accounts.AcUnitSoftwareCharge,0) * dbo.GetStandardVATRate())
  220.     WHEN Accounts.AcUnitTradingType = 'B' THEN (((ISNULL(cteCashReceived.GoodsAmount,0)) - ((ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) + ISNULL(ctePurchases.CansConfTotal, 0)))) + ISNULL(UnitLabourFigures.TotalBacksheetLabour,0))
  221.     WHEN Accounts.AcUnitTradingType = 'C' THEN ISNULL(UnitLabourFigures.TotalBacksheetLabour,0) - ((ISNULL(cteFreeIssues.GoodsAmount, 0) + ISNULL(cteCashReceived.GoodsAmount,0)) - ((ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) + ISNULL(ctePurchases.CansConfTotal, 0)) + (ISNULL(cteUnitPeriodOpeningStock.UStkOpening,0) - ISNULL(cteUnitPeriodClosingStock.UStkClosing,0) ))) + ISNULL(Accounts.AcUnitSoftwareCharge,0)
  222.     WHEN Accounts.AcUnitTradingType = 'D' THEN ((ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteSundryPurchases.Total, 0) + ISNULL(cteSundryCosts.SundryNetTotal,0) + ISNULL(ctePurchases.CansConfTotal, 0)) - ISNULL(cteCashReceived.GoodsAmount,0)) + ISNULL(UnitLabourFigures.TotalBacksheetLabour,0) + ISNULL(Accounts.AcUnitSoftwareCharge,0)
  223.     ELSE 0 END AS InvoiceCharge,
  224.                
  225.     --Fixed GP Shortfall Calculation
  226.     CASE WHEN Accounts.AcUnitIsFixedGP = 1 THEN
  227.         CASE WHEN Accounts.AcUnitUseOverallFixedGP = 1 THEN --Overall Fixed GP
  228.  
  229.             (ISNULL(cteCashReceived.GoodsAmount,0) * (ISNULL(Accounts.AcUnitOverallFixedGPValue,100) / 100)) --Fixed GP
  230.             -
  231.             (
  232.                 ISNULL(cteCashReceived.GoodsAmount,0) --Cash Sales Net
  233.                 -
  234.                 (
  235.                     (
  236.                         ISNULL(ctePurchases.CateringTotal,0) + ISNULL(ctePurchases.VendingTotal,0) + ISNULL(ctePurchases.CansConfTotal,0)
  237.                         + ISNULL(cteUnitPeriodOpeningStock.OpeningCateringStock,0) + ISNULL(cteUnitPeriodOpeningStock.OpeningBeverageStock,0) + ISNULL(cteUnitPeriodOpeningStock.OpeningCansConfStock,0)
  238.                         - ISNULL(cteUnitPeriodClosingStock.ClosingCateringStock,0) - ISNULL(cteUnitPeriodClosingStock.ClosingBeverageStock,0) - ISNULL(cteUnitPeriodClosingStock.ClosingCansConfStock,0)
  239.                     ) --Consumption No Sundries
  240.                     -
  241.                     (ISNULL(cteFreeIssues.GoodsAmount,0) - ISNULL(cteFreeIssues.LabourGoodsAmount,0)) --Free Issues Less Labour
  242.                 ) --ActualConsumption
  243.             ) --Actual GP
  244.  
  245.         ELSE --Catering/Vending/CansConf Fixed GP
  246.  
  247.             --Catering
  248.             CASE WHEN Accounts.AcUnitCateringFixedGPValue IS NOT NULL AND Accounts.AcUnitCateringFixedGPValue > 0 THEN
  249.                 (
  250.                     (ISNULL(cteCashReceived.CateringGoodsAmount,0) + ISNULL(cteCashReceived.VATFreeGoodsAmount,0)) --Catering cash sales plus zero vat cash sales
  251.                     *
  252.                     (ISNULL(Accounts.AcUnitCateringFixedGPValue,100) / 100) --Catering fixed GP %
  253.                 ) --Fixed GP
  254.                 -
  255.                 (
  256.                     (ISNULL(cteCashReceived.CateringGoodsAmount,0) + ISNULL(cteCashReceived.VATFreeGoodsAmount,0))--Catering cash sales plus zero vat cash sales
  257.                     -
  258.                     (
  259.                         ISNULL(ctePurchases.CateringTotal,0) + ISNULL(cteUnitPeriodOpeningStock.OpeningCateringStock,0) - ISNULL(cteUnitPeriodClosingStock.ClosingCateringStock,0) --Catering consumption
  260.                         -
  261.                         ISNULL(cteFreeIssues.CateringGoodsAmount,0) --Catering free issues
  262.                     ) --Actual catering consumption
  263.                 ) --Actual GP
  264.  
  265.             ELSE 0 END
  266.             +
  267.             --Vending
  268.             CASE WHEN Accounts.AcUnitVendingFixedGPValue IS NOT NULL AND Accounts.AcUnitVendingFixedGPValue > 0 THEN
  269.                 (
  270.                     ISNULL(cteCashReceived.VendingGoodsAmount,0) --Vending cash sales
  271.                     *
  272.                     (ISNULL(Accounts.AcUnitVendingFixedGPValue,100) / 100) --Vending fixed GP %
  273.                 ) --Fixed GP
  274.                 -
  275.                 (
  276.                     ISNULL(cteCashReceived.VendingGoodsAmount,0) --Vending cash sales
  277.                     -
  278.                     (
  279.                         ISNULL(ctePurchases.VendingTotal,0) + ISNULL(cteUnitPeriodOpeningStock.OpeningBeverageStock,0) - ISNULL(cteUnitPeriodClosingStock.ClosingBeverageStock,0) --Vending consumption
  280.                         -
  281.                         ISNULL(cteFreeIssues.VendingGoodsAmount,0) --Vending free issues
  282.                     ) --Actual Vending consumption
  283.                 ) --Actual GP
  284.             ELSE 0 END
  285.             +
  286.             --CansConf
  287.             CASE WHEN Accounts.AcUnitCansConfFixedGPValue IS NOT NULL AND Accounts.AcUnitCansConfFixedGPValue > 0 THEN
  288.                 (
  289.                     ISNULL(cteCashReceived.CansConfGoodsAmount,0) --CansConf cash sales
  290.                     *
  291.                     (ISNULL(Accounts.AcUnitCansConfFixedGPValue,100) / 100) --CansConf fixed GP %
  292.                 ) --Fixed GP
  293.                 -
  294.                 (
  295.                     ISNULL(cteCashReceived.CansConfGoodsAmount,0) --CansConf cash sales
  296.                     -
  297.                     (
  298.                         ISNULL(ctePurchases.CansConfTotal,0) + ISNULL(cteUnitPeriodOpeningStock.OpeningCansConfStock,0) - ISNULL(cteUnitPeriodClosingStock.ClosingCansConfStock,0) --CansConf consumption
  299.                         -
  300.                         ISNULL(cteFreeIssues.CansConfGoodsAmount,0) --CansConf free issues
  301.                     ) --Actual CansConf consumption
  302.                 ) --Actual GP
  303.             ELSE 0 END
  304.         END
  305.     ELSE 0 END AS FixedGPShortfall,
  306.  
  307.     ISNULL(cteAdditionalProfit.AdditionalProfitNetTotal,0) AS AdditionalProfitNetTotal, ISNULL(cteExternalFreeIssues.GoodsAmount, 0) AS ExternalFreeIssue,
  308.     CASE WHEN Accounts.AcUnitTradingType = 'B' THEN (CASE WHEN AcUnitNextAnnualFeeDate IS NULL THEN AcUnitAnnualFee WHEN AcUnitNextAnnualFeeDate > Period.EndDate THEN AcUnitAnnualFee ELSE AcUnitNextAnnualFee END) / 12
  309.     ELSE
  310.         CASE WHEN AcUnitManagementFeePeriod = 'W' THEN ((CASE WHEN AcUnitNextManagementFeeDate IS NULL THEN AcUnitManagementFee WHEN AcUnitNextManagementFeeDate > Period.EndDate THEN AcUnitManagementFee ELSE AcUnitNextManagementFee END) / 52) * WeekCount
  311.         ELSE ((CASE WHEN AcUnitNextManagementFeeDate IS NULL THEN AcUnitManagementFee WHEN AcUnitNextManagementFeeDate > Period.EndDate THEN AcUnitManagementFee ELSE AcUnitNextManagementFee END) / 12)
  312.         END
  313.     END AS MonthlyFees,
  314.  
  315.     AcUnitStockTarget,
  316.     AcUnitCashPaidTarget,
  317.     AcUnitCashHeldTarget,
  318.     AcUnitAgencyLabourTarget,
  319.     AcUnitAgencyDebtorsTarget,
  320.     WeekCount,
  321.     ISNULL(SUM(ASubCashCarriedForward),0) / WeekCount AS AvgCashCarriedForwrd,
  322.     ISNULL(TotalCashPaid,0) / WeekCount AS AvgCashPaid,
  323.     Accounts.AcProject AS AreaManager,
  324.     AcUnitSoftwareCharge
  325.     FROM AccountDataSubmission  
  326.     INNER JOIN [SOP].Accounts on Accounts.AcUniqueId = AccountDataSubmission.ASubAccountID
  327.     INNER JOIN Week on Week.WeekNo = AccountDataSubmission.ASubWeekNo AND Week.WeekYear = AccountDataSubmission.ASubYearNo
  328.     INNER JOIN Period on WEEK.WeekEndDate BETWEEN Period.StartDate AND Period.EndDate
  329.     INNER JOIN CompParam on CompParam.CmpDefault = 1   
  330.     LEFT OUTER JOIN ctePeriodWeekCount ON ctePeriodWeekCount.Period = PrdNo AND ctePeriodWeekCount.Year = PrdYear
  331.     LEFT OUTER JOIN cteDiscount on cteDiscount.PtUnitId = Accounts.AcUniqueID AND cteDiscount.PtPeriod = Period.PrdNo AND cteDiscount.PtYear = Period.PrdYear
  332.     LEFT OUTER JOIN cteLabourFigures on cteLabourFigures.LabPeriod = Period.PrdNo AND cteLabourFigures.LabYear = Period.PrdYear AND ASubAccountId = LabUnitId
  333.     LEFT OUTER JOIN cteMobileRelief on cteMobileRelief.McDtUnitId = ASubAccountId AND cteMobileRelief.McHdPeriod = Period.PrdNo AND cteMobileRelief.McHdYear = Period.PrdYear
  334.     LEFT OUTER JOIN ctePurchases on ctePurchases.PtUnitId = AcUniqueID AND ctePurchases.PtPeriod = PrdNo AND ctePurchases.PtYear = Period.PrdYear
  335.     LEFT OUTER JOIN cteSundryPurchases on cteSundryPurchases.PtUnitId = AcUniqueID AND cteSundryPurchases.PtPeriod = PrdNo AND cteSundryPurchases.PtYear = Period.PrdYear
  336.     LEFT OUTER JOIN cteInvoices on cteInvoices.AccountID = ASuBAccountId AND cteInvoices.PrdNo = Period.PrdNo AND Period.PrdYear = cteInvoices.PrdYear
  337.     LEFT OUTER JOIN cteSundryCosts on cteSundryCosts.ScDtAccountId = ASubAccountId AND cteSundryCosts.ScDTPeriodNo = Period.PrdNo AND cteSundryCosts.ScDtYearNo = Period.PrdYear
  338.     LEFT OUTER JOIN cteCashReceived on cteCashReceived.CtUnitId = ASubAccountId AND cteCashReceived.CtPeriod = Period.PrdNo AND cteCashReceived.CtYear = Period.PrdYear
  339.     LEFT OUTER JOIN cteFreeIssues on cteFreeIssues.CtUnitId = ASubAccountId AND cteFreeIssues.CtPeriod = Period.PrdNo AND cteFreeIssues.CtYear = Period.PrdYear
  340.     LEFT OUTER JOIN cteExternalFreeIssues on cteExternalFreeIssues.CtUnitId = ASubAccountId AND cteExternalFreeIssues.CtPeriod = Period.PrdNo AND cteExternalFreeIssues.CtYear = Period.PrdYear
  341.     LEFT OUTER JOIN cteAgencyLabour on cteAgencyLabour.PtUnitId = ASubAccountId AND cteAgencyLabour.PtPeriod = Period.PrdNo AND cteAgencyLabour.PtYear = Period.PrdYear
  342.     LEFT OUTER JOIN cteAdditionalProfit on cteAdditionalProfit.ScDtAccountId = ASubAccountId AND cteAdditionalProfit.ScDTPeriodNo = Period.PrdNo AND cteAdditionalProfit.ScDtYearNo = Period.PrdYear
  343.     LEFT OUTER JOIN cteUnitPeriodOpeningStock on cteUnitPeriodOpeningStock.UStkUnitId = ASubAccountId AND cteUnitPeriodOpeningStock.UStkPeriod = Period.PrdNo AND cteUnitPeriodOpeningStock.UStkYearNo = Period.PrdYear
  344.     LEFT OUTER JOIN cteUnitPeriodClosingStock on cteUnitPeriodClosingStock.UStkUnitId = ASubAccountId AND cteUnitPeriodClosingStock.UStkPeriod = Period.PrdNo AND cteUnitPeriodClosingStock.UStkYearNo = Period.PrdYear
  345.     LEFT OUTER JOIN UnitLabourFigures on AcUniqueID = UnitId AND Period.PrdNo = UnitLabourFigures.Period AND Period.PrdYear = UnitLabourFigures.Year
  346.     GROUP BY AccountDataSubmission.ASubAccountId, AcRef, AcName, Period.PrdNo, StartDate, EndDate, Period.PrdYear, CASE WHEN ASubSubmittedBy IS NULL THEN 0 ELSE 1 END,
  347.     AcUnitTrainingLevy, cteMobileRelief.MobileReliefTotal, cteInvoices.TotalInvoices, ctePurchases.PurchaseTotal, cteSundryCosts.SundryTotal,
  348.     cteLabourFigures.PayRollLabour, cteCashReceived.Total, cteCashReceived.VatTotal, cteInvoices.TotalInvoicesVat, cteUnitPeriodOpeningStock.UStkOpening,
  349.     cteUnitPeriodClosingStock.UStkClosing, cteFreeIssues.GoodsAmount, Accounts.AcUnitTradingType, cteLabourFigures.OtherLabour,
  350.     cteDiscount.DiscountTotal, ctePurchases.PurchaseNetTotal, cteCashReceived.GoodsAmount, ctePurchases.CateringTotal, ctePurchases.VendingTotal,
  351.     cteSundryPurchases.Total, cteSundryCosts.SundryNetTotal, ctePurchases.CansConfTotal, cteAgencyLabour.Total, ctePurchases.VatTotal, cteSundryCosts.SundryVatAmount,
  352.     cteAdditionalProfit.AdditionalProfitNetTotal, ctePurchases.CateringVatAmount, ctePurchases.VendingVatAmount, cteSundryPurchases.VatAmount,
  353.     ctePurchases.CansConfVatAmount, ctePurchases.VatTotal, ISNULL(cteExternalFreeIssues.GoodsAmount,0),
  354.     AcUnitNextAnnualFee, AcUnitNextAnnualFeeDate, AcUnitAnnualFee, AcUnitManagementFeePeriod, AcUnitNextManagementFee, AcUnitNextManagementFeeDate,
  355.     AcUnitManagementFee, ctePeriodWeekCount.WeekCount, CompParam.CmpApprenticeshipLevyPercent, UnitLabourFigures.TotalBacksheetLabour,
  356.     AcUnitStockTarget, ctePurchases.TotalCashPaid, AcUnitCashHeldTarget, AcUnitAgencyLabourTarget, AgencyLabour, AcUnitAgencyDebtorsTarget,
  357.     AcUnitCashPaidTarget, Accounts.AcProject, AcUnitSoftwareCharge,Accounts.AcUnitIsFixedGP, Accounts.AcUnitUseOverallFixedGP, Accounts.AcUnitOverallFixedGPValue,
  358.     cteCashReceived.VATFreeGoodsAmount, cteCashReceived.CateringGoodsAmount, cteCashReceived.VendingGoodsAmount, cteCashReceived.CansConfGoodsAmount,
  359.     cteFreeIssues.CateringGoodsAmount, cteFreeIssues.VendingGoodsAmount, cteFreeIssues.CansConfGoodsAmount, cteFreeIssues.LabourGoodsAmount,
  360.     cteUnitPeriodOpeningStock.OpeningCateringStock, cteUnitPeriodOpeningStock.OpeningBeverageStock, cteUnitPeriodOpeningStock.OpeningCansConfStock,
  361.     cteUnitPeriodClosingStock.ClosingCateringStock, cteUnitPeriodClosingStock.ClosingBeverageStock, cteUnitPeriodClosingStock.ClosingCansConfStock,
  362.     Accounts.AcUnitCateringFixedGPValue, Accounts.AcUnitVendingFixedGPValue, Accounts.AcUnitCansConfFixedGPValue;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement