高章伟
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
public without sharing class MoreMaintenanceContractController {
    public Maintenance_Contract__c mc {get; set;}
    public List<RepairInfo> repairL  {get; set;}
    public Integer proportion {get; set;}
    public Integer GoFlag{get; set;}
    //当前的维修合同id
    public String Id {get; set;}
    public String RecordTypeName {get;set;}
    public Boolean isVM {get;set;} //20210106 zh LJPH-BWJ8PM 制作报告书按钮
    public Integer repairLCount{ 
        get {
            return repairL == null ? 0 : repairL.size();
        }
    }
    public Boolean HATextEdit {get;set;}
    public MoreMaintenanceContractController() {
        Id = ApexPages.currentPage().getParameters().get('Id');
        RecordTypeName = ApexPages.currentPage().getParameters().get('RecordTypeName');
        repairL = new List<RepairInfo>();
        //20210106 zh LJPH-BWJ8PM 制作报告书按钮 start
        isVM = false;
        if ('VM_Contract'.equals(RecordTypeName)) {
            isVM = true;
        }
        //20210106 zh LJPH-BWJ8PM 制作报告书按钮 end
    }
    public void init(){
        mc = [Select Id,Name,HospitalAmountText__c,hospitalQuotationTextSum__c,Management_Code__c,Status__c,RecordType.Name,Contract_Conclusion_Date__c,Contract_Start_Date__c from Maintenance_Contract__c where id =:Id];
        /*[经销商对医院签约价格]当该项有值时,则在合同开始日的3个月时点,信息保存锁死,无法修改。
        合同开始日指:
        1)合同状态为询价中的,不锁定
        2)合同状态为【合同中】的合同:合同日和合同开始日取晚的日期,加3个月作为期限值。
        */
        HATextEdit = false;
        if(mc.Contract_Conclusion_Date__c != null || mc.Contract_Start_Date__c != null){
            Date MaxDate = null; 
            if(mc.Contract_Conclusion_Date__c == null){
                MaxDate = mc.Contract_Start_Date__c.addMonths(3);
            }else if(mc.Contract_Start_Date__c == null){
                MaxDate = mc.Contract_Conclusion_Date__c.addMonths(3);
            }else{
                Integer daysBetween = mc.Contract_Conclusion_Date__c.daysBetween(mc.Contract_Start_Date__c);
                //mc.Contract_Start_Date__c-mc.Contract_Conclusion_Date__c
                if(daysBetween >= 0){
                    MaxDate = mc.Contract_Start_Date__c.addMonths(3);
                }else{
                    MaxDate = mc.Contract_Conclusion_Date__c.addMonths(3);
                }  
            }
            if(mc.Status__c.equals('契約') && MaxDate != null && Date.today() <= MaxDate){
                HATextEdit = true;
            }
        }
        if(mc.HospitalAmountText__c == NULL || mc.Status__c.equals('引合中')){
            HATextEdit = true;
        }
        List<Repair__c> repairList = new List<Repair__c>();
        if(RecordTypeName !=null&&RecordTypeName.equals('NewMaintenance_Contract')){
            repairList = [Select Id, Name,Status1__c,Delivered_Product__c,Delivered_Product__r.Name,SerialNumber__c,Repair_List_Price_formula__c,Repair_Rank__c,
                    hospitalQuotationText__c from Repair__c where  Maintenance_Contract__c =:Id and Status1__c !='0.取消' and Status1__c != '0.删除' order by hospitalQuotationText__c];
        }else if(RecordTypeName !=null&&RecordTypeName.equals('VM_Contract')){
            repairList = [Select Id, Name,Status1__c,Delivered_Product__c,Delivered_Product__r.Name,SerialNumber__c,Repair_List_Price_formula__c,Repair_Rank__c,
                    hospitalQuotationText__c from Repair__c where  VM_Maintenance_Contract__c =:Id and Status1__c !='0.取消' and Status1__c != '0.删除' order by hospitalQuotationText__c ];
        }
        Map<String, RepairInfo> MidMap = new Map<String, RepairInfo>();
        for (Integer i = 0; i < repairList.size(); i++) {
            MidMap.put(repairList[i].Id, new RepairInfo(repairList[i]));
        }
        for (RepairInfo rr : MidMap.values()) {
            repairL.add(rr);
        }
        for(RepairInfo rl:repairL){
            //if(rl.rr.hospitalQuotationText__c ==null||rl.rr.hospitalQuotationText__c ==0.00){
            if(rl.rr.hospitalQuotationText__c ==null){
                rl.edit = true;
            }
        }
    }
    public void RefreshPrice(){
        if(proportion !=null){
            //合同期间修理金额(上传)
            Decimal temphospitalQuotationTextSum = 0.00;
            for(RepairInfo rl:repairL){
                if(rl.edit){
                    rl.rr.hospitalQuotationText__c = rl.rr.Repair_List_Price_formula__c*proportion/100;
                }
                temphospitalQuotationTextSum +=rl.rr.hospitalQuotationText__c;
            }
            mc.hospitalQuotationTextSum__c = temphospitalQuotationTextSum;
        }else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,  '请输入经销商对医院修理价格调整比例'));
        }
    }
    public void HQSum(){
        Decimal temphospitalQuotationTextSum = 0.00;
        for(RepairInfo rl:repairL){
            if(rl.rr.hospitalQuotationText__c != null){
                temphospitalQuotationTextSum +=rl.rr.hospitalQuotationText__c;
            }  
        }
        mc.hospitalQuotationTextSum__c = temphospitalQuotationTextSum;
        proportion = String.isBlank(proportion.format())?null:proportion;
    }
    
    public PageReference Save(){
        //更新维修合同和修理
        Maintenance_Contract__c mcNew = new Maintenance_Contract__c();
        List<Repair__c> repairLNew = new List<Repair__c>();
        Savepoint sp = Database.setSavepoint();
        try {
            /*Boolean isHaveNull = false;
            for(RepairInfo rl:repairL){
                if(rl.rr.hospitalQuotationText__c==null && rl.edit){
                    isHaveNull = true;
                }
            }
            if(isHaveNull){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,  '修理价格存在空项,请确认是否保存'));
                //return null;
            }*/
            //else{
                mcNew.Id = Id;
                mcNew.HospitalAmountText__c = mc.HospitalAmountText__c;                
                Decimal temphospitalQuotationTextSum = 0.00;
                for(RepairInfo rl:repairL){
                    Repair__c rUpdate = new Repair__c();
                    rUpdate.Id = rl.rr.Id;
                    rUpdate.hospitalQuotationText__c = rl.rr.hospitalQuotationText__c;
                    repairLNew.add(rUpdate);
                    if(rl.rr.hospitalQuotationText__c !=null){
                         temphospitalQuotationTextSum +=rl.rr.hospitalQuotationText__c;
                    } 
                }
                mcNew.hospitalQuotationTextSum__c = temphospitalQuotationTextSum;
                update mcNew;
                update repairLNew;
                return GoURL();
            //}
            
        } catch (Exception ex) {
            ApexPages.addMessages(ex);
            Database.rollback(sp);
            return null;
        }   
    }
    public PageReference GoURL() {
        PageReference ref;
        if(GoFlag==0){
            ref = new Pagereference('/apex/MoreMaintenanceContract?Id=' +Id+'&RecordTypeName='+RecordTypeName);
        }else if(GoFlag==1){
            ref = new Pagereference('http://powerbi.olympus.com.cn/Home/Login');
        }
        ref.setRedirect(true);
        return ref;
    }
    class RepairInfo{
        public Boolean edit { get; set; }
        public Repair__c rr {get; set;}
        public RepairInfo(Repair__c e) {
            rr = e;
            edit = false;
        }
    }  
}