checkMargins.php 12 KB

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