21 use Luracast\Restler\RestException;
36 public static $FIELDS = array(
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';
60 if (!empty($conf->global->SOCIETE_EMAIL_MANDATORY)) {
61 static::$FIELDS[] =
'email';
75 public function get($id)
94 return $this->
_fetch(
'',
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
111 return $this->
_fetch(
'',
'',
'', $barcode);
131 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'')
135 if (!DolibarrApiAccess::$user->rights->societe->lire) {
136 throw new RestException(401);
140 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
144 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
145 $search_sale = DolibarrApiAccess::$user->id;
148 $sql =
"SELECT t.rowid";
149 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
150 $sql .=
", sc.fk_soc, sc.fk_user";
152 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
155 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
157 if (!in_array($mode, array(1, 2, 3))) {
158 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
161 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
162 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
164 $sql .=
", ".MAIN_DB_PREFIX.
"c_stcomm as st";
165 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
166 $sql .=
" AND t.fk_stcomm = st.id";
169 $sql .=
" AND t.client IN (1, 3)";
170 } elseif ($mode == 2) {
171 $sql .=
" AND t.client IN (2, 3)";
172 } elseif ($mode == 3) {
173 $sql .=
" AND t.client IN (0)";
174 } elseif ($mode == 4) {
175 $sql .=
" AND t.fournisseur IN (1)";
180 if (!empty($mode) && $mode != 4) {
181 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
182 } elseif (!empty($mode) && $mode == 4) {
183 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
185 $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))";
189 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
190 $sql .=
" AND t.rowid = sc.fk_soc";
194 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
196 if ($search_sale > 0) {
197 $sql .=
" AND t.rowid = sc.fk_soc";
200 if ($search_sale > 0) {
201 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
207 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
209 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
210 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
213 $sql .= $this->
db->order($sortfield, $sortorder);
219 $offset = $limit * $page;
221 $sql .= $this->
db->plimit($limit + 1, $offset);
224 $result = $this->
db->query($sql);
226 $num = $this->
db->num_rows($result);
227 $min = min($num, ($limit <= 0 ? $num : $limit));
230 $obj = $this->
db->fetch_object($result);
232 if ($soc_static->fetch($obj->rowid)) {
238 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->
db->lasterror());
240 if (!count($obj_ret)) {
241 throw new RestException(404,
'Thirdparties not found');
252 public function post($request_data =
null)
254 if (!DolibarrApiAccess::$user->rights->societe->creer) {
255 throw new RestException(401);
258 $result = $this->
_validate($request_data);
260 foreach ($request_data as $field => $value) {
261 $this->company->$field = $value;
263 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
264 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
267 return $this->company->id;
277 public function put($id, $request_data =
null)
279 if (!DolibarrApiAccess::$user->rights->societe->creer) {
280 throw new RestException(401);
283 $result = $this->company->fetch($id);
285 throw new RestException(404,
'Thirdparty not found');
289 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
292 foreach ($request_data as $field => $value) {
293 if ($field ==
'id') {
296 $this->company->$field = $value;
299 if ($this->company->update($id, DolibarrApiAccess::$user, 1,
'',
'',
'update')) {
300 return $this->
get($id);
320 public function merge($id, $idtodelete)
326 if ($id == $idtodelete) {
327 throw new RestException(400,
'Try to merge a thirdparty into itself');
330 if (!DolibarrApiAccess::$user->rights->societe->creer) {
331 throw new RestException(401);
334 $result = $this->company->fetch($id);
336 throw new RestException(404,
'Thirdparty not found');
340 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
343 $this->companytoremove =
new Societe($this->
db);
345 $result = $this->companytoremove->fetch($idtodelete);
347 throw new RestException(404,
'Thirdparty not found');
351 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
354 $soc_origin = $this->companytoremove;
355 $object = $this->company;
356 $user = DolibarrApiAccess::$user;
365 $object->client = $object->client | $soc_origin->client;
366 $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
367 $listofproperties = array(
368 'address',
'zip',
'town',
'state_id',
'country_id',
'phone',
'phone_pro',
'fax',
'email',
'skype',
'url',
'barcode',
369 'idprof1',
'idprof2',
'idprof3',
'idprof4',
'idprof5',
'idprof6',
370 'tva_intra',
'effectif_id',
'forme_juridique',
'remise_percent',
'remise_supplier_percent',
'mode_reglement_supplier_id',
'cond_reglement_supplier_id',
'name_bis',
371 'stcomm_id',
'outstanding_limit',
'price_level',
'parent',
'default_lang',
'ref',
'ref_ext',
'import_key',
'fk_incoterms',
'fk_multicurrency',
372 'code_client',
'code_fournisseur',
'code_compta',
'code_compta_fournisseur',
373 'model_pdf',
'fk_projet'
375 foreach ($listofproperties as $property) {
376 if (empty($object->$property)) {
377 $object->$property = $soc_origin->$property;
382 $listofproperties = array(
383 'note_public',
'note_private'
385 foreach ($listofproperties as $property) {
386 $object->$property =
dol_concatdesc($object->$property, $soc_origin->$property);
390 if (is_array($soc_origin->array_options)) {
391 foreach ($soc_origin->array_options as $key => $val) {
392 if (empty($object->array_options[$key])) {
393 $object->array_options[$key] = $val;
400 $custcats = $static_cat->containing($soc_origin->id,
'customer',
'id');
401 $object->setCategories($custcats,
'customer');
402 $suppcats = $static_cat->containing($soc_origin->id,
'supplier',
'id');
403 $object->setCategories($suppcats,
'supplier');
406 if ($soc_origin->code_client == $object->code_client
407 || $soc_origin->code_fournisseur == $object->code_fournisseur
408 || $soc_origin->barcode == $object->barcode) {
409 dol_syslog(
"We clean customer and supplier code so we will be able to make the update of target");
410 $soc_origin->code_client =
'';
411 $soc_origin->code_fournisseur =
'';
412 $soc_origin->barcode =
'';
413 $soc_origin->update($soc_origin->id, $user, 0, 1, 1,
'merge');
417 $result = $object->update($object->id, $user, 0, 1, 1,
'merge');
427 'Adherent' =>
'/adherents/class/adherent.class.php',
428 'Don' =>
'/don/class/don.class.php',
429 'Societe' =>
'/societe/class/societe.class.php',
431 'ActionComm' =>
'/comm/action/class/actioncomm.class.php',
432 'Propal' =>
'/comm/propal/class/propal.class.php',
433 'Commande' =>
'/commande/class/commande.class.php',
434 'Facture' =>
'/compta/facture/class/facture.class.php',
435 'FactureRec' =>
'/compta/facture/class/facture-rec.class.php',
436 'LignePrelevement' =>
'/compta/prelevement/class/ligneprelevement.class.php',
437 'Mo' =>
'/mrp/class/mo.class.php',
438 'Contact' =>
'/contact/class/contact.class.php',
439 'Contrat' =>
'/contrat/class/contrat.class.php',
440 'Expedition' =>
'/expedition/class/expedition.class.php',
441 'Fichinter' =>
'/fichinter/class/fichinter.class.php',
442 'CommandeFournisseur' =>
'/fourn/class/fournisseur.commande.class.php',
443 'FactureFournisseur' =>
'/fourn/class/fournisseur.facture.class.php',
444 'SupplierProposal' =>
'/supplier_proposal/class/supplier_proposal.class.php',
445 'ProductFournisseur' =>
'/fourn/class/fournisseur.product.class.php',
446 'Delivery' =>
'/delivery/class/delivery.class.php',
447 'Product' =>
'/product/class/product.class.php',
448 'Project' =>
'/projet/class/project.class.php',
449 'Ticket' =>
'/ticket/class/ticket.class.php',
450 'User' =>
'/user/class/user.class.php',
451 'Account' =>
'/compta/bank/class/account.class.php',
452 'ConferenceOrBoothAttendee' =>
'/eventorganization/class/conferenceorboothattendee.class.php'
456 foreach ($objects as $object_name => $object_file) {
457 require_once DOL_DOCUMENT_ROOT.$object_file;
459 if (!$error && !$object_name::replaceThirdparty($this->
db, $soc_origin->id, $object->id)) {
468 $reshook = $hookmanager->executeHooks(
'replaceThirdparty', array(
469 'soc_origin' => $soc_origin->id,
470 'soc_dest' => $object->id
471 ), $soc_dest, $action);
481 $object->context = array(
'merge'=>1,
'mergefromid'=>$soc_origin->id);
484 $result = $object->call_trigger(
'COMPANY_MODIFY', $user);
494 if ($soc_origin->delete($soc_origin->id, $user) < 1) {
502 $this->
db->rollback();
504 throw new RestException(500,
'Error failed to merged thirdparty '.$this->companytoremove->id.
' into '.$id.
'. Enable and read log file for more information.');
509 return $this->
get($id);
518 public function delete($id)
520 if (!DolibarrApiAccess::$user->rights->societe->supprimer) {
521 throw new RestException(401);
523 $result = $this->company->fetch($id);
525 throw new RestException(404,
'Thirdparty not found');
528 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
530 $this->company->oldcopy = clone $this->company;
532 $res = $this->company->delete($id);
534 throw new RestException(500,
"Can't delete, error occurs");
535 } elseif ($res == 0) {
536 throw new RestException(409,
"Can't delete, that product is probably used");
542 'message' =>
'Object deleted'
566 if (empty($conf->societe->enabled)) {
567 throw new RestException(501,
'Module "Thirdparties" needed for this request');
570 if (empty($conf->product->enabled)) {
571 throw new RestException(501,
'Module "Products" needed for this request');
574 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
575 throw new RestException(501,
'Multiprices features activation needed for this request');
578 if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
579 throw new RestException(400,
'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
582 if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
583 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
586 $result = $this->company->fetch($id);
588 throw new RestException(404,
'Thirdparty '.$id.
' not found');
591 if (empty($result)) {
592 throw new RestException(500,
'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
596 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
599 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
601 throw new RestException(500,
'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
620 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
622 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
623 throw new RestException(401);
626 $result = $this->company->fetch($id);
628 throw new RestException(404,
'Thirdparty not found');
633 $result = $categories->getListForItem($id,
'customer', $sortfield, $sortorder, $limit, $page);
635 if (is_numeric($result) && $result < 0) {
636 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
639 if (is_numeric($result) && $result == 0) {
658 if (!DolibarrApiAccess::$user->rights->societe->creer) {
659 throw new RestException(401);
662 $result = $this->company->fetch($id);
664 throw new RestException(404,
'Thirdparty not found');
667 $result = $category->fetch($category_id);
669 throw new RestException(404,
'category not found');
673 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
676 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
679 $category->add_type($this->company,
'customer');
696 if (!DolibarrApiAccess::$user->rights->societe->creer) {
697 throw new RestException(401);
700 $result = $this->company->fetch($id);
702 throw new RestException(404,
'Thirdparty not found');
705 $result = $category->fetch($category_id);
707 throw new RestException(404,
'category not found');
711 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
714 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
717 $category->del_type($this->company,
'customer');
737 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
738 throw new RestException(401);
741 $result = $this->company->fetch($id);
743 throw new RestException(404,
'Thirdparty not found');
748 $result = $categories->getListForItem($id,
'supplier', $sortfield, $sortorder, $limit, $page);
750 if (is_numeric($result) && $result < 0) {
751 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
754 if (is_numeric($result) && $result == 0) {
773 if (!DolibarrApiAccess::$user->rights->societe->creer) {
774 throw new RestException(401);
777 $result = $this->company->fetch($id);
779 throw new RestException(404,
'Thirdparty not found');
782 $result = $category->fetch($category_id);
784 throw new RestException(404,
'category not found');
788 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
791 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
794 $category->add_type($this->company,
'supplier');
811 if (!DolibarrApiAccess::$user->rights->societe->creer) {
812 throw new RestException(401);
815 $result = $this->company->fetch($id);
817 throw new RestException(404,
'Thirdparty not found');
820 $result = $category->fetch($category_id);
822 throw new RestException(404,
'category not found');
826 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
829 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
832 $category->del_type($this->company,
'supplier');
854 if (!DolibarrApiAccess::$user->rights->societe->lire) {
855 throw new RestException(401);
859 throw new RestException(400,
'Thirdparty ID is mandatory');
863 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
866 $result = $this->company->fetch($id);
868 throw new RestException(404,
'Thirdparty not found');
871 $result = $this->company->getOutstandingProposals($mode);
873 unset($result[
'total_ht']);
874 unset($result[
'total_ttc']);
896 if (!DolibarrApiAccess::$user->rights->societe->lire) {
897 throw new RestException(401);
901 throw new RestException(400,
'Thirdparty ID is mandatory');
905 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
908 $result = $this->company->fetch($id);
910 throw new RestException(404,
'Thirdparty not found');
913 $result = $this->company->getOutstandingOrders($mode);
915 unset($result[
'total_ht']);
916 unset($result[
'total_ttc']);
937 if (!DolibarrApiAccess::$user->rights->societe->lire) {
938 throw new RestException(401);
942 throw new RestException(400,
'Thirdparty ID is mandatory');
946 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
949 $result = $this->company->fetch($id);
951 throw new RestException(404,
'Thirdparty not found');
954 $result = $this->company->getOutstandingBills($mode);
956 unset($result[
'total_ht']);
957 unset($result[
'total_ttc']);
978 if (!DolibarrApiAccess::$user->rights->societe->lire) {
979 throw new RestException(401);
983 throw new RestException(400,
'Thirdparty ID is mandatory');
987 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
990 $result = $this->company->fetch($id);
992 throw new RestException(404,
'Thirdparty not found');
995 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1021 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1022 throw new RestException(401);
1026 throw new RestException(400,
'Thirdparty ID is mandatory');
1030 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1033 $result = $this->company->fetch($id);
1035 throw new RestException(404,
'Thirdparty not found');
1039 $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";
1040 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1041 $sql .=
" WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
1042 if ($filter ==
"available") {
1043 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1045 if ($filter ==
"used") {
1046 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1049 $sql .= $this->
db->order($sortfield, $sortorder);
1051 $result = $this->
db->query($sql);
1053 throw new RestException(503, $this->
db->lasterror());
1055 $num = $this->
db->num_rows($result);
1056 while ($obj = $this->
db->fetch_object($result)) {
1081 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1082 throw new RestException(401);
1085 throw new RestException(400,
'Thirdparty ID is mandatory');
1089 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1098 $result = $invoice->list_replacable_invoices($id);
1100 throw new RestException(405, $invoice->error);
1124 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1125 throw new RestException(401);
1128 throw new RestException(400,
'Thirdparty ID is mandatory');
1132 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1141 $result = $invoice->list_qualified_avoir_invoices($id);
1143 throw new RestException(405, $invoice->error);
1160 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1161 throw new RestException(401);
1164 throw new RestException(400,
'Thirdparty ID is mandatory');
1168 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1175 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1176 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1177 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1179 $sql .=
" WHERE fk_soc = ".((int) $id);
1182 $result = $this->
db->query($sql);
1184 if ($this->
db->num_rows($result) == 0) {
1185 throw new RestException(404,
'Account not found');
1190 $accounts = array();
1193 $num = $this->
db->num_rows($result);
1195 $obj = $this->
db->fetch_object($result);
1197 if ($account->fetch($obj->rowid)) {
1198 $accounts[] = $account;
1203 throw new RestException(404,
'Account not found');
1207 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1209 $returnAccounts = array();
1211 foreach ($accounts as $account) {
1213 foreach ($account as $key => $value) {
1214 if (in_array($key, $fields)) {
1215 $object[$key] = $value;
1218 $returnAccounts[] = $object;
1221 return $returnAccounts;
1235 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1236 throw new RestException(401);
1238 if ($this->company->fetch($id) <= 0) {
1239 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1243 $account->socid = $id;
1245 foreach ($request_data as $field => $value) {
1246 $account->$field = $value;
1249 if ($account->create(DolibarrApiAccess::$user) < 0) {
1250 throw new RestException(500,
'Error creating Company Bank account');
1253 if (empty($account->rum)) {
1254 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1256 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1257 $account->date_rum =
dol_now();
1260 if ($account->update(DolibarrApiAccess::$user) < 0) {
1261 throw new RestException(500,
'Error updating values');
1280 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1281 throw new RestException(401);
1283 if ($this->company->fetch($id) <= 0) {
1284 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1288 $account->fetch($bankaccount_id, $id, -1,
'');
1290 if ($account->socid != $id) {
1291 throw new RestException(401);
1295 foreach ($request_data as $field => $value) {
1296 $account->$field = $value;
1299 if (empty($account->rum)) {
1300 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1302 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1303 $account->date_rum =
dol_now();
1306 if ($account->update(DolibarrApiAccess::$user) < 0) {
1307 throw new RestException(500,
'Error updating values');
1325 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1326 throw new RestException(401);
1331 $account->fetch($bankaccount_id);
1333 if (!$account->socid == $id) {
1334 throw new RestException(401);
1337 return $account->delete(DolibarrApiAccess::$user);
1352 global $conf, $langs;
1354 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1356 if ($this->company->fetch($id) <= 0) {
1357 throw new RestException(404,
'Thirdparty not found');
1360 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1361 throw new RestException(401);
1364 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1366 $this->company->fk_bank = $this->company->fk_account;
1368 $outputlangs = $langs;
1372 if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang)) {
1373 if (isset($this->company->thirdparty->default_lang)) {
1374 $newlang = $this->company->thirdparty->default_lang;
1375 } elseif (isset($this->company->default_lang)) {
1376 $newlang = $this->company->default_lang;
1379 if (!empty($newlang)) {
1380 $outputlangs =
new Translate(
"", $conf);
1381 $outputlangs->setDefaultLang($newlang);
1384 $sql =
"SELECT rowid";
1385 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1387 $sql .=
" WHERE fk_soc = ".((int) $id);
1389 if ($companybankid) {
1390 $sql .=
" AND rowid = ".((int) $companybankid);
1394 $accounts = array();
1396 $result = $this->
db->query($sql);
1398 if ($this->
db->num_rows($result) == 0) {
1399 throw new RestException(404,
'Bank account not found');
1402 $num = $this->
db->num_rows($result);
1404 $obj = $this->
db->fetch_object($result);
1407 if ($account->fetch($obj->rowid)) {
1408 $accounts[] = $account;
1413 throw new RestException(500,
'Sql error '.$this->
db->lasterror());
1416 $moreparams = array(
1417 'use_companybankid' => $accounts[0]->
id,
1418 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName($this->company->id)
1421 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1424 return array(
"success" => $result);
1426 throw new RestException(500,
'Error generating the document '.$this->company->error);
1444 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1445 throw new RestException(401);
1449 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1455 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1456 $sql .=
" WHERE fk_soc = ".((int) $id);
1458 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1461 $result = $this->
db->query($sql);
1463 if ($result && $this->
db->num_rows($result) == 0) {
1464 throw new RestException(404,
'This thirdparty does not have any gateway attached or does not exist.');
1469 $accounts = array();
1471 $num = $this->
db->num_rows($result);
1473 $obj = $this->
db->fetch_object($result);
1476 if ($account->fetch($obj->rowid)) {
1477 $accounts[] = $account;
1482 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1484 $returnAccounts = array();
1486 foreach ($accounts as $account) {
1488 foreach ($account as $key => $value) {
1489 if (in_array($key, $fields)) {
1490 $object[$key] = $value;
1493 $returnAccounts[] = $object;
1496 return $returnAccounts;
1520 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1521 throw new RestException(401);
1524 if (!isset($request_data[
'site'])) {
1525 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1528 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($request_data[
'site']).
"'";
1529 $result = $this->
db->query($sql);
1531 if ($result && $this->
db->num_rows($result) == 0) {
1533 if (!isset($request_data[
'login'])) {
1534 $account->login =
"";
1536 $account->fk_soc = $id;
1538 foreach ($request_data as $field => $value) {
1539 $account->$field = $value;
1542 if ($account->create(DolibarrApiAccess::$user) < 0) {
1543 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1550 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1578 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1579 throw new RestException(401);
1582 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->
db->escape($site).
"'";
1583 $result = $this->
db->query($sql);
1586 if ($result && $this->
db->num_rows == 0) {
1587 if (!isset($request_data[
'key_account'])) {
1588 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1591 if (!isset($request_data[
'login'])) {
1592 $account->login =
"";
1595 foreach ($request_data as $field => $value) {
1596 $account->$field = $value;
1599 $account->fk_soc = $id;
1600 $account->site = $site;
1602 if ($account->create(DolibarrApiAccess::$user) < 0) {
1603 throw new RestException(500,
'Error creating SocieteAccount entity.');
1607 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1608 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($request_data[
'site']).
"' ";
1609 $result = $this->
db->query($sql);
1611 if ($result && $this->
db->num_rows($result) !== 0) {
1612 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.");
1616 $obj = $this->
db->fetch_object($result);
1619 $account->id = $obj->rowid;
1620 $account->fk_soc = $id;
1621 $account->site = $site;
1622 if (!isset($request_data[
'login'])) {
1623 $account->login =
"";
1625 $account->fk_user_creat = $obj->fk_user_creat;
1626 $account->date_creation = $obj->date_creation;
1628 foreach ($request_data as $field => $value) {
1629 $account->$field = $value;
1632 if ($account->update(DolibarrApiAccess::$user) < 0) {
1633 throw new RestException(500,
'Error updating SocieteAccount entity.');
1660 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1661 throw new RestException(401);
1664 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($site).
"'";
1665 $result = $this->
db->query($sql);
1667 if ($result && $this->
db->num_rows($result) == 0) {
1668 throw new RestException(404,
"This thirdparty does not have $site gateway attached or does not exist.");
1671 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1672 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($request_data[
'site']).
"' ";
1673 $result = $this->
db->query($sql);
1675 if ($result && $this->
db->num_rows($result) !== 0) {
1676 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.");
1680 $obj = $this->
db->fetch_object($result);
1682 $account->fetch($obj->rowid);
1684 foreach ($request_data as $field => $value) {
1685 $account->$field = $value;
1688 if ($account->update(DolibarrApiAccess::$user) < 0) {
1689 throw new RestException(500,
'Error updating SocieteAccount account');
1713 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1714 throw new RestException(401);
1717 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->
db->escape($site).
"'";
1718 $result = $this->
db->query($sql);
1720 if ($result && $this->
db->num_rows($result) == 0) {
1721 throw new RestException(404);
1723 $obj = $this->
db->fetch_object($result);
1725 $account->fetch($obj->rowid);
1727 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1728 throw new RestException(500,
"Error while deleting $site gateway attached to this third party");
1747 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1748 throw new RestException(401);
1755 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1756 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id);
1758 $result = $this->
db->query($sql);
1760 if ($result && $this->
db->num_rows($result) == 0) {
1761 throw new RestException(404,
'This third party does not have any gateway attached or does not exist.');
1765 $num = $this->
db->num_rows($result);
1767 $obj = $this->
db->fetch_object($result);
1769 $account->fetch($obj->rowid);
1771 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1772 throw new RestException(500,
'Error while deleting gateways attached to this third party');
1789 $object = parent::_cleanObjectDatas($object);
1791 unset($object->nom);
1792 unset($object->name_bis);
1793 unset($object->note);
1794 unset($object->departement);
1795 unset($object->departement_code);
1796 unset($object->pays);
1797 unset($object->particulier);
1798 unset($object->prefix_comm);
1800 unset($object->commercial_id);
1802 unset($object->total_ht);
1803 unset($object->total_tva);
1804 unset($object->total_localtax1);
1805 unset($object->total_localtax2);
1806 unset($object->total_ttc);
1808 unset($object->lines);
1809 unset($object->thirdparty);
1811 unset($object->fk_delivery_address);
1813 unset($object->skype);
1814 unset($object->twitter);
1815 unset($object->facebook);
1816 unset($object->linkedin);
1817 unset($object->instagram);
1818 unset($object->snapchat);
1819 unset($object->googleplus);
1820 unset($object->youtube);
1821 unset($object->whatsapp);
1836 $thirdparty = array();
1837 foreach (Thirdparties::$FIELDS as $field) {
1838 if (!isset($data[$field])) {
1839 throw new RestException(400,
"$field field missing");
1841 $thirdparty[$field] = $data[$field];
1867 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
1871 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1872 throw new RestException(401);
1876 $result = $this->company->initAsSpecimen();
1878 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1881 throw new RestException(404,
'Thirdparty not found');
1885 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1888 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1889 $filterabsolutediscount =
"fk_facture_source IS NULL";
1890 $filtercreditnote =
"fk_facture_source IS NOT NULL";
1892 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1893 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1896 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
1897 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
1898 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
1899 $this->company->absolute_creditnote =
price2num($absolute_creditnote,
'MT');