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

API CONTRACT specific for CliNetworks Koesio #512

Open
wants to merge 4 commits into
base: 17.0_koesio
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
4 changes: 2 additions & 2 deletions htdocs/contrat/card.php
Original file line number Diff line number Diff line change
Expand Up @@ -1945,7 +1945,7 @@
if ($objp->fk_product > 0) {
$product = new Product($db);
$product->fetch($objp->fk_product);
$dateactend = dol_time_plus_duree(time(), $product->duration_value, $product->duration_unit);
$dateactend = dol_time_plus_duree(time(), $product->duration_value ?: 0, $product->duration_unit);
}
}

Expand Down Expand Up @@ -2001,7 +2001,7 @@
if ($objp->fk_product > 0) {
$product = new Product($db);
$product->fetch($objp->fk_product);
$dateactend = dol_time_plus_duree(time(), $product->duration_value, $product->duration_unit);
$dateactend = dol_time_plus_duree(time(), $product->duration_value ?: 0, $product->duration_unit);
}
}
$now = dol_now();
Expand Down
172 changes: 142 additions & 30 deletions htdocs/contrat/class/api_contracts.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
use Luracast\Restler\RestException;

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

dol_include_once('/contact/class/contact.class.php');
dol_include_once('/clinetworks/class/koesioresource.class.php');
dol_include_once('/koesiosite/class/societesite.class.php');

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

public ContratLigne $contractLine;

/**
* Constructor
*/
Expand All @@ -53,6 +60,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 @@ -202,16 +210,17 @@ public function post($request_data = null)
// Check mandatory fields
$result = $this->_validate($request_data);

if (isset($request_data['array_options']['options_reseller'])) {
if ($request_data['array_options']['options_reseller'] != '') {
$fk_reseller = $request_data['array_options']['options_reseller'];
$this->checkReseller($fk_reseller);
}
}

foreach ($request_data as $field => $value) {
$this->contract->$field = $value;
}
/*if (isset($request_data["lines"])) {
$lines = array();
foreach ($request_data["lines"] as $line) {
array_push($lines, (object) $line);
}
$this->contract->lines = $lines;
}*/

if ($this->contract->create(DolibarrApiAccess::$user) < 0) {
throw new RestException(500, "Error creating contract", array_merge(array($this->contract->error), $this->contract->errors));
}
Expand Down Expand Up @@ -277,6 +286,36 @@ public function postLine($id, $request_data = null)

$request_data = (object) $request_data;

if (!isset($request_data->fk_product)) {
throw new RestException(400, "Product/service id missing");
}

if (isset($request_data->array_options['options_invoicing_account'])) {
if ($request_data->array_options['options_invoicing_account'] != '') {
$fk_invoicing_account = $request_data->array_options['options_invoicing_account'];
$this->checkInvoicingAccount($fk_invoicing_account);
} else {
throw new RestException(400, "options_invoicing_account cannot be empty");
}
} else {
throw new RestException(400, "options_invoicing_account extrafield missing");
}

if (isset($request_data->array_options['options_koesioresource'])) {
if ($request_data->array_options['options_koesioresource'] != '') {
$fk_koesioresource = $request_data->array_options['options_koesioresource'];
$this->checkKoesioResource($fk_koesioresource);
}
}

if (isset($request_data->array_options['options_site'])) {
if ($request_data->array_options['options_site'] != '') {
$fk_site = $request_data->array_options['options_site'];
$this->checkSocieteSite($fk_site);
}
}


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

Expand Down Expand Up @@ -338,34 +377,35 @@ public function putLine($id, $lineid, $request_data = null)
$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->fetch($lineid);
$this->contractLine->description = $request_data->desc;
$this->contractLine->qty = $request_data->qty;
$this->contractLine->remise_percent = $request_data->remise_percent;
$this->contractLine->remise_tx = $request_data->remise;
$this->contractLine->datestart = $request_data->date_start;
$this->contractLine->date_end = $request_data->date_end;
$this->contractLine->tva_tx = $request_data->tva_tx ?? $this->contractLine->tva_tx;
$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->price_base_type = $request_data->price_base_type ? $request_data->price_base_type : 'HT';
$this->contractLine->info_bits = $request_data->info_bits ?? $this->contractLine->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->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);
} else {
throw new RestException(400, 'Error updating contract line : '.$this->contractLine->error);
}

