Fix: Quand on modifie les conditions de réglements, la date de cloture de facture est tout de suite mise a jour.

This commit is contained in:
Laurent Destailleur 2005-09-04 02:22:18 +00:00
parent bc0541dcc7
commit 54b303e9dc
2 changed files with 391 additions and 374 deletions

View File

@ -48,8 +48,8 @@ if ($conf->commande->enabled) require_once(DOL_DOCUMENT_ROOT."/commande/commande
$sall=isset($_GET["sall"])?$_GET["sall"]:$_POST["sall"]; $sall=isset($_GET["sall"])?$_GET["sall"]:$_POST["sall"];
if ($_GET["socidp"]) { $socidp=$_GET["socidp"]; }
if (isset($_GET["msg"])) { $msg=urldecode($_GET["msg"]); } if (isset($_GET["msg"])) { $msg=urldecode($_GET["msg"]); }
if ($_GET["socidp"]) { $socidp=$_GET["socidp"]; }
// Sécurité accés client // Sécurité accés client
if ($user->societe_id > 0) if ($user->societe_id > 0)
@ -512,47 +512,41 @@ $html = new Form($db);
**********************************************************************/ **********************************************************************/
if ($_GET["action"] == 'create') if ($_GET["action"] == 'create')
{ {
print_titre($langs->trans("NewBill")); print_titre($langs->trans("NewBill"));
if ($_GET["propalid"]) if ($_GET["propalid"])
{ {
$sql = "SELECT s.nom, s.prefix_comm, s.idp, p.price, p.remise, p.remise_percent, p.tva, p.total, p.ref, ".$db->pdate("p.datep")." as dp, c.id as statut, c.label as lst"; $propal = New Propal($db);
$sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."propal as p, ".MAIN_DB_PREFIX."c_propalst as c"; $propal->fetch($_GET["propalid"]);
$sql .= " WHERE p.fk_soc = s.idp AND p.fk_statut = c.id"; $societe_id = $propal->soc_id;
$sql .= " AND p.rowid = ".$_GET["propalid"];
} }
else elseif ($_GET["commandeid"])
{
$sql = "SELECT s.nom, s.prefix_comm, s.idp ";
$sql .= "FROM ".MAIN_DB_PREFIX."societe as s ";
$sql .= "WHERE s.idp = ".$_GET["socidp"];
}
if ($_GET["commandeid"])
{ {
$commande = New Commande($db); $commande = New Commande($db);
$commande->fetch($_GET["commandeid"]); $commande->fetch($_GET["commandeid"]);
$societe_id = $commande->soc_id; $societe_id = $commande->soc_id;
} }
elseif ($_GET["contratid"])
if ( $db->query($sql) )
{ {
$num = $db->num_rows(); $contrat = New Contrat($db);
if ($num) $contrat->fetch($_GET["contratid"]);
{ $societe_id = $contrat->soc_id;
$obj = $db->fetch_object(); }
else $societe_id=$socidp;
$soc = new Societe($db); $soc = new Societe($db);
$soc->fetch($obj->idp); $soc->fetch($societe_id);
print '<form action="facture.php" method="post">'; print '<form action="facture.php" method="post">';
print '<input type="hidden" name="action" value="add">'; print '<input type="hidden" name="action" value="add">';
print '<input type="hidden" name="socid" value="'.$obj->idp.'">' ."\n"; print '<input type="hidden" name="socid" value="'.$soc->id.'">' ."\n";
print '<input type="hidden" name="remise_percent" value="0">'; print '<input type="hidden" name="remise_percent" value="0">';
print '<table class="border" width="100%">'; print '<table class="border" width="100%">';
print '<tr><td>'.$langs->trans("Ref").'</td><td colspan="2">'.$langs->trans("Draft").'</td></tr>';
print '<input name="facnumber" type="hidden" value="provisoire">';
print '<tr><td>'.$langs->trans("Company").'</td><td colspan="2">'.$soc->nom.'</td>'; print '<tr><td>'.$langs->trans("Company").'</td><td colspan="2">'.$soc->nom.'</td>';
print '</tr>'; print '</tr>';
@ -564,17 +558,23 @@ if ($_GET["action"] == 'create')
$html->select_date(); $html->select_date();
print '</td>'; print '</td>';
print '<td rowspan="5" valign="top">'; // Notes
print '<textarea name="note" wrap="soft" cols="50" rows="5">'; print '<td rowspan="4" valign="top">';
if (is_object($commande) && !empty($commande->projet_id)) print '<textarea name="note" wrap="soft" cols="50" rows="4">';
if (is_object($propal))
{
print $propal->note;
}
if (is_object($commande))
{ {
print $commande->note; print $commande->note;
} }
if (is_object($contrat))
{
print $contrat->note;
}
print '</textarea></td></tr>'; print '</textarea></td></tr>';
print '<tr><td>'.$langs->trans("Ref").'</td><td>Provisoire</td></tr>';
print '<input name="facnumber" type="hidden" value="provisoire">';
// Conditions de réglement // Conditions de réglement
$id_condition_paiements_defaut=1; $id_condition_paiements_defaut=1;
print "<tr><td nowrap>".$langs->trans("PaymentConditions")."</td><td>"; print "<tr><td nowrap>".$langs->trans("PaymentConditions")."</td><td>";
@ -600,6 +600,31 @@ if ($_GET["action"] == 'create')
} }
if ($_GET["propalid"] > 0) if ($_GET["propalid"] > 0)
{
$amount = ($propal->price);
print '<input type="hidden" name="amount" value="'.$amount.'">'."\n";
print '<input type="hidden" name="total" value="'.$propal->total.'">'."\n";
print '<input type="hidden" name="remise" value="'.$propal->remise.'">'."\n";
print '<input type="hidden" name="remise_percent" value="'.$propal->remise_percent.'">'."\n";
print '<input type="hidden" name="tva" value="'.$propal->tva.'">'."\n";
print '<input type="hidden" name="propalid" value="'.$_GET["propalid"].'">';
print '<tr><td>'.$langs->trans("Proposal").'</td><td colspan="2">'.$propal->ref.'</td></tr>';
print '<tr><td>'.$langs->trans("GlobalDiscount").'</td><td colspan="2">'.$propal->remise_percent.'%</td></tr>';
print '<tr><td>'.$langs->trans("TotalHT").'</td><td colspan="2">'.price($amount).'</td></tr>';
print '<tr><td>'.$langs->trans("VAT").'</td><td colspan="2">'.price($propal->tva)."</td></tr>";
print '<tr><td>'.$langs->trans("TotalTTC").'</td><td colspan="2">'.price($propal->total)."</td></tr>";
}
elseif ($_GET["commandeid"] > 0)
{
print '<input type="hidden" name="commandeid" value="'.$commande->id.'">';
print '<tr><td>'.$langs->trans("Order").'</td><td colspan="2">'.$commande->ref.'</td></tr>';
print '<tr><td>'.$langs->trans("TotalHT").'</td><td colspan="2">'.price($commande->total_ht).'</td></tr>';
print '<tr><td>'.$langs->trans("VAT").'</td><td colspan="2">'.price($commande->total_tva)."</td></tr>";
print '<tr><td>'.$langs->trans("TotalTTC").'</td><td colspan="2">'.price($commande->total_ttc)."</td></tr>";
}
elseif ($_GET["contratid"] > 0)
{ {
$amount = ($obj->price); $amount = ($obj->price);
print '<input type="hidden" name="amount" value="'.$amount.'">'."\n"; print '<input type="hidden" name="amount" value="'.$amount.'">'."\n";
@ -607,28 +632,20 @@ if ($_GET["action"] == 'create')
print '<input type="hidden" name="remise" value="'.$obj->remise.'">'."\n"; print '<input type="hidden" name="remise" value="'.$obj->remise.'">'."\n";
print '<input type="hidden" name="remise_percent" value="'.$obj->remise_percent.'">'."\n"; print '<input type="hidden" name="remise_percent" value="'.$obj->remise_percent.'">'."\n";
print '<input type="hidden" name="tva" value="'.$obj->tva.'">'."\n"; print '<input type="hidden" name="tva" value="'.$obj->tva.'">'."\n";
print '<input type="hidden" name="propalid" value="'.$_GET["propalid"].'">'; print '<input type="hidden" name="contratid" value="'.$_GET["contratid"].'">';
print '<tr><td>'.$langs->trans("Proposal").'</td><td colspan="2">'.$obj->ref.'</td></tr>'; print '<tr><td>'.$langs->trans("Contract").'</td><td colspan="2">'.$obj->ref.'</td></tr>';
print '<tr><td>'.$langs->trans("TotalHT").'</td><td colspan="2">'.price($amount).'</td></tr>'; print '<tr><td>'.$langs->trans("TotalHT").'</td><td colspan="2">'.price($amount).'</td></tr>';
print '<tr><td>'.$langs->trans("VAT").'</td><td colspan="2">'.price($obj->tva)."</td></tr>"; print '<tr><td>'.$langs->trans("VAT").'</td><td colspan="2">'.price($obj->tva)."</td></tr>";
print '<tr><td>'.$langs->trans("TotalTTC").'</td><td colspan="2">'.price($obj->total)."</td></tr>"; print '<tr><td>'.$langs->trans("TotalTTC").'</td><td colspan="2">'.price($obj->total)."</td></tr>";
} }
elseif ($_GET["commandeid"] > 0)
{
print '<input type="hidden" name="commandeid" value="'.$commande->id.'">';
print '<tr><td>'.$langs->trans("Order").'</td><td colspan="2">'.$commande->ref.'</td></tr>';
print '<tr><td>'.$langs->trans("TotalHT").'</td><td colspan="2">'.price($commande->total_ht).'</td></tr>';
print '<tr><td>'.$langs->trans("VAT").'</td><td colspan="2">'.price($commande->total_tva)."</td></tr>";
print '<tr><td>'.$langs->trans("TotalTTC").'</td><td colspan="2">'.price($commande->total_ttc)."</td></tr>";
}
else else
{ {
print '<tr><td colspan="3">&nbsp;</td></tr>'; print '<tr><td colspan="3">&nbsp;</td></tr>';
print '<tr><td colspan="3">'; print '<tr><td colspan="3">';
print '<table class="noborder">'; print '<table class="noborder">';
print '<tr><td>Services/Produits prédéfinis</td><td>'.$langs->trans("Qty").'</td><td>'.$langs->trans("Discount").'</td><td> &nbsp; &nbsp; </td>'; print '<tr><td>'.$langs->trans("ProductsAndServices").'</td><td>'.$langs->trans("Qty").'</td><td>'.$langs->trans("Discount").'</td><td> &nbsp; &nbsp; </td>';
if ($conf->service->enabled) if ($conf->service->enabled)
{ {
print '<td>Si produit de type service à durée limitée</td></tr>'; print '<td>Si produit de type service à durée limitée</td></tr>';
@ -689,9 +706,7 @@ if ($_GET["action"] == 'create')
} }
} }
/* // Bouton "Create Draft"
*
*/
print '<tr><td colspan="3" align="center"><input type="submit" name="bouton" value="'.$langs->trans("CreateDraft").'"></td></tr>'; print '<tr><td colspan="3" align="center"><input type="submit" name="bouton" value="'.$langs->trans("CreateDraft").'"></td></tr>';
print "</form>\n"; print "</form>\n";
print "</table>\n"; print "</table>\n";
@ -727,7 +742,7 @@ if ($_GET["action"] == 'create')
print $objp->product?' - '.$objp->product:''; print $objp->product?' - '.$objp->product:'';
print "</td>\n"; print "</td>\n";
print '<td>'; print '<td>';
print $objp->description; print dolibarr_trunc($objp->description,60);
print '</td>'; print '</td>';
print '<td align="right">'.$objp->tva_tx.'%</td>'; print '<td align="right">'.$objp->tva_tx.'%</td>';
print '<td align="right">'.price($objp->price).'</td>'; print '<td align="right">'.price($objp->price).'</td>';
@ -802,7 +817,7 @@ if ($_GET["action"] == 'create')
print $objp->product?' - '.$objp->product:''; print $objp->product?' - '.$objp->product:'';
print "</td>\n"; print "</td>\n";
print '<td>'; print '<td>';
print $objp->description; print dolibarr_trunc($objp->description,60);
print '</td>'; print '</td>';
print '<td align="right">'.price($objp->subprice).'</td>'; print '<td align="right">'.price($objp->subprice).'</td>';
print '<td align="center">'.$objp->remise_percent.'%</td>'; print '<td align="center">'.$objp->remise_percent.'%</td>';
@ -848,12 +863,6 @@ if ($_GET["action"] == 'create')
} }
} }
}
else
{
dolibarr_print_error($db);
}
}
else else
{ {
if ($_GET["facid"] > 0) if ($_GET["facid"] > 0)

View File

@ -112,29 +112,8 @@ class Facture
$this->remise_percent = $_facrec->remise_percent; $this->remise_percent = $_facrec->remise_percent;
} }
$sql = "SELECT fdm,nbjour ";
$sql.= " FROM ".MAIN_DB_PREFIX."cond_reglement";
$sql.= " WHERE rowid = ".$this->cond_reglement;
if ($this->db->query($sql))
{
if ($this->db->num_rows())
{
$obj = $this->db->fetch_object();
$cdr_nbjour = $obj->nbjour;
$cdr_fdm = $obj->fdm;
}
$this->db->free();
}
// Definition de la date limite // Definition de la date limite
$datelim = $this->date + ( $cdr_nbjour * 3600 * 24 ); $datelim=$this->calculate_date_lim_reglement();
if ($cdr_fdm)
{
$mois=date('m', $datelim);
$annee=date('Y', $datelim);
$fins=array(31,28,31,30,31,30,31,31,30,31,30,31);
$datelim=mktime(0,0,0,$mois,$fins[$mois-1],$annee);
}
/* /*
* Insertion dans la base * Insertion dans la base
@ -145,7 +124,7 @@ class Facture
$remise = $this->remise; $remise = $this->remise;
if (! $remise) $remise = 0 ; if (! $remise) $remise = 0 ;
if (strlen($this->mode_reglement)==0) $this->mode_reglement = 0; if (strlen($this->mode_reglement_id)==0) $this->mode_reglement_id = 0;
if (! $this->projetid) $this->projetid = "NULL"; if (! $this->projetid) $this->projetid = "NULL";
$totalht = ($amount - $remise); $totalht = ($amount - $remise);
@ -356,6 +335,7 @@ class Facture
if ($this->db->num_rows($result)) if ($this->db->num_rows($result))
{ {
$obj = $this->db->fetch_object($result); $obj = $this->db->fetch_object($result);
//print strftime("%Y%m%d%H%M%S",$obj->df)." ".$obj->df." ".dolibarr_print_date($obj->df);
$this->id = $rowid; $this->id = $rowid;
$this->datep = $obj->dp; $this->datep = $obj->dp;
@ -637,6 +617,50 @@ class Facture
} }
} }
/**
* \brief Renvoi une date limite de reglement de facture en fonction des
* conditions de reglements de la facture et date de facturation
* \param cond_reglement_id Condition de reglement à utiliser, 0=Condition actuelle de la facture
* \return date Date limite de réglement si ok, <0 si ko
*/
function calculate_date_lim_reglement($cond_reglement_id=0)
{
if (! $cond_reglement_id) $cond_reglement_id=$this->cond_reglement_id;
$sqltemp = "SELECT c.fdm,c.nbjour";
$sqltemp.= " FROM ".MAIN_DB_PREFIX."cond_reglement as c";
$sqltemp.= " WHERE c.rowid=".$cond_reglement_id;
$resqltemp=$this->db->query($sqltemp);
if ($resqltemp)
{
if ($this->db->num_rows($resqltemp))
{
$obj = $this->db->fetch_object($resqltemp);
$cdr_nbjour = $obj->nbjour;
$cdr_fdm = $obj->fdm;
}
}
else
{
$this->error=$this->db->error();
return -1;
}
$this->db->free($resqltemp);
// Definition de la date limite
$datelim = $this->date + ( $cdr_nbjour * 3600 * 24 );
if ($cdr_fdm)
{
$mois=date('m', $datelim);
$annee=date('Y', $datelim);
$fins=array(31,28,31,30,31,30,31,31,30,31,30,31);
$datelim=mktime(12,0,0,$mois,$fins[$mois-1],$annee);
}
return $datelim;
}
/** /**
* \brief Tag la facture comme payée complètement * \brief Tag la facture comme payée complètement
* \param rowid id de la facture à modifier * \param rowid id de la facture à modifier
@ -747,38 +771,17 @@ class Facture
/* Si l'option est activée on force la date de facture */ /* Si l'option est activée on force la date de facture */
if (defined("FAC_FORCE_DATE_VALIDATION") && FAC_FORCE_DATE_VALIDATION == "1") if (defined("FAC_FORCE_DATE_VALIDATION") && FAC_FORCE_DATE_VALIDATION == "1")
{ {
$sql .= ", datef=now()"; $this->date=time();
// du coup, il faut aussi recalculer la date limite de règlement $datelim=$this->calculate_date_lim_reglement();
$sqltemp = "SELECT c.fdm,c.nbjour,c.rowid,f.fk_cond_reglement,f.rowid";
$sqltemp.= " FROM ".MAIN_DB_PREFIX."cond_reglement as c, ".MAIN_DB_PREFIX."facture as f";
$sqltemp.= " WHERE c.rowid=f.fk_cond_reglement AND f.rowid=".$this->id;
if ($this->db->query($sqltemp))
{
if ($this->db->num_rows())
{
$obj = $this->db->fetch_object();
$cdr_nbjour = $obj->nbjour;
$cdr_fdm = $obj->fdm;
}
}
$this->db->free();
// Definition de la date limite
$datelim = time() + ( $cdr_nbjour * 3600 * 24 );
if ($cdr_fdm)
{
$mois=date('m', $datelim);
$annee=date('Y', $datelim);
$fins=array(31,28,31,30,31,30,31,31,30,31,30,31);
$datelim=mktime(0,0,0,$mois,$fins[$mois-1],$annee);
}
$sql .= ", datef=".$this->db->idate($this->date);
$sql .= ", date_lim_reglement=".$this->db->idate($datelim); $sql .= ", date_lim_reglement=".$this->db->idate($datelim);
} }
$sql .= " WHERE rowid = $rowid ;"; $sql .= " WHERE rowid = $rowid ;";
$result = $this->db->query( $sql); $resql = $this->db->query($sql);
if ($result) if ($resql)
{ {
// Appel des triggers // Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
@ -1359,8 +1362,8 @@ class Facture
} }
/** /**
* \brief Change les conditions de réglement * \brief Change les conditions de réglement de la facture
* \param cond_reglement_id Id des nouvelles conditions * \param cond_reglement_id Id de la nouvelle condition de réglement
* \return int >0 si ok, <0 si ko * \return int >0 si ok, <0 si ko
*/ */
function cond_reglement($cond_reglement_id) function cond_reglement($cond_reglement_id)
@ -1368,8 +1371,11 @@ class Facture
dolibarr_syslog("Facture::cond_reglement($cond_reglement_id)"); dolibarr_syslog("Facture::cond_reglement($cond_reglement_id)");
if ($this->statut >= 0 && $this->paye == 0) if ($this->statut >= 0 && $this->paye == 0)
{ {
$datelim=$this->calculate_date_lim_reglement($cond_reglement_id);
$sql = "UPDATE ".MAIN_DB_PREFIX."facture"; $sql = "UPDATE ".MAIN_DB_PREFIX."facture";
$sql .= " SET fk_cond_reglement = ".$cond_reglement_id; $sql .= " SET fk_cond_reglement = ".$cond_reglement_id;
$sql .= ", date_lim_reglement=".$this->db->idate($datelim);
$sql .= " WHERE rowid=".$this->id; $sql .= " WHERE rowid=".$this->id;
if ( $this->db->query($sql) ) if ( $this->db->query($sql) )
@ -1719,13 +1725,15 @@ class FactureLigne
*/ */
function fetch($rowid, $societe_id=0) function fetch($rowid, $societe_id=0)
{ {
$sql = "SELECT fk_product, description, price, qty, rowid, tva_taux, remise, remise_percent, subprice, ".$this->db->pdate("date_start")." as date_start,".$this->db->pdate("date_end")." as date_end"; $sql = "SELECT fk_product, description, price, qty, rowid, tva_taux, remise, remise_percent,";
$sql.= " subprice, ".$this->db->pdate("date_start")." as date_start,".$this->db->pdate("date_end")." as date_end";
$sql.= " FROM ".MAIN_DB_PREFIX."facturedet WHERE rowid = ".$rowid; $sql.= " FROM ".MAIN_DB_PREFIX."facturedet WHERE rowid = ".$rowid;
$result = $this->db->query($sql); $result = $this->db->query($sql);
if ($result) if ($result)
{ {
$objp = $this->db->fetch_object($result); $objp = $this->db->fetch_object($result);
$this->desc = stripslashes($objp->description); $this->desc = stripslashes($objp->description);
$this->qty = $objp->qty; $this->qty = $objp->qty;
$this->price = $objp->price; $this->price = $objp->price;