index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
  4. * Copyright (C) 2013-2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
  5. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/accountancy/supplier/index.php
  22. * \ingroup Accountancy (Double entries)
  23. * \brief Home supplier journalization page
  24. */
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
  27. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  28. require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array("compta","bills","other","main","accountancy"));
  31. // Security check
  32. if (empty($conf->accounting->enabled)) {
  33. accessforbidden();
  34. }
  35. if ($user->societe_id > 0)
  36. accessforbidden();
  37. if (! $user->rights->accounting->bind->write)
  38. accessforbidden();
  39. $month_start= ($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
  40. if (GETPOST("year", 'int')) $year_start = GETPOST("year", 'int');
  41. else
  42. {
  43. $year_start = dol_print_date(dol_now(), '%Y');
  44. if (dol_print_date(dol_now(), '%m') < $month_start) $year_start--; // If current month is lower that starting fiscal month, we start last year
  45. }
  46. $year_end = $year_start + 1;
  47. $month_end = $month_start - 1;
  48. if ($month_end < 1)
  49. {
  50. $month_end = 12;
  51. $year_end--;
  52. }
  53. $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
  54. $search_date_end = dol_get_last_day($year_end, $month_end);
  55. $year_current = $year_start;
  56. // Validate History
  57. $action = GETPOST('action', 'aZ09');
  58. /*
  59. * Actions
  60. */
  61. if ($action == 'clean' || $action == 'validatehistory')
  62. {
  63. // Clean database
  64. $db->begin();
  65. $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facture_fourn_det as fd";
  66. $sql1 .= " SET fk_code_ventilation = 0";
  67. $sql1 .= ' WHERE fd.fk_code_ventilation NOT IN';
  68. $sql1 .= ' (SELECT accnt.rowid ';
  69. $sql1 .= ' FROM ' . MAIN_DB_PREFIX . 'accounting_account as accnt';
  70. $sql1 .= ' INNER JOIN ' . MAIN_DB_PREFIX . 'accounting_system as syst';
  71. $sql1 .= ' ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=' . $conf->global->CHARTOFACCOUNTS . ' AND accnt.entity = '.$conf->entity.')';
  72. $sql1 .= ' AND fd.fk_facture_fourn IN (SELECT rowid FROM ' . MAIN_DB_PREFIX . 'facture_fourn WHERE entity = '.$conf->entity.')';
  73. $sql1 .= ' AND fk_code_ventilation <> 0';
  74. dol_syslog("htdocs/accountancy/customer/index.php fixaccountancycode", LOG_DEBUG);
  75. $resql1 = $db->query($sql1);
  76. if (! $resql1) {
  77. $error ++;
  78. $db->rollback();
  79. setEventMessages($db->lasterror(), null, 'errors');
  80. } else {
  81. $db->commit();
  82. }
  83. // End clean database
  84. }
  85. if ($action == 'validatehistory') {
  86. $error = 0;
  87. $db->begin();
  88. // Now make the binding. Bind automatically only for product with a dedicated account that exists into chart of account, others need a manual bind
  89. if ($db->type == 'pgsql') {
  90. $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facture_fourn_det";
  91. $sql1 .= " SET fk_code_ventilation = accnt.rowid";
  92. $sql1 .= " FROM " . MAIN_DB_PREFIX . "product as p, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst";
  93. $sql1 .= " WHERE " . MAIN_DB_PREFIX . "facture_fourn_det.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . $conf->global->CHARTOFACCOUNTS.' AND accnt.entity = '.$conf->entity;
  94. $sql1 .= " AND accnt.active = 1 AND p.accountancy_code_buy=accnt.account_number";
  95. $sql1 .= " AND " . MAIN_DB_PREFIX . "facture_fourn_det.fk_code_ventilation = 0";
  96. } else {
  97. $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facture_fourn_det as fd, " . MAIN_DB_PREFIX . "product as p, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst";
  98. $sql1 .= " SET fk_code_ventilation = accnt.rowid";
  99. $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;
  100. $sql1 .= " AND accnt.active = 1 AND p.accountancy_code_buy=accnt.account_number";
  101. $sql1 .= " AND fd.fk_code_ventilation = 0";
  102. }
  103. dol_syslog('htdocs/accountancy/supplier/index.php');
  104. $resql1 = $db->query($sql1);
  105. if (! $resql1) {
  106. $error ++;
  107. $db->rollback();
  108. setEventMessages($db->lasterror(), null, 'errors');
  109. } else {
  110. $db->commit();
  111. setEventMessages($langs->trans('AutomaticBindingDone'), null, 'mesgs');
  112. }
  113. }
  114. /*
  115. * View
  116. */
  117. llxHeader('', $langs->trans("SuppliersVentilation"));
  118. $textprevyear = '<a href="' . $_SERVER["PHP_SELF"] . '?year=' . ($year_current - 1) . '">' . img_previous() . '</a>';
  119. $textnextyear = '&nbsp;<a href="' . $_SERVER["PHP_SELF"] . '?year=' . ($year_current + 1) . '">' . img_next() . '</a>';
  120. print load_fiche_titre($langs->trans("SuppliersVentilation") . " " . $textprevyear . "&nbsp;" . $langs->trans("Year") . "&nbsp;" . $year_start . "&nbsp;" . $textnextyear, '', 'title_accountancy');
  121. print '<span class="opacitymedium">'.$langs->trans("DescVentilSupplier") . '<br>';
  122. print $langs->trans("DescVentilMore", $langs->transnoentitiesnoconv("ValidateHistory"), $langs->transnoentitiesnoconv("ToBind")) . '<br>';
  123. print '</span><br>';
  124. $y = $year_current;
  125. $buttonbind = '<a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=validatehistory">' . $langs->trans("ValidateHistory") . '</a>';
  126. print_barre_liste($langs->trans("OverviewOfAmountOfLinesNotBound"), '', '', '', '', '', '', -1, '', '', 0, $buttonbind, '', 0, 1, 1);
  127. //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesNotBound"), $buttonbind, '');
  128. print '<div class="div-table-responsive-no-min">';
  129. print '<table class="noborder" width="100%">';
  130. print '<tr class="liste_titre"><td width="200">' . $langs->trans("Account") . '</td>';
  131. print '<td width="200" class="left">' . $langs->trans("Label") . '</td>';
  132. for($i = 1; $i <= 12; $i ++) {
  133. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START?$conf->global->SOCIETE_FISCAL_MONTH_START:1) - 1;
  134. if ($j > 12) $j-=12;
  135. print '<td width="60" class="right">' . $langs->trans('MonthShort' . str_pad($j, 2, '0', STR_PAD_LEFT)) . '</td>';
  136. }
  137. print '<td width="60" class="right"><b>' . $langs->trans("Total") . '</b></td></tr>';
  138. $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number') ." AS codecomptable,";
  139. $sql .= " " . $db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label') . " AS intitule,";
  140. for($i = 1; $i <= 12; $i ++) {
  141. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START?$conf->global->SOCIETE_FISCAL_MONTH_START:1) - 1;
  142. if ($j > 12) $j-=12;
  143. $sql .= " SUM(" . $db->ifsql('MONTH(ff.datef)=' . $j, 'ffd.total_ht', '0') . ") AS month" . str_pad($j, 2, '0', STR_PAD_LEFT) . ",";
  144. }
  145. $sql .= " SUM(ffd.total_ht) as total";
  146. $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as ffd";
  147. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "facture_fourn as ff ON ff.rowid = ffd.fk_facture_fourn";
  148. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = ffd.fk_code_ventilation";
  149. $sql .= " WHERE ff.datef >= '" . $db->idate($search_date_start) . "'";
  150. $sql .= " AND ff.datef <= '" . $db->idate($search_date_end) . "'";
  151. $sql .= " AND ff.fk_statut > 0";
  152. $sql .= " AND ff.entity IN (" . getEntity('facture_fourn', 0) . ")"; // We don't share object for accountancy
  153. $sql .= " AND aa.account_number IS NULL";
  154. $sql .= " GROUP BY ffd.fk_code_ventilation,aa.account_number,aa.label";
  155. dol_syslog('htdocs/accountancy/supplier/index.php');
  156. $resql = $db->query($sql);
  157. if ($resql) {
  158. $num = $db->num_rows($resql);
  159. while ( $row = $db->fetch_row($resql)) {
  160. print '<tr class="oddeven"><td>';
  161. if ($row[0] == 'tobind')
  162. {
  163. print $langs->trans("Unknown");
  164. }
  165. else print length_accountg($row[0]);
  166. print '</td>';
  167. print '<td class="left">';
  168. if ($row[0] == 'tobind')
  169. {
  170. print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/supplier/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind"));
  171. }
  172. else print $row[1];
  173. print '</td>';
  174. for($i = 2; $i <= 12; $i ++) {
  175. print '<td class="nowrap right">' . price($row[$i]) . '</td>';
  176. }
  177. print '<td class="nowrap right">' . price($row[13]) . '</td>';
  178. print '<td class="nowrap right"><b>' . price($row[14]) . '</b></td>';
  179. print '</tr>';
  180. }
  181. $db->free($resql);
  182. } else {
  183. print $db->lasterror(); // Show last sql error
  184. }
  185. print "</table>\n";
  186. print '</div>';
  187. print '<br>';
  188. print_barre_liste($langs->trans("OverviewOfAmountOfLinesBound"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
  189. //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesBound"), '', '');
  190. print '<div class="div-table-responsive-no-min">';
  191. print '<table class="noborder" width="100%">';
  192. print '<tr class="liste_titre"><td width="200">' . $langs->trans("Account") . '</td>';
  193. print '<td width="200" class="left">' . $langs->trans("Label") . '</td>';
  194. for($i = 1; $i <= 12; $i ++) {
  195. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START?$conf->global->SOCIETE_FISCAL_MONTH_START:1) - 1;
  196. if ($j > 12) $j-=12;
  197. print '<td width="60" class="right">' . $langs->trans('MonthShort' . str_pad($j, 2, '0', STR_PAD_LEFT)) . '</td>';
  198. }
  199. print '<td width="60" class="right"><b>' . $langs->trans("Total") . '</b></td></tr>';
  200. $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number') ." AS codecomptable,";
  201. $sql .= " " . $db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label') . " AS intitule,";
  202. for($i = 1; $i <= 12; $i ++) {
  203. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START?$conf->global->SOCIETE_FISCAL_MONTH_START:1) - 1;
  204. if ($j > 12) $j-=12;
  205. $sql .= " SUM(" . $db->ifsql('MONTH(ff.datef)=' . $j, 'ffd.total_ht', '0') . ") AS month" . str_pad($j, 2, '0', STR_PAD_LEFT) . ",";
  206. }
  207. $sql .= " SUM(ffd.total_ht) as total";
  208. $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as ffd";
  209. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "facture_fourn as ff ON ff.rowid = ffd.fk_facture_fourn";
  210. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = ffd.fk_code_ventilation";
  211. $sql .= " WHERE ff.datef >= '" . $db->idate($search_date_start) . "'";
  212. $sql .= " AND ff.datef <= '" . $db->idate($search_date_end) . "'";
  213. $sql .= " AND ff.fk_statut > 0";
  214. $sql .= " AND ff.entity IN (" . getEntity('facture_fourn', 0) . ")"; // We don't share object for accountancy
  215. $sql .= " AND aa.account_number IS NOT NULL";
  216. $sql .= " GROUP BY ffd.fk_code_ventilation,aa.account_number,aa.label";
  217. dol_syslog('htdocs/accountancy/supplier/index.php');
  218. $resql = $db->query($sql);
  219. if ($resql) {
  220. $num = $db->num_rows($resql);
  221. while ( $row = $db->fetch_row($resql)) {
  222. print '<tr class="oddeven"><td>';
  223. if ($row[0] == 'tobind')
  224. {
  225. print $langs->trans("Unknown");
  226. }
  227. else print length_accountg($row[0]);
  228. print '</td>';
  229. print '<td class="left">';
  230. if ($row[0] == 'tobind')
  231. {
  232. print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/supplier/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind"));
  233. }
  234. else print $row[1];
  235. print '</td>';
  236. for($i = 2; $i <= 12; $i++) {
  237. print '<td class="nowrap right">' . price($row[$i]) . '</td>';
  238. }
  239. print '<td class="nowrap right">' . price($row[13]) . '</td>';
  240. print '<td class="nowrap right"><b>' . price($row[14]) . '</b></td>';
  241. print '</tr>';
  242. }
  243. $db->free($resql);
  244. } else {
  245. print $db->lasterror(); // Show last sql error
  246. }
  247. print "</table>\n";
  248. print '</div>';
  249. 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 ?
  250. {
  251. print '<br>';
  252. print '<br>';
  253. print_barre_liste($langs->trans("OtherInfo"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
  254. //print load_fiche_titre($langs->trans("OtherInfo"), '', '');
  255. print '<div class="div-table-responsive-no-min">';
  256. print '<table class="noborder" width="100%">';
  257. print '<tr class="liste_titre"><td width="400" class="left">' . $langs->trans("Total") . '</td>';
  258. for($i = 1; $i <= 12; $i ++) {
  259. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START?$conf->global->SOCIETE_FISCAL_MONTH_START:1) - 1;
  260. if ($j > 12) $j-=12;
  261. print '<td width="60" class="right">' . $langs->trans('MonthShort' . str_pad($j, 2, '0', STR_PAD_LEFT)) . '</td>';
  262. }
  263. print '<td width="60" class="right"><b>' . $langs->trans("Total") . '</b></td></tr>';
  264. $sql = "SELECT '" . $langs->trans("CAHTF") . "' AS label,";
  265. for($i = 1; $i <= 12; $i ++) {
  266. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START?$conf->global->SOCIETE_FISCAL_MONTH_START:1) - 1;
  267. if ($j > 12) $j-=12;
  268. $sql .= " SUM(" . $db->ifsql('MONTH(ff.datef)=' . $j, 'ffd.total_ht', '0') . ") AS month" . str_pad($j, 2, '0', STR_PAD_LEFT) . ",";
  269. }
  270. $sql .= " SUM(ffd.total_ht) as total";
  271. $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as ffd";
  272. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "facture_fourn as ff ON ff.rowid = ffd.fk_facture_fourn";
  273. $sql .= " WHERE ff.datef >= '" . $db->idate($search_date_start) . "'";
  274. $sql .= " AND ff.datef <= '" . $db->idate($search_date_end) . "'";
  275. $sql .= " AND ff.fk_statut > 0";
  276. $sql .= " AND ff.entity IN (" . getEntity('facture_fourn', 0) . ")"; // We don't share object for accountancy
  277. dol_syslog('htdocs/accountancy/supplier/index.php');
  278. $resql = $db->query($sql);
  279. if ($resql) {
  280. $num = $db->num_rows($resql);
  281. while ($row = $db->fetch_row($resql)) {
  282. print '<tr><td>' . $row[0] . '</td>';
  283. for($i = 1; $i <= 12; $i ++) {
  284. print '<td class="nowrap right">' . price($row[$i]) . '</td>';
  285. }
  286. print '<td class="nowrap right"><b>' . price($row[13]) . '</b></td>';
  287. print '</tr>';
  288. }
  289. $db->free($resql);
  290. } else {
  291. print $db->lasterror(); // Show last sql error
  292. }
  293. print "</table>\n";
  294. print '</div>';
  295. }
  296. // End of page
  297. llxFooter();
  298. $db->close();