dolibarr 20.0.0
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
30require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
31require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array('companies', 'bills', 'products', 'margins'));
35
36$action = GETPOST('action', 'alpha');
37$massaction = GETPOST('massaction', 'alpha');
38$toselect = GETPOST('toselect', 'array');
39$contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'margindetail'; // To manage different context of search
40$backtopage = GETPOST('backtopage', 'alpha');
41$optioncss = GETPOST('optioncss', 'alpha');
42
43// Load variable for pagination
44$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
45$sortfield = GETPOST('sortfield', 'aZ09comma');
46$sortorder = GETPOST('sortorder', 'aZ09comma');
47$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
48if (empty($page) || $page == -1) {
49 $page = 0;
50} // If $page is not defined, or '' or -1
51$offset = $limit * $page;
52$pageprev = $page - 1;
53$pagenext = $page + 1;
54if (!$sortorder) {
55 $sortorder = "DESC";
56}
57if (!$sortfield) {
58 $sortfield = 'f.ref';
59}
60
61$startdate = $enddate = '';
62
63$startdate = dol_mktime(0, 0, 0, GETPOSTINT('startdatemonth'), GETPOSTINT('startdateday'), GETPOSTINT('startdateyear'));
64$enddate = dol_mktime(23, 59, 59, GETPOSTINT('enddatemonth'), GETPOSTINT('enddateday'), GETPOSTINT('enddateyear'));
65
66$search_ref = GETPOST('search_ref', 'alpha');
67
68$hookmanager->initHooks(array('checkmarginlist'));
69
70// Security check
71$result = restrictedArea($user, 'margins');
72
73// Both test are required to be compatible with all browsers
74if (GETPOST("button_search_x") || GETPOST("button_search")) {
75 $action = 'search';
76} elseif (GETPOST("button_updatemagins_x") || GETPOST("button_updatemagins")) {
77 $action = 'update';
78}
79
80$permissiontocreate = $user->hasRight('facture', 'creer');
81
82
83/*
84 * Actions
85 */
86
87if (GETPOST('cancel', 'alpha')) {
88 $action = 'list';
89 $massaction = '';
90}
91if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
92 $massaction = '';
93}
94
95$parameters = array();
96$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
97if ($reshook < 0) {
98 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
99}
100
101if (empty($reshook)) {
102 // Selection of new fields
103 include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
104
105 if ($action == 'update' && $permissiontocreate) {
106 $datapost = $_POST;
107
108 foreach ($datapost as $key => $value) {
109 if (strpos($key, 'buyingprice_') !== false) {
110 $tmp_array = explode('_', $key);
111 if (count($tmp_array) > 0) {
112 $invoicedet_id = $tmp_array[1];
113 if (!empty($invoicedet_id)) {
114 $sql = 'UPDATE '.MAIN_DB_PREFIX.'facturedet';
115 $sql .= " SET buy_price_ht = ".((float) price2num($value));
116 $sql .= ' WHERE rowid = '.((int) $invoicedet_id);
117 $result = $db->query($sql);
118 if (!$result) {
119 setEventMessages($db->lasterror, null, 'errors');
120 }
121 }
122 }
123 }
124 }
125 }
126
127 // Purge search criteria
128 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
129 $search_ref = '';
130 $search_array_options = array();
131 }
132
133 // Mass actions
134 /*
135 $objectclass='Product';
136 if ((string) $type == '1') { $objectlabel='Services'; }
137 if ((string) $type == '0') { $objectlabel='Products'; }
138
139 $permissiontoread = $user->rights->produit->lire;
140 $permissiontodelete = $user->rights->produit->supprimer;
141 $uploaddir = $conf->product->dir_output;
142 include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
143 */
144}
145
146
147/*
148 * View
149 */
150
151$userstatic = new User($db);
152$companystatic = new Societe($db);
153$invoicestatic = new Facture($db);
154$productstatic = new Product($db);
155
156$form = new Form($db);
157
158$title = $langs->trans("MarginDetails");
159
160llxHeader('', $title);
161
162// print load_fiche_titre($text);
163
164$param = '';
165if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
166 $param .= '&contextpage='.$contextpage;
167}
168if ($limit > 0 && $limit != $conf->liste_limit) {
169 $param .= '&limit='.$limit;
170}
171if ($search_ref != '') {
172 $param .= '&search_ref='.urlencode($search_ref);
173}
174if (!empty($startdate)) {
175 $param .= '&startdatemonth='.GETPOSTINT('startdatemonth').'&startdateday='.GETPOSTINT('startdateday').'&startdateyear='.GETPOSTINT('startdateyear');
176}
177if (!empty($enddate)) {
178 $param .= '&enddatemonth='.GETPOSTINT('enddatemonth').'&enddateday='.GETPOSTINT('enddateday').'&enddateyear='.GETPOSTINT('enddateyear');
179}
180if ($optioncss != '') {
181 $param .= '&optioncss='.$optioncss;
182}
183
184// Show tabs
185$head = marges_prepare_head();
186
187$picto = 'margin';
188
189print '<form method="post" name="sel" action="'.$_SERVER['PHP_SELF'].'">';
190print '<input type="hidden" name="token" value="'.newToken().'">';
191
192print dol_get_fiche_head($head, $langs->trans('checkMargins'), $title, 0, $picto);
193
194print '<table class="border centpercent">';
195
196print '<tr><td class="titlefield">'.$langs->trans('DateStart').' ('.$langs->trans("DateValidation").')</td>';
197print '<td>';
198print $form->selectDate($startdate, 'startdate', 0, 0, 1, "sel", 1, 1);
199print '</td>';
200print '<td>'.$langs->trans('DateEnd').' ('.$langs->trans("DateValidation").')</td>';
201print '<td>';
202print $form->selectDate($enddate, 'enddate', 0, 0, 1, "sel", 1, 1);
203print '</td>';
204print '<td style="text-align: center;">';
205print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans('Refresh')).'" name="button_search" />';
206print '</td>';
207print '</tr>';
208print "</table>";
209
210print dol_get_fiche_end();
211
212
213$arrayfields = array();
214$massactionbutton = '';
215
216$invoice_status_except_list = array(Facture::STATUS_DRAFT, Facture::STATUS_ABANDONED);
217
218$sql = "SELECT";
219$sql .= " f.ref, f.rowid as invoiceid,";
220$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";
221$sql .= " FROM ".MAIN_DB_PREFIX."facture as f ";
222$sql .= " INNER JOIN ".MAIN_DB_PREFIX."facturedet as d ON d.fk_facture = f.rowid";
223$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON d.fk_product = p.rowid";
224$sql .= " WHERE f.fk_statut NOT IN (".$db->sanitize(implode(', ', $invoice_status_except_list)).")";
225$sql .= " AND f.entity IN (".getEntity('invoice').") ";
226if (!empty($startdate)) {
227 $sql .= " AND f.datef >= '".$db->idate($startdate)."'";
228}
229if (!empty($enddate)) {
230 $sql .= " AND f.datef <= '".$db->idate($enddate)."'";
231}
232if ($search_ref) {
233 $sql .= natural_search('f.ref', $search_ref);
234}
235$sql .= " AND d.buy_price_ht IS NOT NULL";
236$sql .= $db->order($sortfield, $sortorder);
237
238$nbtotalofrecords = '';
239if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
240 dol_syslog(__FILE__, LOG_DEBUG);
241 $result = $db->query($sql);
242 $nbtotalofrecords = $db->num_rows($result);
243 if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
244 $page = 0;
245 $offset = 0;
246 }
247}
248
249$sql .= $db->plimit($limit + 1, $offset);
250
251$result = $db->query($sql);
252if ($result) {
253 $num = $db->num_rows($result);
254
255 print '<br>';
256 // @phan-suppress-next-line PhanPluginSuspiciousParamOrder
257 print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, '', 0, '', '', $limit, 0, 0, 1);
258
259 if (getDolGlobalString('MARGIN_TYPE') == "1") {
260 $labelcostprice = 'BuyingPrice';
261 } else { // value is 'costprice' or 'pmp'
262 $labelcostprice = 'CostPrice';
263 }
264
265 $moreforfilter = '';
266
267 $varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
268 //$selectedfields=$form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
269 //if ($massactionbutton) $selectedfields.=$form->showCheckAddButtons('checkforselect', 1);
270 $selectedfields = '';
271
272 print '<div class="div-table-responsive">';
273 print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
274
275 print '<tr class="liste_titre liste_titre_search">';
276 print '<td><input type="text" name="search_ref" value="'.dol_escape_htmltag($search_ref).'"></td>';
277 print '<td></td>';
278 print '<td></td>';
279 print '<td></td>';
280 print '<td></td>';
281 print '<td></td>';
282 print '<td class="liste_titre" align="middle">';
283 $searchpitco = $form->showFilterButtons();
284 print $searchpitco;
285 print '</td>';
286 print "</tr>\n";
287
288 print '<tr class="liste_titre">';
289 print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "f.ref", "", $param, '', $sortfield, $sortorder);
290 print_liste_field_titre("Description", $_SERVER["PHP_SELF"], "", "", $param, '', $sortfield, $sortorder);
291 print_liste_field_titre("UnitPriceHT", $_SERVER["PHP_SELF"], "d.subprice", "", $param, '', $sortfield, $sortorder, 'right ');
292 print_liste_field_titre($labelcostprice, $_SERVER["PHP_SELF"], "d.buy_price_ht", "", $param, '', $sortfield, $sortorder, 'right ');
293 print_liste_field_titre("Qty", $_SERVER["PHP_SELF"], "d.qty", "", $param, '', $sortfield, $sortorder, 'right ');
294 print_liste_field_titre("AmountTTC", $_SERVER["PHP_SELF"], "d.total_ht", "", $param, '', $sortfield, $sortorder, 'right ');
295 print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', $param, '', $sortfield, $sortorder, 'maxwidthsearch center ');
296 print "</tr>\n";
297
298 $i = 0;
299 while ($i < min($num, $limit)) {
300 $objp = $db->fetch_object($result);
301
302 print '<tr class="oddeven">';
303 print '<td>';
304 $result_inner = $invoicestatic->fetch($objp->invoiceid);
305 if ($result_inner < 0) {
306 setEventMessages($invoicestatic->error, null, 'errors');
307 } else {
308 print $invoicestatic->getNomUrl(1);
309 }
310 print '</td>';
311 print '<td>';
312 if (!empty($objp->fk_product)) {
313 $result_inner = $productstatic->fetch($objp->fk_product);
314 if ($result_inner < 0) {
315 setEventMessages($productstatic->error, null, 'errors');
316 } else {
317 print $productstatic->getNomUrl(1);
318 }
319 } else {
320 if ($objp->product_type == $productstatic::TYPE_PRODUCT) {
321 print img_picto('', 'product');
322 }
323 if ($objp->product_type == $productstatic::TYPE_SERVICE) {
324 print img_picto('', 'service');
325 }
326 print $objp->label;
327 print '&nbsp;';
328 print $objp->description;
329 }
330 print '</td>';
331 print '<td class="right">';
332 print price($objp->subprice);
333 print '</td>';
334 print '<td class="right">';
335 print '<input type="text" name="buyingprice_'.$objp->invoicedetid.'" id="buyingprice_'.$objp->invoicedetid.'" size="6" value="'.price($objp->buy_price_ht).'" class="right flat">';
336 print '</td>';
337 print '<td class="right">';
338 print $objp->qty;
339 print '</td>';
340 print '<td class="right">';
341 print '<span class="amount">'.price($objp->total_ht).'</span>';
342 print '</td>';
343 print '<td></td>';
344
345 print "</tr>\n";
346
347 $i++;
348 }
349
350 print "</table>";
351
352 print "</div>";
353} else {
354 dol_print_error($db);
355}
356
357
358print '<div class="center">'."\n";
359print '<input type="submit" class="button" name="button_updatemagins" id="button_updatemagins" value="'.$langs->trans("Update").'">';
360print '</div>';
361
362print '</form>';
363
364$db->free($result);
365
366// End of page
367llxFooter();
368$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
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.
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...
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.
print_barre_liste($title, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $hideselectlimit=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
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 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.
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.