From 08dd4483572fe92c29bfc51f94f180c96295f415 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 9 Sep 2010 22:45:05 +0000 Subject: [PATCH] Work on invoice creation when creating subscription --- htdocs/adherents/card_subscriptions.php | 68 ++++++++++++++++--- htdocs/adherents/class/adherent.class.php | 44 +----------- .../compta/paiement/class/paiement.class.php | 13 ++-- 3 files changed, 67 insertions(+), 58 deletions(-) diff --git a/htdocs/adherents/card_subscriptions.php b/htdocs/adherents/card_subscriptions.php index 8e2eb19c19f..7938a1c3104 100644 --- a/htdocs/adherents/card_subscriptions.php +++ b/htdocs/adherents/card_subscriptions.php @@ -235,7 +235,7 @@ if ($user->rights->adherent->cotisation->creer && $_POST["action"] == 'cotisatio { $db->begin(); - // Create subscription (and bank record if option is 'bankdirect') + // Create subscription $crowid=$adh->cotisation($datecotisation, $cotisation, $accountid, $operation, $label, $num_chq, $emetteur_nom, $emetteur_banque, $datesubend, $option); if ($crowid <= 0) { @@ -245,9 +245,47 @@ if ($user->rights->adherent->cotisation->creer && $_POST["action"] == 'cotisatio if (! $error) { - if ($option == 'bankviainvoice' || $option == 'invoiceonly') + // Insert into bank account directlty (if option choosed for) + link to llx_cotisation if option is 'bankdirect' + if ($option == 'bankdirect' && $accountid) + { + require_once(DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'); + + $acct=new Account($db); + $result=$acct->fetch($accountid); + + $dateop=time(); + + $insertid=$acct->addline($dateop, $operation, $label, $cotisation, $num_chq, '', $user, $emetteur_nom, $emetteur_banque); + if ($insertid > 0) + { + $inserturlid=$acct->add_url_line($insertid, $adh->id, DOL_URL_ROOT.'/adherents/fiche.php?rowid=', $adh->getFullname($langs), 'member'); + if ($inserturlid > 0) + { + // Met a jour la table cotisation + $sql ="UPDATE ".MAIN_DB_PREFIX."cotisation SET fk_bank=".$insertid; + $sql.=" WHERE rowid=".$crowid; + + dol_syslog("Adherent::cotisation sql=".$sql); + $resql = $db->query($sql); + if (! $resql) + { + $error=$db->lasterror(); + } + } + else + { + $error=$acct->error; + } + } + else + { + $error=$acct->error; + } + } + + // If option choosed, we create invoice + if (($option == 'bankviainvoice' && $accountid) || $option == 'invoiceonly') { - // If option choosed, we create invoice require_once(DOL_DOCUMENT_ROOT."/compta/facture/class/facture.class.php"); require_once(DOL_DOCUMENT_ROOT."/compta/facture/class/paymentterm.class.php"); @@ -297,8 +335,8 @@ if ($user->rights->adherent->cotisation->creer && $_POST["action"] == 'cotisatio // Validate invoice $result=$invoice->validate($user); - // Add payment - if ($option == 'bankviainvoice') + // Add payment on invoice + if ($option == 'bankviainvoice' && $accountid) { require_once(DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php'); require_once(DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'); @@ -324,12 +362,22 @@ if ($user->rights->adherent->cotisation->creer && $_POST["action"] == 'cotisatio if (! $error) { - $result=$paiement->addPaymentToBank($user,'payment','(SubscriptionPayment)',$accountid,$emetteur_nom,$emetteur_banque); - if (! $result > 0) + $bank_line_id=$paiement->addPaymentToBank($user,'payment','(SubscriptionPayment)',$accountid,$emetteur_nom,$emetteur_banque); + if (! $bank_line_id > 0) { $errmsg=$paiement->error; $error++; } + + // Update fk_bank for subscriptions + $sql = 'UPDATE llx_cotisation set fk_bank='.$bank_line_id; + $sql.= ' WHERE rowid='.$crowid; + dol_syslog('sql='.$sql); + $result = $db->query($sql); + if (! $result) + { + $error++; + } } } } @@ -662,7 +710,9 @@ if ($rowid) jQuery("#invoiceonly").click(function() { jQuery(".bankswitchclass").show(); }); - });'; + '; + if (GETPOST('paymentsave')) print 'jQuery("#'.GETPOST('paymentsave').'").attr(\'checked\',true);'; + print '});'; print ''."\n"; } @@ -720,7 +770,7 @@ if ($rowid) print ''; - // Bank account transaction + // Complementary action if ($conf->banque->enabled || $conf->facture->enabled) { $company=new Societe($db); diff --git a/htdocs/adherents/class/adherent.class.php b/htdocs/adherents/class/adherent.class.php index 2633b578ad1..b7ae82ae61d 100644 --- a/htdocs/adherents/class/adherent.class.php +++ b/htdocs/adherents/class/adherent.class.php @@ -1143,10 +1143,9 @@ class Adherent extends CommonObject * \param emetteur_nom Nom emetteur cheque * \param emetteur_banque Nom banque emetteur cheque * \param datesubend Date fin adhesion - * \param option 'none'=No more action, 'bankdirect'=Add direct bank transaction, 'bankviainvoice'=Add bank transaction via invoice and payment * \return int rowid de l'entree ajoutee, <0 si erreur */ - function cotisation($date, $montant, $accountid=0, $operation='', $label='', $num_chq='', $emetteur_nom='', $emetteur_banque='', $datesubend=0, $option='none') + function cotisation($date, $montant, $accountid=0, $operation='', $label='', $num_chq='', $emetteur_nom='', $emetteur_banque='', $datesubend=0) { global $conf,$langs,$user; @@ -1188,47 +1187,6 @@ class Adherent extends CommonObject $result=$adh->add_to_mailman($conf->global->ADHERENT_MAILMAN_LISTS_COTISANT); } - // Insert into bank account directlty (if option choosed for) - if ($option == 'bankdirect' && $accountid) - { - $acct=new Account($this->db); - $result=$acct->fetch($accountid); - - $dateop=time(); - - $insertid=$acct->addline($dateop, $operation, $label, $montant, $num_chq, '', $user, $emetteur_nom, $emetteur_banque); - if ($insertid > 0) - { - $inserturlid=$acct->add_url_line($insertid, $this->id, DOL_URL_ROOT.'/adherents/fiche.php?rowid=', $this->getFullname($langs), 'member'); - if ($inserturlid > 0) - { - // Met a jour la table cotisation - $sql="UPDATE ".MAIN_DB_PREFIX."cotisation SET fk_bank=".$insertid." WHERE rowid=".$rowid; - - dol_syslog("Adherent::cotisation sql=".$sql); - $resql = $this->db->query($sql); - if (! $resql) - { - $this->error=$this->db->error(); - $this->db->rollback(); - return -5; - } - } - else - { - $this->error=$acct->error(); - $this->db->rollback(); - return -4; - } - } - else - { - $this->error=$acct->error; - $this->db->rollback(); - return -3; - } - } - // Change properties of object (used by triggers) $this->last_subscription_date=$dateop; $this->last_subscription_amount=$montant; diff --git a/htdocs/compta/paiement/class/paiement.class.php b/htdocs/compta/paiement/class/paiement.class.php index fe2271a492d..29c0973ce63 100644 --- a/htdocs/compta/paiement/class/paiement.class.php +++ b/htdocs/compta/paiement/class/paiement.class.php @@ -164,7 +164,7 @@ class Paiement $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiement_facture (fk_facture, fk_paiement, amount)'; $sql .= ' VALUES ('.$facid.', '. $this->id.', \''.$amount.'\')'; - dol_syslog("Paiement::Create Amount line '.$key.' insert paiement_facture sql=".$sql); + dol_syslog('Paiement::Create Amount line '.$key.' insert paiement_facture sql='.$sql); $resql=$this->db->query($sql); if (! $resql) { @@ -309,13 +309,14 @@ class Paiement * @param accountid Id of bank account to do link with * @param emetteur_nom Name of transmitter * @param emetteur_banque Name of bank - * @return int <0 if KO, >0 if OK + * @return int <0 if KO, bank_line_id if OK */ function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque) { global $conf; $error=0; + $bank_line_id=0; if ($conf->banque->enabled) { @@ -325,8 +326,8 @@ class Paiement $acc->fetch($accountid); $total=$this->total; + if ($mode == 'payment') $total=$this->total; if ($mode == 'payment_supplier') $total=-$total; - if ($mode == 'payment_sc') $total=-$total; // Insert payment into llx_bank $bank_line_id = $acc->addline($this->datepaye, @@ -339,7 +340,7 @@ class Paiement $emetteur_nom, $emetteur_banque); - // Mise a jour fk_bank dans llx_paiement. + // Mise a jour fk_bank dans llx_paiement // On connait ainsi le paiement qui a genere l'ecriture bancaire if ($bank_line_id > 0) { @@ -350,7 +351,7 @@ class Paiement dol_print_error($this->db); } - // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction + // Add link 'payment', 'payment_supplier' in bank_url between payment and bank transaction $url=''; if ($mode == 'payment') $url=DOL_URL_ROOT.'/compta/paiement/fiche.php?id='; if ($mode == 'payment_supplier') $url=DOL_URL_ROOT.'/fourn/paiement/fiche.php?id='; @@ -405,7 +406,7 @@ class Paiement if (! $error) { - return 1; + return $bank_line_id; } else {