高章伟
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
public with sharing class OverdueStockController {
 
    /*****************検索用******************/
    public String barcode { get; set; }
 
    // 过期和销存,画面显示用
    public List<orderBean> codPageRecords { get; set; }
 
    // 过期和销存,画面显示用
    public List<orderBean> overduePageRecords { get; set; }
 
    // 登录者工作地
    private String userWorkLocation;
    //经销商用户产品分类(ET、ENG)
    private String agencyProType;
    private String sqlagencyProType;
    // 经销商信息
    private Account accountInfo;
 
     //判断操作人员是否销存
    public Boolean iSinventory = false;
 
    List<Consumable_order_details2__c> overdueList = new List<Consumable_order_details2__c>();
 
    public Integer pageRecordsSize  {
        get {
            return codPageRecords == null ? 0 : codPageRecords.size();
        }
    }
 
    public OverdueStockController() {
 
    }
 
    // 画面初始化
    public void init() {
        codPageRecords = new List<orderBean>();
        overduePageRecords = new List<orderBean>();
        // 获取用户和经销商信息
        user Useracc = [select Accountid, Work_Location__c,UserPro_Type__c from user where id =: UserInfo.getUserId()];
        userWorkLocation = Useracc.Work_Location__c;
        agencyProType = Useracc.UserPro_Type__c;
        if(String.isBlank(Useracc.UserPro_Type__c)){
            agencyProType = 'ET';
        }
        sqlagencyProType = '%' + agencyProType + '%';
        accountInfo = [SELECT id,Name,Dealer_discount__c FROM account WHERE id =:Useracc.accountid];
        // 过期库存汇总信息
        List<AggregateResult>  orderDetZaiku = [SELECT count(Id) countsum,
                        Consumable_Product__c prodid,
                        Consumable_Product__r.Name_Text__c prodname,
                        Box_Piece__c boxPiece
                        FROM Consumable_order_details2__c
                        WHERE Dealer_Arrive__c = true
                        AND Dealer_Shipment__c = false
                        AND Dealer_Saled__c = false
                        AND Lose_Flag__c = false
                        AND Isoverdue__c = 0
                        AND Dealer_Returned__c = false
                        AND Cancellation_Flag__c = false
                        AND Bar_Code__c !=null
                        AND Product_Type__c like : sqlagencyProType
                        AND Dealer_Info_text__c = :accountInfo.Name
                        AND Arrive_Owner_Work_Location__c = :userWorkLocation
                        group by Consumable_Product__c,Box_Piece__c,Consumable_Product__r.Name_Text__c];
        for(AggregateResult overdue : orderDetZaiku){
            codPageRecords.add(new orderBean(overdue));
        }
 
    }
 
    // BarCode录入
    public void searchorderBean(){
        iSinventory = true;
        Set<String> barCodeoverdue = new Set<String>();
        overduePageRecords = new List<orderBean>();
        List<String> barCodeListP = ParseBarCode( barcode );
        // 页面显示数据初始化
        for (orderBean codPage : codPageRecords) {
            codPage.pandian = 0;
        }
        // 查询所有过期库存
        List<Consumable_order_details2__c>  orderDetZaiku = [select Id,  Name,Consumable_Product__c,
                            Bar_Code__c,Consumable_Product__r.Name__c,Dealer_Info_text__c,
                            Asset_Model_No__c,Isoverdue__c,Box_Piece__c,Sterilization_limit__c,
                            Bar_Code_search__c
                        FROM Consumable_order_details2__c
                        WHERE Dealer_Arrive__c = true
                        AND Dealer_Shipment__c = false
                        AND Dealer_Saled__c = false
                        AND Lose_Flag__c = false
                        AND Isoverdue__c = 0
                        AND Dealer_Returned__c = false
                        AND Cancellation_Flag__c = false
                        AND Bar_Code__c !=null
                        AND Product_Type__c like : sqlagencyProType
                        AND Dealer_Info_text__c = :accountInfo.Name
                        AND Arrive_Owner_Work_Location__c = :userWorkLocation];
        // 没有输入条码时,循环页面,所有打钩产品全部销存
        // 取得所有
        overdueList = new List<Consumable_order_details2__c>();
        if (String.isBlank(barcode)) {
            for(orderBean codPage : codPageRecords){
                if (codPage.check) {
                    for(Consumable_order_details2__c cod2 : orderDetZaiku){
                        if(cod2.Consumable_Product__c == codPage.prodid && codPage.boxPiece == cod2.Box_Piece__c){
                            overduePageRecords.add(new orderBean(cod2,'过期库存销存'));
                            overdueList.add(cod2);
                        }
                    }
                    codPage.pandian = codPage.overlimitCount;
                }
            }
            return;
        }
 
        // 输入barcode时
 
        // BarCode的检索  所有在库
        List<Consumable_order_details2__c> reSet = [select Id,Consumable_Product__c,
                        Consumable_Product__r.Name__c,Box_Piece__c,Bar_Code_search__c,
                        Dealer_Shipment__c,Dealer_Saled__c,Lose_Flag__c,Bar_Code__c,
                        Cancellation_Flag__c,Isoverdue__c,Sterilization_limit__c
                    from Consumable_order_details2__c
                    where Bar_Code_search__c in :BarCodeListP
                    AND Dealer_Arrive__c = true
                    AND Cancellation_Flag__c = false
                    and Dealer_Returned__c = false
                    and Bar_Code__c !=null
                    AND Product_Type__c like : sqlagencyProType
                    AND Arrive_Owner_Work_Location__c =: userWorkLocation
                    AND  Dealer_Info_text__c = :accountInfo.Name
                    order by Name];
 
 
        for(Consumable_order_details2__c cod2 : reSet){
            if ((cod2.Dealer_Shipment__c == false &&  cod2.Dealer_Saled__c == false
                && cod2.Lose_Flag__c == false && cod2.Cancellation_Flag__c == false) &&  cod2.Isoverdue__c == 0) {
                for (orderBean codPage : codPageRecords) {
                    if(cod2.Consumable_Product__c == codPage.prodid && cod2.Box_Piece__c == codPage.boxPiece){
                        overduePageRecords.add(new orderBean(cod2,'过期库存销存'));
                        overdueList.add(cod2);
                        codPage.pandian ++;
                        codPage.check = true;
                    }
                }
            }else if (cod2.Dealer_Shipment__c ||  cod2.Dealer_Saled__c) {
                overduePageRecords.add(new orderBean(cod2,'产品已经出库'));
            }else{
                overduePageRecords.add(new orderBean(cod2,'不是过期库存'));
            }
        }
    }
 
 
    // 保存按钮
    public PageReference save() {
        // 跳过明细2不必要更新
        StaticParameter.EscapeConsumableOrderDetail2Trigger = true;
        if(!iSinventory){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'请先点【BarCode录入】'));
            return null;
        }
        integer Lo = 0;
        for(orderBean header : codPageRecords){
            if(header.check==true){
                Lo++;
            }
        }
        if(Lo == 0){
            iSinventory = false;
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'请选择要销存的明细。'));
            return null;
        }
        Integer orderDetNo = 1;
        Savepoint sp = Database.setSavepoint();
        Consumable_order__c po = new Consumable_order__c();
        po.Name ='*';
        po.Order_status__c = '批准';
        po.Inventory_date__c = Date.today();
        po.Order_type__c = '销存';
        po.Dealer_Info__c = accountInfo.Id;
        po.Order_ProType__c = agencyProType;
        po.RecordTypeid = System.Label.RT_ConOrder_Overdue;
        try{
            insert  po;
            Consumable_order__c order = [SELECT Name FROM Consumable_order__c WHERE id =:po.id];
            List<Consumable_orderdetails__c> InsList = New List<Consumable_orderdetails__c>();
            for(orderBean header : codPageRecords){
                if(header.check==true){
                    Consumable_orderdetails__c insPan = new Consumable_orderdetails__c();
                    String str = string.valueOf(orderDetNo);
                    if(str.length() == 1){
                    str = '0' + str;
                    }
                    insPan.Name = order.Name + '-'+ str;
                    insPan.Consumable_Product__c = header.prodId;
                    insPan.Consumable_order__c = po.Id;
                    insPan.Lose_reason__c = '过期库存销存';
                    insPan.Overdue_count__c = header.pandian;
                    insPan.Overdue_SUM__c = header.overlimitCount;
                    insPan.RecordTypeId = System.Label.RT_ConOrderDetail1_Inventory;
                    orderDetNo ++;
                    InsList.add(insPan);
                }
            }
            // 生成明细1
            if(InsList.size()>0){
                insert InsList;
            }
 
            for(Consumable_order_details2__c cod : overdueList){
                cod.Consumable_Inventory_order__c = po.Id;
                cod.Inventory_date__c = Date.today();
                cod.Lose_reason__c = '过期库存销存';
                cod.Lose_Flag__c = true;
 
            }
            if(overdueList.size()>0){
                ControllerUtil.updateOrderDetailsSatus(overdueList);
            }
 
        }catch (Exception ex) {
            Database.rollback(sp);
            ApexPages.addMessages(ex);
            return null;
        }
        return new Pagereference('/' + po.Id);
    }
 
    // 将页面或取得BarCode去掉重复的,转换成List
    public List<String> ParseBarCode(String Code){
        Map<String,Integer> barcodeCountMap = new Map<String,Integer>();
        String[] Cache = new String[]{};
        Cache = Code.split('\n');
        List <String> Buff = new List<String>();
        for(String A : Cache){
            A = A.trim().toUpperCase();
            if(barcodeCountMap.containsKey(A)){
                barcodeCountMap.put(A,barcodeCountMap.get(A) + 1);
            }else{
                barcodeCountMap.put(A,1);
            }
            Buff.add(A + barcodeCountMap.get(A));
        }
        return Buff;
    }
 
     // Data Bean
    class orderBean implements Comparable {
 
        public Consumable_order_details2__c orderdetails2 { get; set; }
        public Product2__c Prod { get; set; }
        public Boolean check { get; set; }
        // 过期库存
        public Integer overlimitCount { get; set; }
        // 销存数量
        public Integer pandian { get; set; }
        // 销存原因
        public String diffReason { get; set; }
        // 单位
        public String boxPiece { get; set; }
        public String prodname { get; set; }
        public String prodid { get; set; }
        orderBean(Consumable_order_details2__c e,string str) {
            orderdetails2 = e;
            Prod = e.Consumable_Product__r;
            DiffReason = str;
        }
        public orderBean(AggregateResult e) {
            check = false;
            overlimitCount = Integer.valueOf(e.get('countsum'));
            prodname = String.valueOf(e.get('prodname'));
            prodid = String.valueOf(e.get('prodid'));
            boxPiece = String.valueOf(e.get('BoxPiece'));
            pandian = 0;
        }
        // 排序
        public Integer compareTo(Object compareTo) {
            orderBean compareToesd =(orderBean)compareTo;
            Integer returnValue = 0;
            if (overlimitCount  > compareToesd.overlimitCount ) {
                returnValue = -1;
            } else if (overlimitCount  < compareToesd.overlimitCount ) {
                returnValue = 1;
            }
            return returnValue;
        }
    }
}