dolibarr  7.0.0-beta
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@capnetworks.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  *
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 <http://www.gnu.org/licenses/>.
23  */
24 
30 global $mysoc;
31 
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.'/compta/facture/class/facture.class.php';
36 require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php';
37 
38 
39 $langs->load("companies");
40 $langs->load("other");
41 $langs->load("compta");
42 
43 $date_startmonth=GETPOST('date_startmonth');
44 $date_startday=GETPOST('date_startday');
45 $date_startyear=GETPOST('date_startyear');
46 $date_endmonth=GETPOST('date_endmonth');
47 $date_endday=GETPOST('date_endday');
48 $date_endyear=GETPOST('date_endyear');
49 
50 // Security check
51 if ($user->societe_id > 0) $socid = $user->societe_id;
52 if (! empty($conf->comptabilite->enabled)) $result=restrictedArea($user,'compta','','','resultat');
53 if (! empty($conf->accounting->enabled)) $result=restrictedArea($user,'accounting','','','comptarapport');
54 
55 /*
56  * Actions
57  */
58 
59 // None
60 
61 
62 
63 /*
64  * View
65  */
66 
67 $form=new Form($db);
68 
69 $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;
70 
71 llxHeader('',$langs->trans("SellsJournal"),'','',0,0,'','',$morequery);
72 
73 
74 $year_current = strftime("%Y",dol_now());
75 $pastmonth = strftime("%m",dol_now()) - 1;
76 $pastmonthyear = $year_current;
77 if ($pastmonth == 0)
78 {
79  $pastmonth = 12;
80  $pastmonthyear--;
81 }
82 
83 $date_start=dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
84 $date_end=dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
85 
86 if (empty($date_start) || empty($date_end)) // We define date_start and date_end
87 {
88  $date_start=dol_get_first_day($pastmonthyear,$pastmonth,false); $date_end=dol_get_last_day($pastmonthyear,$pastmonth,false);
89 }
90 
91 $nom=$langs->trans("SellsJournal");
92 $periodlink='';
93 $exportlink='';
94 $builddate=dol_now();
95 $description=$langs->trans("DescSellsJournal").'<br>';
96 if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description.= $langs->trans("DepositsAreNotIncluded");
97 else $description.= $langs->trans("DepositsAreIncluded");
98 $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);
99 report_header($name,'',$period,$periodlink,$description,$builddate,$exportlink);
100 
101 $p = explode(":", $conf->global->MAIN_INFO_SOCIETE_COUNTRY);
102 $idpays = $p[0];
103 
104 $sql = "SELECT f.rowid, f.facnumber, f.type, f.datef, f.ref_client,";
105 $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,";
106 $sql.= " s.rowid as socid, s.nom as name, s.code_compta, s.client,";
107 $sql.= " p.rowid as pid, p.ref as pref, p.accountancy_code_sell,";
108 $sql.= " ct.accountancy_code_sell as account_tva, ct.recuperableonly";
109 $sql.= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
110 $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = fd.fk_product";
111 $sql.= " JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
112 $sql.= " JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
113 $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."'";
114 $sql.= " WHERE f.entity = ".$conf->entity;
115 $sql.= " AND f.fk_statut > 0";
116 if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
117  $sql.= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_SITUATION.")";
118 }
119 else {
120  $sql.= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_STANDARD.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_DEPOSIT.",".Facture::TYPE_SITUATION.")";
121 }
122 
123 $sql.= " AND fd.product_type IN (0,1)";
124 if ($date_start && $date_end) $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
125 $sql.= " ORDER BY f.rowid";
126 
127 // TODO Find a better trick to avoid problem with some mysql installations
128 if (in_array($db->type, array('mysql', 'mysqli'))) $db->query('SET SQL_BIG_SELECTS=1');
129 
130 $result = $db->query($sql);
131 if ($result)
132 {
133  $tabfac = array();
134  $tabht = array();
135  $tabtva = array();
136  $tablocaltax1 = array();
137  $tablocaltax2 = array();
138  $tabttc = array();
139  $tabcompany = array();
140  $account_localtax1=0;
141  $account_localtax2=0;
142 
143  $num = $db->num_rows($result);
144  $i=0;
145  $resligne=array();
146  while ($i < $num)
147  {
148  $obj = $db->fetch_object($result);
149  // les variables
150  $cptcli = (! empty($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER)?$conf->global->ACCOUNTING_ACCOUNT_CUSTOMER:$langs->trans("CodeNotDef"));
151  $compta_soc = (! empty($obj->code_compta)?$obj->code_compta:$cptcli);
152  $compta_prod = $obj->accountancy_code_sell;
153  if (empty($compta_prod))
154  {
155  if($obj->product_type == 0) $compta_prod = (! empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT)?$conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT:$langs->trans("CodeNotDef"));
156  else $compta_prod = (! empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT)?$conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT:$langs->trans("CodeNotDef"));
157  }
158  $cpttva = (! empty($conf->global->ACCOUNTING_VAT_SOLD_ACCOUNT)?$conf->global->ACCOUNTING_VAT_SOLD_ACCOUNT:$langs->trans("CodeNotDef"));
159  $compta_tva = (! empty($obj->account_tva)?$obj->account_tva:$cpttva);
160 
161  $account_localtax1=getLocalTaxesFromRate($obj->tva_tx, 1, $obj->thirdparty, $mysoc);
162  $compta_localtax1= (! empty($account_localtax1[3])?$account_localtax1[3]:$langs->trans("CodeNotDef"));
163  $account_localtax2=getLocalTaxesFromRate($obj->tva_tx, 2, $obj->thirdparty, $mysoc);
164  $compta_localtax2= (! empty($account_localtax2[3])?$account_localtax2[3]:$langs->trans("CodeNotDef"));
165 
166  // Situation invoices handling
167  $line = new FactureLigne($db);
168  $line->fetch($obj->id); // id of line
169  $prev_progress = 0;
170  if ($obj->type==Facture::TYPE_SITUATION) {
171  // Avoid divide by 0
172  if ($obj->situation_percent == 0) {
173  $situation_ratio = 0;
174  } else {
175  $prev_progress = $line->get_prev_progress($obj->rowid); // id on invoice
176  $situation_ratio = ($obj->situation_percent - $prev_progress) / $obj->situation_percent;
177  }
178  } else {
179  $situation_ratio = 1;
180  }
181 
182  //la ligne facture
183  $tabfac[$obj->rowid]["date"] = $obj->datef;
184  $tabfac[$obj->rowid]["ref"] = $obj->facnumber;
185  $tabfac[$obj->rowid]["type"] = $obj->type;
186  if (! isset($tabttc[$obj->rowid][$compta_soc])) $tabttc[$obj->rowid][$compta_soc]=0;
187  if (! isset($tabht[$obj->rowid][$compta_prod])) $tabht[$obj->rowid][$compta_prod]=0;
188  if (! isset($tabtva[$obj->rowid][$compta_tva])) $tabtva[$obj->rowid][$compta_tva]=0;
189  if (! isset($tablocaltax1[$obj->rowid][$compta_localtax1])) $tablocaltax1[$obj->rowid][$compta_localtax1]=0;
190  if (! isset($tablocaltax2[$obj->rowid][$compta_localtax2])) $tablocaltax2[$obj->rowid][$compta_localtax2]=0;
191  $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc * $situation_ratio;
192  $tabht[$obj->rowid][$compta_prod] += $obj->total_ht * $situation_ratio;
193  if($obj->recuperableonly != 1) $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva * $situation_ratio;
194  $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
195  $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
196  $tabcompany[$obj->rowid]=array('id'=>$obj->socid, 'name'=>$obj->name, 'client'=>$obj->client);
197  $i++;
198  }
199 }
200 else {
201  dol_print_error($db);
202 }
203 
204 
205 /*
206  * Show result array
207  */
208 
209 
210 print '<table class="noborder" width="100%">';
211 print '<tr class="liste_titre">';
212 //print "<td>".$langs->trans("JournalNum")."</td>";
213 print '<td>'.$langs->trans('Date').'</td><td>'.$langs->trans('Piece').' ('.$langs->trans('InvoiceRef').')</td>';
214 print '<td>'.$langs->trans('Account').'</td>';
215 print '<td>'.$langs->trans('Type').'</td><td align="right">'.$langs->trans('Debit').'</td><td align="right">'.$langs->trans('Credit').'</td>';
216 print "</tr>\n";
217 
218 
219 $invoicestatic=new Facture($db);
220 $companystatic=new Client($db);
221 
222 foreach ($tabfac as $key => $val)
223 {
224  $invoicestatic->id=$key;
225  $invoicestatic->ref=$val["ref"];
226  $invoicestatic->type=$val["type"];
227 
228  $companystatic->id=$tabcompany[$key]['id'];
229  $companystatic->name=$tabcompany[$key]['name'];
230  $companystatic->client=$tabcompany[$key]['client'];
231 
232  $lines = array(
233  array(
234  'var' => $tabttc[$key],
235  'label' => $langs->trans('ThirdParty').' ('.$companystatic->getNomUrl(0, 'customer', 16).')',
236  'nomtcheck' => true,
237  'inv' => true
238  ),
239  array(
240  'var' => $tabht[$key],
241  'label' => $langs->trans('Products'),
242  ),
243  array(
244  'var' => $tabtva[$key],
245  'label' => $langs->trans('VAT')
246  ),
247  array(
248  'var' => $tablocaltax1[$key],
249  'label' => $langs->transcountry('LT1', $mysoc->country_code)
250  ),
251  array(
252  'var' => $tablocaltax2[$key],
253  'label' => $langs->transcountry('LT2', $mysoc->country_code)
254  )
255  );
256 
257  foreach ($lines as $line)
258  {
259  foreach ($line['var'] as $k => $mt)
260  {
261  if (isset($line['nomtcheck']) || $mt)
262  {
263  print '<tr class="oddeven">';
264  print "<td>".dol_print_date($db->jdate($val["date"]))."</td>";
265  print "<td>".$invoicestatic->getNomUrl(1)."</td>";
266  print "<td>".$k."</td><td>".$line['label']."</td>";
267 
268  if (isset($line['inv']))
269  {
270  print '<td align="right">'.($mt>=0?price($mt):'')."</td>";
271  print '<td align="right">'.($mt<0?price(-$mt):'')."</td>";
272  }
273  else
274  {
275  print '<td align="right">'.($mt<0?price(-$mt):'')."</td>";
276  print '<td align="right">'.($mt>=0?price($mt):'')."</td>";
277  }
278 
279  print "</tr>";
280  }
281  }
282  }
283 }
284 
285 print "</table>";
286 
287 
288 // End of page
289 llxFooter();
290 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
const TYPE_STANDARD
Standard invoice.
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...
if(GETPOST('cancel','alpha')) if(!GETPOST('confirmmassaction','alpha')&&$massaction!= 'presend'&&$massaction!= 'confirm_presend')
Draft customers invoices.
Definition: list.php:147
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:445
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
if(empty($reshook)) $form
View.
Definition: perms.php:103
const TYPE_SITUATION
Situation invoice.
const TYPE_CREDIT_NOTE
Credit note invoice.
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...
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:459
print
Draft customers invoices.
Definition: index.php:91
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
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.
Class to manage invoice lines.