diff --git a/htdocs/comm/propal.php b/htdocs/comm/propal.php index 6f63cb8450e..a7c08218a92 100644 --- a/htdocs/comm/propal.php +++ b/htdocs/comm/propal.php @@ -1,2053 +1,2030 @@ - - * Copyright (C) 2004-2008 Laurent Destailleur - * Copyright (C) 2004 Eric Seigne - * Copyright (C) 2005 Marc Barilley / Ocebo - * Copyright (C) 2005-2007 Regis Houssin - * Copyright (C) 2006 Andre Cianfarani - * Copyright (C) 2008 Raphael Bertrand (Resultic) - * - * 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. - */ - -/** - \file htdocs/comm/propal.php - \ingroup propale - \brief Page liste des propales (vision commercial) - \version $Id$ - */ - -require("./pre.inc.php"); -require_once(DOL_DOCUMENT_ROOT."/html.formfile.class.php"); -require_once(DOL_DOCUMENT_ROOT."/includes/modules/propale/modules_propale.php"); -require_once(DOL_DOCUMENT_ROOT."/lib/propal.lib.php"); - -$langs->load('companies'); -$langs->load('propal'); -$langs->load('compta'); -$langs->load('bills'); -$langs->load('orders'); -$langs->load('products'); - -if ($conf->projet->enabled) require_once(DOL_DOCUMENT_ROOT.'/project.class.php'); -if ($conf->commande->enabled) require_once(DOL_DOCUMENT_ROOT.'/commande/commande.class.php'); -require_once(DOL_DOCUMENT_ROOT.'/propal.class.php'); -require_once(DOL_DOCUMENT_ROOT.'/actioncomm.class.php'); - -$sall=isset($_GET["sall"])?$_GET["sall"]:$_POST["sall"]; -if (isset($_GET["msg"])) { $mesg=urldecode($_GET["mesg"]); } -$year=isset($_GET["year"])?$_GET["year"]:""; -$month=isset($_GET["month"])?$_GET["month"]:""; -$socid=isset($_GET['socid'])?$_GET['socid']:$_POST['socid']; - -// Security check -$module='propale'; -if (isset($_GET["socid"])) -{ - $objectid=$_GET["socid"]; - $module='societe'; - $dbtable=''; -} -else if (isset($_GET["propalid"]) && $_GET["propalid"] > 0) -{ - $objectid=$_GET["propalid"]; - $module='propale'; - $dbtable='propal'; -} -if ($user->societe_id) $socid=$user->societe_id; -$result = restrictedArea($user, $module, $objectid, $dbtable); - -// Nombre de ligne pour choix de produit/service predefinis -$NBLINES=4; - - -/******************************************************************************/ -/* Actions */ -/******************************************************************************/ - -// Suppression de la propale -if ($_REQUEST['action'] == 'confirm_delete' && $_REQUEST['confirm'] == 'yes') -{ - if ($user->rights->propale->supprimer) - { - $propal = new Propal($db, 0, $_GET['propalid']); - $propal->fetch($_GET['propalid']); - $propal->delete($user); - $propalid = 0; - $brouillon = 1; - } - Header('Location: '.$_SERVER["PHP_SELF"]); - exit; -} - -/* - * Supprime une ligne produit AVEC OU SANS confirmation - */ -if (($_REQUEST['action'] == 'confirm_deleteline' && $_REQUEST['confirm'] == 'yes' && $conf->global->PRODUIT_CONFIRM_DELETE_LINE) -|| ($_GET['action'] == 'deleteline' && !$conf->global->PRODUIT_CONFIRM_DELETE_LINE)) -{ - if ($user->rights->propale->creer) - { - $propal = new Propal($db); - $propal->fetch($_GET['propalid']); - $result = $propal->delete_product($_GET['lineid']); - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); - } - Header('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$_GET['propalid']); - exit; -} - -// Validation de la propale -if ($_REQUEST['action'] == 'confirm_validate' && $_REQUEST['confirm'] == 'yes' && $user->rights->propale->valider) -{ - $propal = new Propal($db); - $propal->fetch($_GET['propalid']); - $propal->fetch_client(); - - $result=$propal->valid($user); - if ($result >= 0) - { - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); - } - else - { - $mesg='
'.$fac->error.'
'; - } -} - -if ($_POST['action'] == 'setecheance') -{ - $propal = new Propal($db); - $propal->fetch($_GET['propalid']); - $result=$propal->set_echeance($user,dolibarr_mktime(12, 0, 0, $_POST['echmonth'], $_POST['echday'], $_POST['echyear'])); - if ($result < 0) dolibarr_print_error($db,$propal->error); -} -if ($_POST['action'] == 'setdate_livraison') -{ - $propal = new Propal($db); - $propal->fetch($_GET['propalid']); - $result=$propal->set_date_livraison($user,dolibarr_mktime(12, 0, 0, $_POST['liv_month'], $_POST['liv_day'], $_POST['liv_year'])); - if ($result < 0) dolibarr_print_error($db,$propal->error); -} - -if ($_POST['action'] == 'setdeliveryadress' && $user->rights->propale->creer) -{ - $propal = new Propal($db); - $propal->fetch($_GET['propalid']); - $result=$propal->set_adresse_livraison($user,$_POST['adresse_livraison_id']); - if ($result < 0) dolibarr_print_error($db,$propal->error); -} - -// Positionne ref client -if ($_POST['action'] == 'set_ref_client' && $user->rights->propale->creer) -{ - $propal = new Propal($db); - $propal->fetch($_GET['propalid']); - $propal->set_ref_client($user, $_POST['ref_client']); -} - -/* - * Creation propale - */ -if ($_POST['action'] == 'add' && $user->rights->propale->creer) -{ - $propal = new Propal($db); - $propal->socid=$_POST['socid']; - $propal->fetch_client(); - - $db->begin(); - - // Si on a selectionne une propal a copier, on realise la copie - if($_POST['createmode']=='copy' && $_POST['copie_propal']) - { - if ($propal->fetch($_POST['copie_propal']) > 0) - { - $propal->datep = dolibarr_mktime(12, 0, 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); - $propal->date_livraison = dolibarr_mktime(12, 0, 0, $_POST['liv_month'], $_POST['liv_day'], $_POST['liv_year']); - $propal->adresse_livraison_id = $_POST['adresse_livraison_id']; - $propal->duree_validite = $_POST['duree_validite']; - $propal->cond_reglement_id = $_POST['cond_reglement_id']; - $propal->mode_reglement_id = $_POST['mode_reglement_id']; - if($_POST['remise_percent']>0) - $propal->remise_percent = $_POST['remise_percent']; - else - $propal->remise_percent = 0; - $propal->remise_absolue = $_POST['remise_absolue']; - $propal->socid = $_POST['socid']; - $propal->contactid = $_POST['contactidp']; - $propal->projetidp = $_POST['projetidp']; - $propal->modelpdf = $_POST['model']; - $propal->author = $user->id; // deprecated - $propal->note = $_POST['note']; - $propal->ref = $_POST['ref']; - $propal->statut = 0; - - $id = $propal->create_from($user); - } - else - { - $mesg = '
'.$langs->trans("ErrorFailedToCopyProposal",$_POST['copie_propal']).'
'; - } - } - else - { - $propal->datep = dolibarr_mktime(12, 0, 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); - $propal->date_livraison = dolibarr_mktime(12, 0, 0, $_POST['liv_month'], $_POST['liv_day'], $_POST['liv_year']); - $propal->adresse_livraison_id = $_POST['adresse_livraison_id']; - $propal->duree_validite = $_POST['duree_validite']; - $propal->cond_reglement_id = $_POST['cond_reglement_id']; - $propal->mode_reglement_id = $_POST['mode_reglement_id']; - - $propal->contactid = $_POST['contactidp']; - $propal->projetidp = $_POST['projetidp']; - $propal->modelpdf = $_POST['model']; - $propal->author = $user->id; // deprecated - $propal->note = $_POST['note']; - $propal->ref_client = $_POST['ref_client']; - $propal->ref = $_POST['ref']; - - for ($i = 1 ; $i <= $conf->global->PROPALE_NEW_FORM_NB_PRODUCT ; $i++) - { - if ($_POST['idprod'.$i]) - { - $xid = 'idprod'.$i; - $xqty = 'qty'.$i; - $xremise = 'remise'.$i; - $propal->add_product($_POST[$xid],$_POST[$xqty],$_POST[$xremise]); - } - } - - $id = $propal->create($user); - } - - if ($id > 0) - { - $error=0; - - // Insertion contact par defaut si defini - if ($_POST["contactidp"]) - { - $result=$propal->add_contact($_POST["contactidp"],'CUSTOMER','external'); - - if ($result > 0) - { - $error=0; - } - else - { - $mesg = '
'.$langs->trans("ErrorFailedToAddContact").'
'; - $error=1; - } - } - - if (! $error) - { - $db->commit(); - - // Generation document PDF - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - propale_pdf_create($db, $id, $_REQUEST['model'], $outputlangs); - dolibarr_syslog('Redirect to '.$_SERVER["PHP_SELF"].'?propalid='.$id); - Header ('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$id); - exit; - } - else - { - $db->rollback(); - } - } - else - { - dolibarr_print_error($db,$propal->error); - $db->rollback(); - exit; - } -} - -/* - * Cloture de la propale - */ -if ($_REQUEST['action'] == 'setstatut' && $user->rights->propale->cloturer) -{ - if (! $_POST['cancel']) - { - $propal = new Propal($db); - $propal->fetch($_GET['propalid']); - // prevent browser refresh from closing proposal several times - if ($propal->statut==1) - { - $propal->cloture($user, $_REQUEST['statut'], $_REQUEST['note']); - } - } -} - - -/* - * Add file - */ -if ($_POST['addfile']) -{ - // Set tmp user directory - $conf->users->dir_tmp=DOL_DATA_ROOT."/users/".$user->id; - $upload_dir = $conf->users->dir_tmp.'/temp/'; - - if (! empty($_FILES['addedfile']['tmp_name'])) - { - if (! is_dir($upload_dir)) create_exdir($upload_dir); - - if (is_dir($upload_dir)) - { - if (dol_move_uploaded_file($_FILES['addedfile']['tmp_name'], $upload_dir . "/" . $_FILES['addedfile']['name'],0) > 0) - { - $mesg = '
'.$langs->trans("FileTransferComplete").'
'; - //print_r($_FILES); - - include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); - $formmail = new FormMail($db); - // Add file in list of files in session - $formmail->add_attached_files($upload_dir . "/" . $_FILES['addedfile']['name'],$_FILES['addedfile']['name'],$_FILES['addedfile']['type']); - } - else - { - // Echec transfert (fichier d�passant la limite ?) - $mesg = '
'.$langs->trans("ErrorFileNotUploaded").'
'; - // print_r($_FILES); - } - } - } - $_GET["action"]='presend'; -} - -/* - * Send mail - */ -if ($_POST['action'] == 'send' && ! $_POST['addfile'] && ! $_POST['cancel']) -{ - $langs->load('mails'); - - $propal= new Propal($db); - if ( $propal->fetch($_POST['propalid']) ) - { - $propalref = sanitize_string($propal->ref); - $file = $conf->propal->dir_output . '/' . $propalref . '/' . $propalref . '.pdf'; - - if (is_readable($file)) - { - $propal->fetch_client(); - - if ($_POST['sendto']) - { - // Le destinataire a ete fourni via le champ libre - $sendto = $_POST['sendto']; - $sendtoid = 0; - } - elseif ($_POST['receiver']) - { - // Le destinataire a ete fourni via la liste deroulante - if ($_POST['receiver'] < 0) // Id du tiers - { - $sendto = $propal->client->email; - $sendtoid = 0; - } - else // Id du contact - { - $sendto = $propal->client->contact_get_email($_POST['receiver']); - $sendtoid = $_POST['receiver']; - } - } - - if (strlen($sendto)) - { - $langs->load("commercial"); - - $from = $_POST['fromname'] . ' <' . $_POST['frommail'] .'>'; - $replyto = $_POST['replytoname']. ' <' . $_POST['replytomail'].'>'; - $message = $_POST['message']; - $sendtocc = $_POST['sendtocc']; - $deliveryreceipt = $_POST['deliveryreceipt']; - - if ($_POST['action'] == 'send') - { - if (strlen($_POST['subject'])) $subject = $_POST['subject']; - else $subject = $langs->transnoentities('Propal').' '.$propal->ref; - $actiontypecode='AC_PROP'; - $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto.".\n"; - if ($message) - { - $actionmsg.=$langs->transnoentities('MailTopic').": ".$subject."\n"; - $actionmsg.=$langs->transnoentities('TextUsedInTheMessageBody').":\n"; - $actionmsg.=$message; - } - $actionmsg2=$langs->transnoentities('Action'.$actiontypecode); - } - - // Create form object - include_once('../html.formmail.class.php'); - $formmail = new FormMail($db); - - $attachedfiles=$formmail->get_attached_files(); - $filepath = $attachedfiles['paths']; - $filename = $attachedfiles['names']; - $mimetype = $attachedfiles['mimes']; - - // Envoi de la propal - require_once(DOL_DOCUMENT_ROOT.'/lib/CMailFile.class.php'); - $mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc,'',$deliveryreceipt); - if ($mailfile->error) - { - $mesg='
'.$mailfile->error.'
'; - } - else - { - $result=$mailfile->sendfile(); - if ($result) - { - $mesg='
'.$langs->trans('MailSuccessfulySent',$from,$sendto).'.
'; - - $error=0; - - // Initialisation donnees - $propal->sendtoid=$sendtoid; - $propal->actiontypecode=$actiontypecode; - $propal->actionmsg = $actionmsg; - $propal->actionmsg2= $actionmsg2; - $propal->propalrowid=$propal->id; - - // Appel des triggers - include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); - $interface=new Interfaces($db); - $result=$interface->run_triggers('PROPAL_SENTBYMAIL',$propal,$user,$langs,$conf); - if ($result < 0) { $error++; $this->errors=$interface->errors; } - // Fin appel triggers - - if ($error) - { - dolibarr_print_error($db); - } - else - { - // Redirect here - // This avoid sending mail twice if going out and then back to page - Header('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&msg='.urlencode($mesg)); - exit; - } - } - else - { - $langs->load("other"); - $mesg='
'; - if ($mailfile->error) - { - $mesg.=$langs->trans('ErrorFailedToSendMail',$from,$sendto); - $mesg.='
'.$mailfile->error; - } - else - { - $mesg.='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS'; - } - $mesg.='
'; - } - } - } - else - { - $langs->load("other"); - $mesg='
'.$langs->trans('ErrorMailRecipientIsEmpty').' !
'; - dolibarr_syslog('Recipient email is empty'); - } - } - else - { - $langs->load("other"); - $mesg='
'.$langs->trans('ErrorCantReadFile',$file).'
'; - dolibarr_syslog('Failed to read file: '.$file); - } - } - else - { - $langs->load("other"); - $mesg='
'.$langs->trans('ErrorFailedToReadEntity',$langs->trans("Proposal")).'
'; - dolibarr_syslog('Impossible de lire les donnees de la facture. Le fichier propal n\'a peut-etre pas ete genere.'); - } -} - -if ($_GET['action'] == 'commande') -{ - /* - * Cloture de la propale - */ - $propal = new Propal($db); - $propal->fetch($propalid); - $propal->create_commande($user); -} - -if ($_GET['action'] == 'modif' && $user->rights->propale->creer) -{ - /* - * Repasse la propale en mode brouillon - */ - $propal = new Propal($db); - $propal->fetch($_GET['propalid']); - $propal->set_draft($user); - - //reg�n�ration pdf - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); -} - -if ($_POST['action'] == "setabsolutediscount" && $user->rights->propale->creer) -{ - if ($_POST["remise_id"]) - { - $prop = new Propal($db); - $prop->id=$_GET['propalid']; - $ret=$prop->fetch($_GET['propalid']); - if ($ret > 0) - { - $result=$prop->insert_discount($_POST["remise_id"]); - if ($result < 0) - { - $mesg='
'.$prop->error.'
'; - } - } - else - { - dolibarr_print_error($db,$prop->error); - } - } -} - -/* - * Ajout d'une ligne produit dans la propale - */ -if ($_POST['action'] == "addligne" && $user->rights->propale->creer) -{ - if (isset($_POST['qty']) && (($_POST['np_price']!='' && ($_POST['np_desc'] || $_POST['dp_desc'])) || $_POST['idprod'])) - { - $propal = new Propal($db); - $ret=$propal->fetch($_POST['propalid']); - if ($ret < 0) - { - dolibarr_print_error($db,$propal->error); - exit; - } - $ret=$propal->fetch_client(); - - $price_base_type = 'HT'; - - // Ecrase $pu par celui du produit - // Ecrase $desc par celui du produit - // Ecrase $txtva par celui du produit - if ($_POST['idprod']) - { - $prod = new Product($db, $_POST['idprod']); - $prod->fetch($_POST['idprod']); - - $tva_tx = get_default_tva($mysoc,$propal->client,$prod->tva_tx); - $tva_npr = get_default_npr($mysoc,$propal->client,$prod->tva_tx); - - // On defini prix unitaire - if ($conf->global->PRODUIT_MULTIPRICES == 1) - { - $pu_ht = $prod->multiprices[$propal->client->price_level]; - $pu_ttc = $prod->multiprices_ttc[$propal->client->price_level]; - $price_base_type = $prod->multiprices_base_type[$propal->client->price_level]; - } - else - { - $pu_ht = $prod->price; - $pu_ttc = $prod->price_ttc; - $price_base_type = $prod->price_base_type; - } - - // On reevalue prix selon taux tva car taux tva transaction peut etre different - // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur). - if ($tva_tx != $prod->tva_tx) - { - if ($price_base_type != 'HT') - { - $pu_ht = price2num($pu_ttc / (1 + ($tva_tx/100)), 'MU'); - } - else - { - $pu_ttc = price2num($pu_ht * (1 + ($tva_tx/100)), 'MU'); - } - } - - $desc = $prod->description; - $desc.= ($prod->description && $_POST['np_desc']) ? "\n" : ""; - $desc.= $_POST['np_desc']; - } - else - { - $pu_ht=$_POST['np_price']; - $tva_tx=eregi_replace('\*','',$_POST['np_tva_tx']); - $tva_npr=eregi('\*',$_POST['np_tva_tx'])?1:0; - $desc=$_POST['dp_desc']; - } - - $info_bits=0; - if ($tva_npr) $info_bits |= 0x01; - - // Define remise_percent - if ($_POST['remise_percent']>0) - $remise_percent=$_POST['remise_percent']; - else - $remise_percent=0; - - // Insert line - $result=$propal->addline( - $_POST['propalid'], - $desc, - $pu_ht, - $_POST['qty'], - $tva_tx, - $_POST['idprod'], - $remise_percent, - $price_base_type, - $pu_ttc, - $info_bits - ); - - if ($result > 0) - { - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); - } - else - { - $mesg='
'.$propal->error.'
'; - } - - } -} - -/* - * Mise a jour d'une ligne dans la propale - */ -if ($_POST['action'] == 'updateligne' && $user->rights->propale->creer && $_POST["save"] == $langs->trans("Save")) -{ - $propal = new Propal($db); - if (! $propal->fetch($_POST['propalid']) > 0) dolibarr_print_error($db); - - // Define info_bits - $info_bits=0; - if (eregi('\*',$_POST['tva_tx'])) $info_bits |= 0x01; - - // Define vat_rate - $vat_rate=$_POST['tva_tx']; - $vat_rate=eregi_replace('\*','',$vat_rate); - - // Define remise_percent - if ($_POST['remise_percent']>0) - $remise_percent=$_POST['remise_percent']; - else - $remise_percent=0; - - - $result = $propal->updateline($_POST['lineid'], - $_POST['subprice'], - $_POST['qty'], - $remise_percent, - $vat_rate, - $_POST['desc'], - 'HT', - $info_bits); - - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); -} - -/* - * Generation doc (depuis lien ou depuis cartouche doc) - */ -if ($_REQUEST['action'] == 'builddoc' && $user->rights->propale->creer) -{ - $propal = new Propal($db); - $propal->fetch($_GET['propalid']); - if ($_REQUEST['model']) - { - $propal->setDocModel($user, $_REQUEST['model']); - } - - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - $result=propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); - if ($result <= 0) - { - dolibarr_print_error($db,$result); - exit; - } - else - { - Header ('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$propal->id.'#builddoc'); - exit; - } -} - -if ($_POST['action'] == 'classin') -{ - $propal = new Propal($db); - $propal->fetch($_GET['propalid']); - $propal->setProject($_POST['projetidp']); -} - -// Conditions de reglement -if ($_POST["action"] == 'setconditions') -{ - $propal = new Propal($db); - $propal->fetch($_REQUEST['propalid']); - $result = $propal->cond_reglement($_POST['cond_reglement_id']); - $_GET['propalid']=$_REQUEST['propalid']; -} - -if ($_REQUEST['action'] == 'setremisepercent' && $user->rights->propale->creer) -{ - // Define remise_percent - if ($_POST['remise_percent']>0) - $remise_percent=$_POST['remise_percent']; - else - $remise_percent=0; - - $propal = new Propal($db); - $propal->fetch($_REQUEST['propalid']); - $result = $propal->set_remise_percent($user, $remise_percent); - $_GET['propalid']=$_REQUEST['propalid']; -} - -if ($_REQUEST['action'] == 'setremiseabsolue' && $user->rights->propale->creer) -{ - $propal = new Propal($db); - $propal->fetch($_REQUEST['propalid']); - $result = $propal->set_remise_absolue($user, $_POST['remise_absolue']); - $_GET['propalid']=$_REQUEST['propalid']; -} - -// Mode de reglement -if ($_POST["action"] == 'setmode') -{ - $propal = new Propal($db); - $propal->fetch($_REQUEST['propalid']); - $result = $propal->mode_reglement($_POST['mode_reglement_id']); - $_GET['propalid']=$_REQUEST['propalid']; -} - -/* - * Ordonnancement des lignes - */ - -if ($_GET['action'] == 'up' && $user->rights->propale->creer) -{ - $propal = new Propal($db, '', $_GET["propalid"]); - $propal->fetch($_GET['propalid']); - $propal->line_up($_GET['rowid']); - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); - Header ('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$_GET["propalid"].'#'.$_GET['rowid']); -} - -if ($_GET['action'] == 'down' && $user->rights->propale->creer) -{ - $propal = new Propal($db, '', $_GET["propalid"]); - $propal->fetch($_GET['propalid']); - $propal->line_down($_GET['rowid']); - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); - Header ('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$_GET["propalid"].'#'.$_GET['rowid']); - exit; -} - - -/* - * Affichage page - */ - -llxHeader('',$langs->trans('Proposal'),'Proposition'); - -$html = new Form($db); -$formfile = new FormFile($db); - -if ($_GET['propalid'] > 0) -{ - /* - * Affichage fiche propal en mode visu - * - */ - - if ($mesg) print "$mesg
"; - - $propal = new Propal($db); - $propal->fetch($_GET['propalid']); - - $societe = new Societe($db); - $societe->fetch($propal->socid); - - $head = propal_prepare_head($propal); - dolibarr_fiche_head($head, 'comm', $langs->trans('Proposal')); - - /* - * Confirmation de la suppression de la propale - */ - if ($_GET['action'] == 'delete') - { - $html->form_confirm($_SERVER["PHP_SELF"].'?propalid='.$propal->id, $langs->trans('DeleteProp'), $langs->trans('ConfirmDeleteProp'), 'confirm_delete'); - print '
'; - } - - /* - * Confirmation de la suppression d'une ligne produit/service - */ - if ($_GET['action'] == 'ask_deleteline' && $conf->global->PRODUIT_CONFIRM_DELETE_LINE) - { - $html->form_confirm($_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&lineid='.$_GET["lineid"], $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteline'); - print '
'; - } - - /* - * Confirmation de la validation de la propale - */ - if ($_GET['action'] == 'validate') - { - $html->form_confirm($_SERVER["PHP_SELF"].'?propalid='.$propal->id, $langs->trans('ValidateProp'), $langs->trans('ConfirmValidateProp'), 'confirm_validate'); - print '
'; - } - - - /* - * Fiche propal - * - */ - - print ''; - - // Ref - print ''; - - // Ref client - print ''; - print ''; - - $rowspan=8; - - // Societe - print ''; - print ''; - - // Ligne info remises tiers - print ''; - - // Dates - print ''; - - if ($conf->projet->enabled) $rowspan++; - if ($conf->expedition_bon->enabled || $conf->livraison_bon->enabled) - { - if ($conf->global->PROPALE_ADD_SHIPPING_DATE || !$conf->commande->enabled) $rowspan++; - if ($conf->global->PROPALE_ADD_DELIVERY_ADDRESS || !$conf->commande->enabled) $rowspan++; - } - - // Notes - print ''; - print ''; - - // Date fin propal - print ''; - print ''; - print ''; - - - // date de livraison (conditionne sur PROPALE_ADD_SHIPPING_DATE car carac a - // gerer par les commandes et non les propales - if ($conf->expedition_bon->enabled || $conf->livraison_bon->enabled) - { - if ($conf->global->PROPALE_ADD_SHIPPING_DATE || !$conf->commande->enabled) - { - $langs->load('deliveries'); - print ''; - print ''; - } - - // adresse de livraison - if ($conf->global->PROPALE_ADD_DELIVERY_ADDRESS || !$conf->commande->enabled) - { - print ''; - } - } - - // Conditions et modes de reglement - print ''; - print ''; - - // Mode paiement - print ''; - print ''; - - // Projet - if ($conf->projet->enabled) - { - $langs->load("projects"); - print ''; - } - else - { - if ($propal->statut == 0 && $user->rights->propale->creer) - { - if ($_GET['action'] != 'classer' && $propal->brouillon) print ''; - print '
'.$langs->trans('Ref').''.$propal->ref.'
'; - print ''; - if ($_GET['action'] != 'refclient' && $propal->brouillon) print ''; - print '
'; - print $langs->trans('RefCustomer').''; - print ''.img_edit($langs->trans('Modify')).'
'; - print '
'; - if ($user->rights->propale->creer && $_GET['action'] == 'refclient') - { - print '
'; - print ''; - print ''; - print ' '; - print '
'; - } - else - { - print $propal->ref_client; - } - print '
'.$langs->trans('Company').''.$societe->getNomUrl(1).'
'.$langs->trans('Discounts').''; - if ($societe->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$societe->remise_client); - else print $langs->trans("CompanyHasNoRelativeDiscount"); - $absolute_discount=$societe->getAvailableDiscounts('','fk_facture_source IS NULL'); - $absolute_creditnote=$societe->getAvailableDiscounts('','fk_facture_source IS NOT NULL'); - print '. '; - if ($absolute_discount) - { - if ($propal->statut > 0) - { - print $langs->trans("CompanyHasAbsoluteDiscount",price($absolute_discount),$langs->transnoentities("Currency".$conf->monnaie)); - } - else - { - // Remise dispo de type non avoir - $filter='fk_facture_source IS NULL'; - print '
'; - $html->form_remise_dispo($_SERVER["PHP_SELF"].'?propalid='.$propal->id,0,'remise_id',$societe->id,$absolute_discount,$filter); - } - } - if ($absolute_creditnote) - { - print $langs->trans("CompanyHasCreditNote",price($absolute_creditnote),$langs->transnoentities("Currency".$conf->monnaie)).'. '; - } - if (! $absolute_discount && ! $absolute_creditnote) print $langs->trans("CompanyHasNoAbsoluteDiscount").'.'; - print '
'.$langs->trans('Date').''; - print dolibarr_print_date($propal->date,'daytext'); - print ''.$langs->trans('NotePublic').' :
'. nl2br($propal->note_public).'
'; - print ''; - if ($_GET['action'] != 'editecheance' && $propal->brouillon) print ''; - print '
'; - print $langs->trans('DateEndPropal'); - print 'id.'">'.img_edit($langs->trans('SetConditions'),1).'
'; - print '
'; - if ($propal->brouillon && $_GET['action'] == 'editecheance') - { - print '
'; - print ''; - $html->select_date($propal->fin_validite,'ech','','','',"editecheance"); - print ''; - print '
'; - } - else - { - if ($propal->fin_validite) - { - print dolibarr_print_date($propal->fin_validite,'daytext'); - if ($propal->statut == 1 && $propal->fin_validite < (time() - $conf->propal->cloture->warning_delay)) print img_warning($langs->trans("Late")); - } - else - { - print ' '; - } - } - print '
'; - print ''; - if ($_GET['action'] != 'editdate_livraison' && $propal->brouillon) print ''; - print '
'; - print $langs->trans('DeliveryDate'); - print 'id.'">'.img_edit($langs->trans('SetDeliveryDate'),1).'
'; - print '
'; - if ($_GET['action'] == 'editdate_livraison') - { - print '
'; - print ''; - $html->select_date($propal->date_livraison,'liv_','','','',"editdate_livraison"); - print ''; - print '
'; - } - else - { - print dolibarr_print_date($propal->date_livraison,'%a %d %B %Y'); - } - print '
'; - print ''; - - if ($_GET['action'] != 'editdelivery_adress' && $propal->brouillon) print ''; - print '
'; - print $langs->trans('DeliveryAddress'); - print 'socid.'&propalid='.$propal->id.'">'.img_edit($langs->trans('SetDeliveryAddress'),1).'
'; - print '
'; - - if ($_GET['action'] == 'editdelivery_adress') - { - $html->form_adresse_livraison($_SERVER['PHP_SELF'].'?propalid='.$propal->id,$propal->adresse_livraison_id,$_GET['socid'],'adresse_livraison_id','propal',$propal->id); - } - else - { - $html->form_adresse_livraison($_SERVER['PHP_SELF'].'?propalid='.$propal->id,$propal->adresse_livraison_id,$_GET['socid'],'none','propal',$propal->id); - } - print '
'; - print ''; - if ($_GET['action'] != 'editconditions' && $propal->brouillon) print ''; - print '
'; - print $langs->trans('PaymentConditionsShort'); - print 'id.'">'.img_edit($langs->trans('SetConditions'),1).'
'; - print '
'; - if ($_GET['action'] == 'editconditions') - { - $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?propalid='.$propal->id,$propal->cond_reglement_id,'cond_reglement_id'); - } - else - { - $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?propalid='.$propal->id,$propal->cond_reglement_id,'none'); - } - print '
'; - print ''; - if ($_GET['action'] != 'editmode' && $propal->brouillon) print ''; - print '
'; - print $langs->trans('PaymentMode'); - print 'id.'">'.img_edit($langs->trans('SetMode'),1).'
'; - print '
'; - if ($_GET['action'] == 'editmode') - { - $html->form_modes_reglement($_SERVER['PHP_SELF'].'?propalid='.$propal->id,$propal->mode_reglement_id,'mode_reglement_id'); - } - else - { - $html->form_modes_reglement($_SERVER['PHP_SELF'].'?propalid='.$propal->id,$propal->mode_reglement_id,'none'); - } - print '
'; - print ''; - $numprojet = $societe->has_projects(); - if (! $numprojet) - { - print '
'; - print $langs->trans('Project').'
'; - print '
'; - print $langs->trans("NoProject").''; - print 'id.'&action=create>'.$langs->trans('AddProject').''; - print ''.img_edit($langs->trans('SetProject')).'
'; - print ''; - if ($_GET['action'] == 'classer') - { - $html->form_project($_SERVER['PHP_SELF'].'?propalid='.$propal->id, $propal->socid, $propal->projetidp, 'projetidp'); - } - else - { - $html->form_project($_SERVER['PHP_SELF'].'?propalid='.$propal->id, $propal->socid, $propal->projetidp, 'none'); - } - print ''; - } - else - { - print ''; - if (!empty($propal->projetidp)) - { - print ''; - $proj = new Project($db); - $proj->fetch($propal->projetidp); - print ''; - print $proj->title; - print ''; - print ''; - } - else { - print ' '; - } - } - } - print ''; - } - - // Amount HT - print ''.$langs->trans('AmountHT').''; - print ''.price($propal->total_ht).''; - print ''.$langs->trans("Currency".$conf->monnaie).''; - - // Amount VAT - print ''.$langs->trans('AmountVAT').''; - print ''.price($propal->total_tva).''; - print ''.$langs->trans("Currency".$conf->monnaie).''; - - // Amount TTC - print ''.$langs->trans('AmountTTC').''; - print ''.price($propal->total_ttc).''; - print ''.$langs->trans("Currency".$conf->monnaie).''; - - // Statut - print ''.$langs->trans('Status').''.$propal->getLibStatut(4).''; - print '
'; - - /* - * Lignes de propale - */ - print ''; - - $sql = 'SELECT pt.rowid, pt.description, pt.fk_product, pt.fk_remise_except,'; - $sql.= ' pt.qty, pt.tva_tx, pt.remise_percent, pt.subprice, pt.info_bits,'; - $sql.= ' pt.total_ht, pt.total_tva, pt.total_ttc, pt.marge_tx, pt.marque_tx, pt.pa_ht, pt.special_code,'; - $sql.= ' p.label as product, p.ref, p.fk_product_type, p.rowid as prodid,'; - $sql.= ' p.description as product_desc'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt'; - $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product=p.rowid'; - $sql.= ' WHERE pt.fk_propal = '.$propal->id; - $sql.= ' ORDER BY pt.rang ASC, pt.rowid'; - $resql = $db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - $i = 0; - - if ($num) - { - print ''; - print ''; - if ($conf->global->PRODUIT_USE_MARKUP) - { - print ''; - } - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print "\n"; - } - $var=true; - while ($i < $num) - { - $objp = $db->fetch_object($resql); - $var=!$var; - - // Ligne en mode visu - if ($_GET['action'] != 'editline' || $_GET['lineid'] != $objp->rowid) - { - print ''; - - // Produit - if ($objp->fk_product > 0) - { - print ''; - } - else - { - print '\n"; - } - - if ($conf->global->PRODUIT_USE_MARKUP && $conf->use_javascript_ajax) - { - $formMarkup = ''."\n"; - $formMarkup.= '
'.$langs->trans('Description').''.$langs->trans('Markup').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').''.$langs->trans('AmountHT').' 
'; - print ''; // ancre pour retourner sur la ligne; - - // Affiche ligne produit - $text = ''; - if ($objp->fk_product_type==1) $text.= img_object($langs->trans('ShowService'),'service'); - else $text.= img_object($langs->trans('ShowProduct'),'product'); - $text.= ' '.$objp->ref.''; - $text.= ' - '.$objp->product; - $description=($conf->global->PRODUIT_DESC_IN_FORM?'':dol_htmlentitiesbr($objp->description)); - print $html->textwithtooltip($text,$description,3,'','',$i); - print_date_range($objp->date_start,$objp->date_end); - if ($conf->global->PRODUIT_DESC_IN_FORM) - { - print ($objp->description && $objp->description!=$objp->product)?'
'.dol_htmlentitiesbr($objp->description):''; - } - - print '
'; - print ''; // ancre pour retourner sur la ligne - if (($objp->info_bits & 2) == 2) - { - print ''; - print img_object($langs->trans("ShowReduc"),'reduc').' '.$langs->trans("Discount"); - print ''; - if ($objp->description) - { - if ($objp->description == '(CREDIT_NOTE)') - { - $discount=new DiscountAbsolute($db); - $discount->fetch($objp->fk_remise_except); - print ' - '.$langs->transnoentities("DiscountFromCreditNote",$discount->getNomUrl(0)); - } - else - { - print ' - '.nl2br($objp->description); - } - } - } - else - { - print nl2br($objp->description); - print_date_range($objp->date_start,$objp->date_end); - } - print "
'."\n"; - if ($objp->fk_product > 0) - { - $formMarkup.= ''."\n"; - $formMarkup.= ''."\n"; - $formMarkup.= ''."\n"; - } - $formMarkup.= ''."\n"; - $formMarkup.= ''."\n"; - $formMarkup.= ''."\n"; - $formMarkup.= ''."\n"; - $formMarkup.= ''."\n"; - $formMarkup.= ''."\n"; - //$formMarkup.= ''."\n"; - $formMarkup.= ''."\n"; - $formMarkup.= ''."\n"; - $formMarkup.= ''."\n"; - $formMarkup.= '
 
 '.$langs->trans('SupplierPrice').''.$html->select_product_fourn_price($objp->fk_product,'productfournpriceid').'
 
 '.$langs->trans('PurchasePrice').' '.$langs->trans('HT').'
 '.$langs->trans('MarkupRate').'
 '.$langs->trans('SellingPrice').' '.$langs->trans('HT').'
 '.$langs->trans('CashFlow').' '.$langs->trans('HT').'
'."\n"; - $formMarkup.= ''."\n"; - //$formMarkup.= '   '."\n"; - $formMarkup.= '
'."\n"; - $formMarkup.= ajax_updaterWithID("rate".$i,"markup","sellingprice_ht".$i,"/product/ajaxproducts.php","&count=".$i,"working")."\n"; - - - print ''."\n"; - - print ''."\n"; - - - print ''; - print ''; - print ''; - print '
'; - if (($objp->info_bits & 2) == 2) - { - // Ligne remise predefinie, on ne permet pas modif - } - else - { - $picto = ''; - $picto.= img_calc(); - $picto.= ''; - print $html->textwithtooltip($picto,$langs->trans("ToCalculateMarkup"),3,'','',$i); - } - print ''.vatrate($objp->marge_tx).'%
'; - print ''; - } - - // VAT Rate - print ''.vatrate($objp->tva_tx,'%',$objp->info_bits).''; - - // U.P HT - print ''.price($objp->subprice)."\n"; - - // Qty - print ''; - if ((($objp->info_bits & 2) != 2) && $objp->special_code != 3) - { - print $objp->qty; - } - else print ' '; - print ''; - - // Remise % - if ($objp->remise_percent > 0 && $objp->special_code != 3) - { - print ''.dolibarr_print_reduction($objp->remise_percent)."\n"; - } - else - { - print ' '; - } - - // Montant total HT - if ($objp->special_code == 3) - { - // Si ligne en option - print ''.$langs->trans('Option').''; - } - else - { - print ''.price($objp->total_ht)."\n"; - } - - // Icone d'edition et suppression - if ($propal->statut == 0 && $user->rights->propale->creer) - { - print ''; - if (($objp->info_bits & 2) == 2) - { - // Ligne remise predefinie, on permet pas modif - } - else - { - print 'rowid.'#'.$objp->rowid.'">'; - print img_edit(); - print ''; - } - print ''; - print ''; - if ($conf->global->PRODUIT_CONFIRM_DELETE_LINE) - { - if ($conf->use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) - { - $url = $_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&lineid='.$objp->rowid.'&action=confirm_deleteline&confirm=yes'; - print 'trans("No").'\',\'deleteline'.$i.'\')">'; - } - else - { - print 'rowid.'">'; - } - } - else - { - print 'rowid.'">'; - } - print img_delete(); - print ''; - if ($num > 1) - { - print ''; - if ($i > 0) - { - print 'rowid.'">'; - print img_up(); - print ''; - } - if ($i < $num-1) - { - print 'rowid.'">'; - print img_down(); - print ''; - } - print ''; - } - } - else - { - print ' '; - } - - print ''; - } - - // Ligne en mode update - if ($propal->statut == 0 && $_GET["action"] == 'editline' && $user->rights->propale->creer && $_GET["lineid"] == $objp->rowid) - { - print '
rowid.'" method="post">'; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; // ancre pour retourner sur la ligne - if ($objp->fk_product > 0) - { - print ''; - if ($objp->fk_product_type==1) print img_object($langs->trans('ShowService'),'service'); - else print img_object($langs->trans('ShowProduct'),'product'); - print ' '.$objp->ref.''; - print ' - '.nl2br($objp->product); - print '
'; - } - if ($_GET["action"] == 'editline') - { - // editeur wysiwyg - if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) - { - require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); - $doleditor=new DolEditor('desc',$objp->description,164,'dolibarr_details'); - $doleditor->Create(); - } - else - { - print ''; - } - } - print ''; - if ($conf->global->PRODUIT_USE_MARKUP) - { - print ''.vatrate($objp->marge_tx).'%'; - } - print ''; - print $html->select_tva('tva_tx',$objp->tva_tx,$mysoc,$societe,'',$objp->info_bits); - print ''; - print ''; - print ''; - if (($objp->info_bits & 2) != 2) - { - print ''; - } - else print ' '; - print ''; - print ''; - if (($objp->info_bits & 2) != 2) - { - print '%'; - } - else print ' '; - print ''; - print ''; - print '
'; - print '' . "\n"; - /* - if ($conf->service->enabled) - { - print ""; - print 'Si produit de type service a duree limitee: Du '; - print $html->select_date($objp->date_start,"date_start",0,0,$objp->date_start?0:1); - print ' au '; - print $html->select_date($objp->date_end,"date_end",0,0,$objp->date_end?0:1); - print ''; - print '' . "\n"; - } - */ - print "
\n"; - } - - $i++; - } - - $db->free($resql); - } - else - { - dolibarr_print_error($db); - } - - /* - * Ajouter une ligne - */ - if ($propal->statut == 0 && $user->rights->propale->creer && $_GET["action"] <> 'editline') - { - if ($conf->global->PRODUIT_USE_MARKUP) $colspan = 'colspan="2"'; - print ''; - print ''; - print ''; // ancre - print $langs->trans('Description').''; - print ''.$langs->trans('VAT').''; - print ''.$langs->trans('PriceUHT').''; - print ''.$langs->trans('Qty').''; - print ''.$langs->trans('ReductionShort').''; - print ' '; - print "\n"; - - // Ajout produit produits/services personnalises - print '
'; - print ''; - print ''; - - $var=true; - - print '\n"; - print ''; - // editeur wysiwyg - if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) - { - require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); - $doleditor=new DolEditor('dp_desc','',100,'dolibarr_details'); - $doleditor->Create(); - } - else - { - print ''; - } - print ''; - print ''; - if($societe->tva_assuj == "0") - { - print '0'; - } - else - { - $html->select_tva('np_tva_tx', $conf->defaulttx, $mysoc, $societe); - } - print "\n"; - print ''; - print ''; - print '%'; - print ''; - print ''; - - print '
'; - - // Ajout de produits/services predefinis - if ($conf->produit->enabled) - { - if ($conf->global->PRODUIT_USE_MARKUP) - { - $colspan = 'colspan="4"'; - } - else - { - $colspan = 'colspan="3"'; - } - print ''; - print ''; - if ($conf->service->enabled) - { - print $langs->trans('RecordedProductsAndServices'); - } - else - { - print $langs->trans('RecordedProducts'); - } - print ''; - print ''.$langs->trans('Qty').''; - print ''.$langs->trans('ReductionShort').''; - print ' '; - print ''; - print '
'; - print ''; - print ''; - - $var=!$var; - - print ''; - print ''; - // multiprix - if($conf->global->PRODUIT_MULTIPRICES == 1) - { - $html->select_produits('','idprod','',$conf->produit->limit_size,$societe->price_level); - } - else - { - $html->select_produits('','idprod','',$conf->produit->limit_size); - } - if (! $conf->global->PRODUIT_USE_SEARCH_TO_SELECT) print '
'; - - // editeur wysiwyg - if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) - { - require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); - $doleditor=new DolEditor('np_desc','',100,'dolibarr_details'); - $doleditor->Create(); - } - else - { - print ''; - } - - print ''; - print ''; - print '%'; - - print ''; - print ''."\n"; - - print '
'; - } - } - - print ''; - - print ''; - print "\n"; - - /* - * Formulaire cloture (signe ou non) - */ - $form_close = '
'; - $form_close.= ''; - $form_close.= ''; - $form_close.= ''; - $form_close.= ''; - $form_close.= '
'.$langs->trans('Note').'
'.$langs->trans("CloseAs").''; - $form_close.= ''; - $form_close.= ''; - $form_close.= '
'; - $form_close.= ''; - if ($conf->use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) - { - $form_close.= '   '; - } - else - { - $form_close.= '   '; - } - $form_close.= '
'; - - if ($_GET['action'] == 'statut') - { - print $form_close; - } - - - /* - * Boutons Actions - */ - if ($_GET['action'] != 'presend') - { - print '
'; - - if ($_GET['action'] != 'statut' && $_GET['action'] <> 'editline') - { - - // Valid - if ($propal->statut == 0 && $propal->total_ttc > 0 && $user->rights->propale->valider) - { - print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) - { - $url = $_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&action=confirm_validate&confirm=yes'; - print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.dol_escape_js($langs->trans('ConfirmValidateProp')).'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'validate\')"'; - } - else - { - print 'href="'.$_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&action=validate"'; - } - print '>'.$langs->trans('Validate').''; - } - - // Edit - if ($propal->statut == 1 && $user->rights->propale->creer) - { - print ''.$langs->trans('Modify').''; - } - - // Send - if ($propal->statut == 1 && $user->rights->propale->envoyer) - { - $propref = sanitize_string($propal->ref); - $file = $conf->propal->dir_output . '/'.$propref.'/'.$propref.'.pdf'; - if (file_exists($file)) - { - print ''.$langs->trans('SendByMail').''; - } - } - - // Close - if ($propal->statut == 1 && $user->rights->propale->cloturer) - { - print ''."\n"; - - print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) - { - print 'href="#" onClick="dialogInfo($(\'confirm_close\').innerHTML)"'."\n"; - } - else - { - print 'href="'.$_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&action=statut"'; - } - print '>'.$langs->trans('Close').''; - } - - // Delete - if ($propal->statut == 0 && $user->rights->propale->supprimer) - { - print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) - { - $url = $_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&action=confirm_delete&confirm=yes'; - print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.$langs->trans('ConfirmDeleteProp').'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'delete\')"'; - } - else - { - print 'href="'.$_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&action=delete"'; - } - print '>'.$langs->trans('Delete').''; - } - - } - - print '
'; - print "
\n"; - } - - if ($_GET['action'] != 'presend') - { - print '
'; - print ''; // ancre - - - /* - * Documents generes - */ - $filename=sanitize_string($propal->ref); - $filedir=$conf->propal->dir_output . "/" . sanitize_string($propal->ref); - $urlsource=$_SERVER["PHP_SELF"]."?propalid=".$propal->id; - $genallowed=$user->rights->propale->creer; - $delallowed=$user->rights->propale->supprimer; - - $var=true; - - $somethingshown=$formfile->show_documents('propal',$filename,$filedir,$urlsource,$genallowed,$delallowed,$propal->modelpdf); - - - /* - * Commandes rattachees - */ - if($conf->commande->enabled) - { - $propal->loadOrders(); - $coms = $propal->commandes; - if (sizeof($coms) > 0) - { - if ($somethingshown) { print '
'; $somethingshown=1; } - print_titre($langs->trans('RelatedOrders')); - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - $var=true; - for ($i = 0 ; $i < sizeof($coms) ; $i++) - { - $var=!$var; - print '\n"; - print ''; - print ''; - print ''; - print "\n"; - } - print '
'.$langs->trans("Ref").''.$langs->trans("Date").''.$langs->trans("Price").''.$langs->trans("Status").'
'; - print ''.img_object($langs->trans("ShowOrder"),"order").' '.$coms[$i]->ref."'.dolibarr_print_date($coms[$i]->date,'day').''.price($coms[$i]->total_ttc).''.$coms[$i]->getLibStatut(3).'
'; - } - } - - print '
'; - - // List of actions on element - include_once(DOL_DOCUMENT_ROOT.'/html.formactions.class.php'); - $formactions=new FormActions($db); - $somethingshown=$formactions->showactions($propal,'propal',$socid); - - print '
'; - } - - - /* - * Action presend - * - */ - if ($_GET['action'] == 'presend') - { - $ref = sanitize_string($propal->ref); - $file = $conf->propal->dir_output . '/' . $ref . '/' . $ref . '.pdf'; - - print '
'; - print_titre($langs->trans('SendPropalByMail')); - - $liste[0]=" "; - foreach ($societe->thirdparty_and_contact_email_array() as $key=>$value) - { - $liste[$key]=$value; - } - - // Create form object - include_once('../html.formmail.class.php'); - $formmail = new FormMail($db); - $formmail->fromtype = 'user'; - $formmail->fromid = $user->id; - $formmail->fromname = $user->fullname; - $formmail->frommail = $user->email; - $formmail->withfrom=1; - $formmail->withto=$liste; - $formmail->withtocc=1; - $formmail->withtopic=$langs->trans('SendPropalRef','__PROPREF__'); - $formmail->withfile=2; - $formmail->withbody=1; - $formmail->withdeliveryreceipt=1; - $formmail->withcancel=1; - // Tableau des substitutions - $formmail->substit['__PROPREF__']=$propal->ref; - // Tableau des parametres complementaires - $formmail->param['action']='send'; - $formmail->param['models']='propal_send'; - $formmail->param['propalid']=$propal->id; - $formmail->param['returnurl']=DOL_URL_ROOT.'/comm/propal.php?propalid='.$propal->id; - - // Init list of files - if (! empty($_REQUEST["mode"]) && $_REQUEST["mode"]=='init') - { - $formmail->clear_attached_files(); - $formmail->add_attached_files($file,$propal->ref.'.pdf','application/pdf'); - } - - $formmail->show_form(); - - print '
'; - } - -} -else -{ - /**************************************************************************** - * * - * Mode Liste des propales * - * * - ****************************************************************************/ - - $sortorder=$_GET['sortorder']; - $sortfield=$_GET['sortfield']; - $page=$_GET['page']; - $viewstatut=addslashes($_GET['viewstatut']); - $propal_statut = addslashes($_GET['propal_statut']); - if($propal_statut != '') - $viewstatut=$propal_statut; - - if (! $sortfield) $sortfield='p.datep'; - if (! $sortorder) $sortorder='DESC'; - $limit = $conf->liste_limit; - $offset = $limit * $page ; - $pageprev = $page - 1; - $pagenext = $page + 1; - - $sql = 'SELECT s.nom, s.rowid, s.client, '; - $sql.= 'p.rowid as propalid, p.total_ht, p.ref, p.fk_statut, '.$db->pdate('p.datep').' as dp,'.$db->pdate('p.fin_validite').' as dfv'; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user"; - $sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s, '.MAIN_DB_PREFIX.'propal as p'; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - if ($sall) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'propaldet as pd ON p.rowid=pd.fk_propal'; - $sql.= ' WHERE p.fk_soc = s.rowid'; - - if (!$user->rights->societe->client->voir && !$socid) //restriction - { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; - } - if (!empty($_GET['search_ref'])) - { - $sql .= " AND p.ref LIKE '%".addslashes($_GET['search_ref'])."%'"; - } - if (!empty($_GET['search_societe'])) - { - $sql .= " AND s.nom LIKE '%".addslashes($_GET['search_societe'])."%'"; - } - if (!empty($_GET['search_montant_ht'])) - { - $sql .= " AND p.total_ht='".addslashes($_GET['search_montant_ht'])."'"; - } - if ($sall) $sql.= " AND (s.nom like '%".addslashes($sall)."%' OR p.note like '%".addslashes($sall)."%' OR pd.description like '%".addslashes($sall)."%')"; - if ($socid) $sql .= ' AND s.rowid = '.$socid; - if ($viewstatut <> '') - { - $sql .= ' AND p.fk_statut in ('.$viewstatut.')'; - } - if ($month > 0) - { - if ($year > 0) - $sql .= " AND date_format(p.datep, '%Y-%m') = '$year-$month'"; - else - $sql .= " AND date_format(p.datep, '%m') = '$month'"; - } - if ($year > 0) - { - $sql .= " AND date_format(p.datep, '%Y') = $year"; - } - if (strlen($_POST['sf_ref']) > 0) - { - $sql .= " AND p.ref like '%".addslashes($_POST["sf_ref"]) . "%'"; - } - - $sql .= ' ORDER BY '.$sortfield.' '.$sortorder.', p.ref DESC'; - $sql .= $db->plimit($limit + 1,$offset); - $result=$db->query($sql); - - if ($result) - { - $num = $db->num_rows($result); - $propalstatic=new Propal($db); - print_barre_liste($langs->trans('ListOfProposals'), $page,'propal.php','&socid='.$socid.'&viewstatut='.$viewstatut,$sortfield,$sortorder,'',$num); - $i = 0; - print ''; - print ''; - print_liste_field_titre($langs->trans('Ref'),$_SERVER["PHP_SELF"],'p.ref','','&socid='.$socid.'&viewstatut='.$viewstatut,'',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('Company'),$_SERVER["PHP_SELF"],'s.nom','','&socid='.$socid.'&viewstatut='.$viewstatut,'',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('Date'),$_SERVER["PHP_SELF"],'p.datep','','&socid='.$socid.'&viewstatut='.$viewstatut, 'align="center"',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('DateEndPropalShort'),$_SERVER["PHP_SELF"],'dfv','','&socid='.$socid.'&viewstatut='.$viewstatut, 'align="center"',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('Price'),$_SERVER["PHP_SELF"],'p.total_ht','','&socid='.$socid.'&viewstatut='.$viewstatut, 'align="right"',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('Status'),$_SERVER["PHP_SELF"],'p.fk_statut','','&socid='.$socid.'&viewstatut='.$viewstatut,'align="right"',$sortfield,$sortorder); - print ''; - print "\n"; - // Lignes des champs de filtre - print ''; - - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print "\n"; - print ''; - - $var=true; - - while ($i < min($num,$limit)) - { - $objp = $db->fetch_object($result); - $now = time(); - $var=!$var; - print ''; - print ''; - - // Date propale - print '\n"; - - // Date fin validite - if ($objp->dfv) - { - print ''; - } - else - { - print ''; - } - - print '\n"; - $propal=New Propal($db); - print '\n"; - print ""; - print "\n"; - - $total = $total + $objp->total_ht; - $subtotal = $subtotal + $objp->total_ht; - - $i++; - } - print '
 
'; - print ''; - print ''; - print ''; - print ''; - print $langs->trans('Month').': '; - print ' '.$langs->trans('Year').': '; - $max_year = date("Y"); - $syear = $year; - if($syear == '') - $syear = date("Y"); - $html->select_year($syear,'year',1, '', $max_year); - print ' '; - print ''; - print ''; - $html->select_propal_statut($viewstatut); - print ''; - print '
'; - - $propalstatic->id=$objp->propalid; - $propalstatic->ref=$objp->ref; - - print ''; - print ''; - - print ''; - - print '
'; - print $propalstatic->getNomUrl(1); - print ''; - if ($objp->fk_statut == 1 && $objp->dfv < (time() - $conf->propal->cloture->warning_delay)) print img_warning($langs->trans("Late")); - print ''; - - $filename=sanitize_string($objp->ref); - $filedir=$conf->propal->dir_output . '/' . sanitize_string($objp->ref); - $urlsource=$_SERVER['PHP_SELF'].'?propalid='.$objp->propalid; - $formfile->show_documents('propal',$filename,$filedir,$urlsource,'','','','','',1); - - print '
'; - - if ($objp->client == 1) - { - $url = DOL_URL_ROOT.'/comm/fiche.php?socid='.$objp->rowid; - } - else - { - $url = DOL_URL_ROOT.'/comm/prospect/fiche.php?socid='.$objp->rowid; - } - - // Societe - print '
'.img_object($langs->trans('ShowCompany'),'company').' '.$objp->nom.''; - $y = strftime('%Y',$objp->dp); - $m = strftime('%m',$objp->dp); - - print strftime('%d',$objp->dp)."\n"; - print ' '; - print dolibarr_print_date($objp->dp,'%b')."\n"; - print ' '; - print strftime('%Y',$objp->dp)."'.dolibarr_print_date($objp->dfv); - print ' '.price($objp->total_ht)."'.$propal->LibStatut($objp->fk_statut,5)." 
'; - $db->free($result); - } - else - { - dolibarr_print_error($db); - } -} -$db->close(); - -llxFooter('$Date$ - $Revision$'); - -?> + + * Copyright (C) 2004-2008 Laurent Destailleur + * Copyright (C) 2004 Eric Seigne + * Copyright (C) 2005 Marc Barilley / Ocebo + * Copyright (C) 2005-2007 Regis Houssin + * Copyright (C) 2006 Andre Cianfarani + * + * 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. + */ + +/** + \file htdocs/comm/propal.php + \ingroup propale + \brief Page liste des propales (vision commercial) + \version $Id$ + */ + +require("./pre.inc.php"); +require_once(DOL_DOCUMENT_ROOT."/html.formfile.class.php"); +require_once(DOL_DOCUMENT_ROOT."/includes/modules/propale/modules_propale.php"); +require_once(DOL_DOCUMENT_ROOT."/lib/propal.lib.php"); + +$langs->load('companies'); +$langs->load('propal'); +$langs->load('compta'); +$langs->load('bills'); +$langs->load('orders'); +$langs->load('products'); + +if ($conf->projet->enabled) require_once(DOL_DOCUMENT_ROOT.'/project.class.php'); +if ($conf->commande->enabled) require_once(DOL_DOCUMENT_ROOT.'/commande/commande.class.php'); +require_once(DOL_DOCUMENT_ROOT.'/propal.class.php'); +require_once(DOL_DOCUMENT_ROOT.'/actioncomm.class.php'); + +$sall=isset($_GET["sall"])?$_GET["sall"]:$_POST["sall"]; +if (isset($_GET["msg"])) { $mesg=urldecode($_GET["mesg"]); } +$year=isset($_GET["year"])?$_GET["year"]:""; +$month=isset($_GET["month"])?$_GET["month"]:""; +$socid=isset($_GET['socid'])?$_GET['socid']:$_POST['socid']; + +// Security check +$module='propale'; +if (isset($_GET["socid"])) +{ + $objectid=$_GET["socid"]; + $module='societe'; + $dbtable=''; +} +else if (isset($_GET["propalid"]) && $_GET["propalid"] > 0) +{ + $objectid=$_GET["propalid"]; + $module='propale'; + $dbtable='propal'; +} +if ($user->societe_id) $socid=$user->societe_id; +$result = restrictedArea($user, $module, $objectid, $dbtable); + +// Nombre de ligne pour choix de produit/service predefinis +$NBLINES=4; + + +/******************************************************************************/ +/* Actions */ +/******************************************************************************/ + +// Suppression de la propale +if ($_REQUEST['action'] == 'confirm_delete' && $_REQUEST['confirm'] == 'yes') +{ + if ($user->rights->propale->supprimer) + { + $propal = new Propal($db, 0, $_GET['propalid']); + $propal->fetch($_GET['propalid']); + $propal->delete($user); + $propalid = 0; + $brouillon = 1; + } + Header('Location: '.$_SERVER["PHP_SELF"]); + exit; +} + +/* + * Supprime une ligne produit AVEC OU SANS confirmation + */ +if (($_REQUEST['action'] == 'confirm_deleteline' && $_REQUEST['confirm'] == 'yes' && $conf->global->PRODUIT_CONFIRM_DELETE_LINE) +|| ($_GET['action'] == 'deleteline' && !$conf->global->PRODUIT_CONFIRM_DELETE_LINE)) +{ + if ($user->rights->propale->creer) + { + $propal = new Propal($db); + $propal->fetch($_GET['propalid']); + $result = $propal->delete_product($_GET['lineid']); + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); + } + Header('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$_GET['propalid']); + exit; +} + +// Validation de la propale +if ($_REQUEST['action'] == 'confirm_validate' && $_REQUEST['confirm'] == 'yes' && $user->rights->propale->valider) +{ + $propal = new Propal($db); + $propal->fetch($_GET['propalid']); + $propal->fetch_client(); + + $result=$propal->valid($user); + if ($result >= 0) + { + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); + } + else + { + $mesg='
'.$fac->error.'
'; + } +} + +if ($_POST['action'] == 'setecheance') +{ + $propal = new Propal($db); + $propal->fetch($_GET['propalid']); + $result=$propal->set_echeance($user,dolibarr_mktime(12, 0, 0, $_POST['echmonth'], $_POST['echday'], $_POST['echyear'])); + if ($result < 0) dolibarr_print_error($db,$propal->error); +} +if ($_POST['action'] == 'setdate_livraison') +{ + $propal = new Propal($db); + $propal->fetch($_GET['propalid']); + $result=$propal->set_date_livraison($user,dolibarr_mktime(12, 0, 0, $_POST['liv_month'], $_POST['liv_day'], $_POST['liv_year'])); + if ($result < 0) dolibarr_print_error($db,$propal->error); +} + +if ($_POST['action'] == 'setdeliveryadress' && $user->rights->propale->creer) +{ + $propal = new Propal($db); + $propal->fetch($_GET['propalid']); + $result=$propal->set_adresse_livraison($user,$_POST['adresse_livraison_id']); + if ($result < 0) dolibarr_print_error($db,$propal->error); +} + +// Positionne ref client +if ($_POST['action'] == 'set_ref_client' && $user->rights->propale->creer) +{ + $propal = new Propal($db); + $propal->fetch($_GET['propalid']); + $propal->set_ref_client($user, $_POST['ref_client']); +} + +/* + * Creation propale + */ +if ($_POST['action'] == 'add' && $user->rights->propale->creer) +{ + $propal = new Propal($db); + $propal->socid=$_POST['socid']; + $propal->fetch_client(); + + $db->begin(); + + // Si on a selectionne une propal a copier, on realise la copie + if($_POST['createmode']=='copy' && $_POST['copie_propal']) + { + if ($propal->fetch($_POST['copie_propal']) > 0) + { + $propal->datep = dolibarr_mktime(12, 0, 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); + $propal->date_livraison = dolibarr_mktime(12, 0, 0, $_POST['liv_month'], $_POST['liv_day'], $_POST['liv_year']); + $propal->adresse_livraison_id = $_POST['adresse_livraison_id']; + $propal->duree_validite = $_POST['duree_validite']; + $propal->cond_reglement_id = $_POST['cond_reglement_id']; + $propal->mode_reglement_id = $_POST['mode_reglement_id']; + $propal->remise_percent = $_POST['remise_percent']; + $propal->remise_absolue = $_POST['remise_absolue']; + $propal->socid = $_POST['socid']; + $propal->contactid = $_POST['contactidp']; + $propal->projetidp = $_POST['projetidp']; + $propal->modelpdf = $_POST['model']; + $propal->author = $user->id; // deprecated + $propal->note = $_POST['note']; + $propal->ref = $_POST['ref']; + $propal->statut = 0; + + $id = $propal->create_from($user); + } + else + { + $mesg = '
'.$langs->trans("ErrorFailedToCopyProposal",$_POST['copie_propal']).'
'; + } + } + else + { + $propal->datep = dolibarr_mktime(12, 0, 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); + $propal->date_livraison = dolibarr_mktime(12, 0, 0, $_POST['liv_month'], $_POST['liv_day'], $_POST['liv_year']); + $propal->adresse_livraison_id = $_POST['adresse_livraison_id']; + $propal->duree_validite = $_POST['duree_validite']; + $propal->cond_reglement_id = $_POST['cond_reglement_id']; + $propal->mode_reglement_id = $_POST['mode_reglement_id']; + + $propal->contactid = $_POST['contactidp']; + $propal->projetidp = $_POST['projetidp']; + $propal->modelpdf = $_POST['model']; + $propal->author = $user->id; // deprecated + $propal->note = $_POST['note']; + $propal->ref_client = $_POST['ref_client']; + $propal->ref = $_POST['ref']; + + for ($i = 1 ; $i <= $conf->global->PROPALE_NEW_FORM_NB_PRODUCT ; $i++) + { + if ($_POST['idprod'.$i]) + { + $xid = 'idprod'.$i; + $xqty = 'qty'.$i; + $xremise = 'remise'.$i; + $propal->add_product($_POST[$xid],$_POST[$xqty],$_POST[$xremise]); + } + } + + $id = $propal->create($user); + } + + if ($id > 0) + { + $error=0; + + // Insertion contact par defaut si defini + if ($_POST["contactidp"]) + { + $result=$propal->add_contact($_POST["contactidp"],'CUSTOMER','external'); + + if ($result > 0) + { + $error=0; + } + else + { + $mesg = '
'.$langs->trans("ErrorFailedToAddContact").'
'; + $error=1; + } + } + + if (! $error) + { + $db->commit(); + + // Generation document PDF + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + propale_pdf_create($db, $id, $_REQUEST['model'], $outputlangs); + dolibarr_syslog('Redirect to '.$_SERVER["PHP_SELF"].'?propalid='.$id); + Header ('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$id); + exit; + } + else + { + $db->rollback(); + } + } + else + { + dolibarr_print_error($db,$propal->error); + $db->rollback(); + exit; + } +} + +/* + * Cloture de la propale + */ +if ($_REQUEST['action'] == 'setstatut' && $user->rights->propale->cloturer) +{ + if (! $_POST['cancel']) + { + $propal = new Propal($db); + $propal->fetch($_GET['propalid']); + // prevent browser refresh from closing proposal several times + if ($propal->statut==1) + { + $propal->cloture($user, $_REQUEST['statut'], $_REQUEST['note']); + } + } +} + + +/* + * Add file + */ +if ($_POST['addfile']) +{ + // Set tmp user directory + $conf->users->dir_tmp=DOL_DATA_ROOT."/users/".$user->id; + $upload_dir = $conf->users->dir_tmp.'/temp/'; + + if (! empty($_FILES['addedfile']['tmp_name'])) + { + if (! is_dir($upload_dir)) create_exdir($upload_dir); + + if (is_dir($upload_dir)) + { + if (dol_move_uploaded_file($_FILES['addedfile']['tmp_name'], $upload_dir . "/" . $_FILES['addedfile']['name'],0) > 0) + { + $mesg = '
'.$langs->trans("FileTransferComplete").'
'; + //print_r($_FILES); + + include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); + $formmail = new FormMail($db); + // Add file in list of files in session + $formmail->add_attached_files($upload_dir . "/" . $_FILES['addedfile']['name'],$_FILES['addedfile']['name'],$_FILES['addedfile']['type']); + } + else + { + // Echec transfert (fichier d�passant la limite ?) + $mesg = '
'.$langs->trans("ErrorFileNotUploaded").'
'; + // print_r($_FILES); + } + } + } + $_GET["action"]='presend'; +} + +/* + * Send mail + */ +if ($_POST['action'] == 'send' && ! $_POST['addfile'] && ! $_POST['cancel']) +{ + $langs->load('mails'); + + $propal= new Propal($db); + if ( $propal->fetch($_POST['propalid']) ) + { + $propalref = sanitize_string($propal->ref); + $file = $conf->propal->dir_output . '/' . $propalref . '/' . $propalref . '.pdf'; + + if (is_readable($file)) + { + $propal->fetch_client(); + + if ($_POST['sendto']) + { + // Le destinataire a ete fourni via le champ libre + $sendto = $_POST['sendto']; + $sendtoid = 0; + } + elseif ($_POST['receiver']) + { + // Le destinataire a ete fourni via la liste deroulante + if ($_POST['receiver'] < 0) // Id du tiers + { + $sendto = $propal->client->email; + $sendtoid = 0; + } + else // Id du contact + { + $sendto = $propal->client->contact_get_email($_POST['receiver']); + $sendtoid = $_POST['receiver']; + } + } + + if (strlen($sendto)) + { + $langs->load("commercial"); + + $from = $_POST['fromname'] . ' <' . $_POST['frommail'] .'>'; + $replyto = $_POST['replytoname']. ' <' . $_POST['replytomail'].'>'; + $message = $_POST['message']; + $sendtocc = $_POST['sendtocc']; + $deliveryreceipt = $_POST['deliveryreceipt']; + + if ($_POST['action'] == 'send') + { + if (strlen($_POST['subject'])) $subject = $_POST['subject']; + else $subject = $langs->transnoentities('Propal').' '.$propal->ref; + $actiontypecode='AC_PROP'; + $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto.".\n"; + if ($message) + { + $actionmsg.=$langs->transnoentities('MailTopic').": ".$subject."\n"; + $actionmsg.=$langs->transnoentities('TextUsedInTheMessageBody').":\n"; + $actionmsg.=$message; + } + $actionmsg2=$langs->transnoentities('Action'.$actiontypecode); + } + + // Create form object + include_once('../html.formmail.class.php'); + $formmail = new FormMail($db); + + $attachedfiles=$formmail->get_attached_files(); + $filepath = $attachedfiles['paths']; + $filename = $attachedfiles['names']; + $mimetype = $attachedfiles['mimes']; + + // Envoi de la propal + require_once(DOL_DOCUMENT_ROOT.'/lib/CMailFile.class.php'); + $mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc,'',$deliveryreceipt); + if ($mailfile->error) + { + $mesg='
'.$mailfile->error.'
'; + } + else + { + $result=$mailfile->sendfile(); + if ($result) + { + $mesg='
'.$langs->trans('MailSuccessfulySent',$from,$sendto).'.
'; + + $error=0; + + // Initialisation donnees + $propal->sendtoid=$sendtoid; + $propal->actiontypecode=$actiontypecode; + $propal->actionmsg = $actionmsg; + $propal->actionmsg2= $actionmsg2; + $propal->propalrowid=$propal->id; + + // Appel des triggers + include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); + $interface=new Interfaces($db); + $result=$interface->run_triggers('PROPAL_SENTBYMAIL',$propal,$user,$langs,$conf); + if ($result < 0) { $error++; $this->errors=$interface->errors; } + // Fin appel triggers + + if ($error) + { + dolibarr_print_error($db); + } + else + { + // Redirect here + // This avoid sending mail twice if going out and then back to page + Header('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&msg='.urlencode($mesg)); + exit; + } + } + else + { + $langs->load("other"); + $mesg='
'; + if ($mailfile->error) + { + $mesg.=$langs->trans('ErrorFailedToSendMail',$from,$sendto); + $mesg.='
'.$mailfile->error; + } + else + { + $mesg.='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS'; + } + $mesg.='
'; + } + } + } + else + { + $langs->load("other"); + $mesg='
'.$langs->trans('ErrorMailRecipientIsEmpty').' !
'; + dolibarr_syslog('Recipient email is empty'); + } + } + else + { + $langs->load("other"); + $mesg='
'.$langs->trans('ErrorCantReadFile',$file).'
'; + dolibarr_syslog('Failed to read file: '.$file); + } + } + else + { + $langs->load("other"); + $mesg='
'.$langs->trans('ErrorFailedToReadEntity',$langs->trans("Proposal")).'
'; + dolibarr_syslog('Impossible de lire les donnees de la facture. Le fichier propal n\'a peut-etre pas ete genere.'); + } +} + +if ($_GET['action'] == 'commande') +{ + /* + * Cloture de la propale + */ + $propal = new Propal($db); + $propal->fetch($propalid); + $propal->create_commande($user); +} + +if ($_GET['action'] == 'modif' && $user->rights->propale->creer) +{ + /* + * Repasse la propale en mode brouillon + */ + $propal = new Propal($db); + $propal->fetch($_GET['propalid']); + $propal->set_draft($user); + + //reg�n�ration pdf + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); +} + +if ($_POST['action'] == "setabsolutediscount" && $user->rights->propale->creer) +{ + if ($_POST["remise_id"]) + { + $prop = new Propal($db); + $prop->id=$_GET['propalid']; + $ret=$prop->fetch($_GET['propalid']); + if ($ret > 0) + { + $result=$prop->insert_discount($_POST["remise_id"]); + if ($result < 0) + { + $mesg='
'.$prop->error.'
'; + } + } + else + { + dolibarr_print_error($db,$prop->error); + } + } +} + +/* + * Ajout d'une ligne produit dans la propale + */ +if ($_POST['action'] == "addligne" && $user->rights->propale->creer) +{ + if (isset($_POST['qty']) && (($_POST['np_price']!='' && ($_POST['np_desc'] || $_POST['dp_desc'])) || $_POST['idprod'])) + { + $propal = new Propal($db); + $ret=$propal->fetch($_POST['propalid']); + if ($ret < 0) + { + dolibarr_print_error($db,$propal->error); + exit; + } + $ret=$propal->fetch_client(); + + $price_base_type = 'HT'; + + // Ecrase $pu par celui du produit + // Ecrase $desc par celui du produit + // Ecrase $txtva par celui du produit + if ($_POST['idprod']) + { + $prod = new Product($db, $_POST['idprod']); + $prod->fetch($_POST['idprod']); + + $tva_tx = get_default_tva($mysoc,$propal->client,$prod->tva_tx); + $tva_npr = get_default_npr($mysoc,$propal->client,$prod->tva_tx); + + // On defini prix unitaire + if ($conf->global->PRODUIT_MULTIPRICES == 1) + { + $pu_ht = $prod->multiprices[$propal->client->price_level]; + $pu_ttc = $prod->multiprices_ttc[$propal->client->price_level]; + $price_base_type = $prod->multiprices_base_type[$propal->client->price_level]; + } + else + { + $pu_ht = $prod->price; + $pu_ttc = $prod->price_ttc; + $price_base_type = $prod->price_base_type; + } + + // On reevalue prix selon taux tva car taux tva transaction peut etre different + // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur). + if ($tva_tx != $prod->tva_tx) + { + if ($price_base_type != 'HT') + { + $pu_ht = price2num($pu_ttc / (1 + ($tva_tx/100)), 'MU'); + } + else + { + $pu_ttc = price2num($pu_ht * (1 + ($tva_tx/100)), 'MU'); + } + } + + $desc = $prod->description; + $desc.= ($prod->description && $_POST['np_desc']) ? "\n" : ""; + $desc.= $_POST['np_desc']; + } + else + { + $pu_ht=$_POST['np_price']; + $tva_tx=eregi_replace('\*','',$_POST['np_tva_tx']); + $tva_npr=eregi('\*',$_POST['np_tva_tx'])?1:0; + $desc=$_POST['dp_desc']; + } + + $info_bits=0; + if ($tva_npr) $info_bits |= 0x01; + + // Insert line + $result=$propal->addline( + $_POST['propalid'], + $desc, + $pu_ht, + $_POST['qty'], + $tva_tx, + $_POST['idprod'], + $_POST['remise_percent'], + $price_base_type, + $pu_ttc, + $info_bits + ); + + if ($result > 0) + { + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); + } + else + { + $mesg='
'.$propal->error.'
'; + } + + } +} + +/* + * Mise a jour d'une ligne dans la propale + */ +if ($_POST['action'] == 'updateligne' && $user->rights->propale->creer && $_POST["save"] == $langs->trans("Save")) +{ + $propal = new Propal($db); + if (! $propal->fetch($_POST['propalid']) > 0) dolibarr_print_error($db); + + // Define info_bits + $info_bits=0; + if (eregi('\*',$_POST['tva_tx'])) $info_bits |= 0x01; + + // Define vat_rate + $vat_rate=$_POST['tva_tx']; + $vat_rate=eregi_replace('\*','',$vat_rate); + + $result = $propal->updateline($_POST['lineid'], + $_POST['subprice'], + $_POST['qty'], + $_POST['remise_percent'], + $vat_rate, + $_POST['desc'], + 'HT', + $info_bits); + + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); +} + +/* + * Generation doc (depuis lien ou depuis cartouche doc) + */ +if ($_REQUEST['action'] == 'builddoc' && $user->rights->propale->creer) +{ + $propal = new Propal($db); + $propal->fetch($_GET['propalid']); + if ($_REQUEST['model']) + { + $propal->setDocModel($user, $_REQUEST['model']); + } + + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + $result=propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); + if ($result <= 0) + { + dolibarr_print_error($db,$result); + exit; + } + else + { + Header ('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$propal->id.'#builddoc'); + exit; + } +} + +if ($_POST['action'] == 'classin') +{ + $propal = new Propal($db); + $propal->fetch($_GET['propalid']); + $propal->setProject($_POST['projetidp']); +} + +// Conditions de reglement +if ($_POST["action"] == 'setconditions') +{ + $propal = new Propal($db); + $propal->fetch($_REQUEST['propalid']); + $result = $propal->cond_reglement($_POST['cond_reglement_id']); + $_GET['propalid']=$_REQUEST['propalid']; +} + +if ($_REQUEST['action'] == 'setremisepercent' && $user->rights->propale->creer) +{ + $propal = new Propal($db); + $propal->fetch($_REQUEST['propalid']); + $result = $propal->set_remise_percent($user, $_POST['remise_percent']); + $_GET['propalid']=$_REQUEST['propalid']; +} + +if ($_REQUEST['action'] == 'setremiseabsolue' && $user->rights->propale->creer) +{ + $propal = new Propal($db); + $propal->fetch($_REQUEST['propalid']); + $result = $propal->set_remise_absolue($user, $_POST['remise_absolue']); + $_GET['propalid']=$_REQUEST['propalid']; +} + +// Mode de reglement +if ($_POST["action"] == 'setmode') +{ + $propal = new Propal($db); + $propal->fetch($_REQUEST['propalid']); + $result = $propal->mode_reglement($_POST['mode_reglement_id']); + $_GET['propalid']=$_REQUEST['propalid']; +} + +/* + * Ordonnancement des lignes + */ + +if ($_GET['action'] == 'up' && $user->rights->propale->creer) +{ + $propal = new Propal($db, '', $_GET["propalid"]); + $propal->fetch($_GET['propalid']); + $propal->line_up($_GET['rowid']); + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); + Header ('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$_GET["propalid"].'#'.$_GET['rowid']); +} + +if ($_GET['action'] == 'down' && $user->rights->propale->creer) +{ + $propal = new Propal($db, '', $_GET["propalid"]); + $propal->fetch($_GET['propalid']); + $propal->line_down($_GET['rowid']); + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + propale_pdf_create($db, $propal->id, $propal->modelpdf, $outputlangs); + Header ('Location: '.$_SERVER["PHP_SELF"].'?propalid='.$_GET["propalid"].'#'.$_GET['rowid']); + exit; +} + + +/* + * Affichage page + */ + +llxHeader('',$langs->trans('Proposal'),'Proposition'); + +$html = new Form($db); +$formfile = new FormFile($db); + +if ($_GET['propalid'] > 0) +{ + /* + * Affichage fiche propal en mode visu + * + */ + + if ($mesg) print "$mesg
"; + + $propal = new Propal($db); + $propal->fetch($_GET['propalid']); + + $societe = new Societe($db); + $societe->fetch($propal->socid); + + $head = propal_prepare_head($propal); + dolibarr_fiche_head($head, 'comm', $langs->trans('Proposal')); + + /* + * Confirmation de la suppression de la propale + */ + if ($_GET['action'] == 'delete') + { + $html->form_confirm($_SERVER["PHP_SELF"].'?propalid='.$propal->id, $langs->trans('DeleteProp'), $langs->trans('ConfirmDeleteProp'), 'confirm_delete'); + print '
'; + } + + /* + * Confirmation de la suppression d'une ligne produit/service + */ + if ($_GET['action'] == 'ask_deleteline' && $conf->global->PRODUIT_CONFIRM_DELETE_LINE) + { + $html->form_confirm($_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&lineid='.$_GET["lineid"], $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteline'); + print '
'; + } + + /* + * Confirmation de la validation de la propale + */ + if ($_GET['action'] == 'validate') + { + $html->form_confirm($_SERVER["PHP_SELF"].'?propalid='.$propal->id, $langs->trans('ValidateProp'), $langs->trans('ConfirmValidateProp'), 'confirm_validate'); + print '
'; + } + + + /* + * Fiche propal + * + */ + + print ''; + + // Ref + print ''; + + // Ref client + print ''; + print ''; + + $rowspan=8; + + // Societe + print ''; + print ''; + + // Ligne info remises tiers + print ''; + + // Dates + print ''; + + if ($conf->projet->enabled) $rowspan++; + if ($conf->expedition_bon->enabled || $conf->livraison_bon->enabled) + { + if ($conf->global->PROPALE_ADD_SHIPPING_DATE || !$conf->commande->enabled) $rowspan++; + if ($conf->global->PROPALE_ADD_DELIVERY_ADDRESS || !$conf->commande->enabled) $rowspan++; + } + + // Notes + print ''; + print ''; + + // Date fin propal + print ''; + print ''; + print ''; + + + // date de livraison (conditionne sur PROPALE_ADD_SHIPPING_DATE car carac a + // gerer par les commandes et non les propales + if ($conf->expedition_bon->enabled || $conf->livraison_bon->enabled) + { + if ($conf->global->PROPALE_ADD_SHIPPING_DATE || !$conf->commande->enabled) + { + $langs->load('deliveries'); + print ''; + print ''; + } + + // adresse de livraison + if ($conf->global->PROPALE_ADD_DELIVERY_ADDRESS || !$conf->commande->enabled) + { + print ''; + } + } + + // Conditions et modes de reglement + print ''; + print ''; + + // Mode paiement + print ''; + print ''; + + // Projet + if ($conf->projet->enabled) + { + $langs->load("projects"); + print ''; + } + else + { + if ($propal->statut == 0 && $user->rights->propale->creer) + { + if ($_GET['action'] != 'classer' && $propal->brouillon) print ''; + print '
'.$langs->trans('Ref').''.$propal->ref.'
'; + print ''; + if ($_GET['action'] != 'refclient' && $propal->brouillon) print ''; + print '
'; + print $langs->trans('RefCustomer').''; + print ''.img_edit($langs->trans('Modify')).'
'; + print '
'; + if ($user->rights->propale->creer && $_GET['action'] == 'refclient') + { + print '
'; + print ''; + print ''; + print ' '; + print '
'; + } + else + { + print $propal->ref_client; + } + print '
'.$langs->trans('Company').''.$societe->getNomUrl(1).'
'.$langs->trans('Discounts').''; + if ($societe->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$societe->remise_client); + else print $langs->trans("CompanyHasNoRelativeDiscount"); + $absolute_discount=$societe->getAvailableDiscounts('','fk_facture_source IS NULL'); + $absolute_creditnote=$societe->getAvailableDiscounts('','fk_facture_source IS NOT NULL'); + print '. '; + if ($absolute_discount) + { + if ($propal->statut > 0) + { + print $langs->trans("CompanyHasAbsoluteDiscount",price($absolute_discount),$langs->transnoentities("Currency".$conf->monnaie)); + } + else + { + // Remise dispo de type non avoir + $filter='fk_facture_source IS NULL'; + print '
'; + $html->form_remise_dispo($_SERVER["PHP_SELF"].'?propalid='.$propal->id,0,'remise_id',$societe->id,$absolute_discount,$filter); + } + } + if ($absolute_creditnote) + { + print $langs->trans("CompanyHasCreditNote",price($absolute_creditnote),$langs->transnoentities("Currency".$conf->monnaie)).'. '; + } + if (! $absolute_discount && ! $absolute_creditnote) print $langs->trans("CompanyHasNoAbsoluteDiscount").'.'; + print '
'.$langs->trans('Date').''; + print dolibarr_print_date($propal->date,'daytext'); + print ''.$langs->trans('NotePublic').' :
'. nl2br($propal->note_public).'
'; + print ''; + if ($_GET['action'] != 'editecheance' && $propal->brouillon) print ''; + print '
'; + print $langs->trans('DateEndPropal'); + print 'id.'">'.img_edit($langs->trans('SetConditions'),1).'
'; + print '
'; + if ($propal->brouillon && $_GET['action'] == 'editecheance') + { + print '
'; + print ''; + $html->select_date($propal->fin_validite,'ech','','','',"editecheance"); + print ''; + print '
'; + } + else + { + if ($propal->fin_validite) + { + print dolibarr_print_date($propal->fin_validite,'daytext'); + if ($propal->statut == 1 && $propal->fin_validite < (time() - $conf->propal->cloture->warning_delay)) print img_warning($langs->trans("Late")); + } + else + { + print ' '; + } + } + print '
'; + print ''; + if ($_GET['action'] != 'editdate_livraison' && $propal->brouillon) print ''; + print '
'; + print $langs->trans('DeliveryDate'); + print 'id.'">'.img_edit($langs->trans('SetDeliveryDate'),1).'
'; + print '
'; + if ($_GET['action'] == 'editdate_livraison') + { + print '
'; + print ''; + $html->select_date($propal->date_livraison,'liv_','','','',"editdate_livraison"); + print ''; + print '
'; + } + else + { + print dolibarr_print_date($propal->date_livraison,'%a %d %B %Y'); + } + print '
'; + print ''; + + if ($_GET['action'] != 'editdelivery_adress' && $propal->brouillon) print ''; + print '
'; + print $langs->trans('DeliveryAddress'); + print 'socid.'&propalid='.$propal->id.'">'.img_edit($langs->trans('SetDeliveryAddress'),1).'
'; + print '
'; + + if ($_GET['action'] == 'editdelivery_adress') + { + $html->form_adresse_livraison($_SERVER['PHP_SELF'].'?propalid='.$propal->id,$propal->adresse_livraison_id,$_GET['socid'],'adresse_livraison_id','propal',$propal->id); + } + else + { + $html->form_adresse_livraison($_SERVER['PHP_SELF'].'?propalid='.$propal->id,$propal->adresse_livraison_id,$_GET['socid'],'none','propal',$propal->id); + } + print '
'; + print ''; + if ($_GET['action'] != 'editconditions' && $propal->brouillon) print ''; + print '
'; + print $langs->trans('PaymentConditionsShort'); + print 'id.'">'.img_edit($langs->trans('SetConditions'),1).'
'; + print '
'; + if ($_GET['action'] == 'editconditions') + { + $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?propalid='.$propal->id,$propal->cond_reglement_id,'cond_reglement_id'); + } + else + { + $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?propalid='.$propal->id,$propal->cond_reglement_id,'none'); + } + print '
'; + print ''; + if ($_GET['action'] != 'editmode' && $propal->brouillon) print ''; + print '
'; + print $langs->trans('PaymentMode'); + print 'id.'">'.img_edit($langs->trans('SetMode'),1).'
'; + print '
'; + if ($_GET['action'] == 'editmode') + { + $html->form_modes_reglement($_SERVER['PHP_SELF'].'?propalid='.$propal->id,$propal->mode_reglement_id,'mode_reglement_id'); + } + else + { + $html->form_modes_reglement($_SERVER['PHP_SELF'].'?propalid='.$propal->id,$propal->mode_reglement_id,'none'); + } + print '
'; + print ''; + $numprojet = $societe->has_projects(); + if (! $numprojet) + { + print '
'; + print $langs->trans('Project').'
'; + print '
'; + print $langs->trans("NoProject").''; + print 'id.'&action=create>'.$langs->trans('AddProject').''; + print ''.img_edit($langs->trans('SetProject')).'
'; + print ''; + if ($_GET['action'] == 'classer') + { + $html->form_project($_SERVER['PHP_SELF'].'?propalid='.$propal->id, $propal->socid, $propal->projetidp, 'projetidp'); + } + else + { + $html->form_project($_SERVER['PHP_SELF'].'?propalid='.$propal->id, $propal->socid, $propal->projetidp, 'none'); + } + print ''; + } + else + { + print ''; + if (!empty($propal->projetidp)) + { + print ''; + $proj = new Project($db); + $proj->fetch($propal->projetidp); + print ''; + print $proj->title; + print ''; + print ''; + } + else { + print ' '; + } + } + } + print ''; + } + + // Amount HT + print ''.$langs->trans('AmountHT').''; + print ''.price($propal->total_ht).''; + print ''.$langs->trans("Currency".$conf->monnaie).''; + + // Amount VAT + print ''.$langs->trans('AmountVAT').''; + print ''.price($propal->total_tva).''; + print ''.$langs->trans("Currency".$conf->monnaie).''; + + // Amount TTC + print ''.$langs->trans('AmountTTC').''; + print ''.price($propal->total_ttc).''; + print ''.$langs->trans("Currency".$conf->monnaie).''; + + // Statut + print ''.$langs->trans('Status').''.$propal->getLibStatut(4).''; + print '
'; + + /* + * Lignes de propale + */ + print ''; + + $sql = 'SELECT pt.rowid, pt.description, pt.fk_product, pt.fk_remise_except,'; + $sql.= ' pt.qty, pt.tva_tx, pt.remise_percent, pt.subprice, pt.info_bits,'; + $sql.= ' pt.total_ht, pt.total_tva, pt.total_ttc, pt.marge_tx, pt.marque_tx, pt.pa_ht, pt.special_code,'; + $sql.= ' p.label as product, p.ref, p.fk_product_type, p.rowid as prodid,'; + $sql.= ' p.description as product_desc'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product=p.rowid'; + $sql.= ' WHERE pt.fk_propal = '.$propal->id; + $sql.= ' ORDER BY pt.rang ASC, pt.rowid'; + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + $i = 0; + + if ($num) + { + print ''; + print ''; + if ($conf->global->PRODUIT_USE_MARKUP) + { + print ''; + } + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print "\n"; + } + $var=true; + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + + // Ligne en mode visu + if ($_GET['action'] != 'editline' || $_GET['lineid'] != $objp->rowid) + { + print ''; + + // Produit + if ($objp->fk_product > 0) + { + print ''; + } + else + { + print '\n"; + } + + if ($conf->global->PRODUIT_USE_MARKUP && $conf->use_javascript_ajax) + { + $formMarkup = ''."\n"; + $formMarkup.= '
'.$langs->trans('Description').''.$langs->trans('Markup').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').''.$langs->trans('AmountHT').' 
'; + print ''; // ancre pour retourner sur la ligne; + + // Affiche ligne produit + $text = ''; + if ($objp->fk_product_type==1) $text.= img_object($langs->trans('ShowService'),'service'); + else $text.= img_object($langs->trans('ShowProduct'),'product'); + $text.= ' '.$objp->ref.''; + $text.= ' - '.$objp->product; + $description=($conf->global->PRODUIT_DESC_IN_FORM?'':dol_htmlentitiesbr($objp->description)); + print $html->textwithtooltip($text,$description,3,'','',$i); + print_date_range($objp->date_start,$objp->date_end); + if ($conf->global->PRODUIT_DESC_IN_FORM) + { + print ($objp->description && $objp->description!=$objp->product)?'
'.dol_htmlentitiesbr($objp->description):''; + } + + print '
'; + print ''; // ancre pour retourner sur la ligne + if (($objp->info_bits & 2) == 2) + { + print ''; + print img_object($langs->trans("ShowReduc"),'reduc').' '.$langs->trans("Discount"); + print ''; + if ($objp->description) + { + if ($objp->description == '(CREDIT_NOTE)') + { + $discount=new DiscountAbsolute($db); + $discount->fetch($objp->fk_remise_except); + print ' - '.$langs->transnoentities("DiscountFromCreditNote",$discount->getNomUrl(0)); + } + else + { + print ' - '.nl2br($objp->description); + } + } + } + else + { + print nl2br($objp->description); + print_date_range($objp->date_start,$objp->date_end); + } + print "
'."\n"; + if ($objp->fk_product > 0) + { + $formMarkup.= ''."\n"; + $formMarkup.= ''."\n"; + $formMarkup.= ''."\n"; + } + $formMarkup.= ''."\n"; + $formMarkup.= ''."\n"; + $formMarkup.= ''."\n"; + $formMarkup.= ''."\n"; + $formMarkup.= ''."\n"; + $formMarkup.= ''."\n"; + //$formMarkup.= ''."\n"; + $formMarkup.= ''."\n"; + $formMarkup.= ''."\n"; + $formMarkup.= ''."\n"; + $formMarkup.= '
 
 '.$langs->trans('SupplierPrice').''.$html->select_product_fourn_price($objp->fk_product,'productfournpriceid').'
 
 '.$langs->trans('PurchasePrice').' '.$langs->trans('HT').'
 '.$langs->trans('MarkupRate').'
 '.$langs->trans('SellingPrice').' '.$langs->trans('HT').'
 '.$langs->trans('CashFlow').' '.$langs->trans('HT').'
'."\n"; + $formMarkup.= ''."\n"; + //$formMarkup.= '   '."\n"; + $formMarkup.= '
'."\n"; + $formMarkup.= ajax_updaterWithID("rate".$i,"markup","sellingprice_ht".$i,"/product/ajaxproducts.php","&count=".$i,"working")."\n"; + + + print ''."\n"; + + print ''."\n"; + + + print ''; + print ''; + print ''; + print '
'; + if (($objp->info_bits & 2) == 2) + { + // Ligne remise predefinie, on ne permet pas modif + } + else + { + $picto = ''; + $picto.= img_calc(); + $picto.= ''; + print $html->textwithtooltip($picto,$langs->trans("ToCalculateMarkup"),3,'','',$i); + } + print ''.vatrate($objp->marge_tx).'%
'; + print ''; + } + + // VAT Rate + print ''.vatrate($objp->tva_tx,'%',$objp->info_bits).''; + + // U.P HT + print ''.price($objp->subprice)."\n"; + + // Qty + print ''; + if ((($objp->info_bits & 2) != 2) && $objp->special_code != 3) + { + print $objp->qty; + } + else print ' '; + print ''; + + // Remise % + if ($objp->remise_percent > 0 && $objp->special_code != 3) + { + print ''.dolibarr_print_reduction($objp->remise_percent)."\n"; + } + else + { + print ' '; + } + + // Montant total HT + if ($objp->special_code == 3) + { + // Si ligne en option + print ''.$langs->trans('Option').''; + } + else + { + print ''.price($objp->total_ht)."\n"; + } + + // Icone d'edition et suppression + if ($propal->statut == 0 && $user->rights->propale->creer) + { + print ''; + if (($objp->info_bits & 2) == 2) + { + // Ligne remise predefinie, on permet pas modif + } + else + { + print 'rowid.'#'.$objp->rowid.'">'; + print img_edit(); + print ''; + } + print ''; + print ''; + if ($conf->global->PRODUIT_CONFIRM_DELETE_LINE) + { + if ($conf->use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) + { + $url = $_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&lineid='.$objp->rowid.'&action=confirm_deleteline&confirm=yes'; + print 'trans("No").'\',\'deleteline'.$i.'\')">'; + } + else + { + print 'rowid.'">'; + } + } + else + { + print 'rowid.'">'; + } + print img_delete(); + print ''; + if ($num > 1) + { + print ''; + if ($i > 0) + { + print 'rowid.'">'; + print img_up(); + print ''; + } + if ($i < $num-1) + { + print 'rowid.'">'; + print img_down(); + print ''; + } + print ''; + } + } + else + { + print ' '; + } + + print ''; + } + + // Ligne en mode update + if ($propal->statut == 0 && $_GET["action"] == 'editline' && $user->rights->propale->creer && $_GET["lineid"] == $objp->rowid) + { + print '
rowid.'" method="post">'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; // ancre pour retourner sur la ligne + if ($objp->fk_product > 0) + { + print ''; + if ($objp->fk_product_type==1) print img_object($langs->trans('ShowService'),'service'); + else print img_object($langs->trans('ShowProduct'),'product'); + print ' '.$objp->ref.''; + print ' - '.nl2br($objp->product); + print '
'; + } + if ($_GET["action"] == 'editline') + { + // editeur wysiwyg + if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) + { + require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); + $doleditor=new DolEditor('desc',$objp->description,164,'dolibarr_details'); + $doleditor->Create(); + } + else + { + print ''; + } + } + print ''; + if ($conf->global->PRODUIT_USE_MARKUP) + { + print ''.vatrate($objp->marge_tx).'%'; + } + print ''; + print $html->select_tva('tva_tx',$objp->tva_tx,$mysoc,$societe,'',$objp->info_bits); + print ''; + print ''; + print ''; + if (($objp->info_bits & 2) != 2) + { + print ''; + } + else print ' '; + print ''; + print ''; + if (($objp->info_bits & 2) != 2) + { + print '%'; + } + else print ' '; + print ''; + print ''; + print '
'; + print '' . "\n"; + /* + if ($conf->service->enabled) + { + print ""; + print 'Si produit de type service a duree limitee: Du '; + print $html->select_date($objp->date_start,"date_start",0,0,$objp->date_start?0:1); + print ' au '; + print $html->select_date($objp->date_end,"date_end",0,0,$objp->date_end?0:1); + print ''; + print '' . "\n"; + } + */ + print "
\n"; + } + + $i++; + } + + $db->free($resql); + } + else + { + dolibarr_print_error($db); + } + + /* + * Ajouter une ligne + */ + if ($propal->statut == 0 && $user->rights->propale->creer && $_GET["action"] <> 'editline') + { + if ($conf->global->PRODUIT_USE_MARKUP) $colspan = 'colspan="2"'; + print ''; + print ''; + print ''; // ancre + print $langs->trans('Description').''; + print ''.$langs->trans('VAT').''; + print ''.$langs->trans('PriceUHT').''; + print ''.$langs->trans('Qty').''; + print ''.$langs->trans('ReductionShort').''; + print ' '; + print "\n"; + + // Ajout produit produits/services personnalises + print '
'; + print ''; + print ''; + + $var=true; + + print '\n"; + print ''; + // editeur wysiwyg + if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) + { + require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); + $doleditor=new DolEditor('dp_desc','',100,'dolibarr_details'); + $doleditor->Create(); + } + else + { + print ''; + } + print ''; + print ''; + if($societe->tva_assuj == "0") + { + print '0'; + } + else + { + $html->select_tva('np_tva_tx', $conf->defaulttx, $mysoc, $societe); + } + print "\n"; + print ''; + print ''; + print '%'; + print ''; + print ''; + + print '
'; + + // Ajout de produits/services predefinis + if ($conf->produit->enabled) + { + if ($conf->global->PRODUIT_USE_MARKUP) + { + $colspan = 'colspan="4"'; + } + else + { + $colspan = 'colspan="3"'; + } + print ''; + print ''; + if ($conf->service->enabled) + { + print $langs->trans('RecordedProductsAndServices'); + } + else + { + print $langs->trans('RecordedProducts'); + } + print ''; + print ''.$langs->trans('Qty').''; + print ''.$langs->trans('ReductionShort').''; + print ' '; + print ''; + print '
'; + print ''; + print ''; + + $var=!$var; + + print ''; + print ''; + // multiprix + if($conf->global->PRODUIT_MULTIPRICES == 1) + { + $html->select_produits('','idprod','',$conf->produit->limit_size,$societe->price_level); + } + else + { + $html->select_produits('','idprod','',$conf->produit->limit_size); + } + if (! $conf->global->PRODUIT_USE_SEARCH_TO_SELECT) print '
'; + + // editeur wysiwyg + if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) + { + require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); + $doleditor=new DolEditor('np_desc','',100,'dolibarr_details'); + $doleditor->Create(); + } + else + { + print ''; + } + + print ''; + print ''; + print '%'; + + print ''; + print ''."\n"; + + print '
'; + } + } + + print ''; + + print ''; + print "\n"; + + /* + * Formulaire cloture (signe ou non) + */ + $form_close = '
'; + $form_close.= ''; + $form_close.= ''; + $form_close.= ''; + $form_close.= ''; + $form_close.= '
'.$langs->trans('Note').'
'.$langs->trans("CloseAs").''; + $form_close.= ''; + $form_close.= ''; + $form_close.= '
'; + $form_close.= ''; + if ($conf->use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) + { + $form_close.= '   '; + } + else + { + $form_close.= '   '; + } + $form_close.= '
'; + + if ($_GET['action'] == 'statut') + { + print $form_close; + } + + + /* + * Boutons Actions + */ + if ($_GET['action'] != 'presend') + { + print '
'; + + if ($_GET['action'] != 'statut' && $_GET['action'] <> 'editline') + { + + // Valid + if ($propal->statut == 0 && $propal->total_ttc > 0 && $user->rights->propale->valider) + { + print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) + { + $url = $_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&action=confirm_validate&confirm=yes'; + print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.dol_escape_js($langs->trans('ConfirmValidateProp')).'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'validate\')"'; + } + else + { + print 'href="'.$_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&action=validate"'; + } + print '>'.$langs->trans('Validate').''; + } + + // Edit + if ($propal->statut == 1 && $user->rights->propale->creer) + { + print ''.$langs->trans('Modify').''; + } + + // Send + if ($propal->statut == 1 && $user->rights->propale->envoyer) + { + $propref = sanitize_string($propal->ref); + $file = $conf->propal->dir_output . '/'.$propref.'/'.$propref.'.pdf'; + if (file_exists($file)) + { + print ''.$langs->trans('SendByMail').''; + } + } + + // Close + if ($propal->statut == 1 && $user->rights->propale->cloturer) + { + print ''."\n"; + + print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) + { + print 'href="#" onClick="dialogInfo($(\'confirm_close\').innerHTML)"'."\n"; + } + else + { + print 'href="'.$_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&action=statut"'; + } + print '>'.$langs->trans('Close').''; + } + + // Delete + if ($propal->statut == 0 && $user->rights->propale->supprimer) + { + print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) + { + $url = $_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&action=confirm_delete&confirm=yes'; + print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.$langs->trans('ConfirmDeleteProp').'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'delete\')"'; + } + else + { + print 'href="'.$_SERVER["PHP_SELF"].'?propalid='.$propal->id.'&action=delete"'; + } + print '>'.$langs->trans('Delete').''; + } + + } + + print '
'; + print "
\n"; + } + + if ($_GET['action'] != 'presend') + { + print '
'; + print ''; // ancre + + + /* + * Documents generes + */ + $filename=sanitize_string($propal->ref); + $filedir=$conf->propal->dir_output . "/" . sanitize_string($propal->ref); + $urlsource=$_SERVER["PHP_SELF"]."?propalid=".$propal->id; + $genallowed=$user->rights->propale->creer; + $delallowed=$user->rights->propale->supprimer; + + $var=true; + + $somethingshown=$formfile->show_documents('propal',$filename,$filedir,$urlsource,$genallowed,$delallowed,$propal->modelpdf); + + + /* + * Commandes rattachees + */ + if($conf->commande->enabled) + { + $propal->loadOrders(); + $coms = $propal->commandes; + if (sizeof($coms) > 0) + { + if ($somethingshown) { print '
'; $somethingshown=1; } + print_titre($langs->trans('RelatedOrders')); + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $var=true; + for ($i = 0 ; $i < sizeof($coms) ; $i++) + { + $var=!$var; + print '\n"; + print ''; + print ''; + print ''; + print "\n"; + } + print '
'.$langs->trans("Ref").''.$langs->trans("Date").''.$langs->trans("Price").''.$langs->trans("Status").'
'; + print ''.img_object($langs->trans("ShowOrder"),"order").' '.$coms[$i]->ref."'.dolibarr_print_date($coms[$i]->date,'day').''.price($coms[$i]->total_ttc).''.$coms[$i]->getLibStatut(3).'
'; + } + } + + print '
'; + + // List of actions on element + include_once(DOL_DOCUMENT_ROOT.'/html.formactions.class.php'); + $formactions=new FormActions($db); + $somethingshown=$formactions->showactions($propal,'propal',$socid); + + print '
'; + } + + + /* + * Action presend + * + */ + if ($_GET['action'] == 'presend') + { + $ref = sanitize_string($propal->ref); + $file = $conf->propal->dir_output . '/' . $ref . '/' . $ref . '.pdf'; + + print '
'; + print_titre($langs->trans('SendPropalByMail')); + + $liste[0]=" "; + foreach ($societe->thirdparty_and_contact_email_array() as $key=>$value) + { + $liste[$key]=$value; + } + + // Create form object + include_once('../html.formmail.class.php'); + $formmail = new FormMail($db); + $formmail->fromtype = 'user'; + $formmail->fromid = $user->id; + $formmail->fromname = $user->fullname; + $formmail->frommail = $user->email; + $formmail->withfrom=1; + $formmail->withto=$liste; + $formmail->withtocc=1; + $formmail->withtopic=$langs->trans('SendPropalRef','__PROPREF__'); + $formmail->withfile=2; + $formmail->withbody=1; + $formmail->withdeliveryreceipt=1; + $formmail->withcancel=1; + // Tableau des substitutions + $formmail->substit['__PROPREF__']=$propal->ref; + // Tableau des parametres complementaires + $formmail->param['action']='send'; + $formmail->param['models']='propal_send'; + $formmail->param['propalid']=$propal->id; + $formmail->param['returnurl']=DOL_URL_ROOT.'/comm/propal.php?propalid='.$propal->id; + + // Init list of files + if (! empty($_REQUEST["mode"]) && $_REQUEST["mode"]=='init') + { + $formmail->clear_attached_files(); + $formmail->add_attached_files($file,$propal->ref.'.pdf','application/pdf'); + } + + $formmail->show_form(); + + print '
'; + } + +} +else +{ + /**************************************************************************** + * * + * Mode Liste des propales * + * * + ****************************************************************************/ + + $sortorder=$_GET['sortorder']; + $sortfield=$_GET['sortfield']; + $page=$_GET['page']; + $viewstatut=addslashes($_GET['viewstatut']); + $propal_statut = addslashes($_GET['propal_statut']); + if($propal_statut != '') + $viewstatut=$propal_statut; + + if (! $sortfield) $sortfield='p.datep'; + if (! $sortorder) $sortorder='DESC'; + $limit = $conf->liste_limit; + $offset = $limit * $page ; + $pageprev = $page - 1; + $pagenext = $page + 1; + + $sql = 'SELECT s.nom, s.rowid, s.client, '; + $sql.= 'p.rowid as propalid, p.total_ht, p.ref, p.fk_statut, '.$db->pdate('p.datep').' as dp,'.$db->pdate('p.fin_validite').' as dfv'; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user"; + $sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s, '.MAIN_DB_PREFIX.'propal as p'; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + if ($sall) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'propaldet as pd ON p.rowid=pd.fk_propal'; + $sql.= ' WHERE p.fk_soc = s.rowid'; + + if (!$user->rights->societe->client->voir && !$socid) //restriction + { + $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; + } + if (!empty($_GET['search_ref'])) + { + $sql .= " AND p.ref LIKE '%".addslashes($_GET['search_ref'])."%'"; + } + if (!empty($_GET['search_societe'])) + { + $sql .= " AND s.nom LIKE '%".addslashes($_GET['search_societe'])."%'"; + } + if (!empty($_GET['search_montant_ht'])) + { + $sql .= " AND p.total_ht='".addslashes($_GET['search_montant_ht'])."'"; + } + if ($sall) $sql.= " AND (s.nom like '%".addslashes($sall)."%' OR p.note like '%".addslashes($sall)."%' OR pd.description like '%".addslashes($sall)."%')"; + if ($socid) $sql .= ' AND s.rowid = '.$socid; + if ($viewstatut <> '') + { + $sql .= ' AND p.fk_statut in ('.$viewstatut.')'; + } + if ($month > 0) + { + if ($year > 0) + $sql .= " AND date_format(p.datep, '%Y-%m') = '$year-$month'"; + else + $sql .= " AND date_format(p.datep, '%m') = '$month'"; + } + if ($year > 0) + { + $sql .= " AND date_format(p.datep, '%Y') = $year"; + } + if (strlen($_POST['sf_ref']) > 0) + { + $sql .= " AND p.ref like '%".addslashes($_POST["sf_ref"]) . "%'"; + } + + $sql .= ' ORDER BY '.$sortfield.' '.$sortorder.', p.ref DESC'; + $sql .= $db->plimit($limit + 1,$offset); + $result=$db->query($sql); + + if ($result) + { + $num = $db->num_rows($result); + $propalstatic=new Propal($db); + print_barre_liste($langs->trans('ListOfProposals'), $page,'propal.php','&socid='.$socid.'&viewstatut='.$viewstatut,$sortfield,$sortorder,'',$num); + $i = 0; + print ''; + print ''; + print_liste_field_titre($langs->trans('Ref'),$_SERVER["PHP_SELF"],'p.ref','','&socid='.$socid.'&viewstatut='.$viewstatut,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('Company'),$_SERVER["PHP_SELF"],'s.nom','','&socid='.$socid.'&viewstatut='.$viewstatut,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('Date'),$_SERVER["PHP_SELF"],'p.datep','','&socid='.$socid.'&viewstatut='.$viewstatut, 'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('DateEndPropalShort'),$_SERVER["PHP_SELF"],'dfv','','&socid='.$socid.'&viewstatut='.$viewstatut, 'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('Price'),$_SERVER["PHP_SELF"],'p.total_ht','','&socid='.$socid.'&viewstatut='.$viewstatut, 'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('Status'),$_SERVER["PHP_SELF"],'p.fk_statut','','&socid='.$socid.'&viewstatut='.$viewstatut,'align="right"',$sortfield,$sortorder); + print ''; + print "\n"; + // Lignes des champs de filtre + print ''; + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print "\n"; + print ''; + + $var=true; + + while ($i < min($num,$limit)) + { + $objp = $db->fetch_object($result); + $now = time(); + $var=!$var; + print ''; + print ''; + + // Date propale + print '\n"; + + // Date fin validite + if ($objp->dfv) + { + print ''; + } + else + { + print ''; + } + + print '\n"; + $propal=New Propal($db); + print '\n"; + print ""; + print "\n"; + + $total = $total + $objp->total_ht; + $subtotal = $subtotal + $objp->total_ht; + + $i++; + } + print '
 
'; + print ''; + print ''; + print ''; + print ''; + print $langs->trans('Month').': '; + print ' '.$langs->trans('Year').': '; + $max_year = date("Y"); + $syear = $year; + if($syear == '') + $syear = date("Y"); + $html->select_year($syear,'year',1, '', $max_year); + print ' '; + print ''; + print ''; + $html->select_propal_statut($viewstatut); + print ''; + print '
'; + + $propalstatic->id=$objp->propalid; + $propalstatic->ref=$objp->ref; + + print ''; + print ''; + + print ''; + + print '
'; + print $propalstatic->getNomUrl(1); + print ''; + if ($objp->fk_statut == 1 && $objp->dfv < (time() - $conf->propal->cloture->warning_delay)) print img_warning($langs->trans("Late")); + print ''; + + $filename=sanitize_string($objp->ref); + $filedir=$conf->propal->dir_output . '/' . sanitize_string($objp->ref); + $urlsource=$_SERVER['PHP_SELF'].'?propalid='.$objp->propalid; + $formfile->show_documents('propal',$filename,$filedir,$urlsource,'','','','','',1); + + print '
'; + + if ($objp->client == 1) + { + $url = DOL_URL_ROOT.'/comm/fiche.php?socid='.$objp->rowid; + } + else + { + $url = DOL_URL_ROOT.'/comm/prospect/fiche.php?socid='.$objp->rowid; + } + + // Societe + print '
'.img_object($langs->trans('ShowCompany'),'company').' '.$objp->nom.''; + $y = strftime('%Y',$objp->dp); + $m = strftime('%m',$objp->dp); + + print strftime('%d',$objp->dp)."\n"; + print ' '; + print dolibarr_print_date($objp->dp,'%b')."\n"; + print ' '; + print strftime('%Y',$objp->dp)."'.dolibarr_print_date($objp->dfv); + print ' '.price($objp->total_ht)."'.$propal->LibStatut($objp->fk_statut,5)." 
'; + $db->free($result); + } + else + { + dolibarr_print_error($db); + } +} +$db->close(); + +llxFooter('$Date$ - $Revision$'); + +?> diff --git a/htdocs/comm/remise.php b/htdocs/comm/remise.php index 50d87510a53..cf878e209b0 100644 --- a/htdocs/comm/remise.php +++ b/htdocs/comm/remise.php @@ -1,184 +1,176 @@ - - * Copyright (C) 2004-2006 Laurent Destailleur - * Copyright (C) 2008 Raphael Bertrand (Resultic) - * - * 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/comm/remise.php - \ingroup commercial - \brief Onglet remise de la societe - \version $Revision$ -*/ - -require_once("./pre.inc.php"); -require_once(DOL_DOCUMENT_ROOT."/lib/company.lib.php"); -require_once(DOL_DOCUMENT_ROOT."/contact.class.php"); - -$langs->load("companies"); -$langs->load("orders"); -$langs->load("bills"); - - -if ($_POST["action"] == 'setremise') -{ - - // Define remise - if ($_POST['remise']>0) - $remise=$_POST['remise']; - else - $remise=0; - - $soc = New Societe($db); - $soc->fetch($_GET["id"]); - $result=$soc->set_remise_client($remise,$_POST["note"],$user); - - if ($result > 0) - { - Header("Location: remise.php?id=".$_GET["id"]); - exit; - } - else - { - $errmesg=$soc->error; - } -} - - -llxHeader(); - -$_socid = $_GET["id"]; - - -// Sécurité si un client essaye d'accéder à une autre fiche que la sienne -if ($user->societe_id > 0) -{ - $_socid = $user->societe_id; -} - - -/********************************************************************************* - * - * Mode fiche - * - *********************************************************************************/ -if ($_socid > 0) -{ - // On recupere les donnees societes par l'objet - $objsoc = new Societe($db); - $objsoc->id=$_socid; - $objsoc->fetch($_socid,$to); - - if ($errmesg) - { - print '
'.$errmesg.'

'; - } - - /* - * Affichage onglets - */ - $head = societe_prepare_head($objsoc); - - dolibarr_fiche_head($head, 'relativediscount', $objsoc->nom); - - - /* - * - * - */ - print '
'; - print ''; - print ''; - print '"; - print '
'; - print ''; - - // Remise - print '"; - - // Nouvelle valeur - print ''; - - // Motif/Note - print ''; - - print ''; - - print "
'; - print $langs->trans("CustomerRelativeDiscount").''.$objsoc->remise_client."%
'; - print $langs->trans("NewValue").'%
'; - print $langs->trans("NoteReason").'
"; - print "
'; - print "
"; - - print "\n"; - print '
'; - - - /* - * Liste de l'historique des avoirs - */ - $sql = "SELECT rc.rowid,rc.remise_client,rc.note,".$db->pdate("rc.datec")." as dc,"; - $sql.= " u.login, u.rowid as user_id"; - $sql.= " FROM ".MAIN_DB_PREFIX."societe_remise as rc, ".MAIN_DB_PREFIX."user as u"; - $sql.= " WHERE rc.fk_soc =". $objsoc->id; - $sql.= " AND u.rowid = rc.fk_user_author"; - $sql.= " ORDER BY rc.datec DESC"; - - $resql=$db->query($sql); - if ($resql) - { - print ''; - $tag = !$tag; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - $i = 0 ; - $num = $db->num_rows($resql); - - while ($i < $num ) - { - $obj = $db->fetch_object($resql); - $tag = !$tag; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - $i++; - } - $db->free($resql); - print "
'.$langs->trans("Date").''.$langs->trans("CustomerRelativeDiscountShort").''.$langs->trans("NoteReason").''.$langs->trans("User").'
'.dolibarr_print_date($obj->dc,"dayhour").''.$obj->remise_client.' %'.$obj->note.''.img_object($langs->trans("ShowUser"),'user').' '.$obj->login.'
"; - } - else - { - dolibarr_print_error($db); - } - -} - -$db->close(); - -llxFooter('$Date$ - $Revision$'); -?> + + * Copyright (C) 2004-2006 Laurent Destailleur + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * 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/comm/remise.php + \ingroup commercial + \brief Onglet remise de la societe + \version $Revision$ +*/ + +require_once("./pre.inc.php"); +require_once(DOL_DOCUMENT_ROOT."/lib/company.lib.php"); +require_once(DOL_DOCUMENT_ROOT."/contact.class.php"); + +$langs->load("companies"); +$langs->load("orders"); +$langs->load("bills"); + + +if ($_POST["action"] == 'setremise') +{ + $soc = New Societe($db); + $soc->fetch($_GET["id"]); + $result=$soc->set_remise_client($_POST["remise"],$_POST["note"],$user); + + if ($result > 0) + { + Header("Location: remise.php?id=".$_GET["id"]); + exit; + } + else + { + $errmesg=$soc->error; + } +} + + +llxHeader(); + +$_socid = $_GET["id"]; + + +// Sécurité si un client essaye d'accéder à une autre fiche que la sienne +if ($user->societe_id > 0) +{ + $_socid = $user->societe_id; +} + + +/********************************************************************************* + * + * Mode fiche + * + *********************************************************************************/ +if ($_socid > 0) +{ + // On recupere les donnees societes par l'objet + $objsoc = new Societe($db); + $objsoc->id=$_socid; + $objsoc->fetch($_socid,$to); + + if ($errmesg) + { + print '
'.$errmesg.'

'; + } + + /* + * Affichage onglets + */ + $head = societe_prepare_head($objsoc); + + dolibarr_fiche_head($head, 'relativediscount', $objsoc->nom); + + + /* + * + * + */ + print '
'; + print ''; + print ''; + print '"; + print '
'; + print ''; + + // Remise + print '"; + + // Nouvelle valeur + print ''; + + // Motif/Note + print ''; + + print ''; + + print "
'; + print $langs->trans("CustomerRelativeDiscount").''.$objsoc->remise_client."%
'; + print $langs->trans("NewValue").'%
'; + print $langs->trans("NoteReason").'
"; + print "
'; + print "
"; + + print "\n"; + print '
'; + + + /* + * Liste de l'historique des avoirs + */ + $sql = "SELECT rc.rowid,rc.remise_client,rc.note,".$db->pdate("rc.datec")." as dc,"; + $sql.= " u.login, u.rowid as user_id"; + $sql.= " FROM ".MAIN_DB_PREFIX."societe_remise as rc, ".MAIN_DB_PREFIX."user as u"; + $sql.= " WHERE rc.fk_soc =". $objsoc->id; + $sql.= " AND u.rowid = rc.fk_user_author"; + $sql.= " ORDER BY rc.datec DESC"; + + $resql=$db->query($sql); + if ($resql) + { + print ''; + $tag = !$tag; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $i = 0 ; + $num = $db->num_rows($resql); + + while ($i < $num ) + { + $obj = $db->fetch_object($resql); + $tag = !$tag; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + $db->free($resql); + print "
'.$langs->trans("Date").''.$langs->trans("CustomerRelativeDiscountShort").''.$langs->trans("NoteReason").''.$langs->trans("User").'
'.dolibarr_print_date($obj->dc,"dayhour").''.$obj->remise_client.' %'.$obj->note.''.img_object($langs->trans("ShowUser"),'user').' '.$obj->login.'
"; + } + else + { + dolibarr_print_error($db); + } + +} + +$db->close(); + +llxFooter('$Date$ - $Revision$'); +?> diff --git a/htdocs/commande/fiche.php b/htdocs/commande/fiche.php index e9921ffa390..d268b87d7e6 100644 --- a/htdocs/commande/fiche.php +++ b/htdocs/commande/fiche.php @@ -1,2026 +1,2007 @@ - - * Copyright (C) 2004-2008 Laurent Destailleur - * Copyright (C) 2005 Marc Barilley / Ocebo - * Copyright (C) 2005-2008 Regis Houssin - * Copyright (C) 2006 Andre Cianfarani - * Copyright (C) 2008 Raphael Bertrand (Resultic) - * - * 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. - */ - -/** - * \file htdocs/commande/fiche.php - * \ingroup commande - * \brief Fiche commande client - * \version $Id$ - */ - -require('./pre.inc.php'); -require_once(DOL_DOCUMENT_ROOT."/html.formfile.class.php"); -require_once(DOL_DOCUMENT_ROOT ."/includes/modules/commande/modules_commande.php"); -require_once(DOL_DOCUMENT_ROOT.'/commande/commande.class.php'); -require_once(DOL_DOCUMENT_ROOT.'/actioncomm.class.php'); -require_once(DOL_DOCUMENT_ROOT."/lib/order.lib.php"); -if ($conf->projet->enabled) require_once(DOL_DOCUMENT_ROOT.'/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.'/propal.class.php'); - -$langs->load('orders'); -$langs->load('sendings'); -$langs->load('companies'); -$langs->load('bills'); -$langs->load('propal'); -$langs->load('deliveries'); -$langs->load('products'); - -if (!$user->rights->commande->lire) accessforbidden(); - - -// Securite acces client -$socid=0; -if ($user->societe_id > 0) -{ - $socid = $user->societe_id; -} -if ($user->societe_id >0 && isset($_GET["id"]) && $_GET["id"]>0) -{ - $commande = new Commande($db); - $commande->fetch((int)$_GET['id']); - if ($user->societe_id != $commande->socid) { - accessforbidden(); - } -} - -// Recuperation de l'id de projet -$projetid = 0; -if ($_GET["projetid"]) -{ - $projetid = $_GET["projetid"]; -} - - -/******************************************************************************/ -/* Actions */ -/******************************************************************************/ - -// Suppression de la commande -if ($_REQUEST['action'] == 'confirm_delete' && $_REQUEST['confirm'] == 'yes') -{ - if ($user->rights->commande->supprimer ) - { - $commande = new Commande($db); - $commande->fetch($_GET['id']); - $commande->delete($user); - Header('Location: index.php'); - exit; - } -} - -/* - * Supprime une ligne produit AVEC ou SANS confirmation - */ -if (($_REQUEST['action'] == 'confirm_deleteline' && $_REQUEST['confirm'] == 'yes' && $conf->global->PRODUIT_CONFIRM_DELETE_LINE) -|| ($_GET['action'] == 'deleteline' && !$conf->global->PRODUIT_CONFIRM_DELETE_LINE)) -{ - if ($user->rights->commande->creer) - { - $commande = new Commande($db); - $commande->fetch($_GET['id']); - $result = $commande->delete_line($_GET['lineid']); - if ($result > 0) - { - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - commande_pdf_create($db, $_GET['id'], $commande->modelpdf, $outputlangs); - } - else - { - print $commande->error; - } - } - Header('Location: '.$_SERVER["PHP_SELF"].'?id='.$_GET['id']); - exit; -} - -// Categorisation dans projet -if ($_POST['action'] == 'classin') -{ - $commande = new Commande($db); - $commande->fetch($_GET['id']); - $commande->setProject($_POST['projetid']); -} - -// Ajout commande -if ($_POST['action'] == 'add' && $user->rights->commande->creer) -{ - $datecommande=''; - $datecommande = dolibarr_mktime(12, 0, 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); - $datelivraison = dolibarr_mktime(12, 0, 0, $_POST['liv_month'],$_POST['liv_day'],$_POST['liv_year']); - - $commande = new Commande($db); - $commande->socid=$_POST['socid']; - $commande->fetch_client(); - - $db->begin(); - - $commande->date_commande = $datecommande; - $commande->note = $_POST['note']; - $commande->note_public = $_POST['note_public']; - $commande->source = $_POST['source_id']; - $commande->projetid = $_POST['projetid']; - $commande->ref_client = $_POST['ref_client']; - $commande->modelpdf = $_POST['model']; - $commande->cond_reglement_id = $_POST['cond_reglement_id']; - $commande->mode_reglement_id = $_POST['mode_reglement_id']; - $commande->date_livraison = $datelivraison; - $commande->adresse_livraison_id = $_POST['adresse_livraison_id']; - $commande->contactid = $_POST['contactidp']; - - $NBLINES=8; - for ($i = 1 ; $i <= $NBLINES ; $i++) - { - if ($_POST['idprod'.$i]) - { - $xid = 'idprod'.$i; - $xqty = 'qty'.$i; - $xremise = 'remise_percent'.$i; - $commande->add_product($_POST[$xid],$_POST[$xqty],$_POST[$xremise]); - } - } - - $commande_id = $commande->create($user); - - if ($commande_id > 0) - { - // Insertion contact par defaut si defini - if ($_POST["contactidp"]) - { - $result=$commande->add_contact($_POST["contactidp"],'CUSTOMER','external'); - - if ($result > 0) - { - $error=0; - } - else - { - $mesg = '
'.$langs->trans("ErrorFailedToAddContact").'
'; - $error=1; - } - } - - $_GET['id'] = $commande->id; - $action = ''; - } - - // Fin creation facture, on l'affiche - if ($commande_id > 0 && ! $error) - { - $db->commit(); - } - else - { - $db->rollback(); - $_GET["action"]='create'; - $_GET['socid']=$_POST['socid']; - if (! $mesg) $mesg='
'.$commande->error.'
'; - } - -} - -// Positionne ref commande client -if ($_POST['action'] == 'set_ref_client' && $user->rights->commande->creer) -{ - $commande = new Commande($db); - $commande->fetch($_GET['id']); - $commande->set_ref_client($user, $_POST['ref_client']); -} - -if ($_POST['action'] == 'setremise' && $user->rights->commande->creer) -{ - $commande = new Commande($db); - $commande->fetch($_GET['id']); - $commande->set_remise($user, $_POST['remise']); -} - -if ($_POST['action'] == "setabsolutediscount" && $user->rights->commande->creer) -{ - if ($_POST["remise_id"]) - { - $com = new Commande($db); - $com->id=$_GET['id']; - $ret=$com->fetch($_GET['id']); - if ($ret > 0) - { - $com->insert_discount($_POST["remise_id"]); - } - else - { - dolibarr_print_error($db,$com->error); - } - } -} - -if ($_POST['action'] == 'setdate_livraison' && $user->rights->commande->creer) -{ - //print "x ".$_POST['liv_month'].", ".$_POST['liv_day'].", ".$_POST['liv_year']; - $datelivraison=dolibarr_mktime(0, 0, 0, $_POST['liv_month'], $_POST['liv_day'], $_POST['liv_year']); - - $commande = new Commande($db); - $commande->fetch($_GET['id']); - $result=$commande->set_date_livraison($user,$datelivraison); - if ($result < 0) - { - $mesg='
'.$commande->error.'
'; - } -} - -if ($_POST['action'] == 'setdeliveryadress' && $user->rights->commande->creer) -{ - $commande = new Commande($db); - $commande->fetch($_GET['id']); - $commande->set_adresse_livraison($user,$_POST['adresse_livraison_id']); -} - -if ($_POST['action'] == 'setmode' && $user->rights->commande->creer) -{ - $commande = new Commande($db); - $commande->fetch($_GET['id']); - $result=$commande->mode_reglement($_POST['mode_reglement_id']); - if ($result < 0) dolibarr_print_error($db,$commande->error); -} - -if ($_POST['action'] == 'setconditions' && $user->rights->commande->creer) -{ - $commande = new Commande($db); - $commande->fetch($_GET['id']); - $result=$commande->cond_reglement($_POST['cond_reglement_id']); - if ($result < 0) dolibarr_print_error($db,$commande->error); -} - -if ($_REQUEST['action'] == 'setremisepercent' && $user->rights->facture->creer) -{ - // Define remise_percent - if ($_POST['remise_percent']>0) - $remise_percent=$_POST['remise_percent']; - else - $remise_percent=0; - - $commande = new Commande($db); - $commande->fetch($_REQUEST['id']); - $result = $commande->set_remise($user, $remise_percent); - $_GET['id']=$_REQUEST['id']; -} - -if ($_REQUEST['action'] == 'setremiseabsolue' && $user->rights->facture->creer) -{ - $commande = new Commande($db); - $commande->fetch($_REQUEST['id']); - $result = $commande->set_remise_absolue($user, $_POST['remise_absolue']); - $_GET['id']=$_REQUEST['id']; -} - -/* - * Ajout d'une ligne produit dans la commande - */ -if ($_POST['action'] == 'addligne' && $user->rights->commande->creer) -{ - if ($_POST['qty'] && (($_POST['pu'] != '' && ($_POST['np_desc'] || $_POST['dp_desc'])) || $_POST['idprod'])) - { - $commande = new Commande($db); - $ret=$commande->fetch($_POST['id']); - if ($ret < 0) - { - dolibarr_print_error($db,$commande->error); - exit; - } - $ret=$commande->fetch_client(); - - $price_base_type = 'HT'; - - // Ecrase $pu par celui du produit - // Ecrase $desc par celui du produit - // Ecrase $txtva par celui du produit - // Ecrase $base_price_type par celui du produit - if ($_POST['idprod']) - { - $prod = new Product($db, $_POST['idprod']); - $prod->fetch($_POST['idprod']); - - $tva_tx = get_default_tva($mysoc,$commande->client,$prod->tva_tx); - - // multiprix - if ($conf->global->PRODUIT_MULTIPRICES == 1) - { - $pu_ht = $prod->multiprices[$commande->client->price_level]; - $pu_ttc = $prod->multiprices_ttc[$commande->client->price_level]; - $price_base_type = $prod->multiprices_base_type[$commande->client->price_level]; - } - else - { - $pu_ht = $prod->price; - $pu_ttc = $prod->price_ttc; - $price_base_type = $prod->price_base_type; - } - - // On reevalue prix selon taux tva car taux tva transaction peut etre different - // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur). - if ($tva_tx != $prod->tva_tx) - { - if ($price_base_type != 'HT') - { - $pu_ht = price2num($pu_ttc / (1 + ($tva_tx/100)), 'MU'); - } - else - { - $pu_ttc = price2num($pu_ht * (1 + ($tva_tx/100)), 'MU'); - } - } - - $desc = $prod->description; - $desc.= $prod->description && $_POST['np_desc'] ? "\n" : ""; - $desc.= $_POST['np_desc']; - } - else - { - $pu_ht=$_POST['pu']; - $tva_tx=eregi_replace('\*','',$_POST['tva_tx']); - $tva_npr=eregi('\*',$_POST['tva_tx'])?1:0; - $desc=$_POST['dp_desc']; - } - - $info_bits=0; - if ($tva_npr) $info_bits |= 0x01; - - // Define remise_percent - if ($_POST['remise_percent']>0) - $remise_percent=$_POST['remise_percent']; - else - $remise_percent=0; - - // Insert line - $result = $commande->addline( - $_POST['id'], - $desc, - $pu_ht, - $_POST['qty'], - $tva_tx, - $_POST['idprod'], - $remise_percent, - $info_bits, - '', - $price_base_type, - $pu_ttc - ); - - if ($result > 0) - { - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); - } - else - { - $mesg='
'.$commande->error.'
'; - } - } -} - -/* - * Mise a jour d'une ligne dans la commande - */ -if ($_POST['action'] == 'updateligne' && $user->rights->commande->creer && $_POST['save'] == $langs->trans('Save')) -{ - $commande = new Commande($db,'',$_POST['id']); - if (! $commande->fetch($_POST['id']) > 0) dolibarr_print_error($db); - - // Define info_bits - $info_bits=0; - if (eregi('\*',$_POST['tva_tx'])) $info_bits |= 0x01; - - // Define vat_rate - $vat_rate=$_POST['tva_tx']; - $vat_rate=eregi_replace('\*','',$vat_rate); - - // Define elremise_percent - if ($_POST['elremise_percent']>0) - $elremise_percent=$_POST['elremise_percent']; - else - $elremise_percent=0; - - $result = $commande->updateline($_POST['elrowid'], - $_POST['eldesc'], - $_POST['pu'], - $_POST['qty'], - $elremise_percent, - $vat_rate, - 'HT', - $info_bits - ); - - if ($result >= 0) - { - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); - } - else - { - dolibarr_print_error($db,$commande->error); - exit; - } - - $_GET['id']=$_POST['id']; // Pour reaffichage de la fiche en cours d'edition -} - -if ($_POST['action'] == 'updateligne' && $user->rights->commande->creer && $_POST['cancel'] == $langs->trans('Cancel')) -{ - Header('Location: fiche.php?id='.$_POST['id']); // Pour reaffichage de la fiche en cours d'edition - exit; -} - -if ($_REQUEST['action'] == 'confirm_validate' && $_REQUEST['confirm'] == 'yes' && $user->rights->commande->valider) -{ - $commande = new Commande($db); - $commande->fetch($_GET['id']); - - $result=$commande->valid($user); - if ($result >= 0) - { - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); - } -} - -if ($_REQUEST['action'] == 'confirm_close' && $_REQUEST['confirm'] == 'yes' && $user->rights->commande->creer) -{ - $commande = new Commande($db); - $commande->fetch($_GET['id']); - $result = $commande->cloture($user); -} - -if ($_REQUEST['action'] == 'confirm_cancel' && $_REQUEST['confirm'] == 'yes' && $user->rights->commande->valider) -{ - $commande = new Commande($db); - $commande->fetch($_GET['id']); - $result = $commande->cancel($user); -} - -if ($_GET['action'] == 'modif' && $user->rights->commande->creer) -{ - /* - * Repasse la commande en mode brouillon - */ - $commande = new Commande($db); - $commande->fetch($_GET['id']); - $commande->set_draft($user); - - $result = $commande->set_draft($user); - if ($result >= 0) - { //regeneration pdf - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); - } -} - -/* - * Ordonnancement des lignes - */ - -if ($_GET['action'] == 'up' && $user->rights->commande->creer) -{ - $commande = new Commande($db,'',$_GET['id']); - $commande->fetch($_GET['id']); - $commande->line_up($_GET['rowid']); - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); - Header ('Location: '.$_SERVER["PHP_SELF"].'?id='.$_GET["id"].'#'.$_GET['rowid']); - exit; -} - -if ($_GET['action'] == 'down' && $user->rights->commande->creer) -{ - $commande = new Commande($db,'',$_GET['id']); - $commande->fetch($_GET['id']); - $commande->line_down($_GET['rowid']); - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); - Header ('Location: '.$_SERVER["PHP_SELF"].'?id='.$_GET["id"].'#'.$_GET['rowid']); - exit; -} - -if ($_REQUEST['action'] == 'builddoc') // In get or post -{ - /* - * Generate order document - * define into /includes/modules/commande/modules_commande.php - */ - - // Sauvegarde le dernier modele choisi pour generer un document - $commande = new Commande($db, 0, $_REQUEST['id']); - $result=$commande->fetch($_REQUEST['id']); - if ($_REQUEST['model']) - { - $commande->setDocModel($user, $_REQUEST['model']); - } - - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - - $result=commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); - if ($result <= 0) - { - dolibarr_print_error($db,$result); - exit; - } - else - { - Header ('Location: '.$_SERVER["PHP_SELF"].'?id='.$commande->id.'#builddoc'); - exit; - } -} - -// Efface les fichiers -if ($_REQUEST['action'] == 'remove_file') -{ - $com = new Commande($db); - - if ($com->fetch($id)) - { - $upload_dir = $conf->commande->dir_output . "/"; - $file = $upload_dir . '/' . urldecode($_GET['file']); - dol_delete_file($file); - $mesg = '
'.$langs->trans("FileWasRemoved").'
'; - } -} - -/* - * Add file - */ -if ($_POST['addfile']) -{ - // Set tmp user directory - $conf->users->dir_tmp=DOL_DATA_ROOT."/users/".$user->id; - $upload_dir = $conf->users->dir_tmp.'/temp/'; - - if (! empty($_FILES['addedfile']['tmp_name'])) - { - if (! is_dir($upload_dir)) create_exdir($upload_dir); - - if (is_dir($upload_dir)) - { - if (dol_move_uploaded_file($_FILES['addedfile']['tmp_name'], $upload_dir . "/" . $_FILES['addedfile']['name'],0) > 0) - { - $mesg = '
'.$langs->trans("FileTransferComplete").'
'; - //print_r($_FILES); - - include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); - $formmail = new FormMail($db); - $formmail->add_attached_files($upload_dir . "/" . $_FILES['addedfile']['name'],$_FILES['addedfile']['name'],$_FILES['addedfile']['type']); - } - else - { - // Echec transfert (fichier d�passant la limite ?) - $mesg = '
'.$langs->trans("ErrorFileNotUploaded").'
'; - // print_r($_FILES); - } - } - } - $_GET["action"]='presend'; -} - -/* - * Send mail - */ -if ($_POST['action'] == 'send' && ! $_POST['addfile'] && ! $_POST['cancel']) -{ - $langs->load('mails'); - - $commande= new Commande($db); - $result=$commande->fetch($_POST['orderid']); - if ($result) - { - $ref = sanitize_string($commande->ref); - $file = $conf->commande->dir_output . '/' . $ref . '/' . $ref . '.pdf'; - - if (is_readable($file)) - { - $commande->fetch_client(); - - if ($_POST['sendto']) - { - // Le destinataire a ete fourni via le champ libre - $sendto = $_POST['sendto']; - $sendtoid = 0; - } - elseif ($_POST['receiver']) - { - // Le destinataire a ete fourni via la liste deroulante - if ($_POST['receiver'] < 0) // Id du tiers - { - $sendto = $commande->client->email; - $sendtoid = 0; - } - else // Id du contact - { - $sendto = $commande->client->contact_get_email($_POST['receiver']); - $sendtoid = $_POST['receiver']; - } - } - - if (strlen($sendto)) - { - $langs->load("commercial"); - - $from = $_POST['fromname'] . ' <' . $_POST['frommail'] .'>'; - $replyto = $_POST['replytoname']. ' <' . $_POST['replytomail'].'>'; - $message = $_POST['message']; - $sendtocc = $_POST['sendtocc']; - $deliveryreceipt = $_POST['deliveryreceipt']; - - if ($_POST['action'] == 'send') - { - if (strlen($_POST['subject'])) $subject=$_POST['subject']; - else $subject = $langs->transnoentities('Order').' '.$commande->ref; - $actiontypecode='AC_COM'; - $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto.".\n"; - if ($message) - { - $actionmsg.=$langs->transnoentities('MailTopic').": ".$subject."\n"; - $actionmsg.=$langs->transnoentities('TextUsedInTheMessageBody').":\n"; - $actionmsg.=$message; - } - $actionmsg2=$langs->transnoentities('Action'.$actiontypecode); - } - - // Create form object - include_once('../html.formmail.class.php'); - $formmail = new FormMail($db); - - $attachedfiles=$formmail->get_attached_files(); - $filepath = $attachedfiles['paths']; - $filename = $attachedfiles['names']; - $mimetype = $attachedfiles['mimes']; - - // Send mail - require_once(DOL_DOCUMENT_ROOT.'/lib/CMailFile.class.php'); - $mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc,'',$deliveryreceipt); - if ($mailfile->error) - { - $mesg='
'.$mailfile->error.'
'; - } - else - { - $result=$mailfile->sendfile(); - if ($result) - { - $mesg='
'.$langs->trans('MailSuccessfulySent',$from,$sendto).'.
'; - - $error=0; - - // Initialisation donnees - $commande->sendtoid=$sendtoid; - $commande->actiontypecode=$actiontypecode; - $commande->actionmsg = $actionmsg; - $commande->actionmsg2= $actionmsg2; - $commande->orderrowid=$commande->id; - - // Appel des triggers - include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); - $interface=new Interfaces($db); - $result=$interface->run_triggers('ORDER_SENTBYMAIL',$commande,$user,$langs,$conf); - if ($result < 0) { $error++; $this->errors=$interface->errors; } - // Fin appel triggers - - if ($error) - { - dolibarr_print_error($db); - } - else - { - // Renvoie sur la fiche - Header('Location: '.$_SERVER["PHP_SELF"].'?id='.$commande->id.'&msg='.urlencode($mesg)); - exit; - } - } - else - { - $langs->load("other"); - $mesg='
'; - if ($mailfile->error) - { - $mesg.=$langs->trans('ErrorFailedToSendMail',$from,$sendto); - $mesg.='
'.$mailfile->error; - } - else - { - $mesg.='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS'; - } - $mesg.='
'; - } - } - } - else - { - $langs->load("other"); - $mesg='
'.$langs->trans('ErrorMailRecipientIsEmpty').' !
'; - $_GET["action"]='presend'; - dolibarr_syslog('Recipient email is empty'); - } - } - else - { - $langs->load("other"); - $mesg='
'.$langs->trans('ErrorCantReadFile',$file).'
'; - dolibarr_syslog('Failed to read file: '.$file); - } - } - else - { - $langs->load("other"); - $mesg='
'.$langs->trans('ErrorFailedToReadEntity',$langs->trans("Invoice")).'
'; - dolibarr_syslog('Impossible de lire les donnees de la facture. Le fichier facture n\'a peut-etre pas ete genere.'); - } -} - - -/* - * View - */ - -llxHeader('',$langs->trans('Order'),'Commande'); - -$html = new Form($db); -$formfile = new FormFile($db); - -/********************************************************************* - * - * Mode creation - * - *********************************************************************/ -if ($_GET['action'] == 'create' && $user->rights->commande->creer) -{ - print_titre($langs->trans('CreateOrder')); - - if ($mesg) print $mesg.'
'; - - $new_commande = new Commande($db); - - if ($propalid) - { - $sql = 'SELECT s.nom, s.prefix_comm, s.rowid'; - $sql.= ', p.price, p.remise, p.remise_percent, p.tva, p.total, p.ref, p.fk_cond_reglement, p.fk_mode_reglement'; - $sql.= ', '.$db->pdate('p.datep').' as dp'; - $sql.= ', c.id as statut, c.label as lst'; - $sql .= ' FROM '.MAIN_DB_PREFIX.'societe as s, '.MAIN_DB_PREFIX.'propal as p, '.MAIN_DB_PREFIX.'c_propalst as c'; - $sql .= ' WHERE p.fk_soc = s.rowid AND p.fk_statut = c.id'; - $sql .= ' AND p.rowid = '.$propalid; - } - else - { - $sql = 'SELECT s.nom, s.prefix_comm, s.rowid, s.mode_reglement, s.cond_reglement '; - $sql .= 'FROM '.MAIN_DB_PREFIX.'societe as s '; - $sql .= 'WHERE s.rowid = '.$_GET['socid']; - } - $resql = $db->query($sql); - if ( $resql ) - { - $num = $db->num_rows($resql); - if ($num) - { - $obj = $db->fetch_object($resql); - - $soc = new Societe($db); - $soc->fetch($obj->rowid); - - $nbrow=10; - - print '
'; - print ''; - print '' ."\n"; - print ''; - print ''; - - print ''; - - // Reference - print ''; - - // Reference client - print ''; - print ''; - - // Client - print ''; - - /* - * Contact de la commande - */ - print "'; - - // Ligne info remises tiers - print ''; - - // Date - print ''; - - // Date de livraison - print ""; - - // Adresse de livraison - print ''; - - // Conditions de reglement - print ''; - - // Mode de reglement - print ''; - - // Projet - if ($conf->projet->enabled) - { - $projetid=$_POST["projetid"]?$_POST["projetid"]:$commande->projetid; - print ''; - } - - print ''; - - print ''; - print '"; - - // Note publique - print ''; - print ''; - print ''; - - // Note priv�e - if (! $user->societe_id) - { - print ''; - print ''; - print ''; - } - - if ($propalid > 0) - { - $amount = ($obj->price); - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''; - - print ''; - print ''; - print ''; - print ''; - } - else - { - if ($conf->global->PRODUCT_SHOW_WHEN_CREATE) - { - /* - * Services/produits predefinis - */ - $NBLINES=8; - - print ''; - } - } - - /* - * - */ - print ''; - print '
'.$langs->trans('Ref').''.$langs->trans("Draft").'
'.$langs->trans('RefCustomer').''; - print '
'.$langs->trans('Customer').''.$soc->getNomUrl(1).'
".$langs->trans("DefaultContact").''; - $html->select_contacts($soc->id,$setcontact,'contactidp',1); - print '
'.$langs->trans('Discounts').''; - if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client); - else print $langs->trans("CompanyHasNoRelativeDiscount"); - $absolute_discount=$soc->getAvailableDiscounts(); - print '. '; - if ($absolute_discount) print $langs->trans("CompanyHasAbsoluteDiscount",price($absolute_discount),$langs->trans("Currency".$conf->monnaie)); - else print $langs->trans("CompanyHasNoAbsoluteDiscount"); - print '.'; - print '
'.$langs->trans('Date').''; - $html->select_date('','re','','','',"crea_commande"); - print '
".$langs->trans("DeliveryDate").""; - if ($conf->global->DATE_LIVRAISON_WEEK_DELAY) - { - $tmpdte = time() + ((7*$conf->global->DATE_LIVRAISON_WEEK_DELAY) * 24 * 60 * 60); - $html->select_date($tmpdte,'liv_','','',1,"crea_commande"); - } - else - { - $dateorder=empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0; - $html->select_date($dateorder,'liv_','','',1,"crea_commande"); - } - print "
'.$langs->trans('DeliveryAddress').''; - $numaddress = $html->select_adresse_livraison($soc->adresse_livraison_id, $_GET['socid'],'adresse_livraison_id',1); - - if ($numaddress==0) - { - print '   '.$langs->trans("AddAddress").''; - } - - print '
'.$langs->trans('PaymentConditionsShort').''; - $html->select_conditions_paiements($soc->cond_reglement,'cond_reglement_id',-1,1); - print '
'.$langs->trans('PaymentMode').''; - $html->select_types_paiements($soc->mode_reglement,'mode_reglement_id'); - print '
'.$langs->trans('Project').''; - $numprojet=select_projects($soc->id,$projetid,'projetid'); - if ($numprojet==0) - { - print '   '.$langs->trans("AddProject").''; - } - print '
'.$langs->trans('Source').''; - $html->selectSourcesCommande('','source_id',1); - print '
'.$langs->trans('Model').''; - // pdf - include_once(DOL_DOCUMENT_ROOT.'/includes/modules/commande/modules_commande.php'); - $model=new ModelePDFCommandes(); - $liste=$model->liste_modeles($db); - $html->select_array('model',$liste,$conf->global->COMMANDE_ADDON_PDF); - print "
'.$langs->trans('NotePublic').''; - print '
'.$langs->trans('NotePrivate').''; - print '
'.$langs->trans('Ref').''.$obj->ref.'
'.$langs->trans('TotalTTC').''.price($amount).'
'.$langs->trans('VAT').''.price($obj->tva).'
'.$langs->trans('TotalTTC').''.price($obj->total).'
'; - - print ''; - print ''; - print ''; - print ''; - print ''; - for ($i = 1 ; $i <= $NBLINES ; $i++) - { - print ''; - print ''; - print ''; - } - - print '
'.$langs->trans('ProductsAndServices').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').'
'; - // multiprix - if($conf->global->PRODUIT_MULTIPRICES == 1) - print $html->select_produits('','idprod'.$i,'',$conf->produit->limit_size,$soc->price_level); - else - print $html->select_produits('','idprod'.$i,'',$conf->produit->limit_size); - print '%
'; - print '
'; - - print '
'; - - if ($propalid) - { - /* - * Produits - */ - print_titre($langs->trans('Products')); - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - $var=false; - - $sql = 'SELECT pt.rowid, p.label as product, p.ref, pt.price, pt.qty, p.rowid as prodid, pt.remise_percent'; - $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt, '.MAIN_DB_PREFIX.'product as p WHERE pt.fk_product = p.rowid AND pt.fk_propal = '.$propalid; - $sql .= ' ORDER BY pt.rowid ASC'; - $result = $db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - $i = 0; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print ''; - print ''; - print ''; - print ''; - print ''; - $i++; - } - } - - $sql = 'SELECT pt.rowid, pt.description as product, pt.price, pt.qty, pt.remise_percent'; - $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt WHERE pt.fk_propal = '.$propalid.' AND pt.fk_product = 0'; - $sql .= ' ORDER BY pt.rowid ASC'; - if ($db->query($sql)) - { - $num = $db->num_rows(); - $i = 0; - while ($i < $num) - { - $objp = $db->fetch_object(); - $var=!$var; - print ''; - print ''; - print ''; - print ''; - print ''; - $i++; - } - } - else - { - dolibarr_print_error($db); - } - - print '
'.$langs->trans('Ref').''.$langs->trans('Product').''.$langs->trans('Price').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').'
['.$objp->ref.']'.img_object($langs->trans('ShowProduct'),'product').' '.$objp->product.''.price($objp->price).''.$objp->qty.'
'.$objp->remise_percent.'%
 '.img_object($langs->trans('ShowProduct'),'product').' '.$objp->product.''.price($objp->price).''.$objp->qty.'
'.$objp->remise_percent.'%
'; - } - } - } - else - { - dolibarr_print_error($db); - } -} -else -{ - /* *************************************************************************** */ - /* */ - /* Mode vue et edition */ - /* */ - /* *************************************************************************** */ - $id = $_GET['id']; - if ($id > 0) - { - if ($mesg) print $mesg.'
'; - - $commande = new Commande($db); - if ( $commande->fetch($_GET['id']) > 0) - { - $soc = new Societe($db); - $soc->fetch($commande->socid); - - $author = new User($db); - $author->id = $commande->user_author_id; - $author->fetch(); - - $head = commande_prepare_head($commande); - dolibarr_fiche_head($head, 'order', $langs->trans("CustomerOrder")); - - /* - * Confirmation de la suppression de la commande - */ - if ($_GET['action'] == 'delete') - { - $html->form_confirm($_SERVER["PHP_SELF"].'?id='.$id, $langs->trans('DeleteOrder'), $langs->trans('ConfirmDeleteOrder'), 'confirm_delete'); - print '
'; - } - - /* - * Confirmation de la validation - */ - if ($_GET['action'] == 'validate') - { - // on verifie si la facture est en numerotation provisoire - $ref = substr($commande->ref, 1, 4); - if ($ref == 'PROV') - { - $num = $commande->getNextNumRef($soc); - } - else - { - $num = $commande->ref; - } - - $text=$langs->trans('ConfirmValidateOrder',$num); - $html->form_confirm($_SERVER["PHP_SELF"].'?id='.$id, $langs->trans('ValidateOrder'), $text, 'confirm_validate'); - print '
'; - } - - /* - * Confirmation de la cloture - */ - if ($_GET['action'] == 'close') - { - $html->form_confirm($_SERVER["PHP_SELF"].'?id='.$id, $langs->trans('CloseOrder'), $langs->trans('ConfirmCloseOrder'), 'confirm_close'); - print '
'; - } - - /* - * Confirmation de l'annulation - */ - if ($_GET['action'] == 'cancel') - { - $html->form_confirm($_SERVER["PHP_SELF"].'?id='.$id, $langs->trans('Cancel'), $langs->trans('ConfirmCancelOrder'), 'confirm_cancel'); - print '
'; - } - - /* - * Confirmation de la suppression d'une ligne produit - */ - if ($_GET['action'] == 'ask_deleteline' && $conf->global->PRODUIT_CONFIRM_DELETE_LINE) - { - $html->form_confirm($_SERVER["PHP_SELF"].'?id='.$id.'&lineid='.$_GET["lineid"], $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteline'); - print '
'; - } - - /* - * Commande - */ - $nbrow=8; - if ($conf->projet->enabled) $nbrow++; - - print ''; - - // Ref - print ''; - print ''; - print ''; - - // Ref commande client - print ''; - print ''; - - - // Societe - print ''; - print ''; - print ''; - - // Ligne info remises tiers - print ''; - - // Date - print ''; - print ''; - print ''; - print ''; - - // Date de livraison - if ($conf->expedition->enabled) - { - print ''; - print ''; - print ''; - - - // Adresse de livraison - print ''; - } - - // Conditions et modes de reglement - print ''; - - //Note public lorsque le module expedition n'est pas active - if (!$conf->projet->enabled) $nbrow--; - if (!$conf->expedition->enabled) - { - $nbrow--; - if ($conf->projet->enabled) $nbrow--; - print ''; - } - - print ''; - print ''; - - // Projet - if ($conf->projet->enabled) - { - $langs->load('projects'); - print ''; - } - - // Lignes de 3 colonnes - - // Total HT - print ''; - print ''; - print ''; - - // Total TVA - print ''; - print ''; - - // Total TTC - print ''; - print ''; - - // Statut - print ''; - print ''; - print ''; - - print '
'.$langs->trans('Ref').''.$commande->ref.'
'; - print ''; - if ($_GET['action'] != 'RefCustomerOrder' && $commande->brouillon) print ''; - print '
'; - print $langs->trans('RefCustomer').''; - print ''.img_edit($langs->trans('Modify')).'
'; - print '
'; - if ($user->rights->commande->creer && $_GET['action'] == 'RefCustomerOrder') - { - print '
'; - print ''; - print ''; - print ' '; - print '
'; - } - else - { - print $commande->ref_client; - } - print '
'.$langs->trans('Company').''.$soc->getNomUrl(1).'
'.$langs->trans('Discounts').''; - if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client); - else print $langs->trans("CompanyHasNoRelativeDiscount"); - $absolute_discount=$soc->getAvailableDiscounts('','fk_facture_source IS NULL'); - $absolute_creditnote=$soc->getAvailableDiscounts('','fk_facture_source IS NOT NULL'); - print '. '; - if ($absolute_discount) - { - if ($commande->statut > 0) - { - print $langs->trans("CompanyHasAbsoluteDiscount",$absolute_discount,$langs->transnoentities("Currency".$conf->monnaie)); - } - else - { - // Remise dispo de type non avoir - $filter='fk_facture_source IS NULL'; - print '
'; - $html->form_remise_dispo($_SERVER["PHP_SELF"].'?id='.$commande->id,0,'remise_id',$soc->id,$absolute_discount,$filter); - } - } - if ($absolute_creditnote) - { - print $langs->trans("CompanyHasCreditNote",price($absolute_creditnote),$langs->transnoentities("Currency".$conf->monnaie)).'. '; - } - if (! $absolute_discount && ! $absolute_creditnote) print $langs->trans("CompanyHasNoAbsoluteDiscount").'.'; - print '
'.$langs->trans('Date').''.dolibarr_print_date($commande->date,'daytext').''.$langs->trans('Source').' : '.$commande->getLabelSource(); - if ($commande->source == 0 && $conf->propal->enabled && $commande->propale_id) - { - // Si source = propal - $propal = new Propal($db); - $propal->fetch($commande->propale_id); - print ' -> '.$propal->ref.''; - } - print '
'; - print ''; - - if ($_GET['action'] != 'editdate_livraison' && $commande->brouillon) print ''; - print '
'; - print $langs->trans('DeliveryDate'); - print 'id.'">'.img_edit($langs->trans('SetDeliveryDate'),1).'
'; - print '
'; - if ($_GET['action'] == 'editdate_livraison') - { - print '
'; - print ''; - $html->select_date($commande->date_livraison,'liv_','','','',"setdate_livraison"); - print ''; - print '
'; - } - else - { - print $commande->date_livraison ? dolibarr_print_date($commande->date_livraison,'daytext') : ' '; - } - print '
'.$langs->trans('NotePublic').' :
'; - print nl2br($commande->note_public); - print '
'; - print ''; - - if ($_GET['action'] != 'editdelivery_adress' && $commande->brouillon) print ''; - print '
'; - print $langs->trans('DeliveryAddress'); - print 'socid.'&id='.$commande->id.'">'.img_edit($langs->trans('SetDeliveryAddress'),1).'
'; - print '
'; - - if ($_GET['action'] == 'editdelivery_adress') - { - $html->form_adresse_livraison($_SERVER['PHP_SELF'].'?id='.$commande->id,$commande->adresse_livraison_id,$_GET['socid'],'adresse_livraison_id','commande',$commande->id); - } - else - { - $html->form_adresse_livraison($_SERVER['PHP_SELF'].'?id='.$commande->id,$commande->adresse_livraison_id,$_GET['socid'],'none','commande',$commande->id); - } - print '
'; - print ''; - - if ($_GET['action'] != 'editconditions' && $commande->brouillon) print ''; - print '
'; - print $langs->trans('PaymentConditionsShort'); - print 'id.'">'.img_edit($langs->trans('SetConditions'),1).'
'; - print '
'; - if ($_GET['action'] == 'editconditions') - { - $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$commande->id,$commande->cond_reglement_id,'cond_reglement_id'); - } - else - { - $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$commande->id,$commande->cond_reglement_id,'none'); - } - print ''.$langs->trans('NotePublic').' :
'; - print nl2br($commande->note_public); - print '
'; - print ''; - if ($_GET['action'] != 'editmode' && $commande->brouillon) print ''; - print '
'; - print $langs->trans('PaymentMode'); - print 'id.'">'.img_edit($langs->trans('SetMode'),1).'
'; - print '
'; - if ($_GET['action'] == 'editmode') - { - $html->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$commande->id,$commande->mode_reglement_id,'mode_reglement_id'); - } - else - { - $html->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$commande->id,$commande->mode_reglement_id,'none'); - } - print '
'; - print ''; - if ($_GET['action'] != 'classer' && $commande->brouillon) print ''; - print '
'; - print $langs->trans('Project'); - print ''.img_edit($langs->trans('SetProject')).'
'; - print '
'; - if ($_GET['action'] == 'classer') - { - $html->form_project($_SERVER['PHP_SELF'].'?id='.$commande->id, $commande->socid, $commande->projet_id, 'projetid'); - } - else - { - $html->form_project($_SERVER['PHP_SELF'].'?id='.$commande->id, $commande->socid, $commande->projet_id, 'none'); - } - print '
'.$langs->trans('AmountHT').''.price($commande->total_ht).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('AmountVAT').''.price($commande->total_tva).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('AmountTTC').''.price($commande->total_ttc).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('Status').''.$commande->getLibStatut(4).'

'; - print "\n"; - - /* - * Lignes de commandes - */ - $sql = 'SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_tx, '; - $sql.= ' l.fk_remise_except, l.remise_percent, l.subprice, l.info_bits,'; - $sql.= ' l.total_ht, l.total_tva, l.total_ttc,'; - $sql.= ' p.label as product, p.ref, p.fk_product_type, p.rowid as prodid, '; - $sql.= ' p.description as product_desc'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as l'; - $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product=p.rowid'; - $sql.= ' WHERE l.fk_commande = '.$commande->id; - $sql.= ' ORDER BY l.rang ASC, l.rowid'; - - $resql = $db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - $i = 0; $total = 0; - - print ''; - if ($num) - { - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print "\n"; - } - $var=true; - while ($i < $num) - { - $objp = $db->fetch_object($resql); - $var=!$var; - - // Ligne en mode visu - if ($_GET['action'] != 'editline' || $_GET['rowid'] != $objp->rowid) - { - print ''; - if ($objp->fk_product > 0) - { - print ''; - } - else - { - print ''; - } - - print ''; - print ''; - print ''; - if ($objp->remise_percent > 0) - { - print ''; - } - else - { - print ''; - } - print ''; - - // Icone d'edition et suppression - if ($commande->statut == 0 && $user->rights->commande->creer) - { - print ''; - print ''; - if ($num > 1) - { - print ''; - } - } - else - { - print ''; - } - print ''; - } - - // Ligne en mode update - if ($_GET['action'] == 'editline' && $user->rights->commande->creer && $_GET['rowid'] == $objp->rowid) - { - print 'rowid.'" method="post">'; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - } - - $total = $total + ($objp->qty * $objp->price); - $i++; - } - $db->free($resql); - - $numlines=$num; - } - else - { - dolibarr_print_error($db); - } - - /* - * Ajouter une ligne - */ - if ($commande->statut == 0 && $user->rights->commande->creer && $_GET["action"] <> 'editline') - { - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - // Ajout produit produits/services personnalises - print ''; - print ''; - print ''; - - $var=true; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - print ''; - - // Ajout de produits/services predefinis - if ($conf->produit->enabled) - { - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - print ''; - print ''; - print ''; - - $var=!$var; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - print ''; - } - } - print '
'.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').''.$langs->trans('AmountHT').' 
'; - print ''; // ancre pour retourner sur la ligne - - // Affiche ligne produit - $text = ''; - if ($objp->fk_product_type==1) $text.= img_object($langs->trans('ShowService'),'service'); - else $text.= img_object($langs->trans('ShowProduct'),'product'); - $text.= ' '.$objp->ref.''; - $text.= ' - '.$objp->product; - $description=($conf->global->PRODUIT_DESC_IN_FORM?'':dol_htmlentitiesbr($objp->description)); - print $html->textwithtooltip($text,$description,3,'','',$i); - // Todo: voir si on insert ou pas en option les dates de debut et de fin de service - //print_date_range($objp->date_start,$objp->date_end); - if ($conf->global->PRODUIT_DESC_IN_FORM) - { - print ($objp->description && $objp->description!=$objp->product)?'
'.dol_htmlentitiesbr($objp->description):''; - } - - print '
'; - print ''; // ancre pour retourner sur la ligne - if (($objp->info_bits & 2) == 2) - { - print ''; - print img_object($langs->trans("ShowReduc"),'reduc').' '.$langs->trans("Discount"); - print ''; - if ($objp->description) - { - if ($objp->description == '(CREDIT_NOTE)') - { - require_once(DOL_DOCUMENT_ROOT.'/discount.class.php'); - $discount=new DiscountAbsolute($db); - $discount->fetch($objp->fk_remise_except); - print ' - '.$langs->transnoentities("DiscountFromCreditNote",$discount->getNomUrl(0)); - } - else - { - print ' - '.nl2br($objp->description); - } - } - } - else - { - print nl2br($objp->description); - } - print ''.vatrate($objp->tva_tx,'%',$objp->info_bits).''.price($objp->subprice).''; - if (($objp->info_bits & 2) != 2) - { - print $objp->qty; - } - else print ' '; - print ''.dolibarr_print_reduction($objp->remise_percent).' '.price($objp->total_ht).''; - if (($objp->info_bits & 2) == 2) - { - // Ligne remise predefinie, on ne permet pas modif - } - else - { - print 'rowid.'#'.$objp->rowid.'">'; - print img_edit(); - print ''; - } - print ''; - if ($conf->global->PRODUIT_CONFIRM_DELETE_LINE) - { - if ($conf->use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) - { - $url = $_SERVER["PHP_SELF"].'?id='.$id.'&lineid='.$objp->rowid.'&action=confirm_deleteline&confirm=yes'; - print 'trans("No").'\',\'deleteline'.$i.'\')">'; - } - else - { - print 'rowid.'">'; - } - } - else - { - print 'rowid.'">'; - } - print img_delete(); - print ''; - if ($i > 0) - { - print 'rowid.'">'; - print img_up(); - print ''; - } - if ($i < $num-1) - { - print 'rowid.'">'; - print img_down(); - print ''; - } - print ' 
'; - print ''; // ancre pour retourner sur la ligne - if ($objp->fk_product > 0) - { - print ''; - if ($objp->fk_product_type==1) print img_object($langs->trans('ShowService'),'service'); - else print img_object($langs->trans('ShowProduct'),'product'); - print ' '.$objp->ref.''; - print ' - '.nl2br($objp->product); - print '
'; - } - // editeur wysiwyg - if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) - { - require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); - $doleditor=new DolEditor('eldesc',$objp->description,200,'dolibarr_details'); - $doleditor->Create(); - } - else - { - print ''; - } - print '
'; - if($soc->tva_assuj == "0") - print '0'; - else - print $html->select_tva('tva_tx',$objp->tva_tx,$mysoc,$soc); - print ''; - if (($objp->info_bits & 2) != 2) - { - print ''; - } - else print ' '; - print ''; - if (($objp->info_bits & 2) != 2) - { - print '%'; - } - else print ' '; - print ''; - print '
'; - print ''; // ancre - print $langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').' 
'; - // editeur wysiwyg - if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) - { - require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); - $doleditor=new DolEditor('dp_desc','',100,'dolibarr_details'); - $doleditor->Create(); - } - else - { - print ''; - } - print ''; - if($soc->tva_assuj == "0") - print '0'; - else - print $html->select_tva('tva_tx',$conf->defaulttx,$mysoc,$soc); - print '%
'; - if ($conf->service->enabled) - { - print $langs->trans('RecordedProductsAndServices'); - } - else - { - print $langs->trans('RecordedProducts'); - } - print ''.$langs->trans('Qty').''.$langs->trans('ReductionShort').' 
'; - // multiprix - if($conf->global->PRODUIT_MULTIPRICES == 1) - { - $html->select_produits('','idprod','',$conf->produit->limit_size,$soc->price_level); - } - else - { - $html->select_produits('','idprod','',$conf->produit->limit_size); - } - - if (! $conf->global->PRODUIT_USE_SEARCH_TO_SELECT) print '
'; - - // editeur wysiwyg - if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) - { - require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); - $doleditor=new DolEditor('np_desc','',100,'dolibarr_details'); - $doleditor->Create(); - } - else - { - print ''; - } - - print '
%
'; - print ''; - - - /* - * Boutons actions - */ - if ($_GET['action'] != 'presend') - { - if ($user->societe_id == 0 && $_GET['action'] <> 'editline') - { - print '
'; - - // Valid - if ($commande->statut == 0 && $commande->total_ttc >= 0 && $numlines > 0 && $user->rights->commande->valider) - { - print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) - { - // on verifie si la commande est en numerotation provisoire - $ref = substr($commande->ref, 1, 4); - if ($ref == 'PROV') - { - $num = $commande->getNextNumRef($soc); - } - else - { - $num = $commande->ref; - } - $url = $_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=confirm_validate&confirm=yes'; - print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.dol_escape_js($langs->trans('ConfirmValidateOrder',$num)).'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'validate\')"'; - } - else - { - print 'href="'.$_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=validate"'; - } - print '>'.$langs->trans('Validate').''; - } - - // Edit - if ($commande->statut == 1) - { - if ($user->rights->commande->creer) - { - print ''.$langs->trans('Modify').''; - } - } - - // Send - if ($commande->statut > 0) - { - if ($user->rights->commande->envoyer) - { - $comref = sanitize_string($commande->ref); - $file = $conf->commande->dir_output . '/'.$comref.'/'.$comref.'.pdf'; - if (file_exists($file)) - { - print ''.$langs->trans('SendByMail').''; - } - } - } - - // Ship - if ($commande->statut > 0 && $commande->statut < 3 && $user->rights->expedition->creer - && $commande->getNbOfProductsLines() > 0) - { - - // Chargement des permissions - $error = $user->load_entrepots(); - if (sizeof($user->entrepots) === 1) - { - print ''; - print $langs->trans('ShipProduct').''; - - } - else - { - print ''.$langs->trans('ShipProduct').''; - } - } - - // Cloturer - if ($commande->statut == 1 || $commande->statut == 2) - { - if ($user->rights->commande->cloturer) - { - print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) - { - $url = $_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=confirm_close&confirm=yes'; - print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.$langs->trans('ConfirmCloseOrder').'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'close\')"'; - } - else - { - print 'href="'.$_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=close"'; - } - print '>'.$langs->trans('Close').''; - } - } - - // Annuler commande - if ($commande->statut == 1) - { - $nb_expedition = $commande->nb_expedition(); - if ($user->rights->commande->annuler && $nb_expedition == 0) - { - print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) - { - $url = $_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=confirm_cancel&confirm=yes'; - print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.$langs->trans('ConfirmCancelOrder').'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'cancel\')"'; - } - else - { - print 'href="'.$_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=cancel"'; - } - print '>'.$langs->trans('CancelOrder').''; - } - } - - // Supprimer commande - if ($commande->statut == 0 && $user->rights->commande->supprimer) - { - print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) - { - $url = $_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=confirm_delete&confirm=yes'; - print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.$langs->trans('ConfirmDeleteOrder').'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'delete\')"'; - } - else - { - print 'href="'.$_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=delete"'; - } - print '>'.$langs->trans('Delete').''; - } - - print '
'; - } - print '
'; - } - - - if ($_GET['action'] != 'presend') - { - print '
'; - print ''; // ancre - - /* - * Documents generes - * - */ - $comref = sanitize_string($commande->ref); - $file = $conf->commande->dir_output . '/' . $comref . '/' . $comref . '.pdf'; - $relativepath = $comref.'/'.$comref.'.pdf'; - $filedir = $conf->commande->dir_output . '/' . $comref; - $urlsource=$_SERVER["PHP_SELF"]."?id=".$commande->id; - $genallowed=$user->rights->commande->creer; - $delallowed=$user->rights->commande->supprimer; - - $somethingshown=$formfile->show_documents('commande',$comref,$filedir,$urlsource,$genallowed,$delallowed,$commande->modelpdf); - - /* - * Liste des factures - */ - $sql = 'SELECT f.rowid,f.facnumber, f.total_ttc, '.$db->pdate('f.datef').' as df'; - $sql .= ' FROM '.MAIN_DB_PREFIX.'facture as f, '.MAIN_DB_PREFIX.'co_fa as cf'; - $sql .= ' WHERE f.rowid = cf.fk_facture AND cf.fk_commande = '. $commande->id; - - $result = $db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - if ($num) - { - print '
'; - print_titre($langs->trans('RelatedBills')); - $i = 0; $total = 0; - print ''; - print '"; - print ''; - print ''; - print ''; - - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print ''; - print ''; - print ''; - print ''; - $i++; - } - print '
'.$langs->trans('Ref')."'.$langs->trans('Date').''.$langs->trans('Price').'
'.img_object($langs->trans('ShowBill'),'bill').' '.$objp->facnumber.''.dolibarr_print_date($objp->df,'day').''.$objp->total_ttc.'
'; - } - } - else - { - dolibarr_print_error($db); - } - print '
'; - - // List of actions on element - include_once(DOL_DOCUMENT_ROOT.'/html.formactions.class.php'); - $formactions=new FormActions($db); - $somethingshown=$formactions->showactions($commande,'order',$socid); - - print '
'; - } - - - /* - * Action presend - * - */ - if ($_GET['action'] == 'presend') - { - $ref = sanitize_string($commande->ref); - $file = $conf->commande->dir_output . '/' . $ref . '/' . $ref . '.pdf'; - - print '
'; - print_titre($langs->trans('SendOrderByMail')); - - $soc = new Societe($db); - $soc->fetch($commande->socid); - - $liste[0]=" "; - foreach ($soc->thirdparty_and_contact_email_array() as $key=>$value) - { - $liste[$key]=$value; - } - - // Cree l'objet formulaire mail - include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); - $formmail = new FormMail($db); - $formmail->fromtype = 'user'; - $formmail->fromid = $user->id; - $formmail->fromname = $user->fullname; - $formmail->frommail = $user->email; - $formmail->withfrom=1; - $formmail->withto=$liste; - $formmail->withtocc=1; - $formmail->withtopic=$langs->trans('SendOrderRef','__ORDERREF__'); - $formmail->withfile=2; - $formmail->withbody=1; - $formmail->withdeliveryreceipt=1; - $formmail->withcancel=1; - // Tableau des substitutions - $formmail->substit['__ORDERREF__']=$commande->ref; - // Tableau des parametres complementaires - $formmail->param['action']='send'; - $formmail->param['models']='order_send'; - $formmail->param['orderid']=$commande->id; - $formmail->param['returnurl']=DOL_URL_ROOT.'/commande/fiche.php?id='.$commande->id; - - // Init list of files - if (! empty($_REQUEST["mode"]) && $_REQUEST["mode"]=='init') - { - $formmail->clear_attached_files(); - $formmail->add_attached_files($file,$ref.'.pdf','application/pdf'); - } - - // Show form - $formmail->show_form(); - - print '
'; - } - } - else - { - // Commande non trouvee - dolibarr_print_error($db); - } - } -} - -$db->close(); - -llxFooter('$Date$ - $Revision$'); -?> + + * Copyright (C) 2004-2008 Laurent Destailleur + * Copyright (C) 2005 Marc Barilley / Ocebo + * Copyright (C) 2005-2008 Regis Houssin + * Copyright (C) 2006 Andre Cianfarani + * + * 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. + */ + +/** + * \file htdocs/commande/fiche.php + * \ingroup commande + * \brief Fiche commande client + * \version $Id$ + */ + +require('./pre.inc.php'); +require_once(DOL_DOCUMENT_ROOT."/html.formfile.class.php"); +require_once(DOL_DOCUMENT_ROOT ."/includes/modules/commande/modules_commande.php"); +require_once(DOL_DOCUMENT_ROOT.'/commande/commande.class.php'); +require_once(DOL_DOCUMENT_ROOT.'/actioncomm.class.php'); +require_once(DOL_DOCUMENT_ROOT."/lib/order.lib.php"); +if ($conf->projet->enabled) require_once(DOL_DOCUMENT_ROOT.'/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.'/propal.class.php'); + +$langs->load('orders'); +$langs->load('sendings'); +$langs->load('companies'); +$langs->load('bills'); +$langs->load('propal'); +$langs->load('deliveries'); +$langs->load('products'); + +if (!$user->rights->commande->lire) accessforbidden(); + + +// Securite acces client +$socid=0; +if ($user->societe_id > 0) +{ + $socid = $user->societe_id; +} +if ($user->societe_id >0 && isset($_GET["id"]) && $_GET["id"]>0) +{ + $commande = new Commande($db); + $commande->fetch((int)$_GET['id']); + if ($user->societe_id != $commande->socid) { + accessforbidden(); + } +} + +// Recuperation de l'id de projet +$projetid = 0; +if ($_GET["projetid"]) +{ + $projetid = $_GET["projetid"]; +} + + +/******************************************************************************/ +/* Actions */ +/******************************************************************************/ + +// Suppression de la commande +if ($_REQUEST['action'] == 'confirm_delete' && $_REQUEST['confirm'] == 'yes') +{ + if ($user->rights->commande->supprimer ) + { + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $commande->delete($user); + Header('Location: index.php'); + exit; + } +} + +/* + * Supprime une ligne produit AVEC ou SANS confirmation + */ +if (($_REQUEST['action'] == 'confirm_deleteline' && $_REQUEST['confirm'] == 'yes' && $conf->global->PRODUIT_CONFIRM_DELETE_LINE) +|| ($_GET['action'] == 'deleteline' && !$conf->global->PRODUIT_CONFIRM_DELETE_LINE)) +{ + if ($user->rights->commande->creer) + { + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $result = $commande->delete_line($_GET['lineid']); + if ($result > 0) + { + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + commande_pdf_create($db, $_GET['id'], $commande->modelpdf, $outputlangs); + } + else + { + print $commande->error; + } + } + Header('Location: '.$_SERVER["PHP_SELF"].'?id='.$_GET['id']); + exit; +} + +// Categorisation dans projet +if ($_POST['action'] == 'classin') +{ + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $commande->setProject($_POST['projetid']); +} + +// Ajout commande +if ($_POST['action'] == 'add' && $user->rights->commande->creer) +{ + $datecommande=''; + $datecommande = dolibarr_mktime(12, 0, 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); + $datelivraison = dolibarr_mktime(12, 0, 0, $_POST['liv_month'],$_POST['liv_day'],$_POST['liv_year']); + + $commande = new Commande($db); + $commande->socid=$_POST['socid']; + $commande->fetch_client(); + + $db->begin(); + + $commande->date_commande = $datecommande; + $commande->note = $_POST['note']; + $commande->note_public = $_POST['note_public']; + $commande->source = $_POST['source_id']; + $commande->projetid = $_POST['projetid']; + $commande->ref_client = $_POST['ref_client']; + $commande->modelpdf = $_POST['model']; + $commande->cond_reglement_id = $_POST['cond_reglement_id']; + $commande->mode_reglement_id = $_POST['mode_reglement_id']; + $commande->date_livraison = $datelivraison; + $commande->adresse_livraison_id = $_POST['adresse_livraison_id']; + $commande->contactid = $_POST['contactidp']; + + $NBLINES=8; + for ($i = 1 ; $i <= $NBLINES ; $i++) + { + if ($_POST['idprod'.$i]) + { + $xid = 'idprod'.$i; + $xqty = 'qty'.$i; + $xremise = 'remise_percent'.$i; + $commande->add_product($_POST[$xid],$_POST[$xqty],$_POST[$xremise]); + } + } + + $commande_id = $commande->create($user); + + if ($commande_id > 0) + { + // Insertion contact par defaut si defini + if ($_POST["contactidp"]) + { + $result=$commande->add_contact($_POST["contactidp"],'CUSTOMER','external'); + + if ($result > 0) + { + $error=0; + } + else + { + $mesg = '
'.$langs->trans("ErrorFailedToAddContact").'
'; + $error=1; + } + } + + $_GET['id'] = $commande->id; + $action = ''; + } + + // Fin creation facture, on l'affiche + if ($commande_id > 0 && ! $error) + { + $db->commit(); + } + else + { + $db->rollback(); + $_GET["action"]='create'; + $_GET['socid']=$_POST['socid']; + if (! $mesg) $mesg='
'.$commande->error.'
'; + } + +} + +// Positionne ref commande client +if ($_POST['action'] == 'set_ref_client' && $user->rights->commande->creer) +{ + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $commande->set_ref_client($user, $_POST['ref_client']); +} + +if ($_POST['action'] == 'setremise' && $user->rights->commande->creer) +{ + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $commande->set_remise($user, $_POST['remise']); +} + +if ($_POST['action'] == "setabsolutediscount" && $user->rights->commande->creer) +{ + if ($_POST["remise_id"]) + { + $com = new Commande($db); + $com->id=$_GET['id']; + $ret=$com->fetch($_GET['id']); + if ($ret > 0) + { + $com->insert_discount($_POST["remise_id"]); + } + else + { + dolibarr_print_error($db,$com->error); + } + } +} + +if ($_POST['action'] == 'setdate_livraison' && $user->rights->commande->creer) +{ + //print "x ".$_POST['liv_month'].", ".$_POST['liv_day'].", ".$_POST['liv_year']; + $datelivraison=dolibarr_mktime(0, 0, 0, $_POST['liv_month'], $_POST['liv_day'], $_POST['liv_year']); + + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $result=$commande->set_date_livraison($user,$datelivraison); + if ($result < 0) + { + $mesg='
'.$commande->error.'
'; + } +} + +if ($_POST['action'] == 'setdeliveryadress' && $user->rights->commande->creer) +{ + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $commande->set_adresse_livraison($user,$_POST['adresse_livraison_id']); +} + +if ($_POST['action'] == 'setmode' && $user->rights->commande->creer) +{ + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $result=$commande->mode_reglement($_POST['mode_reglement_id']); + if ($result < 0) dolibarr_print_error($db,$commande->error); +} + +if ($_POST['action'] == 'setconditions' && $user->rights->commande->creer) +{ + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $result=$commande->cond_reglement($_POST['cond_reglement_id']); + if ($result < 0) dolibarr_print_error($db,$commande->error); +} + +if ($_REQUEST['action'] == 'setremisepercent' && $user->rights->facture->creer) +{ + $commande = new Commande($db); + $commande->fetch($_REQUEST['id']); + $result = $commande->set_remise($user, $_POST['remise_percent']); + $_GET['id']=$_REQUEST['id']; +} + +if ($_REQUEST['action'] == 'setremiseabsolue' && $user->rights->facture->creer) +{ + $commande = new Commande($db); + $commande->fetch($_REQUEST['id']); + $result = $commande->set_remise_absolue($user, $_POST['remise_absolue']); + $_GET['id']=$_REQUEST['id']; +} + +/* + * Ajout d'une ligne produit dans la commande + */ +if ($_POST['action'] == 'addligne' && $user->rights->commande->creer) +{ + if ($_POST['qty'] && (($_POST['pu'] != '' && ($_POST['np_desc'] || $_POST['dp_desc'])) || $_POST['idprod'])) + { + $commande = new Commande($db); + $ret=$commande->fetch($_POST['id']); + if ($ret < 0) + { + dolibarr_print_error($db,$commande->error); + exit; + } + $ret=$commande->fetch_client(); + + $price_base_type = 'HT'; + + // Ecrase $pu par celui du produit + // Ecrase $desc par celui du produit + // Ecrase $txtva par celui du produit + // Ecrase $base_price_type par celui du produit + if ($_POST['idprod']) + { + $prod = new Product($db, $_POST['idprod']); + $prod->fetch($_POST['idprod']); + + $tva_tx = get_default_tva($mysoc,$commande->client,$prod->tva_tx); + + // multiprix + if ($conf->global->PRODUIT_MULTIPRICES == 1) + { + $pu_ht = $prod->multiprices[$commande->client->price_level]; + $pu_ttc = $prod->multiprices_ttc[$commande->client->price_level]; + $price_base_type = $prod->multiprices_base_type[$commande->client->price_level]; + } + else + { + $pu_ht = $prod->price; + $pu_ttc = $prod->price_ttc; + $price_base_type = $prod->price_base_type; + } + + // On reevalue prix selon taux tva car taux tva transaction peut etre different + // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur). + if ($tva_tx != $prod->tva_tx) + { + if ($price_base_type != 'HT') + { + $pu_ht = price2num($pu_ttc / (1 + ($tva_tx/100)), 'MU'); + } + else + { + $pu_ttc = price2num($pu_ht * (1 + ($tva_tx/100)), 'MU'); + } + } + + $desc = $prod->description; + $desc.= $prod->description && $_POST['np_desc'] ? "\n" : ""; + $desc.= $_POST['np_desc']; + } + else + { + $pu_ht=$_POST['pu']; + $tva_tx=eregi_replace('\*','',$_POST['tva_tx']); + $tva_npr=eregi('\*',$_POST['tva_tx'])?1:0; + $desc=$_POST['dp_desc']; + } + + $info_bits=0; + if ($tva_npr) $info_bits |= 0x01; + + // Insert line + $result = $commande->addline( + $_POST['id'], + $desc, + $pu_ht, + $_POST['qty'], + $tva_tx, + $_POST['idprod'], + $_POST['remise_percent'], + $info_bits, + '', + $price_base_type, + $pu_ttc + ); + + if ($result > 0) + { + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); + } + else + { + $mesg='
'.$commande->error.'
'; + } + } +} + +/* + * Mise a jour d'une ligne dans la commande + */ +if ($_POST['action'] == 'updateligne' && $user->rights->commande->creer && $_POST['save'] == $langs->trans('Save')) +{ + $commande = new Commande($db,'',$_POST['id']); + if (! $commande->fetch($_POST['id']) > 0) dolibarr_print_error($db); + + // Define info_bits + $info_bits=0; + if (eregi('\*',$_POST['tva_tx'])) $info_bits |= 0x01; + + // Define vat_rate + $vat_rate=$_POST['tva_tx']; + $vat_rate=eregi_replace('\*','',$vat_rate); + + $result = $commande->updateline($_POST['elrowid'], + $_POST['eldesc'], + $_POST['pu'], + $_POST['qty'], + $_POST['elremise_percent'], + $vat_rate, + 'HT', + $info_bits + ); + + if ($result >= 0) + { + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); + } + else + { + dolibarr_print_error($db,$commande->error); + exit; + } + + $_GET['id']=$_POST['id']; // Pour reaffichage de la fiche en cours d'edition +} + +if ($_POST['action'] == 'updateligne' && $user->rights->commande->creer && $_POST['cancel'] == $langs->trans('Cancel')) +{ + Header('Location: fiche.php?id='.$_POST['id']); // Pour reaffichage de la fiche en cours d'edition + exit; +} + +if ($_REQUEST['action'] == 'confirm_validate' && $_REQUEST['confirm'] == 'yes' && $user->rights->commande->valider) +{ + $commande = new Commande($db); + $commande->fetch($_GET['id']); + + $result=$commande->valid($user); + if ($result >= 0) + { + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); + } +} + +if ($_REQUEST['action'] == 'confirm_close' && $_REQUEST['confirm'] == 'yes' && $user->rights->commande->creer) +{ + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $result = $commande->cloture($user); +} + +if ($_REQUEST['action'] == 'confirm_cancel' && $_REQUEST['confirm'] == 'yes' && $user->rights->commande->valider) +{ + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $result = $commande->cancel($user); +} + +if ($_GET['action'] == 'modif' && $user->rights->commande->creer) +{ + /* + * Repasse la commande en mode brouillon + */ + $commande = new Commande($db); + $commande->fetch($_GET['id']); + $commande->set_draft($user); + + $result = $commande->set_draft($user); + if ($result >= 0) + { //regeneration pdf + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); + } +} + +/* + * Ordonnancement des lignes + */ + +if ($_GET['action'] == 'up' && $user->rights->commande->creer) +{ + $commande = new Commande($db,'',$_GET['id']); + $commande->fetch($_GET['id']); + $commande->line_up($_GET['rowid']); + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); + Header ('Location: '.$_SERVER["PHP_SELF"].'?id='.$_GET["id"].'#'.$_GET['rowid']); + exit; +} + +if ($_GET['action'] == 'down' && $user->rights->commande->creer) +{ + $commande = new Commande($db,'',$_GET['id']); + $commande->fetch($_GET['id']); + $commande->line_down($_GET['rowid']); + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); + Header ('Location: '.$_SERVER["PHP_SELF"].'?id='.$_GET["id"].'#'.$_GET['rowid']); + exit; +} + +if ($_REQUEST['action'] == 'builddoc') // In get or post +{ + /* + * Generate order document + * define into /includes/modules/commande/modules_commande.php + */ + + // Sauvegarde le dernier modele choisi pour generer un document + $commande = new Commande($db, 0, $_REQUEST['id']); + $result=$commande->fetch($_REQUEST['id']); + if ($_REQUEST['model']) + { + $commande->setDocModel($user, $_REQUEST['model']); + } + + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + + $result=commande_pdf_create($db, $commande->id, $commande->modelpdf, $outputlangs); + if ($result <= 0) + { + dolibarr_print_error($db,$result); + exit; + } + else + { + Header ('Location: '.$_SERVER["PHP_SELF"].'?id='.$commande->id.'#builddoc'); + exit; + } +} + +// Efface les fichiers +if ($_REQUEST['action'] == 'remove_file') +{ + $com = new Commande($db); + + if ($com->fetch($id)) + { + $upload_dir = $conf->commande->dir_output . "/"; + $file = $upload_dir . '/' . urldecode($_GET['file']); + dol_delete_file($file); + $mesg = '
'.$langs->trans("FileWasRemoved").'
'; + } +} + +/* + * Add file + */ +if ($_POST['addfile']) +{ + // Set tmp user directory + $conf->users->dir_tmp=DOL_DATA_ROOT."/users/".$user->id; + $upload_dir = $conf->users->dir_tmp.'/temp/'; + + if (! empty($_FILES['addedfile']['tmp_name'])) + { + if (! is_dir($upload_dir)) create_exdir($upload_dir); + + if (is_dir($upload_dir)) + { + if (dol_move_uploaded_file($_FILES['addedfile']['tmp_name'], $upload_dir . "/" . $_FILES['addedfile']['name'],0) > 0) + { + $mesg = '
'.$langs->trans("FileTransferComplete").'
'; + //print_r($_FILES); + + include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); + $formmail = new FormMail($db); + $formmail->add_attached_files($upload_dir . "/" . $_FILES['addedfile']['name'],$_FILES['addedfile']['name'],$_FILES['addedfile']['type']); + } + else + { + // Echec transfert (fichier d�passant la limite ?) + $mesg = '
'.$langs->trans("ErrorFileNotUploaded").'
'; + // print_r($_FILES); + } + } + } + $_GET["action"]='presend'; +} + +/* + * Send mail + */ +if ($_POST['action'] == 'send' && ! $_POST['addfile'] && ! $_POST['cancel']) +{ + $langs->load('mails'); + + $commande= new Commande($db); + $result=$commande->fetch($_POST['orderid']); + if ($result) + { + $ref = sanitize_string($commande->ref); + $file = $conf->commande->dir_output . '/' . $ref . '/' . $ref . '.pdf'; + + if (is_readable($file)) + { + $commande->fetch_client(); + + if ($_POST['sendto']) + { + // Le destinataire a ete fourni via le champ libre + $sendto = $_POST['sendto']; + $sendtoid = 0; + } + elseif ($_POST['receiver']) + { + // Le destinataire a ete fourni via la liste deroulante + if ($_POST['receiver'] < 0) // Id du tiers + { + $sendto = $commande->client->email; + $sendtoid = 0; + } + else // Id du contact + { + $sendto = $commande->client->contact_get_email($_POST['receiver']); + $sendtoid = $_POST['receiver']; + } + } + + if (strlen($sendto)) + { + $langs->load("commercial"); + + $from = $_POST['fromname'] . ' <' . $_POST['frommail'] .'>'; + $replyto = $_POST['replytoname']. ' <' . $_POST['replytomail'].'>'; + $message = $_POST['message']; + $sendtocc = $_POST['sendtocc']; + $deliveryreceipt = $_POST['deliveryreceipt']; + + if ($_POST['action'] == 'send') + { + if (strlen($_POST['subject'])) $subject=$_POST['subject']; + else $subject = $langs->transnoentities('Order').' '.$commande->ref; + $actiontypecode='AC_COM'; + $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto.".\n"; + if ($message) + { + $actionmsg.=$langs->transnoentities('MailTopic').": ".$subject."\n"; + $actionmsg.=$langs->transnoentities('TextUsedInTheMessageBody').":\n"; + $actionmsg.=$message; + } + $actionmsg2=$langs->transnoentities('Action'.$actiontypecode); + } + + // Create form object + include_once('../html.formmail.class.php'); + $formmail = new FormMail($db); + + $attachedfiles=$formmail->get_attached_files(); + $filepath = $attachedfiles['paths']; + $filename = $attachedfiles['names']; + $mimetype = $attachedfiles['mimes']; + + // Send mail + require_once(DOL_DOCUMENT_ROOT.'/lib/CMailFile.class.php'); + $mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc,'',$deliveryreceipt); + if ($mailfile->error) + { + $mesg='
'.$mailfile->error.'
'; + } + else + { + $result=$mailfile->sendfile(); + if ($result) + { + $mesg='
'.$langs->trans('MailSuccessfulySent',$from,$sendto).'.
'; + + $error=0; + + // Initialisation donnees + $commande->sendtoid=$sendtoid; + $commande->actiontypecode=$actiontypecode; + $commande->actionmsg = $actionmsg; + $commande->actionmsg2= $actionmsg2; + $commande->orderrowid=$commande->id; + + // Appel des triggers + include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); + $interface=new Interfaces($db); + $result=$interface->run_triggers('ORDER_SENTBYMAIL',$commande,$user,$langs,$conf); + if ($result < 0) { $error++; $this->errors=$interface->errors; } + // Fin appel triggers + + if ($error) + { + dolibarr_print_error($db); + } + else + { + // Renvoie sur la fiche + Header('Location: '.$_SERVER["PHP_SELF"].'?id='.$commande->id.'&msg='.urlencode($mesg)); + exit; + } + } + else + { + $langs->load("other"); + $mesg='
'; + if ($mailfile->error) + { + $mesg.=$langs->trans('ErrorFailedToSendMail',$from,$sendto); + $mesg.='
'.$mailfile->error; + } + else + { + $mesg.='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS'; + } + $mesg.='
'; + } + } + } + else + { + $langs->load("other"); + $mesg='
'.$langs->trans('ErrorMailRecipientIsEmpty').' !
'; + $_GET["action"]='presend'; + dolibarr_syslog('Recipient email is empty'); + } + } + else + { + $langs->load("other"); + $mesg='
'.$langs->trans('ErrorCantReadFile',$file).'
'; + dolibarr_syslog('Failed to read file: '.$file); + } + } + else + { + $langs->load("other"); + $mesg='
'.$langs->trans('ErrorFailedToReadEntity',$langs->trans("Invoice")).'
'; + dolibarr_syslog('Impossible de lire les donnees de la facture. Le fichier facture n\'a peut-etre pas ete genere.'); + } +} + + +/* + * View + */ + +llxHeader('',$langs->trans('Order'),'Commande'); + +$html = new Form($db); +$formfile = new FormFile($db); + +/********************************************************************* + * + * Mode creation + * + *********************************************************************/ +if ($_GET['action'] == 'create' && $user->rights->commande->creer) +{ + print_titre($langs->trans('CreateOrder')); + + if ($mesg) print $mesg.'
'; + + $new_commande = new Commande($db); + + if ($propalid) + { + $sql = 'SELECT s.nom, s.prefix_comm, s.rowid'; + $sql.= ', p.price, p.remise, p.remise_percent, p.tva, p.total, p.ref, p.fk_cond_reglement, p.fk_mode_reglement'; + $sql.= ', '.$db->pdate('p.datep').' as dp'; + $sql.= ', c.id as statut, c.label as lst'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'societe as s, '.MAIN_DB_PREFIX.'propal as p, '.MAIN_DB_PREFIX.'c_propalst as c'; + $sql .= ' WHERE p.fk_soc = s.rowid AND p.fk_statut = c.id'; + $sql .= ' AND p.rowid = '.$propalid; + } + else + { + $sql = 'SELECT s.nom, s.prefix_comm, s.rowid, s.mode_reglement, s.cond_reglement '; + $sql .= 'FROM '.MAIN_DB_PREFIX.'societe as s '; + $sql .= 'WHERE s.rowid = '.$_GET['socid']; + } + $resql = $db->query($sql); + if ( $resql ) + { + $num = $db->num_rows($resql); + if ($num) + { + $obj = $db->fetch_object($resql); + + $soc = new Societe($db); + $soc->fetch($obj->rowid); + + $nbrow=10; + + print '
'; + print ''; + print '' ."\n"; + print ''; + print ''; + + print ''; + + // Reference + print ''; + + // Reference client + print ''; + print ''; + + // Client + print ''; + + /* + * Contact de la commande + */ + print "'; + + // Ligne info remises tiers + print ''; + + // Date + print ''; + + // Date de livraison + print ""; + + // Adresse de livraison + print ''; + + // Conditions de reglement + print ''; + + // Mode de reglement + print ''; + + // Projet + if ($conf->projet->enabled) + { + $projetid=$_POST["projetid"]?$_POST["projetid"]:$commande->projetid; + print ''; + } + + print ''; + + print ''; + print '"; + + // Note publique + print ''; + print ''; + print ''; + + // Note priv�e + if (! $user->societe_id) + { + print ''; + print ''; + print ''; + } + + if ($propalid > 0) + { + $amount = ($obj->price); + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''; + + print ''; + print ''; + print ''; + print ''; + } + else + { + if ($conf->global->PRODUCT_SHOW_WHEN_CREATE) + { + /* + * Services/produits predefinis + */ + $NBLINES=8; + + print ''; + } + } + + /* + * + */ + print ''; + print '
'.$langs->trans('Ref').''.$langs->trans("Draft").'
'.$langs->trans('RefCustomer').''; + print '
'.$langs->trans('Customer').''.$soc->getNomUrl(1).'
".$langs->trans("DefaultContact").''; + $html->select_contacts($soc->id,$setcontact,'contactidp',1); + print '
'.$langs->trans('Discounts').''; + if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client); + else print $langs->trans("CompanyHasNoRelativeDiscount"); + $absolute_discount=$soc->getAvailableDiscounts(); + print '. '; + if ($absolute_discount) print $langs->trans("CompanyHasAbsoluteDiscount",price($absolute_discount),$langs->trans("Currency".$conf->monnaie)); + else print $langs->trans("CompanyHasNoAbsoluteDiscount"); + print '.'; + print '
'.$langs->trans('Date').''; + $html->select_date('','re','','','',"crea_commande"); + print '
".$langs->trans("DeliveryDate").""; + if ($conf->global->DATE_LIVRAISON_WEEK_DELAY) + { + $tmpdte = time() + ((7*$conf->global->DATE_LIVRAISON_WEEK_DELAY) * 24 * 60 * 60); + $html->select_date($tmpdte,'liv_','','',1,"crea_commande"); + } + else + { + $dateorder=empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0; + $html->select_date($dateorder,'liv_','','',1,"crea_commande"); + } + print "
'.$langs->trans('DeliveryAddress').''; + $numaddress = $html->select_adresse_livraison($soc->adresse_livraison_id, $_GET['socid'],'adresse_livraison_id',1); + + if ($numaddress==0) + { + print '   '.$langs->trans("AddAddress").''; + } + + print '
'.$langs->trans('PaymentConditionsShort').''; + $html->select_conditions_paiements($soc->cond_reglement,'cond_reglement_id',-1,1); + print '
'.$langs->trans('PaymentMode').''; + $html->select_types_paiements($soc->mode_reglement,'mode_reglement_id'); + print '
'.$langs->trans('Project').''; + $numprojet=select_projects($soc->id,$projetid,'projetid'); + if ($numprojet==0) + { + print '   '.$langs->trans("AddProject").''; + } + print '
'.$langs->trans('Source').''; + $html->selectSourcesCommande('','source_id',1); + print '
'.$langs->trans('Model').''; + // pdf + include_once(DOL_DOCUMENT_ROOT.'/includes/modules/commande/modules_commande.php'); + $model=new ModelePDFCommandes(); + $liste=$model->liste_modeles($db); + $html->select_array('model',$liste,$conf->global->COMMANDE_ADDON_PDF); + print "
'.$langs->trans('NotePublic').''; + print '
'.$langs->trans('NotePrivate').''; + print '
'.$langs->trans('Ref').''.$obj->ref.'
'.$langs->trans('TotalTTC').''.price($amount).'
'.$langs->trans('VAT').''.price($obj->tva).'
'.$langs->trans('TotalTTC').''.price($obj->total).'
'; + + print ''; + print ''; + print ''; + print ''; + print ''; + for ($i = 1 ; $i <= $NBLINES ; $i++) + { + print ''; + print ''; + print ''; + } + + print '
'.$langs->trans('ProductsAndServices').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').'
'; + // multiprix + if($conf->global->PRODUIT_MULTIPRICES == 1) + print $html->select_produits('','idprod'.$i,'',$conf->produit->limit_size,$soc->price_level); + else + print $html->select_produits('','idprod'.$i,'',$conf->produit->limit_size); + print '%
'; + print '
'; + + print '
'; + + if ($propalid) + { + /* + * Produits + */ + print_titre($langs->trans('Products')); + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + $var=false; + + $sql = 'SELECT pt.rowid, p.label as product, p.ref, pt.price, pt.qty, p.rowid as prodid, pt.remise_percent'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt, '.MAIN_DB_PREFIX.'product as p WHERE pt.fk_product = p.rowid AND pt.fk_propal = '.$propalid; + $sql .= ' ORDER BY pt.rowid ASC'; + $result = $db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + $i = 0; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + } + + $sql = 'SELECT pt.rowid, pt.description as product, pt.price, pt.qty, pt.remise_percent'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt WHERE pt.fk_propal = '.$propalid.' AND pt.fk_product = 0'; + $sql .= ' ORDER BY pt.rowid ASC'; + if ($db->query($sql)) + { + $num = $db->num_rows(); + $i = 0; + while ($i < $num) + { + $objp = $db->fetch_object(); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + } + else + { + dolibarr_print_error($db); + } + + print '
'.$langs->trans('Ref').''.$langs->trans('Product').''.$langs->trans('Price').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').'
['.$objp->ref.']'.img_object($langs->trans('ShowProduct'),'product').' '.$objp->product.''.price($objp->price).''.$objp->qty.'
'.$objp->remise_percent.'%
 '.img_object($langs->trans('ShowProduct'),'product').' '.$objp->product.''.price($objp->price).''.$objp->qty.'
'.$objp->remise_percent.'%
'; + } + } + } + else + { + dolibarr_print_error($db); + } +} +else +{ + /* *************************************************************************** */ + /* */ + /* Mode vue et edition */ + /* */ + /* *************************************************************************** */ + $id = $_GET['id']; + if ($id > 0) + { + if ($mesg) print $mesg.'
'; + + $commande = new Commande($db); + if ( $commande->fetch($_GET['id']) > 0) + { + $soc = new Societe($db); + $soc->fetch($commande->socid); + + $author = new User($db); + $author->id = $commande->user_author_id; + $author->fetch(); + + $head = commande_prepare_head($commande); + dolibarr_fiche_head($head, 'order', $langs->trans("CustomerOrder")); + + /* + * Confirmation de la suppression de la commande + */ + if ($_GET['action'] == 'delete') + { + $html->form_confirm($_SERVER["PHP_SELF"].'?id='.$id, $langs->trans('DeleteOrder'), $langs->trans('ConfirmDeleteOrder'), 'confirm_delete'); + print '
'; + } + + /* + * Confirmation de la validation + */ + if ($_GET['action'] == 'validate') + { + // on verifie si la facture est en numerotation provisoire + $ref = substr($commande->ref, 1, 4); + if ($ref == 'PROV') + { + $num = $commande->getNextNumRef($soc); + } + else + { + $num = $commande->ref; + } + + $text=$langs->trans('ConfirmValidateOrder',$num); + $html->form_confirm($_SERVER["PHP_SELF"].'?id='.$id, $langs->trans('ValidateOrder'), $text, 'confirm_validate'); + print '
'; + } + + /* + * Confirmation de la cloture + */ + if ($_GET['action'] == 'close') + { + $html->form_confirm($_SERVER["PHP_SELF"].'?id='.$id, $langs->trans('CloseOrder'), $langs->trans('ConfirmCloseOrder'), 'confirm_close'); + print '
'; + } + + /* + * Confirmation de l'annulation + */ + if ($_GET['action'] == 'cancel') + { + $html->form_confirm($_SERVER["PHP_SELF"].'?id='.$id, $langs->trans('Cancel'), $langs->trans('ConfirmCancelOrder'), 'confirm_cancel'); + print '
'; + } + + /* + * Confirmation de la suppression d'une ligne produit + */ + if ($_GET['action'] == 'ask_deleteline' && $conf->global->PRODUIT_CONFIRM_DELETE_LINE) + { + $html->form_confirm($_SERVER["PHP_SELF"].'?id='.$id.'&lineid='.$_GET["lineid"], $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteline'); + print '
'; + } + + /* + * Commande + */ + $nbrow=8; + if ($conf->projet->enabled) $nbrow++; + + print ''; + + // Ref + print ''; + print ''; + print ''; + + // Ref commande client + print ''; + print ''; + + + // Societe + print ''; + print ''; + print ''; + + // Ligne info remises tiers + print ''; + + // Date + print ''; + print ''; + print ''; + print ''; + + // Date de livraison + if ($conf->expedition->enabled) + { + print ''; + print ''; + print ''; + + + // Adresse de livraison + print ''; + } + + // Conditions et modes de reglement + print ''; + + //Note public lorsque le module expedition n'est pas active + if (!$conf->projet->enabled) $nbrow--; + if (!$conf->expedition->enabled) + { + $nbrow--; + if ($conf->projet->enabled) $nbrow--; + print ''; + } + + print ''; + print ''; + + // Projet + if ($conf->projet->enabled) + { + $langs->load('projects'); + print ''; + } + + // Lignes de 3 colonnes + + // Total HT + print ''; + print ''; + print ''; + + // Total TVA + print ''; + print ''; + + // Total TTC + print ''; + print ''; + + // Statut + print ''; + print ''; + print ''; + + print '
'.$langs->trans('Ref').''.$commande->ref.'
'; + print ''; + if ($_GET['action'] != 'RefCustomerOrder' && $commande->brouillon) print ''; + print '
'; + print $langs->trans('RefCustomer').''; + print ''.img_edit($langs->trans('Modify')).'
'; + print '
'; + if ($user->rights->commande->creer && $_GET['action'] == 'RefCustomerOrder') + { + print '
'; + print ''; + print ''; + print ' '; + print '
'; + } + else + { + print $commande->ref_client; + } + print '
'.$langs->trans('Company').''.$soc->getNomUrl(1).'
'.$langs->trans('Discounts').''; + if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client); + else print $langs->trans("CompanyHasNoRelativeDiscount"); + $absolute_discount=$soc->getAvailableDiscounts('','fk_facture_source IS NULL'); + $absolute_creditnote=$soc->getAvailableDiscounts('','fk_facture_source IS NOT NULL'); + print '. '; + if ($absolute_discount) + { + if ($commande->statut > 0) + { + print $langs->trans("CompanyHasAbsoluteDiscount",$absolute_discount,$langs->transnoentities("Currency".$conf->monnaie)); + } + else + { + // Remise dispo de type non avoir + $filter='fk_facture_source IS NULL'; + print '
'; + $html->form_remise_dispo($_SERVER["PHP_SELF"].'?id='.$commande->id,0,'remise_id',$soc->id,$absolute_discount,$filter); + } + } + if ($absolute_creditnote) + { + print $langs->trans("CompanyHasCreditNote",price($absolute_creditnote),$langs->transnoentities("Currency".$conf->monnaie)).'. '; + } + if (! $absolute_discount && ! $absolute_creditnote) print $langs->trans("CompanyHasNoAbsoluteDiscount").'.'; + print '
'.$langs->trans('Date').''.dolibarr_print_date($commande->date,'daytext').''.$langs->trans('Source').' : '.$commande->getLabelSource(); + if ($commande->source == 0 && $conf->propal->enabled && $commande->propale_id) + { + // Si source = propal + $propal = new Propal($db); + $propal->fetch($commande->propale_id); + print ' -> '.$propal->ref.''; + } + print '
'; + print ''; + + if ($_GET['action'] != 'editdate_livraison' && $commande->brouillon) print ''; + print '
'; + print $langs->trans('DeliveryDate'); + print 'id.'">'.img_edit($langs->trans('SetDeliveryDate'),1).'
'; + print '
'; + if ($_GET['action'] == 'editdate_livraison') + { + print '
'; + print ''; + $html->select_date($commande->date_livraison,'liv_','','','',"setdate_livraison"); + print ''; + print '
'; + } + else + { + print $commande->date_livraison ? dolibarr_print_date($commande->date_livraison,'daytext') : ' '; + } + print '
'.$langs->trans('NotePublic').' :
'; + print nl2br($commande->note_public); + print '
'; + print ''; + + if ($_GET['action'] != 'editdelivery_adress' && $commande->brouillon) print ''; + print '
'; + print $langs->trans('DeliveryAddress'); + print 'socid.'&id='.$commande->id.'">'.img_edit($langs->trans('SetDeliveryAddress'),1).'
'; + print '
'; + + if ($_GET['action'] == 'editdelivery_adress') + { + $html->form_adresse_livraison($_SERVER['PHP_SELF'].'?id='.$commande->id,$commande->adresse_livraison_id,$_GET['socid'],'adresse_livraison_id','commande',$commande->id); + } + else + { + $html->form_adresse_livraison($_SERVER['PHP_SELF'].'?id='.$commande->id,$commande->adresse_livraison_id,$_GET['socid'],'none','commande',$commande->id); + } + print '
'; + print ''; + + if ($_GET['action'] != 'editconditions' && $commande->brouillon) print ''; + print '
'; + print $langs->trans('PaymentConditionsShort'); + print 'id.'">'.img_edit($langs->trans('SetConditions'),1).'
'; + print '
'; + if ($_GET['action'] == 'editconditions') + { + $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$commande->id,$commande->cond_reglement_id,'cond_reglement_id'); + } + else + { + $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$commande->id,$commande->cond_reglement_id,'none'); + } + print ''.$langs->trans('NotePublic').' :
'; + print nl2br($commande->note_public); + print '
'; + print ''; + if ($_GET['action'] != 'editmode' && $commande->brouillon) print ''; + print '
'; + print $langs->trans('PaymentMode'); + print 'id.'">'.img_edit($langs->trans('SetMode'),1).'
'; + print '
'; + if ($_GET['action'] == 'editmode') + { + $html->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$commande->id,$commande->mode_reglement_id,'mode_reglement_id'); + } + else + { + $html->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$commande->id,$commande->mode_reglement_id,'none'); + } + print '
'; + print ''; + if ($_GET['action'] != 'classer' && $commande->brouillon) print ''; + print '
'; + print $langs->trans('Project'); + print ''.img_edit($langs->trans('SetProject')).'
'; + print '
'; + if ($_GET['action'] == 'classer') + { + $html->form_project($_SERVER['PHP_SELF'].'?id='.$commande->id, $commande->socid, $commande->projet_id, 'projetid'); + } + else + { + $html->form_project($_SERVER['PHP_SELF'].'?id='.$commande->id, $commande->socid, $commande->projet_id, 'none'); + } + print '
'.$langs->trans('AmountHT').''.price($commande->total_ht).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('AmountVAT').''.price($commande->total_tva).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('AmountTTC').''.price($commande->total_ttc).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('Status').''.$commande->getLibStatut(4).'

'; + print "\n"; + + /* + * Lignes de commandes + */ + $sql = 'SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_tx, '; + $sql.= ' l.fk_remise_except, l.remise_percent, l.subprice, l.info_bits,'; + $sql.= ' l.total_ht, l.total_tva, l.total_ttc,'; + $sql.= ' p.label as product, p.ref, p.fk_product_type, p.rowid as prodid, '; + $sql.= ' p.description as product_desc'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as l'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product=p.rowid'; + $sql.= ' WHERE l.fk_commande = '.$commande->id; + $sql.= ' ORDER BY l.rang ASC, l.rowid'; + + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + $i = 0; $total = 0; + + print ''; + if ($num) + { + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print "\n"; + } + $var=true; + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + + // Ligne en mode visu + if ($_GET['action'] != 'editline' || $_GET['rowid'] != $objp->rowid) + { + print ''; + if ($objp->fk_product > 0) + { + print ''; + } + else + { + print ''; + } + + print ''; + print ''; + print ''; + if ($objp->remise_percent > 0) + { + print ''; + } + else + { + print ''; + } + print ''; + + // Icone d'edition et suppression + if ($commande->statut == 0 && $user->rights->commande->creer) + { + print ''; + print ''; + if ($num > 1) + { + print ''; + } + } + else + { + print ''; + } + print ''; + } + + // Ligne en mode update + if ($_GET['action'] == 'editline' && $user->rights->commande->creer && $_GET['rowid'] == $objp->rowid) + { + print 'rowid.'" method="post">'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + } + + $total = $total + ($objp->qty * $objp->price); + $i++; + } + $db->free($resql); + + $numlines=$num; + } + else + { + dolibarr_print_error($db); + } + + /* + * Ajouter une ligne + */ + if ($commande->statut == 0 && $user->rights->commande->creer && $_GET["action"] <> 'editline') + { + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + // Ajout produit produits/services personnalises + print ''; + print ''; + print ''; + + $var=true; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + + // Ajout de produits/services predefinis + if ($conf->produit->enabled) + { + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + } + } + print '
'.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').''.$langs->trans('AmountHT').' 
'; + print ''; // ancre pour retourner sur la ligne + + // Affiche ligne produit + $text = ''; + if ($objp->fk_product_type==1) $text.= img_object($langs->trans('ShowService'),'service'); + else $text.= img_object($langs->trans('ShowProduct'),'product'); + $text.= ' '.$objp->ref.''; + $text.= ' - '.$objp->product; + $description=($conf->global->PRODUIT_DESC_IN_FORM?'':dol_htmlentitiesbr($objp->description)); + print $html->textwithtooltip($text,$description,3,'','',$i); + // Todo: voir si on insert ou pas en option les dates de debut et de fin de service + //print_date_range($objp->date_start,$objp->date_end); + if ($conf->global->PRODUIT_DESC_IN_FORM) + { + print ($objp->description && $objp->description!=$objp->product)?'
'.dol_htmlentitiesbr($objp->description):''; + } + + print '
'; + print ''; // ancre pour retourner sur la ligne + if (($objp->info_bits & 2) == 2) + { + print ''; + print img_object($langs->trans("ShowReduc"),'reduc').' '.$langs->trans("Discount"); + print ''; + if ($objp->description) + { + if ($objp->description == '(CREDIT_NOTE)') + { + require_once(DOL_DOCUMENT_ROOT.'/discount.class.php'); + $discount=new DiscountAbsolute($db); + $discount->fetch($objp->fk_remise_except); + print ' - '.$langs->transnoentities("DiscountFromCreditNote",$discount->getNomUrl(0)); + } + else + { + print ' - '.nl2br($objp->description); + } + } + } + else + { + print nl2br($objp->description); + } + print ''.vatrate($objp->tva_tx,'%',$objp->info_bits).''.price($objp->subprice).''; + if (($objp->info_bits & 2) != 2) + { + print $objp->qty; + } + else print ' '; + print ''.dolibarr_print_reduction($objp->remise_percent).' '.price($objp->total_ht).''; + if (($objp->info_bits & 2) == 2) + { + // Ligne remise predefinie, on ne permet pas modif + } + else + { + print 'rowid.'#'.$objp->rowid.'">'; + print img_edit(); + print ''; + } + print ''; + if ($conf->global->PRODUIT_CONFIRM_DELETE_LINE) + { + if ($conf->use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) + { + $url = $_SERVER["PHP_SELF"].'?id='.$id.'&lineid='.$objp->rowid.'&action=confirm_deleteline&confirm=yes'; + print 'trans("No").'\',\'deleteline'.$i.'\')">'; + } + else + { + print 'rowid.'">'; + } + } + else + { + print 'rowid.'">'; + } + print img_delete(); + print ''; + if ($i > 0) + { + print 'rowid.'">'; + print img_up(); + print ''; + } + if ($i < $num-1) + { + print 'rowid.'">'; + print img_down(); + print ''; + } + print ' 
'; + print ''; // ancre pour retourner sur la ligne + if ($objp->fk_product > 0) + { + print ''; + if ($objp->fk_product_type==1) print img_object($langs->trans('ShowService'),'service'); + else print img_object($langs->trans('ShowProduct'),'product'); + print ' '.$objp->ref.''; + print ' - '.nl2br($objp->product); + print '
'; + } + // editeur wysiwyg + if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) + { + require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); + $doleditor=new DolEditor('eldesc',$objp->description,200,'dolibarr_details'); + $doleditor->Create(); + } + else + { + print ''; + } + print '
'; + if($soc->tva_assuj == "0") + print '0'; + else + print $html->select_tva('tva_tx',$objp->tva_tx,$mysoc,$soc); + print ''; + if (($objp->info_bits & 2) != 2) + { + print ''; + } + else print ' '; + print ''; + if (($objp->info_bits & 2) != 2) + { + print '%'; + } + else print ' '; + print ''; + print '
'; + print ''; // ancre + print $langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').' 
'; + // editeur wysiwyg + if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) + { + require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); + $doleditor=new DolEditor('dp_desc','',100,'dolibarr_details'); + $doleditor->Create(); + } + else + { + print ''; + } + print ''; + if($soc->tva_assuj == "0") + print '0'; + else + print $html->select_tva('tva_tx',$conf->defaulttx,$mysoc,$soc); + print '%
'; + if ($conf->service->enabled) + { + print $langs->trans('RecordedProductsAndServices'); + } + else + { + print $langs->trans('RecordedProducts'); + } + print ''.$langs->trans('Qty').''.$langs->trans('ReductionShort').' 
'; + // multiprix + if($conf->global->PRODUIT_MULTIPRICES == 1) + { + $html->select_produits('','idprod','',$conf->produit->limit_size,$soc->price_level); + } + else + { + $html->select_produits('','idprod','',$conf->produit->limit_size); + } + + if (! $conf->global->PRODUIT_USE_SEARCH_TO_SELECT) print '
'; + + // editeur wysiwyg + if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) + { + require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); + $doleditor=new DolEditor('np_desc','',100,'dolibarr_details'); + $doleditor->Create(); + } + else + { + print ''; + } + + print '
%
'; + print ''; + + + /* + * Boutons actions + */ + if ($_GET['action'] != 'presend') + { + if ($user->societe_id == 0 && $_GET['action'] <> 'editline') + { + print '
'; + + // Valid + if ($commande->statut == 0 && $commande->total_ttc >= 0 && $numlines > 0 && $user->rights->commande->valider) + { + print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) + { + // on verifie si la commande est en numerotation provisoire + $ref = substr($commande->ref, 1, 4); + if ($ref == 'PROV') + { + $num = $commande->getNextNumRef($soc); + } + else + { + $num = $commande->ref; + } + $url = $_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=confirm_validate&confirm=yes'; + print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.dol_escape_js($langs->trans('ConfirmValidateOrder',$num)).'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'validate\')"'; + } + else + { + print 'href="'.$_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=validate"'; + } + print '>'.$langs->trans('Validate').''; + } + + // Edit + if ($commande->statut == 1) + { + if ($user->rights->commande->creer) + { + print ''.$langs->trans('Modify').''; + } + } + + // Send + if ($commande->statut > 0) + { + if ($user->rights->commande->envoyer) + { + $comref = sanitize_string($commande->ref); + $file = $conf->commande->dir_output . '/'.$comref.'/'.$comref.'.pdf'; + if (file_exists($file)) + { + print ''.$langs->trans('SendByMail').''; + } + } + } + + // Ship + if ($commande->statut > 0 && $commande->statut < 3 && $user->rights->expedition->creer + && $commande->getNbOfProductsLines() > 0) + { + + // Chargement des permissions + $error = $user->load_entrepots(); + if (sizeof($user->entrepots) === 1) + { + print ''; + print $langs->trans('ShipProduct').''; + + } + else + { + print ''.$langs->trans('ShipProduct').''; + } + } + + // Cloturer + if ($commande->statut == 1 || $commande->statut == 2) + { + if ($user->rights->commande->cloturer) + { + print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) + { + $url = $_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=confirm_close&confirm=yes'; + print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.$langs->trans('ConfirmCloseOrder').'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'close\')"'; + } + else + { + print 'href="'.$_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=close"'; + } + print '>'.$langs->trans('Close').''; + } + } + + // Annuler commande + if ($commande->statut == 1) + { + $nb_expedition = $commande->nb_expedition(); + if ($user->rights->commande->annuler && $nb_expedition == 0) + { + print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) + { + $url = $_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=confirm_cancel&confirm=yes'; + print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.$langs->trans('ConfirmCancelOrder').'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'cancel\')"'; + } + else + { + print 'href="'.$_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=cancel"'; + } + print '>'.$langs->trans('CancelOrder').''; + } + } + + // Supprimer commande + if ($commande->statut == 0 && $user->rights->commande->supprimer) + { + print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) + { + $url = $_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=confirm_delete&confirm=yes'; + print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.$langs->trans('ConfirmDeleteOrder').'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'delete\')"'; + } + else + { + print 'href="'.$_SERVER["PHP_SELF"].'?id='.$commande->id.'&action=delete"'; + } + print '>'.$langs->trans('Delete').''; + } + + print '
'; + } + print '
'; + } + + + if ($_GET['action'] != 'presend') + { + print '
'; + print ''; // ancre + + /* + * Documents generes + * + */ + $comref = sanitize_string($commande->ref); + $file = $conf->commande->dir_output . '/' . $comref . '/' . $comref . '.pdf'; + $relativepath = $comref.'/'.$comref.'.pdf'; + $filedir = $conf->commande->dir_output . '/' . $comref; + $urlsource=$_SERVER["PHP_SELF"]."?id=".$commande->id; + $genallowed=$user->rights->commande->creer; + $delallowed=$user->rights->commande->supprimer; + + $somethingshown=$formfile->show_documents('commande',$comref,$filedir,$urlsource,$genallowed,$delallowed,$commande->modelpdf); + + /* + * Liste des factures + */ + $sql = 'SELECT f.rowid,f.facnumber, f.total_ttc, '.$db->pdate('f.datef').' as df'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'facture as f, '.MAIN_DB_PREFIX.'co_fa as cf'; + $sql .= ' WHERE f.rowid = cf.fk_facture AND cf.fk_commande = '. $commande->id; + + $result = $db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + if ($num) + { + print '
'; + print_titre($langs->trans('RelatedBills')); + $i = 0; $total = 0; + print ''; + print '"; + print ''; + print ''; + print ''; + + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + $i++; + } + print '
'.$langs->trans('Ref')."'.$langs->trans('Date').''.$langs->trans('Price').'
'.img_object($langs->trans('ShowBill'),'bill').' '.$objp->facnumber.''.dolibarr_print_date($objp->df,'day').''.$objp->total_ttc.'
'; + } + } + else + { + dolibarr_print_error($db); + } + print '
'; + + // List of actions on element + include_once(DOL_DOCUMENT_ROOT.'/html.formactions.class.php'); + $formactions=new FormActions($db); + $somethingshown=$formactions->showactions($commande,'order',$socid); + + print '
'; + } + + + /* + * Action presend + * + */ + if ($_GET['action'] == 'presend') + { + $ref = sanitize_string($commande->ref); + $file = $conf->commande->dir_output . '/' . $ref . '/' . $ref . '.pdf'; + + print '
'; + print_titre($langs->trans('SendOrderByMail')); + + $soc = new Societe($db); + $soc->fetch($commande->socid); + + $liste[0]=" "; + foreach ($soc->thirdparty_and_contact_email_array() as $key=>$value) + { + $liste[$key]=$value; + } + + // Cree l'objet formulaire mail + include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); + $formmail = new FormMail($db); + $formmail->fromtype = 'user'; + $formmail->fromid = $user->id; + $formmail->fromname = $user->fullname; + $formmail->frommail = $user->email; + $formmail->withfrom=1; + $formmail->withto=$liste; + $formmail->withtocc=1; + $formmail->withtopic=$langs->trans('SendOrderRef','__ORDERREF__'); + $formmail->withfile=2; + $formmail->withbody=1; + $formmail->withdeliveryreceipt=1; + $formmail->withcancel=1; + // Tableau des substitutions + $formmail->substit['__ORDERREF__']=$commande->ref; + // Tableau des parametres complementaires + $formmail->param['action']='send'; + $formmail->param['models']='order_send'; + $formmail->param['orderid']=$commande->id; + $formmail->param['returnurl']=DOL_URL_ROOT.'/commande/fiche.php?id='.$commande->id; + + // Init list of files + if (! empty($_REQUEST["mode"]) && $_REQUEST["mode"]=='init') + { + $formmail->clear_attached_files(); + $formmail->add_attached_files($file,$ref.'.pdf','application/pdf'); + } + + // Show form + $formmail->show_form(); + + print '
'; + } + } + else + { + // Commande non trouvee + dolibarr_print_error($db); + } + } +} + +$db->close(); + +llxFooter('$Date$ - $Revision$'); +?> diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index abf0b8bd0df..87c32fead81 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -1,3524 +1,3494 @@ - - * Copyright (C) 2004 Eric Seigne - * Copyright (C) 2004-2008 Laurent Destailleur - * Copyright (C) 2005 Marc Barilley / Ocebo - * Copyright (C) 2005-2007 Regis Houssin - * Copyright (C) 2006 Andre Cianfarani - * Copyright (C) 2008 Raphael Bertrand (Resultic) - * - * 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. - */ - -/** - * \file htdocs/compta/facture.php - * \ingroup facture - * \brief Page de création/visu facture - * \version $Id$ - */ - -require('./pre.inc.php'); -require_once(DOL_DOCUMENT_ROOT."/html.formfile.class.php"); -require_once(DOL_DOCUMENT_ROOT.'/includes/modules/facture/modules_facture.php'); -require_once(DOL_DOCUMENT_ROOT.'/facture.class.php'); -require_once(DOL_DOCUMENT_ROOT.'/discount.class.php'); -require_once(DOL_DOCUMENT_ROOT.'/paiement.class.php'); -require_once(DOL_DOCUMENT_ROOT.'/lib/invoice.lib.php'); -if ($conf->projet->enabled) require_once(DOL_DOCUMENT_ROOT.'/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.'/propal.class.php'); -if ($conf->contrat->enabled) require_once(DOL_DOCUMENT_ROOT.'/contrat/contrat.class.php'); -if ($conf->commande->enabled) require_once(DOL_DOCUMENT_ROOT.'/commande/commande.class.php'); - -if (! $user->rights->facture->lire) -accessforbidden(); - -$langs->load('bills'); -$langs->load('companies'); -$langs->load('products'); -$langs->load('main'); - -$sall=isset($_GET['sall'])?trim($_GET['sall']):trim($_POST['sall']); -$mesg=isset($_GET['mesg'])?urldecode($_GET['mesg']):''; -$socid=isset($_GET['socid'])?$_GET['socid']:$_POST['socid']; -$projetid=isset($_GET['projetid'])?$_GET['projetid']:0; - -// Security check -$facid = isset($_GET["id"])?$_GET["id"]:''; -if ($user->societe_id) $socid=$user->societe_id; -$result = restrictedArea($user, 'facture', $facid,''); - -// Nombre de ligne pour choix de produit/service prédéfinis -$NBLINES=4; - - -/******************************************************************************/ -/* Actions */ -/******************************************************************************/ - -if ($_GET['action'] == 'reopen' && $user->rights->facture->creer) -{ - $fac = new Facture($db); - $result = $fac->fetch($_GET['facid']); - if ($fac->statut == 3 && ($fac->close_code == 'badcustomer' || $fac->close_code == 'abandon')) - { - $result = $fac->set_unpayed($user); - if ($result > 0) - { - Header('Location: '.$_SERVER["PHP_SELF"].'?facid='.$_GET['facid']); - exit; - } - else - { - $mesg='
'.$fac->error.'
'; - } - } -} - -// Suppression de la facture -if ($_POST['action'] == 'confirm_delete' && $_POST['confirm'] == 'yes' && $user->rights->facture->supprimer) -{ - if ($user->rights->facture->supprimer) - { - $fac = new Facture($db); - $result = $fac->fetch($_GET['facid']); - $result = $fac->delete(); - if ($result > 0) - { - Header('Location: '.$_SERVER["PHP_SELF"]); - exit; - } - else - { - $mesg='
'.$fac->error.'
'; - } - } -} - -/* - * Supprime une ligne produit AVEC ou SANS confirmation - */ -if (($_POST['action'] == 'confirm_deleteproductline' && $_POST['confirm'] == 'yes' && $conf->global->PRODUIT_CONFIRM_DELETE_LINE) -|| ($_GET['action'] == 'deleteline' && !$conf->global->PRODUIT_CONFIRM_DELETE_LINE)) -{ - if ($user->rights->facture->creer) - { - $fac = new Facture($db); - $fac->fetch($_GET['facid']); - $result = $fac->deleteline($_GET['rowid'], $user); - if ($result > 0) - { - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - - $result=facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); - if ($result > 0) - { - Header('Location: '.$_SERVER["PHP_SELF"].'?facid='.$_GET['facid']); - exit; - } - } - else - { - $mesg='
'.$fac->error.'
'; - $_GET['action']=''; - } - } -} - -// Supprime affectation d'un avoir a la facture -if ($_GET['action'] == 'unlinkdiscount') -{ - if ($user->rights->facture->creer) - { - $discount=new DiscountAbsolute($db); - $result=$discount->fetch($_GET["discountid"]); - $discount->unlink_invoice(); - } -} - -// Validation -if ($_GET['action'] == 'valid') -{ - $facture = new Facture($db); - $facture->fetch($_GET['facid']); - - // On verifie signe facture - if ($facture->type == 2) - { - // Si avoir, le signe doit etre négatif - if ($facture->total_ht >= 0) - { - $mesg='
'.$langs->trans("ErrorInvoiceAvoirMustBeNegative").'
'; - $_GET['action']=''; - } - } - else - { - // Si non avoir, le signe doit etre positif - if ($facture->total_ht < 0) - { - $mesg='
'.$langs->trans("ErrorInvoiceOfThisTypeMustBePositive").'
'; - $_GET['action']=''; - } - } -} - -if ($_POST['action'] == 'classin') -{ - $facture = new Facture($db); - $facture->fetch($_GET['facid']); - $facture->setProject($_POST['projetid']); -} - -if ($_POST['action'] == 'setmode') -{ - $facture = new Facture($db); - $facture->fetch($_GET['facid']); - $result=$facture->mode_reglement($_POST['mode_reglement_id']); - if ($result < 0) dolibarr_print_error($facture->db,$facture->error); -} - -if ($_POST['action'] == 'setconditions') -{ - $facture = new Facture($db); - $facture->fetch($_GET['facid']); - $result=$facture->cond_reglement($_POST['cond_reglement_id']); - if ($result < 0) dolibarr_print_error($facture->db,$facture->error); -} - -if ($_REQUEST['action'] == 'setremisepercent' && $user->rights->facture->creer) -{ - // Define remise_percent - if ($_POST['remise_percent']>0) - $remise_percent=$_POST['remise_percent']; - else - $remise_percent=0; - - $fac = new Facture($db); - $fac->fetch($_REQUEST['facid']); - $result = $fac->set_remise($user, $remise_percent); - $_GET['facid']=$_REQUEST['facid']; -} - -if ($_POST['action'] == "setabsolutediscount" && $user->rights->facture->creer) -{ - // POST[remise_id] ou POST[remise_id_for_payment] - if (! empty($_POST["remise_id"])) - { - $fac = new Facture($db); - $fac->id=$_GET['facid']; - $ret=$fac->fetch($_GET['facid']); - if ($ret > 0) - { - $result=$fac->insert_discount($_POST["remise_id"]); - if ($result < 0) - { - $mesg='
'.$fac->error.'
'; - } - } - else - { - dolibarr_print_error($db,$fac->error); - } - } - if (! empty($_POST["remise_id_for_payment"])) - { - require_once(DOL_DOCUMENT_ROOT.'/discount.class.php'); - $discount = new DiscountAbsolute($db); - $discount->fetch($_POST["remise_id_for_payment"]); - - $result=$discount->link_to_invoice(0,$_GET['facid']); - if ($result < 0) - { - $mesg='
'.$discount->error.'
'; - } - } -} - -if ($_POST['action'] == 'set_ref_client') -{ - $facture = new Facture($db); - $facture->fetch($_GET['facid']); - $facture->set_ref_client($_POST['ref_client']); -} - -// Classe à "validée" -if ($_REQUEST['action'] == 'confirm_valid' && $_REQUEST['confirm'] == 'yes' && $user->rights->facture->valider) -{ - $fac = new Facture($db); - $fac->fetch($_GET['facid']); - $fac->fetch_client(); - - $result = $fac->set_valid($user); - if ($result >= 0) - { - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); - } - else - { - $mesg='
'.$fac->error.'
'; - } -} - -// Repasse la facture en mode brouillon -if ($_GET['action'] == 'modif' && $user->rights->facture->modifier && $conf->global->FACTURE_ENABLE_EDITDELETE) -{ - $fac = new Facture($db); - $fac->fetch($_GET['facid']); - - // On vérifie si la facture a des paiements - $sql = 'SELECT pf.amount'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf'; - $sql.= ' WHERE pf.fk_facture = '.$fac->id; - - $result = $db->query($sql); - - if ($result) - { - $i = 0; - $num = $db->num_rows($result); - - while ($i < $num) - { - $objp = $db->fetch_object($result); - $totalpaye += $objp->amount; - $i++; - } - } - - $resteapayer = $fac->total_ttc - $totalpaye; - - // On vérifie si les lignes de factures ont été exportées en compta et/ou ventilées - $ventilExportCompta = $fac->getVentilExportCompta(); - - // On vérifie si aucun paiement n'a été effectué - if ($resteapayer == $fac->total_ttc && $fac->paye == 0 && $ventilExportCompta == 0) - { - $fac->set_draft($user); - //regénération PDF - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); - } -} - -// Classe à "payée" -if ($_POST['action'] == 'confirm_payed' && $_POST['confirm'] == 'yes' && $user->rights->facture->paiement) -{ - $fac = new Facture($db); - $fac->fetch($_GET['facid']); - $result = $fac->set_payed($user); -} -// Classe à "payée partiellement" -if ($_POST['action'] == 'confirm_payed_partially' && $_POST['confirm'] == 'yes' && $user->rights->facture->paiement) -{ - $fac = new Facture($db); - $fac->fetch($_GET['facid']); - $close_code=$_POST["close_code"]; - $close_note=$_POST["close_note"]; - if ($close_code) - { - $result = $fac->set_payed($user,$close_code,$close_note); - } - else - { - $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("Reason")).'
'; - } -} -// Classe à "abandonnée" -if ($_POST['action'] == 'confirm_canceled' && $_POST['confirm'] == 'yes') -{ - $fac = new Facture($db); - $fac->fetch($_GET['facid']); - $close_code=$_POST["close_code"]; - $close_note=$_POST["close_note"]; - if ($close_code) - { - $result = $fac->set_canceled($user,$close_code,$close_note); - } - else - { - $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("Reason")).'
'; - } -} - -// Convertir en reduc -if ($_POST['action'] == 'confirm_converttoreduc' && $_POST['confirm'] == 'yes' && $user->rights->facture->creer) -{ - $db->begin(); - - $fac = new Facture($db); - $fac->fetch($_GET['facid']); - $fac->fetch_client(); - $fac->fetch_lines(); - - if (! $fac->paye) // protection against multiple submit - { - // Boucle sur chaque taux de tva - $i=0; - foreach($fac->lignes as $ligne) - { - $amount_ht[$ligne->tva_tx]+=$ligne->total_ht; - $amount_tva[$ligne->tva_tx]+=$ligne->total_tva; - $amount_ttc[$ligne->tva_tx]+=$ligne->total_ttc; - $i++; - } - - // Insère une remise par famille de taux tva - $discount = new DiscountAbsolute($db); - $discount->desc='(CREDIT_NOTE)'; - $discount->tva_tx=abs($fac->total_ttc); - $discount->fk_soc=$fac->socid; - $discount->fk_facture_source=$fac->id; - - $error=0; - foreach($amount_ht as $tva_tx => $xxx) - { - $discount->amount_ht=abs($amount_ht[$tva_tx]); - $discount->amount_tva=abs($amount_tva[$tva_tx]); - $discount->amount_ttc=abs($amount_ttc[$tva_tx]); - $discount->tva_tx=abs($tva_tx); - - $result=$discount->create($user); - if ($result < 0) - { - $error++; - break; - } - } - - if (! $error) - { - // Classe facture - $result=$fac->set_payed($user); - if ($result > 0) - { - //$mesg='OK'.$discount->id; - $db->commit(); - } - else - { - $mesg='
'.$fac->error.'
'; - $db->rollback(); - } - } - else - { - $mesg='
'.$discount->error.'
'; - $db->rollback(); - } - } -} - - - -/* - * Insert invoice - */ -if ($_POST['action'] == 'add' && $user->rights->facture->creer) -{ - $facture = new Facture($db); - $facture->socid=$_POST['socid']; - $facture->fetch_client(); - - // Define remise_percent - if ($_POST['remise_percent']>0) - $remise_percent=$_POST['remise_percent']; - else - $remise_percent=0; - - $db->begin(); - - // Facture remplacement - if ($_POST['type'] == 1) - { - $datefacture = dolibarr_mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); - if (empty($datefacture)) - { - $error=1; - $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("Date")).'
'; - } - - if (! ($_POST['fac_replacement'] > 0)) - { - $error=1; - $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("ReplaceInvoice")).'
'; - } - - if (! $error) - { - // Si facture remplacement - - $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 = $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); - } - } - - // Facture avoir - if ($_POST['type'] == 2) - { - if (! $_POST['fac_avoir'] > 0) - { - $error=1; - $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("CorrectInvoice")).'
'; - } - - $datefacture = dolibarr_mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); - if (empty($datefacture)) - { - $error=1; - $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("Date")).'
'; - } - - if (! $error) - { - // Si facture avoir - $datefacture = dolibarr_mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); - - //$result=$facture->fetch($_POST['fac_avoir']); - - $facture->socid = $_POST['socid']; - $facture->number = $_POST['facnumber']; - $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 = 0; - $facture->mode_reglement_id = $_POST['mode_reglement_id']; - $facture->remise_absolue = $_POST['remise_absolue']; - $facture->remise_percent = $remise_percent; - - // Propriétés particulieres a facture avoir - $facture->fk_facture_source = $_POST['fac_avoir']; - $facture->type = 2; - - $facid = $facture->create($user); - } - } - - if ($_POST['type'] == 0 && $_POST['fac_rec'] > 0) - { - // Si facture récurrente - $datefacture = dolibarr_mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); - - $facture->socid = $_POST['socid']; - $facture->type = $_POST['type']; - $facture->number = $_POST['facnumber']; - $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']; - - // Propriétés particulieres a facture recurrente - $facture->fac_rec = $_POST['fac_rec']; - $facture->type = 0; - - $facid = $facture->create($user); - } - - if ($_POST['type'] == 0 && $_POST['fac_rec'] <= 0) - { - $datefacture = dolibarr_mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); - if (empty($datefacture)) - { - $error=1; - $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("Date")).'
'; - } - - if (! $error) - { - // Si facture standard - - $facture->socid = $_POST['socid']; - $facture->type = $_POST['type']; - $facture->number = $_POST['facnumber']; - $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->amount = $_POST['amount']; - $facture->remise_absolue = $_POST['remise_absolue']; - $facture->remise_percent = $remise_percent; - - if (! $_POST['propalid'] && ! $_POST['commandeid'] && ! $_POST['contratid']) - { - for ($i = 1; $i <= $NBLINES; $i++) - { - if ($_POST['idprod'.$i]) - { - $startday=''; - $endday=''; - if ($_POST['date_start'.$i.'year'] && $_POST['date_start'.$i.'month'] && $_POST['date_start'.$i.'day']) { - $startday=$_POST['date_start'.$i.'year'].'-'.$_POST['date_start'.$i.'month'].'-'.$_POST['date_start'.$i.'day']; - } - if ($_POST['date_end'.$i.'year'] && $_POST['date_end'.$i.'month'] && $_POST['date_end'.$i.'day']) { - $endday=$_POST['date_end'.$i.'year'].'-'.$_POST['date_end'.$i.'month'].'-'.$_POST['date_end'.$i.'day']; - } - // Define remise_percent - if ($_POST['remise_percent'.$i]>0) - $remise_percent_i=$_POST['remise_percent'.$i]; - else - $remise_percent_i=0; - $facture->add_product($_POST['idprod'.$i],$_POST['qty'.$i],$remise_percent_i,$startday,$endday); - } - } - - $facid = $facture->create($user); - } - else - { - /* - * Si creation depuis propale - */ - if ($_POST['propalid']) - { - $facture->propalid = $_POST['propalid']; - $facid = $facture->create($user); - - if ($facid > 0) - { - $prop = New Propal($db); - if ( $prop->fetch($_POST['propalid']) ) - { - for ($i = 0 ; $i < sizeof($prop->lignes) ; $i++) - { - $desc=($prop->lignes[$i]->desc?$prop->lignes[$i]->desc:$prop->lignes[$i]->libelle); - - $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, - '', - '', - 0, - $prop->lignes[$i]->info_bits, - $prop->lignes[$i]->fk_remise_except - ); - - if ($result < 0) - { - $error++; - break; - } - } - } - else - { - $error++; - } - } - else - { - $error++; - } - } - - /* - * Si création depuis commande - */ - 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; - for ($i = 0 ; $i < sizeof($lines) ; $i++) - { - $desc=($lines[$i]->desc ? $lines[$i]->desc : $lines[$i]->libelle); - - $result = $facture->addline( - $facid, - $desc, - $lines[$i]->subprice, - $lines[$i]->qty, - $lines[$i]->tva_tx, - $lines[$i]->fk_product, - $lines[$i]->remise_percent, - '', - '', - 0, - $lines[$i]->info_bits, - $lines[$i]->fk_remise_except - ); - - if ($result < 0) - { - $error++; - break; - } - } - } - else - { - $error++; - } - } - else - { - $error++; - } - } - - /* - * Si création depuis contrat - */ - 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); - - // Plage de 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 - ); - - if ($result < 0) - { - $error++; - break; - } - } - } - else - { - $error++; - } - } - else - { - $error++; - } - } - } - } - } - - // Fin création facture, on l'affiche - if ($facid > 0 && ! $error) - { - $db->commit(); - Header('Location: '.$_SERVER["PHP_SELF"].'?facid='.$facid); - exit; - } - else - { - $db->rollback(); - $_GET["action"]='create'; - $_GET["propalid"]=$_POST["propalid"]; - $_GET["commandeid"]=$_POST["commandeid"]; - $_GET["contratid"]=$_POST["contratid"]; - if (! $mesg) $mesg='
'.$facture->error.'
'; - } -} - -/* - * Ajout d'une ligne produit dans la facture - */ -if (($_POST['action'] == 'addligne' || $_POST['action'] == 'addligne_predef') && $user->rights->facture->creer) -{ - if ($_POST['qty'] && (($_POST['pu']!='' && ($_POST['np_desc'] || $_POST['dp_desc'])) || $_POST['idprod'])) - { - $fac = new Facture($db); - $ret=$fac->fetch($_POST['facid']); - if ($ret < 0) - { - dolibarr_print_error($db,$fac->error); - exit; - } - $ret=$fac->fetch_client(); - - $date_start=''; - $date_end=''; - // Si ajout champ produit libre - if ($_POST['action'] == 'addligne') - { - if ($_POST['date_startyear'] && $_POST['date_startmonth'] && $_POST['date_startday']) - { - $date_start=$_POST['date_startyear'].'-'.$_POST['date_startmonth'].'-'.$_POST['date_startday']; - } - if ($_POST['date_endyear'] && $_POST['date_endmonth'] && $_POST['date_endday']) - { - $date_end=$_POST['date_endyear'].'-'.$_POST['date_endmonth'].'-'.$_POST['date_endday']; - } - } - // Si ajout champ produit prédéfini - if ($_POST['action'] == 'addligne_predef') - { - if ($_POST['date_start_predefyear'] && $_POST['date_start_predefmonth'] && $_POST['date_start_predefday']) - { - $date_start=$_POST['date_start_predefyear'].'-'.$_POST['date_start_predefmonth'].'-'.$_POST['date_start_predefday']; - } - if ($_POST['date_end_predefyear'] && $_POST['date_end_predefmonth'] && $_POST['date_end_predefday']) - { - $date_end=$_POST['date_end_predefyear'].'-'.$_POST['date_end_predefmonth'].'-'.$_POST['date_end_predefday']; - } - } - - $price_base_type = 'HT'; - - // Ecrase $pu par celui du produit - // Ecrase $desc par celui du produit - // Ecrase $txtva par celui du produit - // Ecrase $base_price_type par celui du produit - if ($_POST['idprod']) - { - $prod = new Product($db, $_POST['idprod']); - $prod->fetch($_POST['idprod']); - - $tva_tx = get_default_tva($mysoc,$fac->client,$prod->tva_tx); - $tva_npr = get_default_npr($mysoc,$fac->client,$prod->tva_tx); - - // On defini prix unitaire - if ($conf->global->PRODUIT_MULTIPRICES == 1) - { - $pu_ht = $prod->multiprices[$fac->client->price_level]; - $pu_ttc = $prod->multiprices_ttc[$fac->client->price_level]; - $price_base_type = $prod->multiprices_base_type[$fac->client->price_level]; - } - else - { - $pu_ht = $prod->price; - $pu_ttc = $prod->price_ttc; - $price_base_type = $prod->price_base_type; - } - - // On reevalue prix selon taux tva car taux tva transaction peut etre different - // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur). - if ($tva_tx != $prod->tva_tx) - { - if ($price_base_type != 'HT') - { - $pu_ht = price2num($pu_ttc / (1 + ($tva_tx/100)), 'MU'); - } - else - { - $pu_ttc = price2num($pu_ht * (1 + ($tva_tx/100)), 'MU'); - } - } - - $desc = $prod->description; - $desc.= $prod->description && $_POST['np_desc'] ? "\n" : ""; - $desc.= $_POST['np_desc']; - } - else - { - $pu_ht=$_POST['pu']; - $tva_tx=eregi_replace('\*','',$_POST['tva_tx']); - $tva_npr=eregi('\*',$_POST['tva_tx'])?1:0; - $desc=$_POST['dp_desc']; - } - - $info_bits=0; - if ($tva_npr) $info_bits |= 0x01; - - // Define remise_percent - if ($_POST['remise_percent']>0) - $remise_percent=$_POST['remise_percent']; - else - $remise_percent=0; - - // Insert line - $result = $fac->addline( - $_POST['facid'], - $desc, - $pu_ht, - $_POST['qty'], - $tva_tx, - $_POST['idprod'], - $remise_percent, - $date_start, - $date_end, - 0, - $info_bits, - '', - $price_base_type, - $pu_ttc - ); - } - - if ($result > 0) - { - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - facture_pdf_create($db, $fac->id, $fac->modelpdf, $outputlangs); - } - else - { - $mesg='
'.$fac->error.'
'; - } - - $_GET['facid']=$_POST['facid']; // Pour réaffichage de la fiche en cours d'édition -} - -if ($_POST['action'] == 'updateligne' && $user->rights->facture->creer && $_POST['save'] == $langs->trans('Save')) -{ - $fac = new Facture($db,'',$_POST['facid']); - if (! $fac->fetch($_POST['facid']) > 0) dolibarr_print_error($db); - - $date_start=''; - $date_end=''; - if ($_POST['date_startyear'] && $_POST['date_startmonth'] && $_POST['date_startday']) { - $date_start=$_POST['date_startyear'].'-'.$_POST['date_startmonth'].'-'.$_POST['date_startday']; - } - if ($_POST['date_endyear'] && $_POST['date_endmonth'] && $_POST['date_endday']) { - $date_end=$_POST['date_endyear'].'-'.$_POST['date_endmonth'].'-'.$_POST['date_endday']; - } - - // Define info_bits - $info_bits=0; - if (eregi('\*',$_POST['tva_tx'])) $info_bits |= 0x01; - - // Define vat_rate - $vat_rate=$_POST['tva_tx']; - $vat_rate=eregi_replace('\*','',$vat_rate); - - // Define remise_percent - if ($_POST['remise_percent']>0) - $remise_percent=$_POST['remise_percent']; - else - $remise_percent=0; - - $result = $fac->updateline($_POST['rowid'], - $_POST['desc'], - $_POST['price'], - $_POST['qty'], - $remise_percent, - $date_start, - $date_end, - $vat_rate, - 'HT', - $info_bits - ); - - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); - - $_GET['facid']=$_POST['facid']; // Pour réaffichage de la fiche en cours d'édition -} - -if ($_POST['action'] == 'updateligne' && $user->rights->facture->creer && $_POST['cancel'] == $langs->trans('Cancel')) -{ - Header('Location: '.$_SERVER["PHP_SELF"].'?facid='.$_POST['facid']); // Pour réaffichage de la fiche en cours d'édition - exit; -} - - -/* - * Ordonnancement des lignes - */ - -if ($_GET['action'] == 'up' && $user->rights->facture->creer) -{ - $fac = new Facture($db,'',$_GET['facid']); - $fac->fetch($_GET['facid']); - $fac->line_up($_GET['rowid']); - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); - Header ('Location: '.$_SERVER["PHP_SELF"].'?facid='.$_GET["facid"].'#'.$_GET['rowid']); - exit; -} - -if ($_GET['action'] == 'down' && $user->rights->facture->creer) -{ - $fac = new Facture($db,'',$_GET['facid']); - $fac->fetch($_GET['facid']); - $fac->line_down($_GET['rowid']); - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); - Header ('Location: '.$_SERVER["PHP_SELF"].'?facid='.$_GET["facid"].'#'.$_GET['rowid']); - exit; -} - -/* - * Add file - */ -if ($_POST['addfile']) -{ - // Set tmp user directory - $conf->users->dir_tmp=DOL_DATA_ROOT."/users/".$user->id; - $upload_dir = $conf->users->dir_tmp.'/temp/'; - - if (! empty($_FILES['addedfile']['tmp_name'])) - { - if (! is_dir($upload_dir)) create_exdir($upload_dir); - - if (is_dir($upload_dir)) - { - if (dol_move_uploaded_file($_FILES['addedfile']['tmp_name'], $upload_dir . "/" . $_FILES['addedfile']['name'],0) > 0) - { - $mesg = '
'.$langs->trans("FileTransferComplete").'
'; - //print_r($_FILES); - - include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); - $formmail = new FormMail($db); - $formmail->add_attached_files($upload_dir . "/" . $_FILES['addedfile']['name'],$_FILES['addedfile']['name'],$_FILES['addedfile']['type']); - } - else - { - // Echec transfert (fichier dépassant la limite ?) - $mesg = '
'.$langs->trans("ErrorFileNotUploaded").'
'; - // print_r($_FILES); - } - } - } - $_GET["action"]='presend'; -} - -/* - * Send mail - */ -if (($_POST['action'] == 'send' || $_POST['action'] == 'relance') && ! $_POST['addfile'] && ! $_POST['cancel']) -{ - $langs->load('mails'); - - $fac = new Facture($db,'',$_POST['facid']); - $result=$fac->fetch($_POST['facid']); - if ($result) - { - $ref = sanitize_string($fac->ref); - $file = $conf->facture->dir_output . '/' . $ref . '/' . $ref . '.pdf'; - - if (is_readable($file)) - { - $fac->fetch_client(); - - if ($_POST['sendto']) - { - // Le destinataire a été fourni via le champ libre - $sendto = $_POST['sendto']; - $sendtoid = 0; - } - elseif ($_POST['receiver']) - { - // Le destinataire a été fourni via la liste déroulante - if ($_POST['receiver'] < 0) // Id du tiers - { - $sendto = $fac->client->email; - $sendtoid = 0; - } - else // Id du contact - { - $sendto = $fac->client->contact_get_email($_POST['receiver']); - $sendtoid = $_POST['receiver']; - } - } - - if (strlen($sendto)) - { - $langs->load("commercial"); - - $from = $_POST['fromname'] . ' <' . $_POST['frommail'] .'>'; - $replyto = $_POST['replytoname']. ' <' . $_POST['replytomail'].'>'; - $message = $_POST['message']; - $sendtocc = $_POST['sendtocc']; - $deliveryreceipt = $_POST['deliveryreceipt']; - - if ($_POST['action'] == 'send') - { - if (strlen($_POST['subject'])) $subject = $_POST['subject']; - else $subject = $langs->transnoentities('Bill').' '.$fac->ref; - $actiontypecode='AC_FAC'; - $actionmsg=$langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto.".\n"; - if ($message) - { - $actionmsg.=$langs->transnoentities('MailTopic').": ".$subject."\n"; - $actionmsg.=$langs->transnoentities('TextUsedInTheMessageBody').":\n"; - $actionmsg.=$message; - } - $actionmsg2=$langs->transnoentities('Action'.$actiontypecode); - } - if ($_POST['action'] == 'relance') - { - if (strlen($_POST['subject'])) $subject = $_POST['subject']; - else $subject = $langs->transnoentities('Relance facture '.$fac->ref); - $actiontypecode='AC_FAC'; - $actionmsg=$langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto.".\n"; - if ($message) { - $actionmsg.=$langs->transnoentities('MailTopic').": ".$subject."\n"; - $actionmsg.=$langs->transnoentities('TextUsedInTheMessageBody').":\n"; - $actionmsg.=$message; - } - $actionmsg2=$langs->transnoentities('Action'.$actiontypecode); - } - - // Create form object - include_once('../html.formmail.class.php'); - $formmail = new FormMail($db); - - $attachedfiles=$formmail->get_attached_files(); - $filepath = $attachedfiles['paths']; - $filename = $attachedfiles['names']; - $mimetype = $attachedfiles['mimes']; - - // Send mail - require_once(DOL_DOCUMENT_ROOT.'/lib/CMailFile.class.php'); - $mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc,'',$deliveryreceipt); - if ($mailfile->error) - { - $mesg='
'.$mailfile->error.'
'; - } - else - { - $result=$mailfile->sendfile(); - if ($result) - { - $mesg='
'.$langs->trans('MailSuccessfulySent',$from,$sendto).'.
'; - - $error=0; - - // Initialisation donnees - $fac->sendtoid=$sendtoid; - $fac->actiontypecode=$actiontypecode; - $fac->actionmsg = $actionmsg; - $fac->actionmsg2= $actionmsg2; - $fac->facid=$fac->id; - - // Appel des triggers - include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); - $interface=new Interfaces($db); - $result=$interface->run_triggers('BILL_SENTBYMAIL',$fac,$user,$langs,$conf); - if ($result < 0) { $error++; $this->errors=$interface->errors; } - // Fin appel triggers - - if ($error) - { - dolibarr_print_error($db); - } - else - { - // Redirect here - // This avoid sending mail twice if going out and then back to page - Header('Location: '.$_SERVER["PHP_SELF"].'?facid='.$fac->id.'&mesg='.urlencode($mesg)); - exit; - } - } - else - { - $langs->load("other"); - $mesg='
'; - if ($mailfile->error) - { - $mesg.=$langs->trans('ErrorFailedToSendMail',$from,$sendto); - $mesg.='
'.$mailfile->error; - } - else - { - $mesg.='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS'; - } - $mesg.='
'; - } - } - } - else - { - $langs->load("other"); - $mesg='
'.$langs->trans('ErrorMailRecipientIsEmpty').'
'; - dolibarr_syslog('Recipient email is empty'); - } - } - else - { - $langs->load("other"); - $mesg='
'.$langs->trans('ErrorCantReadFile',$file).'
'; - dolibarr_syslog('Failed to read file: '.$file); - } - } - else - { - $langs->load("other"); - $mesg='
'.$langs->trans('ErrorFailedToReadEntity',$langs->trans("Invoice")).'
'; - dolibarr_syslog('Impossible de lire les données de la facture. Le fichier facture n\'a peut-être pas été généré.'); - } -} - -/* - * Générer ou regénérer le document PDF - */ -if ($_REQUEST['action'] == 'builddoc') // En get ou en post -{ - $fac = new Facture($db, 0, $_GET['facid']); - $fac->fetch($_GET['facid']); - - if ($_REQUEST['model']) - { - $fac->setDocModel($user, $_REQUEST['model']); - } - - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - - $result=facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); - if ($result <= 0) - { - dolibarr_print_error($db,$result); - exit; - } - else - { - Header ('Location: '.$_SERVER["PHP_SELF"].'?facid='.$fac->id.'#builddoc'); - } -} - - - -/* - * View - */ - -llxHeader('',$langs->trans('Bill'),'HelpInvoice'); - -$html = new Form($db); -$formfile = new FormFile($db); - - -/********************************************************************* - * - * Mode creation - * - **********************************************************************/ -if ($_GET['action'] == 'create') -{ - $facturestatic=new Facture($db); - - print_titre($langs->trans('NewBill')); - - if ($mesg) print $mesg; - - $soc = new Societe($db); - - if ($_GET['propalid']) - { - $propal = New Propal($db); - $propal->fetch($_GET['propalid']); - $propal->fetch_client(); - - $projetid=$propal->projetidp; - $ref_client=$propal->ref_client; - - $soc=$propal->client; - $cond_reglement_id = $propal->cond_reglement_id; - $mode_reglement_id = $propal->mode_reglement_id; - $remise_percent = $propal->remise_percent; - $remise_absolue = $propal->remise_absolue; - } - elseif ($_GET['commandeid']) - { - $commande = New Commande($db); - $commande->fetch($_GET['commandeid']); - $commande->fetch_client(); - - $projetid=$commande->projet_id; - $ref_client=$commande->ref_client; - - $soc=$commande->client; - $cond_reglement_id = $commande->cond_reglement_id; - $mode_reglement_id = $commande->mode_reglement_id; - $remise_percent = $commande->remise_percent; - $remise_absolue = $commande->remise_absolue; - } - elseif ($_GET['contratid']) - { - $contrat = New Contrat($db); - $contrat->fetch($_GET['contratid']); - $contrat->fetch_client(); - - $projetid=$contrat->fk_projet; - - $soc=$contrat->client; - $cond_reglement_id = $soc->cond_reglement; - $mode_reglement_id = $soc->mod_reglement; - $remise_percent = $soc->remise_client; - $remise_absolue = 0; - $dateinvoice=empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0; - } - else - { - $soc->fetch($socid); - $cond_reglement_id = $soc->cond_reglement; - $mode_reglement_id = $soc->mode_reglement; - $remise_percent = $soc->remise_client; - $remise_absolue = 0; - $dateinvoice=empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0; - } - $absolute_discount=$soc->getAvailableDiscounts(); - - - print '
'; - print ''; - print '' ."\n"; - print ''; - - print ''; - - // Ref - print ''; - - /* - \todo - L'info "Reference commande client" est une carac de la commande et non de la facture. - Elle devrait donc etre stockée sur l'objet commande liée à la facture et non sur la facture. - Pour ceux qui veulent l'utiliser au niveau de la facture, positionner la - constante FAC_USE_CUSTOMER_ORDER_REF à 1. - */ - if ($conf->global->FAC_USE_CUSTOMER_ORDER_REF) - { - print ''; - } - - // Tiers - print ''; - print ''."\n"; - - // Type de facture - $facids=$facturestatic->list_replacable_invoices($soc->id); - if ($facids < 0) - { - dolibarr_print_error($db,$facturestatic); - exit; - } - $options=""; - foreach ($facids as $facparam) - { - $options.=''; - } - $facids=$facturestatic->list_qualified_avoir_invoices($soc->id); - $optionsav=""; - foreach ($facids as $key => $value) - { - $newinvoice=new Facture($db); - $newinvoice->fetch($key); - $optionsav.=''; - } - - print ''; - - // Discounts for third party - print ''; - - // Date invoice - print ''; - - // Conditions de règlement - print ''; - - // Mode de règlement - print ''; - - // Projet - if ($conf->projet->enabled) - { - $langs->load('projects'); - print ''; - } - - // Modele PDF - print ''; - print '"; - - // Note publique - print ''; - print ''; - print ''; - - // Note privée - if (! $user->societe_id) - { - print ''; - print ''; - print ''; - } - - if ($_GET['propalid'] > 0) - { - print ''."\n"; - print ''."\n"; - print ''."\n"; - //print ''."\n"; - //print ''."\n"; - print ''; - - print ''; - print ''; - print '"; - print '"; - } - elseif ($_GET['commandeid'] > 0) - { - print ''."\n"; - print ''."\n"; - print ''."\n"; - //print ''."\n"; - //print ''."\n"; - print ''; - - print ''; - print ''; - print '"; - print '"; - } - elseif ($_GET['contratid'] > 0) - { - // Calcul contrat->price (HT), contrat->total (TTC), contrat->tva - $contrat->remise_absolue=$remise_absolue; - $contrat->remise_percent=$remise_percent; - $contrat->update_price(); - - print ''."\n"; - print ''."\n"; - print ''."\n"; - // print ''."\n"; - // print ''."\n"; - print ''; - - print ''; - print ''; - print '"; - print '"; - } - else - { - if ($conf->global->PRODUCT_SHOW_WHEN_CREATE) - { - print ''; - } - } - - /* - * Factures récurrentes - */ - if (! $conf->global->FACTURE_DISABLE_RECUR) - { - if ($_GET['propalid'] == 0 && $_GET['commandeid'] == 0 && $_GET['contratid'] == 0) - { - $sql = 'SELECT r.rowid, r.titre, r.amount FROM '.MAIN_DB_PREFIX.'facture_rec as r'; - $sql .= ' WHERE r.fk_soc = '.$soc->id; - if ( $db->query($sql) ) - { - $num = $db->num_rows(); - $i = 0; - - if ($num > 0) - { - print ''; - } - $db->free(); - } - else - { - dolibarr_print_error($db); - } - } - } - - // Bouton "Create Draft" - print ''; - print "
'.$langs->trans('Ref').''.$langs->trans('Draft').'
'.$langs->trans('RefCustomerOrder').''; - print ''; - print '
'.$langs->trans('Company').''; - print $soc->getNomUrl(1); - print ''; - print '
'.$langs->trans('Type').''; - print ''."\n"; - - // Type - print ''."\n"; - - print ''."\n"; - - print ''."\n"; - - print '
'; - print ''; - print ''; - $desc=$html->textwithhelp($langs->trans("InvoiceStandardAsk"),$langs->transnoentities("InvoiceStandardDesc"),1); - print $desc; - print '
'; - print ''; - print ''; - $text=$langs->trans("InvoiceReplacementAsk").' '; - $text.=''; - $desc=$html->textwithhelp($text,$langs->transnoentities("InvoiceReplacementDesc"),1); - print $desc; - print '
'; - print ''; - print ''; - $text=$langs->transnoentities("InvoiceAvoirAsk").' '; - // $text.=''; - $text.=''; - $desc=$html->textwithhelp($text,$langs->transnoentities("InvoiceAvoirDesc"),1); - //.' ('.$langs->trans("FeatureNotYetAvailable").')',$langs->transnoentities("InvoiceAvoirDesc"),1); - print $desc; - print '
'; - print '
'.$langs->trans('Discounts').''; - if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client); - else print $langs->trans("CompanyHasNoRelativeDiscount"); - print '. '; - if ($absolute_discount) print $langs->trans("CompanyHasAbsoluteDiscount",price($absolute_discount),$langs->trans("Currency".$conf->monnaie)); - else print $langs->trans("CompanyHasNoAbsoluteDiscount"); - print '.'; - print '
'.$langs->trans('Date').''; - $html->select_date($dateinvoice,'','','','',"add"); - print '
'.$langs->trans('PaymentConditionsShort').''; - $html->select_conditions_paiements($cond_reglement_id,'cond_reglement_id'); - print '
'.$langs->trans('PaymentMode').''; - $html->select_types_paiements($mode_reglement_id,'mode_reglement_id'); - print '
'.$langs->trans('Project').''; - select_projects($soc->id, $projetid, 'projetid'); - print '
'.$langs->trans('Model').''; - include_once(DOL_DOCUMENT_ROOT.'/includes/modules/facture/modules_facture.php'); - $model=new ModelePDFFactures(); - $liste=$model->liste_modeles($db); - $html->select_array('model',$liste,$conf->global->FACTURE_ADDON_PDF); - print "
'.$langs->trans('NotePublic').''; - print '
'.$langs->trans('NotePrivate').''; - print '
'.$langs->trans('Proposal').''.img_object($langs->trans("ShowPropal"),'propal').' '.$propal->ref.'
'.$langs->trans('TotalHT').''.price($propal->price).'
'.$langs->trans('TotalVAT').''.price($propal->total_tva)."
'.$langs->trans('TotalTTC').''.price($propal->total)."
'.$langs->trans('Order').''.img_object($langs->trans("ShowOrder"),'order').' '.$commande->ref.'
'.$langs->trans('TotalHT').''.price($commande->total_ht).'
'.$langs->trans('TotalVAT').''.price($commande->total_tva)."
'.$langs->trans('TotalTTC').''.price($commande->total_ttc)."
'.$langs->trans('Contract').''.img_object($langs->trans("ShowContract"),'contract').' '.$contrat->ref.'
'.$langs->trans('TotalHT').''.price($contrat->total_ht).'
'.$langs->trans('TotalVAT').''.price($contrat->total_tva)."
'.$langs->trans('TotalTTC').''.price($contrat->total_ttc)."
'; - - // Zone de choix des produits prédéfinis à la création - print ''; - print ''; - print ''; - print ''; - print ''; - if ($conf->service->enabled) - { - print ''; - } - print ''; - for ($i = 1 ; $i <= $NBLINES ; $i++) - { - print ''; - print ''; - print ''; - print ''; - print ''; - // Si le module service est actif, on propose des dates de début et fin à la ligne - if ($conf->service->enabled) - { - print ''; - } - print "\n"; - } - - print '
'.$langs->trans('ProductsAndServices').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').'     '.$langs->trans('ServiceLimitedDuration').'
'; - // multiprix - if($conf->global->PRODUIT_MULTIPRICES == 1) - $html->select_produits('','idprod'.$i,'',$conf->produit->limit_size,$soc->price_level); - else - $html->select_produits('','idprod'.$i,'',$conf->produit->limit_size); - print '% '; - print ''; - print ''; - print '
'; - print $langs->trans('From').' '; - print ''; - print $html->select_date('','date_start'.$i,0,0,1,"add"); - print '
'; - print $langs->trans('to').' '; - print ''; - print $html->select_date('','date_end'.$i,0,0,1,"add"); - print '
'; - print '
'; - print '
'.$langs->trans('CreateFromRepeatableInvoice').' :
\n"; - - print "
\n"; - - // Si creation depuis un propal - if ($_GET['propalid']) - { - $title=$langs->trans('ProductsAndServices'); - - $sql = 'SELECT pt.rowid, pt.description, pt.price, pt.fk_product, pt.fk_remise_except,'; - $sql.= ' pt.qty, pt.tva_tx, pt.remise_percent, pt.subprice, pt.info_bits,'; - $sql.= ' p.label as product, p.ref, p.fk_product_type, p.rowid as prodid,'; - $sql.= ' p.description as product_desc'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt'; - $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product=p.rowid'; - $sql.= ' WHERE pt.fk_propal = '.$_GET['propalid']; - $sql.= ' ORDER BY pt.rang ASC, pt.rowid'; - } - if ($_GET['commandeid']) - { - $title=$langs->trans('Products'); - - $sql = 'SELECT pt.rowid, pt.subprice, pt.tva_tx, pt.qty, pt.fk_remise_except, pt.remise_percent, pt.description, pt.info_bits,'; - $sql.= ' p.label as product, p.ref, p.rowid as prodid'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as pt'; - $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product = p.rowid'; - $sql.= ' WHERE pt.fk_commande = '.$commande->id; - $sql.= ' ORDER BY pt.rowid ASC'; - } - if ($_GET['contratid']) - { - $title=$langs->trans('Services'); - - $sql = 'SELECT pt.rowid, pt.subprice, pt.tva_tx, pt.qty, pt.remise_percent, pt.description, pt.info_bits,'; - $sql.= ' pt.date_ouverture_prevue as date_debut_prevue, pt.date_ouverture as date_debut_reel,'; - $sql.= ' pt.date_fin_validite as date_fin_prevue, pt.date_cloture as date_fin_reel,'; - $sql.= ' p.label as product, p.ref, p.rowid as prodid'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'contratdet as pt'; - $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product = p.rowid'; - $sql.= ' WHERE pt.fk_contrat = '.$contrat->id; - $sql.= ' ORDER BY pt.rowid ASC'; - } - if ($_GET['propalid'] || $_GET['commandeid'] || $_GET['contratid']) - { - print '
'; - print_titre($title); - - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - // Lignes - $resql = $db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - $i = 0; - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($resql); - $var=!$var; - print '\n"; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - $i++; - } - } - else - { - dolibarr_print_error($db); - } - - print '
'.$langs->trans('Ref').''.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').'
'; - if (($objp->info_bits & 2) == 2) - { - print ''; - print img_object($langs->trans("ShowReduc"),'reduc').' '.$langs->trans("Discount"); - print ''; - } - else if ($objp->prodid) - { - print ''.img_object($langs->trans(''),'service').' '.$objp->ref.''; - print $objp->product?' - '.$objp->product:''; - // Plage de dates si contrat - if ($_GET['contratid']) - { - $date_start=$objp->date_debut_prevue; - if ($objp->date_debut_reel) $date_start=$objp->date_debut_reel; - $date_end=$objp->date_fin_prevue; - if ($objp->date_fin_reel) $date_end=$objp->date_fin_reel; - print_date_range($date_start,$date_end); - } - } - else - { - print ' '; - } - print "'; - if ($objp->description) - { - if ($objp->description == '(CREDIT_NOTE)') - { - $discount=new DiscountAbsolute($db); - $discount->fetch($objp->fk_remise_except); - print $langs->transnoentities("DiscountFromCreditNote",$discount->getNomUrl(0)); - } - else - { - print dolibarr_trunc($objp->description,60); - } - } - print ''.vatrate($objp->tva_tx).'%'.price($objp->subprice).''; - print (($objp->info_bits & 2) != 2) ? $objp->qty : ' '; - print ''; - print (($objp->info_bits & 2) != 2) ? $objp->remise_percent.'%' : ' '; - print '
'; - } - -} -else -{ - $id = $_GET['facid']; - if ($id > 0) - { - /* *************************************************************************** */ - /* */ - /* Fiche en mode visu / edition */ - /* */ - /* *************************************************************************** */ - if ($mesg) print $mesg.'
'; - - $facstatic = new Facture($db); - - $fac = new Facture($db); - $result=$fac->fetch($_GET['facid']); - if ($result > 0) - { - if ($user->societe_id>0 && $user->societe_id!=$fac->socid) accessforbidden('',0); - - $result=$fac->fetch_client(); - - $soc = new Societe($db, $fac->socid); - $soc->fetch($fac->socid); - - $totalpaye = $fac->getSommePaiement(); - $totalavoir = $fac->getSommeCreditNote(); - - // We cal also use bcadd to avoid pb with floating points - // For example print 239.2 - 229.3 - 9.9; does not return 0. - //$resteapayer=bcadd($fac->total_ttc,$totalpaye,$conf->global->MAIN_MAX_DECIMALS_TOT); - //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT); - $resteapayer = price2num($fac->total_ttc - $totalpaye - $totalavoir,'MT'); - - if ($fac->paye) $resteapayer=0; - $resteapayeraffiche=$resteapayer; - - $absolute_discount=$soc->getAvailableDiscounts('','fk_facture_source IS NULL'); - $absolute_creditnote=$soc->getAvailableDiscounts('','fk_facture_source IS NOT NULL'); - - $author = new User($db); - if ($fac->user_author) - { - $author->id = $fac->user_author; - $author->fetch(); - } - - $facidnext=$fac->getIdReplacingInvoice(); - - - $head = facture_prepare_head($fac); - - dolibarr_fiche_head($head, 'compta', $langs->trans('InvoiceCustomer')); - - /* - * Confirmation de la conversion de l'avoir en reduc - */ - if ($_GET['action'] == 'converttoreduc') - { - $text=$langs->trans('ConfirmConvertToReduc'); - $html->form_confirm($_SERVER['PHP_SELF'].'?facid='.$fac->id,$langs->trans('ConvertToReduc'),$text,'confirm_converttoreduc'); - print '
'; - } - - /* - * Confirmation de la suppression de la facture - */ - if ($_GET['action'] == 'delete') - { - $text=$langs->trans('ConfirmDeleteBill'); - $html->form_confirm($_SERVER['PHP_SELF'].'?facid='.$fac->id,$langs->trans('DeleteBill'),$text,'confirm_delete'); - print '
'; - } - - /* - * Confirmation de la validation - */ - if ($_GET['action'] == 'valid') - { - // on vérifie si la facture est en numérotation provisoire - $facref = substr($fac->ref, 1, 4); - if ($facref == 'PROV') - { - $numfa = $fac->getNextNumRef($soc); - } - else - { - $numfa = $fac->ref; - } - - $text=$langs->trans('ConfirmValidateBill',$numfa); - if ($conf->notification->enabled) - { - require_once(DOL_DOCUMENT_ROOT ."/notify.class.php"); - $notify=new Notify($db); - $text.='
'; - $text.=$notify->confirmMessage(2,$fac->socid); - } - - $html->form_confirm($_SERVER["PHP_SELF"].'?facid='.$fac->id,$langs->trans('ValidateBill'),$text,'confirm_valid'); - print '
'; - } - - /* - * Confirmation du classement payé - */ - if ($_GET['action'] == 'payed' && $resteapayer <= 0) - { - $html->form_confirm($_SERVER["PHP_SELF"].'?facid='.$fac->id,$langs->trans('ClassifyPayed'),$langs->trans('ConfirmClassifyPayedBill',$fac->ref),'confirm_payed'); - print '
'; - } - if ($_GET['action'] == 'payed' && $resteapayer > 0) - { - // Code - $i=0; - $close[$i]['code']='discount_vat';$i++; - $close[$i]['code']='badcustomer';$i++; - //$close[$i]['code']='product_returned';$i++; - //$close[$i]['code']='abandon';$i++; - // Help - $i=0; - $close[$i]['label']=$langs->trans("HelpEscompte").'

'.$langs->trans("ConfirmClassifyPayedPartiallyReasonDiscountVatDesc");$i++; - $close[$i]['label']=$langs->trans("ConfirmClassifyPayedPartiallyReasonBadCustomerDesc");$i++; - //$close[$i]['label']=$langs->trans("ConfirmClassifyPayedPartiallyReasonProductReturned");$i++; - //$close[$i]['label']=$langs->trans("ConfirmClassifyPayedPartiallyReasonOtherDesc");$i++; - // Texte - $i=0; - $close[$i]['reason']=$html->textwithhelp($langs->transnoentities("ConfirmClassifyPayedPartiallyReasonDiscountVat",$resteapayer,$langs->trans("Currency".$conf->monnaie)),$close[$i]['label'],1);$i++; - $close[$i]['reason']=$html->textwithhelp($langs->transnoentities("ConfirmClassifyPayedPartiallyReasonBadCustomer",$resteapayer,$langs->trans("Currency".$conf->monnaie)),$close[$i]['label'],1);$i++; - //$close[$i]['reason']=$html->textwithhelp($langs->transnoentities("ConfirmClassifyPayedPartiallyReasonProductReturned",$resteapayer,$langs->trans("Currency".$conf->monnaie)),$close[$i]['label'],1);$i++; - //$close[$i]['reason']=$html->textwithhelp($langs->transnoentities("ConfirmClassifyPayedPartiallyReasonOther",$resteapayer,$langs->trans("Currency".$conf->monnaie)),$close[$i]['label'],1);$i++; - // arrayreasons[code]=reason - foreach($close as $key => $val) - { - $arrayreasons[$close[$key]['code']]=$close[$key]['reason']; - } - - // Crée un tableau formulaire - $formquestion=array( - 'text' => $langs->trans("ConfirmClassifyPayedPartiallyQuestion"), - array('type' => 'radio', 'name' => 'close_code', 'label' => $langs->trans("Reason"), 'values' => $arrayreasons), - array('type' => 'text', 'name' => 'close_note', 'label' => $langs->trans("Comment"), 'value' => '', 'size' => '100') - ); - // Paiement incomplet. On demande si motif = escompte ou autre - $html->form_confirm($_SERVER["PHP_SELF"].'?facid='.$fac->id,$langs->trans('ClassifyPayed'),$langs->trans('ConfirmClassifyPayedPartially',$fac->ref),'confirm_payed_partially',$formquestion); - print '
'; - } - - /* - * Confirmation du classement abandonne - */ - if ($_GET['action'] == 'canceled') - { - // S'il y a une facture de remplacement pas encore validée (etat brouillon), - // on ne permet pas de classer abandonner la facture. - if ($facidnext) - { - $facturereplacement=new Facture($db); - $facturereplacement->fetch($facidnext); - $statusreplacement=$facturereplacement->statut; - } - if ($facidnext && $statusreplacement == 0) - { - print '
'.$langs->trans("ErrorCantCancelIfReplacementInvoiceNotValidated").'
'; - } - else - { - // Code - $close[1]['code']='badcustomer'; - $close[2]['code']='abandon'; - // Help - $close[1]['label']=$langs->trans("ConfirmClassifyPayedPartiallyReasonBadCustomerDesc"); - $close[2]['label']=$langs->trans("ConfirmClassifyAbandonReasonOtherDesc"); - // Texte - $close[1]['reason']=$html->textwithhelp($langs->transnoentities("ConfirmClassifyPayedPartiallyReasonBadCustomer",$fac->ref),$close[1]['label'],1); - $close[2]['reason']=$html->textwithhelp($langs->transnoentities("ConfirmClassifyAbandonReasonOther"),$close[2]['label'],1); - // arrayreasons - $arrayreasons[$close[1]['code']]=$close[1]['reason']; - $arrayreasons[$close[2]['code']]=$close[2]['reason']; - - // Crée un tableau formulaire - $formquestion=array( - 'text' => $langs->trans("ConfirmCancelBillQuestion"), - array('type' => 'radio', 'name' => 'close_code', 'label' => $langs->trans("Reason"), 'values' => $arrayreasons), - array('type' => 'text', 'name' => 'close_note', 'label' => $langs->trans("Comment"), 'value' => '', 'size' => '100') - ); - - $html->form_confirm($_SERVER['PHP_SELF'].'?facid='.$fac->id,$langs->trans('CancelBill'),$langs->trans('ConfirmCancelBill',$fac->ref),'confirm_canceled',$formquestion); - print '
'; - } - } - - /* - * Confirmation de la suppression d'une ligne produit - */ - if ($_GET['action'] == 'delete_product_line' && $conf->global->PRODUIT_CONFIRM_DELETE_LINE) - { - $html->form_confirm($_SERVER["PHP_SELF"].'?facid='.$fac->id.'&rowid='.$_GET["rowid"], $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteproductline'); - print '
'; - } - - /* - * Facture - */ - - print ''; - - // Reference - print ''; - print ''; - - // Ref client - /* - \todo - L'info "Reference commande client" est une carac de la commande et non de la facture. - Elle devrait donc etre stockée sur l'objet commande lié à la facture et non sur la facture. - Pour ceux qui utilisent ainsi, positionner la constante FAC_USE_CUSTOMER_ORDER_REF à 1. - */ - if ($conf->global->FAC_USE_CUSTOMER_ORDER_REF) - { - print ''; - print ''; - } - - // Tiers Société - print ''; - print ''; - print ''; - - // Type - print ''; - - // Ligne info remises tiers - print ''; - - // Dates - print ''; - print ''; - - $nbrows=8; - if ($conf->global->FAC_USE_CUSTOMER_ORDER_REF) $nbrows++; - if ($conf->projet->enabled) $nbrows++; - - print ''; - - // Date limite reglement - print ''; - print ''; - print ''; - - // Conditions de règlement - print ''; - - // Mode de reglement - print ''; - - // Lit lignes de facture pour déterminer montant - // On s'en sert pas mais ca sert pour debuggage - /* - $sql = 'SELECT l.price as price, l.qty, l.rowid, l.tva_taux,'; - $sql .= ' l.remise_percent, l.subprice'; - $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet as l '; - $sql .= ' WHERE l.fk_facture = '.$fac->id; - $resql = $db->query($sql); - if ($resql) - { - $num_lignes = $db->num_rows($resql); - $i=0; - $total_lignes_ht=0; - $total_lignes_vat=0; - $total_lignes_ttc=0; - while ($i < $num_lignes) - { - $obj=$db->fetch_object($resql); - $ligne_ht=($obj->price*$obj->qty); - $ligne_vat=($ligne_ht*$obj->tva_taux/100); - $ligne_ttc=($ligne_ht+$ligne_vat); - $total_lignes_ht+=$ligne_ht; - $total_lignes_vat+=$ligne_vat; - $total_lignes_ttc+=$ligne_ttc; - $i++; - } - } - */ - - // Montants - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - // Statut - print ''; - print ''; - - // Projet - if ($conf->projet->enabled) - { - $langs->load('projects'); - print ''; - print ''; - print ''; - } - - print '
'.$langs->trans('Ref').''.$fac->ref; - $discount=new DiscountAbsolute($db); - $result=$discount->fetch(0,$fac->id); - if ($result > 0) - { - print ' ('.$langs->trans("CreditNoteConvertedIntoDiscount",$discount->getNomUrl(1,'discount')).')'; - } - if ($result < 0) - { - dolibarr_print_error('',$discount->error); - } - print '
'; - print ''; - if ($_GET['action'] != 'RefCustomerOrder' && $fac->brouillon) print ''; - print '
'; - print $langs->trans('RefCustomerOrder').''; - print ''.img_edit($langs->trans('Modify')).'
'; - print '
'; - if ($user->rights->facture->creer && $_GET['action'] == 'RefCustomerOrder') - { - print '
'; - print ''; - print ''; - print ' '; - print '
'; - } - else - { - print $fac->ref_client; - } - print '
'.$langs->trans('Company').''.$soc->getNomUrl(1,'compta').'
'.$langs->trans('Type').''; - print $fac->getLibType(); - if ($fac->type == 1) - { - $facreplaced=new Facture($db); - $facreplaced->fetch($fac->fk_facture_source); - print ' ('.$langs->transnoentities("ReplaceInvoice",$facreplaced->getNomUrl(1)).')'; - } - if ($fac->type == 2) - { - $facreplaced=new Facture($db); - $facreplaced->fetch($fac->fk_facture_source); - print ' ('.$langs->transnoentities("CorrectInvoice",$facreplaced->getNomUrl(1)).')'; - } - $facidavoir=$fac->getListIdAvoirFromInvoice(); - if (sizeof($facidavoir) > 0) - { - print ' ('.$langs->transnoentities("InvoiceHasAvoir"); - $i=0; - foreach($facidavoir as $id) - { - if ($i==0) print ' '; - else print ','; - $facavoir=new Facture($db); - $facavoir->fetch($id); - print $facavoir->getNomUrl(1); - } - print ')'; - } - if ($facidnext > 0) - { - $facthatreplace=new Facture($db); - $facthatreplace->fetch($facidnext); - print ' ('.$langs->transnoentities("ReplacedByInvoice",$facthatreplace->getNomUrl(1)).')'; - } - print '
'.$langs->trans('Discounts').''; - if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client); - else print $langs->trans("CompanyHasNoRelativeDiscount"); - print '. '; - if ($absolute_discount > 0) - { - if ($fac->statut > 0 || $fac->type == 2) - { - print $langs->trans("CompanyHasAbsoluteDiscount",price($absolute_discount),$langs->transnoentities("Currency".$conf->monnaie)).'. '; - } - else - { - // Remise dispo de type non avoir - $filter='fk_facture_source IS NULL'; - print '
'; - $html->form_remise_dispo($_SERVER["PHP_SELF"].'?facid='.$fac->id,0,'remise_id',$soc->id,$absolute_discount,$filter,$resteapayer); - } - } - if ($absolute_creditnote > 0) - { - // If validated, we show link "add credit note to payment" - if ($fac->statut != 1 || $fac->type == 2) - { - print $langs->trans("CompanyHasCreditNote",price($absolute_creditnote),$langs->transnoentities("Currency".$conf->monnaie)).'. '; - } - else - { - // Remise dispo de type avoir - $filter='fk_facture_source IS NOT NULL'; - if (! $absolute_discount) print '
'; - $html->form_remise_dispo($_SERVER["PHP_SELF"].'?facid='.$fac->id,0,'remise_id_for_payment',$soc->id,$absolute_creditnote,$filter,$resteapayer); - } - } - if (! $absolute_discount && ! $absolute_creditnote) print $langs->trans("CompanyHasNoAbsoluteDiscount").'.'; - print '
'.$langs->trans('Date').''.dolibarr_print_date($fac->date,'daytext').''; - - /* - * Liste des paiements - */ - $sql = 'SELECT '.$db->pdate('datep').' as dp, pf.amount,'; - $sql.= ' c.libelle as paiement_type, p.num_paiement, p.rowid'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement as p, '.MAIN_DB_PREFIX.'c_paiement as c, '.MAIN_DB_PREFIX.'paiement_facture as pf'; - $sql.= ' WHERE pf.fk_facture = '.$fac->id.' AND p.fk_paiement = c.id AND pf.fk_paiement = p.rowid'; - $sql.= ' ORDER BY dp, tms'; - - $result = $db->query($sql); - if ($result) - { - $num = $db->num_rows($result); - $i = 0; - print ''; - - // Liste des paiements ou remboursements - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - if ($fac->type != 2) - { - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($result); - $var=!$var; - print ''; - print ''; - print ''; - print ''; - print ''; - $i++; - } - - // Already payed - print ''; - - // Billed - print ''; - $resteapayeraffiche=$resteapayer; - - // Loop on each credit note applied - $sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,"; - $sql.= " re.description, re.fk_facture_source, re.fk_facture_source"; - $sql.= " FROM ".MAIN_DB_PREFIX ."societe_remise_except as re"; - $sql.= " WHERE fk_facture = ".$fac->id; - $resql=$db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - $i = 0; - $invoice=new Facture($db); - while ($i < $num) - { - $obj = $db->fetch_object($resql); - $invoice->fetch($obj->fk_facture_source); - print ''; - print ''; - print ''; - $i++; - } - } - else - { - dolibarr_print_error($db); - } - - // Payé partiellement 'escompte' - if (($fac->statut == 2 || $fac->statut == 3) && $fac->close_code == 'discount_vat') - { - print ''; - $resteapayeraffiche=0; - } - // Payé partiellement ou Abandon 'badcustomer' - if (($fac->statut == 2 || $fac->statut == 3) && $fac->close_code == 'badcustomer') - { - print ''; - //$resteapayeraffiche=0; - } - // Payé partiellement ou Abandon 'product_returned' - if (($fac->statut == 2 || $fac->statut == 3) && $fac->close_code == 'product_returned') - { - print ''; - $resteapayeraffiche=0; - } - // Payé partiellement ou Abandon 'abandon' - if (($fac->statut == 2 || $fac->statut == 3) && $fac->close_code == 'abandon') - { - print ''; - $resteapayeraffiche=0; - } - print ''; - print ''; - print ''; - } - else - { - // Solde avoir - print ''; - print ''; - } - print '
'.($fac->type == 2 ? $langs->trans("PaymentsBack") : $langs->trans('Payments')).''.$langs->trans('Type').''.$langs->trans('Amount').' 
'; - print ''.img_object($langs->trans('ShowPayment'),'payment').' '; - print dolibarr_print_date($objp->dp,'day').''.$objp->paiement_type.' '.$objp->num_paiement.''.price($objp->amount).' 
'.$langs->trans('AlreadyPayed').' :'.price($totalpaye).' 
'.$langs->trans("Billed").' :'.price($fac->total_ttc).' 
'.$langs->trans("CreditNote").' '; - print $invoice->getNomUrl(0); - print ' :'.price($obj->amount_ttc).''; - print 'rowid.'">'.img_delete().''; - print '
'; - print $html->textwithhelp($langs->trans("Escompte").':',$langs->trans("HelpEscompte"),-1); - print ''.price($fac->total_ttc - $totalpaye).' 
'; - print $html->textwithhelp($langs->trans("Abandoned").':',$langs->trans("HelpAbandonBadCustomer"),-1); - print ''.price($fac->total_ttc - $totalpaye).' 
'; - print $html->textwithhelp($langs->trans("ProductReturned").':',$langs->trans("HelpAbandonProductReturned"),-1); - print ''.price($fac->total_ttc - $totalpaye).' 
'; - $text=$langs->trans("HelpAbandonOther"); - if ($fac->close_note) $text.='

'.$langs->trans("Reason").':'.$fac->close_note; - print $html->textwithhelp($langs->trans("Abandoned").':',$text,-1); - print '
'.price($fac->total_ttc - $totalpaye).' 
'; - if ($resteapayeraffiche >= 0) print $langs->trans('RemainderToPay'); - else print $langs->trans('ExcessReceived'); - print ' :'.price($resteapayeraffiche).' 
'.$langs->trans('TotalTTCToYourCredit').' :'.price(abs($fac->total_ttc)).' 
'; - $db->free($result); - } - else - { - dolibarr_print_error($db); - } - - print '
'.$langs->trans('DateMaxPayment').''; - if ($fac->type != 2) - { - print dolibarr_print_date($fac->date_lim_reglement,'daytext'); - if ($fac->date_lim_reglement < (time() - $conf->facture->client->warning_delay) && ! $fac->paye && $fac->statut == 1 && ! $fac->am) print img_warning($langs->trans('Late')); - } - else - { - print ' '; - } - print '
'; - print ''; - if ($fac->type != 2 && $_GET['action'] != 'editconditions' && $fac->brouillon && $user->rights->facture->creer) print ''; - print '
'; - print $langs->trans('PaymentConditionsShort'); - print 'id.'">'.img_edit($langs->trans('SetConditions'),1).'
'; - print '
'; - if ($fac->type != 2) - { - if ($_GET['action'] == 'editconditions') - { - $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->cond_reglement_id,'cond_reglement_id'); - } - else - { - $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->cond_reglement_id,'none'); - } - } - else - { - print ' '; - } - print '
'; - print ''; - if ($_GET['action'] != 'editmode' && $fac->brouillon && $user->rights->facture->creer) print ''; - print '
'; - print $langs->trans('PaymentMode'); - print 'id.'">'.img_edit($langs->trans('SetMode'),1).'
'; - print '
'; - if ($_GET['action'] == 'editmode') - { - $html->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->mode_reglement_id,'mode_reglement_id'); - } - else - { - $html->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->mode_reglement_id,'none'); - } - print '
'.$langs->trans('AmountHT').''.price($fac->total_ht).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('AmountVAT').''.price($fac->total_tva).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('AmountTTC').''.price($fac->total_ttc).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('Status').''.($fac->getLibStatut(4,$totalpaye)).'
'; - - print ''; - if ($_GET['action'] != 'classer') - { - print ''; - } - print '
'; - print $langs->trans('Project'); - print 'id.'">'; - print img_edit($langs->trans('SetProject'),1); - print '
'; - - print '
'; - if ($_GET['action'] == 'classer') - { - $html->form_project($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->socid,$fac->projetid,'projetid'); - } - else - { - $html->form_project($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->socid,$fac->projetid,'none'); - } - print '

'; - - - /* - * Lignes de factures - */ - $sql = 'SELECT l.fk_product, l.product_type, l.description, l.qty, l.rowid, l.tva_taux,'; - $sql.= ' l.fk_remise_except,'; - $sql.= ' l.remise_percent, l.subprice, l.info_bits,'; - $sql.= ' l.total_ht, l.total_tva, l.total_ttc,'; - $sql.= ' '.$db->pdate('l.date_start').' as date_start,'; - $sql.= ' '.$db->pdate('l.date_end').' as date_end,'; - $sql.= ' p.ref, p.fk_product_type, p.label as product,'; - $sql.= ' p.description as product_desc'; - $sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet as l'; - $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON l.fk_product=p.rowid'; - $sql.= ' WHERE l.fk_facture = '.$fac->id; - $sql.= ' ORDER BY l.rang ASC, l.rowid'; - - $resql = $db->query($sql); - if ($resql) - { - $num_lignes = $db->num_rows($resql); - $i = 0; $total = 0; - - print ''; - - if ($num_lignes) - { - - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print "\n"; - - } - $var=true; - while ($i < $num_lignes) - { - $objp = $db->fetch_object($resql); - $var=!$var; - - // Ligne en mode visu - if ($_GET['action'] != 'editline' || $_GET['rowid'] != $objp->rowid) - { - print ''; - if ($objp->fk_product > 0) - { - print ''; - } - else - { - print '\n"; - } - print ''; - print '\n"; - print ''; - if ($objp->remise_percent > 0) - { - print '\n"; - } - else - { - print ''; - } - print '\n"; - - // Icone d'edition et suppression - if ($fac->statut == 0 && $user->rights->facture->creer) - { - print ''; - print ''; - if ($num_lignes > 1) - { - print ''; - } - } - else - { - print ''; - } - print ''; - } - - // Ligne en mode update - if ($_GET['action'] == 'editline' && $user->rights->facture->creer && $_GET['rowid'] == $objp->rowid) - { - print 'rowid.'" method="post">'; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print '' . "\n"; - if ($conf->service->enabled) - { - print ''; - print ''; - print ''; - } - print "\n"; - } - - $total = $total + ($objp->qty * $objp->price); - $i++; - } - - $db->free($resql); - } - else - { - dolibarr_print_error($db); - } - - /* - * Ajouter une ligne - */ - if ($fac->statut == 0 && $user->rights->facture->creer && $_GET['action'] <> 'valid' && $_GET['action'] <> 'editline') - { - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print "\n"; - - // Ajout produit produits/services personalisés - print ''; - print ''; - print ''; - - $var=true; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - if ($conf->service->enabled) - { - print ''; - print ''; - print ''; - } - print ''; - - // Ajout de produits/services prédéfinis - if ($conf->produit->enabled) - { - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - print ''; - print ''; - print ''; - - $var=! $var; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - if ($conf->service->enabled) - { - print ''; - print ''; - print ''; - } - print ''; - } - - } - print "
'.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').''.$langs->trans('TotalHT').' 
'; - print ''; // ancre pour retourner sur la ligne - - // Affiche ligne produit - $text = ''; - if ($objp->fk_product_type==1) $text.= img_object($langs->trans('ShowService'),'service'); - else $text.= img_object($langs->trans('ShowProduct'),'product'); - $text.= ' '.$objp->ref.''; - $text.= ' - '.$objp->product; - $description=($conf->global->PRODUIT_DESC_IN_FORM?'':dol_htmlentitiesbr($objp->description)); - //print $description; - print $html->textwithtooltip($text,$description,3,'','',$i); - print_date_range($objp->date_start,$objp->date_end); - if ($conf->global->PRODUIT_DESC_IN_FORM) - { - print ($objp->description && $objp->description!=$objp->product)?'
'.dol_htmlentitiesbr($objp->description):''; - } - - print '
'; - print ''; // ancre pour retourner sur la ligne - - if (($objp->info_bits & 2) == 2) - { - print ''; - print img_object($langs->trans("ShowReduc"),'reduc').' '.$langs->trans("Discount"); - print ''; - if ($objp->description) - { - if ($objp->description == '(CREDIT_NOTE)') - { - $discount=new DiscountAbsolute($db); - $discount->fetch($objp->fk_remise_except); - print ' - '.$langs->transnoentities("DiscountFromCreditNote",$discount->getNomUrl(0)); - } - else - { - print ' - '.nl2br($objp->description); - } - } - } - else - { - if ($objp->product_type==1) $text = img_object($langs->trans('Service'),'service'); - else $text = img_object($langs->trans('Product'),'product'); - print $text.' '.nl2br($objp->description); - print_date_range($objp->date_start,$objp->date_end); - } - print "'.vatrate($objp->tva_taux,'%',$objp->info_bits).''.price($objp->subprice)."'; - if (($objp->info_bits & 2) != 2) - { - print $objp->qty; - } - else print ' '; - print ''.dolibarr_print_reduction($objp->remise_percent)." '.price($objp->total_ht)."'; - if (($objp->info_bits & 2) == 2) - { - // Ligne remise prédéfinie, on permet pas modif - } - else - { - print 'rowid.'#'.$objp->rowid.'">'; - print img_edit(); - print ''; - } - print ''; - if ($conf->global->PRODUIT_CONFIRM_DELETE_LINE) - { - print 'rowid.'">'; - } - else - { - print 'rowid.'">'; - } - print img_delete(); - print ''; - if ($i > 0) - { - print 'rowid.'">'; - print img_up(); - print ''; - } - if ($i < $num_lignes-1) - { - print 'rowid.'">'; - print img_down(); - print ''; - } - print ' 
'; - print ''; // ancre pour retourner sur la ligne - if ($objp->fk_product > 0) - { - print ''; - if ($objp->fk_product_type==1) print img_object($langs->trans('ShowService'),'service'); - else print img_object($langs->trans('ShowProduct'),'product'); - print ' '.$objp->ref.''; - print ' - '.nl2br($objp->product); - print '
'; - } - // éditeur wysiwyg - if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) - { - require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); - $doleditor=new DolEditor('desc',$objp->description,164,'dolibarr_details'); - $doleditor->Create(); - } - else - { - print ''; - } - print '
'; - print $html->select_tva('tva_tx',$objp->tva_taux,$mysoc,$soc,'',$objp->info_bits); - print ''; - if (($objp->info_bits & 2) != 2) - { - print ''; - } - else print ' '; - print ''; - if (($objp->info_bits & 2) != 2) - { - print '%'; - } - else print ' '; - print ''; - print '
'.$langs->trans('ServiceLimitedDuration').' '.$langs->trans('From').' '; - print $html->select_date($objp->date_start,'date_start',0,0,$objp->date_start?0:1,"updateligne"); - print ' '.$langs->trans('to').' '; - print $html->select_date($objp->date_end,'date_end',0,0,$objp->date_end?0:1,"updateligne"); - print '
'; - print ''; // ancre - print $langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').' 
'; - // éditeur wysiwyg - if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) - { - require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); - $doleditor=new DolEditor('dp_desc','',100,'dolibarr_details'); - $doleditor->Create(); - } - else - { - print ''; - } - print ''; - - $html->select_tva('tva_tx',$conf->defaulttx,$mysoc,$soc); - print '%
'.$langs->trans('ServiceLimitedDuration').' '.$langs->trans('From').' '; - print $html->select_date('','date_start',0,0,1,"addligne"); - print ' '.$langs->trans('to').' '; - print $html->select_date('','date_end',0,0,1,"addligne"); - print '
'; - if ($conf->service->enabled) - { - print $langs->trans('RecordedProductsAndServices'); - } - else - { - print $langs->trans('RecordedProducts'); - } - print ''.$langs->trans('Qty').''.$langs->trans('ReductionShort').' 
'; - // multiprix - if($conf->global->PRODUIT_MULTIPRICES == 1) - { - $html->select_produits('','idprod','',$conf->produit->limit_size,$soc->price_level); - } - else - { - $html->select_produits('','idprod','',$conf->produit->limit_size); - } - - if (! $conf->global->PRODUIT_USE_SEARCH_TO_SELECT) print '
'; - - // éditeur wysiwyg - if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) - { - require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); - $doleditor=new DolEditor('np_desc','',100,'dolibarr_details'); - $doleditor->Create(); - } - else - { - print ''; - } - - print '
%
'.$langs->trans('ServiceLimitedDuration').' '.$langs->trans('From').' '; - print $html->select_date('','date_start_predef',0,0,1,"addligne_predef"); - print ' '.$langs->trans('to').' '; - print $html->select_date('','date_end_predef',0,0,1,"addligne_predef"); - print '
\n"; - - print "\n"; - - - /* - * Boutons actions - */ - if ($_GET['action'] != 'prerelance' && $_GET['action'] != 'presend') - { - if ($user->societe_id == 0 && $_GET['action'] <> 'valid' && $_GET['action'] <> 'editline') - { - print '
'; - - // Editer une facture déjà validée, sans paiement effectué et pas exporté en compta - if ($fac->statut == 1) - { - // On vérifie si les lignes de factures ont été exportées en compta et/ou ventilées - $ventilExportCompta = $fac->getVentilExportCompta(); - - if ($conf->global->FACTURE_ENABLE_EDITDELETE && $user->rights->facture->modifier - && ($resteapayer == $fac->total_ttc && $fac->paye == 0 && $ventilExportCompta == 0)) - { - if (! $facidnext) - { - print ''.$langs->trans('Modify').''; - } - else - { - print ''.$langs->trans('Modify').''; - } - } - } - - // Reopen a classified invoice - if ($fac->statut == 3 && // A abandonned invoice - $fac->getIdReplacingInvoice() == 0 && // Not replaced by another invoice - ($fac->close_code == 'badcustomer' || $fac->close_code == 'abandon')) - { - if (! $facidnext) - { - print ''.$langs->trans('ReOpen').''; - } - else - { - print ''.$langs->trans('ReOpen').''; - } - } - - // Récurrente - if (! $conf->global->FACTURE_DISABLE_RECUR && $fac->type == 0) - { - if (! $facidnext) - { - print ''.$langs->trans("ChangeIntoRepeatableInvoice").''; - } - } - - // Valider - if ($fac->statut == 0 && $num_lignes > 0 && (($fac->type < 2 && $fac->total_ttc >= 0) || ($fac->type == 2 && $fac->total_ttc <= 0))) - { - if ($user->rights->facture->valider) - { - print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) - { - // on verifie si la commande est en numerotation provisoire - $ref = substr($fac->ref, 1, 4); - if ($ref == 'PROV') - { - $num = $fac->getNextNumRef($soc); - } - else - { - $num = $fac->ref; - } - $url = $_SERVER["PHP_SELF"].'?facid='.$fac->id.'&action=confirm_valid&confirm=yes'; - print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.dol_escape_js($langs->trans('ConfirmValidateBill',$num)).'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'validate\')"'; - } - else - { - print 'href="'.$_SERVER["PHP_SELF"].'?facid='.$fac->id.'&action=valid"'; - } - print '>'.$langs->trans('Validate').''; - } - } - - // Envoyer - if (($fac->statut == 1 || $fac->statut == 2) && $user->rights->facture->envoyer) - { - if ($facidnext) - { - print ''.$langs->trans('SendByMail').''; - } - else - { - print ''.$langs->trans('SendByMail').''; - } - } - - // Envoyer une relance - if (($fac->statut == 1 || $fac->statut == 2) && $resteapayer > 0 && $user->rights->facture->envoyer) - { - if ($facidnext) - { - print ''.$langs->trans('SendRemindByMail').''; - } - else - { - print ''.$langs->trans('SendRemindByMail').''; - } - } - - // Emettre paiement - if ($fac->type != 2 && $fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->paiement) - { - if ($facidnext) - { - print ''.$langs->trans('DoPayment').''; - } - else - { - if ($resteapayer == 0) - { - print ''.$langs->trans('DoPayment').''; - } - else - { - print ''.$langs->trans('DoPayment').''; - } - } - } - - // Emettre remboursement ou Convertir en reduc - if ($fac->type == 2) - { - if ($fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->paiement) - { - print ''.$langs->trans('DoPaymentBack').''; - } - - if ($fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->creer && $fac->getSommePaiement() == 0) - { - print ''.$langs->trans('ConvertToReduc').''; - } - } - - // Classer 'payé' - if ($fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->paiement && - (($fac->type != 2 && $resteapayer <= 0) || ($fac->type == 2 && $resteapayer >= 0)) ) - { - print ''.$langs->trans('ClassifyPayed').''; - } - - // Classer 'fermée' (possible si validée et pas encore classée payée) - if ($fac->statut == 1 && $fac->paye == 0 && $resteapayer > 0 - && $user->rights->facture->paiement) - { - if ($totalpaye > 0 || $totalavoir > 0) - { - // If one payment or one credit note was linked to this invoice - print ''.$langs->trans('ClassifyPayedPartially').''; - } - else - { - if ($facidnext) - { - print ''.$langs->trans('ClassifyCanceled').''; - } - else - { - print ''.$langs->trans('ClassifyCanceled').''; - } - } - } - - // Supprimer - if ($fac->is_erasable() && $user->rights->facture->supprimer && $_GET['action'] != 'delete') - { - if ($facidnext) - { - print ''.$langs->trans('Delete').''; - } - else - { - print ''.$langs->trans('Delete').''; - } - } - - print '
'; - } - } - - - if ($_GET['action'] != 'prerelance' && $_GET['action'] != 'presend') - { - print '
'; - print ''; // ancre - - /* - * Documents générés - */ - $filename=sanitize_string($fac->ref); - $filedir=$conf->facture->dir_output . '/' . sanitize_string($fac->ref); - $urlsource=$_SERVER['PHP_SELF'].'?facid='.$fac->id; - $genallowed=$user->rights->facture->creer; - $delallowed=$user->rights->facture->supprimer; - - $var=true; - - print '
'; - $somethingshown=$formfile->show_documents('facture',$filename,$filedir,$urlsource,$genallowed,$delallowed,$fac->modelpdf); - - /* - * Propales rattachées - */ - $sql = 'SELECT '.$db->pdate('p.datep').' as dp, p.total_ht, p.ref, p.ref_client, p.rowid as propalid'; - $sql .= ' FROM '.MAIN_DB_PREFIX.'propal as p'; - $sql .= ", ".MAIN_DB_PREFIX."fa_pr as fp"; - $sql .= " WHERE fp.fk_propal = p.rowid AND fp.fk_facture = ".$fac->id; - - dolibarr_syslog("facture.php: sql=".$sql); - $resql = $db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - if ($num) - { - $i = 0; $total = 0; - if ($somethingshown) print '
'; - $somethingshown=1; - print_titre($langs->trans('RelatedCommercialProposals')); - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - - $var=True; - while ($i < $num) - { - $objp = $db->fetch_object($resql); - $var=!$var; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - $total = $total + $objp->total_ht; - $i++; - } - print ''; - print ''; - print ''; - print ''; - print ''; - print '
'.$langs->trans('Ref').''.$langs->trans('RefCustomer').''.$langs->trans('Date').''.$langs->trans('AmountHT').'
'.img_object($langs->trans('ShowPropal'),'propal').' '.$objp->ref.''.$objp->ref_client.''.dolibarr_print_date($objp->dp,'day').''.price($objp->total_ht).'
'.$langs->trans('TotalHT').'  '.price($total).'
'; - } - } - else - { - dolibarr_print_error($db); - } - - /* - * Commandes rattachées - */ - if($conf->commande->enabled) - { - $sql = 'SELECT '.$db->pdate('c.date_commande').' as date_commande, c.total_ht, c.ref, c.ref_client, c.rowid as id'; - $sql .= ' FROM '.MAIN_DB_PREFIX.'commande as c, '.MAIN_DB_PREFIX.'co_fa as co_fa WHERE co_fa.fk_commande = c.rowid AND co_fa.fk_facture = '.$fac->id; - $resql = $db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - if ($num) - { - $langs->load("orders"); - - $i = 0; $total = 0; - if ($somethingshown) print '
'; - $somethingshown=1; - print_titre($langs->trans('RelatedOrders')); - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - $var=true; - while ($i < $num) - { - $objp = $db->fetch_object($resql); - $var=!$var; - print '\n"; - print ''; - print ''; - print ''; - print "\n"; - $total = $total + $objp->total_ht; - $i++; - } - print ''; - print ''; - print ''; - print ''; - print ''; - print '
'.$langs->trans('Ref').''.$langs->trans('RefCustomerOrderShort').''.$langs->trans('Date').''.$langs->trans('AmountHT').'
'; - print ''.img_object($langs->trans('ShowOrder'), 'order').' '.$objp->ref."'.$objp->ref_client.''.dolibarr_print_date($objp->date_commande,'day').''.price($objp->total_ht).'
'.$langs->trans('TotalHT').'  '.price($total).'
'; - } - } - else - { - dolibarr_print_error($db); - } - } - - print '
'; - - print '
'; - - // List of actions on element - include_once(DOL_DOCUMENT_ROOT.'/html.formactions.class.php'); - $formactions=new FormActions($db); - $somethingshown=$formactions->showactions($fac,'invoice',$socid); - - print '
'; - } - - /* - * Affiche formulaire mail - */ - if ($_GET['action'] == 'presend') - { - $ref = sanitize_string($fac->ref); - $file = $conf->facture->dir_output . '/' . $ref . '/' . $ref . '.pdf'; - - // Construit PDF si non existant - if (! is_readable($file)) - { - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - $result=facture_pdf_create($db, $fac->id, '', $_REQUEST['model'], $outputlangs); - if ($result <= 0) - { - dolibarr_print_error($db,$result); - exit; - } - } - - print '
'; - print_titre($langs->trans('SendBillByMail')); - - $liste[0]=' '; - foreach ($soc->thirdparty_and_contact_email_array() as $key=>$value) - { - $liste[$key]=$value; - } - - // Créé l'objet formulaire mail - include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); - $formmail = new FormMail($db); - $formmail->fromtype = 'user'; - $formmail->fromid = $user->id; - $formmail->fromname = $user->fullname; - $formmail->frommail = $user->email; - $formmail->withfrom=1; - $formmail->withto=$liste; - $formmail->withtocc=1; - $formmail->withtoccc=$conf->global->FACTURE_EMAIL_USECCC; - $formmail->withtopic=$langs->trans('SendBillRef','__FACREF__'); - $formmail->withfile=2; - $formmail->withbody=1; - $formmail->withdeliveryreceipt=1; - $formmail->withcancel=1; - // Tableau des substitutions - $formmail->substit['__FACREF__']=$fac->ref; - // Tableau des paramètres complémentaires du post - $formmail->param['action']='send'; - $formmail->param['models']='facture_send'; - $formmail->param['facid']=$fac->id; - $formmail->param['returnurl']=DOL_URL_ROOT.'/compta/facture.php?facid='.$fac->id; - - // Init list of files - if (! empty($_REQUEST["mode"]) && $_REQUEST["mode"]=='init') - { - $formmail->clear_attached_files(); - $formmail->add_attached_files($file,$ref.'.pdf','application/pdf'); - } - - $formmail->show_form(); - - print '
'; - } - - if ($_GET['action'] == 'prerelance') - { - $ref = sanitize_string($fac->ref); - $file = $conf->facture->dir_output . '/' . $ref . '/' . $ref . '.pdf'; - - // Construit PDF si non existant - if (! is_readable($file)) - { - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate("",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - $result=facture_pdf_create($db, $fac->id, '', $_REQUEST['model'], $outputlangs); - if ($result <= 0) - { - dolibarr_print_error($db,$result); - exit; - } - } - - print '
'; - print_titre($langs->trans('SendReminderBillByMail')); - - $liste[0]=' '; - foreach ($soc->thirdparty_and_contact_email_array() as $key=>$value) - { - $liste[$key]=$value; - } - - // Créé l'objet formulaire mail - include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); - $formmail = new FormMail($db); - $formmail->fromname = $user->fullname; - $formmail->frommail = $user->email; - $formmail->withfrom=1; - $formmail->withto=$liste; - $formmail->withtocc=1; - $formmail->withtopic=$langs->trans('SendReminderBillRef','__FACREF__'); - $formmail->withfile=2; - $formmail->withbody=1; - $formmail->withdeliveryreceipt=1; - $formmail->withcancel=1; - // Tableau des substitutions - $formmail->substit['__FACREF__']=$fac->ref; - // Tableau des paramètres complémentaires - $formmail->param['action']='relance'; - $formmail->param['models']='facture_relance'; - $formmail->param['facid']=$fac->id; - $formmail->param['returnurl']=DOL_URL_ROOT.'/compta/facture.php?facid='.$fac->id; - - // Init list of files - if (! empty($_REQUEST["mode"]) && $_REQUEST["mode"]=='init') - { - $formmail->clear_attached_files(); - $formmail->add_attached_files($file,$ref.'.pdf','application/pdf'); - } - - $formmail->show_form(); - - print '
'; - } - } - else - { - dolibarr_print_error($db,$fac->error); - } - } - else - { - /*************************************************************************** - * * - * Mode Liste * - * * - ***************************************************************************/ - $page =$_GET['page']; - $sortorder=$_GET['sortorder']; - $sortfield=$_GET['sortfield']; - $month =$_GET['month']; - $year =$_GET['year']; - $limit = $conf->liste_limit; - $offset = $limit * $page ; - - if (! $sortorder) $sortorder='DESC'; - if (! $sortfield) $sortfield='f.datef'; - - $facturestatic=new Facture($db); - - if ($page == -1) $page = 0 ; - - $sql = 'SELECT '; - $sql.= ' f.rowid as facid, f.facnumber, f.type, f.increment, f.total, f.total_ttc,'; - $sql.= ' '.$db->pdate('f.datef').' as df, '.$db->pdate('f.date_lim_reglement').' as datelimite,'; - $sql.= ' f.paye as paye, f.fk_statut,'; - $sql.= ' s.nom, s.rowid as socid'; - if (! $sall) $sql.= ' ,sum(pf.amount) as am'; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user"; - $sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s'; - if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql.= ', '.MAIN_DB_PREFIX.'facture as f'; - if (! $sall) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiement_facture as pf ON pf.fk_facture = f.rowid'; - if ($sall) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'facturedet as fd ON fd.fk_facture = f.rowid'; - $sql.= ' WHERE f.fk_soc = s.rowid'; - if (!$user->rights->societe->client->voir && !$socid) //restriction - { - $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; - } - if ($socid) $sql .= ' AND s.rowid = '.$socid; - if ($_GET['filtre']) - { - $filtrearr = split(',', $_GET['filtre']); - foreach ($filtrearr as $fil) - { - $filt = split(':', $fil); - $sql .= ' AND ' . trim($filt[0]) . ' = ' . trim($filt[1]); - } - } - if ($_GET['search_ref']) - { - $sql .= ' AND f.facnumber like \'%'.addslashes(trim($_GET['search_ref'])).'%\''; - } - if ($_GET['search_societe']) - { - $sql .= ' AND s.nom like \'%'.addslashes(trim($_GET['search_societe'])).'%\''; - } - if ($_GET['search_montant_ht']) - { - $sql .= ' AND f.total = \''.addslashes(trim($_GET['search_montant_ht'])).'\''; - } - if ($_GET['search_montant_ttc']) - { - $sql .= ' AND f.total_ttc = \''.addslashes(trim($_GET['search_montant_ttc'])).'\''; - } - if ($month > 0) - { - if ($year > 0) - $sql .= " AND date_format(f.datef, '%Y-%m') = '$year-$month'"; - else - $sql .= " AND date_format(f.datef, '%m') = '$month'"; - } - if ($year > 0) - { - $sql .= ' AND date_format(f.datef, \'%Y\') = '.$year; - } - if ($_POST['sf_ref']) - { - $sql .= ' AND f.facnumber like \'%'.addslashes(trim($_POST['sf_ref'])) . '%\''; - } - if ($sall) - { - $sql .= ' AND (s.nom like \'%'.addslashes($sall).'%\' OR f.facnumber like \'%'.addslashes($sall).'%\' OR f.note like \'%'.addslashes($sall).'%\' OR fd.description like \'%'.addslashes($sall).'%\')'; - } - - $sql .= ' GROUP BY f.rowid'; - - $sql .= ' ORDER BY '; - $listfield=split(',',$sortfield); - foreach ($listfield as $key => $value) - $sql.= $listfield[$key].' '.$sortorder.','; - $sql .= ' f.rowid DESC '; - - $sql .= $db->plimit($limit+1,$offset); - - $resql = $db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - - if ($socid) - { - $soc = new Societe($db); - $soc->fetch($socid); - } - - print_barre_liste($langs->trans('BillsCustomers').' '.($socid?' '.$soc->nom:''),$page,'facture.php','&socid='.$socid,$sortfield,$sortorder,'',$num); - - $i = 0; - print '
'."\n"; - print ''; - print ''; - print_liste_field_titre($langs->trans('Ref'),$_SERVER['PHP_SELF'],'f.facnumber','','&socid='.$socid.'&month='.$month.'&year=' . $year,'',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('Date'),$_SERVER['PHP_SELF'],'f.datef','','&socid='.$socid.'&month='.$month.'&year=' . $year,'align="center"',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('Company'),$_SERVER['PHP_SELF'],'s.nom','','&socid='.$socid.'&month='.$month.'&year=' . $year,'',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('AmountHT'),$_SERVER['PHP_SELF'],'f.total','','&socid='.$socid.'&month='.$month.'&year=' . $year,'align="right"',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('AmountTTC'),$_SERVER['PHP_SELF'],'f.total_ttc','','&socid='.$socid.'&month='.$month.'&year=' . $year,'align="right"',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('Received'),$_SERVER['PHP_SELF'],'am','','&socid='.$socid.'&month='.$month.'&year=' . $year,'align="right"',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('Status'),$_SERVER['PHP_SELF'],'fk_statut,paye,am','','&socid='.$socid.'&month='.$month.'&year=' . $year,'align="right"',$sortfield,$sortorder); - //print ''; - print ''; - - // Lignes des champs de filtre - - print ''; - print ''; - print ''; - print ''; - print '\n"; - - if ($num > 0) - { - $var=True; - $total=0; - $totalrecu=0; - - while ($i < min($num,$limit)) - { - $objp = $db->fetch_object($resql); - $var=!$var; - - print ''; - print '\n"; - - if ($objp->df > 0) - { - print ''; - } - else - { - print ''; - } - print ''; - print ''; - print ''; - print ''; - - // Affiche statut de la facture - print '"; - //print ""; - print "\n"; - $total+=$objp->total; - $total_ttc+=$objp->total_ttc; - $totalrecu+=$objp->am; - $i++; - } - - if (($offset + $num) <= $limit) - { - // Print total - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - } - } - - print "
 
'; - print ''; - print ''; - print ''; - //print ' '.$langs->trans('Year').': '; - $max_year = date("Y"); - $syear = $year; - //if ($syear == '') $syear = date("Y"); - $html->select_year($syear,'year',1, '', $max_year); - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ''; - print ' '; - print ''; - print "
'; - - $facturestatic->id=$objp->facid; - $facturestatic->ref=$objp->facnumber; - $facturestatic->type=$objp->type; - - print ''; - print ''; - if ($objp->datelimite < (time() - $conf->facture->client->warning_delay) && ! $objp->paye && $objp->fk_statut == 1 && ! $objp->am) - { - print ''; - } - print ''; - print '
'; - print $facturestatic->getNomUrl(1); - print $objp->increment; - print ''; - print img_warning($langs->trans('Late')); - print ''; - $filename=sanitize_string($objp->facnumber); - $filedir=$conf->facture->dir_output . '/' . sanitize_string($objp->facnumber); - $urlsource=$_SERVER['PHP_SELF'].'?facid='.$objp->facid; - $formfile->show_documents('facture',$filename,$filedir,$urlsource,'','','','','',1); - print '
'; - - print "
'; - $y = strftime('%Y',$objp->df); - $m = strftime('%m',$objp->df); - print strftime('%d',$objp->df); - print ' '; - print substr(strftime('%B',$objp->df),0,3).''; - print ' '; - print strftime('%Y',$objp->df).'!!!'.img_object($langs->trans('ShowCompany'),'company').' '.dolibarr_trunc($objp->nom,48).''.price($objp->total).''.price($objp->total_ttc).''.price($objp->am).''; - print $facturestatic->LibStatut($objp->paye,$objp->fk_statut,5,$objp->am,$objp->type); - print " 
'.$langs->trans('Total').''.price($total).''.price($total_ttc).''.price($totalrecu).' 
\n"; - print "
\n"; - $db->free($resql); - } - else - { - dolibarr_print_error($db); - } - } -} - -$db->close(); - -llxFooter('$Date$ - $Revision$'); -?> + + * Copyright (C) 2004 Eric Seigne + * Copyright (C) 2004-2008 Laurent Destailleur + * Copyright (C) 2005 Marc Barilley / Ocebo + * Copyright (C) 2005-2007 Regis Houssin + * Copyright (C) 2006 Andre Cianfarani + * + * 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. + */ + +/** + * \file htdocs/compta/facture.php + * \ingroup facture + * \brief Page de création/visu facture + * \version $Id$ + */ + +require('./pre.inc.php'); +require_once(DOL_DOCUMENT_ROOT."/html.formfile.class.php"); +require_once(DOL_DOCUMENT_ROOT.'/includes/modules/facture/modules_facture.php'); +require_once(DOL_DOCUMENT_ROOT.'/facture.class.php'); +require_once(DOL_DOCUMENT_ROOT.'/discount.class.php'); +require_once(DOL_DOCUMENT_ROOT.'/paiement.class.php'); +require_once(DOL_DOCUMENT_ROOT.'/lib/invoice.lib.php'); +if ($conf->projet->enabled) require_once(DOL_DOCUMENT_ROOT.'/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.'/propal.class.php'); +if ($conf->contrat->enabled) require_once(DOL_DOCUMENT_ROOT.'/contrat/contrat.class.php'); +if ($conf->commande->enabled) require_once(DOL_DOCUMENT_ROOT.'/commande/commande.class.php'); + +if (! $user->rights->facture->lire) +accessforbidden(); + +$langs->load('bills'); +$langs->load('companies'); +$langs->load('products'); +$langs->load('main'); + +$sall=isset($_GET['sall'])?trim($_GET['sall']):trim($_POST['sall']); +$mesg=isset($_GET['mesg'])?urldecode($_GET['mesg']):''; +$socid=isset($_GET['socid'])?$_GET['socid']:$_POST['socid']; +$projetid=isset($_GET['projetid'])?$_GET['projetid']:0; + +// Security check +$facid = isset($_GET["id"])?$_GET["id"]:''; +if ($user->societe_id) $socid=$user->societe_id; +$result = restrictedArea($user, 'facture', $facid,''); + +// Nombre de ligne pour choix de produit/service prédéfinis +$NBLINES=4; + + +/******************************************************************************/ +/* Actions */ +/******************************************************************************/ + +if ($_GET['action'] == 'reopen' && $user->rights->facture->creer) +{ + $fac = new Facture($db); + $result = $fac->fetch($_GET['facid']); + if ($fac->statut == 3 && ($fac->close_code == 'badcustomer' || $fac->close_code == 'abandon')) + { + $result = $fac->set_unpayed($user); + if ($result > 0) + { + Header('Location: '.$_SERVER["PHP_SELF"].'?facid='.$_GET['facid']); + exit; + } + else + { + $mesg='
'.$fac->error.'
'; + } + } +} + +// Suppression de la facture +if ($_POST['action'] == 'confirm_delete' && $_POST['confirm'] == 'yes' && $user->rights->facture->supprimer) +{ + if ($user->rights->facture->supprimer) + { + $fac = new Facture($db); + $result = $fac->fetch($_GET['facid']); + $result = $fac->delete(); + if ($result > 0) + { + Header('Location: '.$_SERVER["PHP_SELF"]); + exit; + } + else + { + $mesg='
'.$fac->error.'
'; + } + } +} + +/* + * Supprime une ligne produit AVEC ou SANS confirmation + */ +if (($_POST['action'] == 'confirm_deleteproductline' && $_POST['confirm'] == 'yes' && $conf->global->PRODUIT_CONFIRM_DELETE_LINE) +|| ($_GET['action'] == 'deleteline' && !$conf->global->PRODUIT_CONFIRM_DELETE_LINE)) +{ + if ($user->rights->facture->creer) + { + $fac = new Facture($db); + $fac->fetch($_GET['facid']); + $result = $fac->deleteline($_GET['rowid'], $user); + if ($result > 0) + { + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + + $result=facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); + if ($result > 0) + { + Header('Location: '.$_SERVER["PHP_SELF"].'?facid='.$_GET['facid']); + exit; + } + } + else + { + $mesg='
'.$fac->error.'
'; + $_GET['action']=''; + } + } +} + +// Supprime affectation d'un avoir a la facture +if ($_GET['action'] == 'unlinkdiscount') +{ + if ($user->rights->facture->creer) + { + $discount=new DiscountAbsolute($db); + $result=$discount->fetch($_GET["discountid"]); + $discount->unlink_invoice(); + } +} + +// Validation +if ($_GET['action'] == 'valid') +{ + $facture = new Facture($db); + $facture->fetch($_GET['facid']); + + // On verifie signe facture + if ($facture->type == 2) + { + // Si avoir, le signe doit etre négatif + if ($facture->total_ht >= 0) + { + $mesg='
'.$langs->trans("ErrorInvoiceAvoirMustBeNegative").'
'; + $_GET['action']=''; + } + } + else + { + // Si non avoir, le signe doit etre positif + if ($facture->total_ht < 0) + { + $mesg='
'.$langs->trans("ErrorInvoiceOfThisTypeMustBePositive").'
'; + $_GET['action']=''; + } + } +} + +if ($_POST['action'] == 'classin') +{ + $facture = new Facture($db); + $facture->fetch($_GET['facid']); + $facture->setProject($_POST['projetid']); +} + +if ($_POST['action'] == 'setmode') +{ + $facture = new Facture($db); + $facture->fetch($_GET['facid']); + $result=$facture->mode_reglement($_POST['mode_reglement_id']); + if ($result < 0) dolibarr_print_error($facture->db,$facture->error); +} + +if ($_POST['action'] == 'setconditions') +{ + $facture = new Facture($db); + $facture->fetch($_GET['facid']); + $result=$facture->cond_reglement($_POST['cond_reglement_id']); + if ($result < 0) dolibarr_print_error($facture->db,$facture->error); +} + +if ($_REQUEST['action'] == 'setremisepercent' && $user->rights->facture->creer) +{ + $fac = new Facture($db); + $fac->fetch($_REQUEST['facid']); + $result = $fac->set_remise($user, $_POST['remise_percent']); + $_GET['facid']=$_REQUEST['facid']; +} + +if ($_POST['action'] == "setabsolutediscount" && $user->rights->facture->creer) +{ + // POST[remise_id] ou POST[remise_id_for_payment] + if (! empty($_POST["remise_id"])) + { + $fac = new Facture($db); + $fac->id=$_GET['facid']; + $ret=$fac->fetch($_GET['facid']); + if ($ret > 0) + { + $result=$fac->insert_discount($_POST["remise_id"]); + if ($result < 0) + { + $mesg='
'.$fac->error.'
'; + } + } + else + { + dolibarr_print_error($db,$fac->error); + } + } + if (! empty($_POST["remise_id_for_payment"])) + { + require_once(DOL_DOCUMENT_ROOT.'/discount.class.php'); + $discount = new DiscountAbsolute($db); + $discount->fetch($_POST["remise_id_for_payment"]); + + $result=$discount->link_to_invoice(0,$_GET['facid']); + if ($result < 0) + { + $mesg='
'.$discount->error.'
'; + } + } +} + +if ($_POST['action'] == 'set_ref_client') +{ + $facture = new Facture($db); + $facture->fetch($_GET['facid']); + $facture->set_ref_client($_POST['ref_client']); +} + +// Classe à "validée" +if ($_REQUEST['action'] == 'confirm_valid' && $_REQUEST['confirm'] == 'yes' && $user->rights->facture->valider) +{ + $fac = new Facture($db); + $fac->fetch($_GET['facid']); + $fac->fetch_client(); + + $result = $fac->set_valid($user); + if ($result >= 0) + { + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); + } + else + { + $mesg='
'.$fac->error.'
'; + } +} + +// Repasse la facture en mode brouillon +if ($_GET['action'] == 'modif' && $user->rights->facture->modifier && $conf->global->FACTURE_ENABLE_EDITDELETE) +{ + $fac = new Facture($db); + $fac->fetch($_GET['facid']); + + // On vérifie si la facture a des paiements + $sql = 'SELECT pf.amount'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf'; + $sql.= ' WHERE pf.fk_facture = '.$fac->id; + + $result = $db->query($sql); + + if ($result) + { + $i = 0; + $num = $db->num_rows($result); + + while ($i < $num) + { + $objp = $db->fetch_object($result); + $totalpaye += $objp->amount; + $i++; + } + } + + $resteapayer = $fac->total_ttc - $totalpaye; + + // On vérifie si les lignes de factures ont été exportées en compta et/ou ventilées + $ventilExportCompta = $fac->getVentilExportCompta(); + + // On vérifie si aucun paiement n'a été effectué + if ($resteapayer == $fac->total_ttc && $fac->paye == 0 && $ventilExportCompta == 0) + { + $fac->set_draft($user); + //regénération PDF + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); + } +} + +// Classe à "payée" +if ($_POST['action'] == 'confirm_payed' && $_POST['confirm'] == 'yes' && $user->rights->facture->paiement) +{ + $fac = new Facture($db); + $fac->fetch($_GET['facid']); + $result = $fac->set_payed($user); +} +// Classe à "payée partiellement" +if ($_POST['action'] == 'confirm_payed_partially' && $_POST['confirm'] == 'yes' && $user->rights->facture->paiement) +{ + $fac = new Facture($db); + $fac->fetch($_GET['facid']); + $close_code=$_POST["close_code"]; + $close_note=$_POST["close_note"]; + if ($close_code) + { + $result = $fac->set_payed($user,$close_code,$close_note); + } + else + { + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("Reason")).'
'; + } +} +// Classe à "abandonnée" +if ($_POST['action'] == 'confirm_canceled' && $_POST['confirm'] == 'yes') +{ + $fac = new Facture($db); + $fac->fetch($_GET['facid']); + $close_code=$_POST["close_code"]; + $close_note=$_POST["close_note"]; + if ($close_code) + { + $result = $fac->set_canceled($user,$close_code,$close_note); + } + else + { + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("Reason")).'
'; + } +} + +// Convertir en reduc +if ($_POST['action'] == 'confirm_converttoreduc' && $_POST['confirm'] == 'yes' && $user->rights->facture->creer) +{ + $db->begin(); + + $fac = new Facture($db); + $fac->fetch($_GET['facid']); + $fac->fetch_client(); + $fac->fetch_lines(); + + if (! $fac->paye) // protection against multiple submit + { + // Boucle sur chaque taux de tva + $i=0; + foreach($fac->lignes as $ligne) + { + $amount_ht[$ligne->tva_tx]+=$ligne->total_ht; + $amount_tva[$ligne->tva_tx]+=$ligne->total_tva; + $amount_ttc[$ligne->tva_tx]+=$ligne->total_ttc; + $i++; + } + + // Insère une remise par famille de taux tva + $discount = new DiscountAbsolute($db); + $discount->desc='(CREDIT_NOTE)'; + $discount->tva_tx=abs($fac->total_ttc); + $discount->fk_soc=$fac->socid; + $discount->fk_facture_source=$fac->id; + + $error=0; + foreach($amount_ht as $tva_tx => $xxx) + { + $discount->amount_ht=abs($amount_ht[$tva_tx]); + $discount->amount_tva=abs($amount_tva[$tva_tx]); + $discount->amount_ttc=abs($amount_ttc[$tva_tx]); + $discount->tva_tx=abs($tva_tx); + + $result=$discount->create($user); + if ($result < 0) + { + $error++; + break; + } + } + + if (! $error) + { + // Classe facture + $result=$fac->set_payed($user); + if ($result > 0) + { + //$mesg='OK'.$discount->id; + $db->commit(); + } + else + { + $mesg='
'.$fac->error.'
'; + $db->rollback(); + } + } + else + { + $mesg='
'.$discount->error.'
'; + $db->rollback(); + } + } +} + + + +/* + * Insert invoice + */ +if ($_POST['action'] == 'add' && $user->rights->facture->creer) +{ + $facture = new Facture($db); + $facture->socid=$_POST['socid']; + $facture->fetch_client(); + + $db->begin(); + + // Facture remplacement + if ($_POST['type'] == 1) + { + $datefacture = dolibarr_mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); + if (empty($datefacture)) + { + $error=1; + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("Date")).'
'; + } + + if (! ($_POST['fac_replacement'] > 0)) + { + $error=1; + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("ReplaceInvoice")).'
'; + } + + if (! $error) + { + // Si facture remplacement + + $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); + } + } + + // Facture avoir + if ($_POST['type'] == 2) + { + if (! $_POST['fac_avoir'] > 0) + { + $error=1; + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("CorrectInvoice")).'
'; + } + + $datefacture = dolibarr_mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); + if (empty($datefacture)) + { + $error=1; + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("Date")).'
'; + } + + if (! $error) + { + // Si facture avoir + $datefacture = dolibarr_mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); + + //$result=$facture->fetch($_POST['fac_avoir']); + + $facture->socid = $_POST['socid']; + $facture->number = $_POST['facnumber']; + $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 = 0; + $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 avoir + $facture->fk_facture_source = $_POST['fac_avoir']; + $facture->type = 2; + + $facid = $facture->create($user); + } + } + + if ($_POST['type'] == 0 && $_POST['fac_rec'] > 0) + { + // Si facture récurrente + $datefacture = dolibarr_mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); + + $facture->socid = $_POST['socid']; + $facture->type = $_POST['type']; + $facture->number = $_POST['facnumber']; + $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']; + + // Propriétés particulieres a facture recurrente + $facture->fac_rec = $_POST['fac_rec']; + $facture->type = 0; + + $facid = $facture->create($user); + } + + if ($_POST['type'] == 0 && $_POST['fac_rec'] <= 0) + { + $datefacture = dolibarr_mktime(12, 0 , 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); + if (empty($datefacture)) + { + $error=1; + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("Date")).'
'; + } + + if (! $error) + { + // Si facture standard + + $facture->socid = $_POST['socid']; + $facture->type = $_POST['type']; + $facture->number = $_POST['facnumber']; + $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->amount = $_POST['amount']; + $facture->remise_absolue = $_POST['remise_absolue']; + $facture->remise_percent = $_POST['remise_percent']; + + if (! $_POST['propalid'] && ! $_POST['commandeid'] && ! $_POST['contratid']) + { + for ($i = 1; $i <= $NBLINES; $i++) + { + if ($_POST['idprod'.$i]) + { + $startday=''; + $endday=''; + if ($_POST['date_start'.$i.'year'] && $_POST['date_start'.$i.'month'] && $_POST['date_start'.$i.'day']) { + $startday=$_POST['date_start'.$i.'year'].'-'.$_POST['date_start'.$i.'month'].'-'.$_POST['date_start'.$i.'day']; + } + if ($_POST['date_end'.$i.'year'] && $_POST['date_end'.$i.'month'] && $_POST['date_end'.$i.'day']) { + $endday=$_POST['date_end'.$i.'year'].'-'.$_POST['date_end'.$i.'month'].'-'.$_POST['date_end'.$i.'day']; + } + $facture->add_product($_POST['idprod'.$i],$_POST['qty'.$i],$_POST['remise_percent'.$i],$startday,$endday); + } + } + + $facid = $facture->create($user); + } + else + { + /* + * Si creation depuis propale + */ + if ($_POST['propalid']) + { + $facture->propalid = $_POST['propalid']; + $facid = $facture->create($user); + + if ($facid > 0) + { + $prop = New Propal($db); + if ( $prop->fetch($_POST['propalid']) ) + { + for ($i = 0 ; $i < sizeof($prop->lignes) ; $i++) + { + $desc=($prop->lignes[$i]->desc?$prop->lignes[$i]->desc:$prop->lignes[$i]->libelle); + + $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, + '', + '', + 0, + $prop->lignes[$i]->info_bits, + $prop->lignes[$i]->fk_remise_except + ); + + if ($result < 0) + { + $error++; + break; + } + } + } + else + { + $error++; + } + } + else + { + $error++; + } + } + + /* + * Si création depuis commande + */ + 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; + for ($i = 0 ; $i < sizeof($lines) ; $i++) + { + $desc=($lines[$i]->desc ? $lines[$i]->desc : $lines[$i]->libelle); + + $result = $facture->addline( + $facid, + $desc, + $lines[$i]->subprice, + $lines[$i]->qty, + $lines[$i]->tva_tx, + $lines[$i]->fk_product, + $lines[$i]->remise_percent, + '', + '', + 0, + $lines[$i]->info_bits, + $lines[$i]->fk_remise_except + ); + + if ($result < 0) + { + $error++; + break; + } + } + } + else + { + $error++; + } + } + else + { + $error++; + } + } + + /* + * Si création depuis contrat + */ + 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); + + // Plage de 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 + ); + + if ($result < 0) + { + $error++; + break; + } + } + } + else + { + $error++; + } + } + else + { + $error++; + } + } + } + } + } + + // Fin création facture, on l'affiche + if ($facid > 0 && ! $error) + { + $db->commit(); + Header('Location: '.$_SERVER["PHP_SELF"].'?facid='.$facid); + exit; + } + else + { + $db->rollback(); + $_GET["action"]='create'; + $_GET["propalid"]=$_POST["propalid"]; + $_GET["commandeid"]=$_POST["commandeid"]; + $_GET["contratid"]=$_POST["contratid"]; + if (! $mesg) $mesg='
'.$facture->error.'
'; + } +} + +/* + * Ajout d'une ligne produit dans la facture + */ +if (($_POST['action'] == 'addligne' || $_POST['action'] == 'addligne_predef') && $user->rights->facture->creer) +{ + if ($_POST['qty'] && (($_POST['pu']!='' && ($_POST['np_desc'] || $_POST['dp_desc'])) || $_POST['idprod'])) + { + $fac = new Facture($db); + $ret=$fac->fetch($_POST['facid']); + if ($ret < 0) + { + dolibarr_print_error($db,$fac->error); + exit; + } + $ret=$fac->fetch_client(); + + $date_start=''; + $date_end=''; + // Si ajout champ produit libre + if ($_POST['action'] == 'addligne') + { + if ($_POST['date_startyear'] && $_POST['date_startmonth'] && $_POST['date_startday']) + { + $date_start=$_POST['date_startyear'].'-'.$_POST['date_startmonth'].'-'.$_POST['date_startday']; + } + if ($_POST['date_endyear'] && $_POST['date_endmonth'] && $_POST['date_endday']) + { + $date_end=$_POST['date_endyear'].'-'.$_POST['date_endmonth'].'-'.$_POST['date_endday']; + } + } + // Si ajout champ produit prédéfini + if ($_POST['action'] == 'addligne_predef') + { + if ($_POST['date_start_predefyear'] && $_POST['date_start_predefmonth'] && $_POST['date_start_predefday']) + { + $date_start=$_POST['date_start_predefyear'].'-'.$_POST['date_start_predefmonth'].'-'.$_POST['date_start_predefday']; + } + if ($_POST['date_end_predefyear'] && $_POST['date_end_predefmonth'] && $_POST['date_end_predefday']) + { + $date_end=$_POST['date_end_predefyear'].'-'.$_POST['date_end_predefmonth'].'-'.$_POST['date_end_predefday']; + } + } + + $price_base_type = 'HT'; + + // Ecrase $pu par celui du produit + // Ecrase $desc par celui du produit + // Ecrase $txtva par celui du produit + // Ecrase $base_price_type par celui du produit + if ($_POST['idprod']) + { + $prod = new Product($db, $_POST['idprod']); + $prod->fetch($_POST['idprod']); + + $tva_tx = get_default_tva($mysoc,$fac->client,$prod->tva_tx); + $tva_npr = get_default_npr($mysoc,$fac->client,$prod->tva_tx); + + // On defini prix unitaire + if ($conf->global->PRODUIT_MULTIPRICES == 1) + { + $pu_ht = $prod->multiprices[$fac->client->price_level]; + $pu_ttc = $prod->multiprices_ttc[$fac->client->price_level]; + $price_base_type = $prod->multiprices_base_type[$fac->client->price_level]; + } + else + { + $pu_ht = $prod->price; + $pu_ttc = $prod->price_ttc; + $price_base_type = $prod->price_base_type; + } + + // On reevalue prix selon taux tva car taux tva transaction peut etre different + // de ceux du produit par defaut (par exemple si pays different entre vendeur et acheteur). + if ($tva_tx != $prod->tva_tx) + { + if ($price_base_type != 'HT') + { + $pu_ht = price2num($pu_ttc / (1 + ($tva_tx/100)), 'MU'); + } + else + { + $pu_ttc = price2num($pu_ht * (1 + ($tva_tx/100)), 'MU'); + } + } + + $desc = $prod->description; + $desc.= $prod->description && $_POST['np_desc'] ? "\n" : ""; + $desc.= $_POST['np_desc']; + } + else + { + $pu_ht=$_POST['pu']; + $tva_tx=eregi_replace('\*','',$_POST['tva_tx']); + $tva_npr=eregi('\*',$_POST['tva_tx'])?1:0; + $desc=$_POST['dp_desc']; + } + + $info_bits=0; + if ($tva_npr) $info_bits |= 0x01; + + // Insert line + $result = $fac->addline( + $_POST['facid'], + $desc, + $pu_ht, + $_POST['qty'], + $tva_tx, + $_POST['idprod'], + $_POST['remise_percent'], + $date_start, + $date_end, + 0, + $info_bits, + '', + $price_base_type, + $pu_ttc + ); + } + + if ($result > 0) + { + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + facture_pdf_create($db, $fac->id, $fac->modelpdf, $outputlangs); + } + else + { + $mesg='
'.$fac->error.'
'; + } + + $_GET['facid']=$_POST['facid']; // Pour réaffichage de la fiche en cours d'édition +} + +if ($_POST['action'] == 'updateligne' && $user->rights->facture->creer && $_POST['save'] == $langs->trans('Save')) +{ + $fac = new Facture($db,'',$_POST['facid']); + if (! $fac->fetch($_POST['facid']) > 0) dolibarr_print_error($db); + + $date_start=''; + $date_end=''; + if ($_POST['date_startyear'] && $_POST['date_startmonth'] && $_POST['date_startday']) { + $date_start=$_POST['date_startyear'].'-'.$_POST['date_startmonth'].'-'.$_POST['date_startday']; + } + if ($_POST['date_endyear'] && $_POST['date_endmonth'] && $_POST['date_endday']) { + $date_end=$_POST['date_endyear'].'-'.$_POST['date_endmonth'].'-'.$_POST['date_endday']; + } + + // Define info_bits + $info_bits=0; + if (eregi('\*',$_POST['tva_tx'])) $info_bits |= 0x01; + + // Define vat_rate + $vat_rate=$_POST['tva_tx']; + $vat_rate=eregi_replace('\*','',$vat_rate); + + $result = $fac->updateline($_POST['rowid'], + $_POST['desc'], + $_POST['price'], + $_POST['qty'], + $_POST['remise_percent'], + $date_start, + $date_end, + $vat_rate, + 'HT', + $info_bits + ); + + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); + + $_GET['facid']=$_POST['facid']; // Pour réaffichage de la fiche en cours d'édition +} + +if ($_POST['action'] == 'updateligne' && $user->rights->facture->creer && $_POST['cancel'] == $langs->trans('Cancel')) +{ + Header('Location: '.$_SERVER["PHP_SELF"].'?facid='.$_POST['facid']); // Pour réaffichage de la fiche en cours d'édition + exit; +} + + +/* + * Ordonnancement des lignes + */ + +if ($_GET['action'] == 'up' && $user->rights->facture->creer) +{ + $fac = new Facture($db,'',$_GET['facid']); + $fac->fetch($_GET['facid']); + $fac->line_up($_GET['rowid']); + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); + Header ('Location: '.$_SERVER["PHP_SELF"].'?facid='.$_GET["facid"].'#'.$_GET['rowid']); + exit; +} + +if ($_GET['action'] == 'down' && $user->rights->facture->creer) +{ + $fac = new Facture($db,'',$_GET['facid']); + $fac->fetch($_GET['facid']); + $fac->line_down($_GET['rowid']); + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); + Header ('Location: '.$_SERVER["PHP_SELF"].'?facid='.$_GET["facid"].'#'.$_GET['rowid']); + exit; +} + +/* + * Add file + */ +if ($_POST['addfile']) +{ + // Set tmp user directory + $conf->users->dir_tmp=DOL_DATA_ROOT."/users/".$user->id; + $upload_dir = $conf->users->dir_tmp.'/temp/'; + + if (! empty($_FILES['addedfile']['tmp_name'])) + { + if (! is_dir($upload_dir)) create_exdir($upload_dir); + + if (is_dir($upload_dir)) + { + if (dol_move_uploaded_file($_FILES['addedfile']['tmp_name'], $upload_dir . "/" . $_FILES['addedfile']['name'],0) > 0) + { + $mesg = '
'.$langs->trans("FileTransferComplete").'
'; + //print_r($_FILES); + + include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); + $formmail = new FormMail($db); + $formmail->add_attached_files($upload_dir . "/" . $_FILES['addedfile']['name'],$_FILES['addedfile']['name'],$_FILES['addedfile']['type']); + } + else + { + // Echec transfert (fichier dépassant la limite ?) + $mesg = '
'.$langs->trans("ErrorFileNotUploaded").'
'; + // print_r($_FILES); + } + } + } + $_GET["action"]='presend'; +} + +/* + * Send mail + */ +if (($_POST['action'] == 'send' || $_POST['action'] == 'relance') && ! $_POST['addfile'] && ! $_POST['cancel']) +{ + $langs->load('mails'); + + $fac = new Facture($db,'',$_POST['facid']); + $result=$fac->fetch($_POST['facid']); + if ($result) + { + $ref = sanitize_string($fac->ref); + $file = $conf->facture->dir_output . '/' . $ref . '/' . $ref . '.pdf'; + + if (is_readable($file)) + { + $fac->fetch_client(); + + if ($_POST['sendto']) + { + // Le destinataire a été fourni via le champ libre + $sendto = $_POST['sendto']; + $sendtoid = 0; + } + elseif ($_POST['receiver']) + { + // Le destinataire a été fourni via la liste déroulante + if ($_POST['receiver'] < 0) // Id du tiers + { + $sendto = $fac->client->email; + $sendtoid = 0; + } + else // Id du contact + { + $sendto = $fac->client->contact_get_email($_POST['receiver']); + $sendtoid = $_POST['receiver']; + } + } + + if (strlen($sendto)) + { + $langs->load("commercial"); + + $from = $_POST['fromname'] . ' <' . $_POST['frommail'] .'>'; + $replyto = $_POST['replytoname']. ' <' . $_POST['replytomail'].'>'; + $message = $_POST['message']; + $sendtocc = $_POST['sendtocc']; + $deliveryreceipt = $_POST['deliveryreceipt']; + + if ($_POST['action'] == 'send') + { + if (strlen($_POST['subject'])) $subject = $_POST['subject']; + else $subject = $langs->transnoentities('Bill').' '.$fac->ref; + $actiontypecode='AC_FAC'; + $actionmsg=$langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto.".\n"; + if ($message) + { + $actionmsg.=$langs->transnoentities('MailTopic').": ".$subject."\n"; + $actionmsg.=$langs->transnoentities('TextUsedInTheMessageBody').":\n"; + $actionmsg.=$message; + } + $actionmsg2=$langs->transnoentities('Action'.$actiontypecode); + } + if ($_POST['action'] == 'relance') + { + if (strlen($_POST['subject'])) $subject = $_POST['subject']; + else $subject = $langs->transnoentities('Relance facture '.$fac->ref); + $actiontypecode='AC_FAC'; + $actionmsg=$langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto.".\n"; + if ($message) { + $actionmsg.=$langs->transnoentities('MailTopic').": ".$subject."\n"; + $actionmsg.=$langs->transnoentities('TextUsedInTheMessageBody').":\n"; + $actionmsg.=$message; + } + $actionmsg2=$langs->transnoentities('Action'.$actiontypecode); + } + + // Create form object + include_once('../html.formmail.class.php'); + $formmail = new FormMail($db); + + $attachedfiles=$formmail->get_attached_files(); + $filepath = $attachedfiles['paths']; + $filename = $attachedfiles['names']; + $mimetype = $attachedfiles['mimes']; + + // Send mail + require_once(DOL_DOCUMENT_ROOT.'/lib/CMailFile.class.php'); + $mailfile = new CMailFile($subject,$sendto,$from,$message,$filepath,$mimetype,$filename,$sendtocc,'',$deliveryreceipt); + if ($mailfile->error) + { + $mesg='
'.$mailfile->error.'
'; + } + else + { + $result=$mailfile->sendfile(); + if ($result) + { + $mesg='
'.$langs->trans('MailSuccessfulySent',$from,$sendto).'.
'; + + $error=0; + + // Initialisation donnees + $fac->sendtoid=$sendtoid; + $fac->actiontypecode=$actiontypecode; + $fac->actionmsg = $actionmsg; + $fac->actionmsg2= $actionmsg2; + $fac->facid=$fac->id; + + // Appel des triggers + include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); + $interface=new Interfaces($db); + $result=$interface->run_triggers('BILL_SENTBYMAIL',$fac,$user,$langs,$conf); + if ($result < 0) { $error++; $this->errors=$interface->errors; } + // Fin appel triggers + + if ($error) + { + dolibarr_print_error($db); + } + else + { + // Redirect here + // This avoid sending mail twice if going out and then back to page + Header('Location: '.$_SERVER["PHP_SELF"].'?facid='.$fac->id.'&mesg='.urlencode($mesg)); + exit; + } + } + else + { + $langs->load("other"); + $mesg='
'; + if ($mailfile->error) + { + $mesg.=$langs->trans('ErrorFailedToSendMail',$from,$sendto); + $mesg.='
'.$mailfile->error; + } + else + { + $mesg.='No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS'; + } + $mesg.='
'; + } + } + } + else + { + $langs->load("other"); + $mesg='
'.$langs->trans('ErrorMailRecipientIsEmpty').'
'; + dolibarr_syslog('Recipient email is empty'); + } + } + else + { + $langs->load("other"); + $mesg='
'.$langs->trans('ErrorCantReadFile',$file).'
'; + dolibarr_syslog('Failed to read file: '.$file); + } + } + else + { + $langs->load("other"); + $mesg='
'.$langs->trans('ErrorFailedToReadEntity',$langs->trans("Invoice")).'
'; + dolibarr_syslog('Impossible de lire les données de la facture. Le fichier facture n\'a peut-être pas été généré.'); + } +} + +/* + * Générer ou regénérer le document PDF + */ +if ($_REQUEST['action'] == 'builddoc') // En get ou en post +{ + $fac = new Facture($db, 0, $_GET['facid']); + $fac->fetch($_GET['facid']); + + if ($_REQUEST['model']) + { + $fac->setDocModel($user, $_REQUEST['model']); + } + + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + + $result=facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); + if ($result <= 0) + { + dolibarr_print_error($db,$result); + exit; + } + else + { + Header ('Location: '.$_SERVER["PHP_SELF"].'?facid='.$fac->id.'#builddoc'); + } +} + + + +/* + * View + */ + +llxHeader('',$langs->trans('Bill'),'HelpInvoice'); + +$html = new Form($db); +$formfile = new FormFile($db); + + +/********************************************************************* + * + * Mode creation + * + **********************************************************************/ +if ($_GET['action'] == 'create') +{ + $facturestatic=new Facture($db); + + print_titre($langs->trans('NewBill')); + + if ($mesg) print $mesg; + + $soc = new Societe($db); + + if ($_GET['propalid']) + { + $propal = New Propal($db); + $propal->fetch($_GET['propalid']); + $propal->fetch_client(); + + $projetid=$propal->projetidp; + $ref_client=$propal->ref_client; + + $soc=$propal->client; + $cond_reglement_id = $propal->cond_reglement_id; + $mode_reglement_id = $propal->mode_reglement_id; + $remise_percent = $propal->remise_percent; + $remise_absolue = $propal->remise_absolue; + } + elseif ($_GET['commandeid']) + { + $commande = New Commande($db); + $commande->fetch($_GET['commandeid']); + $commande->fetch_client(); + + $projetid=$commande->projet_id; + $ref_client=$commande->ref_client; + + $soc=$commande->client; + $cond_reglement_id = $commande->cond_reglement_id; + $mode_reglement_id = $commande->mode_reglement_id; + $remise_percent = $commande->remise_percent; + $remise_absolue = $commande->remise_absolue; + } + elseif ($_GET['contratid']) + { + $contrat = New Contrat($db); + $contrat->fetch($_GET['contratid']); + $contrat->fetch_client(); + + $projetid=$contrat->fk_projet; + + $soc=$contrat->client; + $cond_reglement_id = $soc->cond_reglement; + $mode_reglement_id = $soc->mod_reglement; + $remise_percent = $soc->remise_client; + $remise_absolue = 0; + $dateinvoice=empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0; + } + else + { + $soc->fetch($socid); + $cond_reglement_id = $soc->cond_reglement; + $mode_reglement_id = $soc->mode_reglement; + $remise_percent = $soc->remise_client; + $remise_absolue = 0; + $dateinvoice=empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0; + } + $absolute_discount=$soc->getAvailableDiscounts(); + + + print '
'; + print ''; + print '' ."\n"; + print ''; + + print ''; + + // Ref + print ''; + + /* + \todo + L'info "Reference commande client" est une carac de la commande et non de la facture. + Elle devrait donc etre stockée sur l'objet commande liée à la facture et non sur la facture. + Pour ceux qui veulent l'utiliser au niveau de la facture, positionner la + constante FAC_USE_CUSTOMER_ORDER_REF à 1. + */ + if ($conf->global->FAC_USE_CUSTOMER_ORDER_REF) + { + print ''; + } + + // Tiers + print ''; + print ''."\n"; + + // Type de facture + $facids=$facturestatic->list_replacable_invoices($soc->id); + if ($facids < 0) + { + dolibarr_print_error($db,$facturestatic); + exit; + } + $options=""; + foreach ($facids as $facparam) + { + $options.=''; + } + $facids=$facturestatic->list_qualified_avoir_invoices($soc->id); + $optionsav=""; + foreach ($facids as $key => $value) + { + $newinvoice=new Facture($db); + $newinvoice->fetch($key); + $optionsav.=''; + } + + print ''; + + // Discounts for third party + print ''; + + // Date invoice + print ''; + + // Conditions de règlement + print ''; + + // Mode de règlement + print ''; + + // Projet + if ($conf->projet->enabled) + { + $langs->load('projects'); + print ''; + } + + // Modele PDF + print ''; + print '"; + + // Note publique + print ''; + print ''; + print ''; + + // Note privée + if (! $user->societe_id) + { + print ''; + print ''; + print ''; + } + + if ($_GET['propalid'] > 0) + { + print ''."\n"; + print ''."\n"; + print ''."\n"; + //print ''."\n"; + //print ''."\n"; + print ''; + + print ''; + print ''; + print '"; + print '"; + } + elseif ($_GET['commandeid'] > 0) + { + print ''."\n"; + print ''."\n"; + print ''."\n"; + //print ''."\n"; + //print ''."\n"; + print ''; + + print ''; + print ''; + print '"; + print '"; + } + elseif ($_GET['contratid'] > 0) + { + // Calcul contrat->price (HT), contrat->total (TTC), contrat->tva + $contrat->remise_absolue=$remise_absolue; + $contrat->remise_percent=$remise_percent; + $contrat->update_price(); + + print ''."\n"; + print ''."\n"; + print ''."\n"; + // print ''."\n"; + // print ''."\n"; + print ''; + + print ''; + print ''; + print '"; + print '"; + } + else + { + if ($conf->global->PRODUCT_SHOW_WHEN_CREATE) + { + print ''; + } + } + + /* + * Factures récurrentes + */ + if (! $conf->global->FACTURE_DISABLE_RECUR) + { + if ($_GET['propalid'] == 0 && $_GET['commandeid'] == 0 && $_GET['contratid'] == 0) + { + $sql = 'SELECT r.rowid, r.titre, r.amount FROM '.MAIN_DB_PREFIX.'facture_rec as r'; + $sql .= ' WHERE r.fk_soc = '.$soc->id; + if ( $db->query($sql) ) + { + $num = $db->num_rows(); + $i = 0; + + if ($num > 0) + { + print ''; + } + $db->free(); + } + else + { + dolibarr_print_error($db); + } + } + } + + // Bouton "Create Draft" + print ''; + print "
'.$langs->trans('Ref').''.$langs->trans('Draft').'
'.$langs->trans('RefCustomerOrder').''; + print ''; + print '
'.$langs->trans('Company').''; + print $soc->getNomUrl(1); + print ''; + print '
'.$langs->trans('Type').''; + print ''."\n"; + + // Type + print ''."\n"; + + print ''."\n"; + + print ''."\n"; + + print '
'; + print ''; + print ''; + $desc=$html->textwithhelp($langs->trans("InvoiceStandardAsk"),$langs->transnoentities("InvoiceStandardDesc"),1); + print $desc; + print '
'; + print ''; + print ''; + $text=$langs->trans("InvoiceReplacementAsk").' '; + $text.=''; + $desc=$html->textwithhelp($text,$langs->transnoentities("InvoiceReplacementDesc"),1); + print $desc; + print '
'; + print ''; + print ''; + $text=$langs->transnoentities("InvoiceAvoirAsk").' '; + // $text.=''; + $text.=''; + $desc=$html->textwithhelp($text,$langs->transnoentities("InvoiceAvoirDesc"),1); + //.' ('.$langs->trans("FeatureNotYetAvailable").')',$langs->transnoentities("InvoiceAvoirDesc"),1); + print $desc; + print '
'; + print '
'.$langs->trans('Discounts').''; + if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client); + else print $langs->trans("CompanyHasNoRelativeDiscount"); + print '. '; + if ($absolute_discount) print $langs->trans("CompanyHasAbsoluteDiscount",price($absolute_discount),$langs->trans("Currency".$conf->monnaie)); + else print $langs->trans("CompanyHasNoAbsoluteDiscount"); + print '.'; + print '
'.$langs->trans('Date').''; + $html->select_date($dateinvoice,'','','','',"add"); + print '
'.$langs->trans('PaymentConditionsShort').''; + $html->select_conditions_paiements($cond_reglement_id,'cond_reglement_id'); + print '
'.$langs->trans('PaymentMode').''; + $html->select_types_paiements($mode_reglement_id,'mode_reglement_id'); + print '
'.$langs->trans('Project').''; + select_projects($soc->id, $projetid, 'projetid'); + print '
'.$langs->trans('Model').''; + include_once(DOL_DOCUMENT_ROOT.'/includes/modules/facture/modules_facture.php'); + $model=new ModelePDFFactures(); + $liste=$model->liste_modeles($db); + $html->select_array('model',$liste,$conf->global->FACTURE_ADDON_PDF); + print "
'.$langs->trans('NotePublic').''; + print '
'.$langs->trans('NotePrivate').''; + print '
'.$langs->trans('Proposal').''.img_object($langs->trans("ShowPropal"),'propal').' '.$propal->ref.'
'.$langs->trans('TotalHT').''.price($propal->price).'
'.$langs->trans('TotalVAT').''.price($propal->total_tva)."
'.$langs->trans('TotalTTC').''.price($propal->total)."
'.$langs->trans('Order').''.img_object($langs->trans("ShowOrder"),'order').' '.$commande->ref.'
'.$langs->trans('TotalHT').''.price($commande->total_ht).'
'.$langs->trans('TotalVAT').''.price($commande->total_tva)."
'.$langs->trans('TotalTTC').''.price($commande->total_ttc)."
'.$langs->trans('Contract').''.img_object($langs->trans("ShowContract"),'contract').' '.$contrat->ref.'
'.$langs->trans('TotalHT').''.price($contrat->total_ht).'
'.$langs->trans('TotalVAT').''.price($contrat->total_tva)."
'.$langs->trans('TotalTTC').''.price($contrat->total_ttc)."
'; + + // Zone de choix des produits prédéfinis à la création + print ''; + print ''; + print ''; + print ''; + print ''; + if ($conf->service->enabled) + { + print ''; + } + print ''; + for ($i = 1 ; $i <= $NBLINES ; $i++) + { + print ''; + print ''; + print ''; + print ''; + print ''; + // Si le module service est actif, on propose des dates de début et fin à la ligne + if ($conf->service->enabled) + { + print ''; + } + print "\n"; + } + + print '
'.$langs->trans('ProductsAndServices').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').'     '.$langs->trans('ServiceLimitedDuration').'
'; + // multiprix + if($conf->global->PRODUIT_MULTIPRICES == 1) + $html->select_produits('','idprod'.$i,'',$conf->produit->limit_size,$soc->price_level); + else + $html->select_produits('','idprod'.$i,'',$conf->produit->limit_size); + print '% '; + print ''; + print ''; + print '
'; + print $langs->trans('From').' '; + print ''; + print $html->select_date('','date_start'.$i,0,0,1,"add"); + print '
'; + print $langs->trans('to').' '; + print ''; + print $html->select_date('','date_end'.$i,0,0,1,"add"); + print '
'; + print '
'; + print '
'.$langs->trans('CreateFromRepeatableInvoice').' :
\n"; + + print "
\n"; + + // Si creation depuis un propal + if ($_GET['propalid']) + { + $title=$langs->trans('ProductsAndServices'); + + $sql = 'SELECT pt.rowid, pt.description, pt.price, pt.fk_product, pt.fk_remise_except,'; + $sql.= ' pt.qty, pt.tva_tx, pt.remise_percent, pt.subprice, pt.info_bits,'; + $sql.= ' p.label as product, p.ref, p.fk_product_type, p.rowid as prodid,'; + $sql.= ' p.description as product_desc'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product=p.rowid'; + $sql.= ' WHERE pt.fk_propal = '.$_GET['propalid']; + $sql.= ' ORDER BY pt.rang ASC, pt.rowid'; + } + if ($_GET['commandeid']) + { + $title=$langs->trans('Products'); + + $sql = 'SELECT pt.rowid, pt.subprice, pt.tva_tx, pt.qty, pt.fk_remise_except, pt.remise_percent, pt.description, pt.info_bits,'; + $sql.= ' p.label as product, p.ref, p.rowid as prodid'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as pt'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product = p.rowid'; + $sql.= ' WHERE pt.fk_commande = '.$commande->id; + $sql.= ' ORDER BY pt.rowid ASC'; + } + if ($_GET['contratid']) + { + $title=$langs->trans('Services'); + + $sql = 'SELECT pt.rowid, pt.subprice, pt.tva_tx, pt.qty, pt.remise_percent, pt.description, pt.info_bits,'; + $sql.= ' pt.date_ouverture_prevue as date_debut_prevue, pt.date_ouverture as date_debut_reel,'; + $sql.= ' pt.date_fin_validite as date_fin_prevue, pt.date_cloture as date_fin_reel,'; + $sql.= ' p.label as product, p.ref, p.rowid as prodid'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'contratdet as pt'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product = p.rowid'; + $sql.= ' WHERE pt.fk_contrat = '.$contrat->id; + $sql.= ' ORDER BY pt.rowid ASC'; + } + if ($_GET['propalid'] || $_GET['commandeid'] || $_GET['contratid']) + { + print '
'; + print_titre($title); + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + // Lignes + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + $i = 0; + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print '\n"; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + } + else + { + dolibarr_print_error($db); + } + + print '
'.$langs->trans('Ref').''.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').'
'; + if (($objp->info_bits & 2) == 2) + { + print ''; + print img_object($langs->trans("ShowReduc"),'reduc').' '.$langs->trans("Discount"); + print ''; + } + else if ($objp->prodid) + { + print ''.img_object($langs->trans(''),'service').' '.$objp->ref.''; + print $objp->product?' - '.$objp->product:''; + // Plage de dates si contrat + if ($_GET['contratid']) + { + $date_start=$objp->date_debut_prevue; + if ($objp->date_debut_reel) $date_start=$objp->date_debut_reel; + $date_end=$objp->date_fin_prevue; + if ($objp->date_fin_reel) $date_end=$objp->date_fin_reel; + print_date_range($date_start,$date_end); + } + } + else + { + print ' '; + } + print "'; + if ($objp->description) + { + if ($objp->description == '(CREDIT_NOTE)') + { + $discount=new DiscountAbsolute($db); + $discount->fetch($objp->fk_remise_except); + print $langs->transnoentities("DiscountFromCreditNote",$discount->getNomUrl(0)); + } + else + { + print dolibarr_trunc($objp->description,60); + } + } + print ''.vatrate($objp->tva_tx).'%'.price($objp->subprice).''; + print (($objp->info_bits & 2) != 2) ? $objp->qty : ' '; + print ''; + print (($objp->info_bits & 2) != 2) ? $objp->remise_percent.'%' : ' '; + print '
'; + } + +} +else +{ + $id = $_GET['facid']; + if ($id > 0) + { + /* *************************************************************************** */ + /* */ + /* Fiche en mode visu / edition */ + /* */ + /* *************************************************************************** */ + if ($mesg) print $mesg.'
'; + + $facstatic = new Facture($db); + + $fac = new Facture($db); + $result=$fac->fetch($_GET['facid']); + if ($result > 0) + { + if ($user->societe_id>0 && $user->societe_id!=$fac->socid) accessforbidden('',0); + + $result=$fac->fetch_client(); + + $soc = new Societe($db, $fac->socid); + $soc->fetch($fac->socid); + + $totalpaye = $fac->getSommePaiement(); + $totalavoir = $fac->getSommeCreditNote(); + + // We cal also use bcadd to avoid pb with floating points + // For example print 239.2 - 229.3 - 9.9; does not return 0. + //$resteapayer=bcadd($fac->total_ttc,$totalpaye,$conf->global->MAIN_MAX_DECIMALS_TOT); + //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT); + $resteapayer = price2num($fac->total_ttc - $totalpaye - $totalavoir,'MT'); + + if ($fac->paye) $resteapayer=0; + $resteapayeraffiche=$resteapayer; + + $absolute_discount=$soc->getAvailableDiscounts('','fk_facture_source IS NULL'); + $absolute_creditnote=$soc->getAvailableDiscounts('','fk_facture_source IS NOT NULL'); + + $author = new User($db); + if ($fac->user_author) + { + $author->id = $fac->user_author; + $author->fetch(); + } + + $facidnext=$fac->getIdReplacingInvoice(); + + + $head = facture_prepare_head($fac); + + dolibarr_fiche_head($head, 'compta', $langs->trans('InvoiceCustomer')); + + /* + * Confirmation de la conversion de l'avoir en reduc + */ + if ($_GET['action'] == 'converttoreduc') + { + $text=$langs->trans('ConfirmConvertToReduc'); + $html->form_confirm($_SERVER['PHP_SELF'].'?facid='.$fac->id,$langs->trans('ConvertToReduc'),$text,'confirm_converttoreduc'); + print '
'; + } + + /* + * Confirmation de la suppression de la facture + */ + if ($_GET['action'] == 'delete') + { + $text=$langs->trans('ConfirmDeleteBill'); + $html->form_confirm($_SERVER['PHP_SELF'].'?facid='.$fac->id,$langs->trans('DeleteBill'),$text,'confirm_delete'); + print '
'; + } + + /* + * Confirmation de la validation + */ + if ($_GET['action'] == 'valid') + { + // on vérifie si la facture est en numérotation provisoire + $facref = substr($fac->ref, 1, 4); + if ($facref == 'PROV') + { + $numfa = $fac->getNextNumRef($soc); + } + else + { + $numfa = $fac->ref; + } + + $text=$langs->trans('ConfirmValidateBill',$numfa); + if ($conf->notification->enabled) + { + require_once(DOL_DOCUMENT_ROOT ."/notify.class.php"); + $notify=new Notify($db); + $text.='
'; + $text.=$notify->confirmMessage(2,$fac->socid); + } + + $html->form_confirm($_SERVER["PHP_SELF"].'?facid='.$fac->id,$langs->trans('ValidateBill'),$text,'confirm_valid'); + print '
'; + } + + /* + * Confirmation du classement payé + */ + if ($_GET['action'] == 'payed' && $resteapayer <= 0) + { + $html->form_confirm($_SERVER["PHP_SELF"].'?facid='.$fac->id,$langs->trans('ClassifyPayed'),$langs->trans('ConfirmClassifyPayedBill',$fac->ref),'confirm_payed'); + print '
'; + } + if ($_GET['action'] == 'payed' && $resteapayer > 0) + { + // Code + $i=0; + $close[$i]['code']='discount_vat';$i++; + $close[$i]['code']='badcustomer';$i++; + //$close[$i]['code']='product_returned';$i++; + //$close[$i]['code']='abandon';$i++; + // Help + $i=0; + $close[$i]['label']=$langs->trans("HelpEscompte").'

'.$langs->trans("ConfirmClassifyPayedPartiallyReasonDiscountVatDesc");$i++; + $close[$i]['label']=$langs->trans("ConfirmClassifyPayedPartiallyReasonBadCustomerDesc");$i++; + //$close[$i]['label']=$langs->trans("ConfirmClassifyPayedPartiallyReasonProductReturned");$i++; + //$close[$i]['label']=$langs->trans("ConfirmClassifyPayedPartiallyReasonOtherDesc");$i++; + // Texte + $i=0; + $close[$i]['reason']=$html->textwithhelp($langs->transnoentities("ConfirmClassifyPayedPartiallyReasonDiscountVat",$resteapayer,$langs->trans("Currency".$conf->monnaie)),$close[$i]['label'],1);$i++; + $close[$i]['reason']=$html->textwithhelp($langs->transnoentities("ConfirmClassifyPayedPartiallyReasonBadCustomer",$resteapayer,$langs->trans("Currency".$conf->monnaie)),$close[$i]['label'],1);$i++; + //$close[$i]['reason']=$html->textwithhelp($langs->transnoentities("ConfirmClassifyPayedPartiallyReasonProductReturned",$resteapayer,$langs->trans("Currency".$conf->monnaie)),$close[$i]['label'],1);$i++; + //$close[$i]['reason']=$html->textwithhelp($langs->transnoentities("ConfirmClassifyPayedPartiallyReasonOther",$resteapayer,$langs->trans("Currency".$conf->monnaie)),$close[$i]['label'],1);$i++; + // arrayreasons[code]=reason + foreach($close as $key => $val) + { + $arrayreasons[$close[$key]['code']]=$close[$key]['reason']; + } + + // Crée un tableau formulaire + $formquestion=array( + 'text' => $langs->trans("ConfirmClassifyPayedPartiallyQuestion"), + array('type' => 'radio', 'name' => 'close_code', 'label' => $langs->trans("Reason"), 'values' => $arrayreasons), + array('type' => 'text', 'name' => 'close_note', 'label' => $langs->trans("Comment"), 'value' => '', 'size' => '100') + ); + // Paiement incomplet. On demande si motif = escompte ou autre + $html->form_confirm($_SERVER["PHP_SELF"].'?facid='.$fac->id,$langs->trans('ClassifyPayed'),$langs->trans('ConfirmClassifyPayedPartially',$fac->ref),'confirm_payed_partially',$formquestion); + print '
'; + } + + /* + * Confirmation du classement abandonne + */ + if ($_GET['action'] == 'canceled') + { + // S'il y a une facture de remplacement pas encore validée (etat brouillon), + // on ne permet pas de classer abandonner la facture. + if ($facidnext) + { + $facturereplacement=new Facture($db); + $facturereplacement->fetch($facidnext); + $statusreplacement=$facturereplacement->statut; + } + if ($facidnext && $statusreplacement == 0) + { + print '
'.$langs->trans("ErrorCantCancelIfReplacementInvoiceNotValidated").'
'; + } + else + { + // Code + $close[1]['code']='badcustomer'; + $close[2]['code']='abandon'; + // Help + $close[1]['label']=$langs->trans("ConfirmClassifyPayedPartiallyReasonBadCustomerDesc"); + $close[2]['label']=$langs->trans("ConfirmClassifyAbandonReasonOtherDesc"); + // Texte + $close[1]['reason']=$html->textwithhelp($langs->transnoentities("ConfirmClassifyPayedPartiallyReasonBadCustomer",$fac->ref),$close[1]['label'],1); + $close[2]['reason']=$html->textwithhelp($langs->transnoentities("ConfirmClassifyAbandonReasonOther"),$close[2]['label'],1); + // arrayreasons + $arrayreasons[$close[1]['code']]=$close[1]['reason']; + $arrayreasons[$close[2]['code']]=$close[2]['reason']; + + // Crée un tableau formulaire + $formquestion=array( + 'text' => $langs->trans("ConfirmCancelBillQuestion"), + array('type' => 'radio', 'name' => 'close_code', 'label' => $langs->trans("Reason"), 'values' => $arrayreasons), + array('type' => 'text', 'name' => 'close_note', 'label' => $langs->trans("Comment"), 'value' => '', 'size' => '100') + ); + + $html->form_confirm($_SERVER['PHP_SELF'].'?facid='.$fac->id,$langs->trans('CancelBill'),$langs->trans('ConfirmCancelBill',$fac->ref),'confirm_canceled',$formquestion); + print '
'; + } + } + + /* + * Confirmation de la suppression d'une ligne produit + */ + if ($_GET['action'] == 'delete_product_line' && $conf->global->PRODUIT_CONFIRM_DELETE_LINE) + { + $html->form_confirm($_SERVER["PHP_SELF"].'?facid='.$fac->id.'&rowid='.$_GET["rowid"], $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteproductline'); + print '
'; + } + + /* + * Facture + */ + + print ''; + + // Reference + print ''; + print ''; + + // Ref client + /* + \todo + L'info "Reference commande client" est une carac de la commande et non de la facture. + Elle devrait donc etre stockée sur l'objet commande lié à la facture et non sur la facture. + Pour ceux qui utilisent ainsi, positionner la constante FAC_USE_CUSTOMER_ORDER_REF à 1. + */ + if ($conf->global->FAC_USE_CUSTOMER_ORDER_REF) + { + print ''; + print ''; + } + + // Tiers Société + print ''; + print ''; + print ''; + + // Type + print ''; + + // Ligne info remises tiers + print ''; + + // Dates + print ''; + print ''; + + $nbrows=8; + if ($conf->global->FAC_USE_CUSTOMER_ORDER_REF) $nbrows++; + if ($conf->projet->enabled) $nbrows++; + + print ''; + + // Date limite reglement + print ''; + print ''; + print ''; + + // Conditions de règlement + print ''; + + // Mode de reglement + print ''; + + // Lit lignes de facture pour déterminer montant + // On s'en sert pas mais ca sert pour debuggage + /* + $sql = 'SELECT l.price as price, l.qty, l.rowid, l.tva_taux,'; + $sql .= ' l.remise_percent, l.subprice'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet as l '; + $sql .= ' WHERE l.fk_facture = '.$fac->id; + $resql = $db->query($sql); + if ($resql) + { + $num_lignes = $db->num_rows($resql); + $i=0; + $total_lignes_ht=0; + $total_lignes_vat=0; + $total_lignes_ttc=0; + while ($i < $num_lignes) + { + $obj=$db->fetch_object($resql); + $ligne_ht=($obj->price*$obj->qty); + $ligne_vat=($ligne_ht*$obj->tva_taux/100); + $ligne_ttc=($ligne_ht+$ligne_vat); + $total_lignes_ht+=$ligne_ht; + $total_lignes_vat+=$ligne_vat; + $total_lignes_ttc+=$ligne_ttc; + $i++; + } + } + */ + + // Montants + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + // Statut + print ''; + print ''; + + // Projet + if ($conf->projet->enabled) + { + $langs->load('projects'); + print ''; + print ''; + print ''; + } + + print '
'.$langs->trans('Ref').''.$fac->ref; + $discount=new DiscountAbsolute($db); + $result=$discount->fetch(0,$fac->id); + if ($result > 0) + { + print ' ('.$langs->trans("CreditNoteConvertedIntoDiscount",$discount->getNomUrl(1,'discount')).')'; + } + if ($result < 0) + { + dolibarr_print_error('',$discount->error); + } + print '
'; + print ''; + if ($_GET['action'] != 'RefCustomerOrder' && $fac->brouillon) print ''; + print '
'; + print $langs->trans('RefCustomerOrder').''; + print ''.img_edit($langs->trans('Modify')).'
'; + print '
'; + if ($user->rights->facture->creer && $_GET['action'] == 'RefCustomerOrder') + { + print '
'; + print ''; + print ''; + print ' '; + print '
'; + } + else + { + print $fac->ref_client; + } + print '
'.$langs->trans('Company').''.$soc->getNomUrl(1,'compta').'
'.$langs->trans('Type').''; + print $fac->getLibType(); + if ($fac->type == 1) + { + $facreplaced=new Facture($db); + $facreplaced->fetch($fac->fk_facture_source); + print ' ('.$langs->transnoentities("ReplaceInvoice",$facreplaced->getNomUrl(1)).')'; + } + if ($fac->type == 2) + { + $facreplaced=new Facture($db); + $facreplaced->fetch($fac->fk_facture_source); + print ' ('.$langs->transnoentities("CorrectInvoice",$facreplaced->getNomUrl(1)).')'; + } + $facidavoir=$fac->getListIdAvoirFromInvoice(); + if (sizeof($facidavoir) > 0) + { + print ' ('.$langs->transnoentities("InvoiceHasAvoir"); + $i=0; + foreach($facidavoir as $id) + { + if ($i==0) print ' '; + else print ','; + $facavoir=new Facture($db); + $facavoir->fetch($id); + print $facavoir->getNomUrl(1); + } + print ')'; + } + if ($facidnext > 0) + { + $facthatreplace=new Facture($db); + $facthatreplace->fetch($facidnext); + print ' ('.$langs->transnoentities("ReplacedByInvoice",$facthatreplace->getNomUrl(1)).')'; + } + print '
'.$langs->trans('Discounts').''; + if ($soc->remise_client) print $langs->trans("CompanyHasRelativeDiscount",$soc->remise_client); + else print $langs->trans("CompanyHasNoRelativeDiscount"); + print '. '; + if ($absolute_discount > 0) + { + if ($fac->statut > 0 || $fac->type == 2) + { + print $langs->trans("CompanyHasAbsoluteDiscount",price($absolute_discount),$langs->transnoentities("Currency".$conf->monnaie)).'. '; + } + else + { + // Remise dispo de type non avoir + $filter='fk_facture_source IS NULL'; + print '
'; + $html->form_remise_dispo($_SERVER["PHP_SELF"].'?facid='.$fac->id,0,'remise_id',$soc->id,$absolute_discount,$filter,$resteapayer); + } + } + if ($absolute_creditnote > 0) + { + // If validated, we show link "add credit note to payment" + if ($fac->statut != 1 || $fac->type == 2) + { + print $langs->trans("CompanyHasCreditNote",price($absolute_creditnote),$langs->transnoentities("Currency".$conf->monnaie)).'. '; + } + else + { + // Remise dispo de type avoir + $filter='fk_facture_source IS NOT NULL'; + if (! $absolute_discount) print '
'; + $html->form_remise_dispo($_SERVER["PHP_SELF"].'?facid='.$fac->id,0,'remise_id_for_payment',$soc->id,$absolute_creditnote,$filter,$resteapayer); + } + } + if (! $absolute_discount && ! $absolute_creditnote) print $langs->trans("CompanyHasNoAbsoluteDiscount").'.'; + print '
'.$langs->trans('Date').''.dolibarr_print_date($fac->date,'daytext').''; + + /* + * Liste des paiements + */ + $sql = 'SELECT '.$db->pdate('datep').' as dp, pf.amount,'; + $sql.= ' c.libelle as paiement_type, p.num_paiement, p.rowid'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement as p, '.MAIN_DB_PREFIX.'c_paiement as c, '.MAIN_DB_PREFIX.'paiement_facture as pf'; + $sql.= ' WHERE pf.fk_facture = '.$fac->id.' AND p.fk_paiement = c.id AND pf.fk_paiement = p.rowid'; + $sql.= ' ORDER BY dp, tms'; + + $result = $db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + $i = 0; + print ''; + + // Liste des paiements ou remboursements + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + if ($fac->type != 2) + { + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + + // Already payed + print ''; + + // Billed + print ''; + $resteapayeraffiche=$resteapayer; + + // Loop on each credit note applied + $sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,"; + $sql.= " re.description, re.fk_facture_source, re.fk_facture_source"; + $sql.= " FROM ".MAIN_DB_PREFIX ."societe_remise_except as re"; + $sql.= " WHERE fk_facture = ".$fac->id; + $resql=$db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + $i = 0; + $invoice=new Facture($db); + while ($i < $num) + { + $obj = $db->fetch_object($resql); + $invoice->fetch($obj->fk_facture_source); + print ''; + print ''; + print ''; + $i++; + } + } + else + { + dolibarr_print_error($db); + } + + // Payé partiellement 'escompte' + if (($fac->statut == 2 || $fac->statut == 3) && $fac->close_code == 'discount_vat') + { + print ''; + $resteapayeraffiche=0; + } + // Payé partiellement ou Abandon 'badcustomer' + if (($fac->statut == 2 || $fac->statut == 3) && $fac->close_code == 'badcustomer') + { + print ''; + //$resteapayeraffiche=0; + } + // Payé partiellement ou Abandon 'product_returned' + if (($fac->statut == 2 || $fac->statut == 3) && $fac->close_code == 'product_returned') + { + print ''; + $resteapayeraffiche=0; + } + // Payé partiellement ou Abandon 'abandon' + if (($fac->statut == 2 || $fac->statut == 3) && $fac->close_code == 'abandon') + { + print ''; + $resteapayeraffiche=0; + } + print ''; + print ''; + print ''; + } + else + { + // Solde avoir + print ''; + print ''; + } + print '
'.($fac->type == 2 ? $langs->trans("PaymentsBack") : $langs->trans('Payments')).''.$langs->trans('Type').''.$langs->trans('Amount').' 
'; + print ''.img_object($langs->trans('ShowPayment'),'payment').' '; + print dolibarr_print_date($objp->dp,'day').''.$objp->paiement_type.' '.$objp->num_paiement.''.price($objp->amount).' 
'.$langs->trans('AlreadyPayed').' :'.price($totalpaye).' 
'.$langs->trans("Billed").' :'.price($fac->total_ttc).' 
'.$langs->trans("CreditNote").' '; + print $invoice->getNomUrl(0); + print ' :'.price($obj->amount_ttc).''; + print 'rowid.'">'.img_delete().''; + print '
'; + print $html->textwithhelp($langs->trans("Escompte").':',$langs->trans("HelpEscompte"),-1); + print ''.price($fac->total_ttc - $totalpaye).' 
'; + print $html->textwithhelp($langs->trans("Abandoned").':',$langs->trans("HelpAbandonBadCustomer"),-1); + print ''.price($fac->total_ttc - $totalpaye).' 
'; + print $html->textwithhelp($langs->trans("ProductReturned").':',$langs->trans("HelpAbandonProductReturned"),-1); + print ''.price($fac->total_ttc - $totalpaye).' 
'; + $text=$langs->trans("HelpAbandonOther"); + if ($fac->close_note) $text.='

'.$langs->trans("Reason").':'.$fac->close_note; + print $html->textwithhelp($langs->trans("Abandoned").':',$text,-1); + print '
'.price($fac->total_ttc - $totalpaye).' 
'; + if ($resteapayeraffiche >= 0) print $langs->trans('RemainderToPay'); + else print $langs->trans('ExcessReceived'); + print ' :'.price($resteapayeraffiche).' 
'.$langs->trans('TotalTTCToYourCredit').' :'.price(abs($fac->total_ttc)).' 
'; + $db->free($result); + } + else + { + dolibarr_print_error($db); + } + + print '
'.$langs->trans('DateMaxPayment').''; + if ($fac->type != 2) + { + print dolibarr_print_date($fac->date_lim_reglement,'daytext'); + if ($fac->date_lim_reglement < (time() - $conf->facture->client->warning_delay) && ! $fac->paye && $fac->statut == 1 && ! $fac->am) print img_warning($langs->trans('Late')); + } + else + { + print ' '; + } + print '
'; + print ''; + if ($fac->type != 2 && $_GET['action'] != 'editconditions' && $fac->brouillon && $user->rights->facture->creer) print ''; + print '
'; + print $langs->trans('PaymentConditionsShort'); + print 'id.'">'.img_edit($langs->trans('SetConditions'),1).'
'; + print '
'; + if ($fac->type != 2) + { + if ($_GET['action'] == 'editconditions') + { + $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->cond_reglement_id,'cond_reglement_id'); + } + else + { + $html->form_conditions_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->cond_reglement_id,'none'); + } + } + else + { + print ' '; + } + print '
'; + print ''; + if ($_GET['action'] != 'editmode' && $fac->brouillon && $user->rights->facture->creer) print ''; + print '
'; + print $langs->trans('PaymentMode'); + print 'id.'">'.img_edit($langs->trans('SetMode'),1).'
'; + print '
'; + if ($_GET['action'] == 'editmode') + { + $html->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->mode_reglement_id,'mode_reglement_id'); + } + else + { + $html->form_modes_reglement($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->mode_reglement_id,'none'); + } + print '
'.$langs->trans('AmountHT').''.price($fac->total_ht).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('AmountVAT').''.price($fac->total_tva).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('AmountTTC').''.price($fac->total_ttc).''.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('Status').''.($fac->getLibStatut(4,$totalpaye)).'
'; + + print ''; + if ($_GET['action'] != 'classer') + { + print ''; + } + print '
'; + print $langs->trans('Project'); + print 'id.'">'; + print img_edit($langs->trans('SetProject'),1); + print '
'; + + print '
'; + if ($_GET['action'] == 'classer') + { + $html->form_project($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->socid,$fac->projetid,'projetid'); + } + else + { + $html->form_project($_SERVER['PHP_SELF'].'?facid='.$fac->id,$fac->socid,$fac->projetid,'none'); + } + print '

'; + + + /* + * Lignes de factures + */ + $sql = 'SELECT l.fk_product, l.product_type, l.description, l.qty, l.rowid, l.tva_taux,'; + $sql.= ' l.fk_remise_except,'; + $sql.= ' l.remise_percent, l.subprice, l.info_bits,'; + $sql.= ' l.total_ht, l.total_tva, l.total_ttc,'; + $sql.= ' '.$db->pdate('l.date_start').' as date_start,'; + $sql.= ' '.$db->pdate('l.date_end').' as date_end,'; + $sql.= ' p.ref, p.fk_product_type, p.label as product,'; + $sql.= ' p.description as product_desc'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet as l'; + $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON l.fk_product=p.rowid'; + $sql.= ' WHERE l.fk_facture = '.$fac->id; + $sql.= ' ORDER BY l.rang ASC, l.rowid'; + + $resql = $db->query($sql); + if ($resql) + { + $num_lignes = $db->num_rows($resql); + $i = 0; $total = 0; + + print ''; + + if ($num_lignes) + { + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print "\n"; + + } + $var=true; + while ($i < $num_lignes) + { + $objp = $db->fetch_object($resql); + $var=!$var; + + // Ligne en mode visu + if ($_GET['action'] != 'editline' || $_GET['rowid'] != $objp->rowid) + { + print ''; + if ($objp->fk_product > 0) + { + print ''; + } + else + { + print '\n"; + } + print ''; + print '\n"; + print ''; + if ($objp->remise_percent > 0) + { + print '\n"; + } + else + { + print ''; + } + print '\n"; + + // Icone d'edition et suppression + if ($fac->statut == 0 && $user->rights->facture->creer) + { + print ''; + print ''; + if ($num_lignes > 1) + { + print ''; + } + } + else + { + print ''; + } + print ''; + } + + // Ligne en mode update + if ($_GET['action'] == 'editline' && $user->rights->facture->creer && $_GET['rowid'] == $objp->rowid) + { + print 'rowid.'" method="post">'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print '' . "\n"; + if ($conf->service->enabled) + { + print ''; + print ''; + print ''; + } + print "\n"; + } + + $total = $total + ($objp->qty * $objp->price); + $i++; + } + + $db->free($resql); + } + else + { + dolibarr_print_error($db); + } + + /* + * Ajouter une ligne + */ + if ($fac->statut == 0 && $user->rights->facture->creer && $_GET['action'] <> 'valid' && $_GET['action'] <> 'editline') + { + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print "\n"; + + // Ajout produit produits/services personalisés + print ''; + print ''; + print ''; + + $var=true; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + if ($conf->service->enabled) + { + print ''; + print ''; + print ''; + } + print ''; + + // Ajout de produits/services prédéfinis + if ($conf->produit->enabled) + { + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + + $var=! $var; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + if ($conf->service->enabled) + { + print ''; + print ''; + print ''; + } + print ''; + } + + } + print "
'.$langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').''.$langs->trans('TotalHT').' 
'; + print ''; // ancre pour retourner sur la ligne + + // Affiche ligne produit + $text = ''; + if ($objp->fk_product_type==1) $text.= img_object($langs->trans('ShowService'),'service'); + else $text.= img_object($langs->trans('ShowProduct'),'product'); + $text.= ' '.$objp->ref.''; + $text.= ' - '.$objp->product; + $description=($conf->global->PRODUIT_DESC_IN_FORM?'':dol_htmlentitiesbr($objp->description)); + //print $description; + print $html->textwithtooltip($text,$description,3,'','',$i); + print_date_range($objp->date_start,$objp->date_end); + if ($conf->global->PRODUIT_DESC_IN_FORM) + { + print ($objp->description && $objp->description!=$objp->product)?'
'.dol_htmlentitiesbr($objp->description):''; + } + + print '
'; + print ''; // ancre pour retourner sur la ligne + + if (($objp->info_bits & 2) == 2) + { + print ''; + print img_object($langs->trans("ShowReduc"),'reduc').' '.$langs->trans("Discount"); + print ''; + if ($objp->description) + { + if ($objp->description == '(CREDIT_NOTE)') + { + $discount=new DiscountAbsolute($db); + $discount->fetch($objp->fk_remise_except); + print ' - '.$langs->transnoentities("DiscountFromCreditNote",$discount->getNomUrl(0)); + } + else + { + print ' - '.nl2br($objp->description); + } + } + } + else + { + if ($objp->product_type==1) $text = img_object($langs->trans('Service'),'service'); + else $text = img_object($langs->trans('Product'),'product'); + print $text.' '.nl2br($objp->description); + print_date_range($objp->date_start,$objp->date_end); + } + print "'.vatrate($objp->tva_taux,'%',$objp->info_bits).''.price($objp->subprice)."'; + if (($objp->info_bits & 2) != 2) + { + print $objp->qty; + } + else print ' '; + print ''.dolibarr_print_reduction($objp->remise_percent)." '.price($objp->total_ht)."'; + if (($objp->info_bits & 2) == 2) + { + // Ligne remise prédéfinie, on permet pas modif + } + else + { + print 'rowid.'#'.$objp->rowid.'">'; + print img_edit(); + print ''; + } + print ''; + if ($conf->global->PRODUIT_CONFIRM_DELETE_LINE) + { + print 'rowid.'">'; + } + else + { + print 'rowid.'">'; + } + print img_delete(); + print ''; + if ($i > 0) + { + print 'rowid.'">'; + print img_up(); + print ''; + } + if ($i < $num_lignes-1) + { + print 'rowid.'">'; + print img_down(); + print ''; + } + print ' 
'; + print ''; // ancre pour retourner sur la ligne + if ($objp->fk_product > 0) + { + print ''; + if ($objp->fk_product_type==1) print img_object($langs->trans('ShowService'),'service'); + else print img_object($langs->trans('ShowProduct'),'product'); + print ' '.$objp->ref.''; + print ' - '.nl2br($objp->product); + print '
'; + } + // éditeur wysiwyg + if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) + { + require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); + $doleditor=new DolEditor('desc',$objp->description,164,'dolibarr_details'); + $doleditor->Create(); + } + else + { + print ''; + } + print '
'; + print $html->select_tva('tva_tx',$objp->tva_taux,$mysoc,$soc,'',$objp->info_bits); + print ''; + if (($objp->info_bits & 2) != 2) + { + print ''; + } + else print ' '; + print ''; + if (($objp->info_bits & 2) != 2) + { + print '%'; + } + else print ' '; + print ''; + print '
'.$langs->trans('ServiceLimitedDuration').' '.$langs->trans('From').' '; + print $html->select_date($objp->date_start,'date_start',0,0,$objp->date_start?0:1,"updateligne"); + print ' '.$langs->trans('to').' '; + print $html->select_date($objp->date_end,'date_end',0,0,$objp->date_end?0:1,"updateligne"); + print '
'; + print ''; // ancre + print $langs->trans('Description').''.$langs->trans('VAT').''.$langs->trans('PriceUHT').''.$langs->trans('Qty').''.$langs->trans('ReductionShort').' 
'; + // éditeur wysiwyg + if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) + { + require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); + $doleditor=new DolEditor('dp_desc','',100,'dolibarr_details'); + $doleditor->Create(); + } + else + { + print ''; + } + print ''; + + $html->select_tva('tva_tx',$conf->defaulttx,$mysoc,$soc); + print '%
'.$langs->trans('ServiceLimitedDuration').' '.$langs->trans('From').' '; + print $html->select_date('','date_start',0,0,1,"addligne"); + print ' '.$langs->trans('to').' '; + print $html->select_date('','date_end',0,0,1,"addligne"); + print '
'; + if ($conf->service->enabled) + { + print $langs->trans('RecordedProductsAndServices'); + } + else + { + print $langs->trans('RecordedProducts'); + } + print ''.$langs->trans('Qty').''.$langs->trans('ReductionShort').' 
'; + // multiprix + if($conf->global->PRODUIT_MULTIPRICES == 1) + { + $html->select_produits('','idprod','',$conf->produit->limit_size,$soc->price_level); + } + else + { + $html->select_produits('','idprod','',$conf->produit->limit_size); + } + + if (! $conf->global->PRODUIT_USE_SEARCH_TO_SELECT) print '
'; + + // éditeur wysiwyg + if ($conf->fckeditor->enabled && $conf->global->FCKEDITOR_ENABLE_DETAILS) + { + require_once(DOL_DOCUMENT_ROOT."/lib/doleditor.class.php"); + $doleditor=new DolEditor('np_desc','',100,'dolibarr_details'); + $doleditor->Create(); + } + else + { + print ''; + } + + print '
%
'.$langs->trans('ServiceLimitedDuration').' '.$langs->trans('From').' '; + print $html->select_date('','date_start_predef',0,0,1,"addligne_predef"); + print ' '.$langs->trans('to').' '; + print $html->select_date('','date_end_predef',0,0,1,"addligne_predef"); + print '
\n"; + + print "\n"; + + + /* + * Boutons actions + */ + if ($_GET['action'] != 'prerelance' && $_GET['action'] != 'presend') + { + if ($user->societe_id == 0 && $_GET['action'] <> 'valid' && $_GET['action'] <> 'editline') + { + print '
'; + + // Editer une facture déjà validée, sans paiement effectué et pas exporté en compta + if ($fac->statut == 1) + { + // On vérifie si les lignes de factures ont été exportées en compta et/ou ventilées + $ventilExportCompta = $fac->getVentilExportCompta(); + + if ($conf->global->FACTURE_ENABLE_EDITDELETE && $user->rights->facture->modifier + && ($resteapayer == $fac->total_ttc && $fac->paye == 0 && $ventilExportCompta == 0)) + { + if (! $facidnext) + { + print ''.$langs->trans('Modify').''; + } + else + { + print ''.$langs->trans('Modify').''; + } + } + } + + // Reopen a classified invoice + if ($fac->statut == 3 && // A abandonned invoice + $fac->getIdReplacingInvoice() == 0 && // Not replaced by another invoice + ($fac->close_code == 'badcustomer' || $fac->close_code == 'abandon')) + { + if (! $facidnext) + { + print ''.$langs->trans('ReOpen').''; + } + else + { + print ''.$langs->trans('ReOpen').''; + } + } + + // Récurrente + if (! $conf->global->FACTURE_DISABLE_RECUR && $fac->type == 0) + { + if (! $facidnext) + { + print ''.$langs->trans("ChangeIntoRepeatableInvoice").''; + } + } + + // Valider + if ($fac->statut == 0 && $num_lignes > 0 && (($fac->type < 2 && $fac->total_ttc >= 0) || ($fac->type == 2 && $fac->total_ttc <= 0))) + { + if ($user->rights->facture->valider) + { + print 'use_javascript_ajax && $conf->global->MAIN_CONFIRM_AJAX) + { + // on verifie si la commande est en numerotation provisoire + $ref = substr($fac->ref, 1, 4); + if ($ref == 'PROV') + { + $num = $fac->getNextNumRef($soc); + } + else + { + $num = $fac->ref; + } + $url = $_SERVER["PHP_SELF"].'?facid='.$fac->id.'&action=confirm_valid&confirm=yes'; + print 'href="#" onClick="dialogConfirm(\''.$url.'\',\''.dol_escape_js($langs->trans('ConfirmValidateBill',$num)).'\',\''.$langs->trans("Yes").'\',\''.$langs->trans("No").'\',\'validate\')"'; + } + else + { + print 'href="'.$_SERVER["PHP_SELF"].'?facid='.$fac->id.'&action=valid"'; + } + print '>'.$langs->trans('Validate').''; + } + } + + // Envoyer + if (($fac->statut == 1 || $fac->statut == 2) && $user->rights->facture->envoyer) + { + if ($facidnext) + { + print ''.$langs->trans('SendByMail').''; + } + else + { + print ''.$langs->trans('SendByMail').''; + } + } + + // Envoyer une relance + if (($fac->statut == 1 || $fac->statut == 2) && $resteapayer > 0 && $user->rights->facture->envoyer) + { + if ($facidnext) + { + print ''.$langs->trans('SendRemindByMail').''; + } + else + { + print ''.$langs->trans('SendRemindByMail').''; + } + } + + // Emettre paiement + if ($fac->type != 2 && $fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->paiement) + { + if ($facidnext) + { + print ''.$langs->trans('DoPayment').''; + } + else + { + if ($resteapayer == 0) + { + print ''.$langs->trans('DoPayment').''; + } + else + { + print ''.$langs->trans('DoPayment').''; + } + } + } + + // Emettre remboursement ou Convertir en reduc + if ($fac->type == 2) + { + if ($fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->paiement) + { + print ''.$langs->trans('DoPaymentBack').''; + } + + if ($fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->creer && $fac->getSommePaiement() == 0) + { + print ''.$langs->trans('ConvertToReduc').''; + } + } + + // Classer 'payé' + if ($fac->statut == 1 && $fac->paye == 0 && $user->rights->facture->paiement && + (($fac->type != 2 && $resteapayer <= 0) || ($fac->type == 2 && $resteapayer >= 0)) ) + { + print ''.$langs->trans('ClassifyPayed').''; + } + + // Classer 'fermée' (possible si validée et pas encore classée payée) + if ($fac->statut == 1 && $fac->paye == 0 && $resteapayer > 0 + && $user->rights->facture->paiement) + { + if ($totalpaye > 0 || $totalavoir > 0) + { + // If one payment or one credit note was linked to this invoice + print ''.$langs->trans('ClassifyPayedPartially').''; + } + else + { + if ($facidnext) + { + print ''.$langs->trans('ClassifyCanceled').''; + } + else + { + print ''.$langs->trans('ClassifyCanceled').''; + } + } + } + + // Supprimer + if ($fac->is_erasable() && $user->rights->facture->supprimer && $_GET['action'] != 'delete') + { + if ($facidnext) + { + print ''.$langs->trans('Delete').''; + } + else + { + print ''.$langs->trans('Delete').''; + } + } + + print '
'; + } + } + + + if ($_GET['action'] != 'prerelance' && $_GET['action'] != 'presend') + { + print '
'; + print ''; // ancre + + /* + * Documents générés + */ + $filename=sanitize_string($fac->ref); + $filedir=$conf->facture->dir_output . '/' . sanitize_string($fac->ref); + $urlsource=$_SERVER['PHP_SELF'].'?facid='.$fac->id; + $genallowed=$user->rights->facture->creer; + $delallowed=$user->rights->facture->supprimer; + + $var=true; + + print '
'; + $somethingshown=$formfile->show_documents('facture',$filename,$filedir,$urlsource,$genallowed,$delallowed,$fac->modelpdf); + + /* + * Propales rattachées + */ + $sql = 'SELECT '.$db->pdate('p.datep').' as dp, p.total_ht, p.ref, p.ref_client, p.rowid as propalid'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'propal as p'; + $sql .= ", ".MAIN_DB_PREFIX."fa_pr as fp"; + $sql .= " WHERE fp.fk_propal = p.rowid AND fp.fk_facture = ".$fac->id; + + dolibarr_syslog("facture.php: sql=".$sql); + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + if ($num) + { + $i = 0; $total = 0; + if ($somethingshown) print '
'; + $somethingshown=1; + print_titre($langs->trans('RelatedCommercialProposals')); + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $total = $total + $objp->total_ht; + $i++; + } + print ''; + print ''; + print ''; + print ''; + print ''; + print '
'.$langs->trans('Ref').''.$langs->trans('RefCustomer').''.$langs->trans('Date').''.$langs->trans('AmountHT').'
'.img_object($langs->trans('ShowPropal'),'propal').' '.$objp->ref.''.$objp->ref_client.''.dolibarr_print_date($objp->dp,'day').''.price($objp->total_ht).'
'.$langs->trans('TotalHT').'  '.price($total).'
'; + } + } + else + { + dolibarr_print_error($db); + } + + /* + * Commandes rattachées + */ + if($conf->commande->enabled) + { + $sql = 'SELECT '.$db->pdate('c.date_commande').' as date_commande, c.total_ht, c.ref, c.ref_client, c.rowid as id'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'commande as c, '.MAIN_DB_PREFIX.'co_fa as co_fa WHERE co_fa.fk_commande = c.rowid AND co_fa.fk_facture = '.$fac->id; + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + if ($num) + { + $langs->load("orders"); + + $i = 0; $total = 0; + if ($somethingshown) print '
'; + $somethingshown=1; + print_titre($langs->trans('RelatedOrders')); + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $var=true; + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print '\n"; + print ''; + print ''; + print ''; + print "\n"; + $total = $total + $objp->total_ht; + $i++; + } + print ''; + print ''; + print ''; + print ''; + print ''; + print '
'.$langs->trans('Ref').''.$langs->trans('RefCustomerOrderShort').''.$langs->trans('Date').''.$langs->trans('AmountHT').'
'; + print ''.img_object($langs->trans('ShowOrder'), 'order').' '.$objp->ref."'.$objp->ref_client.''.dolibarr_print_date($objp->date_commande,'day').''.price($objp->total_ht).'
'.$langs->trans('TotalHT').'  '.price($total).'
'; + } + } + else + { + dolibarr_print_error($db); + } + } + + print '
'; + + print '
'; + + // List of actions on element + include_once(DOL_DOCUMENT_ROOT.'/html.formactions.class.php'); + $formactions=new FormActions($db); + $somethingshown=$formactions->showactions($fac,'invoice',$socid); + + print '
'; + } + + /* + * Affiche formulaire mail + */ + if ($_GET['action'] == 'presend') + { + $ref = sanitize_string($fac->ref); + $file = $conf->facture->dir_output . '/' . $ref . '/' . $ref . '.pdf'; + + // Construit PDF si non existant + if (! is_readable($file)) + { + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + $result=facture_pdf_create($db, $fac->id, '', $_REQUEST['model'], $outputlangs); + if ($result <= 0) + { + dolibarr_print_error($db,$result); + exit; + } + } + + print '
'; + print_titre($langs->trans('SendBillByMail')); + + $liste[0]=' '; + foreach ($soc->thirdparty_and_contact_email_array() as $key=>$value) + { + $liste[$key]=$value; + } + + // Créé l'objet formulaire mail + include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); + $formmail = new FormMail($db); + $formmail->fromtype = 'user'; + $formmail->fromid = $user->id; + $formmail->fromname = $user->fullname; + $formmail->frommail = $user->email; + $formmail->withfrom=1; + $formmail->withto=$liste; + $formmail->withtocc=1; + $formmail->withtoccc=$conf->global->FACTURE_EMAIL_USECCC; + $formmail->withtopic=$langs->trans('SendBillRef','__FACREF__'); + $formmail->withfile=2; + $formmail->withbody=1; + $formmail->withdeliveryreceipt=1; + $formmail->withcancel=1; + // Tableau des substitutions + $formmail->substit['__FACREF__']=$fac->ref; + // Tableau des paramètres complémentaires du post + $formmail->param['action']='send'; + $formmail->param['models']='facture_send'; + $formmail->param['facid']=$fac->id; + $formmail->param['returnurl']=DOL_URL_ROOT.'/compta/facture.php?facid='.$fac->id; + + // Init list of files + if (! empty($_REQUEST["mode"]) && $_REQUEST["mode"]=='init') + { + $formmail->clear_attached_files(); + $formmail->add_attached_files($file,$ref.'.pdf','application/pdf'); + } + + $formmail->show_form(); + + print '
'; + } + + if ($_GET['action'] == 'prerelance') + { + $ref = sanitize_string($fac->ref); + $file = $conf->facture->dir_output . '/' . $ref . '/' . $ref . '.pdf'; + + // Construit PDF si non existant + if (! is_readable($file)) + { + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + $result=facture_pdf_create($db, $fac->id, '', $_REQUEST['model'], $outputlangs); + if ($result <= 0) + { + dolibarr_print_error($db,$result); + exit; + } + } + + print '
'; + print_titre($langs->trans('SendReminderBillByMail')); + + $liste[0]=' '; + foreach ($soc->thirdparty_and_contact_email_array() as $key=>$value) + { + $liste[$key]=$value; + } + + // Créé l'objet formulaire mail + include_once(DOL_DOCUMENT_ROOT.'/html.formmail.class.php'); + $formmail = new FormMail($db); + $formmail->fromname = $user->fullname; + $formmail->frommail = $user->email; + $formmail->withfrom=1; + $formmail->withto=$liste; + $formmail->withtocc=1; + $formmail->withtopic=$langs->trans('SendReminderBillRef','__FACREF__'); + $formmail->withfile=2; + $formmail->withbody=1; + $formmail->withdeliveryreceipt=1; + $formmail->withcancel=1; + // Tableau des substitutions + $formmail->substit['__FACREF__']=$fac->ref; + // Tableau des paramètres complémentaires + $formmail->param['action']='relance'; + $formmail->param['models']='facture_relance'; + $formmail->param['facid']=$fac->id; + $formmail->param['returnurl']=DOL_URL_ROOT.'/compta/facture.php?facid='.$fac->id; + + // Init list of files + if (! empty($_REQUEST["mode"]) && $_REQUEST["mode"]=='init') + { + $formmail->clear_attached_files(); + $formmail->add_attached_files($file,$ref.'.pdf','application/pdf'); + } + + $formmail->show_form(); + + print '
'; + } + } + else + { + dolibarr_print_error($db,$fac->error); + } + } + else + { + /*************************************************************************** + * * + * Mode Liste * + * * + ***************************************************************************/ + $page =$_GET['page']; + $sortorder=$_GET['sortorder']; + $sortfield=$_GET['sortfield']; + $month =$_GET['month']; + $year =$_GET['year']; + $limit = $conf->liste_limit; + $offset = $limit * $page ; + + if (! $sortorder) $sortorder='DESC'; + if (! $sortfield) $sortfield='f.datef'; + + $facturestatic=new Facture($db); + + if ($page == -1) $page = 0 ; + + $sql = 'SELECT '; + $sql.= ' f.rowid as facid, f.facnumber, f.type, f.increment, f.total, f.total_ttc,'; + $sql.= ' '.$db->pdate('f.datef').' as df, '.$db->pdate('f.date_lim_reglement').' as datelimite,'; + $sql.= ' f.paye as paye, f.fk_statut,'; + $sql.= ' s.nom, s.rowid as socid'; + if (! $sall) $sql.= ' ,sum(pf.amount) as am'; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", sc.fk_soc, sc.fk_user"; + $sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s'; + if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql.= ', '.MAIN_DB_PREFIX.'facture as f'; + if (! $sall) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiement_facture as pf ON pf.fk_facture = f.rowid'; + if ($sall) $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'facturedet as fd ON fd.fk_facture = f.rowid'; + $sql.= ' WHERE f.fk_soc = s.rowid'; + if (!$user->rights->societe->client->voir && !$socid) //restriction + { + $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; + } + if ($socid) $sql .= ' AND s.rowid = '.$socid; + if ($_GET['filtre']) + { + $filtrearr = split(',', $_GET['filtre']); + foreach ($filtrearr as $fil) + { + $filt = split(':', $fil); + $sql .= ' AND ' . trim($filt[0]) . ' = ' . trim($filt[1]); + } + } + if ($_GET['search_ref']) + { + $sql .= ' AND f.facnumber like \'%'.addslashes(trim($_GET['search_ref'])).'%\''; + } + if ($_GET['search_societe']) + { + $sql .= ' AND s.nom like \'%'.addslashes(trim($_GET['search_societe'])).'%\''; + } + if ($_GET['search_montant_ht']) + { + $sql .= ' AND f.total = \''.addslashes(trim($_GET['search_montant_ht'])).'\''; + } + if ($_GET['search_montant_ttc']) + { + $sql .= ' AND f.total_ttc = \''.addslashes(trim($_GET['search_montant_ttc'])).'\''; + } + if ($month > 0) + { + if ($year > 0) + $sql .= " AND date_format(f.datef, '%Y-%m') = '$year-$month'"; + else + $sql .= " AND date_format(f.datef, '%m') = '$month'"; + } + if ($year > 0) + { + $sql .= ' AND date_format(f.datef, \'%Y\') = '.$year; + } + if ($_POST['sf_ref']) + { + $sql .= ' AND f.facnumber like \'%'.addslashes(trim($_POST['sf_ref'])) . '%\''; + } + if ($sall) + { + $sql .= ' AND (s.nom like \'%'.addslashes($sall).'%\' OR f.facnumber like \'%'.addslashes($sall).'%\' OR f.note like \'%'.addslashes($sall).'%\' OR fd.description like \'%'.addslashes($sall).'%\')'; + } + + $sql .= ' GROUP BY f.rowid'; + + $sql .= ' ORDER BY '; + $listfield=split(',',$sortfield); + foreach ($listfield as $key => $value) + $sql.= $listfield[$key].' '.$sortorder.','; + $sql .= ' f.rowid DESC '; + + $sql .= $db->plimit($limit+1,$offset); + + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + + if ($socid) + { + $soc = new Societe($db); + $soc->fetch($socid); + } + + print_barre_liste($langs->trans('BillsCustomers').' '.($socid?' '.$soc->nom:''),$page,'facture.php','&socid='.$socid,$sortfield,$sortorder,'',$num); + + $i = 0; + print '
'."\n"; + print ''; + print ''; + print_liste_field_titre($langs->trans('Ref'),$_SERVER['PHP_SELF'],'f.facnumber','','&socid='.$socid.'&month='.$month.'&year=' . $year,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('Date'),$_SERVER['PHP_SELF'],'f.datef','','&socid='.$socid.'&month='.$month.'&year=' . $year,'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('Company'),$_SERVER['PHP_SELF'],'s.nom','','&socid='.$socid.'&month='.$month.'&year=' . $year,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('AmountHT'),$_SERVER['PHP_SELF'],'f.total','','&socid='.$socid.'&month='.$month.'&year=' . $year,'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('AmountTTC'),$_SERVER['PHP_SELF'],'f.total_ttc','','&socid='.$socid.'&month='.$month.'&year=' . $year,'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('Received'),$_SERVER['PHP_SELF'],'am','','&socid='.$socid.'&month='.$month.'&year=' . $year,'align="right"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('Status'),$_SERVER['PHP_SELF'],'fk_statut,paye,am','','&socid='.$socid.'&month='.$month.'&year=' . $year,'align="right"',$sortfield,$sortorder); + //print ''; + print ''; + + // Lignes des champs de filtre + + print ''; + print ''; + print ''; + print ''; + print '\n"; + + if ($num > 0) + { + $var=True; + $total=0; + $totalrecu=0; + + while ($i < min($num,$limit)) + { + $objp = $db->fetch_object($resql); + $var=!$var; + + print ''; + print '\n"; + + if ($objp->df > 0) + { + print ''; + } + else + { + print ''; + } + print ''; + print ''; + print ''; + print ''; + + // Affiche statut de la facture + print '"; + //print ""; + print "\n"; + $total+=$objp->total; + $total_ttc+=$objp->total_ttc; + $totalrecu+=$objp->am; + $i++; + } + + if (($offset + $num) <= $limit) + { + // Print total + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + } + } + + print "
 
'; + print ''; + print ''; + print ''; + //print ' '.$langs->trans('Year').': '; + $max_year = date("Y"); + $syear = $year; + //if ($syear == '') $syear = date("Y"); + $html->select_year($syear,'year',1, '', $max_year); + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ' '; + print ''; + print "
'; + + $facturestatic->id=$objp->facid; + $facturestatic->ref=$objp->facnumber; + $facturestatic->type=$objp->type; + + print ''; + print ''; + if ($objp->datelimite < (time() - $conf->facture->client->warning_delay) && ! $objp->paye && $objp->fk_statut == 1 && ! $objp->am) + { + print ''; + } + print ''; + print '
'; + print $facturestatic->getNomUrl(1); + print $objp->increment; + print ''; + print img_warning($langs->trans('Late')); + print ''; + $filename=sanitize_string($objp->facnumber); + $filedir=$conf->facture->dir_output . '/' . sanitize_string($objp->facnumber); + $urlsource=$_SERVER['PHP_SELF'].'?facid='.$objp->facid; + $formfile->show_documents('facture',$filename,$filedir,$urlsource,'','','','','',1); + print '
'; + + print "
'; + $y = strftime('%Y',$objp->df); + $m = strftime('%m',$objp->df); + print strftime('%d',$objp->df); + print ' '; + print substr(strftime('%B',$objp->df),0,3).''; + print ' '; + print strftime('%Y',$objp->df).'!!!'.img_object($langs->trans('ShowCompany'),'company').' '.dolibarr_trunc($objp->nom,48).''.price($objp->total).''.price($objp->total_ttc).''.price($objp->am).''; + print $facturestatic->LibStatut($objp->paye,$objp->fk_statut,5,$objp->am,$objp->type); + print " 
'.$langs->trans('Total').''.price($total).''.price($total_ttc).''.price($totalrecu).' 
\n"; + print "
\n"; + $db->free($resql); + } + else + { + dolibarr_print_error($db); + } + } +} + +$db->close(); + +llxFooter('$Date$ - $Revision$'); +?>