28use Luracast\Restler\RestException;
43 public static $FIELDS = array(
60 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
61 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
62 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
63 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
64 require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
66 $this->company =
new Societe($this->db);
69 static::$FIELDS[] =
'email';
85 public function get(
$id)
108 return $this->
_fetch(
null,
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
127 return $this->
_fetch(
null,
'',
'', $barcode);
152 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
156 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
157 throw new RestException(403);
161 $socids = DolibarrApiAccess::$user->socid ? (string) DolibarrApiAccess::$user->socid :
'';
165 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
166 $search_sale = DolibarrApiAccess::$user->id;
169 $sql =
"SELECT t.rowid";
170 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
171 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
174 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
176 if (!in_array($mode, array(1, 2, 3))) {
177 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
180 $sql .=
", ".MAIN_DB_PREFIX.
"c_stcomm as st";
181 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
182 $sql .=
" AND t.fk_stcomm = st.id";
184 $sql .=
" AND t.client IN (1, 3)";
185 } elseif ($mode == 2) {
186 $sql .=
" AND t.client IN (2, 3)";
187 } elseif ($mode == 3) {
188 $sql .=
" AND t.client IN (0)";
189 } elseif ($mode == 4) {
190 $sql .=
" AND t.fournisseur IN (1)";
194 if (!empty($mode) && $mode != 4) {
195 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
196 } elseif (!empty($mode) && $mode == 4) {
197 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
199 $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))";
203 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
206 if ($search_sale && $search_sale !=
'-1') {
207 if ($search_sale == -2) {
208 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.rowid)";
209 } elseif ($search_sale > 0) {
210 $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).
")";
218 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
223 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
225 $sql .= $this->db->order($sortfield, $sortorder);
230 $offset = $limit * $page;
232 $sql .= $this->db->plimit($limit + 1, $offset);
235 $result = $this->db->query($sql);
237 $num = $this->db->num_rows($result);
238 $min = min($num, ($limit <= 0 ? $num : $limit));
241 $obj = $this->db->fetch_object($result);
242 $soc_static =
new Societe($this->db);
243 if ($soc_static->fetch($obj->rowid)) {
245 $soc_static->getNoEmail();
252 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->db->lasterror());
254 if (!count($obj_ret)) {
258 $message =
'No third parties found';
261 $message =
'No customers found';
264 $message =
'No prospects found';
267 $message =
'No other third parties found';
270 $message =
'No suppliers found';
272 throw new RestException(404, $message);
276 if ($pagination_data) {
277 $totalsResult = $this->db->query($sqlTotals);
278 $total = $this->db->fetch_object($totalsResult)->total;
283 $obj_ret[
'data'] = $tmp;
284 $obj_ret[
'pagination'] = [
285 'total' => (int) $total,
287 'page_count' => ceil((
int) $total / $limit),
307 public function post($request_data =
null)
309 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
310 throw new RestException(403);
314 if (!isset($request_data[
'country_id']) && isset($request_data[
'country_code'])) {
315 $field = strlen($request_data[
'country_code']) > 2 ?
'code_iso' :
'code';
316 $id =
dol_getIdFromCode($this->db, $request_data[
'country_code'],
"c_country", $field,
"rowid");
318 throw new RestException(404,
'Country code not found in database: ' . $this->db->error);
320 $request_data[
'country_id'] =
$id;
324 $result = $this->
_validate($request_data);
326 foreach ($request_data as $field => $value) {
327 if ($field ===
'caller') {
329 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
332 if ($field ==
'array_options' && is_array($value)) {
333 foreach ($value as $index => $val) {
334 $this->company->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->company);
339 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
342 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
343 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
345 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
346 $this->company->setNoEmail($this->company->no_email);
349 return $this->company->id;
369 public function put(
$id, $request_data =
null)
371 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
372 throw new RestException(403);
375 $result = $this->company->fetch(
$id);
377 throw new RestException(404,
'Thirdparty not found');
381 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
384 foreach ($request_data as $field => $value) {
385 if ($field ==
'id') {
388 if ($field ===
'caller') {
390 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
393 if ($field ==
'array_options' && is_array($value)) {
394 foreach ($value as $index => $val) {
395 $this->company->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->company);
399 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
402 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
403 $this->company->setNoEmail($this->company->no_email);
406 if ($this->company->update(
$id, DolibarrApiAccess::$user, 1, 1, 1,
'update', 1) > 0) {
407 return $this->
get(
$id);
409 throw new RestException(500, $this->company->error);
436 if (
$id == $idtodelete) {
437 throw new RestException(400,
'Try to merge a thirdparty into itself');
440 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
441 throw new RestException(403);
444 $result = $this->company->fetch(
$id);
446 throw new RestException(404,
'Thirdparty not found');
450 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
453 $companytoremove =
new Societe($this->db);
454 $result = $companytoremove->fetch($idtodelete);
456 throw new RestException(404,
'Thirdparty not found');
460 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
463 $user = DolibarrApiAccess::$user;
464 $result = $this->company->mergeCompany($companytoremove->id);
466 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.
$id.
'. Enable and read log file for more information.');
469 return $this->
get(
$id);
484 public function delete(
$id)
486 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
487 throw new RestException(403);
489 $result = $this->company->fetch(
$id);
491 throw new RestException(404,
'Thirdparty not found');
494 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
496 $this->company->oldcopy = clone $this->company;
498 $res = $this->company->delete(
$id);
500 throw new RestException(500,
"Can't delete, error occurs");
501 } elseif ($res == 0) {
502 throw new RestException(409,
"Can't delete, that product is probably used");
508 'message' =>
'Object deleted'
535 throw new RestException(501,
'Module "Thirdparties" needed for this request');
539 throw new RestException(501,
'Module "Products" needed for this request');
543 throw new RestException(501,
'Multiprices features activation needed for this request');
546 if ($priceLevel < 1 || $priceLevel >
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT')) {
547 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
550 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
551 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
554 $result = $this->company->fetch(
$id);
556 throw new RestException(404,
'Thirdparty '.
$id.
' not found');
559 if (empty($result)) {
560 throw new RestException(500,
'Error fetching thirdparty '.
$id, array_merge(array($this->company->error), $this->company->errors));
564 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
567 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
569 throw new RestException(500,
'Error setting new price level for thirdparty '.
$id, array($this->company->db->lasterror()));
591 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
592 throw new RestException(403);
594 $result = $this->company->fetch(
$id);
596 throw new RestException(404,
'Thirdparty not found');
598 $usertmp =
new User($this->db);
599 $result = $usertmp->fetch($representative_id);
601 throw new RestException(404,
'User not found');
604 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
606 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
627 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
628 throw new RestException(403);
630 $result = $this->company->fetch(
$id);
632 throw new RestException(404,
'Thirdparty not found');
634 $usertmp =
new User($this->db);
635 $result = $usertmp->fetch($representative_id);
637 throw new RestException(404,
'User not found');
640 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
642 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
665 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
667 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
668 throw new RestException(403);
671 $result = $this->company->fetch(
$id);
673 throw new RestException(404,
'Thirdparty not found');
678 $arrayofcateg = $categories->getListForItem(
$id,
'customer', $sortfield, $sortorder, $limit, $page);
680 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
681 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
684 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
688 return $arrayofcateg;
709 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
710 throw new RestException(403);
713 $result = $this->company->fetch(
$id);
715 throw new RestException(404,
'Thirdparty not found');
718 $result = $category->fetch($category_id);
720 throw new RestException(404,
'category not found');
724 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
727 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
730 $category->add_type($this->company,
'customer');
753 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
754 throw new RestException(403);
757 $result = $this->company->fetch(
$id);
759 throw new RestException(404,
'Thirdparty not found');
762 $result = $category->fetch($category_id);
764 throw new RestException(404,
'category not found');
768 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
771 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
774 $category->del_type($this->company,
'customer');
800 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
801 throw new RestException(403);
804 $result = $this->company->fetch(
$id);
806 throw new RestException(404,
'Thirdparty not found');
811 $result = $categories->getListForItem(
$id,
'supplier', $sortfield, $sortorder, $limit, $page);
813 if (is_numeric($result) && $result < 0) {
814 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
817 if (is_numeric($result) && $result == 0) {
842 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
843 throw new RestException(403);
846 $result = $this->company->fetch(
$id);
848 throw new RestException(404,
'Thirdparty not found');
851 $result = $category->fetch($category_id);
853 throw new RestException(404,
'category not found');
857 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
860 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
863 $category->add_type($this->company,
'supplier');
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->del_type($this->company,
'supplier');
933 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
934 throw new RestException(403);
938 throw new RestException(400,
'Thirdparty ID is mandatory');
942 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
945 $result = $this->company->fetch(
$id);
947 throw new RestException(404,
'Thirdparty not found');
950 $result = $this->company->getOutstandingProposals($mode);
952 unset($result[
'total_ht']);
953 unset($result[
'total_ttc']);
979 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
980 throw new RestException(403);
984 throw new RestException(400,
'Thirdparty ID is mandatory');
988 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
991 $result = $this->company->fetch(
$id);
993 throw new RestException(404,
'Thirdparty not found');
996 $result = $this->company->getOutstandingOrders($mode);
998 unset($result[
'total_ht']);
999 unset($result[
'total_ttc']);
1024 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1025 throw new RestException(403);
1029 throw new RestException(400,
'Thirdparty ID is mandatory');
1033 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1036 $result = $this->company->fetch(
$id);
1038 throw new RestException(404,
'Thirdparty not found');
1041 $result = $this->company->getOutstandingBills($mode);
1043 unset($result[
'total_ht']);
1044 unset($result[
'total_ttc']);
1069 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1070 throw new RestException(403);
1074 throw new RestException(400,
'Thirdparty ID is mandatory');
1078 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1081 $result = $this->company->fetch(
$id);
1083 throw new RestException(404,
'Thirdparty not found');
1086 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1119 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1120 throw new RestException(403);
1124 throw new RestException(400,
'Thirdparty ID is mandatory');
1128 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1131 $result = $this->company->fetch(
$id);
1133 throw new RestException(404,
'Thirdparty not found');
1137 if ($mode ===
'customer') {
1138 $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";
1139 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1140 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as f ON f.rowid = re.fk_facture_source";
1141 $sql .=
" WHERE re.fk_soc = ".((int)
$id);
1142 if ($filter ==
"available") {
1143 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1145 if ($filter ==
"used") {
1146 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1148 } elseif ($mode ===
'supplier') {
1149 $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";
1150 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1151 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_fourn as f ON f.rowid = re.fk_invoice_supplier_source";
1152 $sql .=
" WHERE f.rowid = re.fk_invoice_supplier_source AND re.fk_soc = ".((int)
$id);
1153 if ($filter ==
"available") {
1154 $sql .=
" AND re.fk_invoice_supplier IS NULL AND re.fk_invoice_supplier_line IS NULL";
1156 if ($filter ==
"used") {
1157 $sql .=
" AND (re.fk_invoice_supplier IS NOT NULL OR re.fk_invoice_supplier_line IS NOT NULL)";
1161 $sql .= $this->db->order($sortfield, $sortorder);
1163 $result = $this->db->query($sql);
1165 throw new RestException(503, $this->db->lasterror());
1168 while ($obj = $this->db->fetch_object($result)) {
1203 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer') || !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1204 throw new RestException(403);
1208 throw new RestException(400,
'Thirdparty ID is mandatory');
1210 if (empty($discountid)) {
1211 throw new RestException(400,
'Discount ID is mandatory');
1213 if (empty($amount_ttc_1) || empty($amount_ttc_2)) {
1214 throw new RestException(400,
'Amount are mandatory');
1218 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1221 $result = $this->company->fetch(
$id);
1223 throw new RestException(404,
'Thirdparty not found');
1225 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1227 $res = $discount->fetch($discountid);
1229 throw new RestException(404,
'Discount not found');
1231 if ($discount->socid !=
$id) {
1232 throw new RestException(405,
'Discount not owned by this thirdpartie');
1235 if (
price2num((
float) $amount_ttc_1 + (
float) $amount_ttc_2) != $discount->amount_ttc) {
1236 throw new RestException(405,
'Sum of the 2 discounts is different that the original discount');
1238 if ($discount->fk_facture_line) {
1239 throw new RestException(409,
'Discount is already used');
1245 $newdiscount1->fk_facture_source = $discount->fk_facture_source;
1246 $newdiscount2->fk_facture_source = $discount->fk_facture_source;
1247 $newdiscount1->fk_facture = $discount->fk_facture;
1248 $newdiscount2->fk_facture = $discount->fk_facture;
1249 $newdiscount1->fk_facture_line = $discount->fk_facture_line;
1250 $newdiscount2->fk_facture_line = $discount->fk_facture_line;
1251 $newdiscount1->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1252 $newdiscount2->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1253 $newdiscount1->fk_invoice_supplier = $discount->fk_invoice_supplier;
1254 $newdiscount2->fk_invoice_supplier = $discount->fk_invoice_supplier;
1255 $newdiscount1->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1256 $newdiscount2->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1257 if ($discount->description ==
'(CREDIT_NOTE)' || $discount->description ==
'(DEPOSIT)') {
1258 $newdiscount1->description = $discount->description;
1259 $newdiscount2->description = $discount->description;
1261 $newdiscount1->description = $discount->description.
' (1)';
1262 $newdiscount2->description = $discount->description.
' (2)';
1265 $newdiscount1->fk_user = $discount->fk_user;
1266 $newdiscount2->fk_user = $discount->fk_user;
1267 $newdiscount1->fk_soc = $discount->fk_soc;
1268 $newdiscount1->socid = $discount->socid;
1269 $newdiscount2->fk_soc = $discount->fk_soc;
1270 $newdiscount2->socid = $discount->socid;
1271 $newdiscount1->discount_type = $discount->discount_type;
1272 $newdiscount2->discount_type = $discount->discount_type;
1273 $newdiscount1->datec = $discount->datec;
1274 $newdiscount2->datec = $discount->datec;
1275 $newdiscount1->tva_tx = $discount->tva_tx;
1276 $newdiscount2->tva_tx = $discount->tva_tx;
1277 $newdiscount1->vat_src_code = $discount->vat_src_code;
1278 $newdiscount2->vat_src_code = $discount->vat_src_code;
1279 $newdiscount1->amount_ttc = $amount_ttc_1;
1280 $newdiscount2->amount_ttc =
price2num($discount->amount_ttc - $newdiscount1->amount_ttc);
1281 $newdiscount1->amount_ht =
price2num($newdiscount1->amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1282 $newdiscount2->amount_ht =
price2num($newdiscount2->amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1283 $newdiscount1->amount_tva =
price2num($newdiscount1->amount_ttc - $newdiscount1->amount_ht);
1284 $newdiscount2->amount_tva =
price2num($newdiscount2->amount_ttc - $newdiscount2->amount_ht);
1286 $newdiscount1->multicurrency_amount_ttc = (float) $amount_ttc_1 * ($discount->multicurrency_amount_ttc / $discount->amount_ttc);
1287 $newdiscount2->multicurrency_amount_ttc =
price2num($discount->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ttc);
1288 $newdiscount1->multicurrency_amount_ht =
price2num($newdiscount1->multicurrency_amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1289 $newdiscount2->multicurrency_amount_ht =
price2num($newdiscount2->multicurrency_amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1290 $newdiscount1->multicurrency_amount_tva =
price2num($newdiscount1->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ht);
1291 $newdiscount2->multicurrency_amount_tva =
price2num($newdiscount2->multicurrency_amount_ttc - $newdiscount2->multicurrency_amount_ht);
1295 $newdiscount1->total_ttc = (float)
price2num($newdiscount1->amount_ttc);
1296 $newdiscount1->total_ht = (float)
price2num($newdiscount1->amount_ht);
1297 $newdiscount1->total_tva = (float)
price2num($newdiscount1->amount_tva);
1298 $newdiscount2->total_ttc = (float)
price2num($newdiscount2->amount_ttc);
1299 $newdiscount2->total_ht = (float)
price2num($newdiscount2->amount_ht);
1300 $newdiscount2->total_tva = (float)
price2num($newdiscount2->amount_tva);
1301 $newdiscount1->multicurrency_total_ttc = (float)
price2num($newdiscount1->multicurrency_amount_ttc);
1302 $newdiscount1->multicurrency_total_ht = (float)
price2num($newdiscount1->multicurrency_amount_ht);
1303 $newdiscount1->multicurrency_total_tva = (float)
price2num($newdiscount1->multicurrency_amount_tva);
1304 $newdiscount2->multicurrency_total_ttc = (float)
price2num($newdiscount2->multicurrency_amount_ttc);
1305 $newdiscount2->multicurrency_total_ht = (float)
price2num($newdiscount2->multicurrency_amount_ht);
1306 $newdiscount2->multicurrency_total_tva = (float)
price2num($newdiscount2->multicurrency_amount_tva);
1310 $discount->fk_facture_source = 0;
1312 $discount->fk_invoice_supplier_source = 0;
1313 $res = $discount->delete(DolibarrApiAccess::$user);
1314 $newid1 = $newdiscount1->create(DolibarrApiAccess::$user);
1315 $newid2 = $newdiscount2->create(DolibarrApiAccess::$user);
1316 if ($res <= 0 || $newid1 <= 0 || $newid2 <= 0) {
1317 $this->db->rollback();
1318 throw new RestException(500,
'Operation fail');
1321 $this->db->commit();
1323 $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";
1324 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1325 $sql .=
" WHERE re.rowid IN ( $newid1, $newid2 ) AND f.rowid = re.fk_facture_source AND re.fk_soc = ".((int)
$id);
1327 $sql .= $this->db->order(
"f.type",
"ASC");
1329 $result = $this->db->query($sql);
1331 throw new RestException(503, $this->db->lasterror());
1334 while ($obj = $this->db->fetch_object($result)) {
1362 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1363 throw new RestException(403);
1366 throw new RestException(400,
'Thirdparty ID is mandatory');
1370 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1378 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1379 $invoice =
new Facture($this->db);
1380 $result = $invoice->list_replacable_invoices(
$id);
1382 throw new RestException(405, $invoice->error);
1411 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1412 throw new RestException(403);
1415 throw new RestException(400,
'Thirdparty ID is mandatory');
1419 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1427 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1428 $invoice =
new Facture($this->db);
1429 $result = $invoice->list_qualified_avoir_invoices(
$id);
1430 if (!is_array($result) && $result < 0) {
1431 throw new RestException(405, $invoice->error);
1455 throw new RestException(400,
'Thirdparty ID is mandatory');
1457 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1458 throw new RestException(403);
1461 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1468 $sql =
"SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1469 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1471 $sql .=
" WHERE fk_soc = ".((int)
$id);
1474 $result = $this->db->query($sql);
1475 if ($this->db->num_rows($result) == 0) {
1476 throw new RestException(404,
'Notification not found');
1481 $notifications = array();
1485 $num = $this->db->num_rows($result);
1489 $obj = $this->db->fetch_object($result);
1490 $notifications[] = $obj;
1494 throw new RestException(404,
'No notifications found');
1497 $fields = array(
'id',
'socid',
'event',
'contact_id',
'datec',
'tms',
'type');
1499 $returnNotifications = array();
1501 foreach ($notifications as $notification) {
1503 foreach ($notification as $key => $value) {
1504 if (in_array($key, $fields)) {
1508 $returnNotifications[] =
$object;
1512 return $returnNotifications;
1533 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1534 throw new RestException(403,
"User has no right to update thirdparties");
1536 if ($this->company->fetch(
$id) <= 0) {
1537 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1539 $notification =
new Notify($this->db);
1541 $notification->socid =
$id;
1543 foreach ($request_data as $field => $value) {
1544 $notification->$field = $value;
1547 $event = $notification->event;
1549 throw new RestException(500,
'Error creating Thirdparty Notification, request_data missing event');
1551 $socid = $notification->socid;
1552 $contact_id = $notification->contact_id;
1554 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1555 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1556 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1557 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1558 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1560 $exists_result = $this->db->query($exists_sql);
1561 if ($this->db->num_rows($exists_result) > 0) {
1562 throw new RestException(403,
'Notification already exists');
1565 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1566 throw new RestException(500,
'Error creating Thirdparty Notification');
1569 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1570 throw new RestException(500,
'Error updating values');
1596 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1597 throw new RestException(403,
"User has no right to update thirdparties");
1599 if ($this->company->fetch(
$id) <= 0) {
1600 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1602 $notification =
new Notify($this->db);
1603 $notification->socid =
$id;
1605 $sql =
"SELECT t.rowid as id FROM ".MAIN_DB_PREFIX.
"c_action_trigger as t";
1606 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
1608 $result = $this->db->query($sql);
1609 if ($this->db->num_rows($result) == 0) {
1610 throw new RestException(404,
'Action Trigger code not found');
1613 $notification->event = $this->db->fetch_row($result)[0];
1614 foreach ($request_data as $field => $value) {
1615 if ($field ===
'event') {
1616 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains event key');
1618 if ($field ===
'fk_action') {
1619 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains fk_action key');
1621 $notification->$field = $value;
1624 $event = $notification->event;
1625 $socid = $notification->socid;
1626 $contact_id = $notification->contact_id;
1628 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1629 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1630 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1631 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1632 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1634 $exists_result = $this->db->query($exists_sql);
1635 if ($this->db->num_rows($exists_result) > 0) {
1636 throw new RestException(403,
'Notification already exists');
1639 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1640 throw new RestException(500,
'Error creating Thirdparty Notification, are request_data well formed?');
1643 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1644 throw new RestException(500,
'Error updating values');
1666 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1667 throw new RestException(403);
1670 $notification =
new Notify($this->db);
1672 $notification->fetch($notification_id);
1674 $socid = (int) $notification->socid;
1676 if ($socid ==
$id) {
1677 return $notification->delete(DolibarrApiAccess::$user);
1679 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1702 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1703 throw new RestException(403,
"User has no right to update thirdparties");
1705 if ($this->company->fetch(
$id) <= 0) {
1706 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1708 $notification =
new Notify($this->db);
1711 $notification->fetch($notification_id,
$id);
1713 if ($notification->socid !=
$id) {
1714 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1717 foreach ($request_data as $field => $value) {
1718 $notification->$field = $value;
1721 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1722 throw new RestException(500,
'Error updating values');
1745 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1746 throw new RestException(403);
1749 throw new RestException(400,
'Thirdparty ID is mandatory');
1753 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1760 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1761 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1762 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1764 $sql .=
" WHERE fk_soc = ".((int)
$id);
1767 $result = $this->db->query($sql);
1769 if ($this->db->num_rows($result) == 0) {
1770 throw new RestException(404,
'Account not found');
1775 $accounts = array();
1779 $num = $this->db->num_rows($result);
1783 $obj = $this->db->fetch_object($result);
1786 if ($account->fetch($obj->rowid)) {
1787 $accounts[] = $account;
1792 throw new RestException(404,
'Account not found');
1796 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1798 $returnAccounts = array();
1800 foreach ($accounts as $account) {
1802 foreach ($account as $key => $value) {
1803 if (in_array($key, $fields)) {
1804 if ($key ==
'iban') {
1814 return $returnAccounts;
1835 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1836 throw new RestException(403);
1838 if ($this->company->fetch(
$id) <= 0) {
1839 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1843 $account->socid =
$id;
1845 foreach ($request_data as $field => $value) {
1846 if ($field ===
'caller') {
1848 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1852 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
1855 if ($account->create(DolibarrApiAccess::$user) < 0) {
1856 throw new RestException(500,
'Error creating Company Bank account');
1859 if (empty($account->rum)) {
1860 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1862 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, (
string) $account->id);
1863 $account->date_rum =
dol_now();
1866 if ($account->update(DolibarrApiAccess::$user) < 0) {
1867 throw new RestException(500,
'Error updating values');
1892 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1893 throw new RestException(403);
1895 if ($this->company->fetch(
$id) <= 0) {
1896 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1901 $account->fetch($bankaccount_id,
'',
$id, -1,
'');
1903 if ($account->socid !=
$id) {
1904 throw new RestException(403);
1908 foreach ($request_data as $field => $value) {
1909 if ($field ===
'caller') {
1911 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1918 if (empty($account->rum)) {
1919 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1921 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, (
string) $account->id);
1922 $account->date_rum =
dol_now();
1925 if ($account->update(DolibarrApiAccess::$user) < 0) {
1926 throw new RestException(500,
'Error updating values');
1948 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1949 throw new RestException(403);
1954 $account->fetch($bankaccount_id);
1956 $socid = (int) $account->socid;
1958 if ($socid ==
$id) {
1959 return $account->delete(DolibarrApiAccess::$user);
1961 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1985 global $conf, $langs;
1987 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1989 if ($this->company->fetch(
$id) <= 0) {
1990 throw new RestException(404,
'Thirdparty not found');
1993 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1994 throw new RestException(403);
1997 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1999 $this->company->fk_bank = $this->company->fk_account;
2002 $outputlangs = $langs;
2007 if (isset($this->company->thirdparty->default_lang)) {
2008 $newlang = $this->company->thirdparty->default_lang;
2009 } elseif (isset($this->company->default_lang)) {
2010 $newlang = $this->company->default_lang;
2013 if (!empty($newlang)) {
2014 $outputlangs =
new Translate(
"", $conf);
2015 $outputlangs->setDefaultLang($newlang);
2018 $sql =
"SELECT rowid";
2019 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
2021 $sql .=
" WHERE fk_soc = ".((int)
$id);
2023 if ($companybankid) {
2024 $sql .=
" AND rowid = ".((int) $companybankid);
2028 $accounts = array();
2030 $result = $this->db->query($sql);
2032 if ($this->db->num_rows($result) == 0) {
2033 throw new RestException(404,
'Bank account not found');
2036 $num = $this->db->num_rows($result);
2040 $obj = $this->db->fetch_object($result);
2043 if ($account->fetch($obj->rowid)) {
2044 $accounts[] = $account;
2049 throw new RestException(500,
'Sql error '.$this->db->lasterror());
2052 $moreparams = array(
2053 'use_companybankid' => $accounts[0]->
id,
2054 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName((
string) $this->company->id)
2057 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
2060 return array(
"success" => $result);
2062 throw new RestException(500,
'Error generating the document '.$this->company->error);
2085 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2086 throw new RestException(403);
2090 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2096 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2097 $sql .=
" WHERE fk_soc = ".((int)
$id);
2099 $sql .=
" AND site ='".$this->db->escape($site).
"'";
2102 $result = $this->db->query($sql);
2104 if ($result && $this->db->num_rows($result) == 0) {
2105 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
2110 $accounts = array();
2113 $num = $this->db->num_rows($result);
2117 $obj = $this->db->fetch_object($result);
2120 if ($account->fetch($obj->rowid)) {
2121 $accounts[] = $account;
2126 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
2128 $returnAccounts = array();
2130 foreach ($accounts as $account) {
2132 foreach ($account as $key => $value) {
2133 if (in_array($key, $fields)) {
2140 return $returnAccounts;
2162 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2163 throw new RestException(403);
2166 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2167 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
2168 $sql .=
" AND entity IN (".getEntity(
'societe').
")";
2170 $result = $this->db->query($sql);
2172 if ($result && $this->db->num_rows($result) == 1) {
2173 $obj = $this->db->fetch_object($result);
2174 $returnThirdparty = $this->
_fetch($obj->fk_soc);
2176 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
2180 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2183 return $returnThirdparty;
2211 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2212 throw new RestException(403);
2215 if (!isset($request_data[
'site'])) {
2216 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
2219 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
2220 $result = $this->db->query($sql);
2222 if ($result && $this->db->num_rows($result) == 0) {
2224 if (!isset($request_data[
'login'])) {
2225 $account->login =
"";
2227 $account->fk_soc =
$id;
2229 foreach ($request_data as $field => $value) {
2230 if ($field ===
'caller') {
2232 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2239 if ($account->create(DolibarrApiAccess::$user) < 0) {
2240 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
2247 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
2279 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2280 throw new RestException(403);
2283 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
2284 $result = $this->db->query($sql);
2287 if ($result && $this->db->num_rows($result) == 0) {
2288 if (!isset($request_data[
'key_account'])) {
2289 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
2292 if (!isset($request_data[
'login'])) {
2293 $account->login =
"";
2296 foreach ($request_data as $field => $value) {
2297 if ($field ===
'caller') {
2299 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2306 $account->fk_soc =
$id;
2307 $account->site = $site;
2309 if ($account->create(DolibarrApiAccess::$user) < 0) {
2310 throw new RestException(500,
'Error creating SocieteAccount entity.');
2314 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2315 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2316 $result = $this->db->query($sql);
2318 if ($result && $this->db->num_rows($result) !== 0) {
2319 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.");
2323 $obj = $this->db->fetch_object($result);
2326 $account->id = $obj->rowid;
2327 $account->fk_soc =
$id;
2328 $account->site = $site;
2329 if (!isset($request_data[
'login'])) {
2330 $account->login =
"";
2332 $account->fk_user_creat = $obj->fk_user_creat;
2333 $account->date_creation = $obj->date_creation;
2335 foreach ($request_data as $field => $value) {
2336 if ($field ===
'caller') {
2338 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2345 if ($account->update(DolibarrApiAccess::$user) < 0) {
2346 throw new RestException(500,
'Error updating SocieteAccount entity.');
2377 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2378 throw new RestException(403);
2381 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2382 $result = $this->db->query($sql);
2384 if ($result && $this->db->num_rows($result) == 0) {
2385 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
2388 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2389 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2390 $result = $this->db->query($sql);
2392 if ($result && $this->db->num_rows($result) !== 0) {
2393 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.");
2397 $obj = $this->db->fetch_object($result);
2399 $account->fetch($obj->rowid);
2401 foreach ($request_data as $field => $value) {
2402 if ($field ===
'caller') {
2404 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2411 if ($account->update(DolibarrApiAccess::$user) < 0) {
2412 throw new RestException(500,
'Error updating SocieteAccount account');
2441 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2442 throw new RestException(403);
2445 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
2446 $result = $this->db->query($sql);
2448 if ($result && $this->db->num_rows($result) == 0) {
2449 throw new RestException(404);
2451 $obj = $this->db->fetch_object($result);
2453 $account->fetch($obj->rowid);
2455 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2456 throw new RestException(500,
"Error while deleting $site account attached to this third party");
2478 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2479 throw new RestException(403);
2486 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2487 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id);
2489 $result = $this->db->query($sql);
2491 if ($result && $this->db->num_rows($result) == 0) {
2492 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
2497 $num = $this->db->num_rows($result);
2501 $obj = $this->db->fetch_object($result);
2503 $account->fetch($obj->rowid);
2505 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2506 throw new RestException(500,
'Error while deleting account attached to this third party');
2532 unset(
$object->departement_code);
2541 unset(
$object->commercial_id);
2545 unset(
$object->total_localtax1);
2546 unset(
$object->total_localtax2);
2552 unset(
$object->fk_delivery_address);
2567 if ($data ===
null) {
2570 $thirdparty = array();
2571 foreach (Thirdparties::$FIELDS as $field) {
2572 if (!isset($data[$field])) {
2573 throw new RestException(400,
"$field field missing");
2575 $thirdparty[$field] = $data[$field];
2603 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2605 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2606 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2610 $result = $this->company->initAsSpecimen();
2612 $result = $this->company->fetch((
int) $rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2615 throw new RestException(404,
'Thirdparty not found');
2619 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2622 $this->company->getNoEmail();
2626 $filterabsolutediscount =
"fk_facture_source IS NULL";
2627 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2629 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2630 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2633 $absolute_discount = $this->company->getAvailableDiscounts(
null, $filterabsolutediscount);
2634 $absolute_creditnote = $this->company->getAvailableDiscounts(
null, $filtercreditnote);
2635 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2636 $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.
_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.
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.
dolDecrypt($chain, $key='')
Decode a string with a symmetric encryption.