123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace Core\App\Factory;
- class OrderItemFactory extends EntityFactory
- {
- public $remoteProducts;
- public $localProducts;
- public $withDiscountOption;
- public function createOrderProduct(
- $product_ids,
- $product_name,
- $ean,
- $sku,
- $currency_code,
- $price,
- $taxRate,
- $quantity,
- $tax = 0,
- $taxIncluded = false
- )
- {
- $orderProduct = new \Core\App\Entity\OrderItem();
- $prod_cod1 = $this->getPrefix().$product_ids;
- $taxRate = $this->getTaxRate($taxRate) ;
- $initialPrice = $price;
- $initialTax = $tax;
- $price = $this->calculatePrice($price, $product_name, $ean, $sku, $prod_cod1);
- $tax = $this->calculateTax($price, $tax, $taxRate);
- if(!empty($prod_cod1) && in_array($prod_cod1, array_column($this->remoteProducts, 'Alt_Cod')))
- {
- $index = array_search($prod_cod1, array_column($this->remoteProducts, 'Alt_Cod'));
- }
- if(!empty($ean) && in_array($ean, array_column($this->remoteProducts, 'Cod_EAN')))
- {
- $index = array_search($ean, array_column($this->remoteProducts, 'Cod_EAN'));
- }
- if(!empty($sku) && in_array($sku, array_column($this->remoteProducts, 'Cod_SKU')))
- {
- $index = array_search($sku, array_column($this->remoteProducts, 'Cod_SKU'));
- }
- if(!empty($product_name) && in_array($product_name, array_column($this->remoteProducts, 'Denumire')))
- {
- $index = array_search($product_name, array_column($this->remoteProducts, 'Denumire'));
- }
- if(isset($index))
- {
- $product_name = isset($this->remoteProducts[$index]['Denumire']) ? $this->remoteProducts[$index]['Denumire'] : $product_name;
- $ean = isset($this->remoteProducts[$index]['Cod_EAN']) ? $this->remoteProducts[$index]['Cod_EAN'] : $ean;
- $sku = isset($this->remoteProducts[$index]['Cod_SKU']) ? $this->remoteProducts[$index]['Cod_SKU'] : $sku;
- $prod_cod1 = (isset($this->remoteProducts[$index]['Alt_Cod']) && !empty($this->remoteProducts[$index]['Alt_Cod'])) ? $this->remoteProducts[$index]['Alt_Cod'] : $prod_cod1;
- }
-
-
- $pretFtva = $this->getPretftva($price, $taxRate, $tax, $taxIncluded);
- $pretCtva = $this->getPretctva($price, $taxRate, $tax, $taxIncluded);
- $orderProduct->setFacturiProdNume($this->stripSpecialChars($product_name));
- $orderProduct->setFacturiProdMoneda($currency_code);
- $orderProduct->setFacturiProdPretftva($pretFtva);
- $orderProduct->setFacturiProdPretctva($pretCtva);
- $orderProduct->setFacturiProdTva($this->getTaxName($taxRate));
- $orderProduct->setFacturiProdCant($quantity);
- $orderProduct->setFacturiProdVal($pretFtva * $quantity);
- $orderProduct->setFacturiProdValTva(($pretCtva - $pretFtva) * $quantity);
- $orderProduct->setFacturiProdValTot($pretCtva * $quantity);
- $orderProduct->setFacturiProdUm(self::UM);
- $orderProduct->setProdCod($ean);
- $orderProduct->setProdSku($sku);
- $orderProduct->setProdCod1($prod_cod1);
- $orderProduct->setProdCodCautare('all');
-
- if($this->withDiscountOption == \Core\App\Factory\SettingsFactory::SYNC_ENABLED)
- {
- return array((array)$orderProduct);
- }
- else
- {
- $discount = $initialPrice - $price;
- if($discount < 0)
- {
- $tax = $tax - $initialTax;
- $discountFtva = $this->getPretftva($discount, $taxRate, $tax, $taxIncluded);
- $discountCtva = $this->getPretctva($discount, $taxRate, $tax, $taxIncluded);
- if($tax == 0)
- {
- $tax = $discountCtva - $discountFtva;
- }
- $orderProductDiscount = new \Core\App\Entity\OrderItem();
- $orderProductDiscount->setFacturiProdNume('Discount la: ' . $this->stripSpecialChars($product_name));
- $orderProductDiscount->setFacturiProdMoneda($currency_code);
- $orderProductDiscount->setFacturiProdPretftva($discountFtva);
- $orderProductDiscount->setFacturiProdPretctva($discountCtva);
- $orderProductDiscount->setFacturiProdTva($taxRate . '%');
- $orderProductDiscount->setFacturiProdCant($quantity);
- $orderProductDiscount->setFacturiProdVal($discountFtva * $quantity);
- $orderProductDiscount->setFacturiProdValTva($tax * $quantity);
- $orderProductDiscount->setFacturiProdValTot($discountCtva * $quantity);
- $orderProductDiscount->setProdCodCautare('all');
-
- return array((array)$orderProduct, (array)$orderProductDiscount);
- }
- else
- {
- return array((array)$orderProduct);
- }
- }
- }
- public function createOrderTax(
- $product_name,
- $currency_code,
- $price,
- $tax = 0
- )
- {
- if($price != 0)
- {
- $taxValue = round(($tax * 100)/$price);
- }
- else
- {
- $taxValue = 0;
- }
- $orderTax = new \Core\App\Entity\OrderItem();
- $orderTax->setFacturiProdNume($this->stripSpecialChars($product_name));
- $orderTax->setFacturiProdMoneda($currency_code);
- $orderTax->setFacturiProdPretftva($price);
- $orderTax->setFacturiProdPretctva($price + $tax);
- $orderTax->setFacturiProdTva($taxValue . '%');
- $orderTax->setFacturiProdCant(1);
- $orderTax->setFacturiProdVal($price);
- $orderTax->setFacturiProdValTva($tax);
- $orderTax->setFacturiProdValTot($price + $tax);
- $orderTax->setProdCodCautare('all');
-
- return $orderTax;
-
- }
- protected function getPretftva($price, $taxRate, $tax, $taxIncluded)
- {
- if($taxIncluded)
- {
- if($tax != 0)
- {
- return ($price - $tax);
- }
- else
- {
- return ($price / (1 + ($taxRate/100)));
- }
- }
- else
- {
- return $price;
- }
- }
- protected function getPretctva($price, $taxRate, $tax, $taxIncluded)
- {
- if(!$taxIncluded)
- {
- if($tax != 0)
- {
- return ($price + $tax);
- }
- else
- {
- return ($price * (1 + ($taxRate/100)));
- }
- }
- else
- {
- return $price;
- }
- }
- public function setRemoteProducts($remoteProducts)
- {
- $this->remoteProducts = $remoteProducts;
- }
-
- public function setLocalProducts($localProducts)
- {
- $this->localProducts = $localProducts;
- }
- public function setWithDiscountOption($withDiscountOption)
- {
- $this->withDiscountOption = $withDiscountOption;
- }
- public function calculatePrice($price, $product_name, $ean, $sku, $prod_cod1)
- {
- if($this->withDiscountOption == \Core\App\Factory\SettingsFactory::SYNC_ENABLED)
- {
- return $price;
- }
- if (!empty($product_name) && in_array($product_name, array_column($this->localProducts, 'prod_nume'))) {
- $index = array_search($product_name, array_column($this->localProducts, 'prod_nume'));
- $price = $this->localProducts[$index]['prod_pret_ftva'];
- }
- if (!empty($prod_cod1) && in_array($prod_cod1, array_column($this->localProducts, 'prod_cod1'))) {
- $index = array_search($prod_cod1, array_column($this->localProducts, 'prod_cod1'));
- $price = $this->localProducts[$index]['prod_pret_ftva'];
- }
- if (!empty($sku) && in_array($sku, array_column($this->localProducts, 'prod_sku'))) {
- $index = array_search($sku, array_column($this->localProducts, 'prod_sku'));
- $price = $this->localProducts[$index]['prod_pret_ftva'];
- }
- if (!empty($ean) && in_array($ean, array_column($this->localProducts, 'prod_cod'))) {
- $index = array_search($sku, array_column($this->localProducts, 'prod_cod'));
- $price = $this->localProducts[$index]['prod_pret_ftva'];
- }
- return $price;
- }
-
- public function calculateTax($price, $tax, $taxRate)
- {
- if($this->withDiscountOption == \Core\App\Factory\SettingsFactory::SYNC_ENABLED)
- {
- return $tax;
- }
- return ($price * $tax)/100;
- }
- }
-
|