index.php 33 KB

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