test CREATE booking_transactions_view

This commit is contained in:
Edmond Lang
2025-10-25 15:55:52 +08:00
parent 9ccf66c504
commit 6438d25038
@@ -0,0 +1,54 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
public function up(): void
{
DB::statement("
CREATE OR REPLACE VIEW booking_transactions_view AS
SELECT
t.id AS transaction_id,
t.type AS transaction_type,
b.id AS booking_id,
b.marking AS booking_marking,
c.name AS company_name,
c.reference AS company_reference,
st.name AS service_name,
fc.short_code AS currency_code,
b.fix_amount AS booking_amount,
CASE c.e_invoice
WHEN 1 THEN 'Yes'
WHEN 0 THEN 'No'
ELSE 'Not Submitted'
END AS need_e_invoice
FROM transactions t
LEFT JOIN bookings b
ON (
(t.type IN (1,2,4,6,7,8,17) AND b.id = t.owner_id)
OR (t.type IN (3,15,16) AND b.id = (
SELECT bb.id FROM transactions tt
JOIN bookings bb ON bb.id = tt.owner_id
WHERE tt.id = t.owner_id LIMIT 1
))
OR (t.type = 12 AND b.id = (
SELECT bbb.id FROM transactions ttt
JOIN bookings bbb ON bbb.id = ttt.owner_id
WHERE ttt.id = (
SELECT tt2.owner_id FROM transactions tt2 WHERE tt2.id = t.owner_id LIMIT 1
) LIMIT 1
))
)
LEFT JOIN companies c ON c.id = b.company_id
LEFT JOIN service_types st ON st.id = b.service_id
LEFT JOIN currencies fc ON fc.id = b.fix_currency_id;
");
}
public function down(): void
{
DB::statement('DROP VIEW IF EXISTS booking_transactions');
}
};