sellsjournal.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 <https://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. // Load Dolibarr environment
  32. require '../../main.inc.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  35. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php';
  37. // Load translation files required by the page
  38. $langs->loadLangs(array('companies', 'other', 'bills', 'compta'));
  39. $date_startmonth = GETPOST('date_startmonth');
  40. $date_startday = GETPOST('date_startday');
  41. $date_startyear = GETPOST('date_startyear');
  42. $date_endmonth = GETPOST('date_endmonth');
  43. $date_endday = GETPOST('date_endday');
  44. $date_endyear = GETPOST('date_endyear');
  45. // Security check
  46. if ($user->socid > 0) {
  47. $socid = $user->socid;
  48. }
  49. if (isModEnabled('comptabilite')) {
  50. $result = restrictedArea($user, 'compta', '', '', 'resultat');
  51. }
  52. if (isModEnabled('accounting')) {
  53. $result = restrictedArea($user, 'accounting', '', '', 'comptarapport');
  54. }
  55. /*
  56. * Actions
  57. */
  58. // None
  59. /*
  60. * View
  61. */
  62. $form = new Form($db);
  63. $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;
  64. llxHeader('', $langs->trans("SellsJournal"), '', '', 0, 0, '', '', $morequery);
  65. $year_current = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  66. $pastmonth = strftime("%m", dol_now()) - 1;
  67. $pastmonthyear = $year_current;
  68. if ($pastmonth == 0) {
  69. $pastmonth = 12;
  70. $pastmonthyear--;
  71. }
  72. $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  73. $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  74. if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
  75. $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
  76. $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
  77. }
  78. $name = $langs->trans("SellsJournal");
  79. $periodlink = '';
  80. $exportlink = '';
  81. $builddate = dol_now();
  82. $description = $langs->trans("DescSellsJournal").'<br>';
  83. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  84. $description .= $langs->trans("DepositsAreNotIncluded");
  85. } else {
  86. $description .= $langs->trans("DepositsAreIncluded");
  87. }
  88. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  89. report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink);
  90. $p = explode(":", $conf->global->MAIN_INFO_SOCIETE_COUNTRY);
  91. $idpays = $p[0];
  92. $sql = "SELECT f.rowid, f.ref, f.type, f.datef, f.ref_client,";
  93. $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,";
  94. $sql .= " s.rowid as socid, s.nom as name, s.code_compta, s.client,";
  95. $sql .= " p.rowid as pid, p.ref as pref,";
  96. if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
  97. $sql .= " ppe.accountancy_code_sell,";
  98. } else {
  99. $sql .= " p.accountancy_code_sell,";
  100. }
  101. $sql .= " ct.accountancy_code_sell as account_tva, ct.recuperableonly";
  102. $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
  103. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = fd.fk_product";
  104. if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
  105. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
  106. }
  107. $sql .= " JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
  108. $sql .= " JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
  109. $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 = ".((int) $idpays);
  110. $sql .= " WHERE f.entity IN (".getEntity('invoice').")";
  111. $sql .= " AND f.fk_statut > 0";
  112. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  113. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_SITUATION.")";
  114. } else {
  115. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_STANDARD.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_DEPOSIT.",".Facture::TYPE_SITUATION.")";
  116. }
  117. $sql .= " AND fd.product_type IN (0,1)";
  118. if ($date_start && $date_end) {
  119. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  120. }
  121. $sql .= " ORDER BY f.rowid";
  122. // TODO Find a better trick to avoid problem with some mysql installations
  123. if (in_array($db->type, array('mysql', 'mysqli'))) {
  124. $db->query('SET SQL_BIG_SELECTS=1');
  125. }
  126. $result = $db->query($sql);
  127. if ($result) {
  128. $tabfac = array();
  129. $tabht = array();
  130. $tabtva = array();
  131. $tablocaltax1 = array();
  132. $tablocaltax2 = array();
  133. $tabttc = array();
  134. $tabcompany = array();
  135. $account_localtax1 = 0;
  136. $account_localtax2 = 0;
  137. $num = $db->num_rows($result);
  138. $i = 0;
  139. $resligne = array();
  140. while ($i < $num) {
  141. $obj = $db->fetch_object($result);
  142. // les variables
  143. $cptcli = (($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER != "") ? $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER : $langs->trans("CodeNotDef"));
  144. $compta_soc = (!empty($obj->code_compta) ? $obj->code_compta : $cptcli);
  145. $compta_prod = $obj->accountancy_code_sell;
  146. if (empty($compta_prod)) {
  147. if ($obj->product_type == 0) {
  148. $compta_prod = (!empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  149. } else {
  150. $compta_prod = (!empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  151. }
  152. }
  153. $cpttva = (!empty($conf->global->ACCOUNTING_VAT_SOLD_ACCOUNT) ? $conf->global->ACCOUNTING_VAT_SOLD_ACCOUNT : $langs->trans("CodeNotDef"));
  154. $compta_tva = (!empty($obj->account_tva) ? $obj->account_tva : $cpttva);
  155. $account_localtax1 = getLocalTaxesFromRate($obj->tva_tx, 1, $obj->thirdparty, $mysoc);
  156. $compta_localtax1 = (!empty($account_localtax1[3]) ? $account_localtax1[3] : $langs->trans("CodeNotDef"));
  157. $account_localtax2 = getLocalTaxesFromRate($obj->tva_tx, 2, $obj->thirdparty, $mysoc);
  158. $compta_localtax2 = (!empty($account_localtax2[3]) ? $account_localtax2[3] : $langs->trans("CodeNotDef"));
  159. // Situation invoices handling
  160. $line = new FactureLigne($db);
  161. $line->fetch($obj->id); // id of line
  162. $prev_progress = 0;
  163. if ($obj->type == Facture::TYPE_SITUATION) {
  164. // Avoid divide by 0
  165. if ($obj->situation_percent == 0) {
  166. $situation_ratio = 0;
  167. } else {
  168. $prev_progress = $line->get_prev_progress($obj->rowid); // id on invoice
  169. $situation_ratio = ($obj->situation_percent - $prev_progress) / $obj->situation_percent;
  170. }
  171. } else {
  172. $situation_ratio = 1;
  173. }
  174. //la ligne facture
  175. $tabfac[$obj->rowid]["date"] = $obj->datef;
  176. $tabfac[$obj->rowid]["ref"] = $obj->ref;
  177. $tabfac[$obj->rowid]["type"] = $obj->type;
  178. if (!isset($tabttc[$obj->rowid][$compta_soc])) {
  179. $tabttc[$obj->rowid][$compta_soc] = 0;
  180. }
  181. if (!isset($tabht[$obj->rowid][$compta_prod])) {
  182. $tabht[$obj->rowid][$compta_prod] = 0;
  183. }
  184. if (!isset($tabtva[$obj->rowid][$compta_tva])) {
  185. $tabtva[$obj->rowid][$compta_tva] = 0;
  186. }
  187. if (!isset($tablocaltax1[$obj->rowid][$compta_localtax1])) {
  188. $tablocaltax1[$obj->rowid][$compta_localtax1] = 0;
  189. }
  190. if (!isset($tablocaltax2[$obj->rowid][$compta_localtax2])) {
  191. $tablocaltax2[$obj->rowid][$compta_localtax2] = 0;
  192. }
  193. $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc * $situation_ratio;
  194. $tabht[$obj->rowid][$compta_prod] += $obj->total_ht * $situation_ratio;
  195. if ($obj->recuperableonly != 1) {
  196. $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva * $situation_ratio;
  197. }
  198. $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
  199. $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
  200. $tabcompany[$obj->rowid] = array('id'=>$obj->socid, 'name'=>$obj->name, 'client'=>$obj->client);
  201. $i++;
  202. }
  203. } else {
  204. dol_print_error($db);
  205. }
  206. /*
  207. * Show result array
  208. */
  209. print '<table class="noborder centpercent">';
  210. print '<tr class="liste_titre">';
  211. //print "<td>".$langs->trans("JournalNum")."</td>";
  212. print '<td>'.$langs->trans('Date').'</td><td>'.$langs->trans('Piece').' ('.$langs->trans('InvoiceRef').')</td>';
  213. print '<td>'.$langs->trans('Account').'</td>';
  214. print '<td>'.$langs->trans('Type').'</td>';
  215. print '<td class="right">'.$langs->trans('AccountingDebit').'</td>';
  216. print '<td class="right">'.$langs->trans('AccountingCredit').'</td>';
  217. print "</tr>\n";
  218. $invoicestatic = new Facture($db);
  219. $companystatic = new Client($db);
  220. foreach ($tabfac as $key => $val) {
  221. $invoicestatic->id = $key;
  222. $invoicestatic->ref = $val["ref"];
  223. $invoicestatic->type = $val["type"];
  224. $companystatic->id = $tabcompany[$key]['id'];
  225. $companystatic->name = $tabcompany[$key]['name'];
  226. $companystatic->client = $tabcompany[$key]['client'];
  227. $lines = array(
  228. array(
  229. 'var' => $tabttc[$key],
  230. 'label' => $langs->trans('ThirdParty').' ('.$companystatic->getNomUrl(0, 'customer', 16).')',
  231. 'nomtcheck' => true,
  232. 'inv' => true
  233. ),
  234. array(
  235. 'var' => $tabht[$key],
  236. 'label' => $langs->trans('Products'),
  237. ),
  238. array(
  239. 'var' => $tabtva[$key],
  240. 'label' => $langs->trans('VAT')
  241. ),
  242. array(
  243. 'var' => $tablocaltax1[$key],
  244. 'label' => $langs->transcountry('LT1', $mysoc->country_code)
  245. ),
  246. array(
  247. 'var' => $tablocaltax2[$key],
  248. 'label' => $langs->transcountry('LT2', $mysoc->country_code)
  249. )
  250. );
  251. foreach ($lines as $line) {
  252. foreach ($line['var'] as $k => $mt) {
  253. if (isset($line['nomtcheck']) || $mt) {
  254. print '<tr class="oddeven">';
  255. print "<td>".dol_print_date($db->jdate($val["date"]))."</td>";
  256. print "<td>".$invoicestatic->getNomUrl(1)."</td>";
  257. print "<td>".$k."</td><td>".$line['label']."</td>";
  258. if (isset($line['inv'])) {
  259. print '<td class="right">'.($mt >= 0 ?price($mt) : '')."</td>";
  260. print '<td class="right">'.($mt < 0 ?price(-$mt) : '')."</td>";
  261. } else {
  262. print '<td class="right">'.($mt < 0 ?price(-$mt) : '')."</td>";
  263. print '<td class="right">'.($mt >= 0 ?price($mt) : '')."</td>";
  264. }
  265. print "</tr>";
  266. }
  267. }
  268. }
  269. }
  270. print "</table>";
  271. // End of page
  272. llxFooter();
  273. $db->close();