35 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
36 require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobjectline.class.php";
37 require_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
38 require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
48 public $element =
'contrat';
53 public $table_element =
'contrat';
58 public $table_element_line =
'contratdet';
63 public $fk_element =
'fk_contrat';
68 public $picto =
'contract';
74 public $ismultientitymanaged = 1;
79 public $isextrafieldmanaged = 1;
85 public $restrictiononfksoc = 1;
102 public $ref_supplier;
129 public $fk_user_author;
136 public $user_author_id;
141 public $user_creation;
146 public $user_cloture;
151 public $date_creation;
156 public $date_modification;
161 public $date_validation;
166 public $date_contrat;
168 public $commercial_signature_id;
169 public $commercial_suivi_id;
177 public $extraparams = array();
182 public $lines = array();
184 public $nbofservices;
185 public $nbofserviceswait;
186 public $nbofservicesopened;
187 public $nbofservicesexpired;
188 public $nbofservicesclosed;
196 protected $lines_id_index_mapper = array();
227 public $fields = array(
228 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
229 'ref' =>array(
'type'=>
'varchar(50)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>-1,
'showoncombobox'=>1,
'position'=>15,
'searchall'=>1),
230 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'Ref ext',
'enabled'=>1,
'visible'=>0,
'position'=>20),
231 'ref_customer' =>array(
'type'=>
'varchar(50)',
'label'=>
'RefCustomer',
'enabled'=>1,
'visible'=>-1,
'position'=>25,
'searchall'=>1),
232 'ref_supplier' =>array(
'type'=>
'varchar(50)',
'label'=>
'RefSupplier',
'enabled'=>1,
'visible'=>-1,
'position'=>26,
'searchall'=>1),
233 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>30,
'index'=>1),
234 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>35),
235 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-1,
'position'=>40),
236 'date_contrat' =>array(
'type'=>
'datetime',
'label'=>
'Date contrat',
'enabled'=>1,
'visible'=>-1,
'position'=>45),
237 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>
'isModEnabled("societe")',
'visible'=>-1,
'notnull'=>1,
'position'=>70),
238 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label'=>
'Project',
'enabled'=>
"isModEnabled('project')",
'visible'=>-1,
'position'=>75),
239 'fk_commercial_signature' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'SaleRepresentative Signature',
'enabled'=>1,
'visible'=>-1,
'position'=>80),
240 'fk_commercial_suivi' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'SaleRepresentative follower',
'enabled'=>1,
'visible'=>-1,
'position'=>85),
241 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>90),
242 'note_public' =>array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>105,
'searchall'=>1),
243 'note_private' =>array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>110,
'searchall'=>1),
244 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'Model pdf',
'enabled'=>1,
'visible'=>0,
'position'=>115),
245 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>120),
246 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>-1,
'position'=>125),
247 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>135),
248 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'Last main doc',
'enabled'=>1,
'visible'=>-1,
'position'=>140),
249 'statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Statut',
'enabled'=>1,
'visible'=>-1,
'position'=>500,
'notnull'=>1,
'arrayofkeyval'=>array(0=>
'Draft', 1=>
'Validated', 2=>
'Closed'))
253 const STATUS_DRAFT = 0;
254 const STATUS_VALIDATED = 1;
255 const STATUS_CLOSED = 2;
277 global $db, $langs, $conf;
278 $langs->load(
"contracts");
280 if (!empty($conf->global->CONTRACT_ADDON)) {
283 $file = $conf->global->CONTRACT_ADDON.
".php";
284 $classname = $conf->global->CONTRACT_ADDON;
287 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
289 foreach ($dirmodels as $reldir) {
293 $mybool |= @include_once $dir.$file;
301 $obj =
new $classname();
302 $numref = $obj->getNextValue($soc, $this);
307 $this->error = $obj->error;
312 $langs->load(
"errors");
313 print $langs->trans(
"Error").
" ".$langs->trans(
"ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv(
"Contract"));
329 public function active_line($user, $line_id, $date_start, $date_end =
'', $comment =
'')
332 $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->active_line($user, $date_start, $date_end, $comment);
334 $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
335 $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
351 public function close_line($user, $line_id, $date_end, $comment =
'')
354 $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->close_line($user, $date_end, $comment);
356 $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
357 $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
374 public function activateAll($user, $date_start =
'', $notrigger = 0, $comment =
'', $date_end =
'')
376 if (empty($date_start)) {
387 foreach ($this->lines as $contratline) {
389 if ($contratline->statut != ContratLigne::STATUS_OPEN) {
390 $contratline->context = $this->context;
392 $result = $contratline->active_line($user, $date_start, !empty($date_end) ? $date_end : -1, $comment);
395 $this->error = $contratline->error;
396 $this->errors = $contratline->errors;
402 if (!$error && $this->statut == 0) {
403 $result = $this->
validate($user,
'', $notrigger);
413 $this->
db->rollback();
438 foreach ($this->lines as $contratline) {
440 if ($contratline->statut != ContratLigne::STATUS_CLOSED) {
441 $contratline->date_end_real = $now;
442 $contratline->date_cloture = $now;
443 $contratline->fk_user_cloture = $user->id;
444 $contratline->statut = ContratLigne::STATUS_CLOSED;
445 $result = $contratline->close_line($user, $now, $comment, $notrigger);
448 $this->error = $contratline->error;
449 $this->errors = $contratline->errors;
455 if (!$error && $this->statut == 0) {
456 $result = $this->
validate($user,
'', $notrigger);
466 $this->
db->rollback();
479 public function validate(
User $user, $force_number =
'', $notrigger = 0)
481 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
482 global $langs, $conf;
487 dol_syslog(get_class($this).
'::validate user='.$user->id.
', force_number='.$force_number);
495 if (empty($conf->global->CONTRACT_DISABLE_AUTOSET_AS_CLIENT_ON_CONTRACT_VALIDATION)) {
496 $result = $this->thirdparty->set_as_client();
501 $num = $force_number;
502 } elseif (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
510 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contrat SET ref = '".$this->
db->escape($num).
"', statut = 1";
512 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut = 0";
514 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
515 $resql = $this->
db->query(
$sql);
519 $this->error = $this->
db->lasterror();
523 if (!$error && !$notrigger) {
525 $result = $this->
call_trigger(
'CONTRACT_VALIDATE', $user);
533 $this->oldref = $this->ref;
536 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
538 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filename = CONCAT('".$this->
db->escape($this->newref).
"', SUBSTR(filename, ".(strlen($this->
ref) + 1).
")), filepath = 'contract/".$this->
db->escape($this->newref).
"'";
539 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'contract/".$this->
db->escape($this->
ref).
"' and entity = ".$conf->entity;
540 $resql = $this->
db->query(
$sql);
542 $error++; $this->error = $this->
db->lasterror();
548 $dirsource = $conf->contract->dir_output.
'/'.$oldref;
549 $dirdest = $conf->contract->dir_output.
'/'.$newref;
550 if (!$error && file_exists($dirsource)) {
551 dol_syslog(get_class($this).
"::validate rename dir ".$dirsource.
" into ".$dirdest);
553 if (@rename($dirsource, $dirdest)) {
556 $listoffiles =
dol_dir_list($conf->contract->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
557 foreach ($listoffiles as $fileentry) {
558 $dirsource = $fileentry[
'name'];
559 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
560 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
561 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
562 @rename($dirsource, $dirdest);
573 $this->brouillon = 0;
574 $this->date_validation = $now;
584 $this->
db->rollback();
596 public function reopen($user, $notrigger = 0)
598 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
599 global $langs, $conf;
604 dol_syslog(get_class($this).
'::reopen user='.$user->id);
610 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contrat SET statut = 0";
612 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut = 1";
614 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
615 $resql = $this->
db->query(
$sql);
619 $this->error = $this->
db->lasterror();
623 if (!$error && !$notrigger) {
625 $result = $this->
call_trigger(
'CONTRACT_REOPEN', $user);
635 $this->brouillon = 1;
636 $this->date_validation = $now;
643 $this->
db->rollback();
657 public function fetch($id, $ref =
'', $ref_customer =
'', $ref_supplier =
'')
659 $sql =
"SELECT rowid, statut, ref, fk_soc,";
660 $sql .=
" ref_supplier, ref_customer,";
663 $sql .=
" date_contrat as datecontrat,";
664 $sql .=
" fk_user_author,";
665 $sql .=
" fk_projet as fk_project,";
666 $sql .=
" fk_commercial_signature, fk_commercial_suivi,";
667 $sql .=
" note_private, note_public, model_pdf, last_main_doc, extraparams";
668 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat";
670 $sql .=
" WHERE entity IN (".getEntity(
'contract').
")";
672 $sql .=
" WHERE rowid = ".(int) $id;
675 $sql .=
" AND ref_customer = '".$this->db->escape($ref_customer).
"'";
678 $sql .=
" AND ref_supplier = '".$this->db->escape($ref_supplier).
"'";
681 $sql .=
" AND ref = '".$this->db->escape($ref).
"'";
684 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
685 $resql = $this->
db->query(
$sql);
687 $num = $this->
db->num_rows($resql);
689 $this->error =
'Fetch found several records.';
693 $obj = $this->
db->fetch_object($resql);
695 $this->
id = $obj->rowid;
696 $this->
ref = (!isset($obj->ref) || !$obj->ref) ? $obj->rowid : $obj->ref;
697 $this->ref_customer = $obj->ref_customer;
698 $this->ref_supplier = $obj->ref_supplier;
699 $this->ref_ext = $obj->ref_ext;
700 $this->entity = $obj->entity;
701 $this->statut = $obj->statut;
703 $this->date_contrat = $this->db->jdate($obj->datecontrat);
704 $this->date_creation = $this->
db->jdate($obj->datecontrat);
706 $this->user_author_id = $obj->fk_user_author;
708 $this->commercial_signature_id = $obj->fk_commercial_signature;
709 $this->commercial_suivi_id = $obj->fk_commercial_suivi;
711 $this->note_private = $obj->note_private;
712 $this->note_public = $obj->note_public;
713 $this->model_pdf = $obj->model_pdf;
714 $this->modelpdf = $obj->model_pdf;
716 $this->fk_projet = $obj->fk_project;
717 $this->fk_project = $obj->fk_project;
719 $this->socid = $obj->fk_soc;
720 $this->fk_soc = $obj->fk_soc;
721 $this->last_main_doc = $obj->last_main_doc;
722 $this->extraparams = (isset($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) :
null);
724 $this->
db->free($resql);
731 if ($result >= 0 && !empty($this->table_element_line)) {
736 $this->error = $this->
db->lasterror();
743 dol_syslog(get_class($this).
"::fetch Contract not found");
744 $this->error =
"Contract not found";
748 dol_syslog(get_class($this).
"::fetch Error searching contract");
749 $this->error = $this->
db->error();
763 public function fetch_lines($only_services = 0, $loadalsotranslation = 0)
766 global $langs, $conf;
768 $this->nbofservices = 0;
769 $this->nbofserviceswait = 0;
770 $this->nbofservicesopened = 0;
771 $this->nbofservicesexpired = 0;
772 $this->nbofservicesclosed = 0;
780 $this->lines = array();
784 $sql =
"SELECT p.label as product_label, p.description as product_desc, p.ref as product_ref, p.fk_product_type as product_type,";
785 $sql .=
" d.rowid, d.fk_contrat, d.statut, d.description, d.price_ht, d.vat_src_code, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.localtax1_type, d.localtax2_type, d.qty, d.remise_percent, d.subprice, d.fk_product_fournisseur_price as fk_fournprice, d.buy_price_ht as pa_ht,";
786 $sql .=
" d.total_ht,";
787 $sql .=
" d.total_tva,";
788 $sql .=
" d.total_localtax1,";
789 $sql .=
" d.total_localtax2,";
790 $sql .=
" d.total_ttc,";
791 $sql .=
" d.info_bits, d.fk_product,";
792 $sql .=
" d.date_ouverture_prevue as date_start,";
793 $sql .=
" d.date_ouverture as date_start_real,";
794 $sql .=
" d.date_fin_validite as date_end,";
795 $sql .=
" d.date_cloture as date_end_real,";
796 $sql .=
" d.fk_user_author,";
797 $sql .=
" d.fk_user_ouverture,";
798 $sql .=
" d.fk_user_cloture,";
799 $sql .=
" d.fk_unit,";
800 $sql .=
" d.product_type as type,";
802 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as d LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON d.fk_product = p.rowid";
803 $sql .=
" WHERE d.fk_contrat = ".((int) $this->
id);
804 if ($only_services == 1) {
805 $sql .=
" AND d.product_type = 1";
807 $sql .=
" ORDER by d.rang ASC";
809 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
810 $result = $this->
db->query(
$sql);
812 $num = $this->
db->num_rows($result);
816 $objp = $this->
db->fetch_object($result);
820 $line->id = $objp->rowid;
821 $line->ref = $objp->rowid;
822 $line->fk_contrat = $objp->fk_contrat;
823 $line->desc = $objp->description;
824 $line->qty = $objp->qty;
825 $line->vat_src_code = $objp->vat_src_code;
826 $line->tva_tx = $objp->tva_tx;
827 $line->localtax1_tx = $objp->localtax1_tx;
828 $line->localtax2_tx = $objp->localtax2_tx;
829 $line->localtax1_type = $objp->localtax1_type;
830 $line->localtax2_type = $objp->localtax2_type;
831 $line->subprice = $objp->subprice;
832 $line->statut = $objp->statut;
833 $line->remise_percent = $objp->remise_percent;
834 $line->price_ht = $objp->price_ht;
835 $line->price = $objp->price_ht;
836 $line->total_ht = $objp->total_ht;
837 $line->total_tva = $objp->total_tva;
838 $line->total_localtax1 = $objp->total_localtax1;
839 $line->total_localtax2 = $objp->total_localtax2;
840 $line->total_ttc = $objp->total_ttc;
841 $line->fk_product = (($objp->fk_product > 0) ? $objp->fk_product : 0);
842 $line->info_bits = $objp->info_bits;
843 $line->type = $objp->type;
845 $line->fk_fournprice = $objp->fk_fournprice;
846 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_fournprice, $objp->pa_ht);
847 $line->pa_ht = $marginInfos[0];
849 $line->fk_user_author = $objp->fk_user_author;
850 $line->fk_user_ouverture = $objp->fk_user_ouverture;
851 $line->fk_user_cloture = $objp->fk_user_cloture;
852 $line->fk_unit = $objp->fk_unit;
854 $line->ref = $objp->product_ref;
855 $line->product_ref = $objp->product_ref;
856 $line->product_type = $objp->product_type;
857 $line->product_desc = $objp->product_desc;
858 $line->product_label = $objp->product_label;
860 $line->description = $objp->description;
862 $line->date_start = $this->
db->jdate($objp->date_start);
863 $line->date_start_real = $this->
db->jdate($objp->date_start_real);
864 $line->date_end = $this->
db->jdate($objp->date_end);
865 $line->date_end_real = $this->
db->jdate($objp->date_end_real);
876 $line->rang = $objp->rang;
880 $line->fetch_optionals();
883 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
885 $tmpproduct->fetch($objp->fk_product);
886 $tmpproduct->getMultiLangs();
888 $line->multilangs = $tmpproduct->multilangs;
891 $this->lines[$pos] = $line;
893 $this->lines_id_index_mapper[$line->id] = $pos;
898 if ($line->statut == ContratLigne::STATUS_INITIAL) {
899 $this->nbofserviceswait++;
901 if ($line->statut == ContratLigne::STATUS_OPEN && (empty($line->date_end) || $line->date_end >= $now)) {
902 $this->nbofservicesopened++;
904 if ($line->statut == ContratLigne::STATUS_OPEN && (!empty($line->date_end) && $line->date_end < $now)) {
905 $this->nbofservicesexpired++;
907 if ($line->statut == ContratLigne::STATUS_CLOSED) {
908 $this->nbofservicesclosed++;
911 $total_ttc += $objp->total_ttc;
912 $total_vat += $objp->total_tva;
913 $total_ht += $objp->total_ht;
918 $this->
db->free($result);
920 dol_syslog(get_class($this).
"::Fetch Error when reading lines of contracts linked to products");
925 $this->nbofservices = count($this->lines);
926 $this->total_ttc =
price2num($total_ttc);
927 $this->total_tva =
price2num($total_vat);
941 global $conf, $langs, $mysoc;
945 if ($this->commercial_signature_id <= 0) {
946 $langs->load(
"commercial");
947 $this->error .= $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"SalesRepresentativeSignature"));
950 if ($this->commercial_suivi_id <= 0) {
951 $langs->load(
"commercial");
952 $this->error .= ($this->error ?
"<br>" :
'');
953 $this->error .= $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"SalesRepresentativeFollowUp"));
966 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contrat (datec, fk_soc, fk_user_author, date_contrat,";
967 $sql .=
" fk_commercial_signature, fk_commercial_suivi, fk_projet,";
968 $sql .=
" ref, entity, note_private, note_public, ref_customer, ref_supplier, ref_ext)";
969 $sql .=
" VALUES ('".$this->db->idate($now).
"', ".((int) $this->socid).
", ".((int) $user->id);
970 $sql .=
", ".(dol_strlen($this->date_contrat) != 0 ?
"'".$this->db->idate($this->date_contrat).
"'" :
"NULL");
971 $sql .=
",".($this->commercial_signature_id > 0 ? ((int) $this->commercial_signature_id) :
"NULL");
972 $sql .=
",".($this->commercial_suivi_id > 0 ? ((int) $this->commercial_suivi_id) :
"NULL");
973 $sql .=
",".($this->fk_project > 0 ? ((int) $this->fk_project) :
"NULL");
974 $sql .=
", ".(dol_strlen($this->
ref) <= 0 ?
"null" :
"'".$this->db->escape($this->
ref).
"'");
975 $sql .=
", ".((int) $conf->entity);
976 $sql .=
", ".(!empty($this->note_private) ? (
"'".$this->db->escape($this->note_private).
"'") :
"NULL");
977 $sql .=
", ".(!empty($this->note_public) ? (
"'".$this->db->escape($this->note_public).
"'") :
"NULL");
978 $sql .=
", ".(!empty($this->ref_customer) ? (
"'".$this->db->escape($this->ref_customer).
"'") :
"NULL");
979 $sql .=
", ".(!empty($this->ref_supplier) ? (
"'".$this->db->escape($this->ref_supplier).
"'") :
"NULL");
980 $sql .=
", ".(!empty($this->ref_ext) ? (
"'".$this->db->escape($this->ref_ext).
"'") :
"NULL");
982 $resql = $this->
db->query(
$sql);
987 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"contrat");
990 $module = (!empty($conf->global->CONTRACT_ADDON) ? $conf->global->CONTRACT_ADDON :
'mod_contract_serpis');
991 if (substr($module, 0, 13) ==
'mod_contract_' && substr($module, -3) ==
'php') {
992 $module = substr($module, 0,
dol_strlen($module) - 4);
996 $modCodeContract =
new $module();
998 if (!empty($modCodeContract->code_auto)) {
1000 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"contrat SET ref='(PROV".$this->
id.
")' WHERE rowid=".((int) $this->
id);
1001 if ($this->
db->query(
$sql)) {
1003 $this->
ref =
"(PROV".$this->id.
")";
1018 $result = $this->
add_contact($this->commercial_signature_id,
'SALESREPSIGN',
'internal');
1026 $result = $this->
add_contact($this->commercial_suivi_id,
'SALESREPFOLL',
'internal');
1033 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1034 $this->linked_objects = $this->linkedObjectsIds;
1038 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1039 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1040 if (is_array($tmp_origin_id)) {
1041 foreach ($tmp_origin_id as $origin_id) {
1044 $this->error = $this->
db->lasterror();
1050 $origin_id = $tmp_origin_id;
1053 $this->error = $this->
db->lasterror();
1060 if (!$error && $this->
id && !empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && !empty($this->origin) && !empty($this->origin_id)) {
1061 $originforcontact = $this->origin;
1062 $originidforcontact = $this->origin_id;
1063 if ($originforcontact ==
'shipping') {
1064 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1066 $exp->fetch($this->origin_id);
1067 $exp->fetchObjectLinked();
1068 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
1069 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
1070 $originforcontact =
'commande';
1071 $originidforcontact = $value;
1077 $sqlcontact =
"SELECT ctc.code, ctc.source, ec.fk_socpeople FROM ".MAIN_DB_PREFIX.
"element_contact as ec, ".MAIN_DB_PREFIX.
"c_type_contact as ctc";
1078 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->
db->escape($originforcontact).
"'";
1080 $resqlcontact = $this->
db->query($sqlcontact);
1081 if ($resqlcontact) {
1082 while ($objcontact = $this->
db->fetch_object($resqlcontact)) {
1083 if ($objcontact->source ==
'internal' && in_array($objcontact->code, array(
'SALESREPSIGN',
'SALESREPFOLL'))) {
1088 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1098 $result = $this->
call_trigger(
'CONTRACT_CREATE', $user);
1105 $this->
db->commit();
1108 dol_syslog(get_class($this).
"::create - 30 - ".$this->error, LOG_ERR);
1109 $this->
db->rollback();
1113 $this->error =
"Failed to add contract";
1114 dol_syslog(get_class($this).
"::create - 20 - ".$this->error, LOG_ERR);
1115 $this->
db->rollback();
1119 $this->error = $langs->trans(
"UnknownError: ".$this->
db->error().
" -", LOG_DEBUG);
1121 $this->
db->rollback();
1133 public function delete($user)
1135 global $conf, $langs;
1136 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1143 $result = $this->
call_trigger(
'CONTRACT_DELETE', $user);
1153 dol_syslog(get_class($this).
"::delete error", LOG_ERR);
1173 $sql =
"SELECT cdl.rowid as cdlrowid ";
1174 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet_log as cdl, ".MAIN_DB_PREFIX.
"contratdet as cd";
1175 $sql .=
" WHERE cdl.fk_contratdet=cd.rowid AND cd.fk_contrat=".((int) $this->
id);
1177 dol_syslog(get_class($this).
"::delete contratdet_log", LOG_DEBUG);
1178 $resql = $this->
db->query(
$sql);
1180 $this->error = $this->
db->error();
1183 $numressql = $this->
db->num_rows($resql);
1184 if (!$error && $numressql) {
1185 $tab_resql = array();
1186 for ($i = 0; $i < $numressql; $i++) {
1187 $objresql = $this->
db->fetch_object($resql);
1188 $tab_resql[] = $objresql->cdlrowid;
1190 $this->
db->free($resql);
1192 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"contratdet_log ";
1193 $sql .=
" WHERE ".MAIN_DB_PREFIX.
"contratdet_log.rowid IN (".$this->
db->sanitize(implode(
",", $tab_resql)).
")";
1195 dol_syslog(get_class($this).
"::delete contratdet_log", LOG_DEBUG);
1196 $resql = $this->
db->query(
$sql);
1198 $this->error = $this->
db->error();
1207 $main = MAIN_DB_PREFIX.
'contratdet';
1208 $ef = $main.
"_extrafields";
1209 $sql =
"DELETE FROM ".$ef.
" WHERE fk_object IN (SELECT rowid FROM ".$main.
" WHERE fk_contrat = ".((int) $this->
id).
")";
1211 dol_syslog(get_class($this).
"::delete contratdet_extrafields", LOG_DEBUG);
1212 $resql = $this->
db->query(
$sql);
1214 $this->error = $this->
db->error();
1221 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"contratdet";
1222 $sql .=
" WHERE fk_contrat=".((int) $this->
id);
1224 dol_syslog(get_class($this).
"::delete contratdet", LOG_DEBUG);
1225 $resql = $this->
db->query(
$sql);
1227 $this->error = $this->
db->error();
1234 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
"ecm_files WHERE src_object_type = '".$this->
db->escape($this->table_element.(empty($this->module) ?
"" :
"@".$this->module)).
"' AND src_object_id = ".((
int) $this->id);
1235 $resql = $this->
db->query(
$sql);
1237 $this->error = $this->
db->lasterror();
1238 $this->errors[] = $this->error;
1245 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"contrat";
1246 $sql .=
" WHERE rowid=".((int) $this->
id);
1248 dol_syslog(get_class($this).
"::delete contrat", LOG_DEBUG);
1249 $resql = $this->
db->query(
$sql);
1251 $this->error = $this->
db->error();
1261 dol_syslog(get_class($this).
"::delete error -3 ".$this->error, LOG_ERR);
1268 if ($conf->contrat->dir_output) {
1269 $dir = $conf->contrat->multidir_output[$this->entity].
"/".$ref;
1270 if (file_exists($dir)) {
1273 $this->error =
'ErrorFailToDeleteDir';
1281 $this->
db->commit();
1284 $this->error = $this->
db->lasterror();
1285 $this->
db->rollback();
1297 public function update($user, $notrigger = 0)
1299 global $conf, $langs;
1303 if (empty($this->fk_commercial_signature) && $this->commercial_signature_id > 0) {
1304 $this->fk_commercial_signature = $this->commercial_signature_id;
1306 if (empty($this->fk_commercial_suivi) && $this->commercial_suivi_id > 0) {
1307 $this->fk_commercial_suivi = $this->commercial_suivi_id;
1309 if (empty($this->fk_soc) && $this->socid > 0) {
1310 $this->fk_soc = (int) $this->socid;
1312 if (empty($this->fk_project) && $this->projet > 0) {
1313 $this->fk_project = (int) $this->projet;
1316 if (isset($this->
ref)) {
1317 $this->
ref = trim($this->
ref);
1319 if (isset($this->ref_customer)) {
1320 $this->ref_customer = trim($this->ref_customer);
1322 if (isset($this->ref_supplier)) {
1323 $this->ref_supplier = trim($this->ref_supplier);
1325 if (isset($this->ref_ext)) {
1326 $this->ref_ext = trim($this->ref_ext);
1328 if (isset($this->entity)) {
1329 $this->entity = (int) $this->entity;
1331 if (isset($this->statut)) {
1332 $this->statut = (int) $this->statut;
1334 if (isset($this->fk_soc)) {
1335 $this->fk_soc = (int) $this->fk_soc;
1337 if (isset($this->fk_commercial_signature)) {
1338 $this->fk_commercial_signature = trim($this->fk_commercial_signature);
1340 if (isset($this->fk_commercial_suivi)) {
1341 $this->fk_commercial_suivi = trim($this->fk_commercial_suivi);
1343 if (isset($this->note_private)) {
1344 $this->note_private = trim($this->note_private);
1346 if (isset($this->note_public)) {
1347 $this->note_public = trim($this->note_public);
1349 if (isset($this->import_key)) {
1350 $this->import_key = trim($this->import_key);
1358 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contrat SET";
1359 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1360 $sql .=
" ref_customer=".(isset($this->ref_customer) ?
"'".$this->db->escape($this->ref_customer).
"'" :
"null").
",";
1361 $sql .=
" ref_supplier=".(isset($this->ref_supplier) ?
"'".$this->db->escape($this->ref_supplier).
"'" :
"null").
",";
1362 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1363 $sql .=
" entity=".$conf->entity.
",";
1364 $sql .=
" date_contrat=".(dol_strlen($this->date_contrat) != 0 ?
"'".$this->db->idate($this->date_contrat).
"'" :
'null').
",";
1365 $sql .=
" statut=".(isset($this->statut) ? $this->statut :
"null").
",";
1366 $sql .=
" fk_soc=".($this->fk_soc > 0 ? $this->fk_soc :
"null").
",";
1367 $sql .=
" fk_projet=".($this->fk_project > 0 ? $this->fk_project :
"null").
",";
1368 $sql .=
" fk_commercial_signature=".(isset($this->fk_commercial_signature) ? $this->fk_commercial_signature :
"null").
",";
1369 $sql .=
" fk_commercial_suivi=".(isset($this->fk_commercial_suivi) ? $this->fk_commercial_suivi :
"null").
",";
1370 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1371 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1372 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
1374 $sql .=
" WHERE rowid=".((int) $this->
id);
1378 $resql = $this->
db->query(
$sql);
1380 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1390 if (!$error && !$notrigger) {
1392 $result = $this->
call_trigger(
'CONTRACT_MODIFY', $user);
1401 foreach ($this->errors as $errmsg) {
1402 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1403 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1405 $this->
db->rollback();
1408 $this->
db->commit();
1437 public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type =
'HT', $pu_ttc = 0.0, $info_bits = 0, $fk_fournprice =
null, $pa_ht = 0, $array_options = 0, $fk_unit =
null, $rang = 0)
1439 global $user, $langs, $conf, $mysoc;
1442 dol_syslog(get_class($this).
"::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type, $pu_ttc, $info_bits, $rang");
1445 if ($fk_product <= 0 && empty($desc)) {
1446 $this->error =
"ErrorDescRequiredForFreeProductLines";
1450 if ($this->statut >= 0) {
1459 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1460 $vat_src_code = $reg[1];
1461 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1467 $remise_percent =
price2num($remise_percent);
1472 if (empty($info_bits)) {
1475 if (empty($pu_ht) || !is_numeric($pu_ht)) {
1478 if (empty($pu_ttc)) {
1481 if (empty($txtva) || !is_numeric($txtva)) {
1484 if (empty($txlocaltax1) || !is_numeric($txlocaltax1)) {
1487 if (empty($txlocaltax2) || !is_numeric($txlocaltax2)) {
1491 if ($price_base_type ==
'HT') {
1498 if (empty($remise_percent)) {
1499 $remise_percent = 0;
1505 if ($date_start && $date_end && $date_start > $date_end) {
1506 $langs->load(
"errors");
1507 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1513 $localtaxes_type =
getLocalTaxesFromRate($txtva.($vat_src_code ?
' ('.$vat_src_code.
')' :
''), 0, $this->societe, $mysoc);
1520 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1521 $total_ht = $tabprice[0];
1522 $total_tva = $tabprice[1];
1523 $total_ttc = $tabprice[2];
1524 $total_localtax1 = $tabprice[9];
1525 $total_localtax2 = $tabprice[10];
1527 $localtax1_type = $localtaxes_type[0];
1528 $localtax2_type = $localtaxes_type[2];
1535 $remise = round(($pu_ht * $remise_percent / 100), 2);
1536 $price = $pu_ht - $remise;
1539 if (empty($pa_ht)) {
1545 if ($this->pa_ht == 0) {
1546 if (($result = $this->
defineBuyPrice($pu_ht, $remise_percent, $fk_product)) < 0) {
1554 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
1555 $sql .=
" (fk_contrat, label, description, fk_product, qty, tva_tx, vat_src_code,";
1556 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
1557 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
1558 $sql .=
" info_bits,";
1559 $sql .=
" price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
1560 if ($date_start > 0) {
1561 $sql .=
",date_ouverture_prevue";
1563 if ($date_end > 0) {
1564 $sql .=
",date_fin_validite";
1566 $sql .=
", fk_unit";
1568 $sql .=
") VALUES (";
1569 $sql .= $this->
id.
", '', '".$this->
db->escape($desc).
"',";
1570 $sql .= ($fk_product > 0 ? $fk_product :
"null").
",";
1571 $sql .=
" ".((float) $qty).
",";
1572 $sql .=
" ".((float) $txtva).
",";
1573 $sql .=
" ".($vat_src_code ?
"'".$this->db->escape($vat_src_code).
"'" :
"null").
",";
1574 $sql .=
" ".((float) $txlocaltax1).
",";
1575 $sql .=
" ".((float) $txlocaltax2).
",";
1576 $sql .=
" '".$this->db->escape($localtax1_type).
"',";
1577 $sql .=
" '".$this->db->escape($localtax2_type).
"',";
1578 $sql .=
" ".price2num($remise_percent).
",";
1579 $sql .=
" ".price2num($pu_ht).
",";
1581 $sql .=
" '".$this->db->escape($info_bits).
"',";
1583 if (isset($fk_fournprice)) {
1584 $sql .=
' '.((int) $fk_fournprice).
',';
1588 if (isset($pa_ht)) {
1589 $sql .=
' '.price2num($pa_ht);
1593 if ($date_start > 0) {
1594 $sql .=
",'".$this->db->idate($date_start).
"'";
1596 if ($date_end > 0) {
1597 $sql .=
",'".$this->db->idate($date_end).
"'";
1599 $sql .=
", ".($fk_unit ?
"'".$this->db->escape($fk_unit).
"'" :
"null");
1600 $sql .=
", ".(!empty($rang) ? (int) $rang :
"0");
1603 $resql = $this->
db->query(
$sql);
1605 $contractlineid = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"contratdet");
1609 $contractline->array_options = $array_options;
1610 $contractline->id = $contractlineid;
1611 $result = $contractline->insertExtraFields();
1613 $this->error[] = $contractline->error;
1618 if (empty($error)) {
1620 $result = $this->
call_trigger(
'LINECONTRACT_INSERT', $user);
1628 $this->
db->rollback();
1631 $this->
db->commit();
1632 return $contractlineid;
1635 $this->
db->rollback();
1636 $this->error = $this->
db->error().
" sql=".
$sql;
1640 dol_syslog(get_class($this).
"::addline ErrorTryToAddLineOnValidatedContract", LOG_ERR);
1669 public function updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $tvatx, $localtax1tx = 0.0, $localtax2tx = 0.0, $date_start_real =
'', $date_end_real =
'', $price_base_type =
'HT', $info_bits = 0, $fk_fournprice =
null, $pa_ht = 0, $array_options = 0, $fk_unit =
null, $rang = 0)
1671 global $user, $conf, $langs, $mysoc;
1677 $desc = trim($desc);
1678 $desc = trim($desc);
1684 if (empty($fk_fournprice)) {
1694 $remise = round(($pu * $remise_percent / 100), 2);
1695 $price = $pu - $remise;
1697 $remise_percent = 0;
1700 if ($date_start && $date_end && $date_start > $date_end) {
1701 $langs->load(
"errors");
1702 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1706 dol_syslog(get_class($this).
"::updateline $rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $date_start_real, $date_end_real, $tvatx, $localtax1tx, $localtax2tx, $price_base_type, $info_bits, $rang");
1716 $tvatx = preg_replace(
'/\s*\(.*\)/',
'', $tvatx);
1718 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $tvatx, $localtax1tx, $localtax2tx, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1719 $total_ht = $tabprice[0];
1720 $total_tva = $tabprice[1];
1721 $total_ttc = $tabprice[2];
1722 $total_localtax1 = $tabprice[9];
1723 $total_localtax2 = $tabprice[10];
1725 $localtax1_type = (empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0]);
1726 $localtax2_type = (empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2]);
1733 $remise = round(($pu * $remise_percent / 100), 2);
1734 $price = $pu - $remise;
1737 if (empty($pa_ht)) {
1742 if ($this->pa_ht == 0) {
1743 if (($result = $this->
defineBuyPrice($pu, $remise_percent)) < 0) {
1750 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet set description = '".$this->
db->escape($desc).
"'";
1754 $sql .=
",remise_percent = ".((float)
price2num($remise_percent));
1755 $sql .=
",qty = ".((float) $qty);
1757 $sql .=
",localtax1_tx = ".((float)
price2num($localtax1tx));
1758 $sql .=
",localtax2_tx = ".((float)
price2num($localtax2tx));
1759 $sql .=
",localtax1_type='".$this->db->escape($localtax1_type).
"'";
1760 $sql .=
",localtax2_type='".$this->db->escape($localtax2_type).
"'";
1763 $sql .=
", total_localtax1 = ".((float)
price2num($total_localtax1));
1764 $sql .=
", total_localtax2 = ".((float)
price2num($total_localtax2));
1766 $sql .=
", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice :
"null");
1768 if ($date_start > 0) {
1769 $sql .=
",date_ouverture_prevue = '".$this->db->idate($date_start).
"'";
1771 $sql .=
",date_ouverture_prevue = null";
1773 if ($date_end > 0) {
1774 $sql .=
",date_fin_validite = '".$this->db->idate($date_end).
"'";
1776 $sql .=
",date_fin_validite = null";
1778 if ($date_start_real > 0) {
1779 $sql .=
",date_ouverture = '".$this->db->idate($date_start_real).
"'";
1781 $sql .=
",date_ouverture = null";
1783 if ($date_end_real > 0) {
1784 $sql .=
",date_cloture = '".$this->db->idate($date_end_real).
"'";
1786 $sql .=
",date_cloture = null";
1788 $sql .=
", fk_unit = ".($fk_unit > 0 ? ((int) $fk_unit) :
"null");
1789 $sql .=
", rang = ".(!empty($rang) ? ((int) $rang) :
"0");
1790 $sql .=
" WHERE rowid = ".((int) $rowid);
1792 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
1793 $result = $this->
db->query(
$sql);
1795 if (is_array($array_options) && count($array_options) > 0) {
1797 $contractline->fetch($rowid);
1800 foreach ($array_options as $key => $value) {
1801 $contractline->array_options[$key] = $array_options[$key];
1804 $result = $contractline->insertExtraFields();
1806 $this->error[] = $contractline->error;
1811 if (empty($error)) {
1813 $result = $this->
call_trigger(
'LINECONTRACT_MODIFY', $user);
1815 $this->
db->rollback();
1820 $this->
db->commit();
1824 $this->
db->rollback();
1825 $this->error = $this->
db->error();
1826 dol_syslog(get_class($this).
"::updateline Erreur -1");
1840 global $conf, $langs;
1844 if ($this->statut >= 0) {
1846 $result = $this->
call_trigger(
'LINECONTRACT_DELETE', $user);
1854 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element_line;
1855 $sql .=
" WHERE rowid = ".((int) $idline);
1857 dol_syslog(get_class($this).
"::deleteline", LOG_DEBUG);
1858 $resql = $this->
db->query(
$sql);
1860 $this->error =
"Error ".$this->db->lasterror();
1867 $contractline->id = $idline;
1868 $result = $contractline->deleteExtraFields();
1871 $this->error =
"Error ".get_class($this).
"::deleteline deleteExtraFields error -4 ".$contractline->error;
1875 if (empty($error)) {
1876 $this->
db->commit();
1879 dol_syslog(get_class($this).
"::deleteline ERROR:".$this->error, LOG_ERR);
1880 $this->
db->rollback();
1884 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
1901 dol_syslog(__METHOD__.
" is deprecated", LOG_WARNING);
1904 if ($this->statut == 0) {
1929 return $this->
LibStatut($this->statut, $mode);
1945 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1947 $langs->load(
"contracts");
1948 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'ContractStatusDraft');
1949 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'ContractStatusValidated');
1950 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv(
'ContractStatusClosed');
1951 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'ContractStatusDraft');
1952 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'ContractStatusValidated');
1953 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv(
'ContractStatusClosed');
1956 $statusType =
'status'.$status;
1957 if ($status == self::STATUS_VALIDATED) {
1958 $statusType =
'status6';
1961 if ($mode == 4 || $mode == 6 || $mode == 7) {
1964 $text =
'<span class="hideonsmartphone">';
1965 $text .= ($this->nbofserviceswait + $this->nbofservicesopened + $this->nbofservicesexpired + $this->nbofservicesclosed);
1966 $text .=
' '.$langs->trans(
"Services");
1967 $text .=
': ';
1970 $text .= ($mode == 7 ?
'<span class="nowraponall">' :
'');
1971 $text .= ($mode != 7 || $this->nbofserviceswait > 0) ? ($this->nbofserviceswait.ContratLigne::LibStatut(0, 3, -1,
'class="marginleft2"')).(($mode != 7 || $this->nbofservicesopened || $this->nbofservicesexpired || $this->nbofservicesclosed) ?
' ' :
'') :
'';
1972 $text .= ($mode == 7 ?
'</span><span class="nowraponall">' :
'');
1973 $text .= ($mode != 7 || $this->nbofservicesopened > 0) ? ($this->nbofservicesopened.ContratLigne::LibStatut(4, 3, 0,
'class="marginleft2"')).(($mode != 7 || $this->nbofservicesexpired || $this->nbofservicesclosed) ?
' ' :
'') :
'';
1974 $text .= ($mode == 7 ?
'</span><span class="nowraponall">' :
'');
1975 $text .= ($mode != 7 || $this->nbofservicesexpired > 0) ? ($this->nbofservicesexpired.ContratLigne::LibStatut(4, 3, 1,
'class="marginleft2"')).(($mode != 7 || $this->nbofservicesclosed) ?
' ' :
'') :
'';
1976 $text .= ($mode == 7 ?
'</span><span class="nowraponall">' :
'');
1977 $text .= ($mode != 7 || $this->nbofservicesclosed > 0) ? ($this->nbofservicesclosed.ContratLigne::LibStatut(5, 3, -1,
'class="marginleft2"')) :
'';
1978 $text .= ($mode == 7 ?
'</span>' :
'');
1981 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
1993 global $conf, $langs, $user;
1995 $langs->load(
'contracts');
1999 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
2000 return [
'optimize' => $langs->trans(
"ShowContract")];
2002 if ($user->hasRight(
'contrat',
'lire')) {
2003 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Contract").
'</u>';
2008 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.($this->
ref ? $this->
ref : $this->id);
2009 $datas[
'refcustomer'] =
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '. $this->ref_customer;
2010 $datas[
'refsupplier'] =
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
2011 if (!empty($this->total_ht)) {
2012 $datas[
'amountht'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
2014 if (!empty($this->total_tva)) {
2015 $datas[
'vatamount'] =
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
2017 if (!empty($this->total_ttc)) {
2018 $datas[
'amounttc'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
2033 public function getNomUrl($withpicto = 0, $maxlength = 0, $notooltip = 0, $save_lastsearch_value = -1)
2035 global $conf, $langs, $user, $hookmanager;
2039 $url = DOL_URL_ROOT.
'/contrat/card.php?id='.$this->id;
2044 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2045 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
2046 $add_save_lastsearch_values = 1;
2048 if ($add_save_lastsearch_values) {
2049 $url .=
'&save_lastsearch_values=1';
2054 'objecttype' => $this->element,
2056 $classfortooltip =
'classfortooltip';
2059 $classfortooltip =
'classforajaxtooltip';
2060 $dataparams =
' data-params='.json_encode($params);
2067 if (empty($notooltip) && $user->hasRight(
'contrat',
'lire')) {
2068 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
2069 $label = $langs->trans(
"ShowContract");
2070 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
2072 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
2073 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
2075 $linkstart =
'<a href="'.$url.
'"';
2076 $linkstart .= $linkclose.
'>';
2079 $result .= $linkstart;
2081 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') : $dataparams.
' class="'.(($withpicto != 2) ?
'paddingright ' :
'').$classfortooltip.
'"'), 0, 0, $notooltip ? 0 : 1);
2083 if ($withpicto != 2) {
2084 $result .= ($this->
ref ? $this->
ref : $this->id);
2086 $result .= $linkend;
2089 $hookmanager->initHooks(array(
'contractdao'));
2090 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
2091 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
2093 $result = $hookmanager->resPrint;
2095 $result .= $hookmanager->resPrint;
2109 $sql =
"SELECT c.rowid, c.ref, c.datec,";
2110 $sql .=
" c.tms as date_modification,";
2111 $sql .=
" fk_user_author";
2112 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2113 $sql .=
" WHERE c.rowid = ".((int) $id);
2115 $result = $this->
db->query(
$sql);
2117 if ($this->
db->num_rows($result)) {
2118 $obj = $this->
db->fetch_object($result);
2120 $this->
id = $obj->rowid;
2122 if ($obj->fk_user_author) {
2123 $cuser =
new User($this->
db);
2124 $cuser->fetch($obj->fk_user_author);
2125 $this->user_creation = $cuser;
2128 $this->
ref = (!$obj->ref) ? $obj->rowid : $obj->ref;
2129 $this->date_creation = $this->db->jdate($obj->datec);
2130 $this->date_modification = $this->
db->jdate($obj->date_modification);
2133 $this->
db->free($result);
2151 $sql =
"SELECT cd.rowid";
2152 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as cd";
2153 $sql .=
" WHERE fk_contrat =".((int) $this->
id);
2155 $sql .=
" AND statut = ".((int) $status);
2158 dol_syslog(get_class($this).
"::array_detail()", LOG_DEBUG);
2159 $resql = $this->
db->query(
$sql);
2161 $num = $this->
db->num_rows($resql);
2164 $obj = $this->
db->fetch_object($resql);
2165 $tab[$i] = $obj->rowid;
2170 $this->error = $this->
db->error();
2184 public function getListOfContracts($option =
'all', $status = [], $product_categories = [], $line_status = [])
2188 $sql =
"SELECT c.rowid";
2189 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2190 if (!empty($product_categories)) {
2191 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"contratdet as cd ON cd.fk_contrat = c.rowid";
2192 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"categorie_product as cp ON cp.fk_product = cd.fk_product AND cp.fk_categorie IN (".$this->
db->sanitize(implode(
', ', $product_categories)).
")";
2194 $sql .=
" WHERE c.fk_soc =".((int) $this->socid);
2195 $sql .= ($option ==
'others') ?
" AND c.rowid <> ".((
int) $this->id) :
"";
2196 $sql .= (!empty($status)) ?
" AND c.statut IN (".$this->
db->sanitize(implode(
', ', $status)).
")" :
"";
2197 $sql .= (!empty($line_status)) ?
" AND cd.statut IN (".$this->
db->sanitize(implode(
', ', $line_status)).
")" :
"";
2198 $sql .=
" GROUP BY c.rowid";
2200 dol_syslog(get_class($this).
"::getOtherContracts()", LOG_DEBUG);
2201 $resql = $this->
db->query(
$sql);
2203 $num = $this->
db->num_rows($resql);
2206 $obj = $this->
db->fetch_object($resql);
2208 $contrat->fetch($obj->rowid);
2209 $tab[$contrat->id] = $contrat;
2214 $this->error = $this->
db->lasterror();
2231 global $conf, $langs;
2233 $this->from =
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2234 $this->from .=
", ".MAIN_DB_PREFIX.
"contratdet as cd";
2235 $this->from .=
", ".MAIN_DB_PREFIX.
"societe as s";
2236 if (empty($user->rights->societe->client->voir) && !$user->socid) {
2237 $this->from .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2240 if ($mode ==
'inactive') {
2241 $sql =
"SELECT cd.rowid, cd.date_ouverture_prevue as datefin";
2242 $sql .= $this->from;
2243 $sql .=
" WHERE c.statut = 1";
2244 $sql .=
" AND c.rowid = cd.fk_contrat";
2245 $sql .=
" AND cd.statut = 0";
2246 } elseif ($mode ==
'expired') {
2247 $sql =
"SELECT cd.rowid, cd.date_fin_validite as datefin";
2248 $sql .= $this->from;
2249 $sql .=
" WHERE c.statut = 1";
2250 $sql .=
" AND c.rowid = cd.fk_contrat";
2251 $sql .=
" AND cd.statut = 4";
2252 $sql .=
" AND cd.date_fin_validite < '".$this->db->idate(
dol_now()).
"'";
2253 } elseif ($mode ==
'active') {
2254 $sql =
"SELECT cd.rowid, cd.date_fin_validite as datefin";
2255 $sql .= $this->from;
2256 $sql .=
" WHERE c.statut = 1";
2257 $sql .=
" AND c.rowid = cd.fk_contrat";
2258 $sql .=
" AND cd.statut = 4";
2262 $sql .=
" AND c.fk_soc = s.rowid";
2263 $sql .=
" AND c.entity = ".((int) $conf->entity);
2265 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
2267 if (empty($user->rights->societe->client->voir) && !$user->socid) {
2268 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2271 $resql = $this->
db->query(
$sql);
2273 $langs->load(
"contracts");
2276 if ($mode ==
'inactive') {
2277 $warning_delay = $conf->contrat->services->inactifs->warning_delay;
2278 $label = $langs->trans(
"BoardNotActivatedServices");
2279 $labelShort = $langs->trans(
"BoardNotActivatedServicesShort");
2280 $url = DOL_URL_ROOT.
'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=0&sortfield=cd.date_fin_validite&sortorder=asc';
2281 } elseif ($mode ==
'expired') {
2282 $warning_delay = $conf->contrat->services->expires->warning_delay;
2283 $url = DOL_URL_ROOT.
'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&filter=expired&sortfield=cd.date_fin_validite&sortorder=asc';
2284 $label = $langs->trans(
"BoardExpiredServices");
2285 $labelShort = $langs->trans(
"BoardExpiredServicesShort");
2287 $warning_delay = $conf->contrat->services->expires->warning_delay;
2288 $url = DOL_URL_ROOT.
'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&sortfield=cd.date_fin_validite&sortorder=asc';
2291 $label = $langs->trans(
"BoardRunningServices");
2292 $labelShort = $langs->trans(
"BoardRunningServicesShort");
2296 $response->warning_delay = $warning_delay / 60 / 60 / 24;
2297 $response->label = $label;
2298 $response->labelShort = $labelShort;
2299 $response->url = $url;
2302 while ($obj = $this->
db->fetch_object($resql)) {
2303 $response->nbtodo++;
2305 if ($obj->datefin && $this->db->jdate($obj->datefin) < ($now - $warning_delay)) {
2306 $response->nbtodolate++;
2313 $this->error = $this->
db->error();
2327 global $conf, $user;
2329 $this->nb = array();
2332 $sql =
"SELECT count(c.rowid) as nb";
2333 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2334 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON c.fk_soc = s.rowid";
2335 if (empty($user->rights->societe->client->voir) && !$user->socid) {
2336 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
2337 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
2340 $sql .=
" ".$clause.
" c.entity = ".$conf->entity;
2342 $resql = $this->
db->query(
$sql);
2344 while ($obj = $this->
db->fetch_object($resql)) {
2345 $this->nb[
"contracts"] = $obj->nb;
2347 $this->
db->free($resql);
2351 $this->error = $this->
db->error();
2389 global $user, $langs, $conf;
2394 $sql =
"SELECT rowid";
2395 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
2396 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
2397 $sql .=
" AND tosell = 1";
2398 $sql .= $this->
db->plimit(100);
2400 $resql = $this->
db->query(
$sql);
2402 $num_prods = $this->
db->num_rows($resql);
2404 while ($i < $num_prods) {
2406 $row = $this->
db->fetch_row($resql);
2407 $prodids[$i] = $row[0];
2413 $this->specimen = 1;
2415 $this->
ref =
'SPECIMEN';
2416 $this->ref_customer =
'SPECIMENCUST';
2417 $this->ref_supplier =
'SPECIMENSUPP';
2420 $this->date_creation = (
dol_now() - 3600 * 24 * 7);
2421 $this->date_contrat =
dol_now();
2422 $this->commercial_signature_id = 1;
2423 $this->commercial_suivi_id = 1;
2424 $this->note_private =
'This is a comment (private)';
2425 $this->note_public =
'This is a comment (public)';
2426 $this->fk_projet = 0;
2430 while ($xnbp < $nbp) {
2433 $line->subprice = 100;
2435 $line->tva_tx = 19.6;
2436 $line->remise_percent = 10;
2437 $line->total_ht = 90;
2438 $line->total_ttc = 107.64;
2439 $line->total_tva = 17.64;
2440 $line->date_start =
dol_now() - 500000;
2441 $line->date_start_real =
dol_now() - 200000;
2442 $line->date_end =
dol_now() + 500000;
2443 $line->date_end_real =
dol_now() - 100000;
2444 if ($num_prods > 0) {
2445 $prodid = mt_rand(1, $num_prods);
2446 $line->fk_product = $prodids[$prodid];
2448 $this->lines[$xnbp] = $line;
2475 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
2477 global $conf, $langs;
2482 if (!empty($this->model_pdf)) {
2483 $modele = $this->model_pdf;
2484 } elseif (!empty($this->modelpdf)) {
2485 $modele = $this->modelpdf;
2486 } elseif (!empty($conf->global->CONTRACT_ADDON_PDF)) {
2487 $modele = $conf->global->CONTRACT_ADDON_PDF;
2491 if (empty($modele)) {
2494 $langs->load(
"contracts");
2495 $outputlangs->load(
"products");
2497 $modelpath =
"core/modules/contract/doc/";
2498 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2546 global $db, $langs, $conf, $hookmanager, $extrafields;
2552 $this->
fetch($this->
id);
2555 $clonedObj = clone $this;
2556 $clonedObj->socid = $socid;
2562 $objsoc->fetch($clonedObj->socid);
2565 $clonedObj->statut = 0;
2567 if (is_array($clonedObj->array_options) && count($clonedObj->array_options) > 0) {
2568 $extrafields->fetch_name_optionals_label($this->table_element);
2569 foreach ($clonedObj->array_options as $key => $option) {
2570 $shortkey = preg_replace(
'/options_/',
'', $key);
2572 if (!empty($extrafields->attributes[$this->element][
'unique'][$shortkey])) {
2574 unset($clonedObj->array_options[$key]);
2579 if (empty($conf->global->CONTRACT_ADDON) || !is_readable(DOL_DOCUMENT_ROOT.
"/core/modules/contract/".$conf->global->CONTRACT_ADDON.
".php")) {
2580 $this->error =
'ErrorSetupNotComplete';
2586 require_once DOL_DOCUMENT_ROOT.
"/core/modules/contract/".$conf->global->CONTRACT_ADDON.
'.php';
2587 $obj = $conf->global->CONTRACT_ADDON;
2588 $modContract =
new $obj();
2589 $clonedObj->ref = $modContract->getNextValue($objsoc, $clonedObj);
2592 foreach ($this->lines as $line) {
2593 $line->fetch_optionals($line->id);
2597 $clonedObj->context[
'createfromclone'] =
'createfromclone';
2598 $result = $clonedObj->create($user);
2601 $this->error = $clonedObj->error;
2602 $this->errors[] = $clonedObj->error;
2605 if ($this->socid == $clonedObj->socid) {
2606 if ($clonedObj->copy_linked_contact($this,
'external') < 0) {
2613 foreach ($this->lines as $line) {
2614 $result = $clonedObj->addline($line->description, $line->subprice, $line->qty, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, $line->fk_product, $line->remise_percent, $line->date_ouverture, $line->date_cloture,
'HT', 0, $line->info_bits, $line->fk_fournprice, $line->pa_ht, $line->array_options, $line->fk_unit, $line->rang);
2617 $this->error = $clonedObj->error;
2618 $this->errors[] = $clonedObj->error;
2625 if (is_object($hookmanager)) {
2626 $parameters = array(
2628 'clonedObj' => $clonedObj
2631 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $clonedObj, $action);
2639 unset($clonedObj->context[
'createfromclone']);
2643 $this->
db->commit();
2644 return $clonedObj->id;
2646 $this->
db->rollback();
2663 global $langs, $user;
2665 $langs->load(
"agenda");
2675 $contractlineprocessed = array();
2676 $contractignored = array();
2677 $contracterror = array();
2681 $sql =
'SELECT c.rowid, c.ref_customer, cd.rowid as lid, cd.date_fin_validite, p.duration';
2682 $sql.=
' FROM '.MAIN_DB_PREFIX.
'contrat as c, '.MAIN_DB_PREFIX.
'contratdet as cd';
2683 $sql.=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON p.rowid = cd.fk_product';
2684 $sql.=
' WHERE cd.fk_contrat = c.rowid';
2685 $sql.=
" AND date_format(cd.date_fin_validite, '%Y-%m-%d') <= date_format('".$this->db->idate($enddatetoscan).
"', '%Y-%m-%d')";
2686 $sql.=
" AND cd.statut = 4";
2687 if ($thirdparty_id > 0)
$sql.=
" AND c.fk_soc = ".((int) $thirdparty_id);
2690 $resql = $this->
db->query(
$sql);
2692 $num = $this->
db->num_rows($resql);
2694 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
2698 $obj = $this->
db->fetch_object($resql);
2700 if (!empty($contractlineprocessed[$obj->lid]) || !empty($contractignored[$obj->rowid]) || !empty($contracterror[$obj->rowid])) {
2706 $object->fetch($obj->rowid);
2707 $object->fetch_thirdparty();
2709 if ($object->id <= 0) {
2711 $this->errors[] =
'Failed to load contract with id='.$obj->rowid;
2715 dol_syslog(
"* Process contract line in doRenewalContracts for contract id=".$object->id.
" ref=".$object->ref.
" ref_customer=".$object->ref_customer.
" contract line id=".$obj->lid);
2718 $expirationdate = $this->
db->jdate($obj->date_fin_validite);
2719 $duration_value = preg_replace(
'/[^0-9]/',
'', $obj->duration);
2720 $duration_unit = preg_replace(
'/\d/',
'', $obj->duration);
2724 $object->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 1);
2726 if (is_array($object->linkedObjects[
'facture']) && count($object->linkedObjects[
'facture']) > 0) {
2727 usort($object->linkedObjects[
'facture'],
"cmp");
2730 $someinvoicenotpaid=0;
2731 foreach ($object->linkedObjects[
'facture'] as $idinvoice => $invoice) {
2734 if (empty($invoice->paye)) {
2735 $someinvoicenotpaid++;
2738 if ($someinvoicenotpaid) {
2739 $this->output .=
'Contract '.$object->ref.
' is qualified for renewal but there is '.$someinvoicenotpaid.
' invoice(s) unpayed so we cancel renewal'.
"\n";
2740 $contractignored[$object->id]=$object->ref;
2745 if ($expirationdate && $expirationdate < $enddatetoscan) {
2746 dol_syslog(
"Define the newdate of end of services from expirationdate=".$expirationdate);
2747 $newdate = $expirationdate;
2749 while ($newdate < $enddatetoscan && $protecti < 1000) {
2754 if ($protecti < 1000) {
2760 $errorforlocaltransaction = 0;
2762 $label =
'Renewal of contrat '.$object->ref.
' line '.$obj->lid;
2763 $comment =
'Renew date of contract '.$object->ref.
' line '.$obj->lid.
' by doAutoRenewContracts';
2765 $sqlupdate =
'UPDATE '.MAIN_DB_PREFIX.
"contratdet SET date_fin_validite = '".$this->
db->idate($newdate).
"'";
2766 $sqlupdate.=
' WHERE rowid = '.((int) $obj->lid);
2767 $resqlupdate = $this->
db->query($sqlupdate);
2769 $contractlineprocessed[$obj->lid]=$object->ref;
2771 $actioncode =
'RENEW_CONTRACT';
2776 $actioncomm->type_code =
'AC_OTH_AUTO';
2777 $actioncomm->code =
'AC_'.$actioncode;
2778 $actioncomm->label = $label;
2779 $actioncomm->datep = $now;
2780 $actioncomm->datef = $now;
2781 $actioncomm->percentage = -1;
2782 $actioncomm->socid = $object->thirdparty->id;
2783 $actioncomm->authorid = $user->id;
2784 $actioncomm->userownerid = $user->id;
2785 $actioncomm->fk_element = $object->id;
2786 $actioncomm->elementtype =
'contract';
2787 $actioncomm->note_private = $comment;
2789 $ret = $actioncomm->create($user);
2791 $contracterror[$object->id]=$object->ref;
2794 $errorforlocaltransaction++;
2795 $this->error = $this->
db->lasterror();
2798 if (! $errorforlocaltransaction) {
2799 $this->
db->commit();
2801 $this->
db->rollback();
2805 $this->error =
"Bad value for newdate in doAutoRenewContracts - expirationdate=".$expirationdate.
" enddatetoscan=".$enddatetoscan.
" duration_value=".$duration_value.
" duration_unit=".$duration_value;
2814 $this->error = $this->
db->lasterror();
2817 $this->output .= count($contractlineprocessed).
' contract line(s) with end date before '.
dol_print_date($enddatetoscan,
'day').
' were renewed'.(count($contractlineprocessed)>0 ?
' : '.join(
',', $contractlineprocessed) :
'');
2819 return ($error ? 1: 0);
2832 $return =
'<div class="box-flex-item box-flex-grow-zero">';
2833 $return .=
'<div class="info-box info-box-sm">';
2834 $return .=
'<span class="info-box-icon bg-infobox-action">';
2837 $return .=
'</span>';
2838 $return .=
'<div class="info-box-content">';
2839 $return .=
'<span class="info-box-ref">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl() : $this->ref).
'</span>';
2840 if (property_exists($this,
'societe')) {
2841 $return .=
'<br><span class="info-box-label ">'.$this->societe.
'</span>';
2843 if (property_exists($this,
'date_contrat')) {
2844 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateContract").
' : </span><span class="info-box-label">'.
dol_print_date($this->date_contrat,
'day').
'</span>';
2846 if (method_exists($this,
'getLibStatut')) {
2847 $return .=
'<br><div class="info-box-status margintoponly">'.$this->getLibStatut(5).
'</div>';
2849 $return .=
'</div>';
2850 $return .=
'</div>';
2851 $return .=
'</div>';
2865 public $element =
'contratdet';
2870 public $table_element =
'contratdet';
2876 public $element_for_permission =
'contrat';
2918 public $description;
2920 public $product_type;
2921 public $product_ref;
2922 public $product_label;
2924 public $date_commande;
2927 public $date_start_real;
2929 public $date_end_real;
2932 public $vat_src_code;
2933 public $localtax1_tx;
2934 public $localtax2_tx;
2935 public $localtax1_type;
2936 public $localtax2_type;
2938 public $remise_percent;
2944 public $fk_remise_except;
2959 public $total_localtax1;
2960 public $total_localtax2;
2966 public $fk_fournprice;
2975 public $fk_user_author;
2980 public $fk_user_ouverture;
2985 public $fk_user_cloture;
2987 public $commentaire;
2996 const STATUS_INITIAL = 0;
2997 const STATUS_OPEN = 4;
2998 const STATUS_CLOSED = 5;
3005 public $fields = array(
3006 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
3007 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>30,
'index'=>1),
3008 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>35),
3009 'qty' =>array(
'type'=>
'integer',
'label'=>
'Quantity',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'position'=>35,
'isameasure'=>1),
3010 'total_ht' =>array(
'type'=>
'integer',
'label'=>
'AmountHT',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>36,
'isameasure'=>1),
3011 'total_tva' =>array(
'type'=>
'integer',
'label'=>
'AmountVAT',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>37,
'isameasure'=>1),
3012 'total_ttc' =>array(
'type'=>
'integer',
'label'=>
'AmountTTC',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>38,
'isameasure'=>1),
3015 'fk_contrat' =>array(
'type'=>
'integer:Contrat:contrat/class/contrat.class.php',
'label'=>
'Contract',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>70),
3016 'fk_product' =>array(
'type'=>
'integer:Product:product/class/product.class.php:1',
'label'=>
'Product',
'enabled'=>1,
'visible'=>-1,
'position'=>75),
3018 'note_private' =>array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>105),
3019 'note_public' =>array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>110),
3023 'fk_user_ouverture' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserStartingService',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>135),
3024 'fk_user_cloture' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserClosingService',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>135),
3025 'statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Statut',
'enabled'=>1,
'visible'=>-1,
'position'=>500,
'arrayofkeyval'=>array(0=>
'Draft', 4=>
'Open', 5=>
'Closed')),
3026 'rang' =>array(
'type'=>
'integer',
'label'=>
'Rank',
'enabled'=>1,
'visible'=>0,
'position'=>500,
'default' =>0)
3050 return $this->
LibStatut($this->statut, $mode, ((!empty($this->date_end)) ? ($this->date_end <
dol_now() ? 1 : 0) : -1));
3063 public static function LibStatut($status, $mode, $expired = -1, $moreatt =
'')
3067 $langs->load(
"contracts");
3069 if ($status == self::STATUS_INITIAL) {
3070 $labelStatus = $langs->transnoentities(
"ServiceStatusInitial");
3071 $labelStatusShort = $langs->transnoentities(
"ServiceStatusInitial");
3072 } elseif ($status == self::STATUS_OPEN && $expired == -1) {
3073 $labelStatus = $langs->transnoentities(
"ServiceStatusRunning");
3074 $labelStatusShort = $langs->transnoentities(
"ServiceStatusRunning");
3075 } elseif ($status == self::STATUS_OPEN && $expired == 0) {
3076 $labelStatus = $langs->transnoentities(
"ServiceStatusNotLate");
3077 $labelStatusShort = $langs->transnoentities(
"ServiceStatusNotLateShort");
3078 } elseif ($status == self::STATUS_OPEN && $expired == 1) {
3079 $labelStatus = $langs->transnoentities(
"ServiceStatusLate");
3080 $labelStatusShort = $langs->transnoentities(
"ServiceStatusLateShort");
3081 } elseif ($status == self::STATUS_CLOSED) {
3082 $labelStatus = $langs->transnoentities(
"ServiceStatusClosed");
3083 $labelStatusShort = $langs->transnoentities(
"ServiceStatusClosed");
3086 $statusType =
'status'.$status;
3087 if ($status == self::STATUS_OPEN && $expired == 1) {
3088 $statusType =
'status1';
3090 if ($status == self::STATUS_CLOSED) {
3091 $statusType =
'status6';
3094 $params = array(); $reg = array();
3095 if (preg_match(
'/class="(.*)"/', $moreatt, $reg)) {
3096 $params = array(
'badgeParams'=>array(
'css' => $reg[1]));
3098 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode,
'', $params);
3109 global $conf, $langs, $user;
3112 $datas[
'label'] = $langs->trans(
"ShowContractOfService").
': '.$this->label;
3113 if (empty($datas[
'label'])) {
3114 $datas[
'label'] = $this->description;
3132 $label = $langs->trans(
"ShowContractOfService").
': '.$this->label;
3133 if (empty($label)) {
3134 $label = $this->description;
3136 $classfortooltip =
'classfortooltip';
3140 'id' => $this->fk_contrat,
3141 'objecttype' => $this->element,
3143 $classfortooltip =
'classforajaxtooltip';
3144 $dataparams =
' data-params='.json_encode($params);
3147 $link =
'<a href="'.DOL_URL_ROOT.
'/contrat/card.php?id='.$this->fk_contrat;
3148 $link =
'"'.$dataparams.
' title="'.
dol_escape_htmltag($label, 1).
'" class="'.$classfortooltip.
'">';
3152 if ($this->
type == 0) {
3157 $result .= ($link.img_object($label, $picto, $dataparams.
' class="'.$classfortooltip.
'"').$linkend);
3159 if ($withpicto && $withpicto != 2) {
3162 if ($withpicto != 2) {
3163 $result .= $link.($this->product_ref ? $this->product_ref.
' ' :
'').($this->label ? $this->label : $this->
description).$linkend;
3178 if (empty($id) && empty($ref)) {
3183 $sql .=
" t.rowid,";
3185 $sql .=
" t.fk_contrat,";
3186 $sql .=
" t.fk_product,";
3187 $sql .=
" t.statut,";
3188 $sql .=
" t.label,";
3189 $sql .=
" p.ref as product_ref,";
3190 $sql .=
" p.label as product_label,";
3191 $sql .=
" p.description as product_desc,";
3192 $sql .=
" p.fk_product_type as product_type,";
3193 $sql .=
" t.description,";
3194 $sql .=
" t.date_commande,";
3195 $sql .=
" t.date_ouverture_prevue as date_start,";
3196 $sql .=
" t.date_ouverture as date_start_real,";
3197 $sql .=
" t.date_fin_validite as date_end,";
3198 $sql .=
" t.date_cloture as date_end_real,";
3199 $sql .=
" t.tva_tx,";
3200 $sql .=
" t.vat_src_code,";
3201 $sql .=
" t.localtax1_tx,";
3202 $sql .=
" t.localtax2_tx,";
3203 $sql .=
" t.localtax1_type,";
3204 $sql .=
" t.localtax2_type,";
3206 $sql .=
" t.remise_percent,";
3207 $sql .=
" t.remise,";
3208 $sql .=
" t.fk_remise_except,";
3209 $sql .=
" t.subprice,";
3210 $sql .=
" t.price_ht,";
3211 $sql .=
" t.total_ht,";
3212 $sql .=
" t.total_tva,";
3213 $sql .=
" t.total_localtax1,";
3214 $sql .=
" t.total_localtax2,";
3215 $sql .=
" t.total_ttc,";
3216 $sql .=
" t.fk_product_fournisseur_price as fk_fournprice,";
3217 $sql .=
" t.buy_price_ht as pa_ht,";
3218 $sql .=
" t.info_bits,";
3219 $sql .=
" t.fk_user_author,";
3220 $sql .=
" t.fk_user_ouverture,";
3221 $sql .=
" t.fk_user_cloture,";
3222 $sql .=
" t.commentaire,";
3223 $sql .=
" t.fk_unit,";
3225 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as t LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = t.fk_product";
3227 $sql .=
" WHERE t.rowid = ".((int) $id);
3230 $sql .=
" WHERE t.rowid = '".$this->db->escape($ref).
"'";
3233 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
3234 $resql = $this->
db->query(
$sql);
3236 if ($this->
db->num_rows($resql)) {
3237 $obj = $this->
db->fetch_object($resql);
3239 $this->
id = $obj->rowid;
3240 $this->
ref = $obj->rowid;
3242 $this->tms = $this->
db->jdate($obj->tms);
3243 $this->fk_contrat = $obj->fk_contrat;
3244 $this->fk_product = $obj->fk_product;
3245 $this->statut = $obj->statut;
3246 $this->product_ref = $obj->product_ref;
3247 $this->product_label = $obj->product_label;
3248 $this->product_description = $obj->product_description;
3249 $this->product_type = $obj->product_type;
3250 $this->label = $obj->label;
3252 $this->date_commande = $this->
db->jdate($obj->date_commande);
3254 $this->date_start = $this->
db->jdate($obj->date_start);
3255 $this->date_start_real = $this->
db->jdate($obj->date_start_real);
3256 $this->date_end = $this->
db->jdate($obj->date_end);
3257 $this->date_end_real = $this->
db->jdate($obj->date_end_real);
3264 $this->tva_tx = $obj->tva_tx;
3265 $this->vat_src_code = $obj->vat_src_code;
3266 $this->localtax1_tx = $obj->localtax1_tx;
3267 $this->localtax2_tx = $obj->localtax2_tx;
3268 $this->localtax1_type = $obj->localtax1_type;
3269 $this->localtax2_type = $obj->localtax2_type;
3270 $this->qty = $obj->qty;
3271 $this->remise_percent = $obj->remise_percent;
3272 $this->fk_remise_except = $obj->fk_remise_except;
3273 $this->subprice = $obj->subprice;
3274 $this->price_ht = $obj->price_ht;
3275 $this->total_ht = $obj->total_ht;
3276 $this->total_tva = $obj->total_tva;
3277 $this->total_localtax1 = $obj->total_localtax1;
3278 $this->total_localtax2 = $obj->total_localtax2;
3279 $this->total_ttc = $obj->total_ttc;
3280 $this->info_bits = $obj->info_bits;
3281 $this->fk_user_author = $obj->fk_user_author;
3282 $this->fk_user_ouverture = $obj->fk_user_ouverture;
3283 $this->fk_user_cloture = $obj->fk_user_cloture;
3284 $this->commentaire = $obj->commentaire;
3285 $this->fk_fournprice = $obj->fk_fournprice;
3287 $marginInfos =
getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
3288 $this->pa_ht = $marginInfos[0];
3289 $this->fk_unit = $obj->fk_unit;
3291 $this->rang = $obj->rang;
3296 $this->
db->free($resql);
3300 $this->error =
"Error ".$this->db->lasterror();
3313 public function update($user, $notrigger = 0)
3315 global $conf, $langs, $mysoc;
3320 $this->fk_contrat = (int) $this->fk_contrat;
3321 $this->fk_product = (int) $this->fk_product;
3322 $this->statut = (int) $this->statut;
3323 $this->label = trim($this->label);
3325 $this->vat_src_code = trim($this->vat_src_code);
3326 $this->tva_tx = trim($this->tva_tx);
3327 $this->localtax1_tx = trim($this->localtax1_tx);
3328 $this->localtax2_tx = trim($this->localtax2_tx);
3329 $this->qty = trim($this->qty);
3330 $this->remise_percent = trim($this->remise_percent);
3331 $this->fk_remise_except = (int) $this->fk_remise_except;
3332 $this->subprice =
price2num($this->subprice);
3333 $this->price_ht =
price2num($this->price_ht);
3334 $this->total_ht = trim($this->total_ht);
3335 $this->total_tva = trim($this->total_tva);
3336 $this->total_localtax1 = trim($this->total_localtax1);
3337 $this->total_localtax2 = trim($this->total_localtax2);
3338 $this->total_ttc = trim($this->total_ttc);
3339 $this->info_bits = trim($this->info_bits);
3340 $this->fk_user_author = (int) $this->fk_user_author;
3341 $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
3342 $this->fk_user_cloture = (int) $this->fk_user_cloture;
3343 $this->commentaire = trim($this->commentaire);
3344 $this->rang = (int) $this->rang;
3346 if (empty($this->price_ht)) {
3347 $this->price_ht = 0;
3349 if (empty($this->total_ht)) {
3350 $this->total_ht = 0;
3352 if (empty($this->total_tva)) {
3353 $this->total_tva = 0;
3355 if (empty($this->total_ttc)) {
3356 $this->total_ttc = 0;
3358 if (empty($this->localtax1_tx)) {
3359 $this->localtax1_tx = 0;
3361 if (empty($this->localtax2_tx)) {
3362 $this->localtax2_tx = 0;
3364 if (empty($this->remise_percent)) {
3365 $this->remise_percent = 0;
3368 if (empty($this->date_start)) {
3369 $this->date_start = $this->date_start;
3371 if (empty($this->date_start_real)) {
3372 $this->date_start_real = $this->date_start_real;
3374 if (empty($this->date_end)) {
3375 $this->date_end = $this->date_end;
3377 if (empty($this->date_end_real)) {
3378 $this->date_end_real = $this->date_end_real;
3387 $tabprice =
calcul_price_total($this->qty, $this->price_ht, $this->remise_percent, $this->tva_tx, $this->localtax1_tx, $this->localtax2_tx, 0,
'HT', 0, 1, $mysoc, $localtaxes_type);
3388 $this->total_ht = $tabprice[0];
3389 $this->total_tva = $tabprice[1];
3390 $this->total_ttc = $tabprice[2];
3391 $this->total_localtax1 = $tabprice[9];
3392 $this->total_localtax2 = $tabprice[10];
3394 if (empty($this->pa_ht)) {
3399 if ($this->pa_ht == 0) {
3400 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
3403 $this->pa_ht = $result;
3408 if (empty($this->oldcopy)) {
3415 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
3416 $sql .=
" fk_contrat = ".((int) $this->fk_contrat).
",";
3417 $sql .=
" fk_product = ".($this->fk_product ? ((int) $this->fk_product) :
'null').
",";
3418 $sql .=
" statut = ".((int) $this->statut).
",";
3419 $sql .=
" label = '".$this->db->escape($this->label).
"',";
3420 $sql .=
" description = '".$this->db->escape($this->
description).
"',";
3421 $sql .=
" date_commande = ".($this->date_commande !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
"null").
",";
3422 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
3423 $sql .=
" date_ouverture = ".($this->date_start_real !=
'' ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
3424 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
3425 $sql .=
" date_cloture = ".($this->date_end_real !=
'' ?
"'".$this->db->idate($this->date_end_real).
"'" :
"null").
",";
3426 $sql .=
" vat_src_code = '".$this->db->escape($this->vat_src_code).
"',";
3427 $sql .=
" tva_tx = ".price2num($this->tva_tx).
",";
3428 $sql .=
" localtax1_tx = ".price2num($this->localtax1_tx).
",";
3429 $sql .=
" localtax2_tx = ".price2num($this->localtax2_tx).
",";
3430 $sql .=
" qty = ".price2num($this->qty).
",";
3431 $sql .=
" remise_percent = ".price2num($this->remise_percent).
",";
3432 $sql .=
" remise = ".($this->remise ?
price2num($this->remise) :
"null").
",";
3433 $sql .=
" fk_remise_except = ".($this->fk_remise_except > 0 ? $this->fk_remise_except :
"null").
",";
3434 $sql .=
" subprice = ".($this->subprice !=
'' ? $this->subprice :
"null").
",";
3435 $sql .=
" price_ht = ".($this->price_ht !=
'' ? $this->price_ht :
"null").
",";
3436 $sql .=
" total_ht = ".$this->total_ht.
",";
3437 $sql .=
" total_tva = ".$this->total_tva.
",";
3438 $sql .=
" total_localtax1 = ".$this->total_localtax1.
",";
3439 $sql .=
" total_localtax2 = ".$this->total_localtax2.
",";
3440 $sql .=
" total_ttc = ".$this->total_ttc.
",";
3441 $sql .=
" fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"NULL").
",";
3442 $sql .=
" buy_price_ht = '".price2num($this->pa_ht).
"',";
3443 $sql .=
" info_bits = '".$this->db->escape($this->info_bits).
"',";
3444 $sql .=
" fk_user_author = ".($this->fk_user_author >= 0 ? $this->fk_user_author :
"NULL").
",";
3445 $sql .=
" fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? $this->fk_user_ouverture :
"NULL").
",";
3446 $sql .=
" fk_user_cloture = ".($this->fk_user_cloture > 0 ? $this->fk_user_cloture :
"NULL").
",";
3447 $sql .=
" commentaire = '".$this->db->escape($this->commentaire).
"',";
3448 $sql .=
" fk_unit = ".(!$this->fk_unit ?
'NULL' : $this->fk_unit).
",";
3449 $sql .=
" rang = ".(empty($this->rang) ?
'0' : (int) $this->rang);
3450 $sql .=
" WHERE rowid = ".((int) $this->
id);
3452 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3453 $resql = $this->
db->query(
$sql);
3455 $this->error =
"Error ".$this->db->lasterror();
3467 if (!$error && !empty($conf->global->CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES)) {
3468 dol_syslog(get_class($this).
"::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
3470 if ($this->date_start != $this->oldcopy->date_start) {
3471 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
3472 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
3473 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
3475 $resql = $this->
db->query(
$sql);
3478 $this->error =
"Error ".$this->db->lasterror();
3481 if ($this->date_end != $this->oldcopy->date_end) {
3482 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
3483 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
3484 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
3486 $resql = $this->
db->query(
$sql);
3489 $this->error =
"Error ".$this->db->lasterror();
3494 if (!$error && !$notrigger) {
3496 $result = $this->
call_trigger(
'LINECONTRACT_MODIFY', $user);
3499 $this->
db->rollback();
3505 $this->
db->commit();
3508 $this->
db->rollback();
3509 $this->errors[] = $this->error;
3528 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
3529 $sql .=
" total_ht=".price2num($this->total_ht,
'MT');
3530 $sql .=
",total_tva=".price2num($this->total_tva,
'MT');
3531 $sql .=
",total_localtax1=".price2num($this->total_localtax1,
'MT');
3532 $sql .=
",total_localtax2=".price2num($this->total_localtax2,
'MT');
3533 $sql .=
",total_ttc=".price2num($this->total_ttc,
'MT');
3534 $sql .=
" WHERE rowid = ".((int) $this->
id);
3536 dol_syslog(get_class($this).
"::update_total", LOG_DEBUG);
3538 $resql = $this->
db->query(
$sql);
3540 $this->
db->commit();
3543 $this->error = $this->
db->error();
3544 $this->
db->rollback();
3558 global $conf, $user;
3563 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
3564 $sql .=
" (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
3565 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
3566 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
3567 $sql .=
" info_bits,";
3569 $sql .=
" price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
3570 if ($this->date_start > 0) {
3571 $sql .=
",date_ouverture_prevue";
3573 if ($this->date_end > 0) {
3574 $sql .=
",date_fin_validite";
3576 $sql .=
") VALUES ($this->fk_contrat, '', '".$this->db->escape($this->
description).
"',";
3577 $sql .= ($this->fk_product > 0 ? $this->fk_product :
"null").
",";
3578 $sql .=
" '".$this->db->escape($this->qty).
"',";
3579 $sql .=
" '".$this->db->escape($this->vat_src_code).
"',";
3580 $sql .=
" '".$this->db->escape($this->tva_tx).
"',";
3581 $sql .=
" '".$this->db->escape($this->localtax1_tx).
"',";
3582 $sql .=
" '".$this->db->escape($this->localtax2_tx).
"',";
3583 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
3584 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
3585 $sql .=
" ".price2num($this->remise_percent).
",".
price2num($this->subprice).
",";
3587 $sql .=
" '".$this->db->escape($this->info_bits).
"',";
3588 $sql .=
" ".(empty($this->rang) ?
'0' : (int) $this->rang).
",";
3589 $sql .=
" ".price2num($this->price_ht).
",".
price2num($this->remise).
",";
3590 if ($this->fk_fournprice > 0) {
3591 $sql .=
' '.((int) $this->fk_fournprice).
',';
3595 if ($this->pa_ht > 0) {
3600 if ($this->date_start > 0) {
3601 $sql .=
",'".$this->db->idate($this->date_start).
"'";
3603 if ($this->date_end > 0) {
3604 $sql .=
",'".$this->db->idate($this->date_end).
"'";
3608 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
3610 $resql = $this->
db->query(
$sql);
3612 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'contratdet');
3618 $this->
db->rollback();
3625 $result = $this->
call_trigger(
'LINECONTRACT_INSERT', $user);
3627 $this->
db->rollback();
3633 $this->
db->commit();
3636 $this->
db->rollback();
3637 $this->error = $this->
db->error().
" sql=".
$sql;
3652 public function active_line($user, $date, $date_end =
'', $comment =
'')
3655 global $langs, $conf;
3661 $this->statut = ContratLigne::STATUS_OPEN;
3662 $this->date_start_real = $date;
3663 $this->date_end = $date_end;
3664 $this->fk_user_ouverture = $user->id;
3665 $this->date_end_real =
null;
3666 $this->commentaire = $comment;
3668 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) $this->statut).
",";
3669 $sql .=
" date_ouverture = ".(dol_strlen($this->date_start_real) != 0 ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
3670 if ($date_end >= 0) {
3671 $sql .=
" date_fin_validite = ".(dol_strlen($this->date_end) != 0 ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
3673 $sql .=
" fk_user_ouverture = ".((int) $this->fk_user_ouverture).
",";
3674 $sql .=
" date_cloture = null,";
3675 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
3676 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND (statut = ".ContratLigne::STATUS_INITIAL.
" OR statut = ".ContratLigne::STATUS_CLOSED.
")";
3678 dol_syslog(get_class($this).
"::active_line", LOG_DEBUG);
3679 $resql = $this->
db->query(
$sql);
3682 $result = $this->
call_trigger(
'LINECONTRACT_ACTIVATE', $user);
3689 $this->
db->commit();
3692 $this->
db->rollback();
3696 $this->error = $this->
db->lasterror();
3697 $this->
db->rollback();
3712 public function close_line($user, $date_end_real, $comment =
'', $notrigger = 0)
3715 global $langs, $conf;
3718 $this->date_cloture = $date_end_real;
3719 $this->date_end_real = $date_end_real;
3720 $this->fk_user_cloture = $user->id;
3721 $this->commentaire = $comment;
3729 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) ContratLigne::STATUS_CLOSED).
",";
3730 $sql .=
" date_cloture = '".$this->db->idate($date_end_real).
"',";
3731 $sql .=
" fk_user_cloture = ".((int) $user->id).
",";
3732 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
3733 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut = ".((int) ContratLigne::STATUS_OPEN);
3735 $resql = $this->
db->query(
$sql);
3739 $result = $this->
call_trigger(
'LINECONTRACT_CLOSE', $user);
3742 $this->
db->rollback();
3748 $this->
db->commit();
3751 $this->error = $this->
db->lasterror();
3752 $this->
db->rollback();