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