From 9944e5309c0e422eb8b3407c8e767e04f61351bd Mon Sep 17 00:00:00 2001 From: Arsen Mirzaev Tatyano-Muradovich Date: Sat, 14 Dec 2024 00:47:47 +0300 Subject: [PATCH] improved delivery + door + resolved #23 --- examples/nginx/server.conf | 5 + .../system/controllers/delivery.php | 570 +++++++++++++++--- mirzaev/arming_bot/system/models/delivery.php | 92 +++ .../system/models/enumerations/delivery.php | 18 + mirzaev/arming_bot/system/models/telegram.php | 33 +- mirzaev/arming_bot/system/public/index.php | 4 +- mirzaev/arming_bot/system/public/js/core.js | 2 +- .../system/public/js/modules/account.mjs | 2 +- .../system/public/js/modules/cart.mjs | 2 +- .../system/public/js/modules/catalog.mjs | 2 +- .../system/public/js/modules/damper.mjs | 4 +- .../system/public/js/modules/delivery.mjs | 219 ++++++- .../system/public/js/modules/session.mjs | 2 +- .../system/public/themes/default/css/cart.css | 21 +- .../system/public/themes/default/css/main.css | 1 + .../elements/deliveries/cdek/javascript.html | 124 +++- .../elements/deliveries/cdek/section.html | 16 +- 17 files changed, 959 insertions(+), 158 deletions(-) create mode 100755 mirzaev/arming_bot/system/models/delivery.php create mode 100755 mirzaev/arming_bot/system/models/enumerations/delivery.php diff --git a/examples/nginx/server.conf b/examples/nginx/server.conf index 0bc9f48..89d70b6 100644 --- a/examples/nginx/server.conf +++ b/examples/nginx/server.conf @@ -17,6 +17,11 @@ server { try_files $uri $uri/ /index.php; } + location /api/cdek { + rewrite ^/api/cdek(.*)$ /$1 break; + index cdek.php; + } + location ~ /(?categories|products) { root /var/www/arming_bot/mirzaev/arming_bot/system/storage; try_files $uri =404; diff --git a/mirzaev/arming_bot/system/controllers/delivery.php b/mirzaev/arming_bot/system/controllers/delivery.php index 5377d4a..04b3608 100755 --- a/mirzaev/arming_bot/system/controllers/delivery.php +++ b/mirzaev/arming_bot/system/controllers/delivery.php @@ -7,7 +7,8 @@ namespace mirzaev\arming_bot\controllers; // Files of the project use mirzaev\arming_bot\controllers\core, mirzaev\arming_bot\models\cart, - mirzaev\arming_bot\models\deliveries\cdek; + mirzaev\arming_bot\models\deliveries\cdek, + mirzaev\arming_bot\models\enumerations\delivery as company; // Framework for PHP use mirzaev\minimal\http\enumerations\content, @@ -58,19 +59,25 @@ final class delivery extends core * Validate and write delivery data to account and session buffers * * @param string|null $company Name of delivery company - * @param string|null $location Location (city) - * @param string|null $street Address (street with house) - * @param string|int|null $type Type or taruff + * @param string|null $location Receiver location (city) + * @param string|null $street Receiver address (street with house) + * @param string|int|null $apartament Receiver apartament + * @param string|null $type Delivery type ('office', 'door') + * @param string|int|null $tariff Delivery tariff + * @param string|int|float|null $longitude Receiver longitude + * @param string|int|float|null $latitude Receiver latitude * * @return null */ public function write( - /* string|company|null $company = null, */ string|null $company = null, - /* string|location|null $location = null, */ ?string $location = null, ?string $street = null, - string|int|null $type = null + string|int|null $apartament = null, + ?string $type = null, + string|int|null $tariff = null, + string|int|float|null $longitude = null, + string|int|float|null $latitude = null ): null { if (str_contains($this->request->headers['accept'], content::json->value)) { // Request for JSON response @@ -154,34 +161,46 @@ final class delivery extends core // Initialization buffer of delivery parameters $delivery = $this->account?->buffer['delivery'] ?? $this->session?->buffer['delivery'] ?? []; - if (isset($delivery[$normalized])) { - // Initialized delivery company data + // Writing readiness status to the buffer of the response + $response['ready'] = $this->model::ready(company::{$normalized}, $delivery[$normalized] ?? []); - if (!empty($delivery[$normalized]['location']) && !empty($delivery[$normalized]['street'])) { - // Required parameters initialized: company, location, street + if ($response['ready']) { + // Initialized required parameters - // Writing readiness status to the buffer of the response - $response['ready'] = true; + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $normalized . '_ready' => true])); - // Writing to the session buffer - $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_ready' => true])); + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => true])); - // Writing to the account buffer - $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => true])); + // Deinitializing unnecessary variables + unset($name); + } else { + // Not initialized required parameters - // Deinitializing unnecessary variables - unset($name); - } + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [ + $name_ready = 'delivery_' . $normalized . '_ready' => false, + $name_cost = 'delivery_' . $normalized . '_cost' => null, + $name_days = 'delivery_' . $normalized . '_days' => null + ])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [ + $name_ready => false, + $name_cost => null, + $name_days => null + ])); + + // Deinitializing unnecessary variables + unset($name_ready, $name_cost, $name_days); } // Deinitializing unnecessary variables - unset($delivery); + unset($delivery, $normalized); // Writing status of execution to the buffer of the response $response['status'] = 'success'; - - // Deinitializing variable for normalized value of the parameter - unset($normalized); } } else if (isset($location)) { // Received location name @@ -204,7 +223,7 @@ final class delivery extends core // Initialization buffer of delivery parameters $delivery = $this->account?->buffer['delivery'] ?? $this->session?->buffer['delivery'] ?? []; - if (isset($delivery['company'])) { + if (!empty($delivery['company'])) { // Initialized delivery company if (!empty($separated)) { @@ -215,14 +234,19 @@ final class delivery extends core // Normalizing location name foreach ($separated as $value) $normalized[] = mb_ucfirst($value); + /* foreach ($separated as $value) $normalized[] = $value; */ // Deinitializing unnecessary variables unset($separated, $value); + // Reinitializing result value of location name + $result = $normalized[0]; + // Declaring of universalized locations buffer $locations = null; if ($delivery['company'] === 'cdek') { + // CDEK // Searching for locations by name (first part with spaces before first comma or any non word symbol) $cdek = cdek::location($normalized[0], $this->errors['delivery'])?->items; @@ -230,11 +254,20 @@ final class delivery extends core foreach ($cdek ?? [] as $location) { // Iterating over found locations + // Initializing structure + $structure = [$location->country]; + + // Initializing additional instances in the structure + if (!empty($location->region) && $location->region !== $location->city) + $structure[] = $location->region; + if (!empty($location->sub_region) && $location->sub_region !== $location->region && $location->sub_region !== $location->city) + $structure[] = $location->sub_region; + // Universalizing and writing to locations buffer $locations[] = [ 'identifier' => $location->code, 'name' => $location->city, - 'structure' => [$location->country, $location->region, $location->sub_region], + 'structure' => $structure, 'longitude' => $location->longitude, 'latitude' => $location->latitude, 'data' => $location @@ -298,45 +331,46 @@ final class delivery extends core // Reinitializating locations from buffer of validated locations by input values $locations = $buffer; - /* if (count($locations) === 1) { */ - // Identificated location + /* if (count($locations) === 1) */ + if (count($locations) > 0) { + // Identificated location - // Writing to the session buffer - $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_location' => $locations[0]])); + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_location' => $locations[0]])); - // Writing to the account buffer - $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => $locations[0]])); + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => $locations[0]])); - // Deinitializing unnecessary variables - unset($name); + // Deinitializing unnecessary variables + unset($name); - // Writing result value of location name - $result = $locations[0]['name'] . ', ' . implode(', ', array_reverse($locations[0]['structure'])); + // Writing result value of location name + $result = $locations[0]['name'] . ', ' . implode(', ', array_reverse($locations[0]['structure'])); - // Writing location data to the buffer of the response - /* $response['location'] = [ + // Writing to the actual buffer copy + $delivery[$delivery['company']]['location'] = $result; + + // Writing location data to the buffer of the response + /* $response['location'] = [ 'identifier' => $locations[0]['identifier'], 'input' => $result ]; */ - // Writing status of execution to the buffer of the response - $response['status'] = 'success'; - - if (!empty($delivery[$delivery['company']]['street'])) { - // Required parameters initialized: company, location, street - // Writing readiness status to the buffer of the response - $response['ready'] = true; + $response['ready'] = $this->model::ready(company::{$delivery['company']}, $delivery[$delivery['company']] ?? []); - // Writing to the session buffer - $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_ready' => true])); + if ($response['ready']) { + // Initialized required parameters - // Writing to the account buffer - $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => true])); + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_ready' => true])); - // Deinitializing unnecessary variables - unset($name); - /* } else { + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => true])); + + // Deinitializing unnecessary variables + unset($name); + } else { // Not initialized required parameters // Writing to the session buffer @@ -355,16 +389,22 @@ final class delivery extends core // Deinitializing unnecessary variables unset($name_ready, $name_cost, $name_days); - } */ + } + + // Deinitializing unnecessary variables + unset($delivery); // Writing locations into response buffer $response['locations'] = []; + + // Writing status of execution to the buffer of the response + $response['status'] = 'success'; } else { // Not identificated location // Writing to the session buffer $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [ - $name_location = 'delivery_' . $delivery['company'] . '_location' => null, + $name_location = 'delivery_' . $delivery['company'] . '_location' => $result, $name_ready = 'delivery_' . $delivery['company'] . '_ready' => false, $name_cost = 'delivery_' . $delivery['company'] . '_cost' => null, $name_days = 'delivery_' . $delivery['company'] . '_days' => null @@ -372,7 +412,7 @@ final class delivery extends core // Writing to the account buffer $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [ - $name_location => null, + $name_location => $result, $name_ready => false, $name_cost => null, $name_days => null @@ -381,6 +421,9 @@ final class delivery extends core // Deinitializing unnecessary variables unset($name_location, $name_ready, $name_cost, $name_days); + // Writing status of execution to the buffer of the response (???) + $response['status'] = 'success'; + // Declaring buffer of data to send $buffer = []; @@ -458,10 +501,10 @@ final class delivery extends core unset($delivery, $normalized); // Writing to the session buffer - $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: ['delivery_location' => empty($result) ? $location : $result])); + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: ['delivery_location' => $result])); // Writing to the account buffer - $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: ['delivery_location' => empty($result) ? $location : $result])); + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: ['delivery_location' => $result])); // Deinitializing unnecessary variables unset($location, $result, $normalized); @@ -476,7 +519,7 @@ final class delivery extends core $normalized = ''; // Normalizing street - if (preg_match('/[\w\d\s\.\,]+/u', urldecode($street), $matches)) $normalized = mb_ucfirst($matches[0] ?? ''); + if (preg_match('/[\w\d\s\.\,\-]+/u', urldecode($street), $matches)) $normalized = mb_ucfirst($matches[0] ?? ''); // Deinitializing unnecessary variables unset($matches); @@ -496,11 +539,14 @@ final class delivery extends core // Deinitializing unnecessary variables unset($name); - if (!empty($normalized) && !empty($delivery[$delivery['company']]['location'])) { - // Required parameters initialized: company, location, street + // Writing to the actual buffer copy + $delivery[$delivery['company']]['street'] = $normalized; - // Writing readiness status to the buffer of the response - $response['ready'] = true; + // Writing readiness status to the buffer of the response + $response['ready'] = $this->model::ready(company::{$delivery['company']}, $delivery[$delivery['company']] ?? []); + + if ($response['ready']) { + // Initialized required parameters // Writing to the session buffer $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_ready' => true])); @@ -531,6 +577,9 @@ final class delivery extends core unset($name_ready, $name_cost, $name_days); } + // Deinitializing unnecessary variables + unset($delivery); + // Writing status of execution to the buffer of the response $response['status'] = 'success'; } @@ -553,7 +602,7 @@ final class delivery extends core } else if (isset($type)) { // Received delivery type - if ((is_string($type) && mb_strlen($type) < 30) || (is_int($type && $type < 10000))) { + if (mb_strlen($type) < 30) { // Validated delivery type // Declating variable for result value of delivery type @@ -562,8 +611,8 @@ final class delivery extends core // Declating variable for normalized value of delivery type $normalized = ''; - // Normalizing location name - if (preg_match('/[\w\d]+/', trim(urldecode($type)), $matches)) $normalized = $matches[0]; + // Normalizing delivery type + if (preg_match('/[\w]+/', trim(urldecode($type)), $matches)) $normalized = $matches[0]; // Deinitializing unnecessary variables unset($matches); @@ -574,34 +623,43 @@ final class delivery extends core if (isset($delivery['company'])) { // Initialized delivery company - if (!empty($normalized)) { - // Normalized delivery type + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_type' => $normalized])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => $normalized])); + + // Deinitializing unnecessary variables + unset($name); + + // Writing to the actual buffer copy + $delivery[$delivery['company']]['type'] = $normalized; + + // Writing result value of delivery type + $result = $normalized; + + // Deinitializing unnecessary variables + unset($normalized); + + // Writing readiness status to the buffer of the response + $response['ready'] = $this->model::ready(company::{$delivery['company']}, $delivery[$delivery['company']] ?? []); + + if ($response['ready']) { + // Initialized required parameters // Writing to the session buffer - $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_type' => $normalized])); + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_ready' => true])); // Writing to the account buffer - $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => $normalized])); + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => true])); // Deinitializing unnecessary variables unset($name); - - // Writing result value of delivery type - $result = $normalized; - - // Writing status of execution to the buffer of the response - $response['status'] = 'success'; - - // Deinitializing unnecessary variables - unset($normalized); } else { - // Value is empty after separating - // Not initialized required parameters // Writing to the session buffer $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [ - $name_type = 'delivery_' . $delivery['company'] . '_type' => null, $name_ready = 'delivery_' . $delivery['company'] . '_ready' => false, $name_cost = 'delivery_' . $delivery['company'] . '_cost' => null, $name_days = 'delivery_' . $delivery['company'] . '_days' => null @@ -609,15 +667,359 @@ final class delivery extends core // Writing to the account buffer $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [ - $name_type => null, $name_ready => false, $name_cost => null, $name_days => null ])); // Deinitializing unnecessary variables - unset($name_type, $name_ready, $name_cost, $name_days); + unset($name_ready, $name_cost, $name_days); } + + // Deinitializing unnecessary variables + unset($delivery); + + // Writing status of execution to the buffer of the response + $response['status'] = 'success'; + } + + // Deinitializing unnecessary variables + unset($delivery, $result, $normalized); + } + } else if (isset($tariff)) { + // Received delivery tariff + + if ((is_string($tariff) && mb_strlen($tariff) < 30) || (is_int($tariff && $tariff < 10000))) { + // Validated delivery tariff + + // Declating variable for result value of delivery tariff + $result = ''; + + // Declating variable for normalized value of delivery tariff + $normalized = ''; + + // Normalizing delivery tariff + if (preg_match('/[\w\d]+/', trim(urldecode($tariff)), $matches)) $normalized = $matches[0]; + + // Deinitializing unnecessary variables + unset($matches); + + // Initialization buffer of delivery parameters + $delivery = $this->account?->buffer['delivery'] ?? $this->session?->buffer['delivery'] ?? []; + + if (isset($delivery['company'])) { + // Initialized delivery company + + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_tariff' => $normalized])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => $normalized])); + + // Deinitializing unnecessary variables + unset($name); + + // Writing to the actual buffer copy + $delivery[$delivery['company']]['tariff'] = $normalized; + + // Writing result value of delivery tariff + $result = $normalized; + + // Deinitializing unnecessary variables + unset($normalized); + + // Writing readiness status to the buffer of the response + $response['ready'] = $this->model::ready(company::{$delivery['company']}, $delivery[$delivery['company']] ?? []); + + if ($response['ready']) { + // Initialized required parameters + + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_ready' => true])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => true])); + + // Deinitializing unnecessary variables + unset($name); + } else { + // Not initialized required parameters + + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [ + $name_ready = 'delivery_' . $delivery['company'] . '_ready' => false, + $name_cost = 'delivery_' . $delivery['company'] . '_cost' => null, + $name_days = 'delivery_' . $delivery['company'] . '_days' => null + ])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [ + $name_ready => false, + $name_cost => null, + $name_days => null + ])); + + // Deinitializing unnecessary variables + unset($name_ready, $name_cost, $name_days); + } + + // Deinitializing unnecessary variables + unset($delivery); + + // Writing status of execution to the buffer of the response + $response['status'] = 'success'; + } + + // Deinitializing unnecessary variables + unset($delivery, $result, $normalized); + } + } else if (isset($longitude)) { + // Received delivery receiver longitude + + // Declating variable for result value of delivery receiver longitude + $result = ''; + + // Declating variable for normalized value of delivery receiver longitude + $normalized = ''; + + // Normalizing receiver longitude + $normalized = filter_var($longitude, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); + + if ($normalized >= -180 && $normalized <= 180) { + // Validated delivery receiver longitude + + // Initialization buffer of delivery parameters + $delivery = $this->account?->buffer['delivery'] ?? $this->session?->buffer['delivery'] ?? []; + + if (isset($delivery['company'])) { + // Initialized delivery company + + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_longitude' => $normalized])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => $normalized])); + + // Deinitializing unnecessary variables + unset($name); + + // Writing to the actual buffer copy + $delivery[$delivery['company']]['longitude'] = $normalized; + + // Writing result value of delivery receiver longitude + $result = $normalized; + + // Deinitializing unnecessary variables + unset($normalized); + + // Writing readiness status to the buffer of the response + $response['ready'] = $this->model::ready(company::{$delivery['company']}, $delivery[$delivery['company']] ?? []); + + if ($response['ready']) { + // Initialized required parameters + + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_ready' => true])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => true])); + + // Deinitializing unnecessary variables + unset($name); + } else { + // Not initialized required parameters + + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [ + $name_ready = 'delivery_' . $delivery['company'] . '_ready' => false, + $name_cost = 'delivery_' . $delivery['company'] . '_cost' => null, + $name_days = 'delivery_' . $delivery['company'] . '_days' => null + ])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [ + $name_ready => false, + $name_cost => null, + $name_days => null + ])); + + // Deinitializing unnecessary variables + unset($name_ready, $name_cost, $name_days); + } + + // Deinitializing unnecessary variables + unset($delivery); + + // Writing status of execution to the buffer of the response + $response['status'] = 'success'; + } + + // Deinitializing unnecessary variables + unset($delivery, $result, $normalized); + } + } else if (isset($latitude)) { + // Received delivery receiver latitude + + // Declating variable for result value of delivery receiver latitude + $result = ''; + + // Declating variable for normalized value of delivery receiver latitude + $normalized = ''; + + // Normalizing receiver latitude + $normalized = filter_var($latitude, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); + + if ($normalized >= -90 && $normalized <= 90) { + // Validated delivery receiver latitude + + // Initialization buffer of delivery parameters + $delivery = $this->account?->buffer['delivery'] ?? $this->session?->buffer['delivery'] ?? []; + + if (isset($delivery['company'])) { + // Initialized delivery company + + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_latitude' => $normalized])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => $normalized])); + + // Deinitializing unnecessary variables + unset($name); + + // Writing to the actual buffer copy + $delivery[$delivery['company']]['latitude'] = $normalized; + + // Writing result value of delivery receiver latitude + $result = $normalized; + + // Deinitializing unnecessary variables + unset($normalized); + + // Writing readiness status to the buffer of the response + $response['ready'] = $this->model::ready(company::{$delivery['company']}, $delivery[$delivery['company']] ?? []); + + if ($response['ready']) { + // Initialized required parameters + + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_ready' => true])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => true])); + + // Deinitializing unnecessary variables + unset($name); + } else { + // Not initialized required parameters + + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [ + $name_ready = 'delivery_' . $delivery['company'] . '_ready' => false, + $name_cost = 'delivery_' . $delivery['company'] . '_cost' => null, + $name_days = 'delivery_' . $delivery['company'] . '_days' => null + ])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [ + $name_ready => false, + $name_cost => null, + $name_days => null + ])); + + // Deinitializing unnecessary variables + unset($name_ready, $name_cost, $name_days); + } + + // Deinitializing unnecessary variables + unset($delivery); + + // Writing status of execution to the buffer of the response + $response['status'] = 'success'; + } + + // Deinitializing unnecessary variables + unset($delivery, $result, $normalized); + } + } else if (isset($apartament)) { + // Received delivery receiver apartament + + // Declating variable for result value of delivery receiver apartament + $result = ''; + + // Declating variable for normalized value of delivery receiver apartament + $normalized = ''; + + // Normalizing receiver apartament + $normalized = filter_var($apartament, FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_ALLOW_FRACTION); + + if ($normalized > 0 && $normalized <= 1000) { + // Validated delivery receiver apartament + + // Initialization buffer of delivery parameters + $delivery = $this->account?->buffer['delivery'] ?? $this->session?->buffer['delivery'] ?? []; + + if (isset($delivery['company'])) { + // Initialized delivery company + + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_apartament' => $normalized])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => $normalized])); + + // Deinitializing unnecessary variables + unset($name); + + // Writing to the actual buffer copy + $delivery[$delivery['company']]['apartment'] = $normalized; + + // Writing result value of delivery receiver apartament + $result = $normalized; + + // Deinitializing unnecessary variables + unset($normalized); + + // Writing readiness status to the buffer of the response + $response['ready'] = $this->model::ready(company::{$delivery['company']}, $delivery[$delivery['company']] ?? []); + + if ($response['ready']) { + // Initialized required parameters + + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [$name = 'delivery_' . $delivery['company'] . '_ready' => true])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [$name => true])); + + // Deinitializing unnecessary variables + unset($name); + } else { + // Not initialized required parameters + + // Writing to the session buffer + $this->core->request(new request('PATCH', '/session/write', protocol::http_3, parameters: [ + $name_ready = 'delivery_' . $delivery['company'] . '_ready' => false, + $name_cost = 'delivery_' . $delivery['company'] . '_cost' => null, + $name_days = 'delivery_' . $delivery['company'] . '_days' => null + ])); + + // Writing to the account buffer + $this->core->request(new request('PATCH', '/account/write', protocol::http_3, parameters: [ + $name_ready => false, + $name_cost => null, + $name_days => null + ])); + + // Deinitializing unnecessary variables + unset($name_ready, $name_cost, $name_days); + } + + // Deinitializing unnecessary variables + unset($delivery); + + // Writing status of execution to the buffer of the response + $response['status'] = 'success'; } // Deinitializing unnecessary variables @@ -709,7 +1111,7 @@ final class delivery extends core from_street: 'Екатерининская 116', // @todo issues #13 to_location: $delivery[$delivery['company']]['location']['identifier'], to_street: $delivery[$delivery['company']]['street'], - tariff: $delivery[$delivery['company']]['type'] ?? 368, + tariff: $delivery[$delivery['company']]['tariff'] ?? 368, products: $formatted, date: new datetime(), // @todo weekdays only? + timezones errors: $this->errors['delivery'] diff --git a/mirzaev/arming_bot/system/models/delivery.php b/mirzaev/arming_bot/system/models/delivery.php new file mode 100755 index 0000000..6e018ed --- /dev/null +++ b/mirzaev/arming_bot/system/models/delivery.php @@ -0,0 +1,92 @@ + + */ +final class delivery extends core +{ + /** + * Ready + * + * @param company $company Delivery company + * @param array $parameters Delivery company parameters + * @param array &$errors Registry of errors + * + * @return bool|null Is delivery data ready for creating order? + */ + public static function ready(company $company, array $parameters, array &$errors = []): ?bool + { + try { + if (!empty($parameters)) { + // Initialized delivery company data + + if ( + !empty($parameters['type']) + && !empty($parameters['tariff']) + && (!empty($parameters['location']['name']) || (!empty($parameters['longitude']) && !empty($parameters['latitude']))) + && !empty($parameters['street']) + ) { + // Initialized required parameters: company, type, tariff, location (or longitude with latitude), street + + if ($parameters['type'] === 'door') { + // Delivery to the door + + if (!empty($parameters['apartament'])) { + // Required parameters initialized: apartament + + // Exit (success) + return true; + } else { + // Not initialized required parameters + + // Exit (success) + return false; + } + } else if ($parameters['type'] === 'office') { + // Delivery to the office + + // Exit (success) + return true; + } else { + // Not identified delivery type + + // Exit (success) + return false; + } + } else { + // Not initialized required parameters + + // Exit (success) + return false; + } + } + } catch (exception $e) { + // Writing to the registry of errors + $errors[] = [ + 'text' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'stack' => $e->getTrace() + ]; + } + + // Exit (fail) + return null; + } +} diff --git a/mirzaev/arming_bot/system/models/enumerations/delivery.php b/mirzaev/arming_bot/system/models/enumerations/delivery.php new file mode 100755 index 0000000..85f445b --- /dev/null +++ b/mirzaev/arming_bot/system/models/enumerations/delivery.php @@ -0,0 +1,18 @@ + + */ +enum delivery: string +{ + case cdek = 'CDEK'; +} diff --git a/mirzaev/arming_bot/system/models/telegram.php b/mirzaev/arming_bot/system/models/telegram.php index 1c270c2..9f42c55 100755 --- a/mirzaev/arming_bot/system/models/telegram.php +++ b/mirzaev/arming_bot/system/models/telegram.php @@ -99,19 +99,32 @@ final class telegram extends core */ public static function menu(context $ctx): void { - // Инициализация клавиатуры - $keyboard = [ + // Declaring keyboard buttond + $keyboard = []; + + /* if ($ctx->get('account')) { + // Authenticated */ + + // Writing the catalog button + $keyboard[] = [ [ - ['text' => '🛒 Каталог', 'web_app' => ['url' => 'https://arming.dev.mirzaev.sexy']] - ], - [ - ['text' => '🏛️ О компании'], - ['text' => '💬 Контакты'] - ], - [ - ['text' => '🎯 Сообщество'] + 'text' => '🛒 Каталог', + 'web_app' => ['url' => 'https://arming.dev.mirzaev.sexy'] ] ]; + /* } */ + + $keyboard = array_merge( + $keyboard, + [[ + ['text' => '🏛️ О компании'], + ['text' => '💬 Контакты'] + ]], + [[ + ['text' => '🎯 Сообщество'] + ]] + ); + if ($ctx->get('account')?->access['settings']) $keyboard[] = [['text' => '⚙️ Настройки']]; diff --git a/mirzaev/arming_bot/system/public/index.php b/mirzaev/arming_bot/system/public/index.php index d095446..ae7221e 100755 --- a/mirzaev/arming_bot/system/public/index.php +++ b/mirzaev/arming_bot/system/public/index.php @@ -43,8 +43,8 @@ $core->router ->write('/account/write', new route('account', 'write', 'account'), 'PATCH') ->write('/session/write', new route('session', 'write', 'session'), 'PATCH') ->write('/session/connect/telegram', new route('session', 'telegram', 'session'), 'PUT') - ->write('/delivery/write', new route('delivery', 'write'), 'PATCH') - ->write('/delivery/calculate', new route('delivery', 'calculate'), 'GET'); + ->write('/delivery/write', new route('delivery', 'write', 'delivery'), 'PATCH') + ->write('/delivery/calculate', new route('delivery', 'calculate', 'delivery'), 'GET'); // Handling request $core->start(); diff --git a/mirzaev/arming_bot/system/public/js/core.js b/mirzaev/arming_bot/system/public/js/core.js index 45ad9ba..2b872ee 100755 --- a/mirzaev/arming_bot/system/public/js/core.js +++ b/mirzaev/arming_bot/system/public/js/core.js @@ -351,7 +351,7 @@ core.modules.connect("damper").then(() => { damper: core.damper( (...variables) => core.buffer.write.system(...variables), 300, - 3, + 2, ), }, ); diff --git a/mirzaev/arming_bot/system/public/js/modules/account.mjs b/mirzaev/arming_bot/system/public/js/modules/account.mjs index 9653b1f..948efc8 100755 --- a/mirzaev/arming_bot/system/public/js/modules/account.mjs +++ b/mirzaev/arming_bot/system/public/js/modules/account.mjs @@ -148,7 +148,7 @@ core.modules.connect("damper").then(() => { damper: core.damper( (...variables) => account.buffer.write.system(...variables), 300, - 3, + 2, ), }, ); diff --git a/mirzaev/arming_bot/system/public/js/modules/cart.mjs b/mirzaev/arming_bot/system/public/js/modules/cart.mjs index c690cfb..093f5e0 100755 --- a/mirzaev/arming_bot/system/public/js/modules/cart.mjs +++ b/mirzaev/arming_bot/system/public/js/modules/cart.mjs @@ -494,7 +494,7 @@ core.modules.connect("damper").then(() => { (...variables) => cart.product.system(...variables).then(cart.summary.system), 300, - 6, + 5, ), }, ); diff --git a/mirzaev/arming_bot/system/public/js/modules/catalog.mjs b/mirzaev/arming_bot/system/public/js/modules/catalog.mjs index d18617c..35eea67 100755 --- a/mirzaev/arming_bot/system/public/js/modules/catalog.mjs +++ b/mirzaev/arming_bot/system/public/js/modules/catalog.mjs @@ -226,7 +226,7 @@ core.modules.connect("damper").then(() => { damper: core.damper( (...variables) => catalog.product.system(...variables), 200, - 2, + 1, ), }, ); diff --git a/mirzaev/arming_bot/system/public/js/modules/damper.mjs b/mirzaev/arming_bot/system/public/js/modules/damper.mjs index d0b0afc..605834f 100755 --- a/mirzaev/arming_bot/system/public/js/modules/damper.mjs +++ b/mirzaev/arming_bot/system/public/js/modules/damper.mjs @@ -49,7 +49,7 @@ export default function damper(func, timeout = 300, force) { // Requested execution with ignoring the timer // Deleting the force argument - if (typeof force === "number") args.splice(force - 1, force); + if (typeof force === "number") args.splice(force, force); // Writing promise handlers into the arguments variable args.push(resolve, reject); @@ -60,7 +60,7 @@ export default function damper(func, timeout = 300, force) { // Normal execution // Deleting the force argument - if (typeof force === "number") args.splice(force - 1, force); + if (typeof force === "number") args.splice(force, force); // Writing promise handlers into the arguments variable args.push(resolve, reject); diff --git a/mirzaev/arming_bot/system/public/js/modules/delivery.mjs b/mirzaev/arming_bot/system/public/js/modules/delivery.mjs index b1faf72..5a78c22 100755 --- a/mirzaev/arming_bot/system/public/js/modules/delivery.mjs +++ b/mirzaev/arming_bot/system/public/js/modules/delivery.mjs @@ -22,11 +22,12 @@ export default class delivery { * Choose a delivery company * * @param {HTMLInputElement} company The button for choose a delivery company (radio) - * @param {bool} force Ignore the damper? (false) + * @param {bool} [calculate=true] Calculate delivery if the server has confirmed the data is ready + * @param {bool} [force=false] Ignore the damper? (false) * * @return {bool} Did the execution complete without errors? */ - static company = (company, force = false) => { + static company = (company, calculate = true, force = false) => { if (company instanceof HTMLInputElement) { // Initialized the delivery company (radio) element @@ -107,7 +108,7 @@ export default class delivery { } // Calculating delivery - this.calculate(); + if (calculate) this.calculate(); if (json.company) { // Received company property @@ -190,6 +191,7 @@ export default class delivery { section.outerHTML = json.delivery.html; // Reinitializing the deliveries
element + // section = document.getElementById("address"); section = deliveries.nextElementSibling; for (const javascript of json.delivery.javascript) { @@ -273,11 +275,20 @@ export default class delivery { * Write name of the location for delivery * * @param {HTMLInputElement} location The input element with name of the location - * @param {bool} force Ignore the damper? (false) + * @param {bool} [first=false] Select first element in element, if received + * @param {bool} [calculate=false] Calculate delivery if the server has confirmed the data is ready + * @param {bool} [map=true] Move the map cursor to the new coordinates after calculating? + * @param {bool} [force=false] Ignore the damper? (false) * * @return {bool} Did the execution complete without errors? */ - static location = (location, force = false) => { + static location = ( + location, + first = false, + calculate = true, + map = true, + force = false, + ) => { if (location instanceof HTMLInputElement) { // Initialized the location element @@ -349,7 +360,7 @@ export default class delivery { } // Calculating delivery - this.calculate(); + if (calculate) this.calculate(map); if (json.location) { // Received location @@ -386,12 +397,12 @@ export default class delivery { } } + // Initializing the locations suggestions element + let locations = document.getElementById("locations"); + if (json.locations) { // Reveived locations suggestions - // Initializing the locations suggestions element - let locations = document.getElementById("locations"); - if (!(locations instanceof HTMLDataListElement)) { // Not initialized the locations suggestions element @@ -410,6 +421,16 @@ export default class delivery { // Reinitializing the locations suggestions element locations = document.getElementById("locations"); + + if (first && locations.firstelementchild?.value) { + // Requested selecting first