Created
March 12, 2019 08:26
-
-
Save torunar/09c3de032c0c3c4191bbf3a34d0ada54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/addons/rus_sdek/controllers/backend/orders.post.php b/app/addons/rus_sdek/controllers/backend/orders.post.php | |
index f2633db969..da67b8d38a 100644 | |
--- a/app/addons/rus_sdek/controllers/backend/orders.post.php | |
+++ b/app/addons/rus_sdek/controllers/backend/orders.post.php | |
@@ -12,12 +12,15 @@ | |
* "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE. * | |
****************************************************************************/ | |
+use Tygh\Addons\RusTaxes\Receipt\Item as ReceiptItem; | |
+use Tygh\Addons\RusTaxes\TaxType; | |
+use Tygh\Common\OperationResult; | |
+use Tygh\Exceptions\InputException; | |
+use Tygh\ExSimpleXmlElement; | |
+use Tygh\Mailer; | |
use Tygh\Registry; | |
use Tygh\Shippings\RusSdek; | |
-use Tygh\Mailer; | |
use Tygh\Shippings\Shippings; | |
-use Tygh\Addons\RusTaxes\Receipt\Item as ReceiptItem; | |
-use Tygh\Addons\RusTaxes\TaxType; | |
if (!defined('BOOTSTRAP')) { die('Access denied'); } | |
@@ -372,8 +375,6 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { | |
if (!empty($call_courier)) { | |
db_query('INSERT INTO ?:rus_sdek_call_courier ?e', $call_courier); | |
} | |
- | |
- fn_sdek_get_ticket_order($data_auth, $params['order_id'], $shipment_id); | |
} | |
$date_status = RusSdek::orderStatusXml($data_auth, $params['order_id'], $shipment_id); | |
@@ -466,8 +467,6 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { | |
} else { | |
db_query('INSERT INTO ?:rus_sdek_call_recipient ?e', $call_recipient); | |
} | |
- | |
- fn_sdek_get_ticket_order($data_auth, $params['order_id'], $shipment_id); | |
} | |
$date_status = RusSdek::orderStatusXml($data_auth, $params['order_id'], $shipment_id); | |
@@ -811,42 +810,93 @@ if ($mode == 'details') { | |
$order_id = (int) $_REQUEST['order_id']; | |
$shipment_id = (int) $_REQUEST['shipment_id']; | |
- $file = $order_id . '.pdf'; | |
+ list($shipments,) = fn_get_shipments_info(array('order_id' => $order_id, 'advanced_info' => true, 'shipment_id' => $shipment_id)); | |
- $path = fn_get_files_dir_path() . 'sdek/' . $shipment_id . '/'; | |
+ $shipment = reset($shipments); | |
- fn_get_file($path . $file); | |
+ $params_shipping = array( | |
+ 'shipping_id' => $shipment['shipping_id'], | |
+ 'Date' => date('Y-m-d', $shipment['shipment_timestamp']) | |
+ ); | |
- if (defined('AJAX_REQUEST') && !empty($url)) { | |
- Registry::get('ajax')->assign('force_redirection', $url); | |
+ $data_auth = RusSdek::dataAuth($params_shipping); | |
+ $data_auth['OrderCount'] = 1; | |
+ | |
+ $ticket_result = fn_sdek_get_ticket_order($data_auth, $order_id, $shipment_id); | |
+ | |
+ if ($ticket_result->isSuccess()) { | |
+ fn_get_file($ticket_result->getData()); | |
+ } else { | |
+ $ticket_result->showNotifications(); | |
+ } | |
+ | |
+ $url = fn_url('shipments.details?shipment_id=' . $shipment_id); | |
+ | |
+ if (defined('AJAX_REQUEST')) { | |
+ Tygh::$app['ajax']->assign('force_redirection', $url); | |
exit; | |
} | |
- return array(CONTROLLER_STATUS_OK); | |
+ return array(CONTROLLER_STATUS_OK, $url); | |
} | |
-function fn_sdek_get_ticket_order($data_auth, $order_id, $chek_id) | |
+/** | |
+ * Requests shipment receipt creation. | |
+ * | |
+ * @param array $data_auth Authentication data for an API request | |
+ * @param int $order_id Order identifier | |
+ * @param int $shipment_id Shipment identifier | |
+ * | |
+ * @return \Tygh\Common\OperationResult Receipt creation result. | |
+ * Contains file path in its data | |
+ */ | |
+function fn_sdek_get_ticket_order($data_auth, $order_id, $shipment_id) | |
{ | |
- unset($data_auth['Number']); | |
- $xml = ' ' . RusSdek::arraySimpleXml('OrdersPrint', $data_auth, 'open'); | |
- $order_sdek = array ( | |
- 'Number' => $order_id . '_' . $chek_id, | |
- 'Date' => $data_auth['Date'] | |
- ); | |
- $xml .= ' ' . RusSdek::arraySimpleXml('Order', $order_sdek); | |
- $xml .= ' ' . '</OrdersPrint>'; | |
+ $result = new OperationResult(true); | |
- $response = RusSdek::xmlRequest(SDEK_URL_INTEGRATION . 'orders_print.php', $xml, $data_auth); | |
+ $download_file_dir = fn_get_files_dir_path() . '/sdek' . '/' . $shipment_id . '/'; | |
+ $download_file_path = $download_file_dir . $order_id . '.pdf'; | |
+ | |
+ if (is_file($download_file_path)) { | |
+ $result->setData($download_file_path); | |
+ return $result; | |
+ } | |
- $download_file_dir = fn_get_files_dir_path() . '/sdek' . '/' . $chek_id . '/'; | |
+ $xml = RusSdek::arraySimpleXml('OrdersPrint', $data_auth, 'open'); | |
+ $order_sdek = [ | |
+ 'Number' => $order_id . '_' . $shipment_id, | |
+ 'Date' => $data_auth['Date'], | |
+ ]; | |
+ $xml .= RusSdek::arraySimpleXml('Order', $order_sdek); | |
+ $xml .= '</OrdersPrint>'; | |
- fn_rm($download_file_dir); | |
- fn_mkdir($download_file_dir); | |
+ $response = RusSdek::xmlRequest(SDEK_URL_INTEGRATION . 'orders_print.php', $xml, $data_auth); | |
- $name = $order_id . '.pdf'; | |
+ try { | |
+ ExSimpleXmlElement::loadFromString($response); | |
+ $parsed_response = RusSdek::resultXml($response); | |
+ // SDEK returns an XML document when an error occurs | |
+ $result->setSuccess(false); | |
+ if ($parsed_response['error_code'] === 'ERR_INVALID_NUMBER') { | |
+ fn_delete_notification($parsed_response['error_code']); | |
+ $result->addError(0, __('rus_sdek.order_not_synced', ['[error]' => $parsed_response['msg']])); | |
+ } | |
+ } catch (InputException $e) { | |
+ // SDEK returns a PDF, thus XML parsing fails | |
+ } | |
- $download_file_path = $download_file_dir . $name; | |
- if (!fn_is_empty($response)) { | |
- fn_put_contents($download_file_path, $response); | |
+ if ($result->isSuccess()) { | |
+ fn_rm($download_file_dir); | |
+ fn_mkdir($download_file_dir); | |
+ | |
+ if (fn_is_empty($response)) { | |
+ $result->setSuccess(false); | |
+ $result->addError(0, __('rus_sdek.empty_receipt_print_response')); | |
+ } else { | |
+ fn_put_contents($download_file_path, $response); | |
+ $result->setData($download_file_path); | |
+ } | |
} | |
+ | |
+ return $result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/addons/rus_sdek/Tygh/Shippings/RusSdek.php b/app/addons/rus_sdek/Tygh/Shippings/RusSdek.php | |
index 0683333973..70e2b97188 100644 | |
--- a/app/addons/rus_sdek/Tygh/Shippings/RusSdek.php | |
+++ b/app/addons/rus_sdek/Tygh/Shippings/RusSdek.php | |
@@ -15,7 +15,6 @@ | |
namespace Tygh\Shippings; | |
use Tygh\Http; | |
-use Tygh\Registry; | |
class RusSdek | |
{ | |
@@ -42,11 +41,12 @@ class RusSdek | |
public static function resultXml($response) | |
{ | |
- $result = array( | |
- 'error' => false, | |
- 'msg' => false, | |
- 'number' => false, | |
- ); | |
+ $result = [ | |
+ 'error' => false, | |
+ 'error_code' => false, | |
+ 'msg' => false, | |
+ 'number' => false, | |
+ ]; | |
if (!empty($response)) { | |
$xml_result = simplexml_load_string($response); | |
@@ -60,8 +60,15 @@ class RusSdek | |
if (!empty($data['Msg'])) { | |
if (!empty($data['ErrorCode'])) { | |
+ $result['error_code'] = $data['ErrorCode']; | |
$result['msg'] = $data['Msg']; | |
- fn_set_notification('E', __('notice'), $data['Msg']); | |
+ fn_set_notification( | |
+ 'E', | |
+ __('notice'), | |
+ $data['Msg'], | |
+ '', | |
+ $data['ErrorCode'] | |
+ ); | |
if ($data['ErrorCode'] == 'ERR_ORDER_NOTFIND' || $data['ErrorCode'] == 'ERR_ORDER_DUBL_EXISTS') { | |
$result['error'] = false; | |
} else { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment