PageRenderTime 164ms CodeModel.GetById 70ms app.highlight 15ms RepoModel.GetById 56ms app.codeStats 0ms

/htdocs/compta/journal/sellsjournal.php

https://bitbucket.org/speedealing/speedealing
PHP | 238 lines | 157 code | 35 blank | 46 comment | 19 complexity | 40dc0a6ceb82b34abbf5e68da9801930 MD5 | raw 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		Juanjo Menent		<jmenent@2byte.es>
  5 * Copyright (C) 2012		Regis Houssin		<regis.houssin@capnetworks.com>
  6 * Copyright (C) 2011-2012 Alexandre Spangaro	  <alexandre.spangaro@gmail.com>
  7 *
  8 * This program is free software; you can redistribute it and/or modify
  9 * it under the terms of the GNU General Public License as published by
 10 * the Free Software Foundation; either version 3 of the License, or
 11 * (at your option) any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 20 */
 21
 22/**
 23 *   	\file       htdocs/compta/journal/sellsjournal.php
 24 *		\ingroup    societe, facture
 25 *		\brief      Page with sells journal
 26 */
 27require '../../main.inc.php';
 28require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
 29require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
 30require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
 31require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php';
 32
 33
 34$langs->load("companies");
 35$langs->load("other");
 36$langs->load("compta");
 37
 38$date_startmonth=GETPOST('date_startmonth');
 39$date_startday=GETPOST('date_startday');
 40$date_startyear=GETPOST('date_startyear');
 41$date_endmonth=GETPOST('date_endmonth');
 42$date_endday=GETPOST('date_endday');
 43$date_endyear=GETPOST('date_endyear');
 44
 45// Protection if external user
 46if ($user->societe_id > 0)
 47	accessforbidden();
 48
 49$result = restrictedArea($user, 'societe&facture');
 50
 51/*
 52 * Actions
 53 */
 54
 55// None
 56
 57
 58
 59/*
 60 * View
 61 */
 62
 63$form=new Form($db);
 64
 65llxHeader('',$langs->trans("SellsJournal"),'');
 66
 67
 68$year_current = strftime("%Y",dol_now());
 69$pastmonth = strftime("%m",dol_now()) - 1;
 70$pastmonthyear = $year_current;
 71if ($pastmonth == 0)
 72{
 73	$pastmonth = 12;
 74	$pastmonthyear--;
 75}
 76
 77$date_start=dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
 78$date_end=dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
 79
 80if (empty($date_start) || empty($date_end)) // We define date_start and date_end
 81{
 82	$date_start=dol_get_first_day($pastmonthyear,$pastmonth,false); $date_end=dol_get_last_day($pastmonthyear,$pastmonth,false);
 83}
 84
 85$nom=$langs->trans("SellsJournal");
 86$nomlink='';
 87$periodlink='';
 88$exportlink='';
 89$builddate=time();
 90$description=$langs->trans("DescSellsJournal");
 91$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);
 92report_header($nom,$nomlink,$period,$periodlink,$description,$builddate,$exportlink);
 93
 94$p = explode(":", $conf->global->MAIN_INFO_SOCIETE_PAYS);
 95$idpays = $p[0];
 96
 97$sql = "SELECT f.rowid, f.facnumber, f.type, f.datef, f.ref_client,";
 98$sql.= " fd.product_type, fd.total_ht, fd.total_tva, fd.tva_tx, fd.total_ttc,";
 99$sql.= " s.rowid as socid, s.nom as name, s.code_compta, s.client,";
