dolibarr  13.0.2
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 
26 require '../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
30 require_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');
47 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
48 $offset = $limit * $page;
49 $pageprev = $page - 1;
50 $pagenext = $page + 1;
51 if (!$sortorder) $sortorder = "DESC";
52 if (!$sortfield) $sortfield = 'f.ref';
53 
54 $startdate = $enddate = '';
55 
56 $startdate = dol_mktime(0, 0, 0, GETPOST('startdatemonth', 'int'), GETPOST('startdateday', 'int'), GETPOST('startdateyear', 'int'));
57 $enddate = dol_mktime(23, 59, 59, GETPOST('enddatemonth', 'int'), GETPOST('enddateday', 'int'), GETPOST('enddateyear', 'int'));
58 
59 $search_ref = GETPOST('search_ref', 'alpha');
60 
61 // Security check
62 $result = restrictedArea($user, 'margins');
63 
64 // Both test are required to be compatible with all browsers
65 if (GETPOST("button_search_x") || GETPOST("button_search")) {
66  $action = 'search';
67 } elseif (GETPOST("button_updatemagins_x") || GETPOST("button_updatemagins")) {
68  $action = 'update';
69 }
70 
71 
72 /*
73  * Actions
74  */
75 
76 if (GETPOST('cancel', 'alpha')) { $action = 'list'; $massaction = ''; }
77 if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') { $massaction = ''; }
78 
79 $parameters = array();
80 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
81 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
82 
83 if (empty($reshook))
84 {
85  // Selection of new fields
86  include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
87 
88  if ($action == 'update') {
89  $datapost = $_POST;
90 
91  foreach ($datapost as $key => $value) {
92  if (strpos($key, 'buyingprice_') !== false) {
93  $tmp_array = explode('_', $key);
94  if (count($tmp_array) > 0) {
95  $invoicedet_id = $tmp_array[1];
96  if (!empty($invoicedet_id)) {
97  $sql = 'UPDATE '.MAIN_DB_PREFIX.'facturedet';
98  $sql .= ' SET buy_price_ht=\''.price2num($value).'\'';
99  $sql .= ' WHERE rowid='.$invoicedet_id;
100  $result = $db->query($sql);
101  if (!$result) {
102  setEventMessages($db->lasterror, null, 'errors');
103  }
104  }
105  }
106  }
107  }
108  }
109 
110  // Purge search criteria
111  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
112  {
113  $search_ref = '';
114  $search_array_options = array();
115  }
116 
117  // Mass actions
118  /*
119  $objectclass='Product';
120  if ((string) $type == '1') { $objectlabel='Services'; }
121  if ((string) $type == '0') { $objectlabel='Products'; }
122 
123  $permissiontoread = $user->rights->produit->lire;
124  $permissiontodelete = $user->rights->produit->supprimer;
125  $uploaddir = $conf->product->dir_output;
126  include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
127  */
128 }
129 
130 
131 /*
132  * View
133  */
134 
135 $userstatic = new User($db);
136 $companystatic = new Societe($db);
137 $invoicestatic = new Facture($db);
138 $productstatic = new Product($db);
139 
140 $form = new Form($db);
141 
142 $title = $langs->trans("Margins");
143 
144 llxHeader('', $title);
145 
146 // print load_fiche_titre($text);
147 
148 $param = '';
149 if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param .= '&contextpage='.$contextpage;
150 if ($limit > 0 && $limit != $conf->liste_limit) $param .= '&limit='.$limit;
151 if ($search_ref != '') $param .= '&search_ref='.urlencode($search_ref);
152 if (!empty($startdate)) $param .= '&startdatemonth='.GETPOST('startdatemonth', 'int').'&startdateday='.GETPOST('startdateday', 'int').'&startdateyear='.GETPOST('startdateyear', 'int');
153 if (!empty($enddate)) $param .= '&enddatemonth='.GETPOST('enddatemonth', 'int').'&enddateday='.GETPOST('enddateday', 'int').'&enddateyear='.GETPOST('enddateyear', 'int');
154 if ($optioncss != '') $param .= '&optioncss='.$optioncss;
155 
156 // Show tabs
157 $head = marges_prepare_head($user);
158 $picto = 'margin';
159 
160 print '<form method="post" name="sel" action="'.$_SERVER['PHP_SELF'].'">';
161 print '<input type="hidden" name="token" value="'.newToken().'">';
162 
163 print dol_get_fiche_head($head, $langs->trans('checkMargins'), $title, 0, $picto);
164 
165 print '<table class="border centpercent">';
166 
167 print '<tr><td class="titlefield">'.$langs->trans('DateStart').' ('.$langs->trans("DateValidation").')</td>';
168 print '<td>';
169 print $form->selectDate($startdate, 'startdate', '', '', 1, "sel", 1, 1);
170 print '</td>';
171 print '<td>'.$langs->trans('DateEnd').' ('.$langs->trans("DateValidation").')</td>';
172 print '<td>';
173 print $form->selectDate($enddate, 'enddate', '', '', 1, "sel", 1, 1);
174 print '</td>';
175 print '<td style="text-align: center;">';
176 print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans('Refresh')).'" name="button_search" />';
177 print '</td>';
178 print '</tr>';
179 print "</table>";
180 
181 print dol_get_fiche_end();
182 
183 
184 $arrayfields = array();
185 $massactionbutton = '';
186 
187 $invoice_status_except_list = array(Facture::STATUS_DRAFT, Facture::STATUS_ABANDONED);
188 
189 $sql = "SELECT";
190 $sql .= " f.ref, f.rowid as invoiceid, d.rowid as invoicedetid, d.buy_price_ht, d.total_ht, d.subprice, d.label, d.description , d.qty";
191 $sql .= " ,d.fk_product";
192 $sql .= " FROM ".MAIN_DB_PREFIX."facture as f ";
193 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."facturedet as d ON d.fk_facture = f.rowid";
194 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON d.fk_product = p.rowid";
195 $sql .= " WHERE f.fk_statut NOT IN (".implode(', ', $invoice_status_except_list).")";
196 $sql .= " AND f.entity IN (".getEntity('invoice').") ";
197 if (!empty($startdate)) $sql .= " AND f.datef >= '".$db->idate($startdate)."'";
198 if (!empty($enddate)) $sql .= " AND f.datef <= '".$db->idate($enddate)."'";
199 if ($search_ref) $sql .= natural_search('f.ref', $search_ref);
200 $sql .= " AND d.buy_price_ht IS NOT NULL";
201 $sql .= $db->order($sortfield, $sortorder);
202 
203 $nbtotalofrecords = '';
204 if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
205  dol_syslog(__FILE__, LOG_DEBUG);
206  $result = $db->query($sql);
207  $nbtotalofrecords = $db->num_rows($result);
208  if (($page * $limit) > $nbtotalofrecords) // if total resultset is smaller then paging size (filtering), goto and load page 0
209  {
210  $page = 0;
211  $offset = 0;
212  }
213 }
214 
215 $sql .= $db->plimit($limit + 1, $offset);
216 
217 $result = $db->query($sql);
218 if ($result) {
219  $num = $db->num_rows($result);
220 
221  print '<br>';
222  print_barre_liste($langs->trans("MarginDetails"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, '', 0, '', '', $limit);
223 
224  if ($conf->global->MARGIN_TYPE == "1")
225  $labelcostprice = 'BuyingPrice';
226  else // value is 'costprice' or 'pmp'
227  $labelcostprice = 'CostPrice';
228 
229  $moreforfilter = '';
230 
231  $varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
232  //$selectedfields=$form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
233  //if ($massactionbutton) $selectedfields.=$form->showCheckAddButtons('checkforselect', 1);
234  $selectedfields = '';
235 
236  print '<div class="div-table-responsive">';
237  print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
238 
239  print '<tr class="liste_titre liste_titre_search">';
240  print '<td><input type="text" name="search_ref" value="'.dol_escape_htmltag($search_ref).'"></td>';
241  print '<td></td>';
242  print '<td></td>';
243  print '<td></td>';
244  print '<td></td>';
245  print '<td></td>';
246  print '<td class="liste_titre" align="middle">';
247  $searchpitco = $form->showFilterButtons();
248  print $searchpitco;
249  print '</td>';
250  print "</tr>\n";
251 
252  print '<tr class="liste_titre">';
253  print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "f.ref", "", $param, '', $sortfield, $sortorder);
254  print_liste_field_titre("Description", $_SERVER["PHP_SELF"], "", "", $param, 'width=20%', $sortfield, $sortorder);
255  print_liste_field_titre("UnitPriceHT", $_SERVER["PHP_SELF"], "d.subprice", "", $param, 'align="right"', $sortfield, $sortorder);
256  print_liste_field_titre($labelcostprice, $_SERVER["PHP_SELF"], "d.buy_price_ht", "", $param, 'align="right"', $sortfield, $sortorder);
257  print_liste_field_titre("Qty", $_SERVER["PHP_SELF"], "d.qty", "", $param, 'align="right"', $sortfield, $sortorder);
258  print_liste_field_titre("AmountTTC", $_SERVER["PHP_SELF"], "d.total_ht", "", $param, 'align="right"', $sortfield, $sortorder);
259  print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', $param, 'align="center"', $sortfield, $sortorder, 'maxwidthsearch ');
260  print "</tr>\n";
261 
262  $i = 0;
263  while ($i < min($num, $limit))
264  {
265  $objp = $db->fetch_object($result);
266 
267  print '<tr class="oddeven">';
268  print '<td>';
269  $result_inner = $invoicestatic->fetch($objp->invoiceid);
270  if ($result_inner < 0) {
271  setEventMessages($invoicestatic->error, null, 'errors');
272  } else {
273  print $invoicestatic->getNomUrl(1);
274  }
275  print '</td>';
276  print '<td>';
277  if (!empty($objp->fk_product)) {
278  $result_inner = $productstatic->fetch($objp->fk_product);
279  if ($result_inner < 0) {
280  setEventMessages($productstatic->error, null, 'errors');
281  } else {
282  print $productstatic->getNomUrl(1);
283  }
284  } else {
285  print $objp->label;
286  print '&nbsp;';
287  print $objp->description;
288  }
289  print '</td>';
290  print '<td class="right">';
291  print price($objp->subprice);
292  print '</td>';
293  print '<td class="right">';
294  print '<input type="text" name="buyingprice_'.$objp->invoicedetid.'" id="buyingprice_'.$objp->invoicedetid.'" size="6" value="'.price($objp->buy_price_ht).'" class="right flat">';
295  print '</td>';
296  print '<td class="right">';
297  print $objp->qty;
298  print '</td>';
299  print '<td class="right">';
300  print price($objp->total_ht);
301  print '</td>';
302  print '<td></td>';
303 
304  print "</tr>\n";
305 
306  $i++;
307  }
308 
309  print "</table>";
310 
311  print "</div>";
312 } else {
313  dol_print_error($db);
314 }
315 
316 
317 print '<div class="center">'."\n";
318 print '<input type="submit" class="button" name="button_updatemagins" id="button_updatemagins" value="'.$langs->trans("Update").'">';
319 print '</div>';
320 
321 print '</form>';
322 
323 $db->free($result);
324 
325 // End of page
326 llxFooter();
327 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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...
Class to manage products or services.
Class to manage Dolibarr users.
Definition: user.class.php:44
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...
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
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.
Class to manage generation of HTML components Only common components must be here.
marges_prepare_head()
Return array of tabs to used on pages for third parties cards.
Definition: margins.lib.php:59
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
Class to manage third parties objects (customers, suppliers, prospects...)
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
const STATUS_DRAFT
Draft status.
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
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...
const STATUS_ABANDONED
Classified abandoned and no payment done.
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
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.
Class to manage invoices.
llxFooter()
Empty footer.
Definition: wrapper.php:59