purchasesjournal.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <https://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. // Load Dolibarr environment
  30. require '../../main.inc.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
  35. // Load translation files required by the page
  36. $langs->loadlangs(array('companies', 'other', 'bills', 'compta'));
  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. // Security check
  44. if ($user->socid > 0) {
  45. $socid = $user->socid;
  46. }
  47. if (isModEnabled('comptabilite')) {
  48. $result = restrictedArea($user, 'compta', '', '', 'resultat');
  49. }
  50. if (isModEnabled('accounting')) {
  51. $result = restrictedArea($user, 'accounting', '', '', 'comptarapport');
  52. }
  53. /*
  54. * Actions
  55. */
  56. // None
  57. /*
  58. * View
  59. */
  60. $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;
  61. llxHeader('', $langs->trans("PurchasesJournal"), '', '', 0, 0, '', '', $morequery);
  62. $form = new Form($db);
  63. $year_current = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  64. $pastmonth = strftime("%m", dol_now()) - 1;
  65. $pastmonthyear = $year_current;
  66. if ($pastmonth == 0) {
  67. $pastmonth = 12;
  68. $pastmonthyear--;
  69. }
  70. $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  71. $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  72. if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
  73. $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
  74. $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
  75. }
  76. $name = $langs->trans("PurchasesJournal");
  77. $periodlink = '';
  78. $exportlink = '';
  79. $builddate = dol_now();
  80. $description = $langs->trans("DescPurchasesJournal").'<br>';
  81. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  82. $description .= $langs->trans("DepositsAreNotIncluded");
  83. } else {
  84. $description .= $langs->trans("DepositsAreIncluded");
  85. }
  86. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  87. report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink);
  88. $p = explode(":", $conf->global->MAIN_INFO_SOCIETE_COUNTRY);
  89. $idpays = $p[0];
  90. $sql = "SELECT f.rowid, f.ref_supplier, f.type, f.datef, f.libelle as label,";
  91. $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,";
  92. $sql .= " s.rowid as socid, s.nom as name, s.code_compta_fournisseur,";
  93. $sql .= " p.rowid as pid, p.ref as ref, p.accountancy_code_buy,";
  94. $sql .= " ct.accountancy_code_buy as account_tva, ct.recuperableonly";
  95. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as fd";
  96. $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);
  97. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = fd.fk_product";
  98. $sql .= " JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON f.rowid = fd.fk_facture_fourn";
  99. $sql .= " JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
  100. $sql .= " WHERE f.fk_statut > 0 AND f.entity IN (".getEntity('invoice').")";
  101. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  102. $sql .= " AND f.type IN (0,1,2)";
  103. } else {
  104. $sql .= " AND f.type IN (0,1,2,3)";
  105. }
  106. if ($date_start && $date_end) {
  107. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  108. }
  109. // TODO Find a better trick to avoid problem with some mysql installations
  110. if (in_array($db->type, array('mysql', 'mysqli'))) {
  111. $db->query('SET SQL_BIG_SELECTS=1');
  112. }
  113. $result = $db->query($sql);
  114. if ($result) {
  115. $num = $db->num_rows($result);
  116. // les variables
  117. $cptfour = (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER != "") ? $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER : $langs->trans("CodeNotDef"));
  118. $cpttva = (!empty($conf->global->ACCOUNTING_VAT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_VAT_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
  119. $tabfac = array();
  120. $tabht = array();
  121. $tabtva = array();
  122. $tabttc = array();
  123. $tablocaltax1 = array();
  124. $tablocaltax2 = array();
  125. $tabcompany = array();
  126. $i = 0;
  127. while ($i < $num) {
  128. $obj = $db->fetch_object($result);
  129. // contrôles
  130. $compta_soc = (($obj->code_compta_fournisseur != "") ? $obj->code_compta_fournisseur : $cptfour);
  131. $compta_prod = $obj->accountancy_code_buy;
  132. if (empty($compta_prod)) {
  133. if ($obj->product_type == 0) {
  134. $compta_prod = (!empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
  135. } else {
  136. $compta_prod = (!empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : $langs->trans("CodeNotDef"));
  137. }
  138. }
  139. $compta_tva = (!empty($obj->account_tva) ? $obj->account_tva : $cpttva);
  140. $compta_localtax1 = (!empty($obj->account_localtax1) ? $obj->account_localtax1 : $langs->trans("CodeNotDef"));
  141. $compta_localtax2 = (!empty($obj->account_localtax2) ? $obj->account_localtax2 : $langs->trans("CodeNotDef"));
  142. $account_localtax1 = getLocalTaxesFromRate($obj->tva_tx, 1, $mysoc, $obj->thirdparty);
  143. $compta_localtax1 = (!empty($account_localtax1[2]) ? $account_localtax1[2] : $langs->trans("CodeNotDef"));
  144. $account_localtax2 = getLocalTaxesFromRate($obj->tva_tx, 2, $mysoc, $obj->thirdparty);
  145. $compta_localtax2 = (!empty($account_localtax2[2]) ? $account_localtax2[2] : $langs->trans("CodeNotDef"));
  146. $tabfac[$obj->rowid]["date"] = $obj->datef;
  147. $tabfac[$obj->rowid]["ref"] = $obj->ref_supplier;
  148. $tabfac[$obj->rowid]["type"] = $obj->type;
  149. $tabfac[$obj->rowid]["lib"] = $obj->label;
  150. $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc;
  151. $tabht[$obj->rowid][$compta_prod] += $obj->total_ht;
  152. if ($obj->recuperableonly != 1) {
  153. $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
  154. }
  155. $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
  156. $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
  157. $tabcompany[$obj->rowid] = array('id'=>$obj->socid, 'name'=>$obj->name);
  158. $i++;
  159. }
  160. } else {
  161. dol_print_error($db);
  162. }
  163. /*
  164. * Show result array
  165. */
  166. print "<table class=\"noborder\" width=\"100%\">";
  167. print "<tr class=\"liste_titre\">";
  168. ///print "<td>".$langs->trans("JournalNum")."</td>";
  169. print "<td>".$langs->trans("Date")."</td>";
  170. print "<td>".$langs->trans("Piece").' ('.$langs->trans("InvoiceRef").")</td>";
  171. print "<td>".$langs->trans("Account")."</td>";
  172. print "<td>".$langs->trans("Type")."</td>";
  173. print "<td class='right'>".$langs->trans("AccountingDebit")."</td>";
  174. print "<td class='right'>".$langs->trans("AccountingCredit")."</td>";
  175. print "</tr>\n";
  176. $invoicestatic = new FactureFournisseur($db);
  177. $companystatic = new Fournisseur($db);
  178. foreach ($tabfac as $key => $val) {
  179. $invoicestatic->id = $key;
  180. $invoicestatic->ref = $val["ref"];
  181. $invoicestatic->type = $val["type"];
  182. $companystatic->id = $tabcompany[$key]['id'];
  183. $companystatic->name = $tabcompany[$key]['name'];
  184. $lines = array(
  185. array(
  186. 'var' => $tabht[$key],
  187. 'label' => $langs->trans('Products'),
  188. ),
  189. array(
  190. 'var' => $tabtva[$key],
  191. 'label' => $langs->trans('VAT')
  192. ),
  193. array(
  194. 'var' => $tablocaltax1[$key],
  195. 'label' => $langs->transcountry('LT1', $mysoc->country_code)
  196. ),
  197. array(
  198. 'var' => $tablocaltax2[$key],
  199. 'label' => $langs->transcountry('LT2', $mysoc->country_code)
  200. ),
  201. array(
  202. 'var' => $tabttc[$key],
  203. 'label' => $langs->trans('ThirdParty').' ('.$companystatic->getNomUrl(0, 'supplier', 16).')',
  204. 'nomtcheck' => true,
  205. 'inv' => true
  206. )
  207. );
  208. foreach ($lines as $line) {
  209. foreach ($line['var'] as $k => $mt) {
  210. if (isset($line['nomtcheck']) || $mt) {
  211. print '<tr class="oddeven">';
  212. print "<td>".dol_print_date($db->jdate($val["date"]))."</td>";
  213. print "<td>".$invoicestatic->getNomUrl(1)."</td>";
  214. print "<td>".$k."</td><td>".$line['label']."</td>";
  215. if (isset($line['inv'])) {
  216. print '<td class="right">'.($mt < 0 ?price(-$mt) : '')."</td>";
  217. print '<td class="right">'.($mt >= 0 ?price($mt) : '')."</td>";
  218. } else {
  219. print '<td class="right">'.($mt >= 0 ?price($mt) : '')."</td>";
  220. print '<td class="right">'.($mt < 0 ?price(-$mt) : '')."</td>";
  221. }
  222. print "</tr>";
  223. }
  224. }
  225. }
  226. }
  227. print "</table>";
  228. // End of page
  229. llxFooter();
  230. $db->close();