100$sql.= " p.rowid as pid, p.ref as pref, p.accountancy_code_sell,";
101$sql.= " ct.accountancy_code_sell as account_tva";
102$sql.= " FROM ".MAIN_DB_PREFIX."facturedet fd";
103$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product p ON p.rowid = fd.fk_product";
104$sql.= " JOIN ".MAIN_DB_PREFIX."facture f ON f.rowid = fd.fk_facture";
105$sql.= " JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid = f.fk_soc";
106$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_tva ct ON fd.tva_tx = ct.taux AND ct.fk_pays = '".$idpays."'";
107$sql.= " WHERE f.entity = ".$conf->entity;
108$sql.= " AND f.fk_statut > 0";
109$sql.= " AND fd.product_type IN (0,1)";
110if ($date_start && $date_end) $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
111$sql.= " ORDER BY f.rowid";
112
113$result = $db->query($sql);
114if ($result)
115{
116	$tabfac = array();
117	$tabht = array();
118	$tabtva = array();
119	$tabttc = array();
120	$tabcompany = array();
121
122	$num = $db->num_rows($result);
123   	$i=0;
124   	$resligne=array();
125   	while ($i < $num)
126   	{
127   	    $obj = $db->fetch_object($result);
128   	    // les variables
129   	    $cptcli = (! empty($conf->global->COMPTA_ACCOUNT_CUSTOMER)?$conf->global->COMPTA_ACCOUNT_CUSTOMER:$langs->trans("CodeNotDef"));
130   	    $compta_soc = (! empty($obj->code_compta)?$obj->code_compta:$cptcli);
131		$compta_prod = $obj->accountancy_code_sell;
132		if (empty($compta_prod))
133		{
134			if($obj->product_type == 0) $compta_prod = (! empty($conf->global->COMPTA_PRODUCT_SOLD_ACCOUNT)?$conf->global->COMPTA_PRODUCT_SOLD_ACCOUNT:$langs->trans("CodeNotDef"));
135			else $compta_prod = (! empty($conf->global->COMPTA_SERVICE_SOLD_ACCOUNT)?$conf->global->COMPTA_SERVICE_SOLD_ACCOUNT:$langs->trans("CodeNotDef"));
136		}
137		$cpttva = (! empty($conf->global->COMPTA_VAT_ACCOUNT)?$conf->global->COMPTA_VAT_ACCOUNT:$langs->trans("CodeNotDef"));
138		$compta_tva = (! empty($obj->account_tva)?$obj->account_tva:$cpttva);
139
140    	//la ligne facture
141   		$tabfac[$obj->rowid]["date"] = $obj->datef;
142   		$tabfac[$obj->rowid]["ref"] = $obj->facnumber;
143   		$tabfac[$obj->rowid]["type"] = $obj->type;
144   		if (! isset($tabttc[$obj->rowid][$compta_soc])) $tabttc[$obj->rowid][$compta_soc]=0;
145   		if (! isset($tabht[$obj->rowid][$compta_prod])) $tabht[$obj->rowid][$compta_prod]=0;
146   		if (! isset($tabtva[$obj->rowid][$compta_tva])) $tabtva[$obj->rowid][$compta_tva]=0;
147   		$tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc;
148   		$tabht[$obj->rowid][$compta_prod] += $obj->total_ht;
149   		$tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
150   		$tabcompany[$obj->rowid]=array('id'=>$obj->socid, 'name'=>$obj->name, 'client'=>$obj->client);
151   		$i++;
152   	}
153}
154else {
155    dol_print_error($db);
156}
157
158
159/*
160 * Show result array
161 */
162
163
164$i = 0;
165print "<table class=\"noborder\" width=\"100%\">";
166print "<tr class=\"liste_titre\">";
167//print "<td>".$langs->trans("JournalNum")."</td>";
168print "<td>".$langs->trans("Date")."</td><td>".$langs->trans("Piece").' ('.$langs->trans("InvoiceRef").")</td>";
169print "<td>".$langs->trans("Account")."</td>";
170print "<td>".$langs->trans("Type")."</td><td align='right'>".$langs->trans("Debit")."</td><td align='right'>".$langs->trans("Credit")."</td>";
171print "</tr>\n";
172
173$var=true;
174$r='';
175
176$invoicestatic=new Facture($db);
177$companystatic=new Client($db);
178
179foreach ($tabfac as $key => $val)
180{
181	$invoicestatic->id=$key;
182	$invoicestatic->ref=$val["ref"];
183	$invoicestatic->type=$val["type"];
184
185	print "<tr ".$bc[$var].">";
186	// third party
187	//print "<td>".$conf->global->COMPTA_JOURNAL_SELL."</td>";
188	print "<td>".$val["date"]."</td>";
189	print "<td>".$invoicestatic->getNomUrl(1)."</td>";
190	foreach ($tabttc[$key] as $k => $mt)
191	{
192    	$companystatic->id=$tabcompany[$key]['id'];
193    	$companystatic->name=$tabcompany[$key]['name'];
194    	$companystatic->client=$tabcompany[$key]['client'];
195    	print "<td>".$k;
196		print "</td><td>".$langs->trans("ThirdParty");
197		print ' ('.$companystatic->getNomUrl(0,'customer',16).')';
198		print "</td><td align='right'>".($mt>=0?price($mt):'')."</td><td align='right'>".($mt<0?price(-$mt):'')."</td>";
199	}
200	print "</tr>";
201	// product
202	foreach ($tabht[$key] as $k => $mt)
203	{
204		if ($mt)
205		{
206			print "<tr ".$bc[$var].">";
207			//print "<td>".$conf->global->COMPTA_JOURNAL_SELL."</td>";
208			print "<td>".$val["date"]."</td>";
209			print "<td>".$invoicestatic->getNomUrl(1)."</td>";
210			print "<td>".$k;
211			print "</td><td>".$langs->trans("Products")."</td><td align='right'>".($mt<0?price(-$mt):'')."</td><td align='right'>".($mt>=0?price($mt):'')."</td></tr>";
212		}
213	}
214	// vat
215	//var_dump($tabtva);
216	foreach ($tabtva[$key] as $k => $mt)
217	{
218	    if ($mt)
219	    {
220    		print "<tr ".$bc[$var].">";
221    		//print "<td>".$conf->global->COMPTA_JOURNAL_SELL."</td>";
222    		print "<td>".$val["date"]."</td>";
223    		print "<td>".$invoicestatic->getNomUrl(1)."</td>";
224    		print "<td>".$k;
225    		print "</td><td>".$langs->trans("VAT")."</td><td align='right'>".($mt<0?price(-$mt):'')."</td><td align='right'>".($mt>=0?price($mt):'')."</td></tr>";
226	    }
227	}
228
229	$var = !$var;
230}
231
232print "</table>";
233
234
235// End of page
236llxFooter();
237$db->close();
238?>