
					8640 - (8640/4) * (4-1)
					
					SELECT MIN(from_datetime) AS min_date,
					(SELECT COUNT(id) FROM smsevent_instances WHERE event_id='e54d2dec-f6a8-def0-afc2-54cf0110c124' AND target_id='c078815d-1a88-49bb-3e9f-54cf0a22377c' AND (STATUS & 1021136) = 0) AS available_sched,
					(SELECT COUNT(id) FROM smsevent_instances WHERE event_id='e54d2dec-f6a8-def0-afc2-54cf0110c124' AND target_id='c078815d-1a88-49bb-3e9f-54cf0a22377c')  AS all_sched FROM smsevent_instances
					WHERE event_id='e54d2dec-f6a8-def0-afc2-54cf0110c124' AND target_id='c078815d-1a88-49bb-3e9f-54cf0a22377c' AND (status & 33920) = 0


		$cancel_condition="AND (status & 33920) = 0"; // AllRescheduledOrigin -> 33920
		$fee_subcategory_condition="AND (sms_fee.fee_subcategory='受講料' OR sms_fee.fee_subcategory='教材費' OR sms_fee.fee_subcategory='設備費')";		
		$date_condition="(sms_transaction.t_date >= '2015-01-01' OR  smsevent_instances.from_datetime >= '2015-01-01' )";		
		$fee_shokubun_condition=" AND (sms_fee_sub1.fee_shokubun IS NULL OR sms_fee_sub1.fee_shokubun != 1) ";
		$transaction_join = "INNER JOIN sms_transaction ON sms_fee.id=sms_transaction.fee_id AND sms_transaction.deleted=0 ";
		$this->GetExecuteMaeukekinQuery('regular', $this->target_period_start, $cancel_condition, $fee_subcategory_condition, $date_condition, $fee_shokubun_condition, $current_batch_start_datetime, $current_batch_end_datetime, $transaction_join);

SELECT sms_fee.*, sms_fee.id as fee_id, sms_fee_sub1.tanka, sms_fee_sub1.suryo, smsevent_instances.from_datetime, smsevent_instances.instance_id
FROM smsevent_instances 
INNER JOIN sms_fee ON smsevent_instances.target_id=sms_fee.contact_id AND smsevent_instances.event_id = sms_fee.event_id AND sms_fee.deleted = 0				
AND smsevent_instances.category='student' AND (status & 33920) = 0
AND smsevent_instances.event_type='course'  AND (sms_fee.fee_subcategory='受講料' OR sms_fee.fee_subcategory='教材費' OR sms_fee.fee_subcategory='設備費')
AND smsevent_instances.from_datetime >= '2015-01-01' AND smsevent_instances.from_datetime <= '2015-01-15' 				
INNER JOIN sms_fee_sub1 ON sms_fee.id=sms_fee_sub1.id_sub1 AND (sms_fee_sub1.fee_shokubun IS NULL OR sms_fee_sub1.fee_shokubun != 1) 
INNER JOIN sms_transaction ON sms_fee.id=sms_transaction.fee_id AND sms_transaction.deleted=0
WHERE (sms_transaction.t_date >= '2015-01-01' OR  smsevent_instances.from_datetime >= '2015-01-01' ) AND sms_fee.sell_date < '2015-02-01' 


		$cancel_condition=" AND (status & 987216) = 0";   // AllCancelled -> 987216
		$fee_subcategory_condition=" AND (sms_fee.fee_subcategory='受講料' OR sms_fee.fee_subcategory='教材費' OR sms_fee.fee_subcategory='設備費')";		
		$date_condition=" (sms_fee.sell_date >= '2015-01-01' OR  smsevent_instances.from_datetime >= '2015-01-15' )";		
		$fee_shokubun_condition=" AND sms_fee_sub1.fee_shokubun = 1 ";
		$transaction_join = "";
		
