dolibarr/htdocs/propal.class.php

2264 lines
69 KiB
PHP
Raw Normal View History

2004-10-20 21:06:45 +00:00
<?php
2006-02-03 18:21:55 +00:00
/* Copyright (C) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004 <EFBFBD>ric Seigne <eric.seigne@ryxeo.com>
* Copyright (C) 2004-2005 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
2006-02-12 10:59:11 +00:00
* Copyright (C) 2005-2006 Regis Houssin <regis.houssin@cap-networks.com>
2006-02-03 18:21:55 +00:00
* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
2003-09-11 16:00:49 +00:00
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
* $Source$
*/
/**
\file htdocs/propal.class.php
\brief Fichier de la classe des propales
\author Rodolphe Qiedeville
\author Eric Seigne
\author Laurent Destailleur
\version $Revision$
*/
require_once(DOL_DOCUMENT_ROOT ."/product.class.php");
require_once(DOL_DOCUMENT_ROOT ."/contact.class.php");
/**
\class Propal
\brief Classe permettant la gestion des propales
*/
2004-07-13 17:07:05 +00:00
2003-09-11 16:00:49 +00:00
class Propal
{
var $id;
var $db;
var $socidp;
var $contactid;
var $projetidp;
var $author;
var $ref;
var $status;
var $datep;
var $fin_validite;
var $products;
var $products_qty;
var $price;
var $cond_reglement_id;
var $cond_reglement_code;
var $mode_reglement_id;
var $mode_reglement_code;
var $remise;
var $remise_percent;
var $remise_absolue;
var $note;
var $note_public;
var $date_livraison;
var $adresse_livraison_id;
var $labelstatut=array();
var $labelstatut_short=array();
var $product=array();
// Pour board
var $nbtodo;
var $nbtodolate;
var $specimen;
var $error;
2003-09-11 16:00:49 +00:00
2004-07-13 17:07:05 +00:00
/**
2005-08-14 02:47:08 +00:00
* \brief Constructeur
2005-09-02 21:34:22 +00:00
* \param DB Handler d'acc<EFBFBD>s base
* \param soc_idp Id de la soci<EFBFBD>t<EFBFBD>
* \param propalid Id de la propal
2004-10-20 21:06:45 +00:00
*/
function Propal($DB, $soc_idp="", $propalid=0)
2003-09-11 16:00:49 +00:00
{
2005-06-29 01:31:37 +00:00
global $langs;
2003-09-11 16:00:49 +00:00
$this->db = $DB ;
$this->socidp = $soc_idp;
$this->id = $propalid;
$this->products = array();
$this->remise = 0;
$this->remise_percent = 0;
$this->remise_absolue = 0;
2005-06-29 01:31:37 +00:00
$langs->load("propals");
$this->labelstatut[0]=$langs->trans("PropalStatusDraft");
$this->labelstatut[1]=$langs->trans("PropalStatusValidated");
$this->labelstatut[2]=$langs->trans("PropalStatusSigned");
$this->labelstatut[3]=$langs->trans("PropalStatusNotSigned");
$this->labelstatut[4]=$langs->trans("PropalStatusBilled");
$this->labelstatut_short[0]=$langs->trans("PropalStatusDraftShort");
$this->labelstatut_short[1]=$langs->trans("Opened");
$this->labelstatut_short[2]=$langs->trans("PropalStatusSignedShort");
$this->labelstatut_short[3]=$langs->trans("PropalStatusNotSignedShort");
$this->labelstatut_short[4]=$langs->trans("PropalStatusBilledShort");
2003-09-11 16:00:49 +00:00
}
2004-10-20 21:06:45 +00:00
/**
* \brief Ajout d'un produit dans la proposition, en memoire dans l'objet
* \param idproduct Id du produit <EFBFBD> ajouter
* \param qty Quantit<EFBFBD>
* \param remise_percent Remise relative effectu<EFBFBD>e sur le produit
* \return void
* \see insert_product
2003-09-11 16:00:49 +00:00
*/
function add_product($idproduct, $qty, $remise_percent=0)
2003-09-11 16:00:49 +00:00
{
if ($idproduct > 0)
{
$i = sizeof($this->products);
$this->products[$i] = $idproduct;
if (!$qty)
{
$qty = 1 ;
}
$this->products_qty[$i] = $qty;
$this->products_remise_percent[$i] = $remise_percent;
}
2003-09-11 16:00:49 +00:00
}
/**
* \brief Ajout d'un produit dans la proposition, en base
* \param idproduct Id du produit <EFBFBD> ajouter
* \param qty Quantit<EFBFBD>
* \param remise_percent Remise relative effectu<EFBFBD>e sur le produit
* \param p_desc Descriptif optionnel
* \return int >0 si ok, <0 si ko
* \see add_product
*/
function insert_discount($idproduct, $qty, $remise_percent=0, $p_desc='')
{
}
/**
* \brief Ajout d'un produit dans la proposition, en base
* \param idproduct Id du produit <EFBFBD> ajouter
* \param qty Quantit<EFBFBD>
* \param remise_percent Remise relative effectu<EFBFBD>e sur le produit
* \param p_desc Descriptif optionnel
* \return int >0 si ok, <0 si ko
* \see add_product
*/
2006-02-12 14:19:29 +00:00
function insert_product($idproduct, $qty, $remise_percent=0, $p_desc='')
2003-09-11 16:00:49 +00:00
{
2006-02-03 16:42:39 +00:00
global $conf;
dolibarr_syslog("propal.class.php::insert_product $idproduct, $qty, $remise_percent, $p_desc");
if ($this->statut == 0)
{
2005-10-18 20:03:30 +00:00
// Nettoyage parametres
$remise_percent=price2num($remise_percent);
$qty=price2num($qty);
if ($idproduct)
{
$prod = new Product($this->db, $idproduct);
if ($prod->fetch($idproduct) > 0)
{
2006-02-12 12:47:25 +00:00
$p_product_desc = $prod->description;
2006-02-21 13:06:19 +00:00
$this -> fetch_client();
if($this->client->tva_assuj == "0")
$txtva ="0";
else
$txtva=$prod->tva_tx;
2006-02-03 16:42:39 +00:00
// multiprix
if($conf->global->PRODUIT_MULTIPRICES == 1)
{
$price = price2num($prod->multiprices[$this->client->price_level]);
$subprice = price2num($prod->multiprices[$this->client->price_level]);
}
else
{
$price = price2num($prod->price);
$subprice = price2num($prod->price);
}
/*
2006-02-03 16:42:39 +00:00
*/
// Calcul remise et nouveau prix
$remise = 0;
if ($remise_percent > 0)
{
$remise = round(($prod->price * $remise_percent / 100), 2);
$price = $prod->price - $remise;
}
if ($conf->global->PRODUIT_CHANGE_PROD_DESC)
2006-02-12 12:08:04 +00:00
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."propaldet (fk_propal, fk_product, qty, price, tva_tx, description, remise_percent, subprice) VALUES ";
$sql .= " (".$this->id.",". $idproduct.",'". $qty."','". price2num($price)."','".$txtva."','".addslashes($p_product_desc)."','".price2num($remise_percent)."','".price2num($subprice)."')";
2006-02-12 12:08:04 +00:00
}
else
2006-02-12 14:40:34 +00:00
{
2006-02-12 12:08:04 +00:00
$sql = "INSERT INTO ".MAIN_DB_PREFIX."propaldet (fk_propal, fk_product, qty, price, tva_tx, description, remise_percent, subprice) VALUES ";
$sql .= " (".$this->id.",". $idproduct.",'". $qty."','". price2num($price) ."','".$txtva."','".addslashes($p_desc?$p_desc:$prod->label)."','".price2num($remise_percent)."','".price2num($subprice)."')";
2006-02-12 14:40:34 +00:00
}
if ($this->db->query($sql) )
{
$this->update_price();
return 1;
}
else
{
$this->error=$this->db->error();
return -1;
}
}
else
{
2005-09-04 13:10:43 +00:00
$this->error=$this->db->error();
return -2;
}
}
}
2005-09-04 13:10:43 +00:00
else
{
return -3;
}
2003-09-11 16:00:49 +00:00
}
/**
* \brief Ajout d'un produit dans la proposition, en base
* \param p_desc Descriptif optionnel
* \param p_price Prix
* \param p_qty Quantit<EFBFBD>
* \param p_tva_tx Taux tva
* \param remise_percent Remise effectu<EFBFBD>e sur le produit
* \return int >0 si ok, <0 si ko
* \see add_product
*/
2006-02-12 14:44:14 +00:00
function insert_product_generic($p_desc, $p_price, $p_qty, $p_tva_tx, $remise_percent=0)
{
2006-02-12 14:44:14 +00:00
dolibarr_syslog("propal.class.php::insert_product_generic $p_desc, $p_price, $p_qty, $p_tva_tx, $remise_percent");
if ($this->statut == 0)
{
// Nettoyage param<61>tres
$remise_percent=price2num($remise_percent);
$p_qty=price2num($p_qty);
if (strlen(trim($p_qty))==0) $p_qty=1;
$p_price = price2num($p_price);
$price = $p_price;
$subprice = $p_price;
if ($remise_percent > 0)
{
$remise = round(($p_price * $remise_percent / 100), 2);
$price = $p_price - $remise;
}
2006-02-12 11:57:39 +00:00
$sql = "INSERT INTO ".MAIN_DB_PREFIX."propaldet (fk_propal, fk_product, qty, price, tva_tx, description, remise_percent, subprice) VALUES ";
$sql .= " (".$this->id.", 0,'". $p_qty."','". price2num($price)."','".$p_tva_tx."','".addslashes($p_desc)."','$remise_percent', '".price2num($subprice)."') ; ";
if ($this->db->query($sql) )
{
if ($this->update_price() > 0)
{
return 1;
}
else
{
return -1;
}
}
else
{
return -2;
}
}
}
/**
* \brief Mise <EFBFBD> jour d'une ligne de produit
* \param id Id de la ligne
* \param subprice Prix unitaire
* \param qty Quantit<EFBFBD>
* \param remise_percent Remise effectu<EFBFBD>e sur le produit
2005-09-02 21:34:22 +00:00
* \param tva_tx Taux de TVA
* \param desc Description
* \return int 0 en cas de succ<EFBFBD>s
*/
function UpdateLigne($id, $subprice, $qty, $remise_percent=0, $tva_tx, $desc='')
{
if ($this->statut == 0)
{
2005-10-18 20:03:30 +00:00
// Nettoyage param<61>tres
$subprice=price2num($subprice);
$price = $subprice;
2005-10-18 20:03:30 +00:00
$remise_percent=price2num($remise_percent);
$tva_tx=price2num($tva_tx);
if ($remise_percent > 0)
{
$remise = round(($subprice * $remise_percent / 100), 2);
$price = $subprice - $remise;
}
$sql = "UPDATE ".MAIN_DB_PREFIX."propaldet ";
$sql.= " SET qty='".$qty."'";
$sql.= " , price='". price2num($price)."'";
2005-10-18 20:03:30 +00:00
$sql.= " , remise_percent='".$remise_percent."'";
$sql.= " , subprice='".$subprice."'";
$sql.= " , tva_tx='".$tva_tx."'";
$sql.= " , description='".addslashes($desc)."'";
$sql.= " WHERE rowid = '".$id."';";
if ($this->db->query($sql))
{
$this->update_price();
return 0;
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Propal::UpdateLigne Erreur -1");
return -1;
}
}
else
{
dolibarr_syslog("Propal::UpdateLigne Erreur -2 Propal en mode incompatible pour cette action");
return -2;
}
}
/**
* \brief Liste les valeurs possibles de type de contacts pour les factures
* \param source 'internal' ou 'external'
* \return array Tableau des types de contacts
*/
function liste_type_contact($source)
{
global $langs;
$element='propal';
$tab = array();
$sql = "SELECT distinct tc.rowid, tc.code, tc.libelle";
$sql.= " FROM ".MAIN_DB_PREFIX."c_type_contact as tc";
$sql.= " WHERE element='".$element."'";
$sql.= " AND source='".$source."'";
$sql.= " ORDER by tc.code";
$resql=$this->db->query($sql);
if ($resql)
{
$num=$this->db->num_rows($resql);
$i=0;
while ($i < $num)
{
$obj = $this->db->fetch_object($resql);
$transkey="TypeContact_".$element."_".$source."_".$obj->code;
$libelle_type=($langs->trans($transkey)!=$transkey ? $langs->trans($transkey) : $obj->libelle);
$tab[$obj->rowid]=$libelle_type;
$i++;
}
return $tab;
}
else
{
$this->error=$this->db->error();
return null;
}
}
/**
* \brief R<EFBFBD>cup<EFBFBD>re les lignes de contact de l'objet
* \param statut Statut des lignes detail <EFBFBD> r<EFBFBD>cup<EFBFBD>rer
* \param source Source du contact external (llx_socpeople) ou internal (llx_user)
* \return array Tableau des rowid des contacts
*/
function liste_contact($statut=-1,$source='external')
{
global $langs;
$element='propal';
$tab=array();
$sql = "SELECT ec.rowid, ec.statut, ec.fk_socpeople as id,";
if ($source == 'internal') $sql.=" '-1' as socid,";
if ($source == 'external') $sql.=" t.fk_soc as socid,";
if ($source == 'internal') $sql.=" t.name as nom,";
if ($source == 'external') $sql.=" t.name as nom,";
$sql.= "tc.source, tc.element, tc.code, tc.libelle";
$sql.= " FROM ".MAIN_DB_PREFIX."element_contact ec,";
if ($source == 'internal') $sql.=" ".MAIN_DB_PREFIX."user t,";
if ($source == 'external') $sql.=" ".MAIN_DB_PREFIX."socpeople t,";
$sql.= " ".MAIN_DB_PREFIX."c_type_contact tc";
$sql.= " WHERE element_id =".$this->id;
$sql.= " AND ec.fk_c_type_contact=tc.rowid";
$sql.= " AND tc.element='".$element."'";
if ($source == 'internal') $sql.= " AND tc.source = 'internal'";
if ($source == 'external') $sql.= " AND tc.source = 'external'";
$sql.= " AND tc.active=1";
if ($source == 'internal') $sql.= " AND ec.fk_socpeople = t.rowid";
if ($source == 'external') $sql.= " AND ec.fk_socpeople = t.idp";
if ($statut >= 0) $sql.= " AND statut = '$statut'";
$sql.=" ORDER BY t.name ASC";
$resql=$this->db->query($sql);
if ($resql)
{
$num=$this->db->num_rows($resql);
$i=0;
while ($i < $num)
{
$obj = $this->db->fetch_object($resql);
$transkey="TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
$libelle_type=($langs->trans($transkey)!=$transkey ? $langs->trans($transkey) : $obj->libelle);
$tab[$i]=array('source'=>$obj->source,'socid'=>$obj->socid,'id'=>$obj->id,'nom'=>$obj->nom,'rowid'=>$obj->rowid,'code'=>$obj->code,'libelle'=>$libelle_type,'status'=>$obj->statut);
$i++;
}
return $tab;
}
else
{
$this->error=$this->db->error();
dolibarr_print_error($this->db);
return -1;
}
}
/**
* \brief Ajoute un contact associ<EFBFBD> une commande
* \param fk_socpeople Id du contact a ajouter.
* \param type_contact Type de contact
* \param source extern=Contact externe (llx_socpeople), intern=Contact interne (llx_user)
* \return int <0 si erreur, >0 si ok
*/
function add_contact($fk_socpeople, $type_contact, $source='extern')
{
dolibarr_syslog("Commande::add_contact $fk_socpeople, $type_contact, $source");
if ($fk_socpeople <= 0) return -1;
// Verifie type_contact
if (! $type_contact || ! is_numeric($type_contact))
{
$this->error="Valeur pour type_contact incorrect";
return -3;
}
$datecreate = time();
// Insertion dans la base
$sql = "INSERT INTO ".MAIN_DB_PREFIX."element_contact";
$sql.= " (element_id, fk_socpeople, datecreate, statut, fk_c_type_contact) ";
$sql.= " VALUES (".$this->id.", ".$fk_socpeople." , " ;
$sql.= $this->db->idate($datecreate);
$sql.= ", 4, '". $type_contact . "' ";
$sql.= ")";
// Retour
if ( $this->db->query($sql) )
{
return 1;
}
else
{
$this->error=$this->db->error()." - $sql";
return -1;
}
}
/**
* \brief Supprime une ligne de contact
* \param rowid La reference du contact
* \return statur >0 si ok, <0 si ko
*/
function delete_contact($rowid)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."element_contact";
$sql.= " WHERE rowid =".$rowid;
if ($this->db->query($sql))
{
return 1;
}
else
{
return -1;
}
}
/**
* \brief Le d<EFBFBD>tail d'un contact
* \param rowid L'identifiant du contact
* \return object L'objet construit par DoliDb.fetch_object
*/
function detail_contact($rowid)
{
$element='propal';
$sql = "SELECT ec.datecreate, ec.statut, ec.fk_socpeople, ec.fk_c_type_contact,";
$sql.= " tc.code, tc.libelle, s.fk_soc";
$sql.= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc, ";
$sql.= " ".MAIN_DB_PREFIX."socpeople as s";
$sql.= " WHERE ec.rowid =".$rowid;
$sql.= " AND ec.fk_socpeople=s.idp";
$sql.= " AND ec.fk_c_type_contact=tc.rowid";
$sql.= " AND tc.element = '".$element."'";
$resql=$this->db->query($sql);
if ($resql)
{
$obj = $this->db->fetch_object($resql);
return $obj;
}
else
{
$this->error=$this->db->error();
dolibarr_print_error($this->db);
return null;
}
}
/**
* \brief Mise a jour du contact associ<EFBFBD> <EFBFBD> une commande
* \param rowid La reference du lien commande-contact
* \param statut Le nouveau statut
* \param type_contact_id Description du type de contact
* \return int <0 si erreur, =0 si ok
*/
function update_contact($rowid, $statut, $type_contact_id)
{
// Insertion dans la base
$sql = "UPDATE ".MAIN_DB_PREFIX."element_contact set";
$sql.= " statut = $statut,";
$sql.= " fk_c_type_contact = '".$type_contact_id ."'";
$sql.= " where rowid = ".$rowid;
// Retour
if ( $this->db->query($sql) )
{
return 0;
}
else
{
dolibarr_print_error($this->db);
return -1;
}
}
/**
* \brief Renvoi tableau des id des contacts d'un type donn<EFBFBD>
* \param source 'internel' ou 'external'
* \param code Type de contact
* \param array Tableau des id de r<EFBFBD>ponses
*/
function getIdContact($source,$code)
{
$element='propal'; // Contact sur propal
$result=array();
$i=0;
$sql = "SELECT ec.fk_socpeople";
$sql.= " FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc";
$sql.= " WHERE ec.element_id = ".$this->id;
$sql.= " AND ec.fk_c_type_contact=tc.rowid";
$sql.= " AND tc.element = '".$element."'";
$sql.= " AND tc.source = '".$source."'";
$sql.= " AND tc.code = '".$code."'";
$resql=$this->db->query($sql);
if ($resql)
{
while ($obj = $this->db->fetch_object($resql))
{
$result[$i]=$obj->fk_socpeople;
$i++;
}
}
else
{
$this->error=$this->db->error();
return null;
}
return $result;
}
/**
2003-09-11 16:00:49 +00:00
*
*
*/
function fetch_client()
2003-09-11 16:00:49 +00:00
{
$client = new Societe($this->db);
2006-05-09 19:10:23 +00:00
$client->fetch($this->socidp);
2003-09-11 16:00:49 +00:00
$this->client = $client;
}
/**
*
*
*/
function fetch_adresse_livraison($id)
{
$idadresse = $id;
$adresse = new Societe($this->db);
$adresse->fetch_adresse_livraison($idadresse);
$this->adresse = $adresse;
}
/**
*
*
*/
function fetch_contact_propal($id)
{
$idcontact = $id;
$contact = new Contact($this->db);
$contact->fetch($idcontact);
$this->contact = $contact;
}
2005-09-04 13:10:43 +00:00
/**
* \brief Supprime une ligne de detail
* \param idligne Id de la ligne detail <EFBFBD> supprimer
* \return int >0 si ok, <0 si ko
*/
function delete_product($idligne)
2003-09-11 16:00:49 +00:00
{
2005-09-04 13:10:43 +00:00
if ($this->statut == 0)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."propaldet WHERE rowid = ".$idligne;
2005-09-04 13:10:43 +00:00
if ($this->db->query($sql) )
{
$this->update_price();
2005-09-04 13:10:43 +00:00
return 1;
}
else
{
return -1;
}
}
else
{
return -2;
}
2003-09-11 16:00:49 +00:00
}
/**
* \brief Cr<EFBFBD>e une propal
* \return int <0 si ko, >=0 si ok
*/
function create()
2003-09-11 16:00:49 +00:00
{
global $langs,$conf;
// D<>finition param<61>tres
$this->fin_validite = $this->datep + ($this->duree_validite * 24 * 3600);
$this->db->begin();
2003-09-11 16:00:49 +00:00
// Insertion dans la base
$sql = "INSERT INTO ".MAIN_DB_PREFIX."propal (fk_soc, fk_soc_contact, price, remise, remise_percent, remise_absolue,";
2006-04-24 16:58:30 +00:00
$sql.= " tva, total, datep, datec, ref, fk_user_author, note, note_public, model_pdf, fin_validite, fk_cond_reglement, fk_mode_reglement, date_livraison) ";
$sql.= " VALUES ($this->socidp, $this->contactid, 0, $this->remise, $this->remise_percent, $this->remise_absolue,";
$sql.= " 0,0,".$this->db->idate($this->datep).", now(), '$this->ref', $this->author,";
$sql.= "'".addslashes($this->note)."',";
$sql.= "'".addslashes($this->note_public)."',";
$sql.= "'$this->modelpdf',".$this->db->idate($this->fin_validite).",";
$sql.= " $this->cond_reglement_id, $this->mode_reglement_id,";
$sql.= " ".$this->db->idate($this->date_livraison).")";
$resql=$this->db->query($sql);
if ($resql)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."propal");
if ($this->id)
{
/*
* Insertion du detail des produits dans la base
*/
for ($i = 0 ; $i < sizeof($this->products) ; $i++)
{
$prod = new Product($this->db, $this->products[$i]);
$result=$prod->fetch($this->products[$i]);
2005-09-04 13:10:43 +00:00
$result=$this->insert_product($this->products[$i],
$this->products_qty[$i],
$this->products_remise_percent[$i]);
}
2005-09-04 13:10:43 +00:00
// Affectation au projet
if ($this->projetidp)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_projet=$this->projetidp WHERE ref='$this->ref'";
$result=$this->db->query($sql);
}
2006-04-24 16:58:30 +00:00
// Affectation de l'adresse de livraison
if ($this->adresse_livraison_id)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_adresse_livraison=$this->adresse_livraison_id WHERE ref='$this->ref'";
$result=$this->db->query($sql);
}
2005-09-04 13:10:43 +00:00
$resql=$this->update_price();
if ($resql)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('PROPAL_CREATE',$this,$user,$langs,$conf);
// Fin appel triggers
$this->db->commit();
return $this->id;
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Propal::Create -2 ".$this->error);
$this->db->rollback();
return -2;
}
}
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Propal::Create -1 ".$this->error);
$this->db->rollback();
return -1;
}
return $this->id;
2003-09-11 16:00:49 +00:00
}
2005-09-04 13:10:43 +00:00
/**
* \brief Mets <EFBFBD> jour le prix total de la proposition
* \return int <0 si ko, >0 si ok
*/
function update_price()
2003-09-11 16:00:49 +00:00
{
2005-09-04 13:10:43 +00:00
include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php";
2005-09-04 13:10:43 +00:00
/*
* Liste des produits a ajouter
*/
$sql = "SELECT price, qty, tva_tx FROM ".MAIN_DB_PREFIX."propaldet";
$sql.= " WHERE fk_propal = ".$this->id;
2005-09-04 13:10:43 +00:00
if ( $this->db->query($sql) )
{
2005-09-04 13:10:43 +00:00
$num = $this->db->num_rows();
$i = 0;
while ($i < $num)
{
2005-09-04 13:10:43 +00:00
$obj = $this->db->fetch_object();
$products[$i][0] = $obj->price;
$products[$i][1] = $obj->qty;
$products[$i][2] = $obj->tva_tx;
$i++;
}
2005-09-04 13:10:43 +00:00
}
$calculs = calcul_price($products, $this->remise_percent, $this->remise_absolue);
$this->total_remise = $calculs[3];
$this->amount_ht = $calculs[4];
2005-09-04 13:10:43 +00:00
$this->total_ht = $calculs[0];
$this->total_tva = $calculs[1];
$this->total_ttc = $calculs[2];
$tvas = $calculs[5];
2005-09-04 13:10:43 +00:00
// Met a jour en base
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET";
$sql .= " price='". price2num($this->total_ht)."'";
$sql .= ", tva='". price2num($this->total_tva)."'";
$sql .= ", total='". price2num($this->total_ttc)."'";
$sql .= ", remise='".price2num($this->total_remise)."'";
$sql .=" WHERE rowid = ".$this->id;
2005-09-04 13:10:43 +00:00
if ( $this->db->query($sql) )
{
return 1;
}
else
{
2005-09-04 13:10:43 +00:00
$this->error=$this->db->error();
return -1;
}
2005-09-04 13:10:43 +00:00
}
/**
* \brief Stocke un num<EFBFBD>ro de rang pour toutes les lignes de
* detail d'une propale qui n'en ont pas.
*/
function line_order()
{
$sql = 'SELECT count(rowid) FROM '.MAIN_DB_PREFIX.'propaldet';
$sql .= ' WHERE fk_propal='.$this->id;
$sql .= ' AND rang = 0';
$resql = $this->db->query($sql);
if ($resql)
{
$row = $this->db->fetch_row($resql);
$nl = $row[0];
}
if ($nl > 0)
{
$sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'propaldet';
$sql .= ' WHERE fk_propal='.$this->id;
$sql .= ' ORDER BY rang ASC, rowid ASC';
$resql = $this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$row = $this->db->fetch_row($resql);
$li[$i] = $row[0];
$i++;
}
}
for ($i = 0 ; $i < sizeof($li) ; $i++)
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propaldet SET rang = '.($i+1);
$sql .= ' WHERE rowid = '.$li[$i];
if (!$this->db->query($sql) )
{
dolibarr_syslog($this->db->error());
}
}
}
}
function line_up($rowid)
{
$this->line_order();
/* Lecture du rang de la ligne */
$sql = 'SELECT rang FROM '.MAIN_DB_PREFIX.'propaldet';
$sql .= ' WHERE rowid ='.$rowid;
$resql = $this->db->query($sql);
if ($resql)
{
$row = $this->db->fetch_row($resql);
$rang = $row[0];
}
if ($rang > 1 )
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propaldet SET rang = '.$rang ;
$sql .= ' WHERE fk_propal = '.$this->id;
$sql .= ' AND rang = '.($rang - 1);
if ($this->db->query($sql) )
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propaldet SET rang = '.($rang - 1);
$sql .= ' WHERE rowid = '.$rowid;
if (! $this->db->query($sql) )
{
dolibarr_print_error($this->db);
}
}
else
{
dolibarr_print_error($this->db);
}
}
}
function line_down($rowid)
{
$this->line_order();
/* Lecture du rang de la ligne */
$sql = 'SELECT rang FROM '.MAIN_DB_PREFIX.'propaldet';
$sql .= ' WHERE rowid ='.$rowid;
$resql = $this->db->query($sql);
if ($resql)
{
$row = $this->db->fetch_row($resql);
$rang = $row[0];
}
/* Lecture du rang max de la propale */
$sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.'propaldet';
$sql .= ' WHERE fk_propal ='.$this->id;
$resql = $this->db->query($sql);
if ($resql)
{
$row = $this->db->fetch_row($resql);
$max = $row[0];
}
if ($rang < $max )
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propaldet SET rang = '.$rang;
$sql .= ' WHERE fk_propal = '.$this->id;
$sql .= ' AND rang = '.($rang+1);
if ($this->db->query($sql) )
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propaldet SET rang = '.($rang+1);
$sql .= ' WHERE rowid = '.$rowid;
if (! $this->db->query($sql) )
{
dolibarr_print_error($this->db);
}
}
else
{
dolibarr_print_error($this->db);
}
}
}
2005-09-04 13:10:43 +00:00
/**
* \brief Recup<EFBFBD>re de la base les caract<EFBFBD>ristiques d'une propale
* \param rowid id de la propal <EFBFBD> r<EFBFBD>cup<EFBFBD>rer
*/
function fetch($rowid)
{
$sql = "SELECT ref,total,price,remise,remise_percent,remise_absolue,tva,fk_soc,fk_soc_contact";
$sql.= ", ".$this->db->pdate("datep")." as dp";
$sql.= ", ".$this->db->pdate("fin_validite")." as dfv";
$sql.= ", ".$this->db->pdate("date_livraison")." as date_livraison";
$sql.= ", model_pdf";
$sql.= ", note, note_public";
$sql.= ", fk_projet, fk_statut, fk_user_author";
$sql.= ", fk_cond_reglement, fk_mode_reglement, fk_adresse_livraison";
$sql.= ", c.label as statut_label";
$sql.= " FROM ".MAIN_DB_PREFIX."propal,";
$sql.= " ".MAIN_DB_PREFIX."c_propalst as c";
$sql.= " WHERE fk_statut = c.id";
$sql.= " AND rowid='".$rowid."';";
2005-09-04 13:10:43 +00:00
$resql=$this->db->query($sql);
2005-09-04 13:10:43 +00:00
if ($resql)
{
if ($this->db->num_rows($resql))
{
$obj = $this->db->fetch_object($resql);
$this->id = $rowid;
$this->datep = $obj->dp;
$this->fin_validite = $obj->dfv;
$this->date = $obj->dp;
$this->ref = $obj->ref;
$this->price = $obj->price;
$this->remise = $obj->remise;
$this->remise_percent = $obj->remise_percent;
$this->remise_absolue = $obj->remise_absolue;
$this->total = $obj->total;
$this->total_ht = $obj->price;
$this->total_tva = $obj->tva;
$this->total_ttc = $obj->total;
$this->socidp = $obj->fk_soc;
$this->soc_id = $obj->fk_soc;
$this->projetidp = $obj->fk_projet;
$this->contactid = $obj->fk_soc_contact;
$this->modelpdf = $obj->model_pdf;
$this->note = $obj->note;
$this->note_public = $obj->note_public;
$this->statut = $obj->fk_statut;
$this->statut_libelle = $obj->statut_label;
$this->cond_reglement_id = $obj->fk_cond_reglement;
$this->mode_reglement_id = $obj->fk_mode_reglement;
$this->date_livraison = $obj->date_livraison;
$this->adresse_livraison_id = $obj->fk_adresse_livraison;
2005-09-04 13:10:43 +00:00
$this->user_author_id = $obj->fk_user_author;
if ($this->cond_reglement_id)
{
$sql = "SELECT rowid, libelle, code";
$sql.= " FROM ".MAIN_DB_PREFIX."cond_reglement";
$sql.= " WHERE rowid = ".$this->cond_reglement_id;
$resqlcond = $this->db->query($sql);
if ($resqlcond)
{
2006-02-28 18:10:27 +00:00
$objc = $this->db->fetch_object($resqlcond);
$this->cond_reglement = $objc->libelle;
$this->cond_reglement_code = $objc->code;
}
}
if ($this->user_author_id)
{
$sql = "SELECT name, firstname";
$sql.= " FROM ".MAIN_DB_PREFIX."user";
$sql.= " WHERE rowid = ".$this->user_author_id;
$resqluser = $this->db->query($sql);
if ($resqluser)
{
2006-04-03 09:58:10 +00:00
$obju = $this->db->fetch_object($resqluser);
$this->user_author_name = $obju->name;
$this->user_author_firstname = $obju->firstname;
}
}
2005-09-04 13:10:43 +00:00
if ($obj->fk_statut == 0)
{
$this->brouillon = 1;
}
2005-09-04 13:10:43 +00:00
$this->lignes = array();
$this->db->free($resql);
2005-09-04 13:10:43 +00:00
$this->ref_url = '<a href="'.DOL_URL_ROOT.'/comm/propal.php?propalid='.$this->id.'">'.$this->ref.'</a>';
2005-09-04 13:10:43 +00:00
/*
* Lignes propales li<EFBFBD>es <EFBFBD> un produit ou non
*/
$sql = "SELECT d.description, d.price, d.tva_tx, d.qty, d.remise_percent, d.subprice, d.fk_product,";
$sql.= " p.label, p.description as product_desc, p.ref";
2006-06-05 17:52:25 +00:00
$sql.= " FROM ".MAIN_DB_PREFIX."propaldet as d";
2006-06-05 17:48:18 +00:00
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON d.fk_product = p.rowid";
$sql.= " WHERE d.fk_propal = ".$this->id;
$sql.= " ORDER by d.rang";
2005-09-04 13:10:43 +00:00
$result = $this->db->query($sql);
if ($result)
{
$num = $this->db->num_rows($result);
$i = 0;
2005-09-04 13:10:43 +00:00
while ($i < $num)
{
$objp = $this->db->fetch_object($result);
2005-09-04 13:10:43 +00:00
$ligne = new PropaleLigne();
2006-01-08 18:16:30 +00:00
$ligne->desc = $objp->description; // Description ligne
2005-09-04 13:10:43 +00:00
$ligne->qty = $objp->qty;
$ligne->tva_tx = $objp->tva_tx;
$ligne->subprice = $objp->subprice;
$ligne->remise_percent = $objp->remise_percent;
$ligne->price = $objp->price;
2006-06-05 17:48:18 +00:00
$ligne->product_id = $objp->fk_product;
$ligne->coef = $objp->coef;
2006-01-08 18:16:30 +00:00
$ligne->libelle = $objp->label; // Label produit
$ligne->product_desc = $objp->product_desc; // Description produit
$ligne->ref = $objp->ref;
2005-09-04 13:10:43 +00:00
$this->lignes[$i] = $ligne;
//dolibarr_syslog("1 ".$ligne->desc);
//print "xx $i ".$this->lignes[$i]->product_desc;
2005-09-04 13:10:43 +00:00
$i++;
}
$this->db->free($result);
}
else
{
dolibarr_syslog("Propal::Fetch Erreur lecture des produits");
return -1;
}
2006-01-08 18:16:30 +00:00
2005-09-04 13:10:43 +00:00
}
return 1;
}
else
{
dolibarr_syslog("Propal::Fetch Erreur lecture de la propale $rowid");
return -1;
}
}
/**
* \brief Passe au statut valider une propale
* \param user Objet utilisateur qui valide
* \return int <0 si ko, >=0 si ok
*/
function valid($user)
2003-09-11 16:00:49 +00:00
{
global $conf,$langs;
if ($user->rights->propale->valider)
{
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."propal";
$sql.= " SET fk_statut = 1, date_valid=now(), fk_user_valid=".$user->id;
$sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0";
if ($this->db->query($sql))
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('PROPAL_VALIDATE',$this,$user,$langs,$conf);
// Fin appel triggers
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -1;
}
}
}
/**
* \brief D<EFBFBD>finit la date de fin de validit<EFBFBD>
* \param user Objet utilisateur qui modifie
* \param date_fin_validite Date fin
* \return int <0 si ko, >0 si ok
*/
function set_echeance($user, $date_fin_validite)
{
if ($user->rights->propale->creer)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fin_validite = ".$this->db->idate($date_fin_validite);
$sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0";
if ($this->db->query($sql) )
{
$this->fin_validite = $date_fin_validite;
return 1;
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Propal::set_echeance Erreur SQL");
return -1;
}
}
}
/**
2006-03-01 15:25:46 +00:00
* \brief D<EFBFBD>finit une date de livraison
* \param user Objet utilisateur qui modifie
* \param date_livraison date de livraison
* \return int <0 si ko, >0 si ok
2006-03-01 15:25:46 +00:00
*/
function set_date_livraison($user, $date_livraison)
{
if ($user->rights->propale->creer)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."propal ";
$sql.= " SET date_livraison = ".$this->db->idate($date_livraison);
2006-03-01 15:25:46 +00:00
$sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0";
if ($this->db->query($sql))
2006-03-01 15:25:46 +00:00
{
$this->date_livraison = $date_livraison;
return 1;
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Propal::set_date_livraison Erreur SQL");
return -1;
}
}
}
/**
* \brief D<EFBFBD>finit une adresse de livraison
* \param user Objet utilisateur qui modifie
* \param adresse_livraison Adresse de livraison
* \return int <0 si ko, >0 si ok
*/
function set_adresse_livraison($user, $adresse_livraison)
{
if ($user->rights->propale->creer)
{
2006-04-24 14:19:23 +00:00
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_adresse_livraison = '".$adresse_livraison."'";
$sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0";
if ($this->db->query($sql) )
{
$this->adresse_livraison_id = $adresse_livraison;
return 1;
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Propal::set_adresse_livraison Erreur SQL");
return -1;
}
}
}
/**
* \brief D<EFBFBD>finit une remise globale relative sur la proposition
* \param user Objet utilisateur qui modifie
* \param remise Montant remise
* \return int <0 si ko, >0 si ok
*/
function set_remise_percent($user, $remise)
{
$remise=trim($remise)?trim($remise):0;
if ($user->rights->propale->creer)
{
$remise = price2num($remise);
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET remise_percent = ".$remise;
$sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0";
if ($this->db->query($sql) )
{
$this->remise_percent = $remise;
$this->update_price();
return 1;
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Propal::set_remise_percent Error sql=$sql");
return -1;
}
}
}
/**
* \brief D<EFBFBD>finit une remise globale absolue sur la proposition
* \param user Objet utilisateur qui modifie
* \param remise Montant remise
* \return int <0 si ko, >0 si ok
*/
function set_remise_absolue($user, $remise)
{
$remise=trim($remise)?trim($remise):0;
if ($user->rights->propale->creer)
{
$remise = price2num($remise);
$sql = "UPDATE ".MAIN_DB_PREFIX."propal ";
$sql.= " SET remise_absolue = ".$remise;
$sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0";
if ($this->db->query($sql) )
{
$this->remise_absolue = $remise;
$this->update_price();
return 1;
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Propal::set_remise_absolue Error sql=$sql");
return -1;
}
}
}
/*
*
*
*
*/
function set_project($user, $project_id)
{
if ($user->rights->propale->creer)
{
//verif que le projet et la soci<63>t<EFBFBD> concordent
$sql = 'SELECT p.rowid, p.title FROM '.MAIN_DB_PREFIX.'projet as p WHERE p.fk_soc ='.$this->socidp.' AND p.rowid='.$project_id;
$sqlres = $this->db->query($sql);
if ($sqlres)
{
$numprojet = $this->db->num_rows($sqlres);
if ($numprojet > 0)
{
$this->projetidp=$project_id;
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propal SET fk_projet = '.$project_id;
$sql .= ' WHERE rowid = '.$this->id.' AND fk_statut = 0 ;';
$this->db->query($sql);
}
}
else
{
dolibarr_syslog("Propal::set_project Erreur SQL");
}
}
}
/*
*
*
*
*/
function set_contact($user, $contact_id)
{
if ($user->rights->propale->creer)
{
//verif que le contact et la soci<63>t<EFBFBD> concordent
$sql = 'SELECT p.idp FROM '.MAIN_DB_PREFIX.'socpeople as p WHERE p.fk_soc = '.$this->socidp.' AND p.idp='.$contact_id;
$sqlres = $this->db->query($sql);
if ($sqlres)
{
$numprojet = $this->db->num_rows($sqlres);
if ($numprojet > 0)
{
$this->projetidp=$project_id;
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propal SET fk_soc_contact = '.$contact_id;
$sql .= ' WHERE rowid = '.$this->id.' AND fk_statut = 0 ;';
$this->db->query($sql);
}
}
else
{
dolibarr_syslog("Propal::set_contact Erreur SQL");
}
}
}
/**
* \brief Positionne modele derniere generation
* \param user Objet use qui modifie
* \param modelpdf Nom du modele
*/
function set_pdf_model($user, $modelpdf)
2003-09-11 16:00:49 +00:00
{
if ($user->rights->propale->creer)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."propal";
$sql.= " SET model_pdf = '".$modelpdf."'";
$sql.= " WHERE rowid = ".$this->id." AND fk_statut = 0";
if ($this->db->query($sql))
{
$this->modelpdf=$modelpdf;
return 1;
}
else
{
dolibarr_print_error($this->db);
return 0;
}
}
2003-09-11 16:00:49 +00:00
}
/**
* \brief Cloture de la proposition commerciale
* \param user Utilisateur qui cloture
* \param statut Statut
* \param note Commentaire
* \return int <0 si ko, >0 si ok
*/
function cloture($user, $statut, $note)
2003-09-11 16:00:49 +00:00
{
$this->statut = $statut;
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."propal";
$sql.= " SET fk_statut = $statut, note = '".addslashes($note)."', date_cloture=now(), fk_user_cloture=".$user->id;
$sql.= " WHERE rowid = ".$this->id;
$resql=$this->db->query($sql);
if ($resql)
{
if ($statut == 2)
{
// Propale sign<67>e
include_once(DOL_DOCUMENT_ROOT."/commande/commande.class.php");
2003-11-19 14:52:09 +00:00
$result=$this->create_commande($user);
2003-11-19 14:52:09 +00:00
if ($result >= 0)
{
// Classe la soci<63>t<EFBFBD> rattach<63>e comme client
$soc=new Societe($this->db);
$soc->id = $this->socidp;
$result=$soc->set_as_client();
}
if ($result < 0)
{
$this->error=$this->db->error();
$this->db->rollback();
return -2;
}
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('PROPAL_CLOSE_SIGNED',$this,$user,$langs,$conf);
// Fin appel triggers
}
else
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('PROPAL_CLOSE_REFUSED',$this,$user,$langs,$conf);
// Fin appel triggers
}
$this->db->commit();
return 1;
}
else
{
$this->error=$this->db->error();
$this->db->rollback();
return -1;
}
}
/**
* \brief Classe la propale comme factur<EFBFBD>e
* \return int <0 si ko, >0 si ok
*/
function classer_facturee()
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propal SET fk_statut = 4';
$sql .= ' WHERE rowid = '.$this->id.' AND fk_statut > 0 ;';
if ($this->db->query($sql) )
{
return 1;
}
else
{
dolibarr_print_error($this->db);
}
}
/**
* \brief Cr<EFBFBD>e une commande <EFBFBD> partir de la proposition commerciale
* \param user Utilisateur
* \return int <0 si ko, >=0 si ok
*/
function create_commande($user)
2003-11-19 14:52:09 +00:00
{
global $conf;
if ($conf->commande->enabled)
{
if ($this->statut == 2)
{
// Propale sign<67>e
include_once(DOL_DOCUMENT_ROOT."/commande/commande.class.php");
$commande = new Commande($this->db);
$result=$commande->create_from_propale($user, $this->id);
return $result;
}
else return 0;
}
else return 0;
2003-11-19 14:52:09 +00:00
}
/**
*
*
*/
function reopen($userid)
2003-09-11 16:00:49 +00:00
{
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_statut = 0";
$sql .= " WHERE rowid = $this->id;";
if ($this->db->query($sql) )
{
return 1;
}
else
{
dolibarr_print_error($this->db);
}
2003-09-11 16:00:49 +00:00
}
2003-11-19 14:52:09 +00:00
/**
2005-03-15 16:01:11 +00:00
* \brief Renvoi la liste des propal (<EFBFBD>ventuellement filtr<EFBFBD>e sur un user) dans un tableau
* \param brouillon 0=non brouillon, 1=brouillon
* \param user Objet user de filtre
* \return int -1 si erreur, tableau r<EFBFBD>sultat si ok
2003-09-11 16:00:49 +00:00
*/
2005-03-15 16:01:11 +00:00
function liste_array ($brouillon=0, $user='')
2003-09-11 16:00:49 +00:00
{
2005-03-15 16:01:11 +00:00
$ga = array();
2005-03-15 16:01:11 +00:00
$sql = "SELECT rowid, ref FROM ".MAIN_DB_PREFIX."propal";
2005-03-15 16:01:11 +00:00
if ($brouillon)
{
$sql .= " WHERE fk_statut = 0";
if ($user)
{
$sql .= " AND fk_user_author".$user;
}
}
else
{
if ($user)
{
$sql .= " WHERE fk_user_author".$user;
}
}
2005-03-15 16:01:11 +00:00
$sql .= " ORDER BY datep DESC";
$result=$this->db->query($sql);
if ($result)
{
$nump = $this->db->num_rows($result);
2005-03-15 16:01:11 +00:00
if ($nump)
{
$i = 0;
while ($i < $nump)
{
$obj = $this->db->fetch_object($result);
2005-03-15 16:01:11 +00:00
$ga[$obj->rowid] = $obj->ref;
$i++;
}
}
return $ga;
}
else
{
return -1;
}
2003-09-11 16:00:49 +00:00
}
/**
2005-07-10 19:31:44 +00:00
* \brief Renvoie un tableau contenant les num<EFBFBD>ros de commandes associ<EFBFBD>es
* \remarks Fonction plus light que associated_orders
* \sa associated_orders
*/
function commande_liste_array ()
2003-11-19 14:52:09 +00:00
{
$ga = array();
$sql = "SELECT fk_commande FROM ".MAIN_DB_PREFIX."co_pr";
$sql .= " WHERE fk_propale = " . $this->id;
if ($this->db->query($sql) )
{
$nump = $this->db->num_rows();
if ($nump)
{
$i = 0;
while ($i < $nump)
{
$obj = $this->db->fetch_object();
$ga[$i] = $obj->fk_commande;
$i++;
}
}
return $ga;
}
else
{
dolibarr_print_error($this->db);
}
2003-11-19 14:52:09 +00:00
}
2005-12-09 23:31:03 +00:00
/**
* \brief Renvoie un tableau contenant les commandes associ<EFBFBD>es
* \remarks Fonction plus lourde que commande_liste_array
* \sa commande_liste_array
*/
function associated_orders()
{
2005-12-09 23:31:03 +00:00
$ga = array();
2005-12-09 23:31:03 +00:00
$sql = "SELECT fk_commande FROM ".MAIN_DB_PREFIX."co_pr";
$sql.= " WHERE fk_propale = " . $this->id;
if ($this->db->query($sql) )
{
$nump = $this->db->num_rows();
2005-12-09 23:31:03 +00:00
if ($nump)
{
$i = 0;
while ($i < $nump)
{
$obj = $this->db->fetch_object();
$order=new Commande($this->db);
2005-12-09 23:31:03 +00:00
if ($obj->fk_commande)
{
$order->fetch($obj->fk_commande);
$ga[$i] = $order;
}
$i++;
}
}
return $ga;
}
else
{
print $this->db->error();
}
}
/**
2006-05-17 18:55:46 +00:00
* \brief Renvoie un tableau contenant les num<EFBFBD>ros de factures associ<EFBFBD>es
* \return array Tableau des id de factures
*/
2006-05-17 18:55:46 +00:00
function getFactureListeArray ()
{
return $this->FactureListeArray($this->id);
}
/**
* \brief Renvoie un tableau contenant les num<EFBFBD>ros de factures associ<EFBFBD>es
* \param id Id propal
* \return array Tableau des id de factures
*/
function FactureListeArray($id)
{
$ga = array();
$sql = "SELECT fk_facture FROM ".MAIN_DB_PREFIX."fa_pr as fp";
2006-05-17 18:55:46 +00:00
$sql .= " WHERE fk_propal = " . $id;
if ($this->db->query($sql))
{
$nump = $this->db->num_rows();
if ($nump)
{
$i = 0;
while ($i < $nump)
{
$obj = $this->db->fetch_object();
$ga[$i] = $obj->fk_facture;
$i++;
}
}
return $ga;
}
else
{
2006-05-17 18:55:46 +00:00
return -1;
}
}
/**
* \brief Efface propal
* \param user Objet du user qui efface
2003-09-12 12:59:24 +00:00
*/
function delete($user)
2003-09-12 12:59:24 +00:00
{
global $conf;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."propaldet WHERE fk_propal = ".$this->id;
if ( $this->db->query($sql) )
2003-09-12 12:59:24 +00:00
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."propal WHERE rowid = ".$this->id;
if ( $this->db->query($sql) )
2003-09-12 12:59:24 +00:00
{
// On efface le r<>pertoire du pdf
$propalref = sanitize_string($this->ref);
if ($conf->propal->dir_output)
{
$dir = $conf->propal->dir_output . "/" . $propalref ;
$file = $conf->propal->dir_output . "/" . $propalref . "/" . $propalref . ".pdf";
if (file_exists($file))
{
propale_delete_preview($this->db, $this->id, $this->ref);
if (!dol_delete_file($file))
{
$this->error=$langs->trans("ErrorCanNotDeleteFile",$file);
return 0;
}
}
if (file_exists($dir))
{
if (!dol_delete_dir($dir))
{
$this->error=$langs->trans("ErrorCanNotDeleteDir",$dir);
return 0;
}
}
}
dolibarr_syslog("Suppression de la proposition $this->id par $user->fullname ($user->id)");
2003-09-12 12:59:24 +00:00
return 1;
}
else
{
return -2;
}
}
else
{
return -1;
}
}
/**
* \brief Mets <EFBFBD> jour les commentaires priv<EFBFBD>s
* \param note Commentaire
* \return int <0 si ko, >0 si ok
*/
function update_note($note)
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propal';
$sql.= " SET note = '".addslashes($note)."'";
$sql.= " WHERE rowid =". $this->id;
dolibarr_syslog("Propal::update_note $sql");
if ($this->db->query($sql))
{
$this->note = $note;
return 1;
}
else
{
$this->error=$this->db->error();
return -1;
}
}
/**
* \brief Mets <EFBFBD> jour les commentaires publiques
* \param note_public Commentaire
* \return int <0 si ko, >0 si ok
*/
function update_note_public($note_public)
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propal';
$sql.= " SET note_public = '".addslashes($note_public)."'";
$sql.= " WHERE rowid =". $this->id;
dolibarr_syslog("Propal::update_note_public $sql");
if ($this->db->query($sql))
{
$this->note_public = $note_public;
return 1;
}
else
{
$this->error=$this->db->error();
return -1;
}
}
2006-02-15 17:54:04 +00:00
/**
* \brief Change les conditions de r<EFBFBD>glement de la facture
* \param cond_reglement_id Id de la nouvelle condition de r<EFBFBD>glement
* \return int >0 si ok, <0 si ko
*/
function cond_reglement($cond_reglement_id)
{
dolibarr_syslog('Propale::cond_reglement('.$cond_reglement_id.')');
if ($this->statut >= 0)
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propal';
$sql .= ' SET fk_cond_reglement = '.$cond_reglement_id;
$sql .= ' WHERE rowid='.$this->id;
if ( $this->db->query($sql) )
{
$this->cond_reglement_id = $cond_reglement_id;
return 1;
}
else
{
dolibarr_syslog('Propale::cond_reglement Erreur '.$sql.' - '.$this->db->error());
$this->error=$this->db->error();
return -1;
}
}
else
{
dolibarr_syslog('Propale::cond_reglement, etat propale incompatible');
$this->error='Etat propale incompatible '.$this->statut;
return -2;
}
}
/**
* \brief Change le mode de r<EFBFBD>glement
* \param mode Id du nouveau mode
* \return int >0 si ok, <0 si ko
*/
function mode_reglement($mode_reglement_id)
{
dolibarr_syslog('Propale::mode_reglement('.$mode_reglement_id.')');
if ($this->statut >= 0)
{
$sql = 'UPDATE '.MAIN_DB_PREFIX.'propal';
$sql .= ' SET fk_mode_reglement = '.$mode_reglement_id;
$sql .= ' WHERE rowid='.$this->id;
if ( $this->db->query($sql) )
{
$this->mode_reglement_id = $mode_reglement_id;
return 1;
}
else
{
dolibarr_syslog('Propale::mode_reglement Erreur '.$sql.' - '.$this->db->error());
$this->error=$this->db->error();
return -1;
}
}
else
{
dolibarr_syslog('Propale::mode_reglement, etat propale incompatible');
$this->error='Etat facture incompatible '.$this->statut;
return -2;
}
}
/**
* \brief Information sur l'objet propal
* \param id id de la propale
*/
function info($id)
{
$sql = "SELECT c.rowid, ";
$sql.= $this->db->pdate("datec")." as datec, ".$this->db->pdate("date_valid")." as datev, ".$this->db->pdate("date_cloture")." as dateo";
$sql.= ", fk_user_author, fk_user_valid, fk_user_cloture";
$sql.= " FROM ".MAIN_DB_PREFIX."propal as c";
$sql.= " WHERE c.rowid = $id";
if ($this->db->query($sql))
{
if ($this->db->num_rows())
{
$obj = $this->db->fetch_object();
$this->id = $obj->rowid;
$this->date_creation = $obj->datec;
$this->date_validation = $obj->datev;
$this->date_cloture = $obj->dateo;
$cuser = new User($this->db, $obj->fk_user_author);
$cuser->fetch();
$this->user_creation = $cuser;
if ($obj->fk_user_valid)
{
$vuser = new User($this->db, $obj->fk_user_valid);
$vuser->fetch();
$this->user_validation = $vuser;
}
if ($obj->fk_user_cloture)
{
$cluser = new User($this->db, $obj->fk_user_cloture);
$cluser->fetch();
$this->user_cloture = $cluser;
}
}
$this->db->free();
}
else
{
dolibarr_print_error($this->db);
}
}
/**
2006-03-25 12:49:55 +00:00
* \brief Retourne le libell<EFBFBD> du statut d'une propale (brouillon, valid<EFBFBD>e, ...)
* \param mode 0=libell<EFBFBD> long, 1=libell<EFBFBD> court, 2=Picto + Libell<EFBFBD> court, 3=Picto, 4=Picto + Libell<EFBFBD> long, 5=Libell<EFBFBD> court + Picto
* \return string Libell<EFBFBD>
*/
function getLibStatut($mode=0)
{
return $this->LibStatut($this->statut,$mode);
}
/**
2006-03-25 12:49:55 +00:00
* \brief Renvoi le libell<EFBFBD> d'un statut donn<EFBFBD>
* \param statut id statut
* \param mode 0=libell<EFBFBD> long, 1=libell<EFBFBD> court, 2=Picto + Libell<EFBFBD> court, 3=Picto, 4=Picto + Libell<EFBFBD> long, 5=Libell<EFBFBD> court + Picto
* \return string Libell<EFBFBD>
*/
function LibStatut($statut,$mode=1)
{
global $langs;
$langs->load("propal");
2006-03-25 12:49:55 +00:00
if ($mode == 0)
{
return $this->labelstatut[$statut];
}
if ($mode == 1)
{
return $this->labelstatut_short[$statut];
}
if ($mode == 2)
{
if ($statut==0) return img_picto($langs->trans('PropalStatusDraftShort'),'statut0').' '.$this->labelstatut_short[$statut];
if ($statut==1) return img_picto($langs->trans('PropalStatusOpenedShort'),'statut1').' '.$this->labelstatut_short[$statut];
if ($statut==2) return img_picto($langs->trans('PropalStatusSignedShort'),'statut3').' '.$this->labelstatut_short[$statut];
if ($statut==3) return img_picto($langs->trans('PropalStatusNotSignedShort'),'statut5').' '.$this->labelstatut_short[$statut];
if ($statut==4) return img_picto($langs->trans('PropalStatusBilledShort'),'statut6').' '.$this->labelstatut_short[$statut];
}
if ($mode == 3)
{
if ($statut==0) return img_picto($langs->trans('PropalStatusDraftShort'),'statut0');
if ($statut==1) return img_picto($langs->trans('PropalStatusOpenedShort'),'statut1');
if ($statut==2) return img_picto($langs->trans('PropalStatusSignedShort'),'statut3');
if ($statut==3) return img_picto($langs->trans('PropalStatusNotSignedShort'),'statut5');
if ($statut==4) return img_picto($langs->trans('PropalStatusBilledShort'),'statut6');
}
if ($mode == 4)
{
if ($statut==0) return img_picto($langs->trans('PropalStatusDraft'),'statut0').' '.$this->labelstatut[$statut];
if ($statut==1) return img_picto($langs->trans('PropalStatusOpened'),'statut1').' '.$this->labelstatut[$statut];
if ($statut==2) return img_picto($langs->trans('PropalStatusSigned'),'statut3').' '.$this->labelstatut[$statut];
if ($statut==3) return img_picto($langs->trans('PropalStatusNotSigned'),'statut5').' '.$this->labelstatut[$statut];
if ($statut==4) return img_picto($langs->trans('PropalStatusBilled'),'statut6').' '.$this->labelstatut[$statut];
}
2006-03-25 12:49:55 +00:00
if ($mode == 5)
{
if ($statut==0) return $this->labelstatut_short[$statut].' '.img_picto($langs->trans('PropalStatusDraftShort'),'statut0');
if ($statut==1) return $this->labelstatut_short[$statut].' '.img_picto($langs->trans('PropalStatusOpenedShort'),'statut1');
if ($statut==2) return $this->labelstatut_short[$statut].' '.img_picto($langs->trans('PropalStatusSignedShort'),'statut3');
if ($statut==3) return $this->labelstatut_short[$statut].' '.img_picto($langs->trans('PropalStatusNotSignedShort'),'statut5');
if ($statut==4) return $this->labelstatut_short[$statut].' '.img_picto($langs->trans('PropalStatusBilledShort'),'statut6');
}
}
/**
* \brief Charge indicateurs this->nbtodo et this->nbtodolate de tableau de bord
* \param user Objet user
* \param mode "opened" pour propal <EFBFBD> fermer, "signed" pour propale <EFBFBD> facturer
* \return int <0 si ko, >0 si ok
*/
function load_board($user,$mode)
{
global $conf, $user;
$this->nbtodo=$this->nbtodolate=0;
2006-05-17 18:55:46 +00:00
$sql ="SELECT p.rowid, p.ref, ".$this->db->pdate("p.datec")." as datec,".$this->db->pdate("p.fin_validite")." as datefin";
if (!$user->rights->commercial->client->voir && !$user->societe_id) $sql .= ", sc.fk_soc, sc.fk_user";
$sql.=" FROM ".MAIN_DB_PREFIX."propal as p";
if (!$user->rights->commercial->client->voir && !$user->societe_id) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
if ($mode == 'opened') $sql.=" WHERE p.fk_statut = 1";
if ($mode == 'signed') $sql.=" WHERE p.fk_statut = 2";
if ($user->societe_id) $sql.=" AND p.fk_soc = ".$user->societe_id;
if (!$user->rights->commercial->client->voir && !$user->societe_id) $sql .= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
$resql=$this->db->query($sql);
if ($resql)
{
while ($obj=$this->db->fetch_object($resql))
{
$this->nbtodo++;
if ($obj->datefin < (time() - $conf->propal->cloture->warning_delay))
2006-05-17 18:55:46 +00:00
{
if ($mode == 'opened') $this->nbtodolate++;
if ($mode == 'signed') $this->nbtodolate++;
// \todo Definir r<>gle des propales <20> facturer en retard
// if ($mode == 'signed' && ! sizeof($this->FactureListeArray($obj->rowid))) $this->nbtodolate++;
}
}
return 1;
}
else
{
$this->error=$this->db->error();
return -1;
}
}
/**
* \brief Charge l'objet propal en m<>moire avec propal dont l'id est pass<EFBFBD> en argument
* \param $src_propal_id Id de la propal source
* \return int >0 si ok, <0 si ko
* \see fetch
*/
function load_from($src_propal_id)
{
$src_propal = new Propal($this->db);
if($src_propal->fetch($src_propal_id) < 0)
{
dolibarr_syslog("Propal::load_from: Erreur de lecture de la propal source.");
return -1;
}
$this->datep = $src_propal->datep;
$this->fin_validite = $src_propal->fin_validite;
$this->date = $src_propal->date;
$this->ref = $src_propal->ref;
$this->price = $src_propal->price;
$this->remise = $src_propal->remise;
$this->remise_percent = $src_propal->remise_percent;
$this->remise_absolue = $src_propal->remise_absolue;
$this->total = $src_propal->total;
$this->total_ht = $src_propal->total_ht;
$this->total_tva = $src_propal->total_tva;
$this->total_ttc = $src_propal->total_ttc;
$this->socidp = $src_propal->socidp;
$this->soc_id = $src_propal->soc_id;
$this->projetidp = $src_propal->projetidp;
$this->contactid = $src_propal->contactid;
$this->modelpdf = $src_propal->modelpdf;
$this->note = $src_propal->note;
$this->note_public = $src_propal->note_public;
$this->statut = $src_propal->statut;
$this->statut_libelle = $src_propal->statut_libelle;
$this->cond_reglement_id = $src_propal->cond_reglement_id;
$this->mode_reglement_id = $src_propal->mode_reglement_id;
$this->date_livraison = $src_propal->date_livraison;
$this->adresse_livraison_id = $src_propal->adresse_livraison_id;
$this->user_author_id = $src_propal->user_author_id;
$this->cond_reglement = $src_propal->cond_reglement;
$this->cond_reglement_code=$src_propal->cond_reglement_code;
$this->ref_url = $src_propal->ref_url;
$this->lignes = $src_propal->lignes;
return 1;
}
/**
* \brief Insert en base un objet propal compl<EFBFBD>tement d<EFBFBD>finie par ses donn<EFBFBD>es membres (resultant d'une copie par exemple).
* \return int l'id du nouvel objet propal en base si ok, <0 si ko
* \see create
*/
function create_from()
{
$this->db->begin();
$this->fin_validite = $this->datep + ($this->duree_validite * 24 * 3600);
// Insertion dans la base
$sql = "INSERT INTO ".MAIN_DB_PREFIX."propal (fk_soc, fk_soc_contact, price, remise, remise_percent, remise_absolue,";
2006-04-24 16:58:30 +00:00
$sql.= " tva, total, datep, datec, ref, fk_user_author, note, note_public, model_pdf, fin_validite, fk_cond_reglement, fk_mode_reglement, date_livraison, fk_adresse_livraison) ";
$sql.= " VALUES ('$this->socidp', '$this->contactid', '0', '$this->remise', '$this->remise_percent', '$this->remise_absolue',";
$sql.= " '0','0','".$this->db->idate($this->datep)."', now(), '$this->ref', '$this->author',";
$sql.= "'".addslashes($this->note)."',";
$sql.= "'".addslashes($this->note_public)."',";
$sql.= "'$this->modelpdf','".$this->db->idate($this->fin_validite)."',";
$sql.= " '$this->cond_reglement_id', '$this->mode_reglement_id', '".$this->db->idate($this->date_livraison)."', '$this->adresse_livraison_id')";
$resql=$this->db->query($sql);
if ($resql)
{
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."propal");
if ($this->id)
{
/*
* Insertion du detail des produits dans la base
*/
foreach($this->lignes as $ligne)
{
if($ligne->product_id != 0)
{
$result=$this->insert_product($ligne->product_id,
$ligne->qty,
$ligne->remise_percent, $ligne->product_desc);
}
else
{
$this->insert_product_generic($ligne->desc, $ligne->price,
$ligne->qty, $ligne->tva_tx, $ligne->remise_percent);
}
}
// Affectation au projet
if ($this->projetidp)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."propal SET fk_projet=$this->projetidp WHERE ref='$this->ref'";
$result=$this->db->query($sql);
}
$resql=$this->update_price();
if ($resql)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('PROPAL_CREATE',$this,$user,$langs,$conf);
// Fin appel triggers
$this->db->commit();
return $this->id;
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Propal::Create_from -2 ".$this->error);
$this->db->rollback();
return -2;
}
}
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Propal::Create_from -1 ".$this->error);
$this->db->rollback();
return -1;
}
return $this->id;
}
/**
* \brief Initialise la propale avec valeurs fictives al<EFBFBD>atoire
* Sert <EFBFBD> g<EFBFBD>n<EFBFBD>rer une facture pour l'aperu des mod<EFBFBD>les ou demo
*/
function initAsSpecimen()
{
global $user,$langs;
// Charge tableau des id de soci<63>t<EFBFBD> socids
$socids = array();
$sql = "SELECT idp FROM ".MAIN_DB_PREFIX."societe WHERE client=1 LIMIT 10";
$resql = $this->db->query($sql);
if ($resql)
{
$num_socs = $this->db->num_rows($resql);
$i = 0;
while ($i < $num_socs)
{
$i++;
$row = $this->db->fetch_row($resql);
$socids[$i] = $row[0];
}
}
// Charge tableau des produits prodids
$prodids = array();
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product WHERE envente=1";
$resql = $this->db->query($sql);
if ($resql)
{
$num_prods = $this->db->num_rows($resql);
$i = 0;
while ($i < $num_prods)
{
$i++;
$row = $this->db->fetch_row($resql);
$prodids[$i] = $row[0];
}
}
// Initialise param<61>tres
$this->id=0;
$this->ref = 'SPECIMEN';
$this->specimen=1;
$socid = rand(1, $num_socs);
$this->socidp = $socids[$socid];
$this->date = time();
$this->fin_validite = $this->date+3600*24*30;
$this->cond_reglement_code = 'RECEP';
$this->mode_reglement_code = 'CHQ';
$this->note_public='SPECIMEN';
$nbp = rand(1, 9);
$xnbp = 0;
while ($xnbp < $nbp)
{
$ligne=new PropaleLigne($this->db);
$ligne->desc=$langs->trans("Description")." ".$xnbp;
$ligne->qty=1;
$ligne->subprice=100;
$ligne->price=100;
$ligne->tva_taux=19.6;
$prodid = rand(1, $num_prods);
$ligne->produit_id=$prodids[$prodid];
$this->lignes[$xnbp]=$ligne;
$xnbp++;
}
$this->amount_ht = $xnbp*100;
$this->total_ht = $xnbp*100;
$this->total_tva = $xnbp*19.6;
$this->total_ttc = $xnbp*119.6;
}
}
/**
\class PropalLigne
\brief Classe permettant la gestion des lignes de propales
*/
2003-09-11 16:00:49 +00:00
class PropaleLigne
2003-09-11 16:00:49 +00:00
{
2006-01-08 18:16:30 +00:00
// From llx_propaldet
var $qty;
var $tva_tx;
var $subprice;
var $remise_percent;
var $price;
var $product_id;
var $desc; // Description ligne
// From llx_product
var $libelle; // Label produit
var $product_desc; // Description produit
var $ref;
function PropaleLigne()
{
}
2003-09-11 16:00:49 +00:00
}
2003-09-11 16:00:49 +00:00
?>