diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index be79afe3c02e1a2330ff20fb878543ef6c338058..629ec80de1ed2d282a95fe0740875b61a7b30a44 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -275,10 +275,26 @@ if ($_POST['action'] == 'add') if ($_POST['fac_replacement'] > 0) { // Si facture remplacement - $result=$facture->fetch($_POST['fac_replacement']); + $datefacture = mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); - //print "xxx".$result." ".$facture->socidp; - $facid = $facture->create_clone('replace',$user); + $result=$facture->fetch($_POST['fac_replacement']); + + $facture->date = $datefacture; + $facture->note_public = trim($_POST['note_public']); + $facture->note = trim($_POST['note']); + $facture->ref_client = $_POST['ref_client']; + $facture->modelpdf = $_POST['model']; + $facture->projetid = $_POST['projetid']; + $facture->cond_reglement_id = $_POST['cond_reglement_id']; + $facture->mode_reglement_id = $_POST['mode_reglement_id']; + $facture->remise_absolue = $_POST['remise_absolue']; + $facture->remise_percent = $_POST['remise_percent']; + + // Propri�t�s particulieres a facture de remplacement + $facture->fk_facture_source = $_POST['fac_replacement']; + $facture->type = 1; + + $facid=$facture->create_clone($user); } else { @@ -662,7 +678,8 @@ if ($_POST['action'] == 'confirm_delete' && $_POST['confirm'] == 'yes') if ($user->rights->facture->supprimer) { $fac = new Facture($db); - $result = $fac->delete($_GET['facid']); + $result = $fac->fetch($_GET['facid']); + $result = $fac->delete(); if ($result > 0) { Header('Location: '.$_SERVER["PHP_SELF"]); @@ -1622,7 +1639,7 @@ else $facreplaced->fetch($fac->fk_facture_source); print ' ('.$langs->transnoentities("ReplaceInvoice",$facreplaced->getNomUrl(1)).')'; } - $facidnext=$fac->getIdNextInvoice(); + $facidnext=$fac->getIdReplacingInvoice(); if ($facidnext > 0) { $facthatreplace=new Facture($db); diff --git a/htdocs/compta/fiche.php b/htdocs/compta/fiche.php index 84fe152cc76529336f347107479e185608eb76e8..b393152bbf35b475d25899171e3bd19d33467f29 100644 --- a/htdocs/compta/fiche.php +++ b/htdocs/compta/fiche.php @@ -243,7 +243,7 @@ if ($socid > 0) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiement_facture as pf ON f.rowid=pf.fk_facture'; $sql.= " WHERE f.fk_soc = s.idp AND s.idp = ".$societe->id; $sql.= ' GROUP BY f.rowid'; - $sql.= " ORDER BY f.datef DESC"; + $sql.= " ORDER BY f.datef DESC, f.datec DESC"; $resql=$db->query($sql); if ($resql) @@ -355,7 +355,7 @@ if ($socid > 0) } } - print '<a class="butAction" href="'.DOL_URL_ROOT.'/comm/action/fiche.php?action=create&socid='.$objsoc->id.'">'.$langs->trans("AddAction").'</a>'; + print '<a class="butAction" href="'.DOL_URL_ROOT.'/comm/action/fiche.php?action=create&socid='.$socid.'">'.$langs->trans("AddAction").'</a>'; if ($user->rights->societe->contact->creer) { diff --git a/htdocs/facture.class.php b/htdocs/facture.class.php index fe18cf225eae7cd79da5097e975e652c668d0e2e..7410d4239c486f5484ebaece6aba17a8ebbde2c9 100644 --- a/htdocs/facture.class.php +++ b/htdocs/facture.class.php @@ -123,11 +123,11 @@ class Facture extends CommonObject global $langs,$conf,$mysoc; // Nettoyage param�tres - if (! $this->type) $this->type = 0 ; + if (! $this->type) $this->type = 0; $this->ref_client=trim($this->ref_client); $this->note=trim($this->note); $this->note_public=trim($this->note_public); - if (! $this->remise) $this->remise = 0 ; + if (! $this->remise) $this->remise = 0; if (! $this->mode_reglement_id) $this->mode_reglement_id = 0; $this->brouillon = 1; @@ -138,45 +138,6 @@ class Facture extends CommonObject $this->db->begin(); - // Verification param�tres - if ($this->type == 1) // si remplacement - { - // Controle que facture source connue - if ($this->fk_facture_source <= 0) - { - $this->error=$langs->trans("ErrorFieldRequired",$langs->trans("InvoiceReplacement")); - $this->db->rollback(); - return -10; - } - - // Charge la facture source a remplacer - $facreplaced=new Facture($this->db); - $result=$facreplaced->fetch($this->fk_facture_source); - if ($result <= 0) - { - $this->error=$langs->trans("ErrorBadInvoice"); - $this->db->rollback(); - return -11; - } - - // Controle que facture source non deja remplacee - $idreplacement=$facreplaced->getIdNextInvoice(); - if ($idreplacement != 0) - { - $this->error=$langs->trans("ErrorInvoiceAlreadyReplaced",$facreplaced->ref); - $this->db->rollback(); - return -12; - } - - $result=$facreplaced->set_canceled($user,'replaced',''); - if ($result < 0) - { - $this->error=$facreplaced->error." sql=".$sql; - $this->db->rollback(); - return -13; - } - } - // Facture r�currente if ($this->fac_rec > 0) { @@ -200,7 +161,6 @@ class Facture extends CommonObject // Insertion dans la base $socid = $this->socidp; - $number = $this->number; $amount = $this->amount; $remise = $this->remise; @@ -340,33 +300,22 @@ class Facture extends CommonObject /** * \brief Cr�ation de la facture en base depuis une autre - * \param facidsrc Id facture source - * \param invertdetail Inverse le signe des lignes details * \param user Object utilisateur qui cr�e * \return int <0 si ko, >0 si ok */ - function create_clone($reason='',$user) + function create_clone($user,$invertdetail=0) { // Charge facture source $facture=new Facture($this->db); - if ($reason == 'replace') - { - $facture->fk_facture_source = $this->id; - $facture->type = 1; - } - else - { - $facture->type = $this->type; - } - - $facture->socidp = $this->socidp; - $facture->number = $this->number; - $facture->date = $this->date; - $facture->note_public = $this->note_public; - $facture->note = $this->note; - $facture->ref_client = $this->ref_client; - $facture->modelpdf = $this->modelpdf; + $facture->fk_facture_source = $this->fk_facture_source; + $facture->type = $this->type; + $facture->socidp = $this->socidp; + $facture->date = $this->date; + $facture->note_public = $this->note_public; + $facture->note = $this->note; + $facture->ref_client = $this->ref_client; + $facture->modelpdf = $this->modelpdf; $facture->projetid = $this->projetid; $facture->cond_reglement_id = $this->cond_reglement_id; $facture->mode_reglement_id = $this->mode_reglement_id; @@ -374,6 +323,7 @@ class Facture extends CommonObject $facture->remise_absolue = $this->remise_absolue; $facture->remise_percent = $this->remise_percent; $facture->lignes = $this->lignes; // Tableau des lignes de factures + $facture->products = $this->lignes; // Tant que products encore utilis� if ($invertdetail) { @@ -387,7 +337,7 @@ class Facture extends CommonObject } } - dolibarr_syslog("Facture::create_clone invertdetail=$invertdetail socidp=".$this->socidp); + dolibarr_syslog("Facture::create_clone invertdetail=$invertdetail socidp=".$this->socidp." nboflines=".sizeof($facture->lignes)); $facid = $facture->create($user); @@ -731,10 +681,12 @@ class Facture extends CommonObject * \param rowid Id de la facture � supprimer * \return int <0 si ko, >0 si ok */ - function delete($rowid) + function delete($rowid=0) { global $user,$langs,$conf; + if (! $rowid) $rowid=$this->id; + dolibarr_syslog("Facture.class::delete rowid=".$rowid); $this->db->begin(); @@ -1024,6 +976,48 @@ class Facture extends CommonObject { $this->db->begin(); + // Verification param�tres + if ($this->type == 1) // si remplacement + { + // Controle que facture source connue + if ($this->fk_facture_source <= 0) + { + $this->error=$langs->trans("ErrorFieldRequired",$langs->trans("InvoiceReplacement")); + $this->db->rollback(); + return -10; + } + + // Charge la facture source a remplacer + $facreplaced=new Facture($this->db); + $result=$facreplaced->fetch($this->fk_facture_source); + if ($result <= 0) + { + $this->error=$langs->trans("ErrorBadInvoice"); + $this->db->rollback(); + return -11; + } + + // Controle que facture source non deja remplacee + $idreplacement=$facreplaced->getIdReplacingInvoice(); + if ($idreplacement != 0) + { + $facreplacement=new Facture($this->db); + $facreplacement->fetch($idreplacement); + $this->error=$langs->trans("ErrorInvoiceAlreadyReplaced",$facreplaced->ref,$facreplacement->ref); + $this->db->rollback(); + return -12; + } + + $result=$facreplaced->set_canceled($user,'replaced',''); + if ($result < 0) + { + $this->error=$facreplaced->error." sql=".$sql; + $this->db->rollback(); + return -13; + } + } + + // on v�rifie si la facture est en num�rotation provisoire $facref = substr($this->ref, 1, 4); @@ -1073,21 +1067,21 @@ class Facture extends CommonObject { // On renomme repertoire facture ($this->ref = ancienne ref, $numfa = nouvelle ref) // afin de ne pas perdre les fichiers attach�s - $facref = sanitize_string($this->ref); - $snumfa = sanitize_string($numfa); - $dirsource = $conf->facture->dir_output.'/'.$facref; - $dirdest = $conf->facture->dir_output.'/'.$snumfa; - if (file_exists($dirsource)) - { - dolibarr_syslog("Facture::set_valid() renommage rep ".$dirsource." en ".$dirdest); - - if (@rename($dirsource, $dirdest)) - { - dolibarr_syslog("Renommage ok"); - // Suppression ancien fichier PDF dans nouveau rep - dol_delete_file($conf->facture->dir_output.'/'.$snumfa.'/'.$facref.'.*'); - } - } + $facref = sanitize_string($this->ref); + $snumfa = sanitize_string($numfa); + $dirsource = $conf->facture->dir_output.'/'.$facref; + $dirdest = $conf->facture->dir_output.'/'.$snumfa; + if (file_exists($dirsource)) + { + dolibarr_syslog("Facture::set_valid() renommage rep ".$dirsource." en ".$dirdest); + + if (@rename($dirsource, $dirdest)) + { + dolibarr_syslog("Renommage ok"); + // Suppression ancien fichier PDF dans nouveau rep + dol_delete_file($conf->facture->dir_output.'/'.$snumfa.'/'.$facref.'.*'); + } + } } @@ -1208,22 +1202,25 @@ class Facture extends CommonObject } } - /** - * - * - */ + /** + * + * + */ function set_draft($userid) { + dolibarr_syslog("Facture.class::set_draft rowid=".$this->id); + $sql = "UPDATE ".MAIN_DB_PREFIX."facture SET fk_statut = 0"; - $sql .= " WHERE rowid = $this->id;"; + $sql.= " WHERE rowid = ".$this->id; - if ($this->db->query($sql) ) + if ($this->db->query($sql)) { return 1; } else { - dolibarr_print_error($this->db); + $this->error=$this->db->error(); + return -1; } } @@ -1750,11 +1747,12 @@ class Facture extends CommonObject * \brief Renvoie l'id de la facture qui la remplace * \return int <0 si ko, 0 si aucune facture ne remplace, id facture sinon */ - function getIdNextInvoice() + function getIdReplacingInvoice() { $sql = 'SELECT rowid'; $sql.= ' FROM '.MAIN_DB_PREFIX.'facture'; $sql.= ' WHERE fk_facture_source = '.$this->id; + $sql.= ' AND fk_statut > 0'; $resql=$this->db->query($sql); if ($resql) { @@ -2204,7 +2202,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 AND f.paye = 0 AND pf.fk_paiement IS NULL"; - $sql.= " AND ff.rowid IS NULL"; + $sql.= " AND IFNULL(ff.fk_statut,0) = 0"; // Doit renvoy� vrai si pas de jointure trouv� ou si jointure vers statut � 0 if ($socid > 0) $sql.=" AND f.fk_soc = ".$socid; $sql.= " ORDER BY f.facnumber"; diff --git a/htdocs/langs/en_US/bills.lang b/htdocs/langs/en_US/bills.lang index ef70b186d67b97b7be5725e24dbf0789fed3e53d..5ccf5b9ca4dbcfbbce15a2b35e4897dcf15fe68f 100644 --- a/htdocs/langs/en_US/bills.lang +++ b/htdocs/langs/en_US/bills.lang @@ -80,7 +80,7 @@ ErrorVATIntraNotConfigured=Intracommunautary VAT number not yet defined ErrorNoPaiementModeConfigured=No paiment mode yet defined ErrorCreateBankAccount=Creat a bank account then go to Setup panel of Invoice module to define paiement modes ErrorBillNotFound=Invoice %s does not exists -ErrorInvoiceAlreadyReplaced=Error, invoice %s has already been replaced +ErrorInvoiceAlreadyReplaced=Error, invoice %s has already been replaced by invoice %s ErrorDiscountAlreadyUsed=Error, discount already used BillFrom=From BillTo=Bill to diff --git a/htdocs/langs/fr_FR/bills.lang b/htdocs/langs/fr_FR/bills.lang index fb9637fd82f418f1e6d441da4a62e7654aa3397c..08ebda4a8020c89d55bc7159904aa066756dae47 100644 --- a/htdocs/langs/fr_FR/bills.lang +++ b/htdocs/langs/fr_FR/bills.lang @@ -80,7 +80,7 @@ ErrorVATIntraNotConfigured=Num ErrorNoPaiementModeConfigured=Aucun mode de r�glement d�fini ErrorCreateBankAccount=Cr�er un compte bancaire puis aller dans la configuration du module facture pour d�finir les modes de r�glement ErrorBillNotFound=Facture %s inexistante -ErrorInvoiceAlreadyReplaced=Erreur, la facture %s a d�j� �t� remplac�e +ErrorInvoiceAlreadyReplaced=Erreur, la facture %s a d�j� �t� remplac�e par la facture %s ErrorDiscountAlreadyUsed=Erreur, la remise a d�j� �t� attribu�e BillFrom=�metteur BillTo=Adress� �