Skip to content

Commit 531cf19

Browse files
committed
Merge branch 'CMS-253' into 'master'
Cms 253 See merge request ecommerce_modules/wordpress_cdek_delivery!27
2 parents 4334066 + f08fbc5 commit 531cf19

File tree

7 files changed

+187
-119
lines changed

7 files changed

+187
-119
lines changed

assets/js/admin-settings.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jQuery(($) => {
5252
apiKey: window.cdek.apiKey,
5353
sender: true,
5454
debug: true,
55-
defaultLocation: 'Новосибирск',
55+
defaultLocation: officeInput.val() ? officeInput.val().split(', ')[0] : 'Новосибирск',
5656
servicePath: window.cdek_admin_settings.api.offices,
5757
hideFilters: {
5858
type: true,
@@ -61,12 +61,12 @@ jQuery(($) => {
6161
is_dressing_room: true,
6262
},
6363
selected: {
64-
office: officeInput.val() ? officeInput.val().split(' ')[1] : null,
64+
office: officeInput.val() ? officeInput.val().split(', ')[1] : null,
6565
door: doorInput.val() || null,
6666
},
6767
onChoose(type, tariff, target) {
6868
if (type === 'office') {
69-
officeInput.val(`${target.city} ${target.code}`);
69+
officeInput.val(`${target.city}, ${target.code}`);
7070
$('input#woocommerce_official_cdek_country').val(target.country_code);
7171
updateOfficeCode();
7272
} else if (type === 'door') {

cdek.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function getCityCode($city_code, $order) {
5656
return $cityCode;
5757
}
5858

59-
function setPackage($data, $orderId, $currency) {
59+
function setPackage($data, $orderId, $currency, $tariffType = Tariff::SHOP_TYPE) {
6060
$param = [];
6161
if (Helper::getActualShippingMethod()->get_option('has_packages_mode') === 'yes') {
6262
$packageData = json_decode($data->get_param('package_data'));
@@ -106,13 +106,17 @@ function setPackage($data, $orderId, $currency) {
106106
}
107107

108108
$param['packages'] = [
109-
'number' => $orderId,
110-
'length' => $length,
111-
'width' => $width,
112-
'height' => $height,
113-
'weight' => $totalWeight,
114-
'items' => $itemsData,
109+
'number' => $orderId,
110+
'length' => $length,
111+
'width' => $width,
112+
'height' => $height,
113+
'weight' => $totalWeight,
114+
'comment' => 'приложена опись',
115115
];
116+
117+
if ($tariffType === Tariff::SHOP_TYPE) {
118+
$param['packages']['items'] = $itemsData;
119+
}
116120
}
117121

118122
return $param;

src/Actions/CreateOrder.php

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,26 @@
1616
class CreateOrder {
1717
protected $api;
1818

19-
public function __construct()
20-
{
19+
public function __construct() {
2120
$this->api = new CdekApi();
2221
}
2322

24-
public function createOrder($data)
25-
{
23+
public function createOrder($data) {
2624
if (Helper::getActualShippingMethod()->get_option('has_packages_mode') !== 'yes') {
2725
$validate = ValidateCreateOrderForm::validate($data);
2826
if (!$validate->state) {
2927
return $validate->response();
3028
}
3129
}
3230

33-
$orderId = $data->get_param('package_order_id');
34-
$postOrderData = OrderMetaData::getMetaByOrderId($orderId);
35-
$param = setPackage($data, $orderId, $postOrderData['currency']); //data передается в сыром виде
36-
$order = wc_get_order($orderId);
37-
$cityCode = getCityCode($postOrderData['city_code'], $order);
38-
31+
$orderId = $data->get_param('package_order_id');
32+
$order = wc_get_order($orderId);
33+
$postOrderData = OrderMetaData::getMetaByOrderId($orderId);
3934
$postOrderData['tariff_code'] = CheckoutHelper::getOrderShippingMethod($order)->get_meta('tariff_code');
35+
$postOrderData['type'] = Tariff::getTariffType($postOrderData['tariff_code']);
36+
$param = setPackage($data, $orderId, $postOrderData['currency'],
37+
$postOrderData['type']); //data передается в сыром виде
38+
$cityCode = getCityCode($postOrderData['city_code'], $order);
4039

4140
$validate = ValidateCityCode::validate($cityCode);
4241
if (!$validate->state) {
@@ -57,9 +56,9 @@ public function createOrder($data)
5756
OrderMetaData::updateMetaByOrderId($orderId, $postOrderData);
5857

5958
return [
60-
'state' => true,
61-
'code' => $cdekNumber,
62-
'door' => Tariff::isTariffFromDoor($postOrderData['tariff_id']),
59+
'state' => true,
60+
'code' => $cdekNumber,
61+
'door' => Tariff::isTariffFromDoor($postOrderData['tariff_id']),
6362
];
6463
}
6564

@@ -80,6 +79,8 @@ public function createRequestData($postOrderData, $order, $param) {
8079
];
8180
}
8281

82+
$param['type'] = $postOrderData['type'];
83+
8384
$param['recipient'] = [
8485
'name' => $order->get_billing_first_name().' '.$order->get_billing_last_name(),
8586
'email' => $order->get_billing_email(),
@@ -89,36 +90,27 @@ public function createRequestData($postOrderData, $order, $param) {
8990
];
9091

9192
if (Helper::getActualShippingMethod()->get_option('international_mode') === 'yes') {
92-
$param['recipient']['passport_series'] = $order->get_meta('_passport_series', true);
93-
$param['recipient']['passport_number'] = $order->get_meta('_passport_number', true);
93+
$param['recipient']['passport_series'] = $order->get_meta('_passport_series', true);
94+
$param['recipient']['passport_number'] = $order->get_meta('_passport_number', true);
9495
$param['recipient']['passport_date_of_issue'] = $order->get_meta('_passport_date_of_issue', true);
9596
$param['recipient']['passport_organization'] = $order->get_meta('_passport_organization', true);
9697
$param['recipient']['tin'] = $order->get_meta('_tin', true);
9798
$param['recipient']['passport_date_of_birth'] = $order->get_meta('_passport_date_of_birth', true);
9899
}
99100

100101
$param['tariff_code'] = $postOrderData['tariff_code'];
101-
$param['print'] = 'waybill';
102102

103103
$selectedPaymentMethodId = $order->get_payment_method();
104104
if ($selectedPaymentMethodId === 'cod') {
105-
$codPriceThreshold = (int)Helper::getActualShippingMethod()->get_option('stepcodprice');
106-
$total = $this->getOrderPrice($order);
105+
$codPriceThreshold = (int) Helper::getActualShippingMethod()->get_option('stepcodprice');
106+
$total = $this->getOrderPrice($order);
107107
if ($codPriceThreshold === 0 || $codPriceThreshold > $total) {
108108
$param['delivery_recipient_cost'] = [
109109
'value' => $order->get_shipping_total(),
110110
];
111111
}
112112
}
113113

114-
115-
// if ($selectedPaymentMethodId === 'cod' && $codPriceThreshold > 0) {
116-
// $param['delivery_recipient_cost_adv'] = [
117-
// 'sum' => $order->get_shipping_total(),
118-
// 'threshold' => $codPriceThreshold
119-
// ];
120-
// }
121-
122114
return $param;
123115
}
124116

@@ -128,8 +120,10 @@ public function createRequestData($postOrderData, $order, $param) {
128120
* @return string
129121
*/
130122
protected function getOrderPrice($order): string {
131-
$total = number_format((float) $order->get_total() - $order->get_total_tax() - $order->get_total_shipping() - $order->get_shipping_tax(),
132-
wc_get_price_decimals(), '.', '');
123+
$total = number_format((float) $order->get_total() -
124+
$order->get_total_tax() -
125+
$order->get_total_shipping() -
126+
$order->get_shipping_tax(), wc_get_price_decimals(), '.', '');
133127

134128
return $total;
135129
}

src/CdekApi.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(?int $shippingInstanceId = null) {
2828
$this->apiUrl = $this->getApiUrl();
2929
$this->httpClient = new HttpClient();
3030

31-
if ($this->deliveryMethod->get_option('test_mode') === 'yes') {
31+
if (!isset($_ENV['CDEK_REST_API']) && $this->deliveryMethod->get_option('test_mode') === 'yes') {
3232
$this->clientId = Config::TEST_CLIENT_ID;
3333
$this->clientSecret = Config::TEST_CLIENT_SECRET;
3434
} else {
@@ -38,7 +38,11 @@ public function __construct(?int $shippingInstanceId = null) {
3838
}
3939

4040
private function getApiUrl() {
41-
return defined('CDEK_REST_API') ? CDEK_REST_API : ($this->deliveryMethod->get_option('test_mode') === 'yes' ? Config::TEST_API_URL : Config::API_URL);
41+
if ($this->deliveryMethod->get_option('test_mode') === 'yes') {
42+
return $_ENV['CDEK_REST_API'] ?? Config::TEST_API_URL;
43+
}
44+
45+
return Config::API_URL;
4246
}
4347

4448
public function checkAuth(): bool {
@@ -89,14 +93,17 @@ public function createOrder($param) {
8993
$param['sender']['passport_organization'] = $this->deliveryMethod->get_option('passport_organization');
9094
$param['sender']['tin'] = $this->deliveryMethod->get_option('tin');
9195
$param['sender']['passport_date_of_birth'] = $this->deliveryMethod->get_option('passport_date_of_birth');
96+
$param['sender']['name'] = $this->deliveryMethod->get_option('seller_name');
97+
$param['sender']['company'] = $this->deliveryMethod->get_option('seller_name');
98+
$param['sender']['phones'] = ['number' => $this->deliveryMethod->get_option('seller_phone')];
9299
$param['seller'] = ['address' => $this->deliveryMethod->get_option('seller_address')];
93100

94101
if (Tariff::isTariffFromOffice($param['tariff_code'])) {
95-
$param['shipment_point'] = explode(' ',$this->deliveryMethod->get_option('pvz_code'))[1];
102+
$param['shipment_point'] = explode(', ', $this->deliveryMethod->get_option('pvz_code'))[1];
96103
} else {
97104
$param['from_location'] = [
98-
'address' => $this->deliveryMethod->get_option('address'),
99-
'city' => $this->deliveryMethod->get_option('address'),
105+
'address' => $this->deliveryMethod->get_option('address'),
106+
'city' => $this->deliveryMethod->get_option('address'),
100107
'country_code' => $this->deliveryMethod->get_option('country') ?? 'RU',
101108
];
102109
}
@@ -138,13 +145,15 @@ public function deleteOrder($uuid) {
138145
public function calculate($deliveryParam) {
139146
$url = $this->apiUrl.self::CALC_PATH;
140147

141-
$senderCity = $this->deliveryMethod->get_option('pvz_code') ? explode(' ',
142-
$this->deliveryMethod->get_option('pvz_code'))[0] : $this->deliveryMethod->get_option('address');
148+
$senderCity = $this->deliveryMethod->get_option('pvz_code') ?
149+
explode(', ', $this->deliveryMethod->get_option('pvz_code'))[0] :
150+
$this->deliveryMethod->get_option('address');
143151

144152
return HttpClient::sendCdekRequest($url, 'POST', $this->getToken(), [
153+
'type' => $deliveryParam['type'],
145154
'from_location' => [
146-
'address' => $senderCity,
147-
'city' => $senderCity,
155+
'address' => $senderCity,
156+
'city' => $senderCity,
148157
'country_code' => $this->deliveryMethod->get_option('country') ?? 'RU',
149158
],
150159
'to_location' => [
@@ -156,8 +165,8 @@ public function calculate($deliveryParam) {
156165
'width' => $deliveryParam['package_data']['width'],
157166
'height' => $deliveryParam['package_data']['height'],
158167
],
159-
'services' => array_key_exists('selected_services',
160-
$deliveryParam) ? $deliveryParam['selected_services'] : [],
168+
'services' => array_key_exists('selected_services', $deliveryParam) ?
169+
$deliveryParam['selected_services'] : [],
161170
]);
162171
}
163172

src/CdekShippingMethod.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function init_form_fields(): void {
4545
],
4646
];
4747

48-
$this->form_fields = [
48+
$this->form_fields = [
4949
'auth_block_name' => [
5050
'title' => '<h3 style="text-align: center;">Авторизация</h3>',
5151
'type' => 'title',
@@ -133,14 +133,14 @@ public function init_form_fields(): void {
133133
'description' => 'Адрес компании грузоотправителя для международных заказов',
134134
],
135135
'passport_series' => [
136-
'title' => 'Серия паспорта',
137-
'type' => 'text',
136+
'title' => 'Серия паспорта',
137+
'type' => 'text',
138138
],
139139
'passport_number' => [
140140
'title' => 'Номер паспорта',
141141
'type' => 'text',
142142
'custom_attributes' => [
143-
'pattern' => '\d*',
143+
'pattern' => '\d*',
144144
],
145145
],
146146
'passport_date_of_issue' => [
@@ -157,7 +157,7 @@ public function init_form_fields(): void {
157157
'type' => 'text',
158158
'desc_tip' => true,
159159
'custom_attributes' => [
160-
'pattern' => '\d*',
160+
'pattern' => '\d*',
161161
],
162162
],
163163
'passport_date_of_birth' => [
@@ -212,10 +212,10 @@ public function init_form_fields(): void {
212212
'pvz_code' => [
213213
'type' => 'hidden',
214214
],
215-
'address' => [
215+
'address' => [
216216
'type' => 'hidden',
217217
],
218-
'country' => [
218+
'country' => [
219219
'type' => 'hidden',
220220
],
221221
'package_setting_block_name' => [
@@ -401,11 +401,13 @@ public function init_form_fields(): void {
401401
}
402402

403403
public function calculate_shipping($package = []): void {
404-
$deliveryCalc = new DeliveryCalc();
405-
if ($deliveryCalc->calculate($package, $this->id)) {
406-
foreach ($deliveryCalc->rates as $rate) {
407-
$this->add_rate($rate);
408-
}
409-
}
404+
$deliveryCalc = new DeliveryCalc();
405+
if (!$deliveryCalc->calculate($package, $this->id)) {
406+
return;
407+
}
408+
409+
foreach ($deliveryCalc->getRates() as $rate) {
410+
$this->add_rate($rate);
411+
}
410412
}
411413
}

0 commit comments

Comments
 (0)