Mutualize some code

This commit is contained in:
Laurent Destailleur 2010-09-09 01:26:07 +00:00
parent aec5e4ab52
commit 1918fb582e
9 changed files with 539 additions and 522 deletions

View File

@ -235,8 +235,8 @@ if ($user->rights->adherent->cotisation->creer && $_POST["action"] == 'cotisatio
{ {
$db->begin(); $db->begin();
// Create subscription (and bank record if option is 'bankdirect')
$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)
{ {
$error++; $error++;
@ -297,9 +297,9 @@ if ($user->rights->adherent->cotisation->creer && $_POST["action"] == 'cotisatio
// Validate invoice // Validate invoice
$result=$invoice->validate($user); $result=$invoice->validate($user);
// Add payment
if ($option == 'bankviainvoice') if ($option == 'bankviainvoice')
{ {
// Now we add payment
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,7 +324,7 @@ if ($user->rights->adherent->cotisation->creer && $_POST["action"] == 'cotisatio
if (! $error) if (! $error)
{ {
$result=$paiement->addLinkInvoiceBank($user,'(SubscriptionPayment)',$accountid,$emetteur_nom,$emetteur_banque); $result=$paiement->addPaymentToBank($user,'payment','(SubscriptionPayment)',$accountid,$emetteur_nom,$emetteur_banque);
if (! $result > 0) if (! $result > 0)
{ {
$errmsg=$paiement->error; $errmsg=$paiement->error;

View File

@ -1229,12 +1229,6 @@ class Adherent extends CommonObject
} }
} }
// Create invoice and payment
if ($option == 'bankviainvoice' && $accountid)
{
// TODO
}
// 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

@ -243,9 +243,6 @@ switch ( $_GET['action'] )
$paiement_id = $payment->create($user); $paiement_id = $payment->create($user);
if ($paiement_id > 0) if ($paiement_id > 0)
{
// Ajout d'une ecriture sur le compte bancaire
if ($conf->banque->enabled)
{ {
$bankaccountid=0; $bankaccountid=0;
if ( $obj_facturation->mode_reglement() == 'ESP' ) if ( $obj_facturation->mode_reglement() == 'ESP' )
@ -261,53 +258,16 @@ switch ( $_GET['action'] )
$bankaccountid=$conf_fkaccount_cb; $bankaccountid=$conf_fkaccount_cb;
} }
if ($bankaccountid > 0) if (! $error)
{ {
// Insertion dans llx_bank $result=$payment->addPaymentToBank($user,'payment','(CustomerInvoicePayment)',$bankaccountid,'','');
$label = "(CustomerInvoicePayment)"; if (! $result > 0)
$acc = new Account($db, $bankaccountid);
$bank_line_id = $acc->addline($payment->datepaye,
$payment->paiementid, // Payment mode id or code ("CHQ or VIR for example")
$label,
$obj_facturation->prix_total_ttc(),
$payment->num_paiement,
'',
$user,
'',
'');
// Mise a jour fk_bank dans llx_paiement.
// On connait ainsi le paiement qui a genere l'ecriture bancaire
if ($bank_line_id > 0)
{
$payment->update_fk_bank($bank_line_id);
// Mise a jour liens (pour chaque facture concernees par le paiement)
foreach ($payment->amounts as $key => $value)
{
$facid = $key;
$fac = new Facture($db);
$fac->fetch($facid);
$fac->fetch_client();
$acc->add_url_line($bank_line_id,
$paiement_id,
DOL_URL_ROOT.'/compta/paiement/fiche.php?id=',
'(paiement)',
'payment');
$acc->add_url_line($bank_line_id,
$fac->client->id,
DOL_URL_ROOT.'/compta/fiche.php?socid=',
$fac->client->nom,
'company');
}
}
else
{ {
$errmsg=$paiement->error;
$error++; $error++;
} }
} }
} }
}
else else
{ {
$error++; $error++;

View File

@ -202,6 +202,7 @@ class Account extends CommonObject
// Clean parameters // Clean parameters
$emetteur=trim($emetteur); $emetteur=trim($emetteur);
$banque=trim($banque); $banque=trim($banque);
if (is_numeric($oper)) // Clean oper to have a code instead of a rowid if (is_numeric($oper)) // Clean oper to have a code instead of a rowid
{ {
$sql ="SELECT code FROM ".MAIN_DB_PREFIX."c_paiement"; $sql ="SELECT code FROM ".MAIN_DB_PREFIX."c_paiement";
@ -236,7 +237,6 @@ class Account extends CommonObject
return -3; return -3;
} }
$this->db->begin(); $this->db->begin();
$datev = $date; $datev = $date;

View File

@ -48,6 +48,7 @@ $amountsresttopay=array();
$addwarning=0; $addwarning=0;
// Security check // Security check
$socid=0;
if ($user->societe_id > 0) if ($user->societe_id > 0)
{ {
$socid = $user->societe_id; $socid = $user->societe_id;
@ -166,7 +167,7 @@ if ($_POST['action'] == 'confirm_paiement' && $_POST['confirm'] == 'yes')
if (! $error) if (! $error)
{ {
$result=$paiement->addLinkInvoiceBank($user,'(CustomerInvoicePayment)',$_POST['accountid'],$_POST['chqemetteur'],$_POST['chqbank']); $result=$paiement->addPaymentToBank($user,'payment','(CustomerInvoicePayment)',$_POST['accountid'],$_POST['chqemetteur'],$_POST['chqbank']);
if (! $result > 0) if (! $result > 0)
{ {
$errmsg=$paiement->error; $errmsg=$paiement->error;

View File

@ -304,13 +304,14 @@ class Paiement
* A record into bank for payment with links between this bank record and invoices of payment. * A record into bank for payment with links between this bank record and invoices of payment.
* All payment properties must have been set first like after a call to create(). * All payment properties must have been set first like after a call to create().
* @param user Object of user making payment * @param user Object of user making payment
* @param mode 'payment', 'payment_supplier'
* @param label Label to use in bank record * @param label Label to use in bank record
* @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, >0 if OK
*/ */
function addLinkInvoiceBank($user,$label,$accountid,$emetteur_nom,$emetteur_banque) function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque)
{ {
global $conf; global $conf;
@ -323,11 +324,15 @@ class Paiement
$acc = new Account($this->db); $acc = new Account($this->db);
$acc->fetch($accountid); $acc->fetch($accountid);
$total=$this->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,
$this->paiementid, // Payment mode id or code ("CHQ or VIR for example") $this->paiementid, // Payment mode id or code ("CHQ or VIR for example")
$label, $label,
$this->total, $total,
$this->num_paiement, $this->num_paiement,
'', '',
$user, $user,
@ -344,35 +349,52 @@ class Paiement
$error++; $error++;
dol_print_error($this->db); dol_print_error($this->db);
} }
// Add link 'payment' in bank_url between payment and bank transaction
$result=$acc->add_url_line($bank_line_id, // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction
$this->id, $url='';
DOL_URL_ROOT.'/compta/paiement/fiche.php?id=', if ($mode == 'payment') $url=DOL_URL_ROOT.'/compta/paiement/fiche.php?id=';
'(paiement)', if ($mode == 'payment_supplier') $url=DOL_URL_ROOT.'/fourn/paiement/fiche.php?id=';
'payment'); if ($url)
{
$result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
if ($result <= 0) if ($result <= 0)
{ {
$error++; $error++;
dol_print_error($this->db); dol_print_error($this->db);
} }
}
// Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment) // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
$linkaddedforthirdparty=array(); $linkaddedforthirdparty=array();
foreach ($this->amounts as $key => $value) foreach ($this->amounts as $key => $value)
{
if ($mode == 'payment')
{ {
$fac = new Facture($this->db); $fac = new Facture($this->db);
$fac->fetch($key); $fac->fetch($key);
$fac->fetch_thirdparty(); // This should be always same third party but we loop in case of. $fac->fetch_thirdparty(); // This should be always same third party but we loop in case of.
if (! in_array($fac->client->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty if (! in_array($fac->client->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty
{ {
$result=$acc->add_url_line($bank_line_id, $result=$acc->add_url_line($bank_line_id, $fac->client->id,
$fac->client->id, DOL_URL_ROOT.'/compta/fiche.php?socid=', $fac->client->nom, 'company');
DOL_URL_ROOT.'/compta/fiche.php?socid=',
$fac->client->nom,
'company');
if ($result <= 0) dol_print_error($this->db); if ($result <= 0) dol_print_error($this->db);
$linkaddedforthirdparty[$fac->client->id]=$fac->client->id; // Mark as done for this thirdparty $linkaddedforthirdparty[$fac->client->id]=$fac->client->id; // Mark as done for this thirdparty
} }
} }
if ($mode == 'payment_supplier')
{
$fac = new FactureFournisseur($this->db);
$fac->fetch($key);
$fac->fetch_fournisseur(); // This should be always same third party but we loop in case of.
if (! in_array($fac->client->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty
{
$result=$acc->add_url_line($bank_line_id, $fac->fournisseur->id,
DOL_URL_ROOT.'/fourn/fiche.php?socid=', $fac->fournisseur->nom, 'company');
if ($result <= 0) dol_print_error($this->db);
$linkaddedforthirdparty[$fac->fournisseur->id]=$fac->fournisseur->id; // Mark as done for this thirdparty
}
}
}
} }
else else
{ {

View File

@ -30,11 +30,12 @@ include_once(DOL_DOCUMENT_ROOT."/compta/bank/class/account.class.php");
$langs->load("bills"); $langs->load("bills");
$chid=isset($_GET["id"])?$_GET["id"]:$_POST["id"]; $chid=isset($_GET["id"])?$_GET["id"]:$_POST["id"];
$amounts = array();
// Securite acces client // Security check
$socid=0;
if ($user->societe_id > 0) if ($user->societe_id > 0)
{ {
$action = '';
$socid = $user->societe_id; $socid = $user->societe_id;
} }
@ -74,14 +75,12 @@ if ($_POST["action"] == 'add_paiement')
$paymentid = 0; $paymentid = 0;
// Read possible payments // Read possible payments
$amounts = array();
foreach ($_POST as $key => $value) foreach ($_POST as $key => $value)
{ {
if (substr($key,0,7) == 'amount_') if (substr($key,0,7) == 'amount_')
{ {
$other_chid = substr($key,7); $other_chid = substr($key,7);
$amounts[$other_chid] = price2num($_POST[$key]);
$amounts[$other_chid] = $_POST[$key];
} }
} }
@ -95,56 +94,37 @@ if ($_POST["action"] == 'add_paiement')
$paiement->paiementtype = $_POST["paiementtype"]; $paiement->paiementtype = $_POST["paiementtype"];
$paiement->num_paiement = $_POST["num_paiement"]; $paiement->num_paiement = $_POST["num_paiement"];
$paiement->note = $_POST["note"]; $paiement->note = $_POST["note"];
if (! $error)
{
$paymentid = $paiement->create($user); $paymentid = $paiement->create($user);
if (! $paymentid > 0)
$socialcontrib = new ChargeSociales($db);
$socialcontrib->fetch($paiement->chid);
if ($paymentid > 0)
{ {
// On determine le montant total du paiement $errmsg=$paiement->error;
$total=0; $error++;
foreach ($paiement->amounts as $key => $value) }
{
$chid = $key;
$value = trim($value);
$amount = price2num(trim($value), 'MT'); // Un round est ok si nb avec '.'
$total += $amount;
} }
// Insertion dans llx_bank if (! $error)
$langs->load("banks");
$label = $langs->transnoentities("SocialContributionPayment");
$acc = new Account($db, $_POST["accountid"]);
$bank_line_id = $acc->addline($paiement->datepaye, $paiement->paiementtype, $label, -$total, $paiement->num_paiement, '', $user);
// Mise a jour fk_bank dans llx_paiementcharge. On connait ainsi le paiement qui a genere l'ecriture bancaire
if ($bank_line_id > 0)
{ {
$paiement->update_fk_bank($bank_line_id); $result=$paiement->addPaymentToBank($user,'payment_sc','(SocialContributionPayment)',$_POST['accountid'],'','');
if (! $result > 0)
// Mise a jour liens (pour chaque charge concernee par le paiement)
foreach ($paiement->amounts as $key => $value)
{ {
$acc->add_url_line($bank_line_id, $chid, DOL_URL_ROOT.'/compta/charges.php?id=', $socialcontrib->type_libelle.(($socialcontrib->lib && $socialcontrib->lib!=$socialcontrib->type_libelle)?' ('.$socialcontrib->lib.')':''),'sc'); $errmsg=$paiement->error;
$acc->add_url_line($bank_line_id, $paymentid, DOL_URL_ROOT.'/compta/payment_sc/fiche.php?id=', '(paiement)','payment_sc'); $error++;
}
} }
if (! $error)
{
$db->commit(); $db->commit();
$loc = DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$chid; $loc = DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$chid;
Header("Location: ".$loc); Header('Location: '.$loc);
exit; exit;
} }
else {
$db->rollback();
$mesg = "Echec de la creation entree compte: ".$db->error();
}
}
else else
{ {
$db->rollback(); $db->rollback();
$mesg = "Failed to create payment: paiement_id=".$paymentid." ".$db->error();
} }
} }

View File

@ -374,7 +374,7 @@ class PaiementCharge extends CommonObject
* \brief Constructor * \brief Constructor
* \param DB Database handler * \param DB Database handler
*/ */
function Paiementcharge($DB) function PaiementCharge($DB)
{ {
$this->db = $DB; $this->db = $DB;
return 1; return 1;
@ -414,7 +414,6 @@ class PaiementCharge extends CommonObject
$total=0; $total=0;
foreach ($this->amounts as $key => $value) foreach ($this->amounts as $key => $value)
{ {
$facid = $key;
$amount = price2num(trim($value), 'MT'); $amount = price2num(trim($value), 'MT');
$total += $amount; $total += $amount;
} }
@ -758,6 +757,100 @@ class PaiementCharge extends CommonObject
} }
/**
* A record into bank for payment with links between this bank record and invoices of payment.
* All payment properties must have been set first like after a call to create().
* @param user Object of user making payment
* @param mode 'payment_sc'
* @param label Label to use in bank record
* @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
*/
function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque)
{
global $conf;
$error=0;
if ($conf->banque->enabled)
{
require_once(DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php');
$acc = new Account($this->db);
$acc->fetch($accountid);
$total=$this->total;
if ($mode == 'payment_sc') $total=-$total;
// Insert payment into llx_bank
$bank_line_id = $acc->addline($this->datepaye,
$this->paiementtype, // Payment mode id or code ("CHQ or VIR for example")
$label,
$total,
$this->num_paiement,
'',
$user,
$emetteur_nom,
$emetteur_banque);
// Mise a jour fk_bank dans llx_paiement.
// On connait ainsi le paiement qui a genere l'ecriture bancaire
if ($bank_line_id > 0)
{
$result=$this->update_fk_bank($bank_line_id);
if ($result <= 0)
{
$error++;
dol_print_error($this->db);
}
// Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction
$url='';
if ($mode == 'payment_sc') $url=DOL_URL_ROOT.'/compta/payment_sc/fiche.php?id=';
if ($url)
{
$result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
if ($result <= 0)
{
$error++;
dol_print_error($this->db);
}
}
// Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
$linkaddedforthirdparty=array();
foreach ($this->amounts as $key => $value)
{
if ($mode == 'payment_sc')
{
$socialcontrib = new ChargeSociales($this->db);
$socialcontrib->fetch($key);
$result=$acc->add_url_line($bank_line_id, $socialcontrib->id,
DOL_URL_ROOT.'/compta/charges.php?id=', $socialcontrib->type_libelle.(($socialcontrib->lib && $socialcontrib->lib!=$socialcontrib->type_libelle)?' ('.$socialcontrib->lib.')':''),'sc');
if ($result <= 0) dol_print_error($this->db);
}
}
}
else
{
$this->error=$acc->error;
$error++;
}
}
if (! $error)
{
return 1;
}
else
{
return -1;
}
}
/** /**
* \brief Mise a jour du lien entre le paiement de charge et la ligne dans llx_bank generee * \brief Mise a jour du lien entre le paiement de charge et la ligne dans llx_bank generee
* \param id_bank Id de la banque * \param id_bank Id de la banque

View File

@ -48,11 +48,12 @@ $offset = $limit * $page ;
if (! $sortorder) $sortorder="DESC"; if (! $sortorder) $sortorder="DESC";
if (! $sortfield) $sortfield="p.rowid"; if (! $sortfield) $sortfield="p.rowid";
$amounts = array();
// Security check // Security check
$socid=0; $socid=0;
if ($user->societe_id > 0) if ($user->societe_id > 0)
{ {
$action = '';
$socid = $user->societe_id; $socid = $user->societe_id;
} }
@ -73,7 +74,6 @@ if ($action == 'add_paiement')
$paiement_id = 0; $paiement_id = 0;
$total = 0; $total = 0;
// Genere tableau des montants amounts // Genere tableau des montants amounts
$amounts = array();
foreach ($_POST as $key => $value) foreach ($_POST as $key => $value)
{ {
if (substr($key,0,7) == 'amount_') if (substr($key,0,7) == 'amount_')
@ -123,63 +123,30 @@ if ($action == 'add_paiement')
$paiement->num_paiement = $_POST['num_paiement']; $paiement->num_paiement = $_POST['num_paiement'];
$paiement->note = $_POST['comment']; $paiement->note = $_POST['comment'];
if (! $error)
{
$paiement_id = $paiement->create($user); $paiement_id = $paiement->create($user);
if ($paiement_id > 0) if (! $paiement_id > 0)
{
if ($conf->banque->enabled)
{
// Insertion dans llx_bank
$label = "(SupplierInvoicePayment)";
$acc = new Account($db, $_POST['accountid']);
//paiementid contient "CHQ ou VIR par exemple"
$bank_line_id = $acc->addline($paiement->datepaye,
$paiement->paiementid,
$label,
0.0 - $paiement->total,
$paiement->num_paiement,
'',
$user);
// Mise a jour fk_bank dans llx_paiement.
// On connait ainsi le paiement qui a genere l'ecriture bancaire
if ($bank_line_id > 0)
{
$paiement->update_fk_bank($bank_line_id);
// Mise a jour liens (pour chaque facture concernees par le paiement)
foreach ($paiement->amounts as $key => $value)
{
$facid = $key;
$fac = new FactureFournisseur($db);
$fac->fetch($facid);
$fac->fetch_fournisseur();
$acc->add_url_line($bank_line_id,
$paiement_id,
DOL_URL_ROOT.'/fourn/paiement/fiche.php?id=',
'(paiement)',
'payment_supplier');
$acc->add_url_line($bank_line_id,
$fac->fournisseur->id,
DOL_URL_ROOT.'/fourn/fiche.php?socid=',
$fac->fournisseur->nom,
'company');
}
}
else
{ {
$errmsg='<div class="error">'.$paiement->error.'</div>';
$error++; $error++;
} }
} }
}
else if (! $error)
{ {
$mesg = '<div class="error">'.$langs->trans($paiement->error).'</div>'; $result=$paiement->addPaymentToBank($user,'payment_supplier','(SupplierInvoicePayment)',$_POST['accountid'],'','');
if (! $result > 0)
{
$errmsg='<div class="error">'.$paiement->error.'</div>';
$error++; $error++;
} }
}
if ($error == 0) if (! $error)
{ {
$loc = DOL_URL_ROOT.'/fourn/paiement/fiche.php?id='.$paiement_id;
$db->commit(); $db->commit();
$loc = DOL_URL_ROOT.'/fourn/paiement/fiche.php?id='.$paiement_id;
Header('Location: '.$loc); Header('Location: '.$loc);
exit; exit;
} }