continue item/billing impl.

This commit is contained in:
Dennis Eichhorn 2021-02-06 13:48:48 +01:00
parent 1d817519a8
commit abd42067f0
3 changed files with 271 additions and 20 deletions

47
Controller.js Normal file
View File

@ -0,0 +1,47 @@
import { Autoloader } from '../../jsOMS/Autoloader.js';
Autoloader.defineNamespace('jsOMS.Modules');
jsOMS.Modules.ItemManagement = class {
/**
* @constructor
*
* @since 1.0.0
*/
constructor (app)
{
this.app = app;
};
bind (id)
{
const e = typeof id === 'undefined' ? document.getElementsByTagName('canvas') : [document.getElementById(id)],
length = e.length;
for (let i = 0; i < length; ++i) {
if (e[i].getAttribute('data-chart') === null
&& e[i].getAttribute('data-chart') !== 'undefined'
) {
continue;
}
this.bindElement(e[i]);
}
};
bindElement (chart)
{
if (typeof chart === 'undefined' || !chart) {
jsOMS.Log.Logger.instance.error('Invalid chart: ' + chart, 'ItemManagementController');
return;
}
const self = this;
const data = JSON.parse(chart.getAttribute('data-chart'));
const myChart = new Chart(chart.getContext('2d'), data);
};
};
window.omsApp.moduleManager.get('ItemManagement').bind();

View File

@ -16,11 +16,15 @@ namespace Modules\ItemManagement\Controller;
use Model\SettingsEnum;
use Modules\Admin\Models\LocalizationMapper;
use Modules\Billing\Models\BillMapper;
use Modules\ItemManagement\Models\ItemMapper;
use phpOMS\Contract\RenderableInterface;
use phpOMS\Message\RequestAbstract;
use phpOMS\Message\ResponseAbstract;
use phpOMS\Views\View;
use phpOMS\Stdlib\Base\SmartDateTime;
use phpOMS\Localization\Money;
use phpOMS\Asset\AssetType;
/**
* ItemManagement controller class.
@ -178,6 +182,11 @@ final class BackendController extends Controller
*/
public function viewItemManagementSalesItem(RequestAbstract $request, ResponseAbstract $response, $data = null) : RenderableInterface
{
$head = $response->get('Content')->getData('head');
$head->addAsset(AssetType::CSS, 'Resources/chartjs/Chartjs/chart.css');
$head->addAsset(AssetType::JSLATE, 'Resources/chartjs/Chartjs/chart.js');
$head->addAsset(AssetType::JSLATE, 'Modules/ItemManagement/Controller.js', ['type' => 'module']);
$view = new View($this->app->l11nManager, $request, $response);
$view->setTemplate('/Modules/ItemManagement/Theme/Backend/sales-item-profile');
$view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1004805001, $request, $response));
@ -191,6 +200,38 @@ final class BackendController extends Controller
$view->setData('defaultlocalization', LocalizationMapper::get((int) $settings['id']));
if ($this->app->moduleManager->isActive('Billing')) {
$ytd = BillMapper::getSalesByItemId($item->getId(), new SmartDateTime('Y-01-01'), new SmartDateTime('now'));
$mtd = BillMapper::getSalesByItemId($item->getId(), new SmartDateTime('Y-m-01'), new SmartDateTime('now'));
$avg = BillMapper::getAvgSalesPriceByItemId($item->getId(), (new SmartDateTime('now'))->smartModify(-1), new SmartDateTime('now'));
$lastOrder = BillMapper::getLastOrderDateByItemId($item->getId());
$newestInvoices = BillMapper::getNewestItemInvoices($item->getId(), 5);
$topCustomers = BillMapper::getItemTopCustomers($item->getId(), new SmartDateTime('Y-01-01'), new SmartDateTime('now'), 5);
$regionSales = BillMapper::getItemRegionSales($item->getId(), new SmartDateTime('Y-01-01'), new SmartDateTime('now'));
$countrySales = BillMapper::getItemCountrySales($item->getId(), new SmartDateTime('Y-01-01'), new SmartDateTime('now'), 5);
$monthlySalesCosts = BillMapper::getItemMonthlySalesCosts($item->getId(), (new SmartDateTime('now'))->createModify(-1), new SmartDateTime('now'));
} else {
$ytd = new Money();
$mtd = new Money();
$avg = new Money();
$lastOrder = null;
$newestInvoices = [];
$topCustomers = [];
$regionSales = [];
$countrySales = [];
$monthlySalesCosts = [];
}
$view->addData('ytd', $ytd);
$view->addData('mtd', $mtd);
$view->addData('avg', $avg);
$view->addData('lastOrder', $lastOrder);
$view->addData('newestInvoices', $newestInvoices);
$view->addData('topCustomers', $topCustomers);
$view->addData('regionSales', $regionSales);
$view->addData('countrySales', $countrySales);
$view->addData('monthlySalesCosts', $monthlySalesCosts);
return $view;
}
}

