thirdpartyMargins.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /* Copyright (C) 2012-2013 Christophe Battarel <christophe.battarel@altairis.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/margin/tabs/thirdpartyMargins.php
  19. * \ingroup product margins
  20. * \brief Page for invoice margins of a thirdparty
  21. */
  22. // Load Dolibarr environment
  23. require '../../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  27. $langs->loadLangs(array("companies", "bills", "products", "margins"));
  28. // Security check
  29. $socid = GETPOST('socid', 'int');
  30. if (!empty($user->socid)) {
  31. $socid = $user->socid;
  32. }
  33. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  34. $sortfield = GETPOST('sortfield', 'aZ09comma');
  35. $sortorder = GETPOST('sortorder', 'aZ09comma');
  36. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  37. if (empty($page) || $page == -1) {
  38. $page = 0;
  39. } // If $page is not defined, or '' or -1
  40. $offset = $limit * $page;
  41. $pageprev = $page - 1;
  42. $pagenext = $page + 1;
  43. if (!$sortorder) {
  44. $sortorder = "DESC";
  45. }
  46. if (!$sortfield) {
  47. $sortfield = "f.datef";
  48. }
  49. $object = new Societe($db);
  50. if ($socid > 0) {
  51. $object->fetch($socid);
  52. }
  53. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  54. $hookmanager->initHooks(array('thirdpartymargins', 'globalcard'));
  55. $result = restrictedArea($user, 'societe', $object->id, '');
  56. if (!$user->hasRight('margins', 'liretous')) {
  57. accessforbidden();
  58. }
  59. /*
  60. * Actions
  61. */
  62. $parameters = array('id'=>$socid);
  63. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  64. if ($reshook < 0) {
  65. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  66. }
  67. /*
  68. * View
  69. */
  70. $invoicestatic = new Facture($db);
  71. $form = new Form($db);
  72. $title = $langs->trans("ThirdParty").' - '.$langs->trans("Margins");
  73. if (!empty($conf->global->MAIN_HTML_TITLE) && preg_match('/thirdpartynameonly/', $conf->global->MAIN_HTML_TITLE) && $object->name) {
  74. $title = $object->name.' - '.$langs->trans("Files");
  75. }
  76. $help_url = 'EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
  77. llxHeader('', $title, $help_url);
  78. if ($socid > 0) {
  79. $object = new Societe($db);
  80. $object->fetch($socid);
  81. /*
  82. * Affichage onglets
  83. */
  84. $head = societe_prepare_head($object);
  85. print dol_get_fiche_head($head, 'margin', $langs->trans("ThirdParty"), -1, 'company');
  86. $linkback = '<a href="'.DOL_URL_ROOT.'/societe/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  87. dol_banner_tab($object, 'socid', $linkback, ($user->socid ? 0 : 1), 'rowid', 'nom');
  88. print '<div class="fichecenter">';
  89. print '<div class="underbanner clearboth"></div>';
  90. print '<table class="border tableforfield" width="100%">';
  91. // Type Prospect/Customer/Supplier
  92. print '<tr><td class="titlefield">'.$langs->trans('NatureOfThirdParty').'</td><td>';
  93. print $object->getTypeUrl(1);
  94. print '</td></tr>';
  95. if ($object->client) {
  96. print '<tr><td class="titlefield">';
  97. print $langs->trans('CustomerCode').'</td><td colspan="3">';
  98. print showValueWithClipboardCPButton(dol_escape_htmltag($object->code_client));
  99. $tmpcheck = $object->check_codeclient();
  100. if ($tmpcheck != 0 && $tmpcheck != -5) {
  101. print ' <span class="error">('.$langs->trans("WrongCustomerCode").')</span>';
  102. }
  103. print '</td></tr>';
  104. }
  105. if (((isModEnabled("fournisseur") && $user->hasRight('fournisseur', 'lire') && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || (isModEnabled("supplier_order") && $user->hasRight('supplier_order', 'lire')) || (isModEnabled("supplier_invoice") && $user->hasRight('supplier_invoice', 'lire'))) && $object->fournisseur) {
  106. print '<tr><td class="titlefield">';
  107. print $langs->trans('SupplierCode').'</td><td colspan="3">';
  108. print showValueWithClipboardCPButton(dol_escape_htmltag($object->code_fournisseur));
  109. $tmpcheck = $object->check_codefournisseur();
  110. if ($tmpcheck != 0 && $tmpcheck != -5) {
  111. print ' <span class="error">('.$langs->trans("WrongSupplierCode").')</span>';
  112. }
  113. print '</td></tr>';
  114. }
  115. // Total Margin
  116. print '<tr><td class="titlefield">'.$langs->trans("TotalMargin").'</td><td colspan="3">';
  117. print '<span id="totalMargin" class="amount"></span>'; // set by jquery (see below)
  118. print '</td></tr>';
  119. // Margin Rate
  120. if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
  121. print '<tr><td>'.$langs->trans("MarginRate").'</td><td colspan="3">';
  122. print '<span id="marginRate"></span>'; // set by jquery (see below)
  123. print '</td></tr>';
  124. }
  125. // Mark Rate
  126. if (!empty($conf->global->DISPLAY_MARK_RATES)) {
  127. print '<tr><td>'.$langs->trans("MarkRate").'</td><td colspan="3">';
  128. print '<span id="markRate"></span>'; // set by jquery (see below)
  129. print '</td></tr>';
  130. }
  131. print "</table>";
  132. print '</div>';
  133. print '<div class="clearboth"></div>';
  134. print dol_get_fiche_end();
  135. print '<br>';
  136. $sql = "SELECT distinct s.nom, s.rowid as socid, s.code_client,";
  137. $sql .= " f.rowid as facid, f.ref, f.total_ht,";
  138. $sql .= " f.datef, f.paye, f.fk_statut as statut, f.type,";
  139. $sql .= " sum(d.total_ht) as selling_price,"; // may be negative or positive
  140. $sql .= " sum(d.qty * d.buy_price_ht * (d.situation_percent / 100)) as buying_price,"; // always positive
  141. $sql .= " sum(abs(d.total_ht) - (d.buy_price_ht * d.qty * (d.situation_percent / 100))) as marge"; // always positive
  142. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  143. $sql .= ", ".MAIN_DB_PREFIX."facture as f";
  144. $sql .= ", ".MAIN_DB_PREFIX."facturedet as d";
  145. $sql .= " WHERE f.fk_soc = s.rowid";
  146. $sql .= " AND f.fk_statut > 0";
  147. $sql .= " AND f.entity IN (".getEntity('invoice').")";
  148. $sql .= " AND d.fk_facture = f.rowid";
  149. $sql .= " AND f.fk_soc = $socid";
  150. $sql .= " AND d.buy_price_ht IS NOT NULL";
  151. // 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.
  152. // We keep it with value ForceBuyingPriceIfNull = 2 for retroactive effect but results are unpredicable.
  153. if (getDolGlobalInt('ForceBuyingPriceIfNull') == 2) {
  154. $sql .= " AND d.buy_price_ht <> 0";
  155. }
  156. $sql .= " GROUP BY s.nom, s.rowid, s.code_client, f.rowid, f.ref, f.total_ht, f.datef, f.paye, f.fk_statut, f.type";
  157. $sql .= $db->order($sortfield, $sortorder);
  158. // TODO: calculate total to display then restore pagination
  159. //$sql.= $db->plimit($conf->liste_limit +1, $offset);
  160. dol_syslog('margin:tabs:thirdpartyMargins.php', LOG_DEBUG);
  161. $result = $db->query($sql);
  162. if ($result) {
  163. $num = $db->num_rows($result);
  164. print_barre_liste($langs->trans("MarginDetails"), $page, $_SERVER["PHP_SELF"], "&amp;socid=".$object->id, $sortfield, $sortorder, '', $num, $num, '');
  165. $i = 0;
  166. print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  167. print "<table class=\"noborder\" width=\"100%\">";
  168. print '<tr class="liste_titre">';
  169. print_liste_field_titre("Invoice", $_SERVER["PHP_SELF"], "f.ref", "", "&amp;socid=".$_REQUEST["socid"], '', $sortfield, $sortorder);
  170. print_liste_field_titre("DateInvoice", $_SERVER["PHP_SELF"], "f.datef", "", "&amp;socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'center ');
  171. print_liste_field_titre("SoldAmount", $_SERVER["PHP_SELF"], "selling_price", "", "&amp;socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'right ');
  172. print_liste_field_titre("PurchasedAmount", $_SERVER["PHP_SELF"], "buying_price", "", "&amp;socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'right ');
  173. print_liste_field_titre("Margin", $_SERVER["PHP_SELF"], "marge", "", "&amp;socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'right ');
  174. if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
  175. print_liste_field_titre("MarginRate", $_SERVER["PHP_SELF"], "", "", "&amp;socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'right ');
  176. }
  177. if (!empty($conf->global->DISPLAY_MARK_RATES)) {
  178. print_liste_field_titre("MarkRate", $_SERVER["PHP_SELF"], "", "", "&amp;socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'right ');
  179. }
  180. print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "f.paye,f.fk_statut", "", "&amp;socid=".$_REQUEST["socid"], '', $sortfield, $sortorder, 'right ');
  181. print "</tr>\n";
  182. $cumul_achat = 0;
  183. $cumul_vente = 0;
  184. if ($num > 0) {
  185. while ($i < $num /*&& $i < $conf->liste_limit*/) {
  186. $objp = $db->fetch_object($result);
  187. $marginRate = ($objp->buying_price != 0) ? (100 * $objp->marge / $objp->buying_price) : '';
  188. $markRate = ($objp->selling_price != 0) ? (100 * $objp->marge / $objp->selling_price) : '';
  189. $sign = '';
  190. if ($objp->type == Facture::TYPE_CREDIT_NOTE) {
  191. $sign = '-';
  192. }
  193. print '<tr class="oddeven">';
  194. print '<td>';
  195. $invoicestatic->id = $objp->facid;
  196. $invoicestatic->ref = $objp->ref;
  197. print $invoicestatic->getNomUrl(1);
  198. print "</td>\n";
  199. print "<td class=\"center\">";
  200. print dol_print_date($db->jdate($objp->datef), 'day')."</td>";
  201. print "<td class=\"right amount\">".price(price2num($objp->selling_price, 'MT'))."</td>\n";
  202. print "<td class=\"right amount\">".price(price2num(($objp->type == 2 ? -1 : 1) * $objp->buying_price, 'MT'))."</td>\n";
  203. print "<td class=\"right amount\">".$sign.price(price2num($objp->marge, 'MT'))."</td>\n";
  204. if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
  205. print "<td class=\"right\">".(($marginRate === '') ? 'n/a' : $sign.price(price2num($marginRate, 'MT'))."%")."</td>\n";
  206. }
  207. if (!empty($conf->global->DISPLAY_MARK_RATES)) {
  208. print "<td class=\"right\">".(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%")."</td>\n";
  209. }
  210. print '<td class="right">'.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5).'</td>';
  211. print "</tr>\n";
  212. $i++;
  213. $cumul_vente += $objp->selling_price;
  214. $cumul_achat += ($objp->type == 2 ? -1 : 1) * $objp->buying_price;
  215. }
  216. }
  217. // affichage totaux marges
  218. $totalMargin = $cumul_vente - $cumul_achat;
  219. if ($totalMargin < 0) {
  220. $marginRate = ($cumul_achat != 0) ?-1 * (100 * $totalMargin / $cumul_achat) : '';
  221. $markRate = ($cumul_vente != 0) ?-1 * (100 * $totalMargin / $cumul_vente) : '';
  222. } else {
  223. $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) : '';
  224. $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) : '';
  225. }
  226. // Total
  227. print '<tr class="liste_total">';
  228. print '<td colspan=2>'.$langs->trans('TotalMargin')."</td>";
  229. print "<td class=\"right\">".price(price2num($cumul_vente, 'MT'))."</td>\n";
  230. print "<td class=\"right\">".price(price2num($cumul_achat, 'MT'))."</td>\n";
  231. print "<td class=\"right\">".price(price2num($totalMargin, 'MT'))."</td>\n";
  232. if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
  233. print "<td class=\"right\">".(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%")."</td>\n";
  234. }
  235. if (!empty($conf->global->DISPLAY_MARK_RATES)) {
  236. print "<td class=\"right\">".(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%")."</td>\n";
  237. }
  238. print '<td class="right">&nbsp;</td>';
  239. print "</tr>\n";
  240. } else {
  241. dol_print_error($db);
  242. }
  243. print "</table>";
  244. print '</div>';
  245. print '<br>';
  246. $db->free($result);
  247. } else {
  248. dol_print_error('', 'Parameter socid not defined');
  249. }
  250. print '
  251. <script type="text/javascript">
  252. $(document).ready(function() {
  253. $("#totalMargin").html("'. price(price2num($totalMargin, 'MT')).'");
  254. $("#marginRate").html("'.(($marginRate === '') ? 'n/a' : price(price2num($marginRate, 'MT'))."%").'");
  255. $("#markRate").html("'.(($markRate === '') ? 'n/a' : price(price2num($markRate, 'MT'))."%").'");
  256. });
  257. </script>
  258. ';
  259. // End of page
  260. llxFooter();
  261. $db->close();