dolibarr  13.0.2
bankjournal.php
Go to the documentation of this file.
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-2019 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-2020 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 <https://www.gnu.org/licenses/>.
26  */
27 
33 require '../../main.inc.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
35 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
36 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
37 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
38 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
39 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
40 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
41 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
42 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
43 require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
44 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
45 require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
46 require_once DOL_DOCUMENT_ROOT.'/don/class/paymentdonation.class.php';
47 require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
48 require_once DOL_DOCUMENT_ROOT.'/salaries/class/paymentsalary.class.php';
49 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
50 require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
51 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
52 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
53 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/bookkeeping.class.php';
54 require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php';
55 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
56 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/paymentexpensereport.class.php';
57 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/paymentvarious.class.php';
58 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
59 require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
60 require_once DOL_DOCUMENT_ROOT.'/loan/class/paymentloan.class.php';
61 require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
62 
63 // Load translation files required by the page
64 $langs->loadLangs(array("companies", "other", "compta", "banks", "bills", "donations", "loan", "accountancy", "trips", "salaries", "hrm", "members"));
65 
66 // Multi journal
67 $id_journal = GETPOST('id_journal', 'int');
68 
69 $date_startmonth = GETPOST('date_startmonth', 'int');
70 $date_startday = GETPOST('date_startday', 'int');
71 $date_startyear = GETPOST('date_startyear', 'int');
72 $date_endmonth = GETPOST('date_endmonth', 'int');
73 $date_endday = GETPOST('date_endday', 'int');
74 $date_endyear = GETPOST('date_endyear', 'int');
75 $in_bookkeeping = GETPOST('in_bookkeeping', 'aZ09');
76 if ($in_bookkeeping == '') {
77  $in_bookkeeping = 'notyet';
78 }
79 
80 $now = dol_now();
81 
82 $action = GETPOST('action', 'aZ09');
83 
84 // Security check
85 if ($user->socid > 0 && empty($id_journal)) {
87 }
88 
89 
90 /*
91  * Actions
92  */
93 
94 $error = 0;
95 
96 $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
97 $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
98 
99 if (empty($date_startmonth) || empty($date_endmonth)) {
100  // Period by default on transfer
101  $dates = getDefaultDatesForTransfer();
102  $date_start = $dates['date_start'];
103  $date_end = $dates['date_end'];
104  $pastmonthyear = $dates['pastmonthyear'];
105  $pastmonth = $dates['pastmonth'];
106 }
107 
108 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
109  $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
110  $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
111 }
112 
113 $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,";
114 $sql .= " ba.courant, ba.ref as baref, ba.account_number, ba.fk_accountancy_journal,";
115 $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,";
116 $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,";
117 $sql .= " bu3.type as typeop_payment, bu4.type as typeop_payment_supplier";
118 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
119 $sql .= " JOIN ".MAIN_DB_PREFIX."bank_account as ba on b.fk_account=ba.rowid";
120 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu1 ON bu1.fk_bank = b.rowid AND bu1.type='company'";
121 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu2 ON bu2.fk_bank = b.rowid AND bu2.type='user'";
122 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu3 ON bu3.fk_bank = b.rowid AND bu3.type='payment'";
123 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_url as bu4 ON bu4.fk_bank = b.rowid AND bu4.type='payment_supplier'";
124 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as soc on bu1.url_id=soc.rowid";
125 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on bu2.url_id=u.rowid";
126 $sql .= " WHERE ba.fk_accountancy_journal=".$id_journal;
127 $sql .= ' AND b.amount != 0 AND ba.entity IN ('.getEntity('bank_account', 0).')'; // We don't share object for accountancy
128 if ($date_start && $date_end) {
129  $sql .= " AND b.dateo >= '".$db->idate($date_start)."' AND b.dateo <= '".$db->idate($date_end)."'";
130 }
131 // Define begin binding date
132 if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
133  $sql .= " AND b.dateo >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
134 }
135 // Already in bookkeeping or not
136 if ($in_bookkeeping == 'already') {
137  $sql .= " AND (b.rowid IN (SELECT fk_doc FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as ab WHERE ab.doc_type='bank') )";
138 }
139 if ($in_bookkeeping == 'notyet') {
140  $sql .= " AND (b.rowid NOT IN (SELECT fk_doc FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as ab WHERE ab.doc_type='bank') )";
141 }
142 $sql .= " ORDER BY b.datev";
143 //print $sql;
144 
145 $object = new Account($db);
146 $paymentstatic = new Paiement($db);
147 $paymentsupplierstatic = new PaiementFourn($db);
148 $societestatic = new Societe($db);
149 $userstatic = new User($db);
150 $bankaccountstatic = new Account($db);
151 $chargestatic = new ChargeSociales($db);
152 $paymentdonstatic = new PaymentDonation($db);
153 $paymentvatstatic = new TVA($db);
154 $paymentsalstatic = new PaymentSalary($db);
155 $paymentexpensereportstatic = new PaymentExpenseReport($db);
156 $paymentvariousstatic = new PaymentVarious($db);
157 $paymentloanstatic = new PaymentLoan($db);
158 $accountLinestatic = new AccountLine($db);
159 $paymentsubscriptionstatic = new Subscription($db);
160 
161 $tmppayment = new Paiement($db);
162 $tmpinvoice = new Facture($db);
163 
164 $accountingaccount = new AccountingAccount($db);
165 
166 // Get code of finance journal
167 $accountingjournalstatic = new AccountingJournal($db);
168 $accountingjournalstatic->fetch($id_journal);
169 $journal = $accountingjournalstatic->code;
170 $journal_label = $accountingjournalstatic->label;
171 
172 
173 dol_syslog("accountancy/journal/bankjournal.php", LOG_DEBUG);
174 $result = $db->query($sql);
175 if ($result) {
176  $num = $db->num_rows($result);
177  //print $sql;
178 
179  // Variables
180  $account_supplier = (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER != "") ? $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER : 'NotDefined'); // NotDefined is a reserved word
181  $account_customer = (($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER != "") ? $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER : 'NotDefined'); // NotDefined is a reserved word
182  $account_employee = (!empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) ? $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT : 'NotDefined'); // NotDefined is a reserved word
183  $account_pay_vat = (!empty($conf->global->ACCOUNTING_VAT_PAY_ACCOUNT) ? $conf->global->ACCOUNTING_VAT_PAY_ACCOUNT : 'NotDefined'); // NotDefined is a reserved word
184  $account_pay_donation = (!empty($conf->global->DONATION_ACCOUNTINGACCOUNT) ? $conf->global->DONATION_ACCOUNTINGACCOUNT : 'NotDefined'); // NotDefined is a reserved word
185  $account_pay_subscription = (!empty($conf->global->ADHERENT_SUBSCRIPTION_ACCOUNTINGACCOUNT) ? $conf->global->ADHERENT_SUBSCRIPTION_ACCOUNTINGACCOUNT : 'NotDefined'); // NotDefined is a reserved word
186  $account_transfer = (!empty($conf->global->ACCOUNTING_ACCOUNT_TRANSFER_CASH) ? $conf->global->ACCOUNTING_ACCOUNT_TRANSFER_CASH : 'NotDefined'); // NotDefined is a reserved word
187 
188  $tabcompany = array();
189  $tabuser = array();
190  $tabpay = array();
191  $tabbq = array();
192  $tabtp = array();
193  $tabtype = array();
194  $tabmoreinfo = array();
195 
196  // Loop on each line into llx_bank table. For each line, we should get:
197  // one line tabpay = line into bank
198  // one line for bank record = tabbq
199  // one line for thirdparty record = tabtp
200  $i = 0;
201  while ($i < $num) {
202  $obj = $db->fetch_object($result);
203 
204  $lineisapurchase = -1;
205  $lineisasale = -1;
206  // Old method to detect if it's a sale or purchase
207  if ($obj->label == '(SupplierInvoicePayment)' || $obj->label == '(SupplierInvoicePaymentBack)') {
208  $lineisapurchase = 1;
209  }
210  if ($obj->label == '(CustomerInvoicePayment)' || $obj->label == '(CustomerInvoicePaymentBack)') {
211  $lineisasale = 1;
212  }
213  // Try a more reliable method to detect if record is a supplier payment or a customer payment
214  if ($lineisapurchase < 0) {
215  if ($obj->typeop_payment_supplier == 'payment_supplier') {
216  $lineisapurchase = 1;
217  }
218  }
219  if ($lineisasale < 0) {
220  if ($obj->typeop_payment == 'payment') {
221  $lineisasale = 1;
222  }
223  }
224  //var_dump($obj->type_payment); var_dump($obj->type_payment_supplier);
225  //var_dump($lineisapurchase); //var_dump($lineisasale);
226 
227  // Set accountancy code for bank
228  $compta_bank = $obj->account_number;
229 
230  // Set accountancy code for thirdparty (example: '411CU...' or '411' if no subledger account defined on customer)
231  $compta_soc = 'NotDefined';
232  if ($lineisapurchase > 0) {
233  $compta_soc = (($obj->code_compta_fournisseur != "") ? $obj->code_compta_fournisseur : $account_supplier);
234  }
235  if ($lineisasale > 0) {
236  $compta_soc = (!empty($obj->code_compta) ? $obj->code_compta : $account_customer);
237  }
238 
239  $tabcompany[$obj->rowid] = array(
240  'id' => $obj->socid,
241  'name' => $obj->name,
242  'code_compta' => $compta_soc,
243  'email' => $obj->email
244  );
245 
246  // Set accountancy code for user
247  $compta_user = (!empty($obj->accountancy_code) ? $obj->accountancy_code : '');
248 
249  $tabuser[$obj->rowid] = array(
250  'id' => $obj->userid,
251  'name' => dolGetFirstLastname($obj->firstname, $obj->lastname),
252  'lastname' => $obj->lastname,
253  'firstname' => $obj->firstname,
254  'email' => $obj->useremail,
255  'accountancy_code' => $compta_user
256  );
257 
258  // Variable bookkeeping ($obj->rowid is Bank Id)
259  $tabpay[$obj->rowid]["date"] = $obj->do;
260  $tabpay[$obj->rowid]["type_payment"] = $obj->fk_type; // CHQ, VIR, LIQ, CB, ...
261  $tabpay[$obj->rowid]["ref"] = $obj->label; // By default. Not unique. May be changed later
262  $tabpay[$obj->rowid]["fk_bank"] = $obj->rowid;
263  $tabpay[$obj->rowid]["bank_account_ref"] = $obj->baref;
264  $tabpay[$obj->rowid]["fk_bank_account"] = $obj->fk_account;
265  if (preg_match('/^\((.*)\)$/i', $obj->label, $reg)) {
266  $tabpay[$obj->rowid]["lib"] = $langs->trans($reg[1]);
267  } else {
268  $tabpay[$obj->rowid]["lib"] = dol_trunc($obj->label, 60);
269  }
270 
271  // Load of url links to the line into llx_bank
272  $links = $object->get_url($obj->rowid); // Get an array('url'=>, 'url_id'=>, 'label'=>, 'type'=> 'fk_bank'=> )
273 
274  // By default
275  $tabpay[$obj->rowid]['type'] = 'unknown'; // Can be SOLD, miscellaneous entry, payment of patient, or any old record with no links in bank_url.
276  $tabtype[$obj->rowid] = 'unknown';
277  $tabmoreinfo[$obj->rowid] = array();
278 
279  // get_url may return -1 which is not traversable
280  if (is_array($links) && count($links) > 0) {
281  // Now loop on each link of record in bank.
282  foreach ($links as $key => $val) {
283  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'))) {
284  // So we excluded 'company' and 'user' here. We want only payment lines
285 
286  // We save tabtype for a future use, to remember what kind of payment it is
287  $tabpay[$obj->rowid]['type'] = $links[$key]['type'];
288  $tabtype[$obj->rowid] = $links[$key]['type'];
289  } elseif (in_array($links[$key]['type'], array('company', 'user'))) {
290  if ($tabpay[$obj->rowid]['type'] == 'unknown') {
291  // We can guess here it is a bank record for a thirdparty company or a user.
292  // 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.
293  }
294  }
295 
296  if ($links[$key]['type'] == 'withdraw') {
297  $tabmoreinfo[$obj->rowid]['withdraw'] = 1;
298  }
299 
300  if ($links[$key]['type'] == 'payment') {
301  $paymentstatic->id = $links[$key]['url_id'];
302  $paymentstatic->ref = $links[$key]['url_id'];
303  $tabpay[$obj->rowid]["lib"] .= ' '.$paymentstatic->getNomUrl(2, '', ''); // TODO Do not include list of invoice in tooltip, the dol_string_nohtmltag is ko with this
304  $tabpay[$obj->rowid]["paymentid"] = $paymentstatic->id;
305  } elseif ($links[$key]['type'] == 'payment_supplier') {
306  $paymentsupplierstatic->id = $links[$key]['url_id'];
307  $paymentsupplierstatic->ref = $links[$key]['url_id'];
308  $tabpay[$obj->rowid]["lib"] .= ' '.$paymentsupplierstatic->getNomUrl(2);
309  $tabpay[$obj->rowid]["paymentsupplierid"] = $paymentsupplierstatic->id;
310  } elseif ($links[$key]['type'] == 'company') {
311  $societestatic->id = $links[$key]['url_id'];
312  $societestatic->name = $links[$key]['label'];
313  $societestatic->email = $tabcompany[$obj->rowid]['email'];
314  $tabpay[$obj->rowid]["soclib"] = $societestatic->getNomUrl(1, '', 30);
315  if ($compta_soc) {
316  $tabtp[$obj->rowid][$compta_soc] += $obj->amount;
317  }
318  } elseif ($links[$key]['type'] == 'user') {
319  $userstatic->id = $links[$key]['url_id'];
320  $userstatic->name = $links[$key]['label'];
321  $userstatic->email = $tabuser[$obj->rowid]['email'];
322  $userstatic->firstname = $tabuser[$obj->rowid]['firstname'];
323  $userstatic->lastname = $tabuser[$obj->rowid]['lastname'];
324  if ($userstatic->id > 0) {
325  $tabpay[$obj->rowid]["soclib"] = $userstatic->getNomUrl(1, '', 30);
326  } else {
327  $tabpay[$obj->rowid]["soclib"] = '???'; // Should not happen, but happens with old data when id of user was not saved on expense report payment.
328  }
329  if ($compta_user) {
330  $tabtp[$obj->rowid][$compta_user] += $obj->amount;
331  }
332  } elseif ($links[$key]['type'] == 'sc') {
333  $chargestatic->id = $links[$key]['url_id'];
334  $chargestatic->ref = $links[$key]['url_id'];
335 
336  $tabpay[$obj->rowid]["lib"] .= ' '.$chargestatic->getNomUrl(2);
337  $reg = array();
338  if (preg_match('/^\((.*)\)$/i', $links[$key]['label'], $reg)) {
339  if ($reg[1] == 'socialcontribution') {
340  $reg[1] = 'SocialContribution';
341  }
342  $chargestatic->label = $langs->trans($reg[1]);
343  } else {
344  $chargestatic->label = $links[$key]['label'];
345  }
346  $chargestatic->ref = $chargestatic->label;
347  $tabpay[$obj->rowid]["soclib"] = $chargestatic->getNomUrl(1, 30);
348  $tabpay[$obj->rowid]["paymentscid"] = $chargestatic->id;
349 
350  // Retrieve the accounting code of the social contribution of the payment from link of payment.
351  // Note: We have the social contribution id, it can be faster to get accounting code from social contribution id.
352  $sqlmid = 'SELECT cchgsoc.accountancy_code';
353  $sqlmid .= " FROM ".MAIN_DB_PREFIX."c_chargesociales cchgsoc";
354  $sqlmid .= " INNER JOIN ".MAIN_DB_PREFIX."chargesociales as chgsoc ON chgsoc.fk_type=cchgsoc.id";
355  $sqlmid .= " INNER JOIN ".MAIN_DB_PREFIX."paiementcharge as paycharg ON paycharg.fk_charge=chgsoc.rowid";
356  $sqlmid .= " INNER JOIN ".MAIN_DB_PREFIX."bank_url as bkurl ON bkurl.url_id=paycharg.rowid AND bkurl.type = 'payment_sc'";
357  $sqlmid .= " WHERE bkurl.fk_bank=".$obj->rowid;
358 
359  dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=".$sqlmid, LOG_DEBUG);
360  $resultmid = $db->query($sqlmid);
361  if ($resultmid) {
362  $objmid = $db->fetch_object($resultmid);
363  $tabtp[$obj->rowid][$objmid->accountancy_code] += $obj->amount;
364  }
365  } elseif ($links[$key]['type'] == 'payment_donation') {
366  $paymentdonstatic->id = $links[$key]['url_id'];
367  $paymentdonstatic->ref = $links[$key]['url_id'];
368  $paymentdonstatic->fk_donation = $links[$key]['url_id'];
369  $tabpay[$obj->rowid]["lib"] .= ' '.$paymentdonstatic->getNomUrl(2);
370  $tabpay[$obj->rowid]["paymentdonationid"] = $paymentdonstatic->id;
371  $tabtp[$obj->rowid][$account_pay_donation] += $obj->amount;
372  } elseif ($links[$key]['type'] == 'member') {
373  $paymentsubscriptionstatic->id = $links[$key]['url_id'];
374  $paymentsubscriptionstatic->ref = $links[$key]['url_id'];
375  $paymentsubscriptionstatic->label = $links[$key]['label'];
376  $tabpay[$obj->rowid]["lib"] .= ' '.$paymentsubscriptionstatic->getNomUrl(2);
377  $tabpay[$obj->rowid]["paymentsubscriptionid"] = $paymentsubscriptionstatic->id;
378  $paymentsubscriptionstatic->fetch($paymentsubscriptionstatic->id);
379  $tabtp[$obj->rowid][$account_pay_subscription] += $obj->amount;
380  } elseif ($links[$key]['type'] == 'payment_vat') { // Payment VAT
381  $paymentvatstatic->id = $links[$key]['url_id'];
382  $paymentvatstatic->ref = $links[$key]['url_id'];
383  $paymentvatstatic->label = $links[$key]['label'];
384  $tabpay[$obj->rowid]["lib"] .= ' '.$paymentvatstatic->getNomUrl(2);
385  $tabpay[$obj->rowid]["paymentvatid"] = $paymentvatstatic->id;
386  $tabtp[$obj->rowid][$account_pay_vat] += $obj->amount;
387  } elseif ($links[$key]['type'] == 'payment_salary') {
388  $paymentsalstatic->id = $links[$key]['url_id'];
389  $paymentsalstatic->ref = $links[$key]['url_id'];
390  $paymentsalstatic->label = $links[$key]['label'];
391  $tabpay[$obj->rowid]["lib"] .= ' '.$paymentsalstatic->getNomUrl(2);
392  $tabpay[$obj->rowid]["paymentsalid"] = $paymentsalstatic->id;
393  } elseif ($links[$key]['type'] == 'payment_expensereport') {
394  $paymentexpensereportstatic->id = $links[$key]['url_id'];
395  $tabpay[$obj->rowid]["lib"] .= $paymentexpensereportstatic->getNomUrl(2);
396  $tabpay[$obj->rowid]["paymentexpensereport"] = $paymentexpensereportstatic->id;
397  } elseif ($links[$key]['type'] == 'payment_various') {
398  $paymentvariousstatic->id = $links[$key]['url_id'];
399  $paymentvariousstatic->ref = $links[$key]['url_id'];
400  $paymentvariousstatic->label = $links[$key]['label'];
401  $tabpay[$obj->rowid]["lib"] .= ' '.$paymentvariousstatic->getNomUrl(2);
402  $tabpay[$obj->rowid]["paymentvariousid"] = $paymentvariousstatic->id;
403  $paymentvariousstatic->fetch($paymentvariousstatic->id);
404  $account_various = (!empty($paymentvariousstatic->accountancy_code) ? $paymentvariousstatic->accountancy_code : 'NotDefined'); // NotDefined is a reserved word
405  $account_subledger = (!empty($paymentvariousstatic->subledger_account) ? $paymentvariousstatic->subledger_account : ''); // NotDefined is a reserved word
406  $tabpay[$obj->rowid]["account_various"] = $account_various;
407  $tabtp[$obj->rowid][$account_subledger] += $obj->amount;
408  } elseif ($links[$key]['type'] == 'payment_loan') {
409  $paymentloanstatic->id = $links[$key]['url_id'];
410  $paymentloanstatic->ref = $links[$key]['url_id'];
411  $paymentloanstatic->fk_loan = $links[$key]['url_id'];
412  $tabpay[$obj->rowid]["lib"] .= ' '.$paymentloanstatic->getNomUrl(2);
413  $tabpay[$obj->rowid]["paymentloanid"] = $paymentloanstatic->id;
414  //$tabtp[$obj->rowid][$account_pay_loan] += $obj->amount;
415  $sqlmid = 'SELECT pl.amount_capital, pl.amount_insurance, pl.amount_interest, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest';
416  $sqlmid .= ' FROM '.MAIN_DB_PREFIX.'payment_loan as pl, '.MAIN_DB_PREFIX.'loan as l';
417  $sqlmid .= ' WHERE l.rowid = pl.fk_loan AND pl.fk_bank = '.$obj->rowid;
418 
419  dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=".$sqlmid, LOG_DEBUG);
420  $resultmid = $db->query($sqlmid);
421  if ($resultmid) {
422  $objmid = $db->fetch_object($resultmid);
423  $tabtp[$obj->rowid][$objmid->accountancy_account_capital] -= $objmid->amount_capital;
424  $tabtp[$obj->rowid][$objmid->accountancy_account_insurance] -= $objmid->amount_insurance;
425  $tabtp[$obj->rowid][$objmid->accountancy_account_interest] -= $objmid->amount_interest;
426  }
427  } elseif ($links[$key]['type'] == 'banktransfert') {
428  $accountLinestatic->fetch($links[$key]['url_id']);
429  $tabpay[$obj->rowid]["lib"] .= ' '.$langs->trans("BankTransfer").'- '.$accountLinestatic ->getNomUrl(1);
430  $tabtp[$obj->rowid][$account_transfer] += $obj->amount;
431  $bankaccountstatic->fetch($tabpay[$obj->rowid]['fk_bank_account']);
432  $tabpay[$obj->rowid]["soclib"] = $bankaccountstatic->getNomUrl(2);
433  }
434  }
435  }
436 
437  $tabbq[$obj->rowid][$compta_bank] += $obj->amount;
438 
439  // If no links were found to know the amount on thirdparty, we try to guess it.
440  // This may happens on bank entries without the links lines to 'company'.
441  if (empty($tabtp[$obj->rowid]) && !empty($tabmoreinfo[$obj->rowid]['withdraw'])) { // If we dont find 'company' link because it is an old 'withdraw' record
442  foreach ($links as $key => $val) {
443  if ($links[$key]['type'] == 'payment') {
444  // Get thirdparty
445  $tmppayment->fetch($links[$key]['url_id']);
446  $arrayofamounts = $tmppayment->getAmountsArray();
447  if (is_array($arrayofamounts)) {
448  foreach ($arrayofamounts as $invoiceid => $amount) {
449  $tmpinvoice->fetch($invoiceid);
450  $tmpinvoice->fetch_thirdparty();
451  if ($tmpinvoice->thirdparty->code_compta) {
452  $tabtp[$obj->rowid][$tmpinvoice->thirdparty->code_compta] += $amount;
453  }
454  }
455  }
456  }
457  }
458  }
459 
460  // If no links were found to know the amount on thirdparty, we init it to account 'NotDefined'.
461  if (empty($tabtp[$obj->rowid])) {
462  $tabtp[$obj->rowid]['NotDefined'] = $tabbq[$obj->rowid][$compta_bank];
463  }
464 
465  // Check account number is ok
466  /*if ($action == 'writebookkeeping') // Make test now in such a case
467  {
468  reset($tabbq[$obj->rowid]);
469  $first_key_tabbq = key($tabbq[$obj->rowid]);
470  if (empty($first_key_tabbq))
471  {
472  $error++;
473  setEventMessages($langs->trans('ErrorAccountancyCodeOnBankAccountNotDefined', $obj->baref), null, 'errors');
474  }
475  reset($tabtp[$obj->rowid]);
476  $first_key_tabtp = key($tabtp[$obj->rowid]);
477  if (empty($first_key_tabtp))
478  {
479  $error++;
480  setEventMessages($langs->trans('ErrorAccountancyCodeOnThirdPartyNotDefined'), null, 'errors');
481  }
482  }*/
483 
484  // if($obj->socid)$tabtp[$obj->rowid][$compta_soc] += $obj->amount;
485 
486  $i++;
487  }
488 } else {
489  dol_print_error($db);
490 }
491 
492 
493 /*var_dump($tabpay);
494 var_dump($tabcompany);
495 var_dump($tabbq);
496 var_dump($tabtp);
497 var_dump($tabtype);*/
498 
499 // Write bookkeeping
500 if (!$error && $action == 'writebookkeeping') {
501  $now = dol_now();
502 
503  $error = 0;
504  foreach ($tabpay as $key => $val) { // $key is rowid into llx_bank
505  $date = dol_print_date($db->jdate($val["date"]), 'day');
506 
507  $ref = getSourceDocRef($val, $tabtype[$key]);
508 
509  $errorforline = 0;
510 
511  $totalcredit = 0;
512  $totaldebit = 0;
513 
514  $db->begin();
515 
516  // Introduce a protection. Total of tabtp must be total of tabbq
517  /*var_dump($tabpay);
518  var_dump($tabtp);
519  var_dump($tabbq);exit;*/
520 
521  // Bank
522  if (!$errorforline && is_array($tabbq[$key])) {
523  // Line into bank account
524  foreach ($tabbq[$key] as $k => $mt) {
525  if ($mt) {
526  $reflabel = '';
527  if (!empty($val['lib'])) {
528  $reflabel .= dol_string_nohtmltag($val['lib'])." - ";
529  }
530  $reflabel .= $langs->trans("Bank").' '.dol_string_nohtmltag($val['bank_account_ref']);
531  if (!empty($val['soclib'])) {
532  $reflabel .= " - ".dol_string_nohtmltag($val['soclib']);
533  }
534 
535  $bookkeeping = new BookKeeping($db);
536  $bookkeeping->doc_date = $val["date"];
537  $bookkeeping->doc_ref = $ref;
538  $bookkeeping->doc_type = 'bank';
539  $bookkeeping->fk_doc = $key;
540  $bookkeeping->fk_docdet = $val["fk_bank"];
541  $bookkeeping->numero_compte = $k;
542 
543  $accountingaccount->fetch(null, $k, true);
544  $bookkeeping->label_compte = $accountingaccount->label;
545 
546  $bookkeeping->label_operation = $reflabel;
547  $bookkeeping->montant = $mt;
548  $bookkeeping->sens = ($mt >= 0) ? 'D' : 'C';
549  $bookkeeping->debit = ($mt >= 0 ? $mt : 0);
550  $bookkeeping->credit = ($mt < 0 ? -$mt : 0);
551  $bookkeeping->code_journal = $journal;
552  $bookkeeping->journal_label = $langs->transnoentities($journal_label);
553  $bookkeeping->fk_user_author = $user->id;
554  $bookkeeping->date_creation = $now;
555 
556  // No subledger_account value for the bank line but add a specific label_operation
557  $bookkeeping->subledger_account = '';
558  $bookkeeping->label_operation = $reflabel;
559  $bookkeeping->entity = $conf->entity;
560 
561  $totaldebit += $bookkeeping->debit;
562  $totalcredit += $bookkeeping->credit;
563 
564  $result = $bookkeeping->create($user);
565  if ($result < 0) {
566  if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists
567  $error++;
568  $errorforline++;
569  setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
570  } else {
571  $error++;
572  $errorforline++;
573  setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
574  }
575  }
576  }
577  }
578  }
579 
580  // Third party
581  if (!$errorforline) {
582  if (is_array($tabtp[$key])) {
583  // Line into thirdparty account
584  foreach ($tabtp[$key] as $k => $mt) {
585  if ($mt) {
586  $reflabel = '';
587  if (!empty($val['lib'])) {
588  $reflabel .= dol_string_nohtmltag($val['lib']).($val['soclib'] ? " - " : "");
589  }
590  if ($tabtype[$key] == 'banktransfert') {
591  $reflabel .= dol_string_nohtmltag($langs->transnoentitiesnoconv('TransitionalAccount').' '.$account_transfer);
592  } else {
593  $reflabel .= dol_string_nohtmltag($val['soclib']);
594  }
595 
596  $bookkeeping = new BookKeeping($db);
597  $bookkeeping->doc_date = $val["date"];
598  $bookkeeping->doc_ref = $ref;
599  $bookkeeping->doc_type = 'bank';
600  $bookkeeping->fk_doc = $key;
601  $bookkeeping->fk_docdet = $val["fk_bank"];
602  $bookkeeping->label_operation = $reflabel;
603  $bookkeeping->montant = $mt;
604  $bookkeeping->sens = ($mt < 0) ? 'D' : 'C';
605  $bookkeeping->debit = ($mt < 0 ? -$mt : 0);
606  $bookkeeping->credit = ($mt >= 0) ? $mt : 0;
607  $bookkeeping->code_journal = $journal;
608  $bookkeeping->journal_label = $langs->transnoentities($journal_label);
609  $bookkeeping->fk_user_author = $user->id;
610  $bookkeeping->date_creation = $now;
611 
612  if ($tabtype[$key] == 'payment') { // If payment is payment of customer invoice, we get ref of invoice
613  $bookkeeping->subledger_account = $k; // For payment, the subledger account is stored as $key of $tabtp
614  $bookkeeping->subledger_label = $tabcompany[$key]['name']; // $tabcompany is defined only if we are sure there is 1 thirdparty for the bank transaction
615  $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER;
616 
617  $accountingaccount->fetch(null, $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER, true);
618  $bookkeeping->label_compte = $accountingaccount->label;
619  } elseif ($tabtype[$key] == 'payment_supplier') { // If payment is payment of supplier invoice, we get ref of invoice
620  $bookkeeping->subledger_account = $k; // For payment, the subledger account is stored as $key of $tabtp
621  $bookkeeping->subledger_label = $tabcompany[$key]['name']; // $tabcompany is defined only if we are sure there is 1 thirdparty for the bank transaction
622  $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER;
623 
624  $accountingaccount->fetch(null, $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER, true);
625  $bookkeeping->label_compte = $accountingaccount->label;
626  } elseif ($tabtype[$key] == 'payment_expensereport') {
627  $bookkeeping->subledger_account = $tabuser[$key]['accountancy_code'];
628  $bookkeeping->subledger_label = $tabuser[$key]['name'];
629  $bookkeeping->numero_compte = $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT;
630 
631  $accountingaccount->fetch(null, $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT, true);
632  $bookkeeping->label_compte = $accountingaccount->label;
633  } elseif ($tabtype[$key] == 'payment_salary') {
634  $bookkeeping->subledger_account = $tabuser[$key]['accountancy_code'];
635  $bookkeeping->subledger_label = $tabuser[$key]['name'];
636  $bookkeeping->numero_compte = $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT;
637 
638  $accountingaccount->fetch(null, $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT, true);
639  $bookkeeping->label_compte = $accountingaccount->label;
640  } elseif (in_array($tabtype[$key], array('sc', 'payment_sc'))) { // If payment is payment of social contribution
641  $bookkeeping->subledger_account = '';
642  $bookkeeping->subledger_label = '';
643  $bookkeeping->numero_compte = $k;
644 
645  $accountingaccount->fetch(null, $k, true);
646  $bookkeeping->label_compte = $accountingaccount->label;
647  } elseif ($tabtype[$key] == 'payment_vat') {
648  $bookkeeping->subledger_account = '';
649  $bookkeeping->subledger_label = '';
650  $bookkeeping->numero_compte = $k;
651 
652  $accountingaccount->fetch(null, $k, true);
653  $bookkeeping->label_compte = $accountingaccount->label;
654  } elseif ($tabtype[$key] == 'payment_donation') {
655  $bookkeeping->subledger_account = '';
656  $bookkeeping->subledger_label = '';
657  $bookkeeping->numero_compte = $k;
658 
659  $accountingaccount->fetch(null, $k, true);
660  $bookkeeping->label_compte = $accountingaccount->label;
661  } elseif ($tabtype[$key] == 'member') {
662  $bookkeeping->subledger_account = '';
663  $bookkeeping->subledger_label = '';
664  $bookkeeping->numero_compte = $k;
665 
666  $accountingaccount->fetch(null, $k, true);
667  $bookkeeping->label_compte = $accountingaccount->label;
668  } elseif ($tabtype[$key] == 'payment_loan') {
669  $bookkeeping->subledger_account = '';
670  $bookkeeping->subledger_label = '';
671  $bookkeeping->numero_compte = $k;
672 
673  $accountingaccount->fetch(null, $k, true);
674  $bookkeeping->label_compte = $accountingaccount->label;
675  } elseif ($tabtype[$key] == 'payment_various') {
676  $bookkeeping->subledger_account = $k;
677  $bookkeeping->subledger_label = $tabcompany[$key]['name'];
678  $bookkeeping->numero_compte = $tabpay[$key]["account_various"];
679 
680  $accountingaccount->fetch(null, $bookkeeping->numero_compte, true);
681  $bookkeeping->label_compte = $accountingaccount->label;
682  } elseif ($tabtype[$key] == 'banktransfert') {
683  $bookkeeping->subledger_account = '';
684  $bookkeeping->subledger_label = '';
685  $bookkeeping->numero_compte = $k;
686 
687  $accountingaccount->fetch(null, $k, true);
688  $bookkeeping->label_compte = $accountingaccount->label;
689  } else {
690  if ($tabtype[$key] == 'unknown') { // Unknown transaction, we will use a waiting account for thirdparty.
691  // Temporary account
692  $bookkeeping->subledger_account = '';
693  $bookkeeping->subledger_label = '';
694  $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_SUSPENSE;
695 
696  $accountingaccount->fetch(null, $conf->global->ACCOUNTING_ACCOUNT_SUSPENSE, true);
697  $bookkeeping->label_compte = $accountingaccount->label;
698  }
699  }
700  $bookkeeping->label_operation = $reflabel;
701  $bookkeeping->entity = $conf->entity;
702 
703  $totaldebit += $bookkeeping->debit;
704  $totalcredit += $bookkeeping->credit;
705 
706  $result = $bookkeeping->create($user);
707  if ($result < 0) {
708  if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists
709  $error++;
710  $errorforline++;
711  setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
712  } else {
713  $error++;
714  $errorforline++;
715  setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
716  }
717  }
718  }
719  }
720  } else { // If thirdparty unknown, output the waiting account
721  foreach ($tabbq[$key] as $k => $mt) {
722  if ($mt) {
723  $reflabel = '';
724  if (!empty($val['lib'])) {
725  $reflabel .= dol_string_nohtmltag($val['lib'])." - ";
726  }
727  $reflabel .= dol_string_nohtmltag('WaitingAccount');
728 
729  $bookkeeping = new BookKeeping($db);
730  $bookkeeping->doc_date = $val["date"];
731  $bookkeeping->doc_ref = $ref;
732  $bookkeeping->doc_type = 'bank';
733  $bookkeeping->fk_doc = $key;
734  $bookkeeping->fk_docdet = $val["fk_bank"];
735  $bookkeeping->montant = $mt;
736  $bookkeeping->sens = ($mt < 0) ? 'D' : 'C';
737  $bookkeeping->debit = ($mt < 0 ? -$mt : 0);
738  $bookkeeping->credit = ($mt >= 0) ? $mt : 0;
739  $bookkeeping->code_journal = $journal;
740  $bookkeeping->journal_label = $langs->transnoentities($journal_label);
741  $bookkeeping->fk_user_author = $user->id;
742  $bookkeeping->date_creation = $now;
743  $bookkeeping->label_compte = '';
744  $bookkeeping->label_operation = $reflabel;
745  $bookkeeping->entity = $conf->entity;
746 
747  $totaldebit += $bookkeeping->debit;
748  $totalcredit += $bookkeeping->credit;
749 
750  $result = $bookkeeping->create($user);
751 
752  if ($result < 0) {
753  if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists
754  $error++;
755  $errorforline++;
756  setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
757  } else {
758  $error++;
759  $errorforline++;
760  setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
761  }
762  }
763  }
764  }
765  }
766  }
767 
768  if (price2num($totaldebit, 'MT') != price2num($totalcredit, 'MT')) {
769  $error++;
770  $errorforline++;
771  setEventMessages('Try to insert a non balanced transaction in book for '.$ref.'. Canceled. Surely a bug.', null, 'errors');
772  }
773 
774  if (!$errorforline) {
775  $db->commit();
776  } else {
777  //print 'KO for line '.$key.' '.$error.'<br>';
778  $db->rollback();
779 
780  $MAXNBERRORS = 5;
781  if ($error >= $MAXNBERRORS) {
782  setEventMessages($langs->trans("ErrorTooManyErrorsProcessStopped").' (>'.$MAXNBERRORS.')', null, 'errors');
783  break; // Break in the foreach
784  }
785  }
786  }
787 
788  if (empty($error) && count($tabpay) > 0) {
789  setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
790  } elseif (count($tabpay) == $error) {
791  setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
792  } else {
793  setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
794  }
795 
796  $action = '';
797 
798  // Must reload data, so we make a redirect
799  if (count($tabpay) != $error) {
800  $param = 'id_journal='.$id_journal;
801  $param .= '&date_startday='.$date_startday;
802  $param .= '&date_startmonth='.$date_startmonth;
803  $param .= '&date_startyear='.$date_startyear;
804  $param .= '&date_endday='.$date_endday;
805  $param .= '&date_endmonth='.$date_endmonth;
806  $param .= '&date_endyear='.$date_endyear;
807  $param .= '&in_bookkeeping='.$in_bookkeeping;
808  header("Location: ".$_SERVER['PHP_SELF'].($param ? '?'.$param : ''));
809  exit;
810  }
811 }
812 
813 
814 
815 // Export
816 if ($action == 'exportcsv') { // ISO and not UTF8 !
817  $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
818 
819  $filename = 'journal';
820  $type_export = 'journal';
821  include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
822 
823  // CSV header line
824  print '"'.$langs->transnoentitiesnoconv("BankId").'"'.$sep;
825  print '"'.$langs->transnoentitiesnoconv("Date").'"'.$sep;
826  print '"'.$langs->transnoentitiesnoconv("PaymentMode").'"'.$sep;
827  print '"'.$langs->transnoentitiesnoconv("AccountAccounting").'"'.$sep;
828  print '"'.$langs->transnoentitiesnoconv("LedgerAccount").'"'.$sep;
829  print '"'.$langs->transnoentitiesnoconv("SubledgerAccount").'"'.$sep;
830  print '"'.$langs->transnoentitiesnoconv("Label").'"'.$sep;
831  print '"'.$langs->transnoentitiesnoconv("Debit").'"'.$sep;
832  print '"'.$langs->transnoentitiesnoconv("Credit").'"'.$sep;
833  print '"'.$langs->transnoentitiesnoconv("Journal").'"'.$sep;
834  print '"'.$langs->transnoentitiesnoconv("Note").'"'.$sep;
835  print "\n";
836 
837  foreach ($tabpay as $key => $val) {
838  $date = dol_print_date($db->jdate($val["date"]), 'day');
839 
840  $ref = getSourceDocRef($val, $tabtype[$key]);
841 
842  // Bank
843  foreach ($tabbq[$key] as $k => $mt) {
844  if ($mt) {
845  $reflabel = '';
846  if (!empty($val['lib'])) {
847  $reflabel .= dol_string_nohtmltag($val['lib'])." - ";
848  }
849  $reflabel .= $langs->trans("Bank").' '.dol_string_nohtmltag($val['bank_account_ref']);
850  if (!empty($val['soclib'])) {
851  $reflabel .= " - ".dol_string_nohtmltag($val['soclib']);
852  }
853 
854  print '"'.$key.'"'.$sep;
855  print '"'.$date.'"'.$sep;
856  print '"'.$val["type_payment"].'"'.$sep;
857  print '"'.length_accountg(html_entity_decode($k)).'"'.$sep;
858  print '"'.length_accounta(html_entity_decode($k)).'"'.$sep;
859  print " ".$sep;
860  print '"'.$reflabel.'"'.$sep;
861  print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep;
862  print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep;
863  print '"'.$journal.'"'.$sep;
864  print '"'.dol_string_nohtmltag($ref).'"'.$sep;
865  print "\n";
866  }
867  }
868 
869  // Third party
870  if (is_array($tabtp[$key])) {
871  foreach ($tabtp[$key] as $k => $mt) {
872  if ($mt) {
873  $reflabel = '';
874  if (!empty($val['lib'])) {
875  $reflabel .= dol_string_nohtmltag($val['lib']).($val['soclib'] ? " - " : "");
876  }
877  if ($tabtype[$key] == 'banktransfert') {
878  $reflabel .= dol_string_nohtmltag($langs->transnoentitiesnoconv('TransitionalAccount').' '.$account_transfer);
879  } else {
880  $reflabel .= dol_string_nohtmltag($val['soclib']);
881  }
882 
883  print '"'.$key.'"'.$sep;
884  print '"'.$date.'"'.$sep;
885  print '"'.$val["type_payment"].'"'.$sep;
886  print '"'.length_accountg(html_entity_decode($k)).'"'.$sep;
887  if ($tabtype[$key] == 'payment_supplier') {
888  print '"'.$conf->global->ACCOUNTING_ACCOUNT_SUPPLIER.'"'.$sep;
889  } elseif ($tabtype[$key] == 'payment') {
890  print '"'.$conf->global->ACCOUNTING_ACCOUNT_CUSTOMER.'"'.$sep;
891  } elseif ($tabtype[$key] == 'payment_expensereport') {
892  print '"'.$conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT.'"'.$sep;
893  } elseif ($tabtype[$key] == 'payment_salary') {
894  print '"'.$conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT.'"'.$sep;
895  } else {
896  print '"'.length_accountg(html_entity_decode($k)).'"'.$sep;
897  }
898  print '"'.length_accounta(html_entity_decode($k)).'"'.$sep;
899  print '"'.$reflabel.'"'.$sep;
900  print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep;
901  print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep;
902  print '"'.$journal.'"'.$sep;
903  print '"'.dol_string_nohtmltag($ref).'"'.$sep;
904  print "\n";
905  }
906  }
907  } else { // If thirdparty unkown, output the waiting account
908  foreach ($tabbq[$key] as $k => $mt) {
909  if ($mt) {
910  $reflabel = '';
911  if (!empty($val['lib'])) {
912  $reflabel .= dol_string_nohtmltag($val['lib'])." - ";
913  }
914  $reflabel .= dol_string_nohtmltag('WaitingAccount');
915 
916  print '"'.$key.'"'.$sep;
917  print '"'.$date.'"'.$sep;
918  print '"'.$val["type_payment"].'"'.$sep;
919  print '"'.length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE).'"'.$sep;
920  print '"'.length_accounta($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE).'"'.$sep;
921  print "".$sep;
922  print '"'.$reflabel.'"'.$sep;
923  print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep;
924  print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep;
925  print '"'.$journal.'"'.$sep;
926  print '"'.dol_string_nohtmltag($ref).'"'.$sep;
927  print "\n";
928  }
929  }
930  }
931  }
932 }
933 
934 
935 /*
936  * View
937  */
938 
939 $form = new Form($db);
940 
941 if (empty($action) || $action == 'view') {
942  $invoicestatic = new Facture($db);
943  $invoicesupplierstatic = new FactureFournisseur($db);
944  $expensereportstatic = new ExpenseReport($db);
945  $vatstatic = new Tva($db);
946  $donationstatic = new Don($db);
947  $loanstatic = new Loan($db);
948  $salarystatic = new PaymentSalary($db);
949  $variousstatic = new PaymentVarious($db);
950 
951  llxHeader('', $langs->trans("FinanceJournal"));
952 
953  $nom = $langs->trans("FinanceJournal").' | '.$accountingjournalstatic->getNomUrl(0, 1, 1, '', 1);
954  $builddate = dol_now();
955  //$description = $langs->trans("DescFinanceJournal") . '<br>';
956  $description .= $langs->trans("DescJournalOnlyBindedVisible").'<br>';
957 
958  $listofchoices = array('notyet'=>$langs->trans("NotYetInGeneralLedger"), 'already'=>$langs->trans("AlreadyInGeneralLedger"));
959  $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);
960  $period .= ' - '.$langs->trans("JournalizationInLedgerStatus").' '.$form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
961 
962  $varlink = 'id_journal='.$id_journal;
963 
964  journalHead($nom, '', $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
965 
966 
967  // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed)
968  $sql = 'SELECT COUNT(rowid) as nb FROM '.MAIN_DB_PREFIX.'bank_account WHERE entity = '.$conf->entity.' AND fk_accountancy_journal IS NULL AND clos=0';
969  $resql = $db->query($sql);
970  if ($resql) {
971  $obj = $db->fetch_object($resql);
972  if ($obj->nb > 0) {
973  print '<br><div class="warning">'.img_warning().' '.$langs->trans("TheJournalCodeIsNotDefinedOnSomeBankAccount");
974  $desc = ' : '.$langs->trans("AccountancyAreaDescBank", 9, '{link}');
975  $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("BankAccounts").'</strong>', $desc);
976  print $desc;
977  print '</div>';
978  }
979  } else {
980  dol_print_error($db);
981  }
982 
983 
984  // Button to write into Ledger
985  if (($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "") || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1'
986  || ($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "") || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1'
987  || empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) || $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT == '-1') {
988  print '<br>'.img_warning().' '.$langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
989  print ' : '.$langs->trans("AccountancyAreaDescMisc", 4, '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("MenuDefaultAccounts").'</strong>');
990  }
991 
992 
993  print '<div class="tabsAction tabsActionNoBottom">';
994 
995  if (!empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL) && $in_bookkeeping == 'notyet') {
996  print '<input type="button" class="butAction" name="exportcsv" value="'.$langs->trans("ExportDraftJournal").'" onclick="launch_export();" />';
997  }
998 
999  if (($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == "") || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER == '-1'
1000  || ($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "") || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1') {
1001  print '<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")).'" value="'.$langs->trans("WriteBookKeeping").'" />';
1002  } else {
1003  if ($in_bookkeeping == 'notyet') {
1004  print '<input type="button" class="butAction" name="writebookkeeping" value="'.$langs->trans("WriteBookKeeping").'" onclick="writebookkeeping();" />';
1005  } else {
1006  print '<a class="butActionRefused classfortooltip" name="writebookkeeping">'.$langs->trans("WriteBookKeeping").'</a>';
1007  }
1008  }
1009 
1010  print '</div>';
1011 
1012  // TODO Avoid using js. We can use a direct link with $param
1013  print '
1014  <script type="text/javascript">
1015  function launch_export() {
1016  console.log("Set value into form and submit");
1017  $("div.fiche form input[name=\"action\"]").val("exportcsv");
1018  $("div.fiche form input[type=\"submit\"]").click();
1019  $("div.fiche form input[name=\"action\"]").val("");
1020  }
1021  function writebookkeeping() {
1022  console.log("Set value into form and submit");
1023  $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
1024  $("div.fiche form input[type=\"submit\"]").click();
1025  $("div.fiche form input[name=\"action\"]").val("");
1026  }
1027  </script>';
1028 
1029  /*
1030  * Show result array
1031  */
1032  print '<br>';
1033 
1034  $i = 0;
1035  print '<div class="div-table-responsive">';
1036  print "<table class=\"noborder\" width=\"100%\">";
1037  print "<tr class=\"liste_titre\">";
1038  print "<td>".$langs->trans("Date")."</td>";
1039  print "<td>".$langs->trans("Piece").' ('.$langs->trans("ObjectsRef").")</td>";
1040  print "<td>".$langs->trans("AccountAccounting")."</td>";
1041  print "<td>".$langs->trans("SubledgerAccount")."</td>";
1042  print "<td>".$langs->trans("LabelOperation")."</td>";
1043  print '<td class="center">'.$langs->trans("PaymentMode")."</td>";
1044  print '<td class="right">'.$langs->trans("Debit")."</td>";
1045  print '<td class="right">'.$langs->trans("Credit")."</td>";
1046  print "</tr>\n";
1047 
1048  $r = '';
1049 
1050  foreach ($tabpay as $key => $val) { // $key is rowid in llx_bank
1051  $date = dol_print_date($db->jdate($val["date"]), 'day');
1052 
1053  $ref = getSourceDocRef($val, $tabtype[$key]);
1054 
1055  // Bank
1056  foreach ($tabbq[$key] as $k => $mt) {
1057  if ($mt) {
1058  $reflabel = '';
1059  if (!empty($val['lib'])) {
1060  $reflabel .= $val['lib']." - ";
1061  }
1062  $reflabel .= $langs->trans("Bank").' '.$val['bank_account_ref'];
1063  if (!empty($val['soclib'])) {
1064  $reflabel .= " - ".$val['soclib'];
1065  }
1066 
1067  //var_dump($tabpay[$key]);
1068  print '<!-- Bank bank.rowid='.$key.' type='.$tabpay[$key]['type'].' ref='.$tabpay[$key]['ref'].'-->';
1069  print '<tr class="oddeven">';
1070  print "<td>".$date."</td>";
1071  print "<td>".$ref."</td>";
1072  // Ledger account
1073  print "<td>";
1074  $accounttoshow = length_accountg($k);
1075  if (empty($accounttoshow) || $accounttoshow == 'NotDefined') {
1076  print '<span class="error">'.$langs->trans("BankAccountNotDefined").'</span>';
1077  } else {
1078  print $accounttoshow;
1079  }
1080  print "</td>";
1081  // Subledger account
1082  print "<td>";
1083  /*$accounttoshow = length_accountg($k);
1084  if (empty($accounttoshow) || $accounttoshow == 'NotDefined')
1085  {
1086  print '<span class="error">'.$langs->trans("BankAccountNotDefined").'</span>';
1087  }
1088  else print $accounttoshow;*/
1089  print "</td>";
1090  print "<td>";
1091  print $reflabel;
1092  print "</td>";
1093  print '<td class="center">'.$val["type_payment"]."</td>";
1094  print '<td class="right nowraponall">'.($mt >= 0 ? price($mt) : '')."</td>";
1095  print '<td class="right nowraponall">'.($mt < 0 ? price(-$mt) : '')."</td>";
1096  print "</tr>";
1097  }
1098  }
1099 
1100  // Third party
1101  if (is_array($tabtp[$key])) {
1102  foreach ($tabtp[$key] as $k => $mt) {
1103  if ($mt) {
1104  $reflabel = '';
1105  if (!empty($val['lib'])) {
1106  $reflabel .= $val['lib'].($val['soclib'] ? " - " : "");
1107  }
1108  if ($tabtype[$key] == 'banktransfert') {
1109  $reflabel .= $langs->trans('TransitionalAccount').' '.$account_transfer;
1110  } else {
1111  $reflabel .= $val['soclib'];
1112  }
1113 
1114  print '<!-- Thirdparty bank.rowid='.$key.' -->';
1115  print '<tr class="oddeven">';
1116  print "<td>".$date."</td>";
1117  print "<td>".$ref."</td>";
1118  // Ledger account
1119  print "<td>";
1120  $account_ledger = $k;
1121  // Try to force general ledger account depending on type
1122  if ($tabtype[$key] == 'payment') {
1123  $account_ledger = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER;
1124  }
1125  if ($tabtype[$key] == 'payment_supplier') {
1126  $account_ledger = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER;
1127  }
1128  if ($tabtype[$key] == 'payment_expensereport') {
1129  $account_ledger = $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT;
1130  }
1131  if ($tabtype[$key] == 'payment_salary') {
1132  $account_ledger = $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT;
1133  }
1134  if ($tabtype[$key] == 'payment_vat') {
1135  $account_ledger = $conf->global->ACCOUNTING_VAT_PAY_ACCOUNT;
1136  }
1137  if ($tabtype[$key] == 'member') {
1138  $account_ledger = $conf->global->ADHERENT_SUBSCRIPTION_ACCOUNTINGACCOUNT;
1139  }
1140  if ($tabtype[$key] == 'payment_various') {
1141  $account_ledger = $tabpay[$key]["account_various"];
1142  }
1143  $accounttoshow = length_accountg($account_ledger);
1144  if (empty($accounttoshow) || $accounttoshow == 'NotDefined') {
1145  if ($tabtype[$key] == 'unknown') {
1146  // We will accept writing, but into a waiting account
1147  if (empty($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE) || $conf->global->ACCOUNTING_ACCOUNT_SUSPENSE == '-1') {
1148  print '<span class="error">'.$langs->trans('UnknownAccountForThirdpartyAndWaitingAccountNotDefinedBlocking').'</span>';
1149  } else {
1150  print '<span class="warning">'.$langs->trans('UnknownAccountForThirdparty', length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE)).'</span>'; // We will use a waiting account
1151  }
1152  } else {
1153  // We will refuse writing
1154  $errorstring = 'UnknownAccountForThirdpartyBlocking';
1155  if ($tabtype[$key] == 'payment') {
1156  $errorstring = 'MainAccountForCustomersNotDefined';
1157  }
1158  if ($tabtype[$key] == 'payment_supplier') {
1159  $errorstring = 'MainAccountForSuppliersNotDefined';
1160  }
1161  if ($tabtype[$key] == 'payment_expensereport') {
1162  $errorstring = 'MainAccountForUsersNotDefined';
1163  }
1164  if ($tabtype[$key] == 'payment_salary') {
1165  $errorstring = 'MainAccountForUsersNotDefined';
1166  }
1167  if ($tabtype[$key] == 'payment_vat') {
1168  $errorstring = 'MainAccountForVatPaymentNotDefined';
1169  }
1170  if ($tabtype[$key] == 'member') {
1171  $errorstring = 'MainAccountForSubscriptionPaymentNotDefined';
1172  }
1173  print '<span class="error">'.$langs->trans($errorstring).'</span>';
1174  }
1175  } else {
1176  print $accounttoshow;
1177  }
1178  print "</td>";
1179 
1180  // Subledger account
1181  print "<td>";
1182  if (in_array($tabtype[$key], array('payment', 'payment_supplier', 'payment_expensereport', 'payment_salary', 'payment_various'))) { // Type of payment with subledger
1183  $accounttoshowsubledger = length_accounta($k);
1184  if ($accounttoshow != $accounttoshowsubledger) {
1185  if (empty($accounttoshowsubledger) || $accounttoshowsubledger == 'NotDefined') {
1186  /*var_dump($tabpay[$key]);
1187  var_dump($tabtype[$key]);
1188  var_dump($tabbq[$key]);*/
1189  //print '<span class="error">'.$langs->trans("ThirdpartyAccountNotDefined").'</span>';
1190  if (!empty($tabcompany[$key]['code_compta'])) {
1191  if (in_array($tabtype[$key], array('payment_various'))) {
1192  // For such case, if subledger is not defined, we won't use subledger accounts.
1193  print '<span class="warning">'.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknownSubledgerIgnored").'</span>';
1194  } else {
1195  print '<span class="warning">'.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknown", $tabcompany[$key]['code_compta']).'</span>';
1196  }
1197  } else {
1198  print '<span class="error">'.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknownBlocking").'</span>';
1199  }
1200  } else {
1201  print $accounttoshowsubledger;
1202  }
1203  }
1204  }
1205  print "</td>";
1206  print "<td>".$reflabel."</td>";
1207  print '<td class="center">'.$val["type_payment"]."</td>";
1208  print '<td class="right nowraponall">'.($mt < 0 ? price(-$mt) : '')."</td>";
1209  print '<td class="right nowraponall">'.($mt >= 0 ? price($mt) : '')."</td>";
1210  print "</tr>";
1211  }
1212  }
1213  } else { // Waiting account
1214  foreach ($tabbq[$key] as $k => $mt) {
1215  if ($mt) {
1216  $reflabel = '';
1217  if (!empty($val['lib'])) {
1218  $reflabel .= $val['lib']." - ";
1219  }
1220  $reflabel .= 'WaitingAccount';
1221 
1222  print '<!-- Wait bank.rowid='.$key.' -->';
1223  print '<tr class="oddeven">';
1224  print "<td>".$date."</td>";
1225  print "<td>".$ref."</td>";
1226  // Ledger account
1227  print "<td>";
1228  /*if (empty($accounttoshow) || $accounttoshow == 'NotDefined')
1229  {
1230  print '<span class="error">'.$langs->trans("WaitAccountNotDefined").'</span>';
1231  }
1232  else */ print length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE);
1233  print "</td>";
1234  // Subledger account
1235  print "<td>";
1236  /*if (empty($accounttoshowsubledger) || $accounttoshowsubledger == 'NotDefined')
1237  {
1238  print '<span class="error">'.$langs->trans("WaitAccountNotDefined").'</span>';
1239  }
1240  else print length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE);
1241  */
1242  print "</td>";
1243  print "<td>".$reflabel."</td>";
1244  print '<td class="center">'.$val["type_payment"]."</td>";
1245  print '<td class="right nowraponall">'.($mt < 0 ? price(-$mt) : '')."</td>";
1246  print '<td class="right nowraponall">'.($mt >= 0 ? price($mt) : '')."</td>";
1247  print "</tr>";
1248  }
1249  }
1250  }
1251  }
1252 
1253  print "</table>";
1254  print '</div>';
1255 
1256  llxFooter();
1257 }
1258 
1259 $db->close();
1260 
1261 
1262 
1270 function getSourceDocRef($val, $typerecord)
1271 {
1272  global $db, $langs;
1273 
1274  // Defined the docref into $ref (We start with $val['ref'] by default and we complete according to other data)
1275  // WE MUST HAVE SAME REF FOR ALL LINES WE WILL RECORD INTO THE BOOKKEEPING
1276  $ref = $val['ref'];
1277  if ($ref == '(SupplierInvoicePayment)' || $ref == '(SupplierInvoicePaymentBack)') {
1278  $ref = $langs->transnoentitiesnoconv('Supplier');
1279  }
1280  if ($ref == '(CustomerInvoicePayment)' || $ref == '(CustomerInvoicePaymentBack)') {
1281  $ref = $langs->transnoentitiesnoconv('Customer');
1282  }
1283  if ($ref == '(SocialContributionPayment)') {
1284  $ref = $langs->transnoentitiesnoconv('SocialContribution');
1285  }
1286  if ($ref == '(DonationPayment)') {
1287  $ref = $langs->transnoentitiesnoconv('Donation');
1288  }
1289  if ($ref == '(SubscriptionPayment)') {
1290  $ref = $langs->transnoentitiesnoconv('Subscription');
1291  }
1292  if ($ref == '(ExpenseReportPayment)') {
1293  $ref = $langs->transnoentitiesnoconv('Employee');
1294  }
1295  if ($ref == '(LoanPayment)') {
1296  $ref = $langs->transnoentitiesnoconv('Loan');
1297  }
1298  if ($ref == '(payment_salary)') {
1299  $ref = $langs->transnoentitiesnoconv('Employee');
1300  }
1301 
1302  $sqlmid = '';
1303  if ($typerecord == 'payment') {
1304  $sqlmid = 'SELECT payfac.fk_facture as id, f.ref as ref';
1305  $sqlmid .= " FROM ".MAIN_DB_PREFIX."paiement_facture as payfac, ".MAIN_DB_PREFIX."facture as f";
1306  $sqlmid .= " WHERE payfac.fk_facture = f.rowid AND payfac.fk_paiement=".$val["paymentid"];
1307  $ref = $langs->transnoentitiesnoconv("Invoice");
1308  } elseif ($typerecord == 'payment_supplier') {
1309  $sqlmid = 'SELECT payfac.fk_facturefourn as id, f.ref';
1310  $sqlmid .= " FROM ".MAIN_DB_PREFIX."paiementfourn_facturefourn as payfac, ".MAIN_DB_PREFIX."facture_fourn as f";
1311  $sqlmid .= " WHERE payfac.fk_facturefourn = f.rowid AND payfac.fk_paiementfourn=".$val["paymentsupplierid"];
1312  $ref = $langs->transnoentitiesnoconv("SupplierInvoice");
1313  } elseif ($typerecord == 'payment_expensereport') {
1314  $sqlmid = 'SELECT e.rowid as id, e.ref';
1315  $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_expensereport as pe, ".MAIN_DB_PREFIX."expensereport as e";
1316  $sqlmid .= " WHERE pe.rowid=".$val["paymentexpensereport"]." AND pe.fk_expensereport = e.rowid";
1317  $ref = $langs->transnoentitiesnoconv("ExpenseReport");
1318  } elseif ($typerecord == 'payment_salary') {
1319  $sqlmid = 'SELECT s.rowid as ref';
1320  $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_salary as s";
1321  $sqlmid .= " WHERE s.rowid=".$val["paymentsalid"];
1322  $ref = $langs->transnoentitiesnoconv("SalaryPayment");
1323  } elseif ($typerecord == 'sc') {
1324  $sqlmid = 'SELECT sc.rowid as ref';
1325  $sqlmid .= " FROM ".MAIN_DB_PREFIX."paiementcharge as sc";
1326  $sqlmid .= " WHERE sc.rowid=".$val["paymentscid"];
1327  $ref = $langs->transnoentitiesnoconv("SocialContribution");
1328  } elseif ($typerecord == 'payment_vat') {
1329  $sqlmid = 'SELECT v.rowid as ref';
1330  $sqlmid .= " FROM ".MAIN_DB_PREFIX."tva as v";
1331  $sqlmid .= " WHERE v.rowid=".$val["paymentvatid"];
1332  $ref = $langs->transnoentitiesnoconv("PaymentVat");
1333  } elseif ($typerecord == 'payment_donation') {
1334  $sqlmid = 'SELECT payd.fk_donation as ref';
1335  $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_donation as payd";
1336  $sqlmid .= " WHERE payd.fk_donation=".$val["paymentdonationid"];
1337  $ref = $langs->transnoentitiesnoconv("Donation");
1338  } elseif ($typerecord == 'payment_loan') {
1339  $sqlmid = 'SELECT l.rowid as ref';
1340  $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_loan as l";
1341  $sqlmid .= " WHERE l.rowid=".$val["paymentloanid"];
1342  $ref = $langs->transnoentitiesnoconv("LoanPayment");
1343  } elseif ($typerecord == 'payment_various') {
1344  $sqlmid = 'SELECT v.rowid as ref';
1345  $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_various as v";
1346  $sqlmid .= " WHERE v.rowid=".$val["paymentvariousid"];
1347  $ref = $langs->transnoentitiesnoconv("VariousPayment");
1348  }
1349  // Add warning
1350  if (empty($sqlmid)) {
1351  dol_syslog("Found a typerecord=".$typerecord." not supported", LOG_WARNING);
1352  }
1353 
1354  if ($sqlmid) {
1355  dol_syslog("accountancy/journal/bankjournal.php::sqlmid=".$sqlmid, LOG_DEBUG);
1356  $resultmid = $db->query($sqlmid);
1357  if ($resultmid) {
1358  while ($objmid = $db->fetch_object($resultmid)) {
1359  $ref .= ' '.$objmid->ref;
1360  }
1361  } else {
1362  dol_print_error($db);
1363  }
1364  }
1365 
1366  $ref = dol_trunc($langs->transnoentitiesnoconv("BankId").' '.$val['fk_bank'].' - '.$ref, 295); // 295 + 3 dots (...) is < than max size of 300
1367  return $ref;
1368 }
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto= 'UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
Loan.
Definition: loan.class.php:30
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
Class to manage various payments.
Class to manage salary payments.
dol_now($mode= 'auto')
Return date for now.
getDefaultDatesForTransfer()
Return Default dates for transfer based on periodicity option in accountancy setup.
Class to manage Dolibarr users.
Definition: user.class.php:44
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:481
Class to manage bank transaction lines.
Class to manage suppliers invoices.
journalHead($nom, $variante, $period, $periodlink, $description, $builddate, $exportlink= '', $moreparam=array(), $calcmode= '', $varlink= '')
Show header of a page used to transfer/dispatch data in accounting.
Class to manage bank accounts.
Put here description of your class.
Definition: tva.class.php:34
price($amount, $form=0, $outlangs= '', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code= '')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
Class to manage Ledger (General Ledger and Subledger)
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
Class to manage third parties objects (customers, suppliers, prospects...)
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Class to manage payments of loans.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Class to manage payments of customer invoices.
accessforbidden($message= '', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
length_accounta($accounta)
Return Auxiliary accounting account of thirdparties with defined length.
getSourceDocRef($val, $typerecord)
Return source for doc_ref of a bank transaction.
Class to manage Trips and Expenses.
Class to manage subscriptions of foundation members.
Class to manage payments of donations.
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:498
print
Draft customers invoices.
Definition: index.php:89
Class to manage donations.
Definition: don.class.php:37
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Class to manage payments of expense report.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc= 'right', $stringencoding= 'UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
Class to manage accounting accounts.
Class to manage accounting accounts.
Class to manage invoices.
Class to manage payments for supplier invoices.
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous) ...
llxFooter()
Empty footer.
Definition: wrapper.php:59
Classe permettant la gestion des paiements des charges La tva collectee n&#39;est calculee que sur les fa...
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.