31 require
'../../main.inc.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/report.lib.php';
33 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
34 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
35 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.class.php';
38 $langs->loadlangs(array(
'companies',
'other',
'bills',
'compta'));
40 $date_startmonth =
GETPOST(
'date_startmonth');
41 $date_startday =
GETPOST(
'date_startday');
42 $date_startyear =
GETPOST(
'date_startyear');
43 $date_endmonth =
GETPOST(
'date_endmonth');
44 $date_endday =
GETPOST(
'date_endday');
45 $date_endyear =
GETPOST(
'date_endyear');
48 if ($user->socid > 0) {
49 $socid = $user->socid;
55 $result =
restrictedArea($user,
'accounting',
'',
'',
'comptarapport');
70 $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;
72 llxHeader(
'', $langs->trans(
"PurchasesJournal"),
'',
'', 0, 0,
'',
'', $morequery);
76 $year_current = strftime(
"%Y",
dol_now());
77 $pastmonth = strftime(
"%m",
dol_now()) - 1;
78 $pastmonthyear = $year_current;
79 if ($pastmonth == 0) {
84 $date_start =
dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
85 $date_end =
dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
87 if (empty($date_start) || empty($date_end)) {
92 $name = $langs->trans(
"PurchasesJournal");
96 $description = $langs->trans(
"DescPurchasesJournal").
'<br>';
97 if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
98 $description .= $langs->trans(
"DepositsAreNotIncluded");
100 $description .= $langs->trans(
"DepositsAreIncluded");
102 $period =
$form->selectDate($date_start,
'date_start', 0, 0, 0,
'', 1, 0).
' - '.
$form->selectDate($date_end,
'date_end', 0, 0, 0,
'', 1, 0);
104 report_header($name,
'', $period, $periodlink, $description, $builddate, $exportlink);
106 $p = explode(
":", $conf->global->MAIN_INFO_SOCIETE_COUNTRY);
110 $sql =
"SELECT f.rowid, f.ref_supplier, f.type, f.datef, f.libelle as label,";
111 $sql .=
" fd.total_ttc, fd.tva_tx, fd.total_ht, fd.tva as total_tva, fd.product_type, fd.localtax1_tx, fd.localtax2_tx, fd.total_localtax1, fd.total_localtax2,";
112 $sql .=
" s.rowid as socid, s.nom as name, s.code_compta_fournisseur,";
113 $sql .=
" p.rowid as pid, p.ref as ref, p.accountancy_code_buy,";
114 $sql .=
" ct.accountancy_code_buy as account_tva, ct.recuperableonly";
115 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn_det as fd";
116 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_tva as ct ON fd.tva_tx = ct.taux AND fd.info_bits = ct.recuperableonly AND ct.fk_pays = ".((int) $idpays);
117 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = fd.fk_product";
118 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"facture_fourn as f ON f.rowid = fd.fk_facture_fourn";
119 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"societe as s ON s.rowid = f.fk_soc";
120 $sql .=
" WHERE f.fk_statut > 0 AND f.entity IN (".getEntity(
'invoice').
")";
121 if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
122 $sql .=
" AND f.type IN (0,1,2)";
124 $sql .=
" AND f.type IN (0,1,2,3)";
126 if ($date_start && $date_end) {
127 $sql .=
" AND f.datef >= '".$db->idate($date_start).
"' AND f.datef <= '".$db->idate($date_end).
"'";
131 if (in_array($db->type, array(
'mysql',
'mysqli'))) {
132 $db->query(
'SET SQL_BIG_SELECTS=1');
135 $result = $db->query($sql);
137 $num = $db->num_rows($result);
139 $cptfour = (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER !=
"") ? $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER : $langs->trans(
"CodeNotDef"));
140 $cpttva = (!empty($conf->global->ACCOUNTING_VAT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_VAT_BUY_ACCOUNT : $langs->trans(
"CodeNotDef"));
146 $tablocaltax1 = array();
147 $tablocaltax2 = array();
148 $tabcompany = array();
152 $obj = $db->fetch_object($result);
154 $compta_soc = (($obj->code_compta_fournisseur !=
"") ? $obj->code_compta_fournisseur : $cptfour);
155 $compta_prod = $obj->accountancy_code_buy;
156 if (empty($compta_prod)) {
157 if ($obj->product_type == 0) {
158 $compta_prod = (!empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : $langs->trans(
"CodeNotDef"));
160 $compta_prod = (!empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : $langs->trans(
"CodeNotDef"));
163 $compta_tva = (!empty($obj->account_tva) ? $obj->account_tva : $cpttva);
164 $compta_localtax1 = (!empty($obj->account_localtax1) ? $obj->account_localtax1 : $langs->trans(
"CodeNotDef"));
165 $compta_localtax2 = (!empty($obj->account_localtax2) ? $obj->account_localtax2 : $langs->trans(
"CodeNotDef"));
168 $compta_localtax1 = (!empty($account_localtax1[2]) ? $account_localtax1[2] : $langs->trans(
"CodeNotDef"));
170 $compta_localtax2 = (!empty($account_localtax2[2]) ? $account_localtax2[2] : $langs->trans(
"CodeNotDef"));
172 $tabfac[$obj->rowid][
"date"] = $obj->datef;
173 $tabfac[$obj->rowid][
"ref"] = $obj->ref_supplier;
174 $tabfac[$obj->rowid][
"type"] = $obj->type;
175 $tabfac[$obj->rowid][
"lib"] = $obj->label;
176 $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc;
177 $tabht[$obj->rowid][$compta_prod] += $obj->total_ht;
178 if ($obj->recuperableonly != 1) {
179 $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
181 $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
182 $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
183 $tabcompany[$obj->rowid] = array(
'id'=>$obj->socid,
'name'=>$obj->name);
194 print
"<table class=\"noborder\" width=\"100%\">";
195 print
"<tr class=\"liste_titre\">";
197 print
"<td>".$langs->trans(
"Date").
"</td>";
198 print
"<td>".$langs->trans(
"Piece").
' ('.$langs->trans(
"InvoiceRef").
")</td>";
199 print
"<td>".$langs->trans(
"Account").
"</td>";
200 print
"<td>".$langs->trans(
"Type").
"</td><td class='right'>".$langs->trans(
"Debit").
"</td><td class='right'>".$langs->trans(
"Credit").
"</td>";
207 foreach ($tabfac as $key => $val) {
208 $invoicestatic->id = $key;
209 $invoicestatic->ref = $val[
"ref"];
210 $invoicestatic->type = $val[
"type"];
212 $companystatic->id = $tabcompany[$key][
'id'];
213 $companystatic->name = $tabcompany[$key][
'name'];
217 'var' => $tabht[$key],
218 'label' => $langs->trans(
'Products'),
221 'var' => $tabtva[$key],
222 'label' => $langs->trans(
'VAT')
225 'var' => $tablocaltax1[$key],
226 'label' => $langs->transcountry(
'LT1', $mysoc->country_code)
229 'var' => $tablocaltax2[$key],
230 'label' => $langs->transcountry(
'LT2', $mysoc->country_code)
233 'var' => $tabttc[$key],
234 'label' => $langs->trans(
'ThirdParty').
' ('.$companystatic->getNomUrl(0,
'supplier', 16).
')',
240 foreach ($lines as $line) {
241 foreach ($line[
'var'] as $k => $mt) {
242 if (isset($line[
'nomtcheck']) || $mt) {
243 print
'<tr class="oddeven">';
244 print
"<td>".dol_print_date($db->jdate($val[
"date"])).
"</td>";
245 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
246 print
"<td>".$k.
"</td><td>".$line[
'label'].
"</td>";
248 if (isset($line[
'inv'])) {
249 print
'<td class="right">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
250 print
'<td class="right">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
252 print
'<td class="right">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
253 print
'<td class="right">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";