public without sharing class searchContractController {
	public List<Account> at {get;set;}
	private String accountid = null;
	public String searchName {get;set;}               // 検索文字列
	private String ctype;
	Date todayDate = Date.today();
	public searchContractController() {
		ctype = ApexPages.currentPage().getParameters().get('Ctype');
		todayDate = Date.today();

	}
	public void init(){
		User useracc = [SELECT accountid FROM user WHERE id = :UserInfo.getUserId() ];
        accountid = Useracc.accountid;
        at = [SELECT id,Name,State_Master__c,State_Master__r.Name,Sales_Section__c,
        			Contract_Decide_Start_Date__c,Contract_Decide_End_Date__c
                FROM Account
                WHERE ParentId = :accountid
                AND Contact_Type__c like :ctype
        		AND Contract_Decide_Start_Date__c <= :Date.Today()
                AND Contract_Decide_End_Date__c >= :Date.Today()
                AND Secondary_contract__c = false];

	}
	public PageReference serContact(){
		at = Database.query(this.makeSoql(searchName,todayDate));
		return null;
	}

	private String makeSoql(String CateName,Date timetest){
		
        String soql = 'SELECT id,Name,State_Master__c,State_Master__r.Name,Sales_Section__c,';
        soql += ' Contract_Decide_Start_Date__c,Contract_Decide_End_Date__c FROM Account';
        soql += ' where ParentId = \'' + this.accountid + '\'' ;
        soql += ' AND Secondary_contract__c = false' ;
        soql += ' AND Contact_Type__c like \'%' + String.escapeSingleQuotes(ctype.replaceAll('%', '\\%')) + '%\'';
        soql += ' AND Contract_Decide_Start_Date__c <=' + String.valueOf(timetest).substring(0, 10);
        soql += ' AND Contract_Decide_End_Date__c >= ' + String.valueOf(timetest).substring(0, 10);
        if(!String.isBlank(CateName)){
            soql += ' AND Name like \'%' + String.escapeSingleQuotes(CateName.replaceAll('%', '\\%')) + '%\'';
        }
        return soql;
    }
}