thirdpartyMargins.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/margin/tabs/thirdpartyMargins.php
  19. * \ingroup product margins
  20. * \brief Page des marges des factures clients pour un tiers
  21. */
  22. require '../../main.inc.php';
  23. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  24. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  26. $langs->load("companies");
  27. $langs->load("bills");
  28. $langs->load("products");
  29. $langs->load("margins");
  30. // Security check
  31. $socid = GETPOST('socid','int');
  32. if (! empty($user->societe_id)) $socid=$user->societe_id;
  33. $result = restrictedArea($user, 'societe','','');
  34. $mesg = '';
  35. $sortfield = GETPOST("sortfield",'alpha');
  36. $sortorder = GETPOST("sortorder",'alpha');
  37. $page = GETPOST("page",'int');
  38. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  39. $offset = $conf->liste_limit * $page;
  40. $pageprev = $page - 1;
  41. $pagenext = $page + 1;
  42. if (! $sortorder) $sortorder="DESC";
  43. if (! $sortfield) $sortfield="f.datef";
  44. $object = new Societe($db);
  45. if ($socid > 0) $object->fetch($socid);
  46. // Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
  47. $hookmanager->initHooks(array('thirdpartymargins','globalcard'));
  48. /*
  49. * Actions
  50. */
  51. $parameters=array('id'=>$socid);
  52. $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
  53. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  54. /*
  55. * View
  56. */
  57. $invoicestatic=new Facture($db);
  58. $form = new Form($db);
  59. $title=$langs->trans("ThirdParty").' - '.$langs->trans("Margins");
  60. if (! empty($conf->global->MAIN_HTML_TITLE) && preg_match('/thirdpartynameonly/',$conf->global->MAIN_HTML_TITLE) && $object->name) $title=$object->name.' - '.$langs->trans("Files");
  61. $help_url='EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
  62. llxHeader('',$title,$help_url);
  63. if ($socid > 0)
  64. {
  65. $object = new Societe($db);
  66. $object->fetch($socid);
  67. /*
  68. * Affichage onglets
  69. */
  70. $head = societe_prepare_head($object);
  71. dol_fiche_head($head, 'margin', $langs->trans("ThirdParty"), -1, 'company');
  72. $linkback = '<a href="'.DOL_URL_ROOT.'/societe/list.php">'.$langs->trans("BackToList").'</a>';
  73. dol_banner_tab($object, 'socid', $linkback, ($user->societe_id?0:1), 'rowid', 'nom');
  74. print '<div class="fichecenter">';
  75. print '<div class="underbanner clearboth"></div>';
  76. print '<table class="border tableforfield" width="100%">';
  77. if ($object->client)
  78. {
  79. print '<tr><td class="titlefield">';
  80. print $langs->trans('CustomerCode').'</td><td colspan="3">';
  81. print $object->code_client;
  82. if ($object->check_codeclient() <> 0) print ' <font class="error">('.$langs->trans("WrongCustomerCode").')</font>';
  83. print '</td></tr>';
  84. }
  85. if ($object->fournisseur)
  86. {
  87. print '<tr><td class="titlefield">';
  88. print $langs->trans('SupplierCode').'</td><td colspan="3">';
  89. print $object->code_fournisseur;
  90. if ($object->check_codefournisseur() <> 0) print ' <font class="error">('.$langs->trans("WrongSupplierCode").')</font>';
  91. print '</td></tr>';
  92. }
  93. // Total Margin
  94. print '<tr><td class="titlefield">'.$langs->trans("TotalMargin").'</td><td colspan="3">';
  95. print '<span id="totalMargin"></span>'; // set by jquery (see below)
  96. print '</td></tr>';
  97. // Margin Rate
  98. if (! empty($conf->global->DISPLAY_MARGIN_RATES)) {
  99. print '<tr><td>'.$langs->trans("MarginRate").'</td><td colspan="3">';
  100. print '<span id="marginRate"></span>'; // set by jquery (see below)
  101. print '</td></tr>';
  102. }
  103. // Mark Rate
  104. if (! empty($conf->global->DISPLAY_MARK_RATES)) {
  105. print '<tr><td>'.$langs->trans("MarkRate").'</td><td colspan="3">';
  106. print '<span id="markRate"></span>'; // set by jquery (see below)
  107. print '</td></tr>';
  108. }
  109. print "</table>";
  110. print '</div>';
  111. print '<div style="clear:both"></div>';
  112. dol_fiche_end();
  113. print '<br>';
  114. $sql = "SELECT distinct s.nom, s.rowid as socid, s.code_client,";
  115. $sql.= " f.rowid as facid, f.facnumber, f.total as total_ht,";
  116. $sql.= " f.datef, f.paye, f.fk_statut as statut, f.type,";
  117. $sql.= " sum(d.total_ht) as selling_price,"; // may be negative or positive
  118. $sql.= " sum(d.qty * d.buy_price_ht) as buying_price,"; // always positive
  119. $sql.= " sum(abs(d.total_ht) - (d.buy_price_ht * d.qty)) as marge"; // always positive
  120. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  121. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  122. $sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
  123. $sql.= " WHERE f.fk_soc = s.rowid";
  124. $sql.= " AND f.fk_statut > 0";
  125. $sql.= " AND f.entity = ".$conf->entity;
  126. $sql.= " AND d.fk_facture = f.rowid";
  127. $sql.= " AND f.fk_soc = $socid";
  128. $sql.= " AND d.buy_price_ht IS NOT NULL";
  129. if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .= " AND d.buy_price_ht <> 0";
  130. $sql.= " GROUP BY s.nom, s.rowid, s.code_client, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut, f.type";
  131. $sql.= $db->order($sortfield,$sortorder);
  132. // TODO: calculate total to display then restore pagination
  133. //$sql.= $db->plimit($conf->liste_limit +1, $offset);
  134. dol_syslog('margin:tabs:thirdpartyMargins.php', LOG_DEBUG);
  135. $result = $db->query($sql);
  136. if ($result)
  137. {
  138. $num = $db->num_rows($result);
  139. print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&amp;socid=".$object->id,$sortfield,$sortorder,'',0,0,'');
  140. $i = 0;
  141. print "<table class=\"noborder\" width=\"100%\">";
  142. print '<tr class="liste_titre">';
  143. print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&amp;socid=".$_REQUEST["socid"],'',$sortfield,$sortorder);
  144. print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&amp;socid=".$_REQUEST["socid"],'align="center"',$sortfield,$sortorder);
  145. print_liste_field_titre($langs->trans("SoldAmount"),$_SERVER["PHP_SELF"],"selling_price","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
  146. print_liste_field_titre($langs->trans("PurchasedAmount"),$_SERVER["PHP_SELF"],"buying_price","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
  147. print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
  148. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  149. print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
  150. if (! empty($conf->global->DISPLAY_MARK_RATES))
  151. print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
  152. print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"f.paye,f.fk_statut","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
  153. print "</tr>\n";
  154. $cumul_achat = 0;
  155. $cumul_vente = 0;
  156. $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT);
  157. if ($num > 0)
  158. {
  159. $var=True;
  160. while ($i < $num /*&& $i < $conf->liste_limit*/)
  161. {
  162. $objp = $db->fetch_object($result);
  163. $marginRate = ($objp->buying_price != 0)?(100 * $objp->marge / $objp->buying_price):'' ;
  164. $markRate = ($objp->selling_price != 0)?(100 * $objp->marge / $objp->selling_price):'' ;
  165. print '<tr class="oddeven">';
  166. print '<td>';
  167. $invoicestatic->id=$objp->facid;
  168. $invoicestatic->ref=$objp->facnumber;
  169. print $invoicestatic->getNomUrl(1);
  170. print "</td>\n";
  171. print "<td align=\"center\">";
  172. print dol_print_date($db->jdate($objp->datef),'day')."</td>";
  173. print "<td align=\"right\">".price($objp->selling_price, null, null, null, null, $rounding)."</td>\n";
  174. print "<td align=\"right\">".price(($objp->type == 2 ? -1 : 1) * $objp->buying_price, null, null, null, null, $rounding)."</td>\n";
  175. print "<td align=\"right\">".price($objp->marge, null, null, null, null, $rounding)."</td>\n";
  176. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  177. print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."</td>\n";
  178. if (! empty($conf->global->DISPLAY_MARK_RATES))
  179. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."</td>\n";
  180. print '<td align="right">'.$invoicestatic->LibStatut($objp->paye,$objp->statut,5).'</td>';
  181. print "</tr>\n";
  182. $i++;
  183. $cumul_vente += $objp->selling_price;
  184. $cumul_achat += ($objp->type == 2 ? -1 : 1) * $objp->buying_price;
  185. }
  186. }
  187. // affichage totaux marges
  188. $totalMargin = $cumul_vente - $cumul_achat;
  189. if ($totalMargin < 0)
  190. {
  191. $marginRate = ($cumul_achat != 0)?-1*(100 * $totalMargin / $cumul_achat):'';
  192. $markRate = ($cumul_vente != 0)?-1*(100 * $totalMargin / $cumul_vente):'';
  193. }
  194. else
  195. {
  196. $marginRate = ($cumul_achat != 0)?(100 * $totalMargin / $cumul_achat):'';
  197. $markRate = ($cumul_vente != 0)?(100 * $totalMargin / $cumul_vente):'';
  198. }
  199. // Total
  200. print '<tr class="liste_total">';
  201. print '<td colspan=2>'.$langs->trans('TotalMargin')."</td>";
  202. print "<td align=\"right\">".price($cumul_vente, null, null, null, null, $rounding)."</td>\n";
  203. print "<td align=\"right\">".price($cumul_achat, null, null, null, null, $rounding)."</td>\n";
  204. print "<td align=\"right\">".price($totalMargin, null, null, null, null, $rounding)."</td>\n";
  205. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  206. print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."</td>\n";
  207. if (! empty($conf->global->DISPLAY_MARK_RATES))
  208. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."</td>\n";
  209. print '<td align="right">&nbsp;</td>';
  210. print "</tr>\n";
  211. }
  212. else
  213. {
  214. dol_print_error($db);
  215. }
  216. print "</table>";
  217. print '<br>';
  218. $db->free($result);
  219. }
  220. else
  221. {
  222. dol_print_error('', 'Parameter socid not defined');
  223. }
  224. print '
  225. <script type="text/javascript">
  226. $(document).ready(function() {
  227. $("#totalMargin").html("'. price($totalMargin, null, null, null, null, $rounding).'");
  228. $("#marginRate").html("'.(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%").'");
  229. $("#markRate").html("'.(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%").'");
  230. });
  231. </script>
  232. ';
  233. llxFooter();
  234. $db->close();