public without sharing class SaleAndDeliveryController {
    public Consumable_order__c coc { get; set; }
    /*****************検索用******************/
    //经销商产品分类
    public String agencyProType {get;set;}
    //指示单号码
    public String category1 { get; set; }
    //客户名
    public String category2 { get; set; }
    //订单开始、结束日期
    private Date cate2 = Date.today();
    private Date cate3 = Date.today();
    // 显示数据条数限制
    private Integer Select_Limit = 100;
    // 登录用户 ID
    private String userinfoId = '';
    //经销商id
    private String accountid = null;
    // 订单 字段标签
    public static List<String> getTitle() {
        // 获得订单一览
        Map<String, Schema.FieldSet> fsMap = Schema.getGlobalDescribe().get('Consumable_order__c').getDescribe().fieldSets.getMap();
        Schema.FieldSet fs = fsMap.get('SaleAndDelivery');
        // 获得订单中的所有项目
        List<FieldSetMember> fsmList = fs.getFields();
        // 获得字段标签和字段名
        List<String> title = new List<String>();
        for (FieldSetMember fsm : fsmList) {
            if ('Name' == fsm.getFieldPath()) {
                title.add('出库单号');
            } else {
                title.add(fsm.getLabel());
            }
        }
        return title;
    }
    //排序使用
    private String[] orderby = new String[]{ 'Outbound_Date__c','Name','ShipmentAccount__c','SummonsStatus_c__c','Billed_Status__c','CreatedDate'};
    //排序用
    public String sortKey { get; set; }
    public String preSortKey { get; private set; }
    public Boolean sortOrderAsc { get; private set; }
    public String[] sortOrder { get; private set; }

    // 订单 字段名
    public List<String> column;
    public List<List<String>> columns { get; private set; }
    // 画面显示数据
    public List<Consumable_order__c> raesList { get; private set; }

    // 登录者工作地
    private String userWorkLocation;

    public SaleAndDeliveryController() {
    }
    //========20160311======ADD_Start================================
    // 将订单明细以字段SET 方式显示
    //========20160311======ADD_Start================================
    public void init1() {
        this.sortKey = '1';
        this.preSortKey = '1';
        this.sortOrderAsc = false;
        this.sortOrder = new String[4];
        this.sortOrder = new String[]{' ',' ',' ',' ',' ',' '};
        coc = new Consumable_order__c();
        userinfoId = UserInfo.getUserId();
        List<user> Useracc = New List<user>();
        Useracc = [SELECT
                        accountid, Work_Location__c,UserPro_Type__c
                    FROM user
        WHERE id =:userinfoId];
        accountid = Useracc[0].accountid;
        agencyProType = Useracc[0].UserPro_Type__c;
        if(String.isBlank(Useracc[0].UserPro_Type__c)){
            agencyProType = 'ET';
        }
        userWorkLocation = Useracc[0].Work_Location__c;
        //默认检索显示未完成的指示单
        cleanUp();
    }
    //========20160311======ADD_End================================
     //新建出库指示
    public PageReference neworderhead() {
        // 返回备品set
        PageReference ref = new Pagereference('/summonsCreat');
        ref.setRedirect(true);
        return ref;
    }
    // 未完成出库单检索
    public void searchConsumableorderdetails() {
        this.sortKey = '1';
        this.preSortKey = '1';
        this.sortOrderAsc = false;
        this.sortOrder = new String[4];
        this.sortOrder = new String[]{' ',' ',' ',' ',' ',' '};
        //coc = new Consumable_order__c();
        cate2 = coc.Order_date__c;
        cate3 = coc.Deliver_date__c;
        // 获得订单一览
        raesList = new List<Consumable_order__c>();
        String soql = 'select Id, Name,ShipmentAccount__c,SummonsStatus_c__c,Billed_Status__c,CreatedDate,Outbound_Date__c,Order_ForCustomerText__c,InvoiceNotPro_total_amount__c,InvoiceNotPro_money__c from Consumable_order__c';
        soql += ' WHERE   Order_type__c = \'' + '传票' + '\' ';
        soql += ' and Dealer_Info__c =\'' + accountid +'\' ';
        soql += ' and Order_ProType__c =\'' + agencyProType +'\' ';
        soql += ' and Order_Owner_WorkLocal__c = \'' + userWorkLocation + '\' ';
        if (!String.isBlank(category1)) {
            soql += ' and Name like \'%' + String.escapeSingleQuotes(category1.replaceAll('%', '\\%')) + '%\' ';
        }
        if (!String.isBlank(category2)) {
            soql += ' and ShipmentAccount__c like \'%' + String.escapeSingleQuotes(category2.replaceAll('%', '\\%')) + '%\' ';
        }
        if(cate2 != null){
            soql += ' and Order_date__c >= :cate2 ';
        }
        if(cate3 != null){
            soql += ' and Order_date__c <= :cate3 ';
        }
        soql += 'and SummonsStatus_c__c != \'' + '已完成' + '\' ';
        soql += 'order by SummonsStatus_c__c';
        System.debug('soql+++++' + soql);
        raesList = Database.query(soql);
        if(raesList.size()>0){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '共有' + raesList.size() + '个出库单'));
        }else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '没有搜索到相关出库单。'));
        }
     }


    //排序
    public void SortLimited(){
        List<Id> orderIdList = new List<Id>();
        for(Integer i = 0 ; i< raesList.size();i++){
            orderIdList.add(raesList[i].Id);
        }
        if (this.sortKey == this.preSortKey) {
            // 方向が変わるのみ
            this.sortOrderAsc = !this.sortOrderAsc;
            this.sortOrder[Integer.valueOf(this.sortKey)] = (this.sortOrderAsc == true ? '↑' : '↓');
        } else {
            this.sortOrderAsc = true;
            this.sortOrder[Integer.valueOf(this.preSortKey)] = ' ';
            this.sortOrder[Integer.valueOf(this.sortKey)] = (this.sortOrderAsc == true ? '↑' : '↓');
        }

        this.preSortKey = this.sortKey;
        // 所有产品取得
        String soql = 'select Id, Name,ShipmentAccount__c,SummonsStatus_c__c,Billed_Status__c,CreatedDate,Outbound_Date__c,Order_ForCustomerText__c,InvoiceNotPro_total_amount__c,InvoiceNotPro_money__c from Consumable_order__c';
        soql += ' WHERE Id in :orderIdList';
        //soql += ' order by Name';
        soql += ' order by ' + this.orderby[Integer.valueOf(this.sortKey)];
        soql += ' ' + (this.sortOrderAsc == true ? 'asc nulls first' : 'desc nulls last');
        System.debug('soql+++++' + soql);
        List<Consumable_order__c> queryList = Database.query(soql);
        // 選択済みの明细を取得
        //raesList = new List<Consumable_order__c>();
        raesList = queryList;
    }

    // 已完成出库单检索
    public void searchConsumableorFinish() {
        this.sortKey = '1';
        this.preSortKey = '1';
        this.sortOrderAsc = false;
        this.sortOrder = new String[4];
        this.sortOrder = new String[]{' ',' ',' ',' ',' ',' '};
        cate2 = coc.Order_date__c;
        cate3 = coc.Deliver_date__c;
        raesList = new List<Consumable_order__c>();
        String soql = 'select Id, Name,ShipmentAccount__c,SummonsStatus_c__c,Billed_Status__c,CreatedDate,Outbound_Date__c,Order_ForCustomerText__c,InvoiceNotPro_total_amount__c,InvoiceNotPro_money__c from Consumable_order__c';
        soql += ' WHERE   Order_type__c = \'' + '传票' + '\' ';
        soql += ' and Dealer_Info__c =\'' + accountid +'\' ';
        soql += ' and Order_ProType__c =\'' + agencyProType +'\' ';
        soql += ' and Order_Owner_WorkLocal__c = \'' + userWorkLocation + '\' ';
        if (!String.isBlank(category1)) {
            soql += ' and Name like \'%' + String.escapeSingleQuotes(category1.replaceAll('%', '\\%')) + '%\' ';
        }
        if (!String.isBlank(category2)) {
            soql += ' and ShipmentAccount__c like \'%' + String.escapeSingleQuotes(category2.replaceAll('%', '\\%')) + '%\' ';
        }
        if(cate2 != null){
            soql += ' and Outbound_Date__c >= :cate2 ';
        }
        if(cate3 != null){
            soql += ' and Outbound_Date__c <= :cate3 ';
        }
        soql += 'and SummonsStatus_c__c = \'' + '已完成' + '\' ';
        soql += 'and Billed_Status__c = \'' + '全部开票' + '\' ';
        soql += 'order by CreatedDate desc limit ' + Select_Limit;
        raesList = Database.query(soql);
        if(raesList.size()>0){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '共有' + raesList.size() + '个出库单'));
        }else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '没有搜索到相关出库单。'));
        }
     }

     // 未完成开票出库单检索
    public void searchOrderInstatus() {
        this.sortKey = '1';
        this.preSortKey = '1';
        this.sortOrderAsc = false;
        this.sortOrder = new String[4];
        this.sortOrder = new String[]{' ',' ',' ',' ',' ',' '};
        cate2 = coc.Order_date__c;
        cate3 = coc.Deliver_date__c;
        // 获得订单一览
        raesList = new List<Consumable_order__c>();
        String soql = 'select Id, Name,ShipmentAccount__c,SummonsStatus_c__c,Billed_Status__c,CreatedDate,Outbound_Date__c,Order_ForCustomerText__c,InvoiceNotPro_total_amount__c,InvoiceNotPro_money__c from Consumable_order__c';
        soql += ' WHERE   Order_type__c = \'' + '传票' + '\' ';
        soql += ' and Dealer_Info__c =\'' + accountid +'\' ';
        soql += ' and Order_ProType__c =\'' + agencyProType +'\' ';
        soql += ' and Order_Owner_WorkLocal__c = \'' + userWorkLocation + '\' ';
        if (!String.isBlank(category1)) {
            soql += ' and Name like \'%' + String.escapeSingleQuotes(category1.replaceAll('%', '\\%')) + '%\' ';
        }
        if (!String.isBlank(category2)) {
            soql += ' and ShipmentAccount__c like \'%' + String.escapeSingleQuotes(category2.replaceAll('%', '\\%')) + '%\' ';
        }
        if(cate2 != null){
            soql += ' and Outbound_Date__c >= :cate2 ';
        }
        if(cate3 != null){
            soql += ' and Outbound_Date__c <= :cate3 ';
        }
        soql += 'and SummonsForDirction__c != \'' + '互相调货' + '\' ';
        soql += 'and SummonsStatus_c__c = \'' + '已完成' + '\' ';
        soql += 'and Billed_Status__c != \'' + '全部开票' + '\' ';
        soql += 'order by CreatedDate desc limit ' + Select_Limit;
        raesList = Database.query(soql);
        if(raesList.size()>0){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '共有' + raesList.size() + '个出库单'));
        }else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '没有搜索到相关出库单。'));
        }
     }

     public void cleanUp() {
        this.sortKey = '1';
        this.preSortKey = '1';
        this.sortOrderAsc = false;
        this.sortOrder = new String[4];
        this.sortOrder = new String[]{' ',' ',' ',' ',' ',' '};
        //清空数据
        coc.Order_date__c = null;
        coc.Deliver_date__c = null;
        this.category1 = '';
        this.category2 = '';
        // 获得显示数据
        raesList = new List<Consumable_order__c>();
        String soql = 'select Id, Name,ShipmentAccount__c,SummonsStatus_c__c,Billed_Status__c,CreatedDate,Outbound_Date__c,Order_ForCustomerText__c,InvoiceNotPro_total_amount__c,InvoiceNotPro_money__c from Consumable_order__c';
        soql += '  WHERE Order_type__c = \'' + '传票' + '\'  ';
        soql += ' and Order_ProType__c =\'' + agencyProType +'\' ';
        soql += 'and SummonsStatus_c__c != \'' + '已完成' + '\' and Dealer_Info__c =\'' + accountid +'\' ';
        soql += 'and Order_Owner_WorkLocal__c = \'' + userWorkLocation + '\' ';
        soql += 'order by SummonsStatus_c__c';
        raesList = Database.query(soql);
    }
}