| Total Complexity | 140 |
| Total Lines | 827 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Proposals 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 Proposals, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Proposals extends DolibarrApi |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var array $FIELDS Mandatory fields, checked when create and update object |
||
| 35 | */ |
||
| 36 | static $FIELDS = array( |
||
| 37 | 'socid' |
||
| 38 | ); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Propal $propal {@type Propal} |
||
| 42 | */ |
||
| 43 | public $propal; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor |
||
| 47 | */ |
||
| 48 | public function __construct() |
||
| 49 | { |
||
| 50 | global $db, $conf; |
||
| 51 | $this->db = $db; |
||
| 52 | $this->propal = new Propal($this->db); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get properties of a commercial proposal object |
||
| 57 | * |
||
| 58 | * Return an array with commercial proposal informations |
||
| 59 | * |
||
| 60 | * @param int $id ID of commercial proposal |
||
| 61 | * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id |
||
| 62 | * @return array|mixed data without useless information |
||
| 63 | * |
||
| 64 | * @throws RestException |
||
| 65 | */ |
||
| 66 | public function get($id, $contact_list = 1) |
||
| 67 | { |
||
| 68 | return $this->_fetch($id, '', '', $contact_list); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get properties of an proposal object by ref |
||
| 73 | * |
||
| 74 | * Return an array with proposal informations |
||
| 75 | * |
||
| 76 | * @param string $ref Ref of object |
||
| 77 | * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id |
||
| 78 | * @return array|mixed data without useless information |
||
| 79 | * |
||
| 80 | * @url GET ref/{ref} |
||
| 81 | * |
||
| 82 | * @throws RestException |
||
| 83 | */ |
||
| 84 | public function getByRef($ref, $contact_list = 1) |
||
| 85 | { |
||
| 86 | return $this->_fetch('', $ref, '', $contact_list); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get properties of an proposal object by ref_ext |
||
| 91 | * |
||
| 92 | * Return an array with proposal informations |
||
| 93 | * |
||
| 94 | * @param string $ref_ext External reference of object |
||
| 95 | * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id |
||
| 96 | * @return array|mixed data without useless information |
||
| 97 | * |
||
| 98 | * @url GET ref_ext/{ref_ext} |
||
| 99 | * |
||
| 100 | * @throws RestException |
||
| 101 | */ |
||
| 102 | public function getByRefExt($ref_ext, $contact_list = 1) |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get properties of an proposal object |
||
| 109 | * |
||
| 110 | * Return an array with proposal informations |
||
| 111 | * |
||
| 112 | * @param int $id ID of order |
||
| 113 | * @param string $ref Ref of object |
||
| 114 | * @param string $ref_ext External reference of object |
||
| 115 | * @param int $contact_list 0: Returned array of contacts/addresses contains all properties, 1: Return array contains just id |
||
| 116 | * @return array|mixed data without useless information |
||
| 117 | * |
||
| 118 | * @throws RestException |
||
| 119 | */ |
||
| 120 | private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1) |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * List commercial proposals |
||
| 143 | * |
||
| 144 | * Get a list of commercial proposals |
||
| 145 | * |
||
| 146 | * @param string $sortfield Sort field |
||
| 147 | * @param string $sortorder Sort order |
||
| 148 | * @param int $limit Limit for list |
||
| 149 | * @param int $page Page number |
||
| 150 | * @param string $thirdparty_ids Thirdparty ids to filter commercial proposals (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i} |
||
| 151 | * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.datec:<:'20160101')" |
||
| 152 | * @return array Array of order objects |
||
| 153 | */ |
||
| 154 | public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '') |
||
| 155 | { |
||
| 156 | global $db, $conf; |
||
| 157 | |||
| 158 | $obj_ret = array(); |
||
| 159 | |||
| 160 | // case of external user, $thirdparty_ids param is ignored and replaced by user's socid |
||
| 161 | $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids; |
||
| 162 | |||
| 163 | // If the internal user must only see his customers, force searching by him |
||
| 164 | $search_sale = 0; |
||
| 165 | if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id; |
||
| 166 | |||
| 167 | $sql = "SELECT t.rowid"; |
||
| 168 | 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) |
||
| 169 | $sql .= " FROM ".MAIN_DB_PREFIX."propal as t"; |
||
| 170 | |||
| 171 | 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 |
||
| 172 | |||
| 173 | $sql .= ' WHERE t.entity IN ('.getEntity('propal').')'; |
||
| 174 | if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= " AND t.fk_soc = sc.fk_soc"; |
||
| 175 | if ($socids) $sql .= " AND t.fk_soc IN (".$socids.")"; |
||
| 176 | if ($search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale |
||
| 177 | // Insert sale filter |
||
| 178 | if ($search_sale > 0) |
||
| 179 | { |
||
| 180 | $sql .= " AND sc.fk_user = ".$search_sale; |
||
| 181 | } |
||
| 182 | // Add sql filters |
||
| 183 | if ($sqlfilters) |
||
| 184 | { |
||
| 185 | if (!DolibarrApi::_checkFilters($sqlfilters)) |
||
| 186 | { |
||
| 187 | throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); |
||
| 188 | } |
||
| 189 | $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; |
||
| 190 | $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; |
||
| 191 | } |
||
| 192 | |||
| 193 | $sql .= $db->order($sortfield, $sortorder); |
||
| 194 | if ($limit) { |
||
| 195 | if ($page < 0) |
||
| 196 | { |
||
| 197 | $page = 0; |
||
| 198 | } |
||
| 199 | $offset = $limit * $page; |
||
| 200 | |||
| 201 | $sql .= $db->plimit($limit + 1, $offset); |
||
| 202 | } |
||
| 203 | |||
| 204 | dol_syslog("API Rest request"); |
||
| 205 | $result = $db->query($sql); |
||
| 206 | |||
| 207 | if ($result) |
||
| 208 | { |
||
| 209 | $num = $db->num_rows($result); |
||
| 210 | $min = min($num, ($limit <= 0 ? $num : $limit)); |
||
| 211 | $i = 0; |
||
| 212 | while ($i < $min) |
||
| 213 | { |
||
| 214 | $obj = $db->fetch_object($result); |
||
| 215 | $proposal_static = new Propal($db); |
||
| 216 | if ($proposal_static->fetch($obj->rowid)) { |
||
| 217 | // Add external contacts ids |
||
| 218 | $proposal_static->contacts_ids = $proposal_static->liste_contact(-1, 'external', 1); |
||
| 219 | $obj_ret[] = $this->_cleanObjectDatas($proposal_static); |
||
| 220 | } |
||
| 221 | $i++; |
||
| 222 | } |
||
| 223 | } else { |
||
| 224 | throw new RestException(503, 'Error when retrieve propal list : '.$db->lasterror()); |
||
| 225 | } |
||
| 226 | if (!count($obj_ret)) { |
||
| 227 | throw new RestException(404, 'No proposal found'); |
||
| 228 | } |
||
| 229 | return $obj_ret; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Create commercial proposal object |
||
| 234 | * |
||
| 235 | * @param array $request_data Request data |
||
| 236 | * @return int ID of proposal |
||
| 237 | */ |
||
| 238 | public function post($request_data = null) |
||
| 239 | { |
||
| 240 | if (!DolibarrApiAccess::$user->rights->propal->creer) { |
||
| 241 | throw new RestException(401, "Insuffisant rights"); |
||
| 242 | } |
||
| 243 | // Check mandatory fields |
||
| 244 | $result = $this->_validate($request_data); |
||
| 245 | |||
| 246 | foreach ($request_data as $field => $value) { |
||
| 247 | $this->propal->$field = $value; |
||
| 248 | } |
||
| 249 | /*if (isset($request_data["lines"])) { |
||
| 250 | $lines = array(); |
||
| 251 | foreach ($request_data["lines"] as $line) { |
||
| 252 | array_push($lines, (object) $line); |
||
| 253 | } |
||
| 254 | $this->propal->lines = $lines; |
||
| 255 | }*/ |
||
| 256 | if ($this->propal->create(DolibarrApiAccess::$user) < 0) { |
||
| 257 | throw new RestException(500, "Error creating order", array_merge(array($this->propal->error), $this->propal->errors)); |
||
| 258 | } |
||
| 259 | |||
| 260 | return $this->propal->id; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get lines of a commercial proposal |
||
| 265 | * |
||
| 266 | * @param int $id Id of commercial proposal |
||
| 267 | * |
||
| 268 | * @url GET {id}/lines |
||
| 269 | * |
||
| 270 | * @return int |
||
| 271 | */ |
||
| 272 | public function getLines($id) |
||
| 273 | { |
||
| 274 | if (!DolibarrApiAccess::$user->rights->propal->lire) { |
||
| 275 | throw new RestException(401); |
||
| 276 | } |
||
| 277 | |||
| 278 | $result = $this->propal->fetch($id); |
||
| 279 | if (!$result) { |
||
| 280 | throw new RestException(404, 'Commercial Proposal not found'); |
||
| 281 | } |
||
| 282 | |||
| 283 | if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) { |
||
| 284 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 285 | } |
||
| 286 | $this->propal->getLinesArray(); |
||
| 287 | $result = array(); |
||
| 288 | foreach ($this->propal->lines as $line) { |
||
| 289 | array_push($result, $this->_cleanObjectDatas($line)); |
||
| 290 | } |
||
| 291 | return $result; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Add a line to given commercial proposal |
||
| 296 | * |
||
| 297 | * @param int $id Id of commercial proposal to update |
||
| 298 | * @param array $request_data Commercial proposal line data |
||
| 299 | * |
||
| 300 | * @url POST {id}/lines |
||
| 301 | * |
||
| 302 | * @return int |
||
| 303 | */ |
||
| 304 | public function postLine($id, $request_data = null) |
||
| 305 | { |
||
| 306 | if (!DolibarrApiAccess::$user->rights->propal->creer) { |
||
| 307 | throw new RestException(401); |
||
| 308 | } |
||
| 309 | |||
| 310 | $result = $this->propal->fetch($id); |
||
| 311 | if (!$result) { |
||
| 312 | throw new RestException(404, 'Commercial Proposal not found'); |
||
| 313 | } |
||
| 314 | |||
| 315 | if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) { |
||
| 316 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 317 | } |
||
| 318 | |||
| 319 | $request_data = (object) $request_data; |
||
| 320 | |||
| 321 | $updateRes = $this->propal->addline( |
||
| 322 | $request_data->desc, |
||
| 323 | $request_data->subprice, |
||
| 324 | $request_data->qty, |
||
| 325 | $request_data->tva_tx, |
||
| 326 | $request_data->localtax1_tx, |
||
| 327 | $request_data->localtax2_tx, |
||
| 328 | $request_data->fk_product, |
||
| 329 | $request_data->remise_percent, |
||
| 330 | 'HT', |
||
| 331 | 0, |
||
| 332 | $request_data->info_bits, |
||
| 333 | $request_data->product_type, |
||
| 334 | $request_data->rang, |
||
| 335 | $request_data->special_code, |
||
| 336 | $request_data->fk_parent_line, |
||
| 337 | $request_data->fk_fournprice, |
||
| 338 | $request_data->pa_ht, |
||
| 339 | $request_data->label, |
||
| 340 | $request_data->date_start, |
||
| 341 | $request_data->date_end, |
||
| 342 | $request_data->array_options, |
||
| 343 | $request_data->fk_unit, |
||
| 344 | $request_data->origin, |
||
| 345 | $request_data->origin_id, |
||
| 346 | $request_data->multicurrency_subprice, |
||
| 347 | $request_data->fk_remise_except |
||
| 348 | ); |
||
| 349 | |||
| 350 | if ($updateRes > 0) { |
||
| 351 | return $updateRes; |
||
| 352 | } else { |
||
| 353 | throw new RestException(400, $this->propal->error); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Update a line of given commercial proposal |
||
| 359 | * |
||
| 360 | * @param int $id Id of commercial proposal to update |
||
| 361 | * @param int $lineid Id of line to update |
||
| 362 | * @param array $request_data Commercial proposal line data |
||
| 363 | * |
||
| 364 | * @url PUT {id}/lines/{lineid} |
||
| 365 | * |
||
| 366 | * @return object |
||
| 367 | */ |
||
| 368 | public function putLine($id, $lineid, $request_data = null) |
||
| 369 | { |
||
| 370 | if (!DolibarrApiAccess::$user->rights->propal->creer) { |
||
| 371 | throw new RestException(401); |
||
| 372 | } |
||
| 373 | |||
| 374 | $result = $this->propal->fetch($id); |
||
| 375 | if ($result <= 0) { |
||
| 376 | throw new RestException(404, 'Proposal not found'); |
||
| 377 | } |
||
| 378 | |||
| 379 | if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) { |
||
| 380 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 381 | } |
||
| 382 | |||
| 383 | $request_data = (object) $request_data; |
||
| 384 | |||
| 385 | $propalline = new PropaleLigne($this->db); |
||
| 386 | $result = $propalline->fetch($lineid); |
||
| 387 | if ($result <= 0) { |
||
| 388 | throw new RestException(404, 'Proposal line not found'); |
||
| 389 | } |
||
| 390 | |||
| 391 | $updateRes = $this->propal->updateline( |
||
| 392 | $lineid, |
||
| 393 | isset($request_data->subprice) ? $request_data->subprice : $propalline->subprice, |
||
| 394 | isset($request_data->qty) ? $request_data->qty : $propalline->qty, |
||
| 395 | isset($request_data->remise_percent) ? $request_data->remise_percent : $propalline->remise_percent, |
||
| 396 | isset($request_data->tva_tx) ? $request_data->tva_tx : $propalline->tva_tx, |
||
| 397 | isset($request_data->localtax1_tx) ? $request_data->localtax1_tx : $propalline->localtax1_tx, |
||
| 398 | isset($request_data->localtax2_tx) ? $request_data->localtax2_tx : $propalline->localtax2_tx, |
||
| 399 | isset($request_data->desc) ? $request_data->desc : $propalline->desc, |
||
| 400 | 'HT', |
||
| 401 | isset($request_data->info_bits) ? $request_data->info_bits : $propalline->info_bits, |
||
| 402 | isset($request_data->special_code) ? $request_data->special_code : $propalline->special_code, |
||
| 403 | isset($request_data->fk_parent_line) ? $request_data->fk_parent_line : $propalline->fk_parent_line, |
||
| 404 | 0, |
||
| 405 | isset($request_data->fk_fournprice) ? $request_data->fk_fournprice : $propalline->fk_fournprice, |
||
| 406 | isset($request_data->pa_ht) ? $request_data->pa_ht : $propalline->pa_ht, |
||
| 407 | isset($request_data->label) ? $request_data->label : $propalline->label, |
||
| 408 | isset($request_data->product_type) ? $request_data->product_type : $propalline->product_type, |
||
| 409 | isset($request_data->date_start) ? $request_data->date_start : $propalline->date_start, |
||
| 410 | isset($request_data->date_end) ? $request_data->date_end : $propalline->date_end, |
||
| 411 | isset($request_data->array_options) ? $request_data->array_options : $propalline->array_options, |
||
| 412 | isset($request_data->fk_unit) ? $request_data->fk_unit : $propalline->fk_unit, |
||
| 413 | isset($request_data->multicurrency_subprice) ? $request_data->multicurrency_subprice : $propalline->subprice |
||
| 414 | ); |
||
| 415 | |||
| 416 | if ($updateRes > 0) { |
||
| 417 | $result = $this->get($id); |
||
| 418 | unset($result->line); |
||
| 419 | return $this->_cleanObjectDatas($result); |
||
| 420 | } |
||
| 421 | return false; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Delete a line of given commercial proposal |
||
| 426 | * |
||
| 427 | * |
||
| 428 | * @param int $id Id of commercial proposal to update |
||
| 429 | * @param int $lineid Id of line to delete |
||
| 430 | * |
||
| 431 | * @url DELETE {id}/lines/{lineid} |
||
| 432 | * |
||
| 433 | * @return int |
||
| 434 | * |
||
| 435 | * @throws RestException 401 |
||
| 436 | * @throws RestException 404 |
||
| 437 | */ |
||
| 438 | public function deleteLine($id, $lineid) |
||
| 439 | { |
||
| 440 | if (!DolibarrApiAccess::$user->rights->propal->creer) { |
||
| 441 | throw new RestException(401); |
||
| 442 | } |
||
| 443 | |||
| 444 | $result = $this->propal->fetch($id); |
||
| 445 | if (!$result) { |
||
| 446 | throw new RestException(404, 'Proposal not found'); |
||
| 447 | } |
||
| 448 | |||
| 449 | if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) { |
||
| 450 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 451 | } |
||
| 452 | |||
| 453 | // TODO Check the lineid $lineid is a line of ojbect |
||
| 454 | |||
| 455 | $updateRes = $this->propal->deleteline($lineid); |
||
| 456 | if ($updateRes > 0) { |
||
| 457 | return $this->get($id); |
||
| 458 | } else { |
||
| 459 | throw new RestException(405, $this->propal->error); |
||
| 460 | } |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Add a contact type of given commercial proposal |
||
| 465 | * |
||
| 466 | * @param int $id Id of commercial proposal to update |
||
| 467 | * @param int $contactid Id of contact to add |
||
| 468 | * @param string $type Type of the contact (BILLING, SHIPPING, CUSTOMER) |
||
| 469 | * |
||
| 470 | * @url POST {id}/contact/{contactid}/{type} |
||
| 471 | * |
||
| 472 | * @return int |
||
| 473 | * |
||
| 474 | * @throws RestException 401 |
||
| 475 | * @throws RestException 404 |
||
| 476 | */ |
||
| 477 | public function postContact($id, $contactid, $type) |
||
| 478 | { |
||
| 479 | if (!DolibarrApiAccess::$user->rights->propal->creer) { |
||
| 480 | throw new RestException(401); |
||
| 481 | } |
||
| 482 | |||
| 483 | $result = $this->propal->fetch($id); |
||
| 484 | |||
| 485 | if (!$result) { |
||
| 486 | throw new RestException(404, 'Proposal not found'); |
||
| 487 | } |
||
| 488 | |||
| 489 | if (!in_array($type, array('BILLING', 'SHIPPING', 'CUSTOMER'), true)) { |
||
| 490 | throw new RestException(500, 'Availables types: BILLING, SHIPPING OR CUSTOMER'); |
||
| 491 | } |
||
| 492 | |||
| 493 | if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) { |
||
| 494 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 495 | } |
||
| 496 | |||
| 497 | $result = $this->propal->add_contact($contactid, $type, 'external'); |
||
| 498 | |||
| 499 | if (!$result) { |
||
| 500 | throw new RestException(500, 'Error when added the contact'); |
||
| 501 | } |
||
| 502 | |||
| 503 | return $this->propal; |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Delete a contact type of given commercial proposal |
||
| 508 | * |
||
| 509 | * @param int $id Id of commercial proposal to update |
||
| 510 | * @param int $rowid Row key of the contact in the array contact_ids. |
||
| 511 | * |
||
| 512 | * @url DELETE {id}/contact/{rowid} |
||
| 513 | * |
||
| 514 | * @return int |
||
| 515 | * |
||
| 516 | * @throws RestException 401 |
||
| 517 | * @throws RestException 404 |
||
| 518 | * @throws RestException 500 |
||
| 519 | */ |
||
| 520 | public function deleteContact($id, $rowid) |
||
| 521 | { |
||
| 522 | if (!DolibarrApiAccess::$user->rights->propal->creer) { |
||
| 523 | throw new RestException(401); |
||
| 524 | } |
||
| 525 | |||
| 526 | $result = $this->propal->fetch($id); |
||
| 527 | |||
| 528 | if (!$result) { |
||
| 529 | throw new RestException(404, 'Proposal not found'); |
||
| 530 | } |
||
| 531 | |||
| 532 | if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) { |
||
| 533 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 534 | } |
||
| 535 | |||
| 536 | $result = $this->propal->delete_contact($rowid); |
||
| 537 | |||
| 538 | if (!$result) { |
||
| 539 | throw new RestException(500, 'Error when deleted the contact'); |
||
| 540 | } |
||
| 541 | |||
| 542 | return $this->propal; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Update commercial proposal general fields (won't touch lines of commercial proposal) |
||
| 547 | * |
||
| 548 | * @param int $id Id of commercial proposal to update |
||
| 549 | * @param array $request_data Datas |
||
| 550 | * |
||
| 551 | * @return int |
||
| 552 | */ |
||
| 553 | public function put($id, $request_data = null) |
||
| 554 | { |
||
| 555 | if (!DolibarrApiAccess::$user->rights->propal->creer) { |
||
| 556 | throw new RestException(401); |
||
| 557 | } |
||
| 558 | |||
| 559 | $result = $this->propal->fetch($id); |
||
| 560 | if (!$result) { |
||
| 561 | throw new RestException(404, 'Proposal not found'); |
||
| 562 | } |
||
| 563 | |||
| 564 | if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) { |
||
| 565 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 566 | } |
||
| 567 | foreach ($request_data as $field => $value) { |
||
| 568 | if ($field == 'id') continue; |
||
| 569 | $this->propal->$field = $value; |
||
| 570 | } |
||
| 571 | |||
| 572 | // update end of validity date |
||
| 573 | if (empty($this->propal->fin_validite) && !empty($this->propal->duree_validite) && !empty($this->propal->date_creation)) |
||
| 574 | { |
||
| 575 | $this->propal->fin_validite = $this->propal->date_creation + ($this->propal->duree_validite * 24 * 3600); |
||
| 576 | } |
||
| 577 | if (!empty($this->propal->fin_validite)) |
||
| 578 | { |
||
| 579 | if ($this->propal->set_echeance(DolibarrApiAccess::$user, $this->propal->fin_validite) < 0) |
||
| 580 | { |
||
| 581 | throw new RestException(500, $this->propal->error); |
||
| 582 | } |
||
| 583 | } |
||
| 584 | |||
| 585 | if ($this->propal->update(DolibarrApiAccess::$user) > 0) |
||
| 586 | { |
||
| 587 | return $this->get($id); |
||
| 588 | } else { |
||
| 589 | throw new RestException(500, $this->propal->error); |
||
| 590 | } |
||
| 591 | } |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Delete commercial proposal |
||
| 595 | * |
||
| 596 | * @param int $id Commercial proposal ID |
||
| 597 | * |
||
| 598 | * @return array |
||
| 599 | */ |
||
| 600 | public function delete($id) |
||
| 601 | { |
||
| 602 | if (!DolibarrApiAccess::$user->rights->propal->supprimer) { |
||
| 603 | throw new RestException(401); |
||
| 604 | } |
||
| 605 | $result = $this->propal->fetch($id); |
||
| 606 | if (!$result) { |
||
| 607 | throw new RestException(404, 'Commercial Proposal not found'); |
||
| 608 | } |
||
| 609 | |||
| 610 | if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) { |
||
| 611 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 612 | } |
||
| 613 | |||
| 614 | if (!$this->propal->delete(DolibarrApiAccess::$user)) { |
||
| 615 | throw new RestException(500, 'Error when delete Commercial Proposal : '.$this->propal->error); |
||
| 616 | } |
||
| 617 | |||
| 618 | return array( |
||
| 619 | 'success' => array( |
||
| 620 | 'code' => 200, |
||
| 621 | 'message' => 'Commercial Proposal deleted' |
||
| 622 | ) |
||
| 623 | ); |
||
| 624 | } |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Set a proposal to draft |
||
| 628 | * |
||
| 629 | * @param int $id Order ID |
||
| 630 | * |
||
| 631 | * @url POST {id}/settodraft |
||
| 632 | * |
||
| 633 | * @return array |
||
| 634 | */ |
||
| 635 | public function settodraft($id) |
||
| 636 | { |
||
| 637 | if (!DolibarrApiAccess::$user->rights->propal->creer) { |
||
| 638 | throw new RestException(401); |
||
| 639 | } |
||
| 640 | $result = $this->propal->fetch($id); |
||
| 641 | if (!$result) { |
||
| 642 | throw new RestException(404, 'Proposal not found'); |
||
| 643 | } |
||
| 644 | |||
| 645 | if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) { |
||
| 646 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 647 | } |
||
| 648 | |||
| 649 | $result = $this->propal->setDraft(DolibarrApiAccess::$user); |
||
| 650 | if ($result == 0) { |
||
| 651 | throw new RestException(304, 'Nothing done. May be object is already draft'); |
||
| 652 | } |
||
| 653 | if ($result < 0) { |
||
| 654 | throw new RestException(500, 'Error : '.$this->propal->error); |
||
| 655 | } |
||
| 656 | |||
| 657 | $result = $this->propal->fetch($id); |
||
| 658 | if (!$result) { |
||
| 659 | throw new RestException(404, 'Proposal not found'); |
||
| 660 | } |
||
| 661 | |||
| 662 | if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) { |
||
| 663 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 664 | } |
||
| 665 | |||
| 666 | $this->propal->fetchObjectLinked(); |
||
| 667 | |||
| 668 | return $this->_cleanObjectDatas($this->propal); |
||
| 669 | } |
||
| 670 | |||
| 671 | |||
| 672 | /** |
||
| 673 | * Validate a commercial proposal |
||
| 674 | * |
||
| 675 | * If you get a bad value for param notrigger check that ou provide this in body |
||
| 676 | * { |
||
| 677 | * "notrigger": 0 |
||
| 678 | * } |
||
| 679 | * |
||
| 680 | * @param int $id Commercial proposal ID |
||
| 681 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 682 | * |
||
| 683 | * @url POST {id}/validate |
||
| 684 | * |
||
| 685 | * @throws RestException 304 |
||
| 686 | * @throws RestException 401 |
||
| 687 | * @throws RestException 404 |
||
| 688 | * @throws RestException 500 |
||
| 689 | * |
||
| 690 | * @return array |
||
| 691 | */ |
||
| 692 | public function validate($id, $notrigger = 0) |
||
| 726 | } |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Close (Accept or refuse) a quote / commercial proposal |
||
| 730 | * |
||
| 731 | * @param int $id Commercial proposal ID |
||
| 732 | * @param int $status Must be 2 (accepted) or 3 (refused) {@min 2}{@max 3} |
||
| 733 | * @param string $note_private Add this mention at end of private note |
||
| 734 | * @param int $notrigger Disabled triggers |
||
| 735 | * |
||
| 736 | * @url POST {id}/close |
||
| 737 | * |
||
| 738 | * @return array |
||
| 739 | */ |
||
| 740 | public function close($id, $status, $note_private = '', $notrigger = 0) |
||
| 774 | } |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Set a commercial proposal billed. Could be also called setbilled |
||
| 778 | * |
||
| 779 | * @param int $id Commercial proposal ID |
||
| 780 | * |
||
| 781 | * @url POST {id}/setinvoiced |
||
| 782 | * |
||
| 783 | * @return array |
||
| 784 | */ |
||
| 785 | public function setinvoiced($id) |
||
| 786 | { |
||
| 787 | if (!DolibarrApiAccess::$user->rights->propal->creer) { |
||
| 788 | throw new RestException(401); |
||
| 789 | } |
||
| 790 | $result = $this->propal->fetch($id); |
||
| 791 | if (!$result) { |
||
| 792 | throw new RestException(404, 'Commercial Proposal not found'); |
||
| 793 | } |
||
| 794 | |||
| 795 | if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) { |
||
| 796 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 797 | } |
||
| 798 | |||
| 799 | $result = $this->propal->classifyBilled(DolibarrApiAccess::$user); |
||
| 800 | if ($result < 0) { |
||
| 801 | throw new RestException(500, 'Error : '.$this->propal->error); |
||
| 802 | } |
||
| 803 | |||
| 804 | $result = $this->propal->fetch($id); |
||
| 805 | if (!$result) { |
||
| 806 | throw new RestException(404, 'Proposal not found'); |
||
| 807 | } |
||
| 808 | |||
| 809 | if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) { |
||
| 810 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 811 | } |
||
| 812 | |||
| 813 | $this->propal->fetchObjectLinked(); |
||
| 814 | |||
| 815 | return $this->_cleanObjectDatas($this->propal); |
||
| 816 | } |
||
| 817 | |||
| 818 | |||
| 819 | /** |
||
| 820 | * Validate fields before create or update object |
||
| 821 | * |
||
| 822 | * @param array $data Array with data to verify |
||
| 823 | * @return array |
||
| 824 | * @throws RestException |
||
| 825 | */ |
||
| 826 | private function _validate($data) |
||
| 835 | } |
||
| 836 | |||
| 837 | |||
| 838 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
||
| 839 | /** |
||
| 840 | * Clean sensible object datas |
||
| 841 | * |
||
| 842 | * @param object $object Object to clean |
||
| 843 | * @return array Array of cleaned object properties |
||
| 844 | */ |
||
| 845 | protected function _cleanObjectDatas($object) |
||
| 858 | } |
||
| 859 | } |
||
| 860 |