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->hasRight(
'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);
208 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
212 $sql .= $this->
db->order($sortfield, $sortorder);
218 $offset = $limit * $page;
220 $sql .= $this->
db->plimit($limit + 1, $offset);
223 $result = $this->
db->query($sql);
225 $num = $this->
db->num_rows($result);
226 $min = min($num, ($limit <= 0 ? $num : $limit));
229 $obj = $this->
db->fetch_object($result);
231 if ($soc_static->fetch($obj->rowid)) {
233 $soc_static->getNoEmail();
240 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->
db->lasterror());
242 if (!count($obj_ret)) {
243 throw new RestException(404,
'Thirdparties not found');
254 public function post($request_data =
null)
256 if (!DolibarrApiAccess::$user->rights->societe->creer) {
257 throw new RestException(401);
260 $result = $this->
_validate($request_data);
262 foreach ($request_data as $field => $value) {
263 $this->company->$field = $value;
265 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
266 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
268 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
269 $this->company->setNoEmail($this->company->no_email);
272 return $this->company->id;
282 public function put($id, $request_data =
null)
284 if (!DolibarrApiAccess::$user->rights->societe->creer) {
285 throw new RestException(401);
288 $result = $this->company->fetch($id);
290 throw new RestException(404,
'Thirdparty not found');
294 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
297 foreach ($request_data as $field => $value) {
298 if ($field ==
'id') {
301 $this->company->$field = $value;
304 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
305 $this->company->setNoEmail($this->company->no_email);
308 if ($this->company->update($id, DolibarrApiAccess::$user, 1,
'',
'',
'update', 1)) {
309 return $this->
get($id);
329 public function merge($id, $idtodelete)
335 if ($id == $idtodelete) {
336 throw new RestException(400,
'Try to merge a thirdparty into itself');
339 if (!DolibarrApiAccess::$user->rights->societe->creer) {
340 throw new RestException(401);
343 $result = $this->company->fetch($id);
345 throw new RestException(404,
'Thirdparty not found');
349 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
352 $this->companytoremove =
new Societe($this->
db);
354 $result = $this->companytoremove->fetch($idtodelete);
356 throw new RestException(404,
'Thirdparty not found');
360 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
363 $soc_origin = $this->companytoremove;
364 $object = $this->company;
365 $user = DolibarrApiAccess::$user;
374 $object->client = $object->client | $soc_origin->client;
375 $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
376 $listofproperties = array(
377 'address',
'zip',
'town',
'state_id',
'country_id',
'phone',
'phone_pro',
'fax',
'email',
'skype',
'url',
'barcode',
378 'idprof1',
'idprof2',
'idprof3',
'idprof4',
'idprof5',
'idprof6',
379 'tva_intra',
'effectif_id',
'forme_juridique',
'remise_percent',
'remise_supplier_percent',
'mode_reglement_supplier_id',
'cond_reglement_supplier_id',
'name_bis',
380 'stcomm_id',
'outstanding_limit',
'price_level',
'parent',
'default_lang',
'ref',
'ref_ext',
'import_key',
'fk_incoterms',
'fk_multicurrency',
381 'code_client',
'code_fournisseur',
'code_compta',
'code_compta_fournisseur',
382 'model_pdf',
'fk_projet'
384 foreach ($listofproperties as $property) {
385 if (empty($object->$property)) {
386 $object->$property = $soc_origin->$property;
391 $listofproperties = array(
392 'note_public',
'note_private'
394 foreach ($listofproperties as $property) {
395 $object->$property =
dol_concatdesc($object->$property, $soc_origin->$property);
399 if (is_array($soc_origin->array_options)) {
400 foreach ($soc_origin->array_options as $key => $val) {
401 if (empty($object->array_options[$key])) {
402 $object->array_options[$key] = $val;
409 $custcats = $static_cat->containing($soc_origin->id,
'customer',
'id');
410 $object->setCategories($custcats,
'customer');
411 $suppcats = $static_cat->containing($soc_origin->id,
'supplier',
'id');
412 $object->setCategories($suppcats,
'supplier');
415 if ($soc_origin->code_client == $object->code_client
416 || $soc_origin->code_fournisseur == $object->code_fournisseur
417 || $soc_origin->barcode == $object->barcode) {
418 dol_syslog(
"We clean customer and supplier code so we will be able to make the update of target");
419 $soc_origin->code_client =
'';
420 $soc_origin->code_fournisseur =
'';
421 $soc_origin->barcode =
'';
422 $soc_origin->update($soc_origin->id, $user, 0, 1, 1,
'merge');
426 $result = $object->update($object->id, $user, 0, 1, 1,
'merge');
436 'Adherent' =>
'/adherents/class/adherent.class.php',
437 'Don' =>
'/don/class/don.class.php',
438 'Societe' =>
'/societe/class/societe.class.php',
440 'ActionComm' =>
'/comm/action/class/actioncomm.class.php',
441 'Propal' =>
'/comm/propal/class/propal.class.php',
442 'Commande' =>
'/commande/class/commande.class.php',
443 'Facture' =>
'/compta/facture/class/facture.class.php',
444 'FactureRec' =>
'/compta/facture/class/facture-rec.class.php',
445 'LignePrelevement' =>
'/compta/prelevement/class/ligneprelevement.class.php',
446 'Mo' =>
'/mrp/class/mo.class.php',
447 'Contact' =>
'/contact/class/contact.class.php',
448 'Contrat' =>
'/contrat/class/contrat.class.php',
449 'Expedition' =>
'/expedition/class/expedition.class.php',
450 'Fichinter' =>
'/fichinter/class/fichinter.class.php',
451 'CommandeFournisseur' =>
'/fourn/class/fournisseur.commande.class.php',
452 'FactureFournisseur' =>
'/fourn/class/fournisseur.facture.class.php',
453 'SupplierProposal' =>
'/supplier_proposal/class/supplier_proposal.class.php',
454 'ProductFournisseur' =>
'/fourn/class/fournisseur.product.class.php',
455 'Delivery' =>
'/delivery/class/delivery.class.php',
456 'Product' =>
'/product/class/product.class.php',
457 'Project' =>
'/projet/class/project.class.php',
458 'Ticket' =>
'/ticket/class/ticket.class.php',
459 'User' =>
'/user/class/user.class.php',
460 'Account' =>
'/compta/bank/class/account.class.php',
461 'ConferenceOrBoothAttendee' =>
'/eventorganization/class/conferenceorboothattendee.class.php'
465 foreach ($objects as $object_name => $object_file) {
466 require_once DOL_DOCUMENT_ROOT.$object_file;
468 if (!$error && !$object_name::replaceThirdparty($this->
db, $soc_origin->id, $object->id)) {
477 $reshook = $hookmanager->executeHooks(
'replaceThirdparty', array(
478 'soc_origin' => $soc_origin->id,
479 'soc_dest' => $object->id
480 ), $soc_dest, $action);
490 $object->context = array(
'merge'=>1,
'mergefromid'=>$soc_origin->id);
493 $result = $object->call_trigger(
'COMPANY_MODIFY', $user);
503 if ($soc_origin->delete($soc_origin->id, $user) < 1) {
511 $this->
db->rollback();
513 throw new RestException(500,
'Error failed to merged thirdparty '.$this->companytoremove->id.
' into '.$id.
'. Enable and read log file for more information.');
518 return $this->
get($id);
527 public function delete($id)
529 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
530 throw new RestException(401);
532 $result = $this->company->fetch($id);
534 throw new RestException(404,
'Thirdparty not found');
537 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
539 $this->company->oldcopy = clone $this->company;
541 $res = $this->company->delete($id);
543 throw new RestException(500,
"Can't delete, error occurs");
544 } elseif ($res == 0) {
545 throw new RestException(409,
"Can't delete, that product is probably used");
551 'message' =>
'Object deleted'
576 throw new RestException(501,
'Module "Thirdparties" needed for this request');
579 if (empty($conf->product->enabled)) {
580 throw new RestException(501,
'Module "Products" needed for this request');
583 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
584 throw new RestException(501,
'Multiprices features activation needed for this request');
587 if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
588 throw new RestException(400,
'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
591 if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
592 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
595 $result = $this->company->fetch($id);
597 throw new RestException(404,
'Thirdparty '.$id.
' not found');
600 if (empty($result)) {
601 throw new RestException(500,
'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
605 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
608 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
610 throw new RestException(500,
'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
629 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
631 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
632 throw new RestException(401);
635 $result = $this->company->fetch($id);
637 throw new RestException(404,
'Thirdparty not found');
642 $result = $categories->getListForItem($id,
'customer', $sortfield, $sortorder, $limit, $page);
644 if (is_numeric($result) && $result < 0) {
645 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
648 if (is_numeric($result) && $result == 0) {
667 if (!DolibarrApiAccess::$user->rights->societe->creer) {
668 throw new RestException(401);
671 $result = $this->company->fetch($id);
673 throw new RestException(404,
'Thirdparty not found');
676 $result = $category->fetch($category_id);
678 throw new RestException(404,
'category not found');
682 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
685 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
688 $category->add_type($this->company,
'customer');
705 if (!DolibarrApiAccess::$user->rights->societe->creer) {
706 throw new RestException(401);
709 $result = $this->company->fetch($id);
711 throw new RestException(404,
'Thirdparty not found');
714 $result = $category->fetch($category_id);
716 throw new RestException(404,
'category not found');
720 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
723 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
726 $category->del_type($this->company,
'customer');
746 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
747 throw new RestException(401);
750 $result = $this->company->fetch($id);
752 throw new RestException(404,
'Thirdparty not found');
757 $result = $categories->getListForItem($id,
'supplier', $sortfield, $sortorder, $limit, $page);
759 if (is_numeric($result) && $result < 0) {
760 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
763 if (is_numeric($result) && $result == 0) {
782 if (!DolibarrApiAccess::$user->rights->societe->creer) {
783 throw new RestException(401);
786 $result = $this->company->fetch($id);
788 throw new RestException(404,
'Thirdparty not found');
791 $result = $category->fetch($category_id);
793 throw new RestException(404,
'category not found');
797 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
800 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
803 $category->add_type($this->company,
'supplier');
820 if (!DolibarrApiAccess::$user->rights->societe->creer) {
821 throw new RestException(401);
824 $result = $this->company->fetch($id);
826 throw new RestException(404,
'Thirdparty not found');
829 $result = $category->fetch($category_id);
831 throw new RestException(404,
'category not found');
835 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
838 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
841 $category->del_type($this->company,
'supplier');
863 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
864 throw new RestException(401);
868 throw new RestException(400,
'Thirdparty ID is mandatory');
872 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
875 $result = $this->company->fetch($id);
877 throw new RestException(404,
'Thirdparty not found');
880 $result = $this->company->getOutstandingProposals($mode);
882 unset($result[
'total_ht']);
883 unset($result[
'total_ttc']);
905 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
906 throw new RestException(401);
910 throw new RestException(400,
'Thirdparty ID is mandatory');
914 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
917 $result = $this->company->fetch($id);
919 throw new RestException(404,
'Thirdparty not found');
922 $result = $this->company->getOutstandingOrders($mode);
924 unset($result[
'total_ht']);
925 unset($result[
'total_ttc']);
946 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
947 throw new RestException(401);
951 throw new RestException(400,
'Thirdparty ID is mandatory');
955 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
958 $result = $this->company->fetch($id);
960 throw new RestException(404,
'Thirdparty not found');
963 $result = $this->company->getOutstandingBills($mode);
965 unset($result[
'total_ht']);
966 unset($result[
'total_ttc']);
987 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
988 throw new RestException(401);
992 throw new RestException(400,
'Thirdparty ID is mandatory');
996 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
999 $result = $this->company->fetch($id);
1001 throw new RestException(404,
'Thirdparty not found');
1004 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1030 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1031 throw new RestException(401);
1035 throw new RestException(400,
'Thirdparty ID is mandatory');
1039 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1042 $result = $this->company->fetch($id);
1044 throw new RestException(404,
'Thirdparty not found');
1048 $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";
1049 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1050 $sql .=
" WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
1051 if ($filter ==
"available") {
1052 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1054 if ($filter ==
"used") {
1055 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1058 $sql .= $this->
db->order($sortfield, $sortorder);
1060 $result = $this->
db->query($sql);
1062 throw new RestException(503, $this->
db->lasterror());
1064 $num = $this->
db->num_rows($result);
1065 while ($obj = $this->
db->fetch_object($result)) {
1090 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1091 throw new RestException(401);
1094 throw new RestException(400,
'Thirdparty ID is mandatory');
1098 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1107 $result = $invoice->list_replacable_invoices($id);
1109 throw new RestException(405, $invoice->error);
1133 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1134 throw new RestException(401);
1137 throw new RestException(400,
'Thirdparty ID is mandatory');
1141 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1150 $result = $invoice->list_qualified_avoir_invoices($id);
1152 throw new RestException(405, $invoice->error);
1169 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1170 throw new RestException(401);
1173 throw new RestException(400,
'Thirdparty ID is mandatory');
1177 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1184 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1185 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1186 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1188 $sql .=
" WHERE fk_soc = ".((int) $id);
1191 $result = $this->
db->query($sql);
1193 if ($this->
db->num_rows($result) == 0) {
1194 throw new RestException(404,
'Account not found');
1199 $accounts = array();
1202 $num = $this->
db->num_rows($result);
1204 $obj = $this->
db->fetch_object($result);
1206 if ($account->fetch($obj->rowid)) {
1207 $accounts[] = $account;
1212 throw new RestException(404,
'Account not found');
1216 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1218 $returnAccounts = array();
1220 foreach ($accounts as $account) {
1222 foreach ($account as $key => $value) {
1223 if (in_array($key, $fields)) {
1224 $object[$key] = $value;
1227 $returnAccounts[] = $object;
1230 return $returnAccounts;
1244 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1245 throw new RestException(401);
1247 if ($this->company->fetch($id) <= 0) {
1248 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1252 $account->socid = $id;
1254 foreach ($request_data as $field => $value) {
1255 $account->$field = $value;
1258 if ($account->create(DolibarrApiAccess::$user) < 0) {
1259 throw new RestException(500,
'Error creating Company Bank account');
1262 if (empty($account->rum)) {
1263 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1265 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1266 $account->date_rum =
dol_now();
1269 if ($account->update(DolibarrApiAccess::$user) < 0) {
1270 throw new RestException(500,
'Error updating values');
1289 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1290 throw new RestException(401);
1292 if ($this->company->fetch($id) <= 0) {
1293 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1297 $account->fetch($bankaccount_id, $id, -1,
'');
1299 if ($account->socid != $id) {
1300 throw new RestException(401);
1304 foreach ($request_data as $field => $value) {
1305 $account->$field = $value;
1308 if (empty($account->rum)) {
1309 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1311 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1312 $account->date_rum =
dol_now();
1315 if ($account->update(DolibarrApiAccess::$user) < 0) {
1316 throw new RestException(500,
'Error updating values');
1334 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1335 throw new RestException(401);
1340 $account->fetch($bankaccount_id);
1342 if (!$account->socid == $id) {
1343 throw new RestException(401);
1346 return $account->delete(DolibarrApiAccess::$user);
1361 global $conf, $langs;
1363 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1365 if ($this->company->fetch($id) <= 0) {
1366 throw new RestException(404,
'Thirdparty not found');
1369 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1370 throw new RestException(401);
1373 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1375 $this->company->fk_bank = $this->company->fk_account;
1377 $outputlangs = $langs;
1382 if (isset($this->company->thirdparty->default_lang)) {
1383 $newlang = $this->company->thirdparty->default_lang;
1384 } elseif (isset($this->company->default_lang)) {
1385 $newlang = $this->company->default_lang;
1388 if (!empty($newlang)) {
1389 $outputlangs =
new Translate(
"", $conf);
1390 $outputlangs->setDefaultLang($newlang);
1393 $sql =
"SELECT rowid";
1394 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1396 $sql .=
" WHERE fk_soc = ".((int) $id);
1398 if ($companybankid) {
1399 $sql .=
" AND rowid = ".((int) $companybankid);
1403 $accounts = array();
1405 $result = $this->
db->query($sql);
1407 if ($this->
db->num_rows($result) == 0) {
1408 throw new RestException(404,
'Bank account not found');
1411 $num = $this->
db->num_rows($result);
1413 $obj = $this->
db->fetch_object($result);
1416 if ($account->fetch($obj->rowid)) {
1417 $accounts[] = $account;
1422 throw new RestException(500,
'Sql error '.$this->
db->lasterror());
1425 $moreparams = array(
1426 'use_companybankid' => $accounts[0]->
id,
1427 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName($this->company->id)
1430 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1433 return array(
"success" => $result);
1435 throw new RestException(500,
'Error generating the document '.$this->company->error);
1453 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1454 throw new RestException(401);
1458 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1464 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1465 $sql .=
" WHERE fk_soc = ".((int) $id);
1467 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1470 $result = $this->
db->query($sql);
1472 if ($result && $this->
db->num_rows($result) == 0) {
1473 throw new RestException(404,
'This thirdparty does not have any gateway attached or does not exist.');
1478 $accounts = array();
1480 $num = $this->
db->num_rows($result);
1482 $obj = $this->
db->fetch_object($result);
1485 if ($account->fetch($obj->rowid)) {
1486 $accounts[] = $account;
1491 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1493 $returnAccounts = array();
1495 foreach ($accounts as $account) {
1497 foreach ($account as $key => $value) {
1498 if (in_array($key, $fields)) {
1499 $object[$key] = $value;
1502 $returnAccounts[] = $object;
1505 return $returnAccounts;
1529 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1530 throw new RestException(401);
1533 if (!isset($request_data[
'site'])) {
1534 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1537 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($request_data[
'site']).
"'";
1538 $result = $this->
db->query($sql);
1540 if ($result && $this->
db->num_rows($result) == 0) {
1542 if (!isset($request_data[
'login'])) {
1543 $account->login =
"";
1545 $account->fk_soc = $id;
1547 foreach ($request_data as $field => $value) {
1548 $account->$field = $value;
1551 if ($account->create(DolibarrApiAccess::$user) < 0) {
1552 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1559 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1587 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1588 throw new RestException(401);
1591 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->
db->escape($site).
"'";
1592 $result = $this->
db->query($sql);
1595 if ($result && $this->
db->num_rows == 0) {
1596 if (!isset($request_data[
'key_account'])) {
1597 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1600 if (!isset($request_data[
'login'])) {
1601 $account->login =
"";
1604 foreach ($request_data as $field => $value) {
1605 $account->$field = $value;
1608 $account->fk_soc = $id;
1609 $account->site = $site;
1611 if ($account->create(DolibarrApiAccess::$user) < 0) {
1612 throw new RestException(500,
'Error creating SocieteAccount entity.');
1616 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1617 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($request_data[
'site']).
"' ";
1618 $result = $this->
db->query($sql);
1620 if ($result && $this->
db->num_rows($result) !== 0) {
1621 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.");
1625 $obj = $this->
db->fetch_object($result);
1628 $account->id = $obj->rowid;
1629 $account->fk_soc = $id;
1630 $account->site = $site;
1631 if (!isset($request_data[
'login'])) {
1632 $account->login =
"";
1634 $account->fk_user_creat = $obj->fk_user_creat;
1635 $account->date_creation = $obj->date_creation;
1637 foreach ($request_data as $field => $value) {
1638 $account->$field = $value;
1641 if ($account->update(DolibarrApiAccess::$user) < 0) {
1642 throw new RestException(500,
'Error updating SocieteAccount entity.');
1669 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1670 throw new RestException(401);
1673 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($site).
"'";
1674 $result = $this->
db->query($sql);
1676 if ($result && $this->
db->num_rows($result) == 0) {
1677 throw new RestException(404,
"This thirdparty does not have $site gateway attached or does not exist.");
1680 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1681 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($request_data[
'site']).
"' ";
1682 $result = $this->
db->query($sql);
1684 if ($result && $this->
db->num_rows($result) !== 0) {
1685 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.");
1689 $obj = $this->
db->fetch_object($result);
1691 $account->fetch($obj->rowid);
1693 foreach ($request_data as $field => $value) {
1694 $account->$field = $value;
1697 if ($account->update(DolibarrApiAccess::$user) < 0) {
1698 throw new RestException(500,
'Error updating SocieteAccount account');
1722 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1723 throw new RestException(401);
1726 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->
db->escape($site).
"'";
1727 $result = $this->
db->query($sql);
1729 if ($result && $this->
db->num_rows($result) == 0) {
1730 throw new RestException(404);
1732 $obj = $this->
db->fetch_object($result);
1734 $account->fetch($obj->rowid);
1736 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1737 throw new RestException(500,
"Error while deleting $site gateway attached to this third party");
1756 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1757 throw new RestException(401);
1764 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1765 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id);
1767 $result = $this->
db->query($sql);
1769 if ($result && $this->
db->num_rows($result) == 0) {
1770 throw new RestException(404,
'This third party does not have any gateway attached or does not exist.');
1774 $num = $this->
db->num_rows($result);
1776 $obj = $this->
db->fetch_object($result);
1778 $account->fetch($obj->rowid);
1780 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1781 throw new RestException(500,
'Error while deleting gateways attached to this third party');
1798 $object = parent::_cleanObjectDatas($object);
1800 unset($object->nom);
1801 unset($object->name_bis);
1802 unset($object->note);
1803 unset($object->departement);
1804 unset($object->departement_code);
1805 unset($object->pays);
1806 unset($object->particulier);
1807 unset($object->prefix_comm);
1809 unset($object->siren);
1810 unset($object->siret);
1811 unset($object->ape);
1813 unset($object->commercial_id);
1815 unset($object->total_ht);
1816 unset($object->total_tva);
1817 unset($object->total_localtax1);
1818 unset($object->total_localtax2);
1819 unset($object->total_ttc);
1821 unset($object->lines);
1822 unset($object->thirdparty);
1824 unset($object->fk_delivery_address);
1826 unset($object->skype);
1827 unset($object->twitter);
1828 unset($object->facebook);
1829 unset($object->linkedin);
1830 unset($object->instagram);
1831 unset($object->snapchat);
1832 unset($object->googleplus);
1833 unset($object->youtube);
1834 unset($object->whatsapp);
1849 $thirdparty = array();
1850 foreach (Thirdparties::$FIELDS as $field) {
1851 if (!isset($data[$field])) {
1852 throw new RestException(400,
"$field field missing");
1854 $thirdparty[$field] = $data[$field];
1880 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
1884 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1885 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
1889 $result = $this->company->initAsSpecimen();
1891 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1894 throw new RestException(404,
'Thirdparty not found');
1898 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
1901 $this->company->getNoEmail();
1904 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1905 $filterabsolutediscount =
"fk_facture_source IS NULL";
1906 $filtercreditnote =
"fk_facture_source IS NOT NULL";
1908 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1909 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1912 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
1913 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
1914 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
1915 $this->company->absolute_creditnote =
price2num($absolute_creditnote,
'MT');