Remove duplicated code

This commit is contained in:
Laurent Destailleur 2018-06-30 01:34:51 +02:00
parent 31396739f9
commit 0f62408700
2 changed files with 26 additions and 77 deletions

View File

@ -999,9 +999,9 @@ class Thirdparties extends DolibarrApi
} }
/** /**
* Get CompanyBankAccount objects for thirdparty * Get CompanyBankAccount objects for thirdparty
* *
* @param int $socid Thirdparty id * @param int $socid Thirdparty id
* *
* @return array * @return array
*/ */
function getCompanyBankAccount($socid){ function getCompanyBankAccount($socid){
@ -1028,12 +1028,12 @@ class Thirdparties extends DolibarrApi
$sql.= " FROM ".MAIN_DB_PREFIX."societe_rib"; $sql.= " FROM ".MAIN_DB_PREFIX."societe_rib";
if ($socid) $sql.= " WHERE fk_soc = ".$socid." "; if ($socid) $sql.= " WHERE fk_soc = ".$socid." ";
$result = $db->query($sql); $result = $db->query($sql);
if($result->num_rows == 0 ){ if($result->num_rows == 0 ){
throw new RestException(404, 'Account not found'); throw new RestException(404, 'Account not found');
} }
$i=0; $i=0;
@ -1055,7 +1055,7 @@ class Thirdparties extends DolibarrApi
else{ else{
throw new RestException(404, 'Account not found'); throw new RestException(404, 'Account not found');
} }
$fields = ['socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id']; $fields = ['socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id'];
@ -1069,19 +1069,19 @@ class Thirdparties extends DolibarrApi
} }
$returnAccounts[] = $object; $returnAccounts[] = $object;
} }
return $returnAccounts; return $returnAccounts;
} }
/** /**
* Create CompanyBankAccount object for thirdparty * Create CompanyBankAccount object for thirdparty
* @param int $socid thirdparty id * @param int $socid thirdparty id
* @param array $request_data Request datas * @param array $request_data Request datas
* *
* @return object ID of thirdparty * @return object ID of thirdparty
* *
* @url POST {socid}/CompanyBankAccount * @url POST {socid}/CompanyBankAccount
*/ */
function createCompanyBankAccount($socid, $request_data = null) function createCompanyBankAccount($socid, $request_data = null)
@ -1104,20 +1104,20 @@ class Thirdparties extends DolibarrApi
if ($account->update(DolibarrApiAccess::$user) < 0) if ($account->update(DolibarrApiAccess::$user) < 0)
throw new RestException(500, 'Error updating values'); throw new RestException(500, 'Error updating values');
return $account; return $account;
} }
/** /**
* Update CompanyBankAccount object for thirdparty * Update CompanyBankAccount object for thirdparty
* *
* @param int $socid Thirdparty id * @param int $socid Thirdparty id
* @param int $id CompanyBankAccount's id * @param int $id CompanyBankAccount's id
* @param array $request_data Request datas * @param array $request_data Request datas
* *
* @return object ID of thirdparty * @return object ID of thirdparty
* *
* @url PUT {socid}/CompanyBankAccount/{id} * @url PUT {socid}/CompanyBankAccount/{id}
*/ */
function updateCompanyBankAccount($socid, $id, $request_data = null) function updateCompanyBankAccount($socid, $id, $request_data = null)
@ -1128,14 +1128,13 @@ class Thirdparties extends DolibarrApi
$account = new CompanyBankAccount($this->db); $account = new CompanyBankAccount($this->db);
$account->fetchFromApi($id, $socid); $account->fetch($id, $socid, -1, '');
if($account->socid != $socid){ if($account->socid != $socid){
throw new RestException(401); throw new RestException(401);
} }
foreach($request_data as $field => $value) { foreach($request_data as $field => $value) {
$account->$field = $value; $account->$field = $value;
@ -1143,16 +1142,16 @@ class Thirdparties extends DolibarrApi
if ($account->update(DolibarrApiAccess::$user) < 0) if ($account->update(DolibarrApiAccess::$user) < 0)
throw new RestException(500, 'Error updating values'); throw new RestException(500, 'Error updating values');
return $account; return $account;
} }
/** /**
* @param int $id CompanyBankAccount's id * @param int $id CompanyBankAccount's id
* @param int $socid Thirdparty id * @param int $socid Thirdparty id
* *
* @return int -1 if error 1 if correct deletion * @return int -1 if error 1 if correct deletion
* *
* @url DELETE {socid}/CompanyBankAccount/{id} * @url DELETE {socid}/CompanyBankAccount/{id}
*/ */
function deleteCompanyBankAccount($id, $socid){ function deleteCompanyBankAccount($id, $socid){

View File

@ -197,10 +197,12 @@ class CompanyBankAccount extends Account
* Load record from database * Load record from database
* *
* @param int $id Id of record * @param int $id Id of record
* @param int $socid Id of company. If this is filled, function will return the first default RIB of company * @param int $socid Id of company. If this is filled, function will return the first entry found (matching $default and $type)
* @param int $default If id of company filled, we say if we want first record among all (-1), default record (1) or non default record (0)
* @param int $type If id of company filled, we say if we want record of this type only
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function fetch($id, $socid=0) function fetch($id, $socid=0, $default=1, $type='ban')
{ {
if (empty($id) && empty($socid)) return -1; if (empty($id) && empty($socid)) return -1;
@ -208,64 +210,12 @@ class CompanyBankAccount extends Account
$sql.= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur"; $sql.= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
$sql.= " FROM ".MAIN_DB_PREFIX."societe_rib"; $sql.= " FROM ".MAIN_DB_PREFIX."societe_rib";
if ($id) $sql.= " WHERE rowid = ".$id; if ($id) $sql.= " WHERE rowid = ".$id;
if ($socid) $sql.= " WHERE fk_soc = ".$socid." AND default_rib = 1 AND type ='ban'"; if ($socid)
$resql = $this->db->query($sql);
if ($resql)
{ {
if ($this->db->num_rows($resql)) $sql.= " WHERE fk_soc = ".$socid;
{ if ($default > -1) $sql.=" AND default_rib = ".$this->db->escape($default);
$obj = $this->db->fetch_object($resql); if ($type) $sql.= " AND type ='".$this->db->escape($type)."'";
$this->ref = $obj->fk_soc.'-'.$obj->label; // Generate an artificial ref
$this->id = $obj->rowid;
$this->type = $obj->type;
$this->socid = $obj->fk_soc;
$this->bank = $obj->bank;
$this->code_banque = $obj->code_banque;
$this->code_guichet = $obj->code_guichet;
$this->number = $obj->number;
$this->cle_rib = $obj->cle_rib;
$this->bic = $obj->bic;
$this->iban = $obj->iban;
$this->domiciliation = $obj->domiciliation;
$this->proprio = $obj->proprio;
$this->owner_address = $obj->owner_address;
$this->label = $obj->label;
$this->default_rib = $obj->default_rib;
$this->datec = $this->db->jdate($obj->datec);
$this->datem = $this->db->jdate($obj->datem);
$this->rum = $obj->rum;
$this->frstrecur = $obj->frstrecur;
}
$this->db->free($resql);
return 1;
} }
else
{
dol_print_error($this->db);
return -1;
}
}
/**
* Load record from database for the API
*
* @param int $id Id of record
* @param int $socid Id of company. If this is filled, function will return the first default RIB of company
* @return int <0 if KO, >0 if OK
*/
function fetchFromApi($id, $socid=0)
{
if (empty($id) && empty($socid)) return -1;
$sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
$sql.= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
$sql.= " FROM ".MAIN_DB_PREFIX."societe_rib";
if ($id) $sql.= " WHERE rowid = ".$id;
if ($socid) $sql.= " AND fk_soc = ".$socid;
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)