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';
58 $this->company =
new Societe($this->db);
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);
209 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
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);
231 $soc_static =
new Societe($this->db);
232 if ($soc_static->fetch($obj->rowid)) {
234 $soc_static->getNoEmail();
241 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->db->lasterror());
243 if (!count($obj_ret)) {
244 throw new RestException(404,
'Thirdparties not found');
255 public function post($request_data =
null)
257 if (!DolibarrApiAccess::$user->rights->societe->creer) {
258 throw new RestException(401);
261 $result = $this->
_validate($request_data);
263 foreach ($request_data as $field => $value) {
264 $this->company->$field = $value;
266 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
267 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
269 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
270 $this->company->setNoEmail($this->company->no_email);
273 return $this->company->id;
283 public function put($id, $request_data =
null)
285 if (!DolibarrApiAccess::$user->rights->societe->creer) {
286 throw new RestException(401);
289 $result = $this->company->fetch($id);
291 throw new RestException(404,
'Thirdparty not found');
295 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
298 foreach ($request_data as $field => $value) {
299 if ($field ==
'id') {
302 $this->company->$field = $value;
305 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
306 $this->company->setNoEmail($this->company->no_email);
309 if ($this->company->update($id, DolibarrApiAccess::$user, 1,
'',
'',
'update', 1)) {
310 return $this->
get($id);
330 public function merge($id, $idtodelete)
336 if ($id == $idtodelete) {
337 throw new RestException(400,
'Try to merge a thirdparty into itself');
340 if (!DolibarrApiAccess::$user->rights->societe->creer) {
341 throw new RestException(401);
344 $result = $this->company->fetch($id);
346 throw new RestException(404,
'Thirdparty not found');
350 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
353 $this->companytoremove =
new Societe($this->db);
355 $result = $this->companytoremove->fetch($idtodelete);
357 throw new RestException(404,
'Thirdparty not found');
361 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
364 $soc_origin = $this->companytoremove;
365 $object = $this->company;
366 $user = DolibarrApiAccess::$user;
375 $object->client = $object->client | $soc_origin->client;
376 $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
377 $listofproperties = array(
378 'address',
'zip',
'town',
'state_id',
'country_id',
'phone',
'phone_pro',
'fax',
'email',
'skype',
'url',
'barcode',
379 'idprof1',
'idprof2',
'idprof3',
'idprof4',
'idprof5',
'idprof6',
380 'tva_intra',
'effectif_id',
'forme_juridique',
'remise_percent',
'remise_supplier_percent',
'mode_reglement_supplier_id',
'cond_reglement_supplier_id',
'name_bis',
381 'stcomm_id',
'outstanding_limit',
'price_level',
'parent',
'default_lang',
'ref',
'ref_ext',
'import_key',
'fk_incoterms',
'fk_multicurrency',
382 'code_client',
'code_fournisseur',
'code_compta',
'code_compta_fournisseur',
383 'model_pdf',
'fk_projet'
385 foreach ($listofproperties as $property) {
386 if (empty($object->$property)) {
387 $object->$property = $soc_origin->$property;
392 $listofproperties = array(
393 'note_public',
'note_private'
395 foreach ($listofproperties as $property) {
396 $object->$property =
dol_concatdesc($object->$property, $soc_origin->$property);
400 if (is_array($soc_origin->array_options)) {
401 foreach ($soc_origin->array_options as $key => $val) {
402 if (empty($object->array_options[$key])) {
403 $object->array_options[$key] = $val;
410 $custcats = $static_cat->containing($soc_origin->id,
'customer',
'id');
411 $object->setCategories($custcats,
'customer');
412 $suppcats = $static_cat->containing($soc_origin->id,
'supplier',
'id');
413 $object->setCategories($suppcats,
'supplier');
416 if ($soc_origin->code_client == $object->code_client
417 || $soc_origin->code_fournisseur == $object->code_fournisseur
418 || $soc_origin->barcode == $object->barcode) {
419 dol_syslog(
"We clean customer and supplier code so we will be able to make the update of target");
420 $soc_origin->code_client =
'';
421 $soc_origin->code_fournisseur =
'';
422 $soc_origin->barcode =
'';
423 $soc_origin->update($soc_origin->id, $user, 0, 1, 1,
'merge');
427 $result = $object->update($object->id, $user, 0, 1, 1,
'merge');
437 'Adherent' =>
'/adherents/class/adherent.class.php',
438 'Don' =>
'/don/class/don.class.php',
439 'Societe' =>
'/societe/class/societe.class.php',
441 'ActionComm' =>
'/comm/action/class/actioncomm.class.php',
442 'Propal' =>
'/comm/propal/class/propal.class.php',
443 'Commande' =>
'/commande/class/commande.class.php',
444 'Facture' =>
'/compta/facture/class/facture.class.php',
445 'FactureRec' =>
'/compta/facture/class/facture-rec.class.php',
446 'LignePrelevement' =>
'/compta/prelevement/class/ligneprelevement.class.php',
447 'Mo' =>
'/mrp/class/mo.class.php',
448 'Contact' =>
'/contact/class/contact.class.php',
449 'Contrat' =>
'/contrat/class/contrat.class.php',
450 'Expedition' =>
'/expedition/class/expedition.class.php',
451 'Fichinter' =>
'/fichinter/class/fichinter.class.php',
452 'CommandeFournisseur' =>
'/fourn/class/fournisseur.commande.class.php',
453 'FactureFournisseur' =>
'/fourn/class/fournisseur.facture.class.php',
454 'SupplierProposal' =>
'/supplier_proposal/class/supplier_proposal.class.php',
455 'ProductFournisseur' =>
'/fourn/class/fournisseur.product.class.php',
456 'Delivery' =>
'/delivery/class/delivery.class.php',
457 'Product' =>
'/product/class/product.class.php',
458 'Project' =>
'/projet/class/project.class.php',
459 'Ticket' =>
'/ticket/class/ticket.class.php',
460 'User' =>
'/user/class/user.class.php',
461 'Account' =>
'/compta/bank/class/account.class.php',
462 'ConferenceOrBoothAttendee' =>
'/eventorganization/class/conferenceorboothattendee.class.php'
466 foreach ($objects as $object_name => $object_file) {
467 require_once DOL_DOCUMENT_ROOT.$object_file;
469 if (!$error && !$object_name::replaceThirdparty($this->db, $soc_origin->id, $object->id)) {
478 $reshook = $hookmanager->executeHooks(
'replaceThirdparty', array(
479 'soc_origin' => $soc_origin->id,
480 'soc_dest' => $object->id
481 ), $soc_dest, $action);
491 $object->context = array(
'merge'=>1,
'mergefromid'=>$soc_origin->id);
494 $result = $object->call_trigger(
'COMPANY_MODIFY', $user);
504 if ($soc_origin->delete($soc_origin->id, $user) < 1) {
512 $this->db->rollback();
514 throw new RestException(500,
'Error failed to merged thirdparty '.$this->companytoremove->id.
' into '.$id.
'. Enable and read log file for more information.');
519 return $this->
get($id);
528 public function delete($id)
530 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
531 throw new RestException(401);
533 $result = $this->company->fetch($id);
535 throw new RestException(404,
'Thirdparty not found');
538 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
540 $this->company->oldcopy = clone $this->company;
542 $res = $this->company->delete($id);
544 throw new RestException(500,
"Can't delete, error occurs");
545 } elseif ($res == 0) {
546 throw new RestException(409,
"Can't delete, that product is probably used");
552 'message' =>
'Object deleted'
577 throw new RestException(501,
'Module "Thirdparties" needed for this request');
581 throw new RestException(501,
'Module "Products" needed for this request');
584 if (empty($conf->global->PRODUIT_MULTIPRICES)) {
585 throw new RestException(501,
'Multiprices features activation needed for this request');
588 if ($priceLevel < 1 || $priceLevel > $conf->global->PRODUIT_MULTIPRICES_LIMIT) {
589 throw new RestException(400,
'Price level must be between 1 and '.$conf->global->PRODUIT_MULTIPRICES_LIMIT);
592 if (empty(DolibarrApiAccess::$user->rights->societe->creer)) {
593 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
596 $result = $this->company->fetch($id);
598 throw new RestException(404,
'Thirdparty '.$id.
' not found');
601 if (empty($result)) {
602 throw new RestException(500,
'Error fetching thirdparty '.$id, array_merge(array($this->company->error), $this->company->errors));
606 throw new RestException(401,
'Access to thirdparty '.$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
609 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
611 throw new RestException(500,
'Error setting new price level for thirdparty '.$id, array($this->company->db->lasterror()));
629 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
631 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
632 throw new RestException(401);
635 $result = $this->company->fetch($id);
637 throw new RestException(404,
'Thirdparty not found');
642 $arrayofcateg = $categories->getListForItem($id,
'customer', $sortfield, $sortorder, $limit, $page);
644 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
645 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
648 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
652 return $arrayofcateg;
666 if (!DolibarrApiAccess::$user->rights->societe->creer) {
667 throw new RestException(401);
670 $result = $this->company->fetch($id);
672 throw new RestException(404,
'Thirdparty not found');
675 $result = $category->fetch($category_id);
677 throw new RestException(404,
'category not found');
681 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
684 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
687 $category->add_type($this->company,
'customer');
704 if (!DolibarrApiAccess::$user->rights->societe->creer) {
705 throw new RestException(401);
708 $result = $this->company->fetch($id);
710 throw new RestException(404,
'Thirdparty not found');
713 $result = $category->fetch($category_id);
715 throw new RestException(404,
'category not found');
719 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
722 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
725 $category->del_type($this->company,
'customer');
745 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
746 throw new RestException(401);
749 $result = $this->company->fetch($id);
751 throw new RestException(404,
'Thirdparty not found');
756 $result = $categories->getListForItem($id,
'supplier', $sortfield, $sortorder, $limit, $page);
758 if (is_numeric($result) && $result < 0) {
759 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
762 if (is_numeric($result) && $result == 0) {
781 if (!DolibarrApiAccess::$user->rights->societe->creer) {
782 throw new RestException(401);
785 $result = $this->company->fetch($id);
787 throw new RestException(404,
'Thirdparty not found');
790 $result = $category->fetch($category_id);
792 throw new RestException(404,
'category not found');
796 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
799 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
802 $category->add_type($this->company,
'supplier');
819 if (!DolibarrApiAccess::$user->rights->societe->creer) {
820 throw new RestException(401);
823 $result = $this->company->fetch($id);
825 throw new RestException(404,
'Thirdparty not found');
828 $result = $category->fetch($category_id);
830 throw new RestException(404,
'category not found');
834 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
837 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
840 $category->del_type($this->company,
'supplier');
862 if (!DolibarrApiAccess::$user->hasRight(
'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->getOutstandingProposals($mode);
881 unset($result[
'total_ht']);
882 unset($result[
'total_ttc']);
904 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
905 throw new RestException(401);
909 throw new RestException(400,
'Thirdparty ID is mandatory');
913 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
916 $result = $this->company->fetch($id);
918 throw new RestException(404,
'Thirdparty not found');
921 $result = $this->company->getOutstandingOrders($mode);
923 unset($result[
'total_ht']);
924 unset($result[
'total_ttc']);
945 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
946 throw new RestException(401);
950 throw new RestException(400,
'Thirdparty ID is mandatory');
954 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
957 $result = $this->company->fetch($id);
959 throw new RestException(404,
'Thirdparty not found');
962 $result = $this->company->getOutstandingBills($mode);
964 unset($result[
'total_ht']);
965 unset($result[
'total_ttc']);
986 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
987 throw new RestException(401);
991 throw new RestException(400,
'Thirdparty ID is mandatory');
995 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
998 $result = $this->company->fetch($id);
1000 throw new RestException(404,
'Thirdparty not found');
1003 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1029 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1030 throw new RestException(401);
1034 throw new RestException(400,
'Thirdparty ID is mandatory');
1038 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1041 $result = $this->company->fetch($id);
1043 throw new RestException(404,
'Thirdparty not found');
1047 $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";
1048 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1049 $sql .=
" WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
1050 if ($filter ==
"available") {
1051 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1053 if ($filter ==
"used") {
1054 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1057 $sql .= $this->db->order($sortfield, $sortorder);
1059 $result = $this->db->query(
$sql);
1061 throw new RestException(503, $this->db->lasterror());
1063 $num = $this->db->num_rows($result);
1064 while ($obj = $this->db->fetch_object($result)) {
1089 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1090 throw new RestException(401);
1093 throw new RestException(400,
'Thirdparty ID is mandatory');
1097 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1105 $invoice =
new Facture($this->db);
1106 $result = $invoice->list_replacable_invoices($id);
1108 throw new RestException(405, $invoice->error);
1132 if (!DolibarrApiAccess::$user->rights->facture->lire) {
1133 throw new RestException(401);
1136 throw new RestException(400,
'Thirdparty ID is mandatory');
1140 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1148 $invoice =
new Facture($this->db);
1149 $result = $invoice->list_qualified_avoir_invoices($id);
1151 throw new RestException(405, $invoice->error);
1168 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1169 throw new RestException(401);
1172 throw new RestException(400,
'Thirdparty ID is mandatory');
1176 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1183 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1184 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1185 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1187 $sql .=
" WHERE fk_soc = ".((int) $id);
1190 $result = $this->db->query(
$sql);
1192 if ($this->db->num_rows($result) == 0) {
1193 throw new RestException(404,
'Account not found');
1198 $accounts = array();
1201 $num = $this->db->num_rows($result);
1203 $obj = $this->db->fetch_object($result);
1205 if ($account->fetch($obj->rowid)) {
1206 $accounts[] = $account;
1211 throw new RestException(404,
'Account not found');
1215 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1217 $returnAccounts = array();
1219 foreach ($accounts as $account) {
1221 foreach ($account as $key => $value) {
1222 if (in_array($key, $fields)) {
1223 $object[$key] = $value;
1226 $returnAccounts[] = $object;
1229 return $returnAccounts;
1243 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1244 throw new RestException(401);
1246 if ($this->company->fetch($id) <= 0) {
1247 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1251 $account->socid = $id;
1253 foreach ($request_data as $field => $value) {
1254 $account->$field = $value;
1257 if ($account->create(DolibarrApiAccess::$user) < 0) {
1258 throw new RestException(500,
'Error creating Company Bank account');
1261 if (empty($account->rum)) {
1262 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1264 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1265 $account->date_rum =
dol_now();
1268 if ($account->update(DolibarrApiAccess::$user) < 0) {
1269 throw new RestException(500,
'Error updating values');
1288 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1289 throw new RestException(401);
1291 if ($this->company->fetch($id) <= 0) {
1292 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1296 $account->fetch($bankaccount_id, $id, -1,
'');
1298 if ($account->socid != $id) {
1299 throw new RestException(401);
1303 foreach ($request_data as $field => $value) {
1304 $account->$field = $value;
1307 if (empty($account->rum)) {
1308 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1310 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1311 $account->date_rum =
dol_now();
1314 if ($account->update(DolibarrApiAccess::$user) < 0) {
1315 throw new RestException(500,
'Error updating values');
1333 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1334 throw new RestException(401);
1339 $account->fetch($bankaccount_id);
1341 if (!$account->socid == $id) {
1342 throw new RestException(401);
1345 return $account->delete(DolibarrApiAccess::$user);
1360 global $conf, $langs;
1362 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1364 if ($this->company->fetch($id) <= 0) {
1365 throw new RestException(404,
'Thirdparty not found');
1368 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1369 throw new RestException(401);
1372 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1374 $this->company->fk_bank = $this->company->fk_account;
1375 $this->company->fk_account = $this->company->fk_account;
1377 $outputlangs = $langs;
1382 if (isset($this->company->thirdparty->default_lang)) {
1383 $newlang = $this->company->thirdparty->default_lang;
1384 } elseif (isset($this->company->default_lang)) {
1385 $newlang = $this->company->default_lang;
1388 if (!empty($newlang)) {
1389 $outputlangs =
new Translate(
"", $conf);
1390 $outputlangs->setDefaultLang($newlang);
1393 $sql =
"SELECT rowid";
1394 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1396 $sql .=
" WHERE fk_soc = ".((int) $id);
1398 if ($companybankid) {
1399 $sql .=
" AND rowid = ".((int) $companybankid);
1403 $accounts = array();
1405 $result = $this->db->query(
$sql);
1407 if ($this->db->num_rows($result) == 0) {
1408 throw new RestException(404,
'Bank account not found');
1411 $num = $this->db->num_rows($result);
1413 $obj = $this->db->fetch_object($result);
1416 if ($account->fetch($obj->rowid)) {
1417 $accounts[] = $account;
1422 throw new RestException(500,
'Sql error '.$this->db->lasterror());
1425 $moreparams = array(
1426 'use_companybankid' => $accounts[0]->
id,
1427 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName($this->company->id)
1430 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1433 return array(
"success" => $result);
1435 throw new RestException(500,
'Error generating the document '.$this->company->error);
1453 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1454 throw new RestException(401);
1458 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1464 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1465 $sql .=
" WHERE fk_soc = ".((int) $id);
1467 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1470 $result = $this->db->query(
$sql);
1472 if ($result && $this->db->num_rows($result) == 0) {
1473 throw new RestException(404,
'This thirdparty does not have any gateway attached or does not exist.');
1478 $accounts = array();
1480 $num = $this->db->num_rows($result);
1482 $obj = $this->db->fetch_object($result);
1485 if ($account->fetch($obj->rowid)) {
1486 $accounts[] = $account;
1491 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1493 $returnAccounts = array();
1495 foreach ($accounts as $account) {
1497 foreach ($account as $key => $value) {
1498 if (in_array($key, $fields)) {
1499 $object[$key] = $value;
1502 $returnAccounts[] = $object;
1505 return $returnAccounts;
1529 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1530 throw new RestException(401);
1533 if (!isset($request_data[
'site'])) {
1534 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1537 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
1538 $result = $this->db->query(
$sql);
1540 if ($result && $this->db->num_rows($result) == 0) {
1542 if (!isset($request_data[
'login'])) {
1543 $account->login =
"";
1545 $account->fk_soc = $id;
1547 foreach ($request_data as $field => $value) {
1548 $account->$field = $value;
1551 if ($account->create(DolibarrApiAccess::$user) < 0) {
1552 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1559 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1587 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1588 throw new RestException(401);
1591 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1592 $result = $this->db->query(
$sql);
1595 if ($result && $this->db->num_rows == 0) {
1596 if (!isset($request_data[
'key_account'])) {
1597 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1600 if (!isset($request_data[
'login'])) {
1601 $account->login =
"";
1604 foreach ($request_data as $field => $value) {
1605 $account->$field = $value;
1608 $account->fk_soc = $id;
1609 $account->site = $site;
1611 if ($account->create(DolibarrApiAccess::$user) < 0) {
1612 throw new RestException(500,
'Error creating SocieteAccount entity.');
1616 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1617 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1618 $result = $this->db->query(
$sql);
1620 if ($result && $this->db->num_rows($result) !== 0) {
1621 throw new RestException(409,
"You are trying to update this thirdparty SocieteAccount (gateway record) from $site to ".$request_data[
'site'].
" but another SocieteAccount entity already exists with this site key.");
1625 $obj = $this->db->fetch_object($result);
1628 $account->id = $obj->rowid;
1629 $account->fk_soc = $id;
1630 $account->site = $site;
1631 if (!isset($request_data[
'login'])) {
1632 $account->login =
"";
1634 $account->fk_user_creat = $obj->fk_user_creat;
1635 $account->date_creation = $obj->date_creation;
1637 foreach ($request_data as $field => $value) {
1638 $account->$field = $value;
1641 if ($account->update(DolibarrApiAccess::$user) < 0) {
1642 throw new RestException(500,
'Error updating SocieteAccount entity.');
1669 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1670 throw new RestException(401);
1673 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($site).
"'";
1674 $result = $this->db->query(
$sql);
1676 if ($result && $this->db->num_rows($result) == 0) {
1677 throw new RestException(404,
"This thirdparty does not have $site gateway attached or does not exist.");
1680 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1681 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1682 $result = $this->db->query(
$sql);
1684 if ($result && $this->db->num_rows($result) !== 0) {
1685 throw new RestException(409,
"You are trying to update this thirdparty SocieteAccount (gateway record) site member from ".$site.
" to ".$request_data[
'site'].
" but another SocieteAccount entity already exists for this thirdparty with this site key.");
1689 $obj = $this->db->fetch_object($result);
1691 $account->fetch($obj->rowid);
1693 foreach ($request_data as $field => $value) {
1694 $account->$field = $value;
1697 if ($account->update(DolibarrApiAccess::$user) < 0) {
1698 throw new RestException(500,
'Error updating SocieteAccount account');
1722 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1723 throw new RestException(401);
1726 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1727 $result = $this->db->query(
$sql);
1729 if ($result && $this->db->num_rows($result) == 0) {
1730 throw new RestException(404);
1732 $obj = $this->db->fetch_object($result);
1734 $account->fetch($obj->rowid);
1736 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1737 throw new RestException(500,
"Error while deleting $site gateway attached to this third party");
1756 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1757 throw new RestException(401);
1764 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1765 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id);
1767 $result = $this->db->query(
$sql);
1769 if ($result && $this->db->num_rows($result) == 0) {
1770 throw new RestException(404,
'This third party does not have any gateway attached or does not exist.');
1774 $num = $this->db->num_rows($result);
1776 $obj = $this->db->fetch_object($result);
1778 $account->fetch($obj->rowid);
1780 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1781 throw new RestException(500,
'Error while deleting gateways attached to this third party');
1798 $object = parent::_cleanObjectDatas($object);
1800 unset($object->nom);
1801 unset($object->name_bis);
1802 unset($object->note);
1803 unset($object->departement);
1804 unset($object->departement_code);
1805 unset($object->pays);
1806 unset($object->particulier);
1807 unset($object->prefix_comm);
1809 unset($object->siren);
1810 unset($object->siret);
1811 unset($object->ape);
1813 unset($object->commercial_id);
1815 unset($object->total_ht);
1816 unset($object->total_tva);
1817 unset($object->total_localtax1);
1818 unset($object->total_localtax2);
1819 unset($object->total_ttc);
1821 unset($object->lines);
1822 unset($object->thirdparty);
1824 unset($object->fk_delivery_address);
1839 $thirdparty = array();
1840 foreach (Thirdparties::$FIELDS as $field) {
1841 if (!isset($data[$field])) {
1842 throw new RestException(400,
"$field field missing");
1844 $thirdparty[$field] = $data[$field];
1870 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
1874 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1875 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
1879 $result = $this->company->initAsSpecimen();
1881 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1884 throw new RestException(404,
'Thirdparty not found');
1888 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
1891 $this->company->getNoEmail();
1894 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1895 $filterabsolutediscount =
"fk_facture_source IS NULL";
1896 $filtercreditnote =
"fk_facture_source IS NOT NULL";
1898 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1899 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1902 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
1903 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
1904 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
1905 $this->company->absolute_creditnote =
price2num($absolute_creditnote,
'MT');