Improved booking handling with unfulfilled booking checks and Slack notifications. Enhanced customer data retrieval with caching optimizations and discount calculations. Added logging, debug capabilities, and expanded Redis functionalities for better data management.
78 lines
3.0 KiB
PHP
78 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace customers;
|
|
|
|
use economic_m;
|
|
|
|
class economicCustomers extends economic_m
|
|
{
|
|
public function searchCustomers(string|int $search, string $filter, int $limit = 10, int $page = 1): object
|
|
{
|
|
// Make sure the search string is ready for the API
|
|
$search = urlencode($search);
|
|
// Search for customers
|
|
$url = '/customers?filter=' . $filter . '$like:' . $search . '&pagesize=' . $limit . '&skippages=' . $page - 1;
|
|
$response = $this->send_request($url, 'GET', '');
|
|
return json_decode($response);
|
|
}
|
|
|
|
public function getCustomerName(int $customer_number): string|null
|
|
{
|
|
// Check if the customer name is cached
|
|
if (redis->get_economic_customer_name($customer_number)) {
|
|
return redis->get_economic_customer_name($customer_number);
|
|
}
|
|
// Get the customer name from the economic system
|
|
$customer = $this->getCustomerId($customer_number);
|
|
if ($customer) {
|
|
$customer = $customer[0];
|
|
redis->cache_economic_customer_name($customer_number, $customer->name);
|
|
return $customer->name;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function getCustomerId(int $customerNumber): object|bool
|
|
{
|
|
// Check if the customer exists
|
|
$url = '/customers/' . $customerNumber;
|
|
$response = $this->send_request($url, 'GET', '');
|
|
$response = json_decode($response);
|
|
|
|
return isset($response->customerNumber) ? $response : false;
|
|
}
|
|
|
|
public function getCustomerProduct(int $customer_number, int $product_id): object
|
|
{
|
|
// Get the customer product
|
|
$url = '/customers/' . $customer_number . '/templates/invoiceline/' . $product_id;
|
|
$response = $this->send_request($url, 'GET', '');
|
|
return json_decode($response);
|
|
}
|
|
|
|
public function getCustomerDiscountPercentage(int $customer_number): int
|
|
{
|
|
// Get the customer products
|
|
$products = $this->getCustomerProducts($customer_number, 1)->collection;
|
|
$discount = $this->getCustomerProductDiscount($customer_number, $products[0]->product->productNumber);
|
|
// Since the discount is global, we only need to get the discount for one product
|
|
return $discount->discountPercentage;
|
|
}
|
|
|
|
public function getCustomerProducts(int $customer_number, int $limit = 10, int $page = 1): object
|
|
{
|
|
// Get the customer products
|
|
$url = '/customers/' . $customer_number . '/templates/invoiceline/?pagesize=' . $limit . '&skippages=' . $page - 1;
|
|
$response = $this->send_request($url, 'GET', '');
|
|
return json_decode($response);
|
|
}
|
|
|
|
public function getCustomerProductDiscount(int $customer_number, int $product_id)
|
|
{
|
|
// Get the customer global discount
|
|
$url = '/customers/' . $customer_number . '/templates/invoiceline/' . $product_id;
|
|
$response = $this->send_request($url, 'GET', '');
|
|
$response = json_decode($response);
|
|
return $response;
|
|
}
|
|
} |