OrderRepository.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace MarketplaceRepository;
  3. class OrderRepository
  4. extends Repository
  5. implements \Core\App\Repository\Marketplace\OrderInterface
  6. {
  7. public $remoteCustomers;
  8. public $remoteProducts;
  9. public $orderedDaysAgo;
  10. public $withDiscount;
  11. public $localProducts;
  12. public function getAll()
  13. {
  14. $results = array();
  15. $orderFactory = new \Core\App\Factory\OrderFactory();
  16. $orderItemFactory = new \Core\App\Factory\OrderItemFactory();
  17. $orderItemFactory->setPrefix(self::PREFIX);
  18. $orderItemFactory->setRemoteProducts($this->remoteProducts);
  19. $orderItemFactory->setLocalProducts($this->localProducts);
  20. $orderItemFactory->setWithDiscountOption($this->withDiscount);
  21. $orders = $this->getOrders();
  22. if(!empty($orders))
  23. {
  24. foreach($orders as $order)
  25. {
  26. if($this->isValidOrder($order))
  27. {
  28. $orderObj = $this->getOrder($orderFactory, $order);
  29. $orderProducts = $this->getOrderProducts($orderItemFactory, $order);
  30. $orderProducts = (array) $orderProducts;
  31. $orderTaxes = $this->getOrderTaxes($orderItemFactory, $order);
  32. $orderTaxes = (array) $orderTaxes;
  33. $result = array();
  34. $result['idMarketpalce'] = self::MARKETPLACE_ID;
  35. $result['dataFact'] = (array) $orderObj;
  36. $result['dataProd'] = array_merge($orderProducts, $orderTaxes);
  37. $results[] = $result;
  38. }
  39. }
  40. }
  41. return $results;
  42. }
  43. public function setRemoteCustomers($remoteCustomers)
  44. {
  45. $this->remoteCustomers = $remoteCustomers;
  46. }
  47. public function setRemoteProducts($remoteProducts)
  48. {
  49. $this->remoteProducts = $remoteProducts;
  50. }
  51. public function getOrderProducts($orderItemFactory, $order)
  52. {
  53. $orderProducts = array();
  54. $taxIncluded = $this->getTaxIncluded($order);
  55. if(!empty($order['line_items']))
  56. {
  57. foreach($order['line_items'] as $line_item)
  58. {
  59. $taxRate = $this->getTaxRate($line_item);
  60. $variant_id = $this->getVariantId($line_item['variant_id']);
  61. $orderProductObj = $orderItemFactory->createOrderProduct(
  62. $line_item['id'] . $variant_id,
  63. $line_item['name'],
  64. '',//ean
  65. $line_item['sku'],
  66. $order['currency'],
  67. $line_item['price'],
  68. $taxRate,
  69. $line_item['quantity'],
  70. 0,
  71. $taxIncluded
  72. );
  73. $orderProducts = array_merge($orderProducts, $orderProductObj);
  74. }
  75. }
  76. return $orderProducts;
  77. }
  78. public function getOrderTaxes($orderItemFactory, $order)
  79. {
  80. $orderTaxes = array();
  81. $taxRate = (isset($order['tax_lines'][0]['rate']) ? $order['tax_lines'][0]['rate'] : 0);
  82. if(
  83. (isset($order['total_discounts_set']['shop_money']['amount'])) &&
  84. (!empty((int)$order['total_discounts_set']['shop_money']['amount']) || (self::SHOW_EMPTY_TAX))
  85. )
  86. {
  87. $orderTaxObj = $orderItemFactory->createOrderTax(
  88. 'Discount',
  89. $order['currency'],
  90. (-1) * $order['total_discounts_set']['shop_money']['amount'],
  91. (-1) * ($taxRate * $order['total_discounts_set']['shop_money']['amount'])
  92. );
  93. $orderTaxes[] = (array) $orderTaxObj;
  94. }
  95. if(
  96. (isset($order['total_shipping_price_set']['shop_money']['amount'])) &&
  97. (!empty((int)$order['total_shipping_price_set']['shop_money']['amount']) || (self::SHOW_EMPTY_TAX))
  98. )
  99. {
  100. $orderTaxObj = $orderItemFactory->createOrderTax(
  101. 'Taxa de livrare',
  102. $order['currency'],
  103. $order['total_shipping_price_set']['shop_money']['amount'],
  104. $taxRate * $order['total_shipping_price_set']['shop_money']['amount']
  105. );
  106. $orderTaxes[] = (array) $orderTaxObj;
  107. }
  108. return $orderTaxes;
  109. }
  110. protected function getTaxIncluded($order)
  111. {
  112. if($order['taxes_included'] == 1)
  113. {
  114. return true;
  115. }
  116. else
  117. {
  118. return false;
  119. }
  120. }
  121. public function getCodF($billing_address_company)
  122. {
  123. $billing_address_company = $this->stripSpecialChars($billing_address_company);
  124. if(
  125. !empty($billing_address_company) &&
  126. in_array(trim($billing_address_company), array_column($this->remoteCustomers, 'Denumire'))
  127. )
  128. {
  129. $index = array_search($billing_address_company, array_column($this->remoteCustomers, 'Denumire'));
  130. return $this->remoteCustomers[$index]['CUI_CNP'];
  131. }
  132. return '';
  133. }
  134. protected function getPaymentMethod($order)
  135. {
  136. if(isset($order['payment_gateway_names']) && !empty($order['payment_gateway_names']))
  137. {
  138. return implode(', ', $order['payment_gateway_names']);
  139. }
  140. else
  141. {
  142. if(isset($order['gateway']))
  143. {
  144. return $order['gateway'];
  145. }
  146. }
  147. return '';
  148. }
  149. public function setWithDiscount($withDiscount)
  150. {
  151. $this->withDiscount = $withDiscount;
  152. }
  153. public function setLocalProducts($localProducts)
  154. {
  155. if(!empty($localProducts))
  156. {
  157. foreach($localProducts as $localProduct)
  158. {
  159. $this->localProducts[] = (array) $localProduct;
  160. }
  161. }
  162. else
  163. {
  164. $this->localProducts = array();
  165. }
  166. }
  167. protected function getTaxRate($line_item)
  168. {
  169. $taxRate = 0;
  170. if(isset($line_item['tax_lines']) && !empty($line_item['tax_lines']) && ($line_item['taxable'] == 1))
  171. {
  172. foreach($line_item['tax_lines'] as $tax_line)
  173. {
  174. if(isset($tax_line['rate']))
  175. {
  176. $taxRate = $tax_line['rate'];
  177. }
  178. }
  179. }
  180. return $taxRate;
  181. }
  182. public function getOrder($orderFactory, $order)
  183. {
  184. $orderObj = $orderFactory->createOrder(
  185. isset($order['order_number']) ? $order['order_number']: '',
  186. isset($order['created_at']) ? $order['created_at'] : '',
  187. isset($order['currency']) ? $order['currency'] : '',
  188. isset($order['customer']['id']) ? $order['customer']['id']: '',
  189. isset($order['customer']['first_name']) ? $order['customer']['first_name'] : '',
  190. isset($order['customer']['last_name']) ? $order['customer']['last_name'] : '',
  191. isset($order['billing_address']['company']) ? $order['billing_address']['company'] : '',
  192. isset($order['email']) ? $order['email'] : '',
  193. isset($order['billing_address']['first_name']) ? $order['billing_address']['first_name'] : '',
  194. isset($order['billing_address']['last_name']) ? $order['billing_address']['last_name'] : '',
  195. isset($order['billing_address']['address1']) ? $order['billing_address']['address1'] : '',
  196. isset($order['billing_address']['address2']) ? $order['billing_address']['address2'] : '',
  197. isset($order['billing_address']['zip']) ? $order['billing_address']['zip'] : '',
  198. isset($order['billing_address']['city']) ? $order['billing_address']['city'] : '',
  199. isset($order['billing_address']['province']) ? $order['billing_address']['province'] : '',
  200. isset($order['billing_address']['phone']) ? $order['billing_address']['phone'] : '',
  201. isset($order['shipping_address']['first_name']) ? $order['shipping_address']['first_name'] : '',
  202. isset($order['shipping_address']['last_name']) ? $order['shipping_address']['last_name'] : '',
  203. isset($order['shipping_address']['address1']) ? $order['shipping_address']['address1'] : '',
  204. isset($order['shipping_address']['address2']) ? $order['shipping_address']['address2'] : '',
  205. isset($order['shipping_address']['zip']) ? $order['shipping_address']['zip'] : '',
  206. isset($order['shipping_address']['city']) ? $order['shipping_address']['city'] : '',
  207. isset($order['shipping_address']['province']) ? $order['shipping_address']['province'] : '',
  208. isset($order['shipping_address']['phone']) ? $order['shipping_address']['phone'] : '',
  209. isset($order['financial_status']) ? $order['financial_status'] : '',
  210. $this->getPaymentMethod($order),
  211. '',//comentarii
  212. $this->getCodF((isset($order['billing_address']['company']) ? $order['billing_address']['company'] : '')),//cod fiscal
  213. '',//denumire firma - camp custom
  214. ''//nr de inreg
  215. );
  216. return $orderObj;
  217. }
  218. public function isValidOrder($order)
  219. {
  220. $date1 = date('Y-m-d', strtotime('-' . (int) $this->orderedDaysAgo . ' days'));
  221. $date2 = date('Y-m-d', strtotime($order['created_at']));
  222. $dateTimestamp1 = strtotime($date1);
  223. $dateTimestamp2 = strtotime($date2);
  224. if ($dateTimestamp1 <= $dateTimestamp2)
  225. return true;
  226. else
  227. return false;
  228. }
  229. public function setOrderedDaysAgo($orderedDaysAgo)
  230. {
  231. $this->orderedDaysAgo = $orderedDaysAgo;
  232. }
  233. public function getVariantId($variant_id)
  234. {
  235. if(!empty($variant_id))
  236. {
  237. return '_' . $variant_id;
  238. }
  239. else
  240. {
  241. return '';
  242. }
  243. }
  244. public function getByOrderId($orderId)
  245. {
  246. return null;
  247. }
  248. }