dolibarr  20.0.0-alpha
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 
29 global $mysoc;
30 
31 // Load Dolibarr environment
32 require '../../main.inc.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
35 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
36 require_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
49 if ($user->socid > 0) {
50  $socid = $user->socid;
51 }
52 if (isModEnabled('comptabilite')) {
53  $result = restrictedArea($user, 'compta', '', '', 'resultat');
54 }
55 if (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 
73 llxHeader('', $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 $pastmonth = dol_print_date(dol_now(), "%m") - 1;
80 $pastmonthyear = $year_current;
81 if ($pastmonth == 0) {
82  $pastmonth = 12;
83  $pastmonthyear--;
84 }
85 
86 $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
87 $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
88 
89 if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
90  $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
91  $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
92 }
93 
94 $name = $langs->trans("PurchasesJournal");
95 $periodlink = '';
96 $exportlink = '';
97 $builddate = dol_now();
98 $description = $langs->trans("DescPurchasesJournal").'<br>';
99 if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
100  $description .= $langs->trans("DepositsAreNotIncluded");
101 } else {
102  $description .= $langs->trans("DepositsAreIncluded");
103 }
104 $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
105 
106 report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink);
107 
108 $p = explode(":", getDolGlobalString('MAIN_INFO_SOCIETE_COUNTRY'));
109 $idpays = $p[0];
110 
111 
112 $sql = "SELECT f.rowid, f.ref_supplier, f.type, f.datef, f.libelle as label,";
113 $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,";
114 $sql .= " s.rowid as socid, s.nom as name, s.code_compta_fournisseur,";
115 $sql .= " p.rowid as pid, p.ref as ref, p.accountancy_code_buy,";
116 $sql .= " ct.accountancy_code_buy as account_tva, ct.recuperableonly";
117 $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as fd";
118 $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);
119 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = fd.fk_product";
120 $sql .= " JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON f.rowid = fd.fk_facture_fourn";
121 $sql .= " JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
122 $sql .= " WHERE f.fk_statut > 0 AND f.entity IN (".getEntity('invoice').")";
123 if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
124  $sql .= " AND f.type IN (0,1,2)";
125 } else {
126  $sql .= " AND f.type IN (0,1,2,3)";
127 }
128 if ($date_start && $date_end) {
129  $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
130 }
131 
132 // TODO Find a better trick to avoid problem with some mysql installations
133 if (in_array($db->type, array('mysql', 'mysqli'))) {
134  $db->query('SET SQL_BIG_SELECTS=1');
135 }
136 
137 $result = $db->query($sql);
138 if ($result) {
139  $num = $db->num_rows($result);
140  // les variables
141  $cptfour = ((getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') != "") ? $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER : $langs->trans("CodeNotDef"));
142  $cpttva = (getDolGlobalString('ACCOUNTING_VAT_BUY_ACCOUNT') ? $conf->global->ACCOUNTING_VAT_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
143 
144  $tabfac = array();
145  $tabht = array();
146  $tabtva = array();
147  $tabttc = array();
148  $tablocaltax1 = array();
149  $tablocaltax2 = array();
150  $tabcompany = array();
151 
152  $i = 0;
153  while ($i < $num) {
154  $obj = $db->fetch_object($result);
155  // contrôles
156  $compta_soc = (($obj->code_compta_fournisseur != "") ? $obj->code_compta_fournisseur : $cptfour);
157  $compta_prod = $obj->accountancy_code_buy;
158  if (empty($compta_prod)) {
159  if ($obj->product_type == 0) {
160  $compta_prod = (getDolGlobalString('ACCOUNTING_PRODUCT_BUY_ACCOUNT') ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
161  } else {
162  $compta_prod = (getDolGlobalString('ACCOUNTING_SERVICE_BUY_ACCOUNT') ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
163  }
164  }
165  $compta_tva = (!empty($obj->account_tva) ? $obj->account_tva : $cpttva);
166  $compta_localtax1 = (!empty($obj->account_localtax1) ? $obj->account_localtax1 : $langs->trans("CodeNotDef"));
167  $compta_localtax2 = (!empty($obj->account_localtax2) ? $obj->account_localtax2 : $langs->trans("CodeNotDef"));
168 
169  $account_localtax1 = getLocalTaxesFromRate($obj->tva_tx, 1, $mysoc, $obj->thirdparty);
170  $compta_localtax1 = (!empty($account_localtax1[2]) ? $account_localtax1[2] : $langs->trans("CodeNotDef"));
171  $account_localtax2 = getLocalTaxesFromRate($obj->tva_tx, 2, $mysoc, $obj->thirdparty);
172  $compta_localtax2 = (!empty($account_localtax2[2]) ? $account_localtax2[2] : $langs->trans("CodeNotDef"));
173 
174  $tabfac[$obj->rowid]["date"] = $obj->datef;
175  $tabfac[$obj->rowid]["ref"] = $obj->ref_supplier;
176  $tabfac[$obj->rowid]["type"] = $obj->type;
177  $tabfac[$obj->rowid]["lib"] = $obj->label;
178  $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc;
179  $tabht[$obj->rowid][$compta_prod] += $obj->total_ht;
180  if ($obj->recuperableonly != 1) {
181  $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
182  }
183  $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
184  $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
185  $tabcompany[$obj->rowid] = array('id'=>$obj->socid, 'name'=>$obj->name);
186 
187  $i++;
188  }
189 } else {
190  dol_print_error($db);
191 }
192 
193 /*
194  * Show result array
195  */
196 print '<table class="liste noborder centpercent">';
197 print "<tr class=\"liste_titre\">";
199 print "<td>".$langs->trans("Date")."</td>";
200 print "<td>".$langs->trans("Piece").' ('.$langs->trans("InvoiceRef").")</td>";
201 print "<td>".$langs->trans("Account")."</td>";
202 print "<td>".$langs->trans("Type")."</td>";
203 print "<td class='right'>".$langs->trans("AccountingDebit")."</td>";
204 print "<td class='right'>".$langs->trans("AccountingCredit")."</td>";
205 print "</tr>\n";
206 
207 
208 $invoicestatic = new FactureFournisseur($db);
209 $companystatic = new Fournisseur($db);
210 
211 foreach ($tabfac as $key => $val) {
212  $invoicestatic->id = $key;
213  $invoicestatic->ref = $val["ref"];
214  $invoicestatic->type = $val["type"];
215 
216  $companystatic->id = $tabcompany[$key]['id'];
217  $companystatic->name = $tabcompany[$key]['name'];
218 
219  $lines = array(
220  array(
221  'var' => $tabht[$key],
222  'label' => $langs->trans('Products'),
223  ),
224  array(
225  'var' => $tabtva[$key],
226  'label' => $langs->trans('VAT')
227  ),
228  array(
229  'var' => $tablocaltax1[$key],
230  'label' => $langs->transcountry('LT1', $mysoc->country_code)
231  ),
232  array(
233  'var' => $tablocaltax2[$key],
234  'label' => $langs->transcountry('LT2', $mysoc->country_code)
235  ),
236  array(
237  'var' => $tabttc[$key],
238  'label' => $langs->trans('ThirdParty').' ('.$companystatic->getNomUrl(0, 'supplier', 16).')',
239  'nomtcheck' => true,
240  'inv' => true
241  )
242  );
243 
244  foreach ($lines as $line) {
245  foreach ($line['var'] as $k => $mt) {
246  if (isset($line['nomtcheck']) || $mt) {
247  print '<tr class="oddeven">';
248  print "<td>".dol_print_date($db->jdate($val["date"]))."</td>";
249  print "<td>".$invoicestatic->getNomUrl(1)."</td>";
250  print "<td>".$k."</td>";
251  print "<td>".$line['label']."</td>";
252 
253  if (isset($line['inv'])) {
254  print '<td class="right">'.($mt < 0 ? price(-$mt) : '')."</td>";
255  print '<td class="right">'.($mt >= 0 ? price($mt) : '')."</td>";
256  } else {
257  print '<td class="right">'.($mt >= 0 ? price($mt) : '')."</td>";
258  print '<td class="right">'.($mt < 0 ? price(-$mt) : '')."</td>";
259  }
260 
261  print "</tr>";
262  }
263  }
264  }
265 }
266 
267 print "</table>";
268 
269 // End of page
270 llxFooter();
271 $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.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:744
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:594
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:613
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 dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
report_header($reportname, $notused, $period, $periodlink, $description, $builddate, $exportlink='', $moreparam=array(), $calcmode='', $varlink='')
Show header of a report.
Definition: report.lib.php:41
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.