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->lasterror());
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->errorsToString());
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->errorsToString());
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->errorsToString());
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->errorsToString());
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->errorsToString());
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");
1693 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1696 if ($this->company->fetch(
$id) <= 0) {
1697 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1699 $notification =
new Notify($this->db);
1701 $notification->socid =
$id;
1703 foreach ($request_data as $field => $value) {
1704 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1707 $event = $notification->event;
1709 throw new RestException(500,
'Error creating Thirdparty Notification, request_data missing event');
1711 $socid = $notification->socid;
1712 $contact_id = $notification->contact_id;
1714 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1715 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1716 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1717 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1718 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1720 $exists_result = $this->db->query($exists_sql);
1721 if ($this->db->num_rows($exists_result) > 0) {
1722 throw new RestException(403,
'Notification already exists');
1725 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1726 throw new RestException(500,
'Error creating Thirdparty Notification');
1729 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1730 throw new RestException(500,
'Error updating values');
1756 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1757 throw new RestException(403,
"User has no right to update thirdparties");
1761 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1764 if ($this->company->fetch(
$id) <= 0) {
1765 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1767 $notification =
new Notify($this->db);
1768 $notification->socid =
$id;
1770 $sql =
"SELECT t.rowid as id FROM ".MAIN_DB_PREFIX.
"c_action_trigger as t";
1771 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
1773 $result = $this->db->query($sql);
1774 if ($this->db->num_rows($result) == 0) {
1775 throw new RestException(404,
'Action Trigger code not found');
1778 $notification->event = $this->db->fetch_row($result)[0];
1779 foreach ($request_data as $field => $value) {
1780 if ($field ===
'event') {
1781 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains event key');
1783 if ($field ===
'fk_action') {
1784 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains fk_action key');
1786 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1789 $event = $notification->event;
1790 $socid = $notification->socid;
1791 $contact_id = $notification->contact_id;
1793 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1794 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1795 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1796 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1797 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1799 $exists_result = $this->db->query($exists_sql);
1800 if ($this->db->num_rows($exists_result) > 0) {
1801 throw new RestException(403,
'Notification already exists');
1804 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1805 throw new RestException(500,
'Error creating Thirdparty Notification, are request_data well formed?');
1808 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1809 throw new RestException(500,
'Error updating values');
1831 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1832 throw new RestException(403);
1836 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1839 $notification =
new Notify($this->db);
1841 $notification->fetch($notification_id);
1843 $socid = (int) $notification->socid;
1845 if ($socid ==
$id) {
1846 return $notification->delete(DolibarrApiAccess::$user);
1848 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1871 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1872 throw new RestException(403,
"User has no right to update thirdparties");
1876 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1879 if ($this->company->fetch(
$id) <= 0) {
1880 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1882 $notification =
new Notify($this->db);
1885 $notification->fetch($notification_id,
$id);
1887 if ($notification->socid !=
$id) {
1888 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1891 foreach ($request_data as $field => $value) {
1892 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1895 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1896 throw new RestException(500,
'Error updating values');
1919 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1920 throw new RestException(403);
1923 throw new RestException(400,
'Thirdparty ID is mandatory');
1927 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1934 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1935 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1936 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1938 $sql .=
" WHERE fk_soc = ".((int)
$id);
1941 $result = $this->db->query($sql);
1943 if ($this->db->num_rows($result) == 0) {
1944 throw new RestException(404,
'Account not found');
1949 $accounts = array();
1953 $num = $this->db->num_rows($result);
1957 $obj = $this->db->fetch_object($result);
1960 if ($account->fetch($obj->rowid)) {
1961 $accounts[] = $account;
1966 throw new RestException(404,
'Account not found');
1970 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1972 $returnAccounts = array();
1974 foreach ($accounts as $account) {
1976 foreach ($account as $key => $value) {
1977 if (in_array($key, $fields)) {
1978 if ($key ==
'iban') {
1988 return $returnAccounts;
2009 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2010 throw new RestException(403);
2012 if ($this->company->fetch(
$id) <= 0) {
2013 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
2017 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2022 $account->socid =
$id;
2024 foreach ($request_data as $field => $value) {
2025 if ($field ===
'caller') {
2027 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2031 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
2034 if ($account->create(DolibarrApiAccess::$user) < 0) {
2035 throw new RestException(500,
'Error creating Company Bank account');
2038 if (empty($account->rum)) {
2039 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
2041 $account->rum = $prelevement->buildRumNumber((
string) $this->company->code_client, $account->datec, (
string) $account->id);
2042 $account->date_rum =
dol_now();
2045 if ($account->update(DolibarrApiAccess::$user) < 0) {
2046 throw new RestException(500,
'Error updating values');
2071 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2072 throw new RestException(403);
2074 if ($this->company->fetch(
$id) <= 0) {
2075 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
2079 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2085 $account->fetch($bankaccount_id,
'',
$id, -1,
'');
2087 if ($account->socid !=
$id) {
2088 throw new RestException(403);
2092 foreach ($request_data as $field => $value) {
2093 if ($field ===
'caller') {
2095 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2102 if (empty($account->rum)) {
2103 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
2105 $account->rum = $prelevement->buildRumNumber((
string) $this->company->code_client, $account->datec, (
string) $account->id);
2106 $account->date_rum =
dol_now();
2109 if ($account->update(DolibarrApiAccess::$user) < 0) {
2110 throw new RestException(500,
'Error updating values');
2132 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2133 throw new RestException(403);
2137 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2142 $account->fetch($bankaccount_id);
2144 $socid = (int) $account->socid;
2146 if ($socid ==
$id) {
2147 return $account->delete(DolibarrApiAccess::$user);
2149 throw new RestException(403,
"Not allowed due to bad consistency of input data");
2173 global
$conf, $langs;
2175 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
2177 if ($this->company->fetch(
$id) <= 0) {
2178 throw new RestException(404,
'Thirdparty not found');
2181 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2182 throw new RestException(403);
2186 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2189 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
2191 $this->company->fk_bank = $this->company->fk_account;
2194 $outputlangs = $langs;
2199 if (isset($this->company->thirdparty->default_lang)) {
2200 $newlang = $this->company->thirdparty->default_lang;
2201 } elseif (isset($this->company->default_lang)) {
2202 $newlang = $this->company->default_lang;
2205 if (!empty($newlang)) {
2207 $outputlangs->setDefaultLang($newlang);
2210 $sql =
"SELECT rowid";
2211 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
2213 $sql .=
" WHERE fk_soc = ".((int)
$id);
2215 if ($companybankid) {
2216 $sql .=
" AND rowid = ".((int) $companybankid);
2220 $accounts = array();
2222 $result = $this->db->query($sql);
2224 if ($this->db->num_rows($result) == 0) {
2225 throw new RestException(404,
'Bank account not found');
2228 $num = $this->db->num_rows($result);
2232 $obj = $this->db->fetch_object($result);
2235 if ($account->fetch($obj->rowid)) {
2236 $accounts[] = $account;
2241 throw new RestException(500,
'Sql error '.$this->db->lasterror());
2244 $moreparams = array(
2245 'use_companybankid' => $accounts[0]->
id,
2246 'force_dir_output' =>
$conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName((
string) $this->company->id)
2249 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
2252 return array(
"success" => $result);
2254 throw new RestException(500,
'Error generating the document '.$this->company->errorsToString());
2277 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2278 throw new RestException(403);
2282 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2288 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2289 $sql .=
" WHERE fk_soc = ".((int)
$id);
2291 $sql .=
" AND site ='".$this->db->escape($site).
"'";
2294 $result = $this->db->query($sql);
2296 if ($result && $this->db->num_rows($result) == 0) {
2297 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
2302 $accounts = array();
2305 $num = $this->db->num_rows($result);
2309 $obj = $this->db->fetch_object($result);
2312 if ($account->fetch($obj->rowid)) {
2313 $accounts[] = $account;
2318 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
2320 $returnAccounts = array();
2322 foreach ($accounts as $account) {
2324 foreach ($account as $key => $value) {
2325 if (in_array($key, $fields)) {
2332 return $returnAccounts;
2354 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2355 throw new RestException(403);
2358 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2359 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
2360 $sql .=
" AND entity IN (".getEntity(
'societe').
")";
2362 $result = $this->db->query($sql);
2364 if ($result && $this->db->num_rows($result) == 1) {
2365 $obj = $this->db->fetch_object($result);
2366 $returnThirdparty = $this->
_fetch($obj->fk_soc);
2368 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
2372 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2375 return $returnThirdparty;
2403 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2404 throw new RestException(403);
2408 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2411 if (!isset($request_data[
'site'])) {
2412 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
2415 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
2416 $result = $this->db->query($sql);
2418 if ($result && $this->db->num_rows($result) == 0) {
2420 if (!isset($request_data[
'login'])) {
2421 $account->login =
"";
2423 $account->fk_soc =
$id;
2425 foreach ($request_data as $field => $value) {
2426 if ($field ===
'caller') {
2428 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2435 if ($account->create(DolibarrApiAccess::$user) < 0) {
2436 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
2443 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
2475 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2476 throw new RestException(403);
2480 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2483 $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).
"'";
2484 $result = $this->db->query($sql);
2487 if ($result && $this->db->num_rows($result) == 0) {
2488 if (!isset($request_data[
'key_account'])) {
2489 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
2492 if (!isset($request_data[
'login'])) {
2493 $account->login =
"";
2496 foreach ($request_data as $field => $value) {
2497 if ($field ===
'caller') {
2499 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2506 $account->fk_soc =
$id;
2507 $account->site = $site;
2509 if ($account->create(DolibarrApiAccess::$user) < 0) {
2510 throw new RestException(500,
'Error creating SocieteAccount entity.');
2514 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2515 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2516 $result = $this->db->query($sql);
2518 if ($result && $this->db->num_rows($result) !== 0) {
2519 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.");
2523 $obj = $this->db->fetch_object($result);
2526 $account->id = $obj->rowid;
2527 $account->fk_soc =
$id;
2528 $account->site = $site;
2529 if (!isset($request_data[
'login'])) {
2530 $account->login =
"";
2532 $account->fk_user_creat = $obj->fk_user_creat;
2533 $account->date_creation = $obj->date_creation;
2535 foreach ($request_data as $field => $value) {
2536 if ($field ===
'caller') {
2538 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2545 if ($account->update(DolibarrApiAccess::$user) < 0) {
2546 throw new RestException(500,
'Error updating SocieteAccount entity.');
2577 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2578 throw new RestException(403);
2582 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2585 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2586 $result = $this->db->query($sql);
2588 if ($result && $this->db->num_rows($result) == 0) {
2589 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
2592 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2593 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2594 $result = $this->db->query($sql);
2596 if ($result && $this->db->num_rows($result) !== 0) {
2597 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.");
2601 $obj = $this->db->fetch_object($result);
2603 $account->fetch($obj->rowid);
2605 foreach ($request_data as $field => $value) {
2606 if ($field ===
'caller') {
2608 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2615 if ($account->update(DolibarrApiAccess::$user) < 0) {
2616 throw new RestException(500,
'Error updating SocieteAccount account');
2645 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2646 throw new RestException(403);
2650 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2653 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2654 $result = $this->db->query($sql);
2656 if ($result && $this->db->num_rows($result) == 0) {
2657 throw new RestException(404);
2659 $obj = $this->db->fetch_object($result);
2661 $account->fetch($obj->rowid);
2663 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2664 throw new RestException(500,
"Error while deleting $site account attached to this third party");
2686 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2687 throw new RestException(403);
2691 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2698 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2699 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id);
2701 $result = $this->db->query($sql);
2703 if ($result && $this->db->num_rows($result) == 0) {
2704 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
2709 $num = $this->db->num_rows($result);
2713 $obj = $this->db->fetch_object($result);
2715 $account->fetch($obj->rowid);
2717 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2718 throw new RestException(500,
'Error while deleting account attached to this third party');
2744 unset(
$object->departement_code);
2753 unset(
$object->commercial_id);
2757 unset(
$object->total_localtax1);
2758 unset(
$object->total_localtax2);
2764 unset(
$object->fk_delivery_address);
2779 if ($data ===
null) {
2782 $thirdparty = array();
2783 foreach (Thirdparties::$FIELDS as $field) {
2784 if (!isset($data[$field])) {
2785 throw new RestException(400,
"$field field missing");
2787 $thirdparty[$field] = $data[$field];
2815 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2817 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2818 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2822 $result = $this->company->initAsSpecimen();
2824 $result = $this->company->fetch((
int) $rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2827 throw new RestException(404,
'Thirdparty not found');
2831 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2834 $this->company->getNoEmail();
2838 $filterabsolutediscount =
"fk_facture_source IS NULL";
2839 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2841 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2842 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2845 $absolute_discount = $this->company->getAvailableDiscounts(
null, $filterabsolutediscount);
2846 $absolute_creditnote = $this->company->getAvailableDiscounts(
null, $filtercreditnote);
2847 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2848 $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.
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.