Advertisement
Guest User

Untitled

a guest
May 30th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.86 KB | None | 0 0
  1. <?php
  2. App::uses('AppModel', 'Model');
  3. App::uses('CakeEmail', 'Network/Email');
  4. App::uses('Common', 'Lib');
  5. App::uses('Ftp', 'Lib');
  6. App::uses('BarcodeHelper', 'Vendor');
  7. App::uses('BarcodeUtils', 'Utility');
  8.  
  9. class InTime extends AppModel {
  10.     public $useTable = false;
  11.     const API_URL = 'http://zasilky.intime.cz/test/xml_server.php';
  12.     const API_USER = 'ageo';
  13.     const API_PASSWORD = 'ageo123';
  14.  
  15.     protected function getBaseXPosForLabel($posInPage) {
  16.         return $this->getBasePosForLabel($posInPage)['baseX'];
  17.     }
  18.  
  19.     protected function getBaseYPosForLabel($posInPage) {
  20.         return $this->getBasePosForLabel($posInPage)['baseY'];
  21.     }
  22.  
  23.     protected function getBasePosForLabel($posInPage) {
  24.         $baseX = 0;
  25.         $baseY = 0;
  26.         if ($posInPage == 1) {
  27.             $baseX = 106;
  28.         } elseif ($posInPage == 2) {
  29.             $baseY = 149;
  30.         } elseif ($posInPage == 3) {
  31.             $baseX = 106;
  32.             $baseY = 149;
  33.         }
  34.         return ['baseX' => $baseX, 'baseY' => $baseY];
  35.     }
  36.  
  37.     protected function addArticleToAZ($order) {
  38.         $cash_on_delivery = '';
  39.         if ($order['Order']['payment_type_id'] == PaymentsType::PAYMENT_TYPE_CASH) {
  40.             $cash_on_delivery = $order['Order']['total_amount_with_shipping'];
  41.             $cash_on_delivery = str_replace('.', ',', $cash_on_delivery);
  42.         }
  43.         $firstname = Common::transliterateString($order['Shipping']['firstname']);
  44.         $surname = Common::transliterateString($order['Shipping']['lastname']);
  45.         $street = Common::transliterateString($order['Shipping']['street']);
  46.         $city = Common::transliterateString($order['Shipping']['city']);
  47.         $phone = preg_replace('/[\+]/', '00', $order['Shipping']['phone']);
  48.         $phone = str_replace(' ', '', $phone);
  49.         /* weight needs to be <weight>'.$order['OrdersStockProperty']['weight'].'</weight> */
  50.         $input_xml = '<?xml version="1.0" encoding="utf-8"?>
  51.                <request name="import_article">
  52.                <auth username="'.self::API_USER.'" password="'.self::API_PASSWORD.'"/>
  53.                <option name="transaction" value="no"/>
  54.                <option name="auto_complete" value="no"/>
  55.                <option name="customer" value="427"/>
  56.                <option name="department" value="796"/>
  57.                <article>
  58.                    <receiver>
  59.                        <external_id>'.$order['Order']['id'].'</external_id>
  60.                        <name>IN TIME spedice</name>
  61.                        <street>'.$street.'</street>
  62.                        <city>'.$city.'</city>
  63.                        <postal_code>'.$order['Shipping']['code'].'</postal_code>
  64.                        <state>CZ</state>
  65.                        <firstname>'.$firstname.'</firstname>
  66.                        <surname>'.$surname.'</surname>
  67.                        <email>'.$order['User']['email'].'</email>                        
  68.                    </receiver>
  69.                    <sender>
  70.                        <name>E-AGEO.cz, s. r. o.</name>
  71.                        <street>Do Certous 2635 Hala D1</street>
  72.                        <city>Praha</city>
  73.                        <postal_code>193 00</postal_code>
  74.                        <state>CZ</state>
  75.                        <email>orders@ageo.cz</email>
  76.                    </sender>
  77.                    <reference_number>'.$order['Order']['id'].'</reference_number>
  78.                    <package_count>'.$order['OrdersStockProperty']['packages_quantity'].'</package_count>
  79.                    <additional_service name="direct_order" value="yes" />
  80.                    <additional_service name="email_notification" value="yes" />
  81.                    <additional_service name="email_notification_address_direct" value="'.$order['User']['email'].'" />
  82.                    <additional_service name="cash_on_delivery" value="'.$cash_on_delivery.'" />
  83.                    <additional_service name="sms_notification" value="yes" />
  84.                    <additional_service name="sms_notification_number" value="'.$phone.'" />
  85.                    <weight>1</weight>
  86.                    <value>1000</value>
  87.                    <comment>prosim co nejrychleji</comment>
  88.                    <additive>yes</additive>
  89.                </article>
  90.            </request>';
  91.         $response = $this->sendRequestToAZ($input_xml);
  92.         $intime_articles_model = ClassRegistry::init('IntimeArticle');
  93.         $intime_articles_model->create();
  94.         $intime_articles_model->save([
  95.             'order_id' => $order['Order']['id'],
  96.             'order_number' => $response['article']['order_number'],
  97.             'batch_id' => $response['batch']['id'],
  98.             'batch_number' => $response['batch']['number'],
  99.             'product_name' => $response['article']['product_name'],
  100.             'reference_number' => $response['article']['reference_number'],
  101.             'sorting_code' => $response['article']['sorting_code']
  102.         ]);
  103.         return $response;
  104.     }
  105.  
  106.     protected function addPackageToAZ($current_article_data, $count) {
  107.         $input_xml = '<?xml version="1.0" encoding="utf-8"?>
  108.            <request name="article_add_package">
  109.                <auth username="'.self::API_USER.'" password="'.self::API_PASSWORD.'"/>
  110.                <option name="transaction" value="yes" />
  111.                <option name="zpl_code" value="no" />
  112.                <article>
  113.                    <order_number>'.$current_article_data['IntimeArticle']['order_number'].'</order_number>
  114.                    <package_count>'.$count.'</package_count>
  115.                    <weight>1</weight>                    
  116.                </article>
  117.            </request>';
  118.         return $this->sendRequestToAZ($input_xml);
  119.     }
  120.  
  121.     protected function removePackagesFromAz($current_article_data, $count) {
  122.        $input_xml = '<?xml version="1.0" encoding="utf-8"?>
  123.            <request name="article_remove_package">
  124.                <auth username="'.self::API_USER.'" password="'.self::API_PASSWORD.'"/>
  125.                <option name="transaction" value="yes" />
  126.                <option name="zpl_code" value="no" />
  127.                <article>
  128.                    <order_number>'.$current_article_data['IntimeArticle']['order_number'].'</order_number>
  129.                    <package_count>'.$count.'</package_count>
  130.                    <weight>0,01</weight>                    
  131.                </article>
  132.            </request>';
  133.         return $this->sendRequestToAZ($input_xml);
  134.     }
  135.  
  136.     protected function sendRequestToAZ($xml) {
  137.         $ch = curl_init();
  138.         curl_setopt($ch, CURLOPT_URL, self::API_URL);
  139.         curl_setopt($ch, CURLOPT_POSTFIELDS, "xmlRequest=" . $xml);
  140.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  141.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
  142.         $data = curl_exec($ch);
  143.         curl_close($ch);
  144.         return $this->parseResponse($data);
  145.     }
  146.  
  147.     protected function parseResponse($data) {
  148.         $array_data = json_decode(json_encode(simplexml_load_string($data)), true);
  149.         if ($array_data['article']['code'] != 0) {
  150.             throw new Exception($array_data['article']['error']);
  151.         }
  152.  
  153.         return $array_data;
  154.     }
  155.  
  156.     public function sendArticlesToIntime($orders) {
  157.         $intime_articles_model = ClassRegistry::init('IntimeArticle');
  158.         $order_articles_numbers = [];
  159.         foreach ($orders as $order) {
  160.             $order_article = $intime_articles_model->getArticleDataByOrderId($order['Order']['id']);
  161.             $order_articles_numbers[] = $order_article[0]['IntimeArticle']['order_number'];
  162.         }
  163.  
  164.         foreach ($order_articles_numbers as $article_number) {
  165.             $order_numbers_xml = '<article>
  166.                <order_number>'.$article_number.'</order_number>    
  167.            </article>';
  168.         }
  169.         $xml = '<?xml version="1.0" encoding="utf-8"?>
  170.            <request name="complete_article">
  171.                <auth username="'.self::API_USER.'" password="'.self::API_PASSWORD.'"/>
  172.                <option name="transaction" value="no" />
  173.                '.$order_numbers_xml.'
  174.        </request>';
  175.         return $this->sendRequestToAZ($xml);
  176.     }
  177.  
  178.     /**
  179.      * @param $orders
  180.      * @param int $posInPage
  181.      */
  182.     public function generateLabels($orders, $posInPage = 0) {
  183.         define('FPDF_FONTPATH', 'fpdf/font/');
  184.         require('fpdf/PDF_Rotate.php');
  185.         $pdf = new PDF_Rotate();
  186.         $pdf->AddFont('consolas', '', 'Consolas.php');
  187.         $pdf->AddFont('consolas-bold', '', 'Consolas Bold.php');
  188.         $pdf->SetAutoPageBreak(true, 0.9);
  189.         $pdf->AddPage();
  190.         if ($posInPage > 3) $posInPage = 0;             // temp lines
  191.         $barcode = new BarcodeHelper();
  192.         $barcode->barcode();
  193.         $barcode->setType('C128');
  194.         $intime_articles_model = ClassRegistry::init('IntimeArticle');
  195.  
  196.         foreach ($orders as $order) {
  197.             $current_article_data = $intime_articles_model->getArticleDataByOrderId($order['Order']['id']);
  198.             $current_article_data = $current_article_data[0];
  199.             $boxLabelModel = ClassRegistry::init('StockBoxLabel');
  200.             $name = Common::convertToAscii($order['Shipping']['firstname']);
  201.             $labels = $boxLabelModel->findAllByOrderStockPropertyId($order['OrdersStockProperty']['id']);
  202.  
  203.             $count_labels = count($labels);
  204.  
  205.             if ($count_labels < $order['OrdersStockProperty']['packages_quantity']) {
  206.                 if (empty($labels)) {
  207.                     $response = $this->addArticleToAZ($order);
  208.                 } else {
  209.                     $count = $order['OrdersStockProperty']['packages_quantity'] - $count_labels;
  210.                     $response = $this->addPackageToAZ($current_article_data, $count);
  211.                 }
  212.  
  213.                 $k = 0;
  214.                 for ($i = $count_labels; $i < $order['OrdersStockProperty']['packages_quantity']; $i++) {
  215.                     $boxLabelModel->create();
  216.                     if (is_array($response['article']['barcode'])) {
  217.                         $intime_tracking_number = $response['article']['barcode'][$k];
  218.                     } else {
  219.                         $intime_tracking_number = $response['article']['barcode'];
  220.                     }
  221.                     $label = $boxLabelModel->save([
  222.                         'order_stock_property_id' => $order['OrdersStockProperty']['id'],
  223.                         'package_identifier' => $i,
  224.                         'intime_tracking_number' => $intime_tracking_number
  225.                     ]);
  226.  
  227.                     $labels[] = $label;
  228.                     $k++;
  229.                 }
  230.  
  231.             } elseif ($count_labels > $order['OrdersStockProperty']['packages_quantity']) {
  232.                 $response = $this->removePackagesFromAz($current_article_data, $count_labels - $order['OrdersStockProperty']['packages_quantity']);
  233.                 $barcodes = Hash::extract($response, 'article.barcode');
  234.                 $labels_to_delete = [];
  235.                 foreach ($labels as $k => $label) {
  236.                     if (!in_array($label['StockBoxLabel']['intime_tracking_number'], $barcodes)) {
  237.                         $labels_to_delete[] = $label['StockBoxLabel']['id'];
  238.                         unset($labels[$k]);
  239.                     }
  240.                 }
  241.                 if (!empty($labels_to_delete)) {
  242.                     $boxLabelModel->deleteAll([
  243.                         'StockBoxLabel.id' => $labels_to_delete
  244.                     ], false);
  245.                 }
  246.             }
  247.  
  248.             $labels_quantity = count($labels);
  249.             foreach ($labels as $label) {
  250.                 $barcode->setCode($label['StockBoxLabel']['intime_tracking_number']);
  251.                 $barcode->setSize(50, 150);
  252.                 $barcode->setText($label['StockBoxLabel']['intime_tracking_number']);
  253.                 $barcode_path = ROOT . DS . APP_DIR . DS . 'tmp/barcode/intime/intime-'.$label['StockBoxLabel']['id'].'.png';
  254.                 $barcode->writeBarcodeFile($barcode_path);
  255.  
  256.  
  257.                 // if 4 labels done, go to new page
  258.                 if ($posInPage > 3) {
  259.                     $posInPage = 0;
  260.                     $pdf->AddPage();
  261.                 }
  262.  
  263.                 $baseX = $this->getBaseXPosForLabel($posInPage);
  264.                 $baseY = $this->getBaseYPosForLabel($posInPage);
  265.  
  266.                 // Draw sections
  267.                 $pdf->SetDrawColor(1, 0, 0);
  268.                 $pdf->SetLineWidth(0.5);
  269.                 $pdf->line($baseX + 30, $baseY + 75, $baseX + 58, $baseY + 75);
  270.                 $pdf->line($baseX + 58, $baseY, $baseX + 58, $baseY + 75);
  271.  
  272.                 $pdf->rotateImage($barcode_path, 90, $baseX + 3, $baseY + 115, 90, 17);
  273.  
  274.                 // Package info
  275.                 $pdf->SetFont('consolas-bold', '', 11);
  276.                 $pdf->rotateMultiCell($baseX + 34, $baseY + 73  , 90, 150, 7, "Colli: \nZasilka c.: \nReferencni c.:", 0, 'L');
  277.                 $pdf->SetFont('consolas-bold', '', 18);
  278.                 $label_number = sprintf("%03d z %03d", (int)$label['StockBoxLabel']['package_identifier'] + 1, (int)$labels_quantity);
  279.                 $pdf->rotateCell($baseX + 36, $baseY + 38, 90, 78, 0, $label_number);
  280.  
  281.                 $pdf->SetFont('consolas-bold', '', 11);
  282.                 $order_reference_numbers = $current_article_data['IntimeArticle']['order_number']."\n".$current_article_data['IntimeArticle']['reference_number'];
  283.                 $pdf->rotateMultiCell($baseX + 42, $baseY + 38  , 90, 150, 6, $order_reference_numbers, 0, 'L');
  284.  
  285.                 // -- Package info --
  286.  
  287.                 // Sender info
  288.                 $pdf->SetFont('consolas', '', 10);
  289.                 $pdf->rotateCell($baseX + 28, $baseY + 147, 90, 78, 0, 'Prikazce (odesilatel):');
  290.                 $pdf->SetFont('consolas-bold', '', 12);
  291.                 $pdf->rotateCell($baseX + 37, $baseY + 147, 90, 78, 0, 'Test firma (Praha 01)');
  292.                 $pdf->rotateCell($baseX + 43, $baseY + 147, 90, 78, 0, 'Ulice svoz');
  293.                 $pdf->rotateCell($baseX + 49, $baseY + 147, 90, 78, 0, 'Praha, CZ');
  294.                 // -- Sender info --
  295.  
  296.  
  297.                 // Delivery info
  298.                 $pdf->SetFont('consolas-bold', '', 14);
  299.                 $pdf->rotateCell($baseX + 62, $baseY + 147, 90, 78, 0, $current_article_data['IntimeArticle']['product_name']);
  300.  
  301.                 $pdf->SetFont('consolas-bold', '', 8);
  302.                 $pdf->rotateCell($baseX + 68, $baseY + 147, 90, 78, 0, 'Dobirka:');
  303.                 $pdf->rotateCell($baseX + 74, $baseY + 147, 90, 78, 0, 'Dokumenty zpet:');
  304.                 $pdf->rotateCell($baseX + 80, $baseY + 147, 90, 78, 0, 'Tel. avizo:');
  305.                 $pdf->rotateCell($baseX + 86, $baseY + 147, 90, 78, 0, 'Zpetna zasilka:');
  306.  
  307.                 $is_cash_payment = (($order['Order']['payment_type_id'] == PaymentsType::PAYMENT_TYPE_CASH) ? 'Ano' : 'Ne');
  308.  
  309.                 $pdf->rotateCell($baseX + 68, $baseY + 118, 90, 78, 0, $is_cash_payment);
  310.                 $pdf->rotateCell($baseX + 74, $baseY + 118, 90, 78, 0, 'Ne');
  311.                 $pdf->rotateCell($baseX + 80, $baseY + 118, 90, 78, 0, 'Ne');
  312.                 $pdf->rotateCell($baseX + 86, $baseY + 118, 90, 78, 0, 'Ne');
  313.                 // -- Delivery info --
  314.  
  315.                 $pdf->rotateImage(ROOT . DS . APP_DIR . DS . WEBROOT_DIR . DS. 'img/intime-big.jpg', 90, $baseX + 91, $baseY + 145 , 35, 9);
  316.  
  317.                 // Reсiever
  318.  
  319.                 $reciever_data = $order['Shipping']['firstname']." ".$order['Shipping']['lastname']."\n".$order['Shipping']['street'];
  320.                 $pdf->SetFont('consolas', '', 10);
  321.                 $pdf->rotateCell($baseX + 60, $baseY + 100, 90, 78, 0, 'Prijemce:');
  322.                 $pdf->SetFont('consolas-bold', '', 12);
  323.                 $pdf->rotateMultiCell($baseX + 63, $baseY + 101 , 90, 150, 7, $reciever_data, 0, 'L');
  324.                 $pdf->SetFont('consolas-bold', '', 20);
  325.                 $pdf->rotateCell($baseX +81, $baseY + 101, 90, 78, 0, $order['Shipping']['code']);
  326.                 $pdf->SetFont('consolas-bold', '', 12);
  327.                 $pdf->rotateMultiCell($baseX + 85, $baseY + 101 , 90, 150, 7, $order['Shipping']['city'], 0, 'L');
  328.  
  329.                 $pdf->SetFont('consolas', '', 10);
  330.                 $pdf->rotateCell($baseX +95, $baseY + 101, 90, 78, 0, '609112567');
  331.                 // -- Reviever --
  332.  
  333.                 // Code
  334.                 $pdf->SetFont('consolas-bold', '', 30);
  335.                 $pdf->rotateCell($baseX +84, $baseY + 53, 90, 78, 0, $current_article_data['IntimeArticle']['sorting_code']);
  336.                 // -- Code --
  337.  
  338.                 $posInPage++;
  339.                 $pdf->Rotate(0);
  340.             }
  341.         }
  342.  
  343.         // output
  344.         if (count($orders) == 1) {
  345.             $pdfName = 'intime_labels_' . $order['Order']['id'].'.pdf';
  346.         } else {
  347.             $pdfName = 'intime_labels_mass_print.pdf';
  348.         }
  349.         $pdf->Output($pdfName, 'I');
  350.         exit;
  351.     }
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement