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/files.lib.php';
43require_once DOL_DOCUMENT_ROOT.
'/core/lib/bank.lib.php';
44require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
45require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
46require_once DOL_DOCUMENT_ROOT.
'/compta/sociales/class/chargesociales.class.php';
47require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
48require_once DOL_DOCUMENT_ROOT.
'/compta/tva/class/tva.class.php';
49require_once DOL_DOCUMENT_ROOT.
'/fourn/class/paiementfourn.class.php';
50require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
51require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
52require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
53require_once DOL_DOCUMENT_ROOT.
'/compta/tva/class/tva.class.php';
54require_once DOL_DOCUMENT_ROOT.
'/compta/sociales/class/paymentsocialcontribution.class.php';
57$version = DOL_VERSION;
60$hookmanager->initHooks(array(
'cli'));
68print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
69dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
71if (!isset($argv[3]) || !$argv[3]) {
72 print
"Usage: ".$script_file.
" bank_ref [bank_receipt_number|all] (csv|tsv|excel|excel2007) [lang=xx_XX]\n";
80$invoicestatic =
new Facture($db);
82$societestatic =
new Societe($db);
88$paymentvatstatic =
new Tva($db);
93foreach ($argv as $key => $value) {
97 if (preg_match(
'/^lang=/i', $value)) {
99 $valarray = explode(
'=', $value);
100 $newlangid = $valarray[1];
101 print
'Use language '.$newlangid.
".\n";
104$outputlangs = $langs;
105if (!empty($newlangid)) {
106 if ($outputlangs->defaultlang != $newlangid) {
108 $outputlangs->setDefaultLang($newlangid);
113$outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"banks",
"members",
"compta"));
116$result = $acct->fetch(
'', $bankref);
118 print
"Failed to find bank account with ref ".$bankref.
".\n";
121 print
"Export for bank account ".$acct->ref.
" (".$acct->label.
").\n";
125$dir = DOL_DOCUMENT_ROOT.
"/core/modules/export/";
126$file =
"export_".$model.
".modules.php";
127$classname =
"Export".$model;
129 print
"No driver to export with format ".$model.
"\n";
132require_once $dir.$file;
133$objmodel =
new $classname($db);
136$dirname = $conf->bank->dir_temp;
137$filename =
'export-bank-receipts-'.$bankref.
'-'.$num.
'.'.$objmodel->extension;
139$array_fields = array(
140 'bankreceipt' => $outputlangs->transnoentitiesnoconv(
"AccountStatementShort"),
141 'bankaccount' => $outputlangs->transnoentitiesnoconv(
"BankAccount"),
142 'dateop' => $outputlangs->transnoentitiesnoconv(
"DateOperationShort"),
143 'dateval' => $outputlangs->transnoentitiesnoconv(
"DateValueShort"),
144 'type' => $outputlangs->transnoentitiesnoconv(
"Type"),
145 'description' => $outputlangs->transnoentitiesnoconv(
"Description"),
146 'thirdparty' => $outputlangs->transnoentitiesnoconv(
"Tiers"),
147 'accountelem' => $outputlangs->transnoentitiesnoconv(
"Piece"),
148 'debit' => $outputlangs->transnoentitiesnoconv(
"Debit"),
149 'credit' => $outputlangs->transnoentitiesnoconv(
"Credit"),
150 'soldbefore' => $outputlangs->transnoentitiesnoconv(
"BankBalanceBefore"),
151 'soldafter' => $outputlangs->transnoentitiesnoconv(
"BankBalanceAfter"),
152 'comment' => $outputlangs->transnoentitiesnoconv(
"Comment")
154$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');
155$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');
159if (!empty($num) && $num !=
"all") {
161 $arraynum = explode(
',', $num);
162 foreach ($arraynum as $val) {
163 if ($listofnum !=
"'") {
170$sql =
"SELECT b.rowid, b.dateo as do, b.datev as dv,";
171$sql .=
" b.amount, b.label, b.rappro, b.num_releve, b.num_chq, b.fk_type,";
172$sql .=
" ba.rowid as bankid, ba.ref as bankref, ba.label as banklabel";
173$sql .=
" FROM ".MAIN_DB_PREFIX.
"bank_account as ba";
174$sql .=
", ".MAIN_DB_PREFIX.
"bank as b";
175$sql .=
" WHERE b.fk_account = ".((int) $acct->id);
177 $sql .=
" AND b.num_releve IN (".$db->sanitize($listofnum, 1).
")";
180 $sql .=
" OR b.num_releve is null";
182$sql .=
" AND b.fk_account = ba.rowid";
183$sql .= $db->order(
"b.num_releve, b.datev, b.datec",
"ASC");
186$resql = $db->query($sql);
188 $balancebefore = array();
190 $numrows = $db->num_rows($resql);
194 print
'Open file '.$filename.
' into directory '.$dirname.
"\n";
196 $result = $objmodel->open_file($dirname.
"/".$filename, $outputlangs);
199 print
'Failed to create file '.$filename.
' into dir '.$dirname.
'.'.
"\n";
204 $objmodel->write_header($outputlangs);
207 $objmodel->write_title($array_fields, $array_selected, $outputlangs, $array_export_TypeFields);
214 while ($i < $numrows) {
219 $objp = $db->fetch_object($resql);
222 if (!isset($balancebefore[$objp->num_releve])) {
223 print
'Calculate start balance for receipt '.$objp->num_releve.
"\n";
225 $sql2 =
"SELECT sum(b.amount) as amount";
226 $sql2 .=
" FROM ".MAIN_DB_PREFIX.
"bank as b";
227 $sql2 .=
" WHERE b.num_releve < '".$db->escape($objp->num_releve).
"'";
228 $sql2 .=
" AND b.fk_account = ".$objp->bankid;
229 $resql2 = $db->query($sql2);
231 $obj2 = $db->fetch_object($resql2);
232 $balancebefore[$objp->num_releve] = ($obj2->amount ? $obj2->amount : 0);
239 $total = $balancebefore[$objp->num_releve];
242 $totalbefore = $total;
243 $total = $total + $objp->amount;
246 $dateop = $db->jdate($objp->do);
249 $datevalue = $db->jdate($objp->dv);
252 $numchq = ($objp->num_chq ? $objp->num_chq :
'');
256 preg_match(
'/\((.+)\)/i', $objp->label, $reg);
257 if ($reg[1] && $langs->transnoentitiesnoconv($reg[1]) != $reg[1]) {
258 $description = $langs->transnoentitiesnoconv($reg[1]);
260 $description = $objp->label;
266 $links = $acct->get_url($objp->rowid);
267 foreach ($links as $key => $val) {
268 if ($links[$key][
'type'] ==
'payment') {
269 $paymentstatic->fetch($links[$key][
'url_id']);
270 $tmparray = $paymentstatic->getBillsArray(
'');
271 if (is_array($tmparray)) {
272 foreach ($tmparray as $tmpkey => $tmpval) {
273 $invoicestatic->fetch($tmpval);
275 $accountelem .=
', ';
277 $accountelem .= $invoicestatic->ref;
280 } elseif ($links[$key][
'type'] ==
'payment_supplier') {
281 $paymentsupplierstatic->fetch($links[$key][
'url_id']);
282 $tmparray = $paymentsupplierstatic->getBillsArray(
'');
283 if (is_array($tmparray)) {
284 foreach ($tmparray as $tmpkey => $tmpval) {
285 $invoicesupplierstatic->fetch($tmpval);
287 $accountelem .=
', ';
289 $accountelem .= $invoicesupplierstatic->ref;
292 } elseif ($links[$key][
'type'] ==
'payment_sc') {
293 $paymentsocialcontributionstatic->fetch($links[$key][
'url_id']);
295 $accountelem .=
', ';
297 $accountelem .= $langs->transnoentitiesnoconv(
"SocialContribution").
' '.$paymentsocialcontributionstatic->ref;
298 } elseif ($links[$key][
'type'] ==
'payment_vat') {
299 $paymentvatstatic->fetch($links[$key][
'url_id']);
301 $accountelem .=
', ';
303 $accountelem .= $langs->transnoentitiesnoconv(
"VATPayments").
' '.$paymentvatstatic->ref;
304 } elseif ($links[$key][
'type'] ==
'banktransfert') {
305 $comment = $outputlangs->transnoentitiesnoconv(
"Transfer");
306 if ($objp->amount > 0) {
310 $banklinestatic->fetch($links[$key][
'url_id']);
311 $bankstatic->id = $banklinestatic->fk_account;
312 $bankstatic->label = $banklinestatic->bank_account_label;
313 $comment .=
' ('.$langs->transnoentitiesnoconv(
"from").
' ';
314 $comment .= $bankstatic->getNomUrl(1,
'transactions');
315 $comment .=
' '.$langs->transnoentitiesnoconv(
"toward").
' ';
316 $bankstatic->id = $objp->bankid;
317 $bankstatic->label = $objp->bankref;
318 $comment .= $bankstatic->getNomUrl(1,
'');
324 $bankstatic->id = $objp->bankid;
325 $bankstatic->label = $objp->bankref;
326 $comment .=
' ('.$langs->transnoentitiesnoconv(
"from").
' ';
327 $comment .= $bankstatic->getNomUrl(1,
'');
328 $comment .=
' '.$langs->transnoentitiesnoconv(
"toward").
' ';
329 $banklinestatic->fetch($links[$key][
'url_id']);
330 $bankstatic->id = $banklinestatic->fk_account;
331 $bankstatic->label = $banklinestatic->bank_account_label;
332 $comment .= $bankstatic->getNomUrl(1,
'transactions');
335 } elseif ($links[$key][
'type'] ==
'company') {
339 $thirdparty .=
dol_trunc($links[$key][
'label'], 24);
341 } elseif ($links[$key][
'type'] ==
'member') {
343 $accountelem .=
', ';
345 $thirdparty .= $links[$key][
'label'];
369 $debit = $credit =
'';
370 if ($objp->amount < 0) {
371 $totald = $totald + abs($objp->amount);
374 $totalc = $totalc + abs($objp->amount);
381 $rec->bankreceipt = $objp->num_releve;
382 $rec->bankaccount = $objp->banklabel;
385 $rec->type = $objp->fk_type.
' '.($objp->num_chq ? $objp->num_chq :
'');
386 $rec->description = $description;
387 $rec->thirdparty = $thirdparty;
388 $rec->accountelem = $accountelem;
389 $rec->debit = $debit;
390 $rec->credit = $credit;
391 $rec->comment = $comment;
392 $rec->soldbefore =
price2num($totalbefore);
396 $objmodel->write_record($array_selected, $rec, $outputlangs, $array_export_TypeFields);
400 print
"Found ".$numrows.
" records for receipt ".$num.
"\n";
403 $objmodel->write_footer($outputlangs);
406 $objmodel->close_file();
408 print
'File '.$filename.
' was generated into dir '.$dirname.
'.'.
"\n";
412 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)