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);
156 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
160 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
161 throw new RestException(403);
165 $socids = DolibarrApiAccess::$user->socid ? (
string) DolibarrApiAccess::$user->socid :
'';
169 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
170 $search_sale = DolibarrApiAccess::$user->id;
173 $sql =
"SELECT t.rowid";
174 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
175 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
178 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
180 if (!in_array($mode, array(1, 2, 3))) {
181 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
184 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
186 $sql .=
" AND t.client IN (1, 3)";
187 } elseif ($mode == 2) {
188 $sql .=
" AND t.client IN (2, 3)";
189 } elseif ($mode == 3) {
190 $sql .=
" AND t.client IN (0)";
191 } elseif ($mode == 4) {
192 $sql .=
" AND t.fournisseur IN (1)";
196 if (!empty($mode) && $mode != 4) {
197 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
198 } elseif (!empty($mode) && $mode == 4) {
199 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
201 $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))";
205 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
208 if ($search_sale && $search_sale !=
'-1') {
209 if ($search_sale == -2) {
210 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.rowid)";
211 } elseif ($search_sale > 0) {
212 $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).
")";
220 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
225 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
227 $sql .= $this->db->order($sortfield, $sortorder);
232 $offset = $limit * $page;
234 $sql .= $this->db->plimit($limit + 1, $offset);
237 $result = $this->db->query($sql);
239 $num = $this->db->num_rows($result);
240 $min = min($num, ($limit <= 0 ? $num : $limit));
243 $obj = $this->db->fetch_object($result);
244 $soc_static =
new Societe($this->db);
245 if ($soc_static->fetch($obj->rowid)) {
247 $soc_static->getNoEmail();
254 throw new RestException(503,
'Error when retrieve third parties : '.$this->db->lasterror());
256 if (!count($obj_ret)) {
260 $message =
'No third parties found';
263 $message =
'No customers found';
266 $message =
'No prospects found';
269 $message =
'No other third parties found';
272 $message =
'No suppliers found';
274 throw new RestException(404, $message);
278 if ($pagination_data) {
279 $totalsResult = $this->db->query($sqlTotals);
280 $total = $this->db->fetch_object($totalsResult)->total;
285 $obj_ret[
'data'] = $tmp;
286 $obj_ret[
'pagination'] = [
287 'total' => (int) $total,
289 'page_count' => ceil((
int) $total / $limit),
309 public function post($request_data =
null)
311 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
312 throw new RestException(403);
316 if (!isset($request_data[
'country_id']) && isset($request_data[
'country_code'])) {
317 $field = strlen($request_data[
'country_code']) > 2 ?
'code_iso' :
'code';
318 $id =
dol_getIdFromCode($this->db, $request_data[
'country_code'],
"c_country", $field,
"rowid");
320 throw new RestException(404,
'Country code not found in database: ' . $this->db->error);
322 $request_data[
'country_id'] =
$id;
326 $result = $this->
_validate($request_data);
328 foreach ($request_data as $field => $value) {
329 if ($field ===
'caller') {
331 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
334 if ($field ==
'array_options' && is_array($value)) {
335 $this->company->fetch_optionals();
337 foreach ($value as $index => $val) {
343 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
346 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
347 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
349 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
350 $this->company->setNoEmail($this->company->no_email);
353 return $this->company->id;
373 public function put(
$id, $request_data =
null)
375 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
376 throw new RestException(403);
379 $result = $this->company->fetch(
$id);
381 throw new RestException(404,
'Thirdparty not found');
385 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
388 foreach ($request_data as $field => $value) {
389 if ($field ==
'id') {
392 if ($field ===
'caller') {
394 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
397 if ($field ==
'array_options' && is_array($value)) {
398 foreach ($value as $index => $val) {
403 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
406 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
407 $this->company->setNoEmail($this->company->no_email);
410 if ($this->company->update(
$id, DolibarrApiAccess::$user, 1, 1, 1,
'update', 1) > 0) {
411 return $this->
get(
$id);
413 throw new RestException(500, $this->company->error);
440 if (
$id == $idtodelete) {
441 throw new RestException(400,
'Try to merge a thirdparty into itself');
444 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
445 throw new RestException(403);
448 $result = $this->company->fetch(
$id);
450 throw new RestException(404,
'Thirdparty not found');
454 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
457 $companytoremove =
new Societe($this->db);
458 $result = $companytoremove->fetch($idtodelete);
460 throw new RestException(404,
'Thirdparty not found');
464 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
467 $user = DolibarrApiAccess::$user;
468 $result = $this->company->mergeCompany($companytoremove->id);
470 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.
$id.
'. Enable and read log file for more information.');
473 return $this->
get(
$id);
488 public function delete(
$id)
490 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
491 throw new RestException(403);
493 $result = $this->company->fetch(
$id);
495 throw new RestException(404,
'Thirdparty not found');
498 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
500 $this->company->oldcopy = clone $this->company;
502 $res = $this->company->delete(
$id);
504 throw new RestException(500,
"Can't delete, error occurs");
505 } elseif ($res == 0) {
506 throw new RestException(409,
"Can't delete, that product is probably used");
512 'message' =>
'Object deleted'
539 throw new RestException(501,
'Module "Thirdparties" needed for this request');
543 throw new RestException(501,
'Module "Products" needed for this request');
547 throw new RestException(501,
'Multiprices features activation needed for this request');
550 if ($priceLevel < 1 || $priceLevel >
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT')) {
551 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
554 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
555 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
558 $result = $this->company->fetch(
$id);
560 throw new RestException(404,
'Thirdparty '.
$id.
' not found');
563 if (empty($result)) {
564 throw new RestException(500,
'Error fetching thirdparty '.
$id, array_merge(array($this->company->error), $this->company->errors));
568 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
571 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
573 throw new RestException(500,
'Error setting new price level for thirdparty '.
$id, array($this->company->db->lasterror()));
595 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
596 throw new RestException(403);
598 $result = $this->company->fetch(
$id);
600 throw new RestException(404,
'Thirdparty not found');
602 $usertmp =
new User($this->db);
603 $result = $usertmp->fetch($representative_id);
605 throw new RestException(404,
'User not found');
608 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
610 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
631 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
632 throw new RestException(403);
634 $result = $this->company->fetch(
$id);
636 throw new RestException(404,
'Thirdparty not found');
638 $usertmp =
new User($this->db);
639 $result = $usertmp->fetch($representative_id);
641 throw new RestException(404,
'User not found');
644 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
646 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
669 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
671 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
672 throw new RestException(403);
675 $result = $this->company->fetch(
$id);
677 throw new RestException(404,
'Thirdparty not found');
682 throw new RestException(404,
'Third party not allowed for login '.DolibarrApiAccess::$user->login);
687 $arrayofcateg = $categories->getListForItem(
$id,
'customer', $sortfield, $sortorder, $limit, $page);
689 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
690 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
693 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
697 return $arrayofcateg;
718 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
719 throw new RestException(403);
722 $result = $this->company->fetch(
$id);
724 throw new RestException(404,
'Third party not found');
727 $result = $category->fetch($category_id);
729 throw new RestException(404,
'category not found');
733 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
736 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
739 $category->add_type($this->company,
'customer');
762 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
763 throw new RestException(403);
766 $result = $this->company->fetch(
$id);
768 throw new RestException(404,
'Thirdparty not found');
771 $result = $category->fetch($category_id);
773 throw new RestException(404,
'category not found');
777 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
780 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
783 $category->del_type($this->company,
'customer');
809 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
810 throw new RestException(403);
813 $result = $this->company->fetch(
$id);
815 throw new RestException(404,
'Thirdparty not found');
820 throw new RestException(404,
'Third party not allowed for login '.DolibarrApiAccess::$user->login);
825 $result = $categories->getListForItem(
$id,
'supplier', $sortfield, $sortorder, $limit, $page);
827 if (is_numeric($result) && $result < 0) {
828 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
831 if (is_numeric($result) && $result == 0) {
856 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
857 throw new RestException(403);
860 $result = $this->company->fetch(
$id);
862 throw new RestException(404,
'Thirdparty not found');
865 $result = $category->fetch($category_id);
867 throw new RestException(404,
'category not found');
871 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
874 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
877 $category->add_type($this->company,
'supplier');
900 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
901 throw new RestException(403);
904 $result = $this->company->fetch(
$id);
906 throw new RestException(404,
'Thirdparty not found');
909 $result = $category->fetch($category_id);
911 throw new RestException(404,
'category not found');
915 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
918 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
921 $category->del_type($this->company,
'supplier');
947 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
948 throw new RestException(403);
952 throw new RestException(400,
'Thirdparty ID is mandatory');
956 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
959 $result = $this->company->fetch(
$id);
961 throw new RestException(404,
'Thirdparty not found');
964 $result = $this->company->getOutstandingProposals($mode);
966 unset($result[
'total_ht']);
967 unset($result[
'total_ttc']);
993 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
994 throw new RestException(403);
998 throw new RestException(400,
'Thirdparty ID is mandatory');
1002 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1005 $result = $this->company->fetch(
$id);
1007 throw new RestException(404,
'Thirdparty not found');
1010 $result = $this->company->getOutstandingOrders($mode);
1012 unset($result[
'total_ht']);
1013 unset($result[
'total_ttc']);
1038 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1039 throw new RestException(403);
1043 throw new RestException(400,
'Thirdparty ID is mandatory');
1047 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1050 $result = $this->company->fetch(
$id);
1052 throw new RestException(404,
'Thirdparty not found');
1055 $result = $this->company->getOutstandingBills($mode);
1057 unset($result[
'total_ht']);
1058 unset($result[
'total_ttc']);
1083 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1084 throw new RestException(403);
1088 throw new RestException(400,
'Thirdparty ID is mandatory');
1092 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1095 $result = $this->company->fetch(
$id);
1097 throw new RestException(404,
'Thirdparty not found');
1100 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1133 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1134 throw new RestException(403);
1138 throw new RestException(400,
'Thirdparty ID is mandatory');
1142 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1145 $result = $this->company->fetch(
$id);
1147 throw new RestException(404,
'Thirdparty not found');
1151 if ($mode ===
'customer') {
1152 $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";
1153 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1154 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as f ON f.rowid = re.fk_facture_source";
1155 $sql .=
" WHERE re.fk_soc = ".((int)
$id);
1156 if ($filter ==
"available") {
1157 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1159 if ($filter ==
"used") {
1160 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1162 } elseif ($mode ===
'supplier') {
1163 $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";
1164 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1165 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_fourn as f ON f.rowid = re.fk_invoice_supplier_source";
1166 $sql .=
" WHERE f.rowid = re.fk_invoice_supplier_source AND re.fk_soc = ".((int)
$id);
1167 if ($filter ==
"available") {
1168 $sql .=
" AND re.fk_invoice_supplier IS NULL AND re.fk_invoice_supplier_line IS NULL";
1170 if ($filter ==
"used") {
1171 $sql .=
" AND (re.fk_invoice_supplier IS NOT NULL OR re.fk_invoice_supplier_line IS NOT NULL)";
1175 $sql .= $this->db->order($sortfield, $sortorder);
1177 $result = $this->db->query($sql);
1179 throw new RestException(503, $this->db->lasterror());
1182 while ($obj = $this->db->fetch_object($result)) {
1216 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1217 throw new RestException(403);
1222 throw new RestException(400,
'Thirdparty ID is mandatory');
1224 if (!isset($request_data[
'amount'])) {
1225 throw new RestException(400,
'Missing required field: amount');
1227 if (!isset($request_data[
'description'])) {
1228 throw new RestException(400,
'Missing required field: description');
1233 throw new RestException(401,
'Access not allowed for login'.DolibarrApiAccess::$user->login);
1237 if ($this->company->fetch(
$id) <= 0) {
1238 throw new RestException(404,
'Error creating discount, thirdparty not found');
1243 if (!is_numeric($request_data[
'amount']) || $request_data[
'amount'] <= 0) {
1244 throw new RestException(400,
'Invalid amount_ht: must be a positive number');
1246 $amount = (float) $request_data[
'amount'];
1249 if (isset($request_data[
'tva_tx']) && (!is_numeric($request_data[
'tva_tx']) || $request_data[
'tva_tx'] < 0)) {
1250 throw new RestException(400,
'Invalid tva_tx: must be a positive number or zero');
1252 $tva_tx = isset($request_data[
'tva_tx']) ? (float) $request_data[
'tva_tx'] : 0;
1255 $price_base_type =
'HT';
1256 if (isset($request_data[
'price_base_type'])) {
1257 $price_base_type = strtoupper($request_data[
'price_base_type']);
1258 if ($price_base_type !==
'HT' && $price_base_type !==
'TTC') {
1259 throw new RestException(400,
'Invalid price_base_type: must be "HT" or "TTC"');
1265 if (isset($request_data[
'discount_type'])) {
1266 $discount_type = (int) $request_data[
'discount_type'];
1267 if ($discount_type !== 0 && $discount_type !== 1) {
1268 throw new RestException(400,
'Invalid discount_type: must be 0 (customer) or 1 (supplier)');
1273 $description = $request_data[
'description'];
1274 if (empty(trim($description))) {
1275 throw new RestException(400,
'Description cannot be empty');
1280 if (isset($request_data[
'vat_src_code']) && !empty($request_data[
'vat_src_code'])) {
1281 $vatrate = $tva_tx .
' (' . $request_data[
'vat_src_code'] .
')';
1287 $result = $this->company->set_remise_except($amount, DolibarrApiAccess::$user, $description, $vatrate, $discount_type, $price_base_type);
1290 $this->db->commit();
1293 $this->db->rollback();
1294 throw new RestException(500,
'Error creating discount: '.$this->company->error, array_merge(array($this->company->error), $this->company->errors));
1325 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer') || !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1326 throw new RestException(403);
1330 throw new RestException(400,
'Thirdparty ID is mandatory');
1332 if (empty($discountid)) {
1333 throw new RestException(400,
'Discount ID is mandatory');
1335 if (empty($amount_ttc_1) || empty($amount_ttc_2)) {
1336 throw new RestException(400,
'Amount are mandatory');
1340 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1343 $result = $this->company->fetch(
$id);
1345 throw new RestException(404,
'Thirdparty not found');
1347 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1349 $res = $discount->fetch($discountid);
1351 throw new RestException(404,
'Discount not found');
1353 if ($discount->socid !=
$id) {
1354 throw new RestException(405,
'Discount not owned by this thirdpartie');
1357 if (
price2num((
float) $amount_ttc_1 + (
float) $amount_ttc_2) != $discount->amount_ttc) {
1358 throw new RestException(405,
'Sum of the 2 discounts is different that the original discount');
1360 if ($discount->fk_facture_line) {
1361 throw new RestException(409,
'Discount is already used');
1367 $newdiscount1->fk_facture_source = $discount->fk_facture_source;
1368 $newdiscount2->fk_facture_source = $discount->fk_facture_source;
1369 $newdiscount1->fk_facture = $discount->fk_facture;
1370 $newdiscount2->fk_facture = $discount->fk_facture;
1371 $newdiscount1->fk_facture_line = $discount->fk_facture_line;
1372 $newdiscount2->fk_facture_line = $discount->fk_facture_line;
1373 $newdiscount1->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1374 $newdiscount2->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1375 $newdiscount1->fk_invoice_supplier = $discount->fk_invoice_supplier;
1376 $newdiscount2->fk_invoice_supplier = $discount->fk_invoice_supplier;
1377 $newdiscount1->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1378 $newdiscount2->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1379 if ($discount->description ==
'(CREDIT_NOTE)' || $discount->description ==
'(DEPOSIT)') {
1380 $newdiscount1->description = $discount->description;
1381 $newdiscount2->description = $discount->description;
1383 $newdiscount1->description = $discount->description.
' (1)';
1384 $newdiscount2->description = $discount->description.
' (2)';
1387 $newdiscount1->fk_user = $discount->fk_user;
1388 $newdiscount2->fk_user = $discount->fk_user;
1389 $newdiscount1->fk_soc = $discount->fk_soc;
1390 $newdiscount1->socid = $discount->socid;
1391 $newdiscount2->fk_soc = $discount->fk_soc;
1392 $newdiscount2->socid = $discount->socid;
1393 $newdiscount1->discount_type = $discount->discount_type;
1394 $newdiscount2->discount_type = $discount->discount_type;
1395 $newdiscount1->datec = $discount->datec;
1396 $newdiscount2->datec = $discount->datec;
1397 $newdiscount1->tva_tx = $discount->tva_tx;
1398 $newdiscount2->tva_tx = $discount->tva_tx;
1399 $newdiscount1->vat_src_code = $discount->vat_src_code;
1400 $newdiscount2->vat_src_code = $discount->vat_src_code;
1401 $newdiscount1->amount_ttc = $amount_ttc_1;
1402 $newdiscount2->amount_ttc =
price2num($discount->amount_ttc - $newdiscount1->amount_ttc);
1403 $newdiscount1->amount_ht =
price2num($newdiscount1->amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1404 $newdiscount2->amount_ht =
price2num($newdiscount2->amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1405 $newdiscount1->amount_tva =
price2num($newdiscount1->amount_ttc - $newdiscount1->amount_ht);
1406 $newdiscount2->amount_tva =
price2num($newdiscount2->amount_ttc - $newdiscount2->amount_ht);
1408 $newdiscount1->multicurrency_amount_ttc = (float) $amount_ttc_1 * ($discount->multicurrency_amount_ttc / $discount->amount_ttc);
1409 $newdiscount2->multicurrency_amount_ttc =
price2num($discount->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ttc);
1410 $newdiscount1->multicurrency_amount_ht =
price2num($newdiscount1->multicurrency_amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1411 $newdiscount2->multicurrency_amount_ht =
price2num($newdiscount2->multicurrency_amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1412 $newdiscount1->multicurrency_amount_tva =
price2num($newdiscount1->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ht);
1413 $newdiscount2->multicurrency_amount_tva =
price2num($newdiscount2->multicurrency_amount_ttc - $newdiscount2->multicurrency_amount_ht);
1417 $newdiscount1->total_ttc = (float)
price2num($newdiscount1->amount_ttc);
1418 $newdiscount1->total_ht = (float)
price2num($newdiscount1->amount_ht);
1419 $newdiscount1->total_tva = (float)
price2num($newdiscount1->amount_tva);
1420 $newdiscount2->total_ttc = (float)
price2num($newdiscount2->amount_ttc);
1421 $newdiscount2->total_ht = (float)
price2num($newdiscount2->amount_ht);
1422 $newdiscount2->total_tva = (float)
price2num($newdiscount2->amount_tva);
1423 $newdiscount1->multicurrency_total_ttc = (float)
price2num($newdiscount1->multicurrency_amount_ttc);
1424 $newdiscount1->multicurrency_total_ht = (float)
price2num($newdiscount1->multicurrency_amount_ht);
1425 $newdiscount1->multicurrency_total_tva = (float)
price2num($newdiscount1->multicurrency_amount_tva);
1426 $newdiscount2->multicurrency_total_ttc = (float)
price2num($newdiscount2->multicurrency_amount_ttc);
1427 $newdiscount2->multicurrency_total_ht = (float)
price2num($newdiscount2->multicurrency_amount_ht);
1428 $newdiscount2->multicurrency_total_tva = (float)
price2num($newdiscount2->multicurrency_amount_tva);
1432 $discount->fk_facture_source = 0;
1434 $discount->fk_invoice_supplier_source = 0;
1435 $res = $discount->delete(DolibarrApiAccess::$user);
1436 $newid1 = $newdiscount1->create(DolibarrApiAccess::$user);
1437 $newid2 = $newdiscount2->create(DolibarrApiAccess::$user);
1438 if ($res <= 0 || $newid1 <= 0 || $newid2 <= 0) {
1439 $this->db->rollback();
1440 throw new RestException(500,
'Operation fail');
1443 $this->db->commit();
1445 $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";
1446 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1447 $sql .=
" WHERE re.rowid IN (".((int) $newid1).
",".((int) $newid2).
") AND f.rowid = re.fk_facture_source AND re.fk_soc = ".((int)
$id);
1449 $sql .= $this->db->order(
"f.type",
"ASC");
1451 $result = $this->db->query($sql);
1453 throw new RestException(503, $this->db->lasterror());
1456 while ($obj = $this->db->fetch_object($result)) {
1484 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1485 throw new RestException(403);
1488 throw new RestException(400,
'Thirdparty ID is mandatory');
1492 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1500 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1501 $invoice =
new Facture($this->db);
1502 $result = $invoice->list_replacable_invoices(
$id);
1504 throw new RestException(405, $invoice->error);
1533 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1534 throw new RestException(403);
1537 throw new RestException(400,
'Thirdparty ID is mandatory');
1541 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1549 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1550 $invoice =
new Facture($this->db);
1551 $result = $invoice->list_qualified_avoir_invoices(
$id);
1552 if (!is_array($result) && $result < 0) {
1553 throw new RestException(405, $invoice->error);
1577 throw new RestException(400,
'Thirdparty ID is mandatory');
1579 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1580 throw new RestException(403);
1583 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1590 $sql =
"SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1591 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1593 $sql .=
" WHERE fk_soc = ".((int)
$id);
1596 $result = $this->db->query($sql);
1597 if ($this->db->num_rows($result) == 0) {
1598 throw new RestException(404,
'Notification not found');
1603 $notifications = array();
1607 $num = $this->db->num_rows($result);
1611 $obj = $this->db->fetch_object($result);
1612 $notifications[] = $obj;
1616 throw new RestException(404,
'No notifications found');
1619 $fields = array(
'id',
'socid',
'event',
'contact_id',
'datec',
'tms',
'type');
1621 $returnNotifications = array();
1623 foreach ($notifications as $notification) {
1625 foreach ($notification as $key => $value) {
1626 if (in_array($key, $fields)) {
1630 $returnNotifications[] =
$object;
1634 return $returnNotifications;
1655 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1656 throw new RestException(403,
"User has no right to update thirdparties");
1660 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1663 if ($this->company->fetch(
$id) <= 0) {
1664 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1666 $notification =
new Notify($this->db);
1668 $notification->socid =
$id;
1670 foreach ($request_data as $field => $value) {
1671 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1674 $event = $notification->event;
1676 throw new RestException(500,
'Error creating Thirdparty Notification, request_data missing event');
1678 $socid = $notification->socid;
1679 $contact_id = $notification->contact_id;
1681 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1682 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1683 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1684 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1685 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1687 $exists_result = $this->db->query($exists_sql);
1688 if ($this->db->num_rows($exists_result) > 0) {
1689 throw new RestException(403,
'Notification already exists');
1692 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1693 throw new RestException(500,
'Error creating Thirdparty Notification');
1696 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1697 throw new RestException(500,
'Error updating values');
1723 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1724 throw new RestException(403,
"User has no right to update thirdparties");
1728 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1731 if ($this->company->fetch(
$id) <= 0) {
1732 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1734 $notification =
new Notify($this->db);
1735 $notification->socid =
$id;
1737 $sql =
"SELECT t.rowid as id FROM ".MAIN_DB_PREFIX.
"c_action_trigger as t";
1738 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
1740 $result = $this->db->query($sql);
1741 if ($this->db->num_rows($result) == 0) {
1742 throw new RestException(404,
'Action Trigger code not found');
1745 $notification->event = $this->db->fetch_row($result)[0];
1746 foreach ($request_data as $field => $value) {
1747 if ($field ===
'event') {
1748 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains event key');
1750 if ($field ===
'fk_action') {
1751 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains fk_action key');
1753 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1756 $event = $notification->event;
1757 $socid = $notification->socid;
1758 $contact_id = $notification->contact_id;
1760 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1761 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1762 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1763 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1764 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1766 $exists_result = $this->db->query($exists_sql);
1767 if ($this->db->num_rows($exists_result) > 0) {
1768 throw new RestException(403,
'Notification already exists');
1771 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1772 throw new RestException(500,
'Error creating Thirdparty Notification, are request_data well formed?');
1775 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1776 throw new RestException(500,
'Error updating values');
1798 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1799 throw new RestException(403);
1803 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1806 $notification =
new Notify($this->db);
1808 $notification->fetch($notification_id);
1810 $socid = (int) $notification->socid;
1812 if ($socid ==
$id) {
1813 return $notification->delete(DolibarrApiAccess::$user);
1815 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1838 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1839 throw new RestException(403,
"User has no right to update thirdparties");
1843 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1846 if ($this->company->fetch(
$id) <= 0) {
1847 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1849 $notification =
new Notify($this->db);
1852 $notification->fetch($notification_id,
$id);
1854 if ($notification->socid !=
$id) {
1855 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1858 foreach ($request_data as $field => $value) {
1859 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1862 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1863 throw new RestException(500,
'Error updating values');
1886 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1887 throw new RestException(403);
1890 throw new RestException(400,
'Thirdparty ID is mandatory');
1894 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1901 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1902 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1903 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1905 $sql .=
" WHERE fk_soc = ".((int)
$id);
1908 $result = $this->db->query($sql);
1910 if ($this->db->num_rows($result) == 0) {
1911 throw new RestException(404,
'Account not found');
1916 $accounts = array();
1920 $num = $this->db->num_rows($result);
1924 $obj = $this->db->fetch_object($result);
1927 if ($account->fetch($obj->rowid)) {
1928 $accounts[] = $account;
1933 throw new RestException(404,
'Account not found');
1937 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1939 $returnAccounts = array();
1941 foreach ($accounts as $account) {
1943 foreach ($account as $key => $value) {
1944 if (in_array($key, $fields)) {
1945 if ($key ==
'iban') {
1955 return $returnAccounts;
1976 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1977 throw new RestException(403);
1979 if ($this->company->fetch(
$id) <= 0) {
1980 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1984 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1989 $account->socid =
$id;
1991 foreach ($request_data as $field => $value) {
1992 if ($field ===
'caller') {
1994 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1998 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
2001 if ($account->create(DolibarrApiAccess::$user) < 0) {
2002 throw new RestException(500,
'Error creating Company Bank account');
2005 if (empty($account->rum)) {
2006 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
2008 $account->rum = $prelevement->buildRumNumber((
string) $this->company->code_client, $account->datec, (
string) $account->id);
2009 $account->date_rum =
dol_now();
2012 if ($account->update(DolibarrApiAccess::$user) < 0) {
2013 throw new RestException(500,
'Error updating values');
2038 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2039 throw new RestException(403);
2041 if ($this->company->fetch(
$id) <= 0) {
2042 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
2046 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2052 $account->fetch($bankaccount_id,
'',
$id, -1,
'');
2054 if ($account->socid !=
$id) {
2055 throw new RestException(403);
2059 foreach ($request_data as $field => $value) {
2060 if ($field ===
'caller') {
2062 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2069 if (empty($account->rum)) {
2070 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
2072 $account->rum = $prelevement->buildRumNumber((
string) $this->company->code_client, $account->datec, (
string) $account->id);
2073 $account->date_rum =
dol_now();
2076 if ($account->update(DolibarrApiAccess::$user) < 0) {
2077 throw new RestException(500,
'Error updating values');
2099 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2100 throw new RestException(403);
2104 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2109 $account->fetch($bankaccount_id);
2111 $socid = (int) $account->socid;
2113 if ($socid ==
$id) {
2114 return $account->delete(DolibarrApiAccess::$user);
2116 throw new RestException(403,
"Not allowed due to bad consistency of input data");
2140 global
$conf, $langs;
2142 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
2144 if ($this->company->fetch(
$id) <= 0) {
2145 throw new RestException(404,
'Thirdparty not found');
2148 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2149 throw new RestException(403);
2153 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2156 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
2158 $this->company->fk_bank = $this->company->fk_account;
2161 $outputlangs = $langs;
2166 if (isset($this->company->thirdparty->default_lang)) {
2167 $newlang = $this->company->thirdparty->default_lang;
2168 } elseif (isset($this->company->default_lang)) {
2169 $newlang = $this->company->default_lang;
2172 if (!empty($newlang)) {
2174 $outputlangs->setDefaultLang($newlang);
2177 $sql =
"SELECT rowid";
2178 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
2180 $sql .=
" WHERE fk_soc = ".((int)
$id);
2182 if ($companybankid) {
2183 $sql .=
" AND rowid = ".((int) $companybankid);
2187 $accounts = array();
2189 $result = $this->db->query($sql);
2191 if ($this->db->num_rows($result) == 0) {
2192 throw new RestException(404,
'Bank account not found');
2195 $num = $this->db->num_rows($result);
2199 $obj = $this->db->fetch_object($result);
2202 if ($account->fetch($obj->rowid)) {
2203 $accounts[] = $account;
2208 throw new RestException(500,
'Sql error '.$this->db->lasterror());
2211 $moreparams = array(
2212 'use_companybankid' => $accounts[0]->
id,
2213 'force_dir_output' =>
$conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName((
string) $this->company->id)
2216 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
2219 return array(
"success" => $result);
2221 throw new RestException(500,
'Error generating the document '.$this->company->error);
2244 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2245 throw new RestException(403);
2249 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2255 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2256 $sql .=
" WHERE fk_soc = ".((int)
$id);
2258 $sql .=
" AND site ='".$this->db->escape($site).
"'";
2261 $result = $this->db->query($sql);
2263 if ($result && $this->db->num_rows($result) == 0) {
2264 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
2269 $accounts = array();
2272 $num = $this->db->num_rows($result);
2276 $obj = $this->db->fetch_object($result);
2279 if ($account->fetch($obj->rowid)) {
2280 $accounts[] = $account;
2285 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
2287 $returnAccounts = array();
2289 foreach ($accounts as $account) {
2291 foreach ($account as $key => $value) {
2292 if (in_array($key, $fields)) {
2299 return $returnAccounts;
2321 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2322 throw new RestException(403);
2325 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2326 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
2327 $sql .=
" AND entity IN (".getEntity(
'societe').
")";
2329 $result = $this->db->query($sql);
2331 if ($result && $this->db->num_rows($result) == 1) {
2332 $obj = $this->db->fetch_object($result);
2333 $returnThirdparty = $this->
_fetch($obj->fk_soc);
2335 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
2339 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2342 return $returnThirdparty;
2370 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2371 throw new RestException(403);
2375 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2378 if (!isset($request_data[
'site'])) {
2379 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
2382 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
2383 $result = $this->db->query($sql);
2385 if ($result && $this->db->num_rows($result) == 0) {
2387 if (!isset($request_data[
'login'])) {
2388 $account->login =
"";
2390 $account->fk_soc =
$id;
2392 foreach ($request_data as $field => $value) {
2393 if ($field ===
'caller') {
2395 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2402 if ($account->create(DolibarrApiAccess::$user) < 0) {
2403 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
2410 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
2442 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2443 throw new RestException(403);
2447 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2450 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2451 $result = $this->db->query($sql);
2454 if ($result && $this->db->num_rows($result) == 0) {
2455 if (!isset($request_data[
'key_account'])) {
2456 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
2459 if (!isset($request_data[
'login'])) {
2460 $account->login =
"";
2463 foreach ($request_data as $field => $value) {
2464 if ($field ===
'caller') {
2466 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2473 $account->fk_soc =
$id;
2474 $account->site = $site;
2476 if ($account->create(DolibarrApiAccess::$user) < 0) {
2477 throw new RestException(500,
'Error creating SocieteAccount entity.');
2481 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2482 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2483 $result = $this->db->query($sql);
2485 if ($result && $this->db->num_rows($result) !== 0) {
2486 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.");
2490 $obj = $this->db->fetch_object($result);
2493 $account->id = $obj->rowid;
2494 $account->fk_soc =
$id;
2495 $account->site = $site;
2496 if (!isset($request_data[
'login'])) {
2497 $account->login =
"";
2499 $account->fk_user_creat = $obj->fk_user_creat;
2500 $account->date_creation = $obj->date_creation;
2502 foreach ($request_data as $field => $value) {
2503 if ($field ===
'caller') {
2505 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2512 if ($account->update(DolibarrApiAccess::$user) < 0) {
2513 throw new RestException(500,
'Error updating SocieteAccount entity.');
2544 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2545 throw new RestException(403);
2549 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2552 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2553 $result = $this->db->query($sql);
2555 if ($result && $this->db->num_rows($result) == 0) {
2556 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
2559 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2560 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2561 $result = $this->db->query($sql);
2563 if ($result && $this->db->num_rows($result) !== 0) {
2564 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.");
2568 $obj = $this->db->fetch_object($result);
2570 $account->fetch($obj->rowid);
2572 foreach ($request_data as $field => $value) {
2573 if ($field ===
'caller') {
2575 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2582 if ($account->update(DolibarrApiAccess::$user) < 0) {
2583 throw new RestException(500,
'Error updating SocieteAccount account');
2612 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2613 throw new RestException(403);
2617 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2620 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2621 $result = $this->db->query($sql);
2623 if ($result && $this->db->num_rows($result) == 0) {
2624 throw new RestException(404);
2626 $obj = $this->db->fetch_object($result);
2628 $account->fetch($obj->rowid);
2630 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2631 throw new RestException(500,
"Error while deleting $site account attached to this third party");
2653 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2654 throw new RestException(403);
2658 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2665 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2666 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id);
2668 $result = $this->db->query($sql);
2670 if ($result && $this->db->num_rows($result) == 0) {
2671 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
2676 $num = $this->db->num_rows($result);
2680 $obj = $this->db->fetch_object($result);
2682 $account->fetch($obj->rowid);
2684 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2685 throw new RestException(500,
'Error while deleting account attached to this third party');
2711 unset(
$object->departement_code);
2720 unset(
$object->commercial_id);
2724 unset(
$object->total_localtax1);
2725 unset(
$object->total_localtax2);
2731 unset(
$object->fk_delivery_address);
2746 if ($data ===
null) {
2749 $thirdparty = array();
2750 foreach (Thirdparties::$FIELDS as $field) {
2751 if (!isset($data[$field])) {
2752 throw new RestException(400,
"$field field missing");
2754 $thirdparty[$field] = $data[$field];
2782 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2784 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2785 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2789 $result = $this->company->initAsSpecimen();
2791 $result = $this->company->fetch((
int) $rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2794 throw new RestException(404,
'Thirdparty not found');
2798 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2801 $this->company->getNoEmail();
2805 $filterabsolutediscount =
"fk_facture_source IS NULL";
2806 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2808 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2809 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2812 $absolute_discount = $this->company->getAvailableDiscounts(
null, $filterabsolutediscount);
2813 $absolute_creditnote = $this->company->getAvailableDiscounts(
null, $filtercreditnote);
2814 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2815 $this->company->absolute_creditnote =
price2num($absolute_creditnote,
'MT');
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
Class to manage withdrawal receipts.
Class to manage categories.
Class to manage bank accounts description of third parties.
Class to manage absolute discounts.
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
Class to manage invoices.
Class to manage the table of subscription to notifications.
Class for SocieteAccount.
Class to manage third parties objects (customers, suppliers, prospects...)
updateCompanyNotification($id, $notification_id, $request_data=null)
Update a company notification for a third party.
setThirdpartyPriceLevel($id, $priceLevel)
Set a new price level for the given third party.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
getSocieteByAccounts($site, $key_account)
Get a specific third party by account.
getSupplierCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get supplier categories for a third party.
deleteCompanyNotification($id, $notification_id)
Delete a company notification attached to a third party.
getSocieteAccounts($id, $site=null)
Get a specific account attached to a third party.
getOutStandingOrder($id, $mode='customer')
Get outstanding orders for a third party.
addRepresentative($id, $representative_id)
Add a customer representative to a third party.
getByBarcode($barcode)
Get a third party by barcode.
generateBankAccountDocument($id, $companybankid=null, $model='sepamandate')
Generate a document from a bank account record.
createCompanyNotificationByCode($id, $code, $request_data=null)
Create a company notification for a third party using action trigger code.
getCompanyNotification($id)
Get company notifications for a third party.
addCategory($id, $category_id)
Add a customer category to a third party.
getCompanyBankAccount($id)
Get company bank accounts of a third party.
getInvoicesQualifiedForReplacement($id)
Return invoices qualified to be replaced by another invoice.
post($request_data=null)
Create a third party.
put($id, $request_data=null)
Update third party.
getByEmail($email)
Get properties of a third party by email.
_validate($data)
Validate fields before create or update object.
getFixedAmountDiscounts($id, $mode='customer', $filter="none", $sortfield="f.type", $sortorder='ASC')
Get fixed amount discount of a third party.
addSupplierCategory($id, $category_id)
Add a supplier category to a third party.
merge($id, $idtodelete)
Merge a third party into another third party.
deleteSocieteAccounts($id)
Delete all accounts attached to a third party.
__construct()
Constructor.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get customer categories for a third party.
postSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site account for a third party.
deleteSupplierCategory($id, $category_id)
Remove the link between a category and the third party.
createFixedAmountDiscount($id, $request_data=null)
Create a fixed amount discount for a thirdparty.
deleteRepresentative($id, $representative_id)
Remove the link between a customer representative and a third party.
createCompanyNotification($id, $request_data=null)
Create a company notification for a third party.
putSocieteAccount($id, $site, $request_data=null)
Update specified values of a specific account attached to a third party.
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update a company bank account of a third party.
deleteSocieteAccount($id, $site)
Delete a specific site account attached to a third party.
getInvoicesQualifiedForCreditNote($id)
Return invoices qualified to be corrected by a credit note.
getOutStandingProposals($id, $mode='customer')
Get outstanding proposals for a third party.
_fetch($rowid, $ref='', $ref_ext='', $barcode='', $idprof1='', $idprof2='', $idprof3='', $idprof4='', $idprof5='', $idprof6='', $email='', $ref_alias='')
Fetch properties of a thirdparty object.
getSalesRepresentatives($id, $mode=0)
Get representatives of a third party.
getOutStandingInvoices($id, $mode='customer')
Get outstanding invoices for a third party.
splitdiscount($id, $discountid, $amount_ttc_1, $amount_ttc_2)
Split a discount in 2 smaller discount.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List third parties.
deleteCompanyBankAccount($id, $bankaccount_id)
Delete a bank account attached to a third party.
createSocieteAccount($id, $request_data=null)
Create and attach a new account to an existing third party.
createCompanyBankAccount($id, $request_data=null)
Create a company bank account for a third party.
deleteCategory($id, $category_id)
Remove the link between a customer category and the third party.
Class to manage translations.
Class to manage Dolibarr users.
dol_now($mode='gmt')
Return date for now.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
isModEnabled($module)
Is Dolibarr module enabled.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.