diff --git a/db/views/transaction.sql b/db/views/transaction.sql index 4e83da298..1e1cc9d8a 100644 --- a/db/views/transaction.sql +++ b/db/views/transaction.sql @@ -1,71 +1,75 @@ -CREATE VIEW transaction as -SELECT id AS id, - create_by AS create_by, - create_time AS create_time, - update_by AS update_by, - update_time AS update_time, - type AS type, - client_id AS client_id, - payment_proof AS payment_proof, - invoice_number AS invoice_number, - shipping_fee AS shipping_fee, - purchase_fee AS purchase_fee, - amount AS amount, - currency AS currency +CREATE OR REPLACE VIEW transaction as +SELECT combined.id, + combined.create_by, + combined.create_time, + combined.update_by, + combined.update_time, + type, + client_id, + payment_proof, + invoice_number, + shipping_fee, + purchase_fee, + amount, + currency.code AS currency FROM ( - SELECT id AS id, - create_by AS create_by, - create_time AS create_time, - update_by AS update_by, - update_time AS update_time, + SELECT id, + create_by, + create_time, + update_by, + update_time, 'Credit' AS type, - client_id AS client_id, - payment_proof AS payment_proof, + client_id, + payment_proof, NULL AS invoice_number, NULL AS shipping_fee, NULL AS purchase_fee, - amount AS amount, - (SELECT code FROM currency WHERE credit.currency_id = id) AS currency + amount, + currency_id FROM credit UNION ALL - SELECT id AS id, - create_by AS create_by, - create_time AS create_time, - update_by AS update_by, - update_time AS update_time, + SELECT id, + create_by, + create_time, + update_by, + update_time, 'Debit' AS type, - client_id AS client_id, + client_id, NULL AS payment_proof, - invoice_number AS invoice_number, + invoice_number, total_amount AS shipping_fee, - if((invoice_number like '%%%%-%%-7%%%'), - purchase_total(invoice_number), NULL) AS purchase_fee, - if((invoice_number like '%%%%-%%-7%%%'), - (total_amount + - purchase_total(invoice_number)), - total_amount) AS amount, - (SELECT code FROM currency WHERE shipping_invoice.currency_id = id) AS currency - FROM shipping_invoice + pt.total AS purchase_fee, + COALESCE(pt.total + si.total_amount, si.total_amount) AS amount, + currency_id + FROM shipping_invoice si + LEFT JOIN ( + SELECT po.shipping_invoice_number, SUM(poc.purchase_fee) AS total + FROM platform_order_content poc + JOIN platform_order po ON po.id = poc.platform_order_id + WHERE po.shipping_invoice_number LIKE '%-%-7%' + GROUP BY po.shipping_invoice_number + ) pt ON pt.shipping_invoice_number = si.invoice_number WHERE client_id IS NOT NULL AND currency_id IS NOT NULL AND currency_id <> '' UNION ALL - SELECT id AS id, - create_by as create_by, - create_time as create_time, - update_by as update_by, - update_time as update_time, + SELECT id, + create_by, + create_time, + update_by, + update_time, 'Debit' AS type, - client_id AS client_id, + client_id, payment_document AS payment_proof, - invoice_number AS invoice_number, + invoice_number, NULL AS shipping_fee, final_amount AS purchase_fee, final_amount AS amount, - (SELECT code FROM currency WHERE purchase_order.currency_id = id) AS currency + currency_id FROM purchase_order - WHERE invoice_number LIKE '%%%%-%%-1%%%' + WHERE invoice_number LIKE '%-%-1%' AND client_id IS NOT NULL AND currency_id IS NOT NULL AND currency_id <> '' - ) as id; \ No newline at end of file + ) as combined +LEFT JOIN currency ON combined.currency_id = currency.id \ No newline at end of file