index.php 27 KB

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