20 use Luracast\Restler\RestException;
 
   22 require_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
 
   35  public static $FIELDS = array(
 
   38    'commercial_signature_id',
 
   54    $this->contract = 
new Contrat($this->db);
 
 
   66  public function get($id)
 
   68    if (!DolibarrApiAccess::$user->rights->contrat->lire) {
 
   69      throw new RestException(401);
 
   72    $result = $this->contract->fetch($id);
 
   74      throw new RestException(404, 
'Contract not found');
 
   78      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
   81    $this->contract->fetchObjectLinked();
 
 
  104  public function index($sortfield = 
"t.rowid", $sortorder = 
'ASC', $limit = 100, $page = 0, $thirdparty_ids = 
'', $sqlfilters = 
'', $properties = 
'')
 
  108    if (!DolibarrApiAccess::$user->rights->contrat->lire) {
 
  109      throw new RestException(401);
 
  115    $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
 
  119    if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
 
  120      $search_sale = DolibarrApiAccess::$user->id;
 
  123    $sql = 
"SELECT t.rowid";
 
  124    if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
 
  125      $sql .= 
", sc.fk_soc, sc.fk_user"; 
 
  127    $sql .= 
" FROM ".MAIN_DB_PREFIX.
"contrat AS t LEFT JOIN ".MAIN_DB_PREFIX.
"contrat_extrafields AS ef ON (ef.fk_object = t.rowid)"; 
 
  129    if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
 
  130      $sql .= 
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc"; 
 
  133    $sql .= 
' WHERE t.entity IN ('.getEntity(
'contrat').
')';
 
  134    if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
 
  135      $sql .= 
" AND t.fk_soc = sc.fk_soc";
 
  138      $sql .= 
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
 
  140    if ($search_sale > 0) {
 
  141      $sql .= 
" AND t.rowid = sc.fk_soc"; 
 
  144    if ($search_sale > 0) {
 
  145      $sql .= 
" AND sc.fk_user = ".((int) $search_sale);
 
  152        throw new RestException(400, 
'Error when validating parameter sqlfilters -> '.$errormessage);
 
  156    $sql .= $this->db->order($sortfield, $sortorder);
 
  161      $offset = $limit * $page;
 
  163      $sql .= $this->db->plimit($limit + 1, $offset);
 
  167    $result = $this->db->query($sql);
 
  170      $num = $this->db->num_rows($result);
 
  171      $min = min($num, ($limit <= 0 ? $num : $limit));
 
  174        $obj = $this->db->fetch_object($result);
 
  175        $contrat_static = 
new Contrat($this->db);
 
  176        if ($contrat_static->fetch($obj->rowid)) {
 
  182      throw new RestException(503, 
'Error when retrieve contrat list : '.$this->db->lasterror());
 
 
  194  public function post($request_data = 
null)
 
  196    if (!DolibarrApiAccess::$user->rights->contrat->creer) {
 
  197      throw new RestException(401, 
"Insufficient rights");
 
  200    $result = $this->
_validate($request_data);
 
  202    foreach ($request_data as $field => $value) {
 
  203      if ($field === 
'caller') {
 
  205        $this->contract->context[
'caller'] = $request_data[
'caller'];
 
  209      $this->contract->$field = $value;
 
  218    if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
 
  219      throw new RestException(500, 
"Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
 
  222    return $this->contract->id;
 
 
  236    if (!DolibarrApiAccess::$user->rights->contrat->lire) {
 
  237      throw new RestException(401);
 
  240    $result = $this->contract->fetch($id);
 
  242      throw new RestException(404, 
'Contract not found');
 
  246      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  248    $this->contract->getLinesArray();
 
  250    foreach ($this->contract->lines as $line) {
 
 
  266  public function postLine($id, $request_data = 
null)
 
  268    if (!DolibarrApiAccess::$user->rights->contrat->creer) {
 
  269      throw new RestException(401);
 
  272    $result = $this->contract->fetch($id);
 
  274      throw new RestException(404, 
'Contract not found');
 
  278      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  281    $request_data = (object) $request_data;
 
  283    $request_data->desc = 
sanitizeVal($request_data->desc, 
'restricthtml');
 
  284    $request_data->price_base_type = 
sanitizeVal($request_data->price_base_type);
 
  286    $updateRes = $this->contract->addline(
 
  288      $request_data->subprice,
 
  290      $request_data->tva_tx,
 
  291      $request_data->localtax1_tx,
 
  292      $request_data->localtax2_tx,
 
  293      $request_data->fk_product,
 
  294      $request_data->remise_percent,
 
  295      $request_data->date_start,
 
  296      $request_data->date_end,
 
  297      $request_data->price_base_type ? $request_data->price_base_type : 
'HT',
 
  298      $request_data->subprice_excl_tax,
 
  299      $request_data->info_bits,
 
  300      $request_data->fk_fournprice,
 
  301      $request_data->pa_ht,
 
  302      $request_data->array_options,
 
  303      $request_data->fk_unit,
 
  307    if ($updateRes > 0) {
 
 
  324  public function putLine($id, $lineid, $request_data = 
null)
 
  326    if (!DolibarrApiAccess::$user->rights->contrat->creer) {
 
  327      throw new RestException(401);
 
  330    $result = $this->contract->fetch($id);
 
  332      throw new RestException(404, 
'Contrat not found');
 
  336      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  339    $request_data = (object) $request_data;
 
  341    $request_data->desc = 
sanitizeVal($request_data->desc, 
'restricthtml');
 
  342    $request_data->price_base_type = 
sanitizeVal($request_data->price_base_type);
 
  344    $updateRes = $this->contract->updateline(
 
  347      $request_data->subprice,
 
  349      $request_data->remise_percent,
 
  350      $request_data->date_start,
 
  351      $request_data->date_end,
 
  352      $request_data->tva_tx,
 
  353      $request_data->localtax1_tx,
 
  354      $request_data->localtax2_tx,
 
  355      $request_data->date_start_real,
 
  356      $request_data->date_end_real,
 
  357      $request_data->price_base_type ? $request_data->price_base_type : 
'HT',
 
  358      $request_data->info_bits,
 
  359      $request_data->fk_fourn_price,
 
  360      $request_data->pa_ht,
 
  361      $request_data->array_options,
 
  362      $request_data->fk_unit
 
  365    if ($updateRes > 0) {
 
  366      $result = $this->
get($id);
 
  367      unset($result->line);
 
 
  387  public function activateLine($id, $lineid, $datestart, $dateend = 
null, $comment = 
null)
 
  389    if (!DolibarrApiAccess::$user->rights->contrat->creer) {
 
  390      throw new RestException(401);
 
  393    $result = $this->contract->fetch($id);
 
  395      throw new RestException(404, 
'Contrat not found');
 
  399      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  402    $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, $datestart, $dateend, $comment);
 
  404    if ($updateRes > 0) {
 
  405      $result = $this->
get($id);
 
  406      unset($result->line);
 
 
  427    if (!DolibarrApiAccess::$user->rights->contrat->creer) {
 
  428      throw new RestException(401);
 
  431    $result = $this->contract->fetch($id);
 
  433      throw new RestException(404, 
'Contrat not found');
 
  437      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  440    $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, $datestart, $comment);
 
  442    if ($updateRes > 0) {
 
  443      $result = $this->
get($id);
 
  444      unset($result->line);
 
 
  467    if (!DolibarrApiAccess::$user->rights->contrat->creer) {
 
  468      throw new RestException(401);
 
  471    $result = $this->contract->fetch($id);
 
  473      throw new RestException(404, 
'Contrat not found');
 
  477      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  482    $updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user);
 
  483    if ($updateRes > 0) {
 
  484      return $this->
get($id);
 
  486      throw new RestException(405, $this->contract->error);
 
 
  498  public function put($id, $request_data = 
null)
 
  500    if (!DolibarrApiAccess::$user->rights->contrat->creer) {
 
  501      throw new RestException(401);
 
  504    $result = $this->contract->fetch($id);
 
  506      throw new RestException(404, 
'Contrat not found');
 
  510      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  512    foreach ($request_data as $field => $value) {
 
  513      if ($field == 
'id') {
 
  516      if ($field === 
'caller') {
 
  518        $this->contract->context[
'caller'] = $request_data[
'caller'];
 
  522      if ($field == 
'array_options' && is_array($value)) {
 
  523        foreach ($value as $index => $val) {
 
  524          $this->contract->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->contract);
 
  528      $this->contract->$field = $this->
_checkValForAPI($field, $value, $this->contract);
 
  531    if ($this->contract->update(DolibarrApiAccess::$user) > 0) {
 
  532      return $this->
get($id);
 
  534      throw new RestException(500, $this->contract->error);
 
 
  545  public function delete($id)
 
  547    if (!DolibarrApiAccess::$user->hasRight(
'contrat', 
'supprimer')) {
 
  548      throw new RestException(401);
 
  550    $result = $this->contract->fetch($id);
 
  552      throw new RestException(404, 
'Contract not found');
 
  556      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  559    if (!$this->contract->delete(DolibarrApiAccess::$user)) {
 
  560      throw new RestException(500, 
'Error when delete contract : '.$this->contract->error);
 
  566        'message' => 
'Contract deleted' 
 
  589    if (!DolibarrApiAccess::$user->rights->contrat->creer) {
 
  590      throw new RestException(401);
 
  592    $result = $this->contract->fetch($id);
 
  594      throw new RestException(404, 
'Contract not found');
 
  598      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  601    $result = $this->contract->validate(DolibarrApiAccess::$user, 
'', $notrigger);
 
  603      throw new RestException(304, 
'Error nothing done. May be object is already validated');
 
  606      throw new RestException(500, 
'Error when validating Contract: '.$this->contract->error);
 
  612        'message' => 
'Contract validated (Ref='.$this->contract->ref.
')' 
 
  633  public function close($id, $notrigger = 0)
 
  635    if (!DolibarrApiAccess::$user->rights->contrat->creer) {
 
  636      throw new RestException(401);
 
  638    $result = $this->contract->fetch($id);
 
  640      throw new RestException(404, 
'Contract not found');
 
  644      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  647    $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);
 
  649      throw new RestException(304, 
'Error nothing done. May be object is already close');
 
  652      throw new RestException(500, 
'Error when closing Contract: '.$this->contract->error);
 
  658        'message' => 
'Contract closed (Ref='.$this->contract->ref.
'). All services were closed.' 
 
  675    $object = parent::_cleanObjectDatas($object);
 
  677    unset($object->address);
 
  678    unset($object->civility_id);
 
 
  693    foreach (Contracts::$FIELDS as $field) {
 
  694      if (!isset($data[$field])) {
 
  695        throw new RestException(400, 
"$field field missing");
 
  697      $contrat[$field] = $data[$field];
 
 
 
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
List contracts.
 
getLines($id)
Get lines of a contract.
 
putLine($id, $lineid, $request_data=null)
Update a line to given contract.
 
_validate($data)
Validate fields before create or update object.
 
put($id, $request_data=null)
Update contract general fields (won't touch lines of contract)
 
deleteLine($id, $lineid)
Delete a line to given contract.
 
_cleanObjectDatas($object)
Clean sensible object datas.
 
activateLine($id, $lineid, $datestart, $dateend=null, $comment=null)
Activate a service line of a given contract.
 
validate($id, $notrigger=0)
Validate a contract.
 
post($request_data=null)
Create contract object.
 
unactivateLine($id, $lineid, $datestart, $comment=null)
Unactivate a service line of a given contract.
 
__construct()
Constructor.
 
close($id, $notrigger=0)
Close all services of a contract.
 
postLine($id, $request_data=null)
Add a line to given contract.
 
Class to manage contracts.
 
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
 
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
 
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
 
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
 
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
 
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.