23 require
'../../main.inc.php';
24 require_once DOL_DOCUMENT_ROOT.
'/core/lib/report.lib.php';
25 require_once DOL_DOCUMENT_ROOT.
'/core/lib/tax.lib.php';
26 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
27 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formother.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
31 $langs->loadLangs(array(
"products",
"categories",
"errors",
'accountancy'));
34 $socid =
GETPOST(
'socid',
'int');
36 if ($user->socid > 0) $socid = $user->socid;
37 if (!empty($conf->comptabilite->enabled)) $result =
restrictedArea($user,
'compta',
'',
'',
'resultat');
38 if (!empty($conf->accounting->enabled)) $result =
restrictedArea($user,
'accounting',
'',
'',
'comptarapport');
41 $modecompta = $conf->global->ACCOUNTING_MODE;
44 $sortorder = isset($_GET[
"sortorder"]) ? $_GET[
"sortorder"] : $_POST[
"sortorder"];
45 $sortfield = isset($_GET[
"sortfield"]) ? $_GET[
"sortfield"] : $_POST[
"sortfield"];
46 if (!$sortorder) $sortorder =
"asc";
47 if (!$sortfield) $sortfield =
"ref";
50 $selected_cat = (int)
GETPOST(
'search_categ',
'int');
51 $selected_soc = (int)
GETPOST(
'search_soc',
'int');
53 if (
GETPOST(
'subcat',
'alpha') ===
'yes') {
57 $selected_type =
GETPOST(
'search_type',
'int');
58 if ($selected_type ==
'') $selected_type = -1;
61 $hookmanager->initHooks(array(
'supplierturnoverbyprodservlist'));
66 $date_startyear =
GETPOST(
"date_startyear");
67 $date_startmonth =
GETPOST(
"date_startmonth");
68 $date_startday =
GETPOST(
"date_startday");
69 $date_endyear =
GETPOST(
"date_endyear");
70 $date_endmonth =
GETPOST(
"date_endmonth");
71 $date_endday =
GETPOST(
"date_endday");
74 $year_current = strftime(
"%Y",
dol_now());
75 $month_current = strftime(
"%m",
dol_now());
76 $year_start = $year_current;
78 $year_current = $year;
79 $month_current = strftime(
"%m",
dol_now());
85 if (empty($date_start) || empty($date_end))
91 $month_start =
GETPOST(
"month") ?
GETPOST(
"month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
92 $year_end = $year_start;
93 $month_end = $month_start;
96 if (!
GETPOST(
'year') && $month_start > $month_current)
101 $month_end = $month_start - 1;
102 if ($month_end < 1) $month_end = 12;
118 $year_start = $tmps[
'year'];
120 $year_end = $tmpe[
'year'];
121 $nbofyear = ($year_end - $year_start) + 1;
123 $commonparams = array();
124 if (!empty($modecompta)) $commonparams[
'modecompta'] = $modecompta;
125 if (!empty($sortorder)) $commonparams[
'sortorder'] = $sortorder;
126 if (!empty($sortfield)) $commonparams[
'sortfield'] = $sortfield;
128 $headerparams = array();
129 if (!empty($date_startyear)) $headerparams[
'date_startyear'] = $date_startyear;
130 if (!empty($date_startmonth)) $headerparams[
'date_startmonth'] = $date_startmonth;
131 if (!empty($date_startday)) $headerparams[
'date_startday'] = $date_startday;
132 if (!empty($date_endyear)) $headerparams[
'date_endyear'] = $date_endyear;
133 if (!empty($date_endmonth)) $headerparams[
'date_endmonth'] = $date_endmonth;
134 if (!empty($date_endday)) $headerparams[
'date_endday'] = $date_endday;
135 if (!empty($year)) $headerparams[
'year'] = $year;
136 if (!empty($month)) $headerparams[
'month'] = $month;
137 $headerparams[
'q'] = $q;
139 $tableparams = array();
140 if (!empty($selected_cat)) $tableparams[
'search_categ'] = $selected_cat;
141 if (!empty($selected_soc)) $tableparams[
'search_soc'] = $selected_soc;
142 if (!empty($selected_type)) $tableparams[
'search_type'] = $selected_type;
143 $tableparams[
'subcat'] = ($subcat ===
true) ?
'yes' :
'';
146 $allparams = array_merge($commonparams, $headerparams, $tableparams);
147 $headerparams = array_merge($commonparams, $headerparams);
148 $tableparams = array_merge($commonparams, $tableparams);
150 foreach ($allparams as $key => $value) {
151 $paramslink .=
'&'.$key.
'='.$value;
161 $form =
new Form($db);
165 if ($modecompta ==
"BOOKKEEPING") $modecompta =
"CREANCES-DETTES";
166 if ($modecompta ==
"BOOKKEEPINGCOLLECTED") $modecompta =
"RECETTES-DEPENSES";
169 if ($modecompta ==
"CREANCES-DETTES") {
170 $name = $langs->trans(
"PurchaseTurnover").
', '.$langs->trans(
"ByProductsAndServices");
171 $calcmode = $langs->trans(
"CalcModeDebt");
174 $description = $langs->trans(
"RulesPurchaseTurnoverDue");
176 } elseif ($modecompta ==
"RECETTES-DEPENSES")
178 $name = $langs->trans(
"PurchaseTurnoverCollected").
', '.$langs->trans(
"ByProductsAndServices");
179 $calcmode = $langs->trans(
"CalcModeEngagement");
181 $description = $langs->trans(
"RulesPurchaseTurnoverIn");
184 } elseif ($modecompta ==
"BOOKKEEPING")
186 } elseif ($modecompta ==
"BOOKKEEPINGCOLLECTED")
190 $period = $form->selectDate($date_start,
'date_start', 0, 0, 0,
'', 1, 0).
' - '.$form->selectDate($date_end,
'date_end', 0, 0, 0,
'', 1, 0);
191 if ($date_end ==
dol_time_plus_duree($date_start, 1,
'y') - 1) $periodlink =
'<a href="'.$_SERVER[
"PHP_SELF"].
'?year='.($year_start - 1).
'&modecompta='.$modecompta.
'">'.
img_previous().
'</a> <a href="'.
$_SERVER[
"PHP_SELF"].
'?year='.($year_start + 1).
'&modecompta='.$modecompta.
'">'.
img_next().
'</a>';
192 else $periodlink =
'';
194 report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $tableparams, $calcmode);
196 if (!empty($conf->accounting->enabled) && $modecompta !=
'BOOKKEEPING')
210 if ($modecompta ==
'CREANCES-DETTES')
212 $sql =
"SELECT DISTINCT p.rowid as rowid, p.ref as ref, p.label as label, p.fk_product_type as product_type,";
213 $sql .=
" SUM(l.total_ht) as amount, SUM(l.total_ttc) as amount_ttc,";
214 $sql .=
" SUM(CASE WHEN f.type = 2 THEN -l.qty ELSE l.qty END) as qty";
215 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
216 if ($selected_soc > 0) $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as soc ON (soc.rowid = f.fk_soc)";
217 $sql .=
",".MAIN_DB_PREFIX.
"facture_fourn_det as l";
218 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON l.fk_product = p.rowid";
219 if ($selected_cat === -2)
221 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"categorie_product as cp ON p.rowid = cp.fk_product";
222 } elseif ($selected_cat)
224 $sql .=
", ".MAIN_DB_PREFIX.
"categorie as c, ".MAIN_DB_PREFIX.
"categorie_product as cp";
226 $sql .=
" WHERE l.fk_facture_fourn = f.rowid";
227 $sql .=
" AND f.fk_statut in (1,2)";
228 $sql .=
" AND f.type IN (0,2)";
230 if ($date_start && $date_end) {
231 $sql .=
" AND f.datef >= '".$db->idate($date_start).
"' AND f.datef <= '".$db->idate($date_end).
"'";
233 if ($selected_type >= 0)
235 $sql .=
" AND l.product_type = ".$selected_type;
237 if ($selected_cat === -2)
239 $sql .=
" AND cp.fk_product is null";
240 } elseif ($selected_cat) {
241 $sql .=
" AND (c.rowid = ".$selected_cat;
242 if ($subcat) $sql .=
" OR c.fk_parent = ".$selected_cat;
244 $sql .=
" AND cp.fk_categorie = c.rowid AND cp.fk_product = p.rowid";
246 if ($selected_soc > 0) $sql .=
" AND soc.rowid=".$selected_soc;
247 $sql .=
" AND f.entity IN (".getEntity(
'supplier_invoice').
")";
248 $sql .=
" GROUP BY p.rowid, p.ref, p.label, p.fk_product_type";
249 $sql .= $db->order($sortfield, $sortorder);
251 dol_syslog(
"supplier_turnover_by_prodserv", LOG_DEBUG);
252 $result = $db->query($sql);
254 $num = $db->num_rows($result);
257 $obj = $db->fetch_object($result);
258 $amount_ht[$obj->rowid] = $obj->amount;
259 $amount[$obj->rowid] = $obj->amount_ttc;
260 $qty[$obj->rowid] = $obj->qty;
261 $name[$obj->rowid] = $obj->ref.
' - '.$obj->label;
262 $type[$obj->rowid] = $obj->product_type;
263 $catotal_ht += $obj->amount;
264 $catotal += $obj->amount_ttc;
265 $qtytotal += $obj->qty;
274 print '<form method="POST" action="'.$_SERVER[
"PHP_SELF"].
'">';
275 print '<input type="hidden" name="token" value="'.newToken().
'">'.
"\n";
277 foreach ($headerparams as $key => $value)
279 print '<input type="hidden" name="'.$key.
'" value="'.$value.
'">';
284 print '<div class="div-table-responsive">';
285 print '<table class="tagtable liste'.($moreforfilter ?
" listwithfilterbefore" :
"").
'">'.
"\n";
288 print '<tr class="liste_titre">';
290 print $langs->trans(
"Category").
': '.$formother->select_categories(Categorie::TYPE_PRODUCT, $selected_cat,
'search_categ',
true);
292 print $langs->trans(
"SubCats").
'? ';
293 print
'<input type="checkbox" name="subcat" value="yes"';
300 print $langs->trans(
"Type").
': ';
301 $form->select_type_of_lines(isset($selected_type) ? $selected_type : -1,
'search_type', 1, 1, 1);
305 print $langs->trans(
"ThirdParty").
': '.$form->select_thirdparty_list($selected_soc,
'search_soc',
'', 1);
308 print
'<td colspan="5" class="right">';
309 print
'<input type="image" class="liste_titre" name="button_search" src="'.img_picto($langs->trans(
"Search"),
'search.png',
'',
'', 1).
'" value="'.
dol_escape_htmltag($langs->trans(
"Search")).
'" title="'.
dol_escape_htmltag($langs->trans(
"Search")).
'">';
313 print
"<tr class=\"liste_titre\">";
315 $langs->trans(
"Product"),
325 $langs->trans(
'Quantity'),
335 $langs->trans(
"Percentage"),
345 $langs->trans(
'AmountHT'),
355 $langs->trans(
"AmountTTC"),
365 $langs->trans(
"Percentage"),
377 foreach ($name as $key=>$value) {
378 print
'<tr class="oddeven">';
382 $fullname = $name[$key];
384 $linkname =
'<a href="'.DOL_URL_ROOT.
'/product/card.php?id='.$key.
'">'.
img_object($langs->trans(
"ShowProduct"), $type[$key] == 0 ?
'product' :
'service').
' '.$fullname.
'</a>';
386 $linkname = $langs->trans(
"PaymentsNotLinkedToProduct");
392 print
'<td class="right">';
397 print
'<td class="right">'.($qtytotal > 0 ? round(100 * $qty[$key] / $qtytotal, 2).
'%' :
' ').
'</td>';
400 print
'<td class="right">';
401 print
price($amount_ht[$key]);
406 print
'<td class="right">';
407 print
price($amount[$key]);
412 print
'<td class="right">'.($catotal > 0 ? round(100 * $amount[$key] / $catotal, 2).
'%' :
' ').
'</td>';
421 print
'<tr class="liste_total">';
422 print
'<td>'.$langs->trans(
"Total").
'</td>';
423 print
'<td class="right">'.$qtytotal.
'</td>';
424 print
'<td class="right">100%</td>';
425 print
'<td class="right">'.price($catotal_ht).
'</td>';
426 print
'<td class="right">'.price($catotal).
'</td>';
427 print
'<td class="right">100%</td>';
441 print
'<br>'.$langs->trans(
"TurnoverPerProductInCommitmentAccountingNotRelevant").
'<br>';
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_now($mode= 'auto')
Return date for now.
report_header($reportname, $notused, $period, $periodlink, $description, $builddate, $exportlink= '', $moreparam=array(), $calcmode= '', $varlink= '')
Show header of a report.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
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...
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_getdate($timestamp, $fast=false, $forcetimezone= '')
Return an array with locale date info.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
print $_SERVER["PHP_SELF"]
Edit parameters.
img_next($titlealt= 'default', $moreatt= '')
Show next logo.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
print
Draft customers invoices.
img_previous($titlealt= 'default', $moreatt= '')
Show previous logo.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_time_plus_duree($time, $duration_value, $duration_unit)
Add a delay to a date.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin= '1', $morecss= '', $textfordropdown= '')
Show information for admin users or standard users.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $keepmoretags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...