index.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <?php
  2. /* Copyright (C) 2013 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
  4. * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
  5. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * \file htdocs/accountancy/customer/index.php
  24. * \ingroup Accountancy (Double entries)
  25. * \brief Home customer journalization page
  26. */
  27. require '../../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  32. // Load translation files required by the page
  33. $langs->loadLangs(array("compta", "bills", "other", "main", "accountancy"));
  34. // Security check
  35. if (empty($conf->accounting->enabled)) {
  36. accessforbidden();
  37. }
  38. if ($user->socid > 0)
  39. accessforbidden();
  40. if (!$user->rights->accounting->bind->write)
  41. accessforbidden();
  42. $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
  43. if (GETPOST("year", 'int')) $year_start = GETPOST("year", 'int');
  44. else
  45. {
  46. $year_start = dol_print_date(dol_now(), '%Y');
  47. if (dol_print_date(dol_now(), '%m') < $month_start) $year_start--; // If current month is lower that starting fiscal month, we start last year
  48. }
  49. $year_end = $year_start + 1;
  50. $month_end = $month_start - 1;
  51. if ($month_end < 1)
  52. {
  53. $month_end = 12;
  54. $year_end--;
  55. }
  56. $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
  57. $search_date_end = dol_get_last_day($year_end, $month_end);
  58. $year_current = $year_start;
  59. // Validate History
  60. $action = GETPOST('action', 'aZ09');
  61. $chartaccountcode = dol_getIdFromCode($db, $conf->global->CHARTOFACCOUNTS, 'accounting_system', 'rowid', 'pcg_version');
  62. /*
  63. * Actions
  64. */
  65. if ($action == 'clean' || $action == 'validatehistory')
  66. {
  67. // Clean database
  68. $db->begin();
  69. $sql1 = "UPDATE ".MAIN_DB_PREFIX."facturedet as fd";
  70. $sql1 .= " SET fk_code_ventilation = 0";
  71. $sql1 .= ' WHERE fd.fk_code_ventilation NOT IN';
  72. $sql1 .= ' (SELECT accnt.rowid ';
  73. $sql1 .= ' FROM '.MAIN_DB_PREFIX.'accounting_account as accnt';
  74. $sql1 .= ' INNER JOIN '.MAIN_DB_PREFIX.'accounting_system as syst';
  75. $sql1 .= ' ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid='.$conf->global->CHARTOFACCOUNTS.' AND accnt.entity = '.$conf->entity.')';
  76. $sql1 .= ' AND fd.fk_facture IN (SELECT rowid FROM '.MAIN_DB_PREFIX.'facture WHERE entity = '.$conf->entity.')';
  77. $sql1 .= ' AND fk_code_ventilation <> 0';
  78. dol_syslog("htdocs/accountancy/customer/index.php fixaccountancycode", LOG_DEBUG);
  79. $resql1 = $db->query($sql1);
  80. if (!$resql1) {
  81. $error++;
  82. $db->rollback();
  83. setEventMessages($db->lasterror(), null, 'errors');
  84. } else {
  85. $db->commit();
  86. }
  87. // End clean database
  88. }
  89. if ($action == 'validatehistory') {
  90. $error = 0;
  91. $db->begin();
  92. // Now make the binding. Bind automatically only for product with a dedicated account that exists into chart of account, others need a manual bind
  93. /*if ($db->type == 'pgsql') {
  94. $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facturedet";
  95. $sql1 .= " SET fk_code_ventilation = accnt.rowid";
  96. $sql1 .= " FROM " . MAIN_DB_PREFIX . "product as p, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst";
  97. $sql1 .= " WHERE " . MAIN_DB_PREFIX . "facturedet.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . $conf->global->CHARTOFACCOUNTS.' AND accnt.entity = '.$conf->entity;
  98. $sql1 .= " AND accnt.active = 1 AND p.accountancy_code_sell=accnt.account_number";
  99. $sql1 .= " AND " . MAIN_DB_PREFIX . "facturedet.fk_code_ventilation = 0";
  100. } else {
  101. $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facturedet as fd, " . MAIN_DB_PREFIX . "product as p, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst";
  102. $sql1 .= " SET fk_code_ventilation = accnt.rowid";
  103. $sql1 .= " WHERE fd.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . $conf->global->CHARTOFACCOUNTS.' AND accnt.entity = '.$conf->entity;
  104. $sql1 .= " AND accnt.active = 1 AND p.accountancy_code_sell=accnt.account_number";
  105. $sql1 .= " AND fd.fk_code_ventilation = 0";
  106. }*/
  107. // Customer Invoice lines (must be same request than into page list.php for manual binding)
  108. $sql = "SELECT f.rowid as facid, f.ref as ref, f.datef, f.type as ftype,";
  109. $sql .= " l.rowid, l.fk_product, l.description, l.total_ht, l.fk_code_ventilation, l.product_type as type_l, l.tva_tx as tva_tx_line, l.vat_src_code,";
  110. $sql .= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.fk_product_type as type, p.tva_tx as tva_tx_prod,";
  111. $sql .= " p.accountancy_code_sell as code_sell, p.accountancy_code_sell_intra as code_sell_intra, p.accountancy_code_sell_export as code_sell_export,";
  112. $sql .= " aa.rowid as aarowid, aa2.rowid as aarowid_intra, aa3.rowid as aarowid_export,";
  113. $sql .= " co.code as country_code, co.label as country_label,";
  114. $sql .= " s.tva_intra";
  115. $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
  116. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
  117. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON co.rowid = s.fk_pays ";
  118. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."facturedet as l ON f.rowid = l.fk_facture";
  119. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = l.fk_product";
  120. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON p.accountancy_code_sell = aa.account_number AND aa.active = 1 AND aa.fk_pcg_version = '".$chartaccountcode."' AND aa.entity = ".$conf->entity;
  121. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa2 ON p.accountancy_code_sell_intra = aa2.account_number AND aa2.active = 1 AND aa2.fk_pcg_version = '".$chartaccountcode."' AND aa2.entity = ".$conf->entity;
  122. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa3 ON p.accountancy_code_sell_export = aa3.account_number AND aa3.active = 1 AND aa3.fk_pcg_version = '".$chartaccountcode."' AND aa3.entity = ".$conf->entity;
  123. $sql .= " WHERE f.fk_statut > 0 AND l.fk_code_ventilation <= 0";
  124. $sql .= " AND l.product_type <= 2";
  125. dol_syslog('htdocs/accountancy/customer/index.php');
  126. $result = $db->query($sql);
  127. if (!$result) {
  128. $error++;
  129. setEventMessages($db->lasterror(), null, 'errors');
  130. } else {
  131. $num_lines = $db->num_rows($result);
  132. $isSellerInEEC = isInEEC($mysoc);
  133. $i = 0;
  134. while ($i < min($num_lines, 10000)) { // No more than 10000 at once
  135. $objp = $db->fetch_object($result);
  136. $isBuyerInEEC = isInEEC($objp);
  137. // Search suggested account for product/service (similar code exists in page list.php to make manual binding)
  138. $suggestedaccountingaccountfor = '';
  139. if (($objp->country_code == $mysoc->country_code) || empty($objp->country_code)) { // If buyer in same country than seller (if not defined, we assume it is same country)
  140. $objp->code_sell_p = $objp->code_sell;
  141. $objp->aarowid_suggest = $objp->aarowid;
  142. $suggestedaccountingaccountfor = '';
  143. } else {
  144. if ($isSellerInEEC && $isBuyerInEEC && $objp->tva_tx_line != 0) { // European intravat sale, but with VAT
  145. $objp->code_sell_p = $objp->code_sell;
  146. $objp->aarowid_suggest = $objp->aarowid;
  147. $suggestedaccountingaccountfor = 'eecwithvat';
  148. } elseif ($isSellerInEEC && $isBuyerInEEC && empty($objp->tva_intra)) { // European intravat sale, without VAT intra community number
  149. $objp->code_sell_p = $objp->code_sell;
  150. $objp->aarowid_suggest = 0; // There is a doubt, no automatic binding
  151. $suggestedaccountingaccountfor = 'eecwithoutvatnumber';
  152. } elseif ($isSellerInEEC && $isBuyerInEEC) { // European intravat sale
  153. $objp->code_sell_p = $objp->code_sell_intra;
  154. $objp->aarowid_suggest = $objp->aarowid_intra;
  155. $suggestedaccountingaccountfor = 'eec';
  156. } else { // Foreign sale
  157. $objp->code_sell_p = $objp->code_sell_export;
  158. $objp->aarowid_suggest = $objp->aarowid_export;
  159. $suggestedaccountingaccountfor = 'export';
  160. }
  161. }
  162. if ($objp->aarowid_suggest > 0)
  163. {
  164. $sqlupdate = "UPDATE ".MAIN_DB_PREFIX."facturedet";
  165. $sqlupdate .= " SET fk_code_ventilation = ".$objp->aarowid_suggest;
  166. $sqlupdate .= " WHERE fk_code_ventilation <= 0 AND product_type <= 2 AND rowid = ".$objp->rowid;
  167. $resqlupdate = $db->query($sqlupdate);
  168. if (!$resqlupdate)
  169. {
  170. $error++;
  171. setEventMessages($db->lasterror(), null, 'errors');
  172. break;
  173. }
  174. }
  175. $i++;
  176. }
  177. }
  178. if ($error)
  179. {
  180. $db->rollback();
  181. }
  182. else {
  183. $db->commit();
  184. setEventMessages($langs->trans('AutomaticBindingDone'), null, 'mesgs');
  185. }
  186. }
  187. /*
  188. * View
  189. */
  190. llxHeader('', $langs->trans("CustomersVentilation"));
  191. $textprevyear = '<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current - 1).'">'.img_previous().'</a>';
  192. $textnextyear = '&nbsp;<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current + 1).'">'.img_next().'</a>';
  193. print load_fiche_titre($langs->trans("CustomersVentilation")." ".$textprevyear." ".$langs->trans("Year")." ".$year_start." ".$textnextyear, '', 'title_accountancy');
  194. print '<span class="opacitymedium">'.$langs->trans("DescVentilCustomer").'</span><br>';
  195. print '<span class="opacitymedium hideonsmartphone">'.$langs->trans("DescVentilMore", $langs->transnoentitiesnoconv("ValidateHistory"), $langs->transnoentitiesnoconv("ToBind")).'<br>';
  196. print '</span><br>';
  197. $y = $year_current;
  198. $buttonbind = '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?year='.$year_current.'&action=validatehistory">'.$langs->trans("ValidateHistory").'</a>';
  199. print_barre_liste($langs->trans("OverviewOfAmountOfLinesNotBound"), '', '', '', '', '', '', -1, '', '', 0, $buttonbind, '', 0, 1, 1);
  200. //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesNotBound"), $buttonbind, '');
  201. print '<div class="div-table-responsive-no-min">';
  202. print '<table class="noborder centpercent">';
  203. print '<tr class="liste_titre"><td width="200">'.$langs->trans("Account").'</td>';
  204. print '<td width="200" class="left">'.$langs->trans("Label").'</td>';
  205. for ($i = 1; $i <= 12; $i++) {
  206. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  207. if ($j > 12) $j -= 12;
  208. print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
  209. }
  210. print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
  211. $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number')." AS codecomptable,";
  212. $sql .= " ".$db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label')." AS intitule,";
  213. for ($i = 1; $i <= 12; $i++) {
  214. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  215. if ($j > 12) $j -= 12;
  216. $sql .= " SUM(".$db->ifsql('MONTH(f.datef)='.$j, 'fd.total_ht', '0').") AS month".str_pad($j, 2, '0', STR_PAD_LEFT).",";
  217. }
  218. $sql .= " SUM(fd.total_ht) as total";
  219. $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
  220. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
  221. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = fd.fk_code_ventilation";
  222. $sql .= " WHERE f.datef >= '".$db->idate($search_date_start)."'";
  223. $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
  224. $sql .= " AND f.fk_statut > 0";
  225. $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
  226. $sql .= " AND aa.account_number IS NULL";
  227. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  228. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_SITUATION.")";
  229. } else {
  230. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_DEPOSIT.",".Facture::TYPE_SITUATION.")";
  231. }
  232. $sql .= " GROUP BY fd.fk_code_ventilation,aa.account_number,aa.label";
  233. dol_syslog('htdocs/accountancy/customer/index.php sql='.$sql, LOG_DEBUG);
  234. $resql = $db->query($sql);
  235. if ($resql) {
  236. $num = $db->num_rows($resql);
  237. while ($row = $db->fetch_row($resql)) {
  238. print '<tr class="oddeven"><td>';
  239. if ($row[0] == 'tobind')
  240. {
  241. print $langs->trans("Unknown");
  242. }
  243. else print length_accountg($row[0]);
  244. print '</td>';
  245. print '<td class="left">';
  246. if ($row[0] == 'tobind')
  247. {
  248. print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/customer/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind"));
  249. }
  250. else print $row[1];
  251. print '</td>';
  252. for ($i = 2; $i <= 12; $i++) {
  253. print '<td class="nowrap right">'.price($row[$i]).'</td>';
  254. }
  255. print '<td class="nowrap right">'.price($row[13]).'</td>';
  256. print '<td class="nowrap right"><b>'.price($row[14]).'</b></td>';
  257. print '</tr>';
  258. }
  259. $db->free($resql);
  260. } else {
  261. print $db->lasterror(); // Show last sql error
  262. }
  263. print "</table>\n";
  264. print '</div>';
  265. print '<br>';
  266. print_barre_liste($langs->trans("OverviewOfAmountOfLinesBound"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
  267. //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesBound"), '', '');
  268. print '<div class="div-table-responsive-no-min">';
  269. print '<table class="noborder centpercent">';
  270. print '<tr class="liste_titre"><td width="200">'.$langs->trans("Account").'</td>';
  271. print '<td width="200" class="left">'.$langs->trans("Label").'</td>';
  272. for ($i = 1; $i <= 12; $i++) {
  273. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  274. if ($j > 12) $j -= 12;
  275. print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
  276. }
  277. print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
  278. $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number')." AS codecomptable,";
  279. $sql .= " ".$db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label')." AS intitule,";
  280. for ($i = 1; $i <= 12; $i++) {
  281. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  282. if ($j > 12) $j -= 12;
  283. $sql .= " SUM(".$db->ifsql('MONTH(f.datef)='.$j, 'fd.total_ht', '0').") AS month".str_pad($j, 2, '0', STR_PAD_LEFT).",";
  284. }
  285. $sql .= " SUM(fd.total_ht) as total";
  286. $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
  287. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
  288. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = fd.fk_code_ventilation";
  289. $sql .= " WHERE f.datef >= '".$db->idate($search_date_start)."'";
  290. $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
  291. $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
  292. $sql .= " AND f.fk_statut > 0";
  293. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  294. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_SITUATION.")";
  295. } else {
  296. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_DEPOSIT.",".Facture::TYPE_SITUATION.")";
  297. }
  298. $sql .= " AND aa.account_number IS NOT NULL";
  299. $sql .= " GROUP BY fd.fk_code_ventilation,aa.account_number,aa.label";
  300. dol_syslog('htdocs/accountancy/customer/index.php');
  301. $resql = $db->query($sql);
  302. if ($resql) {
  303. $num = $db->num_rows($resql);
  304. while ($row = $db->fetch_row($resql)) {
  305. print '<tr class="oddeven"><td>';
  306. if ($row[0] == 'tobind')
  307. {
  308. print $langs->trans("Unknown");
  309. }
  310. else print length_accountg($row[0]);
  311. print '</td>';
  312. print '<td class="left">';
  313. if ($row[0] == 'tobind')
  314. {
  315. print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/customer/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind"));
  316. }
  317. else print $row[1];
  318. print '</td>';
  319. for ($i = 2; $i <= 12; $i++) {
  320. print '<td class="nowrap right">'.price($row[$i]).'</td>';
  321. }
  322. print '<td class="nowrap right">'.price($row[13]).'</td>';
  323. print '<td class="nowrap right"><b>'.price($row[14]).'</b></td>';
  324. print '</tr>';
  325. }
  326. $db->free($resql);
  327. } else {
  328. print $db->lasterror(); // Show last sql error
  329. }
  330. print "</table>\n";
  331. print '</div>';
  332. if ($conf->global->MAIN_FEATURES_LEVEL > 0) // This part of code looks strange. Why showing a report that should rely on result of this step ?
  333. {
  334. print '<br>';
  335. print '<br>';
  336. print_barre_liste($langs->trans("OtherInfo"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
  337. //print load_fiche_titre($langs->trans("OtherInfo"), '', '');
  338. print '<div class="div-table-responsive-no-min">';
  339. print '<table class="noborder centpercent">';
  340. print '<tr class="liste_titre"><td width="400" class="left">'.$langs->trans("TotalVente").'</td>';
  341. for ($i = 1; $i <= 12; $i++) {
  342. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  343. if ($j > 12) $j -= 12;
  344. print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
  345. }
  346. print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
  347. $sql = "SELECT '".$langs->trans("TotalVente")."' AS total,";
  348. for ($i = 1; $i <= 12; $i++) {
  349. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  350. if ($j > 12) $j -= 12;
  351. $sql .= " SUM(".$db->ifsql('MONTH(f.datef)='.$j, 'fd.total_ht', '0').") AS month".str_pad($j, 2, '0', STR_PAD_LEFT).",";
  352. }
  353. $sql .= " SUM(fd.total_ht) as total";
  354. $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
  355. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
  356. $sql .= " WHERE f.datef >= '".$db->idate($search_date_start)."'";
  357. $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
  358. $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
  359. $sql .= " AND f.fk_statut > 0";
  360. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  361. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_SITUATION.")";
  362. } else {
  363. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_DEPOSIT.",".Facture::TYPE_SITUATION.")";
  364. }
  365. dol_syslog('htdocs/accountancy/customer/index.php');
  366. $resql = $db->query($sql);
  367. if ($resql) {
  368. $num = $db->num_rows($resql);
  369. while ($row = $db->fetch_row($resql)) {
  370. print '<tr><td>'.$row[0].'</td>';
  371. for ($i = 1; $i <= 12; $i++) {
  372. print '<td class="nowrap right">'.price($row[$i]).'</td>';
  373. }
  374. print '<td class="nowrap right"><b>'.price($row[13]).'</b></td>';
  375. print '</tr>';
  376. }
  377. $db->free($resql);
  378. } else {
  379. print $db->lasterror(); // Show last sql error
  380. }
  381. print "</table>\n";
  382. print '</div>';
  383. if (!empty($conf->margin->enabled)) {
  384. print "<br>\n";
  385. print '<div class="div-table-responsive-no-min">';
  386. print '<table class="noborder centpercent">';
  387. print '<tr class="liste_titre"><td width="400">'.$langs->trans("TotalMarge").'</td>';
  388. for ($i = 1; $i <= 12; $i++) {
  389. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  390. if ($j > 12) $j -= 12;
  391. print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
  392. }
  393. print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
  394. $sql = "SELECT '".$langs->trans("Vide")."' AS marge,";
  395. for ($i = 1; $i <= 12; $i++) {
  396. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  397. if ($j > 12) $j -= 12;
  398. $sql .= " SUM(".$db->ifsql('MONTH(f.datef)='.$j, '(fd.total_ht-(fd.qty * fd.buy_price_ht))', '0').") AS month".str_pad($j, 2, '0', STR_PAD_LEFT).",";
  399. }
  400. $sql .= " SUM((fd.total_ht-(fd.qty * fd.buy_price_ht))) as total";
  401. $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
  402. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
  403. $sql .= " WHERE f.datef >= '".$db->idate($search_date_start)."'";
  404. $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
  405. $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
  406. $sql .= " AND f.fk_statut > 0";
  407. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  408. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_SITUATION.")";
  409. } else {
  410. $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_DEPOSIT.",".Facture::TYPE_SITUATION.")";
  411. }
  412. dol_syslog('htdocs/accountancy/customer/index.php');
  413. $resql = $db->query($sql);
  414. if ($resql) {
  415. $num = $db->num_rows($resql);
  416. while ($row = $db->fetch_row($resql)) {
  417. print '<tr><td>'.$row[0].'</td>';
  418. for ($i = 1; $i <= 12; $i++) {
  419. print '<td class="nowrap right">'.price(price2num($row[$i])).'</td>';
  420. }
  421. print '<td class="nowrap right"><b>'.price(price2num($row[13])).'</b></td>';
  422. print '</tr>';
  423. }
  424. $db->free($resql);
  425. } else {
  426. print $db->lasterror(); // Show last sql error
  427. }
  428. print "</table>\n";
  429. print '</div>';
  430. }
  431. }
  432. // End of page
  433. llxFooter();
  434. $db->close();