| Total Complexity | 280 |
| Total Lines | 1826 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Thirdparties often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Thirdparties, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Thirdparties extends DolibarrApi |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * |
||
| 34 | * @var array $FIELDS Mandatory fields, checked when create and update object |
||
| 35 | */ |
||
| 36 | static $FIELDS = array( |
||
| 37 | 'name' |
||
| 38 | ); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Societe $company {@type Societe} |
||
| 42 | */ |
||
| 43 | public $company; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor |
||
| 47 | */ |
||
| 48 | public function __construct() |
||
| 49 | { |
||
| 50 | global $db, $conf; |
||
| 51 | $this->db = $db; |
||
| 52 | |||
| 53 | require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; |
||
| 54 | require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php'; |
||
| 55 | require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; |
||
| 56 | require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php'; |
||
| 57 | |||
| 58 | $this->company = new Societe($this->db); |
||
| 59 | |||
| 60 | if (!empty($conf->global->SOCIETE_EMAIL_MANDATORY)) { |
||
| 61 | static::$FIELDS[] = 'email'; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get properties of a thirdparty object |
||
| 67 | * |
||
| 68 | * Return an array with thirdparty informations |
||
| 69 | * |
||
| 70 | * @param int $id ID of thirdparty |
||
| 71 | * @return array|mixed data without useless information |
||
| 72 | * |
||
| 73 | * @throws RestException |
||
| 74 | */ |
||
| 75 | public function get($id) |
||
| 76 | { |
||
| 77 | return $this->_fetch($id); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get properties of a thirdparty object by email. |
||
| 82 | * |
||
| 83 | * Return an array with thirdparty informations |
||
| 84 | * |
||
| 85 | * @param string $email Sort field |
||
| 86 | * @return array|mixed data without useless information |
||
| 87 | * |
||
| 88 | * @url GET byEmail/{email} |
||
| 89 | * |
||
| 90 | * @throws RestException |
||
| 91 | */ |
||
| 92 | public function getByEmail($email) |
||
| 93 | { |
||
| 94 | return $this->_fetch('', '', '', '', '', '', '', '', '', '', $email); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * List thirdparties |
||
| 99 | * |
||
| 100 | * Get a list of thirdparties |
||
| 101 | * |
||
| 102 | * @param string $sortfield Sort field |
||
| 103 | * @param string $sortorder Sort order |
||
| 104 | * @param int $limit Limit for list |
||
| 105 | * @param int $page Page number |
||
| 106 | * @param int $mode Set to 1 to show only customers |
||
| 107 | * Set to 2 to show only prospects |
||
| 108 | * Set to 3 to show only those are not customer neither prospect |
||
| 109 | * Set to 4 to show only suppliers |
||
| 110 | * @param int $category Use this param to filter list by category |
||
| 111 | * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.nom:like:'TheCompany%') and (t.date_creation:<:'20160101')" |
||
| 112 | * @return array Array of thirdparty objects |
||
| 113 | */ |
||
| 114 | public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters = '') |
||
| 115 | { |
||
| 116 | global $db; |
||
| 117 | |||
| 118 | $obj_ret = array(); |
||
| 119 | |||
| 120 | // case of external user, we force socids |
||
| 121 | $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : ''; |
||
| 122 | |||
| 123 | // If the internal user must only see his customers, force searching by him |
||
| 124 | $search_sale = 0; |
||
| 125 | if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id; |
||
| 126 | |||
| 127 | $sql = "SELECT t.rowid"; |
||
| 128 | if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects) |
||
| 129 | $sql .= " FROM ".MAIN_DB_PREFIX."societe as t"; |
||
| 130 | if ($category > 0) { |
||
| 131 | if ($mode != 4) $sql .= ", ".MAIN_DB_PREFIX."categorie_societe as c"; |
||
| 132 | if (!in_array($mode, array(1, 2, 3))) $sql .= ", ".MAIN_DB_PREFIX."categorie_fournisseur as cc"; |
||
| 133 | } |
||
| 134 | if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale |
||
| 135 | $sql .= ", ".MAIN_DB_PREFIX."c_stcomm as st"; |
||
| 136 | $sql .= " WHERE t.entity IN (".getEntity('societe').")"; |
||
| 137 | $sql .= " AND t.fk_stcomm = st.id"; |
||
| 138 | |||
| 139 | if ($mode == 1) $sql .= " AND t.client IN (1, 3)"; |
||
| 140 | if ($mode == 2) $sql .= " AND t.client IN (2, 3)"; |
||
| 141 | if ($mode == 3) $sql .= " AND t.client IN (0)"; |
||
| 142 | if ($mode == 4) $sql .= " AND t.fournisseur IN (1)"; |
||
| 143 | |||
| 144 | // Select thirdparties of given category |
||
| 145 | if ($category > 0) { |
||
| 146 | if (!empty($mode) && $mode != 4) { $sql .= " AND c.fk_categorie = ".$db->escape($category)." AND c.fk_soc = t.rowid"; } elseif (!empty($mode) && $mode == 4) { $sql .= " AND cc.fk_categorie = ".$db->escape($category)." AND cc.fk_soc = t.rowid"; } else { $sql .= " AND ((c.fk_categorie = ".$db->escape($category)." AND c.fk_soc = t.rowid) OR (cc.fk_categorie = ".$db->escape($category)." AND cc.fk_soc = t.rowid))"; } |
||
| 147 | } |
||
| 148 | |||
| 149 | if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; |
||
| 150 | //if ($email != NULL) $sql.= " AND s.email = \"".$email."\""; |
||
| 151 | if ($socids) $sql .= " AND t.rowid IN (".$socids.")"; |
||
| 152 | if ($search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale |
||
| 153 | // Insert sale filter |
||
| 154 | if ($search_sale > 0) |
||
| 155 | { |
||
| 156 | $sql .= " AND sc.fk_user = ".$search_sale; |
||
| 157 | } |
||
| 158 | // Add sql filters |
||
| 159 | if ($sqlfilters) |
||
| 160 | { |
||
| 161 | if (!DolibarrApi::_checkFilters($sqlfilters)) |
||
| 162 | { |
||
| 163 | throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); |
||
| 164 | } |
||
| 165 | $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; |
||
| 166 | $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; |
||
| 167 | } |
||
| 168 | |||
| 169 | $sql .= $db->order($sortfield, $sortorder); |
||
| 170 | |||
| 171 | if ($limit) { |
||
| 172 | if ($page < 0) |
||
| 173 | { |
||
| 174 | $page = 0; |
||
| 175 | } |
||
| 176 | $offset = $limit * $page; |
||
| 177 | |||
| 178 | $sql .= $db->plimit($limit + 1, $offset); |
||
| 179 | } |
||
| 180 | |||
| 181 | $result = $db->query($sql); |
||
| 182 | if ($result) |
||
| 183 | { |
||
| 184 | $num = $db->num_rows($result); |
||
| 185 | $min = min($num, ($limit <= 0 ? $num : $limit)); |
||
| 186 | $i = 0; |
||
| 187 | while ($i < $min) |
||
| 188 | { |
||
| 189 | $obj = $db->fetch_object($result); |
||
| 190 | $soc_static = new Societe($db); |
||
| 191 | if ($soc_static->fetch($obj->rowid)) { |
||
| 192 | $obj_ret[] = $this->_cleanObjectDatas($soc_static); |
||
| 193 | } |
||
| 194 | $i++; |
||
| 195 | } |
||
| 196 | } else { |
||
| 197 | throw new RestException(503, 'Error when retrieve thirdparties : '.$db->lasterror()); |
||
| 198 | } |
||
| 199 | if (!count($obj_ret)) { |
||
| 200 | throw new RestException(404, 'Thirdparties not found'); |
||
| 201 | } |
||
| 202 | return $obj_ret; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Create thirdparty object |
||
| 207 | * |
||
| 208 | * @param array $request_data Request datas |
||
| 209 | * @return int ID of thirdparty |
||
| 210 | */ |
||
| 211 | public function post($request_data = null) |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Update thirdparty |
||
| 230 | * |
||
| 231 | * @param int $id Id of thirdparty to update |
||
| 232 | * @param array $request_data Datas |
||
| 233 | * @return int |
||
| 234 | */ |
||
| 235 | public function put($id, $request_data = null) |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Merge a thirdparty into another one. |
||
| 263 | * |
||
| 264 | * Merge content (properties, notes) and objects (like invoices, events, orders, proposals, ...) of a thirdparty into a target thirdparty, |
||
| 265 | * then delete the merged thirdparty. |
||
| 266 | * If a property has a defined value both in thirdparty to delete and thirdparty to keep, the value into the thirdparty to |
||
| 267 | * delete will be ignored, the value of target thirdparty will remain, except for notes (content is concatenated). |
||
| 268 | * |
||
| 269 | * @param int $id ID of thirdparty to keep (the target thirdparty) |
||
| 270 | * @param int $idtodelete ID of thirdparty to remove (the thirdparty to delete), once data has been merged into the target thirdparty. |
||
| 271 | * @return int |
||
| 272 | * |
||
| 273 | * @url PUT {id}/merge/{idtodelete} |
||
| 274 | */ |
||
| 275 | public function merge($id, $idtodelete) |
||
| 276 | { |
||
| 277 | global $db, $hookmanager; |
||
| 278 | |||
| 279 | $error = 0; |
||
| 280 | |||
| 281 | if ($id == $idtodelete) |
||
| 282 | { |
||
| 283 | throw new RestException(400, 'Try to merge a thirdparty into itself'); |
||
| 284 | } |
||
| 285 | |||
| 286 | if (!DolibarrApiAccess::$user->rights->societe->creer) { |
||
| 287 | throw new RestException(401); |
||
| 288 | } |
||
| 289 | |||
| 290 | $result = $this->company->fetch($id); // include the fetch of extra fields |
||
| 291 | if (!$result) { |
||
| 292 | throw new RestException(404, 'Thirdparty not found'); |
||
| 293 | } |
||
| 294 | |||
| 295 | if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) { |
||
| 296 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 297 | } |
||
| 298 | |||
| 299 | $this->companytoremove = new Societe($db); |
||
| 300 | |||
| 301 | $result = $this->companytoremove->fetch($idtodelete); // include the fetch of extra fields |
||
| 302 | if (!$result) { |
||
| 303 | throw new RestException(404, 'Thirdparty not found'); |
||
| 304 | } |
||
| 305 | |||
| 306 | if (!DolibarrApi::_checkAccessToResource('societe', $this->companytoremove->id)) { |
||
| 307 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 308 | } |
||
| 309 | |||
| 310 | $soc_origin = $this->companytoremove; |
||
| 311 | $object = $this->company; |
||
| 312 | $user = DolibarrApiAccess::$user; |
||
| 313 | |||
| 314 | |||
| 315 | // Call same code than into action 'confirm_merge' |
||
| 316 | |||
| 317 | |||
| 318 | $db->begin(); |
||
| 319 | |||
| 320 | // Recopy some data |
||
| 321 | $object->client = $object->client | $soc_origin->client; |
||
| 322 | $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur; |
||
| 323 | $listofproperties = array( |
||
| 324 | 'address', 'zip', 'town', 'state_id', 'country_id', 'phone', 'phone_pro', 'fax', 'email', 'skype', 'url', 'barcode', |
||
| 325 | 'idprof1', 'idprof2', 'idprof3', 'idprof4', 'idprof5', 'idprof6', |
||
| 326 | 'tva_intra', 'effectif_id', 'forme_juridique', 'remise_percent', 'remise_supplier_percent', 'mode_reglement_supplier_id', 'cond_reglement_supplier_id', 'name_bis', |
||
| 327 | 'stcomm_id', 'outstanding_limit', 'price_level', 'parent', 'default_lang', 'ref', 'ref_ext', 'import_key', 'fk_incoterms', 'fk_multicurrency', |
||
| 328 | 'code_client', 'code_fournisseur', 'code_compta', 'code_compta_fournisseur', |
||
| 329 | 'model_pdf', 'fk_projet' |
||
| 330 | ); |
||
| 331 | foreach ($listofproperties as $property) |
||
| 332 | { |
||
| 333 | if (empty($object->$property)) $object->$property = $soc_origin->$property; |
||
| 334 | } |
||
| 335 | |||
| 336 | // Concat some data |
||
| 337 | $listofproperties = array( |
||
| 338 | 'note_public', 'note_private' |
||
| 339 | ); |
||
| 340 | foreach ($listofproperties as $property) |
||
| 341 | { |
||
| 342 | $object->$property = dol_concatdesc($object->$property, $soc_origin->$property); |
||
| 343 | } |
||
| 344 | |||
| 345 | // Merge extrafields |
||
| 346 | if (is_array($soc_origin->array_options)) |
||
| 347 | { |
||
| 348 | foreach ($soc_origin->array_options as $key => $val) |
||
| 349 | { |
||
| 350 | if (empty($object->array_options[$key])) $object->array_options[$key] = $val; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | // Merge categories |
||
| 355 | $static_cat = new Categorie($db); |
||
| 356 | $custcats = $static_cat->containing($soc_origin->id, 'customer', 'id'); |
||
| 357 | $object->setCategories($custcats, 'customer'); |
||
| 358 | $suppcats = $static_cat->containing($soc_origin->id, 'supplier', 'id'); |
||
| 359 | $object->setCategories($suppcats, 'supplier'); |
||
| 360 | |||
| 361 | // If thirdparty has a new code that is same than origin, we clean origin code to avoid duplicate key from database unique keys. |
||
| 362 | if ($soc_origin->code_client == $object->code_client |
||
| 363 | || $soc_origin->code_fournisseur == $object->code_fournisseur |
||
| 364 | || $soc_origin->barcode == $object->barcode) |
||
| 365 | { |
||
| 366 | dol_syslog("We clean customer and supplier code so we will be able to make the update of target"); |
||
| 367 | $soc_origin->code_client = ''; |
||
| 368 | $soc_origin->code_fournisseur = ''; |
||
| 369 | $soc_origin->barcode = ''; |
||
| 370 | $soc_origin->update($soc_origin->id, $user, 0, 1, 1, 'merge'); |
||
| 371 | } |
||
| 372 | |||
| 373 | // Update |
||
| 374 | $result = $object->update($object->id, $user, 0, 1, 1, 'merge'); |
||
| 375 | if ($result < 0) |
||
| 376 | { |
||
| 377 | $error++; |
||
| 378 | } |
||
| 379 | |||
| 380 | // Move links |
||
| 381 | if (!$error) |
||
| 382 | { |
||
| 383 | $objects = array( |
||
| 384 | 'Adherent' => '/adherents/class/adherent.class.php', |
||
| 385 | 'Societe' => '/societe/class/societe.class.php', |
||
| 386 | 'Categorie' => '/categories/class/categorie.class.php', |
||
| 387 | 'ActionComm' => '/comm/action/class/actioncomm.class.php', |
||
| 388 | 'Propal' => '/comm/propal/class/propal.class.php', |
||
| 389 | 'Commande' => '/commande/class/commande.class.php', |
||
| 390 | 'Facture' => '/compta/facture/class/facture.class.php', |
||
| 391 | 'FactureRec' => '/compta/facture/class/facture-rec.class.php', |
||
| 392 | 'LignePrelevement' => '/compta/prelevement/class/ligneprelevement.class.php', |
||
| 393 | 'Contact' => '/contact/class/contact.class.php', |
||
| 394 | 'Contrat' => '/contrat/class/contrat.class.php', |
||
| 395 | 'Expedition' => '/expedition/class/expedition.class.php', |
||
| 396 | 'Fichinter' => '/fichinter/class/fichinter.class.php', |
||
| 397 | 'CommandeFournisseur' => '/fourn/class/fournisseur.commande.class.php', |
||
| 398 | 'FactureFournisseur' => '/fourn/class/fournisseur.facture.class.php', |
||
| 399 | 'SupplierProposal' => '/supplier_proposal/class/supplier_proposal.class.php', |
||
| 400 | 'ProductFournisseur' => '/fourn/class/fournisseur.product.class.php', |
||
| 401 | 'Livraison' => '/livraison/class/livraison.class.php', |
||
| 402 | 'Product' => '/product/class/product.class.php', |
||
| 403 | 'Project' => '/projet/class/project.class.php', |
||
| 404 | 'User' => '/user/class/user.class.php', |
||
| 405 | ); |
||
| 406 | |||
| 407 | //First, all core objects must update their tables |
||
| 408 | foreach ($objects as $object_name => $object_file) |
||
| 409 | { |
||
| 410 | require_once DOL_DOCUMENT_ROOT.$object_file; |
||
| 411 | |||
| 412 | if (!$errors && !$object_name::replaceThirdparty($db, $soc_origin->id, $object->id)) |
||
|
|
|||
| 413 | { |
||
| 414 | $errors++; |
||
| 415 | //setEventMessages($db->lasterror(), null, 'errors'); |
||
| 416 | } |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | // External modules should update their ones too |
||
| 421 | if (!$errors) |
||
| 422 | { |
||
| 423 | $reshook = $hookmanager->executeHooks('replaceThirdparty', array( |
||
| 424 | 'soc_origin' => $soc_origin->id, |
||
| 425 | 'soc_dest' => $object->id |
||
| 426 | ), $soc_dest, $action); |
||
| 427 | |||
| 428 | if ($reshook < 0) |
||
| 429 | { |
||
| 430 | //setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); |
||
| 431 | $errors++; |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | |||
| 436 | if (!$error) |
||
| 437 | { |
||
| 438 | $object->context = array('merge'=>1, 'mergefromid'=>$soc_origin->id); |
||
| 439 | |||
| 440 | // Call trigger |
||
| 441 | $result = $object->call_trigger('COMPANY_MODIFY', $user); |
||
| 442 | if ($result < 0) |
||
| 443 | { |
||
| 444 | //setEventMessages($object->error, $object->errors, 'errors'); |
||
| 445 | $error++; |
||
| 446 | } |
||
| 447 | // End call triggers |
||
| 448 | } |
||
| 449 | |||
| 450 | if (!$error) |
||
| 451 | { |
||
| 452 | //We finally remove the old thirdparty |
||
| 453 | if ($soc_origin->delete($soc_origin->id, $user) < 1) |
||
| 454 | { |
||
| 455 | $errors++; |
||
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | // End of merge |
||
| 460 | |||
| 461 | if ($error) |
||
| 462 | { |
||
| 463 | $db->rollback(); |
||
| 464 | |||
| 465 | throw new RestException(500, 'Error failed to merged thirdparty '.$this->companytoremove->id.' into '.$id.'. Enable and read log file for more information.'); |
||
| 466 | } else { |
||
| 467 | $db->commit(); |
||
| 468 | } |
||
| 469 | |||
| 470 | return $this->get($id); |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Delete thirdparty |
||
| 475 | * |
||
| 476 | * @param int $id Thirparty ID |
||
| 477 | * @return integer |
||
| 478 | */ |
||
| 479 | public function delete($id) |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Set new price level for the given thirdparty |
||
| 497 | * |
||
| 498 | * @param int $id ID of thirdparty |
||
| 499 | * @param int $priceLevel Price level to apply to thirdparty |
||
| 500 | * @return object Thirdparty data without useless information |
||
| 501 | * |
||
| 502 | * @url PUT {id}/setpricelevel |
||
| 503 | * |
||
| 504 | * @throws RestException 400 Price level out of bounds |
||
| 505 | * @throws RestException 401 Access not allowed for your login |
||
| 506 | * @throws RestException 404 Thirdparty not found |
||
| 507 | * @throws RestException 500 Error fetching/setting price level |
||
| 508 | * @throws RestException 501 Request needs modules "Thirdparties" and "Products" and setting Multiprices activated |
||
| 509 | */ |
||
| 510 | public function setThirdpartyPriceLevel($id, $priceLevel) |
||
| 511 | { |
||
| 512 | global $conf; |
||
| 513 | |||
| 514 | if (empty($conf->societe->enabled)) { |
||
| 515 | throw new RestException(501, 'Module "Thirdparties" needed for this request'); |
||
| 516 | } |
||
| 517 | |||
| 518 | if (empty($conf->product->enabled)) { |
||
| 519 | throw new RestException(501, 'Module "Products" needed for this request'); |
||
| 520 | } |
||
| 521 | |||
| 522 | if (empty($conf->global->PRODUIT_MULTIPRICES)) { |
||
| 523 | throw new RestException(501, 'Multiprices features activation needed for this request'); |
||
| 524 | } |
||
| 525 | |||
| 526 | if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) { |
||
| 527 | throw new RestException(400, 'Price level must be between 1 and ' . $conf->global->PRODUIT_MULTIPRICES_LIMIT); |
||
| 528 | } |
||
| 529 | |||
| 530 | if (empty(DolibarrApiAccess::$user->rights->societe->creer)) { |
||
| 531 | throw new RestException(401, 'Access to thirdparty ' . $id . ' not allowed for login '. DolibarrApiAccess::$user->login); |
||
| 532 | } |
||
| 533 | |||
| 534 | $result = $this->company->fetch($id); |
||
| 535 | if ($result < 0) { |
||
| 536 | throw new RestException(404, 'Thirdparty ' . $id . ' not found'); |
||
| 537 | } |
||
| 538 | |||
| 539 | if (empty($result)) { |
||
| 540 | throw new RestException(500, 'Error fetching thirdparty ' . $id, array_merge(array($this->company->error), $this->company->errors)); |
||
| 541 | } |
||
| 542 | |||
| 543 | if (empty(DolibarrApi::_checkAccessToResource('societe', $this->company->id))) { |
||
| 544 | throw new RestException(401, 'Access to thirdparty ' . $id . ' not allowed for login ' . DolibarrApiAccess::$user->login); |
||
| 545 | } |
||
| 546 | |||
| 547 | $result = $this->company->set_price_level($priceLevel, DolibarrApiAccess::$user); |
||
| 548 | if ($result <= 0) { |
||
| 549 | throw new RestException(500, 'Error setting new price level for thirdparty ' . $id, array($this->company->db->lasterror())); |
||
| 550 | } |
||
| 551 | |||
| 552 | return $this->_cleanObjectDatas($this->company); |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Get customer categories for a thirdparty |
||
| 557 | * |
||
| 558 | * @param int $id ID of thirdparty |
||
| 559 | * @param string $sortfield Sort field |
||
| 560 | * @param string $sortorder Sort order |
||
| 561 | * @param int $limit Limit for list |
||
| 562 | * @param int $page Page number |
||
| 563 | * |
||
| 564 | * @return mixed |
||
| 565 | * |
||
| 566 | * @url GET {id}/categories |
||
| 567 | */ |
||
| 568 | public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) |
||
| 569 | { |
||
| 570 | if (!DolibarrApiAccess::$user->rights->categorie->lire) { |
||
| 571 | throw new RestException(401); |
||
| 572 | } |
||
| 573 | |||
| 574 | $result = $this->company->fetch($id); |
||
| 575 | if (!$result) |
||
| 576 | { |
||
| 577 | throw new RestException(404, 'Thirdparty not found'); |
||
| 578 | } |
||
| 579 | |||
| 580 | $categories = new Categorie($this->db); |
||
| 581 | |||
| 582 | $result = $categories->getListForItem($id, 'customer', $sortfield, $sortorder, $limit, $page); |
||
| 583 | |||
| 584 | if (is_numeric($result) && $result < 0) |
||
| 585 | { |
||
| 586 | throw new RestException(503, 'Error when retrieve category list : '.$categories->error); |
||
| 587 | } |
||
| 588 | |||
| 589 | if (is_numeric($result) && $result == 0) // To fix a return of 0 instead of empty array of method getListForItem |
||
| 590 | { |
||
| 591 | return array(); |
||
| 592 | } |
||
| 593 | |||
| 594 | return $result; |
||
| 595 | } |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Add a customer category to a thirdparty |
||
| 599 | * |
||
| 600 | * @param int $id Id of thirdparty |
||
| 601 | * @param int $category_id Id of category |
||
| 602 | * |
||
| 603 | * @return mixed |
||
| 604 | * |
||
| 605 | * @url POST {id}/categories/{category_id} |
||
| 606 | */ |
||
| 607 | public function addCategory($id, $category_id) |
||
| 608 | { |
||
| 609 | if (!DolibarrApiAccess::$user->rights->societe->creer) { |
||
| 610 | throw new RestException(401); |
||
| 611 | } |
||
| 612 | |||
| 613 | $result = $this->company->fetch($id); |
||
| 614 | if (!$result) { |
||
| 615 | throw new RestException(404, 'Thirdparty not found'); |
||
| 616 | } |
||
| 617 | $category = new Categorie($this->db); |
||
| 618 | $result = $category->fetch($category_id); |
||
| 619 | if (!$result) { |
||
| 620 | throw new RestException(404, 'category not found'); |
||
| 621 | } |
||
| 622 | |||
| 623 | if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) { |
||
| 624 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 625 | } |
||
| 626 | if (!DolibarrApi::_checkAccessToResource('category', $category->id)) { |
||
| 627 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 628 | } |
||
| 629 | |||
| 630 | $category->add_type($this->company, 'customer'); |
||
| 631 | |||
| 632 | return $this->_cleanObjectDatas($this->company); |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Remove the link between a customer category and the thirdparty |
||
| 637 | * |
||
| 638 | * @param int $id Id of thirdparty |
||
| 639 | * @param int $category_id Id of category |
||
| 640 | * |
||
| 641 | * @return mixed |
||
| 642 | * |
||
| 643 | * @url DELETE {id}/categories/{category_id} |
||
| 644 | */ |
||
| 645 | public function deleteCategory($id, $category_id) |
||
| 646 | { |
||
| 647 | if (!DolibarrApiAccess::$user->rights->societe->creer) { |
||
| 648 | throw new RestException(401); |
||
| 649 | } |
||
| 650 | |||
| 651 | $result = $this->company->fetch($id); |
||
| 652 | if (!$result) { |
||
| 653 | throw new RestException(404, 'Thirdparty not found'); |
||
| 654 | } |
||
| 655 | $category = new Categorie($this->db); |
||
| 656 | $result = $category->fetch($category_id); |
||
| 657 | if (!$result) { |
||
| 658 | throw new RestException(404, 'category not found'); |
||
| 659 | } |
||
| 660 | |||
| 661 | if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) { |
||
| 662 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 663 | } |
||
| 664 | if (!DolibarrApi::_checkAccessToResource('category', $category->id)) { |
||
| 665 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 666 | } |
||
| 667 | |||
| 668 | $category->del_type($this->company, 'customer'); |
||
| 669 | |||
| 670 | return $this->_cleanObjectDatas($this->company); |
||
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Get supplier categories for a thirdparty |
||
| 675 | * |
||
| 676 | * @param int $id ID of thirdparty |
||
| 677 | * @param string $sortfield Sort field |
||
| 678 | * @param string $sortorder Sort order |
||
| 679 | * @param int $limit Limit for list |
||
| 680 | * @param int $page Page number |
||
| 681 | * |
||
| 682 | * @return mixed |
||
| 683 | * |
||
| 684 | * @url GET {id}/supplier_categories |
||
| 685 | */ |
||
| 686 | public function getSupplierCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) |
||
| 687 | { |
||
| 688 | if (!DolibarrApiAccess::$user->rights->categorie->lire) { |
||
| 689 | throw new RestException(401); |
||
| 690 | } |
||
| 691 | |||
| 692 | $result = $this->company->fetch($id); |
||
| 693 | if (!$result) |
||
| 694 | { |
||
| 695 | throw new RestException(404, 'Thirdparty not found'); |
||
| 696 | } |
||
| 697 | |||
| 698 | $categories = new Categorie($this->db); |
||
| 699 | |||
| 700 | $result = $categories->getListForItem($id, 'supplier', $sortfield, $sortorder, $limit, $page); |
||
| 701 | |||
| 702 | if (is_numeric($result) && $result < 0) |
||
| 703 | { |
||
| 704 | throw new RestException(503, 'Error when retrieve category list : '.$categories->error); |
||
| 705 | } |
||
| 706 | |||
| 707 | if (is_numeric($result) && $result == 0) // To fix a return of 0 instead of empty array of method getListForItem |
||
| 708 | { |
||
| 709 | return array(); |
||
| 710 | } |
||
| 711 | |||
| 712 | return $result; |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Add a supplier category to a thirdparty |
||
| 717 | * |
||
| 718 | * @param int $id Id of thirdparty |
||
| 719 | * @param int $category_id Id of category |
||
| 720 | * |
||
| 721 | * @return mixed |
||
| 722 | * |
||
| 723 | * @url POST {id}/supplier_categories/{category_id} |
||
| 724 | */ |
||
| 725 | public function addSupplierCategory($id, $category_id) |
||
| 726 | { |
||
| 727 | if (!DolibarrApiAccess::$user->rights->societe->creer) { |
||
| 728 | throw new RestException(401); |
||
| 729 | } |
||
| 730 | |||
| 731 | $result = $this->company->fetch($id); |
||
| 732 | if (!$result) { |
||
| 733 | throw new RestException(404, 'Thirdparty not found'); |
||
| 734 | } |
||
| 735 | $category = new Categorie($this->db); |
||
| 736 | $result = $category->fetch($category_id); |
||
| 737 | if (!$result) { |
||
| 738 | throw new RestException(404, 'category not found'); |
||
| 739 | } |
||
| 740 | |||
| 741 | if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) { |
||
| 742 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 743 | } |
||
| 744 | if (!DolibarrApi::_checkAccessToResource('category', $category->id)) { |
||
| 745 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 746 | } |
||
| 747 | |||
| 748 | $category->add_type($this->company, 'supplier'); |
||
| 749 | |||
| 750 | return $this->_cleanObjectDatas($this->company); |
||
| 751 | } |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Remove the link between a category and the thirdparty |
||
| 755 | * |
||
| 756 | * @param int $id Id of thirdparty |
||
| 757 | * @param int $category_id Id of category |
||
| 758 | * |
||
| 759 | * @return mixed |
||
| 760 | * |
||
| 761 | * @url DELETE {id}/supplier_categories/{category_id} |
||
| 762 | */ |
||
| 763 | public function deleteSupplierCategory($id, $category_id) |
||
| 764 | { |
||
| 765 | if (!DolibarrApiAccess::$user->rights->societe->creer) { |
||
| 766 | throw new RestException(401); |
||
| 767 | } |
||
| 768 | |||
| 769 | $result = $this->company->fetch($id); |
||
| 770 | if (!$result) { |
||
| 771 | throw new RestException(404, 'Thirdparty not found'); |
||
| 772 | } |
||
| 773 | $category = new Categorie($this->db); |
||
| 774 | $result = $category->fetch($category_id); |
||
| 775 | if (!$result) { |
||
| 776 | throw new RestException(404, 'category not found'); |
||
| 777 | } |
||
| 778 | |||
| 779 | if (!DolibarrApi::_checkAccessToResource('societe', $this->company->id)) { |
||
| 780 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 781 | } |
||
| 782 | if (!DolibarrApi::_checkAccessToResource('category', $category->id)) { |
||
| 783 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 784 | } |
||
| 785 | |||
| 786 | $category->del_type($this->company, 'supplier'); |
||
| 787 | |||
| 788 | return $this->_cleanObjectDatas($this->company); |
||
| 789 | } |
||
| 790 | |||
| 791 | |||
| 792 | /** |
||
| 793 | * Get outstanding proposals of thirdparty |
||
| 794 | * |
||
| 795 | * @param int $id ID of the thirdparty |
||
| 796 | * @param string $mode 'customer' or 'supplier' |
||
| 797 | * |
||
| 798 | * @url GET {id}/outstandingproposals |
||
| 799 | * |
||
| 800 | * @return array List of outstandings proposals of thirdparty |
||
| 801 | * |
||
| 802 | * @throws RestException 400 |
||
| 803 | * @throws RestException 401 |
||
| 804 | * @throws RestException 404 |
||
| 805 | */ |
||
| 806 | public function getOutStandingProposals($id, $mode = 'customer') |
||
| 807 | { |
||
| 808 | $obj_ret = array(); |
||
| 809 | |||
| 810 | if (!DolibarrApiAccess::$user->rights->societe->lire) { |
||
| 811 | throw new RestException(401); |
||
| 812 | } |
||
| 813 | |||
| 814 | if (empty($id)) { |
||
| 815 | throw new RestException(400, 'Thirdparty ID is mandatory'); |
||
| 816 | } |
||
| 817 | |||
| 818 | if (!DolibarrApi::_checkAccessToResource('societe', $id)) { |
||
| 819 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 820 | } |
||
| 821 | |||
| 822 | $result = $this->company->fetch($id); |
||
| 823 | if (!$result) { |
||
| 824 | throw new RestException(404, 'Thirdparty not found'); |
||
| 825 | } |
||
| 826 | |||
| 827 | $result = $this->company->getOutstandingProposals($mode); |
||
| 828 | |||
| 829 | unset($result['total_ht']); |
||
| 830 | unset($result['total_ttc']); |
||
| 831 | |||
| 832 | return $result; |
||
| 833 | } |
||
| 834 | |||
| 835 | |||
| 836 | /** |
||
| 837 | * Get outstanding orders of thirdparty |
||
| 838 | * |
||
| 839 | * @param int $id ID of the thirdparty |
||
| 840 | * @param string $mode 'customer' or 'supplier' |
||
| 841 | * |
||
| 842 | * @url GET {id}/outstandingorders |
||
| 843 | * |
||
| 844 | * @return array List of outstandings orders of thirdparty |
||
| 845 | * |
||
| 846 | * @throws RestException 400 |
||
| 847 | * @throws RestException 401 |
||
| 848 | * @throws RestException 404 |
||
| 849 | */ |
||
| 850 | public function getOutStandingOrder($id, $mode = 'customer') |
||
| 851 | { |
||
| 852 | $obj_ret = array(); |
||
| 853 | |||
| 854 | if (!DolibarrApiAccess::$user->rights->societe->lire) { |
||
| 855 | throw new RestException(401); |
||
| 856 | } |
||
| 857 | |||
| 858 | if (empty($id)) { |
||
| 859 | throw new RestException(400, 'Thirdparty ID is mandatory'); |
||
| 860 | } |
||
| 861 | |||
| 862 | if (!DolibarrApi::_checkAccessToResource('societe', $id)) { |
||
| 863 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 864 | } |
||
| 865 | |||
| 866 | $result = $this->company->fetch($id); |
||
| 867 | if (!$result) { |
||
| 868 | throw new RestException(404, 'Thirdparty not found'); |
||
| 869 | } |
||
| 870 | |||
| 871 | $result = $this->company->getOutstandingOrders($mode); |
||
| 872 | |||
| 873 | unset($result['total_ht']); |
||
| 874 | unset($result['total_ttc']); |
||
| 875 | |||
| 876 | return $result; |
||
| 877 | } |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Get outstanding invoices of thirdparty |
||
| 881 | * |
||
| 882 | * @param int $id ID of the thirdparty |
||
| 883 | * @param string $mode 'customer' or 'supplier' |
||
| 884 | * |
||
| 885 | * @url GET {id}/outstandinginvoices |
||
| 886 | * |
||
| 887 | * @return array List of outstandings invoices of thirdparty |
||
| 888 | * |
||
| 889 | * @throws RestException 400 |
||
| 890 | * @throws RestException 401 |
||
| 891 | * @throws RestException 404 |
||
| 892 | */ |
||
| 893 | public function getOutStandingInvoices($id, $mode = 'customer') |
||
| 894 | { |
||
| 895 | $obj_ret = array(); |
||
| 896 | |||
| 897 | if (!DolibarrApiAccess::$user->rights->societe->lire) { |
||
| 898 | throw new RestException(401); |
||
| 899 | } |
||
| 900 | |||
| 901 | if (empty($id)) { |
||
| 902 | throw new RestException(400, 'Thirdparty ID is mandatory'); |
||
| 903 | } |
||
| 904 | |||
| 905 | if (!DolibarrApi::_checkAccessToResource('societe', $id)) { |
||
| 906 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 907 | } |
||
| 908 | |||
| 909 | $result = $this->company->fetch($id); |
||
| 910 | if (!$result) { |
||
| 911 | throw new RestException(404, 'Thirdparty not found'); |
||
| 912 | } |
||
| 913 | |||
| 914 | $result = $this->company->getOutstandingBills($mode); |
||
| 915 | |||
| 916 | unset($result['total_ht']); |
||
| 917 | unset($result['total_ttc']); |
||
| 918 | |||
| 919 | return $result; |
||
| 920 | } |
||
| 921 | |||
| 922 | /** |
||
| 923 | * Get representatives of thirdparty |
||
| 924 | * |
||
| 925 | * @param int $id ID of the thirdparty |
||
| 926 | * @param string $mode 0=Array with properties, 1=Array of id. |
||
| 927 | * |
||
| 928 | * @url GET {id}/representatives |
||
| 929 | * |
||
| 930 | * @return array List of representatives of thirdparty |
||
| 931 | * |
||
| 932 | * @throws RestException 400 |
||
| 933 | * @throws RestException 401 |
||
| 934 | * @throws RestException 404 |
||
| 935 | */ |
||
| 936 | public function getSalesRepresentatives($id, $mode = 0) |
||
| 937 | { |
||
| 938 | $obj_ret = array(); |
||
| 939 | |||
| 940 | if (!DolibarrApiAccess::$user->rights->societe->lire) { |
||
| 941 | throw new RestException(401); |
||
| 942 | } |
||
| 943 | |||
| 944 | if (empty($id)) { |
||
| 945 | throw new RestException(400, 'Thirdparty ID is mandatory'); |
||
| 946 | } |
||
| 947 | |||
| 948 | if (!DolibarrApi::_checkAccessToResource('societe', $id)) { |
||
| 949 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 950 | } |
||
| 951 | |||
| 952 | $result = $this->company->fetch($id); |
||
| 953 | if (!$result) { |
||
| 954 | throw new RestException(404, 'Thirdparty not found'); |
||
| 955 | } |
||
| 956 | |||
| 957 | $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode); |
||
| 958 | |||
| 959 | return $result; |
||
| 960 | } |
||
| 961 | |||
| 962 | /** |
||
| 963 | * Get fixed amount discount of a thirdparty (all sources: deposit, credit note, commercial offers...) |
||
| 964 | * |
||
| 965 | * @param int $id ID of the thirdparty |
||
| 966 | * @param string $filter Filter exceptional discount. "none" will return every discount, "available" returns unapplied discounts, "used" returns applied discounts {@choice none,available,used} |
||
| 967 | * @param string $sortfield Sort field |
||
| 968 | * @param string $sortorder Sort order |
||
| 969 | * |
||
| 970 | * @url GET {id}/fixedamountdiscounts |
||
| 971 | * |
||
| 972 | * @return array List of fixed discount of thirdparty |
||
| 973 | * |
||
| 974 | * @throws RestException 400 |
||
| 975 | * @throws RestException 401 |
||
| 976 | * @throws RestException 404 |
||
| 977 | * @throws RestException 503 |
||
| 978 | */ |
||
| 979 | public function getFixedAmountDiscounts($id, $filter = "none", $sortfield = "f.type", $sortorder = 'ASC') |
||
| 980 | { |
||
| 981 | $obj_ret = array(); |
||
| 982 | |||
| 983 | if (!DolibarrApiAccess::$user->rights->societe->lire) { |
||
| 984 | throw new RestException(401); |
||
| 985 | } |
||
| 986 | |||
| 987 | if (empty($id)) { |
||
| 988 | throw new RestException(400, 'Thirdparty ID is mandatory'); |
||
| 989 | } |
||
| 990 | |||
| 991 | if (!DolibarrApi::_checkAccessToResource('societe', $id)) { |
||
| 992 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 993 | } |
||
| 994 | |||
| 995 | $result = $this->company->fetch($id); |
||
| 996 | if (!$result) { |
||
| 997 | throw new RestException(404, 'Thirdparty not found'); |
||
| 998 | } |
||
| 999 | |||
| 1000 | |||
| 1001 | $sql = "SELECT f.ref, f.type as factype, re.fk_facture_source, re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc, re.description, re.fk_facture, re.fk_facture_line"; |
||
| 1002 | $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re, ".MAIN_DB_PREFIX."facture as f"; |
||
| 1003 | $sql .= " WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".$id; |
||
| 1004 | if ($filter == "available") $sql .= " AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL"; |
||
| 1005 | if ($filter == "used") $sql .= " AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)"; |
||
| 1006 | |||
| 1007 | $sql .= $this->db->order($sortfield, $sortorder); |
||
| 1008 | |||
| 1009 | $result = $this->db->query($sql); |
||
| 1010 | if (!$result) { |
||
| 1011 | throw new RestException(503, $this->db->lasterror()); |
||
| 1012 | } else { |
||
| 1013 | $num = $this->db->num_rows($result); |
||
| 1014 | while ($obj = $this->db->fetch_object($result)) { |
||
| 1015 | $obj_ret[] = $obj; |
||
| 1016 | } |
||
| 1017 | } |
||
| 1018 | |||
| 1019 | return $obj_ret; |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | |||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Return list of invoices qualified to be replaced by another invoice. |
||
| 1026 | * |
||
| 1027 | * @param int $id Id of thirdparty |
||
| 1028 | * |
||
| 1029 | * @url GET {id}/getinvoicesqualifiedforreplacement |
||
| 1030 | * |
||
| 1031 | * @return array |
||
| 1032 | * @throws RestException 400 |
||
| 1033 | * @throws RestException 401 |
||
| 1034 | * @throws RestException 404 |
||
| 1035 | * @throws RestException 405 |
||
| 1036 | */ |
||
| 1037 | public function getInvoicesQualifiedForReplacement($id) |
||
| 1038 | { |
||
| 1039 | if (!DolibarrApiAccess::$user->rights->facture->lire) { |
||
| 1040 | throw new RestException(401); |
||
| 1041 | } |
||
| 1042 | if (empty($id)) { |
||
| 1043 | throw new RestException(400, 'Thirdparty ID is mandatory'); |
||
| 1044 | } |
||
| 1045 | |||
| 1046 | if (!DolibarrApi::_checkAccessToResource('societe', $id)) { |
||
| 1047 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 1048 | } |
||
| 1049 | |||
| 1050 | /*$result = $this->thirdparty->fetch($id); |
||
| 1051 | if( ! $result ) { |
||
| 1052 | throw new RestException(404, 'Thirdparty not found'); |
||
| 1053 | }*/ |
||
| 1054 | |||
| 1055 | $invoice = new Facture($this->db); |
||
| 1056 | $result = $invoice->list_replacable_invoices($id); |
||
| 1057 | if ($result < 0) { |
||
| 1058 | throw new RestException(405, $this->thirdparty->error); |
||
| 1059 | } |
||
| 1060 | |||
| 1061 | return $result; |
||
| 1062 | } |
||
| 1063 | |||
| 1064 | /** |
||
| 1065 | * Return list of invoices qualified to be corrected by a credit note. |
||
| 1066 | * Invoices matching the following rules are returned |
||
| 1067 | * (validated + payment on process) or classified (payed completely or payed partialy) + not already replaced + not already a credit note |
||
| 1068 | * |
||
| 1069 | * @param int $id Id of thirdparty |
||
| 1070 | * |
||
| 1071 | * @url GET {id}/getinvoicesqualifiedforcreditnote |
||
| 1072 | * |
||
| 1073 | * @return array |
||
| 1074 | * |
||
| 1075 | * @throws RestException 400 |
||
| 1076 | * @throws RestException 401 |
||
| 1077 | * @throws RestException 404 |
||
| 1078 | * @throws RestException 405 |
||
| 1079 | */ |
||
| 1080 | public function getInvoicesQualifiedForCreditNote($id) |
||
| 1081 | { |
||
| 1082 | if (!DolibarrApiAccess::$user->rights->facture->lire) { |
||
| 1083 | throw new RestException(401); |
||
| 1084 | } |
||
| 1085 | if (empty($id)) { |
||
| 1086 | throw new RestException(400, 'Thirdparty ID is mandatory'); |
||
| 1087 | } |
||
| 1088 | |||
| 1089 | if (!DolibarrApi::_checkAccessToResource('societe', $id)) { |
||
| 1090 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 1091 | } |
||
| 1092 | |||
| 1093 | /*$result = $this->thirdparty->fetch($id); |
||
| 1094 | if( ! $result ) { |
||
| 1095 | throw new RestException(404, 'Thirdparty not found'); |
||
| 1096 | }*/ |
||
| 1097 | |||
| 1098 | $invoice = new Facture($this->db); |
||
| 1099 | $result = $invoice->list_qualified_avoir_invoices($id); |
||
| 1100 | if ($result < 0) { |
||
| 1101 | throw new RestException(405, $this->thirdparty->error); |
||
| 1102 | } |
||
| 1103 | |||
| 1104 | return $result; |
||
| 1105 | } |
||
| 1106 | |||
| 1107 | /** |
||
| 1108 | * Get CompanyBankAccount objects for thirdparty |
||
| 1109 | * |
||
| 1110 | * @param int $id ID of thirdparty |
||
| 1111 | * |
||
| 1112 | * @return array |
||
| 1113 | * |
||
| 1114 | * @url GET {id}/bankaccounts |
||
| 1115 | */ |
||
| 1116 | public function getCompanyBankAccount($id) |
||
| 1117 | { |
||
| 1118 | global $db, $conf; |
||
| 1119 | |||
| 1120 | if (!DolibarrApiAccess::$user->rights->facture->lire) { |
||
| 1121 | throw new RestException(401); |
||
| 1122 | } |
||
| 1123 | if (empty($id)) { |
||
| 1124 | throw new RestException(400, 'Thirdparty ID is mandatory'); |
||
| 1125 | } |
||
| 1126 | |||
| 1127 | if (!DolibarrApi::_checkAccessToResource('societe', $id)) { |
||
| 1128 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 1129 | } |
||
| 1130 | |||
| 1131 | /** |
||
| 1132 | * We select all the records that match the socid |
||
| 1133 | */ |
||
| 1134 | |||
| 1135 | $sql = "SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,"; |
||
| 1136 | $sql .= " owner_address, default_rib, label, datec, tms as datem, rum, frstrecur"; |
||
| 1137 | $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib"; |
||
| 1138 | if ($id) $sql .= " WHERE fk_soc = ".$id." "; |
||
| 1139 | |||
| 1140 | |||
| 1141 | $result = $db->query($sql); |
||
| 1142 | |||
| 1143 | if ($result->num_rows == 0) { |
||
| 1144 | throw new RestException(404, 'Account not found'); |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | $i = 0; |
||
| 1148 | |||
| 1149 | $accounts = array(); |
||
| 1150 | |||
| 1151 | if ($result) |
||
| 1152 | { |
||
| 1153 | $num = $db->num_rows($result); |
||
| 1154 | while ($i < $num) |
||
| 1155 | { |
||
| 1156 | $obj = $db->fetch_object($result); |
||
| 1157 | $account = new CompanyBankAccount($db); |
||
| 1158 | if ($account->fetch($obj->rowid)) { |
||
| 1159 | $accounts[] = $account; |
||
| 1160 | } |
||
| 1161 | $i++; |
||
| 1162 | } |
||
| 1163 | } else { |
||
| 1164 | throw new RestException(404, 'Account not found'); |
||
| 1165 | } |
||
| 1166 | |||
| 1167 | |||
| 1168 | $fields = array('socid', 'default_rib', 'frstrecur', '1000110000001', 'datec', 'datem', 'label', 'bank', 'bic', 'iban', 'id', 'rum'); |
||
| 1169 | |||
| 1170 | $returnAccounts = array(); |
||
| 1171 | |||
| 1172 | foreach ($accounts as $account) { |
||
| 1173 | $object = array(); |
||
| 1174 | foreach ($account as $key => $value) { |
||
| 1175 | if (in_array($key, $fields)) { |
||
| 1176 | $object[$key] = $value; |
||
| 1177 | } |
||
| 1178 | } |
||
| 1179 | $returnAccounts[] = $object; |
||
| 1180 | } |
||
| 1181 | |||
| 1182 | return $returnAccounts; |
||
| 1183 | } |
||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * Create CompanyBankAccount object for thirdparty |
||
| 1187 | * @param int $id ID of thirdparty |
||
| 1188 | * @param array $request_data Request data |
||
| 1189 | * |
||
| 1190 | * @return object BankAccount of thirdparty |
||
| 1191 | * |
||
| 1192 | * @url POST {id}/bankaccounts |
||
| 1193 | */ |
||
| 1194 | public function createCompanyBankAccount($id, $request_data = null) |
||
| 1195 | { |
||
| 1196 | if (!DolibarrApiAccess::$user->rights->societe->creer) { |
||
| 1197 | throw new RestException(401); |
||
| 1198 | } |
||
| 1199 | if ($this->company->fetch($id) <= 0) { |
||
| 1200 | throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists'); |
||
| 1201 | } |
||
| 1202 | $account = new CompanyBankAccount($this->db); |
||
| 1203 | |||
| 1204 | $account->socid = $id; |
||
| 1205 | |||
| 1206 | foreach ($request_data as $field => $value) { |
||
| 1207 | $account->$field = $value; |
||
| 1208 | } |
||
| 1209 | |||
| 1210 | if ($account->create(DolibarrApiAccess::$user) < 0) |
||
| 1211 | throw new RestException(500, 'Error creating Company Bank account'); |
||
| 1212 | |||
| 1213 | if (empty($account->rum)) { |
||
| 1214 | require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php'; |
||
| 1215 | $prelevement = new BonPrelevement($this->db); |
||
| 1216 | $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id); |
||
| 1217 | $account->date_rum = dol_now(); |
||
| 1218 | } |
||
| 1219 | |||
| 1220 | if ($account->update(DolibarrApiAccess::$user) < 0) |
||
| 1221 | throw new RestException(500, 'Error updating values'); |
||
| 1222 | |||
| 1223 | return $this->_cleanObjectDatas($account); |
||
| 1224 | } |
||
| 1225 | |||
| 1226 | /** |
||
| 1227 | * Update CompanyBankAccount object for thirdparty |
||
| 1228 | * |
||
| 1229 | * @param int $id ID of thirdparty |
||
| 1230 | * @param int $bankaccount_id ID of CompanyBankAccount |
||
| 1231 | * @param array $request_data Request data |
||
| 1232 | * |
||
| 1233 | * @return object BankAccount of thirdparty |
||
| 1234 | * |
||
| 1235 | * @url PUT {id}/bankaccounts/{bankaccount_id} |
||
| 1236 | */ |
||
| 1237 | public function updateCompanyBankAccount($id, $bankaccount_id, $request_data = null) |
||
| 1238 | { |
||
| 1239 | if (!DolibarrApiAccess::$user->rights->societe->creer) { |
||
| 1240 | throw new RestException(401); |
||
| 1241 | } |
||
| 1242 | if ($this->company->fetch($id) <= 0) { |
||
| 1243 | throw new RestException(404, 'Error creating Company Bank account, Company doesn\'t exists'); |
||
| 1244 | } |
||
| 1245 | $account = new CompanyBankAccount($this->db); |
||
| 1246 | |||
| 1247 | $account->fetch($bankaccount_id, $id, -1, ''); |
||
| 1248 | |||
| 1249 | if ($account->socid != $id) { |
||
| 1250 | throw new RestException(401); |
||
| 1251 | } |
||
| 1252 | |||
| 1253 | |||
| 1254 | foreach ($request_data as $field => $value) { |
||
| 1255 | $account->$field = $value; |
||
| 1256 | } |
||
| 1257 | |||
| 1258 | if (empty($account->rum)) { |
||
| 1259 | require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php'; |
||
| 1260 | $prelevement = new BonPrelevement($this->db); |
||
| 1261 | $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id); |
||
| 1262 | $account->date_rum = dol_now(); |
||
| 1263 | } |
||
| 1264 | |||
| 1265 | if ($account->update(DolibarrApiAccess::$user) < 0) |
||
| 1266 | throw new RestException(500, 'Error updating values'); |
||
| 1267 | |||
| 1268 | return $this->_cleanObjectDatas($account); |
||
| 1269 | } |
||
| 1270 | |||
| 1271 | /** |
||
| 1272 | * Delete a bank account attached to a thirdparty |
||
| 1273 | * |
||
| 1274 | * @param int $id ID of thirdparty |
||
| 1275 | * @param int $bankaccount_id ID of CompanyBankAccount |
||
| 1276 | * |
||
| 1277 | * @return int -1 if error 1 if correct deletion |
||
| 1278 | * |
||
| 1279 | * @url DELETE {id}/bankaccounts/{bankaccount_id} |
||
| 1280 | */ |
||
| 1281 | public function deleteCompanyBankAccount($id, $bankaccount_id) |
||
| 1282 | { |
||
| 1283 | if (!DolibarrApiAccess::$user->rights->societe->creer) { |
||
| 1284 | throw new RestException(401); |
||
| 1285 | } |
||
| 1286 | |||
| 1287 | $account = new CompanyBankAccount($this->db); |
||
| 1288 | |||
| 1289 | $account->fetch($bankaccount_id); |
||
| 1290 | |||
| 1291 | if (!$account->socid == $id) |
||
| 1292 | throw new RestException(401); |
||
| 1293 | |||
| 1294 | return $account->delete(DolibarrApiAccess::$user); |
||
| 1295 | } |
||
| 1296 | |||
| 1297 | /** |
||
| 1298 | * Generate a Document from a bank account record (like SEPA mandate) |
||
| 1299 | * |
||
| 1300 | * @param int $id Thirdparty id |
||
| 1301 | * @param int $companybankid Companybank id |
||
| 1302 | * @param string $model Model of document to generate |
||
| 1303 | * @return void |
||
| 1304 | * |
||
| 1305 | * @url GET {id}/generateBankAccountDocument/{companybankid}/{model} |
||
| 1306 | */ |
||
| 1307 | public function generateBankAccountDocument($id, $companybankid = null, $model = 'sepamandate') |
||
| 1308 | { |
||
| 1309 | global $conf; |
||
| 1310 | |||
| 1311 | $this->langs->loadLangs(array("main", "dict", "commercial", "products", "companies", "banks", "bills", "withdrawals")); |
||
| 1312 | |||
| 1313 | $this->company->fetch($id); |
||
| 1314 | |||
| 1315 | $action = 'builddoc'; |
||
| 1316 | if (!DolibarrApiAccess::$user->rights->societe->creer) |
||
| 1317 | throw new RestException(401); |
||
| 1318 | |||
| 1319 | $this->company->setDocModel(DolibarrApiAccess::$user, $model); |
||
| 1320 | |||
| 1321 | $this->company->fk_bank = $this->company->fk_account; |
||
| 1322 | |||
| 1323 | $outputlangs = $this->langs; |
||
| 1324 | $newlang = ''; |
||
| 1325 | |||
| 1326 | if ($this->conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09'); |
||
| 1327 | if ($this->conf->global->MAIN_MULTILANGS && empty($newlang) && isset($this->company->thirdparty->default_lang)) $newlang = $this->company->thirdparty->default_lang; // for proposal, order, invoice, ... |
||
| 1328 | if ($this->conf->global->MAIN_MULTILANGS && empty($newlang) && isset($this->company->default_lang)) $newlang = $this->company->default_lang; // for thirdparty |
||
| 1329 | if (!empty($newlang)) { |
||
| 1330 | $outputlangs = new Translate("", $conf); |
||
| 1331 | $outputlangs->setDefaultLang($newlang); |
||
| 1332 | } |
||
| 1333 | |||
| 1334 | // To be sure vars is defined |
||
| 1335 | $hidedetails = $hidedesc = $hideref = 0; |
||
| 1336 | $moreparams = null; |
||
| 1337 | if (empty($hidedetails)) $hidedetails = 0; |
||
| 1338 | if (empty($hidedesc)) $hidedesc = 0; |
||
| 1339 | if (empty($hideref)) $hideref = 0; |
||
| 1340 | if (empty($moreparams)) $moreparams = null; |
||
| 1341 | |||
| 1342 | |||
| 1343 | $sql = "SELECT rowid"; |
||
| 1344 | $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib"; |
||
| 1345 | if ($id) $sql .= " WHERE fk_soc = ".$id." "; |
||
| 1346 | if ($companybankid) $sql .= " AND id = ".$companybankid.""; |
||
| 1347 | |||
| 1348 | $i = 0; |
||
| 1349 | $accounts = array(); |
||
| 1350 | |||
| 1351 | $result = $this->db->query($sql); |
||
| 1352 | if ($result) |
||
| 1353 | { |
||
| 1354 | if ($result->num_rows == 0) { |
||
| 1355 | throw new RestException(404, 'Bank account not found'); |
||
| 1356 | } |
||
| 1357 | |||
| 1358 | $num = $this->db->num_rows($result); |
||
| 1359 | while ($i < $num) |
||
| 1360 | { |
||
| 1361 | $obj = $this->db->fetch_object($result); |
||
| 1362 | |||
| 1363 | $account = new CompanyBankAccount($this->db); |
||
| 1364 | if ($account->fetch($obj->rowid)) { |
||
| 1365 | $accounts[] = $account; |
||
| 1366 | } |
||
| 1367 | $i++; |
||
| 1368 | } |
||
| 1369 | } else { |
||
| 1370 | throw new RestException(404, 'Bank account not found'); |
||
| 1371 | } |
||
| 1372 | |||
| 1373 | $moreparams = array( |
||
| 1374 | 'use_companybankid'=>$accounts[0]->id, |
||
| 1375 | 'force_dir_output'=>$this->conf->societe->multidir_output[$this->company->entity].'/'.dol_sanitizeFileName($this->company->id) |
||
| 1376 | ); |
||
| 1377 | |||
| 1378 | $result = 0; |
||
| 1379 | |||
| 1380 | $result = $this->company->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams); |
||
| 1381 | |||
| 1382 | if ($result > 0) |
||
| 1383 | { |
||
| 1384 | return array("success" => $result); |
||
| 1385 | } else { |
||
| 1386 | throw new RestException(500); |
||
| 1387 | } |
||
| 1388 | } |
||
| 1389 | |||
| 1390 | /** |
||
| 1391 | * Get a specific gateway attached to a thirdparty (by specifying the site key) |
||
| 1392 | * |
||
| 1393 | * @param int $id ID of thirdparty |
||
| 1394 | * @param string $site Site key |
||
| 1395 | * |
||
| 1396 | * @return SocieteAccount[] |
||
| 1397 | * @throws RestException 401 Unauthorized: User does not have permission to read thirdparties |
||
| 1398 | * @throws RestException 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty |
||
| 1399 | * |
||
| 1400 | * @url GET {id}/gateways/ |
||
| 1401 | */ |
||
| 1402 | public function getSocieteAccounts($id, $site = null) |
||
| 1458 | } |
||
| 1459 | |||
| 1460 | /** |
||
| 1461 | * Create and attach a new gateway to an existing thirdparty |
||
| 1462 | * |
||
| 1463 | * Possible fields for request_data (request body) are specified in <code>llx_societe_account</code> table.<br> |
||
| 1464 | * See <a href="https://wiki.dolibarr.org/index.php/Table_llx_societe_account">Table llx_societe_account</a> wiki page for more information<br><br> |
||
| 1465 | * <u>Example body payload :</u> <pre>{"key_account": "cus_DAVkLSs1LYyYI", "site": "stripe"}</pre> |
||
| 1466 | * |
||
| 1467 | * @param int $id ID of thirdparty |
||
| 1468 | * @param array $request_data Request data |
||
| 1469 | * |
||
| 1470 | * @return SocieteAccount |
||
| 1471 | * |
||
| 1472 | * @throws RestException 401 Unauthorized: User does not have permission to read thirdparties |
||
| 1473 | * @throws RestException 409 Conflict: A SocieteAccount entity (gateway) already exists for this company and site. |
||
| 1474 | * @throws RestException 422 Unprocessable Entity: You must pass the site attribute in your request data ! |
||
| 1475 | * @throws RestException 500 Internal Server Error: Error creating SocieteAccount account |
||
| 1476 | * |
||
| 1477 | * @url POST {id}/gateways |
||
| 1478 | */ |
||
| 1479 | public function createSocieteAccount($id, $request_data = null) |
||
| 1480 | { |
||
| 1481 | global $db; |
||
| 1482 | |||
| 1483 | if (!DolibarrApiAccess::$user->rights->societe->creer) { |
||
| 1484 | throw new RestException(401); |
||
| 1485 | } |
||
| 1486 | |||
| 1487 | if (!isset($request_data['site'])) { |
||
| 1488 | throw new RestException(422, 'Unprocessable Entity: You must pass the site attribute in your request data !'); |
||
| 1489 | } |
||
| 1490 | |||
| 1491 | $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".$id." AND site = '".$request_data['site']."' "; |
||
| 1492 | $result = $db->query($sql); |
||
| 1493 | |||
| 1494 | if ($result->num_rows == 0) { |
||
| 1495 | $account = new SocieteAccount($this->db); |
||
| 1496 | if (!isset($request_data['login'])) { |
||
| 1497 | $account->login = ""; |
||
| 1498 | } |
||
| 1499 | $account->fk_soc = $id; |
||
| 1500 | |||
| 1501 | foreach ($request_data as $field => $value) { |
||
| 1502 | $account->$field = $value; |
||
| 1503 | } |
||
| 1504 | |||
| 1505 | if ($account->create(DolibarrApiAccess::$user) < 0) |
||
| 1506 | throw new RestException(500, 'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!'); |
||
| 1507 | |||
| 1508 | $this->_cleanObjectDatas($account); |
||
| 1509 | |||
| 1510 | return $account; |
||
| 1511 | } else { |
||
| 1512 | throw new RestException(409, 'A SocieteAccount entity already exists for this company and site.'); |
||
| 1513 | } |
||
| 1514 | } |
||
| 1515 | |||
| 1516 | /** |
||
| 1517 | * Create and attach a new (or replace an existing) specific site gateway to a thirdparty |
||
| 1518 | * |
||
| 1519 | * You <strong>MUST</strong> pass all values to keep (otherwise, they will be deleted) !<br> |
||
| 1520 | * If you just need to update specific fields prefer <code>PATCH /thirdparties/{id}/gateways/{site}</code> endpoint.<br><br> |
||
| 1521 | * When a <strong>SocieteAccount</strong> entity does not exist for the <code>id</code> and <code>site</code> |
||
| 1522 | * supplied, a new one will be created. In that case <code>fk_soc</code> and <code>site</code> members form |
||
| 1523 | * request body payload will be ignored and <code>id</code> and <code>site</code> query strings parameters |
||
| 1524 | * will be used instead. |
||
| 1525 | * |
||
| 1526 | * @param int $id ID of thirdparty |
||
| 1527 | * @param string $site Site key |
||
| 1528 | * @param array $request_data Request data |
||
| 1529 | * |
||
| 1530 | * @return SocieteAccount |
||
| 1531 | * |
||
| 1532 | * @throws RestException 401 Unauthorized: User does not have permission to read thirdparties |
||
| 1533 | * @throws RestException 422 Unprocessable Entity: You must pass the site attribute in your request data ! |
||
| 1534 | * @throws RestException 500 Internal Server Error: Error updating SocieteAccount entity |
||
| 1535 | * |
||
| 1536 | * @url PUT {id}/gateways/{site} |
||
| 1537 | */ |
||
| 1538 | public function putSocieteAccount($id, $site, $request_data = null) |
||
| 1539 | { |
||
| 1540 | global $db; |
||
| 1541 | |||
| 1542 | if (!DolibarrApiAccess::$user->rights->societe->creer) { |
||
| 1543 | throw new RestException(401); |
||
| 1544 | } |
||
| 1545 | |||
| 1546 | $sql = "SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '$site' "; |
||
| 1547 | $result = $db->query($sql); |
||
| 1548 | |||
| 1549 | // We do not found an existing SocieteAccount entity for this fk_soc and site ; we then create a new one. |
||
| 1550 | if ($result->num_rows == 0) { |
||
| 1551 | if (!isset($request_data['key_account'])) { |
||
| 1552 | throw new RestException(422, 'Unprocessable Entity: You must pass the key_account attribute in your request data !'); |
||
| 1553 | } |
||
| 1554 | $account = new SocieteAccount($this->db); |
||
| 1555 | if (!isset($request_data['login'])) { |
||
| 1556 | $account->login = ""; |
||
| 1557 | } |
||
| 1558 | |||
| 1559 | foreach ($request_data as $field => $value) { |
||
| 1560 | $account->$field = $value; |
||
| 1561 | } |
||
| 1562 | |||
| 1563 | $account->fk_soc = $id; |
||
| 1564 | $account->site = $site; |
||
| 1565 | |||
| 1566 | if ($account->create(DolibarrApiAccess::$user) < 0) { |
||
| 1567 | throw new RestException(500, 'Error creating SocieteAccount entity.'); |
||
| 1568 | } |
||
| 1569 | // We found an existing SocieteAccount entity, we are replacing it |
||
| 1570 | } else { |
||
| 1571 | if (isset($request_data['site']) && $request_data['site'] !== $site) { |
||
| 1572 | $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".$id." AND site = '".$request_data['site']."' "; |
||
| 1573 | $result = $db->query($sql); |
||
| 1574 | |||
| 1575 | if ($result->num_rows !== 0) { |
||
| 1576 | throw new RestException(409, "You are trying to update this thirdparty SocieteAccount (gateway record) from $site to ".$request_data['site']." but another SocieteAccount entity already exists with this site key."); |
||
| 1577 | } |
||
| 1578 | } |
||
| 1579 | |||
| 1580 | $obj = $db->fetch_object($result); |
||
| 1581 | |||
| 1582 | $account = new SocieteAccount($this->db); |
||
| 1583 | $account->id = $obj->rowid; |
||
| 1584 | $account->fk_soc = $id; |
||
| 1585 | $account->site = $site; |
||
| 1586 | if (!isset($request_data['login'])) { |
||
| 1587 | $account->login = ""; |
||
| 1588 | } |
||
| 1589 | $account->fk_user_creat = $obj->fk_user_creat; |
||
| 1590 | $account->date_creation = $obj->date_creation; |
||
| 1591 | |||
| 1592 | foreach ($request_data as $field => $value) { |
||
| 1593 | $account->$field = $value; |
||
| 1594 | } |
||
| 1595 | |||
| 1596 | if ($account->update(DolibarrApiAccess::$user) < 0) |
||
| 1597 | throw new RestException(500, 'Error updating SocieteAccount entity.'); |
||
| 1598 | } |
||
| 1599 | |||
| 1600 | $this->_cleanObjectDatas($account); |
||
| 1601 | |||
| 1602 | return $account; |
||
| 1603 | } |
||
| 1604 | |||
| 1605 | /** |
||
| 1606 | * Update specified values of a specific site gateway attached to a thirdparty |
||
| 1607 | * |
||
| 1608 | * @param int $id Id of thirdparty |
||
| 1609 | * @param string $site Site key |
||
| 1610 | * @param array $request_data Request data |
||
| 1611 | * |
||
| 1612 | * @return SocieteAccount |
||
| 1613 | * |
||
| 1614 | * @throws RestException 401 Unauthorized: User does not have permission to read thirdparties |
||
| 1615 | * @throws RestException 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty |
||
| 1616 | * @throws RestException 409 Conflict: Another SocieteAccount entity already exists for this thirdparty with this site key. |
||
| 1617 | * @throws RestException 500 Internal Server Error: Error updating SocieteAccount entity |
||
| 1618 | * |
||
| 1619 | * @url PATCH {id}/gateways/{site} |
||
| 1620 | */ |
||
| 1621 | public function patchSocieteAccount($id, $site, $request_data = null) |
||
| 1622 | { |
||
| 1623 | global $db; |
||
| 1624 | |||
| 1625 | if (!DolibarrApiAccess::$user->rights->societe->creer) { |
||
| 1626 | throw new RestException(401); |
||
| 1627 | } |
||
| 1628 | |||
| 1629 | $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '$site' "; |
||
| 1630 | $result = $db->query($sql); |
||
| 1631 | |||
| 1632 | if ($result->num_rows == 0) { |
||
| 1633 | throw new RestException(404, "This thirdparty does not have $site gateway attached or does not exist."); |
||
| 1634 | } else { |
||
| 1635 | // If the user tries to edit the site member, we check first if |
||
| 1636 | if (isset($request_data['site']) && $request_data['site'] !== $site) { |
||
| 1637 | $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = ".$id." AND site = '".$request_data['site']."' "; |
||
| 1638 | $result = $db->query($sql); |
||
| 1639 | |||
| 1640 | if ($result->num_rows !== 0) |
||
| 1641 | throw new RestException(409, "You are trying to update this thirdparty SocieteAccount (gateway record) site member from $site to ".$request_data['site']." but another SocieteAccount entity already exists for this thirdparty with this site key."); |
||
| 1642 | } |
||
| 1643 | |||
| 1644 | $obj = $db->fetch_object($result); |
||
| 1645 | $account = new SocieteAccount($this->db); |
||
| 1646 | $account->fetch($obj->rowid); |
||
| 1647 | |||
| 1648 | foreach ($request_data as $field => $value) { |
||
| 1649 | $account->$field = $value; |
||
| 1650 | } |
||
| 1651 | |||
| 1652 | if ($account->update(DolibarrApiAccess::$user) < 0) |
||
| 1653 | throw new RestException(500, 'Error updating SocieteAccount account'); |
||
| 1654 | |||
| 1655 | $this->_cleanObjectDatas($account); |
||
| 1656 | |||
| 1657 | return $account; |
||
| 1658 | } |
||
| 1659 | } |
||
| 1660 | |||
| 1661 | /** |
||
| 1662 | * Delete a specific site gateway attached to a thirdparty (by gateway id) |
||
| 1663 | * |
||
| 1664 | * @param int $id ID of thirdparty |
||
| 1665 | * @param int $site Site key |
||
| 1666 | * |
||
| 1667 | * @return void |
||
| 1668 | * @throws RestException 401 Unauthorized: User does not have permission to delete thirdparties gateways |
||
| 1669 | * @throws RestException 404 Not Found: Specified thirdparty ID does not belongs to an existing thirdparty |
||
| 1670 | * @throws RestException 500 Internal Server Error: Error deleting SocieteAccount entity |
||
| 1671 | * |
||
| 1672 | * @url DELETE {id}/gateways/{site} |
||
| 1673 | */ |
||
| 1674 | public function deleteSocieteAccount($id, $site) |
||
| 1675 | { |
||
| 1676 | global /** @var Database $db */ |
||
| 1677 | $db; |
||
| 1678 | |||
| 1679 | if (!DolibarrApiAccess::$user->rights->societe->creer) { |
||
| 1680 | throw new RestException(401); |
||
| 1681 | } |
||
| 1682 | |||
| 1683 | $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id AND site = '$site' "; |
||
| 1684 | $result = $db->query($sql); |
||
| 1685 | |||
| 1686 | if ($result->num_rows == 0) { |
||
| 1687 | throw new RestException(404); |
||
| 1688 | } else { |
||
| 1689 | $obj = $db->fetch_object($result); |
||
| 1690 | $account = new SocieteAccount($this->db); |
||
| 1691 | $account->fetch($obj->rowid); |
||
| 1692 | |||
| 1693 | if ($account->delete(DolibarrApiAccess::$user) < 0) { |
||
| 1694 | throw new RestException(500, "Error while deleting $site gateway attached to this third party"); |
||
| 1695 | } |
||
| 1696 | } |
||
| 1697 | } |
||
| 1698 | |||
| 1699 | /** |
||
| 1700 | * Delete all gateways attached to a thirdparty |
||
| 1701 | * |
||
| 1702 | * @param int $id ID of thirdparty |
||
| 1703 | * |
||
| 1704 | * @return void |
||
| 1705 | * @throws RestException(401) Unauthorized: User does not have permission to delete thirdparties gateways |
||
| 1706 | * @throws RestException(404) Not Found: Specified thirdparty ID does not belongs to an existing thirdparty |
||
| 1707 | * @throws RestException(500) Internal Server Error: Error deleting SocieteAccount entity |
||
| 1708 | * |
||
| 1709 | * @url DELETE {id}/gateways |
||
| 1710 | */ |
||
| 1711 | public function deleteSocieteAccounts($id) |
||
| 1712 | { |
||
| 1713 | global /** @var Database $db */ |
||
| 1714 | $db; |
||
| 1715 | |||
| 1716 | if (!DolibarrApiAccess::$user->rights->societe->creer) { |
||
| 1717 | throw new RestException(401); |
||
| 1718 | } |
||
| 1719 | |||
| 1720 | /** |
||
| 1721 | * We select all the records that match the socid |
||
| 1722 | */ |
||
| 1723 | |||
| 1724 | $sql = "SELECT rowid, fk_soc, key_account, site, date_creation, tms"; |
||
| 1725 | $sql .= " FROM ".MAIN_DB_PREFIX."societe_account WHERE fk_soc = $id "; |
||
| 1726 | |||
| 1727 | $result = $db->query($sql); |
||
| 1728 | |||
| 1729 | if ($result->num_rows == 0) { |
||
| 1730 | throw new RestException(404, 'This third party does not have any gateway attached or does not exist.'); |
||
| 1731 | } else { |
||
| 1732 | $i = 0; |
||
| 1733 | |||
| 1734 | $num = $db->num_rows($result); |
||
| 1735 | while ($i < $num) |
||
| 1736 | { |
||
| 1737 | $obj = $db->fetch_object($result); |
||
| 1738 | $account = new SocieteAccount($db); |
||
| 1739 | $account->fetch($obj->rowid); |
||
| 1740 | |||
| 1741 | if ($account->delete(DolibarrApiAccess::$user) < 0) { |
||
| 1742 | throw new RestException(500, 'Error while deleting gateways attached to this third party'); |
||
| 1743 | } |
||
| 1744 | $i++; |
||
| 1745 | } |
||
| 1746 | } |
||
| 1747 | } |
||
| 1748 | |||
| 1749 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
||
| 1750 | /** |
||
| 1751 | * Clean sensible object datas |
||
| 1752 | * |
||
| 1753 | * @param object $object Object to clean |
||
| 1754 | * @return array Array of cleaned object properties |
||
| 1755 | */ |
||
| 1756 | protected function _cleanObjectDatas($object) |
||
| 1757 | { |
||
| 1758 | // phpcs:enable |
||
| 1759 | $object = parent::_cleanObjectDatas($object); |
||
| 1760 | |||
| 1761 | unset($object->nom); // ->name already defined and nom deprecated |
||
| 1762 | unset($object->name_bis); // ->name_alias already defined |
||
| 1763 | unset($object->note); // ->note_private and note_public already defined |
||
| 1764 | unset($object->departement); |
||
| 1765 | unset($object->departement_code); |
||
| 1766 | unset($object->pays); |
||
| 1767 | unset($object->particulier); |
||
| 1768 | unset($object->prefix_comm); |
||
| 1769 | |||
| 1770 | unset($object->commercial_id); // This property is used in create/update only. It does not exists in read mode because there is several sales representatives. |
||
| 1771 | |||
| 1772 | unset($object->total_ht); |
||
| 1773 | unset($object->total_tva); |
||
| 1774 | unset($object->total_localtax1); |
||
| 1775 | unset($object->total_localtax2); |
||
| 1776 | unset($object->total_ttc); |
||
| 1777 | |||
| 1778 | unset($object->lines); |
||
| 1779 | unset($object->thirdparty); |
||
| 1780 | |||
| 1781 | unset($object->fk_delivery_address); // deprecated feature |
||
| 1782 | |||
| 1783 | return $object; |
||
| 1784 | } |
||
| 1785 | |||
| 1786 | /** |
||
| 1787 | * Validate fields before create or update object |
||
| 1788 | * |
||
| 1789 | * @param array $data Datas to validate |
||
| 1790 | * @return array |
||
| 1791 | * |
||
| 1792 | * @throws RestException |
||
| 1793 | */ |
||
| 1794 | private function _validate($data) |
||
| 1803 | } |
||
| 1804 | |||
| 1805 | /** |
||
| 1806 | * Fetch properties of a thirdparty object. |
||
| 1807 | * |
||
| 1808 | * Return an array with thirdparty informations |
||
| 1809 | * |
||
| 1810 | * @param int $rowid Id of third party to load |
||
| 1811 | * @param string $ref Reference of third party, name (Warning, this can return several records) |
||
| 1812 | * @param string $ref_ext External reference of third party (Warning, this information is a free field not provided by Dolibarr) |
||
| 1813 | * @param string $ref_int Internal reference of third party (not used by dolibarr) |
||
| 1814 | * @param string $idprof1 Prof id 1 of third party (Warning, this can return several records) |
||
| 1815 | * @param string $idprof2 Prof id 2 of third party (Warning, this can return several records) |
||
| 1816 | * @param string $idprof3 Prof id 3 of third party (Warning, this can return several records) |
||
| 1817 | * @param string $idprof4 Prof id 4 of third party (Warning, this can return several records) |
||
| 1818 | * @param string $idprof5 Prof id 5 of third party (Warning, this can return several records) |
||
| 1819 | * @param string $idprof6 Prof id 6 of third party (Warning, this can return several records) |
||
| 1820 | * @param string $email Email of third party (Warning, this can return several records) |
||
| 1821 | * @param string $ref_alias Name_alias of third party (Warning, this can return several records) |
||
| 1822 | * @return array|mixed data without useless information |
||
| 1823 | * |
||
| 1824 | * @throws RestException |
||
| 1825 | */ |
||
| 1826 | private function _fetch($rowid, $ref = '', $ref_ext = '', $ref_int = '', $idprof1 = '', $idprof2 = '', $idprof3 = '', $idprof4 = '', $idprof5 = '', $idprof6 = '', $email = '', $ref_alias = '') |
||
| 1856 | } |
||
| 1857 | } |
||
| 1858 |