mirror of
https://github.com/Karaka-Management/oms-SalesAnalysis.git
synced 2026-02-04 05:48:40 +00:00
many bug fixes
This commit is contained in:
parent
7561985097
commit
b37993e75b
|
|
@ -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);
|
||||
|
||||
[
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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?
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ echo $this->data['nav']->render();
|
|||
-->
|
||||
|
||||
<div class="row">
|
||||
<div class="col-xs-12 col-lg-6">
|
||||
<div class="col-xs-12 col-lg-4">
|
||||
<section class="portlet">
|
||||
<div class="portlet-head">
|
||||
<?= $this->getHtml('SalesProfit'); ?> (<?= $this->getHtml('monthly'); ?>)
|
||||
|
|
@ -281,7 +281,136 @@ echo $this->data['nav']->render();
|
|||
</section>
|
||||
</div>
|
||||
|
||||
<div class="col-xs-12 col-lg-6">
|
||||
<div class="col-xs-12 col-lg-4">
|
||||
<section class="portlet">
|
||||
<div class="portlet-head">
|
||||
<?= $this->getHtml('SalesProfit'); ?> (<?= $this->getHtml('YTD'); ?>)
|
||||
</div>
|
||||
<?php $sales = $this->data['ytdSales']; ?>
|
||||
<div class="portlet-body">
|
||||
<canvas id="sales-profit-ytd" data-chart='{
|
||||
"type": "bar",
|
||||
"data": {
|
||||
"labels": [
|
||||
<?php
|
||||
$temp = [];
|
||||
for ($i = 1; $i < 11; ++$i) {
|
||||
$temp[] = $sales[$i]['year'];
|
||||
}
|
||||
echo \implode(',', $temp);
|
||||
?>
|
||||
],
|
||||
"datasets": [
|
||||
{
|
||||
"label": "<?= $this->getHtml('Profit'); ?>",
|
||||
"type": "line",
|
||||
"data": [
|
||||
<?php
|
||||
$temp = [];
|
||||
for ($i = 1; $i < 11; ++$i) {
|
||||
if ($sales[$i]['net_sales'] === null || $sales[$i]['net_profit'] === null) {
|
||||
$temp[] = 'null';
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$temp[] = $sales[$i]['net_sales'] == 0
|
||||
? 0
|
||||
: $sales[$i]['net_profit'] * 100 / $sales[$i]['net_sales'];
|
||||
}
|
||||
echo \implode(',', $temp);
|
||||
?>
|
||||
],
|
||||
"yAxisID": "y1",
|
||||
"fill": false,
|
||||
"tension": 0.0,
|
||||
"borderColor": "rgb(46, 204, 113)",
|
||||
"backgroundColor": "rgb(46, 204, 113)"
|
||||
},
|
||||
{
|
||||
"label": "<?= $this->getHtml('Sales'); ?>",
|
||||
"type": "bar",
|
||||
"data": [
|
||||
<?php
|
||||
$temp = [];
|
||||
for ($i = 1; $i < 11; ++$i) {
|
||||
$temp[] = $sales[$i]['net_sales'] / FloatInt::DIVISOR;
|
||||
}
|
||||
echo \implode(',', $temp);
|
||||
?>
|
||||
],
|
||||
"yAxisID": "y",
|
||||
"fill": false,
|
||||
"tension": 0.0,
|
||||
"backgroundColor": "rgb(54, 162, 235)"
|
||||
}
|
||||
]
|
||||
},
|
||||
"options": {
|
||||
"responsive": true,
|
||||
"scales": {
|
||||
"x": {
|
||||
"title": {
|
||||
"display": true,
|
||||
"text": "<?= $this->getHtml('Months'); ?>"
|
||||
}
|
||||
},
|
||||
"y": {
|
||||
"title": {
|
||||
"display": true,
|
||||
"text": "<?= $this->getHtml('Sales'); ?>"
|
||||
},
|
||||
"display": true,
|
||||
"position": "left"
|
||||
},
|
||||
"y1": {
|
||||
"title": {
|
||||
"display": true,
|
||||
"text": "<?= $this->getHtml('Profit'); ?> %"
|
||||
},
|
||||
"display": true,
|
||||
"position": "right",
|
||||
"scaleLabel": {
|
||||
"display": true,
|
||||
"labelString": "<?= $this->getHtml('Profit'); ?>"
|
||||
},
|
||||
"grid": {
|
||||
"drawOnChartArea": false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}'></canvas>
|
||||
<div class="more-container">
|
||||
<input id="more-customer-sales-ytd" class="more" type="checkbox" name="more-container">
|
||||
<label class="more" for="more-customer-sales-ytd">
|
||||
<span><?= $this->getHtml('Data'); ?></span>
|
||||
<i class="g-icon expand">chevron_right</i>
|
||||
</label>
|
||||
<div class="slider more">
|
||||
<table class="default sticky">
|
||||
<thead>
|
||||
<tr>
|
||||
<td><?= $this->getHtml('Year'); ?>
|
||||
<td><?= $this->getHtml('Sales'); ?>
|
||||
<td><?= $this->getHtml('Profit'); ?>
|
||||
<tbody>
|
||||
<?php
|
||||
foreach ($sales as $values) :
|
||||
?>
|
||||
<tr>
|
||||
<td><?= $this->printHtml($values['year']); ?>
|
||||
<td><?= $this->getCurrency(((int) $values['net_sales']) / FloatInt::DIVISOR, symbol: ''); ?>
|
||||
<td><?= \sprintf('%.2f', $values['net_sales'] == 0 ? 0 : $values['net_profit'] * 100 / $values['net_sales']); ?> %
|
||||
<?php endforeach; ?>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div class="col-xs-12 col-lg-4">
|
||||
<section class="portlet">
|
||||
<div class="portlet-head">
|
||||
<?= $this->getHtml('SalesProfit'); ?> (<?= $this->getHtml('annually'); ?>)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user