123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- namespace MarketplaceRepository;
- class OrderRepository
- extends Repository
- implements \Core\App\Repository\Marketplace\OrderInterface
- {
- public $remoteCustomers;
- public $remoteProducts;
- public $orderedDaysAgo;
- public $withDiscount;
- public $localProducts;
- public function getAll()
- {
- $results = array();
- $orderFactory = new \Core\App\Factory\OrderFactory();
- $orderItemFactory = new \Core\App\Factory\OrderItemFactory();
- $orderItemFactory->setPrefix(self::PREFIX);
- $orderItemFactory->setRemoteProducts($this->remoteProducts);
- $orderItemFactory->setLocalProducts($this->localProducts);
- $orderItemFactory->setWithDiscountOption($this->withDiscount);
- $orders = $this->getOrders();
- if(!empty($orders))
- {
- foreach($orders as $order)
- {
- if($this->isValidOrder($order))
- {
- $orderObj = $this->getOrder($orderFactory, $order);
- $orderProducts = $this->getOrderProducts($orderItemFactory, $order);
- $orderProducts = (array) $orderProducts;
- $orderTaxes = $this->getOrderTaxes($orderItemFactory, $order);
- $orderTaxes = (array) $orderTaxes;
- $result = array();
- $result['idMarketpalce'] = self::MARKETPLACE_ID;
- $result['dataFact'] = (array) $orderObj;
- $result['dataProd'] = array_merge($orderProducts, $orderTaxes);
- $results[] = $result;
- }
-
- }
- }
- return $results;
- }
- public function setRemoteCustomers($remoteCustomers)
- {
- $this->remoteCustomers = $remoteCustomers;
- }
- public function setRemoteProducts($remoteProducts)
- {
- $this->remoteProducts = $remoteProducts;
- }
- public function getOrderProducts($orderItemFactory, $order)
- {
- $orderProducts = array();
- $taxIncluded = $this->getTaxIncluded($order);
- if(!empty($order['line_items']))
- {
- foreach($order['line_items'] as $line_item)
- {
- $taxRate = $this->getTaxRate($line_item);
- $variant_id = $this->getVariantId($line_item['variant_id']);
- $orderProductObj = $orderItemFactory->createOrderProduct(
- $line_item['id'] . $variant_id,
- $line_item['name'],
- '',//ean
- $line_item['sku'],
-
- $order['currency'],
- $line_item['price'],
- $taxRate,
- $line_item['quantity'],
- 0,
- $taxIncluded
- );
- $orderProducts = array_merge($orderProducts, $orderProductObj);
- }
- }
- return $orderProducts;
- }
- public function getOrderTaxes($orderItemFactory, $order)
- {
- $orderTaxes = array();
- $taxRate = (isset($order['tax_lines'][0]['rate']) ? $order['tax_lines'][0]['rate'] : 0);
- if(
- (isset($order['total_discounts_set']['shop_money']['amount'])) &&
- (!empty((int)$order['total_discounts_set']['shop_money']['amount']) || (self::SHOW_EMPTY_TAX))
- )
- {
- $orderTaxObj = $orderItemFactory->createOrderTax(
- 'Discount',
- $order['currency'],
- (-1) * $order['total_discounts_set']['shop_money']['amount'],
- (-1) * ($taxRate * $order['total_discounts_set']['shop_money']['amount'])
- );
- $orderTaxes[] = (array) $orderTaxObj;
- }
- if(
- (isset($order['total_shipping_price_set']['shop_money']['amount'])) &&
- (!empty((int)$order['total_shipping_price_set']['shop_money']['amount']) || (self::SHOW_EMPTY_TAX))
- )
- {
- $orderTaxObj = $orderItemFactory->createOrderTax(
- 'Taxa de livrare',
- $order['currency'],
- $order['total_shipping_price_set']['shop_money']['amount'],
- $taxRate * $order['total_shipping_price_set']['shop_money']['amount']
- );
-
- $orderTaxes[] = (array) $orderTaxObj;
- }
- return $orderTaxes;
- }
- protected function getTaxIncluded($order)
- {
- if($order['taxes_included'] == 1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public function getCodF($billing_address_company)
- {
- $billing_address_company = $this->stripSpecialChars($billing_address_company);
-
- if(
- !empty($billing_address_company) &&
- in_array(trim($billing_address_company), array_column($this->remoteCustomers, 'Denumire'))
- )
- {
- $index = array_search($billing_address_company, array_column($this->remoteCustomers, 'Denumire'));
- return $this->remoteCustomers[$index]['CUI_CNP'];
- }
- return '';
- }
-
- protected function getPaymentMethod($order)
- {
- if(isset($order['payment_gateway_names']) && !empty($order['payment_gateway_names']))
- {
- return implode(', ', $order['payment_gateway_names']);
- }
- else
- {
- if(isset($order['gateway']))
- {
- return $order['gateway'];
- }
- }
- return '';
- }
- public function setWithDiscount($withDiscount)
- {
- $this->withDiscount = $withDiscount;
- }
-
- public function setLocalProducts($localProducts)
- {
- if(!empty($localProducts))
- {
- foreach($localProducts as $localProduct)
- {
- $this->localProducts[] = (array) $localProduct;
- }
- }
- else
- {
- $this->localProducts = array();
- }
- }
- protected function getTaxRate($line_item)
- {
- $taxRate = 0;
- if(isset($line_item['tax_lines']) && !empty($line_item['tax_lines']) && ($line_item['taxable'] == 1))
- {
- foreach($line_item['tax_lines'] as $tax_line)
- {
- if(isset($tax_line['rate']))
- {
- $taxRate = $tax_line['rate'];
- }
- }
- }
- return $taxRate;
- }
- public function getOrder($orderFactory, $order)
- {
- $orderObj = $orderFactory->createOrder(
- isset($order['order_number']) ? $order['order_number']: '',
- isset($order['created_at']) ? $order['created_at'] : '',
- isset($order['currency']) ? $order['currency'] : '',
- isset($order['customer']['id']) ? $order['customer']['id']: '',
- isset($order['customer']['first_name']) ? $order['customer']['first_name'] : '',
- isset($order['customer']['last_name']) ? $order['customer']['last_name'] : '',
- isset($order['billing_address']['company']) ? $order['billing_address']['company'] : '',
- isset($order['email']) ? $order['email'] : '',
- isset($order['billing_address']['first_name']) ? $order['billing_address']['first_name'] : '',
- isset($order['billing_address']['last_name']) ? $order['billing_address']['last_name'] : '',
- isset($order['billing_address']['address1']) ? $order['billing_address']['address1'] : '',
- isset($order['billing_address']['address2']) ? $order['billing_address']['address2'] : '',
- isset($order['billing_address']['zip']) ? $order['billing_address']['zip'] : '',
- isset($order['billing_address']['city']) ? $order['billing_address']['city'] : '',
- isset($order['billing_address']['province']) ? $order['billing_address']['province'] : '',
- isset($order['billing_address']['phone']) ? $order['billing_address']['phone'] : '',
-
- isset($order['shipping_address']['first_name']) ? $order['shipping_address']['first_name'] : '',
- isset($order['shipping_address']['last_name']) ? $order['shipping_address']['last_name'] : '',
- isset($order['shipping_address']['address1']) ? $order['shipping_address']['address1'] : '',
- isset($order['shipping_address']['address2']) ? $order['shipping_address']['address2'] : '',
- isset($order['shipping_address']['zip']) ? $order['shipping_address']['zip'] : '',
- isset($order['shipping_address']['city']) ? $order['shipping_address']['city'] : '',
- isset($order['shipping_address']['province']) ? $order['shipping_address']['province'] : '',
- isset($order['shipping_address']['phone']) ? $order['shipping_address']['phone'] : '',
- isset($order['financial_status']) ? $order['financial_status'] : '',
- $this->getPaymentMethod($order),
- '',//comentarii
- $this->getCodF((isset($order['billing_address']['company']) ? $order['billing_address']['company'] : '')),//cod fiscal
- '',//denumire firma - camp custom
- ''//nr de inreg
- );
- return $orderObj;
- }
- public function isValidOrder($order)
- {
- $date1 = date('Y-m-d', strtotime('-' . (int) $this->orderedDaysAgo . ' days'));
- $date2 = date('Y-m-d', strtotime($order['created_at']));
- $dateTimestamp1 = strtotime($date1);
- $dateTimestamp2 = strtotime($date2);
- if ($dateTimestamp1 <= $dateTimestamp2)
- return true;
- else
- return false;
- }
- public function setOrderedDaysAgo($orderedDaysAgo)
- {
- $this->orderedDaysAgo = $orderedDaysAgo;
- }
- public function getVariantId($variant_id)
- {
- if(!empty($variant_id))
- {
- return '_' . $variant_id;
- }
- else
- {
- return '';
- }
- }
- public function getByOrderId($orderId)
- {
- return null;
- }
- }
|