27use Luracast\Restler\RestException;
42 public static $FIELDS = array(
59 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
60 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
61 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
62 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companybankaccount.class.php';
63 require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
65 $this->company =
new Societe($this->db);
68 static::$FIELDS[] =
'email';
84 public function get(
$id)
107 return $this->
_fetch(
null,
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
126 return $this->
_fetch(
null,
'',
'', $barcode);
155 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $mode = 0, $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
159 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
160 throw new RestException(403);
164 $socids = DolibarrApiAccess::$user->socid ? (
string) DolibarrApiAccess::$user->socid :
'';
168 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
169 $search_sale = DolibarrApiAccess::$user->id;
172 $sql =
"SELECT t.rowid";
173 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as t";
174 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_extrafields AS ef ON ef.fk_object = t.rowid";
177 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_societe as c";
179 if (!in_array($mode, array(1, 2, 3))) {
180 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_fournisseur as cc";
183 $sql .=
" WHERE t.entity IN (".getEntity(
'societe').
")";
185 $sql .=
" AND t.client IN (1, 3)";
186 } elseif ($mode == 2) {
187 $sql .=
" AND t.client IN (2, 3)";
188 } elseif ($mode == 3) {
189 $sql .=
" AND t.client IN (0)";
190 } elseif ($mode == 4) {
191 $sql .=
" AND t.fournisseur IN (1)";
195 if (!empty($mode) && $mode != 4) {
196 $sql .=
" AND c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid";
197 } elseif (!empty($mode) && $mode == 4) {
198 $sql .=
" AND cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid";
200 $sql .=
" AND ((c.fk_categorie = ".((int) $category).
" AND c.fk_soc = t.rowid) OR (cc.fk_categorie = ".((int) $category).
" AND cc.fk_soc = t.rowid))";
204 $sql .=
" AND t.rowid IN (".$this->db->sanitize($socids).
")";
207 if ($search_sale && $search_sale !=
'-1') {
208 if ($search_sale == -2) {
209 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.rowid)";
210 } elseif ($search_sale > 0) {
211 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.rowid AND sc.fk_user = ".((int) $search_sale).
")";
217 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
219 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
224 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
226 $sql .= $this->db->order($sortfield, $sortorder);
231 $offset = $limit * $page;
233 $sql .= $this->db->plimit($limit + 1, $offset);
236 $result = $this->db->query($sql);
238 $num = $this->db->num_rows($result);
239 $min = min($num, ($limit <= 0 ? $num : $limit));
242 $obj = $this->db->fetch_object($result);
243 $soc_static =
new Societe($this->db);
244 if ($soc_static->fetch($obj->rowid)) {
246 $soc_static->getNoEmail();
253 throw new RestException(503,
'Error when retrieve third parties : '.$this->db->lasterror());
255 if (!count($obj_ret)) {
259 $message =
'No third parties found';
262 $message =
'No customers found';
265 $message =
'No prospects found';
268 $message =
'No other third parties found';
271 $message =
'No suppliers found';
273 throw new RestException(404, $message);
277 if ($pagination_data) {
278 $totalsResult = $this->db->query($sqlTotals);
279 $total = $this->db->fetch_object($totalsResult)->total;
284 $obj_ret[
'data'] = $tmp;
285 $obj_ret[
'pagination'] = [
286 'total' => (int) $total,
288 'page_count' => ceil((
int) $total / $limit),
308 public function post($request_data =
null)
310 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
311 throw new RestException(403);
315 if (!isset($request_data[
'country_id']) && isset($request_data[
'country_code'])) {
316 $field = strlen($request_data[
'country_code']) > 2 ?
'code_iso' :
'code';
317 $id =
dol_getIdFromCode($this->db, $request_data[
'country_code'],
"c_country", $field,
"rowid");
319 throw new RestException(404,
'Country code not found in database: ' . $this->db->error);
321 $request_data[
'country_id'] =
$id;
325 $result = $this->
_validate($request_data);
327 foreach ($request_data as $field => $value) {
328 if ($field ===
'caller') {
330 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
333 if ($field ==
'array_options' && is_array($value)) {
334 $this->company->fetch_optionals();
336 foreach ($value as $index => $val) {
342 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
345 if ($this->company->create(DolibarrApiAccess::$user) < 0) {
346 throw new RestException(500,
'Error creating thirdparty', array_merge(array($this->company->error), $this->company->errors));
348 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
349 $this->company->setNoEmail($this->company->no_email);
352 return $this->company->id;
372 public function put(
$id, $request_data =
null)
374 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
375 throw new RestException(403);
378 $result = $this->company->fetch(
$id);
380 throw new RestException(404,
'Thirdparty not found');
384 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
387 foreach ($request_data as $field => $value) {
388 if ($field ==
'id') {
391 if ($field ===
'caller') {
393 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
396 if ($field ==
'array_options' && is_array($value)) {
397 foreach ($value as $index => $val) {
402 $this->company->$field = $this->
_checkValForAPI($field, $value, $this->company);
405 if (
isModEnabled(
'mailing') && !empty($this->company->email) && isset($this->company->no_email)) {
406 $this->company->setNoEmail($this->company->no_email);
409 if ($this->company->update(
$id, DolibarrApiAccess::$user, 1, 1, 1,
'update', 1) > 0) {
410 return $this->
get(
$id);
412 throw new RestException(500, $this->company->error);
439 if (
$id == $idtodelete) {
440 throw new RestException(400,
'Try to merge a thirdparty into itself');
443 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
444 throw new RestException(403);
447 $result = $this->company->fetch(
$id);
449 throw new RestException(404,
'Thirdparty not found');
453 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
456 $companytoremove =
new Societe($this->db);
457 $result = $companytoremove->fetch($idtodelete);
459 throw new RestException(404,
'Thirdparty not found');
463 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
466 $user = DolibarrApiAccess::$user;
467 $result = $this->company->mergeCompany($companytoremove->id);
469 throw new RestException(500,
'Error failed to merged thirdparty '.$companytoremove->id.
' into '.
$id.
'. Enable and read log file for more information.');
472 return $this->
get(
$id);
487 public function delete(
$id)
489 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
490 throw new RestException(403);
492 $result = $this->company->fetch(
$id);
494 throw new RestException(404,
'Thirdparty not found');
497 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
499 $this->company->oldcopy = clone $this->company;
501 $res = $this->company->delete(
$id);
503 throw new RestException(500,
"Can't delete, error occurs");
504 } elseif ($res == 0) {
505 throw new RestException(409,
"Can't delete, that product is probably used");
511 'message' =>
'Object deleted'
538 throw new RestException(501,
'Module "Thirdparties" needed for this request');
542 throw new RestException(501,
'Module "Products" needed for this request');
546 throw new RestException(501,
'Multiprices features activation needed for this request');
549 if ($priceLevel < 1 || $priceLevel >
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT')) {
550 throw new RestException(400,
'Price level must be between 1 and ' .
getDolGlobalString(
'PRODUIT_MULTIPRICES_LIMIT'));
553 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
554 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
557 $result = $this->company->fetch(
$id);
559 throw new RestException(404,
'Thirdparty '.
$id.
' not found');
562 if (empty($result)) {
563 throw new RestException(500,
'Error fetching thirdparty '.
$id, array_merge(array($this->company->error), $this->company->errors));
567 throw new RestException(403,
'Access to thirdparty '.
$id.
' not allowed for login '.DolibarrApiAccess::$user->login);
570 $result = $this->company->setPriceLevel($priceLevel, DolibarrApiAccess::$user);
572 throw new RestException(500,
'Error setting new price level for thirdparty '.
$id, array($this->company->db->lasterror()));
594 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
595 throw new RestException(403);
597 $result = $this->company->fetch(
$id);
599 throw new RestException(404,
'Thirdparty not found');
601 $usertmp =
new User($this->db);
602 $result = $usertmp->fetch($representative_id);
604 throw new RestException(404,
'User not found');
607 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
609 $result = $this->company->add_commercial(DolibarrApiAccess::$user, $representative_id);
630 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'supprimer')) {
631 throw new RestException(403);
633 $result = $this->company->fetch(
$id);
635 throw new RestException(404,
'Thirdparty not found');
637 $usertmp =
new User($this->db);
638 $result = $usertmp->fetch($representative_id);
640 throw new RestException(404,
'User not found');
643 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
645 $result = $this->company->del_commercial(DolibarrApiAccess::$user, $representative_id);
668 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
670 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
671 throw new RestException(403);
674 $result = $this->company->fetch(
$id);
676 throw new RestException(404,
'Thirdparty not found');
681 $arrayofcateg = $categories->getListForItem(
$id,
'customer', $sortfield, $sortorder, $limit, $page);
683 if (is_numeric($arrayofcateg) && $arrayofcateg < 0) {
684 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
687 if (is_numeric($arrayofcateg) && $arrayofcateg >= 0) {
691 return $arrayofcateg;
712 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
713 throw new RestException(403);
716 $result = $this->company->fetch(
$id);
718 throw new RestException(404,
'Thirdparty not found');
721 $result = $category->fetch($category_id);
723 throw new RestException(404,
'category not found');
727 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
730 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
733 $category->add_type($this->company,
'customer');
756 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
757 throw new RestException(403);
760 $result = $this->company->fetch(
$id);
762 throw new RestException(404,
'Thirdparty not found');
765 $result = $category->fetch($category_id);
767 throw new RestException(404,
'category not found');
771 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
774 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
777 $category->del_type($this->company,
'customer');
803 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
804 throw new RestException(403);
807 $result = $this->company->fetch(
$id);
809 throw new RestException(404,
'Thirdparty not found');
814 $result = $categories->getListForItem(
$id,
'supplier', $sortfield, $sortorder, $limit, $page);
816 if (is_numeric($result) && $result < 0) {
817 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
820 if (is_numeric($result) && $result == 0) {
845 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
846 throw new RestException(403);
849 $result = $this->company->fetch(
$id);
851 throw new RestException(404,
'Thirdparty not found');
854 $result = $category->fetch($category_id);
856 throw new RestException(404,
'category not found');
860 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
863 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
866 $category->add_type($this->company,
'supplier');
889 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
890 throw new RestException(403);
893 $result = $this->company->fetch(
$id);
895 throw new RestException(404,
'Thirdparty not found');
898 $result = $category->fetch($category_id);
900 throw new RestException(404,
'category not found');
904 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
907 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
910 $category->del_type($this->company,
'supplier');
936 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
937 throw new RestException(403);
941 throw new RestException(400,
'Thirdparty ID is mandatory');
945 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
948 $result = $this->company->fetch(
$id);
950 throw new RestException(404,
'Thirdparty not found');
953 $result = $this->company->getOutstandingProposals($mode);
955 unset($result[
'total_ht']);
956 unset($result[
'total_ttc']);
982 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
983 throw new RestException(403);
987 throw new RestException(400,
'Thirdparty ID is mandatory');
991 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
994 $result = $this->company->fetch(
$id);
996 throw new RestException(404,
'Thirdparty not found');
999 $result = $this->company->getOutstandingOrders($mode);
1001 unset($result[
'total_ht']);
1002 unset($result[
'total_ttc']);
1027 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1028 throw new RestException(403);
1032 throw new RestException(400,
'Thirdparty ID is mandatory');
1036 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1039 $result = $this->company->fetch(
$id);
1041 throw new RestException(404,
'Thirdparty not found');
1044 $result = $this->company->getOutstandingBills($mode);
1046 unset($result[
'total_ht']);
1047 unset($result[
'total_ttc']);
1072 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1073 throw new RestException(403);
1077 throw new RestException(400,
'Thirdparty ID is mandatory');
1081 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1084 $result = $this->company->fetch(
$id);
1086 throw new RestException(404,
'Thirdparty not found');
1089 $result = $this->company->getSalesRepresentatives(DolibarrApiAccess::$user, $mode);
1122 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1123 throw new RestException(403);
1127 throw new RestException(400,
'Thirdparty ID is mandatory');
1131 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1134 $result = $this->company->fetch(
$id);
1136 throw new RestException(404,
'Thirdparty not found');
1140 if ($mode ===
'customer') {
1141 $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";
1142 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1143 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as f ON f.rowid = re.fk_facture_source";
1144 $sql .=
" WHERE re.fk_soc = ".((int)
$id);
1145 if ($filter ==
"available") {
1146 $sql .=
" AND re.fk_facture IS NULL AND re.fk_facture_line IS NULL";
1148 if ($filter ==
"used") {
1149 $sql .=
" AND (re.fk_facture IS NOT NULL OR re.fk_facture_line IS NOT NULL)";
1151 } elseif ($mode ===
'supplier') {
1152 $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";
1153 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re";
1154 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture_fourn as f ON f.rowid = re.fk_invoice_supplier_source";
1155 $sql .=
" WHERE f.rowid = re.fk_invoice_supplier_source AND re.fk_soc = ".((int)
$id);
1156 if ($filter ==
"available") {
1157 $sql .=
" AND re.fk_invoice_supplier IS NULL AND re.fk_invoice_supplier_line IS NULL";
1159 if ($filter ==
"used") {
1160 $sql .=
" AND (re.fk_invoice_supplier IS NOT NULL OR re.fk_invoice_supplier_line IS NOT NULL)";
1164 $sql .= $this->db->order($sortfield, $sortorder);
1166 $result = $this->db->query($sql);
1168 throw new RestException(503, $this->db->lasterror());
1171 while ($obj = $this->db->fetch_object($result)) {
1205 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1206 throw new RestException(403);
1211 throw new RestException(400,
'Thirdparty ID is mandatory');
1213 if (!isset($request_data[
'amount'])) {
1214 throw new RestException(400,
'Missing required field: amount');
1216 if (!isset($request_data[
'description'])) {
1217 throw new RestException(400,
'Missing required field: description');
1222 throw new RestException(401,
'Access not allowed for login'.DolibarrApiAccess::$user->login);
1226 if ($this->company->fetch(
$id) <= 0) {
1227 throw new RestException(404,
'Error creating discount, thirdparty not found');
1232 if (!is_numeric($request_data[
'amount']) || $request_data[
'amount'] <= 0) {
1233 throw new RestException(400,
'Invalid amount_ht: must be a positive number');
1235 $amount = (float) $request_data[
'amount'];
1238 if (isset($request_data[
'tva_tx']) && (!is_numeric($request_data[
'tva_tx']) || $request_data[
'tva_tx'] < 0)) {
1239 throw new RestException(400,
'Invalid tva_tx: must be a positive number or zero');
1241 $tva_tx = isset($request_data[
'tva_tx']) ? (float) $request_data[
'tva_tx'] : 0;
1244 $price_base_type =
'HT';
1245 if (isset($request_data[
'price_base_type'])) {
1246 $price_base_type = strtoupper($request_data[
'price_base_type']);
1247 if ($price_base_type !==
'HT' && $price_base_type !==
'TTC') {
1248 throw new RestException(400,
'Invalid price_base_type: must be "HT" or "TTC"');
1254 if (isset($request_data[
'discount_type'])) {
1255 $discount_type = (int) $request_data[
'discount_type'];
1256 if ($discount_type !== 0 && $discount_type !== 1) {
1257 throw new RestException(400,
'Invalid discount_type: must be 0 (customer) or 1 (supplier)');
1262 $description = $request_data[
'description'];
1263 if (empty(trim($description))) {
1264 throw new RestException(400,
'Description cannot be empty');
1269 if (isset($request_data[
'vat_src_code']) && !empty($request_data[
'vat_src_code'])) {
1270 $vatrate = $tva_tx .
' (' . $request_data[
'vat_src_code'] .
')';
1276 $result = $this->company->set_remise_except($amount, DolibarrApiAccess::$user, $description, $vatrate, $discount_type, $price_base_type);
1279 $this->db->commit();
1282 $this->db->rollback();
1283 throw new RestException(500,
'Error creating discount: '.$this->company->error, array_merge(array($this->company->error), $this->company->errors));
1314 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer') || !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1315 throw new RestException(403);
1319 throw new RestException(400,
'Thirdparty ID is mandatory');
1321 if (empty($discountid)) {
1322 throw new RestException(400,
'Discount ID is mandatory');
1324 if (empty($amount_ttc_1) || empty($amount_ttc_2)) {
1325 throw new RestException(400,
'Amount are mandatory');
1329 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1332 $result = $this->company->fetch(
$id);
1334 throw new RestException(404,
'Thirdparty not found');
1336 require_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
1338 $res = $discount->fetch($discountid);
1340 throw new RestException(404,
'Discount not found');
1342 if ($discount->socid !=
$id) {
1343 throw new RestException(405,
'Discount not owned by this thirdpartie');
1346 if (
price2num((
float) $amount_ttc_1 + (
float) $amount_ttc_2) != $discount->amount_ttc) {
1347 throw new RestException(405,
'Sum of the 2 discounts is different that the original discount');
1349 if ($discount->fk_facture_line) {
1350 throw new RestException(409,
'Discount is already used');
1356 $newdiscount1->fk_facture_source = $discount->fk_facture_source;
1357 $newdiscount2->fk_facture_source = $discount->fk_facture_source;
1358 $newdiscount1->fk_facture = $discount->fk_facture;
1359 $newdiscount2->fk_facture = $discount->fk_facture;
1360 $newdiscount1->fk_facture_line = $discount->fk_facture_line;
1361 $newdiscount2->fk_facture_line = $discount->fk_facture_line;
1362 $newdiscount1->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1363 $newdiscount2->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source;
1364 $newdiscount1->fk_invoice_supplier = $discount->fk_invoice_supplier;
1365 $newdiscount2->fk_invoice_supplier = $discount->fk_invoice_supplier;
1366 $newdiscount1->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1367 $newdiscount2->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line;
1368 if ($discount->description ==
'(CREDIT_NOTE)' || $discount->description ==
'(DEPOSIT)') {
1369 $newdiscount1->description = $discount->description;
1370 $newdiscount2->description = $discount->description;
1372 $newdiscount1->description = $discount->description.
' (1)';
1373 $newdiscount2->description = $discount->description.
' (2)';
1376 $newdiscount1->fk_user = $discount->fk_user;
1377 $newdiscount2->fk_user = $discount->fk_user;
1378 $newdiscount1->fk_soc = $discount->fk_soc;
1379 $newdiscount1->socid = $discount->socid;
1380 $newdiscount2->fk_soc = $discount->fk_soc;
1381 $newdiscount2->socid = $discount->socid;
1382 $newdiscount1->discount_type = $discount->discount_type;
1383 $newdiscount2->discount_type = $discount->discount_type;
1384 $newdiscount1->datec = $discount->datec;
1385 $newdiscount2->datec = $discount->datec;
1386 $newdiscount1->tva_tx = $discount->tva_tx;
1387 $newdiscount2->tva_tx = $discount->tva_tx;
1388 $newdiscount1->vat_src_code = $discount->vat_src_code;
1389 $newdiscount2->vat_src_code = $discount->vat_src_code;
1390 $newdiscount1->amount_ttc = $amount_ttc_1;
1391 $newdiscount2->amount_ttc =
price2num($discount->amount_ttc - $newdiscount1->amount_ttc);
1392 $newdiscount1->amount_ht =
price2num($newdiscount1->amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1393 $newdiscount2->amount_ht =
price2num($newdiscount2->amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1394 $newdiscount1->amount_tva =
price2num($newdiscount1->amount_ttc - $newdiscount1->amount_ht);
1395 $newdiscount2->amount_tva =
price2num($newdiscount2->amount_ttc - $newdiscount2->amount_ht);
1397 $newdiscount1->multicurrency_amount_ttc = (float) $amount_ttc_1 * ($discount->multicurrency_amount_ttc / $discount->amount_ttc);
1398 $newdiscount2->multicurrency_amount_ttc =
price2num($discount->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ttc);
1399 $newdiscount1->multicurrency_amount_ht =
price2num($newdiscount1->multicurrency_amount_ttc / (1 + $newdiscount1->tva_tx / 100),
'MT');
1400 $newdiscount2->multicurrency_amount_ht =
price2num($newdiscount2->multicurrency_amount_ttc / (1 + $newdiscount2->tva_tx / 100),
'MT');
1401 $newdiscount1->multicurrency_amount_tva =
price2num($newdiscount1->multicurrency_amount_ttc - $newdiscount1->multicurrency_amount_ht);
1402 $newdiscount2->multicurrency_amount_tva =
price2num($newdiscount2->multicurrency_amount_ttc - $newdiscount2->multicurrency_amount_ht);
1406 $newdiscount1->total_ttc = (float)
price2num($newdiscount1->amount_ttc);
1407 $newdiscount1->total_ht = (float)
price2num($newdiscount1->amount_ht);
1408 $newdiscount1->total_tva = (float)
price2num($newdiscount1->amount_tva);
1409 $newdiscount2->total_ttc = (float)
price2num($newdiscount2->amount_ttc);
1410 $newdiscount2->total_ht = (float)
price2num($newdiscount2->amount_ht);
1411 $newdiscount2->total_tva = (float)
price2num($newdiscount2->amount_tva);
1412 $newdiscount1->multicurrency_total_ttc = (float)
price2num($newdiscount1->multicurrency_amount_ttc);
1413 $newdiscount1->multicurrency_total_ht = (float)
price2num($newdiscount1->multicurrency_amount_ht);
1414 $newdiscount1->multicurrency_total_tva = (float)
price2num($newdiscount1->multicurrency_amount_tva);
1415 $newdiscount2->multicurrency_total_ttc = (float)
price2num($newdiscount2->multicurrency_amount_ttc);
1416 $newdiscount2->multicurrency_total_ht = (float)
price2num($newdiscount2->multicurrency_amount_ht);
1417 $newdiscount2->multicurrency_total_tva = (float)
price2num($newdiscount2->multicurrency_amount_tva);
1421 $discount->fk_facture_source = 0;
1423 $discount->fk_invoice_supplier_source = 0;
1424 $res = $discount->delete(DolibarrApiAccess::$user);
1425 $newid1 = $newdiscount1->create(DolibarrApiAccess::$user);
1426 $newid2 = $newdiscount2->create(DolibarrApiAccess::$user);
1427 if ($res <= 0 || $newid1 <= 0 || $newid2 <= 0) {
1428 $this->db->rollback();
1429 throw new RestException(500,
'Operation fail');
1432 $this->db->commit();
1434 $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";
1435 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_remise_except as re, ".MAIN_DB_PREFIX.
"facture as f";
1436 $sql .=
" WHERE re.rowid IN ( $newid1, $newid2 ) AND f.rowid = re.fk_facture_source AND re.fk_soc = ".((int)
$id);
1438 $sql .= $this->db->order(
"f.type",
"ASC");
1440 $result = $this->db->query($sql);
1442 throw new RestException(503, $this->db->lasterror());
1445 while ($obj = $this->db->fetch_object($result)) {
1473 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1474 throw new RestException(403);
1477 throw new RestException(400,
'Thirdparty ID is mandatory');
1481 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1489 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1490 $invoice =
new Facture($this->db);
1491 $result = $invoice->list_replacable_invoices(
$id);
1493 throw new RestException(405, $invoice->error);
1522 if (!DolibarrApiAccess::$user->hasRight(
'facture',
'lire')) {
1523 throw new RestException(403);
1526 throw new RestException(400,
'Thirdparty ID is mandatory');
1530 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1538 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
1539 $invoice =
new Facture($this->db);
1540 $result = $invoice->list_qualified_avoir_invoices(
$id);
1541 if (!is_array($result) && $result < 0) {
1542 throw new RestException(405, $invoice->error);
1566 throw new RestException(400,
'Thirdparty ID is mandatory');
1568 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1569 throw new RestException(403);
1572 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1579 $sql =
"SELECT rowid as id, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms";
1580 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1582 $sql .=
" WHERE fk_soc = ".((int)
$id);
1585 $result = $this->db->query($sql);
1586 if ($this->db->num_rows($result) == 0) {
1587 throw new RestException(404,
'Notification not found');
1592 $notifications = array();
1596 $num = $this->db->num_rows($result);
1600 $obj = $this->db->fetch_object($result);
1601 $notifications[] = $obj;
1605 throw new RestException(404,
'No notifications found');
1608 $fields = array(
'id',
'socid',
'event',
'contact_id',
'datec',
'tms',
'type');
1610 $returnNotifications = array();
1612 foreach ($notifications as $notification) {
1614 foreach ($notification as $key => $value) {
1615 if (in_array($key, $fields)) {
1619 $returnNotifications[] =
$object;
1623 return $returnNotifications;
1644 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1645 throw new RestException(403,
"User has no right to update thirdparties");
1647 if ($this->company->fetch(
$id) <= 0) {
1648 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1650 $notification =
new Notify($this->db);
1652 $notification->socid =
$id;
1654 foreach ($request_data as $field => $value) {
1655 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1658 $event = $notification->event;
1660 throw new RestException(500,
'Error creating Thirdparty Notification, request_data missing event');
1662 $socid = $notification->socid;
1663 $contact_id = $notification->contact_id;
1665 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1666 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1667 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1668 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1669 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1671 $exists_result = $this->db->query($exists_sql);
1672 if ($this->db->num_rows($exists_result) > 0) {
1673 throw new RestException(403,
'Notification already exists');
1676 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1677 throw new RestException(500,
'Error creating Thirdparty Notification');
1680 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1681 throw new RestException(500,
'Error updating values');
1707 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1708 throw new RestException(403,
"User has no right to update thirdparties");
1710 if ($this->company->fetch(
$id) <= 0) {
1711 throw new RestException(404,
'Error creating Thirdparty Notification, Thirdparty doesn\'t exists');
1713 $notification =
new Notify($this->db);
1714 $notification->socid =
$id;
1716 $sql =
"SELECT t.rowid as id FROM ".MAIN_DB_PREFIX.
"c_action_trigger as t";
1717 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
1719 $result = $this->db->query($sql);
1720 if ($this->db->num_rows($result) == 0) {
1721 throw new RestException(404,
'Action Trigger code not found');
1724 $notification->event = $this->db->fetch_row($result)[0];
1725 foreach ($request_data as $field => $value) {
1726 if ($field ===
'event') {
1727 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains event key');
1729 if ($field ===
'fk_action') {
1730 throw new RestException(500,
'Error creating Thirdparty Notification, request_data contains fk_action key');
1732 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1735 $event = $notification->event;
1736 $socid = $notification->socid;
1737 $contact_id = $notification->contact_id;
1739 $exists_sql =
"SELECT rowid, fk_action as event, fk_soc as socid, fk_contact as contact_id, type, datec, tms as datem";
1740 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1741 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1742 $exists_sql .=
" AND fk_soc = '".$this->db->escape((
string) $socid).
"'";
1743 $exists_sql .=
" AND fk_contact = '".$this->db->escape((
string) $contact_id).
"'";
1745 $exists_result = $this->db->query($exists_sql);
1746 if ($this->db->num_rows($exists_result) > 0) {
1747 throw new RestException(403,
'Notification already exists');
1750 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1751 throw new RestException(500,
'Error creating Thirdparty Notification, are request_data well formed?');
1754 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1755 throw new RestException(500,
'Error updating values');
1777 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1778 throw new RestException(403);
1781 $notification =
new Notify($this->db);
1783 $notification->fetch($notification_id);
1785 $socid = (int) $notification->socid;
1787 if ($socid ==
$id) {
1788 return $notification->delete(DolibarrApiAccess::$user);
1790 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1813 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1814 throw new RestException(403,
"User has no right to update thirdparties");
1816 if ($this->company->fetch(
$id) <= 0) {
1817 throw new RestException(404,
'Error creating Company Notification, Company doesn\'t exists');
1819 $notification =
new Notify($this->db);
1822 $notification->fetch($notification_id,
$id);
1824 if ($notification->socid !=
$id) {
1825 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1828 foreach ($request_data as $field => $value) {
1829 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1832 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1833 throw new RestException(500,
'Error updating values');
1856 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
1857 throw new RestException(403);
1860 throw new RestException(400,
'Thirdparty ID is mandatory');
1864 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1871 $sql =
"SELECT rowid, fk_soc, bank, number, code_banque, code_guichet, cle_rib, bic, iban_prefix as iban, domiciliation as address, proprio,";
1872 $sql .=
" owner_address, default_rib, label, datec, tms as datem, rum, frstrecur";
1873 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
1875 $sql .=
" WHERE fk_soc = ".((int)
$id);
1878 $result = $this->db->query($sql);
1880 if ($this->db->num_rows($result) == 0) {
1881 throw new RestException(404,
'Account not found');
1886 $accounts = array();
1890 $num = $this->db->num_rows($result);
1894 $obj = $this->db->fetch_object($result);
1897 if ($account->fetch($obj->rowid)) {
1898 $accounts[] = $account;
1903 throw new RestException(404,
'Account not found');
1907 $fields = array(
'socid',
'default_rib',
'frstrecur',
'1000110000001',
'datec',
'datem',
'label',
'bank',
'bic',
'iban',
'id',
'rum');
1909 $returnAccounts = array();
1911 foreach ($accounts as $account) {
1913 foreach ($account as $key => $value) {
1914 if (in_array($key, $fields)) {
1915 if ($key ==
'iban') {
1925 return $returnAccounts;
1946 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
1947 throw new RestException(403);
1949 if ($this->company->fetch(
$id) <= 0) {
1950 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
1954 $account->socid =
$id;
1956 foreach ($request_data as $field => $value) {
1957 if ($field ===
'caller') {
1959 $this->company->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
1963 $account->$field = $this->
_checkValForAPI(
'extrafields', $value, $account);
1966 if ($account->create(DolibarrApiAccess::$user) < 0) {
1967 throw new RestException(500,
'Error creating Company Bank account');
1970 if (empty($account->rum)) {
1971 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
1973 $account->rum = $prelevement->buildRumNumber((
string) $this->company->code_client, $account->datec, (
string) $account->id);
1974 $account->date_rum =
dol_now();
1977 if ($account->update(DolibarrApiAccess::$user) < 0) {
1978 throw new RestException(500,
'Error updating values');
2003 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2004 throw new RestException(403);
2006 if ($this->company->fetch(
$id) <= 0) {
2007 throw new RestException(404,
'Error creating Company Bank account, Company doesn\'t exists');
2012 $account->fetch($bankaccount_id,
'',
$id, -1,
'');
2014 if ($account->socid !=
$id) {
2015 throw new RestException(403);
2019 foreach ($request_data as $field => $value) {
2020 if ($field ===
'caller') {
2022 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2029 if (empty($account->rum)) {
2030 require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
2032 $account->rum = $prelevement->buildRumNumber((
string) $this->company->code_client, $account->datec, (
string) $account->id);
2033 $account->date_rum =
dol_now();
2036 if ($account->update(DolibarrApiAccess::$user) < 0) {
2037 throw new RestException(500,
'Error updating values');
2059 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2060 throw new RestException(403);
2065 $account->fetch($bankaccount_id);
2067 $socid = (int) $account->socid;
2069 if ($socid ==
$id) {
2070 return $account->delete(DolibarrApiAccess::$user);
2072 throw new RestException(403,
"Not allowed due to bad consistency of input data");
2096 global
$conf, $langs;
2098 $langs->loadLangs(array(
"main",
"dict",
"commercial",
"products",
"companies",
"banks",
"bills",
"withdrawals"));
2100 if ($this->company->fetch(
$id) <= 0) {
2101 throw new RestException(404,
'Thirdparty not found');
2104 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2105 throw new RestException(403);
2108 $this->company->setDocModel(DolibarrApiAccess::$user, $model);
2110 $this->company->fk_bank = $this->company->fk_account;
2113 $outputlangs = $langs;
2118 if (isset($this->company->thirdparty->default_lang)) {
2119 $newlang = $this->company->thirdparty->default_lang;
2120 } elseif (isset($this->company->default_lang)) {
2121 $newlang = $this->company->default_lang;
2124 if (!empty($newlang)) {
2126 $outputlangs->setDefaultLang($newlang);
2129 $sql =
"SELECT rowid";
2130 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib";
2132 $sql .=
" WHERE fk_soc = ".((int)
$id);
2134 if ($companybankid) {
2135 $sql .=
" AND rowid = ".((int) $companybankid);
2139 $accounts = array();
2141 $result = $this->db->query($sql);
2143 if ($this->db->num_rows($result) == 0) {
2144 throw new RestException(404,
'Bank account not found');
2147 $num = $this->db->num_rows($result);
2151 $obj = $this->db->fetch_object($result);
2154 if ($account->fetch($obj->rowid)) {
2155 $accounts[] = $account;
2160 throw new RestException(500,
'Sql error '.$this->db->lasterror());
2163 $moreparams = array(
2164 'use_companybankid' => $accounts[0]->
id,
2165 'force_dir_output' =>
$conf->societe->multidir_output[$this->company->entity].
'/'.
dol_sanitizeFileName((
string) $this->company->id)
2168 $result = $this->company->generateDocument($model, $outputlangs, 0, 0, 0, $moreparams);
2171 return array(
"success" => $result);
2173 throw new RestException(500,
'Error generating the document '.$this->company->error);
2196 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2197 throw new RestException(403);
2201 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2207 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2208 $sql .=
" WHERE fk_soc = ".((int)
$id);
2210 $sql .=
" AND site ='".$this->db->escape($site).
"'";
2213 $result = $this->db->query($sql);
2215 if ($result && $this->db->num_rows($result) == 0) {
2216 throw new RestException(404,
'This thirdparty does not have any account attached or does not exist.');
2221 $accounts = array();
2224 $num = $this->db->num_rows($result);
2228 $obj = $this->db->fetch_object($result);
2231 if ($account->fetch($obj->rowid)) {
2232 $accounts[] = $account;
2237 $fields = array(
'id',
'fk_soc',
'key_account',
'site',
'date_creation',
'tms');
2239 $returnAccounts = array();
2241 foreach ($accounts as $account) {
2243 foreach ($account as $key => $value) {
2244 if (in_array($key, $fields)) {
2251 return $returnAccounts;
2273 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2274 throw new RestException(403);
2277 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
2278 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
2279 $sql .=
" AND entity IN (".getEntity(
'societe').
")";
2281 $result = $this->db->query($sql);
2283 if ($result && $this->db->num_rows($result) == 1) {
2284 $obj = $this->db->fetch_object($result);
2285 $returnThirdparty = $this->
_fetch($obj->fk_soc);
2287 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
2291 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
2294 return $returnThirdparty;
2322 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2323 throw new RestException(403);
2326 if (!isset($request_data[
'site'])) {
2327 throw new RestException(422,
'Unprocessable Entity: You must pass the site attribute in your request data !');
2330 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"'";
2331 $result = $this->db->query($sql);
2333 if ($result && $this->db->num_rows($result) == 0) {
2335 if (!isset($request_data[
'login'])) {
2336 $account->login =
"";
2338 $account->fk_soc =
$id;
2340 foreach ($request_data as $field => $value) {
2341 if ($field ===
'caller') {
2343 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2350 if ($account->create(DolibarrApiAccess::$user) < 0) {
2351 throw new RestException(500,
'Error creating SocieteAccount entity. Ensure that the ID of thirdparty provided does exist!');
2358 throw new RestException(409,
'A SocieteAccount entity already exists for this company and site.');
2390 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2391 throw new RestException(403);
2394 $sql =
"SELECT rowid, fk_user_creat, date_creation FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
2395 $result = $this->db->query($sql);
2398 if ($result && $this->db->num_rows($result) == 0) {
2399 if (!isset($request_data[
'key_account'])) {
2400 throw new RestException(422,
'Unprocessable Entity: You must pass the key_account attribute in your request data !');
2403 if (!isset($request_data[
'login'])) {
2404 $account->login =
"";
2407 foreach ($request_data as $field => $value) {
2408 if ($field ===
'caller') {
2410 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2417 $account->fk_soc =
$id;
2418 $account->site = $site;
2420 if ($account->create(DolibarrApiAccess::$user) < 0) {
2421 throw new RestException(500,
'Error creating SocieteAccount entity.');
2425 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2426 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2427 $result = $this->db->query($sql);
2429 if ($result && $this->db->num_rows($result) !== 0) {
2430 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.");
2434 $obj = $this->db->fetch_object($result);
2437 $account->id = $obj->rowid;
2438 $account->fk_soc =
$id;
2439 $account->site = $site;
2440 if (!isset($request_data[
'login'])) {
2441 $account->login =
"";
2443 $account->fk_user_creat = $obj->fk_user_creat;
2444 $account->date_creation = $obj->date_creation;
2446 foreach ($request_data as $field => $value) {
2447 if ($field ===
'caller') {
2449 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2456 if ($account->update(DolibarrApiAccess::$user) < 0) {
2457 throw new RestException(500,
'Error updating SocieteAccount entity.');
2488 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2489 throw new RestException(403);
2492 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($site).
"'";
2493 $result = $this->db->query($sql);
2495 if ($result && $this->db->num_rows($result) == 0) {
2496 throw new RestException(404,
"This thirdparty does not have $site account attached or does not exist.");
2499 if (isset($request_data[
'site']) && $request_data[
'site'] !== $site) {
2500 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id).
" AND site = '".$this->db->escape($request_data[
'site']).
"' ";
2501 $result = $this->db->query($sql);
2503 if ($result && $this->db->num_rows($result) !== 0) {
2504 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.");
2508 $obj = $this->db->fetch_object($result);
2510 $account->fetch($obj->rowid);
2512 foreach ($request_data as $field => $value) {
2513 if ($field ===
'caller') {
2515 $account->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
2522 if ($account->update(DolibarrApiAccess::$user) < 0) {
2523 throw new RestException(500,
'Error updating SocieteAccount account');
2552 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2553 throw new RestException(403);
2556 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = $id AND site = '".$this->db->escape($site).
"'";
2557 $result = $this->db->query($sql);
2559 if ($result && $this->db->num_rows($result) == 0) {
2560 throw new RestException(404);
2562 $obj = $this->db->fetch_object($result);
2564 $account->fetch($obj->rowid);
2566 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2567 throw new RestException(500,
"Error while deleting $site account attached to this third party");
2589 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
2590 throw new RestException(403);
2597 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms";
2598 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account WHERE fk_soc = ".((int)
$id);
2600 $result = $this->db->query($sql);
2602 if ($result && $this->db->num_rows($result) == 0) {
2603 throw new RestException(404,
'This third party does not have any account attached or does not exist.');
2608 $num = $this->db->num_rows($result);
2612 $obj = $this->db->fetch_object($result);
2614 $account->fetch($obj->rowid);
2616 if ($account->delete(DolibarrApiAccess::$user) < 0) {
2617 throw new RestException(500,
'Error while deleting account attached to this third party');
2643 unset(
$object->departement_code);
2652 unset(
$object->commercial_id);
2656 unset(
$object->total_localtax1);
2657 unset(
$object->total_localtax2);
2663 unset(
$object->fk_delivery_address);
2678 if ($data ===
null) {
2681 $thirdparty = array();
2682 foreach (Thirdparties::$FIELDS as $field) {
2683 if (!isset($data[$field])) {
2684 throw new RestException(400,
"$field field missing");
2686 $thirdparty[$field] = $data[$field];
2714 private function _fetch($rowid, $ref =
'', $ref_ext =
'', $barcode =
'', $idprof1 =
'', $idprof2 =
'', $idprof3 =
'', $idprof4 =
'', $idprof5 =
'', $idprof6 =
'', $email =
'', $ref_alias =
'')
2716 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
2717 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
'. No read permission on thirdparties.');
2721 $result = $this->company->initAsSpecimen();
2723 $result = $this->company->fetch((
int) $rowid, $ref, $ref_ext, $barcode, $idprof1, $idprof2, $idprof3, $idprof4, $idprof5, $idprof6, $email, $ref_alias);
2726 throw new RestException(404,
'Thirdparty not found');
2730 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login.
' on this thirdparty');
2733 $this->company->getNoEmail();
2737 $filterabsolutediscount =
"fk_facture_source IS NULL";
2738 $filtercreditnote =
"fk_facture_source IS NOT NULL";
2740 $filterabsolutediscount =
"fk_facture_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS RECEIVED)%')";
2741 $filtercreditnote =
"fk_facture_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS RECEIVED)%')";
2744 $absolute_discount = $this->company->getAvailableDiscounts(
null, $filterabsolutediscount);
2745 $absolute_creditnote = $this->company->getAvailableDiscounts(
null, $filtercreditnote);
2746 $this->company->absolute_discount =
price2num($absolute_discount,
'MT');
2747 $this->company->absolute_creditnote =
price2num($absolute_creditnote,
'MT');
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
Class to manage withdrawal receipts.
Class to manage categories.
Class to manage bank accounts description of third parties.
Class to manage absolute discounts.
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class to manage invoices.
Class to manage the table of subscription to notifications.
Class for SocieteAccount.
Class to manage third parties objects (customers, suppliers, prospects...)
updateCompanyNotification($id, $notification_id, $request_data=null)
Update a company notification for a third party.
setThirdpartyPriceLevel($id, $priceLevel)
Set a new price level for the given third party.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
getSocieteByAccounts($site, $key_account)
Get a specific third party by account.
getSupplierCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get supplier categories for a third party.
deleteCompanyNotification($id, $notification_id)
Delete a company notification attached to a third party.
getSocieteAccounts($id, $site=null)
Get a specific account attached to a third party.
getOutStandingOrder($id, $mode='customer')
Get outstanding orders for a third party.
addRepresentative($id, $representative_id)
Add a customer representative to a third party.
getByBarcode($barcode)
Get a third party by barcode.
generateBankAccountDocument($id, $companybankid=null, $model='sepamandate')
Generate a document from a bank account record.
createCompanyNotificationByCode($id, $code, $request_data=null)
Create a company notification for a third party using action trigger code.
getCompanyNotification($id)
Get company notifications for a third party.
addCategory($id, $category_id)
Add a customer category to a third party.
getCompanyBankAccount($id)
Get company bank accounts of a third party.
getInvoicesQualifiedForReplacement($id)
Return invoices qualified to be replaced by another invoice.
post($request_data=null)
Create a third party.
put($id, $request_data=null)
Update third party.
getByEmail($email)
Get properties of a third party by email.
_validate($data)
Validate fields before create or update object.
getFixedAmountDiscounts($id, $mode='customer', $filter="none", $sortfield="f.type", $sortorder='ASC')
Get fixed amount discount of a third party.
addSupplierCategory($id, $category_id)
Add a supplier category to a third party.
merge($id, $idtodelete)
Merge a third party into another third party.
deleteSocieteAccounts($id)
Delete all accounts attached to a third party.
__construct()
Constructor.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get customer categories for a third party.
postSocieteAccount($id, $site, $request_data=null)
Create and attach a new (or replace an existing) specific site account for a third party.
deleteSupplierCategory($id, $category_id)
Remove the link between a category and the third party.
createFixedAmountDiscount($id, $request_data=null)
Create a fixed amount discount for a thirdparty.
deleteRepresentative($id, $representative_id)
Remove the link between a customer representative and a third party.
createCompanyNotification($id, $request_data=null)
Create a company notification for a third party.
putSocieteAccount($id, $site, $request_data=null)
Update specified values of a specific account attached to a third party.
updateCompanyBankAccount($id, $bankaccount_id, $request_data=null)
Update a company bank account of a third party.
deleteSocieteAccount($id, $site)
Delete a specific site account attached to a third party.
getInvoicesQualifiedForCreditNote($id)
Return invoices qualified to be corrected by a credit note.
getOutStandingProposals($id, $mode='customer')
Get outstanding proposals for a third party.
_fetch($rowid, $ref='', $ref_ext='', $barcode='', $idprof1='', $idprof2='', $idprof3='', $idprof4='', $idprof5='', $idprof6='', $email='', $ref_alias='')
Fetch properties of a thirdparty object.
getSalesRepresentatives($id, $mode=0)
Get representatives of a third party.
getOutStandingInvoices($id, $mode='customer')
Get outstanding invoices for a third party.
splitdiscount($id, $discountid, $amount_ttc_1, $amount_ttc_2)
Split a discount in 2 smaller discount.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $mode=0, $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List third parties.
deleteCompanyBankAccount($id, $bankaccount_id)
Delete a bank account attached to a third party.
createSocieteAccount($id, $request_data=null)
Create and attach a new account to an existing third party.
createCompanyBankAccount($id, $request_data=null)
Create a company bank account for a third party.
deleteCategory($id, $category_id)
Remove the link between a customer category and the third party.
Class to manage translations.
Class to manage Dolibarr users.
dol_now($mode='gmt')
Return date for now.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
isModEnabled($module)
Is Dolibarr module enabled.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.