<?php
namespace App\Controller;
use App\Config\Config;
use App\Entity\Orders;
use App\Pdf\AchatsPdf;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use Knp\Snappy\Pdf;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @IsGranted("ROLE_USER")
*/
class IndexController extends AbstractController
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @Route("/", name="admin_homepage")
*/
public function index(Request $request, EntityManagerInterface $entityManager, Config $config)
{
$date_from = $request->query->get('date', date('Y-01-01'));
$date_to = $request->query->get('date_to', date('Y-m-d'));
$ttc = $request->query->get('ttc', 'off');
$orders = $this->getTotalSales($date_from, $date_to);
$nbNewCustomers = $this->getTotalNewCustomers($date_from, $date_to);
$stores = $config->storeAliases;
$livraisons = $config->livraison;
$sales_ca = 0;
$nborders = 0;
$caCC = 0;
$cmdCC = 0;
$caL = 0;
$cmdL = 0;
$cmdNew = 0;
$caNew = 0;
$StoresVals = [];
$livVals = [];
foreach ($stores as $key => $store){
$StoresVals[$key] = array(
"name" => $store,
"ca" => 0,
"cmd" => 0,
);
}
foreach ($livraisons as $key => $livraison){
$livVals[$key] = array(
"name" => $livraison,
"ca" => 0,
"cmd" => 0,
);
}
foreach ($orders as $sale){
$sales_ca += $sale['sales_'.($ttc == 'on'?'ttc':'ht')];
$nborders++;
if($sale['new']){
$cmdNew++;
$caNew += $sale['sales_'.($ttc == 'on'?'ttc':'ht')];
}
if($sale['store']){
$StoresVals[$sale['store']]['ca'] += $sale['sales_'.($ttc == 'on'?'ttc':'ht')];
$StoresVals[$sale['store']]['cmd'] += 1;
$caCC += $sale['sales_'.($ttc == 'on'?'ttc':'ht')];
$cmdCC++;
}elseif($sale['livraison']){
$livVals[$sale['livraison']]['ca'] += $sale['sales_'.($ttc == 'on'?'ttc':'ht')];
$livVals[$sale['livraison']]['cmd'] += 1;
$caL += $sale['sales_'.($ttc == 'on'?'ttc':'ht')];
$cmdL++;
}
}
return $this->render('home.html.twig', [
'ttc' => $ttc,
'date' => $date_from,
'dateTo' => $date_to,
'salesCa' => $sales_ca,
'NbOrders' => $nborders,
'stores' => $stores,
'livraisons' => $livraisons,
'orders' => $orders,
'caCC' => $caCC,
'caL' => $caL,
'cmdCC' => $cmdCC,
'cmdL' => $cmdL,
'storesVals' => $StoresVals,
'nbNewCustomers' => $nbNewCustomers,
'cmdNew' => $cmdNew,
'caNew' => $caNew
]);
}
/**
* @throws Exception
* @throws \Doctrine\DBAL\Exception
*/
private function query($sql, $params = [])
{
$conn = $this->entityManager->getConnection();
$stmt = $conn->prepare($sql);
$stmt->execute($params);
return $stmt;
}
public function getTotalSales($date_from, $date_to)
{
$sql = '
SELECT SUM((total_paid_tax_excl - total_shipping_tax_excl) / o.conversion_rate) AS sales_ht,
SUM((total_paid_tax_incl - total_shipping_tax_incl) / o.conversion_rate) AS sales_ttc,
pdc.id_store as store,
phc.id_reference as livraison,
(SELECT IF(count(so.id_order) > 0, 0, 1) FROM ps36_orders so WHERE (so.id_customer = o.id_customer) AND (so.id_order < o.id_order) LIMIT 1) AS new
FROM `ps36_orders` o
LEFT JOIN `ps36_order_state` os ON o.current_state = os.id_order_state
left join ps36_prestatill_drive_creneau pdc on pdc.id_order = o.id_order
left join ps36_prestatill_homedelivery_creneau phc on phc.id_order = o.id_order
WHERE `invoice_date` BETWEEN "' . $this->escape($date_from) . ' 00:00:00" AND "' . $this->escape($date_to) . ' 23:59:59" AND os.logable = 1
GROUP BY o.id_order';
$query = $this->query($sql);
return $query->fetchAll();
}
public function getTotalNewCustomers($date_from, $date_to)
{
$sql = 'SELECT COUNT(*) FROM ps36_customer WHERE date_add BETWEEN "' . $this->escape($date_from) . ' 00:00:00" AND "' . $this->escape($date_to) . ' 23:59:59"';
$query = $this->query($sql);
return $query->fetchOne();
}
public function escape($string, $html_ok = false, $bq_sql = false)
{
/*if (_PS_MAGIC_QUOTES_GPC_) {
$string = stripslashes($string);
}*/
if (!is_numeric($string)) {
//$string = $this->_escape($string);
if (!$html_ok) {
$string = strip_tags(nl2br($string));
}
if ($bq_sql === true) {
$string = str_replace('`', '\`', $string);
}
}
return $string;
}
}