index.php 32 KB

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