diff --git a/htdocs/facture.class.php b/htdocs/facture.class.php index 71c37974cc1..de40407096f 100644 --- a/htdocs/facture.class.php +++ b/htdocs/facture.class.php @@ -190,16 +190,29 @@ class Facture extends CommonObject $totalht = ($amount - $remise); - $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture ('; - $sql.= ' facnumber, type, fk_soc, datec, amount, remise_absolue, remise_percent,'; - $sql.= ' datef,'; - $sql.= ' note,'; - $sql.= ' note_public,'; - $sql.= ' ref_client,'; - $sql.= ' fk_facture_source, fk_user_author, fk_projet,'; - $sql.= ' fk_cond_reglement, fk_mode_reglement, date_lim_reglement, model_pdf)'; - $sql.= ' VALUES ('; - $sql.= "'(PROV)', '".$this->type."', '".$socid."', ".$this->db->idate(gmmktime()).", '".$totalht."'"; + $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture ("; + $sql.= " facnumber"; + $sql.= ", entity"; + $sql.= ", type"; + $sql.= ", fk_soc"; + $sql.= ", datec"; + $sql.= ", amount"; + $sql.= ", remise_absolue"; + $sql.= ", remise_percent"; + $sql.= ", datef"; + $sql.= ", note"; + $sql.= ", note_public"; + $sql.= ", ref_client"; + $sql.= ", fk_facture_source, fk_user_author, fk_projet"; + $sql.= ", fk_cond_reglement, fk_mode_reglement, date_lim_reglement, model_pdf" + $sql.= ")"; + $sql.= " VALUES ("; + $sql.= "'(PROV)'"; + $sql.= ", ".$conf->entity; + $sql.= ", '".$this->type."'"; + $sql.= ", '".$socid."'"; + $sql.= ", ".$this->db->idate(gmmktime()); + $sql.= ", '".$totalht."'"; $sql.= ",".($this->remise_absolue>0?$this->remise_absolue:'NULL'); $sql.= ",".($this->remise_percent>0?$this->remise_percent:'NULL'); $sql.= ",".$this->db->idate($this->date); @@ -507,8 +520,9 @@ class Facture extends CommonObject $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'cond_reglement as c ON f.fk_cond_reglement = c.rowid'; $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON f.fk_mode_reglement = p.id'; $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'co_fa as cf ON cf.fk_facture = f.rowid'; - if ($ref) $sql.= " WHERE f.facnumber='".$ref."'"; - else $sql.= " WHERE f.rowid=".$rowid; + $sql.= ' WHERE f.entity = '.$conf->entity; + if ($ref) $sql.= " AND f.facnumber='".$ref."'"; + else $sql.= " AND f.rowid=".$rowid; dol_syslog("Facture::Fetch sql=".$sql, LOG_DEBUG); $result = $this->db->query($sql); @@ -1117,9 +1131,9 @@ class Facture extends CommonObject $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; $sql.= ' SET paye=0, fk_statut=1, close_code=null, close_note=null'; $sql.= ' WHERE rowid = '.$this->id; - $resql = $this->db->query($sql); dol_syslog("Facture::set_unpayed sql=".$sql); + $resql = $this->db->query($sql); if ($resql) { $this->use_webcal=($conf->global->PHPWEBCALENDAR_BILLSTATUS=='always'?1:0); @@ -1163,7 +1177,9 @@ class Facture extends CommonObject // On désaffecte de la facture les remises liées // car elles n'ont pas été utilisées vu que la facture est abandonnée. $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except'; - $sql.= ' SET fk_facture = NULL WHERE fk_facture = '.$this->id; + $sql.= ' SET fk_facture = NULL'; + $sql.= ' WHERE fk_facture = '.$this->id; + $resql=$this->db->query($sql); if ($resql) { @@ -1397,7 +1413,8 @@ class Facture extends CommonObject if ($this->statut != 0) { - $sql = "UPDATE ".MAIN_DB_PREFIX."facture SET fk_statut = 0"; + $sql = "UPDATE ".MAIN_DB_PREFIX."facture"; + $sql.= " SET fk_statut = 0"; $sql.= " WHERE rowid = ".$this->id; dol_syslog("Facture::set_draft sql=".$sql, LOG_DEBUG); @@ -1692,7 +1709,9 @@ class Facture extends CommonObject // Libere remise liee a ligne de facture $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except'; - $sql.= ' SET fk_facture_line = NULL where fk_facture_line = '.$rowid; + $sql.= ' SET fk_facture_line = NULL'; + $sql.= ' WHERE fk_facture_line = '.$rowid; + dol_syslog("Facture::Deleteline sql=".$sql); $result = $this->db->query($sql); if (! $result) @@ -1704,7 +1723,9 @@ class Facture extends CommonObject } // Efface ligne de facture - $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facturedet WHERE rowid = '.$rowid; + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facturedet'; + $sql.= ' WHERE rowid = '.$rowid; + dol_syslog("Facture::Deleteline sql=".$sql); $result = $this->db->query($sql); if (! $result) @@ -1745,7 +1766,8 @@ class Facture extends CommonObject $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; $sql.= ' SET remise_percent = '.$remise; - $sql.= ' WHERE rowid = '.$this->id.' AND fk_statut = 0 ;'; + $sql.= ' WHERE rowid = '.$this->id; + $sql.= ' AND fk_statut = 0 ;'; if ($this->db->query($sql)) { @@ -1778,7 +1800,8 @@ class Facture extends CommonObject $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; $sql.= ' SET remise_absolue = '.$remise; - $sql.= ' WHERE rowid = '.$this->id.' AND fk_statut = 0 ;'; + $sql.= ' WHERE rowid = '.$this->id; + $sql.= ' AND fk_statut = 0 ;'; dol_syslog("Facture::set_remise_absolue sql=$sql"); @@ -2309,6 +2332,7 @@ class Facture extends CommonObject // pour ne pas avoir de trou dans la numérotation $sql = "SELECT MAX(facnumber)"; $sql.= " FROM ".MAIN_DB_PREFIX."facture"; + $sql.= " WHERE entity = ".$conf->entity; $resql=$this->db->query($sql); if ($resql) @@ -2351,6 +2375,7 @@ class Facture extends CommonObject $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as ff ON f.rowid = ff.fk_facture_source"; $sql.= " WHERE (f.fk_statut = 1 OR (f.fk_statut = 3 AND f.close_code = 'abandon'))"; + $sql.= " AND f.entity = ".$conf->entity; $sql.= " AND f.paye = 0"; // Pas classée payée complètement $sql.= " AND pf.fk_paiement IS NULL"; // Aucun paiement deja fait $sql.= " AND ff.fk_statut IS NULL"; // Renvoi vrai si pas facture de remplacement @@ -2388,14 +2413,16 @@ class Facture extends CommonObject */ function list_qualified_avoir_invoices($socid=0) { + global $conf; + $return = array(); $sql = "SELECT f.rowid as rowid, f.facnumber, f.fk_statut, pf.fk_paiement"; $sql.= " FROM ".MAIN_DB_PREFIX."facture as f"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as ff ON (f.rowid = ff.fk_facture_source AND ff.type=1)"; - $sql.= " WHERE "; - $sql.= " f.fk_statut in (1,2)"; + $sql.= " WHERE f.entity = ".$conf->entity; + $sql.= " AND f.fk_statut in (1,2)"; // $sql.= " WHERE f.fk_statut >= 1"; // $sql.= " AND (f.paye = 1"; // Classée payée complètement // $sql.= " OR f.close_code IS NOT NULL)"; // Classée payée partiellement @@ -2447,9 +2474,11 @@ class Facture extends CommonObject if ($this->statut > 0 && $this->paye == 0 && $this->mode_reglement_id == 3) { - $sql = 'SELECT count(*) FROM '.MAIN_DB_PREFIX.'prelevement_facture_demande'; - $sql .= ' WHERE fk_facture='.$this->id; - $sql .= ' AND traite = 0'; + $sql = 'SELECT count(*)'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'prelevement_facture_demande'; + $sql.= ' WHERE fk_facture = '.$this->id; + $sql.= ' AND traite = 0'; + if ( $this->db->query( $sql) ) { $row = $this->db->fetch_row(); @@ -2534,7 +2563,6 @@ class Facture extends CommonObject $sql = "SELECT f.rowid, f.date_lim_reglement as datefin"; $sql.= " FROM ".MAIN_DB_PREFIX."facture as f"; - $sql.= ", ".MAIN_DB_PREFIX."societe as s"; if (!$user->rights->societe->client->voir && !$user->societe_id) { $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON f.fk_soc = sc.fk_soc"; @@ -2542,8 +2570,7 @@ class Facture extends CommonObject $clause = " AND"; } $sql.= $clause." f.paye=0"; - $sql.= " AND f.fk_soc = s.rowid"; - $sql.= " AND s.entity = ".$conf->entity; + $sql.= " AND f.entity = ".$conf->entity; $sql.= " AND f.fk_statut = 1"; if ($user->societe_id) $sql.= " AND f.fk_soc = ".$user->societe_id; @@ -2593,11 +2620,17 @@ class Facture extends CommonObject */ function initAsSpecimen() { - global $user,$langs; + global $user,$langs,$conf; // Charge tableau des id de société socids $socids = array(); - $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe WHERE client=1 LIMIT 10"; + + $sql = "SELECT rowid"; + $sql.= " FROM ".MAIN_DB_PREFIX."societe"; + $sql.= " WHERE client = 1"; + $sql.= " AND entity = ".$conf->entity; + $sql.= " LIMIT 10"; + $resql = $this->db->query($sql); if ($resql) { @@ -2614,7 +2647,12 @@ class Facture extends CommonObject // Charge tableau des produits prodids $prodids = array(); - $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product WHERE envente=1"; + + $sql = "SELECT rowid"; + $sql.= " FROM ".MAIN_DB_PREFIX."product"; + $sql.= " WHERE envente = 1"; + $sql.= " AND entity = ".$conf->entity; + $resql = $this->db->query($sql); if ($resql) { @@ -2686,7 +2724,7 @@ class Facture extends CommonObject $sql.= " WHERE sc.fk_user = " .$user->id; $clause = "AND"; } - $sql.= " ".$clause." s.entity = ".$conf->entity; + $sql.= " ".$clause." f.entity = ".$conf->entity; $resql=$this->db->query($sql); if ($resql) diff --git a/mysql/migration/2.6.0-2.7.0.sql b/mysql/migration/2.6.0-2.7.0.sql index 7fb1a6338cc..e4c9f31e377 100644 --- a/mysql/migration/2.6.0-2.7.0.sql +++ b/mysql/migration/2.6.0-2.7.0.sql @@ -27,6 +27,7 @@ ALTER TABLE llx_propal ADD COLUMN entity integer DEFAULT 1 NOT NULL AFTER ref; ALTER TABLE llx_commande ADD COLUMN entity integer DEFAULT 1 NOT NULL AFTER ref; ALTER TABLE llx_commande_fournisseur ADD COLUMN entity integer DEFAULT 1 NOT NULL AFTER ref; ALTER TABLE llx_product_fournisseur ADD COLUMN entity integer DEFAULT 1 NOT NULL AFTER ref_fourn; +ALTER TABLE llx_facture ADD COLUMN entity integer DEFAULT 1 NOT NULL AFTER facnumber; ALTER TABLE llx_rights_def DROP PRIMARY KEY; ALTER TABLE llx_user_param DROP INDEX fk_user; @@ -43,6 +44,7 @@ ALTER TABLE llx_commande DROP INDEX ref; ALTER TABLE llx_commande_fournisseur DROP INDEX uk_commande_fournisseur_ref; ALTER TABLE llx_product_fournisseur DROP INDEX fk_product; ALTER TABLE llx_product_fournisseur DROP INDEX fk_soc; +ALTER TABLE llx_facture DROP INDEX idx_facture_uk_facnumber; ALTER TABLE llx_rights_def ADD PRIMARY KEY (id, entity); ALTER TABLE llx_user_param ADD UNIQUE INDEX uk_user_param (fk_user,param,entity); @@ -59,4 +61,5 @@ ALTER TABLE llx_commande ADD UNIQUE INDEX uk_commande_ref (ref, entity); ALTER TABLE llx_commande_fournisseur ADD UNIQUE INDEX uk_commande_fournisseur_ref (ref, fk_soc, entity); ALTER TABLE llx_product_fournisseur ADD UNIQUE INDEX uk_product_fournisseur_ref (ref_fourn, fk_soc, entity); ALTER TABLE llx_product_fournisseur ADD INDEX idx_product_fourn_fk_product (fk_product, entity); -ALTER TABLE llx_product_fournisseur ADD INDEX idx_product_fourn_fk_soc (fk_soc, entity); \ No newline at end of file +ALTER TABLE llx_product_fournisseur ADD INDEX idx_product_fourn_fk_soc (fk_soc, entity); +ALTER TABLE llx_facture ADD UNIQUE INDEX idx_facture_uk_facnumber (facnumber, entity); \ No newline at end of file diff --git a/mysql/tables/llx_facture.key.sql b/mysql/tables/llx_facture.key.sql index c197066c585..ba8b40b3c0a 100644 --- a/mysql/tables/llx_facture.key.sql +++ b/mysql/tables/llx_facture.key.sql @@ -1,6 +1,7 @@ -- ============================================================================ -- Copyright (C) 2002-2004 Rodolphe Quiedeville --- Copyright (C) 2004 Laurent Destailleur +-- Copyright (C) 2004-2009 Laurent Destailleur +-- Copyright (C) 2005-2009 Regis Houssin -- -- 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 @@ -20,6 +21,8 @@ -- ============================================================================ +ALTER TABLE llx_facture ADD UNIQUE INDEX idx_facture_uk_facnumber (facnumber, entity); + ALTER TABLE llx_facture ADD INDEX idx_facture_fk_soc (fk_soc); ALTER TABLE llx_facture ADD INDEX idx_facture_fk_user_author (fk_user_author); ALTER TABLE llx_facture ADD INDEX idx_facture_fk_user_valid (fk_user_valid); @@ -31,5 +34,3 @@ ALTER TABLE llx_facture ADD CONSTRAINT fk_facture_fk_user_author FOREIGN KEY ALTER TABLE llx_facture ADD CONSTRAINT fk_facture_fk_user_valid FOREIGN KEY (fk_user_valid) REFERENCES llx_user (rowid); ALTER TABLE llx_facture ADD CONSTRAINT fk_facture_fk_facture_source FOREIGN KEY (fk_facture_source) REFERENCES llx_facture (rowid); ALTER TABLE llx_facture ADD CONSTRAINT fk_facture_fk_projet FOREIGN KEY (fk_projet) REFERENCES llx_projet (rowid); - -ALTER TABLE llx_facture ADD UNIQUE INDEX idx_facture_uk_facnumber (facnumber); diff --git a/mysql/tables/llx_facture.sql b/mysql/tables/llx_facture.sql index 6b14ca1e52a..bf7151cfbab 100644 --- a/mysql/tables/llx_facture.sql +++ b/mysql/tables/llx_facture.sql @@ -1,6 +1,7 @@ -- =========================================================================== -- Copyright (C) 2001-2005 Rodolphe Quiedeville --- Copyright (C) 2006 Laurent Destailleur +-- Copyright (C) 2004-2009 Laurent Destailleur +-- Copyright (C) 2005-2009 Regis Houssin -- -- 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 @@ -24,33 +25,34 @@ create table llx_facture ( rowid integer AUTO_INCREMENT PRIMARY KEY, facnumber varchar(30) NOT NULL, - type smallint DEFAULT 0 NOT NULL, + entity integer DEFAULT 1 NOT NULL, -- multi company id + type smallint DEFAULT 0 NOT NULL, ref_client varchar(30), increment varchar(10), fk_soc integer NOT NULL, - datec datetime, -- date de creation de la facture - datef date, -- date de la facture - date_valid date, -- date de validation + datec datetime, -- date de creation de la facture + datef date, -- date de la facture + date_valid date, -- date de validation paye smallint DEFAULT 0 NOT NULL, amount real DEFAULT 0 NOT NULL, - remise_percent real DEFAULT 0, -- remise relative - remise_absolue real DEFAULT 0, -- remise absolue - remise real DEFAULT 0, -- remise totale calculee + remise_percent real DEFAULT 0, -- remise relative + remise_absolue real DEFAULT 0, -- remise absolue + remise real DEFAULT 0, -- remise totale calculee - close_code varchar(16), -- Code motif cloture sans paiement complet - close_note varchar(128), -- Commentaire cloture sans paiement complet + close_code varchar(16), -- Code motif cloture sans paiement complet + close_note varchar(128), -- Commentaire cloture sans paiement complet - tva real DEFAULT 0, -- montant tva apres remise totale - total real DEFAULT 0, -- montant total ht apres remise totale - total_ttc real DEFAULT 0, -- montant total ttc apres remise totale + tva real DEFAULT 0, -- montant tva apres remise totale + total real DEFAULT 0, -- montant total ht apres remise totale + total_ttc real DEFAULT 0, -- montant total ttc apres remise totale fk_statut smallint DEFAULT 0 NOT NULL, - fk_user_author integer, -- createur de la facture - fk_user_valid integer, -- valideur de la facture + fk_user_author integer, -- createur de la facture + fk_user_valid integer, -- valideur de la facture - fk_facture_source integer, -- facture origine si facture avoir - fk_projet integer, -- projet auquel est associée la facture + fk_facture_source integer, -- facture origine si facture avoir + fk_projet integer, -- projet auquel est associée la facture fk_cond_reglement integer DEFAULT 1 NOT NULL, -- condition de reglement (30 jours, fin de mois ...) fk_mode_reglement integer, -- mode de reglement (Virement, Prélèvement) @@ -62,3 +64,11 @@ create table llx_facture import_key varchar(14) )type=innodb; + +-- +-- List of codes for the field entity +-- +-- 1 : first company invoice +-- 2 : second company invoice +-- 3 : etc... +-- \ No newline at end of file