21 use Luracast\Restler\RestException;
36 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 =
'')
136 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
140 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
142 $sql =
"SELECT t.rowid";
143 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", sc.fk_soc, sc.fk_user";
144 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
146 if ($mode != 4) $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
147 if (!in_array($mode, array(1, 2, 3))) $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
149 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
150 $sql .=
", ".MAIN_DB_PREFIX.
"c_stcomm as st";
151 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
152 $sql .=
" AND t.fk_stcomm = st.id";
154 if ($mode == 1) $sql .=
" AND t.client IN (1, 3)";
155 elseif ($mode == 2) $sql .=
" AND t.client IN (2, 3)";
156 elseif ($mode == 3) $sql .=
" AND t.client IN (0)";
157 elseif ($mode == 4) $sql .=
" AND t.fournisseur IN (1)";
161 if (!empty($mode) && $mode != 4) { $sql .=
" AND c.fk_categorie = ".$this->db->escape($category).
" AND c.fk_soc = t.rowid"; }
162 elseif (!empty($mode) && $mode == 4) { $sql .=
" AND cc.fk_categorie = ".$this->db->escape($category).
" AND cc.fk_soc = t.rowid"; }
163 else { $sql .=
" AND ((c.fk_categorie = ".$this->db->escape($category).
" AND c.fk_soc = t.rowid) OR (cc.fk_categorie = ".$this->
db->escape($category).
" AND cc.fk_soc = t.rowid))"; }
166 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
" AND t.rowid = sc.fk_soc";
168 if ($socids) $sql .=
" AND t.rowid IN (".$socids.
")";
169 if ($search_sale > 0) $sql .=
" AND t.rowid = sc.fk_soc";
171 if ($search_sale > 0)
173 $sql .=
" AND sc.fk_user = ".$search_sale;
180 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
182 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
183 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
186 $sql .= $this->
db->order($sortfield, $sortorder);
192 $offset = $limit * $page;
194 $sql .= $this->
db->plimit($limit + 1, $offset);
197 $result = $this->
db->query($sql);
200 $num = $this->
db->num_rows($result);
201 $min = min($num, ($limit <= 0 ? $num : $limit));
205 $obj = $this->
db->fetch_object($result);
207 if ($soc_static->fetch($obj->rowid)) {
213 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->
db->lasterror());
215 if (!count($obj_ret)) {
216 throw new RestException(404,
'Thirdparties not found');
227 public function post($request_data = null)
229 if (!DolibarrApiAccess::$user->rights->societe->creer) {
230 throw new RestException(401);
233 $result = $this->
_validate($request_data);
235 foreach ($request_data as $field => $value) {
236 $this->company->$field = $value;
238 if ($this->company->create(DolibarrApiAccess::$user) < 0)
239 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
241 return $this->company->id;
251 public function put($id, $request_data = null)
253 if (!DolibarrApiAccess::$user->rights->societe->creer) {
254 throw new RestException(401);
257 $result = $this->company->fetch($id);
259 throw new RestException(404,
'Thirdparty not found');
263 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
266 foreach ($request_data as $field => $value) {
267 if ($field ==
'id')
continue;
268 $this->company->$field = $value;
271 if ($this->company->update($id, DolibarrApiAccess::$user, 1,
'',
'',
'update')) {
272 return $this->
get($id);
292 public function merge($id, $idtodelete)
298 if ($id == $idtodelete)
300 throw new RestException(400,
'Try to merge a thirdparty into itself');
303 if (!DolibarrApiAccess::$user->rights->societe->creer) {
304 throw new RestException(401);
307 $result = $this->company->fetch($id);
309 throw new RestException(404,
'Thirdparty not found');
313 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
316 $this->companytoremove =
new Societe($this->
db);
318 $result = $this->companytoremove->fetch($idtodelete);
320 throw new RestException(404,
'Thirdparty not found');
324 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
327 $soc_origin = $this->companytoremove;
328 $object = $this->company;
329 $user = DolibarrApiAccess::$user;
338 $object->client = $object->client | $soc_origin->client;
339 $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
340 $listofproperties = array(
341 'address',
'zip',
'town',
'state_id',
'country_id',
'phone',
'phone_pro',
'fax',
'email',
'skype',
'url',
'barcode',
342 'idprof1',
'idprof2',
'idprof3',
'idprof4',
'idprof5',
'idprof6',
343 'tva_intra',
'effectif_id',
'forme_juridique',
'remise_percent',
'remise_supplier_percent',
'mode_reglement_supplier_id',
'cond_reglement_supplier_id',
'name_bis',
344 'stcomm_id',
'outstanding_limit',
'price_level',
'parent',
'default_lang',
'ref',
'ref_ext',
'import_key',
'fk_incoterms',
'fk_multicurrency',
345 'code_client',
'code_fournisseur',
'code_compta',
'code_compta_fournisseur',
346 'model_pdf',
'fk_projet'
348 foreach ($listofproperties as $property)
350 if (empty($object->$property)) $object->$property = $soc_origin->$property;
354 $listofproperties = array(
355 'note_public',
'note_private'
357 foreach ($listofproperties as $property)
359 $object->$property =
dol_concatdesc($object->$property, $soc_origin->$property);
363 if (is_array($soc_origin->array_options))
365 foreach ($soc_origin->array_options as $key => $val)
367 if (empty($object->array_options[$key])) $object->array_options[$key] = $val;
373 $custcats = $static_cat->containing($soc_origin->id,
'customer',
'id');
374 $object->setCategories($custcats,
'customer');
375 $suppcats = $static_cat->containing($soc_origin->id,
'supplier',
'id');
376 $object->setCategories($suppcats,
'supplier');
379 if ($soc_origin->code_client == $object->code_client
380 || $soc_origin->code_fournisseur == $object->code_fournisseur
381 || $soc_origin->barcode == $object->barcode)
383 dol_syslog(
"We clean customer and supplier code so we will be able to make the update of target");
384 $soc_origin->code_client =
'';
385 $soc_origin->code_fournisseur =
'';
386 $soc_origin->barcode =
'';
387 $soc_origin->update($soc_origin->id, $user, 0, 1, 1,
'merge');
391 $result = $object->update($object->id, $user, 0, 1, 1,
'merge');
402 'Adherent' =>
'/adherents/class/adherent.class.php',
403 'Societe' =>
'/societe/class/societe.class.php',
404 'Categorie' =>
'/categories/class/categorie.class.php',
405 'ActionComm' =>
'/comm/action/class/actioncomm.class.php',
406 'Propal' =>
'/comm/propal/class/propal.class.php',
407 'Commande' =>
'/commande/class/commande.class.php',
408 'Facture' =>
'/compta/facture/class/facture.class.php',
409 'FactureRec' =>
'/compta/facture/class/facture-rec.class.php',
410 'LignePrelevement' =>
'/compta/prelevement/class/ligneprelevement.class.php',
411 'Mo' =>
'/mrp/class/mo.class.php',
412 'Contact' =>
'/contact/class/contact.class.php',
413 'Contrat' =>
'/contrat/class/contrat.class.php',
414 'Expedition' =>
'/expedition/class/expedition.class.php',
415 'Fichinter' =>
'/fichinter/class/fichinter.class.php',
416 'CommandeFournisseur' =>
'/fourn/class/fournisseur.commande.class.php',
417 'FactureFournisseur' =>
'/fourn/class/fournisseur.facture.class.php',
418 'SupplierProposal' =>
'/supplier_proposal/class/supplier_proposal.class.php',
419 'ProductFournisseur' =>
'/fourn/class/fournisseur.product.class.php',
420 'Livraison' =>
'/delivery/class/delivery.class.php',
421 'Product' =>
'/product/class/product.class.php',
422 'Project' =>
'/projet/class/project.class.php',
423 'Ticket' =>
'/ticket/class/ticket.class.php',
424 'User' =>
'/user/class/user.class.php'
428 foreach ($objects as $object_name => $object_file)
430 require_once DOL_DOCUMENT_ROOT.$object_file;
432 if (!$error && !$object_name::replaceThirdparty($this->
db, $soc_origin->id, $object->id)) {
441 $reshook = $hookmanager->executeHooks(
'replaceThirdparty', array(
442 'soc_origin' => $soc_origin->id,
443 'soc_dest' => $object->id
444 ), $soc_dest, $action);
454 $object->context = array(
'merge'=>1,
'mergefromid'=>$soc_origin->id);
457 $result = $object->call_trigger(
'COMPANY_MODIFY', $user);
467 if ($soc_origin->delete($soc_origin->id, $user) < 1) {
475 $this->
db->rollback();
477 throw new RestException(500,
'Error failed to merged thirdparty '.$this->companytoremove->id.
' into '.$id.
'. Enable and read log file for more information.');
482 return $this->
get($id);
491 public function delete($id)
493 if (!DolibarrApiAccess::$user->rights->societe->supprimer) {
494 throw new RestException(401);
496 $result = $this->company->fetch($id);
498 throw new RestException(404,
'Thirdparty not found');
501 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
503 $this->company->oldcopy = clone $this->company;
504 return $this->company->delete($id);
526 if (empty($conf->societe->enabled)) {
527 throw new RestException(501,
'Module "Thirdparties" needed for this request');
530 if (empty($conf->product->enabled)) {
531 throw new RestException(501,
'Module "Products" needed for this request');
534 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
535 throw new RestException(501,
'Multiprices features activation needed for this request');
538 if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
539 throw new RestException(400,
'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
542 if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
543 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
546 $result = $this->company->fetch($id);
548 throw new RestException(404,
'Thirdparty '.$id.
' not found');
551 if (empty($result)) {
552 throw new RestException(500,
'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
556 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
559 $result = $this->company->set_price_level($priceLevel, DolibarrApiAccess::$user);
561 throw new RestException(500,
'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
580 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
582 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
583 throw new RestException(401);
586 $result = $this->company->fetch($id);
589 throw new RestException(404,
'Thirdparty not found');
594 $result = $categories->getListForItem($id,
'customer', $sortfield, $sortorder, $limit, $page);
596 if (is_numeric($result) && $result < 0)
598 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
601 if (is_numeric($result) && $result == 0)
621 if (!DolibarrApiAccess::$user->rights->societe->creer) {
622 throw new RestException(401);
625 $result = $this->company->fetch($id);
627 throw new RestException(404,
'Thirdparty not found');
630 $result = $category->fetch($category_id);
632 throw new RestException(404,
'category not found');
636 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
639 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
642 $category->add_type($this->company,
'customer');
659 if (!DolibarrApiAccess::$user->rights->societe->creer) {
660 throw new RestException(401);
663 $result = $this->company->fetch($id);
665 throw new RestException(404,
'Thirdparty not found');
668 $result = $category->fetch($category_id);
670 throw new RestException(404,
'category not found');
674 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
677 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
680 $category->del_type($this->company,
'customer');
700 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
701 throw new RestException(401);
704 $result = $this->company->fetch($id);
707 throw new RestException(404,
'Thirdparty not found');
712 $result = $categories->getListForItem($id,
'supplier', $sortfield, $sortorder, $limit, $page);
714 if (is_numeric($result) && $result < 0)
716 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
719 if (is_numeric($result) && $result == 0)
739 if (!DolibarrApiAccess::$user->rights->societe->creer) {
740 throw new RestException(401);
743 $result = $this->company->fetch($id);
745 throw new RestException(404,
'Thirdparty not found');
748 $result = $category->fetch($category_id);
750 throw new RestException(404,
'category not found');
754 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
757 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
760 $category->add_type($this->company,
'supplier');
777 if (!DolibarrApiAccess::$user->rights->societe->creer) {
778 throw new RestException(401);
781 $result = $this->company->fetch($id);
783 throw new RestException(404,
'Thirdparty not found');
786 $result = $category->fetch($category_id);
788 throw new RestException(404,
'category not found');
792 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
795 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
798 $category->del_type($this->company,
'supplier');
820 if (!DolibarrApiAccess::$user->rights->societe->lire) {
821 throw new RestException(401);
825 throw new RestException(400,
'Thirdparty ID is mandatory');
829 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
832 $result = $this->company->fetch($id);
834 throw new RestException(404,
'Thirdparty not found');
837 $result = $this->company->getOutstandingProposals($mode);
839 unset($result[
'total_ht']);
840 unset($result[
'total_ttc']);
862 if (!DolibarrApiAccess::$user->rights->societe->lire) {
863 throw new RestException(401);
867 throw new RestException(400,
'Thirdparty ID is mandatory');
871 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
874 $result = $this->company->fetch($id);
876 throw new RestException(404,
'Thirdparty not found');
879 $result = $this->company->getOutstandingOrders($mode);
881 unset($result[
'total_ht']);
882 unset($result[
'total_ttc']);
903 if (!DolibarrApiAccess::$user->rights->societe->lire) {
904 throw new RestException(401);
908 throw new RestException(400,
'Thirdparty ID is mandatory');
912 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
915 $result = $this->company->fetch($id);
917 throw new RestException(404,
'Thirdparty not found');
920 $result = $this->company->getOutstandingBills($mode);
922 unset($result[
'total_ht']);
923 unset($result[
'total_ttc']);
944 if (!DolibarrApiAccess::$user->rights->societe->lire) {
945 throw new RestException(401);
949 throw new RestException(400,
'Thirdparty ID is mandatory');
953 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
956 $result = $this->company->fetch($id);
958 throw new RestException(404,
'Thirdparty not found');
961 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
987 if (!DolibarrApiAccess::$user->rights->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');
1005 $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";
1006 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1007 $sql .=
" WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".$id;
1008 if ($filter ==
"available") $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1009 if ($filter ==
"used") $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1011 $sql .= $this->
db->order($sortfield, $sortorder);
1013 $result = $this->
db->query($sql);
1015 throw new RestException(503, $this->
db->lasterror());
1017 $num = $this->
db->num_rows($result);
1018 while ($obj = $this->
db->fetch_object($result)) {
1043 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1044 throw new RestException(401);
1047 throw new RestException(400,
'Thirdparty ID is mandatory');
1051 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1060 $result = $invoice->list_replacable_invoices($id);
1062 throw new RestException(405, $this->thirdparty->error);
1086 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1087 throw new RestException(401);
1090 throw new RestException(400,
'Thirdparty ID is mandatory');
1094 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1103 $result = $invoice->list_qualified_avoir_invoices($id);
1105 throw new RestException(405, $this->thirdparty->error);
1122 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1123 throw new RestException(401);
1126 throw new RestException(400,
'Thirdparty ID is mandatory');
1130 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1137 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1138 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1139 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1140 if ($id) $sql .=
" WHERE fk_soc = ".$id.
" ";
1143 $result = $this->
db->query($sql);
1145 if ($result->num_rows == 0) {
1146 throw new RestException(404,
'Account not found');
1151 $accounts = array();
1155 $num = $this->
db->num_rows($result);
1158 $obj = $this->
db->fetch_object($result);
1160 if ($account->fetch($obj->rowid)) {
1161 $accounts[] = $account;
1166 throw new RestException(404,
'Account not found');
1170 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1172 $returnAccounts = array();
1174 foreach ($accounts as $account) {
1176 foreach ($account as $key => $value) {
1177 if (in_array($key, $fields)) {
1178 $object[$key] = $value;
1181 $returnAccounts[] = $object;
1184 return $returnAccounts;
1198 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1199 throw new RestException(401);
1201 if ($this->company->fetch($id) <= 0) {
1202 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1206 $account->socid = $id;
1208 foreach ($request_data as $field => $value) {
1209 $account->$field = $value;
1212 if ($account->create(DolibarrApiAccess::$user) < 0)
1213 throw new RestException(500,
'Error creating Company Bank account');
1215 if (empty($account->rum)) {
1216 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1218 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1219 $account->date_rum =
dol_now();
1222 if ($account->update(DolibarrApiAccess::$user) < 0)
1223 throw new RestException(500,
'Error updating values');
1241 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1242 throw new RestException(401);
1244 if ($this->company->fetch($id) <= 0) {
1245 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1249 $account->fetch($bankaccount_id, $id, -1,
'');
1251 if ($account->socid != $id) {
1252 throw new RestException(401);
1256 foreach ($request_data as $field => $value) {
1257 $account->$field = $value;
1260 if (empty($account->rum)) {
1261 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1263 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1264 $account->date_rum =
dol_now();
1267 if ($account->update(DolibarrApiAccess::$user) < 0)
1268 throw new RestException(500,
'Error updating values');
1285 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1286 throw new RestException(401);
1291 $account->fetch($bankaccount_id);
1293 if (!$account->socid == $id)
1294 throw new RestException(401);
1296 return $account->delete(DolibarrApiAccess::$user);
1311 global $conf, $langs;
1313 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1315 if ($this->company->fetch($id) <= 0) {
1316 throw new RestException(404,
'Thirdparty not found');
1319 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1320 throw new RestException(401);
1323 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1325 $this->company->fk_bank = $this->company->fk_account;
1327 $outputlangs = $langs;
1331 if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang)) {
1332 if (isset($this->company->thirdparty->default_lang)) {
1333 $newlang = $this->company->thirdparty->default_lang;
1334 } elseif (isset($this->company->default_lang)) {
1335 $newlang = $this->company->default_lang;
1338 if (!empty($newlang)) {
1339 $outputlangs =
new Translate(
"", $conf);
1340 $outputlangs->setDefaultLang($newlang);
1343 $sql =
"SELECT rowid";
1344 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1345 if ($id) $sql .=
" WHERE fk_soc = ".$id.
" ";
1346 if ($companybankid) $sql .=
" AND rowid = ".$companybankid.
"";
1349 $accounts = array();
1351 $result = $this->
db->query($sql);
1353 if ($this->
db->num_rows($result) == 0) {
1354 throw new RestException(404,
'Bank account not found');
1357 $num = $this->
db->num_rows($result);
1359 $obj = $this->
db->fetch_object($result);
1362 if ($account->fetch($obj->rowid)) {
1363 $accounts[] = $account;
1368 throw new RestException(500,
'Sql error '.$this->
db->lasterror());
1371 $moreparams = array(
1372 'use_companybankid' => $accounts[0]->
id,
1373 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName($this->company->id)
1376 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1379 return array(
"success" => $result);
1381 throw new RestException(500);
1399 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1400 throw new RestException(401);
1404 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1410 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1411 $sql .=
" WHERE fk_soc = $id";
1412 if ($site) $sql .=
" AND site ='$site'";
1414 $result = $this->
db->query($sql);
1416 if ($result && $this->
db->num_rows($result) == 0) {
1417 throw new RestException(404,
'This thirdparty does not have any gateway attached or does not exist.');
1422 $accounts = array();
1424 $num = $this->
db->num_rows($result);
1427 $obj = $this->
db->fetch_object($result);
1430 if ($account->fetch($obj->rowid)) {
1431 $accounts[] = $account;
1436 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1438 $returnAccounts = array();
1440 foreach ($accounts as $account) {
1442 foreach ($account as $key => $value) {
1443 if (in_array($key, $fields)) {
1444 $object[$key] = $value;
1447 $returnAccounts[] = $object;
1450 return $returnAccounts;
1474 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1475 throw new RestException(401);
1478 if (!isset($request_data[
'site'])) {
1479 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1482 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".$id.
" AND site = '".$this->
db->escape($request_data[
'site']).
"'";
1483 $result = $this->
db->query($sql);
1485 if ($result && $this->
db->num_rows($result) == 0) {
1487 if (!isset($request_data[
'login'])) {
1488 $account->login =
"";
1490 $account->fk_soc = $id;
1492 foreach ($request_data as $field => $value) {
1493 $account->$field = $value;
1496 if ($account->create(DolibarrApiAccess::$user) < 0)
1497 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1503 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1531 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1532 throw new RestException(401);
1535 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->
db->escape($site).
"'";
1536 $result = $this->
db->query($sql);
1539 if ($result && $this->
db->num_rows == 0) {
1540 if (!isset($request_data[
'key_account'])) {
1541 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1544 if (!isset($request_data[
'login'])) {
1545 $account->login =
"";
1548 foreach ($request_data as $field => $value) {
1549 $account->$field = $value;
1552 $account->fk_soc = $id;
1553 $account->site = $site;
1555 if ($account->create(DolibarrApiAccess::$user) < 0) {
1556 throw new RestException(500,
'Error creating SocieteAccount entity.');
1560 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1561 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".$id.
" AND site = '".$this->
db->escape($request_data[
'site']).
"' ";
1562 $result = $this->
db->query($sql);
1564 if ($result && $this->
db->num_rows($result) !== 0) {
1565 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.");
1569 $obj = $this->
db->fetch_object($result);
1572 $account->id = $obj->rowid;
1573 $account->fk_soc = $id;
1574 $account->site = $site;
1575 if (!isset($request_data[
'login'])) {
1576 $account->login =
"";
1578 $account->fk_user_creat = $obj->fk_user_creat;
1579 $account->date_creation = $obj->date_creation;
1581 foreach ($request_data as $field => $value) {
1582 $account->$field = $value;
1585 if ($account->update(DolibarrApiAccess::$user) < 0)
1586 throw new RestException(500,
'Error updating SocieteAccount entity.');
1612 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1613 throw new RestException(401);
1616 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '$site' ";
1617 $result = $this->
db->query($sql);
1619 if ($result && $this->
db->num_rows($result) == 0) {
1620 throw new RestException(404,
"This thirdparty does not have $site gateway attached or does not exist.");
1623 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1624 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".$id.
" AND site = '".$this->
db->escape($request_data[
'site']).
"' ";
1625 $result = $this->
db->query($sql);
1627 if ($result && $this->
db->num_rows($result) !== 0)
1628 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.");
1631 $obj = $this->
db->fetch_object($result);
1633 $account->fetch($obj->rowid);
1635 foreach ($request_data as $field => $value) {
1636 $account->$field = $value;
1639 if ($account->update(DolibarrApiAccess::$user) < 0)
1640 throw new RestException(500,
'Error updating SocieteAccount account');
1663 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1664 throw new RestException(401);
1667 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->
db->escape($site).
"'";
1668 $result = $this->
db->query($sql);
1670 if ($result && $this->
db->num_rows($result) == 0) {
1671 throw new RestException(404);
1673 $obj = $this->
db->fetch_object($result);
1675 $account->fetch($obj->rowid);
1677 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1678 throw new RestException(500,
"Error while deleting $site gateway attached to this third party");
1697 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1698 throw new RestException(401);
1705 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1706 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".$id;
1708 $result = $this->
db->query($sql);
1710 if ($result && $this->
db->num_rows($result) == 0) {
1711 throw new RestException(404,
'This third party does not have any gateway attached or does not exist.');
1715 $num = $this->
db->num_rows($result);
1718 $obj = $this->
db->fetch_object($result);
1720 $account->fetch($obj->rowid);
1722 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1723 throw new RestException(500,
'Error while deleting gateways attached to this third party');
1740 $object = parent::_cleanObjectDatas($object);
1742 unset($object->nom);
1743 unset($object->name_bis);
1744 unset($object->note);
1745 unset($object->departement);
1746 unset($object->departement_code);
1747 unset($object->pays);
1748 unset($object->particulier);
1749 unset($object->prefix_comm);
1751 unset($object->commercial_id);
1753 unset($object->total_ht);
1754 unset($object->total_tva);
1755 unset($object->total_localtax1);
1756 unset($object->total_localtax2);
1757 unset($object->total_ttc);
1759 unset($object->lines);
1760 unset($object->thirdparty);
1762 unset($object->fk_delivery_address);
1764 unset($object->skype);
1765 unset($object->twitter);
1766 unset($object->facebook);
1767 unset($object->linkedin);
1782 $thirdparty = array();
1783 foreach (Thirdparties::$FIELDS as $field) {
1784 if (!isset($data[$field]))
1785 throw new RestException(400,
"$field field missing");
1786 $thirdparty[$field] = $data[$field];
1812 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
1815 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1816 throw new RestException(401);
1819 $result = $this->company->initAsSpecimen();
1821 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1824 throw new RestException(404,
'Thirdparty not found');
1828 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1831 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1832 $filterabsolutediscount =
"fk_facture_source IS NULL";
1833 $filtercreditnote =
"fk_facture_source IS NOT NULL";
1835 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1836 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1839 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
1840 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
1841 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
1842 $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.
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.
$conf db
API class for accounts.
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)
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 site 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 user access to a 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)