bankjournal.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. <?php
  2. /* Copyright (C) 2007-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2007-2010 Jean Heimburger <jean@tiaris.info>
  4. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2013 Christophe Battarel <christophe.battarel@altairis.fr>
  7. * Copyright (C) 2013-2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
  8. * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
  9. * Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  10. * Copyright (C) 2017-2018 Frédéric France <frederic.france@netlogic.fr>
  11. * Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es>
  12. * Copyright (C) 2018 Eric Seigne <eric.seigne@cap-rel.fr>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. */
  27. /**
  28. * \file htdocs/accountancy/journal/bankjournal.php
  29. * \ingroup Advanced accountancy
  30. * \brief Page with bank journal
  31. */
  32. require '../../main.inc.php';
  33. require_once DOL_DOCUMENT_ROOT . '/core/lib/report.lib.php';
  34. require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
  35. require_once DOL_DOCUMENT_ROOT . '/core/lib/bank.lib.php';
  36. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  37. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
  38. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingjournal.class.php';
  39. require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  40. require_once DOL_DOCUMENT_ROOT . '/user/class/user.class.php';
  41. require_once DOL_DOCUMENT_ROOT . '/adherents/class/adherent.class.php';
  42. require_once DOL_DOCUMENT_ROOT . '/compta/sociales/class/chargesociales.class.php';
  43. require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php';
  44. require_once DOL_DOCUMENT_ROOT . '/don/class/don.class.php';
  45. require_once DOL_DOCUMENT_ROOT . '/don/class/paymentdonation.class.php';
  46. require_once DOL_DOCUMENT_ROOT . '/compta/tva/class/tva.class.php';
  47. require_once DOL_DOCUMENT_ROOT . '/compta/salaries/class/paymentsalary.class.php';
  48. require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
  49. require_once DOL_DOCUMENT_ROOT . '/fourn/class/paiementfourn.class.php';
  50. require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
  51. require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.class.php';
  52. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php';
  53. require_once DOL_DOCUMENT_ROOT . '/societe/class/client.class.php';
  54. require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php';
  55. require_once DOL_DOCUMENT_ROOT . '/expensereport/class/paymentexpensereport.class.php';
  56. require_once DOL_DOCUMENT_ROOT . '/compta/bank/class/paymentvarious.class.php';
  57. require_once DOL_DOCUMENT_ROOT . '/compta/bank/class/account.class.php';
  58. require_once DOL_DOCUMENT_ROOT . '/loan/class/loan.class.php';
  59. require_once DOL_DOCUMENT_ROOT . '/loan/class/paymentloan.class.php';
  60. require_once DOL_DOCUMENT_ROOT . '/adherents/class/subscription.class.php';
  61. // Load translation files required by the page
  62. $langs->loadLangs(array("companies","other","compta","banks","bills","donations","loan","accountancy","trips","salaries","hrm","members"));
  63. // Multi journal
  64. $id_journal = GETPOST('id_journal', 'int');
  65. $date_startmonth = GETPOST('date_startmonth','int');
  66. $date_startday = GETPOST('date_startday','int');
  67. $date_startyear = GETPOST('date_startyear','int');
  68. $date_endmonth = GETPOST('date_endmonth','int');
  69. $date_endday = GETPOST('date_endday','int');
  70. $date_endyear = GETPOST('date_endyear','int');
  71. $in_bookkeeping = GETPOST('in_bookkeeping','aZ09');
  72. if ($in_bookkeeping == '') $in_bookkeeping = 'notyet';
  73. $now = dol_now();
  74. $action = GETPOST('action','aZ09');
  75. // Security check
  76. if ($user->societe_id > 0 && empty($id_journal))
  77. accessforbidden();
  78. /*
  79. * Actions
  80. */
  81. $error = 0;
  82. $year_current = strftime("%Y", dol_now());
  83. $pastmonth = strftime("%m", dol_now()) - 1;
  84. $pastmonthyear = $year_current;
  85. if ($pastmonth == 0) {
  86. $pastmonth = 12;
  87. $pastmonthyear --;
  88. }
  89. $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  90. $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  91. if (! GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) // We define date_start and date_end, only if we did not submit the form
  92. {
  93. $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
  94. $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
  95. }
  96. $idpays = $mysoc->country_id;
  97. $sql = "SELECT b.rowid, b.dateo as do, b.datev as dv, b.amount, b.label, b.rappro, b.num_releve, b.num_chq, b.fk_type, b.fk_account,";
  98. $sql .= " ba.courant, ba.ref as baref, ba.account_number, ba.fk_accountancy_journal,";
  99. $sql .= " soc.code_compta, soc.code_compta_fournisseur, soc.rowid as socid, soc.nom as name, soc.email as email, bu1.type as typeop_company,";
  100. $sql .= " u.accountancy_code, u.rowid as userid, u.lastname as lastname, u.firstname as firstname, u.email as useremail, bu2.type as typeop_user,";
  101. $sql .= " bu3.type as typeop_payment, bu4.type as typeop_payment_supplier";
  102. $sql .= " FROM " . MAIN_DB_PREFIX . "bank as b";
  103. $sql .= " JOIN " . MAIN_DB_PREFIX . "bank_account as ba on b.fk_account=ba.rowid";
  104. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu1 ON bu1.fk_bank = b.rowid AND bu1.type='company'";
  105. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu2 ON bu2.fk_bank = b.rowid AND bu2.type='user'";
  106. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu3 ON bu3.fk_bank = b.rowid AND bu3.type='payment'";
  107. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu4 ON bu4.fk_bank = b.rowid AND bu4.type='payment_supplier'";
  108. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as soc on bu1.url_id=soc.rowid";
  109. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as u on bu2.url_id=u.rowid";
  110. $sql .= " WHERE ba.fk_accountancy_journal=" . $id_journal;
  111. $sql .= ' AND b.amount != 0 AND ba.entity IN ('.getEntity('bank_account', 0).')'; // We don't share object for accountancy
  112. if ($date_start && $date_end)
  113. $sql .= " AND b.dateo >= '" . $db->idate($date_start) . "' AND b.dateo <= '" . $db->idate($date_end) . "'";
  114. // Already in bookkeeping or not
  115. if ($in_bookkeeping == 'already')
  116. {
  117. $sql .= " AND (b.rowid IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='bank') )";
  118. }
  119. if ($in_bookkeeping == 'notyet')
  120. {
  121. $sql .= " AND (b.rowid NOT IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='bank') )";
  122. }
  123. $sql .= " ORDER BY b.datev";
  124. //print $sql;
  125. $object = new Account($db);
  126. $paymentstatic = new Paiement($db);
  127. $paymentsupplierstatic = new PaiementFourn($db);
  128. $societestatic = new Societe($db);
  129. $userstatic = new User($db);
  130. $bankaccountstatic = new Account($db);
  131. $chargestatic = new ChargeSociales($db);
  132. $paymentdonstatic = new PaymentDonation($db);
  133. $paymentvatstatic = new TVA($db);
  134. $paymentsalstatic = new PaymentSalary($db);
  135. $paymentexpensereportstatic = new PaymentExpenseReport($db);
  136. $paymentvariousstatic = new PaymentVarious($db);
  137. $paymentloanstatic = new PaymentLoan($db);
  138. $accountLinestatic=new AccountLine($db);
  139. $paymentsubscriptionstatic = new Subscription($db);
  140. $accountingaccount = new AccountingAccount($db);
  141. // Get code of finance journal
  142. $accountingjournalstatic = new AccountingJournal($db);
  143. $accountingjournalstatic->fetch($id_journal);
  144. $journal = $accountingjournalstatic->code;
  145. $journal_label = $accountingjournalstatic->label;
  146. dol_syslog("accountancy/journal/bankjournal.php", LOG_DEBUG);
  147. $result = $db->query($sql);
  148. if ($result) {
  149. $num = $db->num_rows($result);
  150. //print $sql;
  151. // Variables
  152. $account_supplier = (! empty($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER) ? $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER : 'NotDefined'); // NotDefined is a reserved word
  153. $account_customer = (! empty($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER) ? $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER : 'NotDefined'); // NotDefined is a reserved word
  154. $account_employee = (! empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) ? $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT : 'NotDefined'); // NotDefined is a reserved word
  155. $account_pay_vat = (! empty($conf->global->ACCOUNTING_VAT_PAY_ACCOUNT) ? $conf->global->ACCOUNTING_VAT_PAY_ACCOUNT : 'NotDefined'); // NotDefined is a reserved word
  156. $account_pay_donation = (! empty($conf->global->DONATION_ACCOUNTINGACCOUNT) ? $conf->global->DONATION_ACCOUNTINGACCOUNT : 'NotDefined'); // NotDefined is a reserved word
  157. $account_pay_subscription = (! empty($conf->global->ADHERENT_SUBSCRIPTION_ACCOUNTINGACCOUNT) ? $conf->global->ADHERENT_SUBSCRIPTION_ACCOUNTINGACCOUNT : 'NotDefined'); // NotDefined is a reserved word
  158. $account_transfer = (! empty($conf->global->ACCOUNTING_ACCOUNT_TRANSFER_CASH) ? $conf->global->ACCOUNTING_ACCOUNT_TRANSFER_CASH : 'NotDefined'); // NotDefined is a reserved word
  159. $tabcompany = array();
  160. $tabuser = array();
  161. $tabpay = array ();
  162. $tabbq = array ();
  163. $tabtp = array ();
  164. $tabtype = array ();
  165. // Loop on each line into llx_bank table. For each line, we should get:
  166. // one line tabpay = line into bank
  167. // one line for bank record = tabbq
  168. // one line for thirdparty record = tabtp
  169. $i = 0;
  170. while ( $i < $num )
  171. {
  172. $obj = $db->fetch_object($result);
  173. $lineisapurchase = -1;
  174. $lineisasale = -1;
  175. // Old method to detect if it's a sale or purchase
  176. if ($obj->label == '(SupplierInvoicePayment)' || $obj->label == '(SupplierInvoicePaymentBack)') $lineisapurchase=1;
  177. if ($obj->label == '(CustomerInvoicePayment)' || $obj->label == '(CustomerInvoicePaymentBack)') $lineisasale=1;
  178. // Try a more reliable method to detect if record is a supplier payment or a customer payment
  179. if ($lineisapurchase < 0)
  180. {
  181. if ($obj->typeop_payment_supplier == 'payment_supplier') $lineisapurchase = 1;
  182. }
  183. if ($lineisasale < 0)
  184. {
  185. if ($obj->typeop_payment == 'payment') $lineisasale = 1;
  186. }
  187. //var_dump($obj->type_payment); var_dump($obj->type_payment_supplier);
  188. //var_dump($lineisapurchase); //var_dump($lineisasale);
  189. // Set accountancy code for bank
  190. $compta_bank = $obj->account_number;
  191. // Set accountancy code for thirdparty
  192. $compta_soc = 'NotDefined';
  193. if ($lineisapurchase > 0)
  194. $compta_soc = (! empty($obj->code_compta_fournisseur) ? $obj->code_compta_fournisseur : $account_supplier);
  195. if ($lineisasale > 0)
  196. $compta_soc = (! empty($obj->code_compta) ? $obj->code_compta : $account_customer);
  197. $tabcompany[$obj->rowid] = array (
  198. 'id' => $obj->socid,
  199. 'name' => $obj->name,
  200. 'code_compta' => $compta_soc,
  201. 'email' => $obj->email
  202. );
  203. // Set accountancy code for user
  204. $compta_user = (! empty($obj->accountancy_code) ? $obj->accountancy_code : $account_employee);
  205. $tabuser[$obj->rowid] = array (
  206. 'id' => $obj->userid,
  207. 'name' => dolGetFirstLastname($obj->firstname, $obj->lastname),
  208. 'lastname' => $obj->lastname,
  209. 'firstname' => $obj->firstname,
  210. 'email' => $obj->useremail,
  211. 'accountancy_code' => $compta_user
  212. );
  213. // Variable bookkeeping ($obj->rowid is Bank Id)
  214. $tabpay[$obj->rowid]["date"] = $obj->do;
  215. $tabpay[$obj->rowid]["type_payment"] = $obj->fk_type; // CHQ, VIR, LIQ, CB, ...
  216. $tabpay[$obj->rowid]["ref"] = $obj->label; // By default. Not unique. May be changed later
  217. $tabpay[$obj->rowid]["fk_bank"] = $obj->rowid;
  218. $tabpay[$obj->rowid]["bank_account_ref"] = $obj->baref;
  219. $tabpay[$obj->rowid]["fk_bank_account"] = $obj->fk_account;
  220. if (preg_match('/^\((.*)\)$/i', $obj->label, $reg)) {
  221. $tabpay[$obj->rowid]["lib"] = $langs->trans($reg[1]);
  222. } else {
  223. $tabpay[$obj->rowid]["lib"] = dol_trunc($obj->label, 60);
  224. }
  225. $links = $object->get_url($obj->rowid); // Get an array('url'=>, 'url_id'=>, 'label'=>, 'type'=> 'fk_bank'=> )
  226. //var_dump($i);
  227. //var_dump($tabpay);
  228. // By default
  229. $tabpay[$obj->rowid]['type'] = 'unknown'; // Can be SOLD, miscellaneous entry, payment of patient, or any old record with no links in bank_url.
  230. $tabtype[$obj->rowid] = 'unknown';
  231. // get_url may return -1 which is not traversable
  232. if (is_array($links) && count($links) > 0) {
  233. // Now loop on each link of record in bank.
  234. foreach ($links as $key => $val) {
  235. if (in_array($links[$key]['type'], array('sc', 'payment_sc', 'payment', 'payment_supplier', 'payment_vat', 'payment_expensereport', 'banktransfert', 'payment_donation', 'member', 'payment_loan', 'payment_salary', 'payment_various')))
  236. {
  237. // So we excluded 'company' and 'user' here. We want only payment lines
  238. // We save tabtype for a future use, to remember what kind of payment it is
  239. $tabpay[$obj->rowid]['type'] = $links[$key]['type'];
  240. $tabtype[$obj->rowid] = $links[$key]['type'];
  241. }
  242. elseif (in_array($links[$key]['type'], array('company', 'user')))
  243. {
  244. if ($tabpay[$obj->rowid]['type'] == 'unknown')
  245. {
  246. // We can guess here it is a bank record for a thirdparty company or a user.
  247. // But we won't be able to record somewhere else than into a waiting account, because there is no other journal to record the contreparty.
  248. }
  249. }
  250. if ($links[$key]['type'] == 'payment') {
  251. $paymentstatic->id = $links[$key]['url_id'];
  252. $paymentstatic->ref = $links[$key]['url_id'];
  253. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentstatic->getNomUrl(2, '', ''); // TODO Do not include list of invoice in tooltip, the dol_string_nohtmltag is ko with this
  254. $tabpay[$obj->rowid]["paymentid"] = $paymentstatic->id;
  255. } elseif ($links[$key]['type'] == 'payment_supplier') {
  256. $paymentsupplierstatic->id = $links[$key]['url_id'];
  257. $paymentsupplierstatic->ref = $links[$key]['url_id'];
  258. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentsupplierstatic->getNomUrl(2);
  259. $tabpay[$obj->rowid]["paymentsupplierid"] = $paymentsupplierstatic->id;
  260. } elseif ($links[$key]['type'] == 'company') {
  261. $societestatic->id = $links[$key]['url_id'];
  262. $societestatic->name = $links[$key]['label'];
  263. $societestatic->email = $tabcompany[$obj->rowid]['email'];
  264. $tabpay[$obj->rowid]["soclib"] = $societestatic->getNomUrl(1, '', 30);
  265. if ($compta_soc) $tabtp[$obj->rowid][$compta_soc] += $obj->amount;
  266. } elseif ($links[$key]['type'] == 'user') {
  267. $userstatic->id = $links[$key]['url_id'];
  268. $userstatic->name = $links[$key]['label'];
  269. $userstatic->email = $tabuser[$obj->rowid]['email'];
  270. $userstatic->firstname = $tabuser[$obj->rowid]['firstname'];
  271. $userstatic->lastname = $tabuser[$obj->rowid]['lastname'];
  272. if ($userstatic->id > 0) $tabpay[$obj->rowid]["soclib"] = $userstatic->getNomUrl(1, '', 30);
  273. else $tabpay[$obj->rowid]["soclib"] = '???'; // Should not happen, but happens with old data when id of user was not saved on expense report payment.
  274. if ($compta_user) $tabtp[$obj->rowid][$compta_user] += $obj->amount;
  275. } elseif ($links[$key]['type'] == 'sc') {
  276. $chargestatic->id = $links[$key]['url_id'];
  277. $chargestatic->ref = $links[$key]['url_id'];
  278. $tabpay[$obj->rowid]["lib"] .= ' ' . $chargestatic->getNomUrl(2);
  279. if (preg_match('/^\((.*)\)$/i', $links[$key]['label'], $reg)) {
  280. if ($reg[1] == 'socialcontribution')
  281. $reg[1] = 'SocialContribution';
  282. $chargestatic->lib = $langs->trans($reg[1]);
  283. } else {
  284. $chargestatic->lib = $links[$key]['label'];
  285. }
  286. $chargestatic->ref = $chargestatic->lib;
  287. $tabpay[$obj->rowid]["soclib"] = $chargestatic->getNomUrl(1, 30);
  288. $tabpay[$obj->rowid]["paymentscid"] = $chargestatic->id;
  289. $sqlmid = 'SELECT cchgsoc.accountancy_code';
  290. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "c_chargesociales cchgsoc ";
  291. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "chargesociales as chgsoc ON chgsoc.fk_type=cchgsoc.id";
  292. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementcharge as paycharg ON paycharg.fk_charge=chgsoc.rowid";
  293. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "bank_url as bkurl ON bkurl.url_id=paycharg.rowid";
  294. $sqlmid .= " WHERE bkurl.fk_bank=" . $obj->rowid;
  295. dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
  296. $resultmid = $db->query($sqlmid);
  297. if ($resultmid) {
  298. $objmid = $db->fetch_object($resultmid);
  299. $tabtp[$obj->rowid][$objmid->accountancy_code] += $obj->amount;
  300. }
  301. } elseif ($links[$key]['type'] == 'payment_donation') {
  302. $paymentdonstatic->id = $links[$key]['url_id'];
  303. $paymentdonstatic->ref = $links[$key]['url_id'];
  304. $paymentdonstatic->fk_donation = $links[$key]['url_id'];
  305. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentdonstatic->getNomUrl(2);
  306. $tabpay[$obj->rowid]["paymentdonationid"] = $paymentdonstatic->id;
  307. $tabtp[$obj->rowid][$account_pay_donation] += $obj->amount;
  308. } elseif ($links[$key]['type'] == 'member') {
  309. $paymentsubscriptionstatic->id = $links[$key]['url_id'];
  310. $paymentsubscriptionstatic->ref = $links[$key]['url_id'];
  311. $paymentsubscriptionstatic->label = $links[$key]['label'];
  312. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentsubscriptionstatic->getNomUrl(2);
  313. $tabpay[$obj->rowid]["paymentsubscriptionid"] = $paymentsubscriptionstatic->id;
  314. $paymentsubscriptionstatic->fetch($paymentsubscriptionstatic->id);
  315. $tabtp[$obj->rowid][$account_pay_subscription] += $obj->amount;
  316. } elseif ($links[$key]['type'] == 'payment_vat') { // Payment VAT
  317. $paymentvatstatic->id = $links[$key]['url_id'];
  318. $paymentvatstatic->ref = $links[$key]['url_id'];
  319. $paymentvatstatic->label = $links[$key]['label'];
  320. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentvatstatic->getNomUrl(2);
  321. $tabpay[$obj->rowid]["paymentvatid"] = $paymentvatstatic->id;
  322. $tabtp[$obj->rowid][$account_pay_vat] += $obj->amount;
  323. } elseif ($links[$key]['type'] == 'payment_salary') {
  324. $paymentsalstatic->id = $links[$key]['url_id'];
  325. $paymentsalstatic->ref = $links[$key]['url_id'];
  326. $paymentsalstatic->label = $links[$key]['label'];
  327. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentsalstatic->getNomUrl(2);
  328. $tabpay[$obj->rowid]["paymentsalid"] = $paymentsalstatic->id;
  329. } elseif ($links[$key]['type'] == 'payment_expensereport') {
  330. $paymentexpensereportstatic->id = $links[$key]['url_id'];
  331. $tabpay[$obj->rowid]["lib"] .= $paymentexpensereportstatic->getNomUrl(2);
  332. $tabpay[$obj->rowid]["paymentexpensereport"] = $paymentexpensereportstatic->id;
  333. } elseif ($links[$key]['type'] == 'payment_various') {
  334. $paymentvariousstatic->id = $links[$key]['url_id'];
  335. $paymentvariousstatic->ref = $links[$key]['url_id'];
  336. $paymentvariousstatic->label = $links[$key]['label'];
  337. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentvariousstatic->getNomUrl(2);
  338. $tabpay[$obj->rowid]["paymentvariousid"] = $paymentvariousstatic->id;
  339. $paymentvariousstatic->fetch($paymentvariousstatic->id);
  340. $account_various = (! empty($paymentvariousstatic->accountancy_code) ? $paymentvariousstatic->accountancy_code : 'NotDefined'); // NotDefined is a reserved word
  341. $tabtp[$obj->rowid][$account_various] += $obj->amount;
  342. } elseif ($links[$key]['type'] == 'payment_loan') {
  343. $paymentloanstatic->id = $links[$key]['url_id'];
  344. $paymentloanstatic->ref = $links[$key]['url_id'];
  345. $paymentloanstatic->fk_loan = $links[$key]['url_id'];
  346. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentloanstatic->getNomUrl(2);
  347. $tabpay[$obj->rowid]["paymentloanid"] = $paymentloanstatic->id;
  348. //$tabtp[$obj->rowid][$account_pay_loan] += $obj->amount;
  349. $sqlmid = 'SELECT pl.amount_capital, pl.amount_insurance, pl.amount_interest, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest';
  350. $sqlmid.= ' FROM '.MAIN_DB_PREFIX.'payment_loan as pl, '.MAIN_DB_PREFIX.'loan as l';
  351. $sqlmid.= ' WHERE l.rowid = pl.fk_loan AND pl.fk_bank = '.$obj->rowid;
  352. dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
  353. $resultmid = $db->query($sqlmid);
  354. if ($resultmid) {
  355. $objmid = $db->fetch_object($resultmid);
  356. $tabtp[$obj->rowid][$objmid->accountancy_account_capital] -= $objmid->amount_capital;
  357. $tabtp[$obj->rowid][$objmid->accountancy_account_insurance] -= $objmid->amount_insurance;
  358. $tabtp[$obj->rowid][$objmid->accountancy_account_interest] -= $objmid->amount_interest;
  359. }
  360. } elseif ($links[$key]['type'] == 'banktransfert') {
  361. $accountLinestatic->fetch($links[$key]['url_id']);
  362. $tabpay[$obj->rowid]["lib"] .= ' '.$langs->trans("BankTransfer").'- ' .$accountLinestatic ->getNomUrl(1);
  363. $tabtp[$obj->rowid][$account_transfer] += $obj->amount;
  364. $bankaccountstatic->fetch($tabpay[$obj->rowid]['fk_bank_account']);
  365. $tabpay[$obj->rowid]["soclib"] = $bankaccountstatic->getNomUrl(2);
  366. }
  367. }
  368. }
  369. $tabbq[$obj->rowid][$compta_bank] += $obj->amount;
  370. // If not links were found to know amount on thirdparty, we init it.
  371. if (empty($tabtp[$obj->rowid])) $tabtp[$obj->rowid]['NotDefined']= $tabbq[$obj->rowid][$compta_bank];
  372. // Check account number is ok
  373. /*if ($action == 'writebookkeeping') // Make test now in such a case
  374. {
  375. reset($tabbq[$obj->rowid]);
  376. $first_key_tabbq = key($tabbq[$obj->rowid]);
  377. if (empty($first_key_tabbq))
  378. {
  379. $error++;
  380. setEventMessages($langs->trans('ErrorAccountancyCodeOnBankAccountNotDefined', $obj->baref), null, 'errors');
  381. }
  382. reset($tabtp[$obj->rowid]);
  383. $first_key_tabtp = key($tabtp[$obj->rowid]);
  384. if (empty($first_key_tabtp))
  385. {
  386. $error++;
  387. setEventMessages($langs->trans('ErrorAccountancyCodeOnThirdPartyNotDefined'), null, 'errors');
  388. }
  389. }*/
  390. // if($obj->socid)$tabtp[$obj->rowid][$compta_soc] += $obj->amount;
  391. $i++;
  392. }
  393. } else {
  394. dol_print_error($db);
  395. }
  396. /*
  397. var_dump($tabpay);
  398. var_dump($tabbq);
  399. var_dump($tabtp);
  400. */
  401. // Write bookkeeping
  402. if (! $error && $action == 'writebookkeeping') {
  403. $now = dol_now();
  404. $error = 0;
  405. foreach ( $tabpay as $key => $val ) // $key is rowid into llx_bank
  406. {
  407. $date = dol_print_date($db->jdate($val["date"]), 'day');
  408. $ref = getSourceDocRef($val, $tabtype[$key]);
  409. $errorforline = 0;
  410. $totalcredit = 0;
  411. $totaldebit = 0;
  412. $db->begin();
  413. // Introduce a protection. Total of tabtp must be total of tabbq
  414. /*var_dump($tabpay);
  415. var_dump($tabtp);
  416. var_dump($tabbq);exit;*/
  417. // Bank
  418. if (! $errorforline && is_array($tabbq[$key]))
  419. {
  420. // Line into bank account
  421. foreach ( $tabbq[$key] as $k => $mt )
  422. {
  423. if ($mt)
  424. {
  425. $reflabel = '';
  426. if (! empty($val['lib'])) $reflabel .= dol_string_nohtmltag($val['lib']) . " - ";
  427. $reflabel.= $langs->trans("Bank").' '.dol_string_nohtmltag($val['bank_account_ref']);
  428. if (! empty($val['soclib'])) $reflabel .= " - " . dol_string_nohtmltag($val['soclib']);
  429. $bookkeeping = new BookKeeping($db);
  430. $bookkeeping->doc_date = $val["date"];
  431. $bookkeeping->doc_ref = $ref;
  432. $bookkeeping->doc_type = 'bank';
  433. $bookkeeping->fk_doc = $key;
  434. $bookkeeping->fk_docdet = $val["fk_bank"];
  435. $bookkeeping->numero_compte = $k;
  436. $accountingaccount->fetch(null, $k, true);
  437. $bookkeeping->label_compte = $accountingaccount->label;
  438. $bookkeeping->label_operation = $reflabel;
  439. $bookkeeping->montant = $mt;
  440. $bookkeeping->sens = ($mt >= 0) ? 'D' : 'C';
  441. $bookkeeping->debit = ($mt >= 0 ? $mt : 0);
  442. $bookkeeping->credit = ($mt < 0 ? - $mt : 0);
  443. $bookkeeping->code_journal = $journal;
  444. $bookkeeping->journal_label = $journal_label;
  445. $bookkeeping->fk_user_author = $user->id;
  446. $bookkeeping->date_create = $now;
  447. // No subledger_account value for the bank line but add a specific label_operation
  448. $bookkeeping->subledger_account = '';
  449. $bookkeeping->label_operation = $reflabel;
  450. $bookkeeping->entity = $conf->entity;
  451. $totaldebit += $bookkeeping->debit;
  452. $totalcredit += $bookkeeping->credit;
  453. $result = $bookkeeping->create($user);
  454. if ($result < 0) {
  455. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  456. {
  457. $error++;
  458. $errorforline++;
  459. setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  460. }
  461. else
  462. {
  463. $error++;
  464. $errorforline++;
  465. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  466. }
  467. }
  468. }
  469. }
  470. }
  471. // Third party
  472. if (! $errorforline)
  473. {
  474. if (is_array($tabtp[$key]))
  475. {
  476. // Line into thirdparty account
  477. foreach ( $tabtp[$key] as $k => $mt ) {
  478. if ($mt)
  479. {
  480. $reflabel = '';
  481. if (! empty($val['lib'])) $reflabel .= dol_string_nohtmltag($val['lib']) . ($val['soclib']?" - ":"");
  482. $reflabel.= dol_string_nohtmltag($val['soclib']);
  483. $bookkeeping = new BookKeeping($db);
  484. $bookkeeping->doc_date = $val["date"];
  485. $bookkeeping->doc_ref = $ref;
  486. $bookkeeping->doc_type = 'bank';
  487. $bookkeeping->fk_doc = $key;
  488. $bookkeeping->fk_docdet = $val["fk_bank"];
  489. $bookkeeping->label_operation = $reflabel;
  490. $bookkeeping->montant = $mt;
  491. $bookkeeping->sens = ($mt < 0) ? 'D' : 'C';
  492. $bookkeeping->debit = ($mt < 0 ? - $mt : 0);
  493. $bookkeeping->credit = ($mt >= 0) ? $mt : 0;
  494. $bookkeeping->code_journal = $journal;
  495. $bookkeeping->journal_label = $journal_label;
  496. $bookkeeping->fk_user_author = $user->id;
  497. $bookkeeping->date_create = $now;
  498. if ($tabtype[$key] == 'payment') { // If payment is payment of customer invoice, we get ref of invoice
  499. $bookkeeping->subledger_account = $tabcompany[$key]['code_compta'];
  500. $bookkeeping->subledger_label = $tabcompany[$key]['name'];
  501. $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER;
  502. $accountingaccount->fetch(null, $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER, true);
  503. $bookkeeping->label_compte = $accountingaccount->label;
  504. } elseif ($tabtype[$key] == 'payment_supplier') { // If payment is payment of supplier invoice, we get ref of invoice
  505. $bookkeeping->subledger_account = $tabcompany[$key]['code_compta'];
  506. $bookkeeping->subledger_label = $tabcompany[$key]['name'];
  507. $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER;
  508. $accountingaccount->fetch(null, $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER, true);
  509. $bookkeeping->label_compte = $accountingaccount->label;
  510. } elseif ($tabtype[$key] == 'payment_expensereport') {
  511. $bookkeeping->subledger_account = $tabuser[$key]['accountancy_code'];
  512. $bookkeeping->subledger_label = $tabuser[$key]['name'];
  513. $bookkeeping->numero_compte = $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT;
  514. $accountingaccount->fetch(null, $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT, true);
  515. $bookkeeping->label_compte = $accountingaccount->label;
  516. } elseif ($tabtype[$key] == 'payment_salary') {
  517. $bookkeeping->subledger_account = $tabuser[$key]['accountancy_code'];
  518. $bookkeeping->subledger_label = $tabuser[$key]['name'];
  519. $bookkeeping->numero_compte = $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT;
  520. $accountingaccount->fetch(null, $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT, true);
  521. $bookkeeping->label_compte = $accountingaccount->label;
  522. } elseif (in_array($tabtype[$key], array('sc', 'payment_sc'))) { // If payment is payment of social contribution
  523. $bookkeeping->subledger_account = '';
  524. $bookkeeping->subledger_label = '';
  525. $bookkeeping->numero_compte = $k;
  526. $bookkeeping->label_compte = $objmid->labelc;
  527. } elseif ($tabtype[$key] == 'payment_vat') {
  528. $bookkeeping->subledger_account = '';
  529. $bookkeeping->subledger_label = '';
  530. $bookkeeping->numero_compte = $k;
  531. $accountingaccount->fetch(null, $k, true);
  532. $bookkeeping->label_compte = $accountingaccount->label;
  533. } elseif ($tabtype[$key] == 'payment_donation') {
  534. $bookkeeping->subledger_account = '';
  535. $bookkeeping->subledger_label = '';
  536. $bookkeeping->numero_compte = $k;
  537. $accountingaccount->fetch(null, $k, true);
  538. $bookkeeping->label_compte = $accountingaccount->label;
  539. } elseif ($tabtype[$key] == 'member') {
  540. $bookkeeping->subledger_account = '';
  541. $bookkeeping->subledger_label = '';
  542. $bookkeeping->numero_compte = $k;
  543. $accountingaccount->fetch(null, $k, true);
  544. $bookkeeping->label_compte = $accountingaccount->label;
  545. } elseif ($tabtype[$key] == 'payment_loan') {
  546. $bookkeeping->subledger_account = '';
  547. $bookkeeping->subledger_label = '';
  548. $bookkeeping->numero_compte = $k;
  549. $accountingaccount->fetch(null, $k, true);
  550. $bookkeeping->label_compte = $accountingaccount->label;
  551. } elseif ($tabtype[$key] == 'payment_various') {
  552. $bookkeeping->subledger_account = '';
  553. $bookkeeping->subledger_label = '';
  554. $bookkeeping->numero_compte = $k;
  555. $accountingaccount->fetch(null, $k, true);
  556. $bookkeeping->label_compte = $accountingaccount->label;
  557. } elseif ($tabtype[$key] == 'banktransfert') {
  558. $bookkeeping->subledger_account = '';
  559. $bookkeeping->subledger_label = '';
  560. $bookkeeping->numero_compte = $k;
  561. $accountingaccount->fetch(null, $k, true);
  562. $bookkeeping->label_compte = $accountingaccount->label;
  563. } else {
  564. if ($tabtype[$key] == 'unknown') // Unknown transaction, we will use a waiting account for thirdparty.
  565. {
  566. // Temporary account
  567. $bookkeeping->subledger_account = '';
  568. $bookkeeping->subledger_label = '';
  569. $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_SUSPENSE;
  570. $accountingaccount->fetch(null, $conf->global->ACCOUNTING_ACCOUNT_SUSPENSE, true);
  571. $bookkeeping->label_compte = $accountingaccount->label;
  572. }
  573. }
  574. $bookkeeping->label_operation = $reflabel;
  575. $bookkeeping->entity = $conf->entity;
  576. $totaldebit += $bookkeeping->debit;
  577. $totalcredit += $bookkeeping->credit;
  578. $result = $bookkeeping->create($user);
  579. if ($result < 0) {
  580. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  581. {
  582. $error++;
  583. $errorforline++;
  584. setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  585. }
  586. else
  587. {
  588. $error++;
  589. $errorforline++;
  590. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  591. }
  592. }
  593. }
  594. }
  595. }
  596. else { // If thirdparty unkown, output the waiting account
  597. foreach ( $tabbq[$key] as $k => $mt ) {
  598. if ($mt)
  599. {
  600. $reflabel = '';
  601. if (! empty($val['lib'])) $reflabel .= dol_string_nohtmltag($val['lib']) . " - ";
  602. $reflabel.= dol_string_nohtmltag('WaitingAccount');
  603. $bookkeeping = new BookKeeping($db);
  604. $bookkeeping->doc_date = $val["date"];
  605. $bookkeeping->doc_ref = $ref;
  606. $bookkeeping->doc_type = 'bank';
  607. $bookkeeping->fk_doc = $key;
  608. $bookkeeping->fk_docdet = $val["fk_bank"];
  609. $bookkeeping->montant = $mt;
  610. $bookkeeping->sens = ($mt < 0) ? 'D' : 'C';
  611. $bookkeeping->debit = ($mt < 0 ? - $mt : 0);
  612. $bookkeeping->credit = ($mt >= 0) ? $mt : 0;
  613. $bookkeeping->code_journal = $journal;
  614. $bookkeeping->journal_label = $journal_label;
  615. $bookkeeping->fk_user_author = $user->id;
  616. $bookkeeping->date_create = $now;
  617. $bookkeeping->label_compte = '';
  618. $bookkeeping->label_operation = $reflabel;
  619. $bookkeeping->entity = $conf->entity;
  620. $totaldebit += $bookkeeping->debit;
  621. $totalcredit += $bookkeeping->credit;
  622. $result = $bookkeeping->create($user);
  623. if ($result < 0) {
  624. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  625. {
  626. $error++;
  627. $errorforline++;
  628. setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  629. }
  630. else
  631. {
  632. $error++;
  633. $errorforline++;
  634. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  635. }
  636. }
  637. }
  638. }
  639. }
  640. }
  641. if (price2num($totaldebit) != price2num($totalcredit))
  642. {
  643. $error++;
  644. $errorforline++;
  645. setEventMessages('Try to insert a non balanced transaction in book for '.$ref.'. Canceled. Surely a bug.', null, 'errors');
  646. }
  647. if (! $errorforline)
  648. {
  649. $db->commit();
  650. }
  651. else
  652. {
  653. //print 'KO for line '.$key.' '.$error.'<br>';
  654. $db->rollback();
  655. $MAXNBERRORS=5;
  656. if ($error >= $MAXNBERRORS)
  657. {
  658. setEventMessages($langs->trans("ErrorTooManyErrorsProcessStopped").' (>'.$MAXNBERRORS.')', null, 'errors');
  659. break; // Break in the foreach
  660. }
  661. }
  662. }
  663. if (empty($error) && count($tabpay) > 0) {
  664. setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
  665. }
  666. elseif (count($tabpay) == $error)
  667. {
  668. setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
  669. }
  670. else
  671. {
  672. setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
  673. }
  674. $action = '';
  675. // Must reload data, so we make a redirect
  676. if (count($tabpay) != $error)
  677. {
  678. $param='id_journal='.$id_journal;
  679. $param.='&date_startday='.$date_startday;
  680. $param.='&date_startmonth='.$date_startmonth;
  681. $param.='&date_startyear='.$date_startyear;
  682. $param.='&date_endday='.$date_endday;
  683. $param.='&date_endmonth='.$date_endmonth;
  684. $param.='&date_endyear='.$date_endyear;
  685. $param.='&in_bookkeeping='.$in_bookkeeping;
  686. header("Location: ".$_SERVER['PHP_SELF'].($param?'?'.$param:''));
  687. exit;
  688. }
  689. }
  690. // Export
  691. if ($action == 'exportcsv') { // ISO and not UTF8 !
  692. $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  693. $filename = 'journal';
  694. $type_export = 'journal';
  695. include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
  696. // CSV header line
  697. print '"' . $langs->transnoentitiesnoconv("BankId").'"' . $sep;
  698. print '"' . $langs->transnoentitiesnoconv("Date") . '"' . $sep;
  699. print '"' . $langs->transnoentitiesnoconv("PaymentMode") . '"' . $sep;
  700. print '"' . $langs->transnoentitiesnoconv("AccountAccounting") . '"' . $sep;
  701. print '"' . $langs->transnoentitiesnoconv("LedgerAccount") . '"' . $sep;
  702. print '"' . $langs->transnoentitiesnoconv("SubledgerAccount") . '"' . $sep;
  703. print '"' . $langs->transnoentitiesnoconv("Label"). '"' . $sep;
  704. print '"' . $langs->transnoentitiesnoconv("Debit") . '"' . $sep;
  705. print '"' . $langs->transnoentitiesnoconv("Credit") . '"' . $sep;
  706. print '"' . $langs->transnoentitiesnoconv("Journal") . '"' . $sep;
  707. print '"' . $langs->transnoentitiesnoconv("Note") . '"' . $sep;
  708. print "\n";
  709. foreach ( $tabpay as $key => $val )
  710. {
  711. $date = dol_print_date($db->jdate($val["date"]), 'day');
  712. $ref = getSourceDocRef($val, $tabtype[$key]);
  713. // Bank
  714. foreach ( $tabbq[$key] as $k => $mt ) {
  715. if ($mt)
  716. {
  717. $reflabel = '';
  718. if (! empty($val['lib'])) $reflabel .= dol_string_nohtmltag($val['lib']) . " - ";
  719. $reflabel.= $langs->trans("Bank").' '.dol_string_nohtmltag($val['bank_account_ref']);
  720. if (! empty($val['soclib'])) $reflabel .= " - " . dol_string_nohtmltag($val['soclib']);
  721. print '"' . $key . '"' . $sep;
  722. print '"' . $date . '"' . $sep;
  723. print '"' . $val["type_payment"] . '"' . $sep;
  724. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  725. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  726. print " " . $sep;
  727. print '"' . $reflabel . '"' . $sep;
  728. print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep;
  729. print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
  730. print '"' . $journal . '"' . $sep;
  731. print '"' . dol_string_nohtmltag($ref) . '"' . $sep;
  732. print "\n";
  733. }
  734. }
  735. // Third party
  736. if (is_array($tabtp[$key])) {
  737. foreach ( $tabtp[$key] as $k => $mt ) {
  738. if ($mt)
  739. {
  740. $reflabel = '';
  741. if (! empty($val['lib'])) $reflabel .= dol_string_nohtmltag($val['lib']) . ($val['soclib']?" - ":"");
  742. $reflabel.= dol_string_nohtmltag($val['soclib']);
  743. print '"' . $key . '"' . $sep;
  744. print '"' . $date . '"' . $sep;
  745. print '"' . $val["type_payment"] . '"' . $sep;
  746. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  747. if ($tabtype[$key] == 'payment_supplier') {
  748. print '"' . $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER . '"' . $sep;
  749. } elseif($tabtype[$key] == 'payment') {
  750. print '"' . $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER . '"' . $sep;
  751. } elseif($tabtype[$key] == 'payment_expensereport') {
  752. print '"' . $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT . '"' . $sep;
  753. } elseif($tabtype[$key] == 'payment_salary') {
  754. print '"' . $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT . '"' . $sep;
  755. } else {
  756. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  757. }
  758. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  759. print '"' . $reflabel . '"' . $sep;
  760. print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
  761. print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep;
  762. print '"' . $journal . '"' . $sep;
  763. print '"' . dol_string_nohtmltag($ref) . '"' . $sep;
  764. print "\n";
  765. }
  766. }
  767. } else { // If thirdparty unkown, output the waiting account
  768. foreach ( $tabbq[$key] as $k => $mt ) {
  769. if ($mt)
  770. {
  771. $reflabel = '';
  772. if (! empty($val['lib'])) $reflabel .= dol_string_nohtmltag($val['lib']) . " - ";
  773. $reflabel.= dol_string_nohtmltag('WaitingAccount');
  774. print '"' . $key . '"' . $sep;
  775. print '"' . $date . '"' . $sep;
  776. print '"' . $val["type_payment"] . '"' . $sep;
  777. print '"' . length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE) . '"' . $sep;
  778. print '"' . length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE) . '"' . $sep;
  779. print "" . $sep;
  780. print '"' . $reflabel . '"' . $sep;
  781. print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
  782. print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep;
  783. print '"' . $journal . '"' . $sep;
  784. print '"' . dol_string_nohtmltag($ref) . '"' . $sep;
  785. print "\n";
  786. }
  787. }
  788. }
  789. }
  790. }
  791. /*
  792. * View
  793. */
  794. $form = new Form($db);
  795. if (empty($action) || $action == 'view') {
  796. $invoicestatic = new Facture($db);
  797. $invoicesupplierstatic = new FactureFournisseur($db);
  798. $expensereportstatic = new ExpenseReport($db);
  799. $vatstatic = new Tva($db);
  800. $donationstatic = new Don($db);
  801. $loanstatic = new Loan($db);
  802. $salarystatic = new PaymentSalary($db);
  803. $variousstatic = new PaymentVarious($db);
  804. llxHeader('', $langs->trans("FinanceJournal"));
  805. $nom = $langs->trans("FinanceJournal") . ' | ' . $accountingjournalstatic->getNomUrl(0,1,1,'',1);
  806. $builddate=dol_now();
  807. //$description = $langs->trans("DescFinanceJournal") . '<br>';
  808. $description.= $langs->trans("DescJournalOnlyBindedVisible").'<br>';
  809. $listofchoices=array('notyet'=>$langs->trans("NotYetInGeneralLedger"), 'already'=>$langs->trans("AlreadyInGeneralLedger"));
  810. $period = $form->selectDate($date_start?$date_start:-1, 'date_start', 0, 0, 0, '', 1, 0) . ' - ' . $form->selectDate($date_end?$date_end:-1, 'date_end', 0, 0, 0, '', 1, 0);
  811. $period .= ' - ' .$langs->trans("JournalizationInLedgerStatus").' '. $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
  812. $varlink = 'id_journal=' . $id_journal;
  813. journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
  814. // Test that setup is complete
  815. $sql = 'SELECT COUNT(rowid) as nb FROM '.MAIN_DB_PREFIX.'bank_account WHERE fk_accountancy_journal IS NULL AND clos=0';
  816. $resql = $db->query($sql);
  817. if ($resql)
  818. {
  819. $obj = $db->fetch_object($resql);
  820. if ($obj->nb > 0)
  821. {
  822. print '<br>'.img_warning().' '.$langs->trans("TheJournalCodeIsNotDefinedOnSomeBankAccount");
  823. print ' : '.$langs->trans("AccountancyAreaDescBank", 9, '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("BankAccounts").'</strong>');
  824. }
  825. }
  826. else dol_print_error($db);
  827. // Button to write into Ledger
  828. if (empty($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER) || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1'
  829. || empty($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER) || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1'
  830. || empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1') {
  831. print '<br>'.img_warning().' '.$langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
  832. print ' : '.$langs->trans("AccountancyAreaDescMisc", 4, '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("MenuDefaultAccounts").'</strong>');
  833. }
  834. print '<div class="tabsAction tabsActionNoBottom">';
  835. if (! empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL)) print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />';
  836. if (empty($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER) || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1'
  837. || empty($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER) || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1') {
  838. print '<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")).'" value="' . $langs->trans("WriteBookKeeping") . '" />';
  839. }
  840. else {
  841. if ($in_bookkeeping == 'notyet') print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
  842. else print '<a class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs->trans("WriteBookKeeping") . '</a>';
  843. }
  844. print '</div>';
  845. // TODO Avoid using js. We can use a direct link with $param
  846. print '
  847. <script type="text/javascript">
  848. function launch_export() {
  849. console.log("Set value into form and submit");
  850. $("div.fiche form input[name=\"action\"]").val("exportcsv");
  851. $("div.fiche form input[type=\"submit\"]").click();
  852. $("div.fiche form input[name=\"action\"]").val("");
  853. }
  854. function writebookkeeping() {
  855. console.log("Set value into form and submit");
  856. $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
  857. $("div.fiche form input[type=\"submit\"]").click();
  858. $("div.fiche form input[name=\"action\"]").val("");
  859. }
  860. </script>';
  861. /*
  862. * Show result array
  863. */
  864. print '<br>';
  865. $i = 0;
  866. print '<div class="div-table-responsive">';
  867. print "<table class=\"noborder\" width=\"100%\">";
  868. print "<tr class=\"liste_titre\">";
  869. print "<td></td>";
  870. print "<td>" . $langs->trans("Date") . "</td>";
  871. print "<td>" . $langs->trans("Piece") . ' (' . $langs->trans("ObjectsRef") . ")</td>";
  872. print "<td>" . $langs->trans("AccountAccounting") . "</td>";
  873. print "<td>" . $langs->trans("SubledgerAccount") . "</td>";
  874. print "<td>" . $langs->trans("LabelOperation") . "</td>";
  875. print "<td>" . $langs->trans("PaymentMode") . "</td>";
  876. print "<td align='right'>" . $langs->trans("Debit") . "</td>";
  877. print "<td align='right'>" . $langs->trans("Credit") . "</td>";
  878. print "</tr>\n";
  879. $r = '';
  880. foreach ( $tabpay as $key => $val ) // $key is rowid in llx_bank
  881. {
  882. $date = dol_print_date($db->jdate($val["date"]), 'day');
  883. $ref = getSourceDocRef($val, $tabtype[$key]);
  884. // Bank
  885. foreach ( $tabbq[$key] as $k => $mt )
  886. {
  887. if ($mt)
  888. {
  889. $reflabel = '';
  890. if (! empty($val['lib'])) $reflabel .= $val['lib'] . " - ";
  891. $reflabel.= $langs->trans("Bank").' '.$val['bank_account_ref'];
  892. if (! empty($val['soclib'])) $reflabel .= " - " . $val['soclib'];
  893. //var_dump($tabpay[$key]);
  894. print '<!-- Bank bank.rowid='.$key.' type='.$tabpay[$key]['type'].' ref='.$tabpay[$key]['ref'].'-->';
  895. print '<tr class="oddeven">';
  896. print "<td></td>";
  897. print "<td>" . $date . "</td>";
  898. print "<td>" . $ref . "</td>";
  899. // Ledger account
  900. print "<td>";
  901. $accounttoshow = length_accountg($k);
  902. if (empty($accounttoshow) || $accounttoshow == 'NotDefined')
  903. {
  904. print '<span class="error">'.$langs->trans("BankAccountNotDefined").'</span>';
  905. }
  906. else print $accounttoshow;
  907. print "</td>";
  908. // Subledger account
  909. print "<td>";
  910. /*$accounttoshow = length_accountg($k);
  911. if (empty($accounttoshow) || $accounttoshow == 'NotDefined')
  912. {
  913. print '<span class="error">'.$langs->trans("BankAccountNotDefined").'</span>';
  914. }
  915. else print $accounttoshow;*/
  916. print "</td>";
  917. print "<td>";
  918. print $reflabel;
  919. print "</td>";
  920. print "<td>" . $val["type_payment"] . "</td>";
  921. print "<td align='right'>" . ($mt >= 0 ? price($mt) : '') . "</td>";
  922. print "<td align='right'>" . ($mt < 0 ? price(- $mt) : '') . "</td>";
  923. print "</tr>";
  924. }
  925. }
  926. // Third party
  927. if (is_array($tabtp[$key])) {
  928. foreach ( $tabtp[$key] as $k => $mt ) {
  929. if ($mt)
  930. {
  931. $reflabel = '';
  932. if (! empty($val['lib'])) $reflabel .= $val['lib'] . ($val['soclib']?" - ":"");
  933. $reflabel.= $val['soclib'];
  934. print '<!-- Thirdparty bank.rowid='.$key.' -->';
  935. print '<tr class="oddeven">';
  936. print "<td></td>";
  937. print "<td>" . $date . "</td>";
  938. print "<td>" . $ref . "</td>";
  939. // Ledger account
  940. print "<td>";
  941. $account_ledger = $k;
  942. // Try to force general ledger account depending on type
  943. if ($tabtype[$key] == 'payment') $account_ledger = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER;
  944. if ($tabtype[$key] == 'payment_supplier') $account_ledger = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER;
  945. if ($tabtype[$key] == 'payment_expensereport') $account_ledger = $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT;
  946. if ($tabtype[$key] == 'payment_salary') $account_ledger = $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT;
  947. if ($tabtype[$key] == 'payment_vat') $account_ledger = $conf->global->ACCOUNTING_VAT_PAY_ACCOUNT;
  948. if ($tabtype[$key] == 'member') $account_ledger = $conf->global->ADHERENT_SUBSCRIPTION_ACCOUNTINGACCOUNT;
  949. $accounttoshow = length_accounta($account_ledger);
  950. if (empty($accounttoshow) || $accounttoshow == 'NotDefined')
  951. {
  952. if ($tabtype[$key] == 'unknown')
  953. {
  954. // We will accept writing, but into a waiting account
  955. if (empty($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE) || $conf->global->ACCOUNTING_ACCOUNT_SUSPENSE == '-1')
  956. {
  957. print '<span class="error">'.$langs->trans('UnknownAccountForThirdpartyAndWaitingAccountNotDefinedBlocking').'</span>';
  958. }
  959. else
  960. {
  961. print '<span class="warning">'.$langs->trans('UnknownAccountForThirdparty', length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE)).'</span>'; // We will a waiting account
  962. }
  963. }
  964. else
  965. {
  966. // We will refuse writing
  967. $errorstring='UnknownAccountForThirdpartyBlocking';
  968. if ($tabtype[$key] == 'payment') $errorstring='MainAccountForCustomersNotDefined';
  969. if ($tabtype[$key] == 'payment_supplier') $errorstring='MainAccountForSuppliersNotDefined';
  970. if ($tabtype[$key] == 'payment_expensereport') $errorstring='MainAccountForUsersNotDefined';
  971. if ($tabtype[$key] == 'payment_salary') $errorstring='MainAccountForUsersNotDefined';
  972. if ($tabtype[$key] == 'payment_vat') $errorstring='MainAccountForVatPaymentNotDefined';
  973. if ($tabtype[$key] == 'member') $errorstring='MainAccountForSubscriptionPaymentNotDefined';
  974. print '<span class="error">'.$langs->trans($errorstring).'</span>';
  975. }
  976. }
  977. else print $accounttoshow;
  978. print "</td>";
  979. // Subledger account
  980. print "<td>";
  981. if (in_array($tabtype[$key], array('payment', 'payment_supplier', 'payment_expensereport', 'payment_salary'))) // Type of payment with subledger
  982. {
  983. $accounttoshowsubledger = length_accounta($k);
  984. if ($accounttoshow != $accounttoshowsubledger)
  985. {
  986. if (empty($accounttoshowsubledger) || $accounttoshowsubledger == 'NotDefined')
  987. {
  988. /*var_dump($tabpay[$key]);
  989. var_dump($tabtype[$key]);
  990. var_dump($tabbq[$key]);*/
  991. //print '<span class="error">'.$langs->trans("ThirdpartyAccountNotDefined").'</span>';
  992. print '<span class="error">'.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknown").'</span>';
  993. }
  994. else print $accounttoshowsubledger;
  995. }
  996. }
  997. print "</td>";
  998. print "<td>" . $reflabel . "</td>";
  999. print "<td>" . $val["type_payment"] . "</td>";
  1000. print "<td align='right'>" . ($mt < 0 ? price(- $mt) : '') . "</td>";
  1001. print "<td align='right'>" . ($mt >= 0 ? price($mt) : '') . "</td>";
  1002. print "</tr>";
  1003. }
  1004. }
  1005. } else { // Waiting account
  1006. foreach ( $tabbq[$key] as $k => $mt ) {
  1007. if ($mt)
  1008. {
  1009. $reflabel = '';
  1010. if (! empty($val['lib'])) $reflabel .= $val['lib'] . " - ";
  1011. $reflabel.= 'WaitingAccount';
  1012. print '<!-- Wait bank.rowid='.$key.' -->';
  1013. print '<tr class="oddeven">';
  1014. print "<td></td>";
  1015. print "<td>" . $date . "</td>";
  1016. print "<td>" . $ref . "</td>";
  1017. // Ledger account
  1018. print "<td>";
  1019. /*if (empty($accounttoshow) || $accounttoshow == 'NotDefined')
  1020. {
  1021. print '<span class="error">'.$langs->trans("WaitAccountNotDefined").'</span>';
  1022. }
  1023. else */ print length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE);
  1024. print "</td>";
  1025. // Subledger account
  1026. print "<td>";
  1027. /*if (empty($accounttoshowsubledger) || $accounttoshowsubledger == 'NotDefined')
  1028. {
  1029. print '<span class="error">'.$langs->trans("WaitAccountNotDefined").'</span>';
  1030. }
  1031. else print length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE);
  1032. */
  1033. print "</td>";
  1034. print "<td>" . $reflabel . "</td>";
  1035. print "<td>" . $val["type_payment"] . "</td>";
  1036. print "<td align='right'>" . ($mt < 0 ? price(- $mt) : '') . "</td>";
  1037. print "<td align='right'>" . ($mt >= 0 ? price($mt) : '') . "</td>";
  1038. print "</tr>";
  1039. }
  1040. }
  1041. }
  1042. }
  1043. print "</table>";
  1044. print '</div>';
  1045. llxFooter();
  1046. }
  1047. $db->close();
  1048. /**
  1049. * Return source for doc_ref of a bank transaction
  1050. *
  1051. * @param string $val Array of val
  1052. * @param string $typerecord Type of record ('payment', 'payment_supplier', 'payment_expensereport', 'payment_vat', ...)
  1053. * @return string A string label to describe a record into llx_bank_url
  1054. */
  1055. function getSourceDocRef($val, $typerecord)
  1056. {
  1057. global $db, $langs;
  1058. // Defined the docref into $ref (We start with $val['ref'] by default and we complete according to other data)
  1059. // WE MUST HAVE SAME REF FOR ALL LINES WE WILL RECORD INTO THE BOOKKEEPING
  1060. $ref = $val['ref'];
  1061. if ($ref == '(SupplierInvoicePayment)' || $ref == '(SupplierInvoicePaymentBack)') {
  1062. $ref = $langs->transnoentitiesnoconv('Supplier');
  1063. }
  1064. if ($ref == '(CustomerInvoicePayment)' || $ref == '(CustomerInvoicePaymentBack)') {
  1065. $ref = $langs->transnoentitiesnoconv('Customer');
  1066. }
  1067. if ($ref == '(SocialContributionPayment)') {
  1068. $ref = $langs->transnoentitiesnoconv('SocialContribution');
  1069. }
  1070. if ($ref == '(DonationPayment)') {
  1071. $ref = $langs->transnoentitiesnoconv('Donation');
  1072. }
  1073. if ($ref == '(SubscriptionPayment)') {
  1074. $ref = $langs->transnoentitiesnoconv('Subscription');
  1075. }
  1076. if ($ref == '(ExpenseReportPayment)') {
  1077. $ref = $langs->transnoentitiesnoconv('Employee');
  1078. }
  1079. if ($ref == '(LoanPayment)') {
  1080. $ref = $langs->transnoentitiesnoconv('Loan');
  1081. }
  1082. if ($ref == '(payment_salary)') {
  1083. $ref = $langs->transnoentitiesnoconv('Employee');
  1084. }
  1085. $sqlmid = '';
  1086. if ($typerecord == 'payment')
  1087. {
  1088. $sqlmid = 'SELECT payfac.fk_facture as id, f.ref as ref';
  1089. $sqlmid .= " FROM ".MAIN_DB_PREFIX."paiement_facture as payfac, ".MAIN_DB_PREFIX."facture as f";
  1090. $sqlmid .= " WHERE payfac.fk_facture = f.rowid AND payfac.fk_paiement=" . $val["paymentid"];
  1091. $ref = $langs->transnoentitiesnoconv("Invoice");
  1092. }
  1093. elseif ($typerecord == 'payment_supplier')
  1094. {
  1095. $sqlmid = 'SELECT payfac.fk_facturefourn as id, f.ref';
  1096. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfac, ".MAIN_DB_PREFIX."facture_fourn as f";
  1097. $sqlmid .= " WHERE payfac.fk_facturefourn = f.rowid AND payfac.fk_paiementfourn=" . $val["paymentsupplierid"];
  1098. $ref = $langs->transnoentitiesnoconv("SupplierInvoice");
  1099. }
  1100. elseif ($typerecord == 'payment_expensereport')
  1101. {
  1102. $sqlmid = 'SELECT e.rowid as id, e.ref';
  1103. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "payment_expensereport as pe, " . MAIN_DB_PREFIX . "expensereport as e";
  1104. $sqlmid .= " WHERE pe.rowid=" . $val["paymentexpensereport"]." AND pe.fk_expensereport = e.rowid";
  1105. $ref = $langs->transnoentitiesnoconv("ExpenseReport");
  1106. }
  1107. elseif ($typerecord == 'payment_salary')
  1108. {
  1109. $sqlmid = 'SELECT s.rowid as ref';
  1110. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "payment_salary as s";
  1111. $sqlmid .= " WHERE s.rowid=" . $val["paymentsalid"];
  1112. $ref = $langs->transnoentitiesnoconv("SalaryPayment");
  1113. }
  1114. elseif ($typerecord == 'sc')
  1115. {
  1116. $sqlmid = 'SELECT sc.rowid as ref';
  1117. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "paiementcharge as sc";
  1118. $sqlmid .= " WHERE sc.rowid=" . $val["paymentscid"];
  1119. $ref = $langs->transnoentitiesnoconv("SocialContribution");
  1120. }
  1121. elseif ($typerecord == 'payment_vat')
  1122. {
  1123. $sqlmid = 'SELECT v.rowid as ref';
  1124. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "tva as v";
  1125. $sqlmid .= " WHERE v.rowid=" . $val["paymentvatid"];
  1126. $ref = $langs->transnoentitiesnoconv("PaymentVat");
  1127. }
  1128. elseif ($typerecord == 'payment_donation')
  1129. {
  1130. $sqlmid = 'SELECT payd.fk_donation as ref';
  1131. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "payment_donation as payd";
  1132. $sqlmid .= " WHERE payd.fk_donation=" . $val["paymentdonationid"];
  1133. $ref = $langs->transnoentitiesnoconv("Donation");
  1134. }
  1135. elseif ($typerecord == 'payment_loan')
  1136. {
  1137. $sqlmid = 'SELECT l.rowid as ref';
  1138. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "payment_loan as l";
  1139. $sqlmid .= " WHERE l.rowid=" . $val["paymentloanid"];
  1140. $ref = $langs->transnoentitiesnoconv("LoanPayment");
  1141. }
  1142. elseif ($typerecord == 'payment_various')
  1143. {
  1144. $sqlmid = 'SELECT v.rowid as ref';
  1145. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "payment_various as v";
  1146. $sqlmid .= " WHERE v.rowid=" . $val["paymentvariousid"];
  1147. $ref = $langs->transnoentitiesnoconv("VariousPayment");
  1148. }
  1149. // Add warning
  1150. if (empty($sqlmid))
  1151. {
  1152. dol_syslog("Found a typerecord=".$typerecord." not supported", LOG_WARNING);
  1153. }
  1154. if ($sqlmid)
  1155. {
  1156. dol_syslog("accountancy/journal/bankjournal.php::sqlmid=" . $sqlmid, LOG_DEBUG);
  1157. $resultmid = $db->query($sqlmid);
  1158. if ($resultmid)
  1159. {
  1160. while ($objmid = $db->fetch_object($resultmid))
  1161. {
  1162. $ref.=' '.$objmid->ref;
  1163. }
  1164. }
  1165. else dol_print_error($db);
  1166. }
  1167. $ref = dol_trunc($langs->transnoentitiesnoconv("BankId").' '.$val['fk_bank'].' - '.$ref, 295); // 295 + 3 dots (...) is < than max size of 300
  1168. return $ref;
  1169. }