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.';
694 $obj = $this->db->fetch_object($resql);
696 $this->
id = $obj->rowid;
697 $this->
ref = (!isset($obj->ref) || !$obj->ref) ? $obj->rowid : $obj->ref;
698 $this->ref_customer = $obj->ref_customer;
699 $this->ref_supplier = $obj->ref_supplier;
700 $this->ref_ext = $obj->ref_ext;
701 $this->entity = $obj->entity;
702 $this->statut = $obj->statut;
704 $this->date_contrat = $this->db->jdate($obj->datecontrat);
705 $this->date_creation = $this->db->jdate($obj->datecontrat);
707 $this->user_author_id = $obj->fk_user_author;
709 $this->commercial_signature_id = $obj->fk_commercial_signature;
710 $this->commercial_suivi_id = $obj->fk_commercial_suivi;
712 $this->note_private = $obj->note_private;
713 $this->note_public = $obj->note_public;
714 $this->model_pdf = $obj->model_pdf;
715 $this->modelpdf = $obj->model_pdf;
717 $this->fk_projet = $obj->fk_project;
718 $this->fk_project = $obj->fk_project;
720 $this->socid = $obj->fk_soc;
721 $this->fk_soc = $obj->fk_soc;
722 $this->last_main_doc = $obj->last_main_doc;
723 $this->extraparams = (isset($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) :
null);
725 $this->db->free($resql);
732 if ($result >= 0 && !empty($this->table_element_line)) {
737 $this->error = $this->db->lasterror();
743 dol_syslog(get_class($this).
"::fetch Contract failed");
744 $this->error =
"Fetch contract failed";
748 dol_syslog(get_class($this).
"::fetch Contract not found");
749 $this->error =
"Contract not found";
753 dol_syslog(get_class($this).
"::fetch Error searching contract");
754 $this->error = $this->db->error();
768 public function fetch_lines($only_services = 0, $loadalsotranslation = 0)
771 global $langs, $conf;
773 $this->nbofservices = 0;
774 $this->nbofserviceswait = 0;
775 $this->nbofservicesopened = 0;
776 $this->nbofservicesexpired = 0;
777 $this->nbofservicesclosed = 0;
785 $this->lines = array();
789 $sql =
"SELECT p.label as product_label, p.description as product_desc, p.ref as product_ref, p.fk_product_type as product_type,";
790 $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,";
791 $sql .=
" d.total_ht,";
792 $sql .=
" d.total_tva,";
793 $sql .=
" d.total_localtax1,";
794 $sql .=
" d.total_localtax2,";
795 $sql .=
" d.total_ttc,";
796 $sql .=
" d.info_bits, d.fk_product,";
797 $sql .=
" d.date_ouverture_prevue as date_start,";
798 $sql .=
" d.date_ouverture as date_start_real,";
799 $sql .=
" d.date_fin_validite as date_end,";
800 $sql .=
" d.date_cloture as date_end_real,";
801 $sql .=
" d.fk_user_author,";
802 $sql .=
" d.fk_user_ouverture,";
803 $sql .=
" d.fk_user_cloture,";
804 $sql .=
" d.fk_unit,";
805 $sql .=
" d.product_type as type,";
807 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as d LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON d.fk_product = p.rowid";
808 $sql .=
" WHERE d.fk_contrat = ".((int) $this->
id);
809 if ($only_services == 1) {
810 $sql .=
" AND d.product_type = 1";
812 $sql .=
" ORDER by d.rang ASC";
814 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
815 $result = $this->db->query(
$sql);
817 $num = $this->db->num_rows($result);
821 $objp = $this->db->fetch_object($result);
825 $line->id = $objp->rowid;
826 $line->ref = $objp->rowid;
827 $line->fk_contrat = $objp->fk_contrat;
828 $line->desc = $objp->description;
829 $line->qty = $objp->qty;
830 $line->vat_src_code = $objp->vat_src_code;
831 $line->tva_tx = $objp->tva_tx;
832 $line->localtax1_tx = $objp->localtax1_tx;
833 $line->localtax2_tx = $objp->localtax2_tx;
834 $line->localtax1_type = $objp->localtax1_type;
835 $line->localtax2_type = $objp->localtax2_type;
836 $line->subprice = $objp->subprice;
837 $line->statut = $objp->statut;
838 $line->remise_percent = $objp->remise_percent;
839 $line->price_ht = $objp->price_ht;
840 $line->price = $objp->price_ht;
841 $line->total_ht = $objp->total_ht;
842 $line->total_tva = $objp->total_tva;
843 $line->total_localtax1 = $objp->total_localtax1;
844 $line->total_localtax2 = $objp->total_localtax2;
845 $line->total_ttc = $objp->total_ttc;
846 $line->fk_product = (($objp->fk_product > 0) ? $objp->fk_product : 0);
847 $line->info_bits = $objp->info_bits;
848 $line->type = $objp->type;
850 $line->fk_fournprice = $objp->fk_fournprice;
851 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_fournprice, $objp->pa_ht);
852 $line->pa_ht = $marginInfos[0];
854 $line->fk_user_author = $objp->fk_user_author;
855 $line->fk_user_ouverture = $objp->fk_user_ouverture;
856 $line->fk_user_cloture = $objp->fk_user_cloture;
857 $line->fk_unit = $objp->fk_unit;
859 $line->ref = $objp->product_ref;
860 $line->product_ref = $objp->product_ref;
861 $line->product_type = $objp->product_type;
862 $line->product_desc = $objp->product_desc;
863 $line->product_label = $objp->product_label;
865 $line->description = $objp->description;
867 $line->date_start = $this->db->jdate($objp->date_start);
868 $line->date_start_real = $this->db->jdate($objp->date_start_real);
869 $line->date_end = $this->db->jdate($objp->date_end);
870 $line->date_end_real = $this->db->jdate($objp->date_end_real);
881 $line->rang = $objp->rang;
885 $line->fetch_optionals();
888 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
889 $tmpproduct =
new Product($this->db);
890 $tmpproduct->fetch($objp->fk_product);
891 $tmpproduct->getMultiLangs();
893 $line->multilangs = $tmpproduct->multilangs;
896 $this->lines[$pos] = $line;
898 $this->lines_id_index_mapper[$line->id] = $pos;
903 if ($line->statut == ContratLigne::STATUS_INITIAL) {
904 $this->nbofserviceswait++;
906 if ($line->statut == ContratLigne::STATUS_OPEN && (empty($line->date_end) || $line->date_end >= $now)) {
907 $this->nbofservicesopened++;
909 if ($line->statut == ContratLigne::STATUS_OPEN && (!empty($line->date_end) && $line->date_end < $now)) {
910 $this->nbofservicesexpired++;
912 if ($line->statut == ContratLigne::STATUS_CLOSED) {
913 $this->nbofservicesclosed++;
916 $total_ttc += $objp->total_ttc;
917 $total_vat += $objp->total_tva;
918 $total_ht += $objp->total_ht;
923 $this->db->free($result);
925 dol_syslog(get_class($this).
"::Fetch Error when reading lines of contracts linked to products");
930 $this->nbofservices = count($this->lines);
931 $this->total_ttc =
price2num($total_ttc);
932 $this->total_tva =
price2num($total_vat);
946 global $conf, $langs, $mysoc;
950 if ($this->commercial_signature_id <= 0) {
951 $langs->load(
"commercial");
952 $this->error .= $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"SalesRepresentativeSignature"));
955 if ($this->commercial_suivi_id <= 0) {
956 $langs->load(
"commercial");
957 $this->error .= ($this->error ?
"<br>" :
'');
958 $this->error .= $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"SalesRepresentativeFollowUp"));
971 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contrat (datec, fk_soc, fk_user_author, date_contrat,";
972 $sql .=
" fk_commercial_signature, fk_commercial_suivi, fk_projet,";
973 $sql .=
" ref, entity, note_private, note_public, ref_customer, ref_supplier, ref_ext)";
974 $sql .=
" VALUES ('".$this->db->idate($now).
"', ".((int) $this->socid).
", ".((int) $user->id);
975 $sql .=
", ".(dol_strlen($this->date_contrat) != 0 ?
"'".$this->db->idate($this->date_contrat).
"'" :
"NULL");
976 $sql .=
",".($this->commercial_signature_id > 0 ? ((int) $this->commercial_signature_id) :
"NULL");
977 $sql .=
",".($this->commercial_suivi_id > 0 ? ((int) $this->commercial_suivi_id) :
"NULL");
978 $sql .=
",".($this->fk_project > 0 ? ((int) $this->fk_project) :
"NULL");
979 $sql .=
", ".(dol_strlen($this->
ref) <= 0 ?
"null" :
"'".$this->db->escape($this->
ref).
"'");
980 $sql .=
", ".((int) $conf->entity);
981 $sql .=
", ".(!empty($this->note_private) ? (
"'".$this->db->escape($this->note_private).
"'") :
"NULL");
982 $sql .=
", ".(!empty($this->note_public) ? (
"'".$this->db->escape($this->note_public).
"'") :
"NULL");
983 $sql .=
", ".(!empty($this->ref_customer) ? (
"'".$this->db->escape($this->ref_customer).
"'") :
"NULL");
984 $sql .=
", ".(!empty($this->ref_supplier) ? (
"'".$this->db->escape($this->ref_supplier).
"'") :
"NULL");
985 $sql .=
", ".(!empty($this->ref_ext) ? (
"'".$this->db->escape($this->ref_ext).
"'") :
"NULL");
987 $resql = $this->db->query(
$sql);
992 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"contrat");
995 $module = (!empty($conf->global->CONTRACT_ADDON) ? $conf->global->CONTRACT_ADDON :
'mod_contract_serpis');
996 if (substr($module, 0, 13) ==
'mod_contract_' && substr($module, -3) ==
'php') {
997 $module = substr($module, 0,
dol_strlen($module) - 4);
1001 $modCodeContract =
new $module();
1003 if (!empty($modCodeContract->code_auto)) {
1005 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"contrat SET ref='(PROV".$this->
id.
")' WHERE rowid=".((int) $this->
id);
1006 if ($this->db->query(
$sql)) {
1008 $this->
ref =
"(PROV".$this->id.
")";
1023 $result = $this->
add_contact($this->commercial_signature_id,
'SALESREPSIGN',
'internal');
1031 $result = $this->
add_contact($this->commercial_suivi_id,
'SALESREPFOLL',
'internal');
1038 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1039 $this->linked_objects = $this->linkedObjectsIds;
1043 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1044 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1045 if (is_array($tmp_origin_id)) {
1046 foreach ($tmp_origin_id as $origin_id) {
1049 $this->error = $this->db->lasterror();
1055 $origin_id = $tmp_origin_id;
1058 $this->error = $this->db->lasterror();
1065 if (!$error && $this->
id && !empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && !empty($this->origin) && !empty($this->origin_id)) {
1066 $originforcontact = $this->origin;
1067 $originidforcontact = $this->origin_id;
1068 if ($originforcontact ==
'shipping') {
1069 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1071 $exp->fetch($this->origin_id);
1072 $exp->fetchObjectLinked();
1073 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
1074 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
1075 $originforcontact =
'commande';
1076 $originidforcontact = $value;
1082 $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";
1083 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact).
"'";
1085 $resqlcontact = $this->db->query($sqlcontact);
1086 if ($resqlcontact) {
1087 while ($objcontact = $this->db->fetch_object($resqlcontact)) {
1088 if ($objcontact->source ==
'internal' && in_array($objcontact->code, array(
'SALESREPSIGN',
'SALESREPFOLL'))) {
1093 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1103 $result = $this->
call_trigger(
'CONTRACT_CREATE', $user);
1110 $this->db->commit();
1113 dol_syslog(get_class($this).
"::create - 30 - ".$this->error, LOG_ERR);
1114 $this->db->rollback();
1118 $this->error =
"Failed to add contract";
1119 dol_syslog(get_class($this).
"::create - 20 - ".$this->error, LOG_ERR);
1120 $this->db->rollback();
1124 $this->error = $langs->trans(
"UnknownError: ".$this->db->error().
" -", LOG_DEBUG);
1126 $this->db->rollback();
1138 public function delete($user)
1140 global $conf, $langs;
1141 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1148 $result = $this->
call_trigger(
'CONTRACT_DELETE', $user);
1158 dol_syslog(get_class($this).
"::delete error", LOG_ERR);
1178 $sql =
"SELECT cdl.rowid as cdlrowid ";
1179 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet_log as cdl, ".MAIN_DB_PREFIX.
"contratdet as cd";
1180 $sql .=
" WHERE cdl.fk_contratdet=cd.rowid AND cd.fk_contrat=".((int) $this->
id);
1182 dol_syslog(get_class($this).
"::delete contratdet_log", LOG_DEBUG);
1183 $resql = $this->db->query(
$sql);
1185 $this->error = $this->db->error();
1188 $numressql = $this->db->num_rows($resql);
1189 if (!$error && $numressql) {
1190 $tab_resql = array();
1191 for ($i = 0; $i < $numressql; $i++) {
1192 $objresql = $this->db->fetch_object($resql);
1193 $tab_resql[] = $objresql->cdlrowid;
1195 $this->db->free($resql);
1197 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"contratdet_log ";
1198 $sql .=
" WHERE ".MAIN_DB_PREFIX.
"contratdet_log.rowid IN (".$this->db->sanitize(implode(
",", $tab_resql)).
")";
1200 dol_syslog(get_class($this).
"::delete contratdet_log", LOG_DEBUG);
1201 $resql = $this->db->query(
$sql);
1203 $this->error = $this->db->error();
1212 $main = MAIN_DB_PREFIX.
'contratdet';
1213 $ef = $main.
"_extrafields";
1214 $sql =
"DELETE FROM ".$ef.
" WHERE fk_object IN (SELECT rowid FROM ".$main.
" WHERE fk_contrat = ".((int) $this->
id).
")";
1216 dol_syslog(get_class($this).
"::delete contratdet_extrafields", LOG_DEBUG);
1217 $resql = $this->db->query(
$sql);
1219 $this->error = $this->db->error();
1226 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"contratdet";
1227 $sql .=
" WHERE fk_contrat=".((int) $this->
id);
1229 dol_syslog(get_class($this).
"::delete contratdet", LOG_DEBUG);
1230 $resql = $this->db->query(
$sql);
1232 $this->error = $this->db->error();
1239 $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);
1240 $resql = $this->db->query(
$sql);
1242 $this->error = $this->db->lasterror();
1243 $this->errors[] = $this->error;
1250 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"contrat";
1251 $sql .=
" WHERE rowid=".((int) $this->
id);
1253 dol_syslog(get_class($this).
"::delete contrat", LOG_DEBUG);
1254 $resql = $this->db->query(
$sql);
1256 $this->error = $this->db->error();
1266 dol_syslog(get_class($this).
"::delete error -3 ".$this->error, LOG_ERR);
1273 if ($conf->contrat->dir_output) {
1274 $dir = $conf->contrat->multidir_output[$this->entity].
"/".$ref;
1275 if (file_exists($dir)) {
1278 $this->error =
'ErrorFailToDeleteDir';
1286 $this->db->commit();
1289 $this->error = $this->db->lasterror();
1290 $this->db->rollback();
1302 public function update($user, $notrigger = 0)
1304 global $conf, $langs;
1308 if (empty($this->fk_commercial_signature) && $this->commercial_signature_id > 0) {
1309 $this->fk_commercial_signature = $this->commercial_signature_id;
1311 if (empty($this->fk_commercial_suivi) && $this->commercial_suivi_id > 0) {
1312 $this->fk_commercial_suivi = $this->commercial_suivi_id;
1314 if (empty($this->fk_soc) && $this->socid > 0) {
1315 $this->fk_soc = (int) $this->socid;
1317 if (empty($this->fk_project) && $this->projet > 0) {
1318 $this->fk_project = (int) $this->projet;
1321 if (isset($this->
ref)) {
1322 $this->
ref = trim($this->
ref);
1324 if (isset($this->ref_customer)) {
1325 $this->ref_customer = trim($this->ref_customer);
1327 if (isset($this->ref_supplier)) {
1328 $this->ref_supplier = trim($this->ref_supplier);
1330 if (isset($this->ref_ext)) {
1331 $this->ref_ext = trim($this->ref_ext);
1333 if (isset($this->entity)) {
1334 $this->entity = (int) $this->entity;
1336 if (isset($this->statut)) {
1337 $this->statut = (int) $this->statut;
1339 if (isset($this->fk_soc)) {
1340 $this->fk_soc = (int) $this->fk_soc;
1342 if (isset($this->fk_commercial_signature)) {
1343 $this->fk_commercial_signature = trim($this->fk_commercial_signature);
1345 if (isset($this->fk_commercial_suivi)) {
1346 $this->fk_commercial_suivi = trim($this->fk_commercial_suivi);
1348 if (isset($this->note_private)) {
1349 $this->note_private = trim($this->note_private);
1351 if (isset($this->note_public)) {
1352 $this->note_public = trim($this->note_public);
1354 if (isset($this->import_key)) {
1355 $this->import_key = trim($this->import_key);
1363 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contrat SET";
1364 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1365 $sql .=
" ref_customer=".(isset($this->ref_customer) ?
"'".$this->db->escape($this->ref_customer).
"'" :
"null").
",";
1366 $sql .=
" ref_supplier=".(isset($this->ref_supplier) ?
"'".$this->db->escape($this->ref_supplier).
"'" :
"null").
",";
1367 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1368 $sql .=
" entity=".$conf->entity.
",";
1369 $sql .=
" date_contrat=".(dol_strlen($this->date_contrat) != 0 ?
"'".$this->db->idate($this->date_contrat).
"'" :
'null').
",";
1370 $sql .=
" statut=".(isset($this->statut) ? $this->statut :
"null").
",";
1371 $sql .=
" fk_soc=".($this->fk_soc > 0 ? $this->fk_soc :
"null").
",";
1372 $sql .=
" fk_projet=".($this->fk_project > 0 ? $this->fk_project :
"null").
",";
1373 $sql .=
" fk_commercial_signature=".(isset($this->fk_commercial_signature) ? $this->fk_commercial_signature :
"null").
",";
1374 $sql .=
" fk_commercial_suivi=".(isset($this->fk_commercial_suivi) ? $this->fk_commercial_suivi :
"null").
",";
1375 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1376 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1377 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
1379 $sql .=
" WHERE rowid=".((int) $this->
id);
1383 $resql = $this->db->query(
$sql);
1385 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1395 if (!$error && !$notrigger) {
1397 $result = $this->
call_trigger(
'CONTRACT_MODIFY', $user);
1406 foreach ($this->errors as $errmsg) {
1407 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1408 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1410 $this->db->rollback();
1413 $this->db->commit();
1442 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)
1444 global $user, $langs, $conf, $mysoc;
1447 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");
1450 if ($fk_product <= 0 && empty($desc)) {
1451 $this->error =
"ErrorDescRequiredForFreeProductLines";
1455 if ($this->statut >= 0) {
1464 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1465 $vat_src_code = $reg[1];
1466 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1472 $remise_percent =
price2num($remise_percent);
1477 if (empty($info_bits)) {
1480 if (empty($pu_ht) || !is_numeric($pu_ht)) {
1483 if (empty($pu_ttc)) {
1486 if (empty($txtva) || !is_numeric($txtva)) {
1489 if (empty($txlocaltax1) || !is_numeric($txlocaltax1)) {
1492 if (empty($txlocaltax2) || !is_numeric($txlocaltax2)) {
1496 if ($price_base_type ==
'HT') {
1503 if (empty($remise_percent)) {
1504 $remise_percent = 0;
1510 if ($date_start && $date_end && $date_start > $date_end) {
1511 $langs->load(
"errors");
1512 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1518 $localtaxes_type =
getLocalTaxesFromRate($txtva.($vat_src_code ?
' ('.$vat_src_code.
')' :
''), 0, $this->societe, $mysoc);
1525 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1526 $total_ht = $tabprice[0];
1527 $total_tva = $tabprice[1];
1528 $total_ttc = $tabprice[2];
1529 $total_localtax1 = $tabprice[9];
1530 $total_localtax2 = $tabprice[10];
1532 $localtax1_type = $localtaxes_type[0];
1533 $localtax2_type = $localtaxes_type[2];
1540 $remise = round(($pu_ht * $remise_percent / 100), 2);
1541 $price = $pu_ht - $remise;
1544 if (empty($pa_ht)) {
1550 if ($this->pa_ht == 0) {
1551 if (($result = $this->
defineBuyPrice($pu_ht, $remise_percent, $fk_product)) < 0) {
1559 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
1560 $sql .=
" (fk_contrat, label, description, fk_product, qty, tva_tx, vat_src_code,";
1561 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
1562 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
1563 $sql .=
" info_bits,";
1564 $sql .=
" price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
1565 if ($date_start > 0) {
1566 $sql .=
",date_ouverture_prevue";
1568 if ($date_end > 0) {
1569 $sql .=
",date_fin_validite";
1571 $sql .=
", fk_unit";
1573 $sql .=
") VALUES (";
1574 $sql .= $this->
id.
", '', '".$this->db->escape($desc).
"',";
1575 $sql .= ($fk_product > 0 ? $fk_product :
"null").
",";
1576 $sql .=
" ".((float) $qty).
",";
1577 $sql .=
" ".((float) $txtva).
",";
1578 $sql .=
" ".($vat_src_code ?
"'".$this->db->escape($vat_src_code).
"'" :
"null").
",";
1579 $sql .=
" ".((float) $txlocaltax1).
",";
1580 $sql .=
" ".((float) $txlocaltax2).
",";
1581 $sql .=
" '".$this->db->escape($localtax1_type).
"',";
1582 $sql .=
" '".$this->db->escape($localtax2_type).
"',";
1583 $sql .=
" ".price2num($remise_percent).
",";
1584 $sql .=
" ".price2num($pu_ht).
",";
1586 $sql .=
" '".$this->db->escape($info_bits).
"',";
1588 if (isset($fk_fournprice)) {
1589 $sql .=
' '.((int) $fk_fournprice).
',';
1593 if (isset($pa_ht)) {
1594 $sql .=
' '.price2num($pa_ht);
1598 if ($date_start > 0) {
1599 $sql .=
",'".$this->db->idate($date_start).
"'";
1601 if ($date_end > 0) {
1602 $sql .=
",'".$this->db->idate($date_end).
"'";
1604 $sql .=
", ".($fk_unit ?
"'".$this->db->escape($fk_unit).
"'" :
"null");
1605 $sql .=
", ".(!empty($rang) ? (int) $rang :
"0");
1608 $resql = $this->db->query(
$sql);
1610 $contractlineid = $this->db->last_insert_id(MAIN_DB_PREFIX.
"contratdet");
1614 $contractline->array_options = $array_options;
1615 $contractline->id = $contractlineid;
1616 $result = $contractline->insertExtraFields();
1618 $this->error[] = $contractline->error;
1623 if (empty($error)) {
1625 $result = $this->
call_trigger(
'LINECONTRACT_INSERT', $user);
1633 $this->db->rollback();
1636 $this->db->commit();
1637 return $contractlineid;
1640 $this->db->rollback();
1641 $this->error = $this->db->error().
" sql=".
$sql;
1645 dol_syslog(get_class($this).
"::addline ErrorTryToAddLineOnValidatedContract", LOG_ERR);
1674 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)
1676 global $user, $conf, $langs, $mysoc;
1682 $desc = trim($desc);
1683 $desc = trim($desc);
1689 if (empty($fk_fournprice)) {
1699 $remise = round(($pu * $remise_percent / 100), 2);
1700 $price = $pu - $remise;
1702 $remise_percent = 0;
1705 if ($date_start && $date_end && $date_start > $date_end) {
1706 $langs->load(
"errors");
1707 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1711 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");
1721 $tvatx = preg_replace(
'/\s*\(.*\)/',
'', $tvatx);
1723 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $tvatx, $localtax1tx, $localtax2tx, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1724 $total_ht = $tabprice[0];
1725 $total_tva = $tabprice[1];
1726 $total_ttc = $tabprice[2];
1727 $total_localtax1 = $tabprice[9];
1728 $total_localtax2 = $tabprice[10];
1730 $localtax1_type = (empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0]);
1731 $localtax2_type = (empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2]);
1738 $remise = round(($pu * $remise_percent / 100), 2);
1739 $price = $pu - $remise;
1742 if (empty($pa_ht)) {
1747 if ($this->pa_ht == 0) {
1748 if (($result = $this->
defineBuyPrice($pu, $remise_percent)) < 0) {
1755 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet set description = '".$this->db->escape($desc).
"'";
1759 $sql .=
",remise_percent = ".((float)
price2num($remise_percent));
1760 $sql .=
",qty = ".((float) $qty);
1762 $sql .=
",localtax1_tx = ".((float)
price2num($localtax1tx));
1763 $sql .=
",localtax2_tx = ".((float)
price2num($localtax2tx));
1764 $sql .=
",localtax1_type='".$this->db->escape($localtax1_type).
"'";
1765 $sql .=
",localtax2_type='".$this->db->escape($localtax2_type).
"'";
1768 $sql .=
", total_localtax1 = ".((float)
price2num($total_localtax1));
1769 $sql .=
", total_localtax2 = ".((float)
price2num($total_localtax2));
1771 $sql .=
", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice :
"null");
1773 if ($date_start > 0) {
1774 $sql .=
",date_ouverture_prevue = '".$this->db->idate($date_start).
"'";
1776 $sql .=
",date_ouverture_prevue = null";
1778 if ($date_end > 0) {
1779 $sql .=
",date_fin_validite = '".$this->db->idate($date_end).
"'";
1781 $sql .=
",date_fin_validite = null";
1783 if ($date_start_real > 0) {
1784 $sql .=
",date_ouverture = '".$this->db->idate($date_start_real).
"'";
1786 $sql .=
",date_ouverture = null";
1788 if ($date_end_real > 0) {
1789 $sql .=
",date_cloture = '".$this->db->idate($date_end_real).
"'";
1791 $sql .=
",date_cloture = null";
1793 $sql .=
", fk_unit = ".($fk_unit > 0 ? ((int) $fk_unit) :
"null");
1794 $sql .=
", rang = ".(!empty($rang) ? ((int) $rang) :
"0");
1795 $sql .=
" WHERE rowid = ".((int) $rowid);
1797 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
1798 $result = $this->db->query(
$sql);
1800 if (is_array($array_options) && count($array_options) > 0) {
1802 $contractline->fetch($rowid);
1805 foreach ($array_options as $key => $value) {
1806 $contractline->array_options[$key] = $array_options[$key];
1809 $result = $contractline->insertExtraFields();
1811 $this->error[] = $contractline->error;
1816 if (empty($error)) {
1818 $result = $this->
call_trigger(
'LINECONTRACT_MODIFY', $user);
1820 $this->db->rollback();
1825 $this->db->commit();
1828 $this->db->rollback();
1832 $this->db->rollback();
1833 $this->error = $this->db->error();
1834 dol_syslog(get_class($this).
"::updateline Erreur -1");
1848 global $conf, $langs;
1852 if ($this->statut >= 0) {
1854 $result = $this->
call_trigger(
'LINECONTRACT_DELETE', $user);
1862 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element_line;
1863 $sql .=
" WHERE rowid = ".((int) $idline);
1865 dol_syslog(get_class($this).
"::deleteline", LOG_DEBUG);
1866 $resql = $this->db->query(
$sql);
1868 $this->error =
"Error ".$this->db->lasterror();
1875 $contractline->id = $idline;
1876 $result = $contractline->deleteExtraFields();
1879 $this->error =
"Error ".get_class($this).
"::deleteline deleteExtraFields error -4 ".$contractline->error;
1883 if (empty($error)) {
1884 $this->db->commit();
1887 dol_syslog(get_class($this).
"::deleteline ERROR:".$this->error, LOG_ERR);
1888 $this->db->rollback();
1892 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
1909 dol_syslog(__METHOD__.
" is deprecated", LOG_WARNING);
1912 if ($this->statut == 0) {
1937 return $this->
LibStatut($this->statut, $mode);
1953 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1955 $langs->load(
"contracts");
1956 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'ContractStatusDraft');
1957 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'ContractStatusValidated');
1958 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv(
'ContractStatusClosed');
1959 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'ContractStatusDraft');
1960 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'ContractStatusValidated');
1961 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv(
'ContractStatusClosed');
1964 $statusType =
'status'.$status;
1965 if ($status == self::STATUS_VALIDATED) {
1966 $statusType =
'status6';
1969 if ($mode == 4 || $mode == 6 || $mode == 7) {
1972 $text =
'<span class="hideonsmartphone">';
1973 $text .= ($this->nbofserviceswait + $this->nbofservicesopened + $this->nbofservicesexpired + $this->nbofservicesclosed);
1974 $text .=
' '.$langs->trans(
"Services");
1975 $text .=
': ';
1978 $text .= ($mode == 7 ?
'<span class="nowraponall">' :
'');
1979 $text .= ($mode != 7 || $this->nbofserviceswait > 0) ? ($this->nbofserviceswait.ContratLigne::LibStatut(0, 3, -1,
'class="marginleft2"')).(($mode != 7 || $this->nbofservicesopened || $this->nbofservicesexpired || $this->nbofservicesclosed) ?
' ' :
'') :
'';
1980 $text .= ($mode == 7 ?
'</span><span class="nowraponall">' :
'');
1981 $text .= ($mode != 7 || $this->nbofservicesopened > 0) ? ($this->nbofservicesopened.ContratLigne::LibStatut(4, 3, 0,
'class="marginleft2"')).(($mode != 7 || $this->nbofservicesexpired || $this->nbofservicesclosed) ?
' ' :
'') :
'';
1982 $text .= ($mode == 7 ?
'</span><span class="nowraponall">' :
'');
1983 $text .= ($mode != 7 || $this->nbofservicesexpired > 0) ? ($this->nbofservicesexpired.ContratLigne::LibStatut(4, 3, 1,
'class="marginleft2"')).(($mode != 7 || $this->nbofservicesclosed) ?
' ' :
'') :
'';
1984 $text .= ($mode == 7 ?
'</span><span class="nowraponall">' :
'');
1985 $text .= ($mode != 7 || $this->nbofservicesclosed > 0) ? ($this->nbofservicesclosed.ContratLigne::LibStatut(5, 3, -1,
'class="marginleft2"')) :
'';
1986 $text .= ($mode == 7 ?
'</span>' :
'');
1989 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
2001 global $conf, $langs, $user;
2003 $langs->load(
'contracts');
2006 $nofetch = !empty($params[
'nofetch']);
2008 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
2009 return [
'optimize' => $langs->trans(
"ShowContract")];
2011 if ($user->hasRight(
'contrat',
'lire')) {
2012 $datas[
'picto'] =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Contract").
'</u>';
2017 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.($this->
ref ? $this->
ref : $this->id);
2019 $langs->load(
'companies');
2020 if (empty($this->thirdparty)) {
2023 $datas[
'customer'] =
'<br><b>'.$langs->trans(
'Customer').
':</b> '.$this->thirdparty->getNomUrl(1,
'', 0, 1);
2025 $datas[
'refcustomer'] =
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '. $this->ref_customer;
2027 $langs->load(
'project');
2028 if (empty($this->project)) {
2031 $datas[
'project'] =
'<br><b>'.$langs->trans(
'Project').
':</b> '.$this->project->getNomUrl(1,
'', 0, 1);
2035 $datas[
'refsupplier'] =
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
2036 if (!empty($this->total_ht)) {
2037 $datas[
'amountht'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
2039 if (!empty($this->total_tva)) {
2040 $datas[
'vatamount'] =
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
2042 if (!empty($this->total_ttc)) {
2043 $datas[
'amounttc'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
2058 public function getNomUrl($withpicto = 0, $maxlength = 0, $notooltip = 0, $save_lastsearch_value = -1)
2060 global $conf, $langs, $user, $hookmanager;
2064 $url = DOL_URL_ROOT.
'/contrat/card.php?id='.$this->id;
2069 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2070 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
2071 $add_save_lastsearch_values = 1;
2073 if ($add_save_lastsearch_values) {
2074 $url .=
'&save_lastsearch_values=1';
2079 'objecttype' => $this->element,
2082 $classfortooltip =
'classfortooltip';
2085 $classfortooltip =
'classforajaxtooltip';
2086 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
2093 if (empty($notooltip) && $user->hasRight(
'contrat',
'lire')) {
2094 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
2095 $label = $langs->trans(
"ShowContract");
2096 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
2098 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
2099 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
2101 $linkstart =
'<a href="'.$url.
'"';
2102 $linkstart .= $linkclose.
'>';
2105 $result .= $linkstart;
2107 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') : $dataparams.
' class="'.(($withpicto != 2) ?
'paddingright ' :
'').$classfortooltip.
'"'), 0, 0, $notooltip ? 0 : 1);
2109 if ($withpicto != 2) {
2110 $result .= ($this->
ref ? $this->
ref : $this->id);
2112 $result .= $linkend;
2115 $hookmanager->initHooks(array(
'contractdao'));
2116 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
2117 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
2119 $result = $hookmanager->resPrint;
2121 $result .= $hookmanager->resPrint;
2135 $sql =
"SELECT c.rowid, c.ref, c.datec,";
2136 $sql .=
" c.tms as date_modification,";
2137 $sql .=
" fk_user_author";
2138 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2139 $sql .=
" WHERE c.rowid = ".((int) $id);
2141 $result = $this->db->query(
$sql);
2143 if ($this->db->num_rows($result)) {
2144 $obj = $this->db->fetch_object($result);
2146 $this->
id = $obj->rowid;
2148 if ($obj->fk_user_author) {
2149 $cuser =
new User($this->db);
2150 $cuser->fetch($obj->fk_user_author);
2151 $this->user_creation = $cuser;
2154 $this->
ref = (!$obj->ref) ? $obj->rowid : $obj->ref;
2155 $this->date_creation = $this->db->jdate($obj->datec);
2156 $this->date_modification = $this->db->jdate($obj->date_modification);
2159 $this->db->free($result);
2177 $sql =
"SELECT cd.rowid";
2178 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as cd";
2179 $sql .=
" WHERE fk_contrat =".((int) $this->
id);
2181 $sql .=
" AND statut = ".((int) $status);
2184 dol_syslog(get_class($this).
"::array_detail()", LOG_DEBUG);
2185 $resql = $this->db->query(
$sql);
2187 $num = $this->db->num_rows($resql);
2190 $obj = $this->db->fetch_object($resql);
2191 $tab[$i] = $obj->rowid;
2196 $this->error = $this->db->error();
2210 public function getListOfContracts($option =
'all', $status = [], $product_categories = [], $line_status = [])
2214 $sql =
"SELECT c.rowid";
2215 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2216 if (!empty($product_categories)) {
2217 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"contratdet as cd ON cd.fk_contrat = c.rowid";
2218 $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)).
")";
2220 $sql .=
" WHERE c.fk_soc =".((int) $this->socid);
2221 $sql .= ($option ==
'others') ?
" AND c.rowid <> ".((
int) $this->id) :
"";
2222 $sql .= (!empty($status)) ?
" AND c.statut IN (".$this->db->sanitize(implode(
', ', $status)).
")" :
"";
2223 $sql .= (!empty($line_status)) ?
" AND cd.statut IN (".$this->db->sanitize(implode(
', ', $line_status)).
")" :
"";
2224 $sql .=
" GROUP BY c.rowid";
2226 dol_syslog(get_class($this).
"::getOtherContracts()", LOG_DEBUG);
2227 $resql = $this->db->query(
$sql);
2229 $num = $this->db->num_rows($resql);
2232 $obj = $this->db->fetch_object($resql);
2233 $contrat =
new Contrat($this->db);
2234 $contrat->fetch($obj->rowid);
2235 $tab[$contrat->id] = $contrat;
2240 $this->error = $this->db->lasterror();
2257 global $conf, $langs;
2259 $this->from =
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2260 $this->from .=
", ".MAIN_DB_PREFIX.
"contratdet as cd";
2261 $this->from .=
", ".MAIN_DB_PREFIX.
"societe as s";
2262 if (empty($user->rights->societe->client->voir) && !$user->socid) {
2263 $this->from .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2266 if ($mode ==
'inactive') {
2267 $sql =
"SELECT cd.rowid, cd.date_ouverture_prevue as datefin";
2268 $sql .= $this->from;
2269 $sql .=
" WHERE c.statut = 1";
2270 $sql .=
" AND c.rowid = cd.fk_contrat";
2271 $sql .=
" AND cd.statut = 0";
2272 } elseif ($mode ==
'expired') {
2273 $sql =
"SELECT cd.rowid, cd.date_fin_validite as datefin";
2274 $sql .= $this->from;
2275 $sql .=
" WHERE c.statut = 1";
2276 $sql .=
" AND c.rowid = cd.fk_contrat";
2277 $sql .=
" AND cd.statut = 4";
2278 $sql .=
" AND cd.date_fin_validite < '".$this->db->idate(
dol_now()).
"'";
2279 } elseif ($mode ==
'active') {
2280 $sql =
"SELECT cd.rowid, cd.date_fin_validite as datefin";
2281 $sql .= $this->from;
2282 $sql .=
" WHERE c.statut = 1";
2283 $sql .=
" AND c.rowid = cd.fk_contrat";
2284 $sql .=
" AND cd.statut = 4";
2288 $sql .=
" AND c.fk_soc = s.rowid";
2289 $sql .=
" AND c.entity = ".((int) $conf->entity);
2291 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
2293 if (empty($user->rights->societe->client->voir) && !$user->socid) {
2294 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2297 $resql = $this->db->query(
$sql);
2299 $langs->load(
"contracts");
2302 if ($mode ==
'inactive') {
2303 $warning_delay = $conf->contrat->services->inactifs->warning_delay;
2304 $label = $langs->trans(
"BoardNotActivatedServices");
2305 $labelShort = $langs->trans(
"BoardNotActivatedServicesShort");
2306 $url = DOL_URL_ROOT.
'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=0&sortfield=cd.date_fin_validite&sortorder=asc';
2307 } elseif ($mode ==
'expired') {
2308 $warning_delay = $conf->contrat->services->expires->warning_delay;
2309 $url = DOL_URL_ROOT.
'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&filter=expired&sortfield=cd.date_fin_validite&sortorder=asc';
2310 $label = $langs->trans(
"BoardExpiredServices");
2311 $labelShort = $langs->trans(
"BoardExpiredServicesShort");
2313 $warning_delay = $conf->contrat->services->expires->warning_delay;
2314 $url = DOL_URL_ROOT.
'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&search_status=4&sortfield=cd.date_fin_validite&sortorder=asc';
2317 $label = $langs->trans(
"BoardRunningServices");
2318 $labelShort = $langs->trans(
"BoardRunningServicesShort");
2322 $response->warning_delay = $warning_delay / 60 / 60 / 24;
2323 $response->label = $label;
2324 $response->labelShort = $labelShort;
2325 $response->url = $url;
2328 while ($obj = $this->db->fetch_object($resql)) {
2329 $response->nbtodo++;
2331 if ($obj->datefin && $this->db->jdate($obj->datefin) < ($now - $warning_delay)) {
2332 $response->nbtodolate++;
2339 $this->error = $this->db->error();
2353 global $conf, $user;
2355 $this->nb = array();
2358 $sql =
"SELECT count(c.rowid) as nb";
2359 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2360 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON c.fk_soc = s.rowid";
2361 if (empty($user->rights->societe->client->voir) && !$user->socid) {
2362 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
2363 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
2366 $sql .=
" ".$clause.
" c.entity = ".$conf->entity;
2368 $resql = $this->db->query(
$sql);
2370 while ($obj = $this->db->fetch_object($resql)) {
2371 $this->nb[
"contracts"] = $obj->nb;
2373 $this->db->free($resql);
2377 $this->error = $this->db->error();
2415 global $user, $langs, $conf;
2420 $sql =
"SELECT rowid";
2421 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
2422 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
2423 $sql .=
" AND tosell = 1";
2424 $sql .= $this->db->plimit(100);
2426 $resql = $this->db->query(
$sql);
2428 $num_prods = $this->db->num_rows($resql);
2430 while ($i < $num_prods) {
2432 $row = $this->db->fetch_row($resql);
2433 $prodids[$i] = $row[0];
2439 $this->specimen = 1;
2441 $this->
ref =
'SPECIMEN';
2442 $this->ref_customer =
'SPECIMENCUST';
2443 $this->ref_supplier =
'SPECIMENSUPP';
2446 $this->date_creation = (
dol_now() - 3600 * 24 * 7);
2447 $this->date_contrat =
dol_now();
2448 $this->commercial_signature_id = 1;
2449 $this->commercial_suivi_id = 1;
2450 $this->note_private =
'This is a comment (private)';
2451 $this->note_public =
'This is a comment (public)';
2452 $this->fk_projet = 0;
2456 while ($xnbp < $nbp) {
2459 $line->subprice = 100;
2461 $line->tva_tx = 19.6;
2462 $line->remise_percent = 10;
2463 $line->total_ht = 90;
2464 $line->total_ttc = 107.64;
2465 $line->total_tva = 17.64;
2466 $line->date_start =
dol_now() - 500000;
2467 $line->date_start_real =
dol_now() - 200000;
2468 $line->date_end =
dol_now() + 500000;
2469 $line->date_end_real =
dol_now() - 100000;
2470 if ($num_prods > 0) {
2471 $prodid = mt_rand(1, $num_prods);
2472 $line->fk_product = $prodids[$prodid];
2474 $this->lines[$xnbp] = $line;
2501 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
2503 global $conf, $langs;
2508 if (!empty($this->model_pdf)) {
2509 $modele = $this->model_pdf;
2510 } elseif (!empty($this->modelpdf)) {
2511 $modele = $this->modelpdf;
2512 } elseif (!empty($conf->global->CONTRACT_ADDON_PDF)) {
2513 $modele = $conf->global->CONTRACT_ADDON_PDF;
2517 if (empty($modele)) {
2520 $langs->load(
"contracts");
2521 $outputlangs->load(
"products");
2523 $modelpath =
"core/modules/contract/doc/";
2524 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2572 global $db, $langs, $conf, $hookmanager, $extrafields;
2578 $this->
fetch($this->
id);
2581 $clonedObj = clone $this;
2582 $clonedObj->socid = $socid;
2586 $objsoc =
new Societe($this->db);
2588 $objsoc->fetch($clonedObj->socid);
2591 $clonedObj->statut = 0;
2593 if (is_array($clonedObj->array_options) && count($clonedObj->array_options) > 0) {
2594 $extrafields->fetch_name_optionals_label($this->table_element);
2595 foreach ($clonedObj->array_options as $key => $option) {
2596 $shortkey = preg_replace(
'/options_/',
'', $key);
2598 if (!empty($extrafields->attributes[$this->element][
'unique'][$shortkey])) {
2600 unset($clonedObj->array_options[$key]);
2605 if (empty($conf->global->CONTRACT_ADDON) || !is_readable(DOL_DOCUMENT_ROOT.
"/core/modules/contract/".$conf->global->CONTRACT_ADDON.
".php")) {
2606 $this->error =
'ErrorSetupNotComplete';
2612 require_once DOL_DOCUMENT_ROOT.
"/core/modules/contract/".$conf->global->CONTRACT_ADDON.
'.php';
2613 $obj = $conf->global->CONTRACT_ADDON;
2614 $modContract =
new $obj();
2615 $clonedObj->ref = $modContract->getNextValue($objsoc, $clonedObj);
2618 foreach ($this->lines as $line) {
2619 $line->fetch_optionals($line->id);
2623 $clonedObj->context[
'createfromclone'] =
'createfromclone';
2624 $result = $clonedObj->create($user);
2627 $this->error = $clonedObj->error;
2628 $this->errors[] = $clonedObj->error;
2631 if ($this->socid == $clonedObj->socid) {
2632 if ($clonedObj->copy_linked_contact($this,
'external') < 0) {
2639 foreach ($this->lines as $line) {
2640 $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);
2643 $this->error = $clonedObj->error;
2644 $this->errors[] = $clonedObj->error;
2651 if (is_object($hookmanager)) {
2652 $parameters = array(
2654 'clonedObj' => $clonedObj
2657 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $clonedObj, $action);
2665 unset($clonedObj->context[
'createfromclone']);
2669 $this->db->commit();
2670 return $clonedObj->id;
2672 $this->db->rollback();
2689 global $langs, $user;
2691 $langs->load(
"agenda");
2701 $contractlineprocessed = array();
2702 $contractignored = array();
2703 $contracterror = array();
2707 $sql =
'SELECT c.rowid, c.ref_customer, cd.rowid as lid, cd.date_fin_validite, p.duration';
2708 $sql.=
' FROM '.MAIN_DB_PREFIX.
'contrat as c, '.MAIN_DB_PREFIX.
'contratdet as cd';
2709 $sql.=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON p.rowid = cd.fk_product';
2710 $sql.=
' WHERE cd.fk_contrat = c.rowid';
2711 $sql.=
" AND date_format(cd.date_fin_validite, '%Y-%m-%d') <= date_format('".$this->db->idate($enddatetoscan).
"', '%Y-%m-%d')";
2712 $sql.=
" AND cd.statut = 4";
2713 if ($thirdparty_id > 0)
$sql.=
" AND c.fk_soc = ".((int) $thirdparty_id);
2716 $resql = $this->db->query(
$sql);
2718 $num = $this->db->num_rows($resql);
2720 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
2724 $obj = $this->db->fetch_object($resql);
2726 if (!empty($contractlineprocessed[$obj->lid]) || !empty($contractignored[$obj->rowid]) || !empty($contracterror[$obj->rowid])) {
2731 $object =
new Contrat($this->db);
2732 $object->fetch($obj->rowid);
2733 $object->fetch_thirdparty();
2735 if ($object->id <= 0) {
2737 $this->errors[] =
'Failed to load contract with id='.$obj->rowid;
2741 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);
2744 $expirationdate = $this->db->jdate($obj->date_fin_validite);
2745 $duration_value = preg_replace(
'/[^0-9]/',
'', $obj->duration);
2746 $duration_unit = preg_replace(
'/\d/',
'', $obj->duration);
2750 $object->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 1);
2752 if (is_array($object->linkedObjects[
'facture']) && count($object->linkedObjects[
'facture']) > 0) {
2753 usort($object->linkedObjects[
'facture'],
"cmp");
2756 $someinvoicenotpaid=0;
2757 foreach ($object->linkedObjects[
'facture'] as $idinvoice => $invoice) {
2760 if (empty($invoice->paye)) {
2761 $someinvoicenotpaid++;
2764 if ($someinvoicenotpaid) {
2765 $this->output .=
'Contract '.$object->ref.
' is qualified for renewal but there is '.$someinvoicenotpaid.
' invoice(s) unpayed so we cancel renewal'.
"\n";
2766 $contractignored[$object->id]=$object->ref;
2771 if ($expirationdate && $expirationdate < $enddatetoscan) {
2772 dol_syslog(
"Define the newdate of end of services from expirationdate=".$expirationdate);
2773 $newdate = $expirationdate;
2775 while ($newdate < $enddatetoscan && $protecti < 1000) {
2780 if ($protecti < 1000) {
2786 $errorforlocaltransaction = 0;
2788 $label =
'Renewal of contrat '.$object->ref.
' line '.$obj->lid;
2789 $comment =
'Renew date of contract '.$object->ref.
' line '.$obj->lid.
' by doAutoRenewContracts';
2791 $sqlupdate =
'UPDATE '.MAIN_DB_PREFIX.
"contratdet SET date_fin_validite = '".$this->db->idate($newdate).
"'";
2792 $sqlupdate.=
' WHERE rowid = '.((int) $obj->lid);
2793 $resqlupdate = $this->db->query($sqlupdate);
2795 $contractlineprocessed[$obj->lid]=$object->ref;
2797 $actioncode =
'RENEW_CONTRACT';
2802 $actioncomm->type_code =
'AC_OTH_AUTO';
2803 $actioncomm->code =
'AC_'.$actioncode;
2804 $actioncomm->label = $label;
2805 $actioncomm->datep = $now;
2806 $actioncomm->datef = $now;
2807 $actioncomm->percentage = -1;
2808 $actioncomm->socid = $object->thirdparty->id;
2809 $actioncomm->authorid = $user->id;
2810 $actioncomm->userownerid = $user->id;
2811 $actioncomm->fk_element = $object->id;
2812 $actioncomm->elementtype =
'contract';
2813 $actioncomm->note_private = $comment;
2815 $ret = $actioncomm->create($user);
2817 $contracterror[$object->id]=$object->ref;
2820 $errorforlocaltransaction++;
2821 $this->error = $this->db->lasterror();
2824 if (! $errorforlocaltransaction) {
2825 $this->db->commit();
2827 $this->db->rollback();
2831 $this->error =
"Bad value for newdate in doAutoRenewContracts - expirationdate=".$expirationdate.
" enddatetoscan=".$enddatetoscan.
" duration_value=".$duration_value.
" duration_unit=".$duration_value;
2840 $this->error = $this->db->lasterror();
2843 $this->output .= count($contractlineprocessed).
' contract line(s) with end date before '.
dol_print_date($enddatetoscan,
'day').
' were renewed'.(count($contractlineprocessed)>0 ?
' : '.join(
',', $contractlineprocessed) :
'');
2845 return ($error ? 1: 0);
2859 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
2861 $return =
'<div class="box-flex-item box-flex-grow-zero">';
2862 $return .=
'<div class="info-box info-box-sm">';
2863 $return .=
'<span class="info-box-icon bg-infobox-action">';
2865 $return .=
'</span>';
2866 $return .=
'<div class="info-box-content">';
2867 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl() : $this->ref).
'</span>';
2868 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
2869 if (!empty($arraydata[
'thirdparty'])) {
2870 $tmpthirdparty = $arraydata[
'thirdparty'];
2871 $return .=
'<br><div class="info-box-label inline-block">'.$tmpthirdparty->getNomUrl(1).
'</div>';
2873 if (property_exists($this,
'date_contrat')) {
2874 $return .=
'<br><span class="opacitymedium">'.$langs->trans(
"DateContract").
' : </span><span class="info-box-label">'.
dol_print_date($this->date_contrat,
'day').
'</span>';
2876 if (method_exists($this,
'getLibStatut')) {
2877 $return .=
'<br><div class="info-box-status margintoponly">'.$this->getLibStatut(7).
'</div>';
2879 $return .=
'</div>';
2880 $return .=
'</div>';
2881 $return .=
'</div>';
2895 public $element =
'contratdet';
2900 public $table_element =
'contratdet';
2906 public $element_for_permission =
'contrat';
2948 public $description;
2950 public $product_type;
2951 public $product_ref;
2952 public $product_label;
2954 public $date_commande;
2957 public $date_start_real;
2959 public $date_end_real;
2962 public $vat_src_code;
2963 public $localtax1_tx;
2964 public $localtax2_tx;
2965 public $localtax1_type;
2966 public $localtax2_type;
2968 public $remise_percent;
2974 public $fk_remise_except;
2989 public $total_localtax1;
2990 public $total_localtax2;
2996 public $fk_fournprice;
3005 public $fk_user_author;
3010 public $fk_user_ouverture;
3015 public $fk_user_cloture;
3017 public $commentaire;
3026 const STATUS_INITIAL = 0;
3027 const STATUS_OPEN = 4;
3028 const STATUS_CLOSED = 5;
3035 public $fields = array(
3036 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
3037 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>30,
'index'=>1),
3038 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>35),
3039 'qty' =>array(
'type'=>
'integer',
'label'=>
'Quantity',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'position'=>35,
'isameasure'=>1),
3040 'total_ht' =>array(
'type'=>
'integer',
'label'=>
'AmountHT',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>36,
'isameasure'=>1),
3041 'total_tva' =>array(
'type'=>
'integer',
'label'=>
'AmountVAT',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>37,
'isameasure'=>1),
3042 'total_ttc' =>array(
'type'=>
'integer',
'label'=>
'AmountTTC',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>38,
'isameasure'=>1),
3045 'fk_contrat' =>array(
'type'=>
'integer:Contrat:contrat/class/contrat.class.php',
'label'=>
'Contract',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>70),
3046 'fk_product' =>array(
'type'=>
'integer:Product:product/class/product.class.php:1',
'label'=>
'Product',
'enabled'=>1,
'visible'=>-1,
'position'=>75),
3048 'note_private' =>array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>105),
3049 'note_public' =>array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>110),
3053 'fk_user_ouverture' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserStartingService',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>135),
3054 'fk_user_cloture' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserClosingService',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>135),
3055 'statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Statut',
'enabled'=>1,
'visible'=>-1,
'position'=>500,
'arrayofkeyval'=>array(0=>
'Draft', 4=>
'Open', 5=>
'Closed')),
3056 'rang' =>array(
'type'=>
'integer',
'label'=>
'Rank',
'enabled'=>1,
'visible'=>0,
'position'=>500,
'default' =>0)
3080 return $this->
LibStatut($this->statut, $mode, ((!empty($this->date_end)) ? ($this->date_end <
dol_now() ? 1 : 0) : -1));
3093 public static function LibStatut($status, $mode, $expired = -1, $moreatt =
'')
3097 $langs->load(
"contracts");
3099 if ($status == self::STATUS_INITIAL) {
3100 $labelStatus = $langs->transnoentities(
"ServiceStatusInitial");
3101 $labelStatusShort = $langs->transnoentities(
"ServiceStatusInitial");
3102 } elseif ($status == self::STATUS_OPEN && $expired == -1) {
3103 $labelStatus = $langs->transnoentities(
"ServiceStatusRunning");
3104 $labelStatusShort = $langs->transnoentities(
"ServiceStatusRunning");
3105 } elseif ($status == self::STATUS_OPEN && $expired == 0) {
3106 $labelStatus = $langs->transnoentities(
"ServiceStatusNotLate");
3107 $labelStatusShort = $langs->transnoentities(
"ServiceStatusNotLateShort");
3108 } elseif ($status == self::STATUS_OPEN && $expired == 1) {
3109 $labelStatus = $langs->transnoentities(
"ServiceStatusLate");
3110 $labelStatusShort = $langs->transnoentities(
"ServiceStatusLateShort");
3111 } elseif ($status == self::STATUS_CLOSED) {
3112 $labelStatus = $langs->transnoentities(
"ServiceStatusClosed");
3113 $labelStatusShort = $langs->transnoentities(
"ServiceStatusClosed");
3116 $statusType =
'status'.$status;
3117 if ($status == self::STATUS_OPEN && $expired == 1) {
3118 $statusType =
'status1';
3120 if ($status == self::STATUS_CLOSED) {
3121 $statusType =
'status6';
3124 $params = array(); $reg = array();
3125 if (preg_match(
'/class="(.*)"/', $moreatt, $reg)) {
3126 $params = array(
'badgeParams'=>array(
'css' => $reg[1]));
3128 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode,
'', $params);
3139 global $conf, $langs, $user;
3142 $datas[
'label'] = $langs->trans(
"ShowContractOfService").
': '.$this->label;
3143 if (empty($datas[
'label'])) {
3144 $datas[
'label'] = $this->description;
3162 $label = $langs->trans(
"ShowContractOfService").
': '.$this->label;
3163 if (empty($label)) {
3164 $label = $this->description;
3166 $classfortooltip =
'classfortooltip';
3170 'id' => $this->fk_contrat,
3171 'objecttype' => $this->element,
3173 $classfortooltip =
'classforajaxtooltip';
3174 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
3178 $link =
'<a href="'.DOL_URL_ROOT.
'/contrat/card.php?id='.$this->fk_contrat.
'"';
3179 $link .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
3180 $link .= $dataparams.
' class="'.$classfortooltip.
'">';
3184 if ($this->
type == 0) {
3189 $result .= ($link.img_object($label, $picto, $dataparams.
' class="'.$classfortooltip.
'"').$linkend);
3191 if ($withpicto && $withpicto != 2) {
3194 if ($withpicto != 2) {
3195 $result .= $link.($this->product_ref ? $this->product_ref.
' ' :
'').($this->label ? $this->label : $this->
description).$linkend;
3210 if (empty($id) && empty($ref)) {
3215 $sql .=
" t.rowid,";
3217 $sql .=
" t.fk_contrat,";
3218 $sql .=
" t.fk_product,";
3219 $sql .=
" t.statut,";
3220 $sql .=
" t.label,";
3221 $sql .=
" p.ref as product_ref,";
3222 $sql .=
" p.label as product_label,";
3223 $sql .=
" p.description as product_desc,";
3224 $sql .=
" p.fk_product_type as product_type,";
3225 $sql .=
" t.description,";
3226 $sql .=
" t.date_commande,";
3227 $sql .=
" t.date_ouverture_prevue as date_start,";
3228 $sql .=
" t.date_ouverture as date_start_real,";
3229 $sql .=
" t.date_fin_validite as date_end,";
3230 $sql .=
" t.date_cloture as date_end_real,";
3231 $sql .=
" t.tva_tx,";
3232 $sql .=
" t.vat_src_code,";
3233 $sql .=
" t.localtax1_tx,";
3234 $sql .=
" t.localtax2_tx,";
3235 $sql .=
" t.localtax1_type,";
3236 $sql .=
" t.localtax2_type,";
3238 $sql .=
" t.remise_percent,";
3239 $sql .=
" t.remise,";
3240 $sql .=
" t.fk_remise_except,";
3241 $sql .=
" t.subprice,";
3242 $sql .=
" t.price_ht,";
3243 $sql .=
" t.total_ht,";
3244 $sql .=
" t.total_tva,";
3245 $sql .=
" t.total_localtax1,";
3246 $sql .=
" t.total_localtax2,";
3247 $sql .=
" t.total_ttc,";
3248 $sql .=
" t.fk_product_fournisseur_price as fk_fournprice,";
3249 $sql .=
" t.buy_price_ht as pa_ht,";
3250 $sql .=
" t.info_bits,";
3251 $sql .=
" t.fk_user_author,";
3252 $sql .=
" t.fk_user_ouverture,";
3253 $sql .=
" t.fk_user_cloture,";
3254 $sql .=
" t.commentaire,";
3255 $sql .=
" t.fk_unit,";
3257 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as t LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = t.fk_product";
3259 $sql .=
" WHERE t.rowid = ".((int) $id);
3262 $sql .=
" WHERE t.rowid = '".$this->db->escape($ref).
"'";
3265 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
3266 $resql = $this->db->query(
$sql);
3268 if ($this->db->num_rows($resql)) {
3269 $obj = $this->db->fetch_object($resql);
3271 $this->
id = $obj->rowid;
3272 $this->
ref = $obj->rowid;
3274 $this->tms = $this->db->jdate($obj->tms);
3275 $this->fk_contrat = $obj->fk_contrat;
3276 $this->fk_product = $obj->fk_product;
3277 $this->statut = $obj->statut;
3278 $this->product_ref = $obj->product_ref;
3279 $this->product_label = $obj->product_label;
3280 $this->product_description = $obj->product_description;
3281 $this->product_type = $obj->product_type;
3282 $this->label = $obj->label;
3284 $this->date_commande = $this->db->jdate($obj->date_commande);
3286 $this->date_start = $this->db->jdate($obj->date_start);
3287 $this->date_start_real = $this->db->jdate($obj->date_start_real);
3288 $this->date_end = $this->db->jdate($obj->date_end);
3289 $this->date_end_real = $this->db->jdate($obj->date_end_real);
3296 $this->tva_tx = $obj->tva_tx;
3297 $this->vat_src_code = $obj->vat_src_code;
3298 $this->localtax1_tx = $obj->localtax1_tx;
3299 $this->localtax2_tx = $obj->localtax2_tx;
3300 $this->localtax1_type = $obj->localtax1_type;
3301 $this->localtax2_type = $obj->localtax2_type;
3302 $this->qty = $obj->qty;
3303 $this->remise_percent = $obj->remise_percent;
3304 $this->fk_remise_except = $obj->fk_remise_except;
3305 $this->subprice = $obj->subprice;
3306 $this->price_ht = $obj->price_ht;
3307 $this->total_ht = $obj->total_ht;
3308 $this->total_tva = $obj->total_tva;
3309 $this->total_localtax1 = $obj->total_localtax1;
3310 $this->total_localtax2 = $obj->total_localtax2;
3311 $this->total_ttc = $obj->total_ttc;
3312 $this->info_bits = $obj->info_bits;
3313 $this->fk_user_author = $obj->fk_user_author;
3314 $this->fk_user_ouverture = $obj->fk_user_ouverture;
3315 $this->fk_user_cloture = $obj->fk_user_cloture;
3316 $this->commentaire = $obj->commentaire;
3317 $this->fk_fournprice = $obj->fk_fournprice;
3319 $marginInfos =
getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
3320 $this->pa_ht = $marginInfos[0];
3321 $this->fk_unit = $obj->fk_unit;
3323 $this->rang = $obj->rang;
3328 $this->db->free($resql);
3332 $this->error =
"Error ".$this->db->lasterror();
3345 public function update($user, $notrigger = 0)
3347 global $conf, $langs, $mysoc;
3352 $this->fk_contrat = (int) $this->fk_contrat;
3353 $this->fk_product = (int) $this->fk_product;
3354 $this->statut = (int) $this->statut;
3355 $this->label = trim($this->label);
3357 $this->vat_src_code = trim($this->vat_src_code);
3358 $this->tva_tx = trim($this->tva_tx);
3359 $this->localtax1_tx = trim($this->localtax1_tx);
3360 $this->localtax2_tx = trim($this->localtax2_tx);
3361 $this->qty = trim($this->qty);
3362 $this->remise_percent = trim($this->remise_percent);
3363 $this->fk_remise_except = (int) $this->fk_remise_except;
3364 $this->subprice =
price2num($this->subprice);
3365 $this->price_ht =
price2num($this->price_ht);
3366 $this->total_ht = trim($this->total_ht);
3367 $this->total_tva = trim($this->total_tva);
3368 $this->total_localtax1 = trim($this->total_localtax1);
3369 $this->total_localtax2 = trim($this->total_localtax2);
3370 $this->total_ttc = trim($this->total_ttc);
3371 $this->info_bits = trim($this->info_bits);
3372 $this->fk_user_author = (int) $this->fk_user_author;
3373 $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
3374 $this->fk_user_cloture = (int) $this->fk_user_cloture;
3375 $this->commentaire = trim($this->commentaire);
3376 $this->rang = (int) $this->rang;
3378 if (empty($this->price_ht)) {
3379 $this->price_ht = 0;
3381 if (empty($this->total_ht)) {
3382 $this->total_ht = 0;
3384 if (empty($this->total_tva)) {
3385 $this->total_tva = 0;
3387 if (empty($this->total_ttc)) {
3388 $this->total_ttc = 0;
3390 if (empty($this->localtax1_tx)) {
3391 $this->localtax1_tx = 0;
3393 if (empty($this->localtax2_tx)) {
3394 $this->localtax2_tx = 0;
3396 if (empty($this->remise_percent)) {
3397 $this->remise_percent = 0;
3400 if (empty($this->date_start)) {
3401 $this->date_start = $this->date_start;
3403 if (empty($this->date_start_real)) {
3404 $this->date_start_real = $this->date_start_real;
3406 if (empty($this->date_end)) {
3407 $this->date_end = $this->date_end;
3409 if (empty($this->date_end_real)) {
3410 $this->date_end_real = $this->date_end_real;
3419 $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);
3420 $this->total_ht = $tabprice[0];
3421 $this->total_tva = $tabprice[1];
3422 $this->total_ttc = $tabprice[2];
3423 $this->total_localtax1 = $tabprice[9];
3424 $this->total_localtax2 = $tabprice[10];
3426 if (empty($this->pa_ht)) {
3431 if ($this->pa_ht == 0) {
3432 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
3435 $this->pa_ht = $result;
3440 if (empty($this->oldcopy)) {
3447 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
3448 $sql .=
" fk_contrat = ".((int) $this->fk_contrat).
",";
3449 $sql .=
" fk_product = ".($this->fk_product ? ((int) $this->fk_product) :
'null').
",";
3450 $sql .=
" statut = ".((int) $this->statut).
",";
3451 $sql .=
" label = '".$this->db->escape($this->label).
"',";
3452 $sql .=
" description = '".$this->db->escape($this->
description).
"',";
3453 $sql .=
" date_commande = ".($this->date_commande !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
"null").
",";
3454 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
3455 $sql .=
" date_ouverture = ".($this->date_start_real !=
'' ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
3456 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
3457 $sql .=
" date_cloture = ".($this->date_end_real !=
'' ?
"'".$this->db->idate($this->date_end_real).
"'" :
"null").
",";
3458 $sql .=
" vat_src_code = '".$this->db->escape($this->vat_src_code).
"',";
3459 $sql .=
" tva_tx = ".price2num($this->tva_tx).
",";
3460 $sql .=
" localtax1_tx = ".price2num($this->localtax1_tx).
",";
3461 $sql .=
" localtax2_tx = ".price2num($this->localtax2_tx).
",";
3462 $sql .=
" qty = ".price2num($this->qty).
",";
3463 $sql .=
" remise_percent = ".price2num($this->remise_percent).
",";
3464 $sql .=
" remise = ".($this->remise ?
price2num($this->remise) :
"null").
",";
3465 $sql .=
" fk_remise_except = ".($this->fk_remise_except > 0 ? $this->fk_remise_except :
"null").
",";
3466 $sql .=
" subprice = ".($this->subprice !=
'' ? $this->subprice :
"null").
",";
3467 $sql .=
" price_ht = ".($this->price_ht !=
'' ? $this->price_ht :
"null").
",";
3468 $sql .=
" total_ht = ".$this->total_ht.
",";
3469 $sql .=
" total_tva = ".$this->total_tva.
",";
3470 $sql .=
" total_localtax1 = ".$this->total_localtax1.
",";
3471 $sql .=
" total_localtax2 = ".$this->total_localtax2.
",";
3472 $sql .=
" total_ttc = ".$this->total_ttc.
",";
3473 $sql .=
" fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"NULL").
",";
3474 $sql .=
" buy_price_ht = '".price2num($this->pa_ht).
"',";
3475 $sql .=
" info_bits = '".$this->db->escape($this->info_bits).
"',";
3476 $sql .=
" fk_user_author = ".($this->fk_user_author >= 0 ? $this->fk_user_author :
"NULL").
",";
3477 $sql .=
" fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? $this->fk_user_ouverture :
"NULL").
",";
3478 $sql .=
" fk_user_cloture = ".($this->fk_user_cloture > 0 ? $this->fk_user_cloture :
"NULL").
",";
3479 $sql .=
" commentaire = '".$this->db->escape($this->commentaire).
"',";
3480 $sql .=
" fk_unit = ".(!$this->fk_unit ?
'NULL' : $this->fk_unit).
",";
3481 $sql .=
" rang = ".(empty($this->rang) ?
'0' : (int) $this->rang);
3482 $sql .=
" WHERE rowid = ".((int) $this->
id);
3484 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3485 $resql = $this->db->query(
$sql);
3487 $this->error =
"Error ".$this->db->lasterror();
3499 if (!$error && !empty($conf->global->CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES)) {
3500 dol_syslog(get_class($this).
"::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
3502 if ($this->date_start != $this->oldcopy->date_start) {
3503 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
3504 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
3505 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
3507 $resql = $this->db->query(
$sql);
3510 $this->error =
"Error ".$this->db->lasterror();
3513 if ($this->date_end != $this->oldcopy->date_end) {
3514 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
3515 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
3516 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
3518 $resql = $this->db->query(
$sql);
3521 $this->error =
"Error ".$this->db->lasterror();
3526 if (!$error && !$notrigger) {
3528 $result = $this->
call_trigger(
'LINECONTRACT_MODIFY', $user);
3531 $this->db->rollback();
3537 $this->db->commit();
3540 $this->db->rollback();
3541 $this->errors[] = $this->error;
3560 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
3561 $sql .=
" total_ht=".price2num($this->total_ht,
'MT');
3562 $sql .=
",total_tva=".price2num($this->total_tva,
'MT');
3563 $sql .=
",total_localtax1=".price2num($this->total_localtax1,
'MT');
3564 $sql .=
",total_localtax2=".price2num($this->total_localtax2,
'MT');
3565 $sql .=
",total_ttc=".price2num($this->total_ttc,
'MT');
3566 $sql .=
" WHERE rowid = ".((int) $this->
id);
3568 dol_syslog(get_class($this).
"::update_total", LOG_DEBUG);
3570 $resql = $this->db->query(
$sql);
3572 $this->db->commit();
3575 $this->error = $this->db->error();
3576 $this->db->rollback();
3590 global $conf, $user;
3595 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
3596 $sql .=
" (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
3597 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
3598 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
3599 $sql .=
" info_bits,";
3601 $sql .=
" price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
3602 if ($this->date_start > 0) {
3603 $sql .=
",date_ouverture_prevue";
3605 if ($this->date_end > 0) {
3606 $sql .=
",date_fin_validite";
3608 $sql .=
") VALUES ($this->fk_contrat, '', '".$this->db->escape($this->
description).
"',";
3609 $sql .= ($this->fk_product > 0 ? $this->fk_product :
"null").
",";
3610 $sql .=
" '".$this->db->escape($this->qty).
"',";
3611 $sql .=
" '".$this->db->escape($this->vat_src_code).
"',";
3612 $sql .=
" '".$this->db->escape($this->tva_tx).
"',";
3613 $sql .=
" '".$this->db->escape($this->localtax1_tx).
"',";
3614 $sql .=
" '".$this->db->escape($this->localtax2_tx).
"',";
3615 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
3616 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
3617 $sql .=
" ".price2num($this->remise_percent).
",".
price2num($this->subprice).
",";
3619 $sql .=
" '".$this->db->escape($this->info_bits).
"',";
3620 $sql .=
" ".(empty($this->rang) ?
'0' : (int) $this->rang).
",";
3621 $sql .=
" ".price2num($this->price_ht).
",".
price2num($this->remise).
",";
3622 if ($this->fk_fournprice > 0) {
3623 $sql .=
' '.((int) $this->fk_fournprice).
',';
3627 if ($this->pa_ht > 0) {
3632 if ($this->date_start > 0) {
3633 $sql .=
",'".$this->db->idate($this->date_start).
"'";
3635 if ($this->date_end > 0) {
3636 $sql .=
",'".$this->db->idate($this->date_end).
"'";
3640 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
3642 $resql = $this->db->query(
$sql);
3644 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'contratdet');
3650 $this->db->rollback();
3657 $result = $this->
call_trigger(
'LINECONTRACT_INSERT', $user);
3659 $this->db->rollback();
3665 $this->db->commit();
3668 $this->db->rollback();
3669 $this->error = $this->db->error().
" sql=".
$sql;
3684 public function active_line($user, $date, $date_end =
'', $comment =
'')
3687 global $langs, $conf;
3693 $this->statut = ContratLigne::STATUS_OPEN;
3694 $this->date_start_real = $date;
3695 $this->date_end = $date_end;
3696 $this->fk_user_ouverture = $user->id;
3697 $this->date_end_real =
null;
3698 $this->commentaire = $comment;
3700 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) $this->statut).
",";
3701 $sql .=
" date_ouverture = ".(dol_strlen($this->date_start_real) != 0 ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
3702 if ($date_end >= 0) {
3703 $sql .=
" date_fin_validite = ".(dol_strlen($this->date_end) != 0 ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
3705 $sql .=
" fk_user_ouverture = ".((int) $this->fk_user_ouverture).
",";
3706 $sql .=
" date_cloture = null,";
3707 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
3708 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND (statut = ".ContratLigne::STATUS_INITIAL.
" OR statut = ".ContratLigne::STATUS_CLOSED.
")";
3710 dol_syslog(get_class($this).
"::active_line", LOG_DEBUG);
3711 $resql = $this->db->query(
$sql);
3714 $result = $this->
call_trigger(
'LINECONTRACT_ACTIVATE', $user);
3721 $this->db->commit();
3724 $this->db->rollback();
3728 $this->error = $this->db->lasterror();
3729 $this->db->rollback();
3744 public function close_line($user, $date_end_real, $comment =
'', $notrigger = 0)
3747 global $langs, $conf;
3750 $this->date_cloture = $date_end_real;
3751 $this->date_end_real = $date_end_real;
3752 $this->fk_user_cloture = $user->id;
3753 $this->commentaire = $comment;
3761 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) ContratLigne::STATUS_CLOSED).
",";
3762 $sql .=
" date_cloture = '".$this->db->idate($date_end_real).
"',";
3763 $sql .=
" fk_user_cloture = ".((int) $user->id).
",";
3764 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
3765 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut = ".((int) ContratLigne::STATUS_OPEN);
3767 $resql = $this->db->query(
$sql);
3771 $result = $this->
call_trigger(
'LINECONTRACT_CLOSE', $user);
3774 $this->db->rollback();
3780 $this->db->commit();
3783 $this->error = $this->db->lasterror();
3784 $this->db->rollback();