sellsjournal.php 11 KB

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