feature : transaction.sql updated

pull/6221/head
Gauthier LO 2024-01-04 17:02:32 +01:00
parent 67cb86e25b
commit b59b2ba00c
1 changed files with 70 additions and 53 deletions

View File

@ -1,54 +1,71 @@
CREATE VIEW transaction as CREATE VIEW transaction as
SELECT id, create_by, create_time, update_by, update_time, SELECT id AS id,
type, client_id, payment_proof, invoice_number, shipping_fee, purchase_fee, amount, currency create_by AS create_by,
FROM create_time AS create_time,
( update_by AS update_by,
SELECT id as id, update_time AS update_time,
create_by, type AS type,
create_time, client_id AS client_id,
update_by, payment_proof AS payment_proof,
update_time, invoice_number AS invoice_number,
'Credit' as type, shipping_fee AS shipping_fee,
client_id, purchase_fee AS purchase_fee,
payment_proof, amount AS amount,
NULL as invoice_number, currency AS currency
NULL as shipping_fee, FROM (
NULL as purchase_fee, SELECT id AS id,
amount as amount, create_by AS create_by,
(SELECT code FROM currency WHERE credit.currency_id = id) as currency create_time AS create_time,
update_by AS update_by,
update_time AS update_time,
'Credit' AS type,
client_id AS client_id,
payment_proof AS 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
FROM credit FROM credit
UNION ALL UNION ALL
SELECT id as id, SELECT id AS id,
create_by, create_by AS create_by,
create_time, create_time AS create_time,
update_by, update_by AS update_by,
update_time, update_time AS update_time,
'Debit' as type, 'Debit' AS type,
client_id, client_id AS client_id,
NULL as payment_proof, NULL AS payment_proof,
invoice_number, invoice_number AS invoice_number,
total_amount as shipping_fee, total_amount AS shipping_fee,
IF(invoice_number LIKE '%%%%-%%-7%%%', if((invoice_number like '%%%%-%%-7%%%'),
purchase_total(invoice_number), purchase_total(invoice_number), NULL) AS purchase_fee,
NULL) as purchase_fee, if((invoice_number like '%%%%-%%-7%%%'),
IF(invoice_number LIKE '%%%%-%%-7%%%', (total_amount +
total_amount + (purchase_total(invoice_number)), purchase_total(invoice_number)),
total_amount) as amount, total_amount) AS amount,
(SELECT code FROM currency WHERE shipping_invoice.currency_id = id) as currency (SELECT code FROM currency WHERE shipping_invoice.currency_id = id) AS currency
FROM shipping_invoice FROM shipping_invoice
WHERE client_id IS NOT NULL WHERE client_id IS NOT NULL
AND shipping_invoice.currency_id IS NOT NULL AND currency_id IS NOT NULL
AND shipping_invoice.currency_id <> '' 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,
'Debit' AS type,
client_id AS client_id,
payment_document AS payment_proof,
invoice_number AS 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
FROM purchase_order
WHERE invoice_number LIKE '%%%%-%%-1%%%'
AND client_id IS NOT NULL
AND currency_id IS NOT NULL
AND currency_id <> ''
) as id; ) as id;
-- Function that computes the total of purchase by order
CREATE FUNCTION purchase_total( invoice_number varchar(12)) RETURNS decimal(10, 2)
BEGIN
RETURN (
SELECT SUM(poc.purchase_fee) as total
FROM platform_order_content as poc
JOIN platform_order po
ON po.id = poc.platform_order_id
WHERE po.shipping_invoice_number = invoice_number
);
END;