SELECT
	sd.id as id,
	sd.sale_id as sale_id,
	sd.sale_sub_code as seq_number,
	ps.product_category_id as product_category_id,
	sd.product_set_id as product_set_id,
	sd.product_model_id as product_id,
	sd.lot_number as lot_number,
	'' as serial_number,
	sd.ubd_date as ubd,
	24 as validity_period,
	1 as unit_code,
	sd.sale_quantity as quantity,
	sd.sale_price as price,
	NULL as products_price_id,
	CASE sd.sale_type_id WHEN 1 THEN 1 WHEN 5 THEN 1 WHEN 2 THEN 2 WHEN 3 THEN NULL WHEN 4 THEN NULL ELSE NULL END as stock_type_code,
	sd.sale_detail_note as note,
	NOW() as created_at,
	NOW() as updated_at
FROM sale_details as sd
LEFT JOIN master_app_production.product_sets as ps
	ON sd.product_set_id = ps.id
INTO OUTFILE "/tmp/sale_details.csv" FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"';