SELECT
g.member_id,
g.member_name,
g.member_code,
branch_name,
(
SELECT
SUM(bill_sale_detail_price * bill_sale_detail_qty - bill_sale_detail_discount)
FROM so_bill_sale_detail
LEFT JOIN so_bill_sale
ON so_bill_sale.bill_sale_id = so_bill_sale_detail.bill_id
LEFT JOIN so_product
ON so_product.product_code = so_bill_sale_detail.bill_sale_detail_barcode
WHERE
so_bill_sale.bill_sale_end_date
AND so_bill_sale.member_id = g.member_id
AND so_bill_sale.bill_sale_status = 'pay'
AND year(so_bill_sale.bill_sale_end_date) = $year
) AS money ,
(
SELECT
bill_sale_id
FROM so_bill_sale
WHERE
so_bill_sale.bill_sale_end_date
AND so_bill_sale.member_id = g.member_id
AND so_bill_sale.bill_sale_status = 'pay'
AND year(so_bill_sale.bill_sale_end_date) = $year
) AS bill_sale_id
FROM
so_customer g
LEFT JOIN so_branch_sale
ON so_branch_sale.branch_id = g.branch_sale_id
ORDER BY g.member_name ASC