OrderItemFactory.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace Core\App\Factory;
  3. class OrderItemFactory extends EntityFactory
  4. {
  5. public $remoteProducts;
  6. public $localProducts;
  7. public $withDiscountOption;
  8. public function createOrderProduct(
  9. $product_ids,
  10. $product_name,
  11. $ean,
  12. $sku,
  13. $currency_code,
  14. $price,
  15. $taxRate,
  16. $quantity,
  17. $tax = 0,
  18. $taxIncluded = false
  19. )
  20. {
  21. $orderProduct = new \Core\App\Entity\OrderItem();
  22. $prod_cod1 = $this->getPrefix().$product_ids;
  23. $taxRate = $this->getTaxRate($taxRate) ;
  24. $initialPrice = $price;
  25. $initialTax = $tax;
  26. $price = $this->calculatePrice($price, $product_name, $ean, $sku, $prod_cod1);
  27. $tax = $this->calculateTax($price, $tax, $taxRate);
  28. if(!empty($prod_cod1) && in_array($prod_cod1, array_column($this->remoteProducts, 'Alt_Cod')))
  29. {
  30. $index = array_search($prod_cod1, array_column($this->remoteProducts, 'Alt_Cod'));
  31. }
  32. if(!empty($ean) && in_array($ean, array_column($this->remoteProducts, 'Cod_EAN')))
  33. {
  34. $index = array_search($ean, array_column($this->remoteProducts, 'Cod_EAN'));
  35. }
  36. if(!empty($sku) && in_array($sku, array_column($this->remoteProducts, 'Cod_SKU')))
  37. {
  38. $index = array_search($sku, array_column($this->remoteProducts, 'Cod_SKU'));
  39. }
  40. if(!empty($product_name) && in_array($product_name, array_column($this->remoteProducts, 'Denumire')))
  41. {
  42. $index = array_search($product_name, array_column($this->remoteProducts, 'Denumire'));
  43. }
  44. if(isset($index))
  45. {
  46. $product_name = isset($this->remoteProducts[$index]['Denumire']) ? $this->remoteProducts[$index]['Denumire'] : $product_name;
  47. $ean = isset($this->remoteProducts[$index]['Cod_EAN']) ? $this->remoteProducts[$index]['Cod_EAN'] : $ean;
  48. $sku = isset($this->remoteProducts[$index]['Cod_SKU']) ? $this->remoteProducts[$index]['Cod_SKU'] : $sku;
  49. $prod_cod1 = (isset($this->remoteProducts[$index]['Alt_Cod']) && !empty($this->remoteProducts[$index]['Alt_Cod'])) ? $this->remoteProducts[$index]['Alt_Cod'] : $prod_cod1;
  50. }
  51. $pretFtva = $this->getPretftva($price, $taxRate, $tax, $taxIncluded);
  52. $pretCtva = $this->getPretctva($price, $taxRate, $tax, $taxIncluded);
  53. $orderProduct->setFacturiProdNume($this->stripSpecialChars($product_name));
  54. $orderProduct->setFacturiProdMoneda($currency_code);
  55. $orderProduct->setFacturiProdPretftva($pretFtva);
  56. $orderProduct->setFacturiProdPretctva($pretCtva);
  57. $orderProduct->setFacturiProdTva($this->getTaxName($taxRate));
  58. $orderProduct->setFacturiProdCant($quantity);
  59. $orderProduct->setFacturiProdVal($pretFtva * $quantity);
  60. $orderProduct->setFacturiProdValTva(($pretCtva - $pretFtva) * $quantity);
  61. $orderProduct->setFacturiProdValTot($pretCtva * $quantity);
  62. $orderProduct->setFacturiProdUm(self::UM);
  63. $orderProduct->setProdCod($ean);
  64. $orderProduct->setProdSku($sku);
  65. $orderProduct->setProdCod1($prod_cod1);
  66. $orderProduct->setProdCodCautare('all');
  67. if($this->withDiscountOption == \Core\App\Factory\SettingsFactory::SYNC_ENABLED)
  68. {
  69. return array((array)$orderProduct);
  70. }
  71. else
  72. {
  73. $discount = $initialPrice - $price;
  74. if($discount < 0)
  75. {
  76. $tax = $tax - $initialTax;
  77. $discountFtva = $this->getPretftva($discount, $taxRate, $tax, $taxIncluded);
  78. $discountCtva = $this->getPretctva($discount, $taxRate, $tax, $taxIncluded);
  79. if($tax == 0)
  80. {
  81. $tax = $discountCtva - $discountFtva;
  82. }
  83. $orderProductDiscount = new \Core\App\Entity\OrderItem();
  84. $orderProductDiscount->setFacturiProdNume('Discount la: ' . $this->stripSpecialChars($product_name));
  85. $orderProductDiscount->setFacturiProdMoneda($currency_code);
  86. $orderProductDiscount->setFacturiProdPretftva($discountFtva);
  87. $orderProductDiscount->setFacturiProdPretctva($discountCtva);
  88. $orderProductDiscount->setFacturiProdTva($taxRate . '%');
  89. $orderProductDiscount->setFacturiProdCant($quantity);
  90. $orderProductDiscount->setFacturiProdVal($discountFtva * $quantity);
  91. $orderProductDiscount->setFacturiProdValTva($tax * $quantity);
  92. $orderProductDiscount->setFacturiProdValTot($discountCtva * $quantity);
  93. $orderProductDiscount->setProdCodCautare('all');
  94. return array((array)$orderProduct, (array)$orderProductDiscount);
  95. }
  96. else
  97. {
  98. return array((array)$orderProduct);
  99. }
  100. }
  101. }
  102. public function createOrderTax(
  103. $product_name,
  104. $currency_code,
  105. $price,
  106. $tax = 0
  107. )
  108. {
  109. if($price != 0)
  110. {
  111. $taxValue = round(($tax * 100)/$price);
  112. }
  113. else
  114. {
  115. $taxValue = 0;
  116. }
  117. $orderTax = new \Core\App\Entity\OrderItem();
  118. $orderTax->setFacturiProdNume($this->stripSpecialChars($product_name));
  119. $orderTax->setFacturiProdMoneda($currency_code);
  120. $orderTax->setFacturiProdPretftva($price);
  121. $orderTax->setFacturiProdPretctva($price + $tax);
  122. $orderTax->setFacturiProdTva($taxValue . '%');
  123. $orderTax->setFacturiProdCant(1);
  124. $orderTax->setFacturiProdVal($price);
  125. $orderTax->setFacturiProdValTva($tax);
  126. $orderTax->setFacturiProdValTot($price + $tax);
  127. $orderTax->setProdCodCautare('all');
  128. return $orderTax;
  129. }
  130. protected function getPretftva($price, $taxRate, $tax, $taxIncluded)
  131. {
  132. if($taxIncluded)
  133. {
  134. if($tax != 0)
  135. {
  136. return ($price - $tax);
  137. }
  138. else
  139. {
  140. return ($price / (1 + ($taxRate/100)));
  141. }
  142. }
  143. else
  144. {
  145. return $price;
  146. }
  147. }
  148. protected function getPretctva($price, $taxRate, $tax, $taxIncluded)
  149. {
  150. if(!$taxIncluded)
  151. {
  152. if($tax != 0)
  153. {
  154. return ($price + $tax);
  155. }
  156. else
  157. {
  158. return ($price * (1 + ($taxRate/100)));
  159. }
  160. }
  161. else
  162. {
  163. return $price;
  164. }
  165. }
  166. public function setRemoteProducts($remoteProducts)
  167. {
  168. $this->remoteProducts = $remoteProducts;
  169. }
  170. public function setLocalProducts($localProducts)
  171. {
  172. $this->localProducts = $localProducts;
  173. }
  174. public function setWithDiscountOption($withDiscountOption)
  175. {
  176. $this->withDiscountOption = $withDiscountOption;
  177. }
  178. public function calculatePrice($price, $product_name, $ean, $sku, $prod_cod1)
  179. {
  180. if($this->withDiscountOption == \Core\App\Factory\SettingsFactory::SYNC_ENABLED)
  181. {
  182. return $price;
  183. }
  184. if (!empty($product_name) && in_array($product_name, array_column($this->localProducts, 'prod_nume'))) {
  185. $index = array_search($product_name, array_column($this->localProducts, 'prod_nume'));
  186. $price = $this->localProducts[$index]['prod_pret_ftva'];
  187. }
  188. if (!empty($prod_cod1) && in_array($prod_cod1, array_column($this->localProducts, 'prod_cod1'))) {
  189. $index = array_search($prod_cod1, array_column($this->localProducts, 'prod_cod1'));
  190. $price = $this->localProducts[$index]['prod_pret_ftva'];
  191. }
  192. if (!empty($sku) && in_array($sku, array_column($this->localProducts, 'prod_sku'))) {
  193. $index = array_search($sku, array_column($this->localProducts, 'prod_sku'));
  194. $price = $this->localProducts[$index]['prod_pret_ftva'];
  195. }
  196. if (!empty($ean) && in_array($ean, array_column($this->localProducts, 'prod_cod'))) {
  197. $index = array_search($sku, array_column($this->localProducts, 'prod_cod'));
  198. $price = $this->localProducts[$index]['prod_pret_ftva'];
  199. }
  200. return $price;
  201. }
  202. public function calculateTax($price, $tax, $taxRate)
  203. {
  204. if($this->withDiscountOption == \Core\App\Factory\SettingsFactory::SYNC_ENABLED)
  205. {
  206. return $tax;
  207. }
  208. return ($price * $tax)/100;
  209. }
  210. }