SELECT sms_fee.*, sms_fee.id as fee_id, sms_fee_sub1.tanka, sms_fee_sub1.suryo, smsevent_instances.from_datetime, smsevent_instances.instance_id
FROM smsevent_instances 
INNER JOIN sms_fee ON smsevent_instances.target_id=sms_fee.contact_id AND smsevent_instances.event_id = sms_fee.event_id AND sms_fee.deleted = 0				
AND smsevent_instances.category='student'  AND (status & 987216) = 0
AND smsevent_instances.event_type='course' AND (sms_fee.fee_subcategory='受講料' OR sms_fee.fee_subcategory='教材費' OR sms_fee.fee_subcategory='設備費')
AND smsevent_instances.from_datetime >= '2015-01-01' AND smsevent_instances.from_datetime <= '2015-01-15' 					
INNER JOIN sms_fee_sub1 ON sms_fee.id=sms_fee_sub1.id_sub1 AND sms_fee_sub1.fee_shokubun = 1
WHERE (sms_fee.sell_date >= '2015-01-01' OR  smsevent_instances.from_datetime >= '2015-01-15' ) AND sms_fee.sell_date < '2015-02-01' 

		$cancel_condition=" AND (status & 918592) = 0";   // CourseInstanceCancelledRescheduledOrigin -> 918592
		$fee_subcategory_condition="AND sms_fee.fee_subcategory='合計データ' ";		
		$date_condition="(sms_fee.sell_date >= '2015-01-01' OR  sms_event_instance.from_datetime >= '2015-01-01' )";		
		$fee_shokubun_condition=" AND sms_fee_sub1.fee_shokubun IS NULL ";
		$transaction_join = "";
		
select sms_fee.*, sms_fee.id as fee_id, sms_fee_sub1.tanka, sms_fee_sub1.suryo, sms_event_instance.from_datetime, sms_event_instance.id as instance_id, sms_event_instance.date_modified as date_modified_ins
FROM sms_event_instance 
INNER JOIN sms_fee ON sms_event_instance.parent_id=sms_fee.event_id  AND sms_fee.deleted = 0 AND (status & 918592) = 0
AND  sms_event_instance.event_type='course' AND sms_fee.fee_subcategory='合計データ' 
AND sms_event_instance.from_datetime >= '2015-01-01' AND sms_event_instance.from_datetime <= '2015-01-15' 	
INNER JOIN sms_fee_sub1 ON sms_fee.id=sms_fee_sub1.id_sub1 AND sms_fee_sub1.fee_shokubun IS NULL
WHERE (sms_fee.sell_date >= '2015-01-01' OR  sms_event_instance.from_datetime >= '2015-01-01' ) AND sms_fee.sell_date < '2015-02-01'  


select sms_fee.*, sms_fee.id as fee_id, sms_fee_sub1.tanka, sms_fee_sub1.suryo, smsevent_instances.from_datetime, smsevent_instances.instance_id
FROM smsevent_instances 
INNER JOIN sms_fee ON smsevent_instances.target_id=sms_fee.contact_id AND smsevent_instances.event_id = sms_fee.event_id AND sms_fee.deleted = 0
AND smsevent_instances.category='student' AND (status & 33920) = 0 
AND smsevent_instances.event_type='course' AND (sms_fee.fee_subcategory='受講料' OR sms_fee.fee_subcategory='教材費' OR sms_fee.fee_subcategory='設備費') 
AND smsevent_instances.from_datetime >= '2015-01-01' AND smsevent_instances.from_datetime <= '2015-01-15' AND sms_fee.sell_date < '2015-02-01' 
INNER JOIN sms_fee_sub1 ON sms_fee.id=sms_fee_sub1.id_sub1 and sms_fee_sub1.fee_shokubun is null 

