dolibarr 24.0.0-beta
purchasesjournal.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-2025 Alexandre spangaro <aspangaro@open-dsi.fr>
7 * Copyright (C) 2013 Marcos García <marcosgdf@gmail.com>
8 * Copyright (C) 2018-2025 Frédéric France <frederic.france@free.fr>
9 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
30global $mysoc;
31
32// Load Dolibarr environment
33require '../../main.inc.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
35require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
36require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
37require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
38
48// Load translation files required by the page
49$langs->loadlangs(array('companies', 'other', 'bills', 'compta'));
50
51$date_startmonth = GETPOST('date_startmonth');
52$date_startday = GETPOST('date_startday');
53$date_startyear = GETPOST('date_startyear');
54$date_endmonth = GETPOST('date_endmonth');
55$date_endday = GETPOST('date_endday');
56$date_endyear = GETPOST('date_endyear');
57
58// Security check
59if ($user->socid > 0) {
60 $socid = $user->socid;
61}
62
63// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
64$hookmanager->initHooks(['purchasejournallist']);
65
66if (isModEnabled('comptabilite')) {
67 $result = restrictedArea($user, 'compta', '', '', 'resultat');
68}
69if (isModEnabled('accounting')) {
70 $result = restrictedArea($user, 'accounting', '', '', 'comptarapport');
71}
72
73/*
74 * Actions
75 */
76
77// None
78
79
80/*
81 * View
82 */
83
84$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;
85
86llxHeader('', $langs->trans("PurchasesJournal"), '', '', 0, 0, '', '', $morequery);
87
88$form = new Form($db);
89
90$year_current = (int) dol_print_date(dol_now('gmt'), "%Y", 'gmt');
91//$pastmonth = strftime("%m", dol_now()) - 1;
92$pastmonth = (int) dol_print_date(dol_now(), "%m") - 1;
93$pastmonthyear = $year_current;
94if ($pastmonth == 0) {
95 $pastmonth = 12;
96 $pastmonthyear--;
97}
98
99$date_start = dol_mktime(0, 0, 0, (int) $date_startmonth, (int) $date_startday, (int) $date_startyear);
100$date_end = dol_mktime(23, 59, 59, (int) $date_endmonth, (int) $date_endday, (int) $date_endyear);
101
102if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
103 $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
104 $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
105}
106
107$name = $langs->trans("PurchasesJournal");
108$periodlink = '';
109$exportlink = '';
110$builddate = dol_now();
111$description = $langs->trans("DescPurchasesJournal").'<br>';
112if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
113 $description .= $langs->trans("DepositsAreNotIncluded");
114} else {
115 $description .= $langs->trans("DepositsAreIncluded");
116}
117$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
118
119report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink);
120
121$idpays = $mysoc->country_id;
122
123
124$sql = "SELECT f.rowid, f.ref_supplier, f.type, f.datef, f.libelle as label,";
125$sql .= " fd.total_ttc, fd.tva_tx, fd.total_ht, fd.tva as total_tva, fd.product_type, fd.localtax1_tx, fd.localtax2_tx, fd.total_localtax1, fd.total_localtax2,";
126$sql .= " s.rowid as socid, s.nom as name, s.code_compta_fournisseur,";
127$sql .= " p.rowid as pid, p.ref as ref, p.accountancy_code_buy,";
128$sql .= " ct.accountancy_code_buy as account_tva, ct.recuperableonly";
129$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as fd";
130$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_tva as ct ON fd.tva_tx = ct.taux AND fd.info_bits = ct.recuperableonly AND ct.fk_pays = ".((int) $idpays);
131$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = fd.fk_product";
132$sql .= " JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON f.rowid = fd.fk_facture_fourn";
133$sql .= " JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
134$sql .= " WHERE f.fk_statut > 0 AND f.entity IN (".getEntity('invoice').")";
135$sql .= " AND ct.entity IN (".getEntity('invoice').")";
136if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
137 $sql .= " AND f.type IN (0,1,2)";
138} else {
139 $sql .= " AND f.type IN (0,1,2,3)";
140}
141if ($date_start && $date_end) {
142 $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
143}
144
145// TODO Find a better trick to avoid problem with some mysql installations
146if (in_array($db->type, array('mysql', 'mysqli'))) {
147 $db->query('SET SQL_BIG_SELECTS=1');
148}
149
150$tabfac = array();
151$tabcompany = array();
152$tabht = array();
153$tabtva = array();
154$tabttc = array();
155$tablocaltax1 = array();
156$tablocaltax2 = array();
157
158$result = $db->query($sql);
159if ($result) {
160 $num = $db->num_rows($result);
161 // les variables
162 $cptfour = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER', $langs->trans("CodeNotDef"));
163 $cpttva = getDolGlobalString('ACCOUNTING_VAT_BUY_ACCOUNT', $langs->trans("CodeNotDef"));
164
165
166 $i = 0;
167 while ($i < $num) {
168 $obj = $db->fetch_object($result);
169 // contrôles
170 $compta_soc = (($obj->code_compta_fournisseur != "") ? $obj->code_compta_fournisseur : $cptfour);
171 $compta_prod = $obj->accountancy_code_buy;
172 if (empty($compta_prod)) {
173 if ($obj->product_type == 0) {
174 $compta_prod = getDolGlobalString('ACCOUNTING_PRODUCT_BUY_ACCOUNT', $langs->trans("CodeNotDef"));
175 } else {
176 $compta_prod = getDolGlobalString('ACCOUNTING_SERVICE_BUY_ACCOUNT', $langs->trans("CodeNotDef"));
177 }
178 }
179 $compta_tva = (!empty($obj->account_tva) ? $obj->account_tva : $cpttva);
180 $compta_localtax1 = (!empty($obj->account_localtax1) ? $obj->account_localtax1 : $langs->trans("CodeNotDef"));
181 $compta_localtax2 = (!empty($obj->account_localtax2) ? $obj->account_localtax2 : $langs->trans("CodeNotDef"));
182
183 $account_localtax1 = getLocalTaxesFromRate($obj->tva_tx, 1, $mysoc, $obj->thirdparty);
184 $compta_localtax1 = (!empty($account_localtax1[2]) ? $account_localtax1[2] : $langs->trans("CodeNotDef"));
185 $account_localtax2 = getLocalTaxesFromRate($obj->tva_tx, 2, $mysoc, $obj->thirdparty);
186 $compta_localtax2 = (!empty($account_localtax2[2]) ? $account_localtax2[2] : $langs->trans("CodeNotDef"));
187
188 // Avoid warnings
189 if (!array_key_exists($obj->rowid, $tabfac)) {
190 $tabfac[$obj->rowid] = array();
191 $tabttc[$obj->rowid] = array();
192 $tabht[$obj->rowid] = array();
193 $tabtva[$obj->rowid] = array();
194 $tablocaltax1[$obj->rowid] = array();
195 $tablocaltax2[$obj->rowid] = array();
196 }
197 if (!isset($tabttc[$obj->rowid][$compta_soc])) {
198 $tabttc[$obj->rowid][$compta_soc] = 0;
199 }
200 if (!isset($tabht[$obj->rowid][$compta_prod])) {
201 $tabht[$obj->rowid][$compta_prod] = 0;
202 }
203 if (!isset($tabtva[$obj->rowid][$compta_tva])) {
204 $tabtva[$obj->rowid][$compta_tva] = 0;
205 }
206 if (!isset($tablocaltax1[$obj->rowid][$compta_localtax1])) {
207 $tablocaltax1[$obj->rowid][$compta_localtax1] = 0;
208 }
209 if (!isset($tablocaltax2[$obj->rowid][$compta_localtax2])) {
210 $tablocaltax2[$obj->rowid][$compta_localtax2] = 0;
211 }
212
213 $tabfac[$obj->rowid]["date"] = $obj->datef;
214 $tabfac[$obj->rowid]["ref"] = $obj->ref_supplier;
215 $tabfac[$obj->rowid]["type"] = $obj->type;
216 $tabfac[$obj->rowid]["lib"] = $obj->label;
217 $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc;
218 $tabht[$obj->rowid][$compta_prod] += $obj->total_ht;
219 if ($obj->recuperableonly != 1) {
220 $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
221 }
222 $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
223 $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
224 $tabcompany[$obj->rowid] = array('id' => $obj->socid, 'name' => $obj->name);
225
226 $i++;
227 }
228} else {
230}
231
232/*
233 * Show result array
234 */
235print '<table class="liste noborder centpercent">';
236print "<tr class=\"liste_titre\">";
238print "<td>".$langs->trans("Date")."</td>";
239print "<td>".$langs->trans("Piece").' ('.$langs->trans("InvoiceRef").")</td>";
240print "<td>".$langs->trans("Account")."</td>";
241print "<td>".$langs->trans("Type")."</td>";
242print "<td class='right'>".$langs->trans("AccountingDebit")."</td>";
243print "<td class='right'>".$langs->trans("AccountingCredit")."</td>";
244print "</tr>\n";
245
246
247$invoicestatic = new FactureFournisseur($db);
248$companystatic = new Fournisseur($db);
249
250if (count($tabfac) && count($tabht) && count($tabtva) && count($tablocaltax1) && count($tablocaltax2) && count($tabttc)) { // Check for static analysis
251 foreach ($tabfac as $key => $val) {
252 $invoicestatic->id = (int) $key;
253 $invoicestatic->ref = (string) $val["ref"];
254 $invoicestatic->type = $val["type"];
255
256 $companystatic->id = $tabcompany[$key]['id'];
257 $companystatic->name = $tabcompany[$key]['name'];
258
259 $lines = array(
260 array(
261 'var' => $tabht[$key],
262 'label' => $langs->trans('Products'),
263 ),
264 array(
265 'var' => $tabtva[$key],
266 'label' => $langs->trans('VAT')
267 ),
268 array(
269 'var' => $tablocaltax1[$key],
270 'label' => $langs->transcountry('LT1', $mysoc->country_code)
271 ),
272 array(
273 'var' => $tablocaltax2[$key],
274 'label' => $langs->transcountry('LT2', $mysoc->country_code)
275 ),
276 array(
277 'var' => $tabttc[$key],
278 'label' => $langs->trans('ThirdParty').' ('.$companystatic->getNomUrl(0, 'supplier', 16).')',
279 'nomtcheck' => true,
280 'inv' => true
281 )
282 );
283
284 foreach ($lines as $line) {
285 foreach ($line['var'] as $k => $mt) {
286 if (isset($line['nomtcheck']) || $mt) {
287 print '<tr class="oddeven">';
288 print '<td class="nowraponall">'.dol_print_date($db->jdate($val["date"]), 'day')."</td>";
289 print '<td class="tdoverflowmax150">'.$invoicestatic->getNomUrl(1)."</td>";
290 print "<td>".$k."</td>";
291 print "<td>".$line['label']."</td>";
292
293 if (isset($line['inv'])) {
294 print '<td class="right">'.($mt < 0 ? price(-$mt) : '')."</td>";
295 print '<td class="right">'.($mt >= 0 ? price($mt) : '')."</td>";
296 } else {
297 print '<td class="right">'.($mt >= 0 ? price($mt) : '')."</td>";
298 print '<td class="right">'.($mt < 0 ? price(-$mt) : '')."</td>";
299 }
300
301 print "</tr>";
302 }
303 }
304 }
305 }
306}
307
308print "</table>";
309
310// End of page
311llxFooter();
312$db->close();
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
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:73
Class to manage suppliers invoices.
Class to manage generation of HTML components Only common components must be here.
Class to manage suppliers.
global $mysoc
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:604
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:623
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
$date_start
Variables from include:
dol_now($mode='gmt')
Return date for now.
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.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
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.
isModEnabled($module)
Is Dolibarr module enabled.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php
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.