dolibarr 21.0.0-alpha
sellsjournal.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2010 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2007-2010 Jean Heimburger <jean@tiaris.info>
4 * Copyright (C) 2011-2014 Juanjo Menent <jmenent@2byte.es>
5 * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
6 * Copyright (C) 2011-2012 Alexandre Spangaro <aspangaro@open-dsi.fr>
7 * Copyright (C) 2012 Cédric Salvador <csalvador@gpcsolutions.fr>
8 * Copyright (C) 2013 Marcos García <marcosgdf@gmail.com>
9 * Copyright (C) 2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
10 * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
11 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 */
26
32global $mysoc;
33
34// Load Dolibarr environment
35require '../../main.inc.php';
36require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
37require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
38require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
39require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php';
40
41// Load translation files required by the page
42$langs->loadLangs(array('companies', 'other', 'bills', 'compta'));
43
44$date_startmonth = GETPOST('date_startmonth');
45$date_startday = GETPOST('date_startday');
46$date_startyear = GETPOST('date_startyear');
47$date_endmonth = GETPOST('date_endmonth');
48$date_endday = GETPOST('date_endday');
49$date_endyear = GETPOST('date_endyear');
50
51// Security check
52if ($user->socid > 0) {
53 $socid = $user->socid;
54}
55
56// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
57$hookmanager->initHooks(['selljournallist']);
58
59if (isModEnabled('comptabilite')) {
60 $result = restrictedArea($user, 'compta', '', '', 'resultat');
61}
62if (isModEnabled('accounting')) {
63 $result = restrictedArea($user, 'accounting', '', '', 'comptarapport');
64}
65
66/*
67 * Actions
68 */
69
70// None
71
72
73
74/*
75 * View
76 */
77
78$form = new Form($db);
79
80$morequery = '&date_startyear='.$date_startyear.'&date_startmonth='.$date_startmonth.'&date_startday='.$date_startday.'&date_endyear='.$date_endyear.'&date_endmonth='.$date_endmonth.'&date_endday='.$date_endday;
81
82llxHeader('', $langs->trans("SellsJournal"), '', '', 0, 0, '', '', $morequery);
83
84
85$year_current = (int) dol_print_date(dol_now('gmt'), "%Y", 'gmt');
86//$pastmonth = strftime("%m", dol_now()) - 1;
87$pastmonth = (int) dol_print_date(dol_now(), "%m") - 1;
88$pastmonthyear = $year_current;
89if ($pastmonth == 0) {
90 $pastmonth = 12;
91 $pastmonthyear--;
92}
93
94$date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
95$date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
96
97if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
98 $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
99 $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
100}
101
102$name = $langs->trans("SellsJournal");
103$periodlink = '';
104$exportlink = '';
105$builddate = dol_now();
106$description = $langs->trans("DescSellsJournal").'<br>';
107if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
108 $description .= $langs->trans("DepositsAreNotIncluded");
109} else {
110 $description .= $langs->trans("DepositsAreIncluded");
111}
112$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
113report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink);
114
115$p = explode(":", getDolGlobalString('MAIN_INFO_SOCIETE_COUNTRY'));
116$idpays = $p[0];
117
118$sql = "SELECT f.rowid, f.ref, f.type, f.datef, f.ref_client,";
119$sql .= " fd.product_type, fd.total_ht, fd.total_tva, fd.tva_tx, fd.total_ttc, fd.localtax1_tx, fd.localtax2_tx, fd.total_localtax1, fd.total_localtax2, fd.rowid as id, fd.situation_percent,";
120$sql .= " s.rowid as socid, s.nom as name, s.code_compta as code_compta_client, s.client,";
121$sql .= " p.rowid as pid, p.ref as pref,";
122if (getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED')) {
123 $sql .= " ppe.accountancy_code_sell,";
124} else {
125 $sql .= " p.accountancy_code_sell,";
126}
127$sql .= " ct.accountancy_code_sell as account_tva, ct.recuperableonly";
128$sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
129$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = fd.fk_product";
130if (getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED')) {
131 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
132}
133$sql .= " JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
134$sql .= " JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
135$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_tva ct ON fd.tva_tx = ct.taux AND fd.info_bits = ct.recuperableonly AND ct.fk_pays = ".((int) $idpays);
136$sql .= " WHERE f.entity IN (".getEntity('invoice').")";
137$sql .= " AND f.fk_statut > 0";
138if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
139 $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_SITUATION.")";
140} else {
141 $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_STANDARD.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_DEPOSIT.",".Facture::TYPE_SITUATION.")";
142}
143
144$sql .= " AND fd.product_type IN (0,1)";
145if ($date_start && $date_end) {
146 $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
147}
148$sql .= " ORDER BY f.rowid";
149
150// TODO Find a better trick to avoid problem with some mysql installations
151if (in_array($db->type, array('mysql', 'mysqli'))) {
152 $db->query('SET SQL_BIG_SELECTS=1');
153}
154
155$result = $db->query($sql);
156if ($result) {
157 $tabfac = array();
158 $tabht = array();
159 $tabtva = array();
160 $tablocaltax1 = array();
161 $tablocaltax2 = array();
162 $tabttc = array();
163 $tabcompany = array();
164 $account_localtax1 = 0;
165 $account_localtax2 = 0;
166
167 $num = $db->num_rows($result);
168 $i = 0;
169 $resligne = array();
170 while ($i < $num) {
171 $obj = $db->fetch_object($result);
172 // les variables
173 $cptcli = ((getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') != "") ? $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER : $langs->trans("CodeNotDef"));
174 $compta_soc = (!empty($obj->code_compta_client) ? $obj->code_compta_client : $cptcli);
175 $compta_prod = $obj->accountancy_code_sell;
176 if (empty($compta_prod)) {
177 if ($obj->product_type == 0) {
178 $compta_prod = (getDolGlobalString('ACCOUNTING_PRODUCT_SOLD_ACCOUNT') ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
179 } else {
180 $compta_prod = (getDolGlobalString('ACCOUNTING_SERVICE_SOLD_ACCOUNT') ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
181 }
182 }
183 $cpttva = (getDolGlobalString('ACCOUNTING_VAT_SOLD_ACCOUNT') ? $conf->global->ACCOUNTING_VAT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
184 $compta_tva = (!empty($obj->account_tva) ? $obj->account_tva : $cpttva);
185
186 $account_localtax1 = getLocalTaxesFromRate($obj->tva_tx, 1, $obj->thirdparty, $mysoc);
187 $compta_localtax1 = (!empty($account_localtax1[3]) ? $account_localtax1[3] : $langs->trans("CodeNotDef"));
188 $account_localtax2 = getLocalTaxesFromRate($obj->tva_tx, 2, $obj->thirdparty, $mysoc);
189 $compta_localtax2 = (!empty($account_localtax2[3]) ? $account_localtax2[3] : $langs->trans("CodeNotDef"));
190
191 // Situation invoices handling
192 $line = new FactureLigne($db);
193 $line->fetch($obj->id); // id of line
194 $prev_progress = 0;
195 if ($obj->type == Facture::TYPE_SITUATION) {
196 // Avoid divide by 0
197 if ($obj->situation_percent == 0) {
198 $situation_ratio = 0;
199 } else {
200 $prev_progress = $line->get_prev_progress($obj->rowid); // id on invoice
201 $situation_ratio = ($obj->situation_percent - $prev_progress) / $obj->situation_percent;
202 }
203 } else {
204 $situation_ratio = 1;
205 }
206
207 //la ligne facture
208 $tabfac[$obj->rowid]["date"] = $obj->datef;
209 $tabfac[$obj->rowid]["ref"] = $obj->ref;
210 $tabfac[$obj->rowid]["type"] = $obj->type;
211 if (!isset($tabttc[$obj->rowid][$compta_soc])) {
212 $tabttc[$obj->rowid][$compta_soc] = 0;
213 }
214 if (!isset($tabht[$obj->rowid][$compta_prod])) {
215 $tabht[$obj->rowid][$compta_prod] = 0;
216 }
217 if (!isset($tabtva[$obj->rowid][$compta_tva])) {
218 $tabtva[$obj->rowid][$compta_tva] = 0;
219 }
220 if (!isset($tablocaltax1[$obj->rowid][$compta_localtax1])) {
221 $tablocaltax1[$obj->rowid][$compta_localtax1] = 0;
222 }
223 if (!isset($tablocaltax2[$obj->rowid][$compta_localtax2])) {
224 $tablocaltax2[$obj->rowid][$compta_localtax2] = 0;
225 }
226 $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc * $situation_ratio;
227 $tabht[$obj->rowid][$compta_prod] += $obj->total_ht * $situation_ratio;
228 if ($obj->recuperableonly != 1) {
229 $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva * $situation_ratio;
230 }
231 $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
232 $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
233 $tabcompany[$obj->rowid] = array('id' => $obj->socid, 'name' => $obj->name, 'client' => $obj->client);
234 $i++;
235 }
236} else {
237 dol_print_error($db);
238}
239
240
241/*
242 * Show result array
243 */
244
245
246print '<table class="liste noborder centpercent">';
247print '<tr class="liste_titre">';
248//print "<td>".$langs->trans("JournalNum")."</td>";
249print '<td>'.$langs->trans('Date').'</td><td>'.$langs->trans('Piece').' ('.$langs->trans('InvoiceRef').')</td>';
250print '<td>'.$langs->trans('Account').'</td>';
251print '<td>'.$langs->trans('Type').'</td>';
252print '<td class="right">'.$langs->trans('AccountingDebit').'</td>';
253print '<td class="right">'.$langs->trans('AccountingCredit').'</td>';
254print "</tr>\n";
255
256
257$invoicestatic = new Facture($db);
258$companystatic = new Client($db);
259
260foreach ($tabfac as $key => $val) {
261 $invoicestatic->id = $key;
262 $invoicestatic->ref = $val["ref"];
263 $invoicestatic->type = $val["type"];
264
265 $companystatic->id = $tabcompany[$key]['id'];
266 $companystatic->name = $tabcompany[$key]['name'];
267 $companystatic->client = $tabcompany[$key]['client'];
268
269 $lines = array(
270 array(
271 'var' => $tabttc[$key],
272 'label' => $langs->trans('ThirdParty').' ('.$companystatic->getNomUrl(0, 'customer', 16).')',
273 'nomtcheck' => true,
274 'inv' => true
275 ),
276 array(
277 'var' => $tabht[$key],
278 'label' => $langs->trans('Products'),
279 ),
280 array(
281 'var' => $tabtva[$key],
282 'label' => $langs->trans('VAT')
283 ),
284 array(
285 'var' => $tablocaltax1[$key],
286 'label' => $langs->transcountry('LT1', $mysoc->country_code)
287 ),
288 array(
289 'var' => $tablocaltax2[$key],
290 'label' => $langs->transcountry('LT2', $mysoc->country_code)
291 )
292 );
293
294 foreach ($lines as $line) {
295 foreach ($line['var'] as $k => $mt) {
296 if (isset($line['nomtcheck']) || $mt) {
297 print '<tr class="oddeven">';
298 print "<td>".dol_print_date($db->jdate($val["date"]))."</td>";
299 print "<td>".$invoicestatic->getNomUrl(1)."</td>";
300 print "<td>".$k."</td><td>".$line['label']."</td>";
301
302 if (isset($line['inv'])) {
303 print '<td class="right">'.($mt >= 0 ? price($mt) : '')."</td>";
304 print '<td class="right">'.($mt < 0 ? price(-$mt) : '')."</td>";
305 } else {
306 print '<td class="right">'.($mt < 0 ? price(-$mt) : '')."</td>";
307 print '<td class="right">'.($mt >= 0 ? price($mt) : '')."</td>";
308 }
309
310 print "</tr>";
311 }
312 }
313 }
314}
315
316print "</table>";
317
318// End of page
319llxFooter();
320$db->close();
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.
Definition wrapper.php:70
Class to manage customers or prospects.
Class to manage invoices.
const TYPE_REPLACEMENT
Replacement invoice.
const TYPE_STANDARD
Standard invoice.
const TYPE_SITUATION
Situation invoice.
const TYPE_DEPOSIT
Deposit invoice.
const TYPE_CREDIT_NOTE
Credit note invoice.
Class to manage invoice lines.
Class to manage generation of HTML components Only common components must be here.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:596
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:615
llxFooter()
Footer empty.
Definition document.php:107
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...
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_now($mode='auto')
Return date for now.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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.
report_header($reportname, $notused, $period, $periodlink, $description, $builddate, $exportlink='', $moreparam=array(), $calcmode='', $varlink='')
Show header of a report.
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.