From 85eac2992596279cab8064575b0dc3ad0d621a64 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 15 May 2013 19:30:32 +0200 Subject: [PATCH] New: Add balance before and after column --- htdocs/compta/bank/releve.php | 2 +- htdocs/langs/en_US/banks.lang | 2 + htdocs/langs/fr_FR/banks.lang | 2 + scripts/bank/export-bank-receipts.php | 492 ++++++++++++++------------ 4 files changed, 278 insertions(+), 220 deletions(-) diff --git a/htdocs/compta/bank/releve.php b/htdocs/compta/bank/releve.php index 3c02d6fd9db..2d9ac9cd86e 100644 --- a/htdocs/compta/bank/releve.php +++ b/htdocs/compta/bank/releve.php @@ -277,8 +277,8 @@ else $sql.= " AND b.fk_account = ba.rowid"; $sql.= $db->order("b.datev, b.datec", "ASC"); // We add date of creation to have correct order when everything is done the same day + dol_syslog("sql=".$sql); $result = $db->query($sql); - if ($result) { $var=True; diff --git a/htdocs/langs/en_US/banks.lang b/htdocs/langs/en_US/banks.lang index de84677fc23..7a9b5644255 100644 --- a/htdocs/langs/en_US/banks.lang +++ b/htdocs/langs/en_US/banks.lang @@ -20,6 +20,8 @@ SavingAccount=Savings account SavingAccounts=Savings accounts ErrorBankLabelAlreadyExists=Financial account label already exists BankBalance=Balance +BankBalanceBefore=Balance before +BankBalanceAfter=Balance after BalanceMinimalAllowed=Minimum allowed balance BalanceMinimalDesired=Minimum desired balance InitialBankBalance=Initial balance diff --git a/htdocs/langs/fr_FR/banks.lang b/htdocs/langs/fr_FR/banks.lang index c51d915602f..bcc281626e1 100644 --- a/htdocs/langs/fr_FR/banks.lang +++ b/htdocs/langs/fr_FR/banks.lang @@ -20,6 +20,8 @@ SavingAccount=Compte épargne SavingAccounts=Comptes épargne/placements ErrorBankLabelAlreadyExists=Libellé de compte financier déjà existant BankBalance=Solde +BankBalanceBefore=Solde avant +BankBalanceAfter=Solde après BalanceMinimalAllowed=Solde minimum autorisé BalanceMinimalDesired=Solde minimum désiré InitialBankBalance=Solde initial diff --git a/scripts/bank/export-bank-receipts.php b/scripts/bank/export-bank-receipts.php index 08ca4a40ec9..1d71c4edfe4 100755 --- a/scripts/bank/export-bank-receipts.php +++ b/scripts/bank/export-bank-receipts.php @@ -119,9 +119,14 @@ $acct=new Account($db); $result=$acct->fetch('',$bankref); if ($result <= 0) { - print "Failed to find bank account with ref ".$bankref."\n"; + print "Failed to find bank account with ref ".$bankref.".\n"; exit; } +else +{ + print "Export for bank account ".$acct->ref." (".$acct->label.").\n"; +} + // Creation de la classe d'export du model ExportXXX $dir = DOL_DOCUMENT_ROOT . "/core/modules/export/"; @@ -141,248 +146,297 @@ $dirname = $conf->banque->dir_temp; $filename = 'export-bank-receipts-'.$bankref.'-'.$num.'.'.$objmodel->extension; -// Open file -print 'Create file '.$filename.' into directory '.$dirname."\n"; -dol_mkdir($dirname); -$result=$objmodel->open_file($dirname."/".$filename, $outputlangs); - -if ($result >= 0) +$array_fields=array( +'bankreceipt'=>$outputlangs->transnoentitiesnoconv("AccountStatementShort"), 'bankaccount'=>$outputlangs->transnoentitiesnoconv("BankAccount"), +'dateop'=>$outputlangs->transnoentitiesnoconv("DateOperationShort"),'dateval'=>$outputlangs->transnoentitiesnoconv("DateValueShort"),'type'=>$outputlangs->transnoentitiesnoconv("Type"), +'description'=>$outputlangs->transnoentitiesnoconv("Description"), 'thirdparty'=>$outputlangs->transnoentitiesnoconv("Tiers"), 'accountelem'=>$outputlangs->transnoentitiesnoconv("Piece"), +'debit'=>$outputlangs->transnoentitiesnoconv("Debit"), 'credit'=>$outputlangs->transnoentitiesnoconv("Credit"), +'soldbefore'=>$outputlangs->transnoentitiesnoconv("BankBalanceBefore"), 'soldafter'=>$outputlangs->transnoentitiesnoconv("BankBalanceAfter"), +'comment'=>$outputlangs->transnoentitiesnoconv("Comment") +); +$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' +); +$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' +); + + +// Recherche les ecritures pour le releve +$listofnum="'"; +$arraynum=explode(',',$num); +foreach($arraynum as $val) { - $numrows=0; + if ($listofnum != "'") $listofnum.="','"; + $listofnum.=$val; +} +$listofnum.="'"; +$sql = "SELECT b.rowid, b.dateo as do, b.datev as dv,"; +$sql.= " b.amount, b.label, b.rappro, b.num_releve, b.num_chq, b.fk_type,"; +$sql.= " ba.rowid as bankid, ba.ref as bankref, ba.label as banklabel"; +$sql.= " FROM ".MAIN_DB_PREFIX."bank_account as ba"; +$sql.= ", ".MAIN_DB_PREFIX."bank as b"; +$sql.= " WHERE b.num_releve IN (".$listofnum.")"; +if (!isset($num)) $sql.= " OR b.num_releve is null"; +$sql.= " AND b.fk_account = ".$acct->id; +$sql.= " AND b.fk_account = ba.rowid"; +$sql.= $db->order("b.num_releve, b.datev, b.datec", "ASC"); // We add date of creation to have correct order when everything is done the same day +//print $sql; +$resql=$db->query($sql); +if ($resql) +{ + $balancebefore=array(); - $array_fields=array( - 'bankreceipt'=>$outputlangs->transnoentitiesnoconv("AccountStatementShort"), 'bankaccount'=>$outputlangs->transnoentitiesnoconv("BankAccount"), - 'dateop'=>$outputlangs->transnoentitiesnoconv("DateOperationShort"),'dateval'=>$outputlangs->transnoentitiesnoconv("DateValueShort"),'type'=>$outputlangs->transnoentitiesnoconv("Type"), - 'description'=>$outputlangs->transnoentitiesnoconv("Description"), 'thirdparty'=>$outputlangs->transnoentitiesnoconv("Tiers"), 'accountelem'=>$outputlangs->transnoentitiesnoconv("Piece"), - 'debit'=>$outputlangs->transnoentitiesnoconv("Debit"), 'credit'=>$outputlangs->transnoentitiesnoconv("Credit"), 'sold'=>$outputlangs->transnoentitiesnoconv("Solde"), - 'comment'=>$outputlangs->transnoentitiesnoconv("Comment") - ); - $array_selected=array( - 'bankreceipt'=>'bankreceipt', 'bankaccount'=>'bankaccount', - 'dateop'=>'dateop','dateval'=>'dateval','type'=>'type', - 'description'=>'description', 'thirdparty'=>'thirdparty', 'accountelem'=>'accountelem', - 'debit'=>'debit', 'credit'=>'credit', 'sold'=>'sold', - 'comment'=>'comment' - ); - $array_export_TypeFields=array( - 'bankreceipt'=>'Text', 'bankaccount'=>'Text', - 'dateop'=>'Date','dateval'=>'Date','type'=>'Text', - 'description'=>'Text', 'thirdparty'=>'Text', 'accountelem'=>'Text', - 'debit'=>'Number', 'credit'=>'Number', 'sold'=>'Number', - 'comment'=>'Text' - ); + $numrows = $db->num_rows($resql); - // Genere en-tete - $objmodel->write_header($outputlangs); - - // Genere ligne de titre - $objmodel->write_title($array_fields,$array_selected,$outputlangs,$array_export_TypeFields); - - - // Recherche les ecritures pour le releve - $sql = "SELECT b.rowid, b.dateo as do, b.datev as dv,"; - $sql.= " b.amount, b.label, b.rappro, b.num_releve, b.num_chq, b.fk_type,"; - $sql.= " ba.rowid as bankid, ba.ref as bankref, ba.label as banklabel"; - $sql.= " FROM ".MAIN_DB_PREFIX."bank_account as ba"; - $sql.= ", ".MAIN_DB_PREFIX."bank as b"; - $sql.= " WHERE b.num_releve IN (".$db->escape($num).")"; - if (!isset($num)) $sql.= " OR b.num_releve is null"; - //$sql.= " AND b.fk_account = ".$acct->id; - $sql.= " AND b.fk_account = ba.rowid"; - $sql.= $db->order("b.datev, b.datec", "ASC"); // We add date of creation to have correct order when everything is done the same day - - $resql=$db->query($sql); - if ($resql) + if ($numrows > 0) { - $numrows = $db->num_rows($resql); + // Open file + print 'Open file '.$filename.' into directory '.$dirname."\n"; + dol_mkdir($dirname); + $result=$objmodel->open_file($dirname."/".$filename, $outputlangs); + + if ($result < 0) + { + print 'Failed to create file '.$filename.' into dir '.$dirname.'.'."\n"; + return -1; + } + + // Genere en-tete + $objmodel->write_header($outputlangs); + + // Genere ligne de titre + $objmodel->write_title($array_fields,$array_selected,$outputlangs,$array_export_TypeFields); + } - $i=0; - while ($i < $numrows) + $i=0; + while ($i < $numrows) + { + $thirdparty=''; + $accountelem=''; + $comment=''; + + $objp = $db->fetch_object($resql); + + // Calculate start balance + if (! isset($balancebefore[$objp->num_releve])) { - //print "Lines ".($i+1)."\n"; - $thirdparty=''; - $accountelem=''; - $comment=''; - - $objp = $db->fetch_object($resql); - $total = $total + $objp->amount; - - $var=!$var; - - // Date operation - $dateop=$db->jdate($objp->do); - - // Date de valeur - $datevalue=$db->jdate($objp->dv); - - // Num cheque - $numchq=($objp->num_chq?$objp->num_chq:''); - - // Libelle - $reg=array(); - preg_match('/\((.+)\)/i',$objp->label,$reg); // Si texte entoure de parenthese on tente recherche de traduction - if ($reg[1] && $langs->transnoentitiesnoconv($reg[1])!=$reg[1]) $description=$langs->transnoentitiesnoconv($reg[1]); - else $description=$objp->label; + print 'Calculate start balance for receipt '.$objp->num_releve."\n"; - /* - * Ajout les liens (societe, company...) - */ - $links = $acct->get_url($objp->rowid); - foreach($links as $key=>$val) + $sql2 = "SELECT sum(b.amount) as amount"; + $sql2.= " FROM ".MAIN_DB_PREFIX."bank as b"; + $sql2.= " WHERE b.num_releve < '".$db->escape($objp->num_releve)."'"; + $sql2.= " AND b.fk_account = ".$objp->bankid; + $resql2=$db->query($sql2); + if ($resql2) { - if ($links[$key]['type']=='payment') - { - $paymentstatic->fetch($links[$key]['url_id']); - $tmparray=$paymentstatic->getBillsArray(''); - foreach($tmparray as $key => $val) - { - $invoicestatic->fetch($val); - if ($accountelem) $accountelem.= ', '; - $accountelem.=$invoicestatic->ref; - } - } - elseif ($links[$key]['type']=='payment_supplier') - { - $paymentsupplierstatic->fetch($links[$key]['url_id']); - $tmparray=$paymentsupplierstatic->getBillsArray(''); - foreach($tmparray as $key => $val) - { - $invoicesupplierstatic->fetch($val); - if ($accountelem) $accountelem.= ', '; - $accountelem.=$invoicesupplierstatic->ref; - } - } - elseif ($links[$key]['type']=='payment_sc') - { - $paymentsocialcontributionstatic->fetch($links[$key]['url_id']); - if ($accountelem) $accountelem.= ', '; - $accountelem.=$langs->transnoentitiesnoconv("SocialContribution").' '.$paymentsocialcontributionstatic->ref; - } - elseif ($links[$key]['type']=='payment_vat') - { - $paymentvatstatic->fetch($links[$key]['url_id']); - if ($accountelem) $accountelem.= ', '; - $accountelem.=$langs->transnoentitiesnoconv("VATPayments").' '.$paymentvatstatic->ref; - } - elseif ($links[$key]['type']=='banktransfert') - { - $comment=$outputlangs->transnoentitiesnoconv("Transfer"); - if ($objp->amount > 0) - { - if ($comment) $comment.= ' '; - $banklinestatic->fetch($links[$key]['url_id']); - $bankstatic->id=$banklinestatic->fk_account; - $bankstatic->label=$banklinestatic->bank_account_label; - $comment.= ' ('.$langs->transnoentitiesnoconv("from").' '; - $comment.= $bankstatic->getNomUrl(1,'transactions'); - $comment.= ' '.$langs->transnoentitiesnoconv("toward").' '; - $bankstatic->id=$objp->bankid; - $bankstatic->label=$objp->bankref; - $comment.= $bankstatic->getNomUrl(1,''); - $comment.= ')'; - } - else - { - if ($comment) $comment.= ' '; - $bankstatic->id=$objp->bankid; - $bankstatic->label=$objp->bankref; - $comment.= ' ('.$langs->transnoentitiesnoconv("from").' '; - $comment.= $bankstatic->getNomUrl(1,''); - $comment.= ' '.$langs->transnoentitiesnoconv("toward").' '; - $banklinestatic->fetch($links[$key]['url_id']); - $bankstatic->id=$banklinestatic->fk_account; - $bankstatic->label=$banklinestatic->bank_account_label; - $comment.= $bankstatic->getNomUrl(1,'transactions'); - $comment.= ')'; - } - } - elseif ($links[$key]['type']=='company') - { - if ($thirdparty) $thirdparty.= ', '; - $thirdparty.= dol_trunc($links[$key]['label'],24); - $newline=0; - } - elseif ($links[$key]['type']=='member') - { - if ($thirdparty) $accountelem.= ', '; - $thirdparty.= $links[$key]['label']; - $newline=0; - } - /*elseif ($links[$key]['type']=='sc') - { - if ($accountelem) $accountelem.= ', '; - //$accountelem.= ''; - //$accountelem.= img_object($langs->transnoentitiesnoconv('ShowBill'),'bill').' '; - $accountelem.= $langs->transnoentitiesnoconv("SocialContribution"); - //$accountelem.= ''; - $newline=0; - } - else - { - if ($accountelem) $accountelem.= ', '; - //$accountelem.= ''; - $accountelem.= $links[$key]['label']; - //$accountelem.= ''; - $newline=0; - }*/ + $obj2=$db->fetch_object($resql2); + $balancebefore[$objp->num_releve] = ($obj2->amount?$obj2->amount:0); + $db->free($resql2); + } + else + { + dol_print_error($db); + exit; } - $debit=$credit=''; - if ($objp->amount < 0) - { - $totald = $totald + abs($objp->amount); - $debit=price2num($objp->amount * -1); - } - else - { - $totalc = $totalc + abs($objp->amount); - $credit=price2num($objp->amount); - } - - $i++; - - $rec=new stdClass(); - $rec->bankreceipt=$objp->num_releve; - $rec->bankaccount=$objp->banklabel; - $rec->dateop=dol_print_date($dateop,'dayrfc'); - $rec->dateval=dol_print_date($datevalue,'dayrfc'); - $rec->type=$objp->fk_type.' '.($objp->num_chq?$objp->num_chq:''); - $rec->description=$description; - $rec->thirdparty=$thirdparty; - $rec->accountelem=$accountelem; - $rec->debit=$debit; - $rec->credit=$credit; - $rec->sold=$sold; - $rec->comment=$comment; - - // end of special operation processing - $objmodel->write_record($array_selected,$rec,$outputlangs,$array_export_TypeFields); + $total = $balancebefore[$objp->num_releve]; } + $totalbefore = $total; + $total = $total + $objp->amount; + + $var=!$var; + + // Date operation + $dateop=$db->jdate($objp->do); + + // Date de valeur + $datevalue=$db->jdate($objp->dv); + + // Num cheque + $numchq=($objp->num_chq?$objp->num_chq:''); + + // Libelle + $reg=array(); + preg_match('/\((.+)\)/i',$objp->label,$reg); // Si texte entoure de parenthese on tente recherche de traduction + if ($reg[1] && $langs->transnoentitiesnoconv($reg[1])!=$reg[1]) $description=$langs->transnoentitiesnoconv($reg[1]); + else $description=$objp->label; + + /* + * Ajout les liens (societe, company...) + */ + $links = $acct->get_url($objp->rowid); + foreach($links as $key=>$val) + { + if ($links[$key]['type']=='payment') + { + $paymentstatic->fetch($links[$key]['url_id']); + $tmparray=$paymentstatic->getBillsArray(''); + foreach($tmparray as $key => $val) + { + $invoicestatic->fetch($val); + if ($accountelem) $accountelem.= ', '; + $accountelem.=$invoicestatic->ref; + } + } + elseif ($links[$key]['type']=='payment_supplier') + { + $paymentsupplierstatic->fetch($links[$key]['url_id']); + $tmparray=$paymentsupplierstatic->getBillsArray(''); + foreach($tmparray as $key => $val) + { + $invoicesupplierstatic->fetch($val); + if ($accountelem) $accountelem.= ', '; + $accountelem.=$invoicesupplierstatic->ref; + } + } + elseif ($links[$key]['type']=='payment_sc') + { + $paymentsocialcontributionstatic->fetch($links[$key]['url_id']); + if ($accountelem) $accountelem.= ', '; + $accountelem.=$langs->transnoentitiesnoconv("SocialContribution").' '.$paymentsocialcontributionstatic->ref; + } + elseif ($links[$key]['type']=='payment_vat') + { + $paymentvatstatic->fetch($links[$key]['url_id']); + if ($accountelem) $accountelem.= ', '; + $accountelem.=$langs->transnoentitiesnoconv("VATPayments").' '.$paymentvatstatic->ref; + } + elseif ($links[$key]['type']=='banktransfert') + { + $comment=$outputlangs->transnoentitiesnoconv("Transfer"); + if ($objp->amount > 0) + { + if ($comment) $comment.= ' '; + $banklinestatic->fetch($links[$key]['url_id']); + $bankstatic->id=$banklinestatic->fk_account; + $bankstatic->label=$banklinestatic->bank_account_label; + $comment.= ' ('.$langs->transnoentitiesnoconv("from").' '; + $comment.= $bankstatic->getNomUrl(1,'transactions'); + $comment.= ' '.$langs->transnoentitiesnoconv("toward").' '; + $bankstatic->id=$objp->bankid; + $bankstatic->label=$objp->bankref; + $comment.= $bankstatic->getNomUrl(1,''); + $comment.= ')'; + } + else + { + if ($comment) $comment.= ' '; + $bankstatic->id=$objp->bankid; + $bankstatic->label=$objp->bankref; + $comment.= ' ('.$langs->transnoentitiesnoconv("from").' '; + $comment.= $bankstatic->getNomUrl(1,''); + $comment.= ' '.$langs->transnoentitiesnoconv("toward").' '; + $banklinestatic->fetch($links[$key]['url_id']); + $bankstatic->id=$banklinestatic->fk_account; + $bankstatic->label=$banklinestatic->bank_account_label; + $comment.= $bankstatic->getNomUrl(1,'transactions'); + $comment.= ')'; + } + } + elseif ($links[$key]['type']=='company') + { + if ($thirdparty) $thirdparty.= ', '; + $thirdparty.= dol_trunc($links[$key]['label'],24); + $newline=0; + } + elseif ($links[$key]['type']=='member') + { + if ($thirdparty) $accountelem.= ', '; + $thirdparty.= $links[$key]['label']; + $newline=0; + } + /*elseif ($links[$key]['type']=='sc') + { + if ($accountelem) $accountelem.= ', '; + //$accountelem.= ''; + //$accountelem.= img_object($langs->transnoentitiesnoconv('ShowBill'),'bill').' '; + $accountelem.= $langs->transnoentitiesnoconv("SocialContribution"); + //$accountelem.= ''; + $newline=0; + } + else + { + if ($accountelem) $accountelem.= ', '; + //$accountelem.= ''; + $accountelem.= $links[$key]['label']; + //$accountelem.= ''; + $newline=0; + }*/ + } + + $debit=$credit=''; + if ($objp->amount < 0) + { + $totald = $totald + abs($objp->amount); + $debit=price2num($objp->amount * -1); + } + else + { + $totalc = $totalc + abs($objp->amount); + $credit=price2num($objp->amount); + } + + $i++; + + $rec=new stdClass(); + $rec->bankreceipt=$objp->num_releve; + $rec->bankaccount=$objp->banklabel; + $rec->dateop=dol_print_date($dateop,'dayrfc'); + $rec->dateval=dol_print_date($datevalue,'dayrfc'); + $rec->type=$objp->fk_type.' '.($objp->num_chq?$objp->num_chq:''); + $rec->description=$description; + $rec->thirdparty=$thirdparty; + $rec->accountelem=$accountelem; + $rec->debit=$debit; + $rec->credit=$credit; + $rec->sold=$sold; + $rec->comment=$comment; + $rec->soldbefore=price2num($totalbefore); + $rec->soldafter=price2num($total); + + // end of special operation processing + $objmodel->write_record($array_selected,$rec,$outputlangs,$array_export_TypeFields); } - else dol_print_error($db); - print "Found ".$numrows." records for receipt ".$num."\n"; + if ($numrows > 0) + { + print "Found ".$numrows." records for receipt ".$num."\n"; - // Genere en-tete - $objmodel->write_footer($outputlangs); + // Genere en-tete + $objmodel->write_footer($outputlangs); - // Close file - $objmodel->close_file(); + // Close file + $objmodel->close_file(); - print 'File '.$filename.' was generated into dir '.$dirname.'.'."\n"; + print 'File '.$filename.' was generated into dir '.$dirname.'.'."\n"; - $ret=0; + $ret=0; + } + else + { + print "No records found for receipt ".$num."\n"; + + $ret=0; + } } else { - print 'Failed to create file '.$filename.' into dir '.$dirname.'.'."\n"; - + dol_print_error($db); $ret=-1; } - - $db->close(); return $ret;