select sms_fee.*, sms_fee.id as fee_id, sms_fee_sub1.tanka, sms_fee_sub1.suryo, smsevent_instances.from_datetime, smsevent_instances.instance_id, smsevent_instances.date_modified as date_modified_ins
FROM smsevent_instances 
INNER JOIN sms_fee ON smsevent_instances.target_id=sms_fee.contact_id AND smsevent_instances.event_id = sms_fee.event_id  AND sms_fee.deleted = 0
AND smsevent_instances.category='student' AND (status & 987216) > 0 
AND  smsevent_instances.event_type='course' AND (sms_fee.fee_subcategory='受講料' OR sms_fee.fee_subcategory='教材費' OR sms_fee.fee_subcategory='設備費') 
AND smsevent_instances.from_datetime >= '2015-01-01' AND smsevent_instances.from_datetime <= '2015-01-15' AND sms_fee.sell_date < '2015-02-01' 
INNER JOIN sms_fee_sub1 ON sms_fee.id=sms_fee_sub1.id_sub1 and sms_fee_sub1.fee_shokubun in (1,2) 

select sms_fee.*, sms_fee.id as fee_id, sms_fee_sub1.tanka, sms_fee_sub1.suryo, sms_event_instance.from_datetime, sms_event_instance.id as instance_id, sms_event_instance.date_modified as date_modified_ins
FROM sms_event_instance 
INNER JOIN sms_fee ON sms_event_instance.parent_id=sms_fee.event_id  AND sms_fee.deleted = 0
AND (status & 918592) = 0 
AND  sms_event_instance.event_type='course' AND sms_fee.fee_subcategory='合計データ' 
AND sms_event_instance.from_datetime >= '2015-01-01' AND sms_event_instance.from_datetime <= '2015-01-15' AND sms_fee.sell_date < '2015-02-01' 
INNER JOIN sms_fee_sub1 ON sms_fee.id=sms_fee_sub1.id_sub1 and sms_fee_sub1.fee_shokubun is null 


-- 02/10

SELECT * FROM `maeukekin` WHERE `student_id` LIKE '1510748' AND `fee_subcategory` LIKE 'j'
SELECT * FROM `smsevent_instances` WHERE `target_id` LIKE '1510748'
SELECT tanka, t_date, sms_transaction.date_modified, sms_fee.* FROM sms_fee, sms_fee_sub1, sms_transaction 
WHERE id_sub1=sms_fee.id AND sms_transaction.fee_id=sms_fee.id AND sms_fee.contact_id LIKE '1510748' AND sms_fee.deleted=0 AND sms_transaction.deleted=0

SELECT t_date, sms_transaction.date_modified, sms_fee.sell_date, sms_fee.date_modified, fee_subcategory, sms_fee.* FROM sms_fee, sms_transaction 
WHERE sms_transaction.fee_id=sms_fee.id AND sms_fee.deleted=0 AND sms_transaction.deleted=0 AND sms_fee.sell_date!=sms_fee.date_modified
AND t_date IS NULL AND basic_fee <0

 SELECT sms_fee.*, sms_fee.id as fee_id, sms_fee_sub1.tanka, sms_fee_sub1.suryo, smsevent_instances.from_datetime, smsevent_instances.instance_id
				FROM smsevent_instances 
				INNER JOIN sms_fee ON smsevent_instances.target_id=sms_fee.contact_id AND smsevent_instances.event_id = sms_fee.event_id AND sms_fee.deleted = 0				
				AND smsevent_instances.category='student'  AND (status & 33920) = 0 
				AND smsevent_instances.event_type='course'  AND sms_fee.fee_subcategory='受講料' 
				AND smsevent_instances.from_datetime >= '2014-01-01 00:00:00' AND smsevent_instances.from_datetime <= '2099-01-01 23:59:59' 				
				INNER JOIN sms_fee_sub1 ON sms_fee.id=sms_fee_sub1.id_sub1  AND (sms_fee_sub1.fee_shokubun IS NULL ) 
				INNER JOIN sms_transaction ON sms_fee.id=sms_transaction.fee_id AND sms_transaction.deleted=0 
				 INNER JOIN smsevent_sub1 ON smsevent_sub1.id_sub1=sms_fee.event_id AND nenki='201501' 
				WHERE (sms_transaction.t_date >= '2015-01-01' OR  smsevent_instances.from_datetime >= '2015-01-01' )  AND sms_transaction.t_date < '2015-02-01' 
				 AND smsevent_instances.event_id='ac94ba2e-4446-cc35-8f03-5462d2bd7013'  AND smsevent_instances.target_id='1510748'  AND sms_fee.contact_id='1510748'  AND sms_fee.school_id='d32c6192-e6be-2370-c9ff-5385aae9d6e2'  ORDER BY sms_fee.date_modified DESC, from_datetime ASC 
				 
				 
				 
 SELECT sms_fee.*, sms_fee.id as fee_id, sms_fee_sub1.tanka, sms_fee_sub1.suryo, smsevent_instances.from_datetime, smsevent_instances.instance_id
				FROM smsevent_instances 
				INNER JOIN sms_fee ON smsevent_instances.target_id=sms_fee.contact_id AND smsevent_instances.event_id = sms_fee.event_id AND sms_fee.deleted = 0				
				AND smsevent_instances.category='student'   AND (status & 987216) > 0 
				AND smsevent_instances.event_type='course'  AND sms_fee.fee_subcategory='受講料' 
				AND smsevent_instances.from_datetime >= '2014-01-01 00:00:00' AND smsevent_instances.from_datetime <= '2099-01-01 23:59:59' 				
				INNER JOIN sms_fee_sub1 ON sms_fee.id=sms_fee_sub1.id_sub1  AND sms_fee_sub1.fee_shokubun IN (1,2) 
				 INNER JOIN smsevent_sub1 ON smsevent_sub1.id_sub1=sms_fee.event_id AND nenki='201501' 
				WHERE  (sms_fee.sell_date >= '2015-01-01' OR  smsevent_instances.from_datetime >= '2015-01-01' )  AND sms_fee.sell_date < '2015-02-01' 
				 AND smsevent_instances.event_id='ac94ba2e-4446-cc35-8f03-5462d2bd7013'  AND smsevent_instances.target_id='1510748'  AND sms_fee.contact_id='1510748'  AND sms_fee.school_id='d32c6192-e6be-2370-c9ff-5385aae9d6e2'  ORDER BY sms_fee.date_modified DESC, from_datetime ASC 
				 
