bug fixes

This commit is contained in:
Dennis Eichhorn 2023-12-08 21:52:33 +00:00
parent ea39466578
commit 6a561ade53
5 changed files with 33 additions and 49 deletions

View File

@ -65,8 +65,8 @@ final class BackendController extends Controller
$view->setTemplate('/Modules/SalesAnalysis/Theme/Backend/analysis-overview-dashboard'); $view->setTemplate('/Modules/SalesAnalysis/Theme/Backend/analysis-overview-dashboard');
$view->data['nav'] = $this->app->moduleManager->get('Navigation')->createNavigationMid(1005401001, $request, $response); $view->data['nav'] = $this->app->moduleManager->get('Navigation')->createNavigationMid(1005401001, $request, $response);
// @todo: limit bill type (invoice/credit note) (customers only) // @todo limit bill type (invoice/credit note) (customers only)
// @todo: limit bill status // @todo limit bill status
$businessStart = 1; $businessStart = 1;
$startOfYear = SmartDateTime::createFromDateTime(SmartDateTime::startOfYear($businessStart)); $startOfYear = SmartDateTime::createFromDateTime(SmartDateTime::startOfYear($businessStart));
@ -235,7 +235,7 @@ final class BackendController extends Controller
$businessStart $businessStart
); );
$annualCountrySales = RegionMapper::annualSalesProfitCountry(clone $historyStart, $endCurrent); $annualCountrySales = RegionMapper::salesProfitCountry($historyStart, $startCurrent, $startCurrent, $endCurrent);
$view->data['ytdADomesticExport'] = RegionMapper::countryToRegion( $view->data['ytdADomesticExport'] = RegionMapper::countryToRegion(
$view->data['ytdAClientCountry'], $view->data['ytdAClientCountry'],

View File

@ -38,7 +38,7 @@ class ClientMapper extends DataMapperFactory
) { ) {
$endCurrentIndex = SmartDateTime::calculateMonthIndex((int) $endCurrent->format('m'), $businessStart); $endCurrentIndex = SmartDateTime::calculateMonthIndex((int) $endCurrent->format('m'), $businessStart);
// @todo: this query doesn't return clients that have not segment etc. defined. // @todo this query doesn't return clients that have not segment etc. defined.
$query = new Builder(self::$db); $query = new Builder(self::$db);
$query->raw( $query->raw(
'SELECT 'SELECT

View File

@ -162,7 +162,7 @@ class GeneralMapper extends DataMapperFactory
$historyStart->smartModify(-10); $historyStart->smartModify(-10);
$oldIndex = 1; $oldIndex = 1;
// @todo: this calculation doesn't consider the start of the fiscal year // @todo this calculation doesn't consider the start of the fiscal year
$period = (int) (((((int) $results[0]['salesyear']) - ((int) $historyStart->format('Y'))) * 12 $period = (int) (((((int) $results[0]['salesyear']) - ((int) $historyStart->format('Y'))) * 12
- ((int) $results[0]['salesmonth']) + ((int) $historyStart->format('m'))) / 12 + 1); - ((int) $results[0]['salesmonth']) + ((int) $historyStart->format('m'))) / 12 + 1);

View File

@ -38,7 +38,7 @@ class ItemMapper extends DataMapperFactory
) { ) {
$endCurrentIndex = SmartDateTime::calculateMonthIndex((int) $endCurrent->format('m'), $businessStart); $endCurrentIndex = SmartDateTime::calculateMonthIndex((int) $endCurrent->format('m'), $businessStart);
// @todo: this query doesn't return clients that have not segment etc. defined. // @todo this query doesn't return clients that have not segment etc. defined.
$query = new Builder(self::$db); $query = new Builder(self::$db);
$query->raw( $query->raw(
'SELECT 'SELECT

View File

@ -27,7 +27,7 @@ use phpOMS\Stdlib\Base\SmartDateTime;
* @link https://jingga.app * @link https://jingga.app
* @since 1.0.0 * @since 1.0.0
* *
* @todo: the periods are wrong if they are disjunct (e.g. A vs PPY) * @todo the periods are wrong if they are disjunct (e.g. A vs PPY)
* solution: functions need a clear start-end time and then called twice for A vs PY comparison * solution: functions need a clear start-end time and then called twice for A vs PY comparison
*/ */
class RegionMapper extends DataMapperFactory class RegionMapper extends DataMapperFactory
@ -146,7 +146,7 @@ class RegionMapper extends DataMapperFactory
$annualCustomer = []; $annualCustomer = [];
$oldIndex = 1; $oldIndex = 1;
// @todo: this calculation doesn't consider the start of the fiscal year // @todo this calculation doesn't consider the start of the fiscal year
$period = (int) (((((int) $results[0]['salesyear']) - ((int) $historyStart->format('Y'))) * 12 $period = (int) (((((int) $results[0]['salesyear']) - ((int) $historyStart->format('Y'))) * 12
- ((int) $results[0]['salesmonth']) + ((int) $historyStart->format('m'))) / 12 + 1); - ((int) $results[0]['salesmonth']) + ((int) $historyStart->format('m'))) / 12 + 1);
@ -190,7 +190,7 @@ class RegionMapper extends DataMapperFactory
\DateTime $endComparison, \DateTime $endComparison,
int $businessStart = 1, int $businessStart = 1,
) : array { ) : array {
// @todo: this cannot be correct since the same customer may buy something in two month (distinct is required over an actual period) // @todo this cannot be correct since the same customer may buy something in two month (distinct is required over an actual period)
$endCurrentIndex = SmartDateTime::calculateMonthIndex((int) $endCurrent->format('m'), $businessStart); $endCurrentIndex = SmartDateTime::calculateMonthIndex((int) $endCurrent->format('m'), $businessStart);
$query = new Builder(self::$db); $query = new Builder(self::$db);
@ -287,17 +287,19 @@ class RegionMapper extends DataMapperFactory
]; ];
} }
public static function annualSalesProfitCountry( // @todo remove businessStart, that should be baked into the historyStart
// Explanation: in the past I had to compare periods which werent even business years!!!
public static function salesProfitCountry(
SmartDateTime $historyStart, SmartDateTime $historyStart,
\DateTime $endCurrent, SmartDateTime $historyEnd,
int $businessStart = 1 \DateTime $currentStart,
\DateTime $currentEnd
) : array { ) : array {
$query = new Builder(self::$db); $query = new Builder(self::$db);
$query->raw( $query->raw(
'SELECT 'SELECT
address_country, address_country,
YEAR(billing_bill_performance_date) as salesyear, billing_bill_performance_date,
MONTH(billing_bill_performance_date) as salesmonth,
SUM(billing_bill_netsales) as netsales, SUM(billing_bill_netsales) as netsales,
SUM(billing_bill_netprofit) as netprofit SUM(billing_bill_netprofit) as netprofit
FROM billing_bill FROM billing_bill
@ -307,60 +309,42 @@ class RegionMapper extends DataMapperFactory
ON clientmgmt_client_address = address_id ON clientmgmt_client_address = address_id
WHERE WHERE
billing_bill_performance_date >= \'' . $historyStart->format('Y-m-d') . '\' billing_bill_performance_date >= \'' . $historyStart->format('Y-m-d') . '\'
AND billing_bill_performance_date <= \'' . $endCurrent->format('Y-m-d') . '\' AND billing_bill_performance_date <= \'' . $currentEnd->format('Y-m-d') . '\'
GROUP BY
address_country,
YEAR(billing_bill_performance_date),
MONTH(billing_bill_performance_date)
ORDER BY ORDER BY
YEAR(billing_bill_performance_date) ASC, billing_bill_performance_date ASC,
MONTH(billing_bill_performance_date) ASC,
address_country' address_country'
); );
$results = $query->execute()->fetchAll(\PDO::FETCH_ASSOC); $results = $query->execute()->fetchAll(\PDO::FETCH_ASSOC);
$annualSales = []; $sales = [];
$period = 0;
$oldIndex = 1;
// @todo: this calculation doesn't consider the start of the fiscal year
$period = (int) (((((int) $results[0]['salesyear']) - ((int) $historyStart->format('Y'))) * 12
- ((int) $results[0]['salesmonth']) + ((int) $historyStart->format('m'))) / 12 + 1);
foreach ($results as $result) { foreach ($results as $result) {
$monthIndex = SmartDateTime::calculateMonthIndex((int) $result['salesmonth'], $businessStart); $date = new \DateTime($result['billing_bill_performance_date']);
if ($monthIndex < $oldIndex) { if ($date->getTimestamp() <= $historyEnd->getTimestamp()) {
$oldIndex = $monthIndex; $period = 0;
} elseif ($date->getTimestamp() >= $currentStart->getTimestamp()) {
++$period; $period = 1;
} else {
continue;
} }
if ($period > 10) { if (!isset($sales[$result['address_country']])) {
break;
}
$oldIndex = $monthIndex;
if (!isset($annualSales[$result['address_country']])) {
for ($i = 1; $i < 11; ++$i) { for ($i = 1; $i < 11; ++$i) {
$annualSales[$result['address_country']][$i] = [ $sales[$result['address_country']][$i] = [
'net_sales' => 0, 'net_sales' => 0,
'net_profit' => 0, 'net_profit' => 0,
'year' => $historyStart->format('Y'), 'year' => $period === 0 ? 'PY' : 'A',
]; ];
}
$historyStart->smartModify(1);
} }
$historyStart->smartModify(-10); $sales[$result['address_country']][$period]['net_sales'] += (int) $result['netsales'];
$sales[$result['address_country']][$period]['net_profit'] += (int) $result['netprofit'];
} }
// indexed according to the fiscal year return $sales;
$annualSales[$result['address_country']][$period]['net_sales'] += (int) $result['netsales'];
$annualSales[$result['address_country']][$period]['net_profit'] += (int) $result['netprofit'];
}
return $annualSales;
} }
public static function mtdYtdCountry( public static function mtdYtdCountry(