qr_exim_export_invoice_contacts:
SELECT exim_export_invoice.*,('=> '+store_name) AS contact_name,store_code AS contact_code,'' AS contact_address,'' AS phone FROM exim_export_invoice,exim_swap_relation,store_list WHERE exim_export_invoice.reason='XC' AND exim_export_invoice.invoice_id=exim_swap_relation.export_invoice_id AND exim_swap_relation.import_store_id=store_list.store_id
UNION ALL
SELECT exim_export_invoice.*,contact_name,contact_code,contact_address,phone FROM exim_export_invoice,contacts WHERE exim_export_invoice.reason<>'XC' AND exim_export_invoice.contact_id=contacts.contact_id
!-!
qr_exim_import_invoice_contacts:
SELECT exim_import_invoice.*,('=> '+store_name) AS contact_name,store_code AS contact_code,'' AS contact_address,'' AS phone FROM exim_export_invoice,exim_import_invoice,exim_swap_relation,store_list WHERE exim_import_invoice.reason='NC' AND exim_import_invoice.invoice_id=exim_swap_relation.import_invoice_id AND exim_export_invoice.invoice_id=exim_swap_relation.export_invoice_id AND exim_export_invoice.store_id=store_list.store_id
UNION ALL
SELECT exim_import_invoice.*,contact_name,contact_code,contact_address,phone FROM exim_import_invoice,contacts WHERE exim_import_invoice.reason<>'NC' AND exim_import_invoice.contact_id=contacts.contact_id
!-!
qr_goods_allunits_base:
SELECT goods.goods_id,goods.goods_code,0 AS unit_id,'' AS goods_code_unit,goods.unit,1 AS unit_ratio,goods.category_id,goods.goods_name,goods.import_price,goods.export_price,goods.export_price_wholesale,NULL AS export_price_bycustomer,goods.comment,goods.continued, goods.warranty,goods.ccard_point, goods.goods_discount_default,goods.goods_discount_money, goods.goods_discount_wholesale_percent,goods.goods_discount_wholesale_money, goods.template_id,goods.time_price_update, goods.description_short,goods.description_full,goods.dm_width,goods.dm_height,goods.dm_depth,goods.profit_default_wholesale,goods.profit_default_wholesale_percent,goods.profit_default,goods.profit_default_percent,net_weight,packing_weight,goods.branch_id,goods.code_br,goods.br_isloaded FROM goods
UNION ALL 
SELECT  goods.goods_id,goods.goods_code,unit_id,goods_code_unit,goods_unit.unit_name AS unit,goods_unit.unit_ratio, goods.category_id,goods.goods_name, goods_unit.import_price,goods_unit.export_price,goods_unit.export_price_wholesale, NULL AS export_price_bycustomer, goods.comment,goods.continued, goods.warranty, goods_unit.ccard_point,goods_unit.goods_discount_default,goods_unit.goods_discount_money, goods_unit.goods_discount_wholesale_percent,goods_unit.goods_discount_wholesale_money, goods.template_id,goods.time_price_update, goods.description_short,goods.description_full,
goods.dm_width,goods.dm_height,goods.dm_depth,goods_unit.profit_default_wholesale,goods_unit.profit_default_wholesale_percent,goods_unit.profit_default,goods_unit.profit_default_percent,goods_unit.net_weight,goods_unit.packing_weight,goods.branch_id,goods.code_br,goods.br_isloaded FROM goods,goods_unit WHERE goods.goods_id=goods_unit.goods_id
!-!
qr_receipt:
SELECT RECEIPT_DEBT_CONTACTS.branch_id,invoice_date,invoice_no,contact_name,invoice_val,RECEIPT_DEBT_CONTACTS.comment,realname,'TN' AS reason 
		FROM (SELECT receipt_debt.branch_id,invoice_no,invoice_val,contact_name,invoice_date,receipt_debt.comment,trade_user_id,invoice_id 
		FROM contacts,receipt_debt WHERE contacts.contact_id=receipt_debt.contact_id) AS RECEIPT_DEBT_CONTACTS 
		LEFT JOIN user_list 
		ON RECEIPT_DEBT_CONTACTS.trade_user_id=user_list.user_id 
		UNION ALL 
		SELECT receipt_others.branch_id,invoice_date,invoice_no,contact_name,invoice_val,receipt_others.comment,NULL AS realname,'TK' AS reason 
		FROM receipt_others 
		LEFT JOIN contacts 
		ON receipt_others.contact_id=contacts.contact_id
