高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
public with sharing class ConInvoiceListController {
    public Consumable_order__c coc { get; set; }
    /*****************検索用******************/
    //经销商产品分类
    public String agencyProType {get;set;}
    //发票号码
    public String category1 { get; set; }
    //客户名
    public String category2 { get; set; }
    //发票开始、结束日期
    private Date cate1 = null;
    private Date cate2 = null;
    // 登录用户 ID
    private String userinfoId = '';
    private String invoiceStatus = '';
    //经销商id
    private String accountid = null;
    // 画面显示数据
    public List<Consumable_order__c> raesList { get; private set; }
 
    //排序使用
    private String[] orderby = new String[]{ 'Invoice_Date__c','Name','ShipmentAccount__c'};
    public String sortKey { get; set; }
    public String preSortKey { get; private set; }
    public Boolean sortOrderAsc { get; private set; }
    public String[] sortOrder { get; private set; }
    // 登录者工作地
    private String userWorkLocation;
 
    public ConInvoiceListController() {
        raesList = new List<Consumable_order__c>();
    }
 
    public void init() {
        this.sortKey = '1';
        this.preSortKey = '1';
        this.sortOrderAsc = false;
        this.sortOrder = new String[2];
        this.sortOrder = new String[]{' ',' ',' ',' '};
 
        coc = new Consumable_order__c();
        cate1 = coc.Order_date__c;
        cate2 = coc.Deliver_date__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;
        userWorkLocation = Useracc[0].Work_Location__c;
        agencyProType = Useracc[0].UserPro_Type__c;
        if(String.isBlank(Useracc[0].UserPro_Type__c)){
            agencyProType = 'ET';
        }
        invoiceStatus = coc.Invoice_status__c;
        String soql = this.makeSoql(category1,category2,invoiceStatus);
        raesList = Database.query(soql);
    }
 
    // 发票单检索
    public void invoiceCodeSearch() {
        this.sortKey = '1';
        this.preSortKey = '1';
        this.sortOrderAsc = false;
        this.sortOrder = new String[2];
        this.sortOrder = new String[]{' ',' ',' ',' '};
 
        cate1 = coc.Order_date__c;
        cate2 = coc.Deliver_date__c;
        invoiceStatus = coc.Invoice_status__c;
        // 获得发票一览
        raesList = new List<Consumable_order__c>();
        String soql = this.makeSoql(category1,category2,invoiceStatus);
        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 clearUp() {
        this.sortKey = '1';
        this.preSortKey = '1';
        this.sortOrderAsc = false;
        this.sortOrder = new String[2];
        this.sortOrder = new String[]{' ',' ',' ',' '};
 
        cate1 = coc.Order_date__c = null;
        cate2 = coc.Deliver_date__c = null;
        this.category1 = '';
        this.category2 = '';
        invoiceStatus = coc.Invoice_status__c = null;
        // 获得发票一览
        raesList = new List<Consumable_order__c>();
        String soql = this.makeSoql(category1,category2,invoiceStatus);
        raesList = Database.query(soql);
     }
 
    //排序
    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,Invoice_Date__c,ShipmentAccount__c,Shipment_total_amount__c,RrturnPro_total_amount__c,Invoice_status__c, ';
        soql += ' InvoicedPro_total_amount__c,InvoiceNotPro_total_amount__c,Invoice_total_amount__c,Invoice_attachment__c,Invoicedet_attachment__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 = queryList;
    }
 
    // 做成检索SQL文
    private String makeSoql(String invoiceCode,String accountName,String invoiceStatus){
        String soql = 'SELECT Id, Name,Invoice_Date__c,ShipmentAccount__c,Shipment_total_amount__c,RrturnPro_total_amount__c,Invoice_status__c, ';
        soql += ' InvoicedPro_total_amount__c,InvoiceNotPro_total_amount__c,Invoice_total_amount__c,Invoice_attachment__c,Invoicedet_attachment__c FROM Consumable_order__c ';
        soql += ' WHERE Dealer_Info__c = \'' +  accountid + '\'';
        soql += ' AND Order_Owner_WorkLocal__c = \'' + userWorkLocation + '\' ';
        soql += ' and Order_ProType__c =\'' + agencyProType +'\' ';
        soql += ' AND recordtypeid = \''+ System.Label.RT_ConOrder_Invoice + '\'';
        if(cate1 != null){
            soql += ' AND Invoice_Date__c >= :cate1 ' ;
        }
        if(cate2 != null){
            soql += ' AND Invoice_Date__c <= :cate2 ' ;
        }
         if(!String.isBlank(invoiceCode)){
            soql += ' AND Name like \'%' + String.escapeSingleQuotes(invoiceCode.replaceAll('%', '\\%')) + '%\' ';
        }
         if(!String.isBlank(accountName)){
            soql += ' AND ShipmentAccount__c like \'%' + String.escapeSingleQuotes(accountName.replaceAll('%', '\\%')) + '%\' ';
        }
        if(!String.isBlank(invoiceStatus)){
            soql += ' AND Invoice_status__c =\'' + invoiceStatus +'\' ';
        }
        System.debug('soql+++++' + soql);
        return soql;
    }
}