29use Luracast\Restler\RestException;
31require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
46 public static $FIELDS = array(
63 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
64 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
65 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
66 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
67 require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
69 $this->company =
new Societe($this->db);
72 static::$FIELDS[] =
'email';
88 public function get(
$id)
111 return $this->
_fetch(
null,
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
130 return $this->
_fetch(
null,
'',
'', $barcode);
159 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
163 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
164 throw new RestException(403);
168 $socids = DolibarrApiAccess::$user->socid ? (string) DolibarrApiAccess::$user->socid :
'';
172 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
173 $search_sale = DolibarrApiAccess::$user->id;
176 $sql =
"SELECT t.rowid";
177 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
178 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
181 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
183 if (!in_array($mode, array(1, 2, 3))) {
184 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
187 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
189 $sql .=
" AND t.client IN (1, 3)";
190 } elseif ($mode == 2) {
191 $sql .=
" AND t.client IN (2, 3)";
192 } elseif ($mode == 3) {
193 $sql .=
" AND t.client IN (0)";
194 } elseif ($mode == 4) {
195 $sql .=
" AND t.fournisseur IN (1)";
199 if (!empty($mode) && $mode != 4) {
200 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
201 } elseif (!empty($mode) && $mode == 4) {
202 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
204 $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))";
208 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
211 if ($search_sale && $search_sale !=
'-1') {
212 if ($search_sale == -2) {
213 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.rowid', 0, 1);
214 } elseif ($search_sale > 0) {
215 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.rowid', (
int) $search_sale);
223 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
228 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
230 $sql .= $this->db->order($sortfield, $sortorder);
235 $offset = $limit * $page;
237 $sql .= $this->db->plimit($limit + 1, $offset);
240 $result = $this->db->query($sql);
242 $num = $this->db->num_rows($result);
243 $min = min($num, ($limit <= 0 ? $num : $limit));
246 $obj = $this->db->fetch_object($result);
247 $soc_static =
new Societe($this->db);
248 if ($soc_static->fetch($obj->rowid)) {
250 $soc_static->getNoEmail();
257 throw new RestException(503,
'Error when retrieve third parties : '.$this->db->lasterror());
259 if (!count($obj_ret)) {
263 $message =
'No third parties found';
266 $message =
'No customers found';
269 $message =
'No prospects found';
272 $message =
'No other third parties found';
275 $message =
'No suppliers found';
277 throw new RestException(404, $message);
281 if ($pagination_data) {
282 $totalsResult = $this->db->query($sqlTotals);
283 $total = $this->db->fetch_object($totalsResult)->total;
288 $obj_ret[
'data'] = $tmp;
289 $obj_ret[
'pagination'] = [
290 'total' => (int) $total,
292 'page_count' => ceil((
int) $total / $limit),
312 public function post($request_data =
null)
314 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
315 throw new RestException(403);
319 if (!isset($request_data[
'country_id']) && isset($request_data[
'country_code'])) {
320 $field = strlen($request_data[
'country_code']) > 2 ?
'code_iso' :
'code';
321 $id =
dol_getIdFromCode($this->db, $request_data[
'country_code'],
"c_country", $field,
"rowid");
323 throw new RestException(404,
'Country code not found in database: ' . $this->db->lasterror());
325 $request_data[
'country_id'] =
$id;
329 $result = $this->
_validate($request_data);
331 foreach ($request_data as $field => $value) {
332 if ($field ===
'caller') {
334 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
337 if ($field ==
'array_options' && is_array($value)) {
338 $this->company->fetch_optionals();
340 foreach ($value as $index => $val) {
346 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
349 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
350 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
352 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
353 $this->company->setNoEmail($this->company->no_email);
356 return $this->company->id;
376 public function put(
$id, $request_data =
null)
378 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
379 throw new RestException(403);
382 $result = $this->company->fetch(
$id);
384 throw new RestException(404,
'Thirdparty not found');
388 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
391 foreach ($request_data as $field => $value) {
392 if ($field ==
'id') {
395 if ($field ===
'caller') {
397 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
400 if ($field ==
'array_options' && is_array($value)) {
401 foreach ($value as $index => $val) {
406 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
409 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
410 $this->company->setNoEmail($this->company->no_email);
413 if ($this->company->update(
$id, DolibarrApiAccess::$user, 1, 1, 1,
'update', 1) > 0) {
414 return $this->
get(
$id);
416 throw new RestException(500, $this->company->errorsToString());
443 if (
$id == $idtodelete) {
444 throw new RestException(400,
'Try to merge a thirdparty into itself');
447 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
448 throw new RestException(403);
451 $result = $this->company->fetch(
$id);
453 throw new RestException(404,
'Thirdparty not found');
457 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
460 $companytoremove =
new Societe($this->db);
461 $result = $companytoremove->fetch($idtodelete);
463 throw new RestException(404,
'Thirdparty not found');
467 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
470 $user = DolibarrApiAccess::$user;
471 $result = $this->company->mergeCompany($companytoremove->id);
473 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.
$id.
'. Enable and read log file for more information.');
476 return $this->
get(
$id);
491 public function delete(
$id)
493 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
494 throw new RestException(403);
496 $result = $this->company->fetch(
$id);
498 throw new RestException(404,
'Thirdparty not found');
501 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
503 $this->company->oldcopy = clone $this->company;
505 $res = $this->company->delete(
$id);
507 throw new RestException(500,
"Can't delete, error occurs");
508 } elseif ($res == 0) {
509 throw new RestException(409,
"Can't delete, that product is probably used");
515 'message' =>
'Object deleted'
542 throw new RestException(501,
'Module "Thirdparties" needed for this request');
546 throw new RestException(501,
'Module "Products" needed for this request');
550 throw new RestException(501,
'Multiprices features activation needed for this request');
553 if ($priceLevel < 1 || $priceLevel >
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT')) {
554 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
557 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
558 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
561 $result = $this->company->fetch(
$id);
563 throw new RestException(404,
'Thirdparty '.
$id.
' not found');
566 if (empty($result)) {
567 throw new RestException(500,
'Error fetching thirdparty '.
$id, array_merge(array($this->company->error), $this->company->errors));
571 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
574 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
576 throw new RestException(500,
'Error setting new price level for thirdparty '.
$id, array($this->company->db->lasterror()));
595 public function getRepresentative(
$id)
597 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'reader')) {
598 throw new RestException(403);
600 $result = $this->company->fetch(
$id);
602 throw new RestException(404,
'Thirdparty not found');
605 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
607 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user);
629 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
630 throw new RestException(403);
632 $result = $this->company->fetch(
$id);
634 throw new RestException(404,
'Thirdparty not found');
636 $usertmp =
new User($this->db);
637 $result = $usertmp->fetch($representative_id);
639 throw new RestException(404,
'User not found');
642 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
644 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
665 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
666 throw new RestException(403);
668 $result = $this->company->fetch(
$id);
670 throw new RestException(404,
'Thirdparty not found');
672 $usertmp =
new User($this->db);
673 $result = $usertmp->fetch($representative_id);
675 throw new RestException(404,
'User not found');
678 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
680 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
703 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
705 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
706 throw new RestException(403);
709 $result = $this->company->fetch(
$id);
711 throw new RestException(404,
'Thirdparty not found');
716 throw new RestException(404,
'Third party not allowed for login '.DolibarrApiAccess::$user->login);
721 $arrayofcateg = $categories->getListForItem(
$id,
'customer', $sortfield, $sortorder, $limit, $page);
723 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
724 throw new RestException(503,
'Error when retrieve category list : '.$categories->errorsToString());
727 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
731 return $arrayofcateg;
752 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
753 throw new RestException(403);
756 $result = $this->company->fetch(
$id);
758 throw new RestException(404,
'Third party not found');
761 $result = $category->fetch($category_id);
763 throw new RestException(404,
'category not found');
767 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
770 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
773 $category->add_type($this->company,
'customer');
796 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
797 throw new RestException(403);
800 $result = $this->company->fetch(
$id);
802 throw new RestException(404,
'Thirdparty not found');
805 $result = $category->fetch($category_id);
807 throw new RestException(404,
'category not found');
811 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
814 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
817 $category->del_type($this->company,
'customer');
843 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
844 throw new RestException(403);
847 $result = $this->company->fetch(
$id);
849 throw new RestException(404,
'Thirdparty not found');
854 throw new RestException(404,
'Third party not allowed for login '.DolibarrApiAccess::$user->login);
859 $result = $categories->getListForItem(
$id,
'supplier', $sortfield, $sortorder, $limit, $page);
861 if (is_numeric($result) && $result < 0) {
862 throw new RestException(503,
'Error when retrieve category list : '.$categories->errorsToString());
865 if (is_numeric($result) && $result == 0) {
890 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
891 throw new RestException(403);
894 $result = $this->company->fetch(
$id);
896 throw new RestException(404,
'Thirdparty not found');
899 $result = $category->fetch($category_id);
901 throw new RestException(404,
'category not found');
905 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
908 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
911 $category->add_type($this->company,
'supplier');
934 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
935 throw new RestException(403);
938 $result = $this->company->fetch(
$id);
940 throw new RestException(404,
'Thirdparty not found');
943 $result = $category->fetch($category_id);
945 throw new RestException(404,
'category not found');
949 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
952 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
955 $category->del_type($this->company,
'supplier');
981 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
982 throw new RestException(403);
986 throw new RestException(400,
'Thirdparty ID is mandatory');
990 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
993 $result = $this->company->fetch(
$id);
995 throw new RestException(404,
'Thirdparty not found');
998 $result = $this->company->getOutstandingProposals($mode);
1000 unset($result[
'total_ht']);
1001 unset($result[
'total_ttc']);
1027 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1028 throw new RestException(403);
1032 throw new RestException(400,
'Thirdparty ID is mandatory');
1036 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1039 $result = $this->company->fetch(
$id);
1041 throw new RestException(404,
'Thirdparty not found');
1044 $result = $this->company->getOutstandingOrders($mode);
1046 unset($result[
'total_ht']);
1047 unset($result[
'total_ttc']);
1072 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1073 throw new RestException(403);
1077 throw new RestException(400,
'Thirdparty ID is mandatory');
1081 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1084 $result = $this->company->fetch(
$id);
1086 throw new RestException(404,
'Thirdparty not found');
1089 $result = $this->company->getOutstandingBills($mode);
1091 unset($result[
'total_ht']);
1092 unset($result[
'total_ttc']);
1117 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1118 throw new RestException(403);
1122 throw new RestException(400,
'Thirdparty ID is mandatory');
1126 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1129 $result = $this->company->fetch(
$id);
1131 throw new RestException(404,
'Thirdparty not found');
1134 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1167 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1168 throw new RestException(403);
1172 throw new RestException(400,
'Thirdparty ID is mandatory');
1176 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1179 $result = $this->company->fetch(
$id);
1181 throw new RestException(404,
'Thirdparty not found');
1185 if ($mode ===
'customer') {
1186 $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";
1187 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1188 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as f ON f.rowid = re.fk_facture_source";
1189 $sql .=
" WHERE re.fk_soc = ".((int)
$id);
1190 if ($filter ==
"available") {
1191 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1193 if ($filter ==
"used") {
1194 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1196 } elseif ($mode ===
'supplier') {
1197 $sql =
"SELECT f.ref, f.type as factype, re.fk_invoice_supplier_source, re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc, re.description, re.fk_invoice_supplier, re.fk_invoice_supplier_line";
1198 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1199 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_fourn as f ON f.rowid = re.fk_invoice_supplier_source";
1200 $sql .=
" WHERE f.rowid = re.fk_invoice_supplier_source AND re.fk_soc = ".((int)
$id);
1201 if ($filter ==
"available") {
1202 $sql .=
" AND re.fk_invoice_supplier IS NULL AND re.fk_invoice_supplier_line IS NULL";
1204 if ($filter ==
"used") {
1205 $sql .=
" AND (re.fk_invoice_supplier IS NOT NULL OR re.fk_invoice_supplier_line IS NOT NULL)";
1209 $sql .= $this->db->order($sortfield, $sortorder);
1211 $result = $this->db->query($sql);
1213 throw new RestException(503, $this->db->lasterror());
1216 while ($obj = $this->db->fetch_object($result)) {
1250 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1251 throw new RestException(403);
1256 throw new RestException(400,
'Thirdparty ID is mandatory');
1258 if (!isset($request_data[
'amount'])) {
1259 throw new RestException(400,
'Missing required field: amount');
1261 if (!isset($request_data[
'description'])) {
1262 throw new RestException(400,
'Missing required field: description');
1267 throw new RestException(401,
'Access not allowed for login'.DolibarrApiAccess::$user->login);
1271 if ($this->company->fetch(
$id) <= 0) {
1272 throw new RestException(404,
'Error creating discount, thirdparty not found');
1277 if (!is_numeric($request_data[
'amount']) || $request_data[
'amount'] <= 0) {
1278 throw new RestException(400,
'Invalid amount_ht: must be a positive number');
1280 $amount = (float) $request_data[
'amount'];
1283 if (isset($request_data[
'tva_tx']) && (!is_numeric($request_data[
'tva_tx']) || $request_data[
'tva_tx'] < 0)) {
1284 throw new RestException(400,
'Invalid tva_tx: must be a positive number or zero');
1286 $tva_tx = isset($request_data[
'tva_tx']) ? (float) $request_data[
'tva_tx'] : 0;
1289 $price_base_type =
'HT';
1290 if (isset($request_data[
'price_base_type'])) {
1291 $price_base_type = strtoupper($request_data[
'price_base_type']);
1292 if ($price_base_type !==
'HT' && $price_base_type !==
'TTC') {
1293 throw new RestException(400,
'Invalid price_base_type: must be "HT" or "TTC"');
1299 if (isset($request_data[
'discount_type'])) {
1300 $discount_type = (int) $request_data[
'discount_type'];
1301 if ($discount_type !== 0 && $discount_type !== 1) {
1302 throw new RestException(400,
'Invalid discount_type: must be 0 (customer) or 1 (supplier)');
1307 $description = $request_data[
'description'];
1308 if (empty(trim($description))) {
1309 throw new RestException(400,
'Description cannot be empty');
1313 $vatrate = (string) $tva_tx;
1314 if (isset($request_data[
'vat_src_code']) && !empty($request_data[
'vat_src_code'])) {
1315 $vatrate = $tva_tx .
' (' . $request_data[
'vat_src_code'] .
')';
1321 $result = $this->company->set_remise_except($amount, DolibarrApiAccess::$user, $description, $vatrate, $discount_type, $price_base_type);
1324 $this->db->commit();
1327 $this->db->rollback();
1328 throw new RestException(500,
'Error creating discount: '.$this->company->error, array_merge(array($this->company->error), $this->company->errors));
1359 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer') || !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1360 throw new RestException(403);
1364 throw new RestException(400,
'Thirdparty ID is mandatory');
1366 if (empty($discountid)) {
1367 throw new RestException(400,
'Discount ID is mandatory');
1369 if (empty($amount_ttc_1) || empty($amount_ttc_2)) {
1370 throw new RestException(400,
'Amount are mandatory');
1374 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1377 $result = $this->company->fetch(
$id);
1379 throw new RestException(404,
'Thirdparty not found');
1381 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1383 $res = $discount->fetch($discountid);
1385 throw new RestException(404,
'Discount not found');
1387 if ($discount->socid !=
$id) {
1388 throw new RestException(405,
'Discount not owned by this thirdpartie');
1391 if (
price2num((
float) $amount_ttc_1 + (
float) $amount_ttc_2) != $discount->amount_ttc) {
1392 throw new RestException(405,
'Sum of the 2 discounts is different that the original discount');
1394 if ($discount->fk_facture_line) {
1395 throw new RestException(409,
'Discount is already used');
1401 $newdiscount1->fk_facture_source = $discount->fk_facture_source;
1402 $newdiscount2->fk_facture_source = $discount->fk_facture_source;
1403 $newdiscount1->fk_facture = $discount->fk_facture;
1404 $newdiscount2->fk_facture = $discount->fk_facture;
1405 $newdiscount1->fk_facture_line = $discount->fk_facture_line;
1406 $newdiscount2->fk_facture_line = $discount->fk_facture_line;
1407 $newdiscount1->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1408 $newdiscount2->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1409 $newdiscount1->fk_invoice_supplier = $discount->fk_invoice_supplier;
1410 $newdiscount2->fk_invoice_supplier = $discount->fk_invoice_supplier;
1411 $newdiscount1->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1412 $newdiscount2->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1413 if ($discount->description ==
'(CREDIT_NOTE)' || $discount->description ==
'(DEPOSIT)') {
1414 $newdiscount1->description = $discount->description;
1415 $newdiscount2->description = $discount->description;
1417 $newdiscount1->description = $discount->description.
' (1)';
1418 $newdiscount2->description = $discount->description.
' (2)';
1421 $newdiscount1->fk_user = $discount->fk_user;
1422 $newdiscount2->fk_user = $discount->fk_user;
1423 $newdiscount1->fk_soc = $discount->fk_soc;
1424 $newdiscount1->socid = $discount->socid;
1425 $newdiscount2->fk_soc = $discount->fk_soc;
1426 $newdiscount2->socid = $discount->socid;
1427 $newdiscount1->discount_type = $discount->discount_type;
1428 $newdiscount2->discount_type = $discount->discount_type;
1429 $newdiscount1->datec = $discount->datec;
1430 $newdiscount2->datec = $discount->datec;
1431 $newdiscount1->tva_tx = $discount->tva_tx;
1432 $newdiscount2->tva_tx = $discount->tva_tx;
1433 $newdiscount1->vat_src_code = $discount->vat_src_code;
1434 $newdiscount2->vat_src_code = $discount->vat_src_code;
1435 $newdiscount1->amount_ttc = $amount_ttc_1;
1436 $newdiscount2->amount_ttc =
price2num($discount->amount_ttc - $newdiscount1->amount_ttc);
1437 $newdiscount1->amount_ht =
price2num($newdiscount1->amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1438 $newdiscount2->amount_ht =
price2num($newdiscount2->amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1439 $newdiscount1->amount_tva =
price2num($newdiscount1->amount_ttc - $newdiscount1->amount_ht);
1440 $newdiscount2->amount_tva =
price2num($newdiscount2->amount_ttc - $newdiscount2->amount_ht);
1442 $newdiscount1->multicurrency_amount_ttc = (float) $amount_ttc_1 * ($discount->multicurrency_amount_ttc / $discount->amount_ttc);
1443 $newdiscount2->multicurrency_amount_ttc =
price2num($discount->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ttc);
1444 $newdiscount1->multicurrency_amount_ht =
price2num($newdiscount1->multicurrency_amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1445 $newdiscount2->multicurrency_amount_ht =
price2num($newdiscount2->multicurrency_amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1446 $newdiscount1->multicurrency_amount_tva =
price2num($newdiscount1->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ht);
1447 $newdiscount2->multicurrency_amount_tva =
price2num($newdiscount2->multicurrency_amount_ttc - $newdiscount2->multicurrency_amount_ht);
1451 $newdiscount1->total_ttc = (float)
price2num($newdiscount1->amount_ttc);
1452 $newdiscount1->total_ht = (float)
price2num($newdiscount1->amount_ht);
1453 $newdiscount1->total_tva = (float)
price2num($newdiscount1->amount_tva);
1454 $newdiscount2->total_ttc = (float)
price2num($newdiscount2->amount_ttc);
1455 $newdiscount2->total_ht = (float)
price2num($newdiscount2->amount_ht);
1456 $newdiscount2->total_tva = (float)
price2num($newdiscount2->amount_tva);
1457 $newdiscount1->multicurrency_total_ttc = (float)
price2num($newdiscount1->multicurrency_amount_ttc);
1458 $newdiscount1->multicurrency_total_ht = (float)
price2num($newdiscount1->multicurrency_amount_ht);
1459 $newdiscount1->multicurrency_total_tva = (float)
price2num($newdiscount1->multicurrency_amount_tva);
1460 $newdiscount2->multicurrency_total_ttc = (float)
price2num($newdiscount2->multicurrency_amount_ttc);
1461 $newdiscount2->multicurrency_total_ht = (float)
price2num($newdiscount2->multicurrency_amount_ht);
1462 $newdiscount2->multicurrency_total_tva = (float)
price2num($newdiscount2->multicurrency_amount_tva);
1466 $discount->fk_facture_source = 0;
1468 $discount->fk_invoice_supplier_source = 0;
1469 $res = $discount->delete(DolibarrApiAccess::$user);
1470 $newid1 = $newdiscount1->create(DolibarrApiAccess::$user);
1471 $newid2 = $newdiscount2->create(DolibarrApiAccess::$user);
1472 if ($res <= 0 || $newid1 <= 0 || $newid2 <= 0) {
1473 $this->db->rollback();
1474 throw new RestException(500,
'Operation fail');
1477 $this->db->commit();
1479 $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";
1480 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1481 $sql .=
" WHERE re.rowid IN (".((int) $newid1).
",".((int) $newid2).
") AND f.rowid = re.fk_facture_source AND re.fk_soc = ".((int)
$id);
1483 $sql .= $this->db->order(
"f.type",
"ASC");
1485 $result = $this->db->query($sql);
1487 throw new RestException(503, $this->db->lasterror());
1490 while ($obj = $this->db->fetch_object($result)) {
1518 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1519 throw new RestException(403);
1522 throw new RestException(400,
'Thirdparty ID is mandatory');
1526 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1534 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1535 $invoice =
new Facture($this->db);
1536 $result = $invoice->list_replacable_invoices(
$id);
1538 throw new RestException(405, $invoice->errorsToString());
1567 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1568 throw new RestException(403);
1571 throw new RestException(400,
'Thirdparty ID is mandatory');
1575 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1583 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1584 $invoice =
new Facture($this->db);
1585 $result = $invoice->list_qualified_avoir_invoices(
$id);
1586 if (!is_array($result) && $result < 0) {
1587 throw new RestException(405, $invoice->errorsToString());
1611 throw new RestException(400,
'Thirdparty ID is mandatory');
1613 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1614 throw new RestException(403);
1617 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1624 $sql =
"SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1625 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1627 $sql .=
" WHERE fk_soc = ".((int)
$id);
1630 $result = $this->db->query($sql);
1631 if ($this->db->num_rows($result) == 0) {
1632 throw new RestException(404,
'Notification not found');
1637 $notifications = array();
1641 $num = $this->db->num_rows($result);
1645 $obj = $this->db->fetch_object($result);
1646 $notifications[] = $obj;
1650 throw new RestException(404,
'No notifications found');
1653 $fields = array(
'id',
'socid',
'event',
'contact_id',
'datec',
'tms',
'type');
1655 $returnNotifications = array();
1657 foreach ($notifications as $notification) {
1659 foreach ($notification as $key => $value) {
1660 if (in_array($key, $fields)) {
1664 $returnNotifications[] =
$object;
1668 return $returnNotifications;
1689 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1690 throw new RestException(403,
"User has no right to update thirdparties");
1694 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1697 if ($this->company->fetch(
$id) <= 0) {
1698 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1700 $notification =
new Notify($this->db);
1702 $notification->socid =
$id;
1704 foreach ($request_data as $field => $value) {
1705 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1708 $event = $notification->event;
1710 throw new RestException(500,
'Error creating Thirdparty Notification, request_data missing event');
1712 $socid = $notification->socid;
1713 $contact_id = $notification->contact_id;
1715 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1716 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1717 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1718 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1719 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1721 $exists_result = $this->db->query($exists_sql);
1722 if ($this->db->num_rows($exists_result) > 0) {
1723 throw new RestException(403,
'Notification already exists');
1726 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1727 throw new RestException(500,
'Error creating Thirdparty Notification');
1730 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1731 throw new RestException(500,
'Error updating values');
1757 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1758 throw new RestException(403,
"User has no right to update thirdparties");
1762 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1765 if ($this->company->fetch(
$id) <= 0) {
1766 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1768 $notification =
new Notify($this->db);
1769 $notification->socid =
$id;
1771 $sql =
"SELECT t.rowid as id FROM ".MAIN_DB_PREFIX.
"c_action_trigger as t";
1772 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
1774 $result = $this->db->query($sql);
1775 if ($this->db->num_rows($result) == 0) {
1776 throw new RestException(404,
'Action Trigger code not found');
1779 $notification->event = $this->db->fetch_row($result)[0];
1780 foreach ($request_data as $field => $value) {
1781 if ($field ===
'event') {
1782 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains event key');
1784 if ($field ===
'fk_action') {
1785 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains fk_action key');
1787 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1790 $event = $notification->event;
1791 $socid = $notification->socid;
1792 $contact_id = $notification->contact_id;
1794 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1795 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1796 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1797 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1798 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1800 $exists_result = $this->db->query($exists_sql);
1801 if ($this->db->num_rows($exists_result) > 0) {
1802 throw new RestException(403,
'Notification already exists');
1805 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1806 throw new RestException(500,
'Error creating Thirdparty Notification, are request_data well formed?');
1809 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1810 throw new RestException(500,
'Error updating values');
1832 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1833 throw new RestException(403);
1837 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1840 $notification =
new Notify($this->db);
1842 $notification->fetch($notification_id);
1844 $socid = (int) $notification->socid;
1846 if ($socid ==
$id) {
1847 return $notification->delete(DolibarrApiAccess::$user);
1849 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1872 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1873 throw new RestException(403,
"User has no right to update thirdparties");
1877 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1880 if ($this->company->fetch(
$id) <= 0) {
1881 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1883 $notification =
new Notify($this->db);
1886 $notification->fetch($notification_id,
$id);
1888 if ($notification->socid !=
$id) {
1889 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1892 foreach ($request_data as $field => $value) {
1893 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1896 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1897 throw new RestException(500,
'Error updating values');
1920 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1921 throw new RestException(403);
1924 throw new RestException(400,
'Thirdparty ID is mandatory');
1928 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1935 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1936 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1937 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1939 $sql .=
" WHERE fk_soc = ".((int)
$id);
1942 $result = $this->db->query($sql);
1944 if ($this->db->num_rows($result) == 0) {
1945 throw new RestException(404,
'Account not found');
1950 $accounts = array();
1954 $num = $this->db->num_rows($result);
1958 $obj = $this->db->fetch_object($result);
1961 if ($account->fetch($obj->rowid)) {
1962 $accounts[] = $account;
1967 throw new RestException(404,
'Account not found');
1971 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum',
'payment_reference');
1973 $returnAccounts = array();
1975 foreach ($accounts as $account) {
1977 foreach ($account as $key => $value) {
1978 if (in_array($key, $fields)) {
1979 if ($key ==
'iban') {
1989 return $returnAccounts;
2010 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2011 throw new RestException(403);
2013 if ($this->company->fetch(
$id) <= 0) {
2014 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
2018 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2023 $account->socid =
$id;
2025 foreach ($request_data as $field => $value) {
2026 if ($field ===
'caller') {
2028 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2032 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
2035 if ($account->create(DolibarrApiAccess::$user) < 0) {
2036 throw new RestException(500,
'Error creating Company Bank account');
2039 if (empty($account->rum)) {
2040 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
2042 $account->rum = $prelevement->buildRumNumber((
string) $this->company->code_client, $account->datec, (
string) $account->id);
2043 $account->date_rum =
dol_now();
2046 if ($account->update(DolibarrApiAccess::$user) < 0) {
2047 throw new RestException(500,
'Error updating values');
2072 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2073 throw new RestException(403);
2075 if ($this->company->fetch(
$id) <= 0) {
2076 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
2080 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2086 $account->fetch($bankaccount_id,
'',
$id, -1,
'');
2088 if ($account->socid !=
$id) {
2089 throw new RestException(403);
2093 foreach ($request_data as $field => $value) {
2094 if ($field ===
'caller') {
2096 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2103 if (empty($account->rum)) {
2104 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
2106 $account->rum = $prelevement->buildRumNumber((
string) $this->company->code_client, $account->datec, (
string) $account->id);
2107 $account->date_rum =
dol_now();
2110 if ($account->update(DolibarrApiAccess::$user) < 0) {
2111 throw new RestException(500,
'Error updating values');
2133 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2134 throw new RestException(403);
2138 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2143 $account->fetch($bankaccount_id);
2145 $socid = (int) $account->socid;
2147 if ($socid ==
$id) {
2148 return $account->delete(DolibarrApiAccess::$user);
2150 throw new RestException(403,
"Not allowed due to bad consistency of input data");
2174 global
$conf, $langs;
2176 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
2178 if ($this->company->fetch(
$id) <= 0) {
2179 throw new RestException(404,
'Thirdparty not found');
2182 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2183 throw new RestException(403);
2187 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2190 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
2192 $this->company->fk_bank = $this->company->fk_account;
2195 $outputlangs = $langs;
2200 if (isset($this->company->thirdparty->default_lang)) {
2201 $newlang = $this->company->thirdparty->default_lang;
2202 } elseif (isset($this->company->default_lang)) {
2203 $newlang = $this->company->default_lang;
2206 if (!empty($newlang)) {
2208 $outputlangs->setDefaultLang($newlang);
2211 $sql =
"SELECT rowid";
2212 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
2214 $sql .=
" WHERE fk_soc = ".((int)
$id);
2216 if ($companybankid) {
2217 $sql .=
" AND rowid = ".((int) $companybankid);
2221 $accounts = array();
2223 $result = $this->db->query($sql);
2225 if ($this->db->num_rows($result) == 0) {
2226 throw new RestException(404,
'Bank account not found');
2229 $num = $this->db->num_rows($result);
2233 $obj = $this->db->fetch_object($result);
2236 if ($account->fetch($obj->rowid)) {
2237 $accounts[] = $account;
2242 throw new RestException(500,
'Sql error '.$this->db->lasterror());
2245 $moreparams = array(
2246 'use_companybankid' => $accounts[0]->
id,
2247 'force_dir_output' =>
$conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName((
string) $this->company->id)
2250 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
2253 return array(
"success" => $result);
2255 throw new RestException(500,
'Error generating the document '.$this->company->errorsToString());
2278 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2279 throw new RestException(403);
2283 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2289 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2290 $sql .=
" WHERE fk_soc = ".((int)
$id);
2292 $sql .=
" AND site ='".$this->db->escape($site).
"'";
2295 $result = $this->db->query($sql);
2297 if ($result && $this->db->num_rows($result) == 0) {
2298 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
2303 $accounts = array();
2306 $num = $this->db->num_rows($result);
2310 $obj = $this->db->fetch_object($result);
2313 if ($account->fetch($obj->rowid)) {
2314 $accounts[] = $account;
2319 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
2321 $returnAccounts = array();
2323 foreach ($accounts as $account) {
2325 foreach ($account as $key => $value) {
2326 if (in_array($key, $fields)) {
2333 return $returnAccounts;
2355 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2356 throw new RestException(403);
2359 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2360 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
2361 $sql .=
" AND entity IN (".getEntity(
'societe').
")";
2363 $result = $this->db->query($sql);
2365 if ($result && $this->db->num_rows($result) == 1) {
2366 $obj = $this->db->fetch_object($result);
2367 $returnThirdparty = $this->
_fetch($obj->fk_soc);
2369 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
2373 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2376 return $returnThirdparty;
2404 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2405 throw new RestException(403);
2409 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2412 if (!isset($request_data[
'site'])) {
2413 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
2416 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
2417 $result = $this->db->query($sql);
2419 if ($result && $this->db->num_rows($result) == 0) {
2421 if (!isset($request_data[
'login'])) {
2422 $account->login =
"";
2424 $account->fk_soc =
$id;
2426 foreach ($request_data as $field => $value) {
2427 if ($field ===
'caller') {
2429 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2436 if ($account->create(DolibarrApiAccess::$user) < 0) {
2437 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
2444 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
2476 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2477 throw new RestException(403);
2481 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2484 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2485 $result = $this->db->query($sql);
2488 if ($result && $this->db->num_rows($result) == 0) {
2489 if (!isset($request_data[
'key_account'])) {
2490 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
2493 if (!isset($request_data[
'login'])) {
2494 $account->login =
"";
2497 foreach ($request_data as $field => $value) {
2498 if ($field ===
'caller') {
2500 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2507 $account->fk_soc =
$id;
2508 $account->site = $site;
2510 if ($account->create(DolibarrApiAccess::$user) < 0) {
2511 throw new RestException(500,
'Error creating SocieteAccount entity.');
2515 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2516 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2517 $result = $this->db->query($sql);
2519 if ($result && $this->db->num_rows($result) !== 0) {
2520 throw new RestException(409,
"You are trying to update this thirdparty Account for $site to ".$request_data[
'site'].
" but another Account already exists with this site key.");
2524 $obj = $this->db->fetch_object($result);
2527 $account->id = $obj->rowid;
2528 $account->fk_soc =
$id;
2529 $account->site = $site;
2530 if (!isset($request_data[
'login'])) {
2531 $account->login =
"";
2533 $account->fk_user_creat = $obj->fk_user_creat;
2534 $account->date_creation = $obj->date_creation;
2536 foreach ($request_data as $field => $value) {
2537 if ($field ===
'caller') {
2539 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2546 if ($account->update(DolibarrApiAccess::$user) < 0) {
2547 throw new RestException(500,
'Error updating SocieteAccount entity.');
2578 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2579 throw new RestException(403);
2583 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2586 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2587 $result = $this->db->query($sql);
2589 if ($result && $this->db->num_rows($result) == 0) {
2590 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
2593 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2594 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2595 $result = $this->db->query($sql);
2597 if ($result && $this->db->num_rows($result) !== 0) {
2598 throw new RestException(409,
"You are trying to update this thirdparty Account for ".$site.
" to ".$request_data[
'site'].
" but another Account already exists for this thirdparty with this site key.");
2602 $obj = $this->db->fetch_object($result);
2604 $account->fetch($obj->rowid);
2606 foreach ($request_data as $field => $value) {
2607 if ($field ===
'caller') {
2609 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2616 if ($account->update(DolibarrApiAccess::$user) < 0) {
2617 throw new RestException(500,
'Error updating SocieteAccount account');
2646 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2647 throw new RestException(403);
2651 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2654 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2655 $result = $this->db->query($sql);
2657 if ($result && $this->db->num_rows($result) == 0) {
2658 throw new RestException(404);
2660 $obj = $this->db->fetch_object($result);
2662 $account->fetch($obj->rowid);
2664 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2665 throw new RestException(500,
"Error while deleting $site account attached to this third party");
2687 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2688 throw new RestException(403);
2692 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2699 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2700 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id);
2702 $result = $this->db->query($sql);
2704 if ($result && $this->db->num_rows($result) == 0) {
2705 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
2710 $num = $this->db->num_rows($result);
2714 $obj = $this->db->fetch_object($result);
2716 $account->fetch($obj->rowid);
2718 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2719 throw new RestException(500,
'Error while deleting account attached to this third party');
2745 unset(
$object->departement_code);
2754 unset(
$object->commercial_id);
2758 unset(
$object->total_localtax1);
2759 unset(
$object->total_localtax2);
2765 unset(
$object->fk_delivery_address);
2780 if ($data ===
null) {
2783 $thirdparty = array();
2784 foreach (Thirdparties::$FIELDS as $field) {
2785 if (!isset($data[$field])) {
2786 throw new RestException(400,
"$field field missing");
2788 $thirdparty[$field] = $data[$field];
2816 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2818 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2819 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2823 $result = $this->company->initAsSpecimen();
2825 $result = $this->company->fetch((
int) $rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2828 throw new RestException(404,
'Thirdparty not found');
2832 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2835 $this->company->getNoEmail();
2839 $filterabsolutediscount =
"fk_facture_source IS NULL";
2840 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2842 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2843 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2846 $absolute_discount = $this->company->getAvailableDiscounts(
null, $filterabsolutediscount);
2847 $absolute_creditnote = $this->company->getAvailableDiscounts(
null, $filtercreditnote);
2848 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2849 $this->company->absolute_creditnote =
price2num($absolute_creditnote,
'MT');
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage withdrawal receipts.
Class to manage categories.
Class to manage bank accounts description of third parties.
Class to manage absolute discounts.
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
Class to manage invoices.
Class to manage the table of subscription to notifications.
Class for SocieteAccount.
Class to manage third parties objects (customers, suppliers, prospects...)
updateCompanyNotification($id, $notification_id, $request_data=null)
Update a company notification for a third party.
setThirdpartyPriceLevel($id, $priceLevel)
Set a new price level for the given third party.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
getSocieteByAccounts($site, $key_account)
Get a specific third party by account.
getSupplierCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get supplier categories for a third party.
deleteCompanyNotification($id, $notification_id)
Delete a company notification attached to a third party.
getSocieteAccounts($id, $site=null)
Get a specific account attached to a third party.
getOutStandingOrder($id, $mode='customer')
Get outstanding orders for a third party.
addRepresentative($id, $representative_id)
Add a customer representative to a third party.
getByBarcode($barcode)
Get a third party by barcode.
generateBankAccountDocument($id, $companybankid=null, $model='sepamandate')
Generate a document from a bank account record.
createCompanyNotificationByCode($id, $code, $request_data=null)
Create a company notification for a third party using action trigger code.
getCompanyNotification($id)
Get company notifications for a third party.
addCategory($id, $category_id)
Add a customer category to a third party.
getCompanyBankAccount($id)
Get company bank accounts of a third party.
getInvoicesQualifiedForReplacement($id)
Return invoices qualified to be replaced by another invoice.
post($request_data=null)
Create a third party.
put($id, $request_data=null)
Update third party.
getByEmail($email)
Get properties of a third party by email.
_validate($data)
Validate fields before create or update object.
getFixedAmountDiscounts($id, $mode='customer', $filter="none", $sortfield="f.type", $sortorder='ASC')
Get fixed amount discount of a third party.
addSupplierCategory($id, $category_id)
Add a supplier category to a third party.
merge($id, $idtodelete)
Merge a third party into another third party.
deleteSocieteAccounts($id)
Delete all accounts attached to a third party.
__construct()
Constructor.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get customer categories for a third party.
postSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site account for a third party.
deleteSupplierCategory($id, $category_id)
Remove the link between a category and the third party.
createFixedAmountDiscount($id, $request_data=null)
Create a fixed amount discount for a thirdparty.
deleteRepresentative($id, $representative_id)
Remove the link between a customer representative and a third party.
createCompanyNotification($id, $request_data=null)
Create a company notification for a third party.
putSocieteAccount($id, $site, $request_data=null)
Update specified values of a specific account attached to a third party.
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update a company bank account of a third party.
deleteSocieteAccount($id, $site)
Delete a specific site account attached to a third party.
getInvoicesQualifiedForCreditNote($id)
Return invoices qualified to be corrected by a credit note.
getOutStandingProposals($id, $mode='customer')
Get outstanding proposals for a third party.
_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 a third party.
getOutStandingInvoices($id, $mode='customer')
Get outstanding invoices for a third party.
splitdiscount($id, $discountid, $amount_ttc_1, $amount_ttc_2)
Split a discount in 2 smaller discount.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List third parties.
deleteCompanyBankAccount($id, $bankaccount_id)
Delete a bank account attached to a third party.
createSocieteAccount($id, $request_data=null)
Create and attach a new account to an existing third party.
createCompanyBankAccount($id, $request_data=null)
Create a company bank account for a third party.
deleteCategory($id, $category_id)
Remove the link between a customer category and the third party.
Class to manage translations.
Class to manage Dolibarr users.
dol_now($mode='gmt')
Return date for now.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
isModEnabled($module)
Is Dolibarr module enabled.
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.