View File

@ -15,11 +15,17 @@ declare(strict_types=1);
use Modules\Media\Models\NullMedia;
use phpOMS\Localization\NullLocalization;
use phpOMS\Uri\UriFactory;
use phpOMS\Localization\Money;
/**
* @var \Modules\ItemManagement\Models\Item $item
*/
$item = $this->getData('item');
$newestInvoices = $this->getData('newestInvoices') ?? [];
$topCustomers = $this->getData('topCustomers') ?? [];
$regionSales = $this->getData('regionSales') ?? [];
$countrySales = $this->getData('countrySales') ?? [];
$monthlySalesCosts = $this->getData('monthlySalesCosts') ?? [];
$languages = \phpOMS\Localization\ISO639Enum::getConstants();
@ -94,9 +100,9 @@ echo $this->getData('nav')->render();
<div class="portlet-body">
<table>
<tr><td>YTD Sales:
<td>
<td><?= $this->getData('ytd')->getCurrency(); ?>
<tr><td>MTD Sales:
<td>
<td><?= $this->getData('mtd')->getCurrency(); ?>
<tr><td>ILV:
<td>
<tr><td>MRR:
@ -111,11 +117,11 @@ echo $this->getData('nav')->render();
<div class="portlet-body">
<table>
<tr><td>Last Order:
<td>
<td><?= $this->getData('lastOrder') !== null ? $this->getData('lastOrder')->format('Y-m-d H:i') : ''; ?>
<tr><td>Price Change:
<td>
<tr><td>Created:
<td>
<td><?= $item->createdAt->format('Y-m-d H:i'); ?>
<tr><td>Modified:
<td>
</table>
@ -128,13 +134,13 @@ echo $this->getData('nav')->render();
<div class="portlet-body">
<table>
<tr><td>Sales Price:
<td>
<td><?= $item->salesPrice->getCurrency(); ?>
<tr><td>Purchase Price:
<td>
<td><?= $item->purchasePrice->getCurrency(); ?>
<tr><td>Margin:
<td>
<td><?= \round(($item->salesPrice->getInt() - $item->purchasePrice->getInt()) / $item->salesPrice->getInt() * 100, 2); ?> %
<tr><td>Avg. Price:
<td>
<td><?= $this->getData('avg')->getCurrency(); ?>
</table>
</div>
</section>
@ -160,40 +166,197 @@ echo $this->getData('nav')->render();
<div class="row">
<div class="col-xs-12">
<section class="portlet">
<div class="portlet-head">Invoices</div>
<div class="portlet-body"></div>
<div class="portlet-head">Recent Invoices</div>
<table id="iSalesItemList" class="default">
<thead>
<tr>
<td>Number
<td class="wf-100">Name
<td>Net
<td>Date
<tbody>
<?php foreach ($newestInvoices as $invoice) : ?>
<tr>
<td><?= $invoice->getNumber(); ?>
<td><?= $invoice->billTo; ?>
<td><?= $invoice->net->getCurrency(); ?>
<td><?= $invoice->createdAt->format('Y-m-d'); ?>
<?php endforeach; ?>
</table>
</section>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-md-6">
<div class="col-xs-12 col-lg-6">
<section class="portlet">
<div class="portlet-head">Customers</div>
<div class="portlet-body"></div>
<div class="portlet-head">Top Customers</div>
<table id="iSalesItemList" class="default">
<thead>
<tr>
<td>Id
<td class="wf-100">Name
<td>Country
<td>Net
<tbody>
<?php $i = -1; foreach ($topCustomers[0] as $client) : ++$i;
$url = UriFactory::build('{/prefix}sales/client/profile?id=' . $client->getId());
?>
<tr data-href="<?= $url; ?>">
<td><a href="<?= $url; ?>"><?= $this->printHtml($client->number); ?></a>
<td><a href="<?= $url; ?>"><?= $this->printHtml($client->profile->account->name1); ?> <?= $this->printHtml($client->profile->account->name2); ?></a>
<td><a href="<?= $url; ?>"><?= $this->printHtml($client->mainAddress->getCountry()); ?></a>
<td><a href="<?= $url; ?>"><?= (new Money((int) $topCustomers[1][$i]['net_sales']))->getCurrency(); ?></a>
<?php endforeach; ?>
</table>
</section>
</div>
<div class="col-xs-12 col-md-6">
<div class="col-xs-12 col-lg-6">
<section class="portlet">
<div class="portlet-head">Sales</div>
<div class="portlet-body"></div>
<div class="portlet-body">
<canvas id="sales-region" data-chart='{
"type": "bar",
"data": {
"labels": [
<?php
$temp = [];
foreach ($monthlySalesCosts as $monthly) {
$temp[] = $monthly['month'] . '/' . \substr((string) $monthly['year'], -2);
}
?>
<?= '"' . \implode('", "', $temp) . '"'; ?>
],
"datasets": [
{
"label": "Margin",
"type": "line",
"data": [
<?php
$temp = [];
foreach ($monthlySalesCosts as $monthly) {
$temp[] = \round(((((int) $monthly['net_sales']) - ((int) $monthly['net_costs'])) / ((int) $monthly['net_sales'])) * 100, 2);
}
?>
<?= \implode(',', $temp); ?>
],
"yAxisID": "axis-2",
"fill": false,
"borderColor": "rgb(255, 99, 132)",
"backgroundColor": "rgb(255, 99, 132)"
},
{
"label": "Sales",
"type": "bar",
"data": [
<?php
$temp = [];
foreach ($monthlySalesCosts as $monthly) {
$temp[] = ((int) $monthly['net_sales']) / 1000;
}
?>
<?= \implode(',', $temp); ?>
],
"yAxisID": "axis-1",
"backgroundColor": "rgb(54, 162, 235)"
}
]
},
"options": {
"scales": {
"yAxes": [
{
"id": "axis-1",
"display": true,
"position": "left"
},
{
"id": "axis-2",
"display": true,
"position": "right",
"scaleLabel": {
"display": true,
"labelString": "Margin %"
},
"beginAtZero": true,
"ticks": {
"min": 0,
"max": 100,
"stepSize": 10
}
}
]
}
}
}'></canvas>
</div>
</section>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-md-6">
<div class="col-xs-12 col-lg-6">
<section class="portlet">
<div class="portlet-head">Regions</div>
<div class="portlet-body"></div>
<div class="portlet-body">
<canvas id="sales-region" data-chart='{
"type": "pie",
"data": {
"labels": [
"Europe", "America", "Asia", "Africa", "CIS", "Other"
],
"datasets": [{
"data": [
<?= (int) ($regionSales['Europe'] ?? 0) / 1000; ?>,
<?= (int) ($regionSales['America'] ?? 0) / 1000; ?>,
<?= (int) ($regionSales['Asia'] ?? 0) / 1000; ?>,
<?= (int) ($regionSales['Africa'] ?? 0) / 1000; ?>,
<?= (int) ($regionSales['CIS'] ?? 0) / 1000; ?>,
<?= (int) ($regionSales['Other'] ?? 0) / 1000; ?>
],
"backgroundColor": [
"rgb(255, 99, 132)",
"rgb(255, 159, 64)",
"rgb(255, 205, 86)",
"rgb(75, 192, 192)",
"rgb(54, 162, 235)",
"rgb(153, 102, 255)"
]
}]
}
}'></canvas>
</div>
</section>
</div>
<div class="col-xs-12 col-md-6">
<div class="col-xs-12 col-lg-6">
<section class="portlet">
<div class="portlet-head">Margins</div>
<div class="portlet-body"></div>
<div class="portlet-head">Countries</div>
<div class="portlet-body">
<canvas id="sales-region" data-chart='{
"type": "bar",
"data": {
"labels": [
<?= '"' . \implode('", "', \array_keys($countrySales)) . '"'; ?>
],
"datasets": [{
"label": "YTD",
"type": "bar",
"data": [
<?php
$temp = [];
foreach ($countrySales as $country) {
$temp[] = ((int) $country) / 1000;
}
?>
<?= \implode(',', $temp); ?>
],
"backgroundColor": "rgb(54, 162, 235)"
}]
}
}'></canvas>
</div>
</section>
</div>
</div>