index.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2021 Florian Henry <florian.henry@open-concept.pro>
  4. * Copyright (C) 2013-2022 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. // Load Dolibarr environment
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
  32. // Load translation files required by the page
  33. $langs->loadLangs(array("compta", "bills", "other", "accountancy"));
  34. $validatemonth = GETPOST('validatemonth', 'int');
  35. $validateyear = GETPOST('validateyear', 'int');
  36. // Security check
  37. if (!isModEnabled('accounting')) {
  38. accessforbidden();
  39. }
  40. if ($user->socid > 0) {
  41. accessforbidden();
  42. }
  43. if (empty($user->rights->accounting->bind->write)) {
  44. accessforbidden();
  45. }
  46. $accountingAccount = new AccountingAccount($db);
  47. $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
  48. if (GETPOST("year", 'int')) {
  49. $year_start = GETPOST("year", 'int');
  50. } else {
  51. $year_start = dol_print_date(dol_now(), '%Y');
  52. if (dol_print_date(dol_now(), '%m') < $month_start) {
  53. $year_start--; // If current month is lower that starting fiscal month, we start last year
  54. }
  55. }
  56. $year_end = $year_start + 1;
  57. $month_end = $month_start - 1;
  58. if ($month_end < 1) {
  59. $month_end = 12;
  60. $year_end--;
  61. }
  62. $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
  63. $search_date_end = dol_get_last_day($year_end, $month_end);
  64. $year_current = $year_start;
  65. // Validate History
  66. $action = GETPOST('action', 'aZ09');
  67. $chartaccountcode = dol_getIdFromCode($db, $conf->global->CHARTOFACCOUNTS, 'accounting_system', 'rowid', 'pcg_version');
  68. // Security check
  69. if (!isModEnabled('accounting')) {
  70. accessforbidden();
  71. }
  72. if ($user->socid > 0) {
  73. accessforbidden();
  74. }
  75. if (empty($user->rights->accounting->mouvements->lire)) {
  76. accessforbidden();
  77. }
  78. /*
  79. * Actions
  80. */
  81. if (($action == 'clean' || $action == 'validatehistory') && $user->rights->accounting->bind->write) {
  82. // Clean database
  83. $db->begin();
  84. $sql1 = "UPDATE ".MAIN_DB_PREFIX."facture_fourn_det as fd";
  85. $sql1 .= " SET fk_code_ventilation = 0";
  86. $sql1 .= ' WHERE fd.fk_code_ventilation NOT IN';
  87. $sql1 .= ' (SELECT accnt.rowid ';
  88. $sql1 .= ' FROM '.MAIN_DB_PREFIX.'accounting_account as accnt';
  89. $sql1 .= ' INNER JOIN '.MAIN_DB_PREFIX.'accounting_system as syst';
  90. $sql1 .= ' ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid='.$conf->global->CHARTOFACCOUNTS.' AND accnt.entity = '.$conf->entity.')';
  91. $sql1 .= ' AND fd.fk_facture_fourn IN (SELECT rowid FROM '.MAIN_DB_PREFIX.'facture_fourn WHERE entity = '.$conf->entity.')';
  92. $sql1 .= ' AND fk_code_ventilation <> 0';
  93. dol_syslog("htdocs/accountancy/customer/index.php fixaccountancycode", LOG_DEBUG);
  94. $resql1 = $db->query($sql1);
  95. if (!$resql1) {
  96. $error++;
  97. $db->rollback();
  98. setEventMessages($db->lasterror(), null, 'errors');
  99. } else {
  100. $db->commit();
  101. }
  102. // End clean database
  103. }
  104. if ($action == 'validatehistory') {
  105. $error = 0;
  106. $nbbinddone = 0;
  107. $notpossible = 0;
  108. $db->begin();
  109. // Now make the binding. Bind automatically only for product with a dedicated account that exists into chart of account, others need a manual bind
  110. /*if ($db->type == 'pgsql') {
  111. $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facture_fourn_det";
  112. $sql1 .= " SET fk_code_ventilation = accnt.rowid";
  113. $sql1 .= " FROM " . MAIN_DB_PREFIX . "product as p, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst";
  114. $sql1 .= " WHERE " . MAIN_DB_PREFIX . "facture_fourn_det.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . ((int) $conf->global->CHARTOFACCOUNTS).' AND accnt.entity = '.$conf->entity;
  115. $sql1 .= " AND accnt.active = 1 AND p.accountancy_code_buy=accnt.account_number";
  116. $sql1 .= " AND " . MAIN_DB_PREFIX . "facture_fourn_det.fk_code_ventilation = 0";
  117. } else {
  118. $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";
  119. $sql1 .= " SET fk_code_ventilation = accnt.rowid";
  120. $sql1 .= " WHERE fd.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . ((int) $conf->global->CHARTOFACCOUNTS).' AND accnt.entity = '.$conf->entity;
  121. $sql1 .= " AND accnt.active = 1 AND p.accountancy_code_buy=accnt.account_number";
  122. $sql1 .= " AND fd.fk_code_ventilation = 0";
  123. }*/
  124. // Supplier Invoice Lines (must be same request than into page list.php for manual binding)
  125. $sql = "SELECT f.rowid as facid, f.ref, f.ref_supplier, f.libelle as invoice_label, f.datef, f.type as ftype, f.fk_facture_source,";
  126. $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,";
  127. $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,";
  128. if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
  129. $sql .= " ppe.accountancy_code_buy as code_buy, ppe.accountancy_code_buy_intra as code_buy_intra, ppe.accountancy_code_buy_export as code_buy_export,";
  130. } else {
  131. $sql .= " p.accountancy_code_buy as code_buy, p.accountancy_code_buy_intra as code_buy_intra, p.accountancy_code_buy_export as code_buy_export,";
  132. }
  133. $sql .= " aa.rowid as aarowid, aa2.rowid as aarowid_intra, aa3.rowid as aarowid_export, aa4.rowid as aarowid_thirdparty,";
  134. $sql .= " co.code as country_code, co.label as country_label,";
  135. $sql .= " s.tva_intra,";
  136. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  137. $sql .= " spe.accountancy_code_buy as company_code_buy";
  138. } else {
  139. $sql .= " s.accountancy_code_buy as company_code_buy";
  140. }
  141. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
  142. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
  143. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  144. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
  145. }
  146. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON co.rowid = s.fk_pays ";
  147. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."facture_fourn_det as l ON f.rowid = l.fk_facture_fourn";
  148. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = l.fk_product";
  149. if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
  150. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
  151. }
  152. $alias_societe_perentity = empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED) ? "s" : "spe";
  153. $alias_product_perentity = empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED) ? "p" : "ppe";
  154. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON " . $alias_product_perentity . ".accountancy_code_buy = aa.account_number AND aa.active = 1 AND aa.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa.entity = ".$conf->entity;
  155. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa2 ON " . $alias_product_perentity . ".accountancy_code_buy_intra = aa2.account_number AND aa2.active = 1 AND aa2.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa2.entity = ".$conf->entity;
  156. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa3 ON " . $alias_product_perentity . ".accountancy_code_buy_export = aa3.account_number AND aa3.active = 1 AND aa3.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa3.entity = ".$conf->entity;
  157. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa4 ON " . $alias_product_perentity . ".accountancy_code_buy = aa4.account_number AND aa4.active = 1 AND aa4.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa4.entity = ".$conf->entity;
  158. $sql .= " WHERE f.fk_statut > 0 AND l.fk_code_ventilation <= 0";
  159. $sql .= " AND l.product_type <= 2";
  160. $sql .= " AND f.entity IN (".getEntity('facture_fourn', 0).")"; // We don't share object for accountancy
  161. if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
  162. $sql .= " AND f.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
  163. }
  164. if ($validatemonth && $validateyear) {
  165. $sql .= dolSqlDateFilter('f.datef', 0, $validatemonth, $validateyear);
  166. }
  167. dol_syslog('htdocs/accountancy/supplier/index.php');
  168. $result = $db->query($sql);
  169. if (!$result) {
  170. $error++;
  171. setEventMessages($db->lasterror(), null, 'errors');
  172. } else {
  173. $num_lines = $db->num_rows($result);
  174. $isBuyerInEEC = isInEEC($mysoc);
  175. $thirdpartystatic = new Societe($db);
  176. $facture_static = new FactureFournisseur($db);
  177. $facture_static_det = new SupplierInvoiceLine($db);
  178. $product_static = new Product($db);
  179. $i = 0;
  180. while ($i < min($num_lines, 10000)) { // No more than 10000 at once
  181. $objp = $db->fetch_object($result);
  182. $thirdpartystatic->id = $objp->socid;
  183. $thirdpartystatic->name = $objp->name;
  184. $thirdpartystatic->client = $objp->client;
  185. $thirdpartystatic->fournisseur = $objp->fournisseur;
  186. $thirdpartystatic->code_client = $objp->code_client;
  187. $thirdpartystatic->code_compta_client = $objp->code_compta_client;
  188. $thirdpartystatic->code_fournisseur = $objp->code_fournisseur;
  189. $thirdpartystatic->code_compta_fournisseur = $objp->code_compta_fournisseur;
  190. $thirdpartystatic->email = $objp->email;
  191. $thirdpartystatic->country_code = $objp->country_code;
  192. $thirdpartystatic->tva_intra = $objp->tva_intra;
  193. $thirdpartystatic->code_compta_product = $objp->company_code_buy; // The accounting account for product stored on thirdparty object (for level3 suggestion)
  194. $product_static->ref = $objp->product_ref;
  195. $product_static->id = $objp->product_id;
  196. $product_static->type = $objp->type;
  197. $product_static->label = $objp->product_label;
  198. $product_static->status = $objp->status;
  199. $product_static->status_buy = $objp->status_buy;
  200. $product_static->accountancy_code_sell = $objp->code_sell;
  201. $product_static->accountancy_code_sell_intra = $objp->code_sell_intra;
  202. $product_static->accountancy_code_sell_export = $objp->code_sell_export;
  203. $product_static->accountancy_code_buy = $objp->code_buy;
  204. $product_static->accountancy_code_buy_intra = $objp->code_buy_intra;
  205. $product_static->accountancy_code_buy_export = $objp->code_buy_export;
  206. $product_static->tva_tx = $objp->tva_tx_prod;
  207. $facture_static->ref = $objp->ref;
  208. $facture_static->id = $objp->facid;
  209. $facture_static->type = $objp->ftype;
  210. $facture_static->ref_supplier = $objp->ref_supplier;
  211. $facture_static->label = $objp->invoice_label;
  212. $facture_static->date = $db->jdate($objp->datef);
  213. $facture_static->fk_facture_source = $objp->fk_facture_source;
  214. $facture_static_det->id = $objp->rowid;
  215. $facture_static_det->total_ht = $objp->total_ht;
  216. $facture_static_det->tva_tx = $objp->tva_tx_line;
  217. $facture_static_det->vat_src_code = $objp->vat_src_code;
  218. $facture_static_det->product_type = $objp->type_l;
  219. $facture_static_det->desc = $objp->description;
  220. $accountingAccountArray = array(
  221. 'dom'=>$objp->aarowid,
  222. 'intra'=>$objp->aarowid_intra,
  223. 'export'=>$objp->aarowid_export,
  224. 'thirdparty' =>$objp->aarowid_thirdparty);
  225. $code_buy_p_notset = '';
  226. $code_buy_t_notset = '';
  227. $suggestedid = 0;
  228. $return = $accountingAccount->getAccountingCodeToBind($mysoc, $thirdpartystatic, $product_static, $facture_static, $facture_static_det, $accountingAccountArray, 'supplier');
  229. if (!is_array($return) && $return<0) {
  230. setEventMessage($accountingAccount->error, 'errors');
  231. } else {
  232. $suggestedid = $return['suggestedid'];
  233. $suggestedaccountingaccountfor = $return['suggestedaccountingaccountfor'];
  234. if (!empty($suggestedid) && $suggestedaccountingaccountfor != '' && $suggestedaccountingaccountfor != 'eecwithoutvatnumber') {
  235. $suggestedid = $return['suggestedid'];
  236. } else {
  237. $suggestedid = 0;
  238. }
  239. }
  240. if ($suggestedid > 0) {
  241. $sqlupdate = "UPDATE ".MAIN_DB_PREFIX."facture_fourn_det";
  242. $sqlupdate .= " SET fk_code_ventilation = ".((int) $suggestedid);
  243. $sqlupdate .= " WHERE fk_code_ventilation <= 0 AND product_type <= 2 AND rowid = ".((int) $facture_static_det->id);
  244. $resqlupdate = $db->query($sqlupdate);
  245. if (!$resqlupdate) {
  246. $error++;
  247. setEventMessages($db->lasterror(), null, 'errors');
  248. break;
  249. } else {
  250. $nbbinddone++;
  251. }
  252. } else {
  253. $notpossible++;
  254. }
  255. $i++;
  256. }
  257. if ($num_lines > 10000) {
  258. $notpossible += ($num_lines - 10000);
  259. }
  260. }
  261. if ($error) {
  262. $db->rollback();
  263. } else {
  264. $db->commit();
  265. setEventMessages($langs->trans('AutomaticBindingDone', $nbbinddone, $notpossible), null, 'mesgs');
  266. }
  267. }
  268. /*
  269. * View
  270. */
  271. llxHeader('', $langs->trans("SuppliersVentilation"));
  272. $textprevyear = '<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current - 1).'">'.img_previous().'</a>';
  273. $textnextyear = '&nbsp;<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current + 1).'">'.img_next().'</a>';
  274. print load_fiche_titre($langs->trans("SuppliersVentilation")." ".$textprevyear."&nbsp;".$langs->trans("Year")."&nbsp;".$year_start."&nbsp;".$textnextyear, '', 'title_accountancy');
  275. print '<span class="opacitymedium">'.$langs->trans("DescVentilSupplier").'</span><br>';
  276. print '<span class="opacitymedium hideonsmartphone">'.$langs->trans("DescVentilMore", $langs->transnoentitiesnoconv("ValidateHistory"), $langs->transnoentitiesnoconv("ToBind")).'<br>';
  277. print '</span><br>';
  278. $y = $year_current;
  279. $buttonbind = '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=validatehistory&token='.newToken().'">'.$langs->trans("ValidateHistory").'</a>';
  280. print_barre_liste(img_picto('', 'unlink', 'class="paddingright fa-color-unset"').$langs->trans("OverviewOfAmountOfLinesNotBound"), '', '', '', '', '', '', -1, '', '', 0, $buttonbind, '', 0, 1, 1);
  281. //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesNotBound"), $buttonbind, '');
  282. print '<div class="div-table-responsive-no-min">';
  283. print '<table class="noborder centpercent">';
  284. print '<tr class="liste_titre"><td class="minwidth100">'.$langs->trans("Account").'</td>';
  285. print '<td>'.$langs->trans("Label").'</td>';
  286. for ($i = 1; $i <= 12; $i++) {
  287. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  288. if ($j > 12) {
  289. $j -= 12;
  290. }
  291. $cursormonth = $j;
  292. if ($cursormonth > 12) {
  293. $cursormonth -= 12;
  294. }
  295. $cursoryear = ($cursormonth < ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1)) ? $y + 1 : $y;
  296. $tmp = dol_getdate(dol_get_last_day($cursoryear, $cursormonth, 'gmt'), false, 'gmt');
  297. print '<td width="60" class="right">';
  298. if (!empty($tmp['mday'])) {
  299. $param = 'search_date_startday=1&search_date_startmonth='.$cursormonth.'&search_date_startyear='.$cursoryear;
  300. $param .= '&search_date_endday='.$tmp['mday'].'&search_date_endmonth='.$tmp['mon'].'&search_date_endyear='.$tmp['year'];
  301. print '<a href="'.DOL_URL_ROOT.'/accountancy/supplier/list.php?'.$param.'">';
  302. }
  303. print $langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT));
  304. if (!empty($tmp['mday'])) {
  305. print '</a>';
  306. }
  307. print '</td>';
  308. }
  309. print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
  310. $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number')." AS codecomptable,";
  311. $sql .= " ".$db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label')." AS intitule,";
  312. for ($i = 1; $i <= 12; $i++) {
  313. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  314. if ($j > 12) {
  315. $j -= 12;
  316. }
  317. $sql .= " SUM(".$db->ifsql("MONTH(ff.datef)=".$j, "ffd.total_ht", "0").") AS month".str_pad($j, 2, "0", STR_PAD_LEFT).",";
  318. }
  319. $sql .= " SUM(ffd.total_ht) as total";
  320. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as ffd";
  321. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as ff ON ff.rowid = ffd.fk_facture_fourn";
  322. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = ffd.fk_code_ventilation";
  323. $sql .= " WHERE ff.datef >= '".$db->idate($search_date_start)."'";
  324. $sql .= " AND ff.datef <= '".$db->idate($search_date_end)."'";
  325. // Define begin binding date
  326. if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
  327. $sql .= " AND ff.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
  328. }
  329. $sql .= " AND ff.fk_statut > 0";
  330. $sql .= " AND ffd.product_type <= 2";
  331. $sql .= " AND ff.entity IN (".getEntity('facture_fourn', 0).")"; // We don't share object for accountancy
  332. $sql .= " AND aa.account_number IS NULL";
  333. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  334. $sql .= " AND ff.type IN (".FactureFournisseur::TYPE_STANDARD.",".FactureFournisseur::TYPE_REPLACEMENT.",".FactureFournisseur::TYPE_CREDIT_NOTE.")";
  335. } else {
  336. $sql .= " AND ff.type IN (".FactureFournisseur::TYPE_STANDARD.",".FactureFournisseur::TYPE_REPLACEMENT.",".FactureFournisseur::TYPE_CREDIT_NOTE.",".FactureFournisseur::TYPE_DEPOSIT.")";
  337. }
  338. $sql .= " GROUP BY ffd.fk_code_ventilation,aa.account_number,aa.label";
  339. dol_syslog('htdocs/accountancy/supplier/index.php', LOG_DEBUG);
  340. $resql = $db->query($sql);
  341. if ($resql) {
  342. $num = $db->num_rows($resql);
  343. while ($row = $db->fetch_row($resql)) {
  344. print '<tr class="oddeven">';
  345. print '<td>';
  346. if ($row[0] == 'tobind') {
  347. print '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>';
  348. } else {
  349. print length_accountg($row[0]);
  350. }
  351. print '</td>';
  352. print '<td>';
  353. if ($row[0] == 'tobind') {
  354. print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/supplier/list.php?search_year='.((int) $y), $langs->transnoentitiesnoconv("ToBind"));
  355. } else {
  356. print $row[1];
  357. }
  358. print '</td>';
  359. for ($i = 2; $i <= 13; $i++) {
  360. print '<td class="right nowraponall amount">';
  361. print price($row[$i]);
  362. print '</td>';
  363. }
  364. print '<td class="right nowraponall amount"><b>'.price($row[14]).'</b></td>';
  365. print '</tr>';
  366. }
  367. $db->free($resql);
  368. if ($num == 0) {
  369. print '<tr class="oddeven"><td colspan="16">';
  370. print '<span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span>';
  371. print '</td></tr>';
  372. }
  373. } else {
  374. print $db->lasterror(); // Show last sql error
  375. }
  376. print "</table>\n";
  377. print '</div>';
  378. print '<br>';
  379. print_barre_liste(img_picto('', 'link', 'class="paddingright fa-color-unset"').$langs->trans("OverviewOfAmountOfLinesBound"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
  380. //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesBound"), '', '');
  381. print '<div class="div-table-responsive-no-min">';
  382. print '<table class="noborder centpercent">';
  383. print '<tr class="liste_titre"><td class="minwidth100">'.$langs->trans("Account").'</td>';
  384. print '<td>'.$langs->trans("Label").'</td>';
  385. for ($i = 1; $i <= 12; $i++) {
  386. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  387. if ($j > 12) {
  388. $j -= 12;
  389. }
  390. $cursormonth = $j;
  391. if ($cursormonth > 12) {
  392. $cursormonth -= 12;
  393. }
  394. $cursoryear = ($cursormonth < ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1)) ? $y + 1 : $y;
  395. $tmp = dol_getdate(dol_get_last_day($cursoryear, $cursormonth, 'gmt'), false, 'gmt');
  396. print '<td width="60" class="right">';
  397. if (!empty($tmp['mday'])) {
  398. $param = 'search_date_startday=1&search_date_startmonth='.$cursormonth.'&search_date_startyear='.$cursoryear;
  399. $param .= '&search_date_endday='.$tmp['mday'].'&search_date_endmonth='.$tmp['mon'].'&search_date_endyear='.$tmp['year'];
  400. print '<a href="'.DOL_URL_ROOT.'/accountancy/supplier/lines.php?'.$param.'">';
  401. }
  402. print $langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT));
  403. if (!empty($tmp['mday'])) {
  404. print '</a>';
  405. }
  406. print '</td>';
  407. }
  408. print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
  409. $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number')." AS codecomptable,";
  410. $sql .= " ".$db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label')." AS intitule,";
  411. for ($i = 1; $i <= 12; $i++) {
  412. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  413. if ($j > 12) {
  414. $j -= 12;
  415. }
  416. $sql .= " SUM(".$db->ifsql("MONTH(ff.datef)=".$j, "ffd.total_ht", "0").") AS month".str_pad($j, 2, "0", STR_PAD_LEFT).",";
  417. }
  418. $sql .= " SUM(ffd.total_ht) as total";
  419. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as ffd";
  420. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as ff ON ff.rowid = ffd.fk_facture_fourn";
  421. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = ffd.fk_code_ventilation";
  422. $sql .= " WHERE ff.datef >= '".$db->idate($search_date_start)."'";
  423. $sql .= " AND ff.datef <= '".$db->idate($search_date_end)."'";
  424. // Define begin binding date
  425. if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
  426. $sql .= " AND ff.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
  427. }
  428. $sql .= " AND ff.entity IN (".getEntity('facture_fourn', 0).")"; // We don't share object for accountancy
  429. $sql .= " AND ff.fk_statut > 0";
  430. $sql .= " AND ffd.product_type <= 2";
  431. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  432. $sql .= " AND ff.type IN (".FactureFournisseur::TYPE_STANDARD.", ".FactureFournisseur::TYPE_REPLACEMENT.", ".FactureFournisseur::TYPE_CREDIT_NOTE.")";
  433. } else {
  434. $sql .= " AND ff.type IN (".FactureFournisseur::TYPE_STANDARD.", ".FactureFournisseur::TYPE_REPLACEMENT.", ".FactureFournisseur::TYPE_CREDIT_NOTE.", ".FactureFournisseur::TYPE_DEPOSIT.")";
  435. }
  436. $sql .= " AND aa.account_number IS NOT NULL";
  437. $sql .= " GROUP BY ffd.fk_code_ventilation,aa.account_number,aa.label";
  438. $sql .= ' ORDER BY aa.account_number';
  439. dol_syslog('htdocs/accountancy/supplier/index.php');
  440. $resql = $db->query($sql);
  441. if ($resql) {
  442. $num = $db->num_rows($resql);
  443. while ($row = $db->fetch_row($resql)) {
  444. print '<tr class="oddeven">';
  445. print '<td>';
  446. if ($row[0] == 'tobind') {
  447. print $langs->trans("Unknown");
  448. } else {
  449. print length_accountg($row[0]);
  450. }
  451. print '</td>';
  452. print '<td class="left">';
  453. if ($row[0] == 'tobind') {
  454. print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/supplier/list.php?search_year='.((int) $y), $langs->transnoentitiesnoconv("ToBind"));
  455. } else {
  456. print $row[1];
  457. }
  458. print '</td>';
  459. for ($i = 2; $i <= 13; $i++) {
  460. print '<td class="right nowraponall amount">';
  461. print price($row[$i]);
  462. print '</td>';
  463. }
  464. print '<td class="right nowraponall amount"><b>'.price($row[14]).'</b></td>';
  465. print '</tr>';
  466. }
  467. $db->free($resql);
  468. if ($num == 0) {
  469. print '<tr class="oddeven"><td colspan="16">';
  470. print '<span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span>';
  471. print '</td></tr>';
  472. }
  473. } else {
  474. print $db->lasterror(); // Show last sql error
  475. }
  476. print "</table>\n";
  477. print '</div>';
  478. 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 ?
  479. print '<br>';
  480. print '<br>';
  481. print_barre_liste($langs->trans("OtherInfo"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
  482. //print load_fiche_titre($langs->trans("OtherInfo"), '', '');
  483. print '<div class="div-table-responsive-no-min">';
  484. print '<table class="noborder centpercent">';
  485. print '<tr class="liste_titre"><td>'.$langs->trans("Total").'</td>';
  486. for ($i = 1; $i <= 12; $i++) {
  487. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  488. if ($j > 12) {
  489. $j -= 12;
  490. }
  491. print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
  492. }
  493. print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
  494. $sql = "SELECT '".$db->escape($langs->trans("CAHTF"))."' AS label,";
  495. for ($i = 1; $i <= 12; $i++) {
  496. $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
  497. if ($j > 12) {
  498. $j -= 12;
  499. }
  500. $sql .= " SUM(".$db->ifsql("MONTH(ff.datef)=".$j, "ffd.total_ht", "0").") AS month".str_pad($j, 2, "0", STR_PAD_LEFT).",";
  501. }
  502. $sql .= " SUM(ffd.total_ht) as total";
  503. $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as ffd";
  504. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as ff ON ff.rowid = ffd.fk_facture_fourn";
  505. $sql .= " WHERE ff.datef >= '".$db->idate($search_date_start)."'";
  506. $sql .= " AND ff.datef <= '".$db->idate($search_date_end)."'";
  507. // Define begin binding date
  508. if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
  509. $sql .= " AND ff.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
  510. }
  511. $sql .= " AND ff.entity IN (".getEntity('facture_fourn', 0).")"; // We don't share object for accountancy
  512. $sql .= " AND ff.fk_statut > 0";
  513. $sql .= " AND ffd.product_type <= 2";
  514. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  515. $sql .= " AND ff.type IN (".FactureFournisseur::TYPE_STANDARD.", ".FactureFournisseur::TYPE_REPLACEMENT.", ".FactureFournisseur::TYPE_CREDIT_NOTE.")";
  516. } else {
  517. $sql .= " AND ff.type IN (".FactureFournisseur::TYPE_STANDARD.", ".FactureFournisseur::TYPE_REPLACEMENT.", ".FactureFournisseur::TYPE_CREDIT_NOTE.", ".FactureFournisseur::TYPE_DEPOSIT.")";
  518. }
  519. dol_syslog('htdocs/accountancy/supplier/index.php');
  520. $resql = $db->query($sql);
  521. if ($resql) {
  522. $num = $db->num_rows($resql);
  523. while ($row = $db->fetch_row($resql)) {
  524. print '<tr><td>'.$row[0].'</td>';
  525. for ($i = 1; $i <= 12; $i++) {
  526. print '<td class="right nowraponall amount">'.price($row[$i]).'</td>';
  527. }
  528. print '<td class="right nowraponall amount"><b>'.price($row[13]).'</b></td>';
  529. print '</tr>';
  530. }
  531. $db->free($resql);
  532. } else {
  533. print $db->lasterror(); // Show last sql error
  534. }
  535. print "</table>\n";
  536. print '</div>';
  537. }
  538. // End of page
  539. llxFooter();
  540. $db->close();