dolibarr 18.0.6
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-2012 Alexandre spangaro <aspangaro@open-dsi.fr>
7 * Copyright (C) 2013 Marcos García <marcosgdf@gmail.com>
8 * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
29global $mysoc;
30
31// Load Dolibarr environment
32require '../../main.inc.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
35require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
36require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
37
38// Load translation files required by the page
39$langs->loadlangs(array('companies', 'other', 'bills', 'compta'));
40
41$date_startmonth = GETPOST('date_startmonth');
42$date_startday = GETPOST('date_startday');
43$date_startyear = GETPOST('date_startyear');
44$date_endmonth = GETPOST('date_endmonth');
45$date_endday = GETPOST('date_endday');
46$date_endyear = GETPOST('date_endyear');
47
48// Security check
49if ($user->socid > 0) {
50 $socid = $user->socid;
51}
52if (isModEnabled('comptabilite')) {
53 $result = restrictedArea($user, 'compta', '', '', 'resultat');
54}
55if (isModEnabled('accounting')) {
56 $result = restrictedArea($user, 'accounting', '', '', 'comptarapport');
57}
58$hookmanager->initHooks(['purchasejournallist']);
59
60/*
61 * Actions
62 */
63
64// None
65
66
67/*
68 * View
69 */
70
71$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;
72
73llxHeader('', $langs->trans("PurchasesJournal"), '', '', 0, 0, '', '', $morequery);
74
75$form = new Form($db);
76
77$year_current = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
78$pastmonth = strftime("%m", dol_now()) - 1;
79$pastmonthyear = $year_current;
80if ($pastmonth == 0) {
81 $pastmonth = 12;
82 $pastmonthyear--;
83}
84
85$date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
86$date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
87
88if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
89 $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
90 $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
91}
92
93$name = $langs->trans("PurchasesJournal");
94$periodlink = '';
95$exportlink = '';
96$builddate = dol_now();
97$description = $langs->trans("DescPurchasesJournal").'<br>';
98if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
99 $description .= $langs->trans("DepositsAreNotIncluded");
100} else {
101 $description .= $langs->trans("DepositsAreIncluded");
102}
103$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
104
105report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink);
106
107$p = explode(":", $conf->global->MAIN_INFO_SOCIETE_COUNTRY);
108$idpays = $p[0];
109
110
111$sql = "SELECT f.rowid, f.ref_supplier, f.type, f.datef, f.libelle as label,";
112$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,";
113$sql .= " s.rowid as socid, s.nom as name, s.code_compta_fournisseur,";
114$sql .= " p.rowid as pid, p.ref as ref, p.accountancy_code_buy,";
115$sql .= " ct.accountancy_code_buy as account_tva, ct.recuperableonly";
116$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as fd";
117$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);
118$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = fd.fk_product";
119$sql .= " JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON f.rowid = fd.fk_facture_fourn";
120$sql .= " JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
121$sql .= " WHERE f.fk_statut > 0 AND f.entity IN (".getEntity('invoice').")";
122if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
123 $sql .= " AND f.type IN (0,1,2)";
124} else {
125 $sql .= " AND f.type IN (0,1,2,3)";
126}
127if ($date_start && $date_end) {
128 $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
129}
130
131// TODO Find a better trick to avoid problem with some mysql installations
132if (in_array($db->type, array('mysql', 'mysqli'))) {
133 $db->query('SET SQL_BIG_SELECTS=1');
134}
135
136$result = $db->query($sql);
137if ($result) {
138 $num = $db->num_rows($result);
139 // les variables
140 $cptfour = (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER != "") ? $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER : $langs->trans("CodeNotDef"));
141 $cpttva = (!empty($conf->global->ACCOUNTING_VAT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_VAT_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
142
143 $tabfac = array();
144 $tabht = array();
145 $tabtva = array();
146 $tabttc = array();
147 $tablocaltax1 = array();
148 $tablocaltax2 = array();
149 $tabcompany = array();
150
151 $i = 0;
152 while ($i < $num) {
153 $obj = $db->fetch_object($result);
154 // contrôles
155 $compta_soc = (($obj->code_compta_fournisseur != "") ? $obj->code_compta_fournisseur : $cptfour);
156 $compta_prod = $obj->accountancy_code_buy;
157 if (empty($compta_prod)) {
158 if ($obj->product_type == 0) {
159 $compta_prod = (!empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
160 } else {
161 $compta_prod = (!empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
162 }
163 }
164 $compta_tva = (!empty($obj->account_tva) ? $obj->account_tva : $cpttva);
165 $compta_localtax1 = (!empty($obj->account_localtax1) ? $obj->account_localtax1 : $langs->trans("CodeNotDef"));
166 $compta_localtax2 = (!empty($obj->account_localtax2) ? $obj->account_localtax2 : $langs->trans("CodeNotDef"));
167
168 $account_localtax1 = getLocalTaxesFromRate($obj->tva_tx, 1, $mysoc, $obj->thirdparty);
169 $compta_localtax1 = (!empty($account_localtax1[2]) ? $account_localtax1[2] : $langs->trans("CodeNotDef"));
170 $account_localtax2 = getLocalTaxesFromRate($obj->tva_tx, 2, $mysoc, $obj->thirdparty);
171 $compta_localtax2 = (!empty($account_localtax2[2]) ? $account_localtax2[2] : $langs->trans("CodeNotDef"));
172
173 $tabfac[$obj->rowid]["date"] = $obj->datef;
174 $tabfac[$obj->rowid]["ref"] = $obj->ref_supplier;
175 $tabfac[$obj->rowid]["type"] = $obj->type;
176 $tabfac[$obj->rowid]["lib"] = $obj->label;
177 $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc;
178 $tabht[$obj->rowid][$compta_prod] += $obj->total_ht;
179 if ($obj->recuperableonly != 1) {
180 $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
181 }
182 $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
183 $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
184 $tabcompany[$obj->rowid] = array('id'=>$obj->socid, 'name'=>$obj->name);
185
186 $i++;
187 }
188} else {
189 dol_print_error($db);
190}
191
192/*
193 * Show result array
194 */
195print '<table class="liste noborder centpercent">';
196print "<tr class=\"liste_titre\">";
198print "<td>".$langs->trans("Date")."</td>";
199print "<td>".$langs->trans("Piece").' ('.$langs->trans("InvoiceRef").")</td>";
200print "<td>".$langs->trans("Account")."</td>";
201print "<td>".$langs->trans("Type")."</td>";
202print "<td class='right'>".$langs->trans("AccountingDebit")."</td>";
203print "<td class='right'>".$langs->trans("AccountingCredit")."</td>";
204print "</tr>\n";
205
206
207$invoicestatic = new FactureFournisseur($db);
208$companystatic = new Fournisseur($db);
209
210foreach ($tabfac as $key => $val) {
211 $invoicestatic->id = $key;
212 $invoicestatic->ref = $val["ref"];
213 $invoicestatic->type = $val["type"];
214
215 $companystatic->id = $tabcompany[$key]['id'];
216 $companystatic->name = $tabcompany[$key]['name'];
217
218 $lines = array(
219 array(
220 'var' => $tabht[$key],
221 'label' => $langs->trans('Products'),
222 ),
223 array(
224 'var' => $tabtva[$key],
225 'label' => $langs->trans('VAT')
226 ),
227 array(
228 'var' => $tablocaltax1[$key],
229 'label' => $langs->transcountry('LT1', $mysoc->country_code)
230 ),
231 array(
232 'var' => $tablocaltax2[$key],
233 'label' => $langs->transcountry('LT2', $mysoc->country_code)
234 ),
235 array(
236 'var' => $tabttc[$key],
237 'label' => $langs->trans('ThirdParty').' ('.$companystatic->getNomUrl(0, 'supplier', 16).')',
238 'nomtcheck' => true,
239 'inv' => true
240 )
241 );
242
243 foreach ($lines as $line) {
244 foreach ($line['var'] as $k => $mt) {
245 if (isset($line['nomtcheck']) || $mt) {
246 print '<tr class="oddeven">';
247 print "<td>".dol_print_date($db->jdate($val["date"]))."</td>";
248 print "<td>".$invoicestatic->getNomUrl(1)."</td>";
249 print "<td>".$k."</td><td>".$line['label']."</td>";
250
251 if (isset($line['inv'])) {
252 print '<td class="right">'.($mt < 0 ?price(-$mt) : '')."</td>";
253 print '<td class="right">'.($mt >= 0 ?price($mt) : '')."</td>";
254 } else {
255 print '<td class="right">'.($mt >= 0 ?price($mt) : '')."</td>";
256 print '<td class="right">'.($mt < 0 ?price(-$mt) : '')."</td>";
257 }
258
259 print "</tr>";
260 }
261 }
262 }
263}
264
265print "</table>";
266
267// End of page
268llxFooter();
269$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:56
llxFooter()
Empty footer.
Definition wrapper.php:70
Class to manage suppliers invoices.
Class to manage generation of HTML components Only common components must be here.
Class to manage suppliers.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:577
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:596
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_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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.