dolibarr  16.0.5
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@open-dsi.fr>
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 <https://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', 'bills', '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->socid > 0) {
51  $socid = $user->socid;
52 }
53 if (isModEnabled('comptabilite')) {
54  $result = restrictedArea($user, 'compta', '', '', 'resultat');
55 }
56 if (isModEnabled('accounting')) {
57  $result = restrictedArea($user, 'accounting', '', '', 'comptarapport');
58 }
59 
60 /*
61  * Actions
62  */
63 
64 // None
65 
66 
67 
68 /*
69  * View
70  */
71 
72 $form = new Form($db);
73 
74 $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;
75 
76 llxHeader('', $langs->trans("SellsJournal"), '', '', 0, 0, '', '', $morequery);
77 
78 
79 $year_current = strftime("%Y", dol_now());
80 $pastmonth = strftime("%m", dol_now()) - 1;
81 $pastmonthyear = $year_current;
82 if ($pastmonth == 0) {
83  $pastmonth = 12;
84  $pastmonthyear--;
85 }
86 
87 $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
88 $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
89 
90 if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
91  $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
92  $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
93 }
94 
95 $name = $langs->trans("SellsJournal");
96 $periodlink = '';
97 $exportlink = '';
98 $builddate = dol_now();
99 $description = $langs->trans("DescSellsJournal").'<br>';
100 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
101  $description .= $langs->trans("DepositsAreNotIncluded");
102 } else {
103  $description .= $langs->trans("DepositsAreIncluded");
104 }
105 $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
106 report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink);
107 
108 $p = explode(":", $conf->global->MAIN_INFO_SOCIETE_COUNTRY);
109 $idpays = $p[0];
110 
111 $sql = "SELECT f.rowid, f.ref, f.type, f.datef, f.ref_client,";
112 $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,";
113 $sql .= " s.rowid as socid, s.nom as name, s.code_compta, s.client,";
114 $sql .= " p.rowid as pid, p.ref as pref,";
115 if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
116  $sql .= " ppe.accountancy_code_sell,";
117 } else {
118  $sql .= " p.accountancy_code_sell,";
119 }
120 $sql .= " ct.accountancy_code_sell as account_tva, ct.recuperableonly";
121 $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
122 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = fd.fk_product";
123 if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
124  $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
125 }
126 $sql .= " JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
127 $sql .= " JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
128 $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 = ".((int) $idpays);
129 $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
130 $sql .= " AND f.fk_statut > 0";
131 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
132  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_SITUATION.")";
133 } else {
134  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_STANDARD.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_DEPOSIT.",".Facture::TYPE_SITUATION.")";
135 }
136 
137 $sql .= " AND fd.product_type IN (0,1)";
138 if ($date_start && $date_end) {
139  $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
140 }
141 $sql .= " ORDER BY f.rowid";
142 
143 // TODO Find a better trick to avoid problem with some mysql installations
144 if (in_array($db->type, array('mysql', 'mysqli'))) {
145  $db->query('SET SQL_BIG_SELECTS=1');
146 }
147 
148 $result = $db->query($sql);
149 if ($result) {
150  $tabfac = array();
151  $tabht = array();
152  $tabtva = array();
153  $tablocaltax1 = array();
154  $tablocaltax2 = array();
155  $tabttc = array();
156  $tabcompany = array();
157  $account_localtax1 = 0;
158  $account_localtax2 = 0;
159 
160  $num = $db->num_rows($result);
161  $i = 0;
162  $resligne = array();
163  while ($i < $num) {
164  $obj = $db->fetch_object($result);
165  // les variables
166  $cptcli = (($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER != "") ? $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER : $langs->trans("CodeNotDef"));
167  $compta_soc = (!empty($obj->code_compta) ? $obj->code_compta : $cptcli);
168  $compta_prod = $obj->accountancy_code_sell;
169  if (empty($compta_prod)) {
170  if ($obj->product_type == 0) {
171  $compta_prod = (!empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
172  } else {
173  $compta_prod = (!empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
174  }
175  }
176  $cpttva = (!empty($conf->global->ACCOUNTING_VAT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_VAT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
177  $compta_tva = (!empty($obj->account_tva) ? $obj->account_tva : $cpttva);
178 
179  $account_localtax1 = getLocalTaxesFromRate($obj->tva_tx, 1, $obj->thirdparty, $mysoc);
180  $compta_localtax1 = (!empty($account_localtax1[3]) ? $account_localtax1[3] : $langs->trans("CodeNotDef"));
181  $account_localtax2 = getLocalTaxesFromRate($obj->tva_tx, 2, $obj->thirdparty, $mysoc);
182  $compta_localtax2 = (!empty($account_localtax2[3]) ? $account_localtax2[3] : $langs->trans("CodeNotDef"));
183 
184  // Situation invoices handling
185  $line = new FactureLigne($db);
186  $line->fetch($obj->id); // id of line
187  $prev_progress = 0;
188  if ($obj->type == Facture::TYPE_SITUATION) {
189  // Avoid divide by 0
190  if ($obj->situation_percent == 0) {
191  $situation_ratio = 0;
192  } else {
193  $prev_progress = $line->get_prev_progress($obj->rowid); // id on invoice
194  $situation_ratio = ($obj->situation_percent - $prev_progress) / $obj->situation_percent;
195  }
196  } else {
197  $situation_ratio = 1;
198  }
199 
200  //la ligne facture
201  $tabfac[$obj->rowid]["date"] = $obj->datef;
202  $tabfac[$obj->rowid]["ref"] = $obj->ref;
203  $tabfac[$obj->rowid]["type"] = $obj->type;
204  if (!isset($tabttc[$obj->rowid][$compta_soc])) {
205  $tabttc[$obj->rowid][$compta_soc] = 0;
206  }
207  if (!isset($tabht[$obj->rowid][$compta_prod])) {
208  $tabht[$obj->rowid][$compta_prod] = 0;
209  }
210  if (!isset($tabtva[$obj->rowid][$compta_tva])) {
211  $tabtva[$obj->rowid][$compta_tva] = 0;
212  }
213  if (!isset($tablocaltax1[$obj->rowid][$compta_localtax1])) {
214  $tablocaltax1[$obj->rowid][$compta_localtax1] = 0;
215  }
216  if (!isset($tablocaltax2[$obj->rowid][$compta_localtax2])) {
217  $tablocaltax2[$obj->rowid][$compta_localtax2] = 0;
218  }
219  $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc * $situation_ratio;
220  $tabht[$obj->rowid][$compta_prod] += $obj->total_ht * $situation_ratio;
221  if ($obj->recuperableonly != 1) {
222  $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva * $situation_ratio;
223  }
224  $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
225  $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
226  $tabcompany[$obj->rowid] = array('id'=>$obj->socid, 'name'=>$obj->name, 'client'=>$obj->client);
227  $i++;
228  }
229 } else {
230  dol_print_error($db);
231 }
232 
233 
234 /*
235  * Show result array
236  */
237 
238 
239 print '<table class="noborder centpercent">';
240 print '<tr class="liste_titre">';
241 //print "<td>".$langs->trans("JournalNum")."</td>";
242 print '<td>'.$langs->trans('Date').'</td><td>'.$langs->trans('Piece').' ('.$langs->trans('InvoiceRef').')</td>';
243 print '<td>'.$langs->trans('Account').'</td>';
244 print '<td>'.$langs->trans('Type').'</td><td class="right">'.$langs->trans('Debit').'</td><td class="right">'.$langs->trans('Credit').'</td>';
245 print "</tr>\n";
246 
247 
248 $invoicestatic = new Facture($db);
249 $companystatic = new Client($db);
250 
251 foreach ($tabfac as $key => $val) {
252  $invoicestatic->id = $key;
253  $invoicestatic->ref = $val["ref"];
254  $invoicestatic->type = $val["type"];
255 
256  $companystatic->id = $tabcompany[$key]['id'];
257  $companystatic->name = $tabcompany[$key]['name'];
258  $companystatic->client = $tabcompany[$key]['client'];
259 
260  $lines = array(
261  array(
262  'var' => $tabttc[$key],
263  'label' => $langs->trans('ThirdParty').' ('.$companystatic->getNomUrl(0, 'customer', 16).')',
264  'nomtcheck' => true,
265  'inv' => true
266  ),
267  array(
268  'var' => $tabht[$key],
269  'label' => $langs->trans('Products'),
270  ),
271  array(
272  'var' => $tabtva[$key],
273  'label' => $langs->trans('VAT')
274  ),
275  array(
276  'var' => $tablocaltax1[$key],
277  'label' => $langs->transcountry('LT1', $mysoc->country_code)
278  ),
279  array(
280  'var' => $tablocaltax2[$key],
281  'label' => $langs->transcountry('LT2', $mysoc->country_code)
282  )
283  );
284 
285  foreach ($lines as $line) {
286  foreach ($line['var'] as $k => $mt) {
287  if (isset($line['nomtcheck']) || $mt) {
288  print '<tr class="oddeven">';
289  print "<td>".dol_print_date($db->jdate($val["date"]))."</td>";
290  print "<td>".$invoicestatic->getNomUrl(1)."</td>";
291  print "<td>".$k."</td><td>".$line['label']."</td>";
292 
293  if (isset($line['inv'])) {
294  print '<td class="right">'.($mt >= 0 ?price($mt) : '')."</td>";
295  print '<td class="right">'.($mt < 0 ?price(-$mt) : '')."</td>";
296  } else {
297  print '<td class="right">'.($mt < 0 ?price(-$mt) : '')."</td>";
298  print '<td class="right">'.($mt >= 0 ?price($mt) : '')."</td>";
299  }
300 
301  print "</tr>";
302  }
303  }
304  }
305 }
306 
307 print "</table>";
308 
309 // End of page
310 llxFooter();
311 $db->close();
getLocalTaxesFromRate
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
Definition: functions.lib.php:6097
restrictedArea
restrictedArea($user, $features, $objectid=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.
Definition: security.lib.php:234
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
Facture
Class to manage invoices.
Definition: facture.class.php:60
Facture\TYPE_DEPOSIT
const TYPE_DEPOSIT
Deposit invoice.
Definition: facture.class.php:397
Facture\TYPE_REPLACEMENT
const TYPE_REPLACEMENT
Replacement invoice.
Definition: facture.class.php:387
FactureLigne
Class to manage invoice lines.
Definition: facture.class.php:5742
dol_get_first_day
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:551
dol_get_last_day
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:570
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:105
Facture\TYPE_CREDIT_NOTE
const TYPE_CREDIT_NOTE
Credit note invoice.
Definition: facture.class.php:392
Facture\TYPE_STANDARD
const TYPE_STANDARD
Standard invoice.
Definition: facture.class.php:382
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
price
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.
Definition: functions.lib.php:5541
dol_mktime
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...
Definition: functions.lib.php:2757
Facture\TYPE_SITUATION
const TYPE_SITUATION
Situation invoice.
Definition: facture.class.php:407
report_header
report_header($reportname, $notused, $period, $periodlink, $description, $builddate, $exportlink='', $moreparam=array(), $calcmode='', $varlink='')
Show header of a report.
Definition: report.lib.php:41
Client
Class to manage customers or prospects.
Definition: client.class.php:31
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59