1817 lines
54 KiB
PHP
1817 lines
54 KiB
PHP
<?php
|
|
require_once('fixed/config/private.php');
|
|
ini_set('memory_limit', '5G');
|
|
set_time_limit(60 * 60 * 24);
|
|
|
|
function dd(...$vars) {
|
|
$isCLI = (php_sapi_name() == 'cli');
|
|
$result = "";
|
|
foreach ($vars as $var) {
|
|
$print = $isCLI ? print_r($var, true) : "<pre>" . var_export($var, true) . "</pre>";
|
|
$result .= $print . "\n\n";
|
|
}
|
|
|
|
echo $result;
|
|
die;
|
|
}
|
|
|
|
function fetch(string $query, array $params = []): array {
|
|
global $db;
|
|
$stmt = mysqli_prepare($db, $query);
|
|
$error = mysqli_error($db);
|
|
if (!empty($error)) {
|
|
dd($error);
|
|
}
|
|
if (!empty($params)) {
|
|
$paramTypes = '';
|
|
foreach ($params as $param) {
|
|
if (is_int($param)) {
|
|
$paramTypes .= 'i';
|
|
} else if (is_float($param)) {
|
|
$paramTypes .= 'd';
|
|
} else {
|
|
$paramTypes .= 's';
|
|
}
|
|
}
|
|
$stmt->bind_param($paramTypes, ...$params);
|
|
}
|
|
$stmt->execute();
|
|
$error = mysqli_error($db);
|
|
if (!empty($error)) {
|
|
dd($error);
|
|
}
|
|
$result = $stmt->get_result();
|
|
return $result->fetch_all(MYSQLI_ASSOC);
|
|
}
|
|
|
|
function transferJsonFiles($file_name, $data) {
|
|
// Clear the contents of the JSON file
|
|
file_put_contents('transferJsonFiles/' . $file_name . '.json', '');
|
|
|
|
// Convert the data array to JSON
|
|
$jsonData = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
|
|
|
// Write the JSON data to the file
|
|
file_put_contents('transferJsonFiles/' . $file_name . '.json', $jsonData);
|
|
}
|
|
/*
|
|
|
|
$commercialRegisterData = fetch(<<<SQL
|
|
SELECT
|
|
id,
|
|
name as name,
|
|
vat_number as vat_number
|
|
FROM commercial_register
|
|
SQL);
|
|
|
|
transferJsonFiles('departments', $commercialRegisterData);
|
|
die;
|
|
|
|
// ------------------------------
|
|
|
|
$branchData = fetch(<<<SQL
|
|
SELECT
|
|
id,
|
|
branch_name as name,
|
|
pu_name as public_name,
|
|
commercial_register_number_700 as commercial_registration_number,
|
|
commercial_register_number_10 as commercial_registration_number_v2,
|
|
CASE activation_id
|
|
WHEN 1 THEN false
|
|
WHEN 2 THEN true
|
|
END as is_hidden, -- 1 = inactive, 2 = active
|
|
commercial_register_id as department_id,
|
|
date_time as created_at
|
|
-- floor_count
|
|
-- invoice_code
|
|
FROM branch
|
|
SQL);
|
|
|
|
transferJsonFiles('branches', $branchData);
|
|
die;
|
|
|
|
// ------------------------------
|
|
|
|
$occupationData = fetch(<<<SQL
|
|
SELECT
|
|
id,
|
|
name,
|
|
date_time as created_at,
|
|
date_time as updated_at
|
|
FROM occupation
|
|
SQL);
|
|
|
|
transferJsonFiles('groups', $occupationData);
|
|
die;
|
|
|
|
// ------------------------------
|
|
|
|
$usersData = fetch(<<<SQL
|
|
SELECT
|
|
user.id,
|
|
user.name as nickname,
|
|
user.username as username,
|
|
user.pass as password, -- md5
|
|
user.number as phone_number,
|
|
CASE user.emp_active_status_id
|
|
WHEN 1 THEN 'pending'
|
|
WHEN 2 THEN 'active'
|
|
WHEN 3 THEN 'blocked'
|
|
END as active, -- 1 = wait, 2 = active 3 = inactive
|
|
user.create_time as created_at,
|
|
user.date_time as updated_at,
|
|
1 as must_update_password,
|
|
emplyee.occupation_id as group_id
|
|
FROM user
|
|
LEFT JOIN emplyee ON emplyee.id = user.emplyee_id
|
|
SQL);
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('users', $usersData);
|
|
die;
|
|
// ------------------------------
|
|
|
|
$employeeData = fetch(<<<SQL
|
|
SELECT
|
|
emplyee.id as id,
|
|
emplyee.name as nickname,
|
|
emplyee.emp_id as national_id,
|
|
4000 as salary,
|
|
emplyee.branch_id as branch_id,
|
|
emplyee.occupation_id as group_id,
|
|
CASE emplyee.G_sex_id
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 0
|
|
WHEN 3 THEN 1
|
|
END as gender,
|
|
8 as working_hours,
|
|
emplyee.date_time as created_at,
|
|
emplyee.date_time as updated_at,
|
|
user.id as user_id,
|
|
CASE emplyee.activation_id
|
|
WHEN 0 THEN 0
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as is_shown_to_employees,
|
|
CASE emplyee.activation_id
|
|
WHEN 0 THEN 0
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as is_shown_to_clients
|
|
FROM emplyee
|
|
LEFT JOIN user ON user.emplyee_id = emplyee.id
|
|
WHERE emplyee.date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('employees', $employeeData);
|
|
die;
|
|
|
|
// ------------------------------
|
|
|
|
$employeeServices = fetch(<<<SQL
|
|
SELECT
|
|
emplyee_id as employee_id,
|
|
services_id as service_id,
|
|
date_time as created_at
|
|
FROM emplyee_has_services
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('employee_services', $employeeServices);
|
|
die;
|
|
|
|
|
|
// ------------------------------
|
|
|
|
$anEmployeeOfferData = fetch(<<<SQL
|
|
SELECT
|
|
id,
|
|
name,
|
|
CASE type_discount_id
|
|
WHEN 1 THEN 'amount'
|
|
WHEN 2 THEN 'amount'
|
|
WHEN 3 THEN 'percentage'
|
|
END as discount_type,
|
|
discount as discount_value,
|
|
date_time as created_at,
|
|
create_time as updated_at,
|
|
CASE activation_id
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as is_discount_active,
|
|
CASE does_discount_enters_id
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as requires_national_id
|
|
FROM an_employee_offer
|
|
WHERE date_time > '2024-12-14 00:00:00' OR create_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('partners', $anEmployeeOfferData);
|
|
die;
|
|
|
|
// ------------------------------
|
|
|
|
$howToHearData = fetch(<<<SQL
|
|
SELECT
|
|
id,
|
|
name,
|
|
0 as is_shown_to_clients,
|
|
CASE activation_id
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as is_shown_to_employees,
|
|
0 as priority,
|
|
date_time as created_at
|
|
FROM how_to_hear
|
|
SQL);
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('referrals', $howToHearData);
|
|
die;
|
|
|
|
// ------------------------------
|
|
|
|
$tretmentData = fetch(<<<SQL
|
|
SELECT
|
|
tretment.id,
|
|
tretment.name as nickname,
|
|
tretment.number as phone_number,
|
|
tretment.id_number as national_id,
|
|
CASE tretment.G_sex_id
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 0
|
|
WHEN 3 THEN 1
|
|
END as gender,
|
|
tretment.age,
|
|
tretment.date_time as created_at,
|
|
tretment.an_employee_offer_id as partner_id,
|
|
CASE tretment.black_list
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as is_blacklisted, -- 1 = not blacklisted, 2 = not blacklisted
|
|
tretment.id_number as national_id,
|
|
tretment.commercial_register_id as department_id,
|
|
clients.first_name as first_name,
|
|
clients.last_name as last_name,
|
|
clients.password as password,
|
|
DATE(clients.birthdate) as birthdate
|
|
FROM tretment
|
|
LEFT JOIN clients ON clients.tretment_id = tretment.id
|
|
WHERE tretment.date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('clients', $tretmentData);
|
|
die;
|
|
|
|
// ------------------------------
|
|
|
|
$tretmentHasHowToHearData = fetch(<<<SQL
|
|
SELECT
|
|
tretment_id as client_id,
|
|
how_to_hear_id as referral_id,
|
|
date_time as created_at
|
|
FROM tretment_has_how_to_hear
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('client_referrals', $tretmentHasHowToHearData);
|
|
die;
|
|
|
|
// ------------------------------
|
|
|
|
$serviceTypeData = fetch(<<<SQL
|
|
SELECT
|
|
id,
|
|
name as name,
|
|
date_time as created_at
|
|
FROM type_of_services
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('service_categories', $serviceTypeData);
|
|
die;
|
|
// ------------------------------
|
|
|
|
$serviceData = fetch(<<<SQL
|
|
SELECT
|
|
id,
|
|
'primary' as type,
|
|
minimum as minimum_time,
|
|
maxmum as maximum_time,
|
|
CASE activation_id
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as is_buyable_by_employees,
|
|
0 as is_buyable_by_clients,
|
|
type_of_services_id as category_id,
|
|
commercial_register_id as department_id,
|
|
name,
|
|
name as description,
|
|
price as price,
|
|
date_time as created_at,
|
|
point as points,
|
|
15 as point_multiplier
|
|
FROM services
|
|
where id <> 0 AND date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
// // Transfer the data to a JSON file
|
|
// transferJsonFiles('primary_services', $serviceData);
|
|
// die;
|
|
|
|
// ------------------------------
|
|
|
|
$additionalServicesData = fetch(<<<SQL
|
|
SELECT
|
|
id,
|
|
'additional' as type,
|
|
CASE activation_id
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as is_buyable_by_employees,
|
|
0 as is_buyable_by_clients,
|
|
commercial_register_id as department_id,
|
|
name,
|
|
name as description,
|
|
price as price,
|
|
date_time as created_at,
|
|
30 as minimum_time,
|
|
30 as maximum_time,
|
|
1 as category_id,
|
|
0 as points,
|
|
0 as point_multiplier
|
|
FROM add_on_product
|
|
where id <> 0 AND date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$serviceData = array_merge($serviceData, $additionalServicesData);
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('services', $serviceData);
|
|
die;
|
|
|
|
// ------------------------------
|
|
|
|
$reservationDataCollect_1 = fetch(<<<SQL
|
|
SELECT
|
|
id,
|
|
id as number,
|
|
tretment_id as client_id,
|
|
user_id as user_id,
|
|
date_time as created_at,
|
|
user_id as user_id,
|
|
notes as note
|
|
-- branch_id,
|
|
-- driver_id,
|
|
-- deleted_at,
|
|
FROM book
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$reservationDataCollect_2 = fetch(<<<SQL
|
|
SELECT
|
|
id as reservation_detail_id,
|
|
book_id as reservation_id,
|
|
day_date as date,
|
|
day_time as time,
|
|
services_id as service_id,
|
|
branch_id as branch_id,
|
|
emplyee_id as employee_id,
|
|
status_id as status_id,
|
|
user_id as user_id,
|
|
date_time as created_at
|
|
FROM book_details
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
ORDER BY id ASC
|
|
SQL);
|
|
|
|
$reservationDataCollect_3 = fetch(<<<SQL
|
|
SELECT
|
|
book_details_id as reservation_detail_id,
|
|
bundle_services_id as service_id,
|
|
user_id as user_id,
|
|
date_time as created_at
|
|
FROM book_details_has_bundle_services
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$reservationDataCollect_4 = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
name as name,
|
|
tretment_id as client_id,
|
|
emplyee_id as employee_id,
|
|
user_id as user_id,
|
|
date_time as created_at
|
|
FROM bundle_services
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$reservationDataCollect_5 = fetch(<<<SQL
|
|
SELECT
|
|
bundle_services_id as bundle_services_id,
|
|
services_id as service_id,
|
|
how_many as quantity,
|
|
user_id as user_id,
|
|
date_time as created_at
|
|
FROM bundle_services_has_services
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$reservationDataCollect_6 = fetch(<<<SQL
|
|
SELECT
|
|
book_id as reservation_id,
|
|
emplyee_id as driver_id,
|
|
services_id as service_id,
|
|
user_id as user_id,
|
|
date_time as created_at
|
|
FROM book_has_driver_for_points
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$reservationData = [];
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('reservations', $reservationDataCollect_1);
|
|
transferJsonFiles('reservation_details', $reservationDataCollect_2);
|
|
transferJsonFiles('reservation_bundle_services', $reservationDataCollect_3);
|
|
transferJsonFiles('reservation_bundles', $reservationDataCollect_4);
|
|
transferJsonFiles('reservation_bundle_services_services', $reservationDataCollect_5);
|
|
transferJsonFiles('reservation_drivers', $reservationDataCollect_6);
|
|
die;
|
|
|
|
// ------------------------------
|
|
|
|
$invoiceDescountTables = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
name as name,
|
|
date_time as created_at
|
|
FROM db_tables
|
|
SQL);
|
|
|
|
$invoiceDataCollect_1 = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
number,
|
|
number_style,
|
|
notes,
|
|
user_id,
|
|
invoices_page_id as page_id, -- 1 = static, 2 = free, 3 = packages, 4 = add money, 5 = no vat
|
|
date_time as created_at
|
|
FROM invoices
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
transferJsonFiles('invoices', $invoiceDataCollect_1);
|
|
die;
|
|
|
|
$invoiceDataCollect_2 = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
invoices_id as invoice_id,
|
|
invoices_status_id as status_id, -- 1 = new, 2 = credit note, 3 = debit note, 4 = new no vat
|
|
branch_id as branch_id,
|
|
date_time as created_at
|
|
FROM invoices_details
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
ORDER BY id ASC
|
|
SQL);
|
|
|
|
$invoiceDataCollect_3 = fetch(<<<SQL
|
|
SELECT
|
|
invoices_details_id as invoice_detail_id,
|
|
how_pay_id as payment_method_id, -- 1 = cash, 2 = card, 3 = wallet serv, 4 = wallet money, 5 = refund, 6 = to bank (ساب), 7 = to bank (الراجحي), 8 = to bank (الاخلي), 9 = to bank (الانماء), 10 = online clickpay
|
|
pay as amount,
|
|
date_time as created_at
|
|
FROM invoices_details_has_how_pay
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$invoiceDataCollect_4 = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
discount as total_discount,
|
|
invoices_details_id as invoice_detail_id,
|
|
date_time as created_at
|
|
FROM invoices_discount
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$invoiceDataCollect_5 = fetch(<<<SQL
|
|
SELECT -- must group by reson and service
|
|
id as id,
|
|
discount as discount,
|
|
reason as reason,
|
|
invoices_discount_id as invoices_discount_id,
|
|
services_id as service_id,
|
|
date_time as created_at
|
|
FROM invoices_discount_details
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$invoiceDataCollect_6 = fetch(<<<SQL
|
|
SELECT
|
|
invoices_discount_details_id as discount_detail_id,
|
|
db_tables_id as db_table_id,
|
|
db_tables_id_to_id as id_from_table_name,
|
|
date_time as created_at
|
|
FROM invoices_discount_details_has_db_tables
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$invoiceDataCollect_7 = fetch(<<<SQL
|
|
SELECT
|
|
invoices_id as invoice_id,
|
|
book_id as book_id,
|
|
date_time as created_at
|
|
FROM invoices_has_book
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$incoiceDataCollect_8 = fetch(<<<SQL
|
|
SELECT
|
|
id,
|
|
`add` as total_amount,
|
|
invoices_details_id as invoice_detail_id,
|
|
date_time as created_at
|
|
FROM invoices_add
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$incoiceDataCollect_9 = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
`add` as amount,
|
|
services_id as service_id,
|
|
reason as reason,
|
|
invoices_add_id as invoice_add_id,
|
|
date_time as created_at
|
|
FROM invoices_add_details
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$incoiceDataCollect_10 = fetch(<<<SQL
|
|
SELECT
|
|
invoices_add_details_id as add_detail_id,
|
|
db_tables_id as db_table_id,
|
|
db_tables_id_to_id as id_from_table_name,
|
|
date_time as created_at
|
|
FROM invoices_add_details_has_db_tables
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$productsData = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
name as name,
|
|
price as price,
|
|
commercial_register_id as department_id,
|
|
date_time as created_at
|
|
FROM products
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$incoiceDataCollect_11 = fetch(<<<SQL
|
|
SELECT -- just if it juice product J stands for juice in invoice
|
|
invoices_details_id as invoice_detail_id,
|
|
products_id as product_id,
|
|
how_many as quantity,
|
|
date_time as created_at
|
|
FROM invoices_details_has_products
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$invoiceData = [];
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('invoice_discount_tables', $invoiceDescountTables);
|
|
transferJsonFiles('invoices', $invoiceDataCollect_1);
|
|
transferJsonFiles('invoice_details', $invoiceDataCollect_2);
|
|
transferJsonFiles('invoice_payments', $invoiceDataCollect_3);
|
|
transferJsonFiles('invoice_discounts', $invoiceDataCollect_4);
|
|
transferJsonFiles('invoice_discount_details', $invoiceDataCollect_5);
|
|
transferJsonFiles('invoice_discount_details_tables', $invoiceDataCollect_6);
|
|
transferJsonFiles('invoice_book', $invoiceDataCollect_7);
|
|
transferJsonFiles('invoice_additions', $incoiceDataCollect_8);
|
|
transferJsonFiles('invoice_addition_details', $incoiceDataCollect_9);
|
|
transferJsonFiles('invoice_addition_details_tables', $incoiceDataCollect_10);
|
|
transferJsonFiles('products', $productsData);
|
|
transferJsonFiles('invoice_products', $incoiceDataCollect_11);
|
|
die;
|
|
|
|
// ------------------------------
|
|
|
|
$pointDataCollect_1 = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
tretment_id as client_id,
|
|
point as points,
|
|
date_time as created_at
|
|
FROM points
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$pointDataCollect_2 = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
point as points,
|
|
tretment_id as client_id,
|
|
invoices_id as invoice_id,
|
|
date_time as created_at
|
|
FROM points_with_invoices
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$wallaReasonsData = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
reason as reason,
|
|
points as points,
|
|
date_time as created_at
|
|
FROM walla_reasons
|
|
SQL);
|
|
|
|
$wallaPointDataCollect_1 = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
points as points,
|
|
tretment_id as client_id,
|
|
walla_reasons_id as reason_id,
|
|
date_time as created_at
|
|
FROM walla_points
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('points', $pointDataCollect_1);
|
|
transferJsonFiles('points_with_invoices', $pointDataCollect_2);
|
|
transferJsonFiles('walla_reasons', $wallaReasonsData);
|
|
transferJsonFiles('walla_points', $wallaPointDataCollect_1);
|
|
die;
|
|
|
|
// ------------------------------
|
|
|
|
$packagesData = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
name as name,
|
|
cu_price as current_price,
|
|
price as price, -- after discount
|
|
commercial_register_id as department_id,
|
|
case activation_id
|
|
when 1 then 0
|
|
when 2 then 1
|
|
end as is_active,
|
|
date_time as created_at
|
|
FROM packages
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$packagesHasServicesData = fetch(<<<SQL
|
|
SELECT
|
|
packages_id as package_id,
|
|
services_id as service_id,
|
|
how_many as quantity,
|
|
date_time as created_at
|
|
FROM packages_has_services
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('packages', $packagesData);
|
|
transferJsonFiles('package_services', $packagesHasServicesData);
|
|
die;
|
|
// ------------------------------
|
|
|
|
$bondsData = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
number as number,
|
|
number_style as style,
|
|
tretment_id as client_id,
|
|
bonds_type_id as type_id, -- 1 = give, 2 = take
|
|
bonds_type_type_id as type_type_id, -- 2 = money, 3 = package, 4 = service
|
|
date_time as created_at,
|
|
user_id
|
|
FROM bonds
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
AND date_time < '2025-01-31 00:00:00'
|
|
SQL);
|
|
|
|
$bondsDetailsData = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
bonds_id as bond_id,
|
|
bonds_status_id as status_id, -- 1 = new, 2 = change it to increse, 3 = cancel it, 4 = expired
|
|
branch_id as branch_id,
|
|
date_time as created_at
|
|
FROM bonds_details
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
AND date_time < '2025-01-31 00:00:00'
|
|
SQL);
|
|
|
|
$bondsDetailsPaymentsData = fetch(<<<SQL
|
|
SELECT
|
|
bonds_details_id as bond_detail_id,
|
|
how_pay_id as payment_method_id, -- 1 = cash, 2 = card, 3 = wallet serv, 4 = wallet money, 5 = refund, 6 = to bank (ساب), 7 = to bank (الراجحي), 8 = to bank (الاخلي), 9 = to bank (الانماء), 10 = online clickpay
|
|
pay as amount,
|
|
date_time as created_at
|
|
FROM bonds_details_has_how_pay
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
AND date_time < '2025-01-31 00:00:00'
|
|
SQL);
|
|
|
|
$bondsDetailsPackagesData = fetch(<<<SQL
|
|
SELECT -- if type_type_id is package come here
|
|
bonds_details_id as bond_detail_id,
|
|
packages_id as package_id,
|
|
date_time as created_at
|
|
FROM bonds_details_has_packages
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
AND date_time < '2025-01-31 00:00:00'
|
|
SQL);
|
|
|
|
$bondsDetailsServicesData = fetch(<<<SQL
|
|
SELECT -- if type_type_id is service come here
|
|
discount as discount,
|
|
type_discount_id as type_discount_id, -- 2 = amount, 3 = %
|
|
bonds_details_id as bond_detail_id,
|
|
services_id as service_id,
|
|
date_time as created_at
|
|
FROM bonds_details_has_services
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
AND date_time < '2025-01-31 00:00:00'
|
|
SQL);
|
|
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('bonds', $bondsData);
|
|
transferJsonFiles('bond_details', $bondsDetailsData);
|
|
transferJsonFiles('bond_payments', $bondsDetailsPaymentsData);
|
|
transferJsonFiles('bond_packages', $bondsDetailsPackagesData);
|
|
transferJsonFiles('bond_services', $bondsDetailsServicesData);
|
|
die;
|
|
|
|
// ------------------------------
|
|
|
|
$tretmentHasServicesData = fetch(<<<SQL
|
|
SELECT
|
|
tretment_id as client_id,
|
|
services_id as service_id,
|
|
done_id as is_done, -- 1 = not done, 2 = done, 3 = canceled, 4 = rejected, 5 = waiting to aproval, 6 = removed
|
|
inv_table_id as bonds_id,
|
|
book_id as reservation_id,
|
|
Case activation_id
|
|
when 1 then 0
|
|
when 2 then 1
|
|
end as is_active,
|
|
date_time as created_at
|
|
FROM tretment_has_service
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$tretmentHasFreeServicesData = fetch(<<<SQL
|
|
SELECT
|
|
tretment_id as client_id,
|
|
services_id as service_id,
|
|
done_id as is_done, -- 1 = not done, 2 = done, 3 = canceled, 4 = rejected, 5 = waiting to aproval, 6 = removed
|
|
db_tables_id as db_table_id,
|
|
table_id as table_to_id,
|
|
book_id as reservation_id,
|
|
Case activation_id
|
|
when 1 then 0
|
|
when 2 then 1
|
|
end as is_active,
|
|
date_time as created_at
|
|
FROM tretment_has_free_service
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
|
|
$tretmentHasMoneyData = fetch(<<<SQL
|
|
SELECT
|
|
tretment_id as client_id,
|
|
bonds_id as bond_id,
|
|
price as amount,
|
|
date_time as created_at
|
|
FROM tretment_has_money
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$tretmentHasFreeMoneyData = fetch(<<<SQL
|
|
SELECT
|
|
tretment_id as client_id,
|
|
price as amount,
|
|
date_time as created_at
|
|
FROM tretment_has_free_money
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$couponsData = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
name as name,
|
|
discount as discount,
|
|
type_discount_id as type_discount_id, -- 2 = amount, 3 = %
|
|
Case activation_id
|
|
when 1 then 0
|
|
when 2 then 1
|
|
end as is_active,
|
|
date_time as created_at
|
|
FROM coupons
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$tretmentHasCouponsData = fetch(<<<SQL
|
|
SELECT
|
|
tretment_id as client_id,
|
|
coupons_id as coupon_id,
|
|
code as code,
|
|
CASE activation_id
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as is_active,
|
|
date_time as created_at
|
|
FROM tretment_has_coupons
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$tretmentHasFree3ServCouponData = fetch(<<<SQL
|
|
SELECT
|
|
tretment_id as client_id,
|
|
status as status, -- 1 = has coupon
|
|
code as code,
|
|
CASE activation_id
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as is_active
|
|
FROM tretment_has_free_3_serv_coupon
|
|
SQL);
|
|
|
|
$tretmentUseMoneyData = fetch(<<<SQL
|
|
SELECT
|
|
invoices_details_has_how_pay_invoices_details_id as invoice_detail_id,
|
|
price as amount,
|
|
tretment_id as client_id,
|
|
date_time as created_at
|
|
FROM tretment_use_money
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$coustomDiscountData = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
reason as reason,
|
|
tretment_id as client_id,
|
|
date_time as created_at
|
|
FROM custom_discount
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$coustomDiscountHasServicesData = fetch(<<<SQL
|
|
SELECT
|
|
custom_discount_id as custom_discount_id,
|
|
services_id as service_id,
|
|
how_many as quantity,
|
|
discount as discount,
|
|
type_discount_id as type_discount_id, -- 2 = amount, 3 = %
|
|
CASE activation_id
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as is_active,
|
|
date_time as created_at
|
|
FROM custom_discount_has_services
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$tretmentHasCustomDiscountData = fetch(<<<SQL
|
|
SELECT
|
|
tretment_id as client_id,
|
|
discount as discount,
|
|
services_id as service_id,
|
|
inv_table_id as custom_discout_id,
|
|
type_discount_id as type_discount_id, -- 2 = amount, 3 = %
|
|
done_id as is_done, -- 1 = not done, 2 = done, 3 = canceled, 4 = rejected, 5 = waiting to aproval, 6 = removed
|
|
date_time as created_at
|
|
FROM tretment_has_custom_discount
|
|
WHERE date_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('client_services', $tretmentHasServicesData);
|
|
transferJsonFiles('client_free_services', $tretmentHasFreeServicesData);
|
|
transferJsonFiles('client_money', $tretmentHasMoneyData);
|
|
transferJsonFiles('client_free_money', $tretmentHasFreeMoneyData);
|
|
transferJsonFiles('coupons', $couponsData);
|
|
transferJsonFiles('client_coupons', $tretmentHasCouponsData);
|
|
transferJsonFiles('client_free_service_coupon', $tretmentHasFree3ServCouponData);
|
|
transferJsonFiles('client_use_money', $tretmentUseMoneyData);
|
|
transferJsonFiles('custom_discounts', $coustomDiscountData);
|
|
transferJsonFiles('custom_discount_services', $coustomDiscountHasServicesData);
|
|
transferJsonFiles('client_custom_discounts', $tretmentHasCustomDiscountData);
|
|
*/
|
|
|
|
/*
|
|
die;
|
|
*/
|
|
|
|
/*
|
|
$start = '2025-01-01 03:00:00';
|
|
$end = '2026-02-20 03:00:00';
|
|
$packagesData = fetch(<<<SQL
|
|
SELECT
|
|
id as id,
|
|
name as name,
|
|
cu_price as current_price,
|
|
price as price, -- after discount
|
|
commercial_register_id as department_id,
|
|
case activation_id
|
|
when 1 then 0
|
|
when 2 then 1
|
|
end as is_active,
|
|
create_time as created_at
|
|
FROM packages
|
|
WHERE create_time > '2024-12-14 00:00:00'
|
|
SQL);
|
|
|
|
$packagesHasServicesData = fetch(<<<SQL
|
|
SELECT
|
|
packages_id as package_id,
|
|
services_id as service_id,
|
|
how_many as quantity,
|
|
create_time as created_at
|
|
FROM packages_has_services
|
|
WHERE create_time >= '2025-02-15 00:00:00'
|
|
SQL);
|
|
|
|
// Transfer the data to a JSON file
|
|
transferJsonFiles('packages', $packagesData);
|
|
transferJsonFiles('package_services', $packagesHasServicesData);
|
|
die;
|
|
|
|
*/
|
|
|
|
$start = '2024-01-01 03:00:00';
|
|
$end = '2025-01-01 03:00:00';
|
|
|
|
$usedClientServices = fetch(<<<SQL
|
|
SELECT
|
|
tretment_id as client_id,
|
|
book_id as reservation_id,
|
|
services_id as service_id,
|
|
CASE done_id
|
|
WHEN 1 THEN 'pending'
|
|
WHEN 2 THEN 'done'
|
|
WHEN 3 THEN 'cancelled'
|
|
WHEN 4 THEN 'cancelled'
|
|
WHEN 5 THEN 'pending'
|
|
WHEN 6 THEN 'cancelled'
|
|
END as status,
|
|
create_time as created_at
|
|
FROM tretment_has_service
|
|
WHERE
|
|
create_time BETWEEN ? AND ? AND
|
|
activation_id = 2
|
|
SQL, [$start, $end]);
|
|
|
|
transferJsonFiles('client_service_statuses', $usedClientServices);
|
|
print "Client service statuses done!\n";
|
|
|
|
$reservations = fetch(<<<SQL
|
|
SELECT
|
|
book.id as id,
|
|
book.tretment_id as client_id,
|
|
book.user_id as user_id,
|
|
book.create_time as created_at,
|
|
book.notes as notes,
|
|
dd.day_date as date,
|
|
dd.day_time as time,
|
|
dd.branch_id as branch_id,
|
|
dd.emplyee_id as employee_id,
|
|
dd.status_id as status_id,
|
|
dd.services_id as service_id,
|
|
dd.id as detail_id,
|
|
invoices_has_book.invoices_id as invoice_id
|
|
FROM book
|
|
INNER JOIN book_details AS dd ON dd.book_id = book.id
|
|
LEFT JOIN invoices_has_book ON invoices_has_book.book_id = book.id
|
|
WHERE
|
|
book.activation_id = 2 AND
|
|
book.create_time BETWEEN ? AND ? AND
|
|
dd.activation_id = 2 AND
|
|
dd.id IN (SELECT MAX(book_details.id)
|
|
FROM book_details
|
|
WHERE book_id = dd.book_id
|
|
GROUP BY book_id) AND
|
|
dd.status_id IN (4, 6, 8, 10, 12, 13, 14, 15, 16, 19, 20, 21, 22, 25, 26)
|
|
SQL, [$start, $end]);
|
|
|
|
$reservationData = [];
|
|
|
|
foreach ($reservations as $reservation) {
|
|
$services = [];
|
|
if ($reservation['service_id'] == 0) {
|
|
$bundle = fetch(<<<SQL
|
|
SELECT
|
|
'primary' as type,
|
|
services.id as id,
|
|
bundle_services_has_services.how_many as quantity
|
|
FROM
|
|
book_details_has_bundle_services as bhs
|
|
INNER JOIN
|
|
bundle_services ON bundle_services.id = bhs.bundle_services_id
|
|
INNER JOIN
|
|
bundle_services_has_services ON bundle_services_has_services.bundle_services_id = bundle_services.id
|
|
INNER JOIN
|
|
services ON services.id = bundle_services_has_services.services_id
|
|
WHERE
|
|
bhs.book_details_id = ?
|
|
SQL, [$reservation['detail_id']]);
|
|
if (empty($bundle)) {
|
|
$bundle = fetch(<<<SQL
|
|
SELECT
|
|
'primary' as type,
|
|
services.id as id,
|
|
bundle_services_has_services.how_many as quantity
|
|
FROM
|
|
book_details
|
|
INNER JOIN
|
|
book_details_has_bundle_services as bhs ON bhs.book_details_id = book_details.id
|
|
INNER JOIN
|
|
bundle_services ON bundle_services.id = bhs.bundle_services_id
|
|
INNER JOIN
|
|
bundle_services_has_services ON bundle_services_has_services.bundle_services_id = bundle_services.id
|
|
INNER JOIN
|
|
services ON services.id = bundle_services_has_services.services_id
|
|
WHERE
|
|
book_details.book_id = ?
|
|
SQL, [$reservation['id']]);
|
|
}
|
|
|
|
if (empty($bundle)) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($bundle as $service) {
|
|
for ($i = 0; $i < $service['quantity']; $i++) {
|
|
$services[] = [
|
|
'id' => $service['id'],
|
|
'type' => 'primary'
|
|
];
|
|
}
|
|
}
|
|
} else {
|
|
$services = fetch(<<<SQL
|
|
SELECT
|
|
services.id as id
|
|
FROM
|
|
book_details as dd
|
|
INNER JOIN
|
|
services ON services.id = dd.services_id
|
|
WHERE
|
|
dd.id = ?
|
|
SQL, [$reservation['detail_id']]);
|
|
}
|
|
|
|
$additionalServices = fetch(<<<SQL
|
|
SELECT
|
|
"additional" as type,
|
|
add_on_product.id as id
|
|
FROM
|
|
book_has_add_on_product as bhap
|
|
INNER JOIN
|
|
add_on_product ON add_on_product.id = bhap.add_on_product_id
|
|
WHERE
|
|
bhap.book_id = ?
|
|
SQL, [$reservation['id']]);
|
|
|
|
$driver = fetch(<<<SQL
|
|
SELECT
|
|
emplyee_id as employee_id
|
|
FROM
|
|
book_has_driver_for_points
|
|
WHERE
|
|
book_id = ?
|
|
SQL, [$reservation['id']]);
|
|
|
|
$services = array_merge($services, $additionalServices);
|
|
$reservation['services'] = $services;
|
|
|
|
if (empty($driver)) {
|
|
$reservation['driver_id'] = null;
|
|
} else {
|
|
$reservation['driver_id'] = $driver[0]['employee_id'];
|
|
}
|
|
|
|
unset($reservation['service_id']);
|
|
unset($reservation['detail_id']);
|
|
|
|
$reservationData[] = $reservation;
|
|
}
|
|
|
|
transferJsonFiles('reservations_new', $reservationData);
|
|
print "Reservations done!\n";
|
|
|
|
$invoices = fetch(<<<SQL
|
|
SELECT
|
|
'invoice' as type,
|
|
invoices.id as id,
|
|
invoices.number as number,
|
|
invoices.user_id as user_id,
|
|
invoices.number_style as number_style,
|
|
invoices.create_time as created_at
|
|
FROM invoices
|
|
WHERE
|
|
invoices.create_time BETWEEN ? AND ? AND
|
|
invoices.activation_id = 2
|
|
SQL, [$start, $end]);
|
|
|
|
$count = count($invoices);
|
|
$lessCount = [];
|
|
$result = [];
|
|
for ($index = 0; $index < $count; ++$index) {
|
|
$invoice = &$invoices[$index];
|
|
$invoiceReservations = fetch(<<<SQL
|
|
SELECT
|
|
book.id as id,
|
|
book.tretment_id as client_id,
|
|
book.user_id as user_id,
|
|
dd.id as detail_id,
|
|
dd.services_id as service_id,
|
|
dd.branch_id as branch_id
|
|
FROM invoices_has_book
|
|
INNER JOIN book ON book.id = invoices_has_book.book_id
|
|
INNER JOIN book_details AS dd ON dd.book_id = book.id
|
|
WHERE
|
|
invoices_has_book.invoices_id = ? AND
|
|
dd.id IN (SELECT MAX(book_details.id)
|
|
FROM book_details
|
|
WHERE book_id = book.id
|
|
GROUP BY book_id) AND
|
|
dd.status_id IN (4, 6, 8, 10, 12, 13, 14, 15, 16, 19, 20, 21, 22, 25, 26)
|
|
SQL, [$invoice['id']]);
|
|
|
|
$invoice['branch_id'] = $invoiceReservations[0]['branch_id'];
|
|
|
|
$services = [];
|
|
foreach ($invoiceReservations as $res) {
|
|
if ($res['service_id'] === 0) {
|
|
$bundle = fetch(<<<SQL
|
|
SELECT
|
|
'primary' as type,
|
|
services.id as id,
|
|
services.name as name,
|
|
services.price as price,
|
|
bundle_services_has_services.how_many as quantity
|
|
FROM
|
|
book_details_has_bundle_services as bhs
|
|
INNER JOIN
|
|
bundle_services ON bundle_services.id = bhs.bundle_services_id
|
|
INNER JOIN
|
|
bundle_services_has_services ON bundle_services_has_services.bundle_services_id = bundle_services.id
|
|
INNER JOIN
|
|
services ON services.id = bundle_services_has_services.services_id
|
|
WHERE
|
|
bhs.book_details_id = ?
|
|
SQL, [$res['detail_id']]);
|
|
if (empty($bundle)) {
|
|
$bundle = fetch(<<<SQL
|
|
SELECT
|
|
'primary' as type,
|
|
services.id as id,
|
|
services.name as name,
|
|
services.price as price,
|
|
bundle_services_has_services.how_many as quantity
|
|
FROM
|
|
book_details
|
|
INNER JOIN
|
|
book_details_has_bundle_services as bhs ON bhs.book_details_id = book_details.id
|
|
INNER JOIN
|
|
bundle_services ON bundle_services.id = bhs.bundle_services_id
|
|
INNER JOIN
|
|
bundle_services_has_services ON bundle_services_has_services.bundle_services_id = bundle_services.id
|
|
INNER JOIN
|
|
services ON services.id = bundle_services_has_services.services_id
|
|
WHERE
|
|
book_details.book_id = ?
|
|
SQL, [$res['id']]);
|
|
}
|
|
|
|
if (empty($bundle)) {
|
|
dd("empty bundle!");
|
|
continue;
|
|
}
|
|
|
|
foreach ($bundle as $service) {
|
|
for ($i = 0; $i < $service['quantity']; $i++) {
|
|
$services[] = [
|
|
'type' => $service['type'],
|
|
'id' => $service['id'],
|
|
'name' => $service['name'],
|
|
'price' => $service['price']
|
|
];
|
|
}
|
|
}
|
|
} else {
|
|
$ss = fetch(<<<SQL
|
|
SELECT
|
|
'primary' as type,
|
|
services.id as id,
|
|
services.name as name,
|
|
services.price as price
|
|
FROM
|
|
book_details as dd
|
|
INNER JOIN
|
|
services ON services.id = dd.services_id
|
|
WHERE
|
|
dd.id = ?
|
|
SQL, [$res['detail_id']]);
|
|
|
|
foreach ($ss as $s) {
|
|
$services[] = $s;
|
|
}
|
|
}
|
|
}
|
|
|
|
$additionalServices = fetch(<<<SQL
|
|
SELECT
|
|
'additional' as type,
|
|
add_on_product.id as id,
|
|
add_on_product.name as name,
|
|
add_on_product.price as price
|
|
FROM
|
|
book_has_add_on_product as bhap
|
|
INNER JOIN
|
|
add_on_product ON add_on_product.id = bhap.add_on_product_id
|
|
WHERE
|
|
bhap.book_id = ?
|
|
SQL, [$res['id']]);
|
|
|
|
$items = array_merge($services, $additionalServices);
|
|
$invoice['items'] = $items;
|
|
|
|
$serviceSum = 0;
|
|
|
|
foreach ($items as $it) {
|
|
$serviceSum += round($it['price'] * 1.15, 2);
|
|
}
|
|
|
|
$payments = fetch(<<<SQL
|
|
SELECT
|
|
invoices_details_has_how_pay.pay as amount,
|
|
'prepaid' as type,
|
|
CASE how_pay.id
|
|
WHEN 1 THEN 'cash'
|
|
WHEN 2 THEN 'card'
|
|
WHEN 3 THEN 'client_service'
|
|
WHEN 4 THEN 'wallet'
|
|
WHEN 5 THEN 'transfer'
|
|
WHEN 6 THEN 'transfer'
|
|
WHEN 7 THEN 'transfer'
|
|
WHEN 8 THEN 'transfer'
|
|
WHEN 9 THEN 'transfer'
|
|
ELSE 'other'
|
|
END as method,
|
|
invoices_details.notes as notes
|
|
FROM invoices_details
|
|
INNER JOIN invoices_details_has_how_pay ON invoices_details.id = invoices_details_has_how_pay.invoices_details_id
|
|
INNER JOIN how_pay ON how_pay.id = invoices_details_has_how_pay.how_pay_id
|
|
WHERE
|
|
invoices_details.invoices_id = ? AND
|
|
invoices_details.invoices_status_id IN (1, 2)
|
|
GROUP BY how_pay.id
|
|
SQL, [$invoice['id']]);
|
|
|
|
$hasCreditNote = fetch(<<<SQL
|
|
SELECT EXISTS(
|
|
SELECT *
|
|
FROM invoices_details
|
|
WHERE invoices_id = ? AND invoices_status_id = 3
|
|
) AS result
|
|
SQL, [$invoice['id']]);
|
|
|
|
if ($hasCreditNote[0]['result'] == 1) {
|
|
$invoice['has_credit_note'] = true;
|
|
} else {
|
|
$invoice['has_credit_note'] = false;
|
|
}
|
|
|
|
$discounts = fetch(<<<SQL
|
|
SELECT
|
|
invoices_discount.discount as discount,
|
|
db_tables.name as discount_reason,
|
|
invoices_discount_details_has_db_tables.db_tables_id_to_id as discount_reason_id
|
|
FROM invoices_discount
|
|
INNER JOIN invoices_details ON invoices_discount.invoices_details_id = invoices_details.id
|
|
INNER JOIN invoices_discount_details ON invoices_discount_details.invoices_discount_id = invoices_discount.id
|
|
INNER JOIN invoices_discount_details_has_db_tables ON invoices_discount_details_has_db_tables.invoices_discount_details_id = invoices_discount_details.id
|
|
INNER JOIN db_tables ON db_tables.id = invoices_discount_details_has_db_tables.db_tables_id
|
|
WHERE
|
|
invoices_details.id IN (
|
|
SELECT MAX(dd.id)
|
|
FROM invoices_details as dd
|
|
WHERE dd.invoices_id = invoices_details.invoices_id
|
|
GROUP BY dd.invoices_id
|
|
) AND
|
|
invoices_details.invoices_id = ? AND
|
|
invoices_details.invoices_status_id IN (1, 2)
|
|
GROUP BY discount_reason, discount_reason_id
|
|
SQL, [$invoice['id']]);
|
|
|
|
$invoice['client_id'] = $invoiceReservations[0]['client_id'];
|
|
$invoice['notes'] = $payments[0]['notes'];
|
|
|
|
$invoice['payments'] = [];
|
|
|
|
$paymentSum = 0;
|
|
foreach ($payments as $payment) {
|
|
if ($payment['method'] == 'client_service') {
|
|
continue 2;
|
|
}
|
|
$invoice['payments'][] = [
|
|
'amount' => $payment['amount'],
|
|
'method' => $payment['method'],
|
|
];
|
|
|
|
$paymentSum += round($payment['amount'], 2);
|
|
}
|
|
|
|
$discountSum = 0;
|
|
|
|
$invoice['discounts'] = [];
|
|
|
|
foreach ($discounts as $discount) {
|
|
$table = $discount['discount_reason'];
|
|
|
|
$d = [];
|
|
$pos = strpos($table, '_offer');
|
|
|
|
if ($pos !== false && $pos > 0) {
|
|
$d = fetch(<<<SQL
|
|
SELECT
|
|
discount as value,
|
|
CASE type_discount_id
|
|
WHEN 2 THEN 'amount'
|
|
WHEN 3 THEN 'percentage'
|
|
ELSE 'other'
|
|
END as type
|
|
FROM $table
|
|
WHERE id = ?
|
|
SQL, [$discount['discount_reason_id']])[0];
|
|
$hasServicesTable = $table . '_has_services';
|
|
$tableWithId = $table . '_id';
|
|
$dServices = fetch(<<<SQL
|
|
SELECT
|
|
services_id as service_id
|
|
FROM $hasServicesTable
|
|
WHERE $tableWithId = ?
|
|
SQL, [$discount['discount_reason_id']]);
|
|
|
|
foreach ($items as $item) {
|
|
if (in_array($item['id'], array_column($dServices, 'service_id'))) {
|
|
if ($d['type'] === 'percentage') {
|
|
$discountSum += round((($item['price'] * $d['value']) / 100) * 1.15, 2);
|
|
} else {
|
|
$discountSum += round($d['value'] * 1.15, 2);
|
|
}
|
|
$d['metadata'] = [
|
|
'service_id' => $item['id'],
|
|
'service_price' => $item['price'],
|
|
'reason' => $discount['discount_reason'],
|
|
'reason_id' => $discount['discount_reason_id'],
|
|
];
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($discount['discount_reason'] == 'an_employee_offer') {
|
|
$d['source'] = 'other';
|
|
} else {
|
|
$d['source'] = 'custom';
|
|
}
|
|
$invoice['discounts'][] = $d;
|
|
continue;
|
|
}
|
|
|
|
$d['type'] = 'amount';
|
|
if ($discount['discount_reason'] == 'an_employee_offer') {
|
|
$type = $d['type'];
|
|
$source = 'other';
|
|
$metadata = [
|
|
'partner_id' => $discount['discount_reason_id'],
|
|
];
|
|
} else {
|
|
$type = $d['type'];
|
|
$source = 'custom';
|
|
$metadata = [
|
|
'reason' => $discount['discount_reason'],
|
|
'reason_id' => $discount['discount_reason_id'],
|
|
];
|
|
}
|
|
|
|
$invoice['discounts'][] = [
|
|
'type' => $type,
|
|
'value' => $d['amount'] ?? $discount['discount'],
|
|
'source' => $source,
|
|
'metadata' => $metadata,
|
|
];
|
|
|
|
$discountSum += round($discount['discount'] * 1.15, 2);
|
|
}
|
|
|
|
$paymentSum = round($paymentSum, 2);
|
|
$discountSum = number_format($discountSum, 2);
|
|
|
|
$diff = number_format((($serviceSum - $discountSum) - $paymentSum) / 1.15, 2);
|
|
|
|
if ($diff >= 1.0) {
|
|
$invoice['discounts'][] = [
|
|
'type' => 'amount',
|
|
'value' => $diff,
|
|
'source' => 'custom',
|
|
'metadata' => [
|
|
'reason' => 'diff',
|
|
'reason_id' => 0,
|
|
],
|
|
];
|
|
} else if ($diff <= -1.0) {
|
|
$invoice['discounts'] = array_unique($invoice['discounts'], SORT_REGULAR);
|
|
$less[] = $invoice['id'];
|
|
}
|
|
|
|
if (count($invoice['payments']) == 0) {
|
|
continue;
|
|
}
|
|
|
|
$result[] = $invoice;
|
|
}
|
|
|
|
$bonds = fetch(<<<SQL
|
|
SELECT
|
|
bonds.id as id,
|
|
bonds.number as number,
|
|
bonds.user_id as user_id,
|
|
bonds.number_style as number_style,
|
|
CASE bonds.bonds_type_type_id
|
|
WHEN 2 THEN 'wallet'
|
|
WHEN 3 THEN 'package'
|
|
WHEN 4 THEN 'service'
|
|
END as reason,
|
|
bonds.tretment_id as client_id,
|
|
bonds.price as amount,
|
|
bonds.create_time as created_at
|
|
FROM bonds
|
|
WHERE
|
|
bonds.create_time BETWEEN ? AND ? AND
|
|
bonds.id > 1 AND
|
|
bonds.activation_id = 2
|
|
SQL, [$start, $end]);
|
|
|
|
$count = count($bonds);
|
|
$bondResults = [];
|
|
for ($index = 0; $index < $count; ++$index) {
|
|
$bond = &$bonds[$index];
|
|
|
|
$payments = fetch(<<<SQL
|
|
SELECT
|
|
bonds_details_has_how_pay.pay as amount,
|
|
'prepaid' as type,
|
|
CASE how_pay.id
|
|
WHEN 1 THEN 'cash'
|
|
WHEN 2 THEN 'card'
|
|
WHEN 4 THEN 'wallet'
|
|
WHEN 5 THEN 'transfer'
|
|
WHEN 6 THEN 'transfer'
|
|
WHEN 7 THEN 'transfer'
|
|
WHEN 8 THEN 'transfer'
|
|
WHEN 9 THEN 'transfer'
|
|
ELSE 'other'
|
|
END as method,
|
|
bonds_details.notes as notes,
|
|
bonds_details.branch_id as branch_id
|
|
FROM bonds_details
|
|
INNER JOIN bonds_details_has_how_pay ON bonds_details.id = bonds_details_has_how_pay.bonds_details_id
|
|
INNER JOIN how_pay ON how_pay.id = bonds_details_has_how_pay.how_pay_id
|
|
WHERE
|
|
bonds_details.bonds_id = ? AND
|
|
bonds_details.bonds_status_id IN (1, 2)
|
|
GROUP BY how_pay.id
|
|
SQL, [$bond['id']]);
|
|
|
|
$branchId = fetch(<<<SQL
|
|
SELECT
|
|
branch_id as branch_id
|
|
FROM bonds_details
|
|
WHERE bonds_id = ?
|
|
SQL, [$bond['id']])[0]['branch_id'];
|
|
|
|
$bond['branch_id'] = $branchId;
|
|
$bond['notes'] = $payments[0]['notes'];
|
|
$bond['payments'] = $payments;
|
|
|
|
if ($bond['reason'] == 'package') {
|
|
$packages = fetch(<<<SQL
|
|
SELECT
|
|
'package' as type,
|
|
bonds_details_has_packages.packages_id as id,
|
|
packages.name as name,
|
|
packages.price as price
|
|
FROM bonds_details
|
|
INNER JOIN bonds_details_has_packages ON bonds_details.id = bonds_details_has_packages.bonds_details_id
|
|
INNER JOIN packages ON packages.id = bonds_details_has_packages.packages_id
|
|
WHERE
|
|
bonds_details.bonds_id = ? AND
|
|
bonds_details.bonds_status_id IN (1, 2)
|
|
SQL, [$bond['id']]);
|
|
|
|
$bond['items'] = $packages;
|
|
} else if ($bond['reason'] == 'service') {
|
|
$services = fetch(<<<SQL
|
|
SELECT
|
|
'service' as type,
|
|
services.id as id,
|
|
services.price as price,
|
|
services.name as name
|
|
FROM bonds_details
|
|
INNER JOIN bonds_details_has_services ON bonds_details_has_services.bonds_details_id = bonds_details.id
|
|
INNER JOIN services ON services.id = bonds_details_has_services.services_id
|
|
WHERE
|
|
bonds_details.bonds_id = ? AND
|
|
bonds_details.bonds_status_id IN (1, 2)
|
|
SQL, [$bond['id']]);
|
|
|
|
$discounts = fetch(<<<SQL
|
|
SELECT
|
|
CASE bonds_details_has_services.type_discount_id
|
|
WHEN 2 THEN 'amount'
|
|
WHEN 3 THEN 'percentage'
|
|
ELSE 'amount'
|
|
END as type,
|
|
bonds_details_has_services.discount as value
|
|
FROM bonds_details
|
|
INNER JOIN bonds_details_has_services ON bonds_details_has_services.bonds_details_id = bonds_details.id
|
|
WHERE
|
|
bonds_details.bonds_id = ? AND
|
|
bonds_details.bonds_status_id IN (1, 2)
|
|
SQL, [$bond['id']]);
|
|
|
|
$bond['items'] = $services;
|
|
$bond['discounts'] = $discounts;
|
|
}
|
|
|
|
$hasCreditNote = fetch(<<<SQL
|
|
SELECT EXISTS(
|
|
SELECT *
|
|
FROM bonds_details
|
|
WHERE bonds_id = ? AND bonds_status_id = 3
|
|
) AS result
|
|
SQL, [$bond['id']]);
|
|
|
|
if (count($invoice['payments']) == 0) {
|
|
continue;
|
|
}
|
|
|
|
$bond['has_credit_note'] = $hasCreditNote[0]['result'] == 1;
|
|
if ($bond['reason'] == 'service' || $bond['reason'] == 'package') {
|
|
$bond['type'] = 'bond';
|
|
$result[] = $bond;
|
|
} else {
|
|
$bondResults[] = $bond;
|
|
}
|
|
}
|
|
|
|
$sorted = usort($result, function ($a, $b) {
|
|
$a = new DateTime($a['created_at']);
|
|
$b = new DateTime($b['created_at']);
|
|
return $a <=> $b;
|
|
});
|
|
|
|
if (!$sorted) {
|
|
dd("Not sorted!");
|
|
}
|
|
|
|
transferJsonFiles('invoices_new', $result);
|
|
print "Invoices done!\n";
|
|
transferJsonFiles('bonds_new', $bondResults);
|
|
print "Bonds done!\n";
|
|
die;
|
|
|
|
$usersData = fetch(<<<SQL
|
|
SELECT
|
|
user.id,
|
|
user.name as nickname,
|
|
user.username as username,
|
|
user.pass as password, -- md5
|
|
user.number as phone_number,
|
|
CASE user.emp_active_status_id
|
|
WHEN 1 THEN 'pending'
|
|
WHEN 2 THEN 'active'
|
|
WHEN 3 THEN 'blocked'
|
|
END as active, -- 1 = wait, 2 = active 3 = inactive
|
|
user.create_time as created_at,
|
|
user.date_time as updated_at,
|
|
1 as must_update_password,
|
|
emplyee.occupation_id as group_id
|
|
FROM user
|
|
LEFT JOIN emplyee ON emplyee.id = user.emplyee_id
|
|
SQL);
|
|
|
|
transferJsonFiles('users', $usersData);
|
|
print "Users done!\n";
|
|
|
|
$employeeData = fetch(<<<SQL
|
|
SELECT
|
|
emplyee.id as id,
|
|
emplyee.name as nickname,
|
|
emplyee.emp_id as national_id,
|
|
4000 as salary,
|
|
emplyee.branch_id as branch_id,
|
|
emplyee.occupation_id as group_id,
|
|
CASE emplyee.G_sex_id
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 0
|
|
WHEN 3 THEN 1
|
|
END as gender,
|
|
8 as working_hours,
|
|
emplyee.date_time as created_at,
|
|
emplyee.date_time as updated_at,
|
|
user.id as user_id,
|
|
CASE emplyee.activation_id
|
|
WHEN 0 THEN 0
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as is_shown_to_employees,
|
|
CASE emplyee.activation_id
|
|
WHEN 0 THEN 0
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as is_shown_to_clients
|
|
FROM emplyee
|
|
LEFT JOIN user ON user.emplyee_id = emplyee.id
|
|
SQL);
|
|
transferJsonFiles('employees', $employeeData);
|
|
print "Employees done!\n";
|
|
|
|
// ------------------------------
|
|
|
|
$employeeServices = fetch(<<<SQL
|
|
SELECT
|
|
emplyee_id as employee_id,
|
|
services_id as service_id,
|
|
date_time as created_at
|
|
FROM emplyee_has_services
|
|
SQL);
|
|
transferJsonFiles('employee_services', $employeeServices);
|
|
print "Employee services done!\n";
|
|
die;
|
|
|
|
$clients = fetch(<<<SQL
|
|
SELECT
|
|
tretment.id as id,
|
|
tretment.name as nickname,
|
|
tretment.number as phone_number,
|
|
tretment.id_number as national_id,
|
|
CASE tretment.G_sex_id
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 0
|
|
WHEN 3 THEN 1
|
|
END as gender,
|
|
tretment.age as age,
|
|
tretment.create_time as created_at,
|
|
tretment.an_employee_offer_id as partner_id,
|
|
CASE tretment.black_list
|
|
WHEN 1 THEN 0
|
|
WHEN 2 THEN 1
|
|
END as is_blacklisted, -- 1 = not blacklisted, 2 = not blacklisted
|
|
tretment.id_number as national_id,
|
|
tretment.commercial_register_id as department_id,
|
|
tretment.user_id as user_id,
|
|
clients.first_name as first_name,
|
|
clients.last_name as last_name,
|
|
clients.password as password,
|
|
DATE(clients.birthdate) as birthdate
|
|
FROM tretment
|
|
LEFT JOIN clients ON clients.tretment_id = tretment.id
|
|
WHERE
|
|
tretment.create_time >= ? AND
|
|
tretment.id > 0
|
|
SQL, [$start]);
|
|
transferJsonFiles('clients_new', $clients);
|
|
print "Clients done!\n";
|
|
|
|
$walaaTransactions = fetch(<<<SQL
|
|
SELECT
|
|
walla_points.id as transaction_id,
|
|
walla_points.tretment_id as client_id,
|
|
walla_points.points as amount,
|
|
walla_points.notes as notes,
|
|
walla_points.user_id as user_id,
|
|
walla_points.walla_reasons_id as context_id,
|
|
walla_points.create_time as created_at
|
|
FROM
|
|
walla_points
|
|
WHERE
|
|
walla_points.create_time >= ? AND
|
|
walla_points.activation_id = 2
|
|
SQL, [$start]);
|
|
transferJsonFiles('walaa_transactions', $walaaTransactions);
|
|
print "Walaa transactions done!\n";
|
|
|
|
$membershipPoints = fetch(<<<SQL
|
|
SELECT
|
|
tretment_id as client_id,
|
|
point as points
|
|
FROM
|
|
points_with_invoices
|
|
WHERE
|
|
point > 0 AND
|
|
create_time >= ?
|
|
SQL, [$start]);
|
|
|
|
transferJsonFiles('membership_points', $membershipPoints);
|
|
print "Membership points done!\n";
|
|
|
|
$clientMoney = fetch(<<<SQL
|
|
SELECT
|
|
tretment_id as client_id,
|
|
money as amount
|
|
FROM tretment_money
|
|
SQL);
|
|
|
|
transferJsonFiles('total_client_money', $clientMoney);
|
|
print "Total client money done!\n";
|
|
|
|
$clientPoints = fetch(<<<SQL
|
|
SELECT
|
|
tretment_id as client_id,
|
|
point as points
|
|
FROM total_points
|
|
SQL);
|
|
|
|
transferJsonFiles('total_client_points', $clientPoints);
|
|
print "Total client points done!\n";
|
|
|
|
$clientWalaaPoints = fetch(<<<SQL
|
|
SELECT
|
|
tretment_id as client_id,
|
|
point as points
|
|
FROM total_walla_points
|
|
SQL);
|
|
|
|
transferJsonFiles('total_client_walaa_points', $clientWalaaPoints);
|
|
print "Total client walaa points done!\n";
|
|
|
|
if (count($less) > 0) {
|
|
dd($less);
|
|
} else {
|
|
dd("No less!");
|
|
}
|
|
die;
|
|
//*/
|
|
|
|
|
|
// additonals
|
|
// packages
|
|
// drinks
|
|
// offers
|
|
// walla answers and regisration
|
|
|
|
|
|
|
|
/*
|
|
book {
|
|
book_details {
|
|
- books details has the services and datetime for the book and the employee and the branch
|
|
- if service_id is 0 then it's a bundle
|
|
- go get the bundle_services table and get the services from there
|
|
- some time the bundle has how many services it is mean that the book has the same service many times like quantity
|
|
}
|
|
}
|
|
|
|
invoices {
|
|
- we must take and rework the sahaby report for fix all the invoices
|
|
invoices_details {
|
|
how_pay {
|
|
- how the invoice was paid
|
|
}
|
|
invoices_discount {
|
|
- the discount for the invoice
|
|
}
|
|
- the invoice details has the status of the invoice 1 for new invoice 2 for debit note 3 for credit note 4 for no vat invoice
|
|
}
|
|
}
|
|
|
|
bonds {
|
|
- the bonds is the money that the client has in the system
|
|
- the bonds has the money and the date and the client id
|
|
- we must change the bonds to invoice and correct it
|
|
}
|
|
*/
|