From ebdbeec4a9bea69ef3e72278dd76191d05298bec Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Wed, 28 Apr 2010 19:23:36 +0000 Subject: [PATCH] Fix: mutualize code --- htdocs/compta/facture.php | 170 +++++------------- htdocs/compta/facture/class/facture.class.php | 22 +-- htdocs/core/class/commonobject.class.php | 1 - 3 files changed, 44 insertions(+), 149 deletions(-) diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 579fd50fded..d3ba746ff1b 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -39,9 +39,6 @@ require_once(DOL_DOCUMENT_ROOT."/lib/functions2.lib.php"); require_once(DOL_DOCUMENT_ROOT.'/lib/invoice.lib.php'); if ($conf->projet->enabled) require_once(DOL_DOCUMENT_ROOT.'/projet/project.class.php'); if ($conf->projet->enabled) require_once(DOL_DOCUMENT_ROOT.'/lib/project.lib.php'); -if ($conf->propal->enabled) require_once(DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php'); -if ($conf->contrat->enabled) require_once(DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php'); -if ($conf->commande->enabled) require_once(DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'); $langs->load('bills'); $langs->load('companies'); @@ -666,84 +663,53 @@ if ($_POST['action'] == 'add' && $user->rights->facture->creer) $facture->remise_percent = $_POST['remise_percent']; // If creation from proposal - if ($_POST['propalid']) + if ($_POST['origin'] && $_POST['originid']) { - $facture->propalid = $_POST['propalid']; + // Parse element/subelement (ex: project_task) + $element = $subelement = $_POST['origin']; + if (preg_match('/^([^_]+)_([^_]+)/i',$_POST['origin'],$regs)) + { + $element = $regs[1]; + $subelement = $regs[2]; + } + + // For compatibility + if ($element == 'order') { $element = $subelement = 'commande'; } + if ($element == 'propal') { $element = 'comm/propal'; $subelement = 'propal'; } + if ($element == 'contract') { $element = $subelement = 'contrat'; } + + $facture->origin = $_POST['origin']; + $facture->origin_id = $_POST['originid']; + $facid = $facture->create($user); if ($facid > 0) { - $prop = new Propal($db); - if ( $prop->fetch($_POST['propalid']) ) + require_once(DOL_DOCUMENT_ROOT.'/'.$element.'/class/'.$subelement.'.class.php'); + $classname = ucfirst($subelement); + $object = new $classname($db); + + if ($object->fetch($_POST['originid'])) { - for ($i = 0 ; $i < sizeof($prop->lignes) ; $i++) - { - $desc=($prop->lignes[$i]->desc?$prop->lignes[$i]->desc:$prop->lignes[$i]->libelle); - - // Dates - $date_start=$prop->lignes[$i]->date_debut_prevue; - if ($prop->lignes[$i]->date_debut_reel) $date_start=$prop->lignes[$i]->date_debut_reel; - $date_end=$prop->lignes[$i]->date_fin_prevue; - if ($prop->lignes[$i]->date_fin_reel) $date_end=$prop->lignes[$i]->date_fin_reel; - - $result = $facture->addline( - $facid, - $desc, - $prop->lignes[$i]->subprice, - $prop->lignes[$i]->qty, - $prop->lignes[$i]->tva_tx, - $prop->lignes[$i]->fk_product, - $prop->lignes[$i]->remise_percent, - $date_start, - $date_end, - 0, - $prop->lignes[$i]->info_bits, - $prop->lignes[$i]->fk_remise_except, - 'HT', - 0, - $prop->lignes[$i]->product_type - ); - - if ($result < 0) - { - $error++; - break; - } - } - } - else - { - $error++; - } - } - else - { - $error++; - } - } - - // If creation from order - else if ($_POST['commandeid']) - { - $facture->commandeid = $_POST['commandeid']; - $facid = $facture->create($user); - - if ($facid > 0) - { - $comm = new Commande($db); - if ( $comm->fetch($_POST['commandeid']) ) - { - $comm->fetch_lines(); - $lines = $comm->lignes; + // TODO mutualiser + $lines = $object->lignes; + if (empty($lines) && method_exists($object,'fetch_lignes')) $lines = $object->fetch_lignes(); + if (empty($lines) && method_exists($object,'fetch_lines')) $lines = $object->fetch_lines(); + for ($i = 0 ; $i < sizeof($lines) ; $i++) { - $desc=($lines[$i]->desc ? $lines[$i]->desc : $lines[$i]->libelle); + $desc=($lines[$i]->desc?$lines[$i]->desc:$lines[$i]->libelle); + $product_type=($lines[$i]->product_type?$lines[$i]->product_type:0); // Dates - $date_start=$comm->lignes[$i]->date_start; - $date_end=$comm->lignes[$i]->date_end; + // TODO mutualiser + $date_start=$lines[$i]->date_debut_prevue; + if ($lines[$i]->date_debut_reel) $date_start=$lines[$i]->date_debut_reel; + if ($lines[$i]->date_start) $date_start=$lines[$i]->date_start; + $date_end=$lines[$i]->date_fin_prevue; + if ($lines[$i]->date_fin_reel) $date_end=$lines[$i]->date_fin_reel; + if ($lines[$i]->date_end) $date_end=$lines[$i]->date_end; - // Should use a function using total_ht, total_ttc and total_vat $result = $facture->addline( $facid, $desc, @@ -759,63 +725,7 @@ if ($_POST['action'] == 'add' && $user->rights->facture->creer) $lines[$i]->fk_remise_except, 'HT', 0, - $lines[$i]->product_type - ); - - if ($result < 0) - { - $error++; - break; - } - } - } - else - { - $error++; - } - } - else - { - $error++; - } - } - - // If creation from contract - else if ($_POST['contratid']) - { - $facture->contratid = $_POST['contratid']; - $facid = $facture->create($user); - - if ($facid > 0) - { - $contrat = New Contrat($db); - if ($contrat->fetch($_POST['contratid']) > 0) - { - $lines = $contrat->fetch_lignes(); - - for ($i = 0 ; $i < sizeof($lines) ; $i++) - { - $desc=($contrat->lignes[$i]->desc?$contrat->lignes[$i]->desc:$contrat->lignes[$i]->libelle); - - // Dates - $date_start=$contrat->lignes[$i]->date_debut_prevue; - if ($contrat->lignes[$i]->date_debut_reel) $date_start=$contrat->lignes[$i]->date_debut_reel; - $date_end=$contrat->lignes[$i]->date_fin_prevue; - if ($contrat->lignes[$i]->date_fin_reel) $date_end=$contrat->lignes[$i]->date_fin_reel; - - $result = $facture->addline( - $facid, - $desc, - $lines[$i]->subprice, - $lines[$i]->qty, - $lines[$i]->tva_tx, - $lines[$i]->fk_product, - $lines[$i]->remise_percent, - $date_start, - $date_end, - 0, - $lines[$i]->info_bits, - $lines[$i]->fk_remise_except + $product_type ); if ($result < 0) @@ -867,9 +777,8 @@ if ($_POST['action'] == 'add' && $user->rights->facture->creer) { $db->rollback(); $_GET["action"]='create'; - $_GET["propalid"]=$_POST["propalid"]; - $_GET["commandeid"]=$_POST["commandeid"]; - $_GET["contratid"]=$_POST["contratid"]; + $_GET["origin"]=$_POST["origin"]; + $_GET["originid"]=$_POST["originid"]; if (! $mesg) $mesg='
'.$facture->error.'
'; } } @@ -1798,6 +1707,7 @@ if ($_GET['action'] == 'create') print ''."\n"; print ''."\n"; print ''."\n"; + print ''; print ''; print ''.$langs->trans($classname).''.$object->getNomUrl(1).''; diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index 83a4ed74ddf..03361860b3c 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -89,7 +89,8 @@ class Facture extends CommonObject var $close_code; //! Commentaire si mis a paye sans paiement complet var $close_note; - var $propalid; + var $origin; + var $origin_id; var $fk_project; var $date_lim_reglement; var $cond_reglement_id; // Id in llx_cond_reglement @@ -253,24 +254,9 @@ class Facture extends CommonObject $resql=$this->db->query($sql); if (! $resql) $error++; - // Mise a jour lien avec propal ou commande - if (! $error && $this->id && $this->propalid) + // Add object linked + if (! $error && $this->id && $this->origin && $this->origin_id) { - // TODO normaliser ? - $this->origin_id = $this->propalid; - $this->origin = "propal"; - $ret = $this->add_object_linked(); - if (! $ret) - { - dol_print_error($this->db); - $error++; - } - } - if (! $error && $this->id && $this->commandeid) - { - // TODO normaliser ? - $this->origin_id = $this->commandeid; - $this->origin = "commande"; $ret = $this->add_object_linked(); if (! $ret) { diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 2d28c217dcb..7b5afc2d400 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -1031,7 +1031,6 @@ class CommonObject $tplpath = $object; if ($object == 'facture') $tplpath = $classpath = 'compta/'.$object; if ($object == 'propal') $tplpath = $classpath = 'comm/'.$object; - if ($object == 'commande') $tplpath = $classpath = $object; $classname = ucfirst($object); if(!class_exists($classname)) require(DOL_DOCUMENT_ROOT."/".$classpath."/".$object.".class.php");