Work on invoice creation when creating subscription

This commit is contained in:
Laurent Destailleur 2010-09-09 22:45:05 +00:00
parent 07f3edeb88
commit 08dd448357
3 changed files with 67 additions and 58 deletions

View File

@ -235,7 +235,7 @@ if ($user->rights->adherent->cotisation->creer && $_POST["action"] == 'cotisatio
{ {
$db->begin(); $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); $crowid=$adh->cotisation($datecotisation, $cotisation, $accountid, $operation, $label, $num_chq, $emetteur_nom, $emetteur_banque, $datesubend, $option);
if ($crowid <= 0) if ($crowid <= 0)
{ {
@ -245,9 +245,47 @@ if ($user->rights->adherent->cotisation->creer && $_POST["action"] == 'cotisatio
if (! $error) 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/facture.class.php");
require_once(DOL_DOCUMENT_ROOT."/compta/facture/class/paymentterm.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 // Validate invoice
$result=$invoice->validate($user); $result=$invoice->validate($user);
// Add payment // Add payment on invoice
if ($option == 'bankviainvoice') if ($option == 'bankviainvoice' && $accountid)
{ {
require_once(DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php'); require_once(DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php');
require_once(DOL_DOCUMENT_ROOT.'/compta/bank/class/account.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) if (! $error)
{ {
$result=$paiement->addPaymentToBank($user,'payment','(SubscriptionPayment)',$accountid,$emetteur_nom,$emetteur_banque); $bank_line_id=$paiement->addPaymentToBank($user,'payment','(SubscriptionPayment)',$accountid,$emetteur_nom,$emetteur_banque);
if (! $result > 0) if (! $bank_line_id > 0)
{ {
$errmsg=$paiement->error; $errmsg=$paiement->error;
$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("#invoiceonly").click(function() {
jQuery(".bankswitchclass").show(); jQuery(".bankswitchclass").show();
}); });
});'; ';
if (GETPOST('paymentsave')) print 'jQuery("#'.GETPOST('paymentsave').'").attr(\'checked\',true);';
print '});';
print '</script>'."\n"; print '</script>'."\n";
} }
@ -720,7 +770,7 @@ if ($rowid)
print '<td><input name="label" type="text" size="32" value="'.$langs->trans("Subscription").' '; print '<td><input name="label" type="text" size="32" value="'.$langs->trans("Subscription").' ';
print dol_print_date(($datefrom?$datefrom:time()),"%Y").'" ></td></tr>'; print dol_print_date(($datefrom?$datefrom:time()),"%Y").'" ></td></tr>';
// Bank account transaction // Complementary action
if ($conf->banque->enabled || $conf->facture->enabled) if ($conf->banque->enabled || $conf->facture->enabled)
{ {
$company=new Societe($db); $company=new Societe($db);

View File

@ -1143,10 +1143,9 @@ class Adherent extends CommonObject
* \param emetteur_nom Nom emetteur cheque * \param emetteur_nom Nom emetteur cheque
* \param emetteur_banque Nom banque emetteur cheque * \param emetteur_banque Nom banque emetteur cheque
* \param datesubend Date fin adhesion * \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 * \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; global $conf,$langs,$user;
@ -1188,47 +1187,6 @@ class Adherent extends CommonObject
$result=$adh->add_to_mailman($conf->global->ADHERENT_MAILMAN_LISTS_COTISANT); $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) // Change properties of object (used by triggers)
$this->last_subscription_date=$dateop; $this->last_subscription_date=$dateop;
$this->last_subscription_amount=$montant; $this->last_subscription_amount=$montant;

View File

@ -164,7 +164,7 @@ class Paiement
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiement_facture (fk_facture, fk_paiement, amount)'; $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiement_facture (fk_facture, fk_paiement, amount)';
$sql .= ' VALUES ('.$facid.', '. $this->id.', \''.$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); $resql=$this->db->query($sql);
if (! $resql) if (! $resql)
{ {
@ -309,13 +309,14 @@ class Paiement
* @param accountid Id of bank account to do link with * @param accountid Id of bank account to do link with
* @param emetteur_nom Name of transmitter * @param emetteur_nom Name of transmitter
* @param emetteur_banque Name of bank * @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) function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque)
{ {
global $conf; global $conf;
$error=0; $error=0;
$bank_line_id=0;
if ($conf->banque->enabled) if ($conf->banque->enabled)
{ {
@ -325,8 +326,8 @@ class Paiement
$acc->fetch($accountid); $acc->fetch($accountid);
$total=$this->total; $total=$this->total;
if ($mode == 'payment') $total=$this->total;
if ($mode == 'payment_supplier') $total=-$total; if ($mode == 'payment_supplier') $total=-$total;
if ($mode == 'payment_sc') $total=-$total;
// Insert payment into llx_bank // Insert payment into llx_bank
$bank_line_id = $acc->addline($this->datepaye, $bank_line_id = $acc->addline($this->datepaye,
@ -339,7 +340,7 @@ class Paiement
$emetteur_nom, $emetteur_nom,
$emetteur_banque); $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 // On connait ainsi le paiement qui a genere l'ecriture bancaire
if ($bank_line_id > 0) if ($bank_line_id > 0)
{ {
@ -350,7 +351,7 @@ class Paiement
dol_print_error($this->db); 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=''; $url='';
if ($mode == 'payment') $url=DOL_URL_ROOT.'/compta/paiement/fiche.php?id='; 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='; if ($mode == 'payment_supplier') $url=DOL_URL_ROOT.'/fourn/paiement/fiche.php?id=';
@ -405,7 +406,7 @@ class Paiement
if (! $error) if (! $error)
{ {
return 1; return $bank_line_id;
} }
else else
{ {