Files
bio-concept-labo/web/modules/cdorderlimit/cdorderlimit.php
2019-11-20 07:44:43 +01:00

392 lines
15 KiB
PHP

<?php
/**
* cdorderlimit :: Autoriser les commandes sous certaines conditions
*
* @author contact@cleanpresta.com (www.cleanpresta.com)
* @copyright 2013-2016 cleandev.net
* @license You only can use module, nothing more!
*/
if (!defined('_PS_VERSION_'))
exit;
if(!class_exists('CleanModule3'))
require_once(dirname(__FILE__) .'/CleanModule3.php');
class CdOrderLimit extends CleanModule3
{
private $_html = '';
public function __construct()
{
$this->name = 'cdorderlimit';
$this->tab = 'administration';
$this->version = '3.1.0';
$this->author = 'cleanpresta.com';
$this->mprefix = "CDOL_";
$this->reference = "CDOL";
$this->addon_id = '19443'; // addon module id
$this->author = $this->l('CleanPresta');
$this->ps_versions_compliancy['min'] = '1.6.0'; // min ps version
$this->ps_versions_compliancy['max'] = '1.8.0'; // min ps version
parent::__construct();
$this->module_key = 'd8144272b5049f0949976ac5a58d5cba';
$this->displayName = $this->l('Restrict or allow orders conditionally');
$this->description = $this->l('This module allows or restrict orders under certain conditions: amount of the order, quantity of products ....');
$this->full_description = $this->l('This module allows or restrict orders under certain conditions: amount of the order, quantity of products ....');
//cleanpresta var
$this->hooks = array('displayHeader','displayShoppingCartFooter', 'displayTop', 'actionFrontControllerAfterInit');
$this->config_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs'
),
'tabs' => array(
'tab1' => $this->l('Cart Conditions'),
'tab2' => $this->l('Suppliers Conditions'),
'tab4' => $this->l('Inclusions'),
'tab5' => $this->l('Exclusions'),
),
'description' => $this->description, // can be success, error, warning
'input' => array(
/*array(
'type' => $this->formBoolType,
'label' => $this->l('Suppliers Order'),
'name' => 'CDO_SUPPLIER_LIMIT',
'is_bool' => true,
'values' => array(
array('id' => 'type_switch_on','value' => 1,'label' => $this->l('Yes')),
array('id' => 'type_switch_off','value' => 0,'label' => $this->l('No'))
),
'desc' => $this->l('Requires the customer to order for a single supplier'),
'tab' => 'tab2',
),*/
array(
'type' => 'radio',
'label' => $this->l('Limit option'),
'name' => 'CDO_OPTION_LIMITE',
'required' => true,
'desc' => $this->l(''),
'default' => 'MIN_AM',
'class' => 't',
'tab' => 'tab1',
'values' => array(
array('id' => 'active_0','value' => 'MIN_AM', 'label' => $this->l('Minimum amount in the Cart')),
array('id' => 'active_1','value' => 'MAX_AM', 'label' => $this->l('Maximum amount in the Cart')),
array('id' => 'active_2','value' => 'MIN_QTY', 'label' => $this->l('Quantity Minimum in the Cart')),
array('id' => 'active_3','value' => 'MAX_QTY', 'label' => $this->l('Quantity Maximum in the Cart')),
array('id' => 'active_4','value' => 'MUL_QTY', 'label' => $this->l('Multiple quantity in the Cart of')),
array('id' => 'active_5','value' => 'N_MUL_QTY', 'label' => $this->l('Not a multiple quantity in the Cart of'))
)
),
array(
'type' => 'text',
'label' => $this->l('Limit value'),
'name' => 'CDO_VALUE',
'required' => true,
'desc' => $this->l('Enter the value of the constraint.'),
'tab' => 'tab1',
),
array(
'type' => 'radio',
'name' => 'CDO_APPLICABLE',
'label' => $this->l('Apply on'),
'desc' => $this->l('Select the type or product category on which we apply the constraint'),
'is_bool' => true,
'tab' => 'tab1',
'values' => array(
array(
'id' => 'active_cart',
'value' => 'APP_CART',
'label' => $this->l('Order')
),
array(
'id' => 'active_prd',
'value' => 'APP_PRD',
'label' => $this->l('Product')
),
array(
'id' => 'active_cat',
'value' => 'APP_CAT',
'label' => $this->l('Category')
)
)
),
array(
'type' => 'radio',
'name' => 'CDO_APPLIQUE_SUR',
'label' => $this->l('Types of items concerned'),
'desc' => $this->l('Select "Product" in "A applied to the " before choosing the types of items concerned.'),
'is_bool' => true,
'tab' => 'tab4',
'values' => array(
array(
'id' => 'active_on',
'value' => 'EXP_PRD',
'label' => $this->l('Product')
),
array(
'id' => 'active_off',
'value' => 'EXP_CAT',
'label' => $this->l('Product Category')
)
)
),
array(
'type' => 'text',
'label' => $this->l('Ids Concerned'),
'name' => 'CDO_IDS_CONCERNE',
'tab' => 'tab4',
'desc' => $this->l('Enter Ids product or category concerned separated by comma. When nothing is indicated, that will take into account all the products and categories of the cart'),
),
array(
'type' => 'group',
'label' => $this->l('Customers Group'),
'name' => 'CDO_GROUP',
'values' => Group::getGroups(Context::getContext()->language->id),
'info_introduction' => $this->l('You now have three default customer groups.'),
'hint' => $this->l('Select the groups concerned'),
'desc' => $this->l('Will choose the groups of customers whom you want to apply the limits. If you do not choose anything, the limits will be applied to all the groups customers.'),
'tab' => 'tab4'
),
array(
'type' => 'radio',
'name' => 'CDO_APPLIQUE_SUR_OFF',
'label' => $this->l('Types of items concerned'),
'desc' => $this->l('Type of elements to exclude in the constraints to apply. This only applies in case you have chosen "products" previously in "Apply to".'),
'is_bool' => true,
'tab' => 'tab5',
'values' => array(
array(
'id' => 'active_on',
'value' => 'EXP_PRD_OFF',
'label' => $this->l('Product')
),
array(
'id' => 'active_off',
'value' => 'EXP_CAT_OFF',
'label' => $this->l('Product Category')
)
)
),
array(
'type' => 'text',
'label' => $this->l('Ids Concerned'),
'name' => 'CDO_IDS_CONCERNE_OFF',
'tab' => 'tab5',
'desc' => $this->l('Enter Ids product or category concerned separated by comma. When nothing is indicated, that will take into account all the products and categories of the cart'),
),
array(
'type' => 'group',
'label' => $this->l('Customers Group'),
'name' => 'CDO_GROUP_OFF',
'values' => Group::getGroups(Context::getContext()->language->id),
'info_introduction' => $this->l('You now have three default customer groups.'),
'hint' => $this->l('Select the groups concerned'),
'desc' => $this->l('Choose the groups of customers that you do not want to apply the limits. If you choose nothing, the limits will be applied to all customer groups.'),
'tab' => 'tab5'
),
),
'submit' => array(
'title' => $this->l('Save')
)
)
);
//table of error
$this->configs = $this->getConfigValues();
$this->cartLimitError = array(
'APP_CART-MIN_AM' => $this->l('you must have a cart amount minimum of : ').Tools::displayPrice($this->configs['cdo_value']),
'APP_CART-MAX_AM' => $this->l('you must have a cart amount maximun of : ').Tools::displayPrice($this->configs['cdo_value']),
'APP_CART-MIN_QTY' => $this->l('you must have a cart quantity minimum of : ').$this->configs['cdo_value'],
'APP_CART-MAX_QTY' => $this->l('you must have a cart quantity maximun of : ').$this->configs['cdo_value'],
'APP_CART-MUL_QTY' => $this->l('you must have a cart quantity of multiple of : ').$this->configs['cdo_value'],
'APP_CART-N_MUL_QTY' => $this->l('you must have a cart quantity of not multiple of : ').$this->configs['cdo_value'],
'APP_PRD-MIN_AM' => $this->l('you must have a amount minimum of each products of : ').Tools::displayPrice($this->configs['cdo_value']),
'APP_PRD-MAX_AM' => $this->l('you must have a amount maximum of each products of : ').Tools::displayPrice($this->configs['cdo_value']),
'APP_PRD-MIN_QTY' => $this->l('you must have a quantity minimum of each products of : ').$this->configs['cdo_value'],
'APP_PRD-MAX_QTY' => $this->l('you must have a quantity maximum of each products of : ').$this->configs['cdo_value'],
'APP_PRD-MUL_QTY' => $this->l('you must have a quantity of multiple products of each products of : ').$this->configs['cdo_value'],
'APP_PRD-N_MUL_QTY' => $this->l('you must have a quantity of not multiple products of each products of : ').$this->configs['cdo_value'],
'APP_CAT-MIN_AM' => $this->l('you must have a amount minimum of each category of : ').Tools::displayPrice($this->configs['cdo_value']),
'APP_CAT-MAX_AM' => $this->l('you must have a amount maximum of each category of : ').Tools::displayPrice($this->configs['cdo_value']),
'APP_CAT-MIN_QTY' => $this->l('you must have a quantity minimum of each category of : ').$this->configs['cdo_value'],
'APP_CAT-MAX_QTY' => $this->l('you must have a quantity maximum of each category of : ').$this->configs['cdo_value'],
'APP_CAT-MUL_QTY' => $this->l('you must have a quantity of multiple quantity of each category of : ').$this->configs['cdo_value'],
'APP_CAT-N_MUL_QTY' => $this->l('you must have a quantity of not multiple quantity of each category of : ').$this->configs['cdo_value'],
);
}
public function cartControl($cart)
{
$configs = $this->configs;
$return = false;
if(!empty($configs['cdo_group'])){
$sql = 'SELECT 1 FROM `'._DB_PREFIX_.'customer_group` WHERE id_customer = '.(int)$cart->id_customer.' AND id_group IN ('.implode(',', $configs['cdo_group']).')';
if(!Db::getInstance()->getValue($sql)){
$return = false;
}
}
$products = $cart->getProducts(true);
$cdo_ids_concerne_off = $cdo_ids_concerne = array();
if(!empty($configs['cdo_ids_concerne'])){
$cdo_ids_concerne = explode(',', trim($configs['cdo_ids_concerne'], ' ,'));
}
if(!empty($configs['cdo_ids_concerne_off'])){
$cdo_ids_concerne_off = explode(',', trim($configs['cdo_ids_concerne_off'], ' ,'));
}
$controlTab = array();
foreach($products as &$product){
// inclusion
if(!empty($configs['cdo_applique_sur']) && ((!empty($cdo_ids_concerne) && !empty($cdo_ids_concerne[0])) && ($configs['cdo_applique_sur'] == 'EXP_PRD' && !in_array($product['id_product'], $cdo_ids_concerne)) || ($configs['cdo_applique_sur'] == 'EXP_CAT' && !in_array($product['id_category_default'], $cdo_ids_concerne)))){
unset($product);
continue;
}
// exlusion
if(!empty($configs['cdo_applique_sur_off']) && ((!empty($cdo_ids_concerne_off) && !empty($cdo_ids_concerne_off[0])) && ($configs['cdo_applique_sur_off'] == 'EXP_PRD_OFF' && in_array($product['id_product'], $cdo_ids_concerne_off)) || ($configs['cdo_applique_sur_off'] == 'EXP_CAT_OFF' && in_array($product['id_category_default'], $cdo_ids_concerne_off)))){
unset($product);
continue;
}
$this->init_var($controlTab['APP_CART']['QTY'][0], 0);
$this->init_var($controlTab['APP_CART']['AM'][1], 0);
$controlTab['APP_CART']['QTY'][0] += $product['cart_quantity'];
$controlTab['APP_CART']['AM'][1] += $product['total_wt'];
$this->init_var($controlTab['APP_CAT']['QTY'][$product['id_category_default']], 0);
$this->init_var($controlTab['APP_CAT']['AM'][$product['id_category_default']], 0);
$controlTab['APP_CAT']['QTY'][$product['id_category_default']] += $product['cart_quantity'];
$controlTab['APP_CAT']['AM'][$product['id_category_default']] += $product['total_wt'];
$this->init_var($controlTab['APP_PRD']['QTY'][$product['id_product']], 0);
$this->init_var($controlTab['APP_PRD']['AM'][$product['id_product']], 0);
$controlTab['APP_PRD']['QTY'][$product['id_product']] += $product['cart_quantity'];
$controlTab['APP_PRD']['AM'][$product['id_product']] += $product['total_wt'];
}
//control
$controlTab = array_filter($controlTab);
if(!empty($controlTab)){
$appli = $configs['cdo_applicable'];
$option = $configs['cdo_option_limite'];
$value = (float)trim($configs['cdo_value']);
if(in_array($option, array('MIN_AM', 'MAX_AM'))){
$dataTab = $controlTab[$appli]['AM'];
}else{
$dataTab = $controlTab[$appli]['QTY'];
}
if(in_array($option, array('MIN_AM', 'MIN_QTY'))){ // minimum
foreach($dataTab as $val){
if($val < $value){
$return = $appli.'-'.$option;
}
}
}elseif(in_array($option, array('MAX_AM', 'MAX_QTY'))){ // maximum
foreach($dataTab as $val){
if($val > $value){
$return = $appli.'-'.$option;
}
}
}elseif($option == 'MUL_QTY'){
foreach($dataTab as $val){
if($val%$value != 0){
$return = $appli.'-'.$option;
}
}
}elseif($option == 'N_MUL_QTY'){
foreach($dataTab as $val){
if($val%$value == 0){
$return = $appli.'-'.$option;
}
}
}
}
return $return;
}
public function hookDisplayShoppingCart($params)
{
if($this->configs['is_17']){
return $this->hookDisplayShoppingCartFooter($params);
}
}
public function hookDisplayShoppingCartFooter($params)
{
$err = $this->cartControl($params['cart']);
if(!empty($this->cartLimitError[$err])){
$this->context->smarty->assign(array(
'cdol_message' => $this->cartLimitError[$err],
'alert' => (bool)Tools::getValue('cdol'),
'less_than_17' => $this->configs['less_than_17']
));
return $this->display(__FILE__, 'cdorderlimit.tpl');
}
}
public function hookActionFrontControllerAfterInit($params)
{
if(Tools::getValue('controller') == 'order'){
$err = $this->cartControl($params['cart']);
if(!empty($err)){
Tools::redirect($this->context->link->getPageLink('cart', null, null, array('cdol' => 1, 'action' => 'show')));
}
}
/*if($controller == 'order'){
$this->errors[] = $this->trans(
'Vous ne pouvez pas passez de commande ce jour',
array(),
'Front.Controller'
);
$this->context->redirectWithNotifications($this->context->link->getPageLink('cart').'?action=show');
}*/
}
public function hookDisplayHeader($params)
{
if((Tools::getValue('step')) && (Tools::getValue('step') > 0)){
$err = $this->cartControl($params['cart']);
if(!empty($err)){
Tools::redirect($this->context->link->getPageLink((Configuration::get('PS_ORDER_PROCESS_TYPE') ? 'order-opc' : 'order'), null, null, array('cdol' => 1)));
}
}
parent::hookdisplayHeader($params);
}
/*public function hookActionBeforeCartUpdateQtyOld($param)
{
if((bool)Configuration::get('CDO_SUPPLIER_LIMIT')){ //supplier limit
$error = '';
$cartProducts = $param['cart']->getProducts(); //var_dump($cartProducts);
if(count($cartProducts) > 0){
if($cartProducts[0]['id_supplier'] != $param['product']->id_supplier){
$error .= $this->l('You can only add supplier Products : ').Supplier::getNameById($cartProducts[0]['id_supplier']);
$error .= $this->l('\n Or remove its products from the cart and start again');
}
}
if(!empty($error)){
echo '<script>alert("'.$error.'");</script>';
echo '<script>return ;</script>';
die();
}
}
return true;
}*/
}