dolibarr  9.0.0
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.dolibarr@gmail.com>
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  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
31 global $mysoc;
32 
33 require '../../main.inc.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
35 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
36 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
37 require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php';
38 
39 // Load translation files required by the page
40 $langs->loadLangs(array('companies', 'other', 'compta'));
41 
42 $date_startmonth=GETPOST('date_startmonth');
43 $date_startday=GETPOST('date_startday');
44 $date_startyear=GETPOST('date_startyear');
45 $date_endmonth=GETPOST('date_endmonth');
46 $date_endday=GETPOST('date_endday');
47 $date_endyear=GETPOST('date_endyear');
48 
49 // Security check
50 if ($user->societe_id > 0) $socid = $user->societe_id;
51 if (! empty($conf->comptabilite->enabled)) $result=restrictedArea($user,'compta','','','resultat');
52 if (! empty($conf->accounting->enabled)) $result=restrictedArea($user,'accounting','','','comptarapport');
53 
54 /*
55  * Actions
56  */
57 
58 // None
59 
60 
61 
62 /*
63  * View
64  */
65 
66 $form=new Form($db);
67 
68 $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;
69 
70 llxHeader('',$langs->trans("SellsJournal"),'','',0,0,'','',$morequery);
71 
72 
73 $year_current = strftime("%Y",dol_now());
74 $pastmonth = strftime("%m",dol_now()) - 1;
75 $pastmonthyear = $year_current;
76 if ($pastmonth == 0)
77 {
78  $pastmonth = 12;
79  $pastmonthyear--;
80 }
81 
82 $date_start=dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
83 $date_end=dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
84 
85 if (empty($date_start) || empty($date_end)) // We define date_start and date_end
86 {
87  $date_start=dol_get_first_day($pastmonthyear,$pastmonth,false); $date_end=dol_get_last_day($pastmonthyear,$pastmonth,false);
88 }
89 
90 $nom=$langs->trans("SellsJournal");
91 $periodlink='';
92 $exportlink='';
93 $builddate=dol_now();
94 $description=$langs->trans("DescSellsJournal").'<br>';
95 if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description.= $langs->trans("DepositsAreNotIncluded");
96 else $description.= $langs->trans("DepositsAreIncluded");
97 $period=$form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
98 report_header($name,'',$period,$periodlink,$description,$builddate,$exportlink);
99 
100 $p = explode(":", $conf->global->MAIN_INFO_SOCIETE_COUNTRY);
101 $idpays = $p[0];
102 
103 $sql = "SELECT f.rowid, f.facnumber, f.type, f.datef, f.ref_client,";
104 $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,";
105 $sql.= " s.rowid as socid, s.nom as name, s.code_compta, s.client,";
106 $sql.= " p.rowid as pid, p.ref as pref, p.accountancy_code_sell,";
107 $sql.= " ct.accountancy_code_sell as account_tva, ct.recuperableonly";
108 $sql.= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
109 $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = fd.fk_product";
110 $sql.= " JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
111 $sql.= " JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
112 $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 = '".$idpays."'";
113 $sql.= " WHERE f.entity = ".$conf->entity;
114 $sql.= " AND f.fk_statut > 0";
115 if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
116  $sql.= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_SITUATION.")";
117 }
118 else {
119  $sql.= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_STANDARD.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_DEPOSIT.",".Facture::TYPE_SITUATION.")";
120 }
121 
122 $sql.= " AND fd.product_type IN (0,1)";
123 if ($date_start && $date_end) $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
124 $sql.= " ORDER BY f.rowid";
125 
126 // TODO Find a better trick to avoid problem with some mysql installations
127 if (in_array($db->type, array('mysql', 'mysqli'))) $db->query('SET SQL_BIG_SELECTS=1');
128 
129 $result = $db->query($sql);
130 if ($result)
131 {
132  $tabfac = array();
133  $tabht = array();
134  $tabtva = array();
135  $tablocaltax1 = array();
136  $tablocaltax2 = array();
137  $tabttc = array();
138  $tabcompany = array();
139  $account_localtax1=0;
140  $account_localtax2=0;
141 
142  $num = $db->num_rows($result);
143  $i=0;
144  $resligne=array();
145  while ($i < $num)
146  {
147  $obj = $db->fetch_object($result);
148  // les variables
149  $cptcli = (! empty($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER)?$conf->global->ACCOUNTING_ACCOUNT_CUSTOMER:$langs->trans("CodeNotDef"));
150  $compta_soc = (! empty($obj->code_compta)?$obj->code_compta:$cptcli);
151  $compta_prod = $obj->accountancy_code_sell;
152  if (empty($compta_prod))
153  {
154  if($obj->product_type == 0) $compta_prod = (! empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT)?$conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT:$langs->trans("CodeNotDef"));
155  else $compta_prod = (! empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT)?$conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT:$langs->trans("CodeNotDef"));
156  }
157  $cpttva = (! empty($conf->global->ACCOUNTING_VAT_SOLD_ACCOUNT)?$conf->global->ACCOUNTING_VAT_SOLD_ACCOUNT:$langs->trans("CodeNotDef"));
158  $compta_tva = (! empty($obj->account_tva)?$obj->account_tva:$cpttva);
159 
160  $account_localtax1=getLocalTaxesFromRate($obj->tva_tx, 1, $obj->thirdparty, $mysoc);
161  $compta_localtax1= (! empty($account_localtax1[3])?$account_localtax1[3]:$langs->trans("CodeNotDef"));
162  $account_localtax2=getLocalTaxesFromRate($obj->tva_tx, 2, $obj->thirdparty, $mysoc);
163  $compta_localtax2= (! empty($account_localtax2[3])?$account_localtax2[3]:$langs->trans("CodeNotDef"));
164 
165  // Situation invoices handling
166  $line = new FactureLigne($db);
167  $line->fetch($obj->id); // id of line
168  $prev_progress = 0;
169  if ($obj->type==Facture::TYPE_SITUATION) {
170  // Avoid divide by 0
171  if ($obj->situation_percent == 0) {
172  $situation_ratio = 0;
173  } else {
174  $prev_progress = $line->get_prev_progress($obj->rowid); // id on invoice
175  $situation_ratio = ($obj->situation_percent - $prev_progress) / $obj->situation_percent;
176  }
177  } else {
178  $situation_ratio = 1;
179  }
180 
181  //la ligne facture
182  $tabfac[$obj->rowid]["date"] = $obj->datef;
183  $tabfac[$obj->rowid]["ref"] = $obj->facnumber;
184  $tabfac[$obj->rowid]["type"] = $obj->type;
185  if (! isset($tabttc[$obj->rowid][$compta_soc])) $tabttc[$obj->rowid][$compta_soc]=0;
186  if (! isset($tabht[$obj->rowid][$compta_prod])) $tabht[$obj->rowid][$compta_prod]=0;
187  if (! isset($tabtva[$obj->rowid][$compta_tva])) $tabtva[$obj->rowid][$compta_tva]=0;
188  if (! isset($tablocaltax1[$obj->rowid][$compta_localtax1])) $tablocaltax1[$obj->rowid][$compta_localtax1]=0;
189  if (! isset($tablocaltax2[$obj->rowid][$compta_localtax2])) $tablocaltax2[$obj->rowid][$compta_localtax2]=0;
190  $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc * $situation_ratio;
191  $tabht[$obj->rowid][$compta_prod] += $obj->total_ht * $situation_ratio;
192  if($obj->recuperableonly != 1) $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva * $situation_ratio;
193  $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
194  $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
195  $tabcompany[$obj->rowid]=array('id'=>$obj->socid, 'name'=>$obj->name, 'client'=>$obj->client);
196  $i++;
197  }
198 }
199 else {
200  dol_print_error($db);
201 }
202 
203 
204 /*
205  * Show result array
206  */
207 
208 
209 print '<table class="noborder" width="100%">';
210 print '<tr class="liste_titre">';
211 //print "<td>".$langs->trans("JournalNum")."</td>";
212 print '<td>'.$langs->trans('Date').'</td><td>'.$langs->trans('Piece').' ('.$langs->trans('InvoiceRef').')</td>';
213 print '<td>'.$langs->trans('Account').'</td>';
214 print '<td>'.$langs->trans('Type').'</td><td align="right">'.$langs->trans('Debit').'</td><td align="right">'.$langs->trans('Credit').'</td>';
215 print "</tr>\n";
216 
217 
218 $invoicestatic=new Facture($db);
219 $companystatic=new Client($db);
220 
221 foreach ($tabfac as $key => $val)
222 {
223  $invoicestatic->id=$key;
224  $invoicestatic->ref=$val["ref"];
225  $invoicestatic->type=$val["type"];
226 
227  $companystatic->id=$tabcompany[$key]['id'];
228  $companystatic->name=$tabcompany[$key]['name'];
229  $companystatic->client=$tabcompany[$key]['client'];
230 
231  $lines = array(
232  array(
233  'var' => $tabttc[$key],
234  'label' => $langs->trans('ThirdParty').' ('.$companystatic->getNomUrl(0, 'customer', 16).')',
235  'nomtcheck' => true,
236  'inv' => true
237  ),
238  array(
239  'var' => $tabht[$key],
240  'label' => $langs->trans('Products'),
241  ),
242  array(
243  'var' => $tabtva[$key],
244  'label' => $langs->trans('VAT')
245  ),
246  array(
247  'var' => $tablocaltax1[$key],
248  'label' => $langs->transcountry('LT1', $mysoc->country_code)
249  ),
250  array(
251  'var' => $tablocaltax2[$key],
252  'label' => $langs->transcountry('LT2', $mysoc->country_code)
253  )
254  );
255 
256  foreach ($lines as $line)
257  {
258  foreach ($line['var'] as $k => $mt)
259  {
260  if (isset($line['nomtcheck']) || $mt)
261  {
262  print '<tr class="oddeven">';
263  print "<td>".dol_print_date($db->jdate($val["date"]))."</td>";
264  print "<td>".$invoicestatic->getNomUrl(1)."</td>";
265  print "<td>".$k."</td><td>".$line['label']."</td>";
266 
267  if (isset($line['inv']))
268  {
269  print '<td align="right">'.($mt>=0?price($mt):'')."</td>";
270  print '<td align="right">'.($mt<0?price(-$mt):'')."</td>";
271  }
272  else
273  {
274  print '<td align="right">'.($mt<0?price(-$mt):'')."</td>";
275  print '<td align="right">'.($mt>=0?price($mt):'')."</td>";
276  }
277 
278  print "</tr>";
279  }
280  }
281  }
282 }
283 
284 print "</table>";
285 
286 // End of page
287 llxFooter();
288 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
const TYPE_STANDARD
Standard invoice.
print
Draft customers invoices.
Definition: index.php:91
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...
Class to manage customers or prospects.
const TYPE_REPLACEMENT
Replacement invoice.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:453
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
const TYPE_SITUATION
Situation invoice.
const TYPE_CREDIT_NOTE
Credit note invoice.
Class to manage generation of HTML components Only common components must be here.
llxHeader()
Empty header.
Definition: wrapper.php:44
if(GETPOST('cancel', 'alpha')) if(! GETPOST( 'confirmmassaction', 'alpha') &&$massaction !='presend' &&$massaction !='confirm_presend')
Draft customers invoices.
Definition: list.php:156
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...
const TYPE_DEPOSIT
Deposit invoice.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:467
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
Class to manage invoices.
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
Class to manage invoice lines.