New: Le lien entre le contrat et la ligne de facture qui l'a généré est fait.

New: Lors de la mise en service, les date de début et fin proposée sont mises par défaut aux valeurs saisies dans la ligne de facture.
Si pas de date ont été saisies, les valeurs par défaut sont positionnées à la date courante pour le début et la date de fin=date courante+durée du service.
This commit is contained in:
Laurent Destailleur 2004-05-17 19:53:34 +00:00
parent 94d39f64b1
commit b3c3e451d0
5 changed files with 167 additions and 80 deletions

View File

@ -1,5 +1,6 @@
<?PHP <?PHP
/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org> /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004 Destailleur Laurent <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -38,19 +39,22 @@ class Contrat
$this->user_service = new User($DB); $this->user_service = new User($DB);
$this->user_cloture = new User($DB); $this->user_cloture = new User($DB);
} }
/* /*
* * Modifie date de mise en service d'un contrat
* * Si la duree est renseignée, date_start=date_start et date_end=date_start+duree
* * sinon date_start=date_start et date_end=date_end
*/ */
Function mise_en_service($user, $date, $duree) Function mise_en_service($user, $date_start, $duree=0, $date_end)
{ {
if ($duree) {
// Si duree renseignee
$duree_value = substr($duree,0,strlen($duree)-1); $duree_value = substr($duree,0,strlen($duree)-1);
$duree_unit = substr($duree,-1); $duree_unit = substr($duree,-1);
$month = date("m",$date); $month = date("m",$date_start);
$day = date("d",$date); $day = date("d",$date_start);
$year = date("Y",$date); $year = date("Y",$date_start);
switch($duree_unit) switch($duree_unit)
{ {
@ -67,17 +71,12 @@ class Contrat
$year = $year + $duree_value; $year = $year + $duree_value;
break; break;
} }
$date_end = mktime(date("H",$date_start), date("i",$date_start), 0, $month, $day, $year);
$fin = mktime(date("H",$date), }
date("i",$date),
0,
$month,
$day,
$year);
$sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET enservice = 1"; $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET enservice = 1";
$sql .= " , mise_en_service = ".$this->db->idate($date).", fk_user_mise_en_service = ".$user->id; $sql .= " , mise_en_service = ".$this->db->idate($date_start).", fk_user_mise_en_service = ".$user->id;
$sql .= " , fin_validite = ". $this->db->idate($fin); $sql .= " , fin_validite = ". $this->db->idate($date_end);
$sql .= " WHERE rowid = ".$this->id . " AND enservice = 0"; $sql .= " WHERE rowid = ".$this->id . " AND enservice = 0";
$result = $this->db->query($sql) ; $result = $this->db->query($sql) ;
@ -119,7 +118,7 @@ class Contrat
$sql = "SELECT rowid, enservice, fk_soc, fk_product, ".$this->db->pdate("mise_en_service")." as datemise"; $sql = "SELECT rowid, enservice, fk_soc, fk_product, ".$this->db->pdate("mise_en_service")." as datemise";
$sql .= ", fk_user_mise_en_service, ".$this->db->pdate("date_cloture")." as datecloture"; $sql .= ", fk_user_mise_en_service, ".$this->db->pdate("date_cloture")." as datecloture";
$sql .= ", ".$this->db->pdate("fin_validite")." as datefin"; $sql .= ", ".$this->db->pdate("fin_validite")." as datefin";
$sql .= ", fk_user_cloture, fk_facture"; $sql .= ", fk_user_cloture, fk_facture, fk_facturedet";
$sql .= " FROM ".MAIN_DB_PREFIX."contrat WHERE rowid = $id"; $sql .= " FROM ".MAIN_DB_PREFIX."contrat WHERE rowid = $id";
$result = $this->db->query($sql) ; $result = $this->db->query($sql) ;
@ -131,9 +130,10 @@ class Contrat
$this->id = $result["rowid"]; $this->id = $result["rowid"];
$this->enservice = $result["enservice"]; $this->enservice = $result["enservice"];
$this->factureid = $result["fk_facture"]; $this->factureid = $result["fk_facture"];
$this->facturedetid = $result["fk_facturedet"];
$this->mise_en_service = $result["datemise"]; $this->mise_en_service = $result["datemise"];
$this->date_cloture = $result["datecloture"];
$this->date_fin_validite = $result["datefin"]; $this->date_fin_validite = $result["datefin"];
$this->date_cloture = $result["datecloture"];
$this->user_service->id = $result["fk_user_mise_en_service"]; $this->user_service->id = $result["fk_user_mise_en_service"];
$this->user_cloture->id = $result["fk_user_cloture"]; $this->user_cloture->id = $result["fk_user_cloture"];
@ -152,13 +152,13 @@ class Contrat
return $result; return $result;
} }
/* /*
* * Crée autant de contrats que de lignes de facture, pour une facture donnée
* *
*/ */
Function create_from_facture($factureid, $user, $socid) Function create_from_facture($factureid, $user, $socid)
{ {
$sql = "SELECT p.rowid FROM ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."facturedet as f"; $sql = "SELECT p.rowid as rowid, fd.rowid as fdrowid FROM ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."facturedet as fd";
$sql .= " WHERE p.rowid = f.fk_product AND p.fk_product_type = 1 AND f.fk_facture = ".$factureid; $sql .= " WHERE p.rowid = fd.fk_product AND p.fk_product_type = 1 AND fd.fk_facture = ".$factureid;
if ($this->db->query($sql)) if ($this->db->query($sql))
{ {
@ -171,19 +171,20 @@ class Contrat
while ($i < $num) while ($i < $num)
{ {
$objp = $this->db->fetch_object($i); $objp = $this->db->fetch_object($i);
$contrats[$i] = $objp->rowid; $prowid[$i] = $objp->rowid;
$fdrowid[$i] = $objp->fdrowid;
$i++; $i++;
} }
$this->db->free(); $this->db->free();
while (list($key, $value) = each ($contrats)) while (list($i, $value) = each ($prowid))
{ {
$sql = "INSERT INTO ".MAIN_DB_PREFIX."contrat (fk_product, fk_facture, fk_soc, fk_user_author)"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."contrat (fk_product, fk_facture, fk_facturedet, fk_soc, fk_user_author)";
$sql .= " VALUES ($value, $factureid, $socid, $user->id)"; $sql .= " VALUES (".$prowid[$i].", $factureid, ".$fdrowid[$i].", $socid, $user->id)";
if (! $this->db->query($sql)) if (! $this->db->query($sql))
{ {
print $this->db->error(); print "Erreur : ".$this->db->error()."<br>".$sql;
} }
} }
} }

View File

@ -53,13 +53,9 @@ if ($_POST["action"] == 'miseenservice')
$contrat->id = $id; $contrat->id = $id;
$contrat->fetch($id); $contrat->fetch($id);
$contrat->mise_en_service($user, $contrat->mise_en_service($user,
mktime($_POST["rehour"], mktime($_POST["date_starthour"],$_POST["date_startmin"],0,$_POST["date_startmonth"],$_POST["date_startday"],$_POST["date_startyear"]),
$_POST["remin"],
0, 0,
$_POST["remonth"], mktime($_POST["date_endhour"],$_POST["date_endmin"],0,$_POST["date_endmonth"],$_POST["date_endday"],$_POST["date_endyear"])
$_POST["reday"],
$_POST["reyear"]),
$_POST["duration"]
); );
} }
@ -102,13 +98,13 @@ if ($action == 'update' && $cancel <> 'Annuler')
$mesg = 'Fiche non mise à jour !' . "<br>" . $product->mesg_error; $mesg = 'Fiche non mise à jour !' . "<br>" . $product->mesg_error;
} }
} }
/*
*
*
*/
$html = new Form($db); $html = new Form($db);
/*
* Fiche création
*
*/
if ($action == 'create') if ($action == 'create')
{ {
print "<form action=\"$PHP_SELF?type=$type\" method=\"post\">\n"; print "<form action=\"$PHP_SELF?type=$type\" method=\"post\">\n";
@ -129,7 +125,8 @@ if ($action == 'create')
print "</textarea></td></tr>"; print "</textarea></td></tr>";
if ($type == 1) if ($type == 1)
{ {
print '<tr><td>Durée</td><TD><input name="duration_value" size="6" value="'.$product->duree.'">'; // Si contrat de type service
print '<tr><td>Durée</td><td><input name="duration_value" size="6" value="'.$product->duree.'">';
print '<input name="duration_unit" type="radio" value="d">jour&nbsp;'; print '<input name="duration_unit" type="radio" value="d">jour&nbsp;';
print '<input name="duration_unit" type="radio" value="w">semaine&nbsp;'; print '<input name="duration_unit" type="radio" value="w">semaine&nbsp;';
print '<input name="duration_unit" type="radio" value="m">mois&nbsp;'; print '<input name="duration_unit" type="radio" value="m">mois&nbsp;';
@ -143,6 +140,10 @@ if ($action == 'create')
} }
else else
{ {
/*
* Fiche visu/édition
*
*/
if ($id) if ($id)
{ {
$contrat = new Contrat($db); $contrat = new Contrat($db);
@ -150,9 +151,8 @@ else
if ( $result ) if ( $result )
{ {
$facture = new Facture($db); $date_start='';
$facture->fetch($contrat->factureid); $date_end='';
print_fiche_titre('Fiche contrat : '.$contrat->id, $mesg); print_fiche_titre('Fiche contrat : '.$contrat->id, $mesg);
@ -160,8 +160,13 @@ else
print "<tr>"; print "<tr>";
print '<td width="20%">Service</td><td colspan=4>'.($contrat->product->ref).' - '.($contrat->product->label_url).'</td>'; print '<td width="20%">Service</td><td colspan=4>'.($contrat->product->ref).' - '.($contrat->product->label_url).'</td>';
print '</tr><tr>'; print '</tr><tr>';
if ($contrat->factureid) if ($contrat->facturedetid)
{ {
// Si contrat lié à une ligne de factures (ce n'est pas le cas sur les anciennes données)
$facturedet = new FactureLigne($db);
$facturedet->fetch($contrat->facturedetid);
$date_start=$facturedet->date_start;
$date_end=$facturedet->date_end;
print '<td>Société</td><td>'.$contrat->societe->nom_url.'</td>'; print '<td>Société</td><td>'.$contrat->societe->nom_url.'</td>';
print '<td>Facture</td><td><a href="../compta/facture.php?facid='.$contrat->factureid.'">Facture</td>'; print '<td>Facture</td><td><a href="../compta/facture.php?facid='.$contrat->factureid.'">Facture</td>';
} }
@ -179,24 +184,62 @@ else
{ {
print "<b>Ce contrat n'est pas en service</b>"; print "<b>Ce contrat n'est pas en service</b>";
} }
print '</td></tr>'; print "</td></tr>\n";
if ($request == 'miseenservice') if ($request == 'miseenservice')
{ {
// Si date_start et date_end ne sont pas connues de la ligne de facture, on les
// definit à une valeur par défaut en fonction de la durée définie pour le service.
if (! $date_start) { $date_start=mktime(); }
if (! $date_end) {
if ($contrat->product->duration) {
// Si duree du service connue
$duree_value = substr($contrat->product->duration,0,strlen($contrat->product->duration)-1);
$duree_unit = substr($contrat->product->duration,-1);
$month = date("m",$date_start);
$day = date("d",$date_start);
$year = date("Y",$date_start);
switch($duree_unit)
{
case "d":
$day = $day + $duree_value;
break;
case "w":
$day = $day + ($duree_value * 7);
break;
case "m":
$month = $month + $duree_value;
break;
case "y":
$year = $year + $duree_value;
break;
}
$date_end = mktime(date("H",$date_start), date("i",$date_start), 0, $month, $day, $year);
}
}
print '<form action="fiche.php?id='.$id.'" method="post">'; print '<form action="fiche.php?id='.$id.'" method="post">';
print '<input type="hidden" name="action" value="miseenservice">'; print '<input type="hidden" name="action" value="miseenservice">';
print '<tr><td>Date de mise en service</td><td colspan="3">';
print $html->select_date('','re',1,1);
print "&nbsp;";
print '</td></tr>';
print '<tr><td>Date de fin</td><td colspan="3">';
// TODO : Offrir le choix de la date de fin de contrat par la durée prédéfinie ou par
// une date explicite.
print $contrat->product->duration." après";
//print "<br>";$html->select_date('','fin',1,1);
print '<tr><td>Durée standard pour ce service</td><td colspan="3">';
print $contrat->product->duration;
print '<input type="hidden" name="duration" value="'.$contrat->product->duration.'">'; print '<input type="hidden" name="duration" value="'.$contrat->product->duration.'">';
print '</td></tr>'; print '</td></tr>';
// Date de début de mise en service
print '<tr><td>Date de mise en service</td><td colspan="3">';
print $html->select_date($date_start,'date_start',1,1);
print "&nbsp;";
print '</td></tr>';
// Date de fin prévue de mise en service
print '<tr><td>Date de fin prévue</td><td colspan="3">';
print $html->select_date($date_end,'date_end',1,1);
print "&nbsp;";
print '</td></tr>';
print '<tr><td colspan="4" align="center">'; print '<tr><td colspan="4" align="center">';
print '<input type="submit" value="Enregistrer">'; print '<input type="submit" value="Enregistrer">';
print '</td></tr>'; print '</td></tr>';
@ -251,7 +294,7 @@ if (! $contrat->enservice)
} }
elseif ($contrat->enservice == 1) elseif ($contrat->enservice == 1)
{ {
print '<a class="tabAction" href="fiche.php?action=annule&id='.$id.'">Annuler</a>'; print '<a class="tabAction" href="fiche.php?action=annule&id='.$id.'">Mettre hors service</a>';
print '<a class="tabAction" href="fiche.php?action=cloture&id='.$id.'">Clôturer</a>'; print '<a class="tabAction" href="fiche.php?action=cloture&id='.$id.'">Clôturer</a>';
} }
print '</div>'; print '</div>';

View File

@ -21,12 +21,49 @@
* *
*/ */
class FactureLigne class FactureLigne
{ {
Function FactureLigne() Function FactureLigne($DB)
{ {
$this->db= $DB ;
} }
/**
* Recupére l'objet ligne de facture
*
*/
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 .= " FROM ".MAIN_DB_PREFIX."facturedet WHERE rowid = ".$rowid;
$result = $this->db->query($sql);
if ($result)
{
$objp = $this->db->fetch_object($i);
$this->desc = stripslashes($objp->description);
$this->qty = $objp->qty;
$this->price = $objp->price;
$this->subprice = $objp->subprice;
$this->tva_taux = $objp->tva_taux;
$this->remise = $objp->remise;
$this->remise_percent = $objp->remise_percent;
$this->produit_id = $objp->fk_product;
$this->date_start = $objp->date_start;
$this->date_end = $objp->date_end;
$i++;
} }
else {
print "Erreur ".$this->db->error()."<br>".$sql;
}
$this->db->free();
}
}
class Facture class Facture
{ {

View File

@ -1,3 +1,7 @@
alter table llx_contrat add fk_facturedet integer NOT NULL default 0 after fk_facture;
alter table llx_contrat change fk_user_cloture fk_user_cloture integer;
alter table llx_contrat change fk_user_mise_en_service fk_user_mise_en_service integer;
alter table llx_facturedet add date_start date; alter table llx_facturedet add date_start date;
alter table llx_facturedet add date_end date; alter table llx_facturedet add date_end date;

View File

@ -1,5 +1,6 @@
-- ============================================================================ -- ============================================================================
-- Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org> -- Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
-- Copyright (C) 2004 Laurent Destailleur <eldy@users.sourceforge.net>
-- --
-- This program is free software; you can redistribute it and/or modify -- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by -- it under the terms of the GNU General Public License as published by
@ -31,8 +32,9 @@ create table llx_contrat
fk_soc integer NOT NULL, fk_soc integer NOT NULL,
fk_product integer NOT NULL, fk_product integer NOT NULL,
fk_facture integer NOT NULL default 0, fk_facture integer NOT NULL default 0,
fk_user_author integer NOT NULL, fk_facturedet integer NOT NULL default 0,
fk_user_mise_en_service integer NOT NULL, fk_user_author integer NOT NULL default 0,
fk_user_cloture integer NOT NULL fk_user_mise_en_service integer,
fk_user_cloture integer
)type=innodb; )type=innodb;