PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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