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;
195 protected $lines_id_index_mapper = array();
226 public $fields = array(
227 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
228 'ref' =>array(
'type'=>
'varchar(50)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>-1,
'showoncombobox'=>1,
'position'=>15),
229 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'Ref ext',
'enabled'=>1,
'visible'=>0,
'position'=>20),
230 'ref_supplier' =>array(
'type'=>
'varchar(50)',
'label'=>
'Ref supplier',
'enabled'=>1,
'visible'=>-1,
'position'=>25),
231 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>30,
'index'=>1),
232 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>35),
233 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-1,
'position'=>40),
234 'date_contrat' =>array(
'type'=>
'datetime',
'label'=>
'Date contrat',
'enabled'=>1,
'visible'=>-1,
'position'=>45),
235 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>
'$conf->societe->enabled',
'visible'=>-1,
'notnull'=>1,
'position'=>70),
236 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:fk_statut=1',
'label'=>
'Project',
'enabled'=>
"isModEnabled('project')",
'visible'=>-1,
'position'=>75),
237 'fk_commercial_signature' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'SaleRepresentative Signature',
'enabled'=>1,
'visible'=>-1,
'position'=>80),
238 'fk_commercial_suivi' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'SaleRepresentative follower',
'enabled'=>1,
'visible'=>-1,
'position'=>85),
239 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>90),
240 'note_public' =>array(
'type'=>
'text',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>105),
241 'note_private' =>array(
'type'=>
'text',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>110),
242 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'Model pdf',
'enabled'=>1,
'visible'=>0,
'position'=>115),
243 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>120),
244 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>-1,
'position'=>125),
245 'ref_customer' =>array(
'type'=>
'varchar(50)',
'label'=>
'Ref customer',
'enabled'=>1,
'visible'=>-1,
'position'=>130),
246 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>135),
247 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'Last main doc',
'enabled'=>1,
'visible'=>-1,
'position'=>140),
248 'statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Statut',
'enabled'=>1,
'visible'=>-1,
'position'=>500,
'notnull'=>1,
'arrayofkeyval'=>array(0=>
'Draft', 1=>
'Validated', 2=>
'Closed'))
252 const STATUS_DRAFT = 0;
253 const STATUS_VALIDATED = 1;
254 const STATUS_CLOSED = 2;
276 global $db, $langs, $conf;
277 $langs->load(
"contracts");
279 if (!empty($conf->global->CONTRACT_ADDON)) {
282 $file = $conf->global->CONTRACT_ADDON.
".php";
283 $classname = $conf->global->CONTRACT_ADDON;
286 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
288 foreach ($dirmodels as $reldir) {
292 $mybool |= @include_once $dir.$file;
300 $obj =
new $classname();
301 $numref = $obj->getNextValue($soc, $this);
306 $this->error = $obj->error;
311 $langs->load(
"errors");
312 print $langs->trans(
"Error").
" ".$langs->trans(
"ErrorModuleSetupNotComplete", $langs->transnoentitiesnoconv(
"Contract"));
328 public function active_line($user, $line_id, $date, $date_end =
'', $comment =
'')
331 $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->active_line($user, $date, $date_end, $comment);
333 $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
334 $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
350 public function close_line($user, $line_id, $date_end, $comment =
'')
353 $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->close_line($user, $date_end, $comment);
355 $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error;
356 $this->errors = $this->lines[$this->lines_id_index_mapper[$line_id]]->errors;
373 public function activateAll($user, $date_start =
'', $notrigger = 0, $comment =
'', $date_end =
'')
375 if (empty($date_start)) {
386 foreach ($this->lines as $contratline) {
388 if ($contratline->statut != ContratLigne::STATUS_OPEN) {
389 $contratline->context = $this->context;
391 $result = $contratline->active_line($user, $date_start, !empty($date_end) ? $date_end : -1, $comment);
394 $this->error = $contratline->error;
395 $this->errors = $contratline->errors;
401 if (!$error && $this->statut == 0) {
402 $result = $this->
validate($user,
'', $notrigger);
412 $this->
db->rollback();
437 foreach ($this->lines as $contratline) {
439 if ($contratline->statut != ContratLigne::STATUS_CLOSED) {
440 $contratline->date_end_real = $now;
441 $contratline->date_cloture = $now;
442 $contratline->fk_user_cloture = $user->id;
443 $contratline->statut = ContratLigne::STATUS_CLOSED;
444 $result = $contratline->close_line($user, $now, $comment, $notrigger);
447 $this->error = $contratline->error;
448 $this->errors = $contratline->errors;
454 if (!$error && $this->statut == 0) {
455 $result = $this->
validate($user,
'', $notrigger);
465 $this->
db->rollback();
478 public function validate(
User $user, $force_number =
'', $notrigger = 0)
480 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
481 global $langs, $conf;
486 dol_syslog(get_class($this).
'::validate user='.$user->id.
', force_number='.$force_number);
494 if (empty($conf->global->CONTRACT_DISABLE_AUTOSET_AS_CLIENT_ON_CONTRACT_VALIDATION)) {
495 $result = $this->thirdparty->set_as_client();
500 $num = $force_number;
501 } elseif (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
509 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contrat SET ref = '".$this->
db->escape($num).
"', statut = 1";
511 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut = 0";
513 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
518 $this->error = $this->
db->lasterror();
522 if (!$error && !$notrigger) {
524 $result = $this->
call_trigger(
'CONTRACT_VALIDATE', $user);
532 $this->oldref = $this->ref;
535 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
537 $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).
"'";
538 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'contract/".$this->
db->escape($this->
ref).
"' and entity = ".$conf->entity;
541 $error++; $this->error = $this->
db->lasterror();
547 $dirsource = $conf->contract->dir_output.
'/'.$oldref;
548 $dirdest = $conf->contract->dir_output.
'/'.$newref;
549 if (!$error && file_exists($dirsource)) {
550 dol_syslog(get_class($this).
"::validate rename dir ".$dirsource.
" into ".$dirdest);
552 if (@rename($dirsource, $dirdest)) {
555 $listoffiles =
dol_dir_list($conf->contract->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
556 foreach ($listoffiles as $fileentry) {
557 $dirsource = $fileentry[
'name'];
558 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
559 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
560 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
561 @rename($dirsource, $dirdest);
572 $this->brouillon = 0;
573 $this->date_validation = $now;
583 $this->
db->rollback();
595 public function reopen($user, $notrigger = 0)
597 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
598 global $langs, $conf;
603 dol_syslog(get_class($this).
'::reopen user='.$user->id);
609 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contrat SET statut = 0";
611 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut = 1";
613 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
618 $this->error = $this->
db->lasterror();
622 if (!$error && !$notrigger) {
624 $result = $this->
call_trigger(
'CONTRACT_REOPEN', $user);
634 $this->brouillon = 1;
635 $this->date_validation = $now;
642 $this->
db->rollback();
656 public function fetch($id, $ref =
'', $ref_customer =
'', $ref_supplier =
'')
658 $sql =
"SELECT rowid, statut, ref, fk_soc,";
659 $sql .=
" ref_supplier, ref_customer,";
662 $sql .=
" date_contrat as datecontrat,";
663 $sql .=
" fk_user_author,";
664 $sql .=
" fk_projet as fk_project,";
665 $sql .=
" fk_commercial_signature, fk_commercial_suivi,";
666 $sql .=
" note_private, note_public, model_pdf, last_main_doc, extraparams";
667 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat";
669 $sql .=
" WHERE entity IN (".getEntity(
'contract').
")";
671 $sql .=
" WHERE rowid = ".(int) $id;
674 $sql .=
" AND ref_customer = '".$this->db->escape($ref_customer).
"'";
677 $sql .=
" AND ref_supplier = '".$this->db->escape($ref_supplier).
"'";
680 $sql .=
" AND ref = '".$this->db->escape($ref).
"'";
683 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
688 $this->error =
'Fetch found several records.';
692 $obj = $this->
db->fetch_object(
$resql);
694 $this->
id = $obj->rowid;
695 $this->
ref = (!isset($obj->ref) || !$obj->ref) ? $obj->rowid : $obj->ref;
696 $this->ref_customer = $obj->ref_customer;
697 $this->ref_supplier = $obj->ref_supplier;
698 $this->ref_ext = $obj->ref_ext;
699 $this->entity = $obj->entity;
700 $this->statut = $obj->statut;
702 $this->date_contrat = $this->db->jdate($obj->datecontrat);
703 $this->date_creation = $this->
db->jdate($obj->datecontrat);
705 $this->user_author_id = $obj->fk_user_author;
707 $this->commercial_signature_id = $obj->fk_commercial_signature;
708 $this->commercial_suivi_id = $obj->fk_commercial_suivi;
710 $this->note_private = $obj->note_private;
711 $this->note_public = $obj->note_public;
712 $this->model_pdf = $obj->model_pdf;
713 $this->modelpdf = $obj->model_pdf;
715 $this->fk_projet = $obj->fk_project;
716 $this->fk_project = $obj->fk_project;
718 $this->socid = $obj->fk_soc;
719 $this->fk_soc = $obj->fk_soc;
720 $this->last_main_doc = $obj->last_main_doc;
721 $this->extraparams = (isset($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) :
null);
730 if ($result >= 0 && !empty($this->table_element_line)) {
735 $this->error = $this->
db->lasterror();
742 dol_syslog(get_class($this).
"::fetch Contract not found");
743 $this->error =
"Contract not found";
747 dol_syslog(get_class($this).
"::fetch Error searching contract");
748 $this->error = $this->
db->error();
762 public function fetch_lines($only_services = 0, $loadalsotranslation = 0)
765 global $langs, $conf;
767 $this->nbofservices = 0;
768 $this->nbofserviceswait = 0;
769 $this->nbofservicesopened = 0;
770 $this->nbofservicesexpired = 0;
771 $this->nbofservicesclosed = 0;
779 $this->lines = array();
783 $sql =
"SELECT p.label as product_label, p.description as product_desc, p.ref as product_ref, p.fk_product_type as product_type,";
784 $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,";
785 $sql .=
" d.total_ht,";
786 $sql .=
" d.total_tva,";
787 $sql .=
" d.total_localtax1,";
788 $sql .=
" d.total_localtax2,";
789 $sql .=
" d.total_ttc,";
790 $sql .=
" d.info_bits, d.fk_product,";
791 $sql .=
" d.date_ouverture_prevue as date_start,";
792 $sql .=
" d.date_ouverture as date_start_real,";
793 $sql .=
" d.date_fin_validite as date_end,";
794 $sql .=
" d.date_cloture as date_end_real,";
795 $sql .=
" d.fk_user_author,";
796 $sql .=
" d.fk_user_ouverture,";
797 $sql .=
" d.fk_user_cloture,";
798 $sql .=
" d.fk_unit,";
799 $sql .=
" d.product_type as type,";
801 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as d LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON d.fk_product = p.rowid";
802 $sql .=
" WHERE d.fk_contrat = ".((int) $this->
id);
803 if ($only_services == 1) {
804 $sql .=
" AND d.product_type = 1";
806 $sql .=
" ORDER by d.rang ASC";
808 dol_syslog(get_class($this).
"::fetch_lines", LOG_DEBUG);
809 $result = $this->
db->query($sql);
811 $num = $this->
db->num_rows($result);
815 $objp = $this->
db->fetch_object($result);
819 $line->id = $objp->rowid;
820 $line->ref = $objp->rowid;
821 $line->fk_contrat = $objp->fk_contrat;
822 $line->desc = $objp->description;
823 $line->qty = $objp->qty;
824 $line->vat_src_code = $objp->vat_src_code;
825 $line->tva_tx = $objp->tva_tx;
826 $line->localtax1_tx = $objp->localtax1_tx;
827 $line->localtax2_tx = $objp->localtax2_tx;
828 $line->localtax1_type = $objp->localtax1_type;
829 $line->localtax2_type = $objp->localtax2_type;
830 $line->subprice = $objp->subprice;
831 $line->statut = $objp->statut;
832 $line->remise_percent = $objp->remise_percent;
833 $line->price_ht = $objp->price_ht;
834 $line->price = $objp->price_ht;
835 $line->total_ht = $objp->total_ht;
836 $line->total_tva = $objp->total_tva;
837 $line->total_localtax1 = $objp->total_localtax1;
838 $line->total_localtax2 = $objp->total_localtax2;
839 $line->total_ttc = $objp->total_ttc;
840 $line->fk_product = (($objp->fk_product > 0) ? $objp->fk_product : 0);
841 $line->info_bits = $objp->info_bits;
842 $line->type = $objp->type;
844 $line->fk_fournprice = $objp->fk_fournprice;
845 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_fournprice, $objp->pa_ht);
846 $line->pa_ht = $marginInfos[0];
848 $line->fk_user_author = $objp->fk_user_author;
849 $line->fk_user_ouverture = $objp->fk_user_ouverture;
850 $line->fk_user_cloture = $objp->fk_user_cloture;
851 $line->fk_unit = $objp->fk_unit;
853 $line->ref = $objp->product_ref;
854 $line->product_ref = $objp->product_ref;
855 $line->product_type = $objp->product_type;
856 $line->product_desc = $objp->product_desc;
857 $line->product_label = $objp->product_label;
859 $line->description = $objp->description;
861 $line->date_start = $this->
db->jdate($objp->date_start);
862 $line->date_start_real = $this->
db->jdate($objp->date_start_real);
863 $line->date_end = $this->
db->jdate($objp->date_end);
864 $line->date_end_real = $this->
db->jdate($objp->date_end_real);
875 $line->rang = $objp->rang;
879 $line->fetch_optionals();
882 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
884 $tmpproduct->fetch($objp->fk_product);
885 $tmpproduct->getMultiLangs();
887 $line->multilangs = $tmpproduct->multilangs;
890 $this->lines[$pos] = $line;
892 $this->lines_id_index_mapper[$line->id] = $pos;
897 if ($line->statut == ContratLigne::STATUS_INITIAL) {
898 $this->nbofserviceswait++;
900 if ($line->statut == ContratLigne::STATUS_OPEN && (empty($line->date_end) || $line->date_end >= $now)) {
901 $this->nbofservicesopened++;
903 if ($line->statut == ContratLigne::STATUS_OPEN && (!empty($line->date_end) && $line->date_end < $now)) {
904 $this->nbofservicesexpired++;
906 if ($line->statut == ContratLigne::STATUS_CLOSED) {
907 $this->nbofservicesclosed++;
910 $total_ttc += $objp->total_ttc;
911 $total_vat += $objp->total_tva;
912 $total_ht += $objp->total_ht;
917 $this->
db->free($result);
919 dol_syslog(get_class($this).
"::Fetch Error when reading lines of contracts linked to products");
924 $this->nbofservices = count($this->lines);
925 $this->total_ttc =
price2num($total_ttc);
926 $this->total_tva =
price2num($total_vat);
940 global $conf, $langs, $mysoc;
944 if ($this->commercial_signature_id <= 0) {
945 $langs->load(
"commercial");
946 $this->error .= $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"SalesRepresentativeSignature"));
949 if ($this->commercial_suivi_id <= 0) {
950 $langs->load(
"commercial");
951 $this->error .= ($this->error ?
"<br>" :
'');
952 $this->error .= $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"SalesRepresentativeFollowUp"));
965 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contrat (datec, fk_soc, fk_user_author, date_contrat,";
966 $sql .=
" fk_commercial_signature, fk_commercial_suivi, fk_projet,";
967 $sql .=
" ref, entity, note_private, note_public, ref_customer, ref_supplier, ref_ext)";
968 $sql .=
" VALUES ('".$this->db->idate($now).
"', ".((int) $this->socid).
", ".((int) $user->id);
969 $sql .=
", ".(dol_strlen($this->date_contrat) != 0 ?
"'".$this->db->idate($this->date_contrat).
"'" :
"NULL");
970 $sql .=
",".($this->commercial_signature_id > 0 ? ((int) $this->commercial_signature_id) :
"NULL");
971 $sql .=
",".($this->commercial_suivi_id > 0 ? ((int) $this->commercial_suivi_id) :
"NULL");
972 $sql .=
",".($this->fk_project > 0 ? ((int) $this->fk_project) :
"NULL");
973 $sql .=
", ".(dol_strlen($this->
ref) <= 0 ?
"null" :
"'".$this->db->escape($this->
ref).
"'");
974 $sql .=
", ".((int) $conf->entity);
975 $sql .=
", ".(!empty($this->note_private) ? (
"'".$this->db->escape($this->note_private).
"'") :
"NULL");
976 $sql .=
", ".(!empty($this->note_public) ? (
"'".$this->db->escape($this->note_public).
"'") :
"NULL");
977 $sql .=
", ".(!empty($this->ref_customer) ? (
"'".$this->db->escape($this->ref_customer).
"'") :
"NULL");
978 $sql .=
", ".(!empty($this->ref_supplier) ? (
"'".$this->db->escape($this->ref_supplier).
"'") :
"NULL");
979 $sql .=
", ".(!empty($this->ref_ext) ? (
"'".$this->db->escape($this->ref_ext).
"'") :
"NULL");
986 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"contrat");
989 $module = (!empty($conf->global->CONTRACT_ADDON) ? $conf->global->CONTRACT_ADDON :
'mod_contract_serpis');
990 if (substr($module, 0, 13) ==
'mod_contract_' && substr($module, -3) ==
'php') {
991 $module = substr($module, 0,
dol_strlen($module) - 4);
995 $modCodeContract =
new $module();
997 if (!empty($modCodeContract->code_auto)) {
999 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"contrat SET ref='(PROV".$this->
id.
")' WHERE rowid=".((int) $this->
id);
1000 if ($this->
db->query($sql)) {
1002 $this->
ref =
"(PROV".$this->id.
")";
1017 $result = $this->
add_contact($this->commercial_signature_id,
'SALESREPSIGN',
'internal');
1025 $result = $this->
add_contact($this->commercial_suivi_id,
'SALESREPFOLL',
'internal');
1032 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
1033 $this->linked_objects = $this->linkedObjectsIds;
1037 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
1038 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
1039 if (is_array($tmp_origin_id)) {
1040 foreach ($tmp_origin_id as $origin_id) {
1043 $this->error = $this->
db->lasterror();
1049 $origin_id = $tmp_origin_id;
1052 $this->error = $this->
db->lasterror();
1059 if (!$error && $this->
id && !empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && !empty($this->origin) && !empty($this->origin_id)) {
1060 $originforcontact = $this->origin;
1061 $originidforcontact = $this->origin_id;
1062 if ($originforcontact ==
'shipping') {
1063 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1065 $exp->fetch($this->origin_id);
1066 $exp->fetchObjectLinked();
1067 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
1068 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
1069 $originforcontact =
'commande';
1070 $originidforcontact = $value;
1076 $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";
1077 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->
db->escape($originforcontact).
"'";
1079 $resqlcontact = $this->
db->query($sqlcontact);
1080 if ($resqlcontact) {
1081 while ($objcontact = $this->
db->fetch_object($resqlcontact)) {
1082 if ($objcontact->source ==
'internal' && in_array($objcontact->code, array(
'SALESREPSIGN',
'SALESREPFOLL'))) {
1087 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
1097 $result = $this->
call_trigger(
'CONTRACT_CREATE', $user);
1104 $this->
db->commit();
1107 dol_syslog(get_class($this).
"::create - 30 - ".$this->error, LOG_ERR);
1108 $this->
db->rollback();
1112 $this->error =
"Failed to add contract";
1113 dol_syslog(get_class($this).
"::create - 20 - ".$this->error, LOG_ERR);
1114 $this->
db->rollback();
1118 $this->error = $langs->trans(
"UnknownError: ".$this->
db->error().
" -", LOG_DEBUG);
1120 $this->
db->rollback();
1132 public function delete($user)
1134 global $conf, $langs;
1135 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1142 $result = $this->
call_trigger(
'CONTRACT_DELETE', $user);
1152 dol_syslog(get_class($this).
"::delete error", LOG_ERR);
1172 $sql =
"SELECT cdl.rowid as cdlrowid ";
1173 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet_log as cdl, ".MAIN_DB_PREFIX.
"contratdet as cd";
1174 $sql .=
" WHERE cdl.fk_contratdet=cd.rowid AND cd.fk_contrat=".((int) $this->
id);
1176 dol_syslog(get_class($this).
"::delete contratdet_log", LOG_DEBUG);
1179 $this->error = $this->
db->error();
1182 $numressql = $this->
db->num_rows(
$resql);
1183 if (!$error && $numressql) {
1184 $tab_resql = array();
1185 for ($i = 0; $i < $numressql; $i++) {
1186 $objresql = $this->
db->fetch_object(
$resql);
1187 $tab_resql[] = $objresql->cdlrowid;
1191 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"contratdet_log ";
1192 $sql .=
" WHERE ".MAIN_DB_PREFIX.
"contratdet_log.rowid IN (".$this->
db->sanitize(implode(
",", $tab_resql)).
")";
1194 dol_syslog(get_class($this).
"::delete contratdet_log", LOG_DEBUG);
1197 $this->error = $this->
db->error();
1206 $main = MAIN_DB_PREFIX.
'contratdet';
1207 $ef = $main.
"_extrafields";
1208 $sql =
"DELETE FROM ".$ef.
" WHERE fk_object IN (SELECT rowid FROM ".$main.
" WHERE fk_contrat = ".((int) $this->
id).
")";
1210 dol_syslog(get_class($this).
"::delete contratdet_extrafields", LOG_DEBUG);
1213 $this->error = $this->
db->error();
1220 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"contratdet";
1221 $sql .=
" WHERE fk_contrat=".((int) $this->
id);
1223 dol_syslog(get_class($this).
"::delete contratdet", LOG_DEBUG);
1226 $this->error = $this->
db->error();
1233 $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);
1236 $this->error = $this->
db->lasterror();
1237 $this->errors[] = $this->error;
1244 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"contrat";
1245 $sql .=
" WHERE rowid=".((int) $this->
id);
1247 dol_syslog(get_class($this).
"::delete contrat", LOG_DEBUG);
1250 $this->error = $this->
db->error();
1260 dol_syslog(get_class($this).
"::delete error -3 ".$this->error, LOG_ERR);
1267 if ($conf->contrat->dir_output) {
1268 $dir = $conf->contrat->multidir_output[$this->entity].
"/".$ref;
1269 if (file_exists($dir)) {
1272 $this->error =
'ErrorFailToDeleteDir';
1280 $this->
db->commit();
1283 $this->error = $this->
db->lasterror();
1284 $this->
db->rollback();
1296 public function update($user, $notrigger = 0)
1298 global $conf, $langs;
1302 if (empty($this->fk_commercial_signature) && $this->commercial_signature_id > 0) {
1303 $this->fk_commercial_signature = $this->commercial_signature_id;
1305 if (empty($this->fk_commercial_suivi) && $this->commercial_suivi_id > 0) {
1306 $this->fk_commercial_suivi = $this->commercial_suivi_id;
1308 if (empty($this->fk_soc) && $this->socid > 0) {
1309 $this->fk_soc = (int) $this->socid;
1311 if (empty($this->fk_project) && $this->projet > 0) {
1312 $this->fk_project = (int) $this->projet;
1315 if (isset($this->
ref)) {
1316 $this->
ref = trim($this->
ref);
1318 if (isset($this->ref_customer)) {
1319 $this->ref_customer = trim($this->ref_customer);
1321 if (isset($this->ref_supplier)) {
1322 $this->ref_supplier = trim($this->ref_supplier);
1324 if (isset($this->ref_ext)) {
1325 $this->ref_ext = trim($this->ref_ext);
1327 if (isset($this->entity)) {
1328 $this->entity = (int) $this->entity;
1330 if (isset($this->statut)) {
1331 $this->statut = (int) $this->statut;
1333 if (isset($this->fk_soc)) {
1334 $this->fk_soc = (int) $this->fk_soc;
1336 if (isset($this->fk_commercial_signature)) {
1337 $this->fk_commercial_signature = trim($this->fk_commercial_signature);
1339 if (isset($this->fk_commercial_suivi)) {
1340 $this->fk_commercial_suivi = trim($this->fk_commercial_suivi);
1342 if (isset($this->note_private)) {
1343 $this->note_private = trim($this->note_private);
1345 if (isset($this->note_public)) {
1346 $this->note_public = trim($this->note_public);
1348 if (isset($this->import_key)) {
1349 $this->import_key = trim($this->import_key);
1357 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contrat SET";
1358 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
1359 $sql .=
" ref_customer=".(isset($this->ref_customer) ?
"'".$this->db->escape($this->ref_customer).
"'" :
"null").
",";
1360 $sql .=
" ref_supplier=".(isset($this->ref_supplier) ?
"'".$this->db->escape($this->ref_supplier).
"'" :
"null").
",";
1361 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
1362 $sql .=
" entity=".$conf->entity.
",";
1363 $sql .=
" date_contrat=".(dol_strlen($this->date_contrat) != 0 ?
"'".$this->db->idate($this->date_contrat).
"'" :
'null').
",";
1364 $sql .=
" statut=".(isset($this->statut) ? $this->statut :
"null").
",";
1365 $sql .=
" fk_soc=".($this->fk_soc > 0 ? $this->fk_soc :
"null").
",";
1366 $sql .=
" fk_projet=".($this->fk_project > 0 ? $this->fk_project :
"null").
",";
1367 $sql .=
" fk_commercial_signature=".(isset($this->fk_commercial_signature) ? $this->fk_commercial_signature :
"null").
",";
1368 $sql .=
" fk_commercial_suivi=".(isset($this->fk_commercial_suivi) ? $this->fk_commercial_suivi :
"null").
",";
1369 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
1370 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
1371 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null");
1373 $sql .=
" WHERE rowid=".((int) $this->
id);
1379 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
1389 if (!$error && !$notrigger) {
1391 $result = $this->
call_trigger(
'CONTRACT_MODIFY', $user);
1400 foreach ($this->errors as $errmsg) {
1401 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
1402 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
1404 $this->
db->rollback();
1407 $this->
db->commit();
1436 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)
1438 global $user, $langs, $conf, $mysoc;
1441 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");
1444 if ($fk_product <= 0 && empty($desc)) {
1445 $this->error =
"ErrorDescRequiredForFreeProductLines";
1449 if ($this->statut >= 0) {
1458 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1459 $vat_src_code = $reg[1];
1460 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1466 $remise_percent =
price2num($remise_percent);
1471 if (empty($info_bits)) {
1474 if (empty($pu_ht) || !is_numeric($pu_ht)) {
1477 if (empty($pu_ttc)) {
1480 if (empty($txtva) || !is_numeric($txtva)) {
1483 if (empty($txlocaltax1) || !is_numeric($txlocaltax1)) {
1486 if (empty($txlocaltax2) || !is_numeric($txlocaltax2)) {
1490 if ($price_base_type ==
'HT') {
1497 if (empty($remise_percent)) {
1498 $remise_percent = 0;
1504 if ($date_start && $date_end && $date_start > $date_end) {
1505 $langs->load(
"errors");
1506 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1512 $localtaxes_type =
getLocalTaxesFromRate($txtva.($vat_src_code ?
' ('.$vat_src_code.
')' :
''), 0, $this->societe, $mysoc);
1519 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1520 $total_ht = $tabprice[0];
1521 $total_tva = $tabprice[1];
1522 $total_ttc = $tabprice[2];
1523 $total_localtax1 = $tabprice[9];
1524 $total_localtax2 = $tabprice[10];
1526 $localtax1_type = $localtaxes_type[0];
1527 $localtax2_type = $localtaxes_type[2];
1534 $remise = round(($pu_ht * $remise_percent / 100), 2);
1535 $price = $pu_ht - $remise;
1538 if (empty($pa_ht)) {
1544 if ($this->pa_ht == 0) {
1545 if (($result = $this->
defineBuyPrice($pu_ht, $remise_percent, $fk_product)) < 0) {
1553 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
1554 $sql .=
" (fk_contrat, label, description, fk_product, qty, tva_tx, vat_src_code,";
1555 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
1556 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
1557 $sql .=
" info_bits,";
1558 $sql .=
" price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
1559 if ($date_start > 0) {
1560 $sql .=
",date_ouverture_prevue";
1562 if ($date_end > 0) {
1563 $sql .=
",date_fin_validite";
1565 $sql .=
", fk_unit";
1567 $sql .=
") VALUES (";
1568 $sql .= $this->
id.
", '', '".$this->
db->escape($desc).
"',";
1569 $sql .= ($fk_product > 0 ? $fk_product :
"null").
",";
1570 $sql .=
" ".((float) $qty).
",";
1571 $sql .=
" ".((float) $txtva).
",";
1572 $sql .=
" ".($vat_src_code ?
"'".$this->db->escape($vat_src_code).
"'" :
"null").
",";
1573 $sql .=
" ".((float) $txlocaltax1).
",";
1574 $sql .=
" ".((float) $txlocaltax2).
",";
1575 $sql .=
" '".$this->db->escape($localtax1_type).
"',";
1576 $sql .=
" '".$this->db->escape($localtax2_type).
"',";
1577 $sql .=
" ".price2num($remise_percent).
",";
1578 $sql .=
" ".price2num($pu_ht).
",";
1580 $sql .=
" '".$this->db->escape($info_bits).
"',";
1581 $sql .=
" ".price2num($price).
",".
price2num($remise).
",";
1582 if (isset($fk_fournprice)) {
1583 $sql .=
' '.((int) $fk_fournprice).
',';
1587 if (isset($pa_ht)) {
1588 $sql .=
' '.price2num($pa_ht);
1592 if ($date_start > 0) {
1593 $sql .=
",'".$this->db->idate($date_start).
"'";
1595 if ($date_end > 0) {
1596 $sql .=
",'".$this->db->idate($date_end).
"'";
1598 $sql .=
", ".($fk_unit ?
"'".$this->db->escape($fk_unit).
"'" :
"null");
1599 $sql .=
", ".(!empty($rang) ? (int) $rang :
"0");
1604 $contractlineid = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"contratdet");
1608 $contractline->array_options = $array_options;
1609 $contractline->id = $contractlineid;
1610 $result = $contractline->insertExtraFields();
1612 $this->error[] = $contractline->error;
1617 if (empty($error)) {
1619 $result = $this->
call_trigger(
'LINECONTRACT_INSERT', $user);
1627 $this->
db->rollback();
1630 $this->
db->commit();
1631 return $contractlineid;
1634 $this->
db->rollback();
1635 $this->error = $this->
db->error().
" sql=".$sql;
1639 dol_syslog(get_class($this).
"::addline ErrorTryToAddLineOnValidatedContract", LOG_ERR);
1668 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)
1670 global $user, $conf, $langs, $mysoc;
1676 $desc = trim($desc);
1677 $desc = trim($desc);
1683 if (empty($fk_fournprice)) {
1693 $remise = round(($pu * $remise_percent / 100), 2);
1694 $price = $pu - $remise;
1696 $remise_percent = 0;
1699 if ($date_start && $date_end && $date_start > $date_end) {
1700 $langs->load(
"errors");
1701 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
1705 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");
1715 $tvatx = preg_replace(
'/\s*\(.*\)/',
'', $tvatx);
1717 $tabprice =
calcul_price_total($qty, $pu, $remise_percent, $tvatx, $localtax1tx, $localtax2tx, 0, $price_base_type, $info_bits, 1, $mysoc, $localtaxes_type);
1718 $total_ht = $tabprice[0];
1719 $total_tva = $tabprice[1];
1720 $total_ttc = $tabprice[2];
1721 $total_localtax1 = $tabprice[9];
1722 $total_localtax2 = $tabprice[10];
1724 $localtax1_type = (empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0]);
1725 $localtax2_type = (empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2]);
1732 $remise = round(($pu * $remise_percent / 100), 2);
1733 $price = $pu - $remise;
1736 if (empty($pa_ht)) {
1741 if ($this->pa_ht == 0) {
1742 if (($result = $this->
defineBuyPrice($pu, $remise_percent)) < 0) {
1749 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet set description = '".$this->
db->escape($desc).
"'";
1750 $sql .=
",price_ht = ".((float)
price2num($price));
1751 $sql .=
",subprice = ".((float)
price2num($subprice));
1752 $sql .=
",remise = ".((float)
price2num($remise));
1753 $sql .=
",remise_percent = ".((float)
price2num($remise_percent));
1754 $sql .=
",qty = ".((float) $qty);
1755 $sql .=
",tva_tx = ".((float)
price2num($tvatx));
1756 $sql .=
",localtax1_tx = ".((float)
price2num($localtax1tx));
1757 $sql .=
",localtax2_tx = ".((float)
price2num($localtax2tx));
1758 $sql .=
",localtax1_type='".$this->db->escape($localtax1_type).
"'";
1759 $sql .=
",localtax2_type='".$this->db->escape($localtax2_type).
"'";
1760 $sql .=
", total_ht = ".((float)
price2num($total_ht));
1761 $sql .=
", total_tva = ".((float)
price2num($total_tva));
1762 $sql .=
", total_localtax1 = ".((float)
price2num($total_localtax1));
1763 $sql .=
", total_localtax2 = ".((float)
price2num($total_localtax2));
1764 $sql .=
", total_ttc = ".((float)
price2num($total_ttc));
1765 $sql .=
", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice :
"null");
1766 $sql .=
", buy_price_ht = ".((float)
price2num($pa_ht));
1767 if ($date_start > 0) {
1768 $sql .=
",date_ouverture_prevue = '".$this->db->idate($date_start).
"'";
1770 $sql .=
",date_ouverture_prevue = null";
1772 if ($date_end > 0) {
1773 $sql .=
",date_fin_validite = '".$this->db->idate($date_end).
"'";
1775 $sql .=
",date_fin_validite = null";
1777 if ($date_start_real > 0) {
1778 $sql .=
",date_ouverture = '".$this->db->idate($date_start_real).
"'";
1780 $sql .=
",date_ouverture = null";
1782 if ($date_end_real > 0) {
1783 $sql .=
",date_cloture = '".$this->db->idate($date_end_real).
"'";
1785 $sql .=
",date_cloture = null";
1787 $sql .=
", fk_unit = ".($fk_unit > 0 ? ((int) $fk_unit) :
"null");
1788 $sql .=
", rang = ".(!empty($rang) ? ((int) $rang) :
"0");
1789 $sql .=
" WHERE rowid = ".((int) $rowid);
1791 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
1792 $result = $this->
db->query($sql);
1794 if (is_array($array_options) && count($array_options) > 0) {
1796 $contractline->fetch($rowid);
1799 foreach ($array_options as $key => $value) {
1800 $contractline->array_options[$key] = $array_options[$key];
1803 $result = $contractline->insertExtraFields();
1805 $this->error[] = $contractline->error;
1810 if (empty($error)) {
1812 $result = $this->
call_trigger(
'LINECONTRACT_MODIFY', $user);
1814 $this->
db->rollback();
1819 $this->
db->commit();
1823 $this->
db->rollback();
1824 $this->error = $this->
db->error();
1825 dol_syslog(get_class($this).
"::updateline Erreur -1");
1839 global $conf, $langs;
1843 if ($this->statut >= 0) {
1845 $result = $this->
call_trigger(
'LINECONTRACT_DELETE', $user);
1853 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$this->table_element_line;
1854 $sql .=
" WHERE rowid = ".((int) $idline);
1856 dol_syslog(get_class($this).
"::deleteline", LOG_DEBUG);
1859 $this->error =
"Error ".$this->db->lasterror();
1866 $contractline->id = $idline;
1867 $result = $contractline->deleteExtraFields();
1870 $this->error =
"Error ".get_class($this).
"::deleteline deleteExtraFields error -4 ".$contractline->error;
1874 if (empty($error)) {
1875 $this->
db->commit();
1878 dol_syslog(get_class($this).
"::deleteline ERROR:".$this->error, LOG_ERR);
1879 $this->
db->rollback();
1883 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
1900 dol_syslog(__METHOD__.
" is deprecated", LOG_WARNING);
1903 if ($this->statut == 0) {
1928 return $this->
LibStatut($this->statut, $mode);
1944 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1946 $langs->load(
"contracts");
1947 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'ContractStatusDraft');
1948 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'ContractStatusValidated');
1949 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv(
'ContractStatusClosed');
1950 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv(
'ContractStatusDraft');
1951 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv(
'ContractStatusValidated');
1952 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv(
'ContractStatusClosed');
1955 $statusType =
'status'.$status;
1956 if ($status == self::STATUS_VALIDATED) {
1957 $statusType =
'status6';
1960 if ($mode == 4 || $mode == 6 || $mode == 7) {
1963 $text =
'<span class="hideonsmartphone">';
1964 $text .= ($this->nbofserviceswait + $this->nbofservicesopened + $this->nbofservicesexpired + $this->nbofservicesclosed);
1965 $text .=
' '.$langs->trans(
"Services");
1966 $text .=
': ';
1969 $text .= ($mode == 7 ?
'<span class="nowraponall">' :
'');
1970 $text .= ($mode != 7 || $this->nbofserviceswait > 0) ? ($this->nbofserviceswait.ContratLigne::LibStatut(0, 3, -1,
'class="marginleft2"')).(($mode != 7 || $this->nbofservicesopened || $this->nbofservicesexpired || $this->nbofservicesclosed) ?
' ' :
'') :
'';
1971 $text .= ($mode == 7 ?
'</span><span class="nowraponall">' :
'');
1972 $text .= ($mode != 7 || $this->nbofservicesopened > 0) ? ($this->nbofservicesopened.ContratLigne::LibStatut(4, 3, 0,
'class="marginleft2"')).(($mode != 7 || $this->nbofservicesexpired || $this->nbofservicesclosed) ?
' ' :
'') :
'';
1973 $text .= ($mode == 7 ?
'</span><span class="nowraponall">' :
'');
1974 $text .= ($mode != 7 || $this->nbofservicesexpired > 0) ? ($this->nbofservicesexpired.ContratLigne::LibStatut(4, 3, 1,
'class="marginleft2"')).(($mode != 7 || $this->nbofservicesclosed) ?
' ' :
'') :
'';
1975 $text .= ($mode == 7 ?
'</span><span class="nowraponall">' :
'');
1976 $text .= ($mode != 7 || $this->nbofservicesclosed > 0) ? ($this->nbofservicesclosed.ContratLigne::LibStatut(5, 3, -1,
'class="marginleft2"')) :
'';
1977 $text .= ($mode == 7 ?
'</span>' :
'');
1980 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
1994 public function getNomUrl($withpicto = 0, $maxlength = 0, $notooltip = 0, $save_lastsearch_value = -1)
1996 global $conf, $langs, $user, $hookmanager;
2000 $url = DOL_URL_ROOT.
'/contrat/card.php?id='.$this->id;
2005 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2006 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
2007 $add_save_lastsearch_values = 1;
2009 if ($add_save_lastsearch_values) {
2010 $url .=
'&save_lastsearch_values=1';
2016 if ($user->rights->contrat->lire) {
2017 $label =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"Contract").
'</u>';
2022 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.($this->
ref ? $this->
ref : $this->id);
2023 $ref_customer = (!empty($this->ref_customer) ? $this->ref_customer : (empty($this->ref_client) ?
'' : $this->ref_client));
2024 $label .=
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.$ref_customer;
2025 $label .=
'<br><b>'.$langs->trans(
'RefSupplier').
':</b> '.$this->ref_supplier;
2026 if (!empty($this->total_ht)) {
2027 $label .=
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
2029 if (!empty($this->total_tva)) {
2030 $label .=
'<br><b>'.$langs->trans(
'VAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
2032 if (!empty($this->total_ttc)) {
2033 $label .=
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
2038 if (empty($notooltip) && $user->rights->contrat->lire) {
2039 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
2040 $label = $langs->trans(
"ShowOrder");
2041 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
2043 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
2044 $linkclose .=
' class="classfortooltip"';
2047 $linkstart =
'<a href="'.$url.
'"';
2048 $linkstart .= $linkclose.
'>';
2051 $result .= $linkstart;
2053 $result .=
img_object(($notooltip ?
'' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
2055 if ($withpicto != 2) {
2056 $result .= ($this->
ref ? $this->
ref : $this->id);
2058 $result .= $linkend;
2061 $hookmanager->initHooks(array(
'contractdao'));
2062 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
2063 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
2065 $result = $hookmanager->resPrint;
2067 $result .= $hookmanager->resPrint;
2081 $sql =
"SELECT c.rowid, c.ref, c.datec,";
2082 $sql .=
" c.tms as date_modification,";
2083 $sql .=
" fk_user_author";
2084 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2085 $sql .=
" WHERE c.rowid = ".((int) $id);
2087 $result = $this->
db->query($sql);
2089 if ($this->
db->num_rows($result)) {
2090 $obj = $this->
db->fetch_object($result);
2092 $this->
id = $obj->rowid;
2094 if ($obj->fk_user_author) {
2095 $cuser =
new User($this->
db);
2096 $cuser->fetch($obj->fk_user_author);
2097 $this->user_creation = $cuser;
2100 $this->
ref = (!$obj->ref) ? $obj->rowid : $obj->ref;
2101 $this->date_creation = $this->db->jdate($obj->datec);
2102 $this->date_modification = $this->
db->jdate($obj->date_modification);
2105 $this->
db->free($result);
2123 $sql =
"SELECT cd.rowid";
2124 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as cd";
2125 $sql .=
" WHERE fk_contrat =".((int) $this->
id);
2127 $sql .=
" AND statut = ".((int) $status);
2130 dol_syslog(get_class($this).
"::array_detail()", LOG_DEBUG);
2133 $num = $this->
db->num_rows(
$resql);
2136 $obj = $this->
db->fetch_object(
$resql);
2137 $tab[$i] = $obj->rowid;
2142 $this->error = $this->
db->error();
2156 public function getListOfContracts($option =
'all', $status = [], $product_categories = [], $line_status = [])
2160 $sql =
"SELECT c.rowid";
2161 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2162 if (!empty($product_categories)) {
2163 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"contratdet as cd ON cd.fk_contrat = c.rowid";
2164 $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)).
")";
2166 $sql .=
" WHERE c.fk_soc =".((int) $this->socid);
2167 $sql .= ($option ==
'others') ?
" AND c.rowid <> ".((
int) $this->id) :
"";
2168 $sql .= (!empty($status)) ?
" AND c.statut IN (".$this->
db->sanitize(implode(
', ', $status)).
")" :
"";
2169 $sql .= (!empty($line_status)) ?
" AND cd.statut IN (".$this->
db->sanitize(implode(
', ', $line_status)).
")" :
"";
2170 $sql .=
" GROUP BY c.rowid";
2172 dol_syslog(get_class($this).
"::getOtherContracts()", LOG_DEBUG);
2175 $num = $this->
db->num_rows(
$resql);
2178 $obj = $this->
db->fetch_object(
$resql);
2180 $contrat->fetch($obj->rowid);
2181 $tab[$contrat->id] = $contrat;
2186 $this->error = $this->
db->lasterror();
2203 global $conf, $langs;
2205 $this->from =
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2206 $this->from .=
", ".MAIN_DB_PREFIX.
"contratdet as cd";
2207 $this->from .=
", ".MAIN_DB_PREFIX.
"societe as s";
2208 if (empty($user->rights->societe->client->voir) && !$user->socid) {
2209 $this->from .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
2212 if ($mode ==
'inactive') {
2213 $sql =
"SELECT cd.rowid, cd.date_ouverture_prevue as datefin";
2214 $sql .= $this->from;
2215 $sql .=
" WHERE c.statut = 1";
2216 $sql .=
" AND c.rowid = cd.fk_contrat";
2217 $sql .=
" AND cd.statut = 0";
2218 } elseif ($mode ==
'expired') {
2219 $sql =
"SELECT cd.rowid, cd.date_fin_validite as datefin";
2220 $sql .= $this->from;
2221 $sql .=
" WHERE c.statut = 1";
2222 $sql .=
" AND c.rowid = cd.fk_contrat";
2223 $sql .=
" AND cd.statut = 4";
2224 $sql .=
" AND cd.date_fin_validite < '".$this->db->idate(
dol_now()).
"'";
2225 } elseif ($mode ==
'active') {
2226 $sql =
"SELECT cd.rowid, cd.date_fin_validite as datefin";
2227 $sql .= $this->from;
2228 $sql .=
" WHERE c.statut = 1";
2229 $sql .=
" AND c.rowid = cd.fk_contrat";
2230 $sql .=
" AND cd.statut = 4";
2234 $sql .=
" AND c.fk_soc = s.rowid";
2235 $sql .=
" AND c.entity = ".((int) $conf->entity);
2237 $sql .=
" AND c.fk_soc = ".((int) $user->socid);
2239 if (empty($user->rights->societe->client->voir) && !$user->socid) {
2240 $sql .=
" AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
2245 $langs->load(
"contracts");
2248 if ($mode ==
'inactive') {
2249 $warning_delay = $conf->contrat->services->inactifs->warning_delay;
2250 $label = $langs->trans(
"BoardNotActivatedServices");
2251 $labelShort = $langs->trans(
"BoardNotActivatedServicesShort");
2252 $url = DOL_URL_ROOT.
'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&mode=0&sortfield=cd.date_fin_validite&sortorder=asc';
2253 } elseif ($mode ==
'expired') {
2254 $warning_delay = $conf->contrat->services->expires->warning_delay;
2255 $url = DOL_URL_ROOT.
'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&mode=4&filter=expired&sortfield=cd.date_fin_validite&sortorder=asc';
2256 $label = $langs->trans(
"BoardExpiredServices");
2257 $labelShort = $langs->trans(
"BoardExpiredServicesShort");
2259 $warning_delay = $conf->contrat->services->expires->warning_delay;
2260 $url = DOL_URL_ROOT.
'/contrat/services_list.php?mainmenu=commercial&leftmenu=contracts&mode=4&sortfield=cd.date_fin_validite&sortorder=asc';
2263 $label = $langs->trans(
"BoardRunningServices");
2264 $labelShort = $langs->trans(
"BoardRunningServicesShort");
2268 $response->warning_delay = $warning_delay / 60 / 60 / 24;
2269 $response->label = $label;
2270 $response->labelShort = $labelShort;
2271 $response->url = $url;
2274 while ($obj = $this->
db->fetch_object(
$resql)) {
2275 $response->nbtodo++;
2277 if ($obj->datefin && $this->db->jdate($obj->datefin) < ($now - $warning_delay)) {
2278 $response->nbtodolate++;
2285 $this->error = $this->
db->error();
2299 global $conf, $user;
2301 $this->nb = array();
2304 $sql =
"SELECT count(c.rowid) as nb";
2305 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contrat as c";
2306 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON c.fk_soc = s.rowid";
2307 if (empty($user->rights->societe->client->voir) && !$user->socid) {
2308 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
2309 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
2312 $sql .=
" ".$clause.
" c.entity = ".$conf->entity;
2316 while ($obj = $this->
db->fetch_object(
$resql)) {
2317 $this->nb[
"contracts"] = $obj->nb;
2323 $this->error = $this->
db->error();
2361 global $user, $langs, $conf;
2366 $sql =
"SELECT rowid";
2367 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
2368 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
2369 $sql .=
" AND tosell = 1";
2370 $sql .= $this->
db->plimit(100);
2374 $num_prods = $this->
db->num_rows(
$resql);
2376 while ($i < $num_prods) {
2378 $row = $this->
db->fetch_row(
$resql);
2379 $prodids[$i] = $row[0];
2385 $this->specimen = 1;
2387 $this->
ref =
'SPECIMEN';
2388 $this->ref_customer =
'SPECIMENCUST';
2389 $this->ref_supplier =
'SPECIMENSUPP';
2392 $this->date_creation = (
dol_now() - 3600 * 24 * 7);
2393 $this->date_contrat =
dol_now();
2394 $this->commercial_signature_id = 1;
2395 $this->commercial_suivi_id = 1;
2396 $this->note_private =
'This is a comment (private)';
2397 $this->note_public =
'This is a comment (public)';
2398 $this->fk_projet = 0;
2402 while ($xnbp < $nbp) {
2405 $line->subprice = 100;
2407 $line->tva_tx = 19.6;
2408 $line->remise_percent = 10;
2409 $line->total_ht = 90;
2410 $line->total_ttc = 107.64;
2411 $line->total_tva = 17.64;
2412 $line->date_start =
dol_now() - 500000;
2413 $line->date_start_real =
dol_now() - 200000;
2414 $line->date_end =
dol_now() + 500000;
2415 $line->date_end_real =
dol_now() - 100000;
2416 if ($num_prods > 0) {
2417 $prodid = mt_rand(1, $num_prods);
2418 $line->fk_product = $prodids[$prodid];
2420 $this->lines[$xnbp] = $line;
2447 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
2449 global $conf, $langs;
2454 if (!empty($this->model_pdf)) {
2455 $modele = $this->model_pdf;
2456 } elseif (!empty($this->modelpdf)) {
2457 $modele = $this->modelpdf;
2458 } elseif (!empty($conf->global->CONTRACT_ADDON_PDF)) {
2459 $modele = $conf->global->CONTRACT_ADDON_PDF;
2463 if (empty($modele)) {
2466 $langs->load(
"contracts");
2467 $outputlangs->load(
"products");
2469 $modelpath =
"core/modules/contract/doc/";
2470 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
2518 global $db, $langs, $conf, $hookmanager, $extrafields;
2524 $this->
fetch($this->
id);
2527 $clonedObj = clone $this;
2528 $clonedObj->socid = $socid;
2534 $objsoc->fetch($clonedObj->socid);
2537 $clonedObj->statut = 0;
2539 if (is_array($clonedObj->array_options) && count($clonedObj->array_options) > 0) {
2540 $extrafields->fetch_name_optionals_label($this->table_element);
2541 foreach ($clonedObj->array_options as $key => $option) {
2542 $shortkey = preg_replace(
'/options_/',
'', $key);
2544 if (!empty($extrafields->attributes[$this->element][
'unique'][$shortkey])) {
2546 unset($clonedObj->array_options[$key]);
2551 if (empty($conf->global->CONTRACT_ADDON) || !is_readable(DOL_DOCUMENT_ROOT.
"/core/modules/contract/".$conf->global->CONTRACT_ADDON.
".php")) {
2552 $this->error =
'ErrorSetupNotComplete';
2558 require_once DOL_DOCUMENT_ROOT.
"/core/modules/contract/".$conf->global->CONTRACT_ADDON.
'.php';
2559 $obj = $conf->global->CONTRACT_ADDON;
2560 $modContract =
new $obj();
2561 $clonedObj->ref = $modContract->getNextValue($objsoc, $clonedObj);
2564 foreach ($this->lines as $line) {
2565 $line->fetch_optionals($line->id);
2569 $clonedObj->context[
'createfromclone'] =
'createfromclone';
2570 $result = $clonedObj->create($user);
2573 $this->error = $clonedObj->error;
2574 $this->errors[] = $clonedObj->error;
2577 if ($this->socid == $clonedObj->socid) {
2578 if ($clonedObj->copy_linked_contact($this,
'external') < 0) {
2585 foreach ($this->lines as $line) {
2586 $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);
2589 $this->error = $clonedObj->error;
2590 $this->errors[] = $clonedObj->error;
2597 if (is_object($hookmanager)) {
2598 $parameters = array(
2600 'clonedObj' => $clonedObj
2603 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $clonedObj, $action);
2610 unset($clonedObj->context[
'createfromclone']);
2614 $this->
db->commit();
2615 return $clonedObj->id;
2617 $this->
db->rollback();
2634 global $langs, $user;
2636 $langs->load(
"agenda");
2646 $contractlineprocessed = array();
2647 $contractignored = array();
2648 $contracterror = array();
2652 $sql =
'SELECT c.rowid, c.ref_customer, cd.rowid as lid, cd.date_fin_validite, p.duration';
2653 $sql.=
' FROM '.MAIN_DB_PREFIX.
'contrat as c, '.MAIN_DB_PREFIX.
'contratdet as cd';
2654 $sql.=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON p.rowid = cd.fk_product';
2655 $sql.=
' WHERE cd.fk_contrat = c.rowid';
2656 $sql.=
" AND date_format(cd.date_fin_validite, '%Y-%m-%d') <= date_format('".$this->db->idate($enddatetoscan).
"', '%Y-%m-%d')";
2657 $sql.=
" AND cd.statut = 4";
2658 if ($thirdparty_id > 0) $sql.=
" AND c.fk_soc = ".((int) $thirdparty_id);
2663 $num = $this->
db->num_rows(
$resql);
2665 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
2669 $obj = $this->
db->fetch_object(
$resql);
2671 if (!empty($contractlineprocessed[$obj->lid]) || !empty($contractignored[$obj->rowid]) || !empty($contracterror[$obj->rowid])) {
2677 $object->fetch($obj->rowid);
2678 $object->fetch_thirdparty();
2680 if ($object->id <= 0) {
2682 $this->errors[] =
'Failed to load contract with id='.$obj->rowid;
2686 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);
2689 $expirationdate = $this->
db->jdate($obj->date_fin_validite);
2690 $duration_value = preg_replace(
'/[^0-9]/',
'', $obj->duration);
2691 $duration_unit = preg_replace(
'/\d/',
'', $obj->duration);
2695 $object->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 1);
2697 if (is_array($object->linkedObjects[
'facture']) && count($object->linkedObjects[
'facture']) > 0) {
2698 usort($object->linkedObjects[
'facture'],
"cmp");
2701 $someinvoicenotpaid=0;
2702 foreach ($object->linkedObjects[
'facture'] as $idinvoice => $invoice) {
2705 if (empty($invoice->paye)) {
2706 $someinvoicenotpaid++;
2709 if ($someinvoicenotpaid) {
2710 $this->output .=
'Contract '.$object->ref.
' is qualified for renewal but there is '.$someinvoicenotpaid.
' invoice(s) unpayed so we cancel renewal'.
"\n";
2711 $contractignored[$object->id]=$object->ref;
2716 if ($expirationdate && $expirationdate < $enddatetoscan) {
2717 dol_syslog(
"Define the newdate of end of services from expirationdate=".$expirationdate);
2718 $newdate = $expirationdate;
2720 while ($newdate < $enddatetoscan && $protecti < 1000) {
2725 if ($protecti < 1000) {
2731 $errorforlocaltransaction = 0;
2733 $label =
'Renewal of contrat '.$object->ref.
' line '.$obj->lid;
2734 $comment =
'Renew date of contract '.$object->ref.
' line '.$obj->lid.
' by doAutoRenewContracts';
2736 $sqlupdate =
'UPDATE '.MAIN_DB_PREFIX.
"contratdet SET date_fin_validite = '".$this->
db->idate($newdate).
"'";
2737 $sqlupdate.=
' WHERE rowid = '.((int) $obj->lid);
2738 $resqlupdate = $this->
db->query($sqlupdate);
2740 $contractlineprocessed[$obj->lid]=$object->ref;
2742 $actioncode =
'RENEW_CONTRACT';
2747 $actioncomm->type_code =
'AC_OTH_AUTO';
2748 $actioncomm->code =
'AC_'.$actioncode;
2749 $actioncomm->label = $label;
2750 $actioncomm->datep = $now;
2751 $actioncomm->datef = $now;
2752 $actioncomm->percentage = -1;
2753 $actioncomm->socid = $object->thirdparty->id;
2754 $actioncomm->authorid = $user->id;
2755 $actioncomm->userownerid = $user->id;
2756 $actioncomm->fk_element = $object->id;
2757 $actioncomm->elementtype =
'contract';
2758 $actioncomm->note_private = $comment;
2760 $ret = $actioncomm->create($user);
2762 $contracterror[$object->id]=$object->ref;
2765 $errorforlocaltransaction++;
2766 $this->error = $this->
db->lasterror();
2769 if (! $errorforlocaltransaction) {
2770 $this->
db->commit();
2772 $this->
db->rollback();
2776 $this->error =
"Bad value for newdate in doAutoRenewContracts - expirationdate=".$expirationdate.
" enddatetoscan=".$enddatetoscan.
" duration_value=".$duration_value.
" duration_unit=".$duration_value;
2785 $this->error = $this->
db->lasterror();
2788 $this->output .= count($contractlineprocessed).
' contract line(s) with end date before '.
dol_print_date($enddatetoscan,
'day').
' were renewed'.(count($contractlineprocessed)>0 ?
' : '.join(
',', $contractlineprocessed) :
'');
2790 return ($error ? 1: 0);
2803 public $element =
'contratdet';
2808 public $table_element =
'contratdet';
2814 public $element_for_permission =
'contrat';
2856 public $description;
2858 public $product_type;
2859 public $product_ref;
2860 public $product_label;
2862 public $date_commande;
2865 public $date_start_real;
2867 public $date_end_real;
2870 public $vat_src_code;
2871 public $localtax1_tx;
2872 public $localtax2_tx;
2873 public $localtax1_type;
2874 public $localtax2_type;
2876 public $remise_percent;
2882 public $fk_remise_except;
2897 public $total_localtax1;
2898 public $total_localtax2;
2904 public $fk_fournprice;
2913 public $fk_user_author;
2918 public $fk_user_ouverture;
2923 public $fk_user_cloture;
2925 public $commentaire;
2934 const STATUS_INITIAL = 0;
2935 const STATUS_OPEN = 4;
2936 const STATUS_CLOSED = 5;
2943 public $fields = array(
2944 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
2945 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>30,
'index'=>1),
2946 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>35),
2947 'qty' =>array(
'type'=>
'integer',
'label'=>
'Quantity',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>35,
'isameasure'=>1),
2948 'total_ht' =>array(
'type'=>
'integer',
'label'=>
'AmountHT',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>36,
'isameasure'=>1),
2949 'total_tva' =>array(
'type'=>
'integer',
'label'=>
'AmountVAT',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>37,
'isameasure'=>1),
2950 'total_ttc' =>array(
'type'=>
'integer',
'label'=>
'AmountTTC',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>38,
'isameasure'=>1),
2953 'fk_contrat' =>array(
'type'=>
'integer:Contrat:contrat/class/contrat.class.php',
'label'=>
'Contract',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>70),
2954 'fk_product' =>array(
'type'=>
'integer:Product:product/class/product.class.php:1',
'label'=>
'Product',
'enabled'=>1,
'visible'=>-1,
'position'=>75),
2956 'note_private' =>array(
'type'=>
'text',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>105),
2957 'note_public' =>array(
'type'=>
'text',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>110),
2961 'fk_user_ouverture' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserStartingService',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>135),
2962 'fk_user_cloture' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserClosingService',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>135),
2963 'statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Statut',
'enabled'=>1,
'visible'=>-1,
'position'=>500,
'arrayofkeyval'=>array(0=>
'Draft', 4=>
'Open', 5=>
'Closed')),
2964 'rang' =>array(
'type'=>
'integer',
'label'=>
'Rank',
'enabled'=>1,
'visible'=>0,
'position'=>500,
'default' =>0)
2988 return $this->
LibStatut($this->statut, $mode, ((!empty($this->date_end)) ? ($this->date_end <
dol_now() ? 1 : 0) : -1));
3001 public static function LibStatut($status, $mode, $expired = -1, $moreatt =
'')
3005 $langs->load(
"contracts");
3007 if ($status == self::STATUS_INITIAL) {
3008 $labelStatus = $langs->transnoentities(
"ServiceStatusInitial");
3009 $labelStatusShort = $langs->transnoentities(
"ServiceStatusInitial");
3010 } elseif ($status == self::STATUS_OPEN && $expired == -1) {
3011 $labelStatus = $langs->transnoentities(
"ServiceStatusRunning");
3012 $labelStatusShort = $langs->transnoentities(
"ServiceStatusRunning");
3013 } elseif ($status == self::STATUS_OPEN && $expired == 0) {
3014 $labelStatus = $langs->transnoentities(
"ServiceStatusNotLate");
3015 $labelStatusShort = $langs->transnoentities(
"ServiceStatusNotLateShort");
3016 } elseif ($status == self::STATUS_OPEN && $expired == 1) {
3017 $labelStatus = $langs->transnoentities(
"ServiceStatusLate");
3018 $labelStatusShort = $langs->transnoentities(
"ServiceStatusLateShort");
3019 } elseif ($status == self::STATUS_CLOSED) {
3020 $labelStatus = $langs->transnoentities(
"ServiceStatusClosed");
3021 $labelStatusShort = $langs->transnoentities(
"ServiceStatusClosed");
3024 $statusType =
'status'.$status;
3025 if ($status == self::STATUS_OPEN && $expired == 1) {
3026 $statusType =
'status1';
3028 if ($status == self::STATUS_CLOSED) {
3029 $statusType =
'status6';
3032 $params = array(); $reg = array();
3033 if (preg_match(
'/class="(.*)"/', $moreatt, $reg)) {
3034 $params = array(
'badgeParams'=>array(
'css' => $reg[1]));
3036 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode,
'', $params);
3051 $label = $langs->trans(
"ShowContractOfService").
': '.$this->label;
3052 if (empty($label)) {
3053 $label = $this->description;
3056 $link =
'<a href="'.DOL_URL_ROOT.
'/contrat/card.php?id='.$this->fk_contrat.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
3060 if ($this->
type == 0) {
3065 $result .= ($link.img_object($label, $picto,
'class="classfortooltip"').$linkend);
3067 if ($withpicto && $withpicto != 2) {
3070 if ($withpicto != 2) {
3071 $result .= $link.($this->product_ref ? $this->product_ref.
' ' :
'').($this->label ? $this->label : $this->
description).$linkend;
3086 if (empty($id) && empty($ref)) {
3091 $sql .=
" t.rowid,";
3093 $sql .=
" t.fk_contrat,";
3094 $sql .=
" t.fk_product,";
3095 $sql .=
" t.statut,";
3096 $sql .=
" t.label,";
3097 $sql .=
" p.ref as product_ref,";
3098 $sql .=
" p.label as product_label,";
3099 $sql .=
" p.description as product_desc,";
3100 $sql .=
" p.fk_product_type as product_type,";
3101 $sql .=
" t.description,";
3102 $sql .=
" t.date_commande,";
3103 $sql .=
" t.date_ouverture_prevue as date_start,";
3104 $sql .=
" t.date_ouverture as date_start_real,";
3105 $sql .=
" t.date_fin_validite as date_end,";
3106 $sql .=
" t.date_cloture as date_end_real,";
3107 $sql .=
" t.tva_tx,";
3108 $sql .=
" t.vat_src_code,";
3109 $sql .=
" t.localtax1_tx,";
3110 $sql .=
" t.localtax2_tx,";
3111 $sql .=
" t.localtax1_type,";
3112 $sql .=
" t.localtax2_type,";
3114 $sql .=
" t.remise_percent,";
3115 $sql .=
" t.remise,";
3116 $sql .=
" t.fk_remise_except,";
3117 $sql .=
" t.subprice,";
3118 $sql .=
" t.price_ht,";
3119 $sql .=
" t.total_ht,";
3120 $sql .=
" t.total_tva,";
3121 $sql .=
" t.total_localtax1,";
3122 $sql .=
" t.total_localtax2,";
3123 $sql .=
" t.total_ttc,";
3124 $sql .=
" t.fk_product_fournisseur_price as fk_fournprice,";
3125 $sql .=
" t.buy_price_ht as pa_ht,";
3126 $sql .=
" t.info_bits,";
3127 $sql .=
" t.fk_user_author,";
3128 $sql .=
" t.fk_user_ouverture,";
3129 $sql .=
" t.fk_user_cloture,";
3130 $sql .=
" t.commentaire,";
3131 $sql .=
" t.fk_unit,";
3133 $sql .=
" FROM ".MAIN_DB_PREFIX.
"contratdet as t LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = t.fk_product";
3135 $sql .=
" WHERE t.rowid = ".((int) $id);
3138 $sql .=
" WHERE t.rowid = '".$this->db->escape($ref).
"'";
3141 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
3145 $obj = $this->
db->fetch_object(
$resql);
3147 $this->
id = $obj->rowid;
3148 $this->
ref = $obj->rowid;
3150 $this->tms = $this->
db->jdate($obj->tms);
3151 $this->fk_contrat = $obj->fk_contrat;
3152 $this->fk_product = $obj->fk_product;
3153 $this->statut = $obj->statut;
3154 $this->product_ref = $obj->product_ref;
3155 $this->product_label = $obj->product_label;
3156 $this->product_description = $obj->product_description;
3157 $this->product_type = $obj->product_type;
3158 $this->label = $obj->label;
3160 $this->date_commande = $this->
db->jdate($obj->date_commande);
3162 $this->date_start = $this->
db->jdate($obj->date_start);
3163 $this->date_start_real = $this->
db->jdate($obj->date_start_real);
3164 $this->date_end = $this->
db->jdate($obj->date_end);
3165 $this->date_end_real = $this->
db->jdate($obj->date_end_real);
3172 $this->tva_tx = $obj->tva_tx;
3173 $this->vat_src_code = $obj->vat_src_code;
3174 $this->localtax1_tx = $obj->localtax1_tx;
3175 $this->localtax2_tx = $obj->localtax2_tx;
3176 $this->localtax1_type = $obj->localtax1_type;
3177 $this->localtax2_type = $obj->localtax2_type;
3178 $this->qty = $obj->qty;
3179 $this->remise_percent = $obj->remise_percent;
3180 $this->fk_remise_except = $obj->fk_remise_except;
3181 $this->subprice = $obj->subprice;
3182 $this->price_ht = $obj->price_ht;
3183 $this->total_ht = $obj->total_ht;
3184 $this->total_tva = $obj->total_tva;
3185 $this->total_localtax1 = $obj->total_localtax1;
3186 $this->total_localtax2 = $obj->total_localtax2;
3187 $this->total_ttc = $obj->total_ttc;
3188 $this->info_bits = $obj->info_bits;
3189 $this->fk_user_author = $obj->fk_user_author;
3190 $this->fk_user_ouverture = $obj->fk_user_ouverture;
3191 $this->fk_user_cloture = $obj->fk_user_cloture;
3192 $this->commentaire = $obj->commentaire;
3193 $this->fk_fournprice = $obj->fk_fournprice;
3195 $marginInfos =
getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
3196 $this->pa_ht = $marginInfos[0];
3197 $this->fk_unit = $obj->fk_unit;
3199 $this->rang = $obj->rang;
3208 $this->error =
"Error ".$this->db->lasterror();
3221 public function update($user, $notrigger = 0)
3223 global $conf, $langs, $mysoc;
3228 $this->fk_contrat = (int) $this->fk_contrat;
3229 $this->fk_product = (int) $this->fk_product;
3230 $this->statut = (int) $this->statut;
3231 $this->label = trim($this->label);
3233 $this->vat_src_code = trim($this->vat_src_code);
3234 $this->tva_tx = trim($this->tva_tx);
3235 $this->localtax1_tx = trim($this->localtax1_tx);
3236 $this->localtax2_tx = trim($this->localtax2_tx);
3237 $this->qty = trim($this->qty);
3238 $this->remise_percent = trim($this->remise_percent);
3239 $this->fk_remise_except = (int) $this->fk_remise_except;
3240 $this->subprice =
price2num($this->subprice);
3241 $this->price_ht =
price2num($this->price_ht);
3242 $this->total_ht = trim($this->total_ht);
3243 $this->total_tva = trim($this->total_tva);
3244 $this->total_localtax1 = trim($this->total_localtax1);
3245 $this->total_localtax2 = trim($this->total_localtax2);
3246 $this->total_ttc = trim($this->total_ttc);
3247 $this->info_bits = trim($this->info_bits);
3248 $this->fk_user_author = (int) $this->fk_user_author;
3249 $this->fk_user_ouverture = (int) $this->fk_user_ouverture;
3250 $this->fk_user_cloture = (int) $this->fk_user_cloture;
3251 $this->commentaire = trim($this->commentaire);
3252 $this->rang = (int) $this->rang;
3254 if (empty($this->price_ht)) {
3255 $this->price_ht = 0;
3257 if (empty($this->total_ht)) {
3258 $this->total_ht = 0;
3260 if (empty($this->total_tva)) {
3261 $this->total_tva = 0;
3263 if (empty($this->total_ttc)) {
3264 $this->total_ttc = 0;
3266 if (empty($this->localtax1_tx)) {
3267 $this->localtax1_tx = 0;
3269 if (empty($this->localtax2_tx)) {
3270 $this->localtax2_tx = 0;
3272 if (empty($this->remise_percent)) {
3273 $this->remise_percent = 0;
3276 if (empty($this->date_start)) {
3277 $this->date_start = $this->date_start;
3279 if (empty($this->date_start_real)) {
3280 $this->date_start_real = $this->date_start_real;
3282 if (empty($this->date_end)) {
3283 $this->date_end = $this->date_end;
3285 if (empty($this->date_end_real)) {
3286 $this->date_end_real = $this->date_end_real;
3295 $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);
3296 $this->total_ht = $tabprice[0];
3297 $this->total_tva = $tabprice[1];
3298 $this->total_ttc = $tabprice[2];
3299 $this->total_localtax1 = $tabprice[9];
3300 $this->total_localtax2 = $tabprice[10];
3302 if (empty($this->pa_ht)) {
3307 if ($this->pa_ht == 0) {
3308 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
3311 $this->pa_ht = $result;
3316 if (empty($this->oldcopy)) {
3323 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
3324 $sql .=
" fk_contrat = ".((int) $this->fk_contrat).
",";
3325 $sql .=
" fk_product = ".($this->fk_product ? ((int) $this->fk_product) :
'null').
",";
3326 $sql .=
" statut = ".((int) $this->statut).
",";
3327 $sql .=
" label = '".$this->db->escape($this->label).
"',";
3328 $sql .=
" description = '".$this->db->escape($this->
description).
"',";
3329 $sql .=
" date_commande = ".($this->date_commande !=
'' ?
"'".$this->db->idate($this->date_commande).
"'" :
"null").
",";
3330 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
3331 $sql .=
" date_ouverture = ".($this->date_start_real !=
'' ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
3332 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
3333 $sql .=
" date_cloture = ".($this->date_end_real !=
'' ?
"'".$this->db->idate($this->date_end_real).
"'" :
"null").
",";
3334 $sql .=
" vat_src_code = '".$this->db->escape($this->vat_src_code).
"',";
3335 $sql .=
" tva_tx = ".price2num($this->tva_tx).
",";
3336 $sql .=
" localtax1_tx = ".price2num($this->localtax1_tx).
",";
3337 $sql .=
" localtax2_tx = ".price2num($this->localtax2_tx).
",";
3338 $sql .=
" qty = ".price2num($this->qty).
",";
3339 $sql .=
" remise_percent = ".price2num($this->remise_percent).
",";
3340 $sql .=
" remise = ".($this->remise ?
price2num($this->remise) :
"null").
",";
3341 $sql .=
" fk_remise_except = ".($this->fk_remise_except > 0 ? $this->fk_remise_except :
"null").
",";
3342 $sql .=
" subprice = ".($this->subprice !=
'' ? $this->subprice :
"null").
",";
3343 $sql .=
" price_ht = ".($this->price_ht !=
'' ? $this->price_ht :
"null").
",";
3344 $sql .=
" total_ht = ".$this->total_ht.
",";
3345 $sql .=
" total_tva = ".$this->total_tva.
",";
3346 $sql .=
" total_localtax1 = ".$this->total_localtax1.
",";
3347 $sql .=
" total_localtax2 = ".$this->total_localtax2.
",";
3348 $sql .=
" total_ttc = ".$this->total_ttc.
",";
3349 $sql .=
" fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"NULL").
",";
3350 $sql .=
" buy_price_ht = '".price2num($this->pa_ht).
"',";
3351 $sql .=
" info_bits = '".$this->db->escape($this->info_bits).
"',";
3352 $sql .=
" fk_user_author = ".($this->fk_user_author >= 0 ? $this->fk_user_author :
"NULL").
",";
3353 $sql .=
" fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? $this->fk_user_ouverture :
"NULL").
",";
3354 $sql .=
" fk_user_cloture = ".($this->fk_user_cloture > 0 ? $this->fk_user_cloture :
"NULL").
",";
3355 $sql .=
" commentaire = '".$this->db->escape($this->commentaire).
"',";
3356 $sql .=
" fk_unit = ".(!$this->fk_unit ?
'NULL' : $this->fk_unit).
",";
3357 $sql .=
" rang = ".(empty($this->rang) ?
'0' : (int) $this->rang);
3358 $sql .=
" WHERE rowid = ".((int) $this->
id);
3360 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
3363 $this->error =
"Error ".$this->db->lasterror();
3375 if (!$error && !empty($conf->global->CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES)) {
3376 dol_syslog(get_class($this).
"::update CONTRACT_SYNC_PLANNED_DATE_OF_SERVICES is on so we update date for all lines", LOG_DEBUG);
3378 if ($this->date_start != $this->oldcopy->date_start) {
3379 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
3380 $sql .=
" date_ouverture_prevue = ".($this->date_start !=
'' ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
3381 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
3386 $this->error =
"Error ".$this->db->lasterror();
3389 if ($this->date_end != $this->oldcopy->date_end) {
3390 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'contratdet SET';
3391 $sql .=
" date_fin_validite = ".($this->date_end !=
'' ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
3392 $sql .=
" WHERE fk_contrat = ".((int) $this->fk_contrat);
3397 $this->error =
"Error ".$this->db->lasterror();
3402 if (!$error && !$notrigger) {
3404 $result = $this->
call_trigger(
'LINECONTRACT_MODIFY', $user);
3407 $this->
db->rollback();
3413 $this->
db->commit();
3416 $this->
db->rollback();
3417 $this->errors[] = $this->error;
3436 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET";
3437 $sql .=
" total_ht=".price2num($this->total_ht,
'MT');
3438 $sql .=
",total_tva=".price2num($this->total_tva,
'MT');
3439 $sql .=
",total_localtax1=".price2num($this->total_localtax1,
'MT');
3440 $sql .=
",total_localtax2=".price2num($this->total_localtax2,
'MT');
3441 $sql .=
",total_ttc=".price2num($this->total_ttc,
'MT');
3442 $sql .=
" WHERE rowid = ".((int) $this->
id);
3444 dol_syslog(get_class($this).
"::update_total", LOG_DEBUG);
3448 $this->
db->commit();
3451 $this->error = $this->
db->error();
3452 $this->
db->rollback();
3466 global $conf, $user;
3471 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"contratdet";
3472 $sql .=
" (fk_contrat, label, description, fk_product, qty, vat_src_code, tva_tx,";
3473 $sql .=
" localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice,";
3474 $sql .=
" total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
3475 $sql .=
" info_bits,";
3477 $sql .=
" price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
3478 if ($this->date_start > 0) {
3479 $sql .=
",date_ouverture_prevue";
3481 if ($this->date_end > 0) {
3482 $sql .=
",date_fin_validite";
3484 $sql .=
") VALUES ($this->fk_contrat, '', '".$this->db->escape($this->
description).
"',";
3485 $sql .= ($this->fk_product > 0 ? $this->fk_product :
"null").
",";
3486 $sql .=
" '".$this->db->escape($this->qty).
"',";
3487 $sql .=
" '".$this->db->escape($this->vat_src_code).
"',";
3488 $sql .=
" '".$this->db->escape($this->tva_tx).
"',";
3489 $sql .=
" '".$this->db->escape($this->localtax1_tx).
"',";
3490 $sql .=
" '".$this->db->escape($this->localtax2_tx).
"',";
3491 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
3492 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
3493 $sql .=
" ".price2num($this->remise_percent).
",".
price2num($this->subprice).
",";
3494 $sql .=
" ".price2num($this->total_ht).
",".
price2num($this->total_tva).
",".
price2num($this->total_localtax1).
",".
price2num($this->total_localtax2).
",".
price2num($this->total_ttc).
",";
3495 $sql .=
" '".$this->db->escape($this->info_bits).
"',";
3496 $sql .=
" ".(empty($this->rang) ?
'0' : (int) $this->rang).
",";
3497 $sql .=
" ".price2num($this->price_ht).
",".
price2num($this->remise).
",";
3498 if ($this->fk_fournprice > 0) {
3499 $sql .=
' '.((int) $this->fk_fournprice).
',';
3503 if ($this->pa_ht > 0) {
3504 $sql .=
' '.((float)
price2num($this->pa_ht));
3508 if ($this->date_start > 0) {
3509 $sql .=
",'".$this->db->idate($this->date_start).
"'";
3511 if ($this->date_end > 0) {
3512 $sql .=
",'".$this->db->idate($this->date_end).
"'";
3516 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
3520 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'contratdet');
3526 $this->
db->rollback();
3533 $result = $this->
call_trigger(
'LINECONTRACT_INSERT', $user);
3535 $this->
db->rollback();
3541 $this->
db->commit();
3544 $this->
db->rollback();
3545 $this->error = $this->
db->error().
" sql=".$sql;
3560 public function active_line($user, $date, $date_end =
'', $comment =
'')
3563 global $langs, $conf;
3569 $this->statut = ContratLigne::STATUS_OPEN;
3570 $this->date_start = $date;
3571 $this->date_end = $date_end;
3572 $this->fk_user_ouverture = $user->id;
3573 $this->date_end_real =
null;
3574 $this->commentaire = $comment;
3576 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".$this->statut.
",";
3577 $sql .=
" date_ouverture = ".(dol_strlen($this->date_start_real) != 0 ?
"'".$this->db->idate($this->date_start_real).
"'" :
"null").
",";
3578 if ($date_end >= 0) {
3579 $sql .=
" date_fin_validite = ".(dol_strlen($this->date_end) != 0 ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
3581 $sql .=
" fk_user_ouverture = ".((int) $this->fk_user_ouverture).
",";
3582 $sql .=
" date_cloture = null,";
3583 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
3584 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND (statut = ".ContratLigne::STATUS_INITIAL.
" OR statut = ".ContratLigne::STATUS_CLOSED.
")";
3586 dol_syslog(get_class($this).
"::active_line", LOG_DEBUG);
3590 $result = $this->
call_trigger(
'LINECONTRACT_ACTIVATE', $user);
3597 $this->
db->commit();
3600 $this->
db->rollback();
3604 $this->error = $this->
db->lasterror();
3605 $this->
db->rollback();
3620 public function close_line($user, $date_end_real, $comment =
'', $notrigger = 0)
3623 global $langs, $conf;
3626 $this->date_cloture = $date_end_real;
3627 $this->date_end_real = $date_end_real;
3628 $this->fk_user_cloture = $user->id;
3629 $this->commentaire = $comment;
3637 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"contratdet SET statut = ".((int) ContratLigne::STATUS_CLOSED).
",";
3638 $sql .=
" date_cloture = '".$this->db->idate($date_end_real).
"',";
3639 $sql .=
" fk_user_cloture = ".((int) $user->id).
",";
3640 $sql .=
" commentaire = '".$this->db->escape($comment).
"'";
3641 $sql .=
" WHERE rowid = ".((int) $this->
id).
" AND statut = ".((int) ContratLigne::STATUS_OPEN);
3647 $result = $this->
call_trigger(
'LINECONTRACT_CLOSE', $user);
3650 $this->
db->rollback();
3656 $this->
db->commit();
3659 $this->error = $this->
db->lasterror();
3660 $this->
db->rollback();
Class to manage agenda events (actions)
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
defineBuyPrice($unitPrice=0.0, $discountPercent=0.0, $fk_product=0)
Get buy price to use for margin calculation.
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
fetch_thirdparty($force_thirdparty_id=0)
Load the third party of object, from id $this->socid or $this->fk_soc, into this->thirdparty.
getIdContact($source, $code, $status=0)
Return id of contacts for a source and a contact code.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid='', $f_user=null, $notrigger=0)
Delete all links between an object $this.
deleteExtraFields()
Delete all extra fields values for the current object.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
delete_linked_contact($source='', $code='')
Delete all links between an object $this and all its contacts.
call_trigger($triggerName, $user)
Call trigger based on this instance.
add_contact($fk_socpeople, $type_contact, $source='external', $notrigger=0)
Add a link between element $this->element and a contact.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Class to manage contracts.
createFromClone(User $user, $socid=0, $notrigger=0)
Load an object from its id and create a new one in database.
update_statut($user)
Update statut of contract according to services.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
getLinesArray()
Create an array of order lines.
validate(User $user, $force_number='', $notrigger=0)
Validate a contract.
getIdBillingContact()
Return id des contacts clients de facturation.
initAsSpecimen()
Initialise an instance with random values.
active_line($user, $line_id, $date, $date_end='', $comment='')
Activate a contract line.
create($user)
Create a contract into database.
close_line($user, $line_id, $date_end, $comment='')
Close a contract line.
reopen($user, $notrigger=0)
Unvalidate a contract.
array_detail($status=-1)
Return list of line rowid.
update($user, $notrigger=0)
Update object into database.
closeAll(User $user, $notrigger=0, $comment='')
Close all lines of a contract.
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)
Ajoute une ligne de contrat en base.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
info($id)
Charge les informations d'ordre info dans l'objet contrat.
getListOfContracts($option='all', $status=[], $product_categories=[], $line_status=[])
Return list of other contracts for the same company than current contract.
deleteline($idline, User $user)
Delete a contract line.
load_board($user, $mode)
Load indicators for dashboard (this->nbtodo and this->nbtodolate)
LibStatut($status, $mode)
Renvoi label of a given contrat status.
activateAll($user, $date_start='', $notrigger=0, $comment='', $date_end='')
Open all lines of a contract.
fetch_lines($only_services=0, $loadalsotranslation=0)
Load lines array into this->lines.
__construct($db)
Constructor.
doAutoRenewContracts($thirdparty_id=0, $delayindaysshort=0)
Action executed by scheduler CAN BE A CRON TASK Loop on each contract lines and update the end of dat...
getNomUrl($withpicto=0, $maxlength=0, $notooltip=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
fetch($id, $ref='', $ref_customer='', $ref_supplier='')
Load a contract from database.
getLibStatut($mode)
Return label of a contract status.
static replaceProduct(DoliDB $db, $origin_id, $dest_id)
Function used to replace a product id with another one.
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)
Mets a jour une ligne de contrat.
getIdServiceContact()
Return id des contacts clients de prestation.
load_state_board()
Charge indicateurs this->nb de tableau de bord.
getNextNumRef($soc)
Return next contract ref.
Class to manage lines of contracts.
active_line($user, $date, $date_end='', $comment='')
Activate a contract line.
getLibStatut($mode)
Return label of this contract line status.
insert($notrigger=0)
Inserts a contrat line into database.
fetch($id, $ref='')
Load object in memory from database.
close_line($user, $date_end_real, $comment='', $notrigger=0)
Close a contract line.
update($user, $notrigger=0)
Update database for contract line.
update_total()
Mise a jour en base des champs total_xxx de ligne Used by migration process.
static LibStatut($status, $mode, $expired=-1, $moreatt='')
Return label of a contract line status.
__construct($db)
Constructor.
getNomUrl($withpicto=0, $maxlength=0)
Return clicable name (with picto eventually)
Class to manage Dolibarr database access.
Class to manage shipments.
const STATUS_DRAFT
Draft status.
Class to manage products or services.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
Return an array with margins information of a line.
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller='', $localtaxes_array='', $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
$conf db
API class for accounts.