checkMargins.php 12 KB

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