1<?php 2/* Copyright (C) 2013 Antoine Iauch <aiauch@gpcsolutions.fr> 3 * Copyright (C) 2013-2016 Laurent Destailleur <eldy@users.sourceforge.net> 4 * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr> 5 * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <https://www.gnu.org/licenses/>. 19 */ 20 21/** 22 * \file htdocs/compta/stats/cabyprodserv.php 23 * \brief Page reporting TO by Products & Services 24 */ 25 26require '../../main.inc.php'; 27require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php'; 28require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php'; 29require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; 30require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; 31require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; 32 33// Load translation files required by the page 34$langs->loadLangs(array("products", "categories", "errors", 'accountancy')); 35 36// Security pack (data & check) 37$socid = GETPOST('socid', 'int'); 38 39if ($user->socid > 0) $socid = $user->socid; 40if (!empty($conf->comptabilite->enabled)) $result = restrictedArea($user, 'compta', '', '', 'resultat'); 41if (!empty($conf->accounting->enabled)) $result = restrictedArea($user, 'accounting', '', '', 'comptarapport'); 42 43// Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES') 44$modecompta = $conf->global->ACCOUNTING_MODE; 45if (GETPOST("modecompta")) $modecompta = GETPOST("modecompta"); 46 47$sortorder = isset($_GET["sortorder"]) ? $_GET["sortorder"] : $_POST["sortorder"]; 48$sortfield = isset($_GET["sortfield"]) ? $_GET["sortfield"] : $_POST["sortfield"]; 49if (!$sortorder) $sortorder = "asc"; 50if (!$sortfield) $sortfield = "ref"; 51 52// Category 53$selected_cat = (int) GETPOST('search_categ', 'int'); 54$selected_soc = (int) GETPOST('search_soc', 'int'); 55$subcat = false; 56if (GETPOST('subcat', 'alpha') === 'yes') { 57 $subcat = true; 58} 59$categorie = new Categorie($db); 60 61// product/service 62$selected_type = GETPOST('search_type', 'int'); 63if ($selected_type == '') $selected_type = -1; 64 65// Hook 66$hookmanager->initHooks(array('cabyprodservlist')); 67 68// Date range 69$year = GETPOST("year"); 70$month = GETPOST("month"); 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"); 77if (empty($year)) 78{ 79 $year_current = strftime("%Y", dol_now()); 80 $month_current = strftime("%m", dol_now()); 81 $year_start = $year_current; 82} else { 83 $year_current = $year; 84 $month_current = strftime("%m", dol_now()); 85 $year_start = $year; 86} 87$date_start = dol_mktime(0, 0, 0, GETPOST("date_startmonth"), GETPOST("date_startday"), GETPOST("date_startyear")); 88$date_end = dol_mktime(23, 59, 59, GETPOST("date_endmonth"), GETPOST("date_endday"), GETPOST("date_endyear")); 89// Quarter 90if (empty($date_start) || empty($date_end)) // We define date_start and date_end 91{ 92 $q = GETPOST("q", "int"); 93 if (empty($q)) 94 { 95 // We define date_start and 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; 99 if (!GETPOST("month")) // If month not forced 100 { 101 if (!GETPOST('year') && $month_start > $month_current) 102 { 103 $year_start--; 104 $year_end--; 105 } 106 $month_end = $month_start - 1; 107 if ($month_end < 1) $month_end = 12; 108 else $year_end++; 109 } 110 $date_start = dol_get_first_day($year_start, $month_start, false); $date_end = dol_get_last_day($year_end, $month_end, false); 111 } else { 112 if ($q == 1) { $date_start = dol_get_first_day($year_start, 1, false); $date_end = dol_get_last_day($year_start, 3, false); } 113 if ($q == 2) { $date_start = dol_get_first_day($year_start, 4, false); $date_end = dol_get_last_day($year_start, 6, false); } 114 if ($q == 3) { $date_start = dol_get_first_day($year_start, 7, false); $date_end = dol_get_last_day($year_start, 9, false); } 115 if ($q == 4) { $date_start = dol_get_first_day($year_start, 10, false); $date_end = dol_get_last_day($year_start, 12, false); } 116 } 117} else { 118 // TODO We define q 119} 120 121// $date_start and $date_end are defined. We force $year_start and $nbofyear 122$tmps = dol_getdate($date_start); 123$year_start = $tmps['year']; 124$tmpe = dol_getdate($date_end); 125$year_end = $tmpe['year']; 126$nbofyear = ($year_end - $year_start) + 1; 127 128$commonparams = array(); 129if (!empty($modecompta)) $commonparams['modecompta'] = $modecompta; 130if (!empty($sortorder)) $commonparams['sortorder'] = $sortorder; 131if (!empty($sortfield)) $commonparams['sortfield'] = $sortfield; 132 133$headerparams = array(); 134if (!empty($date_startyear)) $headerparams['date_startyear'] = $date_startyear; 135if (!empty($date_startmonth)) $headerparams['date_startmonth'] = $date_startmonth; 136if (!empty($date_startday)) $headerparams['date_startday'] = $date_startday; 137if (!empty($date_endyear)) $headerparams['date_endyear'] = $date_endyear; 138if (!empty($date_endmonth)) $headerparams['date_endmonth'] = $date_endmonth; 139if (!empty($date_endday)) $headerparams['date_endday'] = $date_endday; 140if (!empty($year)) $headerparams['year'] = $year; 141if (!empty($month)) $headerparams['month'] = $month; 142$headerparams['q'] = $q; 143 144$tableparams = array(); 145if (!empty($selected_cat)) $tableparams['search_categ'] = $selected_cat; 146if (!empty($selected_soc)) $tableparams['search_soc'] = $selected_soc; 147if (!empty($selected_type)) $tableparams['search_type'] = $selected_type; 148$tableparams['subcat'] = ($subcat === true) ? 'yes' : ''; 149 150// Adding common parameters 151$allparams = array_merge($commonparams, $headerparams, $tableparams); 152$headerparams = array_merge($commonparams, $headerparams); 153$tableparams = array_merge($commonparams, $tableparams); 154 155foreach ($allparams as $key => $value) { 156 $paramslink .= '&'.$key.'='.$value; 157} 158 159 160/* 161 * View 162 */ 163 164llxHeader(); 165 166$form = new Form($db); 167$formother = new FormOther($db); 168 169// TODO Report from bookkeeping not yet available, so we switch on report on business events 170if ($modecompta == "BOOKKEEPING") $modecompta = "CREANCES-DETTES"; 171if ($modecompta == "BOOKKEEPINGCOLLECTED") $modecompta = "RECETTES-DEPENSES"; 172 173// Show report header 174if ($modecompta == "CREANCES-DETTES") { 175 $name = $langs->trans("Turnover").', '.$langs->trans("ByProductsAndServices"); 176 $calcmode = $langs->trans("CalcModeDebt"); 177 //$calcmode.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year_start.'&modecompta=RECETTES-DEPENSES">','</a>').')'; 178 179 $description = $langs->trans("RulesCADue"); 180 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) { 181 $description .= $langs->trans("DepositsAreNotIncluded"); 182 } else { 183 $description .= $langs->trans("DepositsAreIncluded"); 184 } 185 186 $builddate = dol_now(); 187} elseif ($modecompta == "RECETTES-DEPENSES") 188{ 189 $name = $langs->trans("TurnoverCollected").', '.$langs->trans("ByProductsAndServices"); 190 $calcmode = $langs->trans("CalcModeEngagement"); 191 //$calcmode.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year='.$year_start.'&modecompta=CREANCES-DETTES">','</a>').')'; 192 193 $description = $langs->trans("RulesCAIn"); 194 $description .= $langs->trans("DepositsAreIncluded"); 195 196 $builddate = dol_now(); 197} elseif ($modecompta == "BOOKKEEPING") 198{ 199} elseif ($modecompta == "BOOKKEEPINGCOLLECTED") 200{ 201} 202 203$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0); 204if ($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>'; 205else $periodlink = ''; 206 207report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $tableparams, $calcmode); 208 209if (!empty($conf->accounting->enabled) && $modecompta != 'BOOKKEEPING') 210{ 211 print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1); 212} 213 214 215 216$name = array(); 217 218// SQL request 219$catotal = 0; 220$catotal_ht = 0; 221$qtytotal = 0; 222 223if ($modecompta == 'CREANCES-DETTES') 224{ 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) { // Without any category 233 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_product as cp ON p.rowid = cp.fk_product"; 234 } 235 $sql .= " WHERE l.fk_facture = f.rowid"; 236 $sql .= " AND f.fk_statut in (1,2)"; 237 $sql .= " AND l.product_type in (0,1)"; 238 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) { 239 $sql .= " AND f.type IN (0,1,2,5)"; 240 } else { 241 $sql .= " AND f.type IN (0,1,2,3,5)"; 242 } 243 if ($date_start && $date_end) { 244 $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'"; 245 } 246 if ($selected_type >= 0) 247 { 248 $sql .= " AND l.product_type = ".$selected_type; 249 } 250 if ($selected_cat === -2) { // Without any category 251 $sql .= " AND cp.fk_product is null"; 252 } elseif ($selected_cat) { // Into a specific category 253 if ($subcat) { 254 $TListOfCats = $categorie->get_full_arbo('product', $selected_cat, 1); 255 256 $listofcatsql = ""; 257 foreach ($TListOfCats as $key => $cat) 258 { 259 if ($key !== 0) $listofcatsql .= ","; 260 $listofcatsql .= $cat['rowid']; 261 } 262 } 263 264 $sql .= " AND (p.rowid IN "; 265 $sql .= " (SELECT fk_product FROM ".MAIN_DB_PREFIX."categorie_product cp WHERE "; 266 if ($subcat) $sql .= "cp.fk_categorie IN (".$listofcatsql.")"; 267 else $sql .= "cp.fk_categorie = ".$selected_cat; 268 $sql .= "))"; 269 } 270 if ($selected_soc > 0) $sql .= " AND soc.rowid=".$selected_soc; 271 $sql .= " AND f.entity IN (".getEntity('invoice').")"; 272 $sql .= " GROUP BY p.rowid, p.ref, p.label, p.fk_product_type"; 273 $sql .= $db->order($sortfield, $sortorder); 274 275 dol_syslog("cabyprodserv", LOG_DEBUG); 276 $result = $db->query($sql); 277 if ($result) { 278 $num = $db->num_rows($result); 279 $i = 0; 280 while ($i < $num) { 281 $obj = $db->fetch_object($result); 282 $amount_ht[$obj->rowid] = $obj->amount; 283 $amount[$obj->rowid] = $obj->amount_ttc; 284 $qty[$obj->rowid] = $obj->qty; 285 $name[$obj->rowid] = $obj->ref.' - '.$obj->label; 286 $type[$obj->rowid] = $obj->product_type; 287 $catotal_ht += $obj->amount; 288 $catotal += $obj->amount_ttc; 289 $qtytotal += $obj->qty; 290 $i++; 291 } 292 } else { 293 dol_print_error($db); 294 } 295 296 // Show Array 297 $i = 0; 298 print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">'; 299 print '<input type="hidden" name="token" value="'.newToken().'">'."\n"; 300 // Extra parameters management 301 foreach ($headerparams as $key => $value) 302 { 303 print '<input type="hidden" name="'.$key.'" value="'.$value.'">'; 304 } 305 306 $moreforfilter = ''; 307 308 print '<div class="div-table-responsive">'; 309 print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n"; 310 311 // Category filter 312 print '<tr class="liste_titre">'; 313 print '<td>'; 314 print $langs->trans("Category").': '.$formother->select_categories(Categorie::TYPE_PRODUCT, $selected_cat, 'search_categ', true); 315 print ' '; 316 print $langs->trans("SubCats").'? '; 317 print '<input type="checkbox" name="subcat" value="yes"'; 318 if ($subcat) { 319 print ' checked'; 320 } 321 print '>'; 322 // type filter (produit/service) 323 print ' '; 324 print $langs->trans("Type").': '; 325 $form->select_type_of_lines(isset($selected_type) ? $selected_type : -1, 'search_type', 1, 1, 1); 326 327 //select thirdparty 328 print '</br>'; 329 print $langs->trans("ThirdParty").': '.$form->select_thirdparty_list($selected_soc, 'search_soc', '', 1); 330 print '</td>'; 331 332 print '<td colspan="5" class="right">'; 333 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")).'">'; 334 print '</td></tr>'; 335 336 // Array header 337 print "<tr class=\"liste_titre\">"; 338 print_liste_field_titre( 339 $langs->trans("Product"), 340 $_SERVER["PHP_SELF"], 341 "ref", 342 "", 343 $paramslink, 344 "", 345 $sortfield, 346 $sortorder 347 ); 348 print_liste_field_titre( 349 $langs->trans('Quantity'), 350 $_SERVER["PHP_SELF"], 351 "qty", 352 "", 353 $paramslink, 354 'class="right"', 355 $sortfield, 356 $sortorder 357 ); 358 print_liste_field_titre( 359 $langs->trans("Percentage"), 360 $_SERVER["PHP_SELF"], 361 "qty", 362 "", 363 $paramslink, 364 'class="right"', 365 $sortfield, 366 $sortorder 367 ); 368 print_liste_field_titre( 369 $langs->trans('AmountHT'), 370 $_SERVER["PHP_SELF"], 371 "amount", 372 "", 373 $classslink, 374 'class="right"', 375 $sortfield, 376 $sortorder 377 ); 378 print_liste_field_titre( 379 $langs->trans("AmountTTC"), 380 $_SERVER["PHP_SELF"], 381 "amount_ttc", 382 "", 383 $paramslink, 384 'class="right"', 385 $sortfield, 386 $sortorder 387 ); 388 print_liste_field_titre( 389 $langs->trans("Percentage"), 390 $_SERVER["PHP_SELF"], 391 "amount_ttc", 392 "", 393 $paramslink, 394 'class="right"', 395 $sortfield, 396 $sortorder 397 ); 398 print "</tr>\n"; 399 400 if (count($name)) { 401 foreach ($name as $key=>$value) { 402 print '<tr class="oddeven">'; 403 404 // Product 405 print "<td>"; 406 $fullname = $name[$key]; 407 if ($key > 0) { 408 $linkname = '<a href="'.DOL_URL_ROOT.'/product/card.php?id='.$key.'">'.img_object($langs->trans("ShowProduct"), $type[$key] == 0 ? 'product' : 'service').' '.$fullname.'</a>'; 409 } else { 410 $linkname = $langs->trans("PaymentsNotLinkedToProduct"); 411 } 412 print $linkname; 413 print "</td>\n"; 414 415 // Quantity 416 print '<td class="right">'; 417 print $qty[$key]; 418 print '</td>'; 419 420 // Percent; 421 print '<td class="right">'.($qtytotal > 0 ? round(100 * $qty[$key] / $qtytotal, 2).'%' : ' ').'</td>'; 422 423 // Amount w/o VAT 424 print '<td class="right">'; 425 /*if ($key > 0) { 426 print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?productid='.$key.'">'; 427 } else { 428 print '<a href="#">'; 429 }*/ 430 print price($amount_ht[$key]); 431 //print '</a>'; 432 print '</td>'; 433 434 // Amount with VAT 435 print '<td class="right">'; 436 /*if ($key > 0) { 437 print '<a href="'.DOL_URL_ROOT.'/compta/facture/list.php?productid='.$key.'">'; 438 } else { 439 print '<a href="#">'; 440 }*/ 441 print price($amount[$key]); 442 //print '</a>'; 443 print '</td>'; 444 445 // Percent; 446 print '<td class="right">'.($catotal > 0 ? round(100 * $amount[$key] / $catotal, 2).'%' : ' ').'</td>'; 447 448 // TODO: statistics? 449 450 print "</tr>\n"; 451 $i++; 452 } 453 454 // Total 455 print '<tr class="liste_total">'; 456 print '<td>'.$langs->trans("Total").'</td>'; 457 print '<td class="right">'.$qtytotal.'</td>'; 458 print '<td class="right">100%</td>'; 459 print '<td class="right">'.price($catotal_ht).'</td>'; 460 print '<td class="right">'.price($catotal).'</td>'; 461 print '<td class="right">100%</td>'; 462 print '</tr>'; 463 464 $db->free($result); 465 } 466 print "</table>"; 467 print '</div>'; 468 469 print '</form>'; 470} else { 471 // $modecompta != 'CREANCES-DETTES' 472 // "Calculation of part of each product for accountancy in this mode is not possible. When a partial payment (for example 5 euros) is done on an 473 // invoice with 2 product (product A for 10 euros and product B for 20 euros), what is part of paiment for product A and part of paiment for product B ? 474 // Because there is no way to know this, this report is not relevant. 475 print '<br>'.$langs->trans("TurnoverPerProductInCommitmentAccountingNotRelevant").'<br>'; 476} 477 478// End of page 479llxFooter(); 480$db->close(); 481