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;
531 return $this->company->delete($id);
553 if (empty($conf->societe->enabled)) {
554 throw new RestException(501,
'Module "Thirdparties" needed for this request');
557 if (empty($conf->product->enabled)) {
558 throw new RestException(501,
'Module "Products" needed for this request');
561 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
562 throw new RestException(501,
'Multiprices features activation needed for this request');
565 if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
566 throw new RestException(400,
'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
569 if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
570 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
573 $result = $this->company->fetch($id);
575 throw new RestException(404,
'Thirdparty '.$id.
' not found');
578 if (empty($result)) {
579 throw new RestException(500,
'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
583 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
586 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
588 throw new RestException(500,
'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
607 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
609 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
610 throw new RestException(401);
613 $result = $this->company->fetch($id);
615 throw new RestException(404,
'Thirdparty not found');
620 $result = $categories->getListForItem($id,
'customer', $sortfield, $sortorder, $limit, $page);
622 if (is_numeric($result) && $result < 0) {
623 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
626 if (is_numeric($result) && $result == 0) {
645 if (!DolibarrApiAccess::$user->rights->societe->creer) {
646 throw new RestException(401);
649 $result = $this->company->fetch($id);
651 throw new RestException(404,
'Thirdparty not found');
654 $result = $category->fetch($category_id);
656 throw new RestException(404,
'category not found');
660 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
663 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
666 $category->add_type($this->company,
'customer');
683 if (!DolibarrApiAccess::$user->rights->societe->creer) {
684 throw new RestException(401);
687 $result = $this->company->fetch($id);
689 throw new RestException(404,
'Thirdparty not found');
692 $result = $category->fetch($category_id);
694 throw new RestException(404,
'category not found');
698 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
701 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
704 $category->del_type($this->company,
'customer');
724 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
725 throw new RestException(401);
728 $result = $this->company->fetch($id);
730 throw new RestException(404,
'Thirdparty not found');
735 $result = $categories->getListForItem($id,
'supplier', $sortfield, $sortorder, $limit, $page);
737 if (is_numeric($result) && $result < 0) {
738 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
741 if (is_numeric($result) && $result == 0) {
760 if (!DolibarrApiAccess::$user->rights->societe->creer) {
761 throw new RestException(401);
764 $result = $this->company->fetch($id);
766 throw new RestException(404,
'Thirdparty not found');
769 $result = $category->fetch($category_id);
771 throw new RestException(404,
'category not found');
775 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
778 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
781 $category->add_type($this->company,
'supplier');
798 if (!DolibarrApiAccess::$user->rights->societe->creer) {
799 throw new RestException(401);
802 $result = $this->company->fetch($id);
804 throw new RestException(404,
'Thirdparty not found');
807 $result = $category->fetch($category_id);
809 throw new RestException(404,
'category not found');
813 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
816 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
819 $category->del_type($this->company,
'supplier');
841 if (!DolibarrApiAccess::$user->rights->societe->lire) {
842 throw new RestException(401);
846 throw new RestException(400,
'Thirdparty ID is mandatory');
850 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
853 $result = $this->company->fetch($id);
855 throw new RestException(404,
'Thirdparty not found');
858 $result = $this->company->getOutstandingProposals($mode);
860 unset($result[
'total_ht']);
861 unset($result[
'total_ttc']);
883 if (!DolibarrApiAccess::$user->rights->societe->lire) {
884 throw new RestException(401);
888 throw new RestException(400,
'Thirdparty ID is mandatory');
892 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
895 $result = $this->company->fetch($id);
897 throw new RestException(404,
'Thirdparty not found');
900 $result = $this->company->getOutstandingOrders($mode);
902 unset($result[
'total_ht']);
903 unset($result[
'total_ttc']);
924 if (!DolibarrApiAccess::$user->rights->societe->lire) {
925 throw new RestException(401);
929 throw new RestException(400,
'Thirdparty ID is mandatory');
933 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
936 $result = $this->company->fetch($id);
938 throw new RestException(404,
'Thirdparty not found');
941 $result = $this->company->getOutstandingBills($mode);
943 unset($result[
'total_ht']);
944 unset($result[
'total_ttc']);
965 if (!DolibarrApiAccess::$user->rights->societe->lire) {
966 throw new RestException(401);
970 throw new RestException(400,
'Thirdparty ID is mandatory');
974 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
977 $result = $this->company->fetch($id);
979 throw new RestException(404,
'Thirdparty not found');
982 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1008 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1009 throw new RestException(401);
1013 throw new RestException(400,
'Thirdparty ID is mandatory');
1017 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1020 $result = $this->company->fetch($id);
1022 throw new RestException(404,
'Thirdparty not found');
1026 $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";
1027 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1028 $sql .=
" WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
1029 if ($filter ==
"available") {
1030 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1032 if ($filter ==
"used") {
1033 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1036 $sql .= $this->
db->order($sortfield, $sortorder);
1038 $result = $this->
db->query($sql);
1040 throw new RestException(503, $this->
db->lasterror());
1042 $num = $this->
db->num_rows($result);
1043 while ($obj = $this->
db->fetch_object($result)) {
1068 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1069 throw new RestException(401);
1072 throw new RestException(400,
'Thirdparty ID is mandatory');
1076 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1085 $result = $invoice->list_replacable_invoices($id);
1087 throw new RestException(405, $this->thirdparty->error);
1111 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1112 throw new RestException(401);
1115 throw new RestException(400,
'Thirdparty ID is mandatory');
1119 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1128 $result = $invoice->list_qualified_avoir_invoices($id);
1130 throw new RestException(405, $this->thirdparty->error);
1147 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1148 throw new RestException(401);
1151 throw new RestException(400,
'Thirdparty ID is mandatory');
1155 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1162 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1163 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1164 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1166 $sql .=
" WHERE fk_soc = ".((int) $id);
1170 $result = $this->
db->query($sql);
1172 if ($result->num_rows == 0) {
1173 throw new RestException(404,
'Account not found');
1178 $accounts = array();
1181 $num = $this->
db->num_rows($result);
1183 $obj = $this->
db->fetch_object($result);
1185 if ($account->fetch($obj->rowid)) {
1186 $accounts[] = $account;
1191 throw new RestException(404,
'Account not found');
1195 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1197 $returnAccounts = array();
1199 foreach ($accounts as $account) {
1201 foreach ($account as $key => $value) {
1202 if (in_array($key, $fields)) {
1203 $object[$key] = $value;
1206 $returnAccounts[] = $object;
1209 return $returnAccounts;
1223 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1224 throw new RestException(401);
1226 if ($this->company->fetch($id) <= 0) {
1227 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1231 $account->socid = $id;
1233 foreach ($request_data as $field => $value) {
1234 $account->$field = $value;
1237 if ($account->create(DolibarrApiAccess::$user) < 0) {
1238 throw new RestException(500,
'Error creating Company Bank account');
1241 if (empty($account->rum)) {
1242 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1244 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1245 $account->date_rum =
dol_now();
1248 if ($account->update(DolibarrApiAccess::$user) < 0) {
1249 throw new RestException(500,
'Error updating values');
1268 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1269 throw new RestException(401);
1271 if ($this->company->fetch($id) <= 0) {
1272 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1276 $account->fetch($bankaccount_id, $id, -1,
'');
1278 if ($account->socid != $id) {
1279 throw new RestException(401);
1283 foreach ($request_data as $field => $value) {
1284 $account->$field = $value;
1287 if (empty($account->rum)) {
1288 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1290 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1291 $account->date_rum =
dol_now();
1294 if ($account->update(DolibarrApiAccess::$user) < 0) {
1295 throw new RestException(500,
'Error updating values');
1313 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1314 throw new RestException(401);
1319 $account->fetch($bankaccount_id);
1321 if (!$account->socid == $id) {
1322 throw new RestException(401);
1325 return $account->delete(DolibarrApiAccess::$user);
1340 global $conf, $langs;
1342 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1344 if ($this->company->fetch($id) <= 0) {
1345 throw new RestException(404,
'Thirdparty not found');
1348 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1349 throw new RestException(401);
1352 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1354 $this->company->fk_bank = $this->company->fk_account;
1356 $outputlangs = $langs;
1360 if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang)) {
1361 if (isset($this->company->thirdparty->default_lang)) {
1362 $newlang = $this->company->thirdparty->default_lang;
1363 } elseif (isset($this->company->default_lang)) {
1364 $newlang = $this->company->default_lang;
1367 if (!empty($newlang)) {
1368 $outputlangs =
new Translate(
"", $conf);
1369 $outputlangs->setDefaultLang($newlang);
1372 $sql =
"SELECT rowid";
1373 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1375 $sql .=
" WHERE fk_soc = ".((int) $id);
1377 if ($companybankid) {
1378 $sql .=
" AND rowid = ".((int) $companybankid);
1382 $accounts = array();
1384 $result = $this->
db->query($sql);
1386 if ($this->
db->num_rows($result) == 0) {
1387 throw new RestException(404,
'Bank account not found');
1390 $num = $this->
db->num_rows($result);
1392 $obj = $this->
db->fetch_object($result);
1395 if ($account->fetch($obj->rowid)) {
1396 $accounts[] = $account;
1401 throw new RestException(500,
'Sql error '.$this->
db->lasterror());
1404 $moreparams = array(
1405 'use_companybankid' => $accounts[0]->
id,
1406 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName($this->company->id)
1409 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1412 return array(
"success" => $result);
1414 throw new RestException(500,
'Error generating the document '.$this->error);
1432 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1433 throw new RestException(401);
1437 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1443 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1444 $sql .=
" WHERE fk_soc = ".((int) $id);
1446 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1449 $result = $this->
db->query($sql);
1451 if ($result && $this->
db->num_rows($result) == 0) {
1452 throw new RestException(404,
'This thirdparty does not have any gateway attached or does not exist.');
1457 $accounts = array();
1459 $num = $this->
db->num_rows($result);
1461 $obj = $this->
db->fetch_object($result);
1464 if ($account->fetch($obj->rowid)) {
1465 $accounts[] = $account;
1470 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1472 $returnAccounts = array();
1474 foreach ($accounts as $account) {
1476 foreach ($account as $key => $value) {
1477 if (in_array($key, $fields)) {
1478 $object[$key] = $value;
1481 $returnAccounts[] = $object;
1484 return $returnAccounts;
1508 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1509 throw new RestException(401);
1512 if (!isset($request_data[
'site'])) {
1513 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1516 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($request_data[
'site']).
"'";
1517 $result = $this->
db->query($sql);
1519 if ($result && $this->
db->num_rows($result) == 0) {
1521 if (!isset($request_data[
'login'])) {
1522 $account->login =
"";
1524 $account->fk_soc = $id;
1526 foreach ($request_data as $field => $value) {
1527 $account->$field = $value;
1530 if ($account->create(DolibarrApiAccess::$user) < 0) {
1531 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1538 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1566 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1567 throw new RestException(401);
1570 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->
db->escape($site).
"'";
1571 $result = $this->
db->query($sql);
1574 if ($result && $this->
db->num_rows == 0) {
1575 if (!isset($request_data[
'key_account'])) {
1576 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1579 if (!isset($request_data[
'login'])) {
1580 $account->login =
"";
1583 foreach ($request_data as $field => $value) {
1584 $account->$field = $value;
1587 $account->fk_soc = $id;
1588 $account->site = $site;
1590 if ($account->create(DolibarrApiAccess::$user) < 0) {
1591 throw new RestException(500,
'Error creating SocieteAccount entity.');
1595 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1596 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($request_data[
'site']).
"' ";
1597 $result = $this->
db->query($sql);
1599 if ($result && $this->
db->num_rows($result) !== 0) {
1600 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.");
1604 $obj = $this->
db->fetch_object($result);
1607 $account->id = $obj->rowid;
1608 $account->fk_soc = $id;
1609 $account->site = $site;
1610 if (!isset($request_data[
'login'])) {
1611 $account->login =
"";
1613 $account->fk_user_creat = $obj->fk_user_creat;
1614 $account->date_creation = $obj->date_creation;
1616 foreach ($request_data as $field => $value) {
1617 $account->$field = $value;
1620 if ($account->update(DolibarrApiAccess::$user) < 0) {
1621 throw new RestException(500,
'Error updating SocieteAccount entity.');
1648 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1649 throw new RestException(401);
1652 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($site).
"'";
1653 $result = $this->
db->query($sql);
1655 if ($result && $this->
db->num_rows($result) == 0) {
1656 throw new RestException(404,
"This thirdparty does not have $site gateway attached or does not exist.");
1659 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1660 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($request_data[
'site']).
"' ";
1661 $result = $this->
db->query($sql);
1663 if ($result && $this->
db->num_rows($result) !== 0) {
1664 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.");
1668 $obj = $this->
db->fetch_object($result);
1670 $account->fetch($obj->rowid);
1672 foreach ($request_data as $field => $value) {
1673 $account->$field = $value;
1676 if ($account->update(DolibarrApiAccess::$user) < 0) {
1677 throw new RestException(500,
'Error updating SocieteAccount account');
1701 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1702 throw new RestException(401);
1705 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->
db->escape($site).
"'";
1706 $result = $this->
db->query($sql);
1708 if ($result && $this->
db->num_rows($result) == 0) {
1709 throw new RestException(404);
1711 $obj = $this->
db->fetch_object($result);
1713 $account->fetch($obj->rowid);
1715 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1716 throw new RestException(500,
"Error while deleting $site gateway attached to this third party");
1735 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1736 throw new RestException(401);
1743 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1744 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id);
1746 $result = $this->
db->query($sql);
1748 if ($result && $this->
db->num_rows($result) == 0) {
1749 throw new RestException(404,
'This third party does not have any gateway attached or does not exist.');
1753 $num = $this->
db->num_rows($result);
1755 $obj = $this->
db->fetch_object($result);
1757 $account->fetch($obj->rowid);
1759 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1760 throw new RestException(500,
'Error while deleting gateways attached to this third party');
1777 $object = parent::_cleanObjectDatas($object);
1779 unset($object->nom);
1780 unset($object->name_bis);
1781 unset($object->note);
1782 unset($object->departement);
1783 unset($object->departement_code);
1784 unset($object->pays);
1785 unset($object->particulier);
1786 unset($object->prefix_comm);
1788 unset($object->commercial_id);
1790 unset($object->total_ht);
1791 unset($object->total_tva);
1792 unset($object->total_localtax1);
1793 unset($object->total_localtax2);
1794 unset($object->total_ttc);
1796 unset($object->lines);
1797 unset($object->thirdparty);
1799 unset($object->fk_delivery_address);
1801 unset($object->skype);
1802 unset($object->twitter);
1803 unset($object->facebook);
1804 unset($object->linkedin);
1805 unset($object->instagram);
1806 unset($object->snapchat);
1807 unset($object->googleplus);
1808 unset($object->youtube);
1809 unset($object->whatsapp);
1824 $thirdparty = array();
1825 foreach (Thirdparties::$FIELDS as $field) {
1826 if (!isset($data[$field])) {
1827 throw new RestException(400,
"$field field missing");
1829 $thirdparty[$field] = $data[$field];
1855 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
1859 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1860 throw new RestException(401);
1864 $result = $this->company->initAsSpecimen();
1866 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1869 throw new RestException(404,
'Thirdparty not found');
1873 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1876 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1877 $filterabsolutediscount =
"fk_facture_source IS NULL";
1878 $filtercreditnote =
"fk_facture_source IS NOT NULL";
1880 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1881 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1884 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
1885 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
1886 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
1887 $this->company->absolute_creditnote =
price2num($absolute_creditnote,
'MT');
__construct()
Constructor.
deleteCategory($id, $category_id)
Remove the link between a customer category and the thirdparty.
_cleanObjectDatas($object)
Clean sensible object datas.
getSupplierCategories($id, $sortfield="s.rowid", $sortorder= 'ASC', $limit=0, $page=0)
Get supplier categories for a thirdparty.
_fetch($rowid, $ref= '', $ref_ext= '', $barcode= '', $idprof1= '', $idprof2= '', $idprof3= '', $idprof4= '', $idprof5= '', $idprof6= '', $email= '', $ref_alias= '')
Fetch properties of a thirdparty object.
$conf db
API class for accounts.
addCategory($id, $category_id)
Add a customer category to a thirdparty.
getSalesRepresentatives($id, $mode=0)
Get representatives of thirdparty.
dol_now($mode= 'auto')
Return date for now.
getOutStandingOrder($id, $mode= 'customer')
Get outstanding orders of thirdparty.
deleteSupplierCategory($id, $category_id)
Remove the link between a category and the thirdparty.
Class to manage bank accounts description of third parties.
_validate($data)
Validate fields before create or update object.
getByEmail($email)
Get properties of a thirdparty object by email.
post($request_data=null)
Create thirdparty object.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters= '')
List thirdparties.
getByBarcode($barcode)
Get properties of a thirdparty object by barcode.
Class for SocieteAccount.
getSocieteAccounts($id, $site=null)
Get a specific gateway attached to a thirdparty (by specifying the site key)
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...
getOutStandingInvoices($id, $mode= 'customer')
Get outstanding invoices of thirdparty.
getFixedAmountDiscounts($id, $filter="none", $sortfield="f.type", $sortorder= 'ASC')
Get fixed amount discount of a thirdparty (all sources: deposit, credit note, commercial offers...
_checkFilters($sqlfilters, &$error= '')
Return if a $sqlfilters parameter is valid.
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update CompanyBankAccount object for thirdparty.
getInvoicesQualifiedForCreditNote($id)
Return list of invoices qualified to be corrected by a credit note.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage categories.
patchSocieteAccount($id, $site, $request_data=null)
Update specified values of a specific gateway attached to a thirdparty.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
putSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site gateway to a thirdparty.
Class to manage withdrawal receipts.
addSupplierCategory($id, $category_id)
Add a supplier category to a thirdparty.
setThirdpartyPriceLevel($id, $priceLevel)
Set new price level for the given thirdparty.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
getCompanyBankAccount($id)
Get CompanyBankAccount objects for thirdparty.
createSocieteAccount($id, $request_data=null)
Create and attach a new gateway to an existing thirdparty.
Class to manage translations.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
createCompanyBankAccount($id, $request_data=null)
Create CompanyBankAccount object for thirdparty.
getOutStandingProposals($id, $mode= 'customer')
Get outstanding proposals of thirdparty.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check access by user to a given resource.
deleteCompanyBankAccount($id, $bankaccount_id)
Delete a bank account attached to a thirdparty.
merge($id, $idtodelete)
Merge a thirdparty into another one.
Class to manage invoices.
generateBankAccountDocument($id, $companybankid=null, $model= 'sepamandate')
Generate a Document from a bank account record (like SEPA mandate)
getInvoicesQualifiedForReplacement($id)
Return list of invoices qualified to be replaced by another invoice.
getCategories($id, $sortfield="s.rowid", $sortorder= 'ASC', $limit=0, $page=0)
Get customer categories for a thirdparty.
put($id, $request_data=null)
Update thirdparty.
deleteSocieteAccounts($id)
Delete all gateways attached to a thirdparty.
deleteSocieteAccount($id, $site)
Delete a specific site gateway attached to a thirdparty (by gateway id)