21use 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)) {
233 if (isModEnabled(
'mailing')) {
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;
286 public function put($id, $request_data =
null)
288 if (!DolibarrApiAccess::$user->rights->societe->creer) {
289 throw new RestException(401);
292 $result = $this->company->fetch($id);
294 throw new RestException(404,
'Thirdparty not found');
298 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
301 foreach ($request_data as $field => $value) {
302 if ($field ==
'id') {
305 $this->company->$field = $value;
308 if (isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
309 $this->company->setNoEmail($this->company->no_email);
312 if ($this->company->update($id, DolibarrApiAccess::$user, 1,
'',
'',
'update', 1) > 0) {
313 return $this->
get($id);
315 throw new RestException(500, $this->company->error);
333 public function merge($id, $idtodelete)
339 if ($id == $idtodelete) {
340 throw new RestException(400,
'Try to merge a thirdparty into itself');
343 if (!DolibarrApiAccess::$user->rights->societe->creer) {
344 throw new RestException(401);
347 $result = $this->company->fetch($id);
349 throw new RestException(404,
'Thirdparty not found');
353 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
356 $this->companytoremove =
new Societe($this->db);
358 $result = $this->companytoremove->fetch($idtodelete);
360 throw new RestException(404,
'Thirdparty not found');
364 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
367 $soc_origin = $this->companytoremove;
368 $object = $this->company;
369 $user = DolibarrApiAccess::$user;
378 $object->client = $object->client | $soc_origin->client;
379 $object->fournisseur = $object->fournisseur | $soc_origin->fournisseur;
380 $listofproperties = array(
381 'address',
'zip',
'town',
'state_id',
'country_id',
'phone',
'phone_pro',
'fax',
'email',
'skype',
'url',
'barcode',
382 'idprof1',
'idprof2',
'idprof3',
'idprof4',
'idprof5',
'idprof6',
383 'tva_intra',
'effectif_id',
'forme_juridique',
'remise_percent',
'remise_supplier_percent',
'mode_reglement_supplier_id',
'cond_reglement_supplier_id',
'name_bis',
384 'stcomm_id',
'outstanding_limit',
'price_level',
'parent',
'default_lang',
'ref',
'ref_ext',
'import_key',
'fk_incoterms',
'fk_multicurrency',
385 'code_client',
'code_fournisseur',
'code_compta',
'code_compta_fournisseur',
386 'model_pdf',
'fk_projet'
388 foreach ($listofproperties as $property) {
389 if (empty($object->$property)) {
390 $object->$property = $soc_origin->$property;
395 $listofproperties = array(
396 'note_public',
'note_private'
398 foreach ($listofproperties as $property) {
399 $object->$property =
dol_concatdesc($object->$property, $soc_origin->$property);
403 if (is_array($soc_origin->array_options)) {
404 foreach ($soc_origin->array_options as $key => $val) {
405 if (empty($object->array_options[$key])) {
406 $object->array_options[$key] = $val;
413 $custcats = $static_cat->containing($soc_origin->id,
'customer',
'id');
414 $object->setCategories($custcats,
'customer');
415 $suppcats = $static_cat->containing($soc_origin->id,
'supplier',
'id');
416 $object->setCategories($suppcats,
'supplier');
419 if ($soc_origin->code_client == $object->code_client
420 || $soc_origin->code_fournisseur == $object->code_fournisseur
421 || $soc_origin->barcode == $object->barcode) {
422 dol_syslog(
"We clean customer and supplier code so we will be able to make the update of target");
423 $soc_origin->code_client =
'';
424 $soc_origin->code_fournisseur =
'';
425 $soc_origin->barcode =
'';
426 $soc_origin->update($soc_origin->id, $user, 0, 1, 1,
'merge');
430 $result = $object->update($object->id, $user, 0, 1, 1,
'merge');
440 'Adherent' =>
'/adherents/class/adherent.class.php',
441 'Don' =>
'/don/class/don.class.php',
442 'Societe' =>
'/societe/class/societe.class.php',
444 'ActionComm' =>
'/comm/action/class/actioncomm.class.php',
445 'Propal' =>
'/comm/propal/class/propal.class.php',
446 'Commande' =>
'/commande/class/commande.class.php',
447 'Facture' =>
'/compta/facture/class/facture.class.php',
448 'FactureRec' =>
'/compta/facture/class/facture-rec.class.php',
449 'LignePrelevement' =>
'/compta/prelevement/class/ligneprelevement.class.php',
450 'Mo' =>
'/mrp/class/mo.class.php',
451 'Contact' =>
'/contact/class/contact.class.php',
452 'Contrat' =>
'/contrat/class/contrat.class.php',
453 'Expedition' =>
'/expedition/class/expedition.class.php',
454 'Fichinter' =>
'/fichinter/class/fichinter.class.php',
455 'CommandeFournisseur' =>
'/fourn/class/fournisseur.commande.class.php',
456 'FactureFournisseur' =>
'/fourn/class/fournisseur.facture.class.php',
457 'SupplierProposal' =>
'/supplier_proposal/class/supplier_proposal.class.php',
458 'ProductFournisseur' =>
'/fourn/class/fournisseur.product.class.php',
459 'Delivery' =>
'/delivery/class/delivery.class.php',
460 'Product' =>
'/product/class/product.class.php',
461 'Project' =>
'/projet/class/project.class.php',
462 'Ticket' =>
'/ticket/class/ticket.class.php',
463 'User' =>
'/user/class/user.class.php',
464 'Account' =>
'/compta/bank/class/account.class.php',
465 'ConferenceOrBoothAttendee' =>
'/eventorganization/class/conferenceorboothattendee.class.php'
469 foreach ($objects as $object_name => $object_file) {
470 require_once DOL_DOCUMENT_ROOT.$object_file;
472 if (!$error && !$object_name::replaceThirdparty($this->db, $soc_origin->id, $object->id)) {
481 $parameters = array(
'soc_origin' => $soc_origin->id,
'soc_dest' => $object->id);
482 $reshook = $hookmanager->executeHooks(
'replaceThirdparty', $parameters, $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'
577 if (!isModEnabled(
'societe')) {
578 throw new RestException(501,
'Module "Thirdparties" needed for this request');
581 if (!isModEnabled(
"product")) {
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()));
630 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
632 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
633 throw new RestException(401);
636 $result = $this->company->fetch($id);
638 throw new RestException(404,
'Thirdparty not found');
643 $arrayofcateg = $categories->getListForItem($id,
'customer', $sortfield, $sortorder, $limit, $page);
645 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
646 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
649 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
653 return $arrayofcateg;
667 if (!DolibarrApiAccess::$user->rights->societe->creer) {
668 throw new RestException(401);
671 $result = $this->company->fetch($id);
673 throw new RestException(404,
'Thirdparty not found');
676 $result = $category->fetch($category_id);
678 throw new RestException(404,
'category not found');
682 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
685 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
688 $category->add_type($this->company,
'customer');
705 if (!DolibarrApiAccess::$user->rights->societe->creer) {
706 throw new RestException(401);
709 $result = $this->company->fetch($id);
711 throw new RestException(404,
'Thirdparty not found');
714 $result = $category->fetch($category_id);
716 throw new RestException(404,
'category not found');
720 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
723 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
726 $category->del_type($this->company,
'customer');
746 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
747 throw new RestException(401);
750 $result = $this->company->fetch($id);
752 throw new RestException(404,
'Thirdparty not found');
757 $result = $categories->getListForItem($id,
'supplier', $sortfield, $sortorder, $limit, $page);
759 if (is_numeric($result) && $result < 0) {
760 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
763 if (is_numeric($result) && $result == 0) {
782 if (!DolibarrApiAccess::$user->rights->societe->creer) {
783 throw new RestException(401);
786 $result = $this->company->fetch($id);
788 throw new RestException(404,
'Thirdparty not found');
791 $result = $category->fetch($category_id);
793 throw new RestException(404,
'category not found');
797 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
800 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
803 $category->add_type($this->company,
'supplier');
820 if (!DolibarrApiAccess::$user->rights->societe->creer) {
821 throw new RestException(401);
824 $result = $this->company->fetch($id);
826 throw new RestException(404,
'Thirdparty not found');
829 $result = $category->fetch($category_id);
831 throw new RestException(404,
'category not found');
835 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
838 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
841 $category->del_type($this->company,
'supplier');
863 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
864 throw new RestException(401);
868 throw new RestException(400,
'Thirdparty ID is mandatory');
872 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
875 $result = $this->company->fetch($id);
877 throw new RestException(404,
'Thirdparty not found');
880 $result = $this->company->getOutstandingProposals($mode);
882 unset($result[
'total_ht']);
883 unset($result[
'total_ttc']);
905 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
906 throw new RestException(401);
910 throw new RestException(400,
'Thirdparty ID is mandatory');
914 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
917 $result = $this->company->fetch($id);
919 throw new RestException(404,
'Thirdparty not found');
922 $result = $this->company->getOutstandingOrders($mode);
924 unset($result[
'total_ht']);
925 unset($result[
'total_ttc']);
946 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
947 throw new RestException(401);
951 throw new RestException(400,
'Thirdparty ID is mandatory');
955 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
958 $result = $this->company->fetch($id);
960 throw new RestException(404,
'Thirdparty not found');
963 $result = $this->company->getOutstandingBills($mode);
965 unset($result[
'total_ht']);
966 unset($result[
'total_ttc']);
987 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
988 throw new RestException(401);
992 throw new RestException(400,
'Thirdparty ID is mandatory');
996 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
999 $result = $this->company->fetch($id);
1001 throw new RestException(404,
'Thirdparty not found');
1004 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1030 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1031 throw new RestException(401);
1035 throw new RestException(400,
'Thirdparty ID is mandatory');
1039 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1042 $result = $this->company->fetch($id);
1044 throw new RestException(404,
'Thirdparty not found');
1048 $sql =
"SELECT f.ref, f.type as factype, re.fk_facture_source, re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc, re.description, re.fk_facture, re.fk_facture_line";
1049 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1050 $sql .=
" WHERE f.rowid = re.fk_facture_source AND re.fk_soc = ".((int) $id);
1051 if ($filter ==
"available") {
1052 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1054 if ($filter ==
"used") {
1055 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1058 $sql .= $this->db->order($sortfield, $sortorder);
1060 $result = $this->db->query($sql);
1062 throw new RestException(503, $this->db->lasterror());
1064 $num = $this->db->num_rows($result);
1065 while ($obj = $this->db->fetch_object($result)) {
1090 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1091 throw new RestException(401);
1094 throw new RestException(400,
'Thirdparty ID is mandatory');
1098 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1106 $invoice =
new Facture($this->db);
1107 $result = $invoice->list_replacable_invoices($id);
1109 throw new RestException(405, $invoice->error);
1133 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1134 throw new RestException(401);
1137 throw new RestException(400,
'Thirdparty ID is mandatory');
1141 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1149 $invoice =
new Facture($this->db);
1150 $result = $invoice->list_qualified_avoir_invoices($id);
1152 throw new RestException(405, $invoice->error);
1169 if (!DolibarrApiAccess::$user->rights->societe->lire) {
1170 throw new RestException(401);
1173 throw new RestException(400,
'Thirdparty ID is mandatory');
1177 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1184 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation, proprio,";
1185 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1186 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1188 $sql .=
" WHERE fk_soc = ".((int) $id);
1191 $result = $this->db->query($sql);
1193 if ($this->db->num_rows($result) == 0) {
1194 throw new RestException(404,
'Account not found');
1199 $accounts = array();
1202 $num = $this->db->num_rows($result);
1204 $obj = $this->db->fetch_object($result);
1206 if ($account->fetch($obj->rowid)) {
1207 $accounts[] = $account;
1212 throw new RestException(404,
'Account not found');
1216 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1218 $returnAccounts = array();
1220 foreach ($accounts as $account) {
1222 foreach ($account as $key => $value) {
1223 if (in_array($key, $fields)) {
1224 $object[$key] = $value;
1227 $returnAccounts[] = $object;
1230 return $returnAccounts;
1244 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1245 throw new RestException(401);
1247 if ($this->company->fetch($id) <= 0) {
1248 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1252 $account->socid = $id;
1254 foreach ($request_data as $field => $value) {
1255 $account->$field = $value;
1258 if ($account->create(DolibarrApiAccess::$user) < 0) {
1259 throw new RestException(500,
'Error creating Company Bank account');
1262 if (empty($account->rum)) {
1263 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1265 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1266 $account->date_rum =
dol_now();
1269 if ($account->update(DolibarrApiAccess::$user) < 0) {
1270 throw new RestException(500,
'Error updating values');
1289 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1290 throw new RestException(401);
1292 if ($this->company->fetch($id) <= 0) {
1293 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1297 $account->fetch($bankaccount_id, $id, -1,
'');
1299 if ($account->socid != $id) {
1300 throw new RestException(401);
1304 foreach ($request_data as $field => $value) {
1305 $account->$field = $value;
1308 if (empty($account->rum)) {
1309 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1311 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, $account->id);
1312 $account->date_rum =
dol_now();
1315 if ($account->update(DolibarrApiAccess::$user) < 0) {
1316 throw new RestException(500,
'Error updating values');
1334 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1335 throw new RestException(401);
1340 $account->fetch($bankaccount_id);
1342 if (!$account->socid == $id) {
1343 throw new RestException(401);
1346 return $account->delete(DolibarrApiAccess::$user);
1361 global $conf, $langs;
1363 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1365 if ($this->company->fetch($id) <= 0) {
1366 throw new RestException(404,
'Thirdparty not found');
1369 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1370 throw new RestException(401);
1373 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1375 $this->company->fk_bank = $this->company->fk_account;
1376 $this->company->fk_account = $this->company->fk_account;
1378 $outputlangs = $langs;
1383 if (isset($this->company->thirdparty->default_lang)) {
1384 $newlang = $this->company->thirdparty->default_lang;
1385 } elseif (isset($this->company->default_lang)) {
1386 $newlang = $this->company->default_lang;
1389 if (!empty($newlang)) {
1390 $outputlangs =
new Translate(
"", $conf);
1391 $outputlangs->setDefaultLang($newlang);
1394 $sql =
"SELECT rowid";
1395 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1397 $sql .=
" WHERE fk_soc = ".((int) $id);
1399 if ($companybankid) {
1400 $sql .=
" AND rowid = ".((int) $companybankid);
1404 $accounts = array();
1406 $result = $this->db->query($sql);
1408 if ($this->db->num_rows($result) == 0) {
1409 throw new RestException(404,
'Bank account not found');
1412 $num = $this->db->num_rows($result);
1414 $obj = $this->db->fetch_object($result);
1417 if ($account->fetch($obj->rowid)) {
1418 $accounts[] = $account;
1423 throw new RestException(500,
'Sql error '.$this->db->lasterror());
1426 $moreparams = array(
1427 'use_companybankid' => $accounts[0]->
id,
1428 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName($this->company->id)
1431 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
1434 return array(
"success" => $result);
1436 throw new RestException(500,
'Error generating the document '.$this->company->error);
1454 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1455 throw new RestException(401);
1459 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1465 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
1466 $sql .=
" WHERE fk_soc = ".((int) $id);
1468 $sql .=
" AND site ='".$this->db->escape($site).
"'";
1471 $result = $this->db->query($sql);
1473 if ($result && $this->db->num_rows($result) == 0) {
1474 throw new RestException(404,
'This thirdparty does not have any gateway attached or does not exist.');
1479 $accounts = array();
1481 $num = $this->db->num_rows($result);
1483 $obj = $this->db->fetch_object($result);
1486 if ($account->fetch($obj->rowid)) {
1487 $accounts[] = $account;
1492 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
1494 $returnAccounts = array();
1496 foreach ($accounts as $account) {
1498 foreach ($account as $key => $value) {
1499 if (in_array($key, $fields)) {
1500 $object[$key] = $value;
1503 $returnAccounts[] = $object;
1506 return $returnAccounts;
1530 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1531 throw new RestException(401);
1534 if (!isset($request_data[
'site'])) {
1535 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
1538 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
1539 $result = $this->db->query($sql);
1541 if ($result && $this->db->num_rows($result) == 0) {
1543 if (!isset($request_data[
'login'])) {
1544 $account->login =
"";
1546 $account->fk_soc = $id;
1548 foreach ($request_data as $field => $value) {
1549 $account->$field = $value;
1552 if ($account->create(DolibarrApiAccess::$user) < 0) {
1553 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
1560 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
1588 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1589 throw new RestException(401);
1592 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1593 $result = $this->db->query($sql);
1596 if ($result && $this->db->num_rows == 0) {
1597 if (!isset($request_data[
'key_account'])) {
1598 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
1601 if (!isset($request_data[
'login'])) {
1602 $account->login =
"";
1605 foreach ($request_data as $field => $value) {
1606 $account->$field = $value;
1609 $account->fk_soc = $id;
1610 $account->site = $site;
1612 if ($account->create(DolibarrApiAccess::$user) < 0) {
1613 throw new RestException(500,
'Error creating SocieteAccount entity.');
1617 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1618 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1619 $result = $this->db->query($sql);
1621 if ($result && $this->db->num_rows($result) !== 0) {
1622 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.");
1626 $obj = $this->db->fetch_object($result);
1629 $account->id = $obj->rowid;
1630 $account->fk_soc = $id;
1631 $account->site = $site;
1632 if (!isset($request_data[
'login'])) {
1633 $account->login =
"";
1635 $account->fk_user_creat = $obj->fk_user_creat;
1636 $account->date_creation = $obj->date_creation;
1638 foreach ($request_data as $field => $value) {
1639 $account->$field = $value;
1642 if ($account->update(DolibarrApiAccess::$user) < 0) {
1643 throw new RestException(500,
'Error updating SocieteAccount entity.');
1670 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1671 throw new RestException(401);
1674 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($site).
"'";
1675 $result = $this->db->query($sql);
1677 if ($result && $this->db->num_rows($result) == 0) {
1678 throw new RestException(404,
"This thirdparty does not have $site gateway attached or does not exist.");
1681 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
1682 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
1683 $result = $this->db->query($sql);
1685 if ($result && $this->db->num_rows($result) !== 0) {
1686 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.");
1690 $obj = $this->db->fetch_object($result);
1692 $account->fetch($obj->rowid);
1694 foreach ($request_data as $field => $value) {
1695 $account->$field = $value;
1698 if ($account->update(DolibarrApiAccess::$user) < 0) {
1699 throw new RestException(500,
'Error updating SocieteAccount account');
1723 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1724 throw new RestException(401);
1727 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
1728 $result = $this->db->query($sql);
1730 if ($result && $this->db->num_rows($result) == 0) {
1731 throw new RestException(404);
1733 $obj = $this->db->fetch_object($result);
1735 $account->fetch($obj->rowid);
1737 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1738 throw new RestException(500,
"Error while deleting $site gateway attached to this third party");
1757 if (!DolibarrApiAccess::$user->rights->societe->creer) {
1758 throw new RestException(401);
1765 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
1766 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int) $id);
1768 $result = $this->db->query($sql);
1770 if ($result && $this->db->num_rows($result) == 0) {
1771 throw new RestException(404,
'This third party does not have any gateway attached or does not exist.');
1775 $num = $this->db->num_rows($result);
1777 $obj = $this->db->fetch_object($result);
1779 $account->fetch($obj->rowid);
1781 if ($account->delete(DolibarrApiAccess::$user) < 0) {
1782 throw new RestException(500,
'Error while deleting gateways attached to this third party');
1799 $object = parent::_cleanObjectDatas($object);
1801 unset($object->nom);
1802 unset($object->name_bis);
1803 unset($object->note);
1804 unset($object->departement);
1805 unset($object->departement_code);
1806 unset($object->pays);
1807 unset($object->particulier);
1808 unset($object->prefix_comm);
1810 unset($object->siren);
1811 unset($object->siret);
1812 unset($object->ape);
1814 unset($object->commercial_id);
1816 unset($object->total_ht);
1817 unset($object->total_tva);
1818 unset($object->total_localtax1);
1819 unset($object->total_localtax2);
1820 unset($object->total_ttc);
1822 unset($object->lines);
1823 unset($object->thirdparty);
1825 unset($object->fk_delivery_address);
1840 $thirdparty = array();
1841 foreach (Thirdparties::$FIELDS as $field) {
1842 if (!isset($data[$field])) {
1843 throw new RestException(400,
"$field field missing");
1845 $thirdparty[$field] = $data[$field];
1871 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
1875 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1876 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
1880 $result = $this->company->initAsSpecimen();
1882 $result = $this->company->fetch($rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
1885 throw new RestException(404,
'Thirdparty not found');
1889 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
1891 if (isModEnabled(
'mailing')) {
1892 $this->company->getNoEmail();
1895 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
1896 $filterabsolutediscount =
"fk_facture_source IS NULL";
1897 $filtercreditnote =
"fk_facture_source IS NOT NULL";
1899 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
1900 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
1903 $absolute_discount = $this->company->getAvailableDiscounts(
'', $filterabsolutediscount);
1904 $absolute_creditnote = $this->company->getAvailableDiscounts(
'', $filtercreditnote);
1905 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
1906 $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.
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 '.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.