Facebook
From Mungo Butterfly, 5 Years ago, written in PHP.
Embed
Download Paste or View Raw
Hits: 239
  1. <?php
  2. namespace Network\Restext\Model;
  3. use Network\Restext\Api\OrdersInterface;
  4.  
  5. class GetOrders extends \Magento\Sales\Model\Order implements OrdersInterface
  6. {
  7.  
  8.     /**
  9.      * Pobiera zamówienia do SAP
  10.      *
  11.      * @param $id
  12.      * @return string
  13.      *
  14.      */
  15. //    public function getOrders($id)
  16. //    {
  17. //        $orders = $this->getOrdersCollection($id);
  18. //        $lastId = $id;
  19. //
  20. //        $allOrdersData = [];
  21. //        foreach ($orders as $order){
  22. //            $allOrdersData[] = $this->parseOrderToData($order);
  23. //            $lastId = $order->getIncrementId();
  24. //        }
  25. //
  26. //        $array = [
  27. //            'data' => $allOrdersData,
  28. //            'lastorder' => $lastId
  29. //        ];
  30. //
  31. //        return json_encode($array, JSON_UNESCAPED_SLASHES);
  32. //    }
  33.  
  34.     public function getOrders($id)
  35.     {
  36.         $orders = $this->getCollection()->addAttributeToFilter('send_sap', ['null' => true]);
  37.         $lastId = $id;
  38.  
  39.         $allOrdersData = [];
  40.         foreach ($orders as $order){
  41.             if($order->getPayment()->getMethodInstance()->getCode() == 'cashbill') {
  42.                 if(!$order->getCashbillTransactionId())
  43.                     continue;
  44.             }
  45.  
  46.             $allOrdersData[] = $this->parseOrderToDataTestNew($order);
  47.             $lastId = $order->getIncrementId();
  48.             $order->setSendSap(1)->save();
  49.         }
  50.  
  51.         $array = [
  52.             'data' => $allOrdersData,
  53.             'lastorder' => $lastId
  54.         ];
  55.  
  56.         return json_encode($array, JSON_UNESCAPED_SLASHES);
  57.     }
  58.  
  59.     protected function getOrdersCollection($ordersFrom)
  60.     {
  61.         $collection = $this->getCollection()->addAttributeToFilter('increment_id', ['gt' => $ordersFrom]);
  62.         return $collection;
  63.     }
  64.  
  65.     protected function parseOrderToData($order)
  66.     {
  67.         $data = $order->getData();// array_filter();
  68.         $orderId = $data['increment_id'];
  69.         $saveData = [];
  70.         foreach ($data as $key => $value) {
  71.             $saveData[$key] = $value;
  72.         }
  73.  
  74.         if(isset($data['punktodbioru']))
  75.             $saveData['punktodbioru'] = $data['punktodbioru'];
  76.  
  77.         $saveData['billing_address'] = array_filter($order->getBillingAddress()->getData());
  78.         $saveData['shipping_address'] = array_filter($order->getShippingAddress()->getData());
  79.  
  80.         $saveData['payment'] = $order->getPayment()->getMethodInstance()->getCode();
  81.  
  82.         $items = $order->getAllItems();
  83.         foreach ($items as $item){
  84.             $itemData = $item->getData();
  85.             //$stany = [];
  86.  
  87.  
  88.  
  89.             if($itemData['ilosc_do_domu'] || $itemData['ilosc_do_doslania_do_ksiegarni']) {
  90.                 //$stany[] = 'stan01GLOWN';
  91.                 $ilosc = (int)$itemData['ilosc_do_domu'] + (int)$itemData['ilosc_do_doslania_do_ksiegarni'];
  92.  
  93.                 $saveData['items'][] = [
  94.                     'sku' => $itemData['sku'],
  95.                     'name' => $itemData['name'],
  96.                     'price' => $itemData['price_incl_tax'],
  97.                     'qty_ordered' => $ilosc,
  98.                     'discount_amount' => $itemData['discount_amount'],
  99.                     'stocks' => 'stan01GLOWN'
  100.                 ];
  101.  
  102.             }elseif($itemData['ilosc_do_odbioru_w_ksiegarni']) {
  103.                 //$stany[] = $itemData['ksiegarnia'];
  104.  
  105.                 $saveData['items'][] = [
  106.                     'sku' => $itemData['sku'],
  107.                     'name' => $itemData['name'],
  108.                     'price' => $itemData['price_incl_tax'],
  109.                     'qty_ordered' => (int)$itemData['ilosc_do_odbioru_w_ksiegarni'],
  110.                     'discount_amount' => $itemData['discount_amount'],
  111.                     'stocks' => $itemData['ksiegarnia']
  112.                 ];
  113.             }else {
  114.                 $saveData['items'][] = [
  115.                     'sku' => $itemData['sku'],
  116.                     'name' => $itemData['name'],
  117.                     'price' => $itemData['price_incl_tax'],
  118.                     'qty_ordered' => (int)$itemData['qty_ordered'],
  119.                     'discount_amount' => $itemData['discount_amount'],
  120.                     'stocks' => 'stan01GLOWN'
  121.                 ];
  122.             }
  123.  
  124.  
  125.  
  126.  
  127. //            $saveData['items'][] = [
  128. //                'sku' => $itemData['sku'],
  129. //                'name' => $itemData['name'],
  130. //                'price' => $itemData['price'],
  131. //                'qty_ordered' => $itemData['qty_ordered'],
  132. //                'discount_amount' => $itemData['discount_amount'],
  133. //                'stocks' => implode(',', $stany)
  134. //            ];
  135.         }
  136.         return $saveData;
  137.     }
  138.  
  139.     public function parseOrderToDataTestNew($order)
  140.     {
  141.         $data = $order->getData();// array_filter();
  142.         $orderId = $data['increment_id'];
  143.         $saveData = [];
  144.         $v_total_qty = 0;
  145.         $v_all_item_qty = 0;
  146.         foreach ($data as $key => $value) {
  147.             $saveData[$key] = $value;
  148.         }
  149.  
  150.         if(isset($data['punktodbioru']))
  151.             $saveData['punktodbioru'] = $data['punktodbioru'];
  152.  
  153.         $saveData['billing_address'] = array_filter($order->getBillingAddress()->getData());
  154.         $saveData['shipping_address'] = array_filter($order->getShippingAddress()->getData());
  155.  
  156.         $saveData['payment'] = $order->getPayment()->getMethodInstance()->getCode();
  157.  
  158.         $items = $order->getAllItems();
  159.         foreach ($items as $item){
  160.             $itemData = $item->getData();
  161.  
  162.             $sum = (int)$itemData['ilosc_do_domu'] + (int)$itemData['ilosc_do_doslania_do_ksiegarni'] + (int)$itemData['ilosc_do_odbioru_w_ksiegarni'];
  163.             if($itemData['qty_ordered'] != ($sum)){
  164.                 mail("[email protected]","błąd zamówienia w bookland {$order->getIncrementId()}","błąd lub nieprawidłowe ilości w zamówieniu {$order->getIncrementId()} pozycja {$item->getSku()}\n");
  165.             }
  166.  
  167.  
  168.             $price = $itemData['price_incl_tax'];
  169.             $priceWithOutTax = $itemData['price'];
  170.             $discount = $itemData['discount_amount'];
  171.             $tax = $itemData['tax_percent'];
  172.             $tax2 = 1 + ($tax/100);                     //eq 1,05
  173.             $discountWithoutVat = $discount / $tax2;
  174.  
  175.  
  176.             if($itemData['ilosc_do_domu'] || $itemData['ilosc_do_doslania_do_ksiegarni']) {
  177.                 $ilosc = (int)$itemData['ilosc_do_domu'] + (int)$itemData['ilosc_do_doslania_do_ksiegarni'];
  178.  
  179.                 $saveData['items'][] = [
  180.                     'sku' => $itemData['sku'],
  181.                     'name' => $itemData['name'],
  182.                     'price' => $itemData['price_incl_tax'],
  183.                     'qty_ordered' => $ilosc,
  184.                     'discount_amount' => $itemData['discount_amount'],
  185.                     'stocks' => 'stan01GLOWN',
  186.                     'orderline_total' => (float)$itemData['row_total'] - $discountWithoutVat,
  187.                     'orderline_totalAfterVAT' => (float)$itemData['row_total_incl_tax'] - (float)$itemData['discount_amount'],
  188.                 ];
  189.  
  190.             }elseif($itemData['ilosc_do_odbioru_w_ksiegarni']) {
  191.                 $ilosc = (int)$itemData['ilosc_do_odbioru_w_ksiegarni'];
  192.  
  193.                 $saveData['items'][] = [
  194.                     'sku' => $itemData['sku'],
  195.                     'name' => $itemData['name'],
  196.                     'price' => $itemData['price_incl_tax'],
  197.                     'qty_ordered' => $ilosc,
  198.                     'discount_amount' => $itemData['discount_amount'],
  199.                     'stocks' => $itemData['ksiegarnia'],
  200.                     'orderline_total' => (float)$itemData['row_total'] - $discountWithoutVat,
  201.                     'orderline_totalAfterVAT' => (float)$itemData['row_total_incl_tax'] - (float)$itemData['discount_amount'],
  202.                 ];
  203.             }else {
  204.                 $ilosc = (int)$itemData['qty_ordered'];
  205.  
  206.                 $saveData['items'][] = [
  207.                     'sku' => $itemData['sku'],
  208.                     'name' => $itemData['name'],
  209.                     'price' => $itemData['price_incl_tax'],
  210.                     'qty_ordered' => (int)$itemData['qty_ordered'],
  211.                     'discount_amount' => $itemData['discount_amount'],
  212.                     'stocks' => 'stan01GLOWN',
  213.                     'orderline_total' => (float)$itemData['row_total'] - $discountWithoutVat,
  214.                     'orderline_totalAfterVAT' => (float)$itemData['row_total_incl_tax'] - (float)$itemData['discount_amount'],
  215.                 ];
  216.             }
  217.  
  218.  
  219.  
  220.         }
  221.         return $saveData;
  222.     }
  223.  
  224.  
  225.  
  226. }