26if (!defined(
'NOSESSION')) {
27 define(
'NOSESSION',
'1');
30$sapi_type = php_sapi_name();
31$script_file = basename(__FILE__);
35if (substr($sapi_type, 0, 3) ==
'cgi') {
36 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
40require_once $path.
"../../htdocs/master.inc.php";
41require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
42require_once DOL_DOCUMENT_ROOT.
'/core/lib/bank.lib.php';
43require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
44require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
45require_once DOL_DOCUMENT_ROOT.
'/compta/sociales/class/chargesociales.class.php';
46require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
47require_once DOL_DOCUMENT_ROOT.
'/compta/tva/class/tva.class.php';
48require_once DOL_DOCUMENT_ROOT.
'/fourn/class/paiementfourn.class.php';
49require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
50require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
51require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
52require_once DOL_DOCUMENT_ROOT.
'/compta/tva/class/tva.class.php';
53require_once DOL_DOCUMENT_ROOT.
'/compta/sociales/class/paymentsocialcontribution.class.php';
56$version = DOL_VERSION;
59$hookmanager->initHooks(array(
'cli'));
67print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
68dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
70if (!isset($argv[3]) || !$argv[3]) {
71 print
"Usage: ".$script_file.
" bank_ref [bank_receipt_number|all] (csv|tsv|excel|excel2007) [lang=xx_XX]\n";
79$invoicestatic =
new Facture($db);
81$societestatic =
new Societe($db);
87$paymentvatstatic =
new Tva($db);
92foreach ($argv as $key => $value) {
96 if (preg_match(
'/^lang=/i', $value)) {
98 $valarray = explode(
'=', $value);
99 $newlangid = $valarray[1];
100 print
'Use language '.$newlangid.
".\n";
103$outputlangs = $langs;
104if (!empty($newlangid)) {
105 if ($outputlangs->defaultlang != $newlangid) {
107 $outputlangs->setDefaultLang($newlangid);
112$outputlangs->loadLangs(array(
"main",
"companies",
"bills",
"banks",
"members",
"compta"));
115$result = $acct->fetch(
'', $bankref);
117 print
"Failed to find bank account with ref ".$bankref.
".\n";
120 print
"Export for bank account ".$acct->ref.
" (".$acct->label.
").\n";
124$dir = DOL_DOCUMENT_ROOT.
"/core/modules/export/";
125$file =
"export_".$model.
".modules.php";
126$classname =
"Export".$model;
128 print
"No driver to export with format ".$model.
"\n";
131require_once $dir.$file;
132$objmodel =
new $classname($db);
135$dirname = $conf->bank->dir_temp;
136$filename =
'export-bank-receipts-'.$bankref.
'-'.$num.
'.'.$objmodel->extension;
138$array_fields = array(
139 'bankreceipt' => $outputlangs->transnoentitiesnoconv(
"AccountStatementShort"),
140 'bankaccount' => $outputlangs->transnoentitiesnoconv(
"BankAccount"),
141 'dateop' => $outputlangs->transnoentitiesnoconv(
"DateOperationShort"),
142 'dateval' => $outputlangs->transnoentitiesnoconv(
"DateValueShort"),
143 'type' => $outputlangs->transnoentitiesnoconv(
"Type"),
144 'description' => $outputlangs->transnoentitiesnoconv(
"Description"),
145 'thirdparty' => $outputlangs->transnoentitiesnoconv(
"Tiers"),
146 'accountelem' => $outputlangs->transnoentitiesnoconv(
"Piece"),
147 'debit' => $outputlangs->transnoentitiesnoconv(
"Debit"),
148 'credit' => $outputlangs->transnoentitiesnoconv(
"Credit"),
149 'soldbefore' => $outputlangs->transnoentitiesnoconv(
"BankBalanceBefore"),
150 'soldafter' => $outputlangs->transnoentitiesnoconv(
"BankBalanceAfter"),
151 'comment' => $outputlangs->transnoentitiesnoconv(
"Comment")
153$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');
154$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');
158if (!empty($num) && $num !=
"all") {
160 $arraynum = explode(
',', $num);
161 foreach ($arraynum as $val) {
162 if ($listofnum !=
"'") {
169$sql =
"SELECT b.rowid, b.dateo as do, b.datev as dv,";
170$sql .=
" b.amount, b.label, b.rappro, b.num_releve, b.num_chq, b.fk_type,";
171$sql .=
" ba.rowid as bankid, ba.ref as bankref, ba.label as banklabel";
172$sql .=
" FROM ".MAIN_DB_PREFIX.
"bank_account as ba";
173$sql .=
", ".MAIN_DB_PREFIX.
"bank as b";
174$sql .=
" WHERE b.fk_account = ".((int) $acct->id);
176 $sql .=
" AND b.num_releve IN (".$db->sanitize($listofnum, 1).
")";
179 $sql .=
" OR b.num_releve is null";
181$sql .=
" AND b.fk_account = ba.rowid";
182$sql .= $db->order(
"b.num_releve, b.datev, b.datec",
"ASC");
185$resql = $db->query($sql);
187 $balancebefore = array();
189 $numrows = $db->num_rows($resql);
193 print
'Open file '.$filename.
' into directory '.$dirname.
"\n";
195 $result = $objmodel->open_file($dirname.
"/".$filename, $outputlangs);
198 print
'Failed to create file '.$filename.
' into dir '.$dirname.
'.'.
"\n";
203 $objmodel->write_header($outputlangs);
206 $objmodel->write_title($array_fields, $array_selected, $outputlangs, $array_export_TypeFields);
213 while ($i < $numrows) {
218 $objp = $db->fetch_object($resql);
221 if (!isset($balancebefore[$objp->num_releve])) {
222 print
'Calculate start balance for receipt '.$objp->num_releve.
"\n";
224 $sql2 =
"SELECT sum(b.amount) as amount";
225 $sql2 .=
" FROM ".MAIN_DB_PREFIX.
"bank as b";
226 $sql2 .=
" WHERE b.num_releve < '".$db->escape($objp->num_releve).
"'";
227 $sql2 .=
" AND b.fk_account = ".$objp->bankid;
228 $resql2 = $db->query($sql2);
230 $obj2 = $db->fetch_object($resql2);
231 $balancebefore[$objp->num_releve] = ($obj2->amount ? $obj2->amount : 0);
238 $total = $balancebefore[$objp->num_releve];
241 $totalbefore = $total;
242 $total = $total + $objp->amount;
245 $dateop = $db->jdate($objp->do);
248 $datevalue = $db->jdate($objp->dv);
251 $numchq = ($objp->num_chq ? $objp->num_chq :
'');
255 preg_match(
'/\((.+)\)/i', $objp->label, $reg);
256 if ($reg[1] && $langs->transnoentitiesnoconv($reg[1]) != $reg[1]) {
257 $description = $langs->transnoentitiesnoconv($reg[1]);
259 $description = $objp->label;
265 $links = $acct->get_url($objp->rowid);
266 foreach ($links as $key => $val) {
267 if ($links[$key][
'type'] ==
'payment') {
268 $paymentstatic->fetch($links[$key][
'url_id']);
269 $tmparray = $paymentstatic->getBillsArray(
'');
270 if (is_array($tmparray)) {
271 foreach ($tmparray as $tmpkey => $tmpval) {
272 $invoicestatic->fetch($tmpval);
274 $accountelem .=
', ';
276 $accountelem .= $invoicestatic->ref;
279 } elseif ($links[$key][
'type'] ==
'payment_supplier') {
280 $paymentsupplierstatic->fetch($links[$key][
'url_id']);
281 $tmparray = $paymentsupplierstatic->getBillsArray(
'');
282 if (is_array($tmparray)) {
283 foreach ($tmparray as $tmpkey => $tmpval) {
284 $invoicesupplierstatic->fetch($tmpval);
286 $accountelem .=
', ';
288 $accountelem .= $invoicesupplierstatic->ref;
291 } elseif ($links[$key][
'type'] ==
'payment_sc') {
292 $paymentsocialcontributionstatic->fetch($links[$key][
'url_id']);
294 $accountelem .=
', ';
296 $accountelem .= $langs->transnoentitiesnoconv(
"SocialContribution").
' '.$paymentsocialcontributionstatic->ref;
297 } elseif ($links[$key][
'type'] ==
'payment_vat') {
298 $paymentvatstatic->fetch($links[$key][
'url_id']);
300 $accountelem .=
', ';
302 $accountelem .= $langs->transnoentitiesnoconv(
"VATPayments").
' '.$paymentvatstatic->ref;
303 } elseif ($links[$key][
'type'] ==
'banktransfert') {
304 $comment = $outputlangs->transnoentitiesnoconv(
"Transfer");
305 if ($objp->amount > 0) {
309 $banklinestatic->fetch($links[$key][
'url_id']);
310 $bankstatic->id = $banklinestatic->fk_account;
311 $bankstatic->label = $banklinestatic->bank_account_label;
312 $comment .=
' ('.$langs->transnoentitiesnoconv(
"from").
' ';
313 $comment .= $bankstatic->getNomUrl(1,
'transactions');
314 $comment .=
' '.$langs->transnoentitiesnoconv(
"toward").
' ';
315 $bankstatic->id = $objp->bankid;
316 $bankstatic->label = $objp->bankref;
317 $comment .= $bankstatic->getNomUrl(1,
'');
323 $bankstatic->id = $objp->bankid;
324 $bankstatic->label = $objp->bankref;
325 $comment .=
' ('.$langs->transnoentitiesnoconv(
"from").
' ';
326 $comment .= $bankstatic->getNomUrl(1,
'');
327 $comment .=
' '.$langs->transnoentitiesnoconv(
"toward").
' ';
328 $banklinestatic->fetch($links[$key][
'url_id']);
329 $bankstatic->id = $banklinestatic->fk_account;
330 $bankstatic->label = $banklinestatic->bank_account_label;
331 $comment .= $bankstatic->getNomUrl(1,
'transactions');
334 } elseif ($links[$key][
'type'] ==
'company') {
338 $thirdparty .=
dol_trunc($links[$key][
'label'], 24);
340 } elseif ($links[$key][
'type'] ==
'member') {
342 $accountelem .=
', ';
344 $thirdparty .= $links[$key][
'label'];
368 $debit = $credit =
'';
369 if ($objp->amount < 0) {
370 $totald = $totald + abs($objp->amount);
373 $totalc = $totalc + abs($objp->amount);
380 $rec->bankreceipt = $objp->num_releve;
381 $rec->bankaccount = $objp->banklabel;
384 $rec->type = $objp->fk_type.
' '.($objp->num_chq ? $objp->num_chq :
'');
385 $rec->description = $description;
386 $rec->thirdparty = $thirdparty;
387 $rec->accountelem = $accountelem;
388 $rec->debit = $debit;
389 $rec->credit = $credit;
390 $rec->comment = $comment;
391 $rec->soldbefore =
price2num($totalbefore);
395 $objmodel->write_record($array_selected, $rec, $outputlangs, $array_export_TypeFields);
399 print
"Found ".$numrows.
" records for receipt ".$num.
"\n";
402 $objmodel->write_footer($outputlangs);
405 $objmodel->close_file();
407 print
'File '.$filename.
' was generated into dir '.$dirname.
'.'.
"\n";
411 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.
Put here description of your class.
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)