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";
153 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
156 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
158 if (!in_array($mode, array(1, 2, 3))) {
159 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
162 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
163 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
165 $sql .=
", ".MAIN_DB_PREFIX.
"c_stcomm as st";
166 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
167 $sql .=
" AND t.fk_stcomm = st.id";
170 $sql .=
" AND t.client IN (1, 3)";
171 } elseif ($mode == 2) {
172 $sql .=
" AND t.client IN (2, 3)";
173 } elseif ($mode == 3) {
174 $sql .=
" AND t.client IN (0)";
175 } elseif ($mode == 4) {
176 $sql .=
" AND t.fournisseur IN (1)";
181 if (!empty($mode) && $mode != 4) {
182 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
183 } elseif (!empty($mode) && $mode == 4) {
184 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
186 $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))";
190 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
191 $sql .=
" AND t.rowid = sc.fk_soc";
195 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
197 if ($search_sale > 0) {
198 $sql .=
" AND t.rowid = sc.fk_soc";
201 if ($search_sale > 0) {
202 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
208 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
210 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
211 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
214 $sql .= $this->
db->order($sortfield, $sortorder);
220 $offset = $limit * $page;
222 $sql .= $this->
db->plimit($limit + 1, $offset);
225 $result = $this->
db->query($sql);
227 $num = $this->
db->num_rows($result);
228 $min = min($num, ($limit <= 0 ? $num : $limit));
231 $obj = $this->
db->fetch_object($result);
233 if ($soc_static->fetch($obj->rowid)) {
235 $soc_static->getNoEmail();
242 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->
db->lasterror());
244 if (!count($obj_ret)) {
245 throw new RestException(404,
'Thirdparties not found');
256 public function post($request_data =
null)
258 if (!DolibarrApiAccess::$user->rights->societe->creer) {
259 throw new RestException(401);
262 $result = $this->
_validate($request_data);
264 foreach ($request_data as $field => $value) {
265 $this->company->$field = $value;
267 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
268 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
270 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
271 $this->company->setNoEmail($this->company->no_email);
274 return $this->company->id;
284 public function put($id, $request_data =
null)
286 if (!DolibarrApiAccess::$user->rights->societe->creer) {
287 throw new RestException(401);
290 $result = $this->company->fetch($id);
292 throw new RestException(404,
'Thirdparty not found');
296 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
299 foreach ($request_data as $field => $value) {
300 if ($field ==
'id') {
303 $this->company->$field = $value;
306 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
307 $this->company->setNoEmail($this->company->no_email);
310 if ($this->company->update($id, DolibarrApiAccess::$user, 1,
'',
'',
'update', 1)) {
311 return $this->
get($id);
331 public function merge($id, $idtodelete)
337 if ($id == $idtodelete) {
338 throw new RestException(400,
'Try to merge a thirdparty into itself');
341 if (!DolibarrApiAccess::$user->rights->societe->creer) {
342 throw new RestException(401);
345 $result = $this->company->fetch($id);
347 throw new RestException(404,
'Thirdparty not found');
351 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
354 $this->companytoremove =
new Societe($this->
db);
356 $result = $this->companytoremove->fetch($idtodelete);
358 throw new RestException(404,
'Thirdparty not found');
362 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
365 $soc_origin = $this->companytoremove;
366 $object = $this->company;
367 $user = DolibarrApiAccess::$user;
376 $object->client = $object->client | $soc_origin->client;
377 $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
378 $listofproperties = array(
379 'address',
'zip',
'town',
'state_id',
'country_id',
'phone',
'phone_pro',
'fax',
'email',
'skype',
'url',
'barcode',
380 'idprof1',
'idprof2',
'idprof3',
'idprof4',
'idprof5',
'idprof6',
381 'tva_intra',
'effectif_id',
'forme_juridique',
'remise_percent',
'remise_supplier_percent',
'mode_reglement_supplier_id',
'cond_reglement_supplier_id',
'name_bis',
382 'stcomm_id',
'outstanding_limit',
'price_level',
'parent',
'default_lang',
'ref',
'ref_ext',
'import_key',
'fk_incoterms',
'fk_multicurrency',
383 'code_client',
'code_fournisseur',
'code_compta',
'code_compta_fournisseur',
384 'model_pdf',
'fk_projet'
386 foreach ($listofproperties as $property) {
387 if (empty($object->$property)) {
388 $object->$property = $soc_origin->$property;
393 $listofproperties = array(
394 'note_public',
'note_private'
396 foreach ($listofproperties as $property) {
397 $object->$property =
dol_concatdesc($object->$property, $soc_origin->$property);
401 if (is_array($soc_origin->array_options)) {
402 foreach ($soc_origin->array_options as $key => $val) {
403 if (empty($object->array_options[$key])) {
404 $object->array_options[$key] = $val;
411 $custcats = $static_cat->containing($soc_origin->id,
'customer',
'id');
412 $object->setCategories($custcats,
'customer');
413 $suppcats = $static_cat->containing($soc_origin->id,
'supplier',
'id');
414 $object->setCategories($suppcats,
'supplier');
417 if ($soc_origin->code_client == $object->code_client
418 || $soc_origin->code_fournisseur == $object->code_fournisseur
419 || $soc_origin->barcode == $object->barcode) {
420 dol_syslog(
"We clean customer and supplier code so we will be able to make the update of target");
421 $soc_origin->code_client =
'';
422 $soc_origin->code_fournisseur =
'';
423 $soc_origin->barcode =
'';
424 $soc_origin->update($soc_origin->id, $user, 0, 1, 1,
'merge');
428 $result = $object->update($object->id, $user, 0, 1, 1,
'merge');
438 'Adherent' =>
'/adherents/class/adherent.class.php',
439 'Don' =>
'/don/class/don.class.php',
440 'Societe' =>
'/societe/class/societe.class.php',
442 'ActionComm' =>
'/comm/action/class/actioncomm.class.php',
443 'Propal' =>
'/comm/propal/class/propal.class.php',
444 'Commande' =>
'/commande/class/commande.class.php',
445 'Facture' =>
'/compta/facture/class/facture.class.php',
446 'FactureRec' =>
'/compta/facture/class/facture-rec.class.php',
447 'LignePrelevement' =>
'/compta/prelevement/class/ligneprelevement.class.php',
448 'Mo' =>
'/mrp/class/mo.class.php',
449 'Contact' =>
'/contact/class/contact.class.php',
450 'Contrat' =>
'/contrat/class/contrat.class.php',
451 'Expedition' =>
'/expedition/class/expedition.class.php',
452 'Fichinter' =>
'/fichinter/class/fichinter.class.php',
453 'CommandeFournisseur' =>
'/fourn/class/fournisseur.commande.class.php',
454 'FactureFournisseur' =>
'/fourn/class/fournisseur.facture.class.php',
455 'SupplierProposal' =>
'/supplier_proposal/class/supplier_proposal.class.php',
456 'ProductFournisseur' =>
'/fourn/class/fournisseur.product.class.php',
457 'Delivery' =>
'/delivery/class/delivery.class.php',
458 'Product' =>
'/product/class/product.class.php',
459 'Project' =>
'/projet/class/project.class.php',
460 'Ticket' =>
'/ticket/class/ticket.class.php',
461 'User' =>
'/user/class/user.class.php',
462 'Account' =>
'/compta/bank/class/account.class.php',
463 'ConferenceOrBoothAttendee' =>
'/eventorganization/class/conferenceorboothattendee.class.php'
467 foreach ($objects as $object_name => $object_file) {
468 require_once DOL_DOCUMENT_ROOT.$object_file;
470 if (!$error && !$object_name::replaceThirdparty($this->
db, $soc_origin->id, $object->id)) {
479 $reshook = $hookmanager->executeHooks(
'replaceThirdparty', array(
480 'soc_origin' => $soc_origin->id,
481 'soc_dest' => $object->id
482 ), $soc_dest, $action);
492 $object->context = array(
'merge'=>1,
'mergefromid'=>$soc_origin->id);
495 $result = $object->call_trigger(
'COMPANY_MODIFY', $user);
505 if ($soc_origin->delete($soc_origin->id, $user) < 1) {
513 $this->
db->rollback();
515 throw new RestException(500,
'Error failed to merged thirdparty '.$this->companytoremove->id.
' into '.$id.
'. Enable and read log file for more information.');
520 return $this->
get($id);
529 public function delete($id)
531 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
532 throw new RestException(401);
534 $result = $this->company->fetch($id);
536 throw new RestException(404,
'Thirdparty not found');
539 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
541 $this->company->oldcopy = clone $this->company;
543 $res = $this->company->delete($id);
545 throw new RestException(500,
"Can't delete, error occurs");
546 } elseif ($res == 0) {
547 throw new RestException(409,
"Can't delete, that product is probably used");
553 'message' =>
'Object deleted'
578 throw new RestException(501,
'Module "Thirdparties" needed for this request');
581 if (empty($conf->product->enabled)) {
582 throw new RestException(501,
'Module "Products" needed for this request');
585 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
586 throw new RestException(501,
'Multiprices features activation needed for this request');
589 if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
590 throw new RestException(400,
'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
593 if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
594 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
597 $result = $this->company->fetch($id);
599 throw new RestException(404,
'Thirdparty '.$id.
' not found');
602 if (empty($result)) {
603 throw new RestException(500,
'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
607 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
610 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
612 throw new RestException(500,
'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
631 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
633 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
634 throw new RestException(401);
637 $result = $this->company->fetch($id);
639 throw new RestException(404,
'Thirdparty not found');
644 $result = $categories->getListForItem($id,
'customer', $sortfield, $sortorder, $limit, $page);
646 if (is_numeric($result) && $result < 0) {
647 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
650 if (is_numeric($result) && $result == 0) {
669 if (!DolibarrApiAccess::$user->rights->societe->creer) {
670 throw new RestException(401);
673 $result = $this->company->fetch($id);
675 throw new RestException(404,
'Thirdparty not found');
678 $result = $category->fetch($category_id);
680 throw new RestException(404,
'category not found');
684 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
687 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
690 $category->add_type($this->company,
'customer');
707 if (!DolibarrApiAccess::$user->rights->societe->creer) {
708 throw new RestException(401);
711 $result = $this->company->fetch($id);
713 throw new RestException(404,
'Thirdparty not found');
716 $result = $category->fetch($category_id);
718 throw new RestException(404,
'category not found');
722 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
725 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
728 $category->del_type($this->company,
'customer');
748 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
749 throw new RestException(401);
752 $result = $this->company->fetch($id);
754 throw new RestException(404,
'Thirdparty not found');
759 $result = $categories->getListForItem($id,
'supplier', $sortfield, $sortorder, $limit, $page);
761 if (is_numeric($result) && $result < 0) {
762 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
765 if (is_numeric($result) && $result == 0) {
784 if (!DolibarrApiAccess::$user->rights->societe->creer) {
785 throw new RestException(401);
788 $result = $this->company->fetch($id);
790 throw new RestException(404,
'Thirdparty not found');
793 $result = $category->fetch($category_id);
795 throw new RestException(404,
'category not found');
799 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
802 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
805 $category->add_type($this->company,
'supplier');
822 if (!DolibarrApiAccess::$user->rights->societe->creer) {
823 throw new RestException(401);
826 $result = $this->company->fetch($id);
828 throw new RestException(404,
'Thirdparty not found');
831 $result = $category->fetch($category_id);
833 throw new RestException(404,
'category not found');
837 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
840 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
843 $category->del_type($this->company,
'supplier');
865 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
866 throw new RestException(401);
870 throw new RestException(400,
'Thirdparty ID is mandatory');
874 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
877 $result = $this->company->fetch($id);
879 throw new RestException(404,
'Thirdparty not found');
882 $result = $this->company->getOutstandingProposals($mode);
884 unset($result[
'total_ht']);
885 unset($result[
'total_ttc']);
907 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
908 throw new RestException(401);
912 throw new RestException(400,
'Thirdparty ID is mandatory');
916 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
919 $result = $this->company->fetch($id);
921 throw new RestException(404,
'Thirdparty not found');
924 $result = $this->company->getOutstandingOrders($mode);
926 unset($result[
'total_ht']);
927 unset($result[
'total_ttc']);
948 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
949 throw new RestException(401);
953 throw new RestException(400,
'Thirdparty ID is mandatory');
957 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
960 $result = $this->company->fetch($id);
962 throw new RestException(404,
'Thirdparty not found');
965 $result = $this->company->getOutstandingBills($mode);
967 unset($result[
'total_ht']);
968 unset($result[
'total_ttc']);
989 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
990 throw new RestException(401);
994 throw new RestException(400,
'Thirdparty ID is mandatory');
998 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1001 $result = $this->company->fetch($id);
1003 throw new RestException(404,
'Thirdparty not found');
1006 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1032 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1033 throw new RestException(401);
1037 throw new RestException(400,
'Thirdparty ID is mandatory');
1041 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1044 $result = $this->company->fetch($id);
1046 throw new RestException(404,
'Thirdparty not found');
1050 $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";
1051 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1052 $sql .=
" WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
1053 if ($filter ==
"available") {
1054 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1056 if ($filter ==
"used") {
1057 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1060 $sql .= $this->
db->order($sortfield, $sortorder);
1062 $result = $this->
db->query($sql);
1064 throw new RestException(503, $this->
db->lasterror());
1066 $num = $this->
db->num_rows($result);
1067 while ($obj = $this->
db->fetch_object($result)) {
1092 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1093 throw new RestException(401);
1096 throw new RestException(400,
'Thirdparty ID is mandatory');
1100 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1109 $result = $invoice->list_replacable_invoices($id);
1111 throw new RestException(405, $invoice->error);
1135 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1136 throw new RestException(401);
1139 throw new RestException(400,
'Thirdparty ID is mandatory');
1143 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1152 $result = $invoice->list_qualified_avoir_invoices($id);
1154 throw new RestException(405, $invoice->error);
1171 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1172 throw new RestException(401);
1175 throw new RestException(400,
'Thirdparty ID is mandatory');
1179 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1186 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1187 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1188 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1190 $sql .=
" WHERE fk_soc = ".((int) $id);
1193 $result = $this->
db->query($sql);
1195 if ($this->
db->num_rows($result) == 0) {
1196 throw new RestException(404,
'Account not found');
1201 $accounts = array();
1204 $num = $this->
db->num_rows($result);
1206 $obj = $this->
db->fetch_object($result);
1208 if ($account->fetch($obj->rowid)) {
1209 $accounts[] = $account;
1214 throw new RestException(404,
'Account not found');
1218 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1220 $returnAccounts = array();
1222 foreach ($accounts as $account) {
1224 foreach ($account as $key => $value) {
1225 if (in_array($key, $fields)) {
1226 $object[$key] = $value;
1229 $returnAccounts[] = $object;
1232 return $returnAccounts;
1246 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1247 throw new RestException(401);
1249 if ($this->company->fetch($id) <= 0) {
1250 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1254 $account->socid = $id;
1256 foreach ($request_data as $field => $value) {
1257 $account->$field = $value;
1260 if ($account->create(DolibarrApiAccess::$user) < 0) {
1261 throw new RestException(500,
'Error creating Company Bank account');
1264 if (empty($account->rum)) {
1265 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1267 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1268 $account->date_rum =
dol_now();
1271 if ($account->update(DolibarrApiAccess::$user) < 0) {
1272 throw new RestException(500,
'Error updating values');
1291 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1292 throw new RestException(401);
1294 if ($this->company->fetch($id) <= 0) {
1295 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1299 $account->fetch($bankaccount_id, $id, -1,
'');
1301 if ($account->socid != $id) {
1302 throw new RestException(401);
1306 foreach ($request_data as $field => $value) {
1307 $account->$field = $value;
1310 if (empty($account->rum)) {
1311 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1313 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1314 $account->date_rum =
dol_now();
1317 if ($account->update(DolibarrApiAccess::$user) < 0) {
1318 throw new RestException(500,
'Error updating values');
1336 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1337 throw new RestException(401);
1342 $account->fetch($bankaccount_id);
1344 if (!$account->socid == $id) {
1345 throw new RestException(401);
1348 return $account->delete(DolibarrApiAccess::$user);
1363 global $conf, $langs;
1365 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1367 if ($this->company->fetch($id) <= 0) {
1368 throw new RestException(404,
'Thirdparty not found');
1371 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1372 throw new RestException(401);
1375 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1377 $this->company->fk_bank = $this->company->fk_account;
1379 $outputlangs = $langs;
1384 if (isset($this->company->thirdparty->default_lang)) {
1385 $newlang = $this->company->thirdparty->default_lang;
1386 } elseif (isset($this->company->default_lang)) {
1387 $newlang = $this->company->default_lang;
1390 if (!empty($newlang)) {
1391 $outputlangs =
new Translate(
"", $conf);
1392 $outputlangs->setDefaultLang($newlang);
1395 $sql =
"SELECT rowid";
1396 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1398 $sql .=
" WHERE fk_soc = ".((int) $id);
1400 if ($companybankid) {
1401 $sql .=
" AND rowid = ".((int) $companybankid);
1405 $accounts = array();
1407 $result = $this->
db->query($sql);
1409 if ($this->
db->num_rows($result) == 0) {
1410 throw new RestException(404,
'Bank account not found');
1413 $num = $this->
db->num_rows($result);
1415 $obj = $this->
db->fetch_object($result);
1418 if ($account->fetch($obj->rowid)) {
1419 $accounts[] = $account;
1424 throw new RestException(500,
'Sql error '.$this->
db->lasterror());
1427 $moreparams = array(
1428 'use_companybankid' => $accounts[0]->
id,
1429 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName($this->company->id)
1432 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1435 return array(
"success" => $result);
1437 throw new RestException(500,
'Error generating the document '.$this->company->error);
1455 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1456 throw new RestException(401);
1460 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1466 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1467 $sql .=
" WHERE fk_soc = ".((int) $id);
1469 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1472 $result = $this->
db->query($sql);
1474 if ($result && $this->
db->num_rows($result) == 0) {
1475 throw new RestException(404,
'This thirdparty does not have any gateway attached or does not exist.');
1480 $accounts = array();
1482 $num = $this->
db->num_rows($result);
1484 $obj = $this->
db->fetch_object($result);
1487 if ($account->fetch($obj->rowid)) {
1488 $accounts[] = $account;
1493 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1495 $returnAccounts = array();
1497 foreach ($accounts as $account) {
1499 foreach ($account as $key => $value) {
1500 if (in_array($key, $fields)) {
1501 $object[$key] = $value;
1504 $returnAccounts[] = $object;
1507 return $returnAccounts;
1531 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1532 throw new RestException(401);
1535 if (!isset($request_data[
'site'])) {
1536 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1539 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($request_data[
'site']).
"'";
1540 $result = $this->
db->query($sql);
1542 if ($result && $this->
db->num_rows($result) == 0) {
1544 if (!isset($request_data[
'login'])) {
1545 $account->login =
"";
1547 $account->fk_soc = $id;
1549 foreach ($request_data as $field => $value) {
1550 $account->$field = $value;
1553 if ($account->create(DolibarrApiAccess::$user) < 0) {
1554 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1561 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1589 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1590 throw new RestException(401);
1593 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->
db->escape($site).
"'";
1594 $result = $this->
db->query($sql);
1597 if ($result && $this->
db->num_rows == 0) {
1598 if (!isset($request_data[
'key_account'])) {
1599 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1602 if (!isset($request_data[
'login'])) {
1603 $account->login =
"";
1606 foreach ($request_data as $field => $value) {
1607 $account->$field = $value;
1610 $account->fk_soc = $id;
1611 $account->site = $site;
1613 if ($account->create(DolibarrApiAccess::$user) < 0) {
1614 throw new RestException(500,
'Error creating SocieteAccount entity.');
1618 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1619 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($request_data[
'site']).
"' ";
1620 $result = $this->
db->query($sql);
1622 if ($result && $this->
db->num_rows($result) !== 0) {
1623 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.");
1627 $obj = $this->
db->fetch_object($result);
1630 $account->id = $obj->rowid;
1631 $account->fk_soc = $id;
1632 $account->site = $site;
1633 if (!isset($request_data[
'login'])) {
1634 $account->login =
"";
1636 $account->fk_user_creat = $obj->fk_user_creat;
1637 $account->date_creation = $obj->date_creation;
1639 foreach ($request_data as $field => $value) {
1640 $account->$field = $value;
1643 if ($account->update(DolibarrApiAccess::$user) < 0) {
1644 throw new RestException(500,
'Error updating SocieteAccount entity.');
1671 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1672 throw new RestException(401);
1675 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($site).
"'";
1676 $result = $this->
db->query($sql);
1678 if ($result && $this->
db->num_rows($result) == 0) {
1679 throw new RestException(404,
"This thirdparty does not have $site gateway attached or does not exist.");
1682 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1683 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->
db->escape($request_data[
'site']).
"' ";
1684 $result = $this->
db->query($sql);
1686 if ($result && $this->
db->num_rows($result) !== 0) {
1687 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.");
1691 $obj = $this->
db->fetch_object($result);
1693 $account->fetch($obj->rowid);
1695 foreach ($request_data as $field => $value) {
1696 $account->$field = $value;
1699 if ($account->update(DolibarrApiAccess::$user) < 0) {
1700 throw new RestException(500,
'Error updating SocieteAccount account');
1724 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1725 throw new RestException(401);
1728 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->
db->escape($site).
"'";
1729 $result = $this->
db->query($sql);
1731 if ($result && $this->
db->num_rows($result) == 0) {
1732 throw new RestException(404);
1734 $obj = $this->
db->fetch_object($result);
1736 $account->fetch($obj->rowid);
1738 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1739 throw new RestException(500,
"Error while deleting $site gateway attached to this third party");
1758 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1759 throw new RestException(401);
1766 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1767 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id);
1769 $result = $this->
db->query($sql);
1771 if ($result && $this->
db->num_rows($result) == 0) {
1772 throw new RestException(404,
'This third party does not have any gateway attached or does not exist.');
1776 $num = $this->
db->num_rows($result);
1778 $obj = $this->
db->fetch_object($result);
1780 $account->fetch($obj->rowid);
1782 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1783 throw new RestException(500,
'Error while deleting gateways attached to this third party');
1800 $object = parent::_cleanObjectDatas($object);
1802 unset($object->nom);
1803 unset($object->name_bis);
1804 unset($object->note);
1805 unset($object->departement);
1806 unset($object->departement_code);
1807 unset($object->pays);
1808 unset($object->particulier);
1809 unset($object->prefix_comm);
1811 unset($object->siren);
1812 unset($object->siret);
1813 unset($object->ape);
1815 unset($object->commercial_id);
1817 unset($object->total_ht);
1818 unset($object->total_tva);
1819 unset($object->total_localtax1);
1820 unset($object->total_localtax2);
1821 unset($object->total_ttc);
1823 unset($object->lines);
1824 unset($object->thirdparty);
1826 unset($object->fk_delivery_address);
1828 unset($object->skype);
1829 unset($object->twitter);
1830 unset($object->facebook);
1831 unset($object->linkedin);
1832 unset($object->instagram);
1833 unset($object->snapchat);
1834 unset($object->googleplus);
1835 unset($object->youtube);
1836 unset($object->whatsapp);
1851 $thirdparty = array();
1852 foreach (Thirdparties::$FIELDS as $field) {
1853 if (!isset($data[$field])) {
1854 throw new RestException(400,
"$field field missing");
1856 $thirdparty[$field] = $data[$field];
1882 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
1886 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1887 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
1891 $result = $this->company->initAsSpecimen();
1893 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1896 throw new RestException(404,
'Thirdparty not found');
1900 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
1903 $this->company->getNoEmail();
1906 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1907 $filterabsolutediscount =
"fk_facture_source IS NULL";
1908 $filtercreditnote =
"fk_facture_source IS NOT NULL";
1910 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1911 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1914 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
1915 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
1916 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
1917 $this->company->absolute_creditnote =
price2num($absolute_creditnote,
'MT');
Class to manage withdrawal receipts.
Class to manage categories.
Class to manage bank accounts description of third parties.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Class to manage invoices.
Class for SocieteAccount.
Class to manage third parties objects (customers, suppliers, prospects...)
setThirdpartyPriceLevel($id, $priceLevel)
Set new price level for the given thirdparty.
_cleanObjectDatas($object)
Clean sensible object datas.
getSupplierCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get supplier categories for a thirdparty.
getSocieteAccounts($id, $site=null)
Get a specific gateway attached to a thirdparty (by specifying the site key)
getOutStandingOrder($id, $mode='customer')
Get outstanding orders of thirdparty.
getByBarcode($barcode)
Get properties of a thirdparty object by barcode.
generateBankAccountDocument($id, $companybankid=null, $model='sepamandate')
Generate a Document from a bank account record (like SEPA mandate)
addCategory($id, $category_id)
Add a customer category to a thirdparty.
getCompanyBankAccount($id)
Get CompanyBankAccount objects for thirdparty.
getInvoicesQualifiedForReplacement($id)
Return list of invoices qualified to be replaced by another invoice.
post($request_data=null)
Create thirdparty object.
put($id, $request_data=null)
Update thirdparty.
getByEmail($email)
Get properties of a thirdparty object by email.
_validate($data)
Validate fields before create or update object.
addSupplierCategory($id, $category_id)
Add a supplier category to a thirdparty.
merge($id, $idtodelete)
Merge a thirdparty into another one.
deleteSocieteAccounts($id)
Delete all gateways attached to a thirdparty.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='')
List thirdparties.
__construct()
Constructor.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get customer categories for a thirdparty.
deleteSupplierCategory($id, $category_id)
Remove the link between a category and the thirdparty.
putSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site gateway to a thirdparty.
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update CompanyBankAccount object for thirdparty.
deleteSocieteAccount($id, $site)
Delete a specific site gateway attached to a thirdparty (by gateway id)
getInvoicesQualifiedForCreditNote($id)
Return list of invoices qualified to be corrected by a credit note.
patchSocieteAccount($id, $site, $request_data=null)
Update specified values of a specific gateway attached to a thirdparty.
getFixedAmountDiscounts($id, $filter="none", $sortfield="f.type", $sortorder='ASC')
Get fixed amount discount of a thirdparty (all sources: deposit, credit note, commercial offers....
getOutStandingProposals($id, $mode='customer')
Get outstanding proposals of thirdparty.
_fetch($rowid, $ref='', $ref_ext='', $barcode='', $idprof1='', $idprof2='', $idprof3='', $idprof4='', $idprof5='', $idprof6='', $email='', $ref_alias='')
Fetch properties of a thirdparty object.
getSalesRepresentatives($id, $mode=0)
Get representatives of thirdparty.
getOutStandingInvoices($id, $mode='customer')
Get outstanding invoices of thirdparty.
deleteCompanyBankAccount($id, $bankaccount_id)
Delete a bank account attached to a thirdparty.
createSocieteAccount($id, $request_data=null)
Create and attach a new gateway to an existing thirdparty.
createCompanyBankAccount($id, $request_data=null)
Create CompanyBankAccount object for thirdparty.
deleteCategory($id, $category_id)
Remove the link between a customer category and the thirdparty.
Class to manage translations.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
dol_concatdesc($text1, $text2, $forxml=false, $invert=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.