checkMargins.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/margin/checkMargins.php
  22. * \ingroup margin
  23. * \brief Check margins
  24. */
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php';
  27. require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
  28. require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  29. require_once DOL_DOCUMENT_ROOT . '/margin/lib/margins.lib.php';
  30. // Load translation files required by the page
  31. $langs->loadLangs(array('companies', 'bills', 'products', 'margins'));
  32. $action = GETPOST('action','alpha');
  33. $massaction = GETPOST('massaction','alpha');
  34. $toselect = GETPOST('toselect', 'array');
  35. $contextpage= GETPOST('contextpage','aZ')?GETPOST('contextpage','aZ'):'margindetail'; // To manage different context of search
  36. $backtopage = GETPOST('backtopage','alpha');
  37. $optioncss = GETPOST('optioncss','alpha');
  38. // Load variable for pagination
  39. $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
  40. $sortfield = GETPOST('sortfield','alpha');
  41. $sortorder = GETPOST('sortorder','alpha');
  42. $page = GETPOST('page','int');
  43. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  44. $offset = $limit * $page;
  45. $pageprev = $page - 1;
  46. $pagenext = $page + 1;
  47. if (! $sortorder) $sortorder = "DESC";
  48. if (! $sortfield) $sortfield = 'f.facnumber';
  49. $startdate = $enddate = '';
  50. $startdate = dol_mktime(0, 0, 0, GETPOST('startdatemonth', 'int'), GETPOST('startdateday', 'int'), GETPOST('startdateyear', 'int'));
  51. $enddate = dol_mktime(23, 59, 59, GETPOST('enddatemonth', 'int'), GETPOST('enddateday', 'int'), GETPOST('enddateyear', 'int'));
  52. $search_ref = GETPOST('search_ref','alpha');
  53. // Security check
  54. $result=restrictedArea($user,'margins');
  55. // Both test are required to be compatible with all browsers
  56. if (GETPOST("button_search_x") || GETPOST("button_search")) {
  57. $action = 'search';
  58. } elseif (GETPOST("button_updatemagins_x") || GETPOST("button_updatemagins")) {
  59. $action = 'update';
  60. }
  61. /*
  62. * Actions
  63. */
  64. if (GETPOST('cancel','alpha')) { $action='list'; $massaction=''; }
  65. if (! GETPOST('confirmmassaction','alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') { $massaction=''; }
  66. $parameters=array();
  67. $reshook=$hookmanager->executeHooks('doActions',$parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  68. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  69. if (empty($reshook))
  70. {
  71. // Selection of new fields
  72. include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
  73. if ($action == 'update') {
  74. $datapost = $_POST;
  75. foreach ( $datapost as $key => $value ) {
  76. if (strpos($key, 'buyingprice_') !== false) {
  77. $tmp_array = explode('_', $key);
  78. if (count($tmp_array) > 0) {
  79. $invoicedet_id = $tmp_array[1];
  80. if (! empty($invoicedet_id)) {
  81. $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facturedet';
  82. $sql .= ' SET buy_price_ht=\'' . price2num($value) . '\'';
  83. $sql .= ' WHERE rowid=' . $invoicedet_id;
  84. $result = $db->query($sql);
  85. if (!$result) {
  86. setEventMessages($db->lasterror, null, 'errors');
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. // Purge search criteria
  94. 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
  95. {
  96. $search_ref='';
  97. $search_array_options=array();
  98. }
  99. // Mass actions
  100. /*
  101. $objectclass='Product';
  102. if ((string) $type == '1') { $objectlabel='Services'; }
  103. if ((string) $type == '0') { $objectlabel='Products'; }
  104. $permtoread = $user->rights->produit->lire;
  105. $permtodelete = $user->rights->produit->supprimer;
  106. $uploaddir = $conf->product->dir_output;
  107. include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
  108. */
  109. }
  110. /*
  111. * View
  112. */
  113. $userstatic = new User($db);
  114. $companystatic = new Societe($db);
  115. $invoicestatic = new Facture($db);
  116. $productstatic = new Product($db);
  117. $form = new Form($db);
  118. $title = $langs->trans("Margins");
  119. llxHeader('', $title);
  120. // print_fiche_titre($text);
  121. $param='';
  122. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
  123. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
  124. if ($search_ref != '') $param.='&search_ref='.urlencode($search_ref);
  125. if (! empty($startdate)) $param .= '&startdatemonth=' . GETPOST('startdatemonth', 'int') . '&startdateday=' . GETPOST('startdateday', 'int') . '&startdateyear=' . GETPOST('startdateyear', 'int');
  126. if (! empty($enddate)) $param .= '&enddatemonth=' . GETPOST('enddatemonth', 'int') . '&enddateday=' . GETPOST('enddateday', 'int') . '&enddateyear=' . GETPOST('enddateyear', 'int');
  127. if ($optioncss != '') $param.='&optioncss='.$optioncss;
  128. // Show tabs
  129. $head = marges_prepare_head($user);
  130. $picto = 'margin';
  131. print '<form method="post" name="sel" action="' . $_SERVER['PHP_SELF'] . '">';
  132. dol_fiche_head($head, $langs->trans('checkMargins'), $title, 0, $picto);
  133. print '<table class="border" width="100%">';
  134. print '<tr><td class="titlefield">' . $langs->trans('DateStart') . ' (' . $langs->trans("DateValidation") . ')</td>';
  135. print '<td>';
  136. print $form->selectDate($startdate, 'startdate', '', '', 1, "sel", 1, 1);
  137. print '</td>';
  138. print '<td>' . $langs->trans('DateEnd') . ' (' . $langs->trans("DateValidation") . ')</td>';
  139. print '<td>';
  140. print $form->selectDate($enddate, 'enddate', '', '', 1, "sel", 1, 1);
  141. print '</td>';
  142. print '<td style="text-align: center;">';
  143. print '<input type="submit" class="button" value="' . dol_escape_htmltag($langs->trans('Refresh')) . '" name="button_search" />';
  144. print '</td>';
  145. print '</tr>';
  146. print "</table>";
  147. dol_fiche_end();
  148. $arrayfields=array();
  149. $massactionbutton='';
  150. $sql = "SELECT";
  151. $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";
  152. $sql .= " ,d.fk_product";
  153. $sql .= " FROM " . MAIN_DB_PREFIX . "facture as f ";
  154. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "facturedet as d ON d.fk_facture = f.rowid";
  155. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON d.fk_product = p.rowid";
  156. $sql .= " WHERE f.fk_statut > 0";
  157. $sql .= " AND f.entity IN (" . getEntity('facture') . ") ";
  158. if (! empty($startdate)) $sql .= " AND f.datef >= '" . $db->idate($startdate) . "'";
  159. if (! empty($enddate)) $sql .= " AND f.datef <= '" . $db->idate($enddate) . "'";
  160. if ($search_ref) $sql.=natural_search('f.facnumber', $search_ref);
  161. $sql .= " AND d.buy_price_ht IS NOT NULL";
  162. $sql .= $db->order($sortfield, $sortorder);
  163. $nbtotalofrecords = '';
  164. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  165. dol_syslog(__FILE__, LOG_DEBUG);
  166. $result = $db->query($sql);
  167. $nbtotalofrecords = $db->num_rows($result);
  168. if (($page * $limit) > $nbtotalofrecords) // if total resultset is smaller then paging size (filtering), goto and load page 0
  169. {
  170. $page = 0;
  171. $offset = 0;
  172. }
  173. }
  174. $sql .= $db->plimit($limit+1, $offset);
  175. $result = $db->query($sql);
  176. if ($result) {
  177. $num = $db->num_rows($result);
  178. print '<br>';
  179. print_barre_liste($langs->trans("MarginDetails"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, '', 0, '', '', $limit);
  180. if ($conf->global->MARGIN_TYPE == "1")
  181. $labelcostprice='BuyingPrice';
  182. else // value is 'costprice' or 'pmp'
  183. $labelcostprice='CostPrice';
  184. $moreforfilter='';
  185. $varpage=empty($contextpage)?$_SERVER["PHP_SELF"]:$contextpage;
  186. //$selectedfields=$form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
  187. //if ($massactionbutton) $selectedfields.=$form->showCheckAddButtons('checkforselect', 1);
  188. $selectedfields='';
  189. print '<div class="div-table-responsive">';
  190. print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
  191. print '<tr class="liste_titre liste_titre_search">';
  192. print '<td><input type="text" name="search_ref" value="'.dol_escape_htmltag($search_ref).'"></td>';
  193. print '<td></td>';
  194. print '<td></td>';
  195. print '<td></td>';
  196. print '<td></td>';
  197. print '<td></td>';
  198. print '<td class="liste_titre" align="middle">';
  199. $searchpitco=$form->showFilterButtons();
  200. print $searchpitco;
  201. print '</td>';
  202. print "</tr>\n";
  203. print '<tr class="liste_titre">';
  204. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "f.facnumber", "", $param, '', $sortfield, $sortorder);
  205. print_liste_field_titre("Description", $_SERVER["PHP_SELF"], "", "", $param, 'width=20%', $sortfield, $sortorder);
  206. print_liste_field_titre("UnitPriceHT", $_SERVER["PHP_SELF"], "d.subprice", "", $param, 'align="right"', $sortfield, $sortorder);
  207. print_liste_field_titre($labelcostprice, $_SERVER["PHP_SELF"], "d.buy_price_ht", "", $param, 'align="right"', $sortfield, $sortorder);
  208. print_liste_field_titre("Qty", $_SERVER["PHP_SELF"], "d.qty", "", $param, 'align="right"', $sortfield, $sortorder);
  209. print_liste_field_titre("AmountTTC", $_SERVER["PHP_SELF"], "d.total_ht", "", $param, 'align="right"', $sortfield, $sortorder);
  210. print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"],"",'',$param,'align="center"',$sortfield,$sortorder,'maxwidthsearch ');
  211. print "</tr>\n";
  212. $i=0;
  213. while ($i < min($num, $limit))
  214. {
  215. $objp = $db->fetch_object($result);
  216. print '<tr class="oddeven">';
  217. print '<td>';
  218. $result_inner = $invoicestatic->fetch($objp->invoiceid);
  219. if ($result_inner < 0) {
  220. setEventMessages($invoicestatic->error, null, 'errors');
  221. } else {
  222. print $invoicestatic->getNomUrl(1);
  223. }
  224. print '</td>';
  225. print '<td>';
  226. if (! empty($objp->fk_product)) {
  227. $result_inner = $productstatic->fetch($objp->fk_product);
  228. if ($result_inner < 0) {
  229. setEventMessages($productstatic->error, null, 'errors');
  230. } else {
  231. print $productstatic->getNomUrl(1);
  232. }
  233. } else {
  234. print $objp->label;
  235. print '&nbsp;';
  236. print $objp->description;
  237. }
  238. print '</td>';
  239. print '<td align="right">';
  240. print price($objp->subprice);
  241. print '</td>';
  242. print '<td align="right">';
  243. print '<input type="text" name="buyingprice_' . $objp->invoicedetid . '" id="buyingprice_' . $objp->invoicedetid . '" size="6" value="' . price($objp->buy_price_ht) . '" class="right flat">';
  244. print '</td>';
  245. print '<td align="right">';
  246. print $objp->qty;
  247. print '</td>';
  248. print '<td align="right">';
  249. print price($objp->total_ht);
  250. print '</td>';
  251. print '<td></td>';
  252. print "</tr>\n";
  253. $i ++;
  254. }
  255. print "</table>";
  256. print "</div>";
  257. } else {
  258. dol_print_error($db);
  259. }
  260. print '<div class="center">' . "\n";
  261. print '<input type="submit" class="button" name="button_updatemagins" id="button_updatemagins" value="' . $langs->trans("Update") . '">';
  262. print '</div>';
  263. print '</form>';
  264. $db->free($result);
  265. // End of page
  266. llxFooter();
  267. $db->close();