Skip to content

Commit

Permalink
Qual: Mutualize a lot of code between supplier and customer payments
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Oct 1, 2011
1 parent 79546fa commit fe30491
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 212 deletions.
1 change: 0 additions & 1 deletion htdocs/compta/facture.php
Expand Up @@ -501,7 +501,6 @@
}



/*
* Insert new invoice in database
*/
Expand Down
176 changes: 133 additions & 43 deletions htdocs/compta/paiement/class/paiement.class.php
Expand Up @@ -54,8 +54,9 @@ class Paiement extends CommonObject


/**
* \brief Constructeur de la classe
* \param DB handler acces base de donnees
* Constructor
*
* @param DoliDB $DB Database handler
*/
function Paiement($DB)
{
Expand All @@ -64,8 +65,9 @@ function Paiement($DB)

/**
* Load payment from database
* @param id id of payment to get
* @return int <0 if KO, 0 if not found, >0 if OK
*
* @param int $id Id of payment to get
* @return int <0 if KO, 0 if not found, >0 if OK
*/
function fetch($id)
{
Expand Down Expand Up @@ -120,9 +122,10 @@ function fetch($id)
/**
* Create payment of invoices into database.
* Use this->amounts to have list of invoices for the payment
* @param user object user
* @param closepaidinvoices 1=Also close payed invoices to paid, 0=Do nothing more
* @return int id of created payment, < 0 if error
*
* @param User $user Object user
* @param int $closepaidinvoices 1=Also close payed invoices to paid, 0=Do nothing more
* @return int id of created payment, < 0 if error
*/
function create($user,$closepaidinvoices=0)
{
Expand Down Expand Up @@ -252,7 +255,9 @@ function create($user,$closepaidinvoices=0)
* Supprime un paiement ainsi que les lignes qu'il a genere dans comptes
* Si le paiement porte sur un ecriture compte qui est rapprochee, on refuse
* Si le paiement porte sur au moins une facture a "payee", on refuse
* @return int <0 si ko, >0 si ok
*
* @param int $notrigger No trigger
* @return int <0 si ko, >0 si ok
*/
function delete($notrigger=0)
{
Expand Down Expand Up @@ -351,13 +356,15 @@ function delete($notrigger=0)
/**
* 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', 'payment_supplier'
* @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, bank_line_id if OK
*
* @param User $user Object of user making payment
* @param string $mode 'payment', 'payment_supplier'
* @param string $label Label to use in bank record
* @param int $accountid Id of bank account to do link with
* @param string $emetteur_nom Name of transmitter
* @param string $emetteur_banque Name of bank
* @param int $notrigger No trigger
* @return int <0 if KO, bank_line_id if OK
*/
function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque,$notrigger=0)
{
Expand All @@ -382,15 +389,17 @@ function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_
if ($mode == 'payment_supplier') $totalamount=-$totalamount;

// Insert payment into llx_bank
$bank_line_id = $acc->addline($this->datepaye,
$this->paiementid, // Payment mode id or code ("CHQ or VIR for example")
$label,
$totalamount,
$this->num_paiement,
'',
$user,
$emetteur_nom,
$emetteur_banque);
$bank_line_id = $acc->addline(
$this->datepaye,
$this->paiementid, // Payment mode id or code ("CHQ or VIR for example")
$label,
$totalamount,
$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
Expand Down Expand Up @@ -433,8 +442,13 @@ function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_
$fac->fetch_thirdparty();
if (! in_array($fac->thirdparty->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty
{
$result=$acc->add_url_line($bank_line_id, $fac->thirdparty->id,
DOL_URL_ROOT.'/comm/fiche.php?socid=', $fac->thirdparty->nom, 'company');
$result=$acc->add_url_line(
$bank_line_id,
$fac->thirdparty->id,
DOL_URL_ROOT.'/comm/fiche.php?socid=',
$fac->thirdparty->nom,
'company'
);
if ($result <= 0) dol_print_error($this->db);
$linkaddedforthirdparty[$fac->thirdparty->id]=$fac->thirdparty->id; // Mark as done for this thirdparty
}
Expand All @@ -446,8 +460,13 @@ function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_
$fac->fetch_thirdparty();
if (! in_array($fac->thirdparty->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty
{
$result=$acc->add_url_line($bank_line_id, $fac->thirdparty->id,
DOL_URL_ROOT.'/fourn/fiche.php?socid=', $fac->thirdparty->nom, 'company');
$result=$acc->add_url_line(
$bank_line_id,
$fac->thirdparty->id,
DOL_URL_ROOT.'/fourn/fiche.php?socid=',
$fac->thirdparty->nom,
'company'
);
if ($result <= 0) dol_print_error($this->db);
$linkaddedforthirdparty[$fac->thirdparty->id]=$fac->thirdparty->id; // Mark as done for this thirdparty
}
Expand Down Expand Up @@ -485,7 +504,9 @@ function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_

/**
* Mise a jour du lien entre le paiement et la ligne generee dans llx_bank
* @param id_bank Id compte bancaire
*
* @param int $id_bank Id compte bancaire
* @return int <0 if KO, >0 if OK
*/
function update_fk_bank($id_bank)
{
Expand All @@ -506,8 +527,72 @@ function update_fk_bank($id_bank)
}
}

/**
* Updates the payment date
*
* @param timestamp $date New date
* @return int <0 if KO, 0 if OK
*/
function update_date($date)
{
if (!empty($date) && $this->statut!=1)
{
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " SET datep = ".$this->db->idate($date);
$sql.= " WHERE rowid = ".$this->id;

dol_syslog(get_class($this)."::update_date sql=".$sql);
$result = $this->db->query($sql);
if ($result)
{
$this->datepaye = $date;
$this->date = $date;
return 0;
}
else
{
$this->error='Error -1 '.$this->db->error();
dol_syslog(get_class($this)."::update_date ".$this->error, LOG_ERR);
return -2;
}
}
return -1; //no date given or already validated
}

/**
* Updates the payment number
*
* @param string $num New num
* @return int <0 if KO, 0 if OK
*/
function update_num($num)
{
if(!empty($num) && $this->statut!=1)
{
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " SET num_paiement = '".$this->db->escape($num)."'";
$sql.= " WHERE rowid = ".$this->id;

dol_syslog(get_class($this)."::update_num sql=".$sql);
$result = $this->db->query($sql);
if ($result)
{
$this->numero = $this->db->escape($num);
return 0;
}
else
{
$this->error='Error -1 '.$this->db->error();
dol_syslog(get_class($this)."::update_num ".$this->error, LOG_ERR);
return -2;
}
}
return -1; //no num given or already validated
}

/**
* Validate payment
*
* @return int <0 if KO, >0 if OK
*/
function valide()
Expand Down Expand Up @@ -572,9 +657,10 @@ function info($id)
}

/**
* \brief Retourne la liste des factures sur lesquels porte le paiement
* \param filter Critere de filtre
* \return array Tableau des id de factures
* Retourne la liste des factures sur lesquels porte le paiement
*
* @param string $filter Critere de filtre
* @return array Tableau des id de factures
*/
function getBillsArray($filter='')
{
Expand Down Expand Up @@ -608,10 +694,11 @@ function getBillsArray($filter='')


/**
* \brief Renvoie nom clicable (avec eventuellement le picto)
* \param withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
* \param option Sur quoi pointe le lien
* \return string Chaine avec URL
* Renvoie nom clicable (avec eventuellement le picto)
*
* @param int $withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
* @param string $option Sur quoi pointe le lien
* @return string Chaine avec URL
*/
function getNomUrl($withpicto=0,$option='')
{
Expand All @@ -629,24 +716,27 @@ function getNomUrl($withpicto=0,$option='')
}

/**
* \brief Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee)
* \param mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
* \return string Libelle
* Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee)
*
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
* @return string Libelle
*/
function getLibStatut($mode=0)
{
return $this->LibStatut($this->statut,$mode);
}

/**
* \brief Renvoi le libelle d'un statut donne
* \param status Statut
* \param mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
* \return string Libelle du statut
* Renvoi le libelle d'un statut donne
*
* @param int $status Statut
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
* @return string Libelle du statut
*/
function LibStatut($status,$mode=0)
{
global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage

$langs->load('compta');
if ($mode == 0)
{
Expand Down
51 changes: 41 additions & 10 deletions htdocs/compta/paiement/fiche.php
Expand Up @@ -126,6 +126,37 @@
}
}

if ($action == 'setnum' && !empty($_POST['num']))
{
$paiement = new Paiement($db);
$paiement->fetch($id);
$res = $paiement->update_num($_POST['num']);
if ($res === 0)
{
$mesg = '<div class="ok">'.$langs->trans('PaymentNumberUpdateSucceeded').'</div>';
}
else
{
$mesg = '<div class="error">'.$langs->trans('PaymentNumberUpdateFailed').'</div>';
}
}

if ($action == 'setdate' && !empty($_POST['dateday']))
{
$paiement = new Paiement($db);
$paiement->fetch($id);
$datepaye = dol_mktime(12, 0, 0, $_POST['datemonth'], $_POST['dateday'], $_POST['dateyear']);
$res = $paiement->update_date($datepaye);
if ($res === 0)
{
$mesg = '<div class="ok">'.$langs->trans('PaymentDateUpdateSucceeded').'</div>';
}
else
{
$mesg = '<div class="error">'.$langs->trans('PaymentDateUpdateFailed').'</div>';
}
}


/*
* View
Expand Down Expand Up @@ -185,25 +216,25 @@
print '<table class="border" width="100%">';

// Ref
print '<tr><td valign="top" width="140">'.$langs->trans('Ref').'</td><td colspan="3">'.$paiement->id.'</td></tr>';
print '<tr><td valign="top" width="20%">'.$langs->trans('Ref').'</td><td colspan="3">'.$paiement->id.'</td></tr>';

// Date
print '<tr><td valign="top" width="120">'.$langs->trans('Date').'</td><td colspan="3">'.dol_print_date($paiement->date,'day').'</td></tr>';
// Date payment
print '<tr><td valign="top">'.$html->editfieldkey("Date",'date',$paiement->date,'id',$paiement->id,$user->rights->facture->paiement).'</td><td colspan="3">';
print $html->editfieldval("Date",'date',$paiement->date,'id',$paiement->id,$user->rights->facture->paiement,'day');
print '</td></tr>';

// Payment type (VIR, LIQ, ...)
$labeltype=$langs->trans("PaymentType".$paiement->type_code)!=("PaymentType".$paiement->type_code)?$langs->trans("PaymentType".$paiement->type_code):$paiement->type_libelle;
print '<tr><td valign="top">'.$langs->trans('Mode').'</td><td colspan="3">'.$labeltype.'</td></tr>';
print '<tr><td valign="top">'.$langs->trans('PaymentMode').'</td><td colspan="3">'.$labeltype.'</td></tr>';

// Numero
//if ($paiement->montant)
//{
print '<tr><td valign="top">'.$langs->trans('Numero').'</td><td colspan="3">'.$paiement->numero.'</td></tr>';
//}
// Payment numero
print '<tr><td valign="top">'.$html->editfieldkey("Numero",'num',$paiement->numero,'id',$paiement->id,$paiement->statut == 0 && $user->rights->fournisseur->facture->creer).'</td><td colspan="3">';
print $html->editfieldval("Numero",'num',$paiement->numero,'id',$paiement->id,$paiement->statut == 0 && $user->rights->fournisseur->facture->creer,'string');
print '</td></tr>';

// Amount
print '<tr><td valign="top">'.$langs->trans('Amount').'</td><td colspan="3">'.price($paiement->montant).'&nbsp;'.$langs->trans('Currency'.$conf->monnaie).'</td></tr>';


// Note
print '<tr><td valign="top">'.$html->editfieldkey("Note",'note',$paiement->note,'id',$paiement->id,$user->rights->facture->paiement).'</td><td colspan="3">';
print $html->editfieldval("Note",'note',$paiement->note,'id',$paiement->id,$user->rights->facture->paiement,'text');
Expand Down

0 comments on commit fe30491

Please sign in to comment.