From 4c13b6fd77688026dfbde65a180f32f4f1d8ab4e Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 14 Jun 2010 21:48:04 +0000 Subject: [PATCH] New: Debug widthrawal module --- ChangeLog | 2 +- htdocs/compta/facture/class/facture.class.php | 14 +- .../class/bon-prelevement.class.php | 60 +-- htdocs/compta/prelevement/create.php | 4 +- scripts/withdrawals/build_withdrawal_file.php | 342 +----------------- 5 files changed, 49 insertions(+), 373 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7a5db0bfa6b..2524a8a4253 100644 --- a/ChangeLog +++ b/ChangeLog @@ -34,7 +34,7 @@ For users: - New: Works even if Web hosting provider has disabled glob function. - New: Can now send supplier order by mail. - New: task #10076 : Show content of message in notification module. -- New: Widthrawal module is no more experimental. +- Fix: Debug experimental module widthrawal. - Fix: Format number was wrong for ar_AR language. - Fix: Can change password if has only permission change password. - Fix: Project PDF document show the tasks. diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 61837d758e9..28cf1f4a3aa 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -2577,9 +2577,9 @@ class Facture extends CommonObject /** - * \brief Cree une demande de prelevement - * \param user Utilisateur creant la demande - * \return int <0 si ko, >0 si ok + * \brief Create a withdrawal request for a standing order + * \param user User asking standing order + * \return int <0 if KO, >0 if OK */ function demande_prelevement($user) { @@ -2589,7 +2589,7 @@ class Facture extends CommonObject $soc->id = $this->socid; $soc->load_ban(); - if ($this->statut > 0 && $this->paye == 0 && $this->mode_reglement_id == 3) + if ($this->statut > 0 && $this->paye == 0) { $sql = 'SELECT count(*)'; $sql.= ' FROM '.MAIN_DB_PREFIX.'prelevement_facture_demande'; @@ -2623,7 +2623,7 @@ class Facture extends CommonObject } else { - $this->error="Une demande existe deja"; + $this->error="A request already exists"; dol_syslog('Facture::DemandePrelevement Impossible de creer une demande, demande deja en cours'); } } @@ -2636,8 +2636,8 @@ class Facture extends CommonObject } else { - $this->error="Etat facture incompatible avec l'action"; - dol_syslog("Facture::DemandePrelevement Etat facture incompatible $this->statut, $this->paye, $this->mode_reglement_id"); + $this->error="Status of invoice does not allow this"; + dol_syslog("Facture::DemandePrelevement ".$this->error." $this->statut, $this->paye, $this->mode_reglement_id"); return -3; } } diff --git a/htdocs/compta/prelevement/class/bon-prelevement.class.php b/htdocs/compta/prelevement/class/bon-prelevement.class.php index a7b0d45c5c8..a7f876ef597 100644 --- a/htdocs/compta/prelevement/class/bon-prelevement.class.php +++ b/htdocs/compta/prelevement/class/bon-prelevement.class.php @@ -674,11 +674,12 @@ class BonPrelevement extends CommonObject /** * \brief Create a withdraw + * \param mode real=do action, simu=test only * \return int <0 if KO, nbre of invoice withdrawed if OK */ - function Create($banque=0, $guichet=0) + function Create($banque=0, $guichet=0, $mode='real') { - global $conf; + global $conf,$langs; dol_syslog("BonPrelevement::Create banque=$banque guichet=$guichet"); @@ -694,7 +695,7 @@ class BonPrelevement extends CommonObject $month = strftime("%m", $datetimeprev); $year = strftime("%Y", $datetimeprev); - $user = new User($this->db, $conf->global->PRELEVEMENT_USER); + $puser = new User($this->db, $conf->global->PRELEVEMENT_USER); /** * Lectures des factures @@ -774,8 +775,8 @@ class BonPrelevement extends CommonObject } else { - dol_syslog("Erreur de RIB societe $fact->socid $soc->nom", LOG_ERR); - $facture_errors[$fac[0]]="Erreur de RIB societe $fact->socid $soc->nom"; + dol_syslog("Error on third party bank number RIB/IBAN $fact->socid $soc->nom", LOG_ERR); + $facture_errors[$fac[0]]="Error on third party bank number RIB/IBAN $fact->socid $soc->nom"; } } else @@ -785,31 +786,44 @@ class BonPrelevement extends CommonObject } else { - dol_syslog("Impossible de lire la facture", LOG_ERR); + dol_syslog("Failed to read invoice", LOG_ERR); } } } else { - dol_syslog("Aucune factures a traiter"); + dol_syslog("No invoice to process"); } } + $ok=0; // Withdraw invoices in factures_prev array - dol_syslog(sizeof($factures_prev)." invoices will be withdrawed"); + $out=sizeof($factures_prev)." invoices will be withdrawn."; + //print $out."\n"; + dol_syslog($out); - if (sizeof($factures_prev) > 0) + + if (sizeof($factures_prev) > 0) + { + if ($mode=='real') + { + $ok=1; + } + else + { + print "Option for real mode was not set, we stop after this simulation\n"; + } + } + + + if ($ok) { - /* - * Ouverture de la transaction - * - */ - $result=$this->db->begin(); - if ($result <= 0) - { - $error++; - } + /* + * We are in real mode. + * We create withdraw receipt, payments and build withdraw into disk + */ + $this->db->begin(); /* * Traitements @@ -843,13 +857,11 @@ class BonPrelevement extends CommonObject // Create withdraw receipt in database $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_bons ("; - $sql.= " ref"; - $sql.= ", entity"; - $sql.= ",datec"; + $sql.= " ref, entity, datec"; $sql.= ") VALUES ("; $sql.= "'".$ref."'"; $sql.= ", ".$conf->entity; - $sql.= ", ".$this->db->idate(mktime()); + $sql.= ", '".$this->db->idate(mktime())."'"; $sql.= ")"; dol_syslog("Bon-Prelevement::Create sql=".$sql, LOG_DEBUG); @@ -898,7 +910,7 @@ class BonPrelevement extends CommonObject $pai->paiementid = 3; // prelevement $pai->num_paiement = $ref; - if ($pai->create($user, 1) < 0) // on appelle en no_commit + if ($pai->create($puser, 1) < 0) // on appelle en no_commit { $error++; dol_syslog("Erreur creation paiement facture ".$fac[0]); @@ -985,6 +997,7 @@ class BonPrelevement extends CommonObject $bonprev->factures = $factures_prev_id; + // Build file $bonprev->generate(); } dol_syslog( $filebonprev ) ; @@ -1020,6 +1033,7 @@ class BonPrelevement extends CommonObject else { $this->db->rollback(); + dol_syslog("Error",LOG_ERROR); } return sizeof($factures_prev); diff --git a/htdocs/compta/prelevement/create.php b/htdocs/compta/prelevement/create.php index 54762a78332..36ba129042d 100644 --- a/htdocs/compta/prelevement/create.php +++ b/htdocs/compta/prelevement/create.php @@ -113,9 +113,9 @@ if ($nb) { print "
\n"; - if ($nb) print ''.$langs->trans("CreateAll")."\n"; + if ($nb) print ''.$langs->trans("CreateAll")."\n"; if ($nb11) print ''.$langs->trans("CreateBanque")."\n"; - if ($nb1) print ''.$langs->trans("CreateGuichet")."\n"; + if ($nb1) print ''.$langs->trans("CreateGuichet")."\n"; print "
\n"; } diff --git a/scripts/withdrawals/build_withdrawal_file.php b/scripts/withdrawals/build_withdrawal_file.php index 69424b7bdf3..ab9585f9909 100644 --- a/scripts/withdrawals/build_withdrawal_file.php +++ b/scripts/withdrawals/build_withdrawal_file.php @@ -62,348 +62,10 @@ if (! isset($argv[1])) { // Check parameters exit; } -$factures = array(); -$factures_prev = array(); -if (!$error) -{ +$withdrawreceipt=new BonPrelevement($db); +$result=$withdrawreceipt->create($conf->global->PRELEVEMENT_CODE_BANQUE,$conf->global->PRELEVEMENT_CODE_GUICHET,$argv[1]); - $sql = "SELECT f.rowid, pfd.rowid as pfdrowid, f.fk_soc"; - $sql.= ", pfd.code_banque, pfd.code_guichet, pfd.number, pfd.cle_rib"; - $sql.= ", pfd.amount"; - $sql.= ", s.nom"; - $sql.= " FROM ".MAIN_DB_PREFIX."facture as f"; - $sql.= ", ".MAIN_DB_PREFIX."societe as s"; - $sql.= ", ".MAIN_DB_PREFIX."prelevement_facture_demande as pfd"; - $sql.= " WHERE f.rowid = pfd.fk_facture"; - $sql.= " AND f.entity = ".$conf->entity; - $sql.= " AND s.rowid = f.fk_soc"; - $sql.= " AND f.fk_statut = 1"; - $sql.= " AND f.paye = 0"; - $sql.= " AND pfd.traite = 0"; - $sql.= " AND f.total_ttc > 0"; - $sql.= " AND f.fk_mode_reglement = 3"; - - $resql= $db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - $i = 0; - - while ($i < $num) - { - $row = $db->fetch_row($resql); - - $factures[$i] = $row; - - $i++; - } - $db->free($resql); - dol_syslog($i." invoices to withdraw."); - } - else - { - $error = 1; - dol_syslog("Erreur -1"); - dol_syslog($db->error()); - } -} - -/* - * - * Verif des clients - * - */ - -if (!$error) -{ - /* - * Verification des RIB - * - */ - $i = 0; - print "Start to check bank numbers RIB/IBAN.\n"; - - if (sizeof($factures) > 0) - { - foreach ($factures as $fac) - { - $fact = new Facture($db); - - if ($fact->fetch($fac[0]) == 1) - { - $soc = new Societe($db); - if ($soc->fetch($fact->socid) == 1) - { - if ($soc->verif_rib() == 1) - { - $factures_prev[$i] = $fac; - /* second tableau necessaire pour bon-prelevement */ - $factures_prev_id[$i] = $fac[0]; - $i++; - } - else - { - print "Bad value for bank RIB/IBAN: Third party id=".$fact->socid.", name=".$soc->nom."\n"; - dol_syslog("Bad value for bank RIB/IBAN ".$fact->socid." ".$soc->nom); - } - } - else - { - print "Failed to read third party\n"; - dol_syslog("Failed to read third party"); - } - } - else - { - - print "Failed to read invoice\n"; - dol_syslog("Failed to read invoice"); - } - } - } - else - { - print "No invoices to process\n"; - dol_syslog("No invoice to process"); - } -} - - - - -/* - * Run withdrawal - */ - -$ok=0; - -$out=sizeof($factures_prev)." invoices will be withdrawn."; -print $out."\n"; -dol_syslog($out); - -if (sizeof($factures_prev) > 0) -{ - if ($argv[1]==='real') - { - $ok=1; - } - else - { - print "Option for real mode was not set, we stop after this simulation\n"; - } -} - -if ($ok) -{ - /* - * We are in real mode. - * We create withdraw receipt, payments and build withdraw into disk - */ - - $db->begin(); - - if (!$error) - { - $ref = "T".substr($year,-2).$month; - - $sql = "SELECT count(*)"; - $sql.= " FROM ".MAIN_DB_PREFIX."prelevement_bons"; - $sql.= " WHERE ref LIKE '".$ref."%'"; - $sql.= " AND entity = ".$conf->entity; - - $resql=$db->query($sql); - if ($resql) - { - $row = $db->fetch_row($resql); - } - else - { - $error++; - dol_syslog("Erreur recherche reference"); - } - - $ref = $ref . substr("00".($row[0]+1), -2); - - $filebonprev = $ref; - - /* - * Creation du bon de prelevement - */ - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."prelevement_bons ("; - $sql.= "ref"; - $sql.= ", entity"; - $sql.= ", datec"; - $sql.= ") VALUES ("; - $sql.= "'".$ref."'"; - $sql.= ", ".$conf->entity; - $sql.= ", '".$db->idate(mktime())."'"; - $sql.= ")"; - - $resql=$db->query($sql); - if ($resql) - { - $prev_id = $db->last_insert_id(MAIN_DB_PREFIX."prelevement_bons"); - - $bonprev = new BonPrelevement($db, $conf->prelevement->dir_output."/receipts/".$filebonprev); - $bonprev->id = $prev_id; - } - else - { - $error++; - dol_syslog("Failed to create withdrawal ticket"); - } - - } - - - if (!$error) - { - dol_syslog("Start generation of payments"); - dol_syslog("Number of invoices: ".sizeof($factures_prev)); - - if (sizeof($factures_prev) > 0) - { - foreach ($factures_prev as $fac) - { - $fact = new Facture($db); - $fact->fetch($fac[0]); - - $pai = new Paiement($db); - - $pai->amounts = array(); - $pai->amounts[$fac[0]] = $fact->total_ttc; - $pai->datepaye = $datetimeprev; - $pai->paiementid = 3; // prelevement - $pai->num_paiement = $ref; - - if ($pai->create($user, 1) == -1) // on appelle en no_commit - { - $error++; - $out="Failed to create payments for invoice ".$fac[0]; - print $out."\n"; - dol_syslog($out); - } - else - { - /* - * Validation du paiement - */ - $pai->valide(); - - /* - * Ajout d'une ligne de prelevement - * - * - * $fac[3] : banque - * $fac[4] : guichet - * $fac[5] : number - * $fac[6] : cle rib - * $fac[7] : amount - * $fac[8] : client nom - * $fac[2] : client id - */ - - $ri = $bonprev->AddFacture($fac[0], $fac[2], $fac[8], $fac[7], - $fac[3], $fac[4], $fac[5], $fac[6]); - if ($ri <> 0) - { - $error++; - } - - /* - * Mise a jour des demandes - * - */ - $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_facture_demande"; - $sql.= " SET traite = 1"; - $sql.= ", date_traite = '".$db->idate(mktime())."'"; - $sql.= ", fk_prelevement_bons = ".$prev_id; - $sql.= " WHERE rowid = ".$fac[1]; - - if ($db->query($sql)) - { - - } - else - { - $error++; - dol_syslog("Erreur mise a jour des demandes ".$db->error()); - } - - } - } - } - - dol_syslog("End payments"); - } - - if (!$error) - { - /* - * Bon de Prelevement - */ - - dol_syslog("Start generation of widthdrawal"); - dol_syslog("Number of invoices ".sizeof($factures_prev)); - - if (sizeof($factures_prev) > 0) - { - $bonprev->date_echeance = $datetimeprev; - $bonprev->reference_remise = $ref; - - - $bonprev->numero_national_emetteur = $conf->global->PRELEVEMENT_NUMERO_NATIONAL_EMETTEUR; - $bonprev->raison_sociale = $conf->global->PRELEVEMENT_RAISON_SOCIALE; - - $bonprev->emetteur_code_etablissement = $conf->global->PRELEVEMENT_CODE_BANQUE; - $bonprev->emetteur_code_guichet = $conf->global->PRELEVEMENT_CODE_GUICHET; - $bonprev->emetteur_numero_compte = $conf->global->PRELEVEMENT_NUMERO_COMPTE; - - - $bonprev->factures = $factures_prev_id; - - // Build file - $bonprev->generate(); - } - dol_syslog( $filebonprev ) ; - dol_syslog("Fin prelevement"); - } - - /* - * Mise a jour du total - * - */ - - $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_bons"; - $sql.= " SET amount = ".price2num($bonprev->total); - $sql.= " WHERE rowid = ".$prev_id; - $sql.= " AND entity = ".$conf->entity; - - if (!$db->query($sql)) - { - $error++; - dol_syslog("Erreur mise a jour du total"); - dol_syslog($sql); - } - - /* - * Rollback ou Commit - * - */ - if (!$error) - { - $db->commit(); - } - else - { - $db->rollback(); - dol_syslog("Error",LOG_ERROR); - } -} $db->close(); - -// FIN ?>