Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX : Use contract line for api contracts #31243

Open
wants to merge 3 commits into
base: 17.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 52 additions & 30 deletions htdocs/contrat/class/api_contracts.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use Luracast\Restler\RestException;
use Luracast\Restler\RestException;

require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';

/**
* API class for contracts
Expand All @@ -45,6 +45,11 @@ class Contracts extends DolibarrApi
*/
public $contract;

/**
* @var ContratLigne $contractLine {@type ContratLigne}
*/
public $contractLine;

/**
* Constructor
*/
Expand All @@ -53,6 +58,7 @@ public function __construct()
global $db, $conf;
$this->db = $db;
$this->contract = new Contrat($this->db);
$this->contractLine = new ContratLigne($this->db);
}

/**
Expand Down Expand Up @@ -316,7 +322,7 @@ public function postLine($id, $request_data = null)
*
* @url PUT {id}/lines/{lineid}
*
* @return Object|bool
* @return Object
*/
public function putLine($id, $lineid, $request_data = null)
{
Expand All @@ -333,35 +339,38 @@ public function putLine($id, $lineid, $request_data = null)
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}

$resultLine = $this->contractLine->fetch($lineid);

if (!$resultLine) {
throw new RestException(404, 'Contract line not found');
}

$request_data = (object) $request_data;

$request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
$request_data->price_base_type = sanitizeVal($request_data->price_base_type);

$updateRes = $this->contract->updateline(
$lineid,
$request_data->desc,
$request_data->subprice,
$request_data->qty,
$request_data->remise_percent,
$request_data->date_start,
$request_data->date_end,
$request_data->tva_tx,
$request_data->localtax1_tx,
$request_data->localtax2_tx,
$request_data->date_start_real,
$request_data->date_end_real,
$request_data->price_base_type ? $request_data->price_base_type : 'HT',
$request_data->info_bits,
$request_data->fk_fourn_price,
$request_data->pa_ht,
$request_data->array_options,
$request_data->fk_unit
);
$this->contractLine->description = $request_data->desc;
$this->contractLine->subprice = $request_data->subprice;
$this->contractLine->qty = $request_data->qty;
$this->contractLine->remise_percent = $request_data->remise_percent;
$this->contractLine->date_start = $request_data->date_start;
$this->contractLine->date_end = $request_data->date_end;
$this->contractLine->tva_tx = $request_data->tva_tx ?? 0;
$this->contractLine->localtax1_tx = $request_data->localtax1_tx;
$this->contractLine->localtax2_tx = $request_data->localtax2_tx;
$this->contractLine->date_start_real = $request_data->date_start_real;
$this->contractLine->date_end_real = $request_data->date_end_real;
$this->contractLine->info_bits = $request_data->info_bits;
$this->contractLine->fk_fournprice = $request_data->fk_fourn_price;
$this->contractLine->pa_ht = $request_data->pa_ht;
$this->contractLine->array_options = $request_data->array_options ?? $this->contractLine->array_options;
$this->contractLine->fk_unit = $request_data->fk_unit;

$updateRes = $this->contractLine->update(DolibarrApiAccess::$user);

if ($updateRes > 0) {
$result = $this->get($id);
unset($result->line);
return $this->_cleanObjectDatas($result);
}

Expand Down Expand Up @@ -392,15 +401,19 @@ public function activateLine($id, $lineid, $datestart, $dateend = null, $comment
throw new RestException(404, 'Contrat not found');
}

$resultLine = $this->contractLine->fetch($lineid);
if (!$resultLine) {
throw new RestException(404, 'Contract line not found');
}

if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}

$updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, $datestart, $dateend, $comment);
$updateRes = $this->contractLine->active_line(DolibarrApiAccess::$user, $datestart, $dateend, $comment);

if ($updateRes > 0) {
$result = $this->get($id);
unset($result->line);
return $this->_cleanObjectDatas($result);
}

Expand Down Expand Up @@ -430,15 +443,19 @@ public function unactivateLine($id, $lineid, $datestart, $comment = null)
throw new RestException(404, 'Contrat not found');
}

$resultLine = $this->contractLine->fetch($lineid);
if (!$resultLine) {
throw new RestException(404, 'Contract line not found');
}

if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}

$updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, $datestart, $comment);
$updateRes = $this->contractLine->close_line(DolibarrApiAccess::$user, $datestart, $comment);

if ($updateRes > 0) {
$result = $this->get($id);
unset($result->line);
return $this->_cleanObjectDatas($result);
}

Expand Down Expand Up @@ -470,17 +487,22 @@ public function deleteLine($id, $lineid)
throw new RestException(404, 'Contrat not found');
}

$resultLine = $this->contractLine->fetch($lineid);
if (!$resultLine) {
throw new RestException(404, 'Contract line not found');
}

if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}

// TODO Check the lineid $lineid is a line of object

$updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user);
$updateRes = $this->contractLine->delete(DolibarrApiAccess::$user);
if ($updateRes > 0) {
return $this->get($id);
} else {
throw new RestException(405, $this->contract->error);
throw new RestException(405, $this->contractLine->error);
}
}

Expand Down
50 changes: 49 additions & 1 deletion htdocs/contrat/class/contrat.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3349,7 +3349,9 @@ public function update($user, $notrigger = 0)
$sql .= " total_ttc = ".$this->total_ttc.",";
$sql .= " fk_product_fournisseur_price = ".(!empty($this->fk_fournprice) ? $this->fk_fournprice : "NULL").",";
$sql .= " buy_price_ht = '".price2num($this->pa_ht)."',";
$sql .= " info_bits = '".$this->db->escape($this->info_bits)."',";
if (!empty($this->info_bits)) {
$sql .= " info_bits = '".$this->db->escape($this->info_bits)."',";
}
$sql .= " fk_user_author = ".($this->fk_user_author >= 0 ? $this->fk_user_author : "NULL").",";
$sql .= " fk_user_ouverture = ".($this->fk_user_ouverture > 0 ? $this->fk_user_ouverture : "NULL").",";
$sql .= " fk_user_cloture = ".($this->fk_user_cloture > 0 ? $this->fk_user_cloture : "NULL").",";
Expand Down Expand Up @@ -3662,4 +3664,50 @@ public function close_line($user, $date_end_real, $comment = '', $notrigger = 0)
return -1;
}
}

/**
* Delete line in database
*
* @param User $user Object user
* @param bool $notrigger Disable triggers
* @return int <0 if KO, >0 if OK
*/
public function delete($user, $notrigger = false)
{
$error = 0;

$this->db->begin();

if (!$notrigger) {
$result = $this->call_trigger('LINECONTRACT_DELETE', $user);
if ($result < 0) {
$error++;
}
}

if (!$error) {
$result = $this->deleteExtraFields();
if ($result < 0) {
$error++;
}
}

if (!$error) {
$sql = "DELETE FROM " . $this->db->prefix() . $this->table_element . " WHERE rowid=" . intval($this->id);

$res = $this->db->query($sql);
if (!$res) {
$error++;
$this->errors[] = $this->db->lasterror();
}
}

if ($error) {
$this->db->rollback();
return -1;
} else {
$this->db->commit();
return 1;
}
}
}
Loading