bankjournal.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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@dolibarr.fr>
  6. * Copyright (C) 2013 Christophe Battarel <christophe.battarel@altairis.fr>
  7. * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro@zendsi.com>
  8. * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
  9. * Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * \file htdocs/accountancy/journal/bankjournal.php
  26. * \ingroup Advanced accountancy
  27. * \brief Page with bank journal
  28. */
  29. require '../../main.inc.php';
  30. // Class
  31. require_once DOL_DOCUMENT_ROOT . '/core/lib/report.lib.php';
  32. require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
  33. require_once DOL_DOCUMENT_ROOT . '/core/lib/bank.lib.php';
  34. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  35. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingjournal.class.php';
  36. require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  37. require_once DOL_DOCUMENT_ROOT . '/user/class/user.class.php';
  38. require_once DOL_DOCUMENT_ROOT . '/adherents/class/adherent.class.php';
  39. require_once DOL_DOCUMENT_ROOT . '/compta/sociales/class/chargesociales.class.php';
  40. require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php';
  41. require_once DOL_DOCUMENT_ROOT . '/don/class/don.class.php';
  42. require_once DOL_DOCUMENT_ROOT . '/don/class/paymentdonation.class.php';
  43. require_once DOL_DOCUMENT_ROOT . '/compta/tva/class/tva.class.php';
  44. require_once DOL_DOCUMENT_ROOT . '/compta/salaries/class/paymentsalary.class.php';
  45. require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
  46. require_once DOL_DOCUMENT_ROOT . '/fourn/class/paiementfourn.class.php';
  47. require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
  48. require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.class.php';
  49. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php';
  50. require_once DOL_DOCUMENT_ROOT . '/societe/class/client.class.php';
  51. require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php';
  52. require_once DOL_DOCUMENT_ROOT . '/expensereport/class/paymentexpensereport.class.php';
  53. // Langs
  54. $langs->load("companies");
  55. $langs->load("other");
  56. $langs->load("compta");
  57. $langs->load("banks");
  58. $langs->load('bills');
  59. $langs->load('donations');
  60. $langs->load("accountancy");
  61. $langs->load("trips");
  62. $langs->load("salaries");
  63. // Multi journal
  64. $id_journal = GETPOST('id_journal', 'int');
  65. $date_startmonth = GETPOST('date_startmonth');
  66. $date_startday = GETPOST('date_startday');
  67. $date_startyear = GETPOST('date_startyear');
  68. $date_endmonth = GETPOST('date_endmonth');
  69. $date_endday = GETPOST('date_endday');
  70. $date_endyear = GETPOST('date_endyear');
  71. $action = GETPOST('action','aZ09');
  72. $now = dol_now();
  73. // Security check
  74. if ($user->societe_id > 0 && empty($id_journal))
  75. accessforbidden();
  76. /*
  77. * Actions
  78. */
  79. $error = 0;
  80. $year_current = strftime("%Y", dol_now());
  81. $pastmonth = strftime("%m", dol_now()) - 1;
  82. $pastmonthyear = $year_current;
  83. if ($pastmonth == 0) {
  84. $pastmonth = 12;
  85. $pastmonthyear --;
  86. }
  87. $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  88. $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  89. if (empty($date_start) || empty($date_end)) // We define date_start and date_end
  90. {
  91. $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
  92. $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
  93. }
  94. $p = explode(":", $conf->global->MAIN_INFO_SOCIETE_COUNTRY);
  95. $idpays = $p[0];
  96. $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,";
  97. $sql .= " ba.courant, ba.ref as baref, ba.account_number, ba.fk_accountancy_journal,";
  98. $sql .= " soc.code_compta, soc.code_compta_fournisseur, soc.rowid as socid, soc.nom as name, bu1.type as typeop,";
  99. $sql .= " u.accountancy_code, u.rowid as userid, u.lastname as lastname, u.firstname as firstname, bu2.type as typeop";
  100. $sql .= " FROM " . MAIN_DB_PREFIX . "bank as b";
  101. $sql .= " JOIN " . MAIN_DB_PREFIX . "bank_account as ba on b.fk_account=ba.rowid";
  102. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu1 ON bu1.fk_bank = b.rowid AND bu1.type='company'";
  103. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu2 ON bu2.fk_bank = b.rowid AND bu2.type='user'";
  104. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as soc on bu1.url_id=soc.rowid";
  105. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as u on bu2.url_id=u.rowid";
  106. $sql .= " WHERE ba.fk_accountancy_journal=" . $id_journal;
  107. $sql .= ' AND ba.entity IN ('.getEntity('bank_account', 0).')'; // We don't share object for accountancy
  108. if ($date_start && $date_end)
  109. $sql .= " AND b.dateo >= '" . $db->idate($date_start) . "' AND b.dateo <= '" . $db->idate($date_end) . "'";
  110. $sql .= " ORDER BY b.datev";
  111. $object = new Account($db);
  112. $paymentstatic = new Paiement($db);
  113. $paymentsupplierstatic = new PaiementFourn($db);
  114. $societestatic = new Societe($db);
  115. $userstatic = new User($db);
  116. $chargestatic = new ChargeSociales($db);
  117. $paymentdonstatic = new PaymentDonation($db);
  118. $paymentvatstatic = new TVA($db);
  119. $paymentsalstatic = new PaymentSalary($db);
  120. $paymentexpensereportstatic = new PaymentExpenseReport($db);
  121. // Get code of finance journal
  122. $accountingjournalstatic = new AccountingJournal($db);
  123. $accountingjournalstatic->fetch($id_journal);
  124. $journal = $accountingjournalstatic->code;
  125. dol_syslog("accountancy/journal/bankjournal.php", LOG_DEBUG);
  126. $result = $db->query($sql);
  127. if ($result) {
  128. $num = $db->num_rows($result);
  129. // Variables
  130. $account_supplier = (! empty($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER) ? $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER : $langs->trans("CodeNotDef"));
  131. $account_customer = (! empty($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER) ? $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER : $langs->trans("CodeNotDef"));
  132. $account_employee = (! empty($conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT) ? $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT : $langs->trans("CodeNotDef"));
  133. $account_pay_vat = (! empty($conf->global->ACCOUNTING_VAT_PAY_ACCOUNT) ? $conf->global->ACCOUNTING_VAT_PAY_ACCOUNT : $langs->trans("CodeNotDef"));
  134. $account_pay_donation = (! empty($conf->global->DONATION_ACCOUNTINGACCOUNT) ? $conf->global->DONATION_ACCOUNTINGACCOUNT : $langs->trans("CodeNotDef"));
  135. $account_transfer = (! empty($conf->global->ACCOUNTING_ACCOUNT_TRANSFER_CASH) ? $conf->global->ACCOUNTING_ACCOUNT_TRANSFER_CASH : $langs->trans("CodeNotDef"));
  136. $tabcompany = array();
  137. $tabuser = array();
  138. $tabpay = array ();
  139. $tabbq = array ();
  140. $tabtp = array ();
  141. $tabtype = array ();
  142. // Loop on each line into bank account. For each line, we should get:
  143. // on line tabpay = line into bank
  144. // one line for bank jounral = tabbq
  145. // one line for thirdparty journal = tabtp
  146. $i = 0;
  147. while ( $i < $num )
  148. {
  149. $obj = $db->fetch_object($result);
  150. // Set accountancy code (for bank and thirdparty)
  151. $compta_bank = $obj->account_number;
  152. if ($obj->label == '(SupplierInvoicePayment)')
  153. $compta_soc = (! empty($obj->code_compta_fournisseur) ? $obj->code_compta_fournisseur : $account_supplier);
  154. if ($obj->label == '(CustomerInvoicePayment)')
  155. $compta_soc = (! empty($obj->code_compta) ? $obj->code_compta : $account_customer);
  156. $tabcompany[$obj->rowid] = array (
  157. 'id' => $obj->socid,
  158. 'name' => $obj->name,
  159. 'code_compta' => $compta_soc,
  160. );
  161. $compta_user = (! empty($obj->accountancy_code) ? $obj->accountancy_code : $account_employee);
  162. $tabuser[$obj->rowid] = array (
  163. 'id' => $obj->userid,
  164. 'name' => dolGetFirstLastname($obj->firstname, $obj->lastname),
  165. 'lastname' => $obj->lastname,
  166. 'firstname' => $obj->firstname,
  167. 'accountancy_code' => $compta_user,
  168. );
  169. // Variable bookkeeping
  170. $tabpay[$obj->rowid]["date"] = $obj->do;
  171. $tabpay[$obj->rowid]["type_payment"] = $obj->fk_type;
  172. $tabpay[$obj->rowid]["ref"] = $obj->label;
  173. $tabpay[$obj->rowid]["fk_bank"] = $obj->rowid;
  174. if (preg_match('/^\((.*)\)$/i', $obj->label, $reg)) {
  175. $tabpay[$obj->rowid]["lib"] = $langs->trans($reg[1]);
  176. } else {
  177. $tabpay[$obj->rowid]["lib"] = dol_trunc($obj->label, 60);
  178. }
  179. $links = $object->get_url($obj->rowid);
  180. // get_url may return -1 which is not traversable
  181. if (is_array($links)) {
  182. // Now loop on each link of record in bank.
  183. foreach ( $links as $key => $val ) {
  184. if (in_array($links[$key]['type'], array('sc', 'payment_sc', 'payment', 'payment_supplier', 'payment_vat', 'payment_expensereport', 'banktransfert', 'payment_donation', 'payment_salary'))) // So we excluded 'company' here
  185. {
  186. // We save tabtype for a future use, to remember what kind of payment it is
  187. $tabtype[$obj->rowid] = $links[$key]['type'];
  188. }
  189. if ($links[$key]['type'] == 'payment') {
  190. $paymentstatic->id = $links[$key]['url_id'];
  191. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentstatic->getNomUrl(2);
  192. $tabpay[$obj->rowid]["paymentid"] = $paymentstatic->id;
  193. } else if ($links[$key]['type'] == 'payment_supplier') {
  194. $paymentsupplierstatic->id = $links[$key]['url_id'];
  195. $paymentsupplierstatic->ref = $links[$key]['url_id'];
  196. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentsupplierstatic->getNomUrl(2);
  197. $tabpay[$obj->rowid]["paymentsupplierid"] = $paymentsupplierstatic->id;
  198. } else if ($links[$key]['type'] == 'company') {
  199. $societestatic->id = $links[$key]['url_id'];
  200. $societestatic->name = $links[$key]['label'];
  201. $tabpay[$obj->rowid]["soclib"] = $societestatic->getNomUrl(1, '', 30);
  202. $tabtp[$obj->rowid][$compta_soc] += $obj->amount;
  203. } else if ($links[$key]['type'] == 'user') {
  204. $userstatic->id = $links[$key]['url_id'];
  205. $userstatic->name = $links[$key]['label'];
  206. $tabpay[$obj->rowid]["soclib"] = $userstatic->getNomUrl(1, '', 30);
  207. $tabtp[$obj->rowid][$compta_user] += $obj->amount;
  208. } else if ($links[$key]['type'] == 'sc') {
  209. $chargestatic->id = $links[$key]['url_id'];
  210. $chargestatic->ref = $links[$key]['url_id'];
  211. $tabpay[$obj->rowid]["lib"] .= ' ' . $chargestatic->getNomUrl(2);
  212. if (preg_match('/^\((.*)\)$/i', $links[$key]['label'], $reg)) {
  213. if ($reg[1] == 'socialcontribution')
  214. $reg[1] = 'SocialContribution';
  215. $chargestatic->lib = $langs->trans($reg[1]);
  216. } else {
  217. $chargestatic->lib = $links[$key]['label'];
  218. }
  219. $chargestatic->ref = $chargestatic->lib;
  220. $tabpay[$obj->rowid]["soclib"] = $chargestatic->getNomUrl(1, 30);
  221. $sqlmid = 'SELECT cchgsoc.accountancy_code';
  222. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "c_chargesociales cchgsoc ";
  223. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "chargesociales as chgsoc ON chgsoc.fk_type=cchgsoc.id";
  224. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementcharge as paycharg ON paycharg.fk_charge=chgsoc.rowid";
  225. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "bank_url as bkurl ON bkurl.url_id=paycharg.rowid";
  226. $sqlmid .= " WHERE bkurl.fk_bank=" . $obj->rowid;
  227. dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
  228. $resultmid = $db->query($sqlmid);
  229. if ($resultmid) {
  230. $objmid = $db->fetch_object($resultmid);
  231. $tabtp[$obj->rowid][$objmid->accountancy_code] += $obj->amount;
  232. }
  233. } else if ($links[$key]['type'] == 'payment_donation') {
  234. $paymentdonstatic->id = $links[$key]['url_id'];
  235. $paymentdonstatic->fk_donation = $links[$key]['url_id'];
  236. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentdonstatic->getNomUrl(2);
  237. $tabpay[$obj->rowid]["paymentdonationid"] = $paymentdonstatic->id;
  238. $tabtp[$obj->rowid][$account_pay_donation] += $obj->amount;
  239. } else if ($links[$key]['type'] == 'payment_vat') {
  240. $paymentvatstatic->id = $links[$key]['url_id'];
  241. $paymentvatstatic->ref = $links[$key]['url_id'];
  242. $paymentvatstatic->label = $links[$key]['label'];
  243. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentvatstatic->getNomUrl(2);
  244. $tabpay[$obj->rowid]["paymentvatid"] = $paymentvatstatic->id;
  245. $tabtp[$obj->rowid][$account_pay_vat] += $obj->amount;
  246. } else if ($links[$key]['type'] == 'payment_salary') {
  247. $paymentsalstatic->id = $links[$key]['url_id'];
  248. $paymentsalstatic->ref = $links[$key]['url_id'];
  249. $paymentsalstatic->label = $links[$key]['label'];
  250. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentsalstatic->getNomUrl(2);
  251. $tabpay[$obj->rowid]["paymentsalid"] = $paymentsalstatic->id;
  252. } else if ($links[$key]['type'] == 'payment_expensereport') {
  253. $paymentexpensereportstatic->id = $links[$key]['url_id'];
  254. $paymentexpensereportstatic->fk_expensereport = $links[$key]['url_id'];
  255. $tabpay[$obj->rowid]["lib"] .= ' ' . $paymentexpensereportstatic->getNomUrl(2);
  256. $tabpay[$obj->rowid]["fk_expensereport"] = $paymentexpensereportstatic->id;
  257. } else if ($links[$key]['type'] == 'banktransfert') {
  258. $tabpay[$obj->rowid]["lib"] .= ' ' . $langs->trans("BankTransfer");
  259. $tabtp[$obj->rowid][$account_transfer] += $obj->amount;
  260. }
  261. }
  262. }
  263. $tabbq[$obj->rowid][$compta_bank] += $obj->amount;
  264. // Check account number is ok
  265. /*if ($action == 'writebookkeeping') // Make test now in such a case
  266. {
  267. reset($tabbq[$obj->rowid]);
  268. $first_key_tabbq = key($tabbq[$obj->rowid]);
  269. if (empty($first_key_tabbq))
  270. {
  271. $error++;
  272. setEventMessages($langs->trans('ErrorAccountancyCodeOnBankAccountNotDefined', $obj->baref), null, 'errors');
  273. }
  274. reset($tabtp[$obj->rowid]);
  275. $first_key_tabtp = key($tabtp[$obj->rowid]);
  276. if (empty($first_key_tabtp))
  277. {
  278. $error++;
  279. setEventMessages($langs->trans('ErrorAccountancyCodeOnThirdPartyNotDefined'), null, 'errors');
  280. }
  281. }*/
  282. // if($obj->socid)$tabtp[$obj->rowid][$compta_soc] += $obj->amount;
  283. $i ++;
  284. }
  285. } else {
  286. dol_print_error($db);
  287. }
  288. /*
  289. var_dump($tabpay);
  290. var_dump($tabbq);
  291. var_dump($tabtp);
  292. */
  293. // Write bookkeeping
  294. if (! $error && $action == 'writebookkeeping') {
  295. $now = dol_now();
  296. $error = 0;
  297. foreach ( $tabpay as $key => $val ) { // $key is rowid into llx_bank
  298. $errorforline = 0;
  299. $db->begin();
  300. // Bank
  301. if (! $errorforline)
  302. {
  303. // Line into bank account
  304. foreach ( $tabbq[$key] as $k => $mt )
  305. {
  306. if ($mt) {
  307. $bookkeeping = new BookKeeping($db);
  308. $bookkeeping->doc_date = $val["date"];
  309. $bookkeeping->doc_ref = $val["ref"];
  310. $bookkeeping->doc_type = 'bank';
  311. $bookkeeping->fk_doc = $key;
  312. $bookkeeping->fk_docdet = $val["fk_bank"];
  313. $bookkeeping->numero_compte = $k;
  314. $bookkeeping->label_compte = $compte->label;
  315. $bookkeeping->montant = ($mt < 0 ? - $mt : $mt);
  316. $bookkeeping->sens = ($mt >= 0) ? 'D' : 'C';
  317. $bookkeeping->debit = ($mt >= 0 ? $mt : 0);
  318. $bookkeeping->credit = ($mt < 0 ? - $mt : 0);
  319. $bookkeeping->code_journal = $journal;
  320. $bookkeeping->fk_user_author = $user->id;
  321. $bookkeeping->date_create = $now;
  322. if ($tabtype[$key] == 'payment') {
  323. $bookkeeping->code_tiers = $tabcompany[$key]['code_compta'];
  324. $sqlmid = 'SELECT fac.facnumber';
  325. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture fac";
  326. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON payfac.fk_facture=fac.rowid";
  327. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement as pay ON payfac.fk_paiement=pay.rowid";
  328. $sqlmid .= " WHERE pay.fk_bank=" . $key;
  329. dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
  330. $resultmid = $db->query($sqlmid);
  331. if ($resultmid) {
  332. $objmid = $db->fetch_object($resultmid);
  333. $bookkeeping->doc_ref = $objmid->facnumber; // Ref of invoice
  334. }
  335. } else if ($tabtype[$key] == 'payment_supplier') {
  336. $bookkeeping->code_tiers = $tabcompany[$key]['code_compta'];
  337. $sqlmid = 'SELECT facf.ref_supplier, facf.ref';
  338. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf";
  339. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid";
  340. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid";
  341. $sqlmid .= " WHERE payf.fk_bank=" . $key;
  342. dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
  343. $resultmid = $db->query($sqlmid);
  344. if ($resultmid) {
  345. $objmid = $db->fetch_object($resultmid);
  346. $bookkeeping->doc_ref = $objmid->ref_supplier . ' (' . $objmid->ref . ')'; // Ref on invoice
  347. }
  348. } else if ($tabtype[$key] == 'payment_expensereport') {
  349. $bookkeeping->code_tiers = $tabuser[$key]['accountancy_code'];
  350. $sqlmid = 'SELECT e.ref';
  351. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "expensereport as e";
  352. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "payment_expensereport as payer ON payer.fk_expensereport=e.rowid";
  353. $sqlmid .= " WHERE payer.fk_expensereport=" . $val["fk_expensereport"];
  354. dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
  355. $resultmid = $db->query($sqlmid);
  356. if ($resultmid) {
  357. $objmid = $db->fetch_object($resultmid);
  358. $bookkeeping->doc_ref = $objmid->ref; // Ref of expensereport
  359. }
  360. } else if ($tabtype[$key] == 'payment_vat') {
  361. $bookkeeping->code_tiers = '';
  362. $bookkeeping->doc_ref = $langs->trans("PaymentVat") . ' (' . $val["paymentvatid"] . ')'; // Rowid of vat payment
  363. } else if ($tabtype[$key] == 'payment_donation') {
  364. $bookkeeping->code_tiers = '';
  365. $bookkeeping->doc_ref = $langs->trans("Donation") . ' (' . $val["paymentdonationid"] . ')'; // Rowid of donation
  366. } else if ($tabtype[$key] == 'payment_salary') {
  367. $bookkeeping->code_tiers = '';
  368. $bookkeeping->label_compte = $tabuser[$key]['name'];
  369. $bookkeeping->doc_ref = $langs->trans("SalaryPayment") . ' (' . $val["paymentsalid"] . ')'; // Ref of salary payment
  370. }
  371. $result = $bookkeeping->create($user);
  372. if ($result < 0) {
  373. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  374. {
  375. $error++;
  376. $errorforline++;
  377. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->doc_ref.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  378. }
  379. else
  380. {
  381. $error++;
  382. $errorforline++;
  383. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  384. }
  385. }
  386. }
  387. }
  388. }
  389. // Third party
  390. if (! $errorforline)
  391. {
  392. // Line into thirdparty account
  393. foreach ( $tabtp[$key] as $k => $mt ) {
  394. if ($mt) {
  395. $bookkeeping = new BookKeeping($db);
  396. $bookkeeping->doc_date = $val["date"];
  397. $bookkeeping->doc_ref = $val["ref"];
  398. $bookkeeping->doc_type = 'bank';
  399. $bookkeeping->fk_doc = $key;
  400. $bookkeeping->fk_docdet = $val["fk_bank"];
  401. $bookkeeping->label_compte = $tabcompany[$key]['name'];
  402. $bookkeeping->montant = ($mt < 0 ? - $mt : $mt);
  403. $bookkeeping->sens = ($mt < 0) ? 'D' : 'C';
  404. $bookkeeping->debit = ($mt < 0 ? - $mt : 0);
  405. $bookkeeping->credit = ($mt >= 0) ? $mt : 0;
  406. $bookkeeping->code_journal = $journal;
  407. $bookkeeping->fk_user_author = $user->id;
  408. $bookkeeping->date_create = $now;
  409. if (in_array($tabtype[$key], array('sc', 'payment_sc'))) { // If payment is payment of social contribution
  410. $bookkeeping->code_tiers = '';
  411. $bookkeeping->numero_compte = $k;
  412. } else if ($tabtype[$key] == 'payment') { // If payment is payment of customer invoice, we get ref of invoice
  413. $sqlmid = 'SELECT fac.facnumber';
  414. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture fac ";
  415. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON payfac.fk_facture=fac.rowid";
  416. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement as pay ON payfac.fk_paiement=pay.rowid";
  417. $sqlmid .= " WHERE pay.fk_bank=" . $key;
  418. dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
  419. $resultmid = $db->query($sqlmid);
  420. if ($resultmid) {
  421. $objmid = $db->fetch_object($resultmid);
  422. $bookkeeping->doc_ref = $objmid->facnumber;
  423. }
  424. $bookkeeping->code_tiers = $tabcompany[$key]['code_compta'];
  425. $bookkeeping->numero_compte = $k;
  426. } else if ($tabtype[$key] == 'payment_supplier') { // If payment is payment of supplier invoice, we get ref of invoice
  427. $sqlmid = 'SELECT facf.ref_supplier,facf.ref';
  428. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf ";
  429. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid";
  430. $sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid";
  431. $sqlmid .= " WHERE payf.fk_bank=" . $key;
  432. dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
  433. $resultmid = $db->query($sqlmid);
  434. if ($resultmid) {
  435. $objmid = $db->fetch_object($resultmid);
  436. $bookkeeping->doc_ref = $objmid->ref_supplier . ' (' . $objmid->ref . ')';
  437. }
  438. $bookkeeping->code_tiers = $tabcompany[$key]['code_compta'];
  439. $bookkeeping->numero_compte = $k;
  440. } else if ($tabtype[$key] == 'payment_vat') {
  441. $bookkeeping->code_tiers = '';
  442. $bookkeeping->numero_compte = $k;
  443. $bookkeeping->doc_ref = $langs->trans("PaymentVat") . ' (' . $val["paymentvatid"] . ')'; // Rowid of vat
  444. } else if ($tabtype[$key] == 'payment_donation') {
  445. $bookkeeping->code_tiers = '';
  446. $bookkeeping->numero_compte = $k;
  447. $bookkeeping->doc_ref = $langs->trans("Donation") . ' (' . $val["paymentdonationid"] . ')'; // Rowid of donation
  448. } else if ($tabtype[$key] == 'payment_salary') {
  449. $bookkeeping->code_tiers = $tabuser[$key]['accountancy_code'];
  450. $bookkeeping->numero_compte = $conf->global->SALARIES_ACCOUNTING_ACCOUNT_PAYMENT;
  451. $bookkeeping->label_compte = $tabuser[$key]['name'];
  452. $bookkeeping->doc_ref = $langs->trans("SalaryPayment") . ' (' . $val["paymentsalid"] . ')'; // Rowid of salary payment
  453. } else if ($tabtype[$key] == 'banktransfert') {
  454. $bookkeeping->code_tiers = '';
  455. $bookkeeping->numero_compte = $k;
  456. } else {
  457. // FIXME Should be a temporary account ???
  458. $bookkeeping->doc_ref = $k;
  459. //$bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER;
  460. $bookkeeping->numero_compte = 'CodeNotDef';
  461. }
  462. $result = $bookkeeping->create($user);
  463. if ($result < 0) {
  464. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  465. {
  466. $error++;
  467. $errorforline++;
  468. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->doc_ref.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  469. }
  470. else
  471. {
  472. $error++;
  473. $errorforline++;
  474. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  475. }
  476. }
  477. }
  478. }
  479. }
  480. if (! $errorforline)
  481. {
  482. $db->commit();
  483. }
  484. else
  485. {
  486. $db->rollback();
  487. }
  488. }
  489. if (empty($error)) {
  490. setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
  491. }
  492. elseif (count($tabpay) == $error)
  493. {
  494. setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
  495. }
  496. else
  497. {
  498. setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
  499. }
  500. $action = '';
  501. }
  502. // Export
  503. /*
  504. if ($action == 'export_csv') {
  505. $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  506. include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
  507. $companystatic = new Client($db);
  508. $userstatic = new User($db);
  509. // Model Cegid Expert Export
  510. if ($conf->global->ACCOUNTING_EXPORT_MODELCSV == 2)
  511. {
  512. $sep = ";";
  513. foreach ( $tabpay as $key => $val ) {
  514. $date = dol_print_date($db->jdate($val["date"]), '%d%m%Y');
  515. $reflabel = $val["ref"];
  516. if ($reflabel == '(SupplierInvoicePayment)') {
  517. $reflabel = $langs->trans('Supplier');
  518. }
  519. if ($reflabel == '(CustomerInvoicePayment)') {
  520. $reflabel = $langs->trans('Customer');
  521. }
  522. if ($reflabel == '(SocialContributionPayment)') {
  523. $reflabel = $langs->trans('SocialContribution');
  524. }
  525. if ($reflabel == '(DonationPayment)') {
  526. $reflabel = $langs->trans('Donation');
  527. }
  528. if ($reflabel == '(SubscriptionPayment)') {
  529. $reflabel = $langs->trans('Subscription');
  530. }
  531. if ($reflabel == '(ExpenseReportPayment)') {
  532. $reflabel = $langs->trans('Employee');
  533. }
  534. $companystatic->id = $tabcompany[$key]['id'];
  535. $companystatic->name = $tabcompany[$key]['name'];
  536. $userstatic->id = $tabuser[$key]['id'];
  537. $userstatic->lastname = $tabuser[$key]['lastname'];
  538. $userstatic->firstname = $tabuser[$key]['firstname'];
  539. // Bank
  540. foreach ( $tabbq[$key] as $k => $mt ) {
  541. print $date . $sep;
  542. print $journal . $sep;
  543. print length_accountg(html_entity_decode($k)) . $sep;
  544. print $sep;
  545. print ($mt < 0 ? 'C' : 'D') . $sep;
  546. print ($mt <= 0 ? price(- $mt) : $mt) . $sep;
  547. if ($companystatic->name == '') {
  548. print $langs->trans('Bank')." - ". utf8_decode($val["ref"]) . $sep;
  549. } else {
  550. print $langs->trans("Bank") .' - '.utf8_decode($companystatic->name) . $sep;
  551. }
  552. print utf8_decode($reflabel) . $sep;
  553. print "\n";
  554. }
  555. // Third party
  556. if (is_array($tabtp[$key])) {
  557. foreach ( $tabtp[$key] as $k => $mt ) {
  558. if ($mt) {
  559. print $date . $sep;
  560. print $journal . $sep;
  561. if ($tabtype[$key] == 'payment') {
  562. print length_accountg($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER) . $sep;
  563. print length_accounta(html_entity_decode($k)) . $sep;
  564. } else if ($tabtype[$key] == 'payment_supplier') {
  565. print length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER) . $sep;
  566. print length_accounta(html_entity_decode($k)) . $sep;
  567. } else {
  568. print length_accountg(html_entity_decode($k)) . $sep;
  569. print $sep;
  570. }
  571. print ($mt < 0 ? 'D' : 'C') . $sep;
  572. print ($mt <= 0 ? price(- $mt) : $mt) . $sep;
  573. if ($companystatic->name == '') {
  574. print $langs->trans('ThirdParty')." - ". utf8_decode($val["ref"]) . $sep;
  575. } else {
  576. print $langs->trans('ThirdParty')." - ". utf8_decode($companystatic->name) . $sep;
  577. }
  578. print utf8_decode($reflabel) . $sep;
  579. print "\n";
  580. }
  581. }
  582. } else {
  583. foreach ( $tabbq[$key] as $k => $mt ) {
  584. print $date . $sep;
  585. print $journal . $sep;
  586. print length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE) . $sep;
  587. print $sep;
  588. print ($mt < 0 ? 'D' : 'C') . $sep;
  589. print ($mt <= 0 ? price(- $mt) : $mt) . $sep;
  590. if ($companystatic->name == '') {
  591. print $langs->trans('ThirdParty')." - ". utf8_decode($val["ref"]) . $sep;
  592. } else {
  593. print $langs->trans('ThirdParty')." - ". utf8_decode($companystatic->name) . $sep;
  594. }
  595. print utf8_decode($reflabel) . $sep;
  596. print "\n";
  597. }
  598. }
  599. }
  600. } else {
  601. // Model Classic Export
  602. foreach ( $tabpay as $key => $val ) {
  603. $date = dol_print_date($db->jdate($val["date"]), 'day');
  604. $companystatic->id = $tabcompany[$key]['id'];
  605. $companystatic->name = $tabcompany[$key]['name'];
  606. // Bank
  607. foreach ( $tabbq[$key] as $k => $mt ) {
  608. print '"' . $journal . '"' . $sep;
  609. print '"' . $date . '"' . $sep;
  610. print '"' . $val["type_payment"] . '"' . $sep;
  611. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  612. if ($companystatic->name == '') {
  613. print '"' . $langs->trans('Bank') . " - " . utf8_decode($val["ref"]) . '"' . $sep;
  614. } else {
  615. print '"' . $langs->trans("Bank") . ' - ' . utf8_decode($companystatic->name) . '"' . $sep;
  616. }
  617. print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep;
  618. print '"' . ($mt < 0 ? price(- $mt) : '') . '"';
  619. print "\n";
  620. }
  621. // Third party
  622. if (is_array($tabtp[$key])) {
  623. foreach ( $tabtp[$key] as $k => $mt ) {
  624. if ($mt) {
  625. print '"' . $journal . '"' . $sep;
  626. print '"' . $date . '"' . $sep;
  627. print '"' . $val["type_payment"] . '"' . $sep;
  628. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  629. if ($companystatic->name == '') {
  630. print '"' . $langs->trans('ThirdParty') . " - " . utf8_decode($val["ref"]) . '"' . $sep;
  631. } else {
  632. print '"' . $langs->trans('ThirdParty') . " - " . utf8_decode($companystatic->name) . '"' . $sep;
  633. }
  634. print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
  635. print '"' . ($mt >= 0 ? price($mt) : '') . '"';
  636. print "\n";
  637. }
  638. }
  639. } else {
  640. foreach ( $tabbq[$key] as $k => $mt ) {
  641. print '"' . $journal . '"' . $sep;
  642. print '"' . $date . '"' . $sep;
  643. print '"' . $val["ref"] . '"' . $sep;
  644. print '"' . length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE) . '"' . $sep;
  645. if ($companystatic->name == '') {
  646. print '"' . $langs->trans("Bank") . ' - ' . utf8_decode($val["ref"]) . '"' . $sep;
  647. } else {
  648. print '"' . $langs->trans("Bank") . ' - ' . utf8_decode($companystatic->name) . '"' . $sep;
  649. }
  650. print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
  651. print '"' . ($mt >= 0 ? price($mt) : '') . '"';
  652. print "\n";
  653. }
  654. }
  655. }
  656. }
  657. }
  658. */
  659. /*
  660. * View
  661. */
  662. $form = new Form($db);
  663. if (empty($action) || $action == 'view') {
  664. $invoicestatic = new Facture($db);
  665. $invoicesupplierstatic = new FactureFournisseur($db);
  666. $expensereportstatic = new ExpenseReport($db);
  667. $vatstatic = new Tva($db);
  668. $donationstatic = new Don($db);
  669. $salarystatic = new PaymentSalary($db);
  670. llxHeader('', $langs->trans("FinanceJournal"));
  671. $nom = $langs->trans("FinanceJournal") . ' - ' . $accountingjournalstatic->getNomUrl(1);
  672. $builddate = time();
  673. //$description = $langs->trans("DescFinanceJournal") . '<br>';
  674. $description.= $langs->trans("DescJournalOnlyBindedVisible").'<br>';
  675. $period = $form->select_date($date_start, 'date_start', 0, 0, 0, '', 1, 0, 1) . ' - ' . $form->select_date($date_end, 'date_end', 0, 0, 0, '', 1, 0, 1);
  676. $varlink = 'id_journal=' . $id_journal;
  677. journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
  678. /*if ($conf->global->ACCOUNTING_EXPORT_MODELCSV != 1 && $conf->global->ACCOUNTING_EXPORT_MODELCSV != 2) {
  679. print '<input type="button" class="butActionRefused" style="float: right;" value="' . $langs->trans('Export') . '" disabled="disabled" title="' . $langs->trans('ExportNotSupported') . '"/>';
  680. } else {
  681. print '<input type="button" class="butAction" style="float: right;" value="' . $langs->trans("Export") . '" onclick="launch_export();" />';
  682. }*/
  683. print '<div class="tabsAction">';
  684. print '<input type="button" class="butAction" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
  685. print '</div>';
  686. // TODO Avoid using js. We can use a direct link with $param
  687. print '
  688. <script type="text/javascript">
  689. function launch_export() {
  690. console.log("Set value into form and submit");
  691. $("div.fiche div.tabBar form input[name=\"action\"]").val("export_csv");
  692. $("div.fiche div.tabBar form input[type=\"submit\"]").click();
  693. $("div.fiche div.tabBar form input[name=\"action\"]").val("");
  694. }
  695. function writebookkeeping() {
  696. console.log("Set value into form and submit");
  697. $("div.fiche div.tabBar form input[name=\"action\"]").val("writebookkeeping");
  698. $("div.fiche div.tabBar form input[type=\"submit\"]").click();
  699. $("div.fiche div.tabBar form input[name=\"action\"]").val("");
  700. }
  701. </script>';
  702. /*
  703. * Show result array
  704. */
  705. print '<br>';
  706. $i = 0;
  707. print "<table class=\"noborder\" width=\"100%\">";
  708. print "<tr class=\"liste_titre\">";
  709. print "<td></td>";
  710. print "<td>" . $langs->trans("Date") . "</td>";
  711. print "<td>" . $langs->trans("Piece") . ' (' . $langs->trans("InvoiceRef") . ")</td>";
  712. print "<td>" . $langs->trans("AccountAccounting") . "</td>";
  713. print "<td>" . $langs->trans("Type") . "</td>";
  714. print "<td>" . $langs->trans("PaymentMode") . "</td>";
  715. print "<td align='right'>" . $langs->trans("Debit") . "</td>";
  716. print "<td align='right'>" . $langs->trans("Credit") . "</td>";
  717. print "</tr>\n";
  718. $r = '';
  719. foreach ( $tabpay as $key => $val ) { // $key is rowid in llx_bank
  720. $date = dol_print_date($db->jdate($val["date"]), 'day');
  721. $reflabel = $val["ref"];
  722. if ($reflabel == '(SupplierInvoicePayment)') {
  723. $reflabel = $langs->trans('Supplier');
  724. }
  725. if ($reflabel == '(CustomerInvoicePayment)') {
  726. $reflabel = $langs->trans('Customer');
  727. }
  728. if ($reflabel == '(SocialContributionPayment)') {
  729. $reflabel = $langs->trans('SocialContribution');
  730. }
  731. if ($reflabel == '(DonationPayment)') {
  732. $reflabel = $langs->trans('Donation');
  733. }
  734. if ($reflabel == '(SubscriptionPayment)') {
  735. $reflabel = $langs->trans('Subscription');
  736. }
  737. if ($reflabel == '(ExpenseReportPayment)') {
  738. $reflabel = $langs->trans('Employee');
  739. }
  740. $ref=$reflabel;
  741. if ($tabtype[$key] == 'payment')
  742. {
  743. $sqlmid = 'SELECT payfac.fk_facture as id';
  744. $sqlmid .= " FROM ".MAIN_DB_PREFIX."paiement_facture as payfac";
  745. $sqlmid .= " WHERE payfac.fk_paiement=" . $val["paymentid"];
  746. dol_syslog("accountancy/journal/bankjournal.php::sqlmid=" . $sqlmid, LOG_DEBUG);
  747. $resultmid = $db->query($sqlmid);
  748. if ($resultmid) {
  749. $objmid = $db->fetch_object($resultmid);
  750. $invoicestatic->fetch($objmid->id);
  751. $ref=$langs->trans("Invoice").' '.$invoicestatic->getNomUrl(1);
  752. }
  753. else dol_print_error($db);
  754. }
  755. elseif ($tabtype[$key] == 'payment_supplier')
  756. {
  757. $sqlmid = 'SELECT payfac.fk_facturefourn as id';
  758. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfac";
  759. $sqlmid .= " WHERE payfac.fk_paiementfourn=" . $val["paymentsupplierid"];
  760. dol_syslog("accountancy/journal/bankjournal.php::sqlmid=" . $sqlmid, LOG_DEBUG);
  761. $resultmid = $db->query($sqlmid);
  762. if ($resultmid) {
  763. $objmid = $db->fetch_object($resultmid);
  764. $invoicesupplierstatic->fetch($objmid->id);
  765. $ref=$langs->trans("SupplierInvoice").' '.$invoicesupplierstatic->getNomUrl(1);
  766. }
  767. else dol_print_error($db);
  768. }
  769. elseif ($tabtype[$key] == 'payment_expensereport')
  770. {
  771. $sqlmid = 'SELECT payer.fk_expensereport as id';
  772. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "payment_expensereport as payer";
  773. $sqlmid .= " WHERE payer.fk_expensereport=" . $val["fk_expensereport"];
  774. dol_syslog("accountancy/journal/bankjournal.php::sqlmid=" . $sqlmid, LOG_DEBUG);
  775. $resultmid = $db->query($sqlmid);
  776. if ($resultmid) {
  777. $objmid = $db->fetch_object($resultmid);
  778. $expensereportstatic->fetch($objmid->id);
  779. $ref=$langs->trans("ExpenseReport").' '.$expensereportstatic->getNomUrl(1);
  780. }
  781. else dol_print_error($db);
  782. }
  783. elseif ($tabtype[$key] == 'payment_vat')
  784. {
  785. $sqlmid = 'SELECT v.rowid as id';
  786. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "tva as v";
  787. $sqlmid .= " WHERE v.rowid=" . $val["paymentvatid"];
  788. dol_syslog("accountancy/journal/bankjournal.php::sqlmid=" . $sqlmid, LOG_DEBUG);
  789. $resultmid = $db->query($sqlmid);
  790. if ($resultmid) {
  791. $objmid = $db->fetch_object($resultmid);
  792. $vatstatic->fetch($objmid->id);
  793. $ref=$langs->trans("PaymentVat").' '.$vatstatic->getNomUrl(1);
  794. }
  795. else dol_print_error($db);
  796. }
  797. elseif ($tabtype[$key] == 'payment_donation')
  798. {
  799. $sqlmid = 'SELECT payd.fk_donation as id';
  800. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "payment_donation as payd";
  801. $sqlmid .= " WHERE payd.fk_donation=" . $val["paymentdonationid"];
  802. dol_syslog("accountancy/journal/bankjournal.php::sqlmid=" . $sqlmid, LOG_DEBUG);
  803. $resultmid = $db->query($sqlmid);
  804. if ($resultmid) {
  805. $objmid = $db->fetch_object($resultmid);
  806. $donationstatic->fetch($objmid->id);
  807. $ref=$langs->trans("Donation").' '.$donationstatic->getNomUrl(1);
  808. }
  809. else dol_print_error($db);
  810. }
  811. elseif ($tabtype[$key] == 'payment_salary')
  812. {
  813. $sqlmid = 'SELECT s.rowid as id';
  814. $sqlmid .= " FROM " . MAIN_DB_PREFIX . "payment_salary as s";
  815. $sqlmid .= " WHERE s.rowid=" . $val["paymentsalid"];
  816. dol_syslog("accountancy/journal/bankjournal.php::sqlmid=" . $sqlmid, LOG_DEBUG);
  817. $resultmid = $db->query($sqlmid);
  818. if ($resultmid) {
  819. $objmid = $db->fetch_object($resultmid);
  820. $salarystatic->fetch($objmid->id);
  821. $ref=$langs->trans("SalaryPayment").' '.$salarystatic->getNomUrl(1);
  822. }
  823. else dol_print_error($db);
  824. }
  825. /*$invoicestatic->id = $key;
  826. $invoicestatic->ref = $val["ref"];
  827. $invoicestatic->type = $val["type"];*/
  828. // Bank
  829. foreach ( $tabbq[$key] as $k => $mt )
  830. {
  831. print '<tr class="oddeven">';
  832. print "<td><!-- Bank bank.rowid=".$key."--></td>";
  833. print "<td>" . $date . "</td>";
  834. print "<td>" . $ref . "</td>";
  835. print "<td>";
  836. $accountoshow = length_accountg($k);
  837. if (empty($accountoshow) || $accountoshow == 'NotDefined')
  838. {
  839. print '<span class="error">'.$langs->trans("BankAccountNotDefined").'</span>';
  840. }
  841. else print $accountoshow;
  842. print "</td>";
  843. if ($val['soclib'] == '') {
  844. print "<td>" . $bankstatic->label . " - " . $reflabel . "</td>";
  845. } else {
  846. print "<td>" . $bankstatic->label . " - " . $val['soclib'] . "</td>";
  847. }
  848. print "<td>" . $val["type_payment"] . "</td>";
  849. print "<td align='right'>" . ($mt >= 0 ? price($mt) : '') . "</td>";
  850. print "<td align='right'>" . ($mt < 0 ? price(- $mt) : '') . "</td>";
  851. print "</tr>";
  852. }
  853. // Third party
  854. if (is_array($tabtp[$key])) {
  855. foreach ( $tabtp[$key] as $k => $mt ) {
  856. if ($k != 'type') {
  857. print '<tr class="oddeven">';
  858. print "<td><!-- Thirdparty bank.rowid=".$key." --></td>";
  859. print "<td>" . $date . "</td>";
  860. print "<td>" . $ref . "</td>";
  861. print "<td>";
  862. $accountoshow = length_accounta($k);
  863. if (empty($accountoshow) || $accountoshow == 'NotDefined')
  864. {
  865. print '<span class="error">'.$langs->trans("ThirdpartyAccountNotDefined").'</span>';
  866. }
  867. else print $accountoshow;
  868. print "</td>";
  869. print "<td>" . $reflabel . ' ' . $val['soclib'] . "</td>";
  870. print "<td>" . $val["type_payment"] . "</td>";
  871. print "<td align='right'>" . ($mt < 0 ? price(- $mt) : '') . "</td>";
  872. print "<td align='right'>" . ($mt >= 0 ? price($mt) : '') . "</td>";
  873. print "</tr>";
  874. }
  875. }
  876. } else {
  877. foreach ( $tabbq[$key] as $k => $mt ) {
  878. print '<tr class="oddeven">';
  879. print "<td><!-- Wait bank.rowid=".$key." --></td>";
  880. print "<td>" . $date . "</td>";
  881. print "<td>" . $ref . "</td>";
  882. print "<td>";
  883. if (empty($accountoshow) || $accountoshow == 'NotDefined')
  884. {
  885. print '<span class="error">'.$langs->trans("WaitAccountNotDefined").'</span>';
  886. }
  887. else print length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUSPENSE);
  888. print "</td>";
  889. print "<td>" . $reflabel . "</td>";
  890. print "<td>&nbsp;</td>";
  891. print "<td align='right'>" . ($mt < 0 ? price(- $mt) : '') . "</td>";
  892. print "<td align='right'>" . ($mt >= 0 ? price($mt) : '') . "</td>";
  893. print "</tr>";
  894. }
  895. }
  896. }
  897. print "</table>";
  898. llxFooter();
  899. }
  900. $db->close();