22use Luracast\Restler\RestException;
37 public static $FIELDS = array(
54 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
55 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
56 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
57 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
59 $this->company =
new Societe($this->db);
61 if (!empty($conf->global->SOCIETE_EMAIL_MANDATORY)) {
62 static::$FIELDS[] =
'email';
76 public function get($id)
95 return $this->
_fetch(
'',
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
112 return $this->
_fetch(
'',
'',
'', $barcode);
132 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'')
136 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
137 throw new RestException(401);
141 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
145 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
146 $search_sale = DolibarrApiAccess::$user->id;
149 $sql =
"SELECT t.rowid";
150 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
151 $sql .=
", sc.fk_soc, sc.fk_user";
153 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
154 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
157 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
159 if (!in_array($mode, array(1, 2, 3))) {
160 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
163 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
164 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
166 $sql .=
", ".MAIN_DB_PREFIX.
"c_stcomm as st";
167 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
168 $sql .=
" AND t.fk_stcomm = st.id";
171 $sql .=
" AND t.client IN (1, 3)";
172 } elseif ($mode == 2) {
173 $sql .=
" AND t.client IN (2, 3)";
174 } elseif ($mode == 3) {
175 $sql .=
" AND t.client IN (0)";
176 } elseif ($mode == 4) {
177 $sql .=
" AND t.fournisseur IN (1)";
182 if (!empty($mode) && $mode != 4) {
183 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
184 } elseif (!empty($mode) && $mode == 4) {
185 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
187 $sql .=
" AND ((c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid) OR (cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid))";
191 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
192 $sql .=
" AND t.rowid = sc.fk_soc";
196 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
198 if ($search_sale > 0) {
199 $sql .=
" AND t.rowid = sc.fk_soc";
202 if ($search_sale > 0) {
203 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
210 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
214 $sql .= $this->db->order($sortfield, $sortorder);
220 $offset = $limit * $page;
222 $sql .= $this->db->plimit($limit + 1, $offset);
225 $result = $this->db->query($sql);
227 $num = $this->db->num_rows($result);
228 $min = min($num, ($limit <= 0 ? $num : $limit));
231 $obj = $this->db->fetch_object($result);
232 $soc_static =
new Societe($this->db);
233 if ($soc_static->fetch($obj->rowid)) {
234 if (isModEnabled(
'mailing')) {
235 $soc_static->getNoEmail();
242 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->db->lasterror());
244 if (!count($obj_ret)) {
245 throw new RestException(404,
'Thirdparties not found');
256 public function post($request_data =
null)
258 if (!DolibarrApiAccess::$user->rights->societe->creer) {
259 throw new RestException(401);
262 $result = $this->
_validate($request_data);
264 foreach ($request_data as $field => $value) {
265 $this->company->$field = $value;
267 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
268 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
270 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
271 $this->company->setNoEmail($this->company->no_email);
274 return $this->company->id;
287 public function put($id, $request_data =
null)
289 if (!DolibarrApiAccess::$user->rights->societe->creer) {
290 throw new RestException(401);
293 $result = $this->company->fetch($id);
295 throw new RestException(404,
'Thirdparty not found');
299 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
302 foreach ($request_data as $field => $value) {
303 if ($field ==
'id') {
306 if ($field ==
'array_options' && is_array($value)) {
307 foreach ($value as $index => $val) {
308 $this->company->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->company);
312 $this->company->$field = $value;
315 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
316 $this->company->setNoEmail($this->company->no_email);
319 if ($this->company->update($id, DolibarrApiAccess::$user, 1,
'',
'',
'update', 1) > 0) {
320 return $this->
get($id);
322 throw new RestException(500, $this->company->error);
340 public function merge($id, $idtodelete)
346 if ($id == $idtodelete) {
347 throw new RestException(400,
'Try to merge a thirdparty into itself');
350 if (!DolibarrApiAccess::$user->rights->societe->creer) {
351 throw new RestException(401);
354 $result = $this->company->fetch($id);
356 throw new RestException(404,
'Thirdparty not found');
360 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
363 $this->companytoremove =
new Societe($this->db);
365 $result = $this->companytoremove->fetch($idtodelete);
367 throw new RestException(404,
'Thirdparty not found');
371 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
374 $soc_origin = $this->companytoremove;
375 $object = $this->company;
376 $user = DolibarrApiAccess::$user;
385 $object->client = $object->client | $soc_origin->client;
386 $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
387 $listofproperties = array(
388 'address',
'zip',
'town',
'state_id',
'country_id',
'phone',
'phone_pro',
'fax',
'email',
'skype',
'url',
'barcode',
389 'idprof1',
'idprof2',
'idprof3',
'idprof4',
'idprof5',
'idprof6',
390 'tva_intra',
'effectif_id',
'forme_juridique',
'remise_percent',
'remise_supplier_percent',
'mode_reglement_supplier_id',
'cond_reglement_supplier_id',
'name_bis',
391 'stcomm_id',
'outstanding_limit',
'price_level',
'parent',
'default_lang',
'ref',
'ref_ext',
'import_key',
'fk_incoterms',
'fk_multicurrency',
392 'code_client',
'code_fournisseur',
'code_compta',
'code_compta_fournisseur',
393 'model_pdf',
'fk_projet'
395 foreach ($listofproperties as $property) {
396 if (empty($object->$property)) {
397 $object->$property = $soc_origin->$property;
402 $listofproperties = array(
403 'note_public',
'note_private'
405 foreach ($listofproperties as $property) {
406 $object->$property =
dol_concatdesc($object->$property, $soc_origin->$property);
410 if (is_array($soc_origin->array_options)) {
411 foreach ($soc_origin->array_options as $key => $val) {
412 if (empty($object->array_options[$key])) {
413 $object->array_options[$key] = $val;
420 $custcats = $static_cat->containing($soc_origin->id,
'customer',
'id');
421 $object->setCategories($custcats,
'customer');
422 $suppcats = $static_cat->containing($soc_origin->id,
'supplier',
'id');
423 $object->setCategories($suppcats,
'supplier');
426 if ($soc_origin->code_client == $object->code_client
427 || $soc_origin->code_fournisseur == $object->code_fournisseur
428 || $soc_origin->barcode == $object->barcode) {
429 dol_syslog(
"We clean customer and supplier code so we will be able to make the update of target");
430 $soc_origin->code_client =
'';
431 $soc_origin->code_fournisseur =
'';
432 $soc_origin->barcode =
'';
433 $soc_origin->update($soc_origin->id, $user, 0, 1, 1,
'merge');
437 $result = $object->update($object->id, $user, 0, 1, 1,
'merge');
447 'Adherent' =>
'/adherents/class/adherent.class.php',
448 'Don' =>
'/don/class/don.class.php',
449 'Societe' =>
'/societe/class/societe.class.php',
451 'ActionComm' =>
'/comm/action/class/actioncomm.class.php',
452 'Propal' =>
'/comm/propal/class/propal.class.php',
453 'Commande' =>
'/commande/class/commande.class.php',
454 'Facture' =>
'/compta/facture/class/facture.class.php',
455 'FactureRec' =>
'/compta/facture/class/facture-rec.class.php',
456 'LignePrelevement' =>
'/compta/prelevement/class/ligneprelevement.class.php',
457 'Mo' =>
'/mrp/class/mo.class.php',
458 'Contact' =>
'/contact/class/contact.class.php',
459 'Contrat' =>
'/contrat/class/contrat.class.php',
460 'Expedition' =>
'/expedition/class/expedition.class.php',
461 'Fichinter' =>
'/fichinter/class/fichinter.class.php',
462 'CommandeFournisseur' =>
'/fourn/class/fournisseur.commande.class.php',
463 'FactureFournisseur' =>
'/fourn/class/fournisseur.facture.class.php',
464 'SupplierProposal' =>
'/supplier_proposal/class/supplier_proposal.class.php',
465 'ProductFournisseur' =>
'/fourn/class/fournisseur.product.class.php',
466 'Delivery' =>
'/delivery/class/delivery.class.php',
467 'Product' =>
'/product/class/product.class.php',
468 'Project' =>
'/projet/class/project.class.php',
469 'Ticket' =>
'/ticket/class/ticket.class.php',
470 'User' =>
'/user/class/user.class.php',
471 'Account' =>
'/compta/bank/class/account.class.php',
472 'ConferenceOrBoothAttendee' =>
'/eventorganization/class/conferenceorboothattendee.class.php'
476 foreach ($objects as $object_name => $object_file) {
477 require_once DOL_DOCUMENT_ROOT.$object_file;
479 if (!$error && !$object_name::replaceThirdparty($this->db, $soc_origin->id, $object->id)) {
488 $parameters = array(
'soc_origin' => $soc_origin->id,
'soc_dest' => $object->id);
489 $reshook = $hookmanager->executeHooks(
'replaceThirdparty', $parameters, $soc_dest, $action);
499 $object->context = array(
'merge'=>1,
'mergefromid'=>$soc_origin->id);
502 $result = $object->call_trigger(
'COMPANY_MODIFY', $user);
512 if ($soc_origin->delete($soc_origin->id, $user) < 1) {
520 $this->db->rollback();
522 throw new RestException(500,
'Error failed to merged thirdparty '.$this->companytoremove->id.
' into '.$id.
'. Enable and read log file for more information.');
527 return $this->
get($id);
536 public function delete($id)
538 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
539 throw new RestException(401);
541 $result = $this->company->fetch($id);
543 throw new RestException(404,
'Thirdparty not found');
546 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
548 $this->company->oldcopy = clone $this->company;
550 $res = $this->company->delete($id);
552 throw new RestException(500,
"Can't delete, error occurs");
553 } elseif ($res == 0) {
554 throw new RestException(409,
"Can't delete, that product is probably used");
560 'message' =>
'Object deleted'
584 if (!isModEnabled(
'societe')) {
585 throw new RestException(501,
'Module "Thirdparties" needed for this request');
588 if (!isModEnabled(
"product")) {
589 throw new RestException(501,
'Module "Products" needed for this request');
592 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
593 throw new RestException(501,
'Multiprices features activation needed for this request');
596 if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
597 throw new RestException(400,
'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
600 if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
601 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
604 $result = $this->company->fetch($id);
606 throw new RestException(404,
'Thirdparty '.$id.
' not found');
609 if (empty($result)) {
610 throw new RestException(500,
'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
614 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
617 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
619 throw new RestException(500,
'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
637 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
639 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
640 throw new RestException(401);
643 $result = $this->company->fetch($id);
645 throw new RestException(404,
'Thirdparty not found');
650 $arrayofcateg = $categories->getListForItem($id,
'customer', $sortfield, $sortorder, $limit, $page);
652 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
653 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
656 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
660 return $arrayofcateg;
674 if (!DolibarrApiAccess::$user->rights->societe->creer) {
675 throw new RestException(401);
678 $result = $this->company->fetch($id);
680 throw new RestException(404,
'Thirdparty not found');
683 $result = $category->fetch($category_id);
685 throw new RestException(404,
'category not found');
689 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
692 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
695 $category->add_type($this->company,
'customer');
712 if (!DolibarrApiAccess::$user->rights->societe->creer) {
713 throw new RestException(401);
716 $result = $this->company->fetch($id);
718 throw new RestException(404,
'Thirdparty not found');
721 $result = $category->fetch($category_id);
723 throw new RestException(404,
'category not found');
727 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
730 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
733 $category->del_type($this->company,
'customer');
753 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
754 throw new RestException(401);
757 $result = $this->company->fetch($id);
759 throw new RestException(404,
'Thirdparty not found');
764 $result = $categories->getListForItem($id,
'supplier', $sortfield, $sortorder, $limit, $page);
766 if (is_numeric($result) && $result < 0) {
767 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
770 if (is_numeric($result) && $result == 0) {
789 if (!DolibarrApiAccess::$user->rights->societe->creer) {
790 throw new RestException(401);
793 $result = $this->company->fetch($id);
795 throw new RestException(404,
'Thirdparty not found');
798 $result = $category->fetch($category_id);
800 throw new RestException(404,
'category not found');
804 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
807 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
810 $category->add_type($this->company,
'supplier');
827 if (!DolibarrApiAccess::$user->rights->societe->creer) {
828 throw new RestException(401);
831 $result = $this->company->fetch($id);
833 throw new RestException(404,
'Thirdparty not found');
836 $result = $category->fetch($category_id);
838 throw new RestException(404,
'category not found');
842 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
845 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
848 $category->del_type($this->company,
'supplier');
870 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
871 throw new RestException(401);
875 throw new RestException(400,
'Thirdparty ID is mandatory');
879 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
882 $result = $this->company->fetch($id);
884 throw new RestException(404,
'Thirdparty not found');
887 $result = $this->company->getOutstandingProposals($mode);
889 unset($result[
'total_ht']);
890 unset($result[
'total_ttc']);
912 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
913 throw new RestException(401);
917 throw new RestException(400,
'Thirdparty ID is mandatory');
921 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
924 $result = $this->company->fetch($id);
926 throw new RestException(404,
'Thirdparty not found');
929 $result = $this->company->getOutstandingOrders($mode);
931 unset($result[
'total_ht']);
932 unset($result[
'total_ttc']);
953 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
954 throw new RestException(401);
958 throw new RestException(400,
'Thirdparty ID is mandatory');
962 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
965 $result = $this->company->fetch($id);
967 throw new RestException(404,
'Thirdparty not found');
970 $result = $this->company->getOutstandingBills($mode);
972 unset($result[
'total_ht']);
973 unset($result[
'total_ttc']);
994 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
995 throw new RestException(401);
999 throw new RestException(400,
'Thirdparty ID is mandatory');
1003 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1006 $result = $this->company->fetch($id);
1008 throw new RestException(404,
'Thirdparty not found');
1011 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1037 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1038 throw new RestException(401);
1042 throw new RestException(400,
'Thirdparty ID is mandatory');
1046 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1049 $result = $this->company->fetch($id);
1051 throw new RestException(404,
'Thirdparty not found');
1055 $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";
1056 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1057 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as f ON f.rowid = re.fk_facture_source";
1058 $sql .=
" WHERE re.fk_soc = ".((int) $id);
1059 if ($filter ==
"available") {
1060 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1062 if ($filter ==
"used") {
1063 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1066 $sql .= $this->db->order($sortfield, $sortorder);
1068 $result = $this->db->query($sql);
1070 throw new RestException(503, $this->db->lasterror());
1072 $num = $this->db->num_rows($result);
1073 while ($obj = $this->db->fetch_object($result)) {
1098 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1099 throw new RestException(401);
1102 throw new RestException(400,
'Thirdparty ID is mandatory');
1106 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1114 $invoice =
new Facture($this->db);
1115 $result = $invoice->list_replacable_invoices($id);
1117 throw new RestException(405, $invoice->error);
1141 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1142 throw new RestException(401);
1145 throw new RestException(400,
'Thirdparty ID is mandatory');
1149 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1157 $invoice =
new Facture($this->db);
1158 $result = $invoice->list_qualified_avoir_invoices($id);
1160 throw new RestException(405, $invoice->error);
1177 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1178 throw new RestException(401);
1181 throw new RestException(400,
'Thirdparty ID is mandatory');
1185 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1192 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1193 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1194 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1196 $sql .=
" WHERE fk_soc = ".((int) $id);
1199 $result = $this->db->query($sql);
1201 if ($this->db->num_rows($result) == 0) {
1202 throw new RestException(404,
'Account not found');
1207 $accounts = array();
1210 $num = $this->db->num_rows($result);
1212 $obj = $this->db->fetch_object($result);
1214 if ($account->fetch($obj->rowid)) {
1215 $accounts[] = $account;
1220 throw new RestException(404,
'Account not found');
1224 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1226 $returnAccounts = array();
1228 foreach ($accounts as $account) {
1230 foreach ($account as $key => $value) {
1231 if (in_array($key, $fields)) {
1232 $object[$key] = $value;
1235 $returnAccounts[] = $object;
1238 return $returnAccounts;
1252 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1253 throw new RestException(401);
1255 if ($this->company->fetch($id) <= 0) {
1256 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1260 $account->socid = $id;
1262 foreach ($request_data as $field => $value) {
1263 $account->$field = $value;
1266 if ($account->create(DolibarrApiAccess::$user) < 0) {
1267 throw new RestException(500,
'Error creating Company Bank account');
1270 if (empty($account->rum)) {
1271 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1273 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1274 $account->date_rum =
dol_now();
1277 if ($account->update(DolibarrApiAccess::$user) < 0) {
1278 throw new RestException(500,
'Error updating values');
1297 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1298 throw new RestException(401);
1300 if ($this->company->fetch($id) <= 0) {
1301 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1305 $account->fetch($bankaccount_id, $id, -1,
'');
1307 if ($account->socid != $id) {
1308 throw new RestException(401);
1312 foreach ($request_data as $field => $value) {
1313 $account->$field = $value;
1316 if (empty($account->rum)) {
1317 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1319 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1320 $account->date_rum =
dol_now();
1323 if ($account->update(DolibarrApiAccess::$user) < 0) {
1324 throw new RestException(500,
'Error updating values');
1342 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1343 throw new RestException(401);
1348 $account->fetch($bankaccount_id);
1350 if (!$account->socid == $id) {
1351 throw new RestException(401);
1354 return $account->delete(DolibarrApiAccess::$user);
1369 global $conf, $langs;
1371 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1373 if ($this->company->fetch($id) <= 0) {
1374 throw new RestException(404,
'Thirdparty not found');
1377 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1378 throw new RestException(401);
1381 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1383 $this->company->fk_bank = $this->company->fk_account;
1384 $this->company->fk_account = $this->company->fk_account;
1386 $outputlangs = $langs;
1391 if (isset($this->company->thirdparty->default_lang)) {
1392 $newlang = $this->company->thirdparty->default_lang;
1393 } elseif (isset($this->company->default_lang)) {
1394 $newlang = $this->company->default_lang;
1397 if (!empty($newlang)) {
1398 $outputlangs =
new Translate(
"", $conf);
1399 $outputlangs->setDefaultLang($newlang);
1402 $sql =
"SELECT rowid";
1403 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1405 $sql .=
" WHERE fk_soc = ".((int) $id);
1407 if ($companybankid) {
1408 $sql .=
" AND rowid = ".((int) $companybankid);
1412 $accounts = array();
1414 $result = $this->db->query($sql);
1416 if ($this->db->num_rows($result) == 0) {
1417 throw new RestException(404,
'Bank account not found');
1420 $num = $this->db->num_rows($result);
1422 $obj = $this->db->fetch_object($result);
1425 if ($account->fetch($obj->rowid)) {
1426 $accounts[] = $account;
1431 throw new RestException(500,
'Sql error '.$this->db->lasterror());
1434 $moreparams = array(
1435 'use_companybankid' => $accounts[0]->
id,
1436 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName($this->company->id)
1439 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1442 return array(
"success" => $result);
1444 throw new RestException(500,
'Error generating the document '.$this->company->error);
1462 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1463 throw new RestException(401);
1467 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1473 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1474 $sql .=
" WHERE fk_soc = ".((int) $id);
1476 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1479 $result = $this->db->query($sql);
1481 if ($result && $this->db->num_rows($result) == 0) {
1482 throw new RestException(404,
'This thirdparty does not have any gateway attached or does not exist.');
1487 $accounts = array();
1489 $num = $this->db->num_rows($result);
1491 $obj = $this->db->fetch_object($result);
1494 if ($account->fetch($obj->rowid)) {
1495 $accounts[] = $account;
1500 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1502 $returnAccounts = array();
1504 foreach ($accounts as $account) {
1506 foreach ($account as $key => $value) {
1507 if (in_array($key, $fields)) {
1508 $object[$key] = $value;
1511 $returnAccounts[] = $object;
1514 return $returnAccounts;
1538 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1539 throw new RestException(401);
1542 if (!isset($request_data[
'site'])) {
1543 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1546 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
1547 $result = $this->db->query($sql);
1549 if ($result && $this->db->num_rows($result) == 0) {
1551 if (!isset($request_data[
'login'])) {
1552 $account->login =
"";
1554 $account->fk_soc = $id;
1556 foreach ($request_data as $field => $value) {
1557 $account->$field = $value;
1560 if ($account->create(DolibarrApiAccess::$user) < 0) {
1561 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1568 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1596 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1597 throw new RestException(401);
1600 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1601 $result = $this->db->query($sql);
1604 if ($result && $this->db->num_rows == 0) {
1605 if (!isset($request_data[
'key_account'])) {
1606 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1609 if (!isset($request_data[
'login'])) {
1610 $account->login =
"";
1613 foreach ($request_data as $field => $value) {
1614 $account->$field = $value;
1617 $account->fk_soc = $id;
1618 $account->site = $site;
1620 if ($account->create(DolibarrApiAccess::$user) < 0) {
1621 throw new RestException(500,
'Error creating SocieteAccount entity.');
1625 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1626 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1627 $result = $this->db->query($sql);
1629 if ($result && $this->db->num_rows($result) !== 0) {
1630 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.");
1634 $obj = $this->db->fetch_object($result);
1637 $account->id = $obj->rowid;
1638 $account->fk_soc = $id;
1639 $account->site = $site;
1640 if (!isset($request_data[
'login'])) {
1641 $account->login =
"";
1643 $account->fk_user_creat = $obj->fk_user_creat;
1644 $account->date_creation = $obj->date_creation;
1646 foreach ($request_data as $field => $value) {
1647 $account->$field = $value;
1650 if ($account->update(DolibarrApiAccess::$user) < 0) {
1651 throw new RestException(500,
'Error updating SocieteAccount entity.');
1678 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1679 throw new RestException(401);
1682 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($site).
"'";
1683 $result = $this->db->query($sql);
1685 if ($result && $this->db->num_rows($result) == 0) {
1686 throw new RestException(404,
"This thirdparty does not have $site gateway attached or does not exist.");
1689 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1690 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1691 $result = $this->db->query($sql);
1693 if ($result && $this->db->num_rows($result) !== 0) {
1694 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.");
1698 $obj = $this->db->fetch_object($result);
1700 $account->fetch($obj->rowid);
1702 foreach ($request_data as $field => $value) {
1703 $account->$field = $value;
1706 if ($account->update(DolibarrApiAccess::$user) < 0) {
1707 throw new RestException(500,
'Error updating SocieteAccount account');
1731 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1732 throw new RestException(401);
1735 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1736 $result = $this->db->query($sql);
1738 if ($result && $this->db->num_rows($result) == 0) {
1739 throw new RestException(404);
1741 $obj = $this->db->fetch_object($result);
1743 $account->fetch($obj->rowid);
1745 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1746 throw new RestException(500,
"Error while deleting $site gateway attached to this third party");
1765 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1766 throw new RestException(401);
1773 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1774 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id);
1776 $result = $this->db->query($sql);
1778 if ($result && $this->db->num_rows($result) == 0) {
1779 throw new RestException(404,
'This third party does not have any gateway attached or does not exist.');
1783 $num = $this->db->num_rows($result);
1785 $obj = $this->db->fetch_object($result);
1787 $account->fetch($obj->rowid);
1789 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1790 throw new RestException(500,
'Error while deleting gateways attached to this third party');
1807 $object = parent::_cleanObjectDatas($object);
1809 unset($object->nom);
1810 unset($object->name_bis);
1811 unset($object->note);
1812 unset($object->departement);
1813 unset($object->departement_code);
1814 unset($object->pays);
1815 unset($object->particulier);
1816 unset($object->prefix_comm);
1818 unset($object->siren);
1819 unset($object->siret);
1820 unset($object->ape);
1822 unset($object->commercial_id);
1824 unset($object->total_ht);
1825 unset($object->total_tva);
1826 unset($object->total_localtax1);
1827 unset($object->total_localtax2);
1828 unset($object->total_ttc);
1830 unset($object->lines);
1831 unset($object->thirdparty);
1833 unset($object->fk_delivery_address);
1848 $thirdparty = array();
1849 foreach (Thirdparties::$FIELDS as $field) {
1850 if (!isset($data[$field])) {
1851 throw new RestException(400,
"$field field missing");
1853 $thirdparty[$field] = $data[$field];
1879 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
1883 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1884 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
1888 $result = $this->company->initAsSpecimen();
1890 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1893 throw new RestException(404,
'Thirdparty not found');
1897 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
1899 if (isModEnabled(
'mailing')) {
1900 $this->company->getNoEmail();
1903 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1904 $filterabsolutediscount =
"fk_facture_source IS NULL";
1905 $filtercreditnote =
"fk_facture_source IS NOT NULL";
1907 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1908 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1911 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
1912 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
1913 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
1914 $this->company->absolute_creditnote =
price2num($absolute_creditnote,
'MT');
Class to manage withdrawal receipts.
Class to manage categories.
Class to manage bank accounts description of third parties.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class to manage invoices.
Class for SocieteAccount.
Class to manage third parties objects (customers, suppliers, prospects...)
setThirdpartyPriceLevel($id, $priceLevel)
Set new price level for the given thirdparty.
_cleanObjectDatas($object)
Clean sensible object datas.
getSupplierCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get supplier categories for a thirdparty.
getSocieteAccounts($id, $site=null)
Get a specific gateway attached to a thirdparty (by specifying the site key)
getOutStandingOrder($id, $mode='customer')
Get outstanding orders of thirdparty.
getByBarcode($barcode)
Get properties of a thirdparty object by barcode.
generateBankAccountDocument($id, $companybankid=null, $model='sepamandate')
Generate a Document from a bank account record (like SEPA mandate)
addCategory($id, $category_id)
Add a customer category to a thirdparty.
getCompanyBankAccount($id)
Get CompanyBankAccount objects for thirdparty.
getInvoicesQualifiedForReplacement($id)
Return list of invoices qualified to be replaced by another invoice.
post($request_data=null)
Create thirdparty object.
put($id, $request_data=null)
Update thirdparty.
getByEmail($email)
Get properties of a thirdparty object by email.
_validate($data)
Validate fields before create or update object.
addSupplierCategory($id, $category_id)
Add a supplier category to a thirdparty.
merge($id, $idtodelete)
Merge a thirdparty into another one.
deleteSocieteAccounts($id)
Delete all gateways attached to a thirdparty.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='')
List thirdparties.
__construct()
Constructor.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get customer categories for a thirdparty.
deleteSupplierCategory($id, $category_id)
Remove the link between a category and the thirdparty.
putSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site gateway to a thirdparty.
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update CompanyBankAccount object for thirdparty.
deleteSocieteAccount($id, $site)
Delete a specific site gateway attached to a thirdparty (by gateway id)
getInvoicesQualifiedForCreditNote($id)
Return list of invoices qualified to be corrected by a credit note.
patchSocieteAccount($id, $site, $request_data=null)
Update specified values of a specific gateway attached to a thirdparty.
getFixedAmountDiscounts($id, $filter="none", $sortfield="f.type", $sortorder='ASC')
Get fixed amount discount of a thirdparty (all sources: deposit, credit note, commercial offers....
getOutStandingProposals($id, $mode='customer')
Get outstanding proposals of thirdparty.
_fetch($rowid, $ref='', $ref_ext='', $barcode='', $idprof1='', $idprof2='', $idprof3='', $idprof4='', $idprof5='', $idprof6='', $email='', $ref_alias='')
Fetch properties of a thirdparty object.
getSalesRepresentatives($id, $mode=0)
Get representatives of thirdparty.
getOutStandingInvoices($id, $mode='customer')
Get outstanding invoices of thirdparty.
deleteCompanyBankAccount($id, $bankaccount_id)
Delete a bank account attached to a thirdparty.
createSocieteAccount($id, $request_data=null)
Create and attach a new gateway to an existing thirdparty.
createCompanyBankAccount($id, $request_data=null)
Create CompanyBankAccount object for thirdparty.
deleteCategory($id, $category_id)
Remove the link between a customer category and the thirdparty.
Class to manage translations.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
dol_concatdesc($text1, $text2, $forxml=false, $invert=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.