dolibarr 21.0.0-beta
checkMargins.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2012-2013 Christophe Battarel <christophe.battarel@altairis.fr>
3 * Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es>
4 * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
5 * Copyright (C) 2016 Florian Henry <florian.henry@open-concept.pro>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
31require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
32require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
33
42// Load translation files required by the page
43$langs->loadLangs(array('companies', 'bills', 'products', 'margins'));
44
45$action = GETPOST('action', 'alpha');
46$massaction = GETPOST('massaction', 'alpha');
47$toselect = GETPOST('toselect', 'array');
48$contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'margindetail'; // To manage different context of search
49$backtopage = GETPOST('backtopage', 'alpha');
50$optioncss = GETPOST('optioncss', 'alpha');
51
52// Load variable for pagination
53$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
54$sortfield = GETPOST('sortfield', 'aZ09comma');
55$sortorder = GETPOST('sortorder', 'aZ09comma');
56$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
57if (empty($page) || $page == -1) {
58 $page = 0;
59} // If $page is not defined, or '' or -1
60$offset = $limit * $page;
61$pageprev = $page - 1;
62$pagenext = $page + 1;
63if (!$sortorder) {
64 $sortorder = "DESC";
65}
66if (!$sortfield) {
67 $sortfield = 'f.ref';
68}
69
70$startdate = $enddate = '';
71
72$startdate = dol_mktime(0, 0, 0, GETPOSTINT('startdatemonth'), GETPOSTINT('startdateday'), GETPOSTINT('startdateyear'));
73$enddate = dol_mktime(23, 59, 59, GETPOSTINT('enddatemonth'), GETPOSTINT('enddateday'), GETPOSTINT('enddateyear'));
74
75$search_ref = GETPOST('search_ref', 'alpha');
76
77$hookmanager->initHooks(array('checkmarginlist'));
78
79// Security check
80$result = restrictedArea($user, 'margins');
81
82// Both test are required to be compatible with all browsers
83if (GETPOST("button_search_x") || GETPOST("button_search")) {
84 $action = 'search';
85} elseif (GETPOST("button_updatemagins_x") || GETPOST("button_updatemagins")) {
86 $action = 'update';
87}
88
89$permissiontocreate = $user->hasRight('facture', 'creer');
90
91
92/*
93 * Actions
94 */
95
96if (GETPOST('cancel', 'alpha')) {
97 $action = 'list';
98 $massaction = '';
99}
100if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
101 $massaction = '';
102}
103
104$parameters = array();
105$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
106if ($reshook < 0) {
107 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
108}
109
110if (empty($reshook)) {
111 // Selection of new fields
112 include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
113
114 if ($action == 'update' && $permissiontocreate) {
115 $datapost = $_POST;
116
117 foreach ($datapost as $key => $value) {
118 if (strpos($key, 'buyingprice_') !== false) {
119 $tmp_array = explode('_', $key);
120 if (count($tmp_array) > 0) {
121 $invoicedet_id = $tmp_array[1];
122 if (!empty($invoicedet_id)) {
123 $sql = 'UPDATE '.MAIN_DB_PREFIX.'facturedet';
124 $sql .= " SET buy_price_ht = ".((float) price2num($value));
125 $sql .= ' WHERE rowid = '.((int) $invoicedet_id);
126 $result = $db->query($sql);
127 if (!$result) {
128 setEventMessages($db->lasterror, null, 'errors');
129 }
130 }
131 }
132 }
133 }
134 }
135
136 // Purge search criteria
137 if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
138 $search_ref = '';
139 $search_array_options = array();
140 }
141
142 // Mass actions
143 /*
144 $objectclass='Product';
145 if ((string) $type == '1') { $objectlabel='Services'; }
146 if ((string) $type == '0') { $objectlabel='Products'; }
147
148 $permissiontoread = $user->rights->produit->lire;
149 $permissiontodelete = $user->rights->produit->supprimer;
150 $uploaddir = $conf->product->dir_output;
151 include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
152 */
153}
154
155
156/*
157 * View
158 */
159
160$userstatic = new User($db);
161$companystatic = new Societe($db);
162$invoicestatic = new Facture($db);
163$productstatic = new Product($db);
164
165$form = new Form($db);
166
167$title = $langs->trans("MarginDetails");
168
169llxHeader('', $title, '', '', 0, 0, '', '', '', 'mod-margin page-checkmargins');
170
171// print load_fiche_titre($text);
172
173$param = '';
174if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
175 $param .= '&contextpage='.$contextpage;
176}
177if ($limit > 0 && $limit != $conf->liste_limit) {
178 $param .= '&limit='.$limit;
179}
180if ($search_ref != '') {
181 $param .= '&search_ref='.urlencode($search_ref);
182}
183if (!empty($startdate)) {
184 $param .= '&startdatemonth='.GETPOSTINT('startdatemonth').'&startdateday='.GETPOSTINT('startdateday').'&startdateyear='.GETPOSTINT('startdateyear');
185}
186if (!empty($enddate)) {
187 $param .= '&enddatemonth='.GETPOSTINT('enddatemonth').'&enddateday='.GETPOSTINT('enddateday').'&enddateyear='.GETPOSTINT('enddateyear');
188}
189if ($optioncss != '') {
190 $param .= '&optioncss='.$optioncss;
191}
192
193// Show tabs
194$head = marges_prepare_head();
195
196$picto = 'margin';
197
198print '<form method="post" name="sel" action="'.$_SERVER['PHP_SELF'].'">';
199print '<input type="hidden" name="token" value="'.newToken().'">';
200
201print dol_get_fiche_head($head, $langs->trans('checkMargins'), $title, 0, $picto);
202
203print '<table class="border centpercent">';
204
205print '<tr><td class="titlefield">'.$langs->trans('DateStart').' ('.$langs->trans("DateValidation").')</td>';
206print '<td>';
207print $form->selectDate($startdate, 'startdate', 0, 0, 1, "sel", 1, 1);
208print '</td>';
209print '<td>'.$langs->trans('DateEnd').' ('.$langs->trans("DateValidation").')</td>';
210print '<td>';
211print $form->selectDate($enddate, 'enddate', 0, 0, 1, "sel", 1, 1);
212print '</td>';
213print '<td style="text-align: center;">';
214print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans('Refresh')).'" name="button_search" />';
215print '</td>';
216print '</tr>';
217print "</table>";
218
219print dol_get_fiche_end();
220
221
222$arrayfields = array();
223$massactionbutton = '';
224
225$invoice_status_except_list = array(Facture::STATUS_DRAFT, Facture::STATUS_ABANDONED);
226
227$sql = "SELECT";
228$sql .= " f.ref, f.rowid as invoiceid,";
229$sql .= " d.rowid as invoicedetid, d.product_type, d.buy_price_ht, d.total_ht, d.subprice, d.label, d.description, d.qty, d.fk_product";
230$sql .= " FROM ".MAIN_DB_PREFIX."facture as f ";
231$sql .= " INNER JOIN ".MAIN_DB_PREFIX."facturedet as d ON d.fk_facture = f.rowid";
232$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON d.fk_product = p.rowid";
233$sql .= " WHERE f.fk_statut NOT IN (".$db->sanitize(implode(', ', $invoice_status_except_list)).")";
234$sql .= " AND f.entity IN (".getEntity('invoice').") ";
235if (!empty($startdate)) {
236 $sql .= " AND f.datef >= '".$db->idate($startdate)."'";
237}
238if (!empty($enddate)) {
239 $sql .= " AND f.datef <= '".$db->idate($enddate)."'";
240}
241if ($search_ref) {
242 $sql .= natural_search('f.ref', $search_ref);
243}
244$sql .= " AND d.buy_price_ht IS NOT NULL";
245$sql .= $db->order($sortfield, $sortorder);
246
247$nbtotalofrecords = '';
248if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
249 dol_syslog(__FILE__, LOG_DEBUG);
250 $result = $db->query($sql);
251 $nbtotalofrecords = $db->num_rows($result);
252 if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
253 $page = 0;
254 $offset = 0;
255 }
256}
257
258$sql .= $db->plimit($limit + 1, $offset);
259
260$result = $db->query($sql);
261if ($result) {
262 $num = $db->num_rows($result);
263
264 print '<br>';
265 // @phan-suppress-next-line PhanPluginSuspiciousParamOrder
266 print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, '', 0, '', '', $limit, 0, 0, 1);
267
268 if (getDolGlobalString('MARGIN_TYPE') == "1") {
269 $labelcostprice = 'BuyingPrice';
270 } else { // value is 'costprice' or 'pmp'
271 $labelcostprice = 'CostPrice';
272 }
273
274 $moreforfilter = '';
275
276 $varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
277 //$selectedfields=$form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
278 //if ($massactionbutton) $selectedfields.=$form->showCheckAddButtons('checkforselect', 1);
279 $selectedfields = '';
280
281 print '<div class="div-table-responsive">';
282 print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
283
284 print '<tr class="liste_titre liste_titre_search">';
285 print '<td><input type="text" name="search_ref" value="'.dol_escape_htmltag($search_ref).'"></td>';
286 print '<td></td>';
287 print '<td></td>';
288 print '<td></td>';
289 print '<td></td>';
290 print '<td></td>';
291 print '<td class="liste_titre" align="middle">';
292 $searchpitco = $form->showFilterButtons();
293 print $searchpitco;
294 print '</td>';
295 print "</tr>\n";
296
297 print '<tr class="liste_titre">';
298 print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "f.ref", "", $param, '', $sortfield, $sortorder);
299 print_liste_field_titre("Description", $_SERVER["PHP_SELF"], "", "", $param, '', $sortfield, $sortorder);
300 print_liste_field_titre("UnitPriceHT", $_SERVER["PHP_SELF"], "d.subprice", "", $param, '', $sortfield, $sortorder, 'right ');
301 print_liste_field_titre($labelcostprice, $_SERVER["PHP_SELF"], "d.buy_price_ht", "", $param, '', $sortfield, $sortorder, 'right ');
302 print_liste_field_titre("Qty", $_SERVER["PHP_SELF"], "d.qty", "", $param, '', $sortfield, $sortorder, 'right ');
303 print_liste_field_titre("AmountTTC", $_SERVER["PHP_SELF"], "d.total_ht", "", $param, '', $sortfield, $sortorder, 'right ');
304 print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', $param, '', $sortfield, $sortorder, 'maxwidthsearch center ');
305 print "</tr>\n";
306
307 $i = 0;
308 while ($i < min($num, $limit)) {
309 $objp = $db->fetch_object($result);
310
311 print '<tr class="oddeven">';
312 print '<td>';
313 $result_inner = $invoicestatic->fetch($objp->invoiceid);
314 if ($result_inner < 0) {
315 setEventMessages($invoicestatic->error, null, 'errors');
316 } else {
317 print $invoicestatic->getNomUrl(1);
318 }
319 print '</td>';
320 print '<td>';
321 if (!empty($objp->fk_product)) {
322 $result_inner = $productstatic->fetch($objp->fk_product);
323 if ($result_inner < 0) {
324 setEventMessages($productstatic->error, null, 'errors');
325 } else {
326 print $productstatic->getNomUrl(1);
327 }
328 } else {
329 if ($objp->product_type == $productstatic::TYPE_PRODUCT) {
330 print img_picto('', 'product');
331 }
332 if ($objp->product_type == $productstatic::TYPE_SERVICE) {
333 print img_picto('', 'service');
334 }
335 print $objp->label;
336 print '&nbsp;';
337 print $objp->description;
338 }
339 print '</td>';
340 print '<td class="right">';
341 print price($objp->subprice);
342 print '</td>';
343 print '<td class="right">';
344 print '<input type="text" name="buyingprice_'.$objp->invoicedetid.'" id="buyingprice_'.$objp->invoicedetid.'" size="6" value="'.price($objp->buy_price_ht).'" class="right flat">';
345 print '</td>';
346 print '<td class="right">';
347 print $objp->qty;
348 print '</td>';
349 print '<td class="right">';
350 print '<span class="amount">'.price($objp->total_ht).'</span>';
351 print '</td>';
352 print '<td></td>';
353
354 print "</tr>\n";
355
356 $i++;
357 }
358
359 print "</table>";
360
361 print "</div>";
362} else {
363 dol_print_error($db);
364}
365
366
367print '<div class="center">'."\n";
368print '<input type="submit" class="button" name="button_updatemagins" id="button_updatemagins" value="'.$langs->trans("Update").'">';
369print '</div>';
370
371print '</form>';
372
373$db->free($result);
374
375// End of page
376llxFooter();
377$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:71
Class to manage invoices.
const STATUS_DRAFT
Draft status.
const STATUS_ABANDONED
Classified abandoned and no payment done.
Class to manage generation of HTML components Only common components must be here.
Class to manage products or services.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
llxFooter()
Footer empty.
Definition document.php:107
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
print_barre_liste($title, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $selectlimitsuffix=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
natural_search($fields, $value, $mode=0, $nofirstand=0)
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
marges_prepare_head()
Return array of tabs to used on pages for third parties cards.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.