CREATE VIEW 0_order_summary_view AS
SELECT debtor_no, debtor_ref, branch_ref, stk_code
, min(delivery_date) delivery_date
, sum(sod.quantity - qty_sent) as quantity
, sum((sod.quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
, min(required_date) required_date
, group_concat(distinct comments separator ';') as order_comment
, group_concat(distinct comment separator ';') as detail_comment
, max(quantity_before) as quantity_before
FROM 0_sales_order_details sod
JOIN 0_sales_orders so ON (so.order_no = sod.order_no
	AND so.trans_type = sod.trans_type
	AND so.trans_type = 30
)
NATURAL JOIN 0_debtors_master
NATURAL JOIN 0_cust_branch
JOIN 0_denorm_order_details_queue  d ON (d.id = sod.id)
WHERE sod.quantity > qty_sent
GROUP BY debtor_no, branch_code, stk_code
