27if (!defined(
'NOSESSION')) {
28 define(
'NOSESSION',
'1');
31$sapi_type = php_sapi_name();
32$script_file = basename(__FILE__);
36if (substr($sapi_type, 0, 3) ==
'cgi') {
37 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
41require_once $path.
"../../htdocs/master.inc.php";
42require_once DOL_DOCUMENT_ROOT.
'/core/lib/functionscli.lib.php';
43require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
44require_once DOL_DOCUMENT_ROOT.
'/core/lib/bank.lib.php';
45require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
46require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
47require_once DOL_DOCUMENT_ROOT.
'/compta/sociales/class/chargesociales.class.php';
48require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
49require_once DOL_DOCUMENT_ROOT.
'/compta/tva/class/tva.class.php';
50require_once DOL_DOCUMENT_ROOT.
'/fourn/class/paiementfourn.class.php';
51require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
52require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
53require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
54require_once DOL_DOCUMENT_ROOT.
'/compta/tva/class/tva.class.php';
55require_once DOL_DOCUMENT_ROOT.
'/compta/sociales/class/paymentsocialcontribution.class.php';
58$version = DOL_VERSION;
61$hookmanager->initHooks(array(
'cli'));
69print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
70dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
72if (!isset($argv[3]) || !$argv[3]) {
73 print
"Usage: ".$script_file.
" bank_ref [bank_receipt_number|all] (csv|tsv|excel|excel2007) [lang=xx_XX]\n";
81$invoicestatic =
new Facture($db);
83$societestatic =
new Societe($db);
89$paymentvatstatic =
new Tva($db);
94foreach ($argv as $key => $value) {
98 if (preg_match(
'/^lang=/i', $value)) {
100 $valarray = explode(
'=', $value);
101 $newlangid = $valarray[1];
102 print
'Use language '.$newlangid.
".\n";
105$outputlangs = $langs;
106if (!empty($newlangid)) {
107 if ($outputlangs->defaultlang != $newlangid) {
109 $outputlangs->setDefaultLang($newlangid);
114$outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"banks",
"members",
"compta"));
117$result = $acct->fetch(
'', $bankref);
119 print
"Failed to find bank account with ref ".$bankref.
".\n";
122 print
"Export for bank account ".$acct->ref.
" (".$acct->label.
").\n";
126$dir = DOL_DOCUMENT_ROOT.
"/core/modules/export/";
127$file =
"export_".$model.
".modules.php";
128$classname =
"Export".$model;
130 print
"No driver to export with format ".$model.
"\n";
133require_once $dir.$file;
134$objmodel =
new $classname($db);
137$dirname = $conf->bank->dir_temp;
138$filename =
'export-bank-receipts-'.$bankref.
'-'.$num.
'.'.$objmodel->extension;
140$array_fields = array(
141 'bankreceipt' => $outputlangs->transnoentitiesnoconv(
"AccountStatementShort"),
142 'bankaccount' => $outputlangs->transnoentitiesnoconv(
"BankAccount"),
143 'dateop' => $outputlangs->transnoentitiesnoconv(
"DateOperationShort"),
144 'dateval' => $outputlangs->transnoentitiesnoconv(
"DateValueShort"),
145 'type' => $outputlangs->transnoentitiesnoconv(
"Type"),
146 'description' => $outputlangs->transnoentitiesnoconv(
"Description"),
147 'thirdparty' => $outputlangs->transnoentitiesnoconv(
"Tiers"),
148 'accountelem' => $outputlangs->transnoentitiesnoconv(
"Piece"),
149 'debit' => $outputlangs->transnoentitiesnoconv(
"Debit"),
150 'credit' => $outputlangs->transnoentitiesnoconv(
"Credit"),
151 'soldbefore' => $outputlangs->transnoentitiesnoconv(
"BankBalanceBefore"),
152 'soldafter' => $outputlangs->transnoentitiesnoconv(
"BankBalanceAfter"),
153 'comment' => $outputlangs->transnoentitiesnoconv(
"Comment")
155$array_selected = array(
'bankreceipt' =>
'bankreceipt',
'bankaccount' =>
'bankaccount',
'dateop' =>
'dateop',
'dateval' =>
'dateval',
'type' =>
'type',
'description' =>
'description',
'thirdparty' =>
'thirdparty',
'accountelem' =>
'accountelem',
'debit' =>
'debit',
'credit' =>
'credit',
'soldbefore' =>
'soldbefore',
'soldafter' =>
'soldafter',
'comment' =>
'comment');
156$array_export_TypeFields = array(
'bankreceipt' =>
'Text',
'bankaccount' =>
'Text',
'dateop' =>
'Date',
'dateval' =>
'Date',
'type' =>
'Text',
'description' =>
'Text',
'thirdparty' =>
'Text',
'accountelem' =>
'Text',
'debit' =>
'Number',
'credit' =>
'Number',
'soldbefore' =>
'Number',
'soldafter' =>
'Number',
'comment' =>
'Text');
160if (!empty($num) && $num !=
"all") {
162 $arraynum = explode(
',', $num);
163 foreach ($arraynum as $val) {
164 if ($listofnum !=
"'") {
171$sql =
"SELECT b.rowid, b.dateo as do, b.datev as dv,";
172$sql .=
" b.amount, b.label, b.rappro, b.num_releve, b.num_chq, b.fk_type,";
173$sql .=
" ba.rowid as bankid, ba.ref as bankref, ba.label as banklabel";
174$sql .=
" FROM ".MAIN_DB_PREFIX.
"bank_account as ba";
175$sql .=
", ".MAIN_DB_PREFIX.
"bank as b";
176$sql .=
" WHERE b.fk_account = ".((int) $acct->id);
178 $sql .=
" AND b.num_releve IN (".$db->sanitize($listofnum, 1).
")";
181 $sql .=
" OR b.num_releve is null";
183$sql .=
" AND b.fk_account = ba.rowid";
184$sql .= $db->order(
"b.num_releve, b.datev, b.datec",
"ASC");
187$resql = $db->query($sql);
189 $balancebefore = array();
191 $numrows = $db->num_rows($resql);
195 print
'Open file '.$filename.
' into directory '.$dirname.
"\n";
197 $result = $objmodel->open_file($dirname.
"/".$filename, $outputlangs);
200 print
'Failed to create file '.$filename.
' into dir '.$dirname.
'.'.
"\n";
205 $objmodel->write_header($outputlangs);
208 $objmodel->write_title($array_fields, $array_selected, $outputlangs, $array_export_TypeFields);
215 while ($i < $numrows) {
220 $objp = $db->fetch_object($resql);
223 if (!isset($balancebefore[$objp->num_releve])) {
224 print
'Calculate start balance for receipt '.$objp->num_releve.
"\n";
226 $sql2 =
"SELECT sum(b.amount) as amount";
227 $sql2 .=
" FROM ".MAIN_DB_PREFIX.
"bank as b";
228 $sql2 .=
" WHERE b.num_releve < '".$db->escape($objp->num_releve).
"'";
229 $sql2 .=
" AND b.fk_account = ".$objp->bankid;
230 $resql2 = $db->query($sql2);
232 $obj2 = $db->fetch_object($resql2);
233 $balancebefore[$objp->num_releve] = ($obj2->amount ? $obj2->amount : 0);
240 $total = $balancebefore[$objp->num_releve];
243 $totalbefore = $total;
244 $total = $total + $objp->amount;
247 $dateop = $db->jdate($objp->do);
250 $datevalue = $db->jdate($objp->dv);
253 $numchq = ($objp->num_chq ? $objp->num_chq :
'');
257 preg_match(
'/\((.+)\)/i', $objp->label, $reg);
258 if ($reg[1] && $langs->transnoentitiesnoconv($reg[1]) != $reg[1]) {
259 $description = $langs->transnoentitiesnoconv($reg[1]);
261 $description = $objp->label;
267 $links = $acct->get_url($objp->rowid);
268 foreach ($links as $key => $val) {
269 if ($links[$key][
'type'] ==
'payment') {
270 $paymentstatic->fetch($links[$key][
'url_id']);
271 $tmparray = $paymentstatic->getBillsArray(
'');
272 if (is_array($tmparray)) {
273 foreach ($tmparray as $tmpkey => $tmpval) {
274 $invoicestatic->fetch($tmpval);
276 $accountelem .=
', ';
278 $accountelem .= $invoicestatic->ref;
281 } elseif ($links[$key][
'type'] ==
'payment_supplier') {
282 $paymentsupplierstatic->fetch($links[$key][
'url_id']);
283 $tmparray = $paymentsupplierstatic->getBillsArray(
'');
284 if (is_array($tmparray)) {
285 foreach ($tmparray as $tmpkey => $tmpval) {
286 $invoicesupplierstatic->fetch($tmpval);
288 $accountelem .=
', ';
290 $accountelem .= $invoicesupplierstatic->ref;
293 } elseif ($links[$key][
'type'] ==
'payment_sc') {
294 $paymentsocialcontributionstatic->fetch($links[$key][
'url_id']);
296 $accountelem .=
', ';
298 $accountelem .= $langs->transnoentitiesnoconv(
"SocialContribution").
' '.$paymentsocialcontributionstatic->ref;
299 } elseif ($links[$key][
'type'] ==
'payment_vat') {
300 $paymentvatstatic->fetch($links[$key][
'url_id']);
302 $accountelem .=
', ';
304 $accountelem .= $langs->transnoentitiesnoconv(
"VATPayments").
' '.$paymentvatstatic->ref;
305 } elseif ($links[$key][
'type'] ==
'banktransfert') {
306 $comment = $outputlangs->transnoentitiesnoconv(
"Transfer");
307 if ($objp->amount > 0) {
311 $banklinestatic->fetch($links[$key][
'url_id']);
312 $bankstatic->id = $banklinestatic->fk_account;
313 $bankstatic->label = $banklinestatic->bank_account_label;
314 $comment .=
' ('.$langs->transnoentitiesnoconv(
"from").
' ';
315 $comment .= $bankstatic->getNomUrl(1,
'transactions');
316 $comment .=
' '.$langs->transnoentitiesnoconv(
"toward").
' ';
317 $bankstatic->id = $objp->bankid;
318 $bankstatic->label = $objp->bankref;
319 $comment .= $bankstatic->getNomUrl(1,
'');
325 $bankstatic->id = $objp->bankid;
326 $bankstatic->label = $objp->bankref;
327 $comment .=
' ('.$langs->transnoentitiesnoconv(
"from").
' ';
328 $comment .= $bankstatic->getNomUrl(1,
'');
329 $comment .=
' '.$langs->transnoentitiesnoconv(
"toward").
' ';
330 $banklinestatic->fetch($links[$key][
'url_id']);
331 $bankstatic->id = $banklinestatic->fk_account;
332 $bankstatic->label = $banklinestatic->bank_account_label;
333 $comment .= $bankstatic->getNomUrl(1,
'transactions');
336 } elseif ($links[$key][
'type'] ==
'company') {
340 $thirdparty .=
dol_trunc($links[$key][
'label'], 24);
342 } elseif ($links[$key][
'type'] ==
'member') {
344 $accountelem .=
', ';
346 $thirdparty .= $links[$key][
'label'];
370 $debit = $credit =
'';
371 if ($objp->amount < 0) {
372 $totald = $totald + abs($objp->amount);
375 $totalc = $totalc + abs($objp->amount);
382 $rec->bankreceipt = $objp->num_releve;
383 $rec->bankaccount = $objp->banklabel;
386 $rec->type = $objp->fk_type.
' '.($objp->num_chq ? $objp->num_chq :
'');
387 $rec->description = $description;
388 $rec->thirdparty = $thirdparty;
389 $rec->accountelem = $accountelem;
390 $rec->debit = $debit;
391 $rec->credit = $credit;
392 $rec->comment = $comment;
393 $rec->soldbefore =
price2num($totalbefore);
397 $objmodel->write_record($array_selected, $rec, $outputlangs, $array_export_TypeFields);
401 print
"Found ".$numrows.
" records for receipt ".$num.
"\n";
404 $objmodel->write_footer($outputlangs);
407 $objmodel->close_file();
409 print
'File '.$filename.
' was generated into dir '.$dirname.
'.'.
"\n";
413 print
"No records found for receipt ".$num.
"\n";
Class to manage bank accounts.
Class to manage bank transaction lines.
Class to manage members of a foundation.
Class for managing the social charges.
Class to manage suppliers invoices.
Class to manage invoices.
Class to manage payments for supplier invoices.
Class to manage payments of customer invoices.
Class to manage payments of social contributions.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage translations.
Class to manage VAT - Value-added tax (also known in French as TVA - Taxe sur la valeur ajoutée)
dol_is_file($pathoffile)
Return if path is a file.
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $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 '…' if string larger than length.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)