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';
30 $langs->loadLangs(array(
"companies",
"bills",
"products",
"margins"));
33 $socid =
GETPOST(
'socid',
'int');
34 if (!empty($user->socid)) {
35 $socid = $user->socid;
38 $limit =
GETPOST(
'limit',
'int') ?
GETPOST(
'limit',
'int') : $conf->liste_limit;
39 $sortfield =
GETPOST(
'sortfield',
'aZ09comma');
40 $sortorder =
GETPOST(
'sortorder',
'aZ09comma');
42 if (empty($page) || $page == -1) {
45 $offset = $limit * $page;
46 $pageprev = $page - 1;
47 $pagenext = $page + 1;
52 $sortfield =
"f.datef";
57 $object->fetch($socid);
61 $hookmanager->initHooks(array(
'thirdpartymargins',
'globalcard'));
65 if (empty($user->rights->margins->liretous)) {
74 $parameters = array(
'id'=>$socid);
75 $reshook = $hookmanager->executeHooks(
'doActions', $parameters, $object, $action);
86 $invoicestatic =
new Facture($db);
89 $title = $langs->trans(
"ThirdParty").
' - '.$langs->trans(
"Margins");
90 if (!empty($conf->global->MAIN_HTML_TITLE) && preg_match(
'/thirdpartynameonly/', $conf->global->MAIN_HTML_TITLE) && $object->name) {
91 $title = $object->name.
' - '.$langs->trans(
"Files");
93 $help_url =
'EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
98 $object->fetch($socid);
106 print
dol_get_fiche_head($head,
'margin', $langs->trans(
"ThirdParty"), -1,
'company');
108 $linkback =
'<a href="'.DOL_URL_ROOT.
'/societe/list.php?restore_lastsearch_values=1">'.$langs->trans(
"BackToList").
'</a>';
110 dol_banner_tab($object,
'socid', $linkback, ($user->socid ? 0 : 1),
'rowid',
'nom');
112 print
'<div class="fichecenter">';
114 print
'<div class="underbanner clearboth"></div>';
115 print
'<table class="border tableforfield" width="100%">';
118 print
'<tr><td class="titlefield">'.$langs->trans(
'NatureOfThirdParty').
'</td><td>';
119 print $object->getTypeUrl(1);
122 if ($object->client) {
123 print
'<tr><td class="titlefield">';
124 print $langs->trans(
'CustomerCode').
'</td><td colspan="3">';
126 $tmpcheck = $object->check_codeclient();
127 if ($tmpcheck != 0 && $tmpcheck != -5) {
128 print
' <span class="error">('.$langs->trans(
"WrongCustomerCode").
')</span>';
133 if (((
isModEnabled(
"fournisseur") && !empty($user->rights->fournisseur->lire) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || (
isModEnabled(
"supplier_order") && !empty($user->rights->supplier_order->lire)) || (
isModEnabled(
"supplier_invoice") && !empty($user->rights->supplier_invoice->lire))) && $object->fournisseur) {
134 print
'<tr><td class="titlefield">';
135 print $langs->trans(
'SupplierCode').
'</td><td colspan="3">';
137 $tmpcheck = $object->check_codefournisseur();
138 if ($tmpcheck != 0 && $tmpcheck != -5) {
139 print
' <span class="error">('.$langs->trans(
"WrongSupplierCode").
')</span>';
145 print
'<tr><td class="titlefield">'.$langs->trans(
"TotalMargin").
'</td><td colspan="3">';
146 print
'<span id="totalMargin" class="amount"></span>';
150 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
151 print
'<tr><td>'.$langs->trans(
"MarginRate").
'</td><td colspan="3">';
152 print
'<span id="marginRate"></span>';
157 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
158 print
'<tr><td>'.$langs->trans(
"MarkRate").
'</td><td colspan="3">';
159 print
'<span id="markRate"></span>';
166 print
'<div style="clear:both"></div>';
172 $sql =
"SELECT distinct s.nom, s.rowid as socid, s.code_client,";
173 $sql .=
" f.rowid as facid, f.ref, f.total_ht,";
174 $sql .=
" f.datef, f.paye, f.fk_statut as statut, f.type,";
175 $sql .=
" sum(d.total_ht) as selling_price,";
176 $sql .=
" sum(d.qty * d.buy_price_ht * (d.situation_percent / 100)) as buying_price,";
177 $sql .=
" sum(abs(d.total_ht) - (d.buy_price_ht * d.qty * (d.situation_percent / 100))) as marge";
178 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s";
179 $sql .=
", ".MAIN_DB_PREFIX.
"facture as f";
180 $sql .=
", ".MAIN_DB_PREFIX.
"facturedet as d";
181 $sql .=
" WHERE f.fk_soc = s.rowid";
182 $sql .=
" AND f.fk_statut > 0";
183 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
184 $sql .=
" AND d.fk_facture = f.rowid";
185 $sql .=
" AND f.fk_soc = $socid";
186 $sql .=
" AND d.buy_price_ht IS NOT NULL";
189 if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 2) {
190 $sql .=
" AND d.buy_price_ht <> 0";
192 $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";
193 $sql .= $db->order($sortfield, $sortorder);
197 dol_syslog(
'margin:tabs:thirdpartyMargins.php', LOG_DEBUG);
198 $result = $db->query($sql);
200 $num = $db->num_rows($result);
202 print_barre_liste($langs->trans(
"MarginDetails"), $page, $_SERVER[
"PHP_SELF"],
"&socid=".$object->id, $sortfield, $sortorder,
'', $num, $num,
'');
205 print
'<div class="div-table-responsive">';
206 print
"<table class=\"noborder\" width=\"100%\">";
208 print
'<tr class="liste_titre">';
209 print_liste_field_titre(
"Invoice", $_SERVER[
"PHP_SELF"],
"f.ref",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder);
210 print_liste_field_titre(
"DateInvoice", $_SERVER[
"PHP_SELF"],
"f.datef",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'center ');
211 print_liste_field_titre(
"SoldAmount", $_SERVER[
"PHP_SELF"],
"selling_price",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'right ');
212 print_liste_field_titre(
"PurchasedAmount", $_SERVER[
"PHP_SELF"],
"buying_price",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'right ');
213 print_liste_field_titre(
"Margin", $_SERVER[
"PHP_SELF"],
"marge",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'right ');
214 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
215 print_liste_field_titre(
"MarginRate", $_SERVER[
"PHP_SELF"],
"",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'right ');
217 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
218 print_liste_field_titre(
"MarkRate", $_SERVER[
"PHP_SELF"],
"",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'right ');
220 print_liste_field_titre(
"Status", $_SERVER[
"PHP_SELF"],
"f.paye,f.fk_statut",
"",
"&socid=".$_REQUEST[
"socid"],
'', $sortfield, $sortorder,
'right ');
228 $objp = $db->fetch_object($result);
230 $marginRate = ($objp->buying_price != 0) ? (100 * $objp->marge / $objp->buying_price) :
'';
231 $markRate = ($objp->selling_price != 0) ? (100 * $objp->marge / $objp->selling_price) :
'';
238 print
'<tr class="oddeven">';
240 $invoicestatic->id = $objp->facid;
241 $invoicestatic->ref = $objp->ref;
242 print $invoicestatic->getNomUrl(1);
244 print
"<td class=\"center\">";
246 print
"<td class=\"right amount\">".price(
price2num($objp->selling_price,
'MT')).
"</td>\n";
247 print
"<td class=\"right amount\">".price(
price2num(($objp->type == 2 ? -1 : 1) * $objp->buying_price,
'MT')).
"</td>\n";
248 print
"<td class=\"right amount\">".$sign.price(
price2num($objp->marge,
'MT')).
"</td>\n";
249 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
250 print
"<td class=\"right\">".(($marginRate ===
'') ?
'n/a' : $sign.price(
price2num($marginRate,
'MT')).
"%").
"</td>\n";
252 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
253 print
"<td class=\"right\">".(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
"</td>\n";
255 print
'<td class="right">'.$invoicestatic->LibStatut($objp->paye, $objp->statut, 5).
'</td>';
258 $cumul_vente += $objp->selling_price;
259 $cumul_achat += ($objp->type == 2 ? -1 : 1) * $objp->buying_price;
265 $totalMargin = $cumul_vente - $cumul_achat;
266 if ($totalMargin < 0) {
267 $marginRate = ($cumul_achat != 0) ?-1 * (100 * $totalMargin / $cumul_achat) :
'';
268 $markRate = ($cumul_vente != 0) ?-1 * (100 * $totalMargin / $cumul_vente) :
'';
270 $marginRate = ($cumul_achat != 0) ? (100 * $totalMargin / $cumul_achat) :
'';
271 $markRate = ($cumul_vente != 0) ? (100 * $totalMargin / $cumul_vente) :
'';
275 print
'<tr class="liste_total">';
276 print
'<td colspan=2>'.$langs->trans(
'TotalMargin').
"</td>";
277 print
"<td class=\"right\">".price(
price2num($cumul_vente,
'MT')).
"</td>\n";
278 print
"<td class=\"right\">".price(
price2num($cumul_achat,
'MT')).
"</td>\n";
279 print
"<td class=\"right\">".price(
price2num($totalMargin,
'MT')).
"</td>\n";
280 if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
281 print
"<td class=\"right\">".(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
"</td>\n";
283 if (!empty($conf->global->DISPLAY_MARK_RATES)) {
284 print
"<td class=\"right\">".(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
"</td>\n";
286 print
'<td class="right"> </td>';
302 <script type="text/javascript">
303 $(document).ready(function() {
304 $("#totalMargin").html("'.
price(
price2num($totalMargin,
'MT')).
'");
305 $("#marginRate").html("'.(($marginRate ===
'') ?
'n/a' :
price(
price2num($marginRate,
'MT')).
"%").
'");
306 $("#markRate").html("'.(($markRate ===
'') ?
'n/a' :
price(
price2num($markRate,
'MT')).
"%").
'");
if(GETPOST('button_removefilter_x', 'alpha')||GETPOST('button_removefilter.x', 'alpha')||GETPOST('button_removefilter', 'alpha')) if(GETPOST('button_search_x', 'alpha')||GETPOST('button_search.x', 'alpha')||GETPOST('button_search', 'alpha')) if($action=="save" &&empty($cancel)) $help_url
View.
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
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)
Return array of tabs to used on pages for third parties cards.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
showValueWithClipboardCPButton($valuetocopy, $showonlyonhover=1, $texttoshow='')
Create a button to copy $valuetocopy in the clipboard (for copy and paste feature).
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_get_fiche_end($notab=0)
Return tab footer of a card.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
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.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print_barre_liste($titre, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $hideselectlimit=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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.