src/Model/Product/Product.php line 24

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Model\Product;
  15. use Pimcore\Model\DataObject\AccessoryPart\Listing;
  16. use Pimcore\Model\DataObject\Data\Hotspotimage;
  17. use Pimcore\Db;
  18. use Symfony\Component\Security\Core\User\UserInterface;
  19. use Pimcore\Model\DataObject;
  20. class Product extends \Pimcore\Model\DataObject\Product
  21. {
  22.     const OBJECT_TYPE_ACTUAL_PRODUCT 'product';
  23.     /**
  24.      * @return string
  25.      */
  26.     public function getOSName($language=null): ?string
  27.     {
  28.         return $this->getItem();
  29.     }
  30.     public function getProductName($language null) {
  31.         return $this->getItem($language);
  32.     }
  33.     /**
  34.      * @return string
  35.      */
  36.     public function getSubText()
  37.     {
  38.         $textParts = [];
  39.         $textParts[] = $this->getDescription() ? $this->getDescription() : '';
  40.         return $this->getDescription();
  41.     }
  42.     /**
  43.      * @return int|string
  44.      */
  45.     public function getOSProductNumber(): ?string
  46.     {
  47.         return $this->getId();
  48.     }
  49.     /**
  50.      * @return int|string
  51.      */
  52.     public function getEuropeanPrice(): ?string
  53.     {
  54.         $floatPrice = (float) $this->getPrice();
  55.         $formatter = new \NumberFormatter("it-IT"\NumberFormatter::CURRENCY);
  56.         $formatter->setAttribute$formatter::FRACTION_DIGITS);
  57.         return $formatter->formatCurrency($floatPrice"EUR");
  58.     }
  59.     public function getEuropeanPriceWithCheck(): ?string
  60.     {
  61.         $floatPrice = (float) $this->getPrice('vendita');
  62.         $formatter = new \NumberFormatter("it-IT"\NumberFormatter::CURRENCY);
  63.         $formatter->setAttribute$formatter::FRACTION_DIGITS);
  64.         return $formatter->formatCurrency($floatPrice"EUR");
  65.     }
  66.     // /**
  67.     //  * @return int|string
  68.     //  */
  69.     public function getCustomSuggestedPrice(): ?string
  70.     {
  71.         // $service = \Pimcore::getContainer()->get(\Pimcore\Localization\IntlFormatter::class);
  72.         // $service->setLocale("it_IT");
  73.         return parent::getSuggestedPrice(); //$service->formatCurrency(parent::getSuggestedPrice(), "EUR");
  74.     }
  75.     /**
  76.      * @return int
  77.      */
  78.     public function getOSParentId()
  79.     {
  80.         return parent::getOSParentId();
  81.     }
  82.     /**
  83.      * @return Image|null
  84.      */
  85.     public function getMainImage(): ?\Pimcore\Model\Asset\Image
  86.     {
  87.         return (parent::getMainImage())?parent::getMainImage():$this->getDefaultImage();
  88.     }
  89.     public function getDefaultImage(){
  90.         return \Pimcore\Model\Asset::getByPath('/thumbnail.jpg');
  91.     }
  92.     /**
  93.      * @return Category|null
  94.      */
  95.     public function getMainCategory(): ?Category
  96.     {
  97.         $categories $this->getCategories();
  98.         $category reset($categories);
  99.         return $category ?: null;
  100.     }
  101.     public function isInWishList(){
  102.         $result false;
  103.         $user \Pimcore::getContainer()->get('security.token_storage')->getToken()->getUser();
  104.         if($user instanceof \Pimcore\Model\DataObject){
  105.             $stmt Db::get()->fetchAll("SELECT * FROM `object_query_wishlist` WHERE `user__id` = ".$user->getId()." AND `item__id` = '".$this->getId()."'");
  106.             if(!empty($stmt)){
  107.                 $result true;
  108.             }
  109.         }
  110.         return $result;
  111.     }
  112.     public function getPrice($type false): ?string{
  113.         $price parent::getCONSORZ();
  114.         $productId $this->getId();
  115.         if(\Pimcore::getContainer()->get('security.token_storage')->getToken()){
  116.             $user \Pimcore::getContainer()->get('security.token_storage')->getToken()->getUser();
  117.             if($productId && $user instanceof \Pimcore\Model\DataObject\Customer){
  118.                 if($user->getUserRoles() != "Customer" && $type != 'vendita'){
  119.                     $partner $user->getPartner();
  120.                     $priceList = ($partner->getPriceList())?$partner->getPriceList():"CONSORZ.";
  121.                     $priceColumn 'CONSORZ';
  122.                     switch($priceList){
  123.                         case 'CONSORZ.':
  124.                                 $priceColumn 'CONSORZ';
  125.                             break;
  126.                         case 'ESTERO':
  127.                                 $priceColumn 'ESTERO';
  128.                             break;
  129.                         case 'EST.20':
  130.                                 $priceColumn 'EST20';
  131.                             break;
  132.                         case 'FERRAM.':
  133.                                 $priceColumn 'FERRAM';
  134.                             break;
  135.                         case 'RIVENDIT':
  136.                                 $priceColumn 'RIVENDIT';
  137.                             break;
  138.                         case 'PRI/IMP':
  139.                                 $priceColumn 'PRIIMP';
  140.                             break;
  141.                         case 'RIVEDILE':
  142.                                 $priceColumn 'RIVEDILE';
  143.                             break;
  144.                             case 'FERR 10%':
  145.                                 $priceColumn 'FERR10';
  146.                             break;
  147.                         case 'PUNTO3':
  148.                                 $priceColumn 'PUNTO3';
  149.                             break;
  150.                         case 'EDILBITT':
  151.                                 $priceColumn 'EDILBITT';
  152.                             break;
  153.                         case 'LSKOSOVO':
  154.                                 $priceColumn 'LSKOSOVO';
  155.                             break;
  156.                         case 'LSLOVENI':
  157.                                 $priceColumn 'LSLOVENI';
  158.                             break;
  159.                         default:
  160.                                 $priceColumn 'CONSORZ';
  161.                             break;
  162.                     }
  163.                 }else if($user->getUserRoles() == "Customer" || $type=='vendita'){
  164.                     $percentageMargin DEFAULT_B2C_MARGIN;
  165.                     $b2cPriceList \Pimcore\Model\WebsiteSetting::getByName('b2cPriceList');
  166.                     $b2cPriceList $b2cPriceList->getData();
  167.                     $region Db::get()->fetchAll("SELECT region from province where code = '".$user->getProvince()."'");
  168.                     if($region){
  169.                         $stmt Db::get()->fetchAll("SELECT PercentageMargin FROM `object_collection_PricingRuleCustomer_b2c_pricing` WHERE `Region` LIKE '%".$region[0]['region']."%'");
  170.                         if(!empty($stmt)){
  171.                             $percentageMargin = isset($stmt[0]['PercentageMargin'])?$stmt[0]['PercentageMargin']:1;
  172.                         }
  173.                     }
  174.                     $price = (float) $this->getSuggestedPrice();
  175.                     $price $price + (($price*$percentageMargin)/100);
  176.                     // Adding extra tax
  177.                     if($user->getPrivato() == 'privato'){
  178.                         $price $price + (( (float) $price* (float) $b2cPriceList->getCustomerTax())/100);
  179.                     }
  180.                 }
  181.             }
  182.         }
  183.         return (string) $price;
  184.     }
  185.     public function isActive(bool $inProductList false): bool{
  186.         return (parent::getCONSORZ() > 0)?true:false;
  187.     }
  188.     public function getResiduoCount(){
  189.         $totalResiduo 0;
  190.         $productId $this->getId();
  191.        // echo "SELECT SUM(Residuo) as totalResiduo FROM object_dt WHERE Item__id = $productId";
  192.         $stmt Db::get()->fetchAll("SELECT SUM(Residuo) as totalResiduo FROM object_dt WHERE Item__id = $productId");
  193.         if(!empty($stmt)){
  194.             $totalResiduo = ($stmt[0]['totalResiduo'])?$stmt[0]['totalResiduo']:0;
  195.         }
  196.         return $totalResiduo;
  197.     }
  198.     public function getPendingOrders(){
  199.         $result = [];
  200.         $productId $this->getId();
  201.         $orders Db::get()->fetchAll("SELECT ExpectedDeliveryDate,Residuo, OrderDate FROM object_query_dt WHERE Item__id = $productId");
  202.         if($orders){
  203.             $count 0;
  204.             foreach($orders as $order){
  205.                 $result[$count]['ExpectedDeliveryDate'] = date('d\/m\/Y',$order['ExpectedDeliveryDate']);
  206.                 $result[$count]['Residuo'] = $order['Residuo'];
  207.                 $result[$count]['OrderDate'] = date('d\/m\/Y',$order['OrderDate']);
  208.                 $count++;
  209.             }
  210.         }
  211.         return $result;
  212.     }
  213.     public function getSalesRepresentatives(){
  214.         $representatives = [];
  215.         $manufacturer $this->getManufacturer();
  216.         if($manufacturer)
  217.         {
  218.             $salesRepresentatives $manufacturer->getSalesRepesentative();
  219.             if($salesRepresentatives){
  220.                 foreach($salesRepresentatives as $salesRepresentative){
  221.                     $representatives[$salesRepresentative['Name']->getData()]['Name'] = $salesRepresentative['Name']->getData();
  222.                     $representatives[$salesRepresentative['Name']->getData()]['Email'] = $salesRepresentative['PhoneNo']->getData();
  223.                 }
  224.             }
  225.             if($representatives){
  226.                 //$representatives = array_unique($representatives);
  227.             }
  228.         }
  229.         return $representatives;
  230.     }
  231.     public function getBranches(UserInterface $user){
  232.         $branches = [];
  233.         if($user){
  234.             $partner $user->getPartner();
  235.             if($partner){
  236.                 $taxIdNumber $partner->getTaxIdNumber();
  237.                 if($user->getUserRoles() == "Executive" || $user->getUserRoles() == "Customer"){
  238.                     $branches[] = $partner->getId();
  239.                 }
  240.                 else if($user->getUserRoles() == 'Manager'){
  241.                     $partnerBranches = new DataObject\Partners\Listing();
  242.                     $partnerBranches->setCondition("TaxIdNumber = ?",[$partner->getTaxIdNumber()]);
  243.                     $partnerBranches $partnerBranches->load();
  244.                     if($partnerBranches)
  245.                     {
  246.                         foreach($partnerBranches as $branch){
  247.                             $branches[] = $branch->getId();
  248.                         }
  249.                     }
  250.                 }
  251.             }
  252.         }
  253.         return $branches;
  254.     }
  255.     public function getPendingBookingsAndOrders(){
  256.         $bookings 0;
  257.         $orders 0;
  258.         $orderQty 0;
  259.         $bookedQty 0;
  260.         if(\Pimcore::getContainer()->get('security.token_storage')->getToken()){
  261.                 $user \Pimcore::getContainer()->get('security.token_storage')->getToken()->getUser();
  262.                 $partner $user->getPartner();
  263.                 $productId $this->getId();
  264.                 if($partner && $productId){
  265.                     $partnerId $partner->getId();
  266.                     $this->db \Pimcore::getContainer()->get('doctrine.dbal.default_connection');
  267.                     $pendingBooking $this->db->fetchAll("SELECT count(poi.POItem) as totalpendingbooking, COUNT(poi.Qty) as totalpendingqty FROM `object_po` po LEFT JOIN object_poi poi ON po.oo_id = poi.o_parentId WHERE po.Branch__id = $partnerId AND poi.product__id = $productId AND po.isCancelled IS NULL AND po.isExpired = 0 AND po.WorkflowState = 'Booked'");
  268.                     if($pendingBooking){
  269.                         $bookings =$pendingBooking[0]['totalpendingbooking'];
  270.                         $bookedQty =$pendingBooking[0]['totalpendingqty'];
  271.                     }
  272.                     $pendingOrder $this->db->fetchAll("SELECT count(po.`oo_id`) as totalpendingorder, SUM(poi.amount) as totalpendingqty  FROM `object_EF_OSO` po LEFT JOIN object_EF_OSOI poi ON po.oo_id = poi.o_parentId WHERE po.Branch__id = $partnerId AND poi.product__id = $productId AND po.WorkflowState != 'delivered'; ");
  273.                     if($pendingOrder){
  274.                         $orders =$pendingOrder[0]['totalpendingorder'];
  275.                         $orderQty $pendingOrder[0]['totalpendingqty'];
  276.                     }
  277.                 }
  278.         }
  279.         return ['booking'=>$bookings,'order'=>$orders,'orderQty'=>$orderQty,'bookedQty'=>$bookedQty];
  280.     }
  281.     public function getLastDelivery(){
  282.         $this->db \Pimcore::getContainer()->get('doctrine.dbal.default_connection');
  283.         $result $this->db->fetchAll('SELECT op.Item, dd.Qty,  dd.UOM, dd.DocumentDate FROM `object_dd` dd LEFT JOIN object_prod op ON dd.Item__id = op.oo_id WHERE dd.Item__id = '.$this->getId().' AND itemDelivered = 1 ORDER BY DocumentDate DESC LIMIT 1');
  284.         return $result;
  285.     }
  286.     public function getPrezzoVendita($qty 1)
  287.     {
  288.         return $this->getPrice('vendita')*$qty;
  289.     }
  290. }