diff --git a/Controller/BackendController.php b/Controller/BackendController.php index 9f2253e..23443b2 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -105,6 +105,7 @@ final class BackendController extends Controller ); $historyStart = $startOfYear->createModify(-9); + $view->data['ytdSales'] = GeneralMapper::ytdSalesProfit($historyStart, $endCurrent, $businessStart); $view->data['annualSales'] = GeneralMapper::annualSalesProfit($historyStart, $endCurrent, $businessStart); [ diff --git a/Controller/Controller.js b/Controller/Controller.js index c03ef8e..19352d1 100755 --- a/Controller/Controller.js +++ b/Controller/Controller.js @@ -74,4 +74,6 @@ omsApp.Modules.SalesAnalysis = class { }; }; +// @bug We cannot guarantee that this is run after the app is setup. Fix it +// Additionally, this way is extremely ugly!!! window.omsApp.moduleManager.get('SalesAnalysis').bind(); diff --git a/Models/GeneralMapper.php b/Models/GeneralMapper.php index 2f649f5..e8e2013 100755 --- a/Models/GeneralMapper.php +++ b/Models/GeneralMapper.php @@ -132,6 +132,79 @@ class GeneralMapper extends DataMapperFactory ]; } + public static function ytdSalesProfit( + SmartDateTime $historyStart, + \DateTime $endCurrent, + int $businessStart = 1 + ) : array { + $query = new Builder(self::$db); + $query->raw( + 'SELECT + YEAR(billing_bill_performance_date) as salesyear, + MONTH(billing_bill_performance_date) as salesmonth, + SUM(billing_bill_netsales * billing_type_sign) as netsales, + SUM(billing_bill_netprofit * billing_type_sign) as netprofit + FROM billing_bill + LEFT JOIN billing_type + ON billing_bill_type = billing_type_id + WHERE + billing_type_transfer_type = ' . BillTransferType::SALES . ' + AND billing_type_accounting = 1 + AND billing_bill_status = ' . BillStatus::ARCHIVED . ' + AND billing_bill_performance_date >= \'' . $historyStart->format('Y-m-d') . '\' + AND billing_bill_performance_date <= \'' . $endCurrent->format('Y-m-d') . '\' + GROUP BY + YEAR(billing_bill_performance_date), + MONTH(billing_bill_performance_date) + ORDER BY + YEAR(billing_bill_performance_date) ASC, + MONTH(billing_bill_performance_date) ASC' + ); + + $results = $query->execute()?->fetchAll(\PDO::FETCH_ASSOC) ?? []; + + $ytdSales = []; + $currentYear = (int) $endCurrent->format('Y'); + $currentMonth = (int) $endCurrent->format('m'); + + // Initialize the YTD sales array + for ($i = 1; $i < 11; ++$i) { + $ytdSales[$i] = [ + 'net_sales' => 0, + 'net_profit' => 0, + 'year' => $historyStart->format('Y'), + ]; + + $historyStart->smartModify(1); + } + + $historyStart->smartModify(-10); + + // Calculate YTD sales and profit for each year + foreach ($results as $result) { + $salesYear = (int) $result['salesyear']; + $salesMonth = (int) $result['salesmonth']; + + // Only consider months up to the current month for each year + if ($salesYear < $currentYear && $salesMonth > $currentMonth) { + continue; + } + + $yearDiff = $salesYear - (int) $historyStart->format('Y'); + $period = $yearDiff + 1; + + if ($period > 10) { + continue; + } + + // Accumulate YTD values + $ytdSales[$period]['net_sales'] += (int) $result['netsales']; + $ytdSales[$period]['net_profit'] += (int) $result['netprofit']; + } + + return $ytdSales; + } + /** * @todo Probably re-implement, still used? */ diff --git a/Theme/Backend/analysis-overview-dashboard.tpl.php b/Theme/Backend/analysis-overview-dashboard.tpl.php index 3c8a1ca..a46f6c9 100755 --- a/Theme/Backend/analysis-overview-dashboard.tpl.php +++ b/Theme/Backend/analysis-overview-dashboard.tpl.php @@ -91,7 +91,7 @@ echo $this->data['nav']->render(); -->