From 22a63001a40ecd1bdead714f62c0b3cb3b7d8573 Mon Sep 17 00:00:00 2001 From: Kamel Khelifa Date: Tue, 25 Jun 2024 10:48:31 +0200 Subject: [PATCH] =?UTF-8?q?-=20Ajout=20de=20la=20synchronisation=20des=20M?= =?UTF-8?q?ode(s)=20d=E2=80=99exp=C3=A9dition=20par=20zone=20-=20Ajout=20d?= =?UTF-8?q?u=20choix=20de=20l'entrepot=20synchronis=C3=A9e=20en=20fonction?= =?UTF-8?q?=20du=20choix=20du=20mode=20d'expedition=20-=20Correction=20de?= =?UTF-8?q?=20la=20synchro=20des=20meta-donn=C3=A9es=20vers=20woocommerce?= =?UTF-8?q?=20en=20convertissant=20en=20string=20-=20Modification=20de=20l?= =?UTF-8?q?'ecran=20de=20parametrage=20de=20de=20l'onglet=20"stocks"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChangeLog.md | 9 +- VERSION | 2 +- admin/stock.php | 273 +++++++++++---- class/business/eCommerceSynchro.class.php | 174 ++++++++-- class/data/eCommerceRemoteAccess.class.php | 27 ++ ...ommerceRemoteShippingZoneMethods.class.php | 315 ++++++++++++++++++ .../eCommerceRemoteShippingZones.class.php | 252 ++++++++++++++ class/data/eCommerceSite.class.php | 2 +- ...eCommerceRemoteAccessWoocommerce.class.php | 102 +++++- langs/en_US/ecommerce.lang | 6 +- langs/en_US/woocommerce.lang | 2 +- langs/fr_FR/ecommerce.lang | 21 +- langs/fr_FR/woocommerce.lang | 4 +- lib/eCommerce.lib.php | 107 ++++++ ...rceng_remote_shipping_zone_methods.key.sql | 20 ++ ...ommerceng_remote_shipping_zone_methods.sql | 34 ++ ..._ecommerceng_remote_shipping_zones.key.sql | 19 ++ sql/llx_ecommerceng_remote_shipping_zones.sql | 28 ++ 18 files changed, 1285 insertions(+), 112 deletions(-) create mode 100644 class/data/eCommerceRemoteShippingZoneMethods.class.php create mode 100644 class/data/eCommerceRemoteShippingZones.class.php create mode 100644 sql/llx_ecommerceng_remote_shipping_zone_methods.key.sql create mode 100644 sql/llx_ecommerceng_remote_shipping_zone_methods.sql create mode 100644 sql/llx_ecommerceng_remote_shipping_zones.key.sql create mode 100644 sql/llx_ecommerceng_remote_shipping_zones.sql diff --git a/ChangeLog.md b/ChangeLog.md index 9090c1b..b418ebc 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,11 @@ # ChangeLog +## [14.0.4] - 21-06-2024 +- Ajout de la synchronisation des Mode(s) d’expédition par zone +- Ajout du choix de l'entrepot synchronisée en fonction du choix du mode d'expedition +- Correction de la synchro des meta-données vers woocommerce en convertissant en string +- Modification de l'ecran de parametrage de de l'onglet "stocks" + ## [14.0.3] - 17-06-2024 - Ajout version de PHP. @@ -901,6 +907,7 @@ - Initial version. -[Non Distribué]: https://github.com/OPEN-DSI/ecommerceng_woosync/compare/14.0.3...HEAD +[Non Distribué]: https://github.com/OPEN-DSI/ecommerceng_woosync/compare/14.0.4...HEAD +[14.0.4]: https://github.com/OPEN-DSI/ecommerceng_woosync/commits/14.0.4 [14.0.3]: https://github.com/OPEN-DSI/ecommerceng_woosync/commits/14.0.3 [14.0.2]: https://github.com/OPEN-DSI/ecommerceng_woosync/commits/14.0.2 \ No newline at end of file diff --git a/VERSION b/VERSION index de29f67..25f846f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -14.0.3 +14.0.4 \ No newline at end of file diff --git a/admin/stock.php b/admin/stock.php index 8d5ca4f..614e4ec 100644 --- a/admin/stock.php +++ b/admin/stock.php @@ -31,6 +31,7 @@ dol_include_once('/ecommerceng/lib/eCommerce.lib.php'); dol_include_once('/ecommerceng/class/html.formecommerceng.class.php'); dol_include_once('/ecommerceng/class/data/eCommerceRemoteWarehouses.class.php'); +dol_include_once('/ecommerceng/class/data/eCommerceRemoteShippingZoneMethods.class.php'); $langs->loadLangs(array("admin", "stocks", "ecommerce@ecommerceng", "woocommerce@ecommerceng", "opendsi@ecommerceng")); @@ -73,6 +74,16 @@ setEventMessages($eCommerceRemoteWarehouses->error, $eCommerceRemoteWarehouses->errors, 'errors'); } } +if (!empty($object->parameters['enable_warehouse_depending_on_shipping_zone_method'])) { + $eCommerceRemoteShippingZoneMethods = new eCommerceRemoteShippingZoneMethods($db); + $remote_shipping_zone_methods = $eCommerceRemoteShippingZoneMethods->get_all($object->id); + if (!is_array($remote_shipping_zone_methods)) { + setEventMessages($eCommerceRemoteShippingZoneMethods->error, $eCommerceRemoteShippingZoneMethods->errors, 'errors'); + } +} + +$movement_stock_on_validate_order = !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1; +$movement_stock_on_validate_invoice = !empty($conf->global->STOCK_CALCULATE_ON_BILL); /* @@ -82,9 +93,6 @@ if ($action == 'set_options') { $object->oldcopy = clone $object; - $object->parameters['order_actions']['valid_order_fk_warehouse'] = !empty($object->parameters['order_actions']['create_order']) ? GETPOST('valid_order_fk_warehouse', 'int') : -1; - $object->parameters['order_actions']['valid_invoice_fk_warehouse'] = !empty($object->parameters['order_actions']['create_invoice']) ? GETPOST('valid_invoice_fk_warehouse', 'int') : -1; - $object->parameters['order_actions']['valid_supplier_invoice_fk_warehouse'] = !empty($object->parameters['order_actions']['create_supplier_invoice']) ? GETPOST('valid_supplier_invoice_fk_warehouse', 'int') : -1; $object->stock_sync_direction = GETPOST('stock_sync_direction', 'az09'); $object->parameters['enable_warehouse_plugin_support'] = $object->stock_sync_direction != 'none' ? GETPOST('enable_warehouse_plugin_support', 'aZ09') : ''; if ($object->parameters['enable_warehouse_plugin_support'] == -1) $object->parameters['enable_warehouse_plugin_support'] = ''; @@ -124,6 +132,51 @@ header("Location: " . $_SERVER["PHP_SELF"] . '?id=' . $object->id); exit; +} elseif ( + (!empty($object->parameters['order_actions']['create_order']) && $movement_stock_on_validate_order) || + (!empty($object->parameters['order_actions']['create_invoice']) && $movement_stock_on_validate_invoice) +) { + if ($action == 'set_movement_stock_on_validate_options') { + $object->parameters['order_actions']['valid_order_fk_warehouse'] = !empty($object->parameters['order_actions']['create_order']) ? GETPOST('valid_order_fk_warehouse', 'int') : -1; + $object->parameters['order_actions']['valid_invoice_fk_warehouse'] = !empty($object->parameters['order_actions']['create_invoice']) ? GETPOST('valid_invoice_fk_warehouse', 'int') : -1; + $object->parameters['order_actions']['valid_supplier_invoice_fk_warehouse'] = !empty($object->parameters['order_actions']['create_supplier_invoice']) ? GETPOST('valid_supplier_invoice_fk_warehouse', 'int') : -1; + $object->parameters['enable_warehouse_depending_on_shipping_zone_method'] = GETPOST('enable_warehouse_depending_on_shipping_zone_method', 'int') ? 1 : 0; + + $result = $object->update($user); + + if ($result < 0) { + setEventMessages($object->error, $object->errors, 'errors'); + } else { + setEventMessage($langs->trans("SetupSaved")); + header("Location: " . $_SERVER["PHP_SELF"] . '?id=' . $object->id); + exit; + } + } elseif ($action == 'set_remote_shipping_zone_method_options' && !empty($object->parameters['enable_warehouse_depending_on_shipping_zone_method'])) { + foreach ($remote_shipping_zone_methods as $key1 => $remote_shipping_zone_infos) { + foreach ($remote_shipping_zone_infos['methods'] as $key2 => $infos) { + $warehouse_id = (int)GETPOST('warehouse_id_' . $key1 . '_' . $key2, 'int'); + $warehouse_id = max(0, $warehouse_id); + + $remote_shipping_zone_methods[$key1]['methods'][$key2]['warehouse_id'] = $warehouse_id; + } + } + + $result = $eCommerceRemoteShippingZoneMethods->set($object->id, $remote_shipping_zone_methods); + + if ($result < 0) { + setEventMessages($eCommerceRemoteShippingZoneMethods->error, $eCommerceRemoteShippingZoneMethods->errors, 'errors'); + } else { + setEventMessage($langs->trans("SetupSaved")); + header("Location: " . $_SERVER["PHP_SELF"] . '?id=' . $object->id); + exit; + } + } elseif ($action == 'confirm_update_remote_shipping_zone_methods' && $confirm == "yes" && !empty($object->parameters['enable_warehouse_depending_on_shipping_zone_method'])) { + $result = ecommerceng_update_remote_shipping_zone_methods($db, $object); + if ($result) setEventMessage($langs->trans('ECommerceRemoteShippingZoneMethodsUpdated'), 'mesgs'); + + header("Location: " . $_SERVER["PHP_SELF"] . '?id=' . $object->id); + exit; + } } @@ -141,7 +194,9 @@ $formconfirm = ''; if ($action == 'update_remote_warehouses' && !empty($object->parameters['enable_warehouse_plugin_support'])) { - $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id . '&plugin=' . $plugin, $langs->trans('ECommerceUpdateRemoteWarehouses'), $langs->trans('ECommerceConfirmUpdateRemoteWarehouses'), 'confirm_update_remote_warehouses', '', 0, 1, 200, 800); + $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('ECommerceUpdateRemoteWarehouses'), $langs->trans('ECommerceConfirmUpdateRemoteWarehouses'), 'confirm_update_remote_warehouses', '', 0, 1, 200, 800); +} elseif ($action == 'update_remote_shipping_zone_methods' && !empty($object->parameters['enable_warehouse_depending_on_shipping_zone_method'])) { + $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('ECommerceUpdateRemoteShippingMethods'), $langs->trans('ECommerceConfirmUpdateRemoteShippingMethods'), 'confirm_update_remote_shipping_zone_methods', '', 0, 1, 200, 800); } // Call Hook formConfirm @@ -170,6 +225,11 @@ print '' . $langs->trans('ECommerceUpdateRemoteWarehouses') . ''; print ''; } +if (!empty($object->parameters['enable_warehouse_depending_on_shipping_zone_method'])) { + print '
'; + print '' . $langs->trans('ECommerceUpdateRemoteShippingZoneMethods') . ''; + print '
'; +} /** * Settings. @@ -189,41 +249,6 @@ print ''.$langs->trans("Value").''."\n"; print "\n"; -$p_warehouseplugin = !empty($object->parameters['enable_warehouse_plugin_support']) ? $object->parameters['enable_warehouse_plugin_support'] : ''; -if ($p_warehouseplugin != 'wmlim') { - if (!empty($object->parameters['order_actions']['create_order'])) { - // Warehouse used when valid a order - print '' . "\n"; - print '' . $langs->trans("ECommerceValidOrderWarehouse") . '' . "\n"; - print '' . $langs->transnoentities("ECommerceValidOrderWarehouseDescription") . '' . "\n"; - print '' . "\n"; - $p_orderactionsorder = !empty($object->parameters['order_actions']['valid_order_fk_warehouse']) ? $object->parameters['order_actions']['valid_order_fk_warehouse'] : ''; - print $formproduct->selectWarehouses($p_orderactionsorder, 'valid_order_fk_warehouse', 0, 1); - print '' . "\n"; - } - - if (!empty($object->parameters['order_actions']['create_invoice'])) { - // Warehouse used when valid a invoice - print '' . "\n"; - print '' . $langs->trans("ECommerceValidInvoiceWarehouse") . '' . "\n"; - print '' . $langs->transnoentities("ECommerceValidInvoiceWarehouseDescription") . '' . "\n"; - print '' . "\n"; - $p_orderactionsinvoice = !empty($object->parameters['order_actions']['valid_invoice_fk_warehouse']) ? $object->parameters['order_actions']['valid_invoice_fk_warehouse'] : ''; - print $formproduct->selectWarehouses($p_orderactionsinvoice, 'valid_invoice_fk_warehouse', 0, 1); - print '' . "\n"; - } - - //if (!empty($object->parameters['order_actions']['create_supplier_invoice'])) { - // // Warehouse used when valid a supplier invoice - // print '' . "\n"; - // print '' . $langs->trans("ECommerceValidSupplierInvoiceWarehouse") . '' . "\n"; - // print '' . $langs->transnoentities("ECommerceValidSupplierInvoiceWarehouseDescription") . '' . "\n"; - // print '' . "\n"; - // print $formproduct->selectWarehouses($object->parameters['order_actions']['valid_supplier_invoice_fk_warehouse'], 'valid_supplier_invoice_fk_warehouse', 0, 1); - // print '' . "\n"; - //} -} - // Synchronize sens print '' . "\n"; print ''.$langs->trans("ECommerceStockSyncDirection").''."\n"; @@ -237,7 +262,7 @@ print $form->selectarray('stock_sync_direction', $synchronize_sens, $object->stock_sync_direction, 0, 0, 0, '', 0, 0, 0, '', 'minwidth200 centpercent') . "\n"; print '' . "\n"; -if (in_array($object->stock_sync_direction, [ 'dolibarr2ecommerce', 'ecommerce2dolibarr' ])) { +if (!in_array($object->stock_sync_direction, [ 'dolibarr2ecommerce', 'ecommerce2dolibarr' ]) || $movement_stock_on_validate_order || $movement_stock_on_validate_invoice) { // Support of WooCommerce warehouse plugins : Stock Locations for WooCommerce, WooCommerce Multi Locations Inventory Management print '' . "\n"; print '' . $langs->trans("ECommerceWoocommerceEnableWarehousePluginSupport") . '' . "\n"; @@ -249,37 +274,37 @@ ); print $form->selectarray('enable_warehouse_plugin_support', $warehouse_plugins, $object->parameters['enable_warehouse_plugin_support'], 1, 0, 0, '', 0, 0, 0, 0, 'minwidth200 centpercent'); print '' . "\n"; +} - if (empty($object->parameters['enable_warehouse_plugin_support'])) { - if ($object->stock_sync_direction == 'dolibarr2ecommerce') { - // Update virtual stock - print '' . "\n"; - print '' . $langs->trans("ECommerceUpdateVirtualStock") . '' . "\n"; - print '' . $langs->transnoentities("ECommerceUpdateVirtualStockDescription") . '' . "\n"; - print '' . "\n"; - print 'parameters['update_virtual_stock']) ? ' checked' : '') . ' />' . "\n"; - print '' . "\n"; - } - - // Warehouses +if (in_array($object->stock_sync_direction, [ 'dolibarr2ecommerce', 'ecommerce2dolibarr' ]) && empty($object->parameters['enable_warehouse_plugin_support'])) { + if ($object->stock_sync_direction == 'dolibarr2ecommerce') { + // Update virtual stock print '' . "\n"; - print '' . $langs->trans("ECommerceStockProduct") . '' . "\n"; - print '' . $langs->transnoentities("ECommerceStockProductDescription") . '' . "\n"; + print '' . $langs->trans("ECommerceUpdateVirtualStock") . '' . "\n"; + print '' . $langs->transnoentities("ECommerceUpdateVirtualStockDescription") . '' . "\n"; print '' . "\n"; - if ($object->stock_sync_direction == 'dolibarr2ecommerce') { - $value = isset($object->parameters['fk_warehouse_to_ecommerce']) ? $object->parameters['fk_warehouse_to_ecommerce'] : array(); - print $formecommerceng->multiselectWarehouses($value, 'fk_warehouse_to_ecommerce'); - } else { - print $formproduct->selectWarehouses($object->fk_warehouse, 'fk_warehouse', 0, 1); - } + print 'parameters['update_virtual_stock']) ? ' checked' : '') . ' />' . "\n"; print '' . "\n"; } + + // Warehouses + print '' . "\n"; + print '' . $langs->trans("ECommerceStockProduct") . '' . "\n"; + print '' . $langs->transnoentities("ECommerceStockProductDescription") . '' . "\n"; + print '' . "\n"; + if ($object->stock_sync_direction == 'dolibarr2ecommerce') { + $value = isset($object->parameters['fk_warehouse_to_ecommerce']) ? $object->parameters['fk_warehouse_to_ecommerce'] : array(); + print $formecommerceng->multiselectWarehouses($value, 'fk_warehouse_to_ecommerce'); + } else { + print $formproduct->selectWarehouses($object->fk_warehouse > 0 ? $object->fk_warehouse : -1, 'fk_warehouse', 0, 1); + } + print '' . "\n"; } print ''."\n"; print '
'; -print '
'; +print '
'; print ''; print '
'; @@ -311,7 +336,7 @@ print '' . $infos['remote_id'] . '' . "\n"; print '' . $infos['remote_code'] . '' . "\n"; print '' . $infos['remote_name'] . '' . "\n"; - print '' . $formproduct->selectWarehouses($infos['warehouse_id'], 'warehouse_id_' . $remote_warehouse_id, 0, 1) . '' . "\n"; + print '' . $formproduct->selectWarehouses($infos['warehouse_id'] > 0 ? $infos['warehouse_id'] : -1, 'warehouse_id_' . $remote_warehouse_id, 0, 1) . '' . "\n"; print '' . "\n"; print '' . yn($infos['old_entry']) . '' . "\n"; print '' . "\n"; @@ -320,13 +345,135 @@ print '' . "\n"; print '
'; - print '
'; + print '
'; print ''; print '
'; print ''; } +if ((!empty($object->parameters['order_actions']['create_order']) && $movement_stock_on_validate_order) || + (!empty($object->parameters['order_actions']['create_invoice']) && $movement_stock_on_validate_invoice) +) { + /** + * Settings movement stock at validation of order or invoice. + */ + + print '
'; + print load_fiche_titre($langs->trans("ECommerceMovementStockOnValidateOrderOrInvoice", $movement_stock_on_validate_order ? $langs->transnoentitiesnoconv('Order') : $langs->transnoentitiesnoconv('Invoice')), '', ''); + print info_admin($langs->trans("ECommerceSearchWarehouseOrder") . + (!empty($object->parameters['enable_warehouse_plugin_support']) ? $langs->trans("ECommerceSearchWarehouseByMultiWarehouseCorrespondence") : '') . + $langs->trans("ECommerceSearchWarehouseByShippingMethodCorrespondence") . + $langs->trans("ECommerceSearchWarehouseByDefaultWarehouse") + ); + + print '
'; + print ''; + print ''; + + print ''; + print ''; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print "\n"; + + if (!empty($object->parameters['order_actions']['create_order']) && $movement_stock_on_validate_order) { + // Warehouse used when valid a order + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + } + + if (!empty($object->parameters['order_actions']['create_invoice']) && $movement_stock_on_validate_invoice) { + // Warehouse used when valid a invoice + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + } + + // Support warehouse depending on shipping mode + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + + print '
' . $langs->trans("Parameters") . '' . $langs->trans("Description") . '' . $langs->trans("Value") . '
' . $langs->trans("ECommerceValidOrderWarehouse") . '' . $langs->transnoentities("ECommerceValidOrderWarehouseDescription") . '' . "\n"; + $p_orderactionsorder = !empty($object->parameters['order_actions']['valid_order_fk_warehouse']) ? $object->parameters['order_actions']['valid_order_fk_warehouse'] : -1; + print $formproduct->selectWarehouses($p_orderactionsorder, 'valid_order_fk_warehouse', 0, 1); + print '
' . $langs->trans("ECommerceValidInvoiceWarehouse") . '' . $langs->transnoentities("ECommerceValidInvoiceWarehouseDescription") . '' . "\n"; + $p_orderactionsinvoice = !empty($object->parameters['order_actions']['valid_invoice_fk_warehouse']) ? $object->parameters['order_actions']['valid_invoice_fk_warehouse'] : -1; + print $formproduct->selectWarehouses($p_orderactionsinvoice, 'valid_invoice_fk_warehouse', 0, 1); + print '
' . $langs->trans("ECommerceEnableWarehouseDependingOnShippingZoneMethodSupport") . '' . $langs->transnoentities("ECommerceEnableWarehouseDependingOnShippingZoneMethodSupportDescription") . '' . "\n"; + print 'parameters['enable_warehouse_depending_on_shipping_zone_method']) ? ' checked' : '') . ' />' . "\n"; + print '
' . "\n"; + + print '
'; + print '
'; + print ''; + print '
'; + + print '
'; + + if (!empty($object->parameters['enable_warehouse_depending_on_shipping_zone_method'])) { + /** + * Remote shipping mode + */ + print '
'; + print load_fiche_titre($langs->trans('ECommerceRemoteShippingZoneMethodsCorrespondence'), '', ''); + + print '
'; + print ''; + print ''; + + print ''; + print ''; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print "\n"; + + $shipping_method_enabled_badge = dolGetBadge($langs->trans('Enabled'), '', 'status4', 'dot'); + $shipping_method_disabled_badge = dolGetBadge($langs->trans('Disabled'), '', 'status9', 'dot'); + + foreach ($remote_shipping_zone_methods as $key1 => $remote_shipping_zone_infos) { + if (!empty($remote_shipping_zone_infos['methods'])) { + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + + foreach ($remote_shipping_zone_infos['methods'] as $key2 => $infos) { + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + print '' . "\n"; + } + } + } + + print '
' . $langs->trans("ECommerceRemoteID") . '' . $langs->trans("Label") . '' . $langs->trans("Status") . '' . $langs->trans("Warehouse") . '' . $langs->trans("ECommerceShippingMethodOldEntry") . '
' . $remote_shipping_zone_infos['remote_id'] . '' . $remote_shipping_zone_infos['remote_name'] . '' . yn($remote_shipping_zone_infos['old_entry']) . '
' . $infos['remote_instance_id'] . ' ( ' . $infos['remote_method_id'] . ')' . $form->textwithpicto($infos['remote_title'], + 'Remote method title: ' . $infos['remote_method_title'] . '
' . + 'Remote method description: ' . $infos['remote_method_description'] + ) . '
' . (empty($infos['remote_enabled']) ? $shipping_method_disabled_badge : $shipping_method_enabled_badge) . '' . $formproduct->selectWarehouses($infos['warehouse_id'] > 0 ? $infos['warehouse_id'] : -1, 'warehouse_id_' . $key1 . '_' . $key2, 0, 1) . '' . yn($infos['old_entry']) . '
' . "\n"; + + print '
'; + print '
'; + print ''; + print '
'; + + print '
'; + } +} + print dol_get_fiche_end(); llxFooter(); diff --git a/class/business/eCommerceSynchro.class.php b/class/business/eCommerceSynchro.class.php index 3b2c32e..64c3270 100755 --- a/class/business/eCommerceSynchro.class.php +++ b/class/business/eCommerceSynchro.class.php @@ -2548,6 +2548,37 @@ public function getAllRemoteWarehouses() return $result; } + /** + * Get all remote shipping zones + * + * @return array|false List of shipping zones or false if error + */ + public function getAllRemoteShippingZones() + { + $result = $this->eCommerceRemoteAccess->getAllRemoteShippingZones(); + if (!$result) { + $this->error = $this->eCommerceRemoteAccess->error; + $this->errors = $this->eCommerceRemoteAccess->errors; + } + return $result; + } + + /** + * Get all remote shipping zone methods + * + * @param integer $remote_zone_id Remote zone ID + * @return array|false List of shipping zone methods or false if error + */ + public function getAllRemoteShippingZoneMethods($remote_zone_id) + { + $result = $this->eCommerceRemoteAccess->getAllRemoteShippingZoneMethods($remote_zone_id); + if (!$result) { + $this->error = $this->eCommerceRemoteAccess->error; + $this->errors = $this->eCommerceRemoteAccess->errors; + } + return $result; + } + /** * Get all webhooks * @@ -4436,10 +4467,28 @@ public function synchronizeOrder($order_data, $dont_synchronize_products = false // Update the order status if (!$error && ($new_order || ($order->statut != $order_data['status']))) { // Always when creating - $warehouse_id = $this->eCommerceSite->parameters['order_actions']['valid_order_fk_warehouse'] > 0 && empty($warehouseByLine) ? $this->eCommerceSite->parameters['order_actions']['valid_order_fk_warehouse'] : 0; - if (empty($warehouse_id) && empty($warehouseByLine) && !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1) { - $this->errors[] = $this->langs->trans('ECommerceErrorValidOrderWarehouseNotConfigured', $this->eCommerceSite->name); - $error++; + // Get default warehouse ID + $default_warehouse_id = 0; + if (!empty($this->eCommerceSite->parameters['enable_warehouse_depending_on_shipping_zone_method'])) { + $default_warehouse_id = $this->getWarehouseFromShippingMethod($order_data['shipping_lines']); + if ($default_warehouse_id < 0) { + $error++; + } + } + if (!$error && empty($default_warehouse_id)) { + $default_warehouse_id = max(0, (int) $this->eCommerceSite->parameters['order_actions']['valid_order_fk_warehouse']); + } + + // Set validate warehouse ID if no warehouse plugin support activated and movement stock on validate order set + $warehouse_id = 0; + if (!$error && empty($this->eCommerceSite->parameters['enable_warehouse_plugin_support']) && + !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1 + ) { + $warehouse_id = $default_warehouse_id; + if (empty($warehouse_id)) { + $this->errors[] = $this->langs->trans('ECommerceErrorValidOrderWarehouseNotConfigured'); + $error++; + } } // Valid the order if the distant order is not at the draft status but the order is draft. For set the order ref. @@ -4451,7 +4500,7 @@ public function synchronizeOrder($order_data, $dont_synchronize_products = false $this->errors = array_merge($this->errors, $order->errors); $error++; } else { - $result = $this->setLinesMovementStockOnDifferentWarehouse($order, $warehouseByLine); + $result = $this->setLinesMovementStockOnDifferentWarehouse($order, $warehouseByLine, $default_warehouse_id); if ($result < 0) { $this->errors[] = $this->langs->trans("ECommerceErrorWhenMovementStockOnDifferentWarehouse"); $error++; @@ -4485,7 +4534,7 @@ public function synchronizeOrder($order_data, $dont_synchronize_products = false $this->errors = array_merge($this->errors, $order->errors); $error++; } else { - $result = $this->setLinesMovementStockOnDifferentWarehouse($order, $warehouseByLine, true); + $result = $this->setLinesMovementStockOnDifferentWarehouse($order, $warehouseByLine, $default_warehouse_id, true); if ($result < 0) { $this->errors[] = $this->langs->trans("ECommerceErrorWhenMovementStockOnDifferentWarehouse"); $error++; @@ -4631,16 +4680,17 @@ public function synchronizeOrder($order_data, $dont_synchronize_products = false /** * Set movement stock for each product line on different warehouse * - * @param CommonObject $object Object handler - * @param array $warehouseByLine List of warehouse ID for each line - * @param boolean $undoMovement True to undo movements - * @return int <0 if KO, >0 if OK + * @param CommonObject $object Object handler + * @param array $warehouseByLine List of warehouse ID for each line + * @param int $default_warehouse_id Default warehouse ID + * @param boolean $undoMovement True to undo movements + * @return int <0 if KO, >0 if OK */ - public function setLinesMovementStockOnDifferentWarehouse($object, $warehouseByLine, $undoMovement = false) + public function setLinesMovementStockOnDifferentWarehouse($object, $warehouseByLine, $default_warehouse_id, $undoMovement = false) { global $conf; - $canMakeMovement = !empty($warehouseByLine) && !empty($conf->stock->enabled); + $canMakeMovement = !empty($this->eCommerceSite->parameters['enable_warehouse_plugin_support']) && !empty($conf->stock->enabled); if ($object->element == 'commande') { $canMakeMovement &= !empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1; $movementLabel = $this->langs->trans($undoMovement ? "OrderCanceledInDolibarr" : "OrderValidatedInDolibarr", $object->ref); @@ -4671,16 +4721,16 @@ public function setLinesMovementStockOnDifferentWarehouse($object, $warehouseByL $error = 0; $this->db->begin(); foreach ($object->lines as $line) { - if ($line->fk_product > 0) { + if ($line->fk_product > 0 && ($line->product_type != Product::TYPE_SERVICE || getDolGlobalString('STOCK_SUPPORTS_SERVICES'))) { $remoteWarehouseId = isset($warehouseByLine[$line->id]) && $warehouseByLine[$line->id] > 0 ? $warehouseByLine[$line->id] : 0; - if (empty($remoteWarehouseId) && $line->product_type == Product::TYPE_PRODUCT) { + if (empty($remoteWarehouseId) && empty($default_warehouse_id)) { $this->errors[] = $this->langs->trans("ECommerceErrorWarehouseIdNotSpecifiedForTheLine", $line->fk_product); $error++; break; } - $warehouseId = isset($remoteWarehousesList[$remoteWarehouseId]['warehouse_id']) && $remoteWarehousesList[$remoteWarehouseId]['warehouse_id'] > 0 ? $remoteWarehousesList[$remoteWarehouseId]['warehouse_id'] : 0; - if ($remoteWarehouseId > 0 && empty($warehouseId)) { + $warehouseId = !empty($remoteWarehouseId) && isset($remoteWarehousesList[$remoteWarehouseId]['warehouse_id']) && $remoteWarehousesList[$remoteWarehouseId]['warehouse_id'] > 0 ? $remoteWarehousesList[$remoteWarehouseId]['warehouse_id'] : $default_warehouse_id; + if (!empty($remoteWarehouseId) && empty($warehouseId)) { dol_syslog(__METHOD__ . ' Warehouse not configured for remote warehouse ID ' . $remoteWarehouseId . ' so we don\'t process this remote warehouse', LOG_WARNING); continue; // $this->errors[] = $this->langs->trans("ECommerceErrorWarehouseIdNotConfiguredForRemoteWarehouse", $remoteWarehouseId, $this->eCommerceSite->name); @@ -4716,6 +4766,38 @@ public function setLinesMovementStockOnDifferentWarehouse($object, $warehouseByL return 1; } + /** + * Get warehouse from shipping method + * + * @param array $shipping_lines List of shipping lines ID + * @return int <0 if KO, =0 if not found, >0 if warehouse ID found + */ + public function getWarehouseFromShippingMethod($shipping_lines) + { + // Get all shipping methods + dol_include_once('/ecommerceng/class/data/eCommerceRemoteShippingZoneMethods.class.php'); + $remote_shipping_methods = new eCommerceRemoteShippingZoneMethods($this->db); + $remoteShippingMethodsList = $remote_shipping_methods->get_all($this->eCommerceSite->id); + if (!is_array($remoteShippingMethodsList) && $remoteShippingMethodsList < 0) { + $this->errors[] = $remote_shipping_methods->errorsToString(); + return -1; + } + + foreach ($remoteShippingMethodsList as $key1 => $remote_shipping_zone_infos) { + foreach ($remote_shipping_zone_infos['methods'] as $key2 => $infos) { + foreach ($shipping_lines as $shipping_line) { + if ($infos['remote_instance_id'] == $shipping_line['instance_id'] && + $infos['remote_method_id'] == $shipping_line['method_id'] + ) { + return (int) $infos['warehouse_id']; + } + } + } + } + + return 0; + } + /** * Synchronize a order data in the invoice in Dolibarr database * @@ -5027,11 +5109,28 @@ public function synchronizeInvoiceFromOrder($order_data, $dont_synchronize_produ } } - // Get warehouse ID - $warehouse_id = $this->eCommerceSite->parameters['order_actions']['valid_invoice_fk_warehouse'] > 0 && empty($warehouseByLine) ? $this->eCommerceSite->parameters['order_actions']['valid_invoice_fk_warehouse'] : 0; - if (empty($warehouse_id) && empty($warehouseByLine) && !empty($conf->global->STOCK_CALCULATE_ON_BILL)) { - $this->errors[] = $this->langs->trans('ECommerceErrorInvoiceValidateWarehouseNotConfigured'); - $error++; + // Get default warehouse ID + $default_warehouse_id = 0; + if (!empty($this->eCommerceSite->parameters['enable_warehouse_depending_on_shipping_zone_method'])) { + $default_warehouse_id = $this->getWarehouseFromShippingMethod($order_data['shipping_lines']); + if ($default_warehouse_id < 0) { + $error++; + } + } + if (!$error && empty($default_warehouse_id)) { + $default_warehouse_id = max(0, (int) $this->eCommerceSite->parameters['order_actions']['valid_invoice_fk_warehouse']); + } + + // Set validate warehouse ID if no warehouse plugin support activated and movement stock on validate order set + $warehouse_id = 0; + if (!$error && empty($this->eCommerceSite->parameters['enable_warehouse_plugin_support']) && + !empty($conf->global->STOCK_CALCULATE_ON_BILL) + ) { + $warehouse_id = $default_warehouse_id; + if (empty($warehouse_id)) { + $this->errors[] = $this->langs->trans('ECommerceErrorInvoiceValidateWarehouseNotConfigured'); + $error++; + } } if ($isDepositType) { @@ -5052,7 +5151,7 @@ public function synchronizeInvoiceFromOrder($order_data, $dont_synchronize_produ $this->errors = array_merge($this->errors, $invoice->errors); $error++; } else { - $result = $this->setLinesMovementStockOnDifferentWarehouse($invoice, $warehouseByLine); + $result = $this->setLinesMovementStockOnDifferentWarehouse($invoice, $warehouseByLine, $default_warehouse_id); if ($result < 0) { $this->errors[] = $this->langs->trans("ECommerceErrorWhenMovementStockOnDifferentWarehouse"); $error++; @@ -5208,7 +5307,7 @@ public function synchronizeInvoiceFromOrder($order_data, $dont_synchronize_produ // Validate supplier invoice if (!$error) { // Get warehouse ID - $warehouse_id = $this->eCommerceSite->parameters['order_actions']['valid_supplier_invoice_fk_warehouse'] > 0 ? $this->eCommerceSite->parameters['order_actions']['valid_supplier_invoice_fk_warehouse'] : 0; + $warehouse_id = /*$this->eCommerceSite->parameters['order_actions']['valid_supplier_invoice_fk_warehouse'] > 0 ? $this->eCommerceSite->parameters['order_actions']['valid_supplier_invoice_fk_warehouse'] :*/ 0; $result = $supplier_invoice->validate($this->user, '', $warehouse_id); if ($result < 0) { @@ -5840,11 +5939,28 @@ public function synchronizeInvoiceRefundsFromOrder($order_data, $dont_synchroniz } } - // Get warehouse ID - $warehouse_id = $this->eCommerceSite->parameters['order_actions']['valid_invoice_fk_warehouse'] > 0 ? $this->eCommerceSite->parameters['order_actions']['valid_invoice_fk_warehouse'] : 0; - if (empty($warehouse_id) && empty($warehouseByLine) && !empty($conf->global->STOCK_CALCULATE_ON_BILL)) { - $this->errors[] = $this->langs->trans('ECommerceErrorInvoiceValidateWarehouseNotConfigured'); - $error++; + // Get default warehouse ID + $default_warehouse_id = 0; + if (!empty($this->eCommerceSite->parameters['enable_warehouse_depending_on_shipping_zone_method'])) { + $default_warehouse_id = $this->getWarehouseFromShippingMethod($order_data['shipping_lines']); + if ($default_warehouse_id < 0) { + $error++; + } + } + if (!$error && empty($default_warehouse_id)) { + $default_warehouse_id = max(0, (int) $this->eCommerceSite->parameters['order_actions']['valid_invoice_fk_warehouse']); + } + + // Set validate warehouse ID if no warehouse plugin support activated and movement stock on validate order set + $warehouse_id = 0; + if (!$error && empty($this->eCommerceSite->parameters['enable_warehouse_plugin_support']) && + !empty($conf->global->STOCK_CALCULATE_ON_BILL) + ) { + $warehouse_id = $default_warehouse_id; + if (empty($warehouse_id)) { + $this->errors[] = $this->langs->trans('ECommerceErrorInvoiceValidateWarehouseNotConfigured'); + $error++; + } } // Validate invoice @@ -5856,7 +5972,7 @@ public function synchronizeInvoiceRefundsFromOrder($order_data, $dont_synchroniz $this->errors = array_merge($this->errors, $invoice_refund->errors); $error++; } else { - $result = $this->setLinesMovementStockOnDifferentWarehouse($invoice_refund, $warehouseByLine); + $result = $this->setLinesMovementStockOnDifferentWarehouse($invoice_refund, $warehouseByLine, $default_warehouse_id); if ($result < 0) { $this->errors[] = $this->langs->trans("ECommerceErrorWhenMovementStockOnDifferentWarehouse"); $error++; diff --git a/class/data/eCommerceRemoteAccess.class.php b/class/data/eCommerceRemoteAccess.class.php index e4d2d4c..7a411bb 100644 --- a/class/data/eCommerceRemoteAccess.class.php +++ b/class/data/eCommerceRemoteAccess.class.php @@ -544,6 +544,33 @@ public function getAllRemoteWarehouses() return $result; } + /** + * Get all remote shipping zones + * + * @return array|false List of remote shipping zones or false if error + */ + public function getAllRemoteShippingZones() + { + $result=$this->class->getAllRemoteShippingZones(); + $this->error=$this->class->error; + $this->errors=$this->class->errors; + return $result; + } + + /** + * Get all remote shipping zone methods + * + * @param integer $remote_zone_id Remote zone ID + * @return array|false List of remote shipping zone methods or false if error + */ + public function getAllRemoteShippingZoneMethods($remote_zone_id) + { + $result=$this->class->getAllRemoteShippingZoneMethods($remote_zone_id); + $this->error=$this->class->error; + $this->errors=$this->class->errors; + return $result; + } + /** * Get all webhooks * diff --git a/class/data/eCommerceRemoteShippingZoneMethods.class.php b/class/data/eCommerceRemoteShippingZoneMethods.class.php new file mode 100644 index 0000000..001b9f6 --- /dev/null +++ b/class/data/eCommerceRemoteShippingZoneMethods.class.php @@ -0,0 +1,315 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file htdocs/ecommerceng/class/data/eCommerceRemoteShippingZoneMethods.class.php + * \ingroup ecommerceng + * \brief + */ +dol_include_once('/ecommerceng/class/data/eCommerceRemoteShippingZones.class.php'); + +/** + * Class eCommerceRemoteShippingZoneMethods + * + * Put here description of your class + */ +class eCommerceRemoteShippingZoneMethods +{ + /** + * @var DoliDB Database handler. + */ + public $db; + /** + * @var string Error + */ + public $error = ''; + /** + * @var array Errors + */ + public $errors = array(); + + public $table_element = 'ecommerceng_remote_shipping_zone_methods'; + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + public function __construct($db) + { + $this->db = $db; + } + + /** + * Set all remote shipping zone methods of a site + * + * @param int $site_id Site ID + * @param array $remote_shipping_zone_methods List of infos of each remote shipping zone methods + * @return int >0 if OK, <0 if KO + * @throws Exception + */ + public function set($site_id, $remote_shipping_zone_methods) + { + global $conf, $langs; + dol_syslog(__METHOD__ . " site_id=$site_id, remote_shipping_zone_methods=" . json_encode($remote_shipping_zone_methods)); + + $errors = 0; + $this->errors = array(); + + // Clean values + $site_id = max(0, (int) $site_id); + $remote_shipping_zone_methods = is_array($remote_shipping_zone_methods) ? $remote_shipping_zone_methods : array(); + + // Check values + if ($site_id == 0) { + $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ECommerceSite")); + return -1; + } + + $this->db->begin(); + + // Set shipping zones + $eCommerceRemoteShippingZones = new eCommerceRemoteShippingZones($this->db); + $result = $eCommerceRemoteShippingZones->set($site_id, $remote_shipping_zone_methods); + if ($result < 0) { + $this->error = $eCommerceRemoteShippingZones->error; + $this->errors = $eCommerceRemoteShippingZones->errors; + return -1; + } + + foreach ($remote_shipping_zone_methods as $remote_shipping_zone_infos) { + if ($remote_shipping_zone_infos['methods']) { + foreach ($remote_shipping_zone_infos['methods'] as $infos) { + // Search shipping zone method + $sql = 'SELECT rowid'; + $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element; + $sql .= ' WHERE site_id = ' . $site_id; + $sql .= ' AND entity = ' . $conf->entity; + $sql .= " AND remote_zone_id = " . ((int) $remote_shipping_zone_infos['remote_id']); + $sql .= " AND remote_instance_id = " . ((int) $infos['remote_instance_id']); + $sql .= " AND remote_method_id = '" . $this->db->escape($infos['remote_method_id']) . "'"; + + $resql = $this->db->query($sql); + if (!$resql) { + dol_syslog(__METHOD__ . ' SQL: ' . $sql . '; Errors: ' . $this->db->lasterror(), LOG_ERR); + $this->errors[] = $this->db->lasterror(); + $errors++; + break; + } + + $line_id = 0; + if ($obj = $this->db->fetch_object($resql)) { + $line_id = $obj->rowid; + } + + $this->db->free($resql); + + if ($line_id > 0) { + // Update values + $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET'; + $sql .= " remote_title = '" . $this->db->escape($infos['remote_title']) . "'"; + $sql .= ", remote_order = " . ((int) $infos['remote_order']); + $sql .= ", remote_enabled = " . (!empty($infos['remote_enabled']) ? 1 : 0); + $sql .= ", remote_method_title = '" . $this->db->escape($infos['remote_method_title']) . "'"; + $sql .= ", remote_method_description = '" . $this->db->escape($infos['remote_method_description']) . "'"; + $sql .= ", warehouse_id = " . ($infos['warehouse_id'] > 0 ? $infos['warehouse_id'] : 'NULL'); + $sql .= ', old_entry = ' . (!empty($infos['old_entry']) ? 1 : 'NULL'); + $sql .= ' WHERE rowid = ' . $line_id; + + $resql = $this->db->query($sql); + } else { + // Insert values + $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(site_id, remote_zone_id, remote_instance_id'; + $sql .= ', remote_title, remote_order, remote_enabled, remote_method_id, remote_method_title, remote_method_description'; + $sql .= ', warehouse_id, old_entry, entity) VALUES ('; + $sql .= $site_id; + $sql .= ", " . ((int) $remote_shipping_zone_infos['remote_id']); + $sql .= ", " . ((int) $infos['remote_instance_id']); + $sql .= ", '" . $this->db->escape($infos['remote_title']) . "'"; + $sql .= ", " . ((int) $infos['remote_order']); + $sql .= ", " . (!empty($infos['remote_enabled']) ? 1 : 0); + $sql .= ", '" . $this->db->escape($infos['remote_method_id']) . "'"; + $sql .= ", '" . $this->db->escape($infos['remote_method_title']) . "'"; + $sql .= ", '" . $this->db->escape($infos['remote_method_description']) . "'"; + $sql .= ', ' . ($infos['warehouse_id'] > 0 ? $infos['warehouse_id'] : 'NULL'); + $sql .= ', ' . (!empty($infos['old_entry']) ? 1 : 'NULL'); + $sql .= ', ' . $conf->entity; + $sql .= ')'; + $resql = $this->db->query($sql); + } + if (!$resql) { + dol_syslog(__METHOD__ . ' SQL: ' . $sql . '; Errors: ' . $this->db->lasterror(), LOG_ERR); + $this->errors[] = $this->db->lasterror(); + $errors++; + break; + } + } + if ($errors) { + break; + } + } + } + + if ($errors) { + $this->db->rollback(); + return -1; + } else { + $this->db->commit(); + return 1; + } + } + + /** + * Get a remote shipping zone method of a site by the remote shipping zone method instance id and remote shipping zone method id + * + * @param int $site_id Site ID + * @param integer $remote_instance_id Remote shipping zone method instance ID on site + * @param string $remote_method_id Remote shipping zone method ID on site + * @return array|int 0 if not found, <0 if errors or array of infos + * @throws Exception + */ + public function get($site_id, $remote_instance_id=0, $remote_method_id='') + { + global $conf; + dol_syslog(__METHOD__ . " site_id=$site_id, remote_instance_id=$remote_instance_id, remote_method_id=$remote_method_id"); + + $sql = 'SELECT remote_zone_id, remote_instance_id'; + $sql .= ', remote_title, remote_order, remote_enabled, remote_method_id, remote_method_title, remote_method_description'; + $sql .= ', warehouse_id, old_entry FROM ' . MAIN_DB_PREFIX . $this->table_element; + $sql .= ' WHERE site_id = ' . $site_id . ' AND entity = ' . $conf->entity; + $sql .= " AND remote_instance_id = " . ((int) $remote_instance_id); + $sql .= " AND remote_method_id = '" . $this->db->escape($remote_method_id) . "'"; + $resql = $this->db->query($sql); + if ($resql) { + if ($obj = $this->db->fetch_object($resql)) { + return array( + 'remote_zone_id' => $obj->remote_zone_id, + 'remote_instance_id' => $obj->remote_instance_id, + 'remote_title' => $obj->remote_title, + 'remote_order' => $obj->remote_order, + 'remote_enabled' => !empty($obj->remote_enabled), + 'remote_method_id' => $obj->remote_method_id, + 'remote_method_title' => $obj->remote_method_title, + 'remote_method_description' => $obj->remote_method_description, + 'warehouse_id' => $obj->warehouse_id > 0 ? $obj->warehouse_id : 0, + 'old_entry' => !empty($obj->old_entry) ? 1 : 0, + ); + } + + return 0; + } else { + dol_syslog(__METHOD__ . ' SQL: ' . $sql . '; Errors: ' . $this->db->lasterror(), LOG_ERR); + $this->errors[] = $this->db->lasterror(); + return -1; + } + } + + /** + * Get all remote shipping zone methods of a site + * + * @param int $site_id Site ID + * @return array|int List of all remote shipping zone methods infos + * @throws Exception + */ + public function get_all($site_id) + { + global $conf; + dol_syslog(__METHOD__ . " site_id=$site_id"); + + // Get all shipping zones + $eCommerceRemoteShippingZones = new eCommerceRemoteShippingZones($this->db); + $remote_shipping_zones = $eCommerceRemoteShippingZones->get_all($site_id); + if (!is_array($remote_shipping_zones) && $remote_shipping_zones < 0) { + $this->error = $eCommerceRemoteShippingZones->error; + $this->errors = $eCommerceRemoteShippingZones->errors; + return -1; + } + + foreach ($remote_shipping_zones as $key1 => $remote_shipping_zone_info) { + $sql = 'SELECT remote_instance_id, remote_title, remote_order, remote_enabled, remote_method_id'; + $sql .= ', remote_method_title, remote_method_description, warehouse_id, old_entry'; + $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element; + $sql .= ' WHERE site_id = ' . ((int) $site_id); + $sql .= ' AND remote_zone_id = ' . ((int) $remote_shipping_zone_info['remote_id']); + $sql .= ' AND entity = ' . ((int) $conf->entity); + $resql = $this->db->query($sql); + if ($resql) { + $shipping_methods = []; + while ($obj = $this->db->fetch_object($resql)) { + $key2 = $obj->remote_instance_id . '_' . $obj->remote_method_id; + $shipping_methods[$key2] = array( + 'remote_zone_id' => ((int) $obj->remote_zone_id), + 'remote_instance_id' => ((int) $obj->remote_instance_id), + 'remote_title' => ((string) $obj->remote_title), + 'remote_order' => ((int) $obj->remote_order), + 'remote_enabled' => !empty($obj->remote_enabled), + 'remote_method_id' => ((string) $obj->remote_method_id), + 'remote_method_title' => ((string) $obj->remote_method_title), + 'remote_method_description' => ((string) $obj->remote_method_description), + 'warehouse_id' => ((int) $obj->warehouse_id), + 'old_entry' => !empty($obj->old_entry) ? 1 : 0, + ); + } + + $shipping_methods = dol_sort_array($shipping_methods, 'remote_order', 'asc', 0, 0, 1); + + $remote_shipping_zones[$key1]['methods'] = $shipping_methods; + } else { + dol_syslog(__METHOD__ . ' SQL: ' . $sql . '; Errors: ' . $this->db->lasterror(), LOG_ERR); + $this->errors[] = $this->db->lasterror(); + return -1; + } + } + + return $remote_shipping_zones; + } + + /** + * Delete all remote shipping zone methods of a site + * + * @param int $site_id Site ID + * @return int >0 if OK, <0 if KO + * @throws Exception + */ + public function delete_all($site_id) + { + global $conf; + dol_syslog(__METHOD__ . " site_id=$site_id"); + + // Delete all line for the site + $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element . ' WHERE site_id = ' . $site_id . ' AND entity = ' . $conf->entity; + $resql = $this->db->query($sql); + if (!$resql) { + dol_syslog(__METHOD__ . ' SQL: ' . $sql . '; Errors: ' . $this->db->lasterror(), LOG_ERR); + $this->errors[] = $this->db->lasterror(); + return -1; + } + + return 1; + } + + /** + * Method to output saved errors + * + * @param string $separator Separator between each error + * @return string String with errors + */ + public function errorsToString($separator = ', ') + { + return (is_array($this->errors) ? join($separator, $this->errors) : ''); + } +} \ No newline at end of file diff --git a/class/data/eCommerceRemoteShippingZones.class.php b/class/data/eCommerceRemoteShippingZones.class.php new file mode 100644 index 0000000..65efe09 --- /dev/null +++ b/class/data/eCommerceRemoteShippingZones.class.php @@ -0,0 +1,252 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file htdocs/ecommerceng/class/data/eCommerceRemoteShippingZones.class.php + * \ingroup ecommerceng + * \brief + */ + + +/** + * Class eCommerceRemoteShippingZones + * + * Put here description of your class + */ +class eCommerceRemoteShippingZones +{ + /** + * @var DoliDB Database handler. + */ + public $db; + /** + * @var string Error + */ + public $error = ''; + /** + * @var array Errors + */ + public $errors = array(); + + public $table_element = 'ecommerceng_remote_shipping_zones'; + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + public function __construct($db) + { + $this->db = $db; + } + + /** + * Set all remote shipping zones of a site + * + * @param int $site_id Site ID + * @param array $remote_shipping_zones List of infos of each remote shipping zones + * @return int >0 if OK, <0 if KO + * @throws Exception + */ + public function set($site_id, $remote_shipping_zones) + { + global $conf, $langs; + dol_syslog(__METHOD__ . " site_id=$site_id, remote_shipping_zones=" . json_encode($remote_shipping_zones)); + + $errors = 0; + $this->errors = array(); + + // Clean values + $site_id = $site_id > 0 ? $site_id : 0; + $remote_shipping_zones = is_array($remote_shipping_zones) ? $remote_shipping_zones : array(); + + // Check values + if ($site_id == 0) { + $this->errors[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ECommerceSite")); + return -1; + } + + $this->db->begin(); + + foreach ($remote_shipping_zones as $infos) { + // Search shipping mode + $sql = 'SELECT rowid'; + $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element; + $sql .= ' WHERE site_id = ' . $site_id; + $sql .= ' AND entity = ' . $conf->entity; + $sql .= " AND remote_id = '" . $this->db->escape($infos['remote_id']) . "'"; + + $resql = $this->db->query($sql); + if (!$resql) { + dol_syslog(__METHOD__ . ' SQL: ' . $sql . '; Errors: ' . $this->db->lasterror(), LOG_ERR); + $this->errors[] = $this->db->lasterror(); + $errors++; + break; + } + + $line_id = 0; + if ($obj = $this->db->fetch_object($resql)) { + $line_id = $obj->rowid; + } + + $this->db->free($resql); + + if ($line_id > 0) { + // Update values + $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET'; + $sql .= " remote_name = '" . $this->db->escape($infos['remote_name']) . "'"; + $sql .= ", remote_order = " . ((int) $infos['remote_order']); + $sql .= ', old_entry = ' . (!empty($infos['old_entry']) ? 1 : 'NULL'); + $sql .= ' WHERE rowid = ' . $line_id; + + $resql = $this->db->query($sql); + } else { + // Insert values + $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(site_id, remote_id, remote_name, remote_order, old_entry, entity) VALUES ('; + $sql .= $site_id; + $sql .= ", '" . $this->db->escape($infos['remote_id']) . "'"; + $sql .= ", '" . $this->db->escape($infos['remote_name']) . "'"; + $sql .= ", " . ((int) $infos['remote_order']); + $sql .= ', ' . (!empty($infos['old_entry']) ? 1 : 'NULL'); + $sql .= ', ' . $conf->entity; + $sql .= ')'; + $resql = $this->db->query($sql); + } + if (!$resql) { + dol_syslog(__METHOD__ . ' SQL: ' . $sql . '; Errors: ' . $this->db->lasterror(), LOG_ERR); + $this->errors[] = $this->db->lasterror(); + $errors++; + break; + } + } + + if ($errors) { + $this->db->rollback(); + return -1; + } else { + $this->db->commit(); + return 1; + } + } + + /** + * Get a remote shipping zone of a site by the remote shipping zone id + * + * @param int $site_id Site ID + * @param string $remote_id Remote shipping zone ID on site + * @return array|int 0 if not found, <0 if errors or array of infos + * @throws Exception + */ + public function get($site_id, $remote_id='') + { + global $conf; + dol_syslog(__METHOD__ . " site_id=$site_id, remote_id=$remote_id"); + + $sql = 'SELECT remote_id, remote_name, remote_order, old_entry FROM ' . MAIN_DB_PREFIX . $this->table_element; + $sql .= ' WHERE site_id = ' . $site_id . ' AND entity = ' . $conf->entity; + $sql .= " AND remote_id = '" . $this->db->escape($remote_id) . "'"; + $resql = $this->db->query($sql); + if ($resql) { + if ($obj = $this->db->fetch_object($resql)) { + return array( + 'remote_id' => $obj->remote_id, + 'remote_name' => $obj->remote_name, + 'remote_order' => $obj->remote_order, + 'old_entry' => !empty($obj->old_entry) ? 1 : 0, + ); + } + + return 0; + } else { + dol_syslog(__METHOD__ . ' SQL: ' . $sql . '; Errors: ' . $this->db->lasterror(), LOG_ERR); + $this->errors[] = $this->db->lasterror(); + return -1; + } + } + + /** + * Get all remote shipping zones of a site + * + * @param int $site_id Site ID + * @return array|int List of all remote shipping zones infos + * @throws Exception + */ + public function get_all($site_id) + { + global $conf; + dol_syslog(__METHOD__ . " site_id=$site_id"); + + $remote_shipping_zones = array(); + + $sql = 'SELECT remote_id, remote_name, remote_order, old_entry FROM ' . MAIN_DB_PREFIX . $this->table_element; + $sql .= ' WHERE site_id = ' . $site_id . ' AND entity = ' . $conf->entity; + $resql = $this->db->query($sql); + if ($resql) { + while ($obj = $this->db->fetch_object($resql)) { + $key = $obj->remote_id; + $remote_shipping_zones[$key] = array( + 'remote_id' => $obj->remote_id, + 'remote_name' => $obj->remote_name, + 'remote_order' => $obj->remote_order, + 'old_entry' => !empty($obj->old_entry) ? 1 : 0, + ); + } + } else { + dol_syslog(__METHOD__ . ' SQL: ' . $sql . '; Errors: ' . $this->db->lasterror(), LOG_ERR); + $this->errors[] = $this->db->lasterror(); + return -1; + } + + $remote_shipping_zones = dol_sort_array($remote_shipping_zones, 'remote_order', 'asc', 0, 0, 1); + return $remote_shipping_zones; + } + + /** + * Delete all remote shipping zones of a site + * + * @param int $site_id Site ID + * @return int >0 if OK, <0 if KO + * @throws Exception + */ + public function delete_all($site_id) + { + global $conf; + dol_syslog(__METHOD__ . " site_id=$site_id"); + + // Delete all line for the site + $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element . ' WHERE site_id = ' . $site_id . ' AND entity = ' . $conf->entity; + $resql = $this->db->query($sql); + if (!$resql) { + dol_syslog(__METHOD__ . ' SQL: ' . $sql . '; Errors: ' . $this->db->lasterror(), LOG_ERR); + $this->errors[] = $this->db->lasterror(); + return -1; + } + + return 1; + } + + /** + * Method to output saved errors + * + * @param string $separator Separator between each error + * @return string String with errors + */ + public function errorsToString($separator = ', ') + { + return (is_array($this->errors) ? join($separator, $this->errors) : ''); + } +} \ No newline at end of file diff --git a/class/data/eCommerceSite.class.php b/class/data/eCommerceSite.class.php index f238248..065794d 100644 --- a/class/data/eCommerceSite.class.php +++ b/class/data/eCommerceSite.class.php @@ -867,7 +867,7 @@ public function upgradeParameters() // stock if (!empty($this->parameters['enable_warehouse_plugin_sl_support'])) { $this->parameters['enable_warehouse_plugin_support'] = 'slfw'; unset($this->parameters['enable_warehouse_plugin_sl_support']); } if (!isset($this->parameters['order_actions']['valid_invoice_fk_warehouse'])) $this->parameters['order_actions']['valid_invoice_fk_warehouse'] = $this->parameters['order_actions']['valid_order_fk_warehouse']; - if (!isset($this->parameters['order_actions']['valid_supplier_invoice_fk_warehouse'])) $this->parameters['order_actions']['valid_supplier_invoice_fk_warehouse'] = $this->parameters['order_actions']['valid_order_fk_warehouse']; + if (!isset($this->parameters['order_actions']['valid_supplier_invoice_fk_warehouse'])) unset($this->parameters['order_actions']['valid_supplier_invoice_fk_warehouse']); // extra fields $table_elements = [ 'societe', 'product', 'commande', 'commandedet' ]; foreach ($table_elements as $table_element) { diff --git a/class/data/woocommerce/eCommerceRemoteAccessWoocommerce.class.php b/class/data/woocommerce/eCommerceRemoteAccessWoocommerce.class.php index 89cddfe..c46925d 100755 --- a/class/data/woocommerce/eCommerceRemoteAccessWoocommerce.class.php +++ b/class/data/woocommerce/eCommerceRemoteAccessWoocommerce.class.php @@ -1686,6 +1686,7 @@ public function convertOrderDataIntoProcessedData($remote_data) } // Set shipping lines + $shipping_lines = []; if (!empty($remote_data['shipping_lines'])) { $shipment_service_id = $this->site->parameters['shipping_service'] > 0 ? $this->site->parameters['shipping_service'] : 0; foreach ($remote_data['shipping_lines'] as $item) { @@ -1733,6 +1734,10 @@ public function convertOrderDataIntoProcessedData($remote_data) } $items[] = $item_data; + $shipping_lines[$item['instance_id'] . '_' . $item['method_id']] = [ + 'instance_id' => $item['instance_id'], + 'method_id' => $item['method_id'], + ]; } } @@ -2111,6 +2116,7 @@ public function convertOrderDataIntoProcessedData($remote_data) 'payment_method_id' => $remote_data['payment_method'], 'payment_amount_ttc' => $remote_data['total'], 'fee_lines' => $fee_lines, + 'shipping_lines' => $shipping_lines, 'refunds' => $refunds, 'extrafields' => [ "ecommerceng_online_payment_{$conf->entity}" => empty($remote_data['transaction_id']) ? 0 : 1, @@ -2333,6 +2339,7 @@ public function convertRefundDataIntoProcessedData($remote_data) } // Set shipping lines + $shipping_lines = []; if (!empty($remote_data['shipping_lines'])) { $shipment_service_id = $this->site->parameters['shipping_service'] > 0 ? $this->site->parameters['shipping_service'] : 0; foreach ($remote_data['shipping_lines'] as $item) { @@ -2383,6 +2390,10 @@ public function convertRefundDataIntoProcessedData($remote_data) $sum_total_ht += $item_data['total_ht']; $items[] = $item_data; + $shipping_lines[$item['instance_id'] . '_' . $item['method_id']] = [ + 'instance_id' => $item['instance_id'], + 'method_id' => $item['method_id'], + ]; } } @@ -2541,6 +2552,7 @@ public function convertRefundDataIntoProcessedData($remote_data) 'total_ttc' => abs($remote_data['amount']), 'items' => $items, 'fee_lines' => $fee_lines, + 'shipping_lines' => $shipping_lines, ]; return $refund; @@ -3180,7 +3192,7 @@ public function convertObjectIntoProductData($remote_id, $object) if (!empty($this->site->parameters['extra_fields']['product']['activated']['mdt'][$cr_key])) { $data_key = $this->site->parameters['extra_fields']['product']['values']['mdt'][$cr_key]; if (!empty($data_key)) { - $variationData['meta_data'][] = array('key' => $data_key, 'value' => $value); + $variationData['meta_data'][] = array('key' => $data_key, 'value' => (string) $value); } } @@ -3451,7 +3463,7 @@ public function convertObjectIntoProductData($remote_id, $object) if (!empty($this->site->parameters['extra_fields']['product']['activated']['mdt'][$cr_key])) { $data_key = $this->site->parameters['extra_fields']['product']['values']['mdt'][$cr_key]; if (!empty($data_key)) { - $productData['meta_data'][] = array('key' => $data_key, 'value' => $value); + $productData['meta_data'][] = array('key' => $data_key, 'value' => (string) $value); } } @@ -5489,11 +5501,11 @@ public function getAllRemoteWarehouses() // plugin warehouse support if (!empty($this->site->parameters['enable_warehouse_plugin_support'])) { $plugin_support = $this->site->parameters['enable_warehouse_plugin_support']; - if ($plugin_support == 'wmlim') { - $remote_warehouses = $this->client->sendToApi(eCommerceClientApi::METHOD_GET, 'locations'); - } else { - $remote_warehouses = $this->worpressclient->sendToApi(eCommerceClientApi::METHOD_GET, 'location'); - } +// if ($plugin_support == 'wmlim') { +// $remote_warehouses = $this->client->sendToApi(eCommerceClientApi::METHOD_GET, 'locations'); +// } else { +// $remote_warehouses = $this->worpressclient->sendToApi(eCommerceClientApi::METHOD_GET, 'location'); +// } $remote_warehouses = $this->worpressclient->sendToApi(eCommerceClientApi::METHOD_GET, $plugin_support == 'wmlim' ? 'locations' : 'location'); if (!isset($remote_warehouses)) { $this->errors[] = $langs->trans('ECommerceWoocommerceGetAllWoocommerceRemoteWarehouses', $this->site->name); @@ -5517,6 +5529,82 @@ public function getAllRemoteWarehouses() return $remoteWarehousesTable; } + /** + * Get all remote shipping zones + * + * @return array|false List of remote shipping zones or false if error + */ + public function getAllRemoteShippingZones() + { + dol_syslog(__METHOD__ . ": Retrieve all Woocommerce remote shipping zones", LOG_DEBUG); + global $langs; + $remoteShippingZonesTable = []; + + // plugin warehouse support + if (!empty($this->site->parameters['enable_warehouse_depending_on_shipping_zone_method'])) { + $remote_shipping_zones = $this->client->sendToApi(eCommerceClientApi::METHOD_GET, 'shipping/zones'); + if (!isset($remote_shipping_zones)) { + $this->errors[] = $langs->trans('ECommerceWoocommerceGetAllWoocommerceRemoteShippingModes', $this->site->name); + $this->errors[] = $this->client->errorsToString(); + dol_syslog(__METHOD__ . ': Error:' . $this->errorsToString(), LOG_ERR); + return false; + } + + foreach ($remote_shipping_zones as $infos) { + $key = $infos["id"]; + $remoteShippingZonesTable[$key] = [ + 'remote_id' => $infos["id"], + 'name' => $infos["name"], + 'order' => $infos["order"], + ]; + } + } + + dol_syslog(__METHOD__ . ": end, return: ".json_encode($remoteShippingZonesTable), LOG_DEBUG); + return $remoteShippingZonesTable; + } + + /** + * Get all remote shipping zone methods + * + * @param integer $remote_zone_id Remote zone ID + * @return array|false List of remote shipping zone methods or false if error + */ + public function getAllRemoteShippingZoneMethods($remote_zone_id) + { + dol_syslog(__METHOD__ . ": Retrieve all Woocommerce remote shipping zone methods - remote_zone_id: " . $remote_zone_id, LOG_DEBUG); + global $langs; + $remote_zone_id = (int) $remote_zone_id; + $remoteShippingZoneMethodsTable = []; + + // plugin warehouse support + if (!empty($this->site->parameters['enable_warehouse_depending_on_shipping_zone_method'])) { + $remote_shipping_zones = $this->client->sendToApi(eCommerceClientApi::METHOD_GET, 'shipping/zones/' . $remote_zone_id . '/methods'); + if (!isset($remote_shipping_zones)) { + $this->errors[] = $langs->trans('ECommerceWoocommerceGetAllWoocommerceRemoteShippingZoneMethods', $this->site->name, $remote_zone_id); + $this->errors[] = $this->client->errorsToString(); + dol_syslog(__METHOD__ . ': Error:' . $this->errorsToString(), LOG_ERR); + return false; + } + + foreach ($remote_shipping_zones as $infos) { + $key = $infos["instance_id"] . '_' . $infos["method_id"]; + $remoteShippingZoneMethodsTable[$key] = [ + 'instance_id' => $infos["instance_id"], + 'title' => $infos["title"], + 'order' => $infos["order"], + 'enabled' => $infos["enabled"], + 'method_id' => $infos["method_id"], + 'method_title' => $infos["method_title"], + 'method_description' => $infos["method_description"], + ]; + } + } + + dol_syslog(__METHOD__ . ": end, return: ".json_encode($remoteShippingZoneMethodsTable), LOG_DEBUG); + return $remoteShippingZoneMethodsTable; + } + /** * Get all webhooks * diff --git a/langs/en_US/ecommerce.lang b/langs/en_US/ecommerce.lang index ebeecb2..eec5a17 100755 --- a/langs/en_US/ecommerce.lang +++ b/langs/en_US/ecommerce.lang @@ -164,11 +164,9 @@ SetupOfWarehouseNotDefinedForThisSite=A new or modified product were found on eC ECommerceStockSyncDirection=Stock synchronisation direction ECommerceStockSyncDirectionDescription=Define if you want to see stock decrease in Dolibarr when stock decrease on Magento (rare), or overwrite Magento stock when stock in Dolibarr is modified (common choice) ECommerceValidOrderWarehouse =Validation d'une commande -ECommerceValidOrderWarehouseDescription =Entrepot utilisé lors de la validation d'une commande pendant une synchronisation de E-Commerce vers Dolibarr +ECommerceValidOrderWarehouseDescription =Entrepot utilisé par défaut lors de la validation d'une commande pendant une synchronisation de E-Commerce vers Dolibarr ECommerceValidInvoiceWarehouse =Validation d'une facture -ECommerceValidInvoiceWarehouseDescription =Entrepot utilisé lors de la validation d'une facture pendant une synchronisation de E-Commerce vers Dolibarr -ECommerceValidSupplierInvoiceWarehouse =Validation d'une facture fournisseur -ECommerceValidSupplierInvoiceWarehouseDescription =Entrepot utilisé lors de la validation d'une facture fournisseur pendant une synchronisation de E-Commerce vers Dolibarr +ECommerceValidInvoiceWarehouseDescription =Entrepot utilisé par défaut lors de la validation d'une facture pendant une synchronisation de E-Commerce vers Dolibarr MainSyncSetup=Main synchronization setup StockSyncSetup=Stock synchronization setup ErrorModuleSoapRequired=Error: PHP module SOAP is required to have this module working. diff --git a/langs/en_US/woocommerce.lang b/langs/en_US/woocommerce.lang index 015eb6b..0420156 100644 --- a/langs/en_US/woocommerce.lang +++ b/langs/en_US/woocommerce.lang @@ -158,7 +158,7 @@ ECommerceWoocommerceErrorGetOrderData = Erreur lo ECommerceWooCommerceErrorLanguageMatchNotFound = La correspondance de langue pour le code '%s' et site ID '%s' n'as pas été trouvée dans le dictionnaire 'Liste des langues sur Woocommerce' ECommerceWoocommerceGetRemoteProduct = Récupération du produit distant (ID distant: %s) sur le site '%s' -ECommerceErrorWarehouseIdNotSpecifiedForTheLine = L'entrepôt distant n'a pas été trouver pour la ligne de produit +ECommerceErrorWarehouseIdNotSpecifiedForTheLine = L'entrepôt distant n'a pas été trouver pour la ligne de produit %s ECommerceErrorWarehouseIdNotConfiguredForRemoteWarehouse = L'entrepôt dolibarr n'as pas été configuré pour l'entrepôt distant (ID: %s) sur le site '%s' ECommerceErrorWhenMovementStockOnDifferentWarehouse = Erreur lors de la mise a jour des stocks des produits sur differents entrepots ECommerceErrorValidOrderWarehouseNotConfigured = L'entrepot par defaut pour la validation de la commande n'est pas configuré pour le site '%s' diff --git a/langs/fr_FR/ecommerce.lang b/langs/fr_FR/ecommerce.lang index 9396360..c3da784 100644 --- a/langs/fr_FR/ecommerce.lang +++ b/langs/fr_FR/ecommerce.lang @@ -166,11 +166,9 @@ SetupOfWarehouseNotDefinedForThisSite ECommerceStockSyncDirection =Synchronisation des stocks ECommerceStockSyncDirectionDescription =Ce paramètre détermine :
- Si le stock doit être décrémenté dans Dolibarr suite à une décrémentation du stock E-Commerce (rare) ou
- Si le stock E-Commerce doit être remplacé par le stock Dolibarr suite à sa modification (choix commun) ECommerceValidOrderWarehouse =Validation d'une commande -ECommerceValidOrderWarehouseDescription =Entrepot utilisé lors de la validation d'une commande pendant une synchronisation de E-Commerce vers Dolibarr +ECommerceValidOrderWarehouseDescription =Entrepot utilisé par défaut lors de la validation d'une commande pendant une synchronisation de E-Commerce vers Dolibarr ECommerceValidInvoiceWarehouse =Validation d'une facture -ECommerceValidInvoiceWarehouseDescription =Entrepot utilisé lors de la validation d'une facture pendant une synchronisation de E-Commerce vers Dolibarr -ECommerceValidSupplierInvoiceWarehouse =Validation d'une facture fournisseur -ECommerceValidSupplierInvoiceWarehouseDescription =Entrepot utilisé lors de la validation d'une facture fournisseur pendant une synchronisation de E-Commerce vers Dolibarr +ECommerceValidInvoiceWarehouseDescription =Entrepot utilisé par défaut lors de la validation d'une facture pendant une synchronisation de E-Commerce vers Dolibarr MainSyncSetup =Synchronisation générale StockSyncSetup =Synchronisation des stocks ErrorModuleSoapRequired =Erreur: le module PHP SOAP est requis pour le bon fonctionnement du module. @@ -339,6 +337,21 @@ ECommerceConfirmUpdateRemoteWarehouses ECommerceRemoteWarehousesUpdated =Les entrepôts distants depuis le site ont été mise à jour. ECommerceRemoteID =ID distant +ECommerceMovementStockOnValidateOrderOrInvoice =Parametrage des entrepôts utiliser lors de la validation de la %s +ECommerceSearchWarehouseOrder =Ordre de recherche de l'entrepôt à utilisé lors de la validation pour la ligne de produit : +ECommerceSearchWarehouseByMultiWarehouseCorrespondence =
- Par la configuration multi-entrepots +ECommerceSearchWarehouseByShippingMethodCorrespondence =
- Par la configuration des modes de transport +ECommerceSearchWarehouseByDefaultWarehouse =
- Par la configuration par défaut + +ECommerceRemoteShippingZoneMethodsCorrespondence =Correspondance des entrpôts avec le mode de transport +ECommerceUpdateRemoteShippingZoneMethods =Mise à jour des modes de transport distants +ECommerceUpdateRemoteShippingMethods =Mise à jour des modes de transport distants +ECommerceConfirmUpdateRemoteShippingMethods =Confirmer la mise à jour des modes de transport distants pour ce site ? +ECommerceEnableWarehouseDependingOnShippingZoneMethodSupport =Choix de l'entrepot en fonction du mode de transport +ECommerceEnableWarehouseDependingOnShippingZoneMethodSupportDescription =Définir l'entrepot utilisé lors de la validation en fonction du mode de transport
(si la commande a plusieurs mode de transport seul le premier rencontrer sera pris en compte) +ECommerceRemoteShippingZoneMethodsUpdated =Les modes de transport distants depuis le site ont été mise à jour. +ECommerceShippingMethodOldEntry =Ancien mode de transport + ECommercePaymentGatewaysCorrespondence =Correspondance des modes de paiements avec le site ECommercePaymentGatewayLabel =Mode de paiement sur le site ECommerceUpdatePaymentGateways =Mise à jour des modes de paiement diff --git a/langs/fr_FR/woocommerce.lang b/langs/fr_FR/woocommerce.lang index afa8f8c..d48f21c 100644 --- a/langs/fr_FR/woocommerce.lang +++ b/langs/fr_FR/woocommerce.lang @@ -39,6 +39,8 @@ ECommerceWoocommerceGetAllWoocommercePaymentGateways =Erreur de ECommerceWoocommerceGetAllWoocommerceRemoteWarehouses =Erreur de récupération des données des entrepots distants sur le site '%s': ECommerceWoocommerceGetTranslatedProductVariation =Erreur de récupération de la traduction 'id:%s' de la variation 'id:%s' du produit distant 'id:%s' sur le site '%s': ECommerceWoocommerceErrorGetRemoteProduct =Erreur de récupération du produit distant 'id:%s' sur le site '%s': +ECommerceWoocommerceGetAllWoocommerceRemoteShippingModes =Erreur de récupération des données des zones de transport distants sur le site '%s': +ECommerceWoocommerceGetAllWoocommerceRemoteShippingZoneMethods =Erreur de récupération des données des modes de transport distants sur le site '%s' et la zone (ID distant : %s): ECommerceWoocommerceStatusProductSupported = Statuts supportés ECommerceWoocommerceStatusProductSupportedDescription = Liste des statuts supportés pour la synchronisation.(vide pour tous les statuts; liste de valeurs séparées par des virgules)
Seules les valeurs retournées par l'API sont supportées. ex: draft, pending, private, publish, ... @@ -169,7 +171,7 @@ ECommerceWoocommerceErrorGetOrderData = Erreur lo ECommerceWooCommerceErrorLanguageMatchNotFound = La correspondance de langue pour le code '%s' et site ID '%s' n'as pas été trouvée dans le dictionnaire 'Liste des langues sur Woocommerce' ECommerceWoocommerceGetRemoteProduct = Récupération du produit distant (ID distant: %s) sur le site '%s' -ECommerceErrorWarehouseIdNotSpecifiedForTheLine = L'entrepôt distant n'a pas été trouver pour la ligne de produit +ECommerceErrorWarehouseIdNotSpecifiedForTheLine = L'entrepôt distant n'a pas été trouver pour la ligne de produit %s ECommerceErrorWarehouseIdNotConfiguredForRemoteWarehouse = L'entrepôt dolibarr n'as pas été configuré pour l'entrepôt distant (ID: %s) sur le site '%s' ECommerceErrorWhenMovementStockOnDifferentWarehouse = Erreur lors de la mise a jour des stocks des produits sur differents entrepots ECommerceErrorValidOrderWarehouseNotConfigured = L'entrepot par defaut pour la validation de la commande n'est pas configuré pour le site '%s' diff --git a/lib/eCommerce.lib.php b/lib/eCommerce.lib.php index 9e5448f..4337b2d 100644 --- a/lib/eCommerce.lib.php +++ b/lib/eCommerce.lib.php @@ -853,6 +853,113 @@ function ecommerceng_update_remote_warehouses($db, $site) return true; } +function ecommerceng_update_remote_shipping_zone_methods($db, $site) +{ + global $conf, $langs; + $langs->load('woocommerce@ecommerceng'); + + if (empty($site->parameters['enable_warehouse_depending_on_shipping_zone_method'])) { + return 1; + } + + dol_include_once('/ecommerceng/class/business/eCommerceSynchro.class.php'); + $synchro = new eCommerceSynchro($db, $site, 0, 0); + + dol_syslog("site.php Try to connect to eCommerce site " . $site->name); + $result = $synchro->connect(); + if (!$result) { + setEventMessages($synchro->error, $synchro->errors, 'errors'); + return false; + } + + $remote_shipping_zones_list = $synchro->getAllRemoteShippingZones(); + if ($remote_shipping_zones_list === false) { + setEventMessages($synchro->error, $synchro->errors, 'errors'); + return false; + } + + // Get all shipping zone methods + dol_include_once('/ecommerceng/class/data/eCommerceRemoteShippingZoneMethods.class.php'); + $remote_shipping_zone_methods = new eCommerceRemoteShippingZoneMethods($db); + $currentRemoteShippingZoneMethods = $remote_shipping_zone_methods->get_all($site->id); + if (!is_array($currentRemoteShippingZoneMethods) && $currentRemoteShippingZoneMethods < 0) { + setEventMessages($remote_shipping_zone_methods->error, $remote_shipping_zone_methods->errors, 'errors'); + return false; + } + + $finalRemoteShippingZoneMethods = array(); + + // Add remotes shipping modes + foreach ($remote_shipping_zones_list as $key1 => $remote_shipping_zone_infos) { + $finalRemoteShippingZoneMethods[$key1] = array( + 'remote_id' => $remote_shipping_zone_infos['remote_id'], + 'remote_name' => $remote_shipping_zone_infos['name'], + 'remote_order' => $remote_shipping_zone_infos['order'], + 'old_entry' => 0, + ); + + $remote_shipping_zone_methods_list = $synchro->getAllRemoteShippingZoneMethods($remote_shipping_zone_infos['remote_id']); + if ($remote_shipping_zone_methods_list === false) { + setEventMessages($synchro->error, $synchro->errors, 'errors'); + return false; + } + + foreach ($remote_shipping_zone_methods_list as $key2 => $infos) { + $current_method = isset($currentRemoteShippingZoneMethods[$key1]['methods'][$key2]) ? $currentRemoteShippingZoneMethods[$key1]['methods'][$key2] : []; + + $finalRemoteShippingZoneMethods[$key1]['methods'][$key2] = array( + 'remote_zone_id' => $remote_shipping_zone_infos['remote_id'], + 'remote_instance_id' => $infos['instance_id'], + 'remote_title' => $infos['title'], + 'remote_order' => $infos['order'], + 'remote_enabled' => !empty($infos['enabled']), + 'remote_method_id' => $infos['method_id'], + 'remote_method_title' => $infos['method_title'], + 'remote_method_description' => $infos['method_description'], + 'warehouse_id' => !empty($current_method['warehouse_id']) && $current_method['warehouse_id'] > 0 ? $current_method['warehouse_id'] : 0, + 'old_entry' => 0, + ); + } + } + + // Add current shipping zones and shipping zone methods who have been deleted + foreach ($currentRemoteShippingZoneMethods as $key1 => $remote_shipping_zone_infos) { + if (!isset($finalRemoteShippingZoneMethods[$key1])) { + $finalRemoteShippingZoneMethods[$key1] = array( + 'remote_id' => $remote_shipping_zone_infos['remote_id'], + 'remote_name' => $remote_shipping_zone_infos['remote_name'], + 'remote_order' => $remote_shipping_zone_infos['remote_order'], + 'old_entry' => 1, + ); + } + + foreach ($remote_shipping_zone_infos['methods'] as $key2 => $infos) { + if (!isset($finalRemoteShippingZoneMethods[$key1]['methods'][$key2])) { + $finalRemoteShippingZoneMethods[$key1]['methods'][$key2] = array( + 'remote_zone_id' => $infos['remote_zone_id'], + 'remote_instance_id' => $infos['remote_instance_id'], + 'remote_title' => $infos['remote_title'], + 'remote_order' => $infos['remote_order'], + 'remote_enabled' => false, + 'remote_method_id' => $infos['remote_method_id'], + 'remote_method_title' => $infos['remote_method_title'], + 'remote_method_description' => $infos['remote_method_description'], + 'warehouse_id' => $infos['warehouse_id'], + 'old_entry' => 1, + ); + } + } + } + + $result = $remote_shipping_zone_methods->set($site->id, $finalRemoteShippingZoneMethods); + if ($result < 0) { + setEventMessages($remote_shipping_zone_methods->error, $remote_shipping_zone_methods->errors, 'errors'); + return false; + } + + return true; +} + function get_company_by_email($db, $email, $site=0) { $email = $db->escape($email); diff --git a/sql/llx_ecommerceng_remote_shipping_zone_methods.key.sql b/sql/llx_ecommerceng_remote_shipping_zone_methods.key.sql new file mode 100644 index 0000000..81d89e5 --- /dev/null +++ b/sql/llx_ecommerceng_remote_shipping_zone_methods.key.sql @@ -0,0 +1,20 @@ +-- =================================================================== +-- Copyright (C) 2024 Easya Solutions +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 2 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program; if not, write to the Free Software +-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +-- +-- =================================================================== + +ALTER TABLE llx_ecommerceng_remote_shipping_zone_methods ADD UNIQUE INDEX uk_ecommerceng_rszm(site_id, remote_zone_id, remote_instance_id, remote_method_id, entity); +--ALTER TABLE llx_ecommerceng_remote_shipping_zone_methods ADD CONSTRAINT fk_ecommerceng_remote_rszm_warehouse_id FOREIGN KEY (warehouse_id) REFERENCES llx_entrepot(rowid); \ No newline at end of file diff --git a/sql/llx_ecommerceng_remote_shipping_zone_methods.sql b/sql/llx_ecommerceng_remote_shipping_zone_methods.sql new file mode 100644 index 0000000..08802da --- /dev/null +++ b/sql/llx_ecommerceng_remote_shipping_zone_methods.sql @@ -0,0 +1,34 @@ +-- =================================================================== +-- Copyright (C) 2024 Easya Solutions +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 2 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program; if not, write to the Free Software +-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +-- +-- =================================================================== + +create table llx_ecommerceng_remote_shipping_zone_methods +( + rowid integer AUTO_INCREMENT PRIMARY KEY, + site_id integer NOT NULL, + remote_zone_id integer NOT NULL, -- Remote shipping zone ID + remote_instance_id integer NOT NULL, -- Remote shipping zone method instance ID + remote_title varchar(255) NOT NULL, -- Remote shipping zone method customer facing title + remote_order integer NOT NULL, -- Remote shipping zone method sort order + remote_enabled integer(1) NOT NULL, -- Remote shipping zone method enabled status + remote_method_id varchar(255) NOT NULL, -- Remote shipping zone method ID + remote_method_title varchar(255) NOT NULL, -- Remote shipping zone method title + remote_method_description TEXT NOT NULL, -- Remote shipping zone method description + warehouse_id integer, -- Dolibarr warehouse ID + old_entry tinyint(1), -- Flag for set if this shipping zone method has been delete on WooCommerce + entity integer DEFAULT 1 +) ENGINE=InnoDB; \ No newline at end of file diff --git a/sql/llx_ecommerceng_remote_shipping_zones.key.sql b/sql/llx_ecommerceng_remote_shipping_zones.key.sql new file mode 100644 index 0000000..d661e1a --- /dev/null +++ b/sql/llx_ecommerceng_remote_shipping_zones.key.sql @@ -0,0 +1,19 @@ +-- =================================================================== +-- Copyright (C) 2024 Easya Solutions +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 2 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program; if not, write to the Free Software +-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +-- +-- =================================================================== + +ALTER TABLE llx_ecommerceng_remote_shipping_zones ADD UNIQUE INDEX uk_ecommerceng_remote_shipping_zones(site_id, remote_id, entity); diff --git a/sql/llx_ecommerceng_remote_shipping_zones.sql b/sql/llx_ecommerceng_remote_shipping_zones.sql new file mode 100644 index 0000000..8941f0f --- /dev/null +++ b/sql/llx_ecommerceng_remote_shipping_zones.sql @@ -0,0 +1,28 @@ +-- =================================================================== +-- Copyright (C) 2024 Easya Solutions +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 2 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program; if not, write to the Free Software +-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +-- +-- =================================================================== + +create table llx_ecommerceng_remote_shipping_zones +( + rowid integer AUTO_INCREMENT PRIMARY KEY, + site_id integer NOT NULL, + remote_id integer NOT NULL, --Remote shipping zone ID + remote_name varchar(255) NOT NULL, --Remote shipping zone name + remote_order integer NOT NULL, --Remote shipping zone position + old_entry tinyint(1), -- Flag for set if this shipping zone has been delete on WooCommerce + entity integer DEFAULT 1 +) ENGINE=InnoDB; \ No newline at end of file