SELECT course_id, sum(kingaku) FROM maeukekin WHERE school_id='d32c6192-e6be-2370-c9ff-5385aae9d6e2' AND fee_subcategory='t' AND keijobi > '2015-01-00' GROUP BY course_id

SELECT course_id, student_id, sum(kingaku) FROM maeukekin WHERE school_id='d32c6192-e6be-2370-c9ff-5385aae9d6e2' AND fee_subcategory='t' AND keijobi > '2015-01-00' AND course_id='ac94ba2e-4446-cc35-8f03-5462d2bd7013' GROUP BY student_id 


SELECT tanka, t_date, sms_transaction.date_modified, sms_fee.* FROM sms_fee, sms_fee_sub1, sms_transaction 
WHERE id_sub1=sms_fee.id AND sms_transaction.fee_id=sms_fee.id AND sms_fee.contact_id='ec893b84-1b35-c814-666d-54d19b71c590' AND sms_fee.event_id='cdcc1d21-aaed-3430-d152-54d189124ac7' AND sms_fee.deleted=0 AND sms_transaction.deleted=0


 SELECT sms_fee.*, sms_fee.id as fee_id, sms_fee_sub1.tanka, sms_fee_sub1.suryo, smsevent_instances.from_datetime, smsevent_instances.instance_id
