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);
151 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
155 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
156 throw new RestException(403);
160 $socids = DolibarrApiAccess::$user->socid ? (string) DolibarrApiAccess::$user->socid :
'';
164 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
165 $search_sale = DolibarrApiAccess::$user->id;
168 $sql =
"SELECT t.rowid";
169 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
170 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
173 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
175 if (!in_array($mode, array(1, 2, 3))) {
176 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
179 $sql .=
", ".MAIN_DB_PREFIX.
"c_stcomm as st";
180 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
181 $sql .=
" AND t.fk_stcomm = st.id";
183 $sql .=
" AND t.client IN (1, 3)";
184 } elseif ($mode == 2) {
185 $sql .=
" AND t.client IN (2, 3)";
186 } elseif ($mode == 3) {
187 $sql .=
" AND t.client IN (0)";
188 } elseif ($mode == 4) {
189 $sql .=
" AND t.fournisseur IN (1)";
193 if (!empty($mode) && $mode != 4) {
194 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
195 } elseif (!empty($mode) && $mode == 4) {
196 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
198 $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))";
202 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
205 if ($search_sale && $search_sale !=
'-1') {
206 if ($search_sale == -2) {
207 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.rowid)";
208 } elseif ($search_sale > 0) {
209 $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).
")";
217 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
222 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
224 $sql .= $this->db->order($sortfield, $sortorder);
229 $offset = $limit * $page;
231 $sql .= $this->db->plimit($limit + 1, $offset);
234 $result = $this->db->query($sql);
236 $num = $this->db->num_rows($result);
237 $min = min($num, ($limit <= 0 ? $num : $limit));
240 $obj = $this->db->fetch_object($result);
241 $soc_static =
new Societe($this->db);
242 if ($soc_static->fetch($obj->rowid)) {
244 $soc_static->getNoEmail();
251 throw new RestException(503,
'Error when retrieve thirdparties : '.$this->db->lasterror());
253 if (!count($obj_ret)) {
257 $message =
'No third parties found';
260 $message =
'No customers found';
263 $message =
'No prospects found';
266 $message =
'No other third parties found';
269 $message =
'No suppliers found';
271 throw new RestException(404, $message);
275 if ($pagination_data) {
276 $totalsResult = $this->db->query($sqlTotals);
277 $total = $this->db->fetch_object($totalsResult)->total;
282 $obj_ret[
'data'] = $tmp;
283 $obj_ret[
'pagination'] = [
284 'total' => (int) $total,
286 'page_count' => ceil((
int) $total / $limit),
306 public function post($request_data =
null)
308 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
309 throw new RestException(403);
313 if (!isset($request_data[
'country_id']) && isset($request_data[
'country_code'])) {
314 $field = strlen($request_data[
'country_code']) > 2 ?
'code_iso' :
'code';
315 $id =
dol_getIdFromCode($this->db, $request_data[
'country_code'],
"c_country", $field,
"rowid");
317 throw new RestException(404,
'Country code not found in database: ' . $this->db->error);
319 $request_data[
'country_id'] =
$id;
323 $result = $this->
_validate($request_data);
325 foreach ($request_data as $field => $value) {
326 if ($field ===
'caller') {
328 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
331 if ($field ==
'array_options' && is_array($value)) {
332 foreach ($value as $index => $val) {
333 $this->company->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->company);
338 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
341 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
342 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
344 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
345 $this->company->setNoEmail($this->company->no_email);
348 return $this->company->id;
368 public function put(
$id, $request_data =
null)
370 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
371 throw new RestException(403);
374 $result = $this->company->fetch(
$id);
376 throw new RestException(404,
'Thirdparty not found');
380 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
383 foreach ($request_data as $field => $value) {
384 if ($field ==
'id') {
387 if ($field ===
'caller') {
389 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
392 if ($field ==
'array_options' && is_array($value)) {
393 foreach ($value as $index => $val) {
394 $this->company->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->company);
398 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
401 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
402 $this->company->setNoEmail($this->company->no_email);
405 if ($this->company->update(
$id, DolibarrApiAccess::$user, 1, 1, 1,
'update', 1) > 0) {
406 return $this->
get(
$id);
408 throw new RestException(500, $this->company->error);
435 if (
$id == $idtodelete) {
436 throw new RestException(400,
'Try to merge a thirdparty into itself');
439 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
440 throw new RestException(403);
443 $result = $this->company->fetch(
$id);
445 throw new RestException(404,
'Thirdparty not found');
449 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
452 $companytoremove =
new Societe($this->db);
453 $result = $companytoremove->fetch($idtodelete);
455 throw new RestException(404,
'Thirdparty not found');
459 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
462 $user = DolibarrApiAccess::$user;
463 $result = $this->company->mergeCompany($companytoremove->id);
465 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.
$id.
'. Enable and read log file for more information.');
468 return $this->
get(
$id);
483 public function delete(
$id)
485 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
486 throw new RestException(403);
488 $result = $this->company->fetch(
$id);
490 throw new RestException(404,
'Thirdparty not found');
493 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 $this->company->oldcopy = clone $this->company;
497 $res = $this->company->delete(
$id);
499 throw new RestException(500,
"Can't delete, error occurs");
500 } elseif ($res == 0) {
501 throw new RestException(409,
"Can't delete, that product is probably used");
507 'message' =>
'Object deleted'
534 throw new RestException(501,
'Module "Thirdparties" needed for this request');
538 throw new RestException(501,
'Module "Products" needed for this request');
542 throw new RestException(501,
'Multiprices features activation needed for this request');
545 if ($priceLevel < 1 || $priceLevel >
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT')) {
546 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
549 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
550 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
553 $result = $this->company->fetch(
$id);
555 throw new RestException(404,
'Thirdparty '.
$id.
' not found');
558 if (empty($result)) {
559 throw new RestException(500,
'Error fetching thirdparty '.
$id, array_merge(array($this->company->error), $this->company->errors));
563 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
566 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
568 throw new RestException(500,
'Error setting new price level for thirdparty '.
$id, array($this->company->db->lasterror()));
590 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
591 throw new RestException(403);
593 $result = $this->company->fetch(
$id);
595 throw new RestException(404,
'Thirdparty not found');
597 $usertmp =
new User($this->db);
598 $result = $usertmp->fetch($representative_id);
600 throw new RestException(404,
'User not found');
603 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
605 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
626 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
627 throw new RestException(403);
629 $result = $this->company->fetch(
$id);
631 throw new RestException(404,
'Thirdparty not found');
633 $usertmp =
new User($this->db);
634 $result = $usertmp->fetch($representative_id);
636 throw new RestException(404,
'User not found');
639 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
641 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
664 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
666 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
667 throw new RestException(403);
670 $result = $this->company->fetch(
$id);
672 throw new RestException(404,
'Thirdparty not found');
677 $arrayofcateg = $categories->getListForItem(
$id,
'customer', $sortfield, $sortorder, $limit, $page);
679 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
680 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
683 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
687 return $arrayofcateg;
708 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
709 throw new RestException(403);
712 $result = $this->company->fetch(
$id);
714 throw new RestException(404,
'Thirdparty not found');
717 $result = $category->fetch($category_id);
719 throw new RestException(404,
'category not found');
723 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
726 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
729 $category->add_type($this->company,
'customer');
752 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
753 throw new RestException(403);
756 $result = $this->company->fetch(
$id);
758 throw new RestException(404,
'Thirdparty not found');
761 $result = $category->fetch($category_id);
763 throw new RestException(404,
'category not found');
767 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
770 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
773 $category->del_type($this->company,
'customer');
799 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
800 throw new RestException(403);
803 $result = $this->company->fetch(
$id);
805 throw new RestException(404,
'Thirdparty not found');
810 $result = $categories->getListForItem(
$id,
'supplier', $sortfield, $sortorder, $limit, $page);
812 if (is_numeric($result) && $result < 0) {
813 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
816 if (is_numeric($result) && $result == 0) {
841 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
842 throw new RestException(403);
845 $result = $this->company->fetch(
$id);
847 throw new RestException(404,
'Thirdparty not found');
850 $result = $category->fetch($category_id);
852 throw new RestException(404,
'category not found');
856 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
859 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
862 $category->add_type($this->company,
'supplier');
885 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
886 throw new RestException(403);
889 $result = $this->company->fetch(
$id);
891 throw new RestException(404,
'Thirdparty not found');
894 $result = $category->fetch($category_id);
896 throw new RestException(404,
'category not found');
900 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
903 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
906 $category->del_type($this->company,
'supplier');
932 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
933 throw new RestException(403);
937 throw new RestException(400,
'Thirdparty ID is mandatory');
941 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
944 $result = $this->company->fetch(
$id);
946 throw new RestException(404,
'Thirdparty not found');
949 $result = $this->company->getOutstandingProposals($mode);
951 unset($result[
'total_ht']);
952 unset($result[
'total_ttc']);
978 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
979 throw new RestException(403);
983 throw new RestException(400,
'Thirdparty ID is mandatory');
987 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
990 $result = $this->company->fetch(
$id);
992 throw new RestException(404,
'Thirdparty not found');
995 $result = $this->company->getOutstandingOrders($mode);
997 unset($result[
'total_ht']);
998 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->getOutstandingBills($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);
1081 if (!is_array($result)) {
1082 throw new RestException(404,
'Thirdparty not found');
1085 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1118 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1119 throw new RestException(403);
1123 throw new RestException(400,
'Thirdparty ID is mandatory');
1127 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1130 $result = $this->company->fetch(
$id);
1132 throw new RestException(404,
'Thirdparty not found');
1136 if ($mode ===
'customer') {
1137 $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";
1138 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1139 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as f ON f.rowid = re.fk_facture_source";
1140 $sql .=
" WHERE re.fk_soc = ".((int)
$id);
1141 if ($filter ==
"available") {
1142 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1144 if ($filter ==
"used") {
1145 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1147 } elseif ($mode ===
'supplier') {
1148 $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";
1149 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1150 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_fourn as f ON f.rowid = re.fk_invoice_supplier_source";
1151 $sql .=
" WHERE f.rowid = re.fk_invoice_supplier_source AND re.fk_soc = ".((int)
$id);
1152 if ($filter ==
"available") {
1153 $sql .=
" AND re.fk_invoice_supplier IS NULL AND re.fk_invoice_supplier_line IS NULL";
1155 if ($filter ==
"used") {
1156 $sql .=
" AND (re.fk_invoice_supplier IS NOT NULL OR re.fk_invoice_supplier_line IS NOT NULL)";
1160 $sql .= $this->db->order($sortfield, $sortorder);
1162 $result = $this->db->query($sql);
1164 throw new RestException(503, $this->db->lasterror());
1167 while ($obj = $this->db->fetch_object($result)) {
1202 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer') || !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1203 throw new RestException(403);
1207 throw new RestException(400,
'Thirdparty ID is mandatory');
1209 if (empty($discountid)) {
1210 throw new RestException(400,
'Discount ID is mandatory');
1212 if (empty($amount_ttc_1) || empty($amount_ttc_2)) {
1213 throw new RestException(400,
'Amount are mandatory');
1217 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1220 $result = $this->company->fetch(
$id);
1222 throw new RestException(404,
'Thirdparty not found');
1224 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1226 $res = $discount->fetch($discountid);
1228 throw new RestException(404,
'Discount not found');
1230 if ($discount->socid !=
$id) {
1231 throw new RestException(405,
'Discount not owned by this thirdpartie');
1234 if (
price2num((
float) $amount_ttc_1 + (
float) $amount_ttc_2) != $discount->amount_ttc) {
1235 throw new RestException(405,
'Sum of the 2 discounts is different that the original discount');
1237 if ($discount->fk_facture_line) {
1238 throw new RestException(409,
'Discount is already used');
1244 $newdiscount1->fk_facture_source = $discount->fk_facture_source;
1245 $newdiscount2->fk_facture_source = $discount->fk_facture_source;
1246 $newdiscount1->fk_facture = $discount->fk_facture;
1247 $newdiscount2->fk_facture = $discount->fk_facture;
1248 $newdiscount1->fk_facture_line = $discount->fk_facture_line;
1249 $newdiscount2->fk_facture_line = $discount->fk_facture_line;
1250 $newdiscount1->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1251 $newdiscount2->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1252 $newdiscount1->fk_invoice_supplier = $discount->fk_invoice_supplier;
1253 $newdiscount2->fk_invoice_supplier = $discount->fk_invoice_supplier;
1254 $newdiscount1->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1255 $newdiscount2->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1256 if ($discount->description ==
'(CREDIT_NOTE)' || $discount->description ==
'(DEPOSIT)') {
1257 $newdiscount1->description = $discount->description;
1258 $newdiscount2->description = $discount->description;
1260 $newdiscount1->description = $discount->description.
' (1)';
1261 $newdiscount2->description = $discount->description.
' (2)';
1264 $newdiscount1->fk_user = $discount->fk_user;
1265 $newdiscount2->fk_user = $discount->fk_user;
1266 $newdiscount1->fk_soc = $discount->fk_soc;
1267 $newdiscount1->socid = $discount->socid;
1268 $newdiscount2->fk_soc = $discount->fk_soc;
1269 $newdiscount2->socid = $discount->socid;
1270 $newdiscount1->discount_type = $discount->discount_type;
1271 $newdiscount2->discount_type = $discount->discount_type;
1272 $newdiscount1->datec = $discount->datec;
1273 $newdiscount2->datec = $discount->datec;
1274 $newdiscount1->tva_tx = $discount->tva_tx;
1275 $newdiscount2->tva_tx = $discount->tva_tx;
1276 $newdiscount1->vat_src_code = $discount->vat_src_code;
1277 $newdiscount2->vat_src_code = $discount->vat_src_code;
1278 $newdiscount1->amount_ttc = $amount_ttc_1;
1279 $newdiscount2->amount_ttc =
price2num($discount->amount_ttc - $newdiscount1->amount_ttc);
1280 $newdiscount1->amount_ht =
price2num($newdiscount1->amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1281 $newdiscount2->amount_ht =
price2num($newdiscount2->amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1282 $newdiscount1->amount_tva =
price2num($newdiscount1->amount_ttc - $newdiscount1->amount_ht);
1283 $newdiscount2->amount_tva =
price2num($newdiscount2->amount_ttc - $newdiscount2->amount_ht);
1285 $newdiscount1->multicurrency_amount_ttc = (float) $amount_ttc_1 * ($discount->multicurrency_amount_ttc / $discount->amount_ttc);
1286 $newdiscount2->multicurrency_amount_ttc =
price2num($discount->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ttc);
1287 $newdiscount1->multicurrency_amount_ht =
price2num($newdiscount1->multicurrency_amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1288 $newdiscount2->multicurrency_amount_ht =
price2num($newdiscount2->multicurrency_amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1289 $newdiscount1->multicurrency_amount_tva =
price2num($newdiscount1->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ht);
1290 $newdiscount2->multicurrency_amount_tva =
price2num($newdiscount2->multicurrency_amount_ttc - $newdiscount2->multicurrency_amount_ht);
1294 $newdiscount1->total_ttc = (float)
price2num($newdiscount1->amount_ttc);
1295 $newdiscount1->total_ht = (float)
price2num($newdiscount1->amount_ht);
1296 $newdiscount1->total_tva = (float)
price2num($newdiscount1->amount_tva);
1297 $newdiscount2->total_ttc = (float)
price2num($newdiscount2->amount_ttc);
1298 $newdiscount2->total_ht = (float)
price2num($newdiscount2->amount_ht);
1299 $newdiscount2->total_tva = (float)
price2num($newdiscount2->amount_tva);
1300 $newdiscount1->multicurrency_total_ttc = (float)
price2num($newdiscount1->multicurrency_amount_ttc);
1301 $newdiscount1->multicurrency_total_ht = (float)
price2num($newdiscount1->multicurrency_amount_ht);
1302 $newdiscount1->multicurrency_total_tva = (float)
price2num($newdiscount1->multicurrency_amount_tva);
1303 $newdiscount2->multicurrency_total_ttc = (float)
price2num($newdiscount2->multicurrency_amount_ttc);
1304 $newdiscount2->multicurrency_total_ht = (float)
price2num($newdiscount2->multicurrency_amount_ht);
1305 $newdiscount2->multicurrency_total_tva = (float)
price2num($newdiscount2->multicurrency_amount_tva);
1309 $discount->fk_facture_source = 0;
1311 $discount->fk_invoice_supplier_source = 0;
1312 $res = $discount->delete(DolibarrApiAccess::$user);
1313 $newid1 = $newdiscount1->create(DolibarrApiAccess::$user);
1314 $newid2 = $newdiscount2->create(DolibarrApiAccess::$user);
1315 if ($res <= 0 || $newid1 <= 0 || $newid2 <= 0) {
1316 $this->db->rollback();
1317 throw new RestException(500,
'Operation fail');
1320 $this->db->commit();
1322 $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";
1323 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1324 $sql .=
" WHERE re.rowid IN ( $newid1, $newid2 ) AND f.rowid = re.fk_facture_source AND re.fk_soc = ".((int)
$id);
1326 $sql .= $this->db->order(
"f.type",
"ASC");
1328 $result = $this->db->query($sql);
1330 throw new RestException(503, $this->db->lasterror());
1333 while ($obj = $this->db->fetch_object($result)) {
1361 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1362 throw new RestException(403);
1365 throw new RestException(400,
'Thirdparty ID is mandatory');
1369 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1377 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1378 $invoice =
new Facture($this->db);
1379 $result = $invoice->list_replacable_invoices(
$id);
1381 throw new RestException(405, $invoice->error);
1410 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1411 throw new RestException(403);
1414 throw new RestException(400,
'Thirdparty ID is mandatory');
1418 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1426 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1427 $invoice =
new Facture($this->db);
1428 $result = $invoice->list_qualified_avoir_invoices(
$id);
1429 if (!is_array($result) && $result < 0) {
1430 throw new RestException(405, $invoice->error);
1454 throw new RestException(400,
'Thirdparty ID is mandatory');
1456 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1457 throw new RestException(403);
1460 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1467 $sql =
"SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1468 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1470 $sql .=
" WHERE fk_soc = ".((int)
$id);
1473 $result = $this->db->query($sql);
1474 if ($this->db->num_rows($result) == 0) {
1475 throw new RestException(404,
'Notification not found');
1480 $notifications = array();
1484 $num = $this->db->num_rows($result);
1488 $obj = $this->db->fetch_object($result);
1489 $notifications[] = $obj;
1493 throw new RestException(404,
'No notifications found');
1496 $fields = array(
'id',
'socid',
'event',
'contact_id',
'datec',
'tms',
'type');
1498 $returnNotifications = array();
1500 foreach ($notifications as $notification) {
1502 foreach ($notification as $key => $value) {
1503 if (in_array($key, $fields)) {
1507 $returnNotifications[] =
$object;
1511 return $returnNotifications;
1532 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1533 throw new RestException(403,
"User has no right to update thirdparties");
1535 if ($this->company->fetch(
$id) <= 0) {
1536 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1538 $notification =
new Notify($this->db);
1540 $notification->socid =
$id;
1542 foreach ($request_data as $field => $value) {
1543 $notification->$field = $value;
1546 $event = $notification->event;
1548 throw new RestException(500,
'Error creating Thirdparty Notification, request_data missing event');
1550 $socid = $notification->socid;
1551 $contact_id = $notification->contact_id;
1553 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1554 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1555 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1556 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1557 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1559 $exists_result = $this->db->query($exists_sql);
1560 if ($this->db->num_rows($exists_result) > 0) {
1561 throw new RestException(403,
'Notification already exists');
1564 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1565 throw new RestException(500,
'Error creating Thirdparty Notification');
1568 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1569 throw new RestException(500,
'Error updating values');
1595 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1596 throw new RestException(403,
"User has no right to update thirdparties");
1598 if ($this->company->fetch(
$id) <= 0) {
1599 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1601 $notification =
new Notify($this->db);
1602 $notification->socid =
$id;
1604 $sql =
"SELECT t.rowid as id FROM ".MAIN_DB_PREFIX.
"c_action_trigger as t";
1605 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
1607 $result = $this->db->query($sql);
1608 if ($this->db->num_rows($result) == 0) {
1609 throw new RestException(404,
'Action Trigger code not found');
1612 $notification->event = $this->db->fetch_row($result)[0];
1613 foreach ($request_data as $field => $value) {
1614 if ($field ===
'event') {
1615 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains event key');
1617 if ($field ===
'fk_action') {
1618 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains fk_action key');
1620 $notification->$field = $value;
1623 $event = $notification->event;
1624 $socid = $notification->socid;
1625 $contact_id = $notification->contact_id;
1627 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1628 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1629 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1630 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1631 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1633 $exists_result = $this->db->query($exists_sql);
1634 if ($this->db->num_rows($exists_result) > 0) {
1635 throw new RestException(403,
'Notification already exists');
1638 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1639 throw new RestException(500,
'Error creating Thirdparty Notification, are request_data well formed?');
1642 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1643 throw new RestException(500,
'Error updating values');
1665 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1666 throw new RestException(403);
1669 $notification =
new Notify($this->db);
1671 $notification->fetch($notification_id);
1673 $socid = (int) $notification->socid;
1675 if ($socid ==
$id) {
1676 return $notification->delete(DolibarrApiAccess::$user);
1678 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1701 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1702 throw new RestException(403,
"User has no right to update thirdparties");
1704 if ($this->company->fetch(
$id) <= 0) {
1705 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1707 $notification =
new Notify($this->db);
1710 $notification->fetch($notification_id,
$id);
1712 if ($notification->socid !=
$id) {
1713 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1716 foreach ($request_data as $field => $value) {
1717 $notification->$field = $value;
1720 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1721 throw new RestException(500,
'Error updating values');
1744 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1745 throw new RestException(403);
1748 throw new RestException(400,
'Thirdparty ID is mandatory');
1752 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1759 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1760 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1761 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1763 $sql .=
" WHERE fk_soc = ".((int)
$id);
1766 $result = $this->db->query($sql);
1768 if ($this->db->num_rows($result) == 0) {
1769 throw new RestException(404,
'Account not found');
1774 $accounts = array();
1778 $num = $this->db->num_rows($result);
1782 $obj = $this->db->fetch_object($result);
1785 if ($account->fetch($obj->rowid)) {
1786 $accounts[] = $account;
1791 throw new RestException(404,
'Account not found');
1795 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1797 $returnAccounts = array();
1799 foreach ($accounts as $account) {
1801 foreach ($account as $key => $value) {
1802 if (in_array($key, $fields)) {
1803 if ($key ==
'iban') {
1813 return $returnAccounts;
1834 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1835 throw new RestException(403);
1837 if ($this->company->fetch(
$id) <= 0) {
1838 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1842 $account->socid =
$id;
1844 foreach ($request_data as $field => $value) {
1845 if ($field ===
'caller') {
1847 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1851 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
1854 if ($account->create(DolibarrApiAccess::$user) < 0) {
1855 throw new RestException(500,
'Error creating Company Bank account');
1858 if (empty($account->rum)) {
1859 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1861 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, (
string) $account->id);
1862 $account->date_rum =
dol_now();
1865 if ($account->update(DolibarrApiAccess::$user) < 0) {
1866 throw new RestException(500,
'Error updating values');
1891 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1892 throw new RestException(403);
1894 if ($this->company->fetch(
$id) <= 0) {
1895 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1900 $account->fetch($bankaccount_id,
'',
$id, -1,
'');
1902 if ($account->socid !=
$id) {
1903 throw new RestException(403);
1907 foreach ($request_data as $field => $value) {
1908 if ($field ===
'caller') {
1910 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1917 if (empty($account->rum)) {
1918 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1920 $account->rum = $prelevement->buildRumNumber($this->company->code_client, $account->datec, (
string) $account->id);
1921 $account->date_rum =
dol_now();
1924 if ($account->update(DolibarrApiAccess::$user) < 0) {
1925 throw new RestException(500,
'Error updating values');
1947 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1948 throw new RestException(403);
1953 $account->fetch($bankaccount_id);
1955 $socid = (int) $account->socid;
1957 if ($socid ==
$id) {
1958 return $account->delete(DolibarrApiAccess::$user);
1960 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1984 global $conf, $langs;
1986 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
1988 if ($this->company->fetch(
$id) <= 0) {
1989 throw new RestException(404,
'Thirdparty not found');
1992 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1993 throw new RestException(403);
1996 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
1998 $this->company->fk_bank = $this->company->fk_account;
2001 $outputlangs = $langs;
2006 if (isset($this->company->thirdparty->default_lang)) {
2007 $newlang = $this->company->thirdparty->default_lang;
2008 } elseif (isset($this->company->default_lang)) {
2009 $newlang = $this->company->default_lang;
2012 if (!empty($newlang)) {
2013 $outputlangs =
new Translate(
"", $conf);
2014 $outputlangs->setDefaultLang($newlang);
2017 $sql =
"SELECT rowid";
2018 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
2020 $sql .=
" WHERE fk_soc = ".((int)
$id);
2022 if ($companybankid) {
2023 $sql .=
" AND rowid = ".((int) $companybankid);
2027 $accounts = array();
2029 $result = $this->db->query($sql);
2031 if ($this->db->num_rows($result) == 0) {
2032 throw new RestException(404,
'Bank account not found');
2035 $num = $this->db->num_rows($result);
2039 $obj = $this->db->fetch_object($result);
2042 if ($account->fetch($obj->rowid)) {
2043 $accounts[] = $account;
2048 throw new RestException(500,
'Sql error '.$this->db->lasterror());
2051 $moreparams = array(
2052 'use_companybankid' => $accounts[0]->
id,
2053 'force_dir_output' => $conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName((
string) $this->company->id)
2056 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
2059 return array(
"success" => $result);
2061 throw new RestException(500,
'Error generating the document '.$this->company->error);
2084 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2085 throw new RestException(403);
2089 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2095 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2096 $sql .=
" WHERE fk_soc = ".((int)
$id);
2098 $sql .=
" AND site ='".$this->db->escape($site).
"'";
2101 $result = $this->db->query($sql);
2103 if ($result && $this->db->num_rows($result) == 0) {
2104 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
2109 $accounts = array();
2112 $num = $this->db->num_rows($result);
2116 $obj = $this->db->fetch_object($result);
2119 if ($account->fetch($obj->rowid)) {
2120 $accounts[] = $account;
2125 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
2127 $returnAccounts = array();
2129 foreach ($accounts as $account) {
2131 foreach ($account as $key => $value) {
2132 if (in_array($key, $fields)) {
2139 return $returnAccounts;
2161 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2162 throw new RestException(403);
2165 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2166 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
2167 $sql .=
" AND entity IN (".getEntity(
'societe').
")";
2169 $result = $this->db->query($sql);
2171 if ($result && $this->db->num_rows($result) == 1) {
2172 $obj = $this->db->fetch_object($result);
2173 $returnThirdparty = $this->
_fetch($obj->fk_soc);
2175 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
2179 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2182 return $returnThirdparty;
2210 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2211 throw new RestException(403);
2214 if (!isset($request_data[
'site'])) {
2215 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
2218 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
2219 $result = $this->db->query($sql);
2221 if ($result && $this->db->num_rows($result) == 0) {
2223 if (!isset($request_data[
'login'])) {
2224 $account->login =
"";
2226 $account->fk_soc =
$id;
2228 foreach ($request_data as $field => $value) {
2229 if ($field ===
'caller') {
2231 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2238 if ($account->create(DolibarrApiAccess::$user) < 0) {
2239 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
2246 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
2278 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2279 throw new RestException(403);
2282 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
2283 $result = $this->db->query($sql);
2286 if ($result && $this->db->num_rows($result) == 0) {
2287 if (!isset($request_data[
'key_account'])) {
2288 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
2291 if (!isset($request_data[
'login'])) {
2292 $account->login =
"";
2295 foreach ($request_data as $field => $value) {
2296 if ($field ===
'caller') {
2298 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2305 $account->fk_soc =
$id;
2306 $account->site = $site;
2308 if ($account->create(DolibarrApiAccess::$user) < 0) {
2309 throw new RestException(500,
'Error creating SocieteAccount entity.');
2313 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2314 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2315 $result = $this->db->query($sql);
2317 if ($result && $this->db->num_rows($result) !== 0) {
2318 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.");
2322 $obj = $this->db->fetch_object($result);
2325 $account->id = $obj->rowid;
2326 $account->fk_soc =
$id;
2327 $account->site = $site;
2328 if (!isset($request_data[
'login'])) {
2329 $account->login =
"";
2331 $account->fk_user_creat = $obj->fk_user_creat;
2332 $account->date_creation = $obj->date_creation;
2334 foreach ($request_data as $field => $value) {
2335 if ($field ===
'caller') {
2337 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2344 if ($account->update(DolibarrApiAccess::$user) < 0) {
2345 throw new RestException(500,
'Error updating SocieteAccount entity.');
2376 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2377 throw new RestException(403);
2380 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2381 $result = $this->db->query($sql);
2383 if ($result && $this->db->num_rows($result) == 0) {
2384 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
2387 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2388 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2389 $result = $this->db->query($sql);
2391 if ($result && $this->db->num_rows($result) !== 0) {
2392 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.");
2396 $obj = $this->db->fetch_object($result);
2398 $account->fetch($obj->rowid);
2400 foreach ($request_data as $field => $value) {
2401 if ($field ===
'caller') {
2403 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2410 if ($account->update(DolibarrApiAccess::$user) < 0) {
2411 throw new RestException(500,
'Error updating SocieteAccount account');
2440 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2441 throw new RestException(403);
2444 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
2445 $result = $this->db->query($sql);
2447 if ($result && $this->db->num_rows($result) == 0) {
2448 throw new RestException(404);
2450 $obj = $this->db->fetch_object($result);
2452 $account->fetch($obj->rowid);
2454 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2455 throw new RestException(500,
"Error while deleting $site account attached to this third party");
2477 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2478 throw new RestException(403);
2485 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2486 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id);
2488 $result = $this->db->query($sql);
2490 if ($result && $this->db->num_rows($result) == 0) {
2491 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
2496 $num = $this->db->num_rows($result);
2500 $obj = $this->db->fetch_object($result);
2502 $account->fetch($obj->rowid);
2504 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2505 throw new RestException(500,
'Error while deleting account attached to this third party');
2531 unset(
$object->departement_code);
2540 unset(
$object->commercial_id);
2544 unset(
$object->total_localtax1);
2545 unset(
$object->total_localtax2);
2551 unset(
$object->fk_delivery_address);
2566 if ($data ===
null) {
2569 $thirdparty = array();
2570 foreach (Thirdparties::$FIELDS as $field) {
2571 if (!isset($data[$field])) {
2572 throw new RestException(400,
"$field field missing");
2574 $thirdparty[$field] = $data[$field];
2602 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2604 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2605 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2609 $result = $this->company->initAsSpecimen();
2611 $result = $this->company->fetch((
int) $rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2614 throw new RestException(404,
'Thirdparty not found');
2618 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2621 $this->company->getNoEmail();
2625 $filterabsolutediscount =
"fk_facture_source IS NULL";
2626 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2628 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2629 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2632 $absolute_discount = $this->company->getAvailableDiscounts(
null, $filterabsolutediscount);
2633 $absolute_creditnote = $this->company->getAvailableDiscounts(
null, $filtercreditnote);
2634 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2635 $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.