!-!
qr_pay:
SELECT PAY_DEBT_CONTACTS.branch_id,invoice_date,invoice_no,contact_name,invoice_val,PAY_DEBT_CONTACTS.comment,realname,'CN' AS reason 
		FROM (SELECT pay_debt.branch_id,invoice_no,invoice_val,contact_name,invoice_date,pay_debt.comment,trade_user_id,invoice_id 
		FROM contacts,pay_debt WHERE contacts.contact_id=pay_debt.contact_id) AS PAY_DEBT_CONTACTS 
		LEFT JOIN user_list 
		ON PAY_DEBT_CONTACTS.trade_user_id=user_list.user_id 
		UNION ALL SELECT pay_others.branch_id,invoice_date,invoice_no,contact_name,invoice_val,pay_others.comment,NULL AS realname,'CK' AS reason 
		FROM pay_others 
		LEFT JOIN contacts 
		ON pay_others.contact_id=contacts.contact_id
!-!
qr_contacts_debit:
SELECT contacts.branch_id, contacts.contact_id, contact_code, contact_name, phone, contact_address, continued, type_customer, type_supplier, 
		[_ISNULL_ debit_export] AS debit_export_nor, 
		[_ISNULL_ debit_import] AS debit_import_nor, 
		[_ISNULL_ debit_pay] AS debit_pay_nor, 
		[_ISNULL_ debit_receipt] AS debit_receipt_nor, 
		[_ISNULL_ phaithu_dauky] AS debit_init_receipt_nor, 
		[_ISNULL_ phaitra_dauky] AS debit_init_pay_nor, 
		([_ISNULL_ debit_export]-[_ISNULL_ debit_import]+[_ISNULL_ debit_pay]-[_ISNULL_ debit_receipt]+[_ISNULL_ phaithu_dauky]- [_ISNULL_ phaitra_dauky]) AS debit FROM (((contacts LEFT JOIN (SELECT contact_id, SUM(money_debit) AS debit_import 		
		FROM exim_import_invoice 		
		GROUP BY contact_id) AS Q_DEBIT_IMPORT ON contacts.contact_id=Q_DEBIT_IMPORT.contact_id) 
		LEFT JOIN (SELECT contact_id, SUM(money_debit) AS debit_export 		
		FROM exim_export_invoice 		GROUP BY contact_id) AS Q_DEBIT_EXPORT 
		ON contacts.contact_id=Q_DEBIT_EXPORT.contact_id) LEFT JOIN 
		(SELECT contact_id, SUM(invoice_val) AS debit_pay 		FROM pay_debt 		GROUP BY contact_id) AS Q_DEBIT_PAY 
		ON contacts.contact_id=Q_DEBIT_PAY.contact_id) LEFT JOIN (SELECT contact_id, SUM(invoice_val) AS debit_receipt 		
		FROM receipt_debt 		GROUP BY contact_id) AS Q_DEBIT_RECEIPT ON contacts.contact_id=Q_DEBIT_RECEIPT.contact_id
!-!
qr_exim_export_import_details:
SELECT exim_export_import_details.*,exim_export_invoice_details.invoice_id AS export_invoice_id,0 AS import_invoice_id 		FROM exim_export_import_details,exim_export_invoice_details 		WHERE exim_export_import_details.export_invoice_details_id=exim_export_invoice_details.id 		AND exim_export_import_details.import_invoice_details_id<=0 		UNION SELECT exim_export_import_details.*,exim_export_invoice_details.invoice_id AS export_invoice_id,exim_import_invoice_details.invoice_id AS import_invoice_id 		FROM exim_export_import_details,exim_export_invoice_details,exim_import_invoice_details 		WHERE exim_export_import_details.export_invoice_details_id=exim_export_invoice_details.id 		AND exim_export_import_details.import_invoice_details_id=exim_import_invoice_details.id