static function getRemise(&$PDOdb, $type, $total, $zip = '', $fk_shipment_mode = 0, $fk_soc = 0)
 {
     global $db, $conf;
     if (!empty($conf->global->REMISE_USE_THIRDPARTY_DISCOUNT) && !empty($fk_soc)) {
         dol_include_once('/societe/class/societe.class.php');
         $s = new Societe($db);
         $s->fetch($fk_soc);
         return $s->array_options['options_remsup'];
     }
     $TRemise = TRemise::getAll($PDOdb, $type, true, !empty($zip), !empty($fk_shipment_mode));
     $remise_used = 0;
     $find = false;
     if (!empty($TRemise)) {
         foreach ($TRemise as &$remise) {
             if ($type === 'WEIGHT' && $total >= $remise['palier'] && ($remise['remise'] > $remise_used || empty($remise_used))) {
                 if (!empty($zip) && !empty($remise['zip']) && strpos($zip, $remise['zip']) === 0) {
                     $remise_used = $remise['remise'];
                     $find = true;
                     break;
                 } else {
                     if (empty($zip) && empty($remise['zip'])) {
                         // pas de remise associée au code poste trouvé avant
                         $find = true;
                         $remise_used = $remise['remise'];
                         break;
                     }
                 }
             } else {
                 if ($type === 'AMOUNT') {
                     if ($total >= $remise['palier'] && ($remise['remise'] > $remise_used || empty($remise_used))) {
                         $remise_used = $remise['remise'];
                         $find = true;
                     }
                 }
             }
         }
     }
     if (!$find && !empty($zip)) {
         return TRemise::getRemise($PDOdb, $type, $total);
     } else {
         return $remise_used;
     }
 }
 /**
  * Function called when a Dolibarrr business event is done.
  * All functions "run_trigger" are triggered if file
  * is inside directory core/triggers
  *
  * 	@param		string		$action		Event action code
  * 	@param		Object		$object		Object
  * 	@param		User		$user		Object user
  * 	@param		Translate	$langs		Object langs
  * 	@param		conf		$conf		Object conf
  * 	@return		int						<0 if KO, 0 if no triggered ran, >0 if OK
  */
 public function run_trigger($action, $object, $user, $langs, $conf)
 {
     // Put here code you want to execute when a Dolibarr business events occurs.
     // Data and type of action are stored into $object and $action
     // Users
     if ($action == 'ORDER_VALIDATE' || $action == 'PROPAL_VALIDATE' || $action == 'BILL_VALIDATE') {
         global $db, $conf;
         $langs->load('remise@remise');
         $object->fetch_optionals($object->id);
         /*echo "<pre>";
         		print_r($object);
         		echo "</pre>";*/
         dol_include_once('core/lib/admin.lib.php');
         define('INC_FROM_DOLIBARR', true);
         dol_include_once('/remise/config.php');
         dol_include_once('/remise/class/remise.class.php');
         $PDOdb = new TPDOdb();
         // On récupère les remises définis dans la configuration du module
         $remiseAlreadyInDoc = TRemise::alreadyAdded($object);
         $fk_product = $conf->global->REMISE_ID_SERVICE_TO_USE;
         if (!$remiseAlreadyInDoc && !empty($fk_product) && $object->array_options['options_use_remise'] === 'Oui') {
             dol_include_once('/product/class/product.class.php', 'Product');
             $total = TRemise::getTotal($object);
             $remise_used_montant = TRemise::getRemise($PDOdb, 'AMOUNT', $total, '', 0, $object->socid);
             $remise_used_weight = 0;
             if ($conf->global->REMISE_USE_WEIGHT) {
                 $total_weight = TRemise::getTotalWeight($object);
                 $remise_used_weight = TRemise::getRemise($PDOdb, 'WEIGHT', $total_weight, $object->client->zip);
             }
             $remise_used = max($remise_used_weight, $remise_used_montant);
             $p = new Product($db);
             $p->fetch($fk_product);
             $object->statut = 0;
             $object->brouillon = 1;
             $used_tva = $object->client->tva_assuj == 1 ? $p->tva_tx : 0;
             if (empty($conf->global->REMISE_USE_THIRDPARTY_DISCOUNT)) {
                 if ($object->element == 'commande' && (double) DOL_VERSION == 3.6) {
                     // Les paramètres sont tous dans le même ordre dans doulibarr 3.6
                     $object->addline("Remise de " . $remise_used . ' %', $total * $remise_used / 100 * -1, 1, $used_tva, 0, 0, $fk_product, 0, 0, 0, 'HT', 0, '', '', $p->type);
                 } else {
                     if ($object->element == 'propal') {
                         $object->addline("Remise de " . $remise_used . ' %', $total * $remise_used / 100 * -1, 1, $used_tva, 0, 0, $fk_product, 0, 'HT', 0, 0, $p->type);
                     } else {
                         if ($object->element == 'commande') {
                             $object->addline("Remise de " . $remise_used . ' %', $total * $remise_used / 100 * -1, 1, $used_tva, 0, 0, $fk_product);
                         }
                     }
                 }
             } else {
                 if (!empty($remise_used)) {
                     $object->addline("Remise de " . price($remise_used) . ' %', $total * $remise_used / 100 * -1, 1, $used_tva, 0, 0, $fk_product);
                 }
             }
             // setEventMessage($langs->trans('PortTaxAdded').' : '.price($remise_used).$conf->currency.' '.$langs->trans('VAT').' '.$used_tva.'%' );
             // Je vire cette partie, ça empêche le passage d'autres trigger sur la validation puisque du coup statut = 1...
             //$object->fetch($object->id);
             //$object->statut = 1; // TODO AA à quoi ça sert... Puisqu'il n'ya pas de save... :-|
         }
         dol_syslog("Trigger '" . $this->name . "' for action '{$action}' launched by " . __FILE__ . ". id=" . $object->id);
     }
     return 0;
 }
 /**
  * Function called when module is enabled.
  * The init function add constants, boxes, permissions and menus
  * (defined in constructor) into Dolibarr database.
  * It also creates data directories
  *
  * 	@param		string	$options	Options when enabling module ('', 'noboxes')
  * 	@return		int					1 if OK, 0 if KO
  */
 public function init($options = '')
 {
     global $db, $conf;
     $sql = array();
     $result = $this->loadTables();
     // Création extrafield pour choix si remise doit apparaitre sur doc.
     dol_include_once('/core/class/extrafields.class.php');
     //function addExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique=0, $required=0,$default_value='', $param=0)
     $ext = new ExtraFields($db);
     $res = $ext->addExtraField("use_remise", 'Automatisation des remises', 'select', 0, "", 'propal', 0, 0, '', array("options" => array("Oui" => "Oui", "Non" => "Non")));
     $res = $ext->addExtraField("use_remise", 'Automatisation des remises', 'select', 0, "", 'commande', 0, 0, '', array("options" => array("Oui" => "Oui", "Non" => "Non")));
     $res = $ext->addExtraField("use_remise", 'Automatisation des remises', 'select', 0, "", 'facture', 0, 0, '', array("options" => array("Oui" => "Oui", "Non" => "Non")));
     define('INC_FROM_DOLIBARR', true);
     dol_include_once('/remise/config.php');
     dol_include_once('/remise/class/remise.class.php');
     $PDOdb = new TPDOdb();
     $o = new TRemise();
     $o->init_db_by_vars($PDOdb);
     if (!empty($conf->global->REMISE_WEIGHT_ARRAY)) {
         $TRemise = unserialize($conf->global->REMISE_WEIGHT_ARRAY);
         foreach ($TRemise as $remise) {
             $o = new TRemise();
             $o->palier = $remise['weight'];
             $o->remise = $remise['remise'];
             $o->zip = $remise['zip'];
             $o->type = 'WEIGHT';
             $o->save($PDOdb);
         }
         dolibarr_del_const($db, 'REMISE_WEIGHT_ARRAY');
     }
     if (!empty($conf->global->REMISE_ARRAY)) {
         $TRemise = unserialize($conf->global->REMISE_ARRAY);
         foreach ($TRemise as $palier => $remise) {
             $o = new TRemise();
             $o->palier = $palier;
             $o->remise = $remise;
             $o->type = 'AMOUNT';
             $o->save($PDOdb);
         }
         dolibarr_del_const($db, 'REMISE_ARRAY');
     }
     return $this->_init($sql, $options);
 }
<?php

require '../config.php';
dol_include_once('/remise/lib/remise.lib.php');
dol_include_once('/remise/class/remise.class.php');
$langs->load("admin");
$langs->load("deliveries");
$type = GETPOST('type');
if (empty($type)) {
    $type = 'AMOUNT';
}
$action = GETPOST('action');
$remise = new TRemise();
$PDOdb = new TPDOdb();
switch ($action) {
    case 'save':
        if (GETPOST('bt_cancel') != '') {
            header('location:' . dol_buildpath('/remise/admin/remise.php?type=' . GETPOST('type'), 1));
        } else {
            $remise->load($PDOdb, GETPOST('id'));
            $remise->set_values($_POST);
            $remise->save($PDOdb);
            setEventMessage($langs->trans('RemiseSaved'));
            header('location:' . dol_buildpath('/remise/admin/remise.php?type=' . GETPOST('type') . '&TListTBS[lPrice][orderBy][date_maj]=DESC', 1));
        }
    case 'edit':
        $remise->load($PDOdb, GETPOST('id'));
        fiche($remise, $type, 'edit');
        break;
    case 'new':
        fiche($remise, $type, 'edit');
$etape = 1;
if ($action === 'import') {
    if (isset($_REQUEST['bt_preview']) && !empty($_FILES['f1']) && $_FILES['f1']['error'] == 0) {
        $etape = 2;
        $TData = array();
        $f1 = fopen($_FILES['f1']['tmp_name'], 'r') or die('Fichier illisible');
        while ($ligne = fgetcsv($f1, 4096, ';', '"')) {
            $TData[] = $ligne;
        }
    } else {
        if ($_REQUEST['bt_import'] && !empty($_REQUEST['data'])) {
            $TData = unserialize($_REQUEST['data']);
            $etape = 3;
            foreach ($TData as &$data) {
                $data['ok'] = 1;
                $o = new TRemise();
                $o->zip = str_pad($data[0], 2, '0', STR_PAD_LEFT);
                $o->type = $data[1] > 0 ? 'WEIGHT' : 'AMOUNT';
                $o->palier = $data[1] > 0 ? $data[1] : $data[2];
                $o->fdp = price2num($data[3], 5);
                $o->save($PDOdb);
            }
        }
    }
}
$page_name = "RemiseImport";
llxHeader('', $langs->trans($page_name));
// Subheader
$linkback = '<a href="' . DOL_URL_ROOT . '/admin/modules.php">' . $langs->trans("BackToModuleList") . '</a>';
print_fiche_titre($langs->trans($page_name), $linkback);
// Configuration header