productMargins.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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) 2020 Alexandre Spangaro <aspangaro@open-dsi.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/margin/productMargins.php
  21. * \ingroup margin
  22. * \brief Page des marges par produit
  23. */
  24. // Load Dolibarr environment
  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. $id = GETPOST('id', 'int');
  33. $ref = GETPOST('ref', 'alpha');
  34. $action = GETPOST('action', 'aZ09');
  35. $confirm = GETPOST('confirm', 'alpha');
  36. $TSelectedCats = GETPOST('categories', 'array');
  37. $socid = 0;
  38. $mesg = '';
  39. // Load variable for pagination
  40. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  41. $sortfield = GETPOST('sortfield', 'aZ09comma');
  42. $sortorder = GETPOST('sortorder', 'aZ09comma');
  43. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  44. if (empty($page) || $page == -1) {
  45. $page = 0;
  46. } // If $page is not defined, or '' or -1
  47. $offset = $limit * $page;
  48. $pageprev = $page - 1;
  49. $pagenext = $page + 1;
  50. if (!$sortfield) {
  51. if ($id > 0) {
  52. $sortfield = "f.datef";
  53. $sortorder = "DESC";
  54. } else {
  55. $sortfield = "p.ref";
  56. $sortorder = "ASC";
  57. }
  58. }
  59. $startdate = $enddate = '';
  60. if (GETPOST('startdatemonth')) {
  61. $startdate = dol_mktime(0, 0, 0, GETPOST('startdatemonth', 'int'), GETPOST('startdateday', 'int'), GETPOST('startdateyear', 'int'));
  62. }
  63. if (GETPOST('enddatemonth')) {
  64. $enddate = dol_mktime(23, 59, 59, GETPOST('enddatemonth', 'int'), GETPOST('enddateday', 'int'), GETPOST('enddateyear'));
  65. }
  66. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  67. $object = new Product($db);
  68. $hookmanager->initHooks(array('marginproductlist'));
  69. // Security check
  70. $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
  71. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  72. if (!empty($user->socid)) {
  73. $socid = $user->socid;
  74. }
  75. $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
  76. if (empty($user->rights->margins->liretous)) {
  77. accessforbidden();
  78. }
  79. /*
  80. * View
  81. */
  82. $product_static = new Product($db);
  83. $invoicestatic = new Facture($db);
  84. $form = new Form($db);
  85. llxHeader('', $langs->trans("Margins").' - '.$langs->trans("Products"));
  86. $text = $langs->trans("Margins");
  87. //print load_fiche_titre($text);
  88. // Show tabs
  89. $head = marges_prepare_head();
  90. $titre = $langs->trans("Margins");
  91. $picto = 'margin';
  92. print '<form method="post" name="sel" action="'.$_SERVER['PHP_SELF'].'">';
  93. print '<input type="hidden" name="token" value="'.newToken().'">';
  94. print dol_get_fiche_head($head, 'productMargins', $titre, 0, $picto);
  95. print '<table class="border centpercent">';
  96. // Product
  97. print '<tr><td class="titlefield">'.$langs->trans('ProductOrService').'</td>';
  98. print '<td class="maxwidthonsmartphone" colspan="4">';
  99. print img_picto('', 'product').$form->select_produits(($id > 0 ? $id : ''), 'id', '', 20, 0, 1, 2, '', 1, array(), 0, 'All', 0, '', 0, '', null, 1);
  100. print '</td></tr>';
  101. // Categories
  102. $TCats = $form->select_all_categories('product', array(), '', 64, 0, 1);
  103. print '<tr>';
  104. print '<td class="titlefield">'.$langs->trans('Category').'</td>';
  105. print '<td class="maxwidthonsmartphone" colspan="4">';
  106. print img_picto('', 'category', 'class="pictofixedwidth"').$form->multiselectarray('categories', $TCats, $TSelectedCats, 0, 0, 'quatrevingtpercent widthcentpercentminusx');
  107. print '</td>';
  108. print '</tr>';
  109. // Start date
  110. print '<tr>';
  111. print '<td class="titlefield">'.$langs->trans('DateStart').' ('.$langs->trans("DateValidation").')</td>';
  112. print '<td>';
  113. print $form->selectDate($startdate, 'startdate', '', '', 1, "sel", 1, 1);
  114. print '</td>';
  115. print '<td>'.$langs->trans('DateEnd').' ('.$langs->trans("DateValidation").')</td>';
  116. print '<td>';
  117. print $form->selectDate($enddate, 'enddate', '', '', 1, "sel", 1, 1);
  118. print '</td>';
  119. print '<td style="text-align: center;">';
  120. print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans('Refresh')).'" />';
  121. print '</td></tr>';
  122. print "</table>";
  123. print '<br>';
  124. print '<table class="border centpercent">';
  125. // Total Margin
  126. print '<tr><td class="titlefield">'.$langs->trans("TotalMargin").'</td><td colspan="4">';
  127. print '<span id="totalMargin" class="amount"></span> <span class="amount">'.$langs->getCurrencySymbol($conf->currency).'</span>'; // set by jquery (see below)
  128. print '</td></tr>';
  129. // Margin Rate
  130. if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
  131. print '<tr><td>'.$langs->trans("MarginRate").'</td><td colspan="4">';
  132. print '<span id="marginRate"></span>'; // set by jquery (see below)
  133. print '</td></tr>';
  134. }
  135. // Mark Rate
  136. if (!empty($conf->global->DISPLAY_MARK_RATES)) {
  137. print '<tr><td>'.$langs->trans("MarkRate").'</td><td colspan="4">';
  138. print '<span id="markRate"></span>'; // set by jquery (see below)
  139. print '</td></tr>';
  140. }
  141. print "</table>";
  142. print dol_get_fiche_end();
  143. print '</form>';
  144. $invoice_status_except_list = array(Facture::STATUS_DRAFT, Facture::STATUS_ABANDONED);
  145. $sql = "SELECT p.label, p.rowid, p.fk_product_type, p.ref, p.entity as pentity,";
  146. if ($id > 0) {
  147. $sql .= " d.fk_product,";
  148. }
  149. if ($id > 0) {
  150. $sql .= " f.rowid as facid, f.ref, f.total_ht, f.datef, f.paye, f.fk_statut as statut,";
  151. }
  152. $sql .= " SUM(d.total_ht) as selling_price,";
  153. $sql .= " SUM(d.qty) as product_qty,";
  154. // Note: qty and buy_price_ht is always positive (if not your database may be corrupted, you can update this)
  155. $sql .= " SUM(".$db->ifsql('(d.total_ht < 0 OR (d.total_ht = 0 AND f.type = 2))', '-1 * d.qty * d.buy_price_ht * (d.situation_percent / 100)', 'd.qty * d.buy_price_ht * (d.situation_percent / 100)').") as buying_price,";
  156. $sql .= " SUM(".$db->ifsql('(d.total_ht < 0 OR (d.total_ht = 0 AND f.type = 2))', '-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty * (d.situation_percent / 100)))', 'd.total_ht - (d.buy_price_ht * d.qty * (d.situation_percent / 100))').") as marge";
  157. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  158. $sql .= ", ".MAIN_DB_PREFIX."facture as f";
  159. $sql .= ", ".MAIN_DB_PREFIX."facturedet as d";
  160. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = d.fk_product";
  161. if (!empty($TSelectedCats)) {
  162. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_product as cp ON cp.fk_product=p.rowid';
  163. }
  164. $sql .= " WHERE f.fk_soc = s.rowid";
  165. $sql .= ' AND f.entity IN ('.getEntity('invoice').')';
  166. $sql .= " AND f.fk_statut NOT IN (".$db->sanitize(implode(', ', $invoice_status_except_list)).")";
  167. $sql .= " AND d.fk_facture = f.rowid";
  168. if ($id > 0) {
  169. $sql .= " AND d.fk_product =".((int) $id);
  170. }
  171. if (!empty($TSelectedCats)) {
  172. $sql .= ' AND cp.fk_categorie IN ('.$db->sanitize(implode(',', $TSelectedCats)).')';
  173. }
  174. if (!empty($startdate)) {
  175. $sql .= " AND f.datef >= '".$db->idate($startdate)."'";
  176. }
  177. if (!empty($enddate)) {
  178. $sql .= " AND f.datef <= '".$db->idate($enddate)."'";
  179. }
  180. $sql .= " AND d.buy_price_ht IS NOT NULL";
  181. // We should not use this here. Option ForceBuyingPriceIfNull should have effect only when inserting data. Once data is recorded, it must be used as it is for report.
  182. // We keep it with value ForceBuyingPriceIfNull = 2 for retroactive effect but results are unpredicable.
  183. if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 2) {
  184. $sql .= " AND d.buy_price_ht <> 0";
  185. }
  186. if ($id > 0) {
  187. $sql .= " GROUP BY p.label, p.rowid, p.fk_product_type, p.ref, p.entity, d.fk_product, f.rowid, f.ref, f.total_ht, f.datef, f.paye, f.fk_statut";
  188. } else {
  189. $sql .= " GROUP BY p.label, p.rowid, p.fk_product_type, p.ref, p.entity";
  190. }
  191. $sql .= $db->order($sortfield, $sortorder);
  192. // TODO: calculate total to display then restore pagination
  193. //$sql.= $db->plimit($conf->liste_limit +1, $offset);
  194. $param = '&id='.((int) $id);
  195. if (GETPOST('startdatemonth', 'int')) {
  196. $param .= '&startdateyear='.GETPOST('startdateyear', 'int');
  197. $param .= '&startdatemonth='.GETPOST('startdatemonth', 'int');
  198. $param .= '&startdateday='.GETPOST('startdateday', 'int');
  199. }
  200. if (GETPOST('enddatemonth', 'int')) {
  201. $param .= '&enddateyear='.GETPOST('enddateyear', 'int');
  202. $param .= '&enddatemonth='.GETPOST('enddatemonth', 'int');
  203. $param .= '&enddateday='.GETPOST('enddateday', 'int');
  204. }
  205. $listofcateg = GETPOST('categories', 'array:int');
  206. if (is_array($listofcateg)) {
  207. foreach ($listofcateg as $val) {
  208. $param .= '&categories[]='.$val;
  209. }
  210. }
  211. dol_syslog('margin::productMargins.php', LOG_DEBUG);
  212. $result = $db->query($sql);
  213. if ($result) {
  214. $num = $db->num_rows($result);
  215. print '<br>';
  216. print_barre_liste($langs->trans("MarginDetails"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $num, '', 0, '', '', 0, 1);
  217. //var_dump($conf->global->MARGIN_TYPE);
  218. if ($conf->global->MARGIN_TYPE == "1") {
  219. $labelcostprice = 'BuyingPrice';
  220. } else { // value is 'costprice' or 'pmp'
  221. $labelcostprice = 'CostPrice';
  222. }
  223. $moreforfilter = '';
  224. $i = 0;
  225. print '<div class="div-table-responsive">';
  226. print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
  227. print '<tr class="liste_titre">';
  228. if ($id > 0) {
  229. print_liste_field_titre("Invoice", $_SERVER["PHP_SELF"], "f.ref", "", $param, '', $sortfield, $sortorder);
  230. print_liste_field_titre("DateInvoice", $_SERVER["PHP_SELF"], "f.datef", "", $param, '', $sortfield, $sortorder, 'center ');
  231. } else {
  232. print_liste_field_titre("ProductService", $_SERVER["PHP_SELF"], "p.ref", "", $param, '', $sortfield, $sortorder);
  233. }
  234. print_liste_field_titre("Qty", $_SERVER["PHP_SELF"], "product_qty", "", $param, '', $sortfield, $sortorder, 'center ');
  235. print_liste_field_titre("SellingPrice", $_SERVER["PHP_SELF"], "selling_price", "", $param, '', $sortfield, $sortorder, 'right ');
  236. print_liste_field_titre($labelcostprice, $_SERVER["PHP_SELF"], "buying_price", "", $param, '', $sortfield, $sortorder, 'right ');
  237. print_liste_field_titre("Margin", $_SERVER["PHP_SELF"], "marge", "", $param, '', $sortfield, $sortorder, 'right ');
  238. if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
  239. print_liste_field_titre("MarginRate", $_SERVER["PHP_SELF"], "", "", $param, '', $sortfield, $sortorder, 'right ');
  240. }
  241. if (!empty($conf->global->DISPLAY_MARK_RATES)) {
  242. print_liste_field_titre("MarkRate", $_SERVER["PHP_SELF"], "", "", $param, '', $sortfield, $sortorder, 'right ');
  243. }
  244. print "</tr>\n";
  245. $cumul_achat = 0;
  246. $cumul_vente = 0;
  247. $cumul_qty = 0;
  248. if ($num > 0) {
  249. while ($i < $num /*&& $i < $conf->liste_limit*/) {
  250. $objp = $db->fetch_object($result);
  251. $qty = $objp->product_qty;
  252. $pa = $objp->buying_price;
  253. $pv = $objp->selling_price;
  254. $marge = $objp->marge;
  255. if ($marge < 0) {
  256. $marginRate = ($pa != 0) ?-1 * (100 * $marge / $pa) : '';
  257. $markRate = ($pv != 0) ?-1 * (100 * $marge / $pv) : '';
  258. } else {
  259. $marginRate = ($pa != 0) ? (100 * $marge / $pa) : '';
  260. $markRate = ($pv != 0) ? (100 * $marge / $pv) : '';
  261. }
  262. print '<tr class="oddeven">';
  263. if ($id > 0) {
  264. print '<td>';
  265. $invoicestatic->id = $objp->facid;
  266. $invoicestatic->ref = $objp->ref;
  267. print $invoicestatic->getNomUrl(1);
  268. print "</td>\n";
  269. print "<td class=\"center\">";
  270. print dol_print_date($db->jdate($objp->datef), 'day')."</td>";
  271. } else {
  272. print '<td>';
  273. if ($objp->rowid > 0) {
  274. $product_static->type = $objp->fk_product_type;
  275. $product_static->id = $objp->rowid;
  276. $product_static->ref = $objp->ref;
  277. $product_static->label = $objp->label;
  278. $product_static->entity = $objp->pentity;
  279. $text = $product_static->getNomUrl(1);
  280. print $text .= ' - '.$objp->label;
  281. } else {
  282. print img_object('', 'product').' '.$langs->trans("NotPredefinedProducts");
  283. }
  284. print "</td>\n";
  285. //print "<td>".$product_static->getNomUrl(1)."</td>\n";
  286. }
  287. print '<td class="center">'.$qty.'</td>';
  288. print '<td class="nowrap right"><span class="amount">'.price(price2num($pv, 'MT')).'</span></td>';
  289. print '<td class="nowrap right"><span class="amount">'.price(price2num($pa, 'MT')).'</span></td>';
  290. print '<td class="nowrap right"><span class="amount">'.price(price2num($marge, 'MT')).'</span></td>';
  291. if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
  292. print '<td class="nowrap right">'.(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%").'</td>';
  293. }
  294. if (!empty($conf->global->DISPLAY_MARK_RATES)) {
  295. print '<td class="nowrap right">'.(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%").'</td>';
  296. }
  297. print "</tr>\n";
  298. $i++;
  299. $cumul_achat += $objp->buying_price;
  300. $cumul_vente += $objp->selling_price;
  301. $cumul_qty += $objp->product_qty;
  302. }
  303. }
  304. // affichage totaux marges
  305. $totalMargin = $cumul_vente - $cumul_achat;
  306. $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) : '';
  307. $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) : '';
  308. print '<tr class="liste_total">';
  309. if ($id > 0) {
  310. print '<td colspan=2>';
  311. } else {
  312. print '<td>';
  313. }
  314. print $langs->trans('TotalMargin').'</td>';
  315. print '<td class="center">'.$cumul_qty.'</td>';
  316. print '<td class="nowrap right">'.price(price2num($cumul_vente, 'MT')).'</td>';
  317. print '<td class="nowrap right">'.price(price2num($cumul_achat, 'MT')).'</td>';
  318. print '<td class="nowrap right">'.price(price2num($totalMargin, 'MT')).'</td>';
  319. if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
  320. print '<td class="nowrap right">'.(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%").'</td>';
  321. }
  322. if (!empty($conf->global->DISPLAY_MARK_RATES)) {
  323. print '<td class="nowrap right">'.(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%").'</td>';
  324. }
  325. print "</tr>\n";
  326. print "</table>";
  327. print '</div>';
  328. } else {
  329. dol_print_error($db);
  330. }
  331. $db->free($result);
  332. print '
  333. <script type="text/javascript">
  334. $(document).ready(function() {
  335. console.log("Init some values");
  336. $("#totalMargin").html("'.price(price2num($totalMargin, 'MT')).'");
  337. $("#marginRate").html("'.(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%").'");
  338. $("#markRate").html("'.(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%").'");
  339. });
  340. </script>
  341. ';
  342. // End of page
  343. llxFooter();
  344. $db->close();