diff --git a/htdocs/contrat/contrat.class.php b/htdocs/contrat/contrat.class.php index 782a6e85e1d..921f82d83ab 100644 --- a/htdocs/contrat/contrat.class.php +++ b/htdocs/contrat/contrat.class.php @@ -1,5 +1,6 @@ + * Copyright (C) 2004 Destailleur Laurent * * 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 @@ -38,52 +39,50 @@ class Contrat $this->user_service = 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) { - $duree_value = substr($duree,0,strlen($duree)-1); - $duree_unit = substr($duree,-1); + if ($duree) { + // Si duree renseignee + $duree_value = substr($duree,0,strlen($duree)-1); + $duree_unit = substr($duree,-1); - $month = date("m",$date); - $day = date("d",$date); - $year = date("Y",$date); + $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; - } - - $fin = mktime(date("H",$date), - date("i",$date), - 0, - $month, - $day, - $year); + 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); + } $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 .= " , fin_validite = ". $this->db->idate($fin); + $sql .= " , mise_en_service = ".$this->db->idate($date_start).", fk_user_mise_en_service = ".$user->id; + $sql .= " , fin_validite = ". $this->db->idate($date_end); $sql .= " WHERE rowid = ".$this->id . " AND enservice = 0"; $result = $this->db->query($sql) ; if (!$result) { - print $this->db->error() . "
" . $sql; + print $this->db->error() . "
" . $sql; } } /* @@ -119,7 +118,7 @@ class Contrat $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 .= ", ".$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"; $result = $this->db->query($sql) ; @@ -131,9 +130,10 @@ class Contrat $this->id = $result["rowid"]; $this->enservice = $result["enservice"]; $this->factureid = $result["fk_facture"]; + $this->facturedetid = $result["fk_facturedet"]; $this->mise_en_service = $result["datemise"]; - $this->date_cloture = $result["datecloture"]; $this->date_fin_validite = $result["datefin"]; + $this->date_cloture = $result["datecloture"]; $this->user_service->id = $result["fk_user_mise_en_service"]; $this->user_cloture->id = $result["fk_user_cloture"]; @@ -152,13 +152,13 @@ class Contrat return $result; } /* - * + * Crée autant de contrats que de lignes de facture, pour une facture donnée * */ 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 .= " WHERE p.rowid = f.fk_product AND p.fk_product_type = 1 AND f.fk_facture = ".$factureid; + $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 = fd.fk_product AND p.fk_product_type = 1 AND fd.fk_facture = ".$factureid; if ($this->db->query($sql)) { @@ -171,19 +171,20 @@ class Contrat while ($i < $num) { $objp = $this->db->fetch_object($i); - $contrats[$i] = $objp->rowid; + $prowid[$i] = $objp->rowid; + $fdrowid[$i] = $objp->fdrowid; $i++; } $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 .= " VALUES ($value, $factureid, $socid, $user->id)"; + $sql = "INSERT INTO ".MAIN_DB_PREFIX."contrat (fk_product, fk_facture, fk_facturedet, fk_soc, fk_user_author)"; + $sql .= " VALUES (".$prowid[$i].", $factureid, ".$fdrowid[$i].", $socid, $user->id)"; if (! $this->db->query($sql)) { - print $this->db->error(); + print "Erreur : ".$this->db->error()."
".$sql; } } } diff --git a/htdocs/contrat/fiche.php b/htdocs/contrat/fiche.php index a3b375e08b0..bf9d7cc2204 100644 --- a/htdocs/contrat/fiche.php +++ b/htdocs/contrat/fiche.php @@ -1,6 +1,6 @@ - * Copyright (C) 2004 Laurent Destailleur + * Copyright (C) 2004 Laurent Destailleur * * 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 @@ -53,14 +53,10 @@ if ($_POST["action"] == 'miseenservice') $contrat->id = $id; $contrat->fetch($id); $contrat->mise_en_service($user, - mktime($_POST["rehour"], - $_POST["remin"], - 0, - $_POST["remonth"], - $_POST["reday"], - $_POST["reyear"]), - $_POST["duration"] - ); + mktime($_POST["date_starthour"],$_POST["date_startmin"],0,$_POST["date_startmonth"],$_POST["date_startday"],$_POST["date_startyear"]), + 0, + mktime($_POST["date_endhour"],$_POST["date_endmin"],0,$_POST["date_endmonth"],$_POST["date_endday"],$_POST["date_endyear"]) + ); } if ($_GET["action"] == 'cloture') @@ -102,13 +98,13 @@ if ($action == 'update' && $cancel <> 'Annuler') $mesg = 'Fiche non mise à jour !' . "
" . $product->mesg_error; } } -/* - * - * - */ $html = new Form($db); +/* + * Fiche création + * + */ if ($action == 'create') { print "
\n"; @@ -129,7 +125,8 @@ if ($action == 'create') print ""; if ($type == 1) { - print 'Durée'; + // Si contrat de type service + print 'Durée'; print 'jour '; print 'semaine '; print 'mois '; @@ -143,25 +140,33 @@ if ($action == 'create') } else { +/* + * Fiche visu/édition + * + */ if ($id) { $contrat = new Contrat($db); $result = $contrat->fetch($id); if ( $result ) - { - $facture = new Facture($db); - $facture->fetch($contrat->factureid); - - + { + $date_start=''; + $date_end=''; + print_fiche_titre('Fiche contrat : '.$contrat->id, $mesg); print ''; print ""; print ''; print ''; - 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 ''; print ''; } @@ -179,24 +184,62 @@ else { print "Ce contrat n'est pas en service"; } - print ''; + print "\n"; 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 ''; print ''; - print ''; - print ''; + + // Date de début de mise en service + print ''; + + // Date de fin prévue de mise en service + print ''; + print ''; @@ -251,7 +294,7 @@ if (! $contrat->enservice) } elseif ($contrat->enservice == 1) { - print 'Annuler'; + print 'Mettre hors service'; print 'Clôturer'; } print ''; diff --git a/htdocs/facture.class.php b/htdocs/facture.class.php index 9b186e91fde..7b6153f6ffb 100644 --- a/htdocs/facture.class.php +++ b/htdocs/facture.class.php @@ -21,13 +21,50 @@ * */ + 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()."
".$sql; + } + $this->db->free(); + } + } + + + class Facture { var $id; diff --git a/mysql/migration/1.1.0-1.2.0-RC1.sql b/mysql/migration/1.1.0-1.2.0-RC1.sql index e02ae19fb14..6c8518ffd38 100644 --- a/mysql/migration/1.1.0-1.2.0-RC1.sql +++ b/mysql/migration/1.1.0-1.2.0-RC1.sql @@ -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_end date; diff --git a/mysql/tables/llx_contrat.sql b/mysql/tables/llx_contrat.sql index f18b911b3dd..566891f1471 100644 --- a/mysql/tables/llx_contrat.sql +++ b/mysql/tables/llx_contrat.sql @@ -1,5 +1,6 @@ -- ============================================================================ -- Copyright (C) 2002-2003 Rodolphe Quiedeville +-- Copyright (C) 2004 Laurent Destailleur -- -- 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 @@ -31,8 +32,9 @@ create table llx_contrat fk_soc integer NOT NULL, fk_product integer NOT NULL, fk_facture integer NOT NULL default 0, - fk_user_author integer NOT NULL, - fk_user_mise_en_service integer NOT NULL, - fk_user_cloture integer NOT NULL + fk_facturedet integer NOT NULL default 0, + fk_user_author integer NOT NULL default 0, + fk_user_mise_en_service integer, + fk_user_cloture integer )type=innodb;
Service'.($contrat->product->ref).' - '.($contrat->product->label_url).'
Société'.$contrat->societe->nom_url.'FactureFacture
Date de mise en service'; - print $html->select_date('','re',1,1); - print " "; - print '
Date de fin'; - - // 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 "
";$html->select_date('','fin',1,1); + print '
Durée standard pour ce service'; + print $contrat->product->duration; print ''; print '
Date de mise en service'; + print $html->select_date($date_start,'date_start',1,1); + print " "; + print '
Date de fin prévue'; + print $html->select_date($date_end,'date_end',1,1); + print " "; + print '
'; print ''; print '