purchasesjournal.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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) 2013 Marcos García <marcosgdf@gmail.com>
  8. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/compta/journal/purchasesjournal.php
  25. * \ingroup societe, fournisseur, facture
  26. * \brief Page with purchases journal
  27. */
  28. global $mysoc;
  29. require '../../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
  34. // Load translation files required by the page
  35. $langs->loadlangs(array('companies', 'other', 'compta'));
  36. $date_startmonth=GETPOST('date_startmonth');
  37. $date_startday=GETPOST('date_startday');
  38. $date_startyear=GETPOST('date_startyear');
  39. $date_endmonth=GETPOST('date_endmonth');
  40. $date_endday=GETPOST('date_endday');
  41. $date_endyear=GETPOST('date_endyear');
  42. // Security check
  43. if ($user->societe_id > 0) $socid = $user->societe_id;
  44. if (! empty($conf->comptabilite->enabled)) $result=restrictedArea($user,'compta','','','resultat');
  45. if (! empty($conf->accounting->enabled)) $result=restrictedArea($user,'accounting','','','comptarapport');
  46. /*
  47. * Actions
  48. */
  49. // None
  50. /*
  51. * View
  52. */
  53. $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;
  54. llxHeader('',$langs->trans("PurchasesJournal"),'','',0,0,'','',$morequery);
  55. $form=new Form($db);
  56. $year_current = strftime("%Y",dol_now());
  57. $pastmonth = strftime("%m",dol_now()) - 1;
  58. $pastmonthyear = $year_current;
  59. if ($pastmonth == 0)
  60. {
  61. $pastmonth = 12;
  62. $pastmonthyear--;
  63. }
  64. $date_start=dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  65. $date_end=dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  66. if (empty($date_start) || empty($date_end)) // We define date_start and date_end
  67. {
  68. $date_start=dol_get_first_day($pastmonthyear,$pastmonth,false);
  69. $date_end=dol_get_last_day($pastmonthyear,$pastmonth,false);
  70. }
  71. $name=$langs->trans("PurchasesJournal");
  72. $periodlink='';
  73. $exportlink='';
  74. $builddate=dol_now();
  75. $description=$langs->trans("DescPurchasesJournal").'<br>';
  76. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description.= $langs->trans("DepositsAreNotIncluded");
  77. else $description.= $langs->trans("DepositsAreIncluded");
  78. $period=$form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  79. report_header($name,'',$period,$periodlink,$description,$builddate,$exportlink);
  80. $p = explode(":", $conf->global->MAIN_INFO_SOCIETE_COUNTRY);
  81. $idpays = $p[0];
  82. $sql = "SELECT f.rowid, f.ref_supplier, f.type, f.datef, f.libelle,";
  83. $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,";
  84. $sql.= " s.rowid as socid, s.nom as name, s.code_compta_fournisseur,";
  85. $sql.= " p.rowid as pid, p.ref as ref, p.accountancy_code_buy,";
  86. $sql.= " ct.accountancy_code_buy as account_tva, ct.recuperableonly";
  87. $sql.= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as fd";
  88. $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 = '".$idpays."'";
  89. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = fd.fk_product";
  90. $sql.= " JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON f.rowid = fd.fk_facture_fourn";
  91. $sql.= " JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc" ;
  92. $sql.= " WHERE f.fk_statut > 0 AND f.entity IN (".getEntity('invoice').")";
  93. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2)";
  94. else $sql.= " AND f.type IN (0,1,2,3)";
  95. if ($date_start && $date_end) $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  96. // TODO Find a better trick to avoid problem with some mysql installations
  97. if (in_array($db->type, array('mysql', 'mysqli'))) $db->query('SET SQL_BIG_SELECTS=1');
  98. $result = $db->query($sql);
  99. if ($result)
  100. {
  101. $num = $db->num_rows($result);
  102. // les variables
  103. $cptfour = (! empty($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER)?$conf->global->ACCOUNTING_ACCOUNT_SUPPLIER:$langs->trans("CodeNotDef"));
  104. $cpttva = (! empty($conf->global->ACCOUNTING_VAT_BUY_ACCOUNT)?$conf->global->ACCOUNTING_VAT_BUY_ACCOUNT:$langs->trans("CodeNotDef"));
  105. $tabfac = array();
  106. $tabht = array();
  107. $tabtva = array();
  108. $tabttc = array();
  109. $tablocaltax1 = array();
  110. $tablocaltax2 = array();
  111. $tabcompany = array();
  112. $i=0;
  113. while ($i < $num)
  114. {
  115. $obj = $db->fetch_object($result);
  116. // contrôles
  117. $compta_soc = (! empty($obj->code_compta_fournisseur)?$obj->code_compta_fournisseur:$cptfour);
  118. $compta_prod = $obj->accountancy_code_buy;
  119. if (empty($compta_prod))
  120. {
  121. if($obj->product_type == 0) $compta_prod = (! empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT)?$conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT:$langs->trans("CodeNotDef"));
  122. else $compta_prod = (! empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT)?$conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT:$langs->trans("CodeNotDef"));
  123. }
  124. $compta_tva = (! empty($obj->account_tva)?$obj->account_tva:$cpttva);
  125. $compta_localtax1 = (! empty($obj->account_localtax1)?$obj->account_localtax1:$langs->trans("CodeNotDef"));
  126. $compta_localtax2 = (! empty($obj->account_localtax2)?$obj->account_localtax2:$langs->trans("CodeNotDef"));
  127. $account_localtax1=getLocalTaxesFromRate($obj->tva_tx, 1, $mysoc, $obj->thirdparty);
  128. $compta_localtax1= (! empty($account_localtax1[2])?$account_localtax1[2]:$langs->trans("CodeNotDef"));
  129. $account_localtax2=getLocalTaxesFromRate($obj->tva_tx, 2, $mysoc, $obj->thirdparty);
  130. $compta_localtax2= (! empty($account_localtax2[2])?$account_localtax2[2]:$langs->trans("CodeNotDef"));
  131. $tabfac[$obj->rowid]["date"] = $obj->datef;
  132. $tabfac[$obj->rowid]["ref"] = $obj->ref_supplier;
  133. $tabfac[$obj->rowid]["type"] = $obj->type;
  134. $tabfac[$obj->rowid]["lib"] = $obj->libelle;
  135. $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc;
  136. $tabht[$obj->rowid][$compta_prod] += $obj->total_ht;
  137. if ($obj->recuperableonly != 1) $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
  138. $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
  139. $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
  140. $tabcompany[$obj->rowid]=array('id'=>$obj->socid,'name'=>$obj->name);
  141. $i++;
  142. }
  143. }
  144. else {
  145. dol_print_error($db);
  146. }
  147. /*
  148. * Show result array
  149. */
  150. print "<table class=\"noborder\" width=\"100%\">";
  151. print "<tr class=\"liste_titre\">";
  152. ///print "<td>".$langs->trans("JournalNum")."</td>";
  153. print "<td>".$langs->trans("Date")."</td>";
  154. print "<td>".$langs->trans("Piece").' ('.$langs->trans("InvoiceRef").")</td>";
  155. print "<td>".$langs->trans("Account")."</td>";
  156. print "<td>".$langs->trans("Type")."</td><td align='right'>".$langs->trans("Debit")."</td><td align='right'>".$langs->trans("Credit")."</td>";
  157. print "</tr>\n";
  158. $invoicestatic=new FactureFournisseur($db);
  159. $companystatic=new Fournisseur($db);
  160. foreach ($tabfac as $key => $val)
  161. {
  162. $invoicestatic->id = $key;
  163. $invoicestatic->ref = $val["ref"];
  164. $invoicestatic->type = $val["type"];
  165. $companystatic->id = $tabcompany[$key]['id'];
  166. $companystatic->name = $tabcompany[$key]['name'];
  167. $lines = array(
  168. array(
  169. 'var' => $tabht[$key],
  170. 'label' => $langs->trans('Products'),
  171. ),
  172. array(
  173. 'var' => $tabtva[$key],
  174. 'label' => $langs->trans('VAT')
  175. ),
  176. array(
  177. 'var' => $tablocaltax1[$key],
  178. 'label' => $langs->transcountry('LT1', $mysoc->country_code)
  179. ),
  180. array(
  181. 'var' => $tablocaltax2[$key],
  182. 'label' => $langs->transcountry('LT2', $mysoc->country_code)
  183. ),
  184. array(
  185. 'var' => $tabttc[$key],
  186. 'label' => $langs->trans('ThirdParty').' ('.$companystatic->getNomUrl(0, 'supplier', 16).')',
  187. 'nomtcheck' => true,
  188. 'inv' => true
  189. )
  190. );
  191. foreach ($lines as $line)
  192. {
  193. foreach ($line['var'] as $k => $mt)
  194. {
  195. if (isset($line['nomtcheck']) || $mt)
  196. {
  197. print '<tr class="oddeven">';
  198. print "<td>".dol_print_date($db->jdate($val["date"]))."</td>";
  199. print "<td>".$invoicestatic->getNomUrl(1)."</td>";
  200. print "<td>".$k."</td><td>".$line['label']."</td>";
  201. if (isset($line['inv']))
  202. {
  203. print '<td align="right">'.($mt<0?price(-$mt):'')."</td>";
  204. print '<td align="right">'.($mt>=0?price($mt):'')."</td>";
  205. }
  206. else
  207. {
  208. print '<td align="right">'.($mt>=0?price($mt):'')."</td>";
  209. print '<td align="right">'.($mt<0?price(-$mt):'')."</td>";
  210. }
  211. print "</tr>";
  212. }
  213. }
  214. }
  215. }
  216. print "</table>";
  217. // End of page
  218. llxFooter();
  219. $db->close();