Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torunar/41e62214bcd53ecc08f0c1a06850e5ad to your computer and use it in GitHub Desktop.
Save torunar/41e62214bcd53ecc08f0c1a06850e5ad to your computer and use it in GitHub Desktop.
diff --git a/app/addons/suppliers/controllers/backend/shippings.post.php b/app/addons/suppliers/controllers/backend/shippings.post.php
index e51c4cd139..8aa64cb7e6 100644
--- a/app/addons/suppliers/controllers/backend/shippings.post.php
+++ b/app/addons/suppliers/controllers/backend/shippings.post.php
@@ -25,6 +25,16 @@ if ($mode == 'update') {
$shipping_data = Tygh::$app['view']->getTemplateVars('shipping');
list($suppliers) = fn_get_suppliers();
+ if (fn_allowed_for('ULTIMATE') && !fn_get_runtime_company_id()) {
+ $suppliers = fn_suppliers_filter_objects_by_sharing(
+ $suppliers,
+ 'suppliers',
+ 'supplier_id',
+ 'shippings',
+ $shipping_data['shipping_id']
+ );
+ }
+
$linked_suppliers = fn_get_shippings_suppliers($shipping_data['shipping_id']);
Tygh::$app['view']->assign('suppliers', $suppliers);
diff --git a/app/addons/suppliers/controllers/backend/suppliers.php b/app/addons/suppliers/controllers/backend/suppliers.php
index 924d8ba317..e9989a6d11 100644
--- a/app/addons/suppliers/controllers/backend/suppliers.php
+++ b/app/addons/suppliers/controllers/backend/suppliers.php
@@ -85,34 +85,47 @@ if ($mode == 'manage') {
} elseif ($mode == 'update' || $mode == 'add') {
- Registry::set('navigation.tabs', array (
- 'general' => array (
+ $supplier_id = isset($_REQUEST['supplier_id'])
+ ? $_REQUEST['supplier_id']
+ : null;
+
+ $tabs = [
+ 'general' => [
'title' => __('general'),
- 'js' => true
- ),
- 'products' => array (
+ 'js' => true,
+ ],
+ 'products' => [
'title' => __('products'),
- 'js' => true
- ),
- 'shippings' => array (
+ 'js' => true,
+ ],
+ ];
+
+ if ($supplier_id || fn_allowed_for('MULTIVENDOR') || fn_get_runtime_company_id()) {
+ $tabs['shippings'] = [
'title' => __('shippings'),
- 'js' => true
- ),
- ));
-
- $supplier = !empty($_REQUEST['supplier_id']) ? fn_get_supplier_data($_REQUEST['supplier_id']) : array();
-
- $condition = " AND ?:shippings.status = 'A'";
- if (Registry::get('runtime.company_id') && !fn_allowed_for('ULTIMATE')) {
- $condition = fn_get_company_condition('?:shippings.company_id');
- $company_data = Registry::get('runtime.company_data');
- if (!empty($company_data['shippings'])) {
- $condition .= db_quote(" OR ?:shippings.shipping_id IN (?n)", explode(',', $company_data['shippings']));
- }
+ 'js' => true,
+ ];
}
- $shippings = db_get_hash_array("SELECT ?:shippings.shipping_id, ?:shipping_descriptions.shipping FROM ?:shippings LEFT JOIN ?:shipping_descriptions ON ?:shippings.shipping_id = ?:shipping_descriptions.shipping_id AND ?:shipping_descriptions.lang_code = ?s LEFT JOIN ?:companies ON ?:companies.company_id = ?:shippings.company_id WHERE 1 $condition ORDER BY ?:shippings.position", 'shipping_id', CART_LANGUAGE);
+ Registry::set('navigation.tabs', $tabs);
+
+ $supplier = $supplier_id
+ ? fn_get_supplier_data($supplier_id)
+ : [];
+
+ $company_id = Registry::ifGet('runtime.company_id', null);
+ $shippings = fn_get_available_shippings($company_id);
+ if (fn_allowed_for('ULTIMATE') && !fn_get_runtime_company_id()) {
+ $shippings = fn_suppliers_filter_objects_by_sharing(
+ $shippings,
+ 'shippings',
+ 'shipping_id',
+ 'suppliers',
+ $supplier_id
+ );
+ }
+ /** @var \Tygh\SmartyEngine\Core $view */
$view = Tygh::$app['view'];
$view->assign('shippings', $shippings);
diff --git a/app/addons/suppliers/func.php b/app/addons/suppliers/func.php
index 1b9bc78bc0..9856d149cc 100644
--- a/app/addons/suppliers/func.php
+++ b/app/addons/suppliers/func.php
@@ -12,11 +12,10 @@
* "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE. *
****************************************************************************/
-use Tygh\Registry;
+use Tygh\Addons\ProductVariations\ServiceProvider as ProductVariationsServiceProvider;
use Tygh\Navigation\LastView;
-use Tygh\Settings;
use Tygh\Pdf;
-use Tygh\Addons\ProductVariations\ServiceProvider as ProductVariationsServiceProvider;
+use Tygh\Registry;
if (!defined('BOOTSTRAP')) { die('Access denied'); }
@@ -63,25 +62,42 @@ function fn_update_supplier($supplier_id, $supplier_data)
/**
* Update supplier shippings links
*
- * @param int $supplier_id
- * @param array $shippings
+ * @param int $supplier_id Supplier ID
+ * @param int[] $shippings Shipping method IDs
+ *
* @return bool Always true
*/
function fn_update_supplier_shippings($supplier_id, $shippings)
{
- db_query('DELETE FROM ?:supplier_links WHERE object_type = ?s AND supplier_id = ?i', 'S', $supplier_id);
+ $current_supplier_data = fn_get_supplier_data($supplier_id);
+ $deleted_shippings = array_diff($current_supplier_data['shippings'], $shippings);
- if (!empty($shippings)) {
- $parts = array();
-
- foreach ($shippings as $shipping_id) {
- $parts[] = db_quote('(?i, ?i, ?s)', $supplier_id, $shipping_id, 'S');
- }
+ /**
+ * Executes when updating a supplier's shipping methods, before removing shipping methods links.
+ * Allows you to modify the list of shipping methods that would be removed
+ *
+ * @param int $supplier_id Supplier ID
+ * @param int[] $shippings Shipping method IDs
+ * @param array $current_supplier_data Current supplier data
+ * @param int[] $deleted_shippings Deleted shipping method IDs
+ */
+ fn_set_hook('suppliers_update_supplier_shippings_before_delete_shippings', $supplier_id, $shippings, $current_supplier_data, $deleted_shippings);
+
+ if ($deleted_shippings) {
+ db_query(
+ 'DELETE FROM ?:supplier_links WHERE object_type = ?s AND supplier_id = ?i AND object_id IN (?n)',
+ 'S',
+ $supplier_id,
+ $deleted_shippings
+ );
+ }
- if (!empty($parts)) {
- $query = 'INSERT INTO ?:supplier_links (supplier_id, object_id, object_type) VALUES ' . implode(',', $parts);
- db_query($query);
- }
+ foreach ($shippings as $shipping_id) {
+ db_replace_into('supplier_links', [
+ 'supplier_id' => $supplier_id,
+ 'object_id' => $shipping_id,
+ 'object_type' => 'S',
+ ]);
}
return true;
@@ -1005,14 +1021,14 @@ function fn_suppliers_store_shipping_rates_pre($order_id, &$cart, $customer_auth
}
/**
- * Generate order invoice for supplier.
+ * Generate order invoice for supplier.
*
* @param array $order_ids List of order identifiers
* @param array $supplier Supplier data
* @param bool $pdf Whether to create pdf, default false
* @param string $lang_code Language code
*
- * @return false|string
+ * @return false|string
*/
function fn_print_supplier_invoices($order_ids, $supplier, $pdf = false, $lang_code = CART_LANGUAGE)
{
@@ -1024,7 +1040,7 @@ function fn_print_supplier_invoices($order_ids, $supplier, $pdf = false, $lang_c
$view->assign('profile_fields', fn_get_profile_fields('I'));
$view->assign('supplier', $supplier);
}
-
+
if (!is_array($order_ids)) {
$order_ids = array($order_ids);
}
@@ -1104,3 +1120,72 @@ function fn_product_variations_suppliers_link_product_post($supplier_id, $produc
$sync_service = ProductVariationsServiceProvider::getSyncService();
$sync_service->onTableChanged('supplier_links', $product_id, ['supplier_id' => $supplier_id]);
}
+
+/**
+ * Hook handler: prevents removal of shared shipping methods from supplier when editing a supplier in the shared store.
+ */
+function fn_ult_suppliers_update_supplier_shippings_before_delete_shippings($supplier_id, $shippings, $current_supplier_data, &$deleted_shippings)
+{
+ $runtime_company_id = fn_get_runtime_company_id();
+ if (!$runtime_company_id) {
+ return;
+ }
+
+ $sharing_backup = Registry::get('runtime.skip_sharing_selection');
+ Registry::set('runtime.skip_sharing_selection', true);
+
+ $deleted_shippings_owners = db_get_hash_single_array(
+ 'SELECT shipping_id, company_id FROM ?:shippings WHERE shipping_id IN (?n)',
+ ['shipping_id', 'company_id'],
+ $deleted_shippings
+ );
+
+ Registry::set('runtime.skip_sharing_selection', $sharing_backup);
+
+ $deleted_shippings = array_filter(
+ $deleted_shippings,
+ function ($shipping_id) use ($runtime_company_id, $deleted_shippings_owners) {
+ return isset($deleted_shippings_owners[$shipping_id])
+ && $deleted_shippings_owners[$shipping_id] == $runtime_company_id;
+ }
+ );
+}
+
+/**
+ * Filters source entities list by its availability for the shared entitiy's companies.
+ *
+ * @param array $objects_list Source entities list
+ * @param string $source_type Source entities' sharing object_type
+ * @param string $source_id_field Field of source entity which stores its ID
+ * @param string $shared_type Shared entity's sharing object_type
+ * @param int $shared_object_id Shared entity ID
+ *
+ * @return array
+ */
+function fn_suppliers_filter_objects_by_sharing(
+ array $objects_list,
+ $source_type,
+ $source_id_field,
+ $shared_type,
+ $shared_object_id
+) {
+ $shared_object_companies = fn_ult_get_object_shared_companies($shared_type, $shared_object_id);
+ if (!$shared_object_companies) {
+ return [];
+ }
+
+ $filtered_list = array_filter(
+ $objects_list,
+ function ($source_object) use ($source_id_field, $source_type, $shared_object_companies) {
+ foreach ($shared_object_companies as $company_id) {
+ if (fn_ult_is_shared_object($source_type, $source_object[$source_id_field], $company_id)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+ );
+
+ return $filtered_list;
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment