26require
'../../main.inc.php';
34require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
35require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
36require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
38$langs->loadLangs(array(
"companies",
"bills",
"products",
"margins"));
41$dol_openinpopup =
GETPOST(
'dol_openinpopup',
'aZ09');
45if (!empty($user->socid)) {
46 $socid = $user->socid;
50$sortfield =
GETPOST(
'sortfield',
'aZ09comma');
51$sortorder =
GETPOST(
'sortorder',
'aZ09comma');
53if (empty($page) || $page < 0 ||
GETPOST(
'button_search',
'alpha') ||
GETPOST(
'button_removefilter',
'alpha')) {
57$offset = $limit * $page;
64 $sortfield =
"f.datef";
73$hookmanager->initHooks(array(
'thirdpartymargins',
'globalcard'));
77if (!$user->hasRight(
'margins',
'liretous')) {
86$parameters = array(
'id' => $socid);
87$reshook = $hookmanager->executeHooks(
'doActions', $parameters, $object, $action);
92$search_invoice_date_start =
'';
93$search_invoice_date_end =
'';
94if (
GETPOSTINT(
'search_invoice_date_start_month')) {
97if (
GETPOSTINT(
'search_invoice_date_end_month')) {
102if (
GETPOST(
'button_removefilter_x',
'alpha') ||
GETPOST(
'button_removefilter.x',
'alpha') ||
GETPOST(
'button_removefilter',
'alpha')) {
103 $search_invoice_date_start =
'';
104 $search_invoice_date_end =
'';
108if (empty($search_invoice_date_start) && empty($search_invoice_date_end) && !GETPOSTISSET(
'restore_lastsearch_values')) {
109 $query =
"SELECT date_start, date_end";
110 $query .=
" FROM ".MAIN_DB_PREFIX.
"accounting_fiscalyear";
111 $query .=
" WHERE date_start < '".$db->idate(
dol_now()).
"' and date_end > '".
$db->idate(
dol_now()).
"' limit 1";
112 $res =
$db->query($query);
114 if ($res &&
$db->num_rows($res) > 0) {
115 $fiscalYear =
$db->fetch_object($res);
116 $search_invoice_date_start = strtotime($fiscalYear->date_start);
117 $search_invoice_date_end = strtotime($fiscalYear->date_end);
124 $year_end = $year_start + 1;
125 $month_end = $month_start - 1;
126 if ($month_end < 1) {
130 $search_invoice_date_start =
dol_mktime(0, 0, 0, $month_start, 1, $year_start);
143$title = $langs->trans(
"ThirdParty").
' - '.$langs->trans(
"Margins");
145 $title =
$object->name.
' - '.$langs->trans(
"Files");
147$help_url =
'EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
148llxHeader(
'', $title, $help_url,
'', 0, 0,
'',
'',
'',
'mod-margin page-tabs_thirdpartymargins');
150$param =
"&socid=".$socid;
151if ($limit > 0 && $limit !=
$conf->liste_limit) {
152 $param .=
'&limit='.((int) $limit);
154if ($dol_openinpopup) {
155 $param .=
'&dol_openinpopup='.urlencode($dol_openinpopup);
157if ($search_invoice_date_start) {
158 $param .=
'&search_invoice_date_start_day='.dol_print_date($search_invoice_date_start,
'%d').
'&search_invoice_date_start_month='.
dol_print_date($search_invoice_date_start,
'%m').
'&search_invoice_date_start_year='.
dol_print_date($search_invoice_date_start,
'%Y');
160if ($search_invoice_date_end) {
161 $param .=
'&search_invoice_date_end_day='.dol_print_date($search_invoice_date_end,
'%d').
'&search_invoice_date_end_month='.
dol_print_date($search_invoice_date_end,
'%m').
'&search_invoice_date_end_year='.
dol_print_date($search_invoice_date_end,
'%Y');
172 if (empty($dol_openinpopup)) {
177 print
dol_get_fiche_head($head,
'customer', $langs->trans(
"ThirdParty"), -1,
'company');
179 $linkback =
'<a href="'.DOL_URL_ROOT.
'/societe/list.php?restore_lastsearch_values=1">'.$langs->trans(
"BackToList").
'</a>';
181 dol_banner_tab($object,
'socid', $linkback, ($user->socid ? 0 : 1),
'rowid',
'nom');
183 print
'<div class="fichecenter">';
185 print
'<div class="underbanner clearboth"></div>';
186 print
'<table class="border tableforfield centpercent">';
189 print
'<tr><td class="titlefield">'.$langs->trans(
'NatureOfThirdParty').
'</td><td>';
194 print
'<tr><td class="titlefield">';
195 print $langs->trans(
'CustomerCode').
'</td><td colspan="3">';
197 $tmpcheck =
$object->check_codeclient();
198 if ($tmpcheck != 0 && $tmpcheck != -5) {
199 print
' <span class="error">('.$langs->trans(
"WrongCustomerCode").
')</span>';
204 if (((
isModEnabled(
"fournisseur") && $user->hasRight(
'fournisseur',
'lire') && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD')) || (
isModEnabled(
"supplier_order") && $user->hasRight(
'supplier_order',
'lire')) || (
isModEnabled(
"supplier_invoice") && $user->hasRight(
'supplier_invoice',
'lire'))) &&
$object->fournisseur) {
205 print
'<tr><td class="titlefield">';
206 print $langs->trans(
'SupplierCode').
'</td><td colspan="3">';
208 $tmpcheck =
$object->check_codefournisseur();
209 if ($tmpcheck != 0 && $tmpcheck != -5) {
210 print
' <span class="error">('.$langs->trans(
"WrongSupplierCode").
')</span>';
216 print
'<tr><td class="titlefield">'.$langs->trans(
"TotalMargin").
'</td><td colspan="3">';
217 print
'<span id="totalMargin" class="amount"></span>';
222 print
'<tr><td>'.$langs->trans(
"MarginRate").
'</td><td colspan="3">';
223 print
'<span id="marginRate"></span>';
229 print
'<tr><td>'.$langs->trans(
"MarkRate").
'</td><td colspan="3">';
230 print
'<span id="markRate"></span>';
237 print
'<div class="clearboth"></div>';
244 $sql =
"SELECT s.nom, s.rowid as socid, s.code_client,";
245 $sql .=
" f.rowid as facid, f.ref, f.total_ht,";
246 $sql .=
" f.datef, f.paye, f.fk_statut as statut, f.type,";
247 $sql .=
" sum(d.total_ht) as selling_price,";
248 $sql .=
" sum(d.qty * d.buy_price_ht * (d.situation_percent / 100)) as buying_price,";
249 $sql .=
" sum(abs(d.total_ht) - (d.buy_price_ht * d.qty * (d.situation_percent / 100))) as marge";
250 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s";
251 $sql .=
", ".MAIN_DB_PREFIX.
"facture as f";
252 $sql .=
", ".MAIN_DB_PREFIX.
"facturedet as d";
253 $sql .=
" WHERE f.fk_soc = s.rowid";
254 $sql .=
" AND f.fk_statut > 0";
255 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
256 $sql .=
" AND d.fk_facture = f.rowid";
257 $sql .=
" AND f.fk_soc = $socid";
258 $sql .=
" AND d.buy_price_ht IS NOT NULL";
262 $sql .=
" AND d.buy_price_ht <> 0";
264 if (!empty($search_invoice_date_start)) {
265 $sql .=
" AND f.datef >= '".$db->idate($search_invoice_date_start).
"'";
267 if (!empty($search_invoice_date_end)) {
268 $sql .=
" AND f.datef <= '".$db->idate($search_invoice_date_end).
"'";
270 $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";
275 $sql .=
$db->order($sortfield, $sortorder);
277 $sql .=
$db->plimit($limit + 1, $offset);
280 dol_syslog(
'margin:tabs:thirdpartyMargins.php', LOG_DEBUG);
281 $result =
$db->query($sql);
283 $num =
$db->num_rows($result);
285 print
'<form method="post" action="'.$_SERVER [
'PHP_SELF'].
'?socid='.$socid.
'" name="search_form">'.
"\n";
286 print
'<input type="hidden" name="token" value="'.newToken().
'">';
287 print
'<input type="hidden" name="dol_openinpopup" value="'.$dol_openinpopup.
'">';
288 if (!empty($sortfield)) {
289 print
'<input type="hidden" name="sortfield" value="'.$sortfield.
'"/>';
291 if (!empty($sortorder)) {
292 print
'<input type="hidden" name="sortorder" value="'.$sortorder.
'"/>';
296 print_barre_liste($langs->trans(
"MarginDetails"), $page, $_SERVER[
"PHP_SELF"], $param, $sortfield, $sortorder,
'', $num,
'',
'margin');
300 $parameters = array();
301 $reshook = $hookmanager->executeHooks(
'printFieldPreListTitle', $parameters, $object, $action);
302 if (empty($reshook)) {
303 $moreforfilter .= $hookmanager->resPrint;
305 $moreforfilter = $hookmanager->resPrint;
308 if (!empty($moreforfilter)) {
309 print
'<div class="liste_titre liste_titre_bydiv centpercent">';
310 print $moreforfilter;
314 $selectedfields =
'';
317 print
'<div class="div-table-responsive">';
318 print
'<table class="noborder centpercent">';
322 print
'<tr class="liste_titre liste_titre_filter">';
324 if (
$conf->main_checkbox_left_column) {
325 print
'<th class="liste_titre center maxwidthsearch">';
326 $searchpicto = $form->showFilterButtons(
'left');
332 print
'<th class="liste_titre">';
336 print
'<th class="liste_titre center">';
337 print
'<div class="nowrapfordate">';
338 print $form->selectDate($search_invoice_date_start ?: -1,
'search_invoice_date_start_', 0, 0, 1,
'', 1, 0, 0,
'',
'',
'',
'', 1,
'', $langs->trans(
'From'));
340 print
'<div class="nowrapfordate">';
341 print $form->selectDate($search_invoice_date_end ?: -1,
'search_invoice_date_end_', 0, 0, 1,
'', 1, 0, 0,
'',
'',
'',
'', 1,
'', $langs->trans(
'to'));
346 print
'<th class="liste_titre">';
350 print
'<th class="liste_titre">';
354 print
'<th class="liste_titre">';
359 print
'<th class="liste_titre">';
365 print
'<th class="liste_titre">';
370 print
'<th class="liste_titre">';
374 if (!
$conf->main_checkbox_left_column) {
375 print
'<th class="liste_titre center maxwidthsearch">';
376 $searchpicto = $form->showFilterButtons();
383 print
'<tr class="liste_titre">';
385 if (
$conf->main_checkbox_left_column) {
386 print_liste_field_titre($selectedfields, $_SERVER[
"PHP_SELF"],
"",
'', $param,
'', $sortfield, $sortorder,
'maxwidthsearch center ');
389 print_liste_field_titre(
"DateInvoice", $_SERVER[
"PHP_SELF"],
"f.datef",
"", $param,
'', $sortfield, $sortorder,
'center ');
390 print_liste_field_titre(
"SoldAmount", $_SERVER[
"PHP_SELF"],
"selling_price",
"", $param,
'', $sortfield, $sortorder,
'right ');
391 print_liste_field_titre(
"PurchasedAmount", $_SERVER[
"PHP_SELF"],
"buying_price",
"", $param,
'', $sortfield, $sortorder,
'right ');
392 print_liste_field_titre(
"Margin", $_SERVER[
"PHP_SELF"],
"marge",
"", $param,
'', $sortfield, $sortorder,
'right ');
394 print_liste_field_titre(
"MarginRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'', $sortfield, $sortorder,
'right ');
397 print_liste_field_titre(
"MarkRate", $_SERVER[
"PHP_SELF"],
"",
"", $param,
'', $sortfield, $sortorder,
'right ');
399 print_liste_field_titre(
"Status", $_SERVER[
"PHP_SELF"],
"f.paye,f.fk_statut",
"", $param,
'', $sortfield, $sortorder,
'right ');
401 if (!
$conf->main_checkbox_left_column) {
402 print_liste_field_titre($selectedfields, $_SERVER[
"PHP_SELF"],
"",
'', $param,
'', $sortfield, $sortorder,
'maxwidthsearch center ');
410 $imaxinloop = ($limit ? min($num, $limit) : $num);
411 while ($i < $imaxinloop) {
412 $objp =
$db->fetch_object($result);
414 $marginRate = ($objp->buying_price != 0) ? (100 * $objp->marge / $objp->buying_price) :
'';
415 $markRate = ($objp->selling_price != 0) ? (100 * $objp->marge / $objp->selling_price) :
'';
422 print
'<tr class="oddeven">';
424 if (
$conf->main_checkbox_left_column) {
425 print
'<td class="nowrap center">';
430 $invoicestatic->id = $objp->facid;
431 $invoicestatic->ref = $objp->ref;
432 $invoicestatic->type = $objp->type;
433 print $invoicestatic->getNomUrl(1);
435 print
"<td class=\"center\">";
437 print
"<td class=\"right amount\">".price(
price2num($objp->selling_price,
'MT')).
"</td>\n";
438 print
"<td class=\"right amount\">".price(
price2num(($objp->type == 2 ? -1 : 1) * $objp->buying_price,
'MT')).
"</td>\n";
439 print
"<td class=\"right amount\">".$sign.price(
price2num($objp->marge,
'MT')).
"</td>\n";
441 print
"<td class=\"right\">".(($marginRate ===
'') ?
'<span class="opacitymedium">'.$langs->trans(
"NA").
'</span>' : $sign.price(
price2num($marginRate,
'MT')).
"%").
"</td>\n";
444 print
"<td class=\"right\">".(($markRate ===
'') ?
'<span class="opacitymedium">'.$langs->trans(
"NA").
'</span>' :
price(
price2num($markRate,
'MT')).
"%").
"</td>\n";
446 print
'<td class="right">'.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5).
'</td>';
449 if (!
$conf->main_checkbox_left_column) {
450 print
'<td class="nowrap center">';
455 $cumul_vente += $objp->selling_price;
456 $cumul_achat += ($objp->type == 2 ? -1 : 1) * $objp->buying_price;
462 $totalMargin = $cumul_vente - $cumul_achat;
463 if ($totalMargin < 0) {
464 $marginRate = ($cumul_achat != 0) ? -1 * (100 * $totalMargin / $cumul_achat) :
'';
465 $markRate = ($cumul_vente != 0) ? -1 * (100 * $totalMargin / $cumul_vente) :
'';
467 $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) :
'';
468 $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) :
'';
472 print
'<tr class="liste_total">';
474 if (
$conf->main_checkbox_left_column) {
477 print
'<td colspan="'.$colspan.
'">'.$langs->trans(
'TotalMargin').
"</td>";
478 print
"<td class=\"right\">".price(
price2num($cumul_vente,
'MT')).
"</td>\n";
479 print
"<td class=\"right\">".price(
price2num($cumul_achat,
'MT')).
"</td>\n";
480 print
"<td class=\"right\">".price(
price2num($totalMargin,
'MT')).
"</td>\n";
482 print
"<td class=\"right\">".(($marginRate ===
'') ?
'<span class="opacitymedium">'.$langs->trans(
"NA").
'</span>' :
price(
price2num($marginRate,
'MT')).
"%").
"</td>\n";
485 print
"<td class=\"right\">".(($markRate ===
'') ?
'<span class="opacitymedium">'.$langs->trans(
"NA").
'</span>' :
price(
price2num($markRate,
'MT')).
"%").
"</td>\n";
487 print
'<td class="right"> </td>';
488 if (!
$conf->main_checkbox_left_column) {
508 <script type="text/javascript">
509 $(document).ready(function() {
510 $("#totalMargin").html(\''.
price(
price2num($totalMargin,
'MT')).
'\');
511 $(
"#marginRate").html(\
''.(($marginRate ===
'') ?
'<span class="opacitymedium">'.$langs->trans(
"NA").
'</span>' :
price(
price2num($marginRate,
'MT')).
"%").
'\');
512 $("#markRate").html(\''.(($markRate ===
'') ?
'<span class="opacitymedium">'.$langs->trans(
"NA").
'</span>' :
price(
price2num($markRate,
'MT')).
"%").
'\');
if(! $sortfield) if(! $sortorder) $object
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Class to manage invoices.
const TYPE_CREDIT_NOTE
Credit note invoice.
Class to manage third parties objects (customers, suppliers, prospects...)
societe_prepare_head(Societe $object, $subtabs='')
Return array of tabs to used on pages for third parties cards.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
dol_now($mode='gmt')
Return date for now.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
print_liste_field_titre($name, $file="", $field="", $begin="", $param="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
print_barre_liste($title, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $selectlimitsuffix=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.