FROM smsevent_instances 
INNER JOIN sms_fee ON smsevent_instances.target_id=sms_fee.contact_id AND smsevent_instances.event_id = sms_fee.event_id AND sms_fee.deleted = 0				
AND smsevent_instances.category='student'  AND (status & 33920) = 0 
AND smsevent_instances.event_type='course'  AND sms_fee.fee_subcategory='受講料' 
AND smsevent_instances.from_datetime >= '2014-01-01 00:00:00' AND smsevent_instances.from_datetime <= '2099-01-01 23:59:59' 				
INNER JOIN sms_fee_sub1 ON sms_fee.id=sms_fee_sub1.id_sub1   
INNER JOIN sms_transaction ON sms_fee.id=sms_transaction.fee_id AND sms_transaction.deleted=0 
INNER JOIN smsevent_sub1 ON smsevent_sub1.id_sub1=sms_fee.event_id AND nenki='201501' 
WHERE (sms_fee_sub1.fee_shokubun=1 AND sms_fee.sell_date >= '2015-02-01') OR sms_transaction.t_date >= '2015-02-01' OR  smsevent_instances.from_datetime >= '2015-02-01' )  
	AND (sms_transaction.t_date IS NULL OR sms_transaction.t_date < '2015-02-10')
	AND smsevent_instances.event_id='cdcc1d21-aaed-3430-d152-54d189124ac7'  AND smsevent_instances.target_id='ec893b84-1b35-c814-666d-54d19b71c590'  AND sms_fee.contact_id='ec893b84-1b35-c814-666d-54d19b71c590'  
	AND sms_fee.school_id='e86510a2-302b-7c34-250d-5001d1fc1b41'  ORDER BY sms_fee.date_modified DESC, from_datetime ASC 

SELECT sms_fee_sub1.*, sms_fee.* FROM sms_fee, sms_fee_sub1 WHERE sms_fee.id=sms_fee_sub1.id_sub1 AND sms_fee.deleted=0 AND sms_fee.id='d302bfdc-b8e1-bd81-1585-54ae4ac151f3'
SELECT sms_fee_sub1.*, sms_fee.* FROM sms_fee, sms_fee_sub1 WHERE sms_fee.id=sms_fee_sub1.id_sub1 AND sms_fee.deleted=0 AND sms_fee.id='72ee719a-1222-55f1-208b-54ae4b37b538'

SELECT * FROM maeukekin WHERE course_id='72f5963a-f9a6-8fa0-b548-543e0e079a97' AND student_id='998183'

-- 2015-03-09
SELECT * FROM `maeukekin` WHERE date_modified > '2015-02-20' AND date_modified < '2015-03-01' AND keijobi LIKE '2015-01-%' AND `student_id` = '1509400' AND `course_id` = '1e2bcf4e-8c52-f27b-a293-544e22276596'
DELETE FROM `maeukekin` WHERE date_modified > '2015-02-20' AND date_modified < '2015-03-01' AND keijobi LIKE '2015-01-%' AND `student_id` = '1509400' AND `course_id` = '1e2bcf4e-8c52-f27b-a293-544e22276596'
SELECT * FROM `maeukekin` WHERE date_modified LIKE '2015-03-01%' AND `student_id` = '1509400' AND `course_id` = '1e2bcf4e-8c52-f27b-a293-544e22276596'
DELETE FROM `maeukekin` WHERE date_modified LIKE '2015-03-01%' AND `student_id` = '1509400' AND `course_id` = '1e2bcf4e-8c52-f27b-a293-544e22276596'
SELECT * FROM `maeukekin` WHERE date_modified LIKE '2015-03-07%' AND keijobi LIKE '2015-01-%' AND `student_id` = '1509400' AND `course_id` = '1e2bcf4e-8c52-f27b-a293-544e22276596'
DELETE FROM `maeukekin` WHERE date_modified LIKE '2015-03-07%' AND keijobi LIKE '2015-01-%' AND `student_id` = '1509400' AND `course_id` = '1e2bcf4e-8c52-f27b-a293-544e22276596'


SELECT * FROM `maeukekin` WHERE date_modified > '2015-02-20' AND date_modified < '2015-03-01' AND keijobi LIKE '2015-01-%' 
DELETE FROM `maeukekin` WHERE date_modified > '2015-02-20' AND date_modified < '2015-03-01' AND keijobi LIKE '2015-01-%' 
SELECT * FROM `maeukekin` WHERE date_modified LIKE '2015-03-01%' 
DELETE FROM `maeukekin` WHERE date_modified LIKE '2015-03-01%' 
SELECT * FROM `maeukekin` WHERE date_modified LIKE '2015-03-07%' AND keijobi LIKE '2015-01-%' 
DELETE FROM `maeukekin` WHERE date_modified LIKE '2015-03-07%' AND keijobi LIKE '2015-01-%' 
