26 require
'../../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.
'/core/lib/report.lib.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/lib/tax.lib.php';
29 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formother.class.php';
31 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
34 $langs->loadLangs(array(
"products",
"categories",
"errors",
'accountancy'));
37 $socid =
GETPOST(
'socid',
'int');
39 if ($user->socid > 0) $socid = $user->socid;
40 if (!empty($conf->comptabilite->enabled)) $result =
restrictedArea($user,
'compta',
'',
'',
'resultat');
41 if (!empty($conf->accounting->enabled)) $result =
restrictedArea($user,
'accounting',
'',
'',
'comptarapport');
44 $modecompta = $conf->global->ACCOUNTING_MODE;
47 $sortorder = isset($_GET[
"sortorder"]) ? $_GET[
"sortorder"] : $_POST[
"sortorder"];
48 $sortfield = isset($_GET[
"sortfield"]) ? $_GET[
"sortfield"] : $_POST[
"sortfield"];
49 if (!$sortorder) $sortorder =
"asc";
50 if (!$sortfield) $sortfield =
"ref";
53 $selected_cat = (int)
GETPOST(
'search_categ',
'int');
54 $selected_soc = (int)
GETPOST(
'search_soc',
'int');
56 if (
GETPOST(
'subcat',
'alpha') ===
'yes') {
62 $selected_type =
GETPOST(
'search_type',
'int');
63 if ($selected_type ==
'') $selected_type = -1;
66 $hookmanager->initHooks(array(
'cabyprodservlist'));
71 $date_startyear =
GETPOST(
"date_startyear");
72 $date_startmonth =
GETPOST(
"date_startmonth");
73 $date_startday =
GETPOST(
"date_startday");
74 $date_endyear =
GETPOST(
"date_endyear");
75 $date_endmonth =
GETPOST(
"date_endmonth");
76 $date_endday =
GETPOST(
"date_endday");
79 $year_current = strftime(
"%Y",
dol_now());
80 $month_current = strftime(
"%m",
dol_now());
81 $year_start = $year_current;
83 $year_current = $year;
84 $month_current = strftime(
"%m",
dol_now());
90 if (empty($date_start) || empty($date_end))
96 $month_start =
GETPOST(
"month") ?
GETPOST(
"month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
97 $year_end = $year_start;
98 $month_end = $month_start;
101 if (!
GETPOST(
'year') && $month_start > $month_current)
106 $month_end = $month_start - 1;
107 if ($month_end < 1) $month_end = 12;
123 $year_start = $tmps[
'year'];
125 $year_end = $tmpe[
'year'];
126 $nbofyear = ($year_end - $year_start) + 1;
128 $commonparams = array();
129 if (!empty($modecompta)) $commonparams[
'modecompta'] = $modecompta;
130 if (!empty($sortorder)) $commonparams[
'sortorder'] = $sortorder;
131 if (!empty($sortfield)) $commonparams[
'sortfield'] = $sortfield;
133 $headerparams = array();
134 if (!empty($date_startyear)) $headerparams[
'date_startyear'] = $date_startyear;
135 if (!empty($date_startmonth)) $headerparams[
'date_startmonth'] = $date_startmonth;
136 if (!empty($date_startday)) $headerparams[
'date_startday'] = $date_startday;
137 if (!empty($date_endyear)) $headerparams[
'date_endyear'] = $date_endyear;
138 if (!empty($date_endmonth)) $headerparams[
'date_endmonth'] = $date_endmonth;
139 if (!empty($date_endday)) $headerparams[
'date_endday'] = $date_endday;
140 if (!empty($year)) $headerparams[
'year'] = $year;
141 if (!empty($month)) $headerparams[
'month'] = $month;
142 $headerparams[
'q'] = $q;
144 $tableparams = array();
145 if (!empty($selected_cat)) $tableparams[
'search_categ'] = $selected_cat;
146 if (!empty($selected_soc)) $tableparams[
'search_soc'] = $selected_soc;
147 if (!empty($selected_type)) $tableparams[
'search_type'] = $selected_type;
148 $tableparams[
'subcat'] = ($subcat ===
true) ?
'yes' :
'';
151 $allparams = array_merge($commonparams, $headerparams, $tableparams);
152 $headerparams = array_merge($commonparams, $headerparams);
153 $tableparams = array_merge($commonparams, $tableparams);
155 foreach ($allparams as $key => $value) {
156 $paramslink .=
'&'.$key.
'='.$value;
166 $form =
new Form($db);
170 if ($modecompta ==
"BOOKKEEPING") $modecompta =
"CREANCES-DETTES";
171 if ($modecompta ==
"BOOKKEEPINGCOLLECTED") $modecompta =
"RECETTES-DEPENSES";
174 if ($modecompta ==
"CREANCES-DETTES") {
175 $name = $langs->trans(
"Turnover").
', '.$langs->trans(
"ByProductsAndServices");
176 $calcmode = $langs->trans(
"CalcModeDebt");
179 $description = $langs->trans(
"RulesCADue");
180 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
181 $description .= $langs->trans(
"DepositsAreNotIncluded");
183 $description .= $langs->trans(
"DepositsAreIncluded");
187 } elseif ($modecompta ==
"RECETTES-DEPENSES")
189 $name = $langs->trans(
"TurnoverCollected").
', '.$langs->trans(
"ByProductsAndServices");
190 $calcmode = $langs->trans(
"CalcModeEngagement");
193 $description = $langs->trans(
"RulesCAIn");
194 $description .= $langs->trans(
"DepositsAreIncluded");
197 } elseif ($modecompta ==
"BOOKKEEPING")
199 } elseif ($modecompta ==
"BOOKKEEPINGCOLLECTED")
203 $period = $form->selectDate($date_start,
'date_start', 0, 0, 0,
'', 1, 0).
' - '.$form->selectDate($date_end,
'date_end', 0, 0, 0,
'', 1, 0);
204 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>';
205 else $periodlink =
'';
207 report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $tableparams, $calcmode);
209 if (!empty($conf->accounting->enabled) && $modecompta !=
'BOOKKEEPING')
223 if ($modecompta ==
'CREANCES-DETTES')
225 $sql =
"SELECT DISTINCT p.rowid as rowid, p.ref as ref, p.label as label, p.fk_product_type as product_type,";
226 $sql .=
" SUM(l.total_ht) as amount, SUM(l.total_ttc) as amount_ttc,";
227 $sql .=
" SUM(CASE WHEN f.type = 2 THEN -l.qty ELSE l.qty END) as qty";
228 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
229 if ($selected_soc > 0) $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as soc ON (soc.rowid = f.fk_soc)";
230 $sql .=
",".MAIN_DB_PREFIX.
"facturedet as l";
231 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON l.fk_product = p.rowid";
232 if ($selected_cat === -2) {
233 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"categorie_product as cp ON p.rowid = cp.fk_product";
234 } elseif ($selected_cat) {
235 $sql .=
", ".MAIN_DB_PREFIX.
"categorie as c, ".MAIN_DB_PREFIX.
"categorie_product as cp";
237 $sql .=
" WHERE l.fk_facture = f.rowid";
238 $sql .=
" AND f.fk_statut in (1,2)";
239 $sql .=
" AND l.product_type in (0,1)";
240 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
241 $sql .=
" AND f.type IN (0,1,2,5)";
243 $sql .=
" AND f.type IN (0,1,2,3,5)";
245 if ($date_start && $date_end) {
246 $sql .=
" AND f.datef >= '".$db->idate($date_start).
"' AND f.datef <= '".$db->idate($date_end).
"'";
248 if ($selected_type >= 0)
250 $sql .=
" AND l.product_type = ".$selected_type;
252 if ($selected_cat === -2) {
253 $sql .=
" AND cp.fk_product is null";
254 } elseif ($selected_cat) {
256 $TListOfCats = $categorie->get_full_arbo(
'product', $selected_cat, 1);
259 foreach ($TListOfCats as $key => $cat)
261 if ($key !== 0) $listofcatsql .=
",";
262 $listofcatsql .= $cat[
'rowid'];
266 $sql .=
" AND (p.rowid IN ";
267 $sql .=
" (SELECT fk_product FROM ".MAIN_DB_PREFIX.
"categorie_product cp WHERE ";
268 if ($subcat) $sql .=
"cp.fk_categorie IN (".$listofcatsql.
")";
269 else $sql .=
"cp.fk_categorie = ".$selected_cat;
272 if ($selected_soc > 0) $sql .=
" AND soc.rowid=".$selected_soc;
273 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
274 $sql .=
" GROUP BY p.rowid, p.ref, p.label, p.fk_product_type";
275 $sql .= $db->order($sortfield, $sortorder);
278 $result = $db->query($sql);
280 $num = $db->num_rows($result);
283 $obj = $db->fetch_object($result);
284 $amount_ht[$obj->rowid] = $obj->amount;
285 $amount[$obj->rowid] = $obj->amount_ttc;
286 $qty[$obj->rowid] = $obj->qty;
287 $name[$obj->rowid] = $obj->ref.
' - '.$obj->label;
288 $type[$obj->rowid] = $obj->product_type;
289 $catotal_ht += $obj->amount;
290 $catotal += $obj->amount_ttc;
291 $qtytotal += $obj->qty;
300 print '<form method="POST" action="'.$_SERVER[
"PHP_SELF"].
'">';
301 print '<input type="hidden" name="token" value="'.newToken().
'">'.
"\n";
303 foreach ($headerparams as $key => $value)
305 print '<input type="hidden" name="'.$key.
'" value="'.$value.
'">';
310 print '<div class="div-table-responsive">';
311 print '<table class="tagtable liste'.($moreforfilter ?
" listwithfilterbefore" :
"").
'">'.
"\n";
314 print '<tr class="liste_titre">';
316 print $langs->trans(
"Category").
': '.$formother->select_categories(Categorie::TYPE_PRODUCT, $selected_cat,
'search_categ',
true);
318 print $langs->trans(
"SubCats").
'? ';
319 print
'<input type="checkbox" name="subcat" value="yes"';
326 print $langs->trans(
"Type").
': ';
327 $form->select_type_of_lines(isset($selected_type) ? $selected_type : -1,
'search_type', 1, 1, 1);
331 print $langs->trans(
"ThirdParty").
': '.$form->select_thirdparty_list($selected_soc,
'search_soc',
'', 1);
334 print
'<td colspan="5" class="right">';
335 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")).
'">';
339 print
"<tr class=\"liste_titre\">";
341 $langs->trans(
"Product"),
351 $langs->trans(
'Quantity'),
361 $langs->trans(
"Percentage"),
371 $langs->trans(
'AmountHT'),
381 $langs->trans(
"AmountTTC"),
391 $langs->trans(
"Percentage"),
403 foreach ($name as $key=>$value) {
404 print
'<tr class="oddeven">';
408 $fullname = $name[$key];
410 $linkname =
'<a href="'.DOL_URL_ROOT.
'/product/card.php?id='.$key.
'">'.
img_object($langs->trans(
"ShowProduct"), $type[$key] == 0 ?
'product' :
'service').
' '.$fullname.
'</a>';
412 $linkname = $langs->trans(
"PaymentsNotLinkedToProduct");
418 print
'<td class="right">';
423 print
'<td class="right">'.($qtytotal > 0 ? round(100 * $qty[$key] / $qtytotal, 2).
'%' :
' ').
'</td>';
426 print
'<td class="right">';
432 print
price($amount_ht[$key]);
437 print
'<td class="right">';
443 print
price($amount[$key]);
448 print
'<td class="right">'.($catotal > 0 ? round(100 * $amount[$key] / $catotal, 2).
'%' :
' ').
'</td>';
457 print
'<tr class="liste_total">';
458 print
'<td>'.$langs->trans(
"Total").
'</td>';
459 print
'<td class="right">'.$qtytotal.
'</td>';
460 print
'<td class="right">100%</td>';
461 print
'<td class="right">'.price($catotal_ht).
'</td>';
462 print
'<td class="right">'.price($catotal).
'</td>';
463 print
'<td class="right">100%</td>';
477 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.
Class to manage categories.
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...