PageRenderTime 403ms CodeModel.GetById 97ms app.highlight 197ms RepoModel.GetById 95ms app.codeStats 1ms

/htdocs/compta/journal/purchasesjournal.php

https://bitbucket.org/speedealing/speedealing
PHP | 238 lines | 157 code | 35 blank | 46 comment | 16 complexity | 4de498380d35676d40d5ae7ebbfa9761 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/purchasesjournal.php
 24 *		\ingroup    societe, fournisseur, facture
 25 *		\brief      Page with purchases 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.'/fourn/class/fournisseur.facture.class.php';
 31require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
 32
 33$langs->load("companies");
 34$langs->load("other");
 35$langs->load("compta");
 36
 37$date_startmonth=GETPOST('date_startmonth');
 38$date_startday=GETPOST('date_startday');
 39$date_startyear=GETPOST('date_startyear');
 40$date_endmonth=GETPOST('date_endmonth');
 41$date_endday=GETPOST('date_endday');
 42$date_endyear=GETPOST('date_endyear');
 43
 44// Protection if external user
 45if ($user->societe_id > 0)
 46	accessforbidden();
 47
 48$result = restrictedArea($user, 'societe&facture');
 49
 50
 51/*
 52 * Actions
 53 */
 54
 55// None
 56
 57
 58/*
 59 * View
 60 */
 61
 62llxHeader('','','');
 63
 64$form=new Form($db);
 65
 66$year_current = strftime("%Y",dol_now());
 67$pastmonth = strftime("%m",dol_now()) - 1;
 68$pastmonthyear = $year_current;
 69if ($pastmonth == 0)
 70{
 71	$pastmonth = 12;
 72	$pastmonthyear--;
 73}
 74
 75$date_start=dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
 76$date_end=dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
 77
 78if (empty($date_start) || empty($date_end)) // We define date_start and date_end
 79{
 80	$date_start=dol_get_first_day($pastmonthyear,$pastmonth,false); $date_end=dol_get_last_day($pastmonthyear,$pastmonth,false);
 81}
 82
 83$nom=$langs->trans("PurchasesJournal");
 84$nomlink='';
 85$periodlink='';
 86$exportlink='';
 87$builddate=time();
 88$description=$langs->trans("DescPurchasesJournal");
 89$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);
 90report_header($nom,$nomlink,$period,$periodlink,$description,$builddate,$exportlink);
 91
 92$p = explode(":", $conf->global->MAIN_INFO_SOCIETE_PAYS);
 93$idpays = $p[0];
 94
 95$sql = "SELECT f.rowid, f.facnumber, f.type, f.datef, f.libelle,";
 96$sql.= " fd.total_ttc, fd.tva_tx, fd.total_ht, fd.tva as total_tva, fd.product_type,";
 97$sql.= " s.rowid as socid, s.nom as name, s.code_compta_fournisseur,";
 98$sql.= " p.rowid as pid, p.ref as ref, p.accountancy_code_buy,";
 99$sql.= " ct.accountancy_code_buy as account_tva";
