Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- App::uses('AppModel', 'Model');
- App::uses('CakeEmail', 'Network/Email');
- App::uses('Common', 'Lib');
- App::uses('Ftp', 'Lib');
- App::uses('BarcodeHelper', 'Vendor');
- App::uses('BarcodeUtils', 'Utility');
- class InTime extends AppModel {
- public $useTable = false;
- const API_URL = 'http://zasilky.intime.cz/test/xml_server.php';
- const API_USER = 'ageo';
- const API_PASSWORD = 'ageo123';
- protected function getBaseXPosForLabel($posInPage) {
- return $this->getBasePosForLabel($posInPage)['baseX'];
- }
- protected function getBaseYPosForLabel($posInPage) {
- return $this->getBasePosForLabel($posInPage)['baseY'];
- }
- protected function getBasePosForLabel($posInPage) {
- $baseX = 0;
- $baseY = 0;
- if ($posInPage == 1) {
- $baseX = 106;
- } elseif ($posInPage == 2) {
- $baseY = 149;
- } elseif ($posInPage == 3) {
- $baseX = 106;
- $baseY = 149;
- }
- return ['baseX' => $baseX, 'baseY' => $baseY];
- }
- protected function addArticleToAZ($order) {
- $cash_on_delivery = '';
- if ($order['Order']['payment_type_id'] == PaymentsType::PAYMENT_TYPE_CASH) {
- $cash_on_delivery = $order['Order']['total_amount_with_shipping'];
- $cash_on_delivery = str_replace('.', ',', $cash_on_delivery);
- }
- $firstname = Common::transliterateString($order['Shipping']['firstname']);
- $surname = Common::transliterateString($order['Shipping']['lastname']);
- $street = Common::transliterateString($order['Shipping']['street']);
- $city = Common::transliterateString($order['Shipping']['city']);
- $phone = preg_replace('/[\+]/', '00', $order['Shipping']['phone']);
- $phone = str_replace(' ', '', $phone);
- /* weight needs to be <weight>'.$order['OrdersStockProperty']['weight'].'</weight> */
- $input_xml = '<?xml version="1.0" encoding="utf-8"?>
- <request name="import_article">
- <auth username="'.self::API_USER.'" password="'.self::API_PASSWORD.'"/>
- <option name="transaction" value="no"/>
- <option name="auto_complete" value="no"/>
- <option name="customer" value="427"/>
- <option name="department" value="796"/>
- <article>
- <receiver>
- <external_id>'.$order['Order']['id'].'</external_id>
- <name>IN TIME spedice</name>
- <street>'.$street.'</street>
- <city>'.$city.'</city>
- <postal_code>'.$order['Shipping']['code'].'</postal_code>
- <state>CZ</state>
- <firstname>'.$firstname.'</firstname>
- <surname>'.$surname.'</surname>
- <email>'.$order['User']['email'].'</email>
- </receiver>
- <sender>
- <name>E-AGEO.cz, s. r. o.</name>
- <street>Do Certous 2635 Hala D1</street>
- <city>Praha</city>
- <postal_code>193 00</postal_code>
- <state>CZ</state>
- <email>orders@ageo.cz</email>
- </sender>
- <reference_number>'.$order['Order']['id'].'</reference_number>
- <package_count>'.$order['OrdersStockProperty']['packages_quantity'].'</package_count>
- <additional_service name="direct_order" value="yes" />
- <additional_service name="email_notification" value="yes" />
- <additional_service name="email_notification_address_direct" value="'.$order['User']['email'].'" />
- <additional_service name="cash_on_delivery" value="'.$cash_on_delivery.'" />
- <additional_service name="sms_notification" value="yes" />
- <additional_service name="sms_notification_number" value="'.$phone.'" />
- <weight>1</weight>
- <value>1000</value>
- <comment>prosim co nejrychleji</comment>
- <additive>yes</additive>
- </article>
- </request>';
- $response = $this->sendRequestToAZ($input_xml);
- $intime_articles_model = ClassRegistry::init('IntimeArticle');
- $intime_articles_model->create();
- $intime_articles_model->save([
- 'order_id' => $order['Order']['id'],
- 'order_number' => $response['article']['order_number'],
- 'batch_id' => $response['batch']['id'],
- 'batch_number' => $response['batch']['number'],
- 'product_name' => $response['article']['product_name'],
- 'reference_number' => $response['article']['reference_number'],
- 'sorting_code' => $response['article']['sorting_code']
- ]);
- return $response;
- }
- protected function addPackageToAZ($current_article_data, $count) {
- $input_xml = '<?xml version="1.0" encoding="utf-8"?>
- <request name="article_add_package">
- <auth username="'.self::API_USER.'" password="'.self::API_PASSWORD.'"/>
- <option name="transaction" value="yes" />
- <option name="zpl_code" value="no" />
- <article>
- <order_number>'.$current_article_data['IntimeArticle']['order_number'].'</order_number>
- <package_count>'.$count.'</package_count>
- <weight>1</weight>
- </article>
- </request>';
- return $this->sendRequestToAZ($input_xml);
- }
- protected function removePackagesFromAz($current_article_data, $count) {
- $input_xml = '<?xml version="1.0" encoding="utf-8"?>
- <request name="article_remove_package">
- <auth username="'.self::API_USER.'" password="'.self::API_PASSWORD.'"/>
- <option name="transaction" value="yes" />
- <option name="zpl_code" value="no" />
- <article>
- <order_number>'.$current_article_data['IntimeArticle']['order_number'].'</order_number>
- <package_count>'.$count.'</package_count>
- <weight>0,01</weight>
- </article>
- </request>';
- return $this->sendRequestToAZ($input_xml);
- }
- protected function sendRequestToAZ($xml) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, self::API_URL);
- curl_setopt($ch, CURLOPT_POSTFIELDS, "xmlRequest=" . $xml);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
- $data = curl_exec($ch);
- curl_close($ch);
- return $this->parseResponse($data);
- }
- protected function parseResponse($data) {
- $array_data = json_decode(json_encode(simplexml_load_string($data)), true);
- if ($array_data['article']['code'] != 0) {
- throw new Exception($array_data['article']['error']);
- }
- return $array_data;
- }
- public function sendArticlesToIntime($orders) {
- $intime_articles_model = ClassRegistry::init('IntimeArticle');
- $order_articles_numbers = [];
- foreach ($orders as $order) {
- $order_article = $intime_articles_model->getArticleDataByOrderId($order['Order']['id']);
- $order_articles_numbers[] = $order_article[0]['IntimeArticle']['order_number'];
- }
- foreach ($order_articles_numbers as $article_number) {
- $order_numbers_xml = '<article>
- <order_number>'.$article_number.'</order_number>
- </article>';
- }
- $xml = '<?xml version="1.0" encoding="utf-8"?>
- <request name="complete_article">
- <auth username="'.self::API_USER.'" password="'.self::API_PASSWORD.'"/>
- <option name="transaction" value="no" />
- '.$order_numbers_xml.'
- </request>';
- return $this->sendRequestToAZ($xml);
- }
- /**
- * @param $orders
- * @param int $posInPage
- */
- public function generateLabels($orders, $posInPage = 0) {
- define('FPDF_FONTPATH', 'fpdf/font/');
- require('fpdf/PDF_Rotate.php');
- $pdf = new PDF_Rotate();
- $pdf->AddFont('consolas', '', 'Consolas.php');
- $pdf->AddFont('consolas-bold', '', 'Consolas Bold.php');
- $pdf->SetAutoPageBreak(true, 0.9);
- $pdf->AddPage();
- if ($posInPage > 3) $posInPage = 0; // temp lines
- $barcode = new BarcodeHelper();
- $barcode->barcode();
- $barcode->setType('C128');
- $intime_articles_model = ClassRegistry::init('IntimeArticle');
- foreach ($orders as $order) {
- $current_article_data = $intime_articles_model->getArticleDataByOrderId($order['Order']['id']);
- $current_article_data = $current_article_data[0];
- $boxLabelModel = ClassRegistry::init('StockBoxLabel');
- $name = Common::convertToAscii($order['Shipping']['firstname']);
- $labels = $boxLabelModel->findAllByOrderStockPropertyId($order['OrdersStockProperty']['id']);
- $count_labels = count($labels);
- if ($count_labels < $order['OrdersStockProperty']['packages_quantity']) {
- if (empty($labels)) {
- $response = $this->addArticleToAZ($order);
- } else {
- $count = $order['OrdersStockProperty']['packages_quantity'] - $count_labels;
- $response = $this->addPackageToAZ($current_article_data, $count);
- }
- $k = 0;
- for ($i = $count_labels; $i < $order['OrdersStockProperty']['packages_quantity']; $i++) {
- $boxLabelModel->create();
- if (is_array($response['article']['barcode'])) {
- $intime_tracking_number = $response['article']['barcode'][$k];
- } else {
- $intime_tracking_number = $response['article']['barcode'];
- }
- $label = $boxLabelModel->save([
- 'order_stock_property_id' => $order['OrdersStockProperty']['id'],
- 'package_identifier' => $i,
- 'intime_tracking_number' => $intime_tracking_number
- ]);
- $labels[] = $label;
- $k++;
- }
- } elseif ($count_labels > $order['OrdersStockProperty']['packages_quantity']) {
- $response = $this->removePackagesFromAz($current_article_data, $count_labels - $order['OrdersStockProperty']['packages_quantity']);
- $barcodes = Hash::extract($response, 'article.barcode');
- $labels_to_delete = [];
- foreach ($labels as $k => $label) {
- if (!in_array($label['StockBoxLabel']['intime_tracking_number'], $barcodes)) {
- $labels_to_delete[] = $label['StockBoxLabel']['id'];
- unset($labels[$k]);
- }
- }
- if (!empty($labels_to_delete)) {
- $boxLabelModel->deleteAll([
- 'StockBoxLabel.id' => $labels_to_delete
- ], false);
- }
- }
- $labels_quantity = count($labels);
- foreach ($labels as $label) {
- $barcode->setCode($label['StockBoxLabel']['intime_tracking_number']);
- $barcode->setSize(50, 150);
- $barcode->setText($label['StockBoxLabel']['intime_tracking_number']);
- $barcode_path = ROOT . DS . APP_DIR . DS . 'tmp/barcode/intime/intime-'.$label['StockBoxLabel']['id'].'.png';
- $barcode->writeBarcodeFile($barcode_path);
- // if 4 labels done, go to new page
- if ($posInPage > 3) {
- $posInPage = 0;
- $pdf->AddPage();
- }
- $baseX = $this->getBaseXPosForLabel($posInPage);
- $baseY = $this->getBaseYPosForLabel($posInPage);
- // Draw sections
- $pdf->SetDrawColor(1, 0, 0);
- $pdf->SetLineWidth(0.5);
- $pdf->line($baseX + 30, $baseY + 75, $baseX + 58, $baseY + 75);
- $pdf->line($baseX + 58, $baseY, $baseX + 58, $baseY + 75);
- $pdf->rotateImage($barcode_path, 90, $baseX + 3, $baseY + 115, 90, 17);
- // Package info
- $pdf->SetFont('consolas-bold', '', 11);
- $pdf->rotateMultiCell($baseX + 34, $baseY + 73 , 90, 150, 7, "Colli: \nZasilka c.: \nReferencni c.:", 0, 'L');
- $pdf->SetFont('consolas-bold', '', 18);
- $label_number = sprintf("%03d z %03d", (int)$label['StockBoxLabel']['package_identifier'] + 1, (int)$labels_quantity);
- $pdf->rotateCell($baseX + 36, $baseY + 38, 90, 78, 0, $label_number);
- $pdf->SetFont('consolas-bold', '', 11);
- $order_reference_numbers = $current_article_data['IntimeArticle']['order_number']."\n".$current_article_data['IntimeArticle']['reference_number'];
- $pdf->rotateMultiCell($baseX + 42, $baseY + 38 , 90, 150, 6, $order_reference_numbers, 0, 'L');
- // -- Package info --
- // Sender info
- $pdf->SetFont('consolas', '', 10);
- $pdf->rotateCell($baseX + 28, $baseY + 147, 90, 78, 0, 'Prikazce (odesilatel):');
- $pdf->SetFont('consolas-bold', '', 12);
- $pdf->rotateCell($baseX + 37, $baseY + 147, 90, 78, 0, 'Test firma (Praha 01)');
- $pdf->rotateCell($baseX + 43, $baseY + 147, 90, 78, 0, 'Ulice svoz');
- $pdf->rotateCell($baseX + 49, $baseY + 147, 90, 78, 0, 'Praha, CZ');
- // -- Sender info --
- // Delivery info
- $pdf->SetFont('consolas-bold', '', 14);
- $pdf->rotateCell($baseX + 62, $baseY + 147, 90, 78, 0, $current_article_data['IntimeArticle']['product_name']);
- $pdf->SetFont('consolas-bold', '', 8);
- $pdf->rotateCell($baseX + 68, $baseY + 147, 90, 78, 0, 'Dobirka:');
- $pdf->rotateCell($baseX + 74, $baseY + 147, 90, 78, 0, 'Dokumenty zpet:');
- $pdf->rotateCell($baseX + 80, $baseY + 147, 90, 78, 0, 'Tel. avizo:');
- $pdf->rotateCell($baseX + 86, $baseY + 147, 90, 78, 0, 'Zpetna zasilka:');
- $is_cash_payment = (($order['Order']['payment_type_id'] == PaymentsType::PAYMENT_TYPE_CASH) ? 'Ano' : 'Ne');
- $pdf->rotateCell($baseX + 68, $baseY + 118, 90, 78, 0, $is_cash_payment);
- $pdf->rotateCell($baseX + 74, $baseY + 118, 90, 78, 0, 'Ne');
- $pdf->rotateCell($baseX + 80, $baseY + 118, 90, 78, 0, 'Ne');
- $pdf->rotateCell($baseX + 86, $baseY + 118, 90, 78, 0, 'Ne');
- // -- Delivery info --
- $pdf->rotateImage(ROOT . DS . APP_DIR . DS . WEBROOT_DIR . DS. 'img/intime-big.jpg', 90, $baseX + 91, $baseY + 145 , 35, 9);
- // Reсiever
- $reciever_data = $order['Shipping']['firstname']." ".$order['Shipping']['lastname']."\n".$order['Shipping']['street'];
- $pdf->SetFont('consolas', '', 10);
- $pdf->rotateCell($baseX + 60, $baseY + 100, 90, 78, 0, 'Prijemce:');
- $pdf->SetFont('consolas-bold', '', 12);
- $pdf->rotateMultiCell($baseX + 63, $baseY + 101 , 90, 150, 7, $reciever_data, 0, 'L');
- $pdf->SetFont('consolas-bold', '', 20);
- $pdf->rotateCell($baseX +81, $baseY + 101, 90, 78, 0, $order['Shipping']['code']);
- $pdf->SetFont('consolas-bold', '', 12);
- $pdf->rotateMultiCell($baseX + 85, $baseY + 101 , 90, 150, 7, $order['Shipping']['city'], 0, 'L');
- $pdf->SetFont('consolas', '', 10);
- $pdf->rotateCell($baseX +95, $baseY + 101, 90, 78, 0, '609112567');
- // -- Reviever --
- // Code
- $pdf->SetFont('consolas-bold', '', 30);
- $pdf->rotateCell($baseX +84, $baseY + 53, 90, 78, 0, $current_article_data['IntimeArticle']['sorting_code']);
- // -- Code --
- $posInPage++;
- $pdf->Rotate(0);
- }
- }
- // output
- if (count($orders) == 1) {
- $pdfName = 'intime_labels_' . $order['Order']['id'].'.pdf';
- } else {
- $pdfName = 'intime_labels_mass_print.pdf';
- }
- $pdf->Output($pdfName, 'I');
- exit;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement