28use Luracast\Restler\RestException;
30require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
45 public static $FIELDS = array(
62 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
63 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
64 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
65 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
66 require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
68 $this->company =
new Societe($this->db);
71 static::$FIELDS[] =
'email';
87 public function get(
$id)
110 return $this->
_fetch(
null,
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
129 return $this->
_fetch(
null,
'',
'', $barcode);
158 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
162 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
163 throw new RestException(403);
167 $socids = DolibarrApiAccess::$user->socid ? (
string) DolibarrApiAccess::$user->socid :
'';
171 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
172 $search_sale = DolibarrApiAccess::$user->id;
175 $sql =
"SELECT t.rowid";
176 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
177 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
180 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
182 if (!in_array($mode, array(1, 2, 3))) {
183 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
186 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
188 $sql .=
" AND t.client IN (1, 3)";
189 } elseif ($mode == 2) {
190 $sql .=
" AND t.client IN (2, 3)";
191 } elseif ($mode == 3) {
192 $sql .=
" AND t.client IN (0)";
193 } elseif ($mode == 4) {
194 $sql .=
" AND t.fournisseur IN (1)";
198 if (!empty($mode) && $mode != 4) {
199 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
200 } elseif (!empty($mode) && $mode == 4) {
201 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
203 $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))";
207 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
210 if ($search_sale && $search_sale !=
'-1') {
211 if ($search_sale == -2) {
212 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.rowid', 0, 1);
213 } elseif ($search_sale > 0) {
214 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.rowid', (
int) $search_sale);
222 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
227 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
229 $sql .= $this->db->order($sortfield, $sortorder);
234 $offset = $limit * $page;
236 $sql .= $this->db->plimit($limit + 1, $offset);
239 $result = $this->db->query($sql);
241 $num = $this->db->num_rows($result);
242 $min = min($num, ($limit <= 0 ? $num : $limit));
245 $obj = $this->db->fetch_object($result);
246 $soc_static =
new Societe($this->db);
247 if ($soc_static->fetch($obj->rowid)) {
249 $soc_static->getNoEmail();
256 throw new RestException(503,
'Error when retrieve third parties : '.$this->db->lasterror());
258 if (!count($obj_ret)) {
262 $message =
'No third parties found';
265 $message =
'No customers found';
268 $message =
'No prospects found';
271 $message =
'No other third parties found';
274 $message =
'No suppliers found';
276 throw new RestException(404, $message);
280 if ($pagination_data) {
281 $totalsResult = $this->db->query($sqlTotals);
282 $total = $this->db->fetch_object($totalsResult)->total;
287 $obj_ret[
'data'] = $tmp;
288 $obj_ret[
'pagination'] = [
289 'total' => (int) $total,
291 'page_count' => ceil((
int) $total / $limit),
311 public function post($request_data =
null)
313 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
314 throw new RestException(403);
318 if (!isset($request_data[
'country_id']) && isset($request_data[
'country_code'])) {
319 $field = strlen($request_data[
'country_code']) > 2 ?
'code_iso' :
'code';
320 $id =
dol_getIdFromCode($this->db, $request_data[
'country_code'],
"c_country", $field,
"rowid");
322 throw new RestException(404,
'Country code not found in database: ' . $this->db->error);
324 $request_data[
'country_id'] =
$id;
328 $result = $this->
_validate($request_data);
330 foreach ($request_data as $field => $value) {
331 if ($field ===
'caller') {
333 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
336 if ($field ==
'array_options' && is_array($value)) {
337 $this->company->fetch_optionals();
339 foreach ($value as $index => $val) {
345 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
348 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
349 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
351 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
352 $this->company->setNoEmail($this->company->no_email);
355 return $this->company->id;
375 public function put(
$id, $request_data =
null)
377 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
378 throw new RestException(403);
381 $result = $this->company->fetch(
$id);
383 throw new RestException(404,
'Thirdparty not found');
387 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
390 foreach ($request_data as $field => $value) {
391 if ($field ==
'id') {
394 if ($field ===
'caller') {
396 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
399 if ($field ==
'array_options' && is_array($value)) {
400 foreach ($value as $index => $val) {
405 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
408 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
409 $this->company->setNoEmail($this->company->no_email);
412 if ($this->company->update(
$id, DolibarrApiAccess::$user, 1, 1, 1,
'update', 1) > 0) {
413 return $this->
get(
$id);
415 throw new RestException(500, $this->company->error);
442 if (
$id == $idtodelete) {
443 throw new RestException(400,
'Try to merge a thirdparty into itself');
446 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
447 throw new RestException(403);
450 $result = $this->company->fetch(
$id);
452 throw new RestException(404,
'Thirdparty not found');
456 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
459 $companytoremove =
new Societe($this->db);
460 $result = $companytoremove->fetch($idtodelete);
462 throw new RestException(404,
'Thirdparty not found');
466 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
469 $user = DolibarrApiAccess::$user;
470 $result = $this->company->mergeCompany($companytoremove->id);
472 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.
$id.
'. Enable and read log file for more information.');
475 return $this->
get(
$id);
490 public function delete(
$id)
492 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
493 throw new RestException(403);
495 $result = $this->company->fetch(
$id);
497 throw new RestException(404,
'Thirdparty not found');
500 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
502 $this->company->oldcopy = clone $this->company;
504 $res = $this->company->delete(
$id);
506 throw new RestException(500,
"Can't delete, error occurs");
507 } elseif ($res == 0) {
508 throw new RestException(409,
"Can't delete, that product is probably used");
514 'message' =>
'Object deleted'
541 throw new RestException(501,
'Module "Thirdparties" needed for this request');
545 throw new RestException(501,
'Module "Products" needed for this request');
549 throw new RestException(501,
'Multiprices features activation needed for this request');
552 if ($priceLevel < 1 || $priceLevel >
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT')) {
553 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
556 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
557 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
560 $result = $this->company->fetch(
$id);
562 throw new RestException(404,
'Thirdparty '.
$id.
' not found');
565 if (empty($result)) {
566 throw new RestException(500,
'Error fetching thirdparty '.
$id, array_merge(array($this->company->error), $this->company->errors));
570 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
573 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
575 throw new RestException(500,
'Error setting new price level for thirdparty '.
$id, array($this->company->db->lasterror()));
594 public function getRepresentative(
$id)
596 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'reader')) {
597 throw new RestException(403);
599 $result = $this->company->fetch(
$id);
601 throw new RestException(404,
'Thirdparty not found');
604 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
606 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user);
628 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
629 throw new RestException(403);
631 $result = $this->company->fetch(
$id);
633 throw new RestException(404,
'Thirdparty not found');
635 $usertmp =
new User($this->db);
636 $result = $usertmp->fetch($representative_id);
638 throw new RestException(404,
'User not found');
641 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
643 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
664 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
665 throw new RestException(403);
667 $result = $this->company->fetch(
$id);
669 throw new RestException(404,
'Thirdparty not found');
671 $usertmp =
new User($this->db);
672 $result = $usertmp->fetch($representative_id);
674 throw new RestException(404,
'User not found');
677 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
679 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
702 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
704 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
705 throw new RestException(403);
708 $result = $this->company->fetch(
$id);
710 throw new RestException(404,
'Thirdparty not found');
715 throw new RestException(404,
'Third party not allowed for login '.DolibarrApiAccess::$user->login);
720 $arrayofcateg = $categories->getListForItem(
$id,
'customer', $sortfield, $sortorder, $limit, $page);
722 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
723 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
726 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
730 return $arrayofcateg;
751 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
752 throw new RestException(403);
755 $result = $this->company->fetch(
$id);
757 throw new RestException(404,
'Third party not found');
760 $result = $category->fetch($category_id);
762 throw new RestException(404,
'category not found');
766 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
769 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
772 $category->add_type($this->company,
'customer');
795 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
796 throw new RestException(403);
799 $result = $this->company->fetch(
$id);
801 throw new RestException(404,
'Thirdparty not found');
804 $result = $category->fetch($category_id);
806 throw new RestException(404,
'category not found');
810 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
813 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
816 $category->del_type($this->company,
'customer');
842 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
843 throw new RestException(403);
846 $result = $this->company->fetch(
$id);
848 throw new RestException(404,
'Thirdparty not found');
853 throw new RestException(404,
'Third party not allowed for login '.DolibarrApiAccess::$user->login);
858 $result = $categories->getListForItem(
$id,
'supplier', $sortfield, $sortorder, $limit, $page);
860 if (is_numeric($result) && $result < 0) {
861 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
864 if (is_numeric($result) && $result == 0) {
889 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
890 throw new RestException(403);
893 $result = $this->company->fetch(
$id);
895 throw new RestException(404,
'Thirdparty not found');
898 $result = $category->fetch($category_id);
900 throw new RestException(404,
'category not found');
904 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
907 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
910 $category->add_type($this->company,
'supplier');
933 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
934 throw new RestException(403);
937 $result = $this->company->fetch(
$id);
939 throw new RestException(404,
'Thirdparty not found');
942 $result = $category->fetch($category_id);
944 throw new RestException(404,
'category not found');
948 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
951 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
954 $category->del_type($this->company,
'supplier');
980 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
981 throw new RestException(403);
985 throw new RestException(400,
'Thirdparty ID is mandatory');
989 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
992 $result = $this->company->fetch(
$id);
994 throw new RestException(404,
'Thirdparty not found');
997 $result = $this->company->getOutstandingProposals($mode);
999 unset($result[
'total_ht']);
1000 unset($result[
'total_ttc']);
1026 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1027 throw new RestException(403);
1031 throw new RestException(400,
'Thirdparty ID is mandatory');
1035 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1038 $result = $this->company->fetch(
$id);
1040 throw new RestException(404,
'Thirdparty not found');
1043 $result = $this->company->getOutstandingOrders($mode);
1045 unset($result[
'total_ht']);
1046 unset($result[
'total_ttc']);
1071 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1072 throw new RestException(403);
1076 throw new RestException(400,
'Thirdparty ID is mandatory');
1080 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1083 $result = $this->company->fetch(
$id);
1085 throw new RestException(404,
'Thirdparty not found');
1088 $result = $this->company->getOutstandingBills($mode);
1090 unset($result[
'total_ht']);
1091 unset($result[
'total_ttc']);
1116 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1117 throw new RestException(403);
1121 throw new RestException(400,
'Thirdparty ID is mandatory');
1125 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1128 $result = $this->company->fetch(
$id);
1130 throw new RestException(404,
'Thirdparty not found');
1133 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1166 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1167 throw new RestException(403);
1171 throw new RestException(400,
'Thirdparty ID is mandatory');
1175 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1178 $result = $this->company->fetch(
$id);
1180 throw new RestException(404,
'Thirdparty not found');
1184 if ($mode ===
'customer') {
1185 $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";
1186 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1187 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as f ON f.rowid = re.fk_facture_source";
1188 $sql .=
" WHERE re.fk_soc = ".((int)
$id);
1189 if ($filter ==
"available") {
1190 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1192 if ($filter ==
"used") {
1193 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1195 } elseif ($mode ===
'supplier') {
1196 $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";
1197 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1198 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_fourn as f ON f.rowid = re.fk_invoice_supplier_source";
1199 $sql .=
" WHERE f.rowid = re.fk_invoice_supplier_source AND re.fk_soc = ".((int)
$id);
1200 if ($filter ==
"available") {
1201 $sql .=
" AND re.fk_invoice_supplier IS NULL AND re.fk_invoice_supplier_line IS NULL";
1203 if ($filter ==
"used") {
1204 $sql .=
" AND (re.fk_invoice_supplier IS NOT NULL OR re.fk_invoice_supplier_line IS NOT NULL)";
1208 $sql .= $this->db->order($sortfield, $sortorder);
1210 $result = $this->db->query($sql);
1212 throw new RestException(503, $this->db->lasterror());
1215 while ($obj = $this->db->fetch_object($result)) {
1249 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1250 throw new RestException(403);
1255 throw new RestException(400,
'Thirdparty ID is mandatory');
1257 if (!isset($request_data[
'amount'])) {
1258 throw new RestException(400,
'Missing required field: amount');
1260 if (!isset($request_data[
'description'])) {
1261 throw new RestException(400,
'Missing required field: description');
1266 throw new RestException(401,
'Access not allowed for login'.DolibarrApiAccess::$user->login);
1270 if ($this->company->fetch(
$id) <= 0) {
1271 throw new RestException(404,
'Error creating discount, thirdparty not found');
1276 if (!is_numeric($request_data[
'amount']) || $request_data[
'amount'] <= 0) {
1277 throw new RestException(400,
'Invalid amount_ht: must be a positive number');
1279 $amount = (float) $request_data[
'amount'];
1282 if (isset($request_data[
'tva_tx']) && (!is_numeric($request_data[
'tva_tx']) || $request_data[
'tva_tx'] < 0)) {
1283 throw new RestException(400,
'Invalid tva_tx: must be a positive number or zero');
1285 $tva_tx = isset($request_data[
'tva_tx']) ? (float) $request_data[
'tva_tx'] : 0;
1288 $price_base_type =
'HT';
1289 if (isset($request_data[
'price_base_type'])) {
1290 $price_base_type = strtoupper($request_data[
'price_base_type']);
1291 if ($price_base_type !==
'HT' && $price_base_type !==
'TTC') {
1292 throw new RestException(400,
'Invalid price_base_type: must be "HT" or "TTC"');
1298 if (isset($request_data[
'discount_type'])) {
1299 $discount_type = (int) $request_data[
'discount_type'];
1300 if ($discount_type !== 0 && $discount_type !== 1) {
1301 throw new RestException(400,
'Invalid discount_type: must be 0 (customer) or 1 (supplier)');
1306 $description = $request_data[
'description'];
1307 if (empty(trim($description))) {
1308 throw new RestException(400,
'Description cannot be empty');
1313 if (isset($request_data[
'vat_src_code']) && !empty($request_data[
'vat_src_code'])) {
1314 $vatrate = $tva_tx .
' (' . $request_data[
'vat_src_code'] .
')';
1320 $result = $this->company->set_remise_except($amount, DolibarrApiAccess::$user, $description, $vatrate, $discount_type, $price_base_type);
1323 $this->db->commit();
1326 $this->db->rollback();
1327 throw new RestException(500,
'Error creating discount: '.$this->company->error, array_merge(array($this->company->error), $this->company->errors));
1358 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer') || !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1359 throw new RestException(403);
1363 throw new RestException(400,
'Thirdparty ID is mandatory');
1365 if (empty($discountid)) {
1366 throw new RestException(400,
'Discount ID is mandatory');
1368 if (empty($amount_ttc_1) || empty($amount_ttc_2)) {
1369 throw new RestException(400,
'Amount are mandatory');
1373 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1376 $result = $this->company->fetch(
$id);
1378 throw new RestException(404,
'Thirdparty not found');
1380 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1382 $res = $discount->fetch($discountid);
1384 throw new RestException(404,
'Discount not found');
1386 if ($discount->socid !=
$id) {
1387 throw new RestException(405,
'Discount not owned by this thirdpartie');
1390 if (
price2num((
float) $amount_ttc_1 + (
float) $amount_ttc_2) != $discount->amount_ttc) {
1391 throw new RestException(405,
'Sum of the 2 discounts is different that the original discount');
1393 if ($discount->fk_facture_line) {
1394 throw new RestException(409,
'Discount is already used');
1400 $newdiscount1->fk_facture_source = $discount->fk_facture_source;
1401 $newdiscount2->fk_facture_source = $discount->fk_facture_source;
1402 $newdiscount1->fk_facture = $discount->fk_facture;
1403 $newdiscount2->fk_facture = $discount->fk_facture;
1404 $newdiscount1->fk_facture_line = $discount->fk_facture_line;
1405 $newdiscount2->fk_facture_line = $discount->fk_facture_line;
1406 $newdiscount1->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1407 $newdiscount2->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1408 $newdiscount1->fk_invoice_supplier = $discount->fk_invoice_supplier;
1409 $newdiscount2->fk_invoice_supplier = $discount->fk_invoice_supplier;
1410 $newdiscount1->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1411 $newdiscount2->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1412 if ($discount->description ==
'(CREDIT_NOTE)' || $discount->description ==
'(DEPOSIT)') {
1413 $newdiscount1->description = $discount->description;
1414 $newdiscount2->description = $discount->description;
1416 $newdiscount1->description = $discount->description.
' (1)';
1417 $newdiscount2->description = $discount->description.
' (2)';
1420 $newdiscount1->fk_user = $discount->fk_user;
1421 $newdiscount2->fk_user = $discount->fk_user;
1422 $newdiscount1->fk_soc = $discount->fk_soc;
1423 $newdiscount1->socid = $discount->socid;
1424 $newdiscount2->fk_soc = $discount->fk_soc;
1425 $newdiscount2->socid = $discount->socid;
1426 $newdiscount1->discount_type = $discount->discount_type;
1427 $newdiscount2->discount_type = $discount->discount_type;
1428 $newdiscount1->datec = $discount->datec;
1429 $newdiscount2->datec = $discount->datec;
1430 $newdiscount1->tva_tx = $discount->tva_tx;
1431 $newdiscount2->tva_tx = $discount->tva_tx;
1432 $newdiscount1->vat_src_code = $discount->vat_src_code;
1433 $newdiscount2->vat_src_code = $discount->vat_src_code;
1434 $newdiscount1->amount_ttc = $amount_ttc_1;
1435 $newdiscount2->amount_ttc =
price2num($discount->amount_ttc - $newdiscount1->amount_ttc);
1436 $newdiscount1->amount_ht =
price2num($newdiscount1->amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1437 $newdiscount2->amount_ht =
price2num($newdiscount2->amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1438 $newdiscount1->amount_tva =
price2num($newdiscount1->amount_ttc - $newdiscount1->amount_ht);
1439 $newdiscount2->amount_tva =
price2num($newdiscount2->amount_ttc - $newdiscount2->amount_ht);
1441 $newdiscount1->multicurrency_amount_ttc = (float) $amount_ttc_1 * ($discount->multicurrency_amount_ttc / $discount->amount_ttc);
1442 $newdiscount2->multicurrency_amount_ttc =
price2num($discount->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ttc);
1443 $newdiscount1->multicurrency_amount_ht =
price2num($newdiscount1->multicurrency_amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1444 $newdiscount2->multicurrency_amount_ht =
price2num($newdiscount2->multicurrency_amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1445 $newdiscount1->multicurrency_amount_tva =
price2num($newdiscount1->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ht);
1446 $newdiscount2->multicurrency_amount_tva =
price2num($newdiscount2->multicurrency_amount_ttc - $newdiscount2->multicurrency_amount_ht);
1450 $newdiscount1->total_ttc = (float)
price2num($newdiscount1->amount_ttc);
1451 $newdiscount1->total_ht = (float)
price2num($newdiscount1->amount_ht);
1452 $newdiscount1->total_tva = (float)
price2num($newdiscount1->amount_tva);
1453 $newdiscount2->total_ttc = (float)
price2num($newdiscount2->amount_ttc);
1454 $newdiscount2->total_ht = (float)
price2num($newdiscount2->amount_ht);
1455 $newdiscount2->total_tva = (float)
price2num($newdiscount2->amount_tva);
1456 $newdiscount1->multicurrency_total_ttc = (float)
price2num($newdiscount1->multicurrency_amount_ttc);
1457 $newdiscount1->multicurrency_total_ht = (float)
price2num($newdiscount1->multicurrency_amount_ht);
1458 $newdiscount1->multicurrency_total_tva = (float)
price2num($newdiscount1->multicurrency_amount_tva);
1459 $newdiscount2->multicurrency_total_ttc = (float)
price2num($newdiscount2->multicurrency_amount_ttc);
1460 $newdiscount2->multicurrency_total_ht = (float)
price2num($newdiscount2->multicurrency_amount_ht);
1461 $newdiscount2->multicurrency_total_tva = (float)
price2num($newdiscount2->multicurrency_amount_tva);
1465 $discount->fk_facture_source = 0;
1467 $discount->fk_invoice_supplier_source = 0;
1468 $res = $discount->delete(DolibarrApiAccess::$user);
1469 $newid1 = $newdiscount1->create(DolibarrApiAccess::$user);
1470 $newid2 = $newdiscount2->create(DolibarrApiAccess::$user);
1471 if ($res <= 0 || $newid1 <= 0 || $newid2 <= 0) {
1472 $this->db->rollback();
1473 throw new RestException(500,
'Operation fail');
1476 $this->db->commit();
1478 $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";
1479 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1480 $sql .=
" WHERE re.rowid IN (".((int) $newid1).
",".((int) $newid2).
") AND f.rowid = re.fk_facture_source AND re.fk_soc = ".((int)
$id);
1482 $sql .= $this->db->order(
"f.type",
"ASC");
1484 $result = $this->db->query($sql);
1486 throw new RestException(503, $this->db->lasterror());
1489 while ($obj = $this->db->fetch_object($result)) {
1517 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1518 throw new RestException(403);
1521 throw new RestException(400,
'Thirdparty ID is mandatory');
1525 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1533 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1534 $invoice =
new Facture($this->db);
1535 $result = $invoice->list_replacable_invoices(
$id);
1537 throw new RestException(405, $invoice->error);
1566 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1567 throw new RestException(403);
1570 throw new RestException(400,
'Thirdparty ID is mandatory');
1574 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1582 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1583 $invoice =
new Facture($this->db);
1584 $result = $invoice->list_qualified_avoir_invoices(
$id);
1585 if (!is_array($result) && $result < 0) {
1586 throw new RestException(405, $invoice->error);
1610 throw new RestException(400,
'Thirdparty ID is mandatory');
1612 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1613 throw new RestException(403);
1616 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1623 $sql =
"SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1624 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1626 $sql .=
" WHERE fk_soc = ".((int)
$id);
1629 $result = $this->db->query($sql);
1630 if ($this->db->num_rows($result) == 0) {
1631 throw new RestException(404,
'Notification not found');
1636 $notifications = array();
1640 $num = $this->db->num_rows($result);
1644 $obj = $this->db->fetch_object($result);
1645 $notifications[] = $obj;
1649 throw new RestException(404,
'No notifications found');
1652 $fields = array(
'id',
'socid',
'event',
'contact_id',
'datec',
'tms',
'type');
1654 $returnNotifications = array();
1656 foreach ($notifications as $notification) {
1658 foreach ($notification as $key => $value) {
1659 if (in_array($key, $fields)) {
1663 $returnNotifications[] =
$object;
1667 return $returnNotifications;
1688 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1689 throw new RestException(403,
"User has no right to update thirdparties");
1691 if ($this->company->fetch(
$id) <= 0) {
1692 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1694 $notification =
new Notify($this->db);
1696 $notification->socid =
$id;
1698 foreach ($request_data as $field => $value) {
1699 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1702 $event = $notification->event;
1704 throw new RestException(500,
'Error creating Thirdparty Notification, request_data missing event');
1706 $socid = $notification->socid;
1707 $contact_id = $notification->contact_id;
1709 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1710 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1711 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1712 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1713 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1715 $exists_result = $this->db->query($exists_sql);
1716 if ($this->db->num_rows($exists_result) > 0) {
1717 throw new RestException(403,
'Notification already exists');
1720 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1721 throw new RestException(500,
'Error creating Thirdparty Notification');
1724 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1725 throw new RestException(500,
'Error updating values');
1751 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1752 throw new RestException(403,
"User has no right to update thirdparties");
1754 if ($this->company->fetch(
$id) <= 0) {
1755 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1757 $notification =
new Notify($this->db);
1758 $notification->socid =
$id;
1760 $sql =
"SELECT t.rowid as id FROM ".MAIN_DB_PREFIX.
"c_action_trigger as t";
1761 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
1763 $result = $this->db->query($sql);
1764 if ($this->db->num_rows($result) == 0) {
1765 throw new RestException(404,
'Action Trigger code not found');
1768 $notification->event = $this->db->fetch_row($result)[0];
1769 foreach ($request_data as $field => $value) {
1770 if ($field ===
'event') {
1771 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains event key');
1773 if ($field ===
'fk_action') {
1774 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains fk_action key');
1776 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1779 $event = $notification->event;
1780 $socid = $notification->socid;
1781 $contact_id = $notification->contact_id;
1783 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1784 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1785 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1786 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1787 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1789 $exists_result = $this->db->query($exists_sql);
1790 if ($this->db->num_rows($exists_result) > 0) {
1791 throw new RestException(403,
'Notification already exists');
1794 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1795 throw new RestException(500,
'Error creating Thirdparty Notification, are request_data well formed?');
1798 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1799 throw new RestException(500,
'Error updating values');
1821 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1822 throw new RestException(403);
1825 $notification =
new Notify($this->db);
1827 $notification->fetch($notification_id);
1829 $socid = (int) $notification->socid;
1831 if ($socid ==
$id) {
1832 return $notification->delete(DolibarrApiAccess::$user);
1834 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1857 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1858 throw new RestException(403,
"User has no right to update thirdparties");
1860 if ($this->company->fetch(
$id) <= 0) {
1861 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1863 $notification =
new Notify($this->db);
1866 $notification->fetch($notification_id,
$id);
1868 if ($notification->socid !=
$id) {
1869 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1872 foreach ($request_data as $field => $value) {
1873 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1876 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1877 throw new RestException(500,
'Error updating values');
1900 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1901 throw new RestException(403);
1904 throw new RestException(400,
'Thirdparty ID is mandatory');
1908 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1915 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1916 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1917 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1919 $sql .=
" WHERE fk_soc = ".((int)
$id);
1922 $result = $this->db->query($sql);
1924 if ($this->db->num_rows($result) == 0) {
1925 throw new RestException(404,
'Account not found');
1930 $accounts = array();
1934 $num = $this->db->num_rows($result);
1938 $obj = $this->db->fetch_object($result);
1941 if ($account->fetch($obj->rowid)) {
1942 $accounts[] = $account;
1947 throw new RestException(404,
'Account not found');
1951 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1953 $returnAccounts = array();
1955 foreach ($accounts as $account) {
1957 foreach ($account as $key => $value) {
1958 if (in_array($key, $fields)) {
1959 if ($key ==
'iban') {
1969 return $returnAccounts;
1990 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1991 throw new RestException(403);
1993 if ($this->company->fetch(
$id) <= 0) {
1994 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1998 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2003 $account->socid =
$id;
2005 foreach ($request_data as $field => $value) {
2006 if ($field ===
'caller') {
2008 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2012 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
2015 if ($account->create(DolibarrApiAccess::$user) < 0) {
2016 throw new RestException(500,
'Error creating Company Bank account');
2019 if (empty($account->rum)) {
2020 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
2022 $account->rum = $prelevement->buildRumNumber((
string) $this->company->code_client, $account->datec, (
string) $account->id);
2023 $account->date_rum =
dol_now();
2026 if ($account->update(DolibarrApiAccess::$user) < 0) {
2027 throw new RestException(500,
'Error updating values');
2052 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2053 throw new RestException(403);
2055 if ($this->company->fetch(
$id) <= 0) {
2056 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
2060 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2066 $account->fetch($bankaccount_id,
'',
$id, -1,
'');
2068 if ($account->socid !=
$id) {
2069 throw new RestException(403);
2073 foreach ($request_data as $field => $value) {
2074 if ($field ===
'caller') {
2076 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2083 if (empty($account->rum)) {
2084 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
2086 $account->rum = $prelevement->buildRumNumber((
string) $this->company->code_client, $account->datec, (
string) $account->id);
2087 $account->date_rum =
dol_now();
2090 if ($account->update(DolibarrApiAccess::$user) < 0) {
2091 throw new RestException(500,
'Error updating values');
2113 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2114 throw new RestException(403);
2118 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2123 $account->fetch($bankaccount_id);
2125 $socid = (int) $account->socid;
2127 if ($socid ==
$id) {
2128 return $account->delete(DolibarrApiAccess::$user);
2130 throw new RestException(403,
"Not allowed due to bad consistency of input data");
2154 global
$conf, $langs;
2156 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
2158 if ($this->company->fetch(
$id) <= 0) {
2159 throw new RestException(404,
'Thirdparty not found');
2162 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2163 throw new RestException(403);
2166 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
2168 $this->company->fk_bank = $this->company->fk_account;
2171 $outputlangs = $langs;
2176 if (isset($this->company->thirdparty->default_lang)) {
2177 $newlang = $this->company->thirdparty->default_lang;
2178 } elseif (isset($this->company->default_lang)) {
2179 $newlang = $this->company->default_lang;
2182 if (!empty($newlang)) {
2184 $outputlangs->setDefaultLang($newlang);
2187 $sql =
"SELECT rowid";
2188 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
2190 $sql .=
" WHERE fk_soc = ".((int)
$id);
2192 if ($companybankid) {
2193 $sql .=
" AND rowid = ".((int) $companybankid);
2197 $accounts = array();
2199 $result = $this->db->query($sql);
2201 if ($this->db->num_rows($result) == 0) {
2202 throw new RestException(404,
'Bank account not found');
2205 $num = $this->db->num_rows($result);
2209 $obj = $this->db->fetch_object($result);
2212 if ($account->fetch($obj->rowid)) {
2213 $accounts[] = $account;
2218 throw new RestException(500,
'Sql error '.$this->db->lasterror());
2221 $moreparams = array(
2222 'use_companybankid' => $accounts[0]->
id,
2223 'force_dir_output' =>
$conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName((
string) $this->company->id)
2226 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
2229 return array(
"success" => $result);
2231 throw new RestException(500,
'Error generating the document '.$this->company->error);
2254 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2255 throw new RestException(403);
2259 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2265 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2266 $sql .=
" WHERE fk_soc = ".((int)
$id);
2268 $sql .=
" AND site ='".$this->db->escape($site).
"'";
2271 $result = $this->db->query($sql);
2273 if ($result && $this->db->num_rows($result) == 0) {
2274 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
2279 $accounts = array();
2282 $num = $this->db->num_rows($result);
2286 $obj = $this->db->fetch_object($result);
2289 if ($account->fetch($obj->rowid)) {
2290 $accounts[] = $account;
2295 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
2297 $returnAccounts = array();
2299 foreach ($accounts as $account) {
2301 foreach ($account as $key => $value) {
2302 if (in_array($key, $fields)) {
2309 return $returnAccounts;
2331 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2332 throw new RestException(403);
2335 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2336 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
2337 $sql .=
" AND entity IN (".getEntity(
'societe').
")";
2339 $result = $this->db->query($sql);
2341 if ($result && $this->db->num_rows($result) == 1) {
2342 $obj = $this->db->fetch_object($result);
2343 $returnThirdparty = $this->
_fetch($obj->fk_soc);
2345 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
2349 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2352 return $returnThirdparty;
2380 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2381 throw new RestException(403);
2384 if (!isset($request_data[
'site'])) {
2385 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
2388 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
2389 $result = $this->db->query($sql);
2391 if ($result && $this->db->num_rows($result) == 0) {
2393 if (!isset($request_data[
'login'])) {
2394 $account->login =
"";
2396 $account->fk_soc =
$id;
2398 foreach ($request_data as $field => $value) {
2399 if ($field ===
'caller') {
2401 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2408 if ($account->create(DolibarrApiAccess::$user) < 0) {
2409 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
2416 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
2448 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2449 throw new RestException(403);
2453 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2456 $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).
"'";
2457 $result = $this->db->query($sql);
2460 if ($result && $this->db->num_rows($result) == 0) {
2461 if (!isset($request_data[
'key_account'])) {
2462 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
2465 if (!isset($request_data[
'login'])) {
2466 $account->login =
"";
2469 foreach ($request_data as $field => $value) {
2470 if ($field ===
'caller') {
2472 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2479 $account->fk_soc =
$id;
2480 $account->site = $site;
2482 if ($account->create(DolibarrApiAccess::$user) < 0) {
2483 throw new RestException(500,
'Error creating SocieteAccount entity.');
2487 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2488 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2489 $result = $this->db->query($sql);
2491 if ($result && $this->db->num_rows($result) !== 0) {
2492 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.");
2496 $obj = $this->db->fetch_object($result);
2499 $account->id = $obj->rowid;
2500 $account->fk_soc =
$id;
2501 $account->site = $site;
2502 if (!isset($request_data[
'login'])) {
2503 $account->login =
"";
2505 $account->fk_user_creat = $obj->fk_user_creat;
2506 $account->date_creation = $obj->date_creation;
2508 foreach ($request_data as $field => $value) {
2509 if ($field ===
'caller') {
2511 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2518 if ($account->update(DolibarrApiAccess::$user) < 0) {
2519 throw new RestException(500,
'Error updating SocieteAccount entity.');
2550 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2551 throw new RestException(403);
2555 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2558 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2559 $result = $this->db->query($sql);
2561 if ($result && $this->db->num_rows($result) == 0) {
2562 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
2565 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2566 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2567 $result = $this->db->query($sql);
2569 if ($result && $this->db->num_rows($result) !== 0) {
2570 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.");
2574 $obj = $this->db->fetch_object($result);
2576 $account->fetch($obj->rowid);
2578 foreach ($request_data as $field => $value) {
2579 if ($field ===
'caller') {
2581 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2588 if ($account->update(DolibarrApiAccess::$user) < 0) {
2589 throw new RestException(500,
'Error updating SocieteAccount account');
2618 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2619 throw new RestException(403);
2623 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2626 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2627 $result = $this->db->query($sql);
2629 if ($result && $this->db->num_rows($result) == 0) {
2630 throw new RestException(404);
2632 $obj = $this->db->fetch_object($result);
2634 $account->fetch($obj->rowid);
2636 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2637 throw new RestException(500,
"Error while deleting $site account attached to this third party");
2659 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2660 throw new RestException(403);
2667 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2668 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id);
2670 $result = $this->db->query($sql);
2672 if ($result && $this->db->num_rows($result) == 0) {
2673 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
2678 $num = $this->db->num_rows($result);
2682 $obj = $this->db->fetch_object($result);
2684 $account->fetch($obj->rowid);
2686 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2687 throw new RestException(500,
'Error while deleting account attached to this third party');
2713 unset(
$object->departement_code);
2722 unset(
$object->commercial_id);
2726 unset(
$object->total_localtax1);
2727 unset(
$object->total_localtax2);
2733 unset(
$object->fk_delivery_address);
2748 if ($data ===
null) {
2751 $thirdparty = array();
2752 foreach (Thirdparties::$FIELDS as $field) {
2753 if (!isset($data[$field])) {
2754 throw new RestException(400,
"$field field missing");
2756 $thirdparty[$field] = $data[$field];
2784 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2786 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2787 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2791 $result = $this->company->initAsSpecimen();
2793 $result = $this->company->fetch((
int) $rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2796 throw new RestException(404,
'Thirdparty not found');
2800 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2803 $this->company->getNoEmail();
2807 $filterabsolutediscount =
"fk_facture_source IS NULL";
2808 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2810 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2811 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2814 $absolute_discount = $this->company->getAvailableDiscounts(
null, $filterabsolutediscount);
2815 $absolute_creditnote = $this->company->getAvailableDiscounts(
null, $filtercreditnote);
2816 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2817 $this->company->absolute_creditnote =
price2num($absolute_creditnote,
'MT');
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
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)
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.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.