100$sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn_det fd";
101$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_tva ct ON fd.tva_tx = ct.taux AND ct.fk_pays = '".$idpays."'";
102$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product p ON p.rowid = fd.fk_product";
103$sql.= " JOIN ".MAIN_DB_PREFIX."facture_fourn f ON f.rowid = fd.fk_facture_fourn";
104$sql.= " JOIN ".MAIN_DB_PREFIX."societe s ON s.rowid = f.fk_soc" ;
105$sql.= " WHERE f.fk_statut > 0 AND f.entity = ".$conf->entity;
106if ($date_start && $date_end) $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
107
108$result = $db->query($sql);
109if ($result)
110{
111	$num = $db->num_rows($result);
112	// les variables
113	$cptfour = (! empty($conf->global->COMPTA_ACCOUNT_SUPPLIER)?$conf->global->COMPTA_ACCOUNT_SUPPLIER:$langs->trans("CodeNotDef"));
114	$cpttva = (! empty($conf->global->COMPTA_VAT_ACCOUNT)?$conf->global->COMPTA_VAT_ACCOUNT:$langs->trans("CodeNotDef"));
115
116	$tabfac = array();
117	$tabht = array();
118	$tabtva = array();
119	$tabttc = array();
120	$tabcompany = array();
121
122	$i=0;
123	while ($i < $num)
124	{
125		$obj = $db->fetch_object($result);
126		// contrĂ´les
127		$compta_soc = (! empty($obj->code_compta_fournisseur)?$obj->code_compta_fournisseur:$cptfour);
128		$compta_prod = $obj->accountancy_code_buy;
129		if (empty($compta_prod))
130		{
131			if($obj->product_type == 0) $compta_prod = (! empty($conf->global->COMPTA_PRODUCT_BUY_ACCOUNT)?$conf->global->COMPTA_PRODUCT_BUY_ACCOUNT:$langs->trans("CodeNotDef"));
132			else $compta_prod = (! empty($conf->global->COMPTA_SERVICE_BUY_ACCOUNT)?$conf->global->COMPTA_SERVICE_BUY_ACCOUNT:$langs->trans("CodeNotDef"));
133		}
134		$compta_tva = (! empty($obj->account_tva)?$obj->account_tva:$cpttva);
135
136		$tabfac[$obj->rowid]["date"] = $obj->datef;
137		$tabfac[$obj->rowid]["ref"] = $obj->facnumber;
138		$tabfac[$obj->rowid]["type"] = $obj->type;
139		$tabfac[$obj->rowid]["lib"] = $obj->libelle;
140		$tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc;
141		$tabht[$obj->rowid][$compta_prod] += $obj->total_ht;
142		$tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
143		$tabcompany[$obj->rowid]=array('id'=>$obj->socid,'name'=>$obj->name);
144
145		$i++;
146	}
147}
148else {
149	dol_print_error($db);
150}
151
152/*
153 * Show result array
154 */
155$i = 0;
156print "<table class=\"noborder\" width=\"100%\">";
157print "<tr class=\"liste_titre\">";
158///print "<td>".$langs->trans("JournalNum")."</td>";
159print "<td>".$langs->trans("Date")."</td>";
160print "<td>".$langs->trans("Piece").' ('.$langs->trans("InvoiceRef").")</td>";
161print "<td>".$langs->trans("Account")."</td>";
162print "<td>".$langs->trans("Type")."</td><td align='right'>".$langs->trans("Debit")."</td><td align='right'>".$langs->trans("Credit")."</td>";
163print "</tr>\n";
164
165$var=true;
166$r='';
167
168$invoicestatic=new FactureFournisseur($db);
169$companystatic=new Fournisseur($db);
170
171foreach ($tabfac as $key => $val)
172{
173	$invoicestatic->id=$key;
174	$invoicestatic->ref=$val["ref"];
175	$invoicestatic->type=$val["type"];
176
177	// product
178	foreach ($tabht[$key] as $k => $mt)
179	{
180		if ($mt)
181		{
182			print "<tr ".$bc[$var]." >";
183			//print "<td>".$conf->global->COMPTA_JOURNAL_BUY."</td>";
184			print "<td>".$val["date"]."</td>";
185			print "<td>".$invoicestatic->getNomUrl(1)."</td>";
186			print "<td>".$k."</td><td>".$langs->trans("Products")."</td>";
187			print '<td align="right">'.($mt>=0?price($mt):'')."</td>";
188			print '<td align="right">'.($mt<0?price(-$mt):'')."</td>";
189			print "</tr>";
190		}
191	}
192	// vat
193	//var_dump($tabtva);
194	foreach ($tabtva[$key] as $k => $mt)
195	{
196		if ($mt)
197		{
198			print "<tr ".$bc[$var]." >";
199			//print "<td>".$conf->global->COMPTA_JOURNAL_BUY."</td>";
200			print "<td>".$val["date"]."</td>";
201			print "<td>".$invoicestatic->getNomUrl(1)."</td>";
202			print "<td>".$k."</td><td>".$langs->trans("VAT")." ".$key."</td>";
203			print '<td align="right">'.($mt>=0?price($mt):'')."</td>";
204			print '<td align="right">'.($mt<0?price(-$mt):'')."</td>";
205			print "</tr>";
206		}
207	}
208	print "<tr ".$bc[$var].">";
209	// third party
210	//print "<td>".$conf->global->COMPTA_JOURNAL_BUY."</td>";
211	print "<td>".$val["date"]."</td>";
212	print "<td>".$invoicestatic->getNomUrl(1)."</td>";
213
214	foreach ($tabttc[$key] as $k => $mt)
215	{
216    	$companystatic->id=$tabcompany[$key]['id'];
217    	$companystatic->name=$tabcompany[$key]['name'];
218
219    	print "<td>".$k;
220	    print "</td><td>".$langs->trans("ThirdParty");
221		print ' ('.$companystatic->getNomUrl(0,'supplier',16).')';
222	    print "</td>";
223	    print '<td align="right">'.($mt<0?price(-$mt):'')."</td>";
224	    print '<td align="right">'.($mt>=0?price($mt):'')."</td>";
225	}
226	print "</tr>";
227
228	$var = !$var;
229}
230
231print "</table>";
232
233
234// End of page
235llxFooter();
236
237$db->close();
238?>