return false;
}

/**
Expand Down Expand Up @@ -396,7 +436,8 @@ public function activateLine($id, $lineid, $datestart, $dateend = null, $comment
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}

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

if ($updateRes > 0) {
$result = $this->get($id);
Expand Down Expand Up @@ -476,7 +517,11 @@ public function deleteLine($id, $lineid)

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

$updateRes = $this->contract->deleteline($lineid, DolibarrApiAccess::$user);

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

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

if ($updateRes > 0) {
return $this->get($id);
} else {
Expand Down Expand Up @@ -683,4 +728,71 @@ private function _validate($data)
}
return $contrat;
}


/**
* Checks invoicing account
*
* @param int $fk_invoicing_account
* @return bool
* @throws RestException
*/
private function checkInvoicingAccount(int $fk_invoicing_account): bool
{
$contact = new Contact($this->db);
if ($contact->fetch($fk_invoicing_account)) {
return true;
} else {
throw new RestException(400, "Invoicing account is not valid");
}
}


/**
*
* Checks koesioresource
*
* @param int $fk_koesioresource
* @return bool
* @throws RestException
*/
private function checkKoesioResource(int $fk_koesioresource): bool
{
$koesioResource = new KoesioResource($this->db);
if ($koesioResource->fetch($fk_koesioresource)) {
return true;
} else {
throw new RestException(400, "KoesioResource is not valid");
}
}

/**
* @param int $fk_societesite
* @return bool
* @throws RestException
*/
private function checkSocieteSite(int $fk_societesite): bool
{
$societeSite = new SocieteSite($this->db);
if ($societeSite->fetch($fk_societesite)) {
return true;
} else {
throw new RestException(400, "SocieteSite is not valid");
}
}

/**
* @param int $fk_reseller
* @return bool
* @throws RestException
*/
private function checkReseller(int $fk_reseller): bool
{
$societe = new Societe($this->db);
if ($societe->fetch($fk_reseller)) {
return true;
} else {
throw new RestException(400, "Thirdparty is not valid");
}
}
}
12 changes: 7 additions & 5 deletions htdocs/contrat/class/contrat.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3338,18 +3338,20 @@ public function update($user, $notrigger = 0)
$sql .= " localtax2_tx = ".price2num($this->localtax2_tx).",";
$sql .= " qty = ".price2num($this->qty).",";
$sql .= " remise_percent = ".price2num($this->remise_percent).",";
$sql .= " remise = ".($this->remise ?price2num($this->remise) : "null").",";
$sql .= " fk_remise_except = ".($this->fk_remise_except > 0 ? $this->fk_remise_except : "null").",";
$sql .= " subprice = ".($this->subprice != '' ? $this->subprice : "null").",";
$sql .= " price_ht = ".($this->price_ht != '' ? $this->price_ht : "null").",";
$sql .= " remise = ".($this->remise ?price2num($this->remise) : "NULL").",";
$sql .= " fk_remise_except = ".($this->fk_remise_except > 0 ? $this->fk_remise_except : "NULL").",";
$sql .= " subprice = ".($this->subprice != '' ? $this->subprice : "NULL").",";
$sql .= " price_ht = ".($this->price_ht != '' ? $this->price_ht : "NULL").",";
$sql .= " total_ht = ".$this->total_ht.",";
$sql .= " total_tva = ".$this->total_tva.",";
$sql .= " total_localtax1 = ".$this->total_localtax1.",";
$sql .= " total_localtax2 = ".$this->total_localtax2.",";
$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
Loading