/**
  * 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);
 }
$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');
        break;
    default:
        liste($type);
        break;
}
function fiche(&$remise, $type, $mode)
        $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
$head = remiseAdminPrepareHead();
dol_fiche_head($head, 'import', $langs->trans("Module104921Name"), 0, "remise@remise");
$form = new TFormCore('auto', 'formImport', 'post', true);
echo $form->hidden('action', 'import');
print_titre('Etape 1');