27use Luracast\Restler\RestException;
42 public static $FIELDS = array(
59 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
60 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
61 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
62 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
63 require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
65 $this->company =
new Societe($this->db);
68 static::$FIELDS[] =
'email';
84 public function get(
$id)
107 return $this->
_fetch(
null,
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
126 return $this->
_fetch(
null,
'',
'', $barcode);
155 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
159 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
160 throw new RestException(403);
164 $socids = DolibarrApiAccess::$user->socid ? (
string) DolibarrApiAccess::$user->socid :
'';
168 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
169 $search_sale = DolibarrApiAccess::$user->id;
172 $sql =
"SELECT t.rowid";
173 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
174 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
177 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
179 if (!in_array($mode, array(1, 2, 3))) {
180 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
183 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
185 $sql .=
" AND t.client IN (1, 3)";
186 } elseif ($mode == 2) {
187 $sql .=
" AND t.client IN (2, 3)";
188 } elseif ($mode == 3) {
189 $sql .=
" AND t.client IN (0)";
190 } elseif ($mode == 4) {
191 $sql .=
" AND t.fournisseur IN (1)";
195 if (!empty($mode) && $mode != 4) {
196 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
197 } elseif (!empty($mode) && $mode == 4) {
198 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
200 $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))";
204 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
207 if ($search_sale && $search_sale !=
'-1') {
208 if ($search_sale == -2) {
209 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.rowid)";
210 } elseif ($search_sale > 0) {
211 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.rowid AND sc.fk_user = ".((int) $search_sale).
")";
219 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
224 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
226 $sql .= $this->db->order($sortfield, $sortorder);
231 $offset = $limit * $page;
233 $sql .= $this->db->plimit($limit + 1, $offset);
236 $result = $this->db->query($sql);
238 $num = $this->db->num_rows($result);
239 $min = min($num, ($limit <= 0 ? $num : $limit));
242 $obj = $this->db->fetch_object($result);
243 $soc_static =
new Societe($this->db);
244 if ($soc_static->fetch($obj->rowid)) {
246 $soc_static->getNoEmail();
253 throw new RestException(503,
'Error when retrieve third parties : '.$this->db->lasterror());
255 if (!count($obj_ret)) {
259 $message =
'No third parties found';
262 $message =
'No customers found';
265 $message =
'No prospects found';
268 $message =
'No other third parties found';
271 $message =
'No suppliers found';
273 throw new RestException(404, $message);
277 if ($pagination_data) {
278 $totalsResult = $this->db->query($sqlTotals);
279 $total = $this->db->fetch_object($totalsResult)->total;
284 $obj_ret[
'data'] = $tmp;
285 $obj_ret[
'pagination'] = [
286 'total' => (int) $total,
288 'page_count' => ceil((
int) $total / $limit),
308 public function post($request_data =
null)
310 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
311 throw new RestException(403);
315 if (!isset($request_data[
'country_id']) && isset($request_data[
'country_code'])) {
316 $field = strlen($request_data[
'country_code']) > 2 ?
'code_iso' :
'code';
317 $id =
dol_getIdFromCode($this->db, $request_data[
'country_code'],
"c_country", $field,
"rowid");
319 throw new RestException(404,
'Country code not found in database: ' . $this->db->error);
321 $request_data[
'country_id'] =
$id;
325 $result = $this->
_validate($request_data);
327 foreach ($request_data as $field => $value) {
328 if ($field ===
'caller') {
330 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
333 if ($field ==
'array_options' && is_array($value)) {
334 $this->company->fetch_optionals();
336 foreach ($value as $index => $val) {
342 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
345 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
346 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
348 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
349 $this->company->setNoEmail($this->company->no_email);
352 return $this->company->id;
372 public function put(
$id, $request_data =
null)
374 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
375 throw new RestException(403);
378 $result = $this->company->fetch(
$id);
380 throw new RestException(404,
'Thirdparty not found');
384 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
387 foreach ($request_data as $field => $value) {
388 if ($field ==
'id') {
391 if ($field ===
'caller') {
393 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
396 if ($field ==
'array_options' && is_array($value)) {
397 foreach ($value as $index => $val) {
402 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
405 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
406 $this->company->setNoEmail($this->company->no_email);
409 if ($this->company->update(
$id, DolibarrApiAccess::$user, 1, 1, 1,
'update', 1) > 0) {
410 return $this->
get(
$id);
412 throw new RestException(500, $this->company->error);
439 if (
$id == $idtodelete) {
440 throw new RestException(400,
'Try to merge a thirdparty into itself');
443 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
444 throw new RestException(403);
447 $result = $this->company->fetch(
$id);
449 throw new RestException(404,
'Thirdparty not found');
453 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
456 $companytoremove =
new Societe($this->db);
457 $result = $companytoremove->fetch($idtodelete);
459 throw new RestException(404,
'Thirdparty not found');
463 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
466 $user = DolibarrApiAccess::$user;
467 $result = $this->company->mergeCompany($companytoremove->id);
469 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.
$id.
'. Enable and read log file for more information.');
472 return $this->
get(
$id);
487 public function delete(
$id)
489 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
490 throw new RestException(403);
492 $result = $this->company->fetch(
$id);
494 throw new RestException(404,
'Thirdparty not found');
497 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
499 $this->company->oldcopy = clone $this->company;
501 $res = $this->company->delete(
$id);
503 throw new RestException(500,
"Can't delete, error occurs");
504 } elseif ($res == 0) {
505 throw new RestException(409,
"Can't delete, that product is probably used");
511 'message' =>
'Object deleted'
538 throw new RestException(501,
'Module "Thirdparties" needed for this request');
542 throw new RestException(501,
'Module "Products" needed for this request');
546 throw new RestException(501,
'Multiprices features activation needed for this request');
549 if ($priceLevel < 1 || $priceLevel >
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT')) {
550 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
553 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
554 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
557 $result = $this->company->fetch(
$id);
559 throw new RestException(404,
'Thirdparty '.
$id.
' not found');
562 if (empty($result)) {
563 throw new RestException(500,
'Error fetching thirdparty '.
$id, array_merge(array($this->company->error), $this->company->errors));
567 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
570 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
572 throw new RestException(500,
'Error setting new price level for thirdparty '.
$id, array($this->company->db->lasterror()));
591 public function getRepresentative(
$id)
593 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'reader')) {
594 throw new RestException(403);
596 $result = $this->company->fetch(
$id);
598 throw new RestException(404,
'Thirdparty not found');
601 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
603 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user);
625 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
626 throw new RestException(403);
628 $result = $this->company->fetch(
$id);
630 throw new RestException(404,
'Thirdparty not found');
632 $usertmp =
new User($this->db);
633 $result = $usertmp->fetch($representative_id);
635 throw new RestException(404,
'User not found');
638 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
640 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
661 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
662 throw new RestException(403);
664 $result = $this->company->fetch(
$id);
666 throw new RestException(404,
'Thirdparty not found');
668 $usertmp =
new User($this->db);
669 $result = $usertmp->fetch($representative_id);
671 throw new RestException(404,
'User not found');
674 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
676 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
699 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
701 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
702 throw new RestException(403);
705 $result = $this->company->fetch(
$id);
707 throw new RestException(404,
'Thirdparty not found');
712 throw new RestException(404,
'Third party not allowed for login '.DolibarrApiAccess::$user->login);
717 $arrayofcateg = $categories->getListForItem(
$id,
'customer', $sortfield, $sortorder, $limit, $page);
719 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
720 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
723 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
727 return $arrayofcateg;
748 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
749 throw new RestException(403);
752 $result = $this->company->fetch(
$id);
754 throw new RestException(404,
'Third party not found');
757 $result = $category->fetch($category_id);
759 throw new RestException(404,
'category not found');
763 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
766 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
769 $category->add_type($this->company,
'customer');
792 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
793 throw new RestException(403);
796 $result = $this->company->fetch(
$id);
798 throw new RestException(404,
'Thirdparty not found');
801 $result = $category->fetch($category_id);
803 throw new RestException(404,
'category not found');
807 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
810 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
813 $category->del_type($this->company,
'customer');
839 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
840 throw new RestException(403);
843 $result = $this->company->fetch(
$id);
845 throw new RestException(404,
'Thirdparty not found');
850 throw new RestException(404,
'Third party not allowed for login '.DolibarrApiAccess::$user->login);
855 $result = $categories->getListForItem(
$id,
'supplier', $sortfield, $sortorder, $limit, $page);
857 if (is_numeric($result) && $result < 0) {
858 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
861 if (is_numeric($result) && $result == 0) {
886 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
887 throw new RestException(403);
890 $result = $this->company->fetch(
$id);
892 throw new RestException(404,
'Thirdparty not found');
895 $result = $category->fetch($category_id);
897 throw new RestException(404,
'category not found');
901 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
904 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
907 $category->add_type($this->company,
'supplier');
930 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
931 throw new RestException(403);
934 $result = $this->company->fetch(
$id);
936 throw new RestException(404,
'Thirdparty not found');
939 $result = $category->fetch($category_id);
941 throw new RestException(404,
'category not found');
945 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
948 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
951 $category->del_type($this->company,
'supplier');
977 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
978 throw new RestException(403);
982 throw new RestException(400,
'Thirdparty ID is mandatory');
986 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
989 $result = $this->company->fetch(
$id);
991 throw new RestException(404,
'Thirdparty not found');
994 $result = $this->company->getOutstandingProposals($mode);
996 unset($result[
'total_ht']);
997 unset($result[
'total_ttc']);
1023 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1024 throw new RestException(403);
1028 throw new RestException(400,
'Thirdparty ID is mandatory');
1032 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1035 $result = $this->company->fetch(
$id);
1037 throw new RestException(404,
'Thirdparty not found');
1040 $result = $this->company->getOutstandingOrders($mode);
1042 unset($result[
'total_ht']);
1043 unset($result[
'total_ttc']);
1068 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1069 throw new RestException(403);
1073 throw new RestException(400,
'Thirdparty ID is mandatory');
1077 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1080 $result = $this->company->fetch(
$id);
1082 throw new RestException(404,
'Thirdparty not found');
1085 $result = $this->company->getOutstandingBills($mode);
1087 unset($result[
'total_ht']);
1088 unset($result[
'total_ttc']);
1113 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1114 throw new RestException(403);
1118 throw new RestException(400,
'Thirdparty ID is mandatory');
1122 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1125 $result = $this->company->fetch(
$id);
1127 throw new RestException(404,
'Thirdparty not found');
1130 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1163 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1164 throw new RestException(403);
1168 throw new RestException(400,
'Thirdparty ID is mandatory');
1172 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1175 $result = $this->company->fetch(
$id);
1177 throw new RestException(404,
'Thirdparty not found');
1181 if ($mode ===
'customer') {
1182 $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";
1183 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1184 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as f ON f.rowid = re.fk_facture_source";
1185 $sql .=
" WHERE re.fk_soc = ".((int)
$id);
1186 if ($filter ==
"available") {
1187 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1189 if ($filter ==
"used") {
1190 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1192 } elseif ($mode ===
'supplier') {
1193 $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";
1194 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1195 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_fourn as f ON f.rowid = re.fk_invoice_supplier_source";
1196 $sql .=
" WHERE f.rowid = re.fk_invoice_supplier_source AND re.fk_soc = ".((int)
$id);
1197 if ($filter ==
"available") {
1198 $sql .=
" AND re.fk_invoice_supplier IS NULL AND re.fk_invoice_supplier_line IS NULL";
1200 if ($filter ==
"used") {
1201 $sql .=
" AND (re.fk_invoice_supplier IS NOT NULL OR re.fk_invoice_supplier_line IS NOT NULL)";
1205 $sql .= $this->db->order($sortfield, $sortorder);
1207 $result = $this->db->query($sql);
1209 throw new RestException(503, $this->db->lasterror());
1212 while ($obj = $this->db->fetch_object($result)) {
1246 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1247 throw new RestException(403);
1252 throw new RestException(400,
'Thirdparty ID is mandatory');
1254 if (!isset($request_data[
'amount'])) {
1255 throw new RestException(400,
'Missing required field: amount');
1257 if (!isset($request_data[
'description'])) {
1258 throw new RestException(400,
'Missing required field: description');
1263 throw new RestException(401,
'Access not allowed for login'.DolibarrApiAccess::$user->login);
1267 if ($this->company->fetch(
$id) <= 0) {
1268 throw new RestException(404,
'Error creating discount, thirdparty not found');
1273 if (!is_numeric($request_data[
'amount']) || $request_data[
'amount'] <= 0) {
1274 throw new RestException(400,
'Invalid amount_ht: must be a positive number');
1276 $amount = (float) $request_data[
'amount'];
1279 if (isset($request_data[
'tva_tx']) && (!is_numeric($request_data[
'tva_tx']) || $request_data[
'tva_tx'] < 0)) {
1280 throw new RestException(400,
'Invalid tva_tx: must be a positive number or zero');
1282 $tva_tx = isset($request_data[
'tva_tx']) ? (float) $request_data[
'tva_tx'] : 0;
1285 $price_base_type =
'HT';
1286 if (isset($request_data[
'price_base_type'])) {
1287 $price_base_type = strtoupper($request_data[
'price_base_type']);
1288 if ($price_base_type !==
'HT' && $price_base_type !==
'TTC') {
1289 throw new RestException(400,
'Invalid price_base_type: must be "HT" or "TTC"');
1295 if (isset($request_data[
'discount_type'])) {
1296 $discount_type = (int) $request_data[
'discount_type'];
1297 if ($discount_type !== 0 && $discount_type !== 1) {
1298 throw new RestException(400,
'Invalid discount_type: must be 0 (customer) or 1 (supplier)');
1303 $description = $request_data[
'description'];
1304 if (empty(trim($description))) {
1305 throw new RestException(400,
'Description cannot be empty');
1310 if (isset($request_data[
'vat_src_code']) && !empty($request_data[
'vat_src_code'])) {
1311 $vatrate = $tva_tx .
' (' . $request_data[
'vat_src_code'] .
')';
1317 $result = $this->company->set_remise_except($amount, DolibarrApiAccess::$user, $description, $vatrate, $discount_type, $price_base_type);
1320 $this->db->commit();
1323 $this->db->rollback();
1324 throw new RestException(500,
'Error creating discount: '.$this->company->error, array_merge(array($this->company->error), $this->company->errors));
1355 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer') || !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1356 throw new RestException(403);
1360 throw new RestException(400,
'Thirdparty ID is mandatory');
1362 if (empty($discountid)) {
1363 throw new RestException(400,
'Discount ID is mandatory');
1365 if (empty($amount_ttc_1) || empty($amount_ttc_2)) {
1366 throw new RestException(400,
'Amount are mandatory');
1370 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1373 $result = $this->company->fetch(
$id);
1375 throw new RestException(404,
'Thirdparty not found');
1377 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1379 $res = $discount->fetch($discountid);
1381 throw new RestException(404,
'Discount not found');
1383 if ($discount->socid !=
$id) {
1384 throw new RestException(405,
'Discount not owned by this thirdpartie');
1387 if (
price2num((
float) $amount_ttc_1 + (
float) $amount_ttc_2) != $discount->amount_ttc) {
1388 throw new RestException(405,
'Sum of the 2 discounts is different that the original discount');
1390 if ($discount->fk_facture_line) {
1391 throw new RestException(409,
'Discount is already used');
1397 $newdiscount1->fk_facture_source = $discount->fk_facture_source;
1398 $newdiscount2->fk_facture_source = $discount->fk_facture_source;
1399 $newdiscount1->fk_facture = $discount->fk_facture;
1400 $newdiscount2->fk_facture = $discount->fk_facture;
1401 $newdiscount1->fk_facture_line = $discount->fk_facture_line;
1402 $newdiscount2->fk_facture_line = $discount->fk_facture_line;
1403 $newdiscount1->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1404 $newdiscount2->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1405 $newdiscount1->fk_invoice_supplier = $discount->fk_invoice_supplier;
1406 $newdiscount2->fk_invoice_supplier = $discount->fk_invoice_supplier;
1407 $newdiscount1->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1408 $newdiscount2->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1409 if ($discount->description ==
'(CREDIT_NOTE)' || $discount->description ==
'(DEPOSIT)') {
1410 $newdiscount1->description = $discount->description;
1411 $newdiscount2->description = $discount->description;
1413 $newdiscount1->description = $discount->description.
' (1)';
1414 $newdiscount2->description = $discount->description.
' (2)';
1417 $newdiscount1->fk_user = $discount->fk_user;
1418 $newdiscount2->fk_user = $discount->fk_user;
1419 $newdiscount1->fk_soc = $discount->fk_soc;
1420 $newdiscount1->socid = $discount->socid;
1421 $newdiscount2->fk_soc = $discount->fk_soc;
1422 $newdiscount2->socid = $discount->socid;
1423 $newdiscount1->discount_type = $discount->discount_type;
1424 $newdiscount2->discount_type = $discount->discount_type;
1425 $newdiscount1->datec = $discount->datec;
1426 $newdiscount2->datec = $discount->datec;
1427 $newdiscount1->tva_tx = $discount->tva_tx;
1428 $newdiscount2->tva_tx = $discount->tva_tx;
1429 $newdiscount1->vat_src_code = $discount->vat_src_code;
1430 $newdiscount2->vat_src_code = $discount->vat_src_code;
1431 $newdiscount1->amount_ttc = $amount_ttc_1;
1432 $newdiscount2->amount_ttc =
price2num($discount->amount_ttc - $newdiscount1->amount_ttc);
1433 $newdiscount1->amount_ht =
price2num($newdiscount1->amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1434 $newdiscount2->amount_ht =
price2num($newdiscount2->amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1435 $newdiscount1->amount_tva =
price2num($newdiscount1->amount_ttc - $newdiscount1->amount_ht);
1436 $newdiscount2->amount_tva =
price2num($newdiscount2->amount_ttc - $newdiscount2->amount_ht);
1438 $newdiscount1->multicurrency_amount_ttc = (float) $amount_ttc_1 * ($discount->multicurrency_amount_ttc / $discount->amount_ttc);
1439 $newdiscount2->multicurrency_amount_ttc =
price2num($discount->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ttc);
1440 $newdiscount1->multicurrency_amount_ht =
price2num($newdiscount1->multicurrency_amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1441 $newdiscount2->multicurrency_amount_ht =
price2num($newdiscount2->multicurrency_amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1442 $newdiscount1->multicurrency_amount_tva =
price2num($newdiscount1->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ht);
1443 $newdiscount2->multicurrency_amount_tva =
price2num($newdiscount2->multicurrency_amount_ttc - $newdiscount2->multicurrency_amount_ht);
1447 $newdiscount1->total_ttc = (float)
price2num($newdiscount1->amount_ttc);
1448 $newdiscount1->total_ht = (float)
price2num($newdiscount1->amount_ht);
1449 $newdiscount1->total_tva = (float)
price2num($newdiscount1->amount_tva);
1450 $newdiscount2->total_ttc = (float)
price2num($newdiscount2->amount_ttc);
1451 $newdiscount2->total_ht = (float)
price2num($newdiscount2->amount_ht);
1452 $newdiscount2->total_tva = (float)
price2num($newdiscount2->amount_tva);
1453 $newdiscount1->multicurrency_total_ttc = (float)
price2num($newdiscount1->multicurrency_amount_ttc);
1454 $newdiscount1->multicurrency_total_ht = (float)
price2num($newdiscount1->multicurrency_amount_ht);
1455 $newdiscount1->multicurrency_total_tva = (float)
price2num($newdiscount1->multicurrency_amount_tva);
1456 $newdiscount2->multicurrency_total_ttc = (float)
price2num($newdiscount2->multicurrency_amount_ttc);
1457 $newdiscount2->multicurrency_total_ht = (float)
price2num($newdiscount2->multicurrency_amount_ht);
1458 $newdiscount2->multicurrency_total_tva = (float)
price2num($newdiscount2->multicurrency_amount_tva);
1462 $discount->fk_facture_source = 0;
1464 $discount->fk_invoice_supplier_source = 0;
1465 $res = $discount->delete(DolibarrApiAccess::$user);
1466 $newid1 = $newdiscount1->create(DolibarrApiAccess::$user);
1467 $newid2 = $newdiscount2->create(DolibarrApiAccess::$user);
1468 if ($res <= 0 || $newid1 <= 0 || $newid2 <= 0) {
1469 $this->db->rollback();
1470 throw new RestException(500,
'Operation fail');
1473 $this->db->commit();
1475 $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";
1476 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1477 $sql .=
" WHERE re.rowid IN (".((int) $newid1).
",".((int) $newid2).
") AND f.rowid = re.fk_facture_source AND re.fk_soc = ".((int)
$id);
1479 $sql .= $this->db->order(
"f.type",
"ASC");
1481 $result = $this->db->query($sql);
1483 throw new RestException(503, $this->db->lasterror());
1486 while ($obj = $this->db->fetch_object($result)) {
1514 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1515 throw new RestException(403);
1518 throw new RestException(400,
'Thirdparty ID is mandatory');
1522 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1530 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1531 $invoice =
new Facture($this->db);
1532 $result = $invoice->list_replacable_invoices(
$id);
1534 throw new RestException(405, $invoice->error);
1563 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1564 throw new RestException(403);
1567 throw new RestException(400,
'Thirdparty ID is mandatory');
1571 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1579 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1580 $invoice =
new Facture($this->db);
1581 $result = $invoice->list_qualified_avoir_invoices(
$id);
1582 if (!is_array($result) && $result < 0) {
1583 throw new RestException(405, $invoice->error);
1607 throw new RestException(400,
'Thirdparty ID is mandatory');
1609 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1610 throw new RestException(403);
1613 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1620 $sql =
"SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1621 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1623 $sql .=
" WHERE fk_soc = ".((int)
$id);
1626 $result = $this->db->query($sql);
1627 if ($this->db->num_rows($result) == 0) {
1628 throw new RestException(404,
'Notification not found');
1633 $notifications = array();
1637 $num = $this->db->num_rows($result);
1641 $obj = $this->db->fetch_object($result);
1642 $notifications[] = $obj;
1646 throw new RestException(404,
'No notifications found');
1649 $fields = array(
'id',
'socid',
'event',
'contact_id',
'datec',
'tms',
'type');
1651 $returnNotifications = array();
1653 foreach ($notifications as $notification) {
1655 foreach ($notification as $key => $value) {
1656 if (in_array($key, $fields)) {
1660 $returnNotifications[] =
$object;
1664 return $returnNotifications;
1685 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1686 throw new RestException(403,
"User has no right to update thirdparties");
1688 if ($this->company->fetch(
$id) <= 0) {
1689 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1691 $notification =
new Notify($this->db);
1693 $notification->socid =
$id;
1695 foreach ($request_data as $field => $value) {
1696 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1699 $event = $notification->event;
1701 throw new RestException(500,
'Error creating Thirdparty Notification, request_data missing event');
1703 $socid = $notification->socid;
1704 $contact_id = $notification->contact_id;
1706 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1707 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1708 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1709 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1710 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1712 $exists_result = $this->db->query($exists_sql);
1713 if ($this->db->num_rows($exists_result) > 0) {
1714 throw new RestException(403,
'Notification already exists');
1717 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1718 throw new RestException(500,
'Error creating Thirdparty Notification');
1721 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1722 throw new RestException(500,
'Error updating values');
1748 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1749 throw new RestException(403,
"User has no right to update thirdparties");
1751 if ($this->company->fetch(
$id) <= 0) {
1752 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1754 $notification =
new Notify($this->db);
1755 $notification->socid =
$id;
1757 $sql =
"SELECT t.rowid as id FROM ".MAIN_DB_PREFIX.
"c_action_trigger as t";
1758 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
1760 $result = $this->db->query($sql);
1761 if ($this->db->num_rows($result) == 0) {
1762 throw new RestException(404,
'Action Trigger code not found');
1765 $notification->event = $this->db->fetch_row($result)[0];
1766 foreach ($request_data as $field => $value) {
1767 if ($field ===
'event') {
1768 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains event key');
1770 if ($field ===
'fk_action') {
1771 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains fk_action key');
1773 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1776 $event = $notification->event;
1777 $socid = $notification->socid;
1778 $contact_id = $notification->contact_id;
1780 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1781 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1782 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1783 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1784 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1786 $exists_result = $this->db->query($exists_sql);
1787 if ($this->db->num_rows($exists_result) > 0) {
1788 throw new RestException(403,
'Notification already exists');
1791 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1792 throw new RestException(500,
'Error creating Thirdparty Notification, are request_data well formed?');
1795 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1796 throw new RestException(500,
'Error updating values');
1818 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1819 throw new RestException(403);
1822 $notification =
new Notify($this->db);
1824 $notification->fetch($notification_id);
1826 $socid = (int) $notification->socid;
1828 if ($socid ==
$id) {
1829 return $notification->delete(DolibarrApiAccess::$user);
1831 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1854 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1855 throw new RestException(403,
"User has no right to update thirdparties");
1857 if ($this->company->fetch(
$id) <= 0) {
1858 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1860 $notification =
new Notify($this->db);
1863 $notification->fetch($notification_id,
$id);
1865 if ($notification->socid !=
$id) {
1866 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1869 foreach ($request_data as $field => $value) {
1870 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1873 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1874 throw new RestException(500,
'Error updating values');
1897 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1898 throw new RestException(403);
1901 throw new RestException(400,
'Thirdparty ID is mandatory');
1905 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1912 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1913 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1914 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1916 $sql .=
" WHERE fk_soc = ".((int)
$id);
1919 $result = $this->db->query($sql);
1921 if ($this->db->num_rows($result) == 0) {
1922 throw new RestException(404,
'Account not found');
1927 $accounts = array();
1931 $num = $this->db->num_rows($result);
1935 $obj = $this->db->fetch_object($result);
1938 if ($account->fetch($obj->rowid)) {
1939 $accounts[] = $account;
1944 throw new RestException(404,
'Account not found');
1948 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1950 $returnAccounts = array();
1952 foreach ($accounts as $account) {
1954 foreach ($account as $key => $value) {
1955 if (in_array($key, $fields)) {
1956 if ($key ==
'iban') {
1966 return $returnAccounts;
1987 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1988 throw new RestException(403);
1990 if ($this->company->fetch(
$id) <= 0) {
1991 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1995 $account->socid =
$id;
1997 foreach ($request_data as $field => $value) {
1998 if ($field ===
'caller') {
2000 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2004 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
2007 if ($account->create(DolibarrApiAccess::$user) < 0) {
2008 throw new RestException(500,
'Error creating Company Bank account');
2011 if (empty($account->rum)) {
2012 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
2014 $account->rum = $prelevement->buildRumNumber((
string) $this->company->code_client, $account->datec, (
string) $account->id);
2015 $account->date_rum =
dol_now();
2018 if ($account->update(DolibarrApiAccess::$user) < 0) {
2019 throw new RestException(500,
'Error updating values');
2044 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2045 throw new RestException(403);
2047 if ($this->company->fetch(
$id) <= 0) {
2048 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
2053 $account->fetch($bankaccount_id,
'',
$id, -1,
'');
2055 if ($account->socid !=
$id) {
2056 throw new RestException(403);
2060 foreach ($request_data as $field => $value) {
2061 if ($field ===
'caller') {
2063 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2070 if (empty($account->rum)) {
2071 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
2073 $account->rum = $prelevement->buildRumNumber((
string) $this->company->code_client, $account->datec, (
string) $account->id);
2074 $account->date_rum =
dol_now();
2077 if ($account->update(DolibarrApiAccess::$user) < 0) {
2078 throw new RestException(500,
'Error updating values');
2100 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2101 throw new RestException(403);
2106 $account->fetch($bankaccount_id);
2108 $socid = (int) $account->socid;
2110 if ($socid ==
$id) {
2111 return $account->delete(DolibarrApiAccess::$user);
2113 throw new RestException(403,
"Not allowed due to bad consistency of input data");
2137 global
$conf, $langs;
2139 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
2141 if ($this->company->fetch(
$id) <= 0) {
2142 throw new RestException(404,
'Thirdparty not found');
2145 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2146 throw new RestException(403);
2149 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
2151 $this->company->fk_bank = $this->company->fk_account;
2154 $outputlangs = $langs;
2159 if (isset($this->company->thirdparty->default_lang)) {
2160 $newlang = $this->company->thirdparty->default_lang;
2161 } elseif (isset($this->company->default_lang)) {
2162 $newlang = $this->company->default_lang;
2165 if (!empty($newlang)) {
2167 $outputlangs->setDefaultLang($newlang);
2170 $sql =
"SELECT rowid";
2171 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
2173 $sql .=
" WHERE fk_soc = ".((int)
$id);
2175 if ($companybankid) {
2176 $sql .=
" AND rowid = ".((int) $companybankid);
2180 $accounts = array();
2182 $result = $this->db->query($sql);
2184 if ($this->db->num_rows($result) == 0) {
2185 throw new RestException(404,
'Bank account not found');
2188 $num = $this->db->num_rows($result);
2192 $obj = $this->db->fetch_object($result);
2195 if ($account->fetch($obj->rowid)) {
2196 $accounts[] = $account;
2201 throw new RestException(500,
'Sql error '.$this->db->lasterror());
2204 $moreparams = array(
2205 'use_companybankid' => $accounts[0]->
id,
2206 'force_dir_output' =>
$conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName((
string) $this->company->id)
2209 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
2212 return array(
"success" => $result);
2214 throw new RestException(500,
'Error generating the document '.$this->company->error);
2237 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2238 throw new RestException(403);
2242 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2248 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2249 $sql .=
" WHERE fk_soc = ".((int)
$id);
2251 $sql .=
" AND site ='".$this->db->escape($site).
"'";
2254 $result = $this->db->query($sql);
2256 if ($result && $this->db->num_rows($result) == 0) {
2257 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
2262 $accounts = array();
2265 $num = $this->db->num_rows($result);
2269 $obj = $this->db->fetch_object($result);
2272 if ($account->fetch($obj->rowid)) {
2273 $accounts[] = $account;
2278 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
2280 $returnAccounts = array();
2282 foreach ($accounts as $account) {
2284 foreach ($account as $key => $value) {
2285 if (in_array($key, $fields)) {
2292 return $returnAccounts;
2314 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2315 throw new RestException(403);
2318 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2319 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
2320 $sql .=
" AND entity IN (".getEntity(
'societe').
")";
2322 $result = $this->db->query($sql);
2324 if ($result && $this->db->num_rows($result) == 1) {
2325 $obj = $this->db->fetch_object($result);
2326 $returnThirdparty = $this->
_fetch($obj->fk_soc);
2328 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
2332 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2335 return $returnThirdparty;
2363 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2364 throw new RestException(403);
2367 if (!isset($request_data[
'site'])) {
2368 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
2371 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
2372 $result = $this->db->query($sql);
2374 if ($result && $this->db->num_rows($result) == 0) {
2376 if (!isset($request_data[
'login'])) {
2377 $account->login =
"";
2379 $account->fk_soc =
$id;
2381 foreach ($request_data as $field => $value) {
2382 if ($field ===
'caller') {
2384 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2391 if ($account->create(DolibarrApiAccess::$user) < 0) {
2392 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
2399 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
2431 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2432 throw new RestException(403);
2435 $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).
"'";
2436 $result = $this->db->query($sql);
2439 if ($result && $this->db->num_rows($result) == 0) {
2440 if (!isset($request_data[
'key_account'])) {
2441 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
2444 if (!isset($request_data[
'login'])) {
2445 $account->login =
"";
2448 foreach ($request_data as $field => $value) {
2449 if ($field ===
'caller') {
2451 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2458 $account->fk_soc =
$id;
2459 $account->site = $site;
2461 if ($account->create(DolibarrApiAccess::$user) < 0) {
2462 throw new RestException(500,
'Error creating SocieteAccount entity.');
2466 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2467 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2468 $result = $this->db->query($sql);
2470 if ($result && $this->db->num_rows($result) !== 0) {
2471 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.");
2475 $obj = $this->db->fetch_object($result);
2478 $account->id = $obj->rowid;
2479 $account->fk_soc =
$id;
2480 $account->site = $site;
2481 if (!isset($request_data[
'login'])) {
2482 $account->login =
"";
2484 $account->fk_user_creat = $obj->fk_user_creat;
2485 $account->date_creation = $obj->date_creation;
2487 foreach ($request_data as $field => $value) {
2488 if ($field ===
'caller') {
2490 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2497 if ($account->update(DolibarrApiAccess::$user) < 0) {
2498 throw new RestException(500,
'Error updating SocieteAccount entity.');
2529 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2530 throw new RestException(403);
2533 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2534 $result = $this->db->query($sql);
2536 if ($result && $this->db->num_rows($result) == 0) {
2537 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
2540 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2541 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2542 $result = $this->db->query($sql);
2544 if ($result && $this->db->num_rows($result) !== 0) {
2545 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.");
2549 $obj = $this->db->fetch_object($result);
2551 $account->fetch($obj->rowid);
2553 foreach ($request_data as $field => $value) {
2554 if ($field ===
'caller') {
2556 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2563 if ($account->update(DolibarrApiAccess::$user) < 0) {
2564 throw new RestException(500,
'Error updating SocieteAccount account');
2593 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2594 throw new RestException(403);
2597 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2598 $result = $this->db->query($sql);
2600 if ($result && $this->db->num_rows($result) == 0) {
2601 throw new RestException(404);
2603 $obj = $this->db->fetch_object($result);
2605 $account->fetch($obj->rowid);
2607 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2608 throw new RestException(500,
"Error while deleting $site account attached to this third party");
2630 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2631 throw new RestException(403);
2638 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2639 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id);
2641 $result = $this->db->query($sql);
2643 if ($result && $this->db->num_rows($result) == 0) {
2644 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
2649 $num = $this->db->num_rows($result);
2653 $obj = $this->db->fetch_object($result);
2655 $account->fetch($obj->rowid);
2657 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2658 throw new RestException(500,
'Error while deleting account attached to this third party');
2684 unset(
$object->departement_code);
2693 unset(
$object->commercial_id);
2697 unset(
$object->total_localtax1);
2698 unset(
$object->total_localtax2);
2704 unset(
$object->fk_delivery_address);
2719 if ($data ===
null) {
2722 $thirdparty = array();
2723 foreach (Thirdparties::$FIELDS as $field) {
2724 if (!isset($data[$field])) {
2725 throw new RestException(400,
"$field field missing");
2727 $thirdparty[$field] = $data[$field];
2755 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2757 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2758 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2762 $result = $this->company->initAsSpecimen();
2764 $result = $this->company->fetch((
int) $rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2767 throw new RestException(404,
'Thirdparty not found');
2771 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2774 $this->company->getNoEmail();
2778 $filterabsolutediscount =
"fk_facture_source IS NULL";
2779 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2781 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2782 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2785 $absolute_discount = $this->company->getAvailableDiscounts(
null, $filterabsolutediscount);
2786 $absolute_creditnote = $this->company->getAvailableDiscounts(
null, $filtercreditnote);
2787 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2788 $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.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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
buildzip.php
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.