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