diff --git a/htdocs/margin/checkMargins.php b/htdocs/margin/checkMargins.php new file mode 100644 index 00000000000..ac53d079298 --- /dev/null +++ b/htdocs/margin/checkMargins.php @@ -0,0 +1,254 @@ + + * Copyright (C) 2014 Ferran Marcet + * Copyright (C) 2015 Marcos García + * Copyright (C) 2016 Florian Henry + * + * 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 . + */ + +/** + * \file htdocs/margin/checkMargins.php + * \ingroup margin + * \brief Check margins + */ +require '../main.inc.php'; +require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php'; +require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php'; +require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php'; +require_once DOL_DOCUMENT_ROOT . '/margin/lib/margins.lib.php'; + +$langs->load("companies"); +$langs->load("bills"); +$langs->load("products"); +$langs->load("margins"); + +// Security check + +if ($user->rights->margins->creer) { + $agentid = $user->id; +} else { + accessforbidden(); +} + +$sortfield = GETPOST("sortfield", 'alpha'); +$sortorder = GETPOST("sortorder", 'alpha'); +if (! $sortorder) + $sortorder = "DESC"; +if (! $sortfield) { + $sortfield = 'f.rowid'; +} +$page = GETPOST("page", 'int'); +if ($page == - 1) { + $page = 0; +} +$offset = $conf->liste_limit * $page; +$pageprev = $page - 1; +$pagenext = $page + 1; + +// Both test are required to be compatible with all browsers +if (GETPOST("button_search_x") || GETPOST("button_search")) { + $action = 'search'; +} elseif (GETPOST("button_updatemagins_x") || GETPOST("button_updatemagins")) { + $action = 'update'; +} + +if ($action == 'update') { + $datapost = $_POST; + + foreach ( $datapost as $key => $value ) { + if (strpos($key, 'buyingprice_') !== false) { + $tmp_array = explode('_', $key); + if (count($tmp_array) > 0) { + $invoicedet_id = $tmp_array[1]; + if (! empty($invoicedet_id)) { + $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facturedet'; + $sql .= ' SET buy_price_ht=\'' . price2num($value) . '\''; + $sql .= ' WHERE rowid=' . $invoicedet_id; + $result = $db->query($sql); + if (!$result) { + setEventMessages($db->lasterror, null, 'errors'); + } + } + } + } + } +} + +$startdate = $enddate = ''; + +$startdate = dol_mktime(0, 0, 0, GETPOST('startdatemonth', 'int'), GETPOST('startdateday', 'int'), GETPOST('startdateyear', 'int')); +$enddate = dol_mktime(23, 59, 59, GETPOST('enddatemonth', 'int'), GETPOST('enddateday', 'int'), GETPOST('enddateyear', 'int')); + +if (! empty($startdate)) { + $options .= '&startdatemonth=' . GETPOST('startdatemonth', 'int') . '&startdateday=' . GETPOST('startdateday', 'int') . '&startdateyear=' . GETPOST('startdateyear', 'int'); +} +if (! empty($enddate)) { + $options .= '&enddatemonth=' . GETPOST('enddatemonth', 'int') . '&enddateday=' . GETPOST('enddateday', 'int') . '&enddateyear=' . GETPOST('enddateyear', 'int'); +} + +/* + * View + */ + +$userstatic = new User($db); +$companystatic = new Societe($db); +$invoicestatic = new Facture($db); +$productstatic = new Product($db); + +$form = new Form($db); + +$title = $langs->trans("CheckMargins"); + +llxHeader('', $title); + +// print_fiche_titre($text); + +// Show tabs +$head = marges_prepare_head($user); +$picto = 'margin'; +dol_fiche_head($head, 'checkMargins', $title, 0, $picto); + +print '
'; +print ''; + +// Start date +print ''; +print ''; +print ''; +print ''; +print ''; +print "
' . $langs->trans('StartDate') . ' (' . $langs->trans("DateValidation") . ')'; +$form->select_date($startdate, 'startdate', '', '', 1, "sel", 1, 1); +print '' . $langs->trans('EndDate') . ' (' . $langs->trans("DateValidation") . ')'; +$form->select_date($enddate, 'enddate', '', '', 1, "sel", 1, 1); +print ''; +print ''; +print '
"; + +$sql = "SELECT"; +$sql .= " f.facnumber, f.rowid as invoiceid, d.rowid as invoicedetid, d.buy_price_ht, d.total_ht, d.subprice, d.label, d.description , d.qty"; +$sql .= " ,d.fk_product"; +$sql .= " FROM " . MAIN_DB_PREFIX . "facture as f "; +$sql .= " INNER JOIN " . MAIN_DB_PREFIX . "facturedet as d ON d.fk_facture = f.rowid"; +$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON d.fk_product = p.rowid"; +$sql .= " WHERE f.fk_statut > 0"; +$sql .= " AND f.entity = " . getEntity('facture', 1); +if (! empty($startdate)) + $sql .= " AND f.datef >= '" . $db->idate($startdate) . "'"; +if (! empty($enddate)) + $sql .= " AND f.datef <= '" . $db->idate($enddate) . "'"; +$sql .= " AND d.buy_price_ht IS NOT NULL"; +$sql .= $db->order($sortfield, $sortorder); + +$nbtotalofrecords = 0; +if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) { + + dol_syslog(__FILE__, LOG_DEBUG); + $result = $db->query($sql); + if ($result) { + $nbtotalofrecords = $db->num_rows($result); + } else { + setEventMessages($db->lasterror, null, 'errors'); + } +} + +$sql .= $db->plimit($conf->liste_limit + 1, $offset); + +dol_syslog(__FILE__, LOG_DEBUG); +$result = $db->query($sql); +if ($result) { + $num = $db->num_rows($result); + + print '
'; + print_barre_liste($langs->trans("MarginDetails"), $page, $_SERVER["PHP_SELF"], "", $sortfield, $sortorder, '', $num, $nbtotalofrecords, ''); + + print ""; + + print ''; + + print_liste_field_titre($langs->trans("Ref"), $_SERVER["PHP_SELF"], "f.ref", "", $options, '', $sortfield, $sortorder); + print_liste_field_titre($langs->trans("Description"), $_SERVER["PHP_SELF"], "", "", $options, 'width=20%', $sortfield, $sortorder); + print_liste_field_titre($langs->trans("UnitPriceHT"), $_SERVER["PHP_SELF"], "d.subprice", "", $options, 'align="right"', $sortfield, $sortorder); + if ($conf->global->MARGIN_TYPE == "1") { + print_liste_field_titre($langs->trans("BuyingPrice"), $_SERVER["PHP_SELF"], "d.buy_price_ht", "", $options, 'align="right"', $sortfield, $sortorder); + } else { + print_liste_field_titre($langs->trans("CostPrice"), $_SERVER["PHP_SELF"], "d.buy_price_ht", "", $options, 'align="right"', $sortfield, $sortorder); + } + print_liste_field_titre($langs->trans("Qty"), $_SERVER["PHP_SELF"], "d.qty", "", $options, 'align="right"', $sortfield, $sortorder); + print_liste_field_titre($langs->trans("AmountTTC"), $_SERVER["PHP_SELF"], "d.total_ht", "", $options, 'align="right"', $sortfield, $sortorder); + + print "\n"; + + if ($num > 0) { + $var = true; + + while ( $objp = $db->fetch_object($result) ) { + $var = ! $var; + + print ""; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print "\n"; + + $i ++; + } + } + print "
'; + $result_inner = $invoicestatic->fetch($objp->invoiceid); + if ($result_inner < 0) { + setEventMessages($invoicestatic->error, null, 'errors'); + } else { + print $invoicestatic->getNomUrl(1); + } + print ''; + if (! empty($objp->fk_product)) { + $result_inner = $productstatic->fetch($objp->fk_product); + if ($result_inner < 0) { + setEventMessages($productstatic->error, null, 'errors'); + } else { + print $productstatic->getNomUrl(1); + } + } else { + print $objp->label; + print ' '; + print $objp->description; + } + print ''; + print price($objp->subprice); + print ''; + print ''; + print ''; + print $objp->qty; + print ''; + print price($objp->total_ht); + print '
"; +} else { + dol_print_error($db); +} + +print '
' . "\n"; +print '
'; +print '
'; +print '
'; + +$db->free($result); + +llxFooter(); +$db->close(); \ No newline at end of file diff --git a/htdocs/margin/lib/margins.lib.php b/htdocs/margin/lib/margins.lib.php index ce100d49390..2a3d0775eaa 100644 --- a/htdocs/margin/lib/margins.lib.php +++ b/htdocs/margin/lib/margins.lib.php @@ -1,6 +1,7 @@ * Copyright (C) 2014-2015 Marcos García + * Copyright (C) 2016 Florian Henry * * 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 @@ -87,6 +88,14 @@ function marges_prepare_head() $head[$h][1] = $langs->trans($title); $head[$h][2] = 'agentMargins'; + + if ($user->rights->margins->creer) { + $h++; + $head[$h][0] = DOL_URL_ROOT."/margin/checkMargins.php"; + $head[$h][1] = $langs->trans('CheckMargins'); + $head[$h][2] = 'checkMargins'; + } + return $head; }