From 3962c2bb0435484b60a3e408e4738d792e249a53 Mon Sep 17 00:00:00 2001
From: buli <137736985@qq.com>
Date: 星期一, 05 六月 2023 11:09:55 +0800
Subject: [PATCH] LEX CommunityNewCmp

---
 force-app/main/default/classes/LexConInvoiceViewController.cls | 1629 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 1,629 insertions(+), 0 deletions(-)

diff --git a/force-app/main/default/classes/LexConInvoiceViewController.cls b/force-app/main/default/classes/LexConInvoiceViewController.cls
new file mode 100644
index 0000000..0173b6c
--- /dev/null
+++ b/force-app/main/default/classes/LexConInvoiceViewController.cls
@@ -0,0 +1,1629 @@
+public without sharing class LexConInvoiceViewController {
+    public static String agencyProType { get; set; }
+    public static Consumable_order__c coc { get; set; }
+    public static Consumable_order__c outbound { get; set; }
+    public static String invoiceId { get; set; }
+    public static String orderRemind { get; set; }
+    public static String deliveryId { get; set; }
+    public static String reopen { get; set; }
+    public static Decimal sumPrice { get; set; }
+    public static Boolean isChange { get; set; }
+    public static String accountid { get; set; }
+    public static String userId { get; set; }
+    public static String errorMsg { get; set; }
+
+    public static Boolean getdone() {
+        return (invoiceOrderdetail1Recoeds.size() > 0);
+    }
+
+    public static Boolean getExistOutbound() {
+        return (coc.Outbound_order__c != null);
+    }
+
+    public static String getInvoiceName() {
+        return (coc.Name);
+    }
+    public static List<Consumable_Orderdetails__c> conoList { get; set; }
+
+    public static String shipmentAccount {
+        get {
+            return (coc.ShipmentAccount__c);
+        }
+    }
+    public static String statusEdit = '';
+    public static Boolean editAble { get; set; }
+    public static List<InvoiceOrderInfo> invoiceOrderRecoeds { get; set; }
+    public static List<InvoiceOrderInfo> invoiceOrderdetail1Recoeds { get; set; }
+    public static List<InvoiceOrderInfo> invoiceOrderRecoedschange { get; set; }
+    public static List<InvoiceOrderInfo> attachmentRecoeds { get; set; }
+    public static List<Consumable_order__c> invoiceOrderList { get; set; }
+    public static List<Consumable_order__c> invoiceOrderSelectedList { get; set; }
+    public static Map<String, Decimal> ordermx1defaultMap { get; set; }
+    public static Map<String, Decimal> outorderMoneytMap { get; set; }
+    public static Map<String, Consumable_Orderdetails__c> outordercountMap { get; set; }
+    public static List<String> outOrderStringList { get; set; }
+    public static List<SelectOption> provinceOpts { get; set; }
+    public static Map<String, String> provinceOptsMap { get; set; }
+    public static String SecondDealer { get; set; }
+    public static String HospitalInfo { get; set; }
+    public static String HospitalName { get; set; }
+
+    public static Integer invoiceOrderRecoedsCount {
+        get {
+            return invoiceOrderRecoeds == null ? 0 : invoiceOrderRecoeds.size();
+        }
+    }
+
+    public static list<Dealer_elationship__c> Dealerelationship { get; set; }
+    public static String[] orderby = new List<String>{ 'Outbound_Date__c', 'Name', 'ShipmentAccount__c' };
+    public static String sortKey { get; set; }
+    public static String preSortKey { get; set; }
+    public static Boolean sortOrderAsc { get; set; }
+    public static String[] sortOrder { get; set; }
+    public static String userWorkLocation { get; set; }
+
+    //椤甸潰鍒濆鍖�
+    @AuraEnabled
+    public static ResponseBodyLWC init(String invoiceId, String statusEdit, String deliveryId) {
+        ResponseBodyLWC res = new ResponseBodyLWC();
+        Map<String, object> data = new Map<String, object>();
+        res.entity = data;
+
+        provinceOpts = new List<SelectOption>();
+        provinceOptsMap = new Map<String, String>();
+        invoiceOrderRecoeds = new List<InvoiceOrderInfo>();
+        attachmentRecoeds = new List<InvoiceOrderInfo>();
+        invoiceOrderdetail1Recoeds = new List<InvoiceOrderInfo>();
+        invoiceOrderRecoedschange = new List<InvoiceOrderInfo>();
+        orderRemind = '';
+        isChange = false;
+        conoList = new List<Consumable_Orderdetails__c>();
+        outorderMoneytMap = new Map<String, Decimal>();
+        outOrderStringList = new List<String>();
+        outordercountMap = new Map<String, Consumable_Orderdetails__c>();
+
+        sumPrice = 0;
+        // this.sortKey = '1';
+        // this.preSortKey = '1';
+        // this.sortOrderAsc = false;
+        // this.sortOrder = new List<String>(2);
+        // this.sortOrder = new List<String>{ ' ', ' ', ' ', ' ' };
+
+        invoiceId = invoiceId;
+        statusEdit = statusEdit;
+        deliveryId = deliveryId;
+
+        if (invoiceId != null && invoiceId != '' && (statusEdit == '' || statusEdit == null)) {
+            editAble = false;
+        } else if ((invoiceId == null || invoiceId == '') && (statusEdit == '' || statusEdit == null)) {
+            editAble = true;
+        } else if (invoiceId != null && invoiceId != '' && statusEdit != '' && statusEdit != null) {
+            editAble = true;
+        }
+        userId = UserInfo.getUserId();
+        List<user> Useracc = new List<user>();
+        Useracc = [SELECT accountid, Work_Location__c, UserPro_Type__c FROM user WHERE id = :userId];
+        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';
+        }
+        coc = new Consumable_order__c();
+
+        Dealerelationship = [
+            SELECT Dealer_subordinate__c, Dealer_subordinate__r.Name
+            FROM Dealer_elationship__c
+            WHERE Dealer_principal__c = :accountid
+        ];
+        List<Agency_Hospital_Link__c> AgencyHospitalLink = new List<Agency_Hospital_Link__c>();
+        AgencyHospitalLink = [
+            SELECT Id, Name, Hospital__c, Hospital__r.Id, Hospital__r.Name, Agency__c
+            FROM Agency_Hospital_Link__c
+            WHERE Agency__c = :accountId
+        ];
+        // 鏂拌
+        if (invoiceId == null || invoiceId == '') {
+            if (deliveryId == null || deliveryId == '') {
+                String msoql = makeSoql();
+                invoiceOrderList = Database.query(msoql);
+                for (Integer i = 0; i < invoiceOrderList.size(); i++) {
+                    invoiceOrderRecoeds.add(new InvoiceOrderInfo(invoiceOrderList[i]));
+                }
+            } else {
+                if (statusEdit == '' || statusEdit == null) {
+                    EditAble = false;
+                } else if (statusEdit != '' && statusEdit != null) {
+                    EditAble = true;
+                }
+                coc = [
+                    SELECT
+                        Id,
+                        Name,
+                        Invoice_status__c,
+                        Invoice_Date__c,
+                        Order_ForDealerText__c,
+                        Dealer_Info__c,
+                        Order_ForDealer__c,
+                        Order_ForDealer__r.Name,
+                        Order_ForHospital__c,
+                        Order_ForHospital__r.Name,
+                        Order_date__c,
+                        Billed_Status__c,
+                        ShipmentAccount__c,
+                        Order_Dealer_Info__c,
+                        SummonsForDirction__c,
+                        Order_ForCustomerText__c,
+                        Invoice_Note__c,
+                        Invoice_attachment__c,
+                        Invoicedet_attachment__c,
+                        Outbound_order__c
+                    FROM Consumable_order__c
+                    WHERE Id = :deliveryId
+                ];
+                coc.Name = '';
+                if (coc.Order_ForDealer__c != null) {
+                    SecondDealer = coc.Order_ForDealer__c;
+                }
+
+                if (coc.Order_ForHospital__c != null) {
+                    HospitalInfo = coc.Order_ForHospital__c;
+                    HospitalName = coc.Order_ForHospital__r.Name;
+                }
+
+                //鍙戠エ鍏宠仈鍑哄簱鍗曞彇寰楋紙鍑哄簱鍗曪級
+                invoiceOrderSelectedList = [
+                    SELECT
+                        Id,
+                        Order_ForDealer__r.Name,
+                        Order_ForHospital__r.Name,
+                        Order_ForDealerText__c,
+                        Name,
+                        Outbound_Date__c,
+                        ShipmentAccount__c,
+                        Order_Dealer_Info__c,
+                        SummonsForDirction__c,
+                        Shipment_total_amount__c,
+                        RrturnPro_total_amount__c,
+                        Total_Invoiced_Procount__c,
+                        InvoiceNotPro_total_amount__c,
+                        InvoiceNotPro_money__c,
+                        Invoice_total_amount__c,
+                        Billed_Status__c
+                    FROM Consumable_order__c
+                    WHERE Id = :deliveryId
+                    ORDER BY Name
+                ];
+                for (Integer i = 0; i < invoiceOrderSelectedList.size(); i++) {
+                    invoiceOrderRecoeds.add(new InvoiceOrderInfo(invoiceOrderSelectedList[i]));
+                }
+            }
+        } else {
+            coc = [
+                SELECT
+                    Id,
+                    Name,
+                    Invoice_status__c,
+                    Invoice_Date__c,
+                    Order_ForDealerText__c,
+                    Dealer_Info__c,
+                    Order_ForDealer__c,
+                    Order_ForDealer__r.Name,
+                    Order_ForHospital__c,
+                    Order_ForHospital__r.Name,
+                    Order_date__c,
+                    Billed_Status__c,
+                    ShipmentAccount__c,
+                    Order_Dealer_Info__c,
+                    SummonsForDirction__c,
+                    Order_ForCustomerText__c,
+                    Invoice_Note__c,
+                    Invoice_attachment__c,
+                    Invoicedet_attachment__c,
+                    Outbound_order__c
+                FROM Consumable_order__c
+                WHERE Id = :invoiceId AND Order_type__c = '鍙戠エ'
+            ];
+            if (coc.Order_ForDealer__c != null) {
+                SecondDealer = coc.Order_ForDealer__c;
+            }
+            if (coc.Order_ForHospital__c != null) {
+                HospitalInfo = coc.Order_ForHospital__c;
+                HospitalName = coc.Order_ForHospital__r.Name;
+            }
+            //鍒伴摼鎺ヨ〃涓煡鎵惧叧鑱斿嚭搴撳崟
+            List<Consumable_order_LinkTable__c> outOrderList = [
+                SELECT Outboundorder_Code_link__c
+                FROM Consumable_order_LinkTable__c
+                WHERE Invoice_Code_link__c = :invoiceId
+            ];
+            for (Integer i = 0; i < outOrderList.size(); i++) {
+                outOrderStringList.add(outOrderList[i].Outboundorder_Code_link__c);
+            }
+            //鍙栧緱鍑哄簱鍗曚俊鎭�
+            invoiceOrderSelectedList = [
+                SELECT
+                    Id,
+                    Order_ForDealer__r.Name,
+                    Order_ForHospital__r.Name,
+                    Order_ForDealerText__c,
+                    Name,
+                    Outbound_Date__c,
+                    ShipmentAccount__c,
+                    Shipment_total_amount__c,
+                    RrturnPro_total_amount__c,
+                    Total_Invoiced_Procount__c,
+                    InvoiceNotPro_total_amount__c,
+                    InvoiceNotPro_money__c,
+                    Invoice_total_amount__c,
+                    Billed_Status__c
+                FROM Consumable_order__c
+                WHERE Id IN :outOrderStringList AND Order_Owner_WorkLocal__c = :userWorkLocation
+                ORDER BY Name
+            ];
+            for (Integer i = 0; i < invoiceOrderSelectedList.size(); i++) {
+                outorderMoneytMap.put(invoiceOrderSelectedList[i].id, 0);
+                invoiceOrderRecoeds.add(new InvoiceOrderInfo(invoiceOrderSelectedList[i]));
+            }
+            if (String.isNotBlank(deliveryId)) {
+                //鍙戠エ鍏宠仈鍑哄簱鍗曞彇寰楋紙鍑哄簱鍗曪級
+                isChange = true;
+                invoiceOrderSelectedList = [
+                    SELECT
+                        Id,
+                        Order_ForDealer__r.Name,
+                        Order_ForHospital__r.Name,
+                        Order_ForDealerText__c,
+                        Name,
+                        Outbound_Date__c,
+                        ShipmentAccount__c,
+                        Shipment_total_amount__c,
+                        RrturnPro_total_amount__c,
+                        Total_Invoiced_Procount__c,
+                        InvoiceNotPro_total_amount__c,
+                        InvoiceNotPro_money__c,
+                        Invoice_total_amount__c,
+                        Billed_Status__c
+                    FROM Consumable_order__c
+                    WHERE Id = :deliveryId AND Order_Owner_WorkLocal__c = :userWorkLocation
+                    ORDER BY Name
+                ];
+                for (Integer i = 0; i < invoiceOrderSelectedList.size(); i++) {
+                    if (!outorderMoneytMap.containsKey(invoiceOrderSelectedList[i].Id)) {
+                        invoiceOrderRecoeds.add(new InvoiceOrderInfo(invoiceOrderSelectedList[i]));
+                        invoiceOrderRecoedschange.add(new InvoiceOrderInfo(invoiceOrderSelectedList[i]));
+                    }
+                }
+            }
+            //鍙戠エ鏄庣粏涓�瑙�
+            List<Consumable_Orderdetails__c> countDel = [
+                SELECT
+                    Id,
+                    Consumable_ZS_order__c,
+                    Name,
+                    Consumable_order__c,
+                    Invoicedet1_OD_link__c,
+                    Invoicedet1_OD_link__r.Name,
+                    Consumable_order__r.Name,
+                    Product_OutDate__c,
+                    Asset_Model_No__c,
+                    Invoice_Unitprice__c,
+                    Delivery_List_RMB__c,
+                    Invoiced_Procount__c,
+                    Invoiced_Count__c,
+                    InvoicedProCost_RMB__c,
+                    Box_Piece__c,
+                    Invoice_Unit__c,
+                    ProductPacking_list_manual__c
+                FROM Consumable_Orderdetails__c
+                WHERE Consumable_order__c = :invoiceId
+                ORDER BY Invoicedet1_OD_link__c
+            ];
+            if (countDel.size() > 0) {
+                for (Integer i = 0; i < countDel.size(); i++) {
+                    outordercountMap.put(countDel[i].Invoicedet1_OD_link__c + countDel[i].Asset_Model_No__c, countDel[i]);
+                    if (outorderMoneytMap.containsKey(countDel[i].Invoicedet1_OD_link__c)) {
+                        outorderMoneytMap.put(
+                            countDel[i].Invoicedet1_OD_link__c,
+                            outorderMoneytMap.get(countDel[i].Invoicedet1_OD_link__c) + countDel[i].InvoicedProCost_RMB__c
+                        );
+                    }
+                    invoiceOrderdetail1Recoeds.add(new InvoiceOrderInfo(countDel[i]));
+                }
+            }
+            for (InvoiceOrderInfo ass : invoiceOrderRecoeds) {
+                if (outorderMoneytMap.containsKey(ass.esd.id)) {
+                    ass.needInvoiceCount = outorderMoneytMap.get(ass.esd.id);
+                }
+                ass.check = true;
+            }
+        }
+
+        if (SecondDealer == null || SecondDealer == '') {
+            provinceOpts.add(new SelectOption('', '-鏃�-'));
+            provinceOptsMap.put('', '-鏃�-');
+            for (Integer i = 0; i < Dealerelationship.size(); i++) {
+                provinceOpts.add(
+                    new SelectOption(Dealerelationship[i].Dealer_subordinate__c, Dealerelationship[i].Dealer_subordinate__r.Name)
+                );
+                provinceOptsMap.put(Dealerelationship[i].Dealer_subordinate__c, Dealerelationship[i].Dealer_subordinate__r.Name);
+            }
+        } else {
+            provinceOpts.add(new SelectOption('', '-鏃�-'));
+            provinceOptsMap.put('', '-鏃�-');
+            for (Integer i = 0; i < Dealerelationship.size(); i++) {
+                if (Dealerelationship[i].Dealer_subordinate__r.Name == SecondDealer) {
+                    provinceOpts.add(new SelectOption(Dealerelationship[i].Dealer_subordinate__c, SecondDealer));
+                    provinceOptsMap.put(Dealerelationship[i].Dealer_subordinate__c, SecondDealer);
+                }
+            }
+            for (Integer i = 0; i < Dealerelationship.size(); i++) {
+                if (Dealerelationship[i].Dealer_subordinate__r.Name != SecondDealer) {
+                    provinceOpts.add(
+                        new SelectOption(Dealerelationship[i].Dealer_subordinate__c, Dealerelationship[i].Dealer_subordinate__r.Name)
+                    );
+                    provinceOptsMap.put(Dealerelationship[i].Dealer_subordinate__c, Dealerelationship[i].Dealer_subordinate__r.Name);
+                }
+            }
+        }
+
+        Map<String, String> consumableaccessoriesMap = new Map<String, String>();
+        Map<String, String> invorceOrderIdMap = new Map<String, String>();
+        List<Consumable_accessories_invoice__c> consumableaccessories = [
+            SELECT Id, Name, Attachment_ID__c, Accessories_type__c
+            FROM Consumable_accessories_invoice__c
+            WHERE Invoice_code__c = :invoiceId
+        ];
+        System.debug('consumableaccessories = ' + consumableaccessories);
+        if (consumableaccessories.size() > 0) {
+            for (Integer i = 0; i < consumableaccessories.size(); i++) {
+                consumableaccessoriesMap.put(consumableaccessories[i].Attachment_ID__c, consumableaccessories[i].Accessories_type__c);
+                invorceOrderIdMap.put(consumableaccessories[i].Attachment_ID__c, consumableaccessories[i].Id);
+            }
+        }
+
+        //List<Attachment> attachmentinfo = [SELECT Id, Name, OwnerId,Owner.Name, CreatedDate FROM Attachment WHERE parentid = :invoiceId];
+        // if (attachmentinfo.size() > 0) {
+        //     for (Integer i = 0; i < attachmentinfo.size(); i++) {
+        //         attachmentRecoeds.add(new InvoiceOrderInfo(attachmentinfo[i]));
+        //     }
+        // }
+        List<ContentVersion> cvInfo = [SELECT Id, Title, OwnerId,Owner.Name, CreatedDate,ContentDocumentId FROM ContentVersion WHERE FirstPublishLocationId = :invoiceId];
+        if (cvInfo.size() > 0) {
+            for (Integer i = 0; i < cvInfo.size(); i++) {
+                attachmentRecoeds.add(new InvoiceOrderInfo(cvInfo[i]));
+            }
+        }
+        System.debug('attachmentRecoeds = ' + attachmentRecoeds);
+        System.debug('consumableaccessoriesMap = ' + consumableaccessoriesMap);
+        for (InvoiceOrderInfo ass : attachmentRecoeds) {
+            if (consumableaccessoriesMap.containsKey(ass.cvInfo.Id)) {
+                ass.mailSelectOptsin = consumableaccessoriesMap.get(ass.cvInfo.Id);
+                ass.invoiceOrderId = invorceOrderIdMap.get(ass.cvInfo.Id);
+            }
+        }
+        data.put('EditAble', EditAble);
+        data.put('isChange', isChange);
+        data.put('coc', coc);
+        data.put('ExistOutbound', getExistOutbound());
+        data.put('invoiceOrderRecoeds', invoiceOrderRecoeds);
+        data.put('provinceOptsMap', provinceOptsMap);
+        data.put('HospitalName', HospitalName);
+        data.put('HospitalInfo', HospitalInfo);
+        data.put('agencyProType', agencyProType);
+        data.put('userWorkLocation', userWorkLocation);
+        data.put('done', getdone());
+        data.put('invoiceOrderdetail1Recoeds', invoiceOrderdetail1Recoeds);
+        data.put('accountid', accountid);
+        data.put('invoiceOrderRecoedschange',invoiceOrderRecoedschange);
+        data.put('attachmentRecoeds',attachmentRecoeds);
+        data.put('outOrderStringList',outOrderStringList);
+        data.put('outordercountMap',outordercountMap);
+        res.status = 'Success';
+        res.code = 200;
+        System.debug('res = ' + res);
+        return res;
+    }
+
+    
+    //淇濆瓨
+    @AuraEnabled
+    public static ResponseBodyLWC save(
+        Consumable_order__c cocLwc,
+        String HospitalNameLwc,
+        String HospitalInfoLwc,
+        String SecondDealerLwc,
+        String invoiceOrderRecoedsLwc,
+        String deliveryIdLwc,
+        String invoiceIdLwc,
+        String accountidLwc,
+        String agencyProTypeLwc,
+        String reopenLwc,
+        String invoiceOrderRecoedschangeLwc
+    ) {
+        System.debug('enter LexConInvoiceViewController.save success');
+        ResponseBodyLWC res = new ResponseBodyLWC();
+        Map<String, object> data = new Map<String, object>();
+        res.entity = data;
+
+        coc = cocLwc;
+        HospitalName = HospitalNameLwc;
+        HospitalInfo = HospitalInfoLwc;
+        System.debug('HospitalInfo = ' + HospitalInfo);
+        SecondDealer = SecondDealerLwc;
+        errorMsg = '';
+        deliveryId = deliveryIdLwc;
+        invoiceOrderRecoeds = (List<InvoiceOrderInfo>)JSON.deserialize(invoiceOrderRecoedsLwc, List<InvoiceOrderInfo>.class);
+        invoiceId = invoiceIdLwc;
+        accountid = accountidLwc;
+        agencyProType = agencyProTypeLwc;
+        reopen = reopenLwc;
+        invoiceOrderRecoedschange = (List<InvoiceOrderInfo>)JSON.deserialize(invoiceOrderRecoedschangeLwc, List<InvoiceOrderInfo>.class);
+
+        List<String> chukudanID = new List<String>();
+        if (coc.Name == '' || coc.Name == null) {
+            // coc.Name.addError('璇峰綍鍏ュ彂绁ㄥ彿');
+            // return null;
+            return new ResponseBodyLWC('Error', 500, '璇峰綍鍏ュ彂绁ㄥ彿', '');
+        }
+        if (coc.Invoice_Date__c == null) {
+            // coc.Invoice_Date__c.addError('璇峰綍鍏ュ彂绁ㄦ棩鏈�');
+            // return null;
+            return new ResponseBodyLWC('Error', 500, '璇峰綍鍏ュ彂绁ㄦ棩鏈�', '');
+        }
+        if (String.isBlank(HospitalInfo) && String.isBlank(SecondDealer)) {
+            // coc.addError('蹇呴』杈撳叆瀹㈡埛鍚嶆垨鑰呬簩绾х粡閿�鍟�');
+            // return null;
+            return new ResponseBodyLWC('Error', 500, '蹇呴』杈撳叆瀹㈡埛鍚嶆垨鑰呬簩绾х粡閿�鍟�', '');
+        }
+        if (HospitalInfo != null && HospitalInfo != '' && String.isNotEmpty(SecondDealer)) {
+            //coc.addError('璇疯緭鍏ュ鎴峰悕銆佷簩绾х粡閿�鍟嗗彧鍙緭鍏ヤ竴涓�');
+            errorMsg += '璇疯緭鍏ュ鎴峰悕銆佷簩绾х粡閿�鍟嗗彧鍙緭鍏ヤ竴涓�';
+        }
+        if (HospitalInfo != null && HospitalInfo != '') {
+            if (coc.Order_ForCustomerText__c == null) {
+                // coc.Order_ForCustomerText__c.addError('璇疯緭鍏ョ瀹�');
+                // return null;
+                return new ResponseBodyLWC('Error', 500, '璇疯緭鍏ョ瀹�', '');
+            }
+        } else {
+            if (coc.Order_ForCustomerText__c != null) {
+                // coc.Order_ForCustomerText__c.addError('涓嶉渶瑕佽緭鍏ョ瀹�');
+                // return null;
+                return new ResponseBodyLWC('Error', 500, '涓嶉渶瑕佽緭鍏ョ瀹�', '');
+            }
+        }
+        String shipmentAccountString = '';
+        if (HospitalInfo != null) {
+            shipmentAccountString = HospitalInfo;
+        } else if (String.isNotEmpty(SecondDealer)) {
+            shipmentAccountString = SecondDealer;
+        } else {
+            shipmentAccountString = coc.Order_ForDealerText__c == null ? '' : coc.Order_ForDealerText__c;
+        }
+
+        Integer x = 0;
+        List<String> invoiceOrderAccountList = new List<String>();
+        for (InvoiceOrderInfo ass : invoiceOrderRecoeds) {
+            if (String.isBlank(deliveryId)) {
+                if (ass.check == true) {
+                    x++;
+                    System.debug(shipmentAccountString);
+                    if (HospitalInfo != null && HospitalInfo != '') {
+                        if (ass.esd.Order_ForHospital__c != shipmentAccountString) {
+                            invoiceOrderAccountList.add(ass.esd.Name);
+                        }
+                    }
+                    if (String.isNotEmpty(SecondDealer)) {
+                        if (
+                            ass.esd.Order_ForDealer__c != shipmentAccountString &&
+                            ass.esd.Order_ForDealerText__c != shipmentAccountString
+                        ) {
+                            invoiceOrderAccountList.add(ass.esd.Name);
+                        }
+                    }
+                    if (coc.Order_ForDealerText__c != null && (HospitalInfo == null || HospitalInfo == '')) {
+                        if (
+                            (ass.esd.Order_ForDealerText__c != null || ass.esd.Order_ForDealerText__c != '') &&
+                            ass.esd.ShipmentAccount__c != shipmentAccountString
+                        ) {
+                            invoiceOrderAccountList.add(ass.esd.Name);
+                        }
+                    }
+                }
+            } else {
+                if (HospitalInfo != null && HospitalInfo != '') {
+                    
+                    System.debug('++++++++++++' + ass.esd.Order_ForHospital__c + '   ' + shipmentAccountString);
+                    if (ass.esd.Order_ForHospital__c != shipmentAccountString) {
+                        System.debug('HospitalInfo != null ass.esd.Order_ForHospital__c != shipmentAccountString');
+                        invoiceOrderAccountList.add(ass.esd.Name);
+                    }
+                }
+                //if (String.isNotEmpty(SecondDealer)) {
+                if (String.isNotEmpty(SecondDealer)) {
+                    if (ass.esd.Order_ForDealer__c != shipmentAccountString) {
+                        System.debug('String.isNotEmpty(SecondDealer) ass.esd.Order_ForHospital__c != shipmentAccountString');
+                        invoiceOrderAccountList.add(ass.esd.Name);
+                    }
+                }
+                if (coc.Order_ForDealerText__c != null && (HospitalInfo == null || HospitalInfo == '')) {
+                    if (
+                        (ass.esd.Order_ForDealerText__c != null || ass.esd.Order_ForDealerText__c != '') &&
+                        ass.esd.ShipmentAccount__c != shipmentAccountString
+                    ) {
+                        System.debug('HospitalInfo == null && ass.esd.Order_ForDealerText__c != null ass.esd.ShipmentAccount__c != shipmentAccountString');
+                        invoiceOrderAccountList.add(ass.esd.Name);
+                    }
+                }
+            }
+        }
+        // invoiceOrderAccountList.add('12345');
+        if (invoiceOrderAccountList.size() > 0) {
+            orderRemind = invoiceOrderAccountList + '鍑哄簱鍗曞鎴峰悕涓庡彂绁ㄥ鎴峰悕涓嶄竴鑷达紒';
+            // coc.addError(invoiceOrderAccountList + '鍑哄簱鍗曞鎴峰悕涓庡彂绁ㄥ鎴峰悕涓嶄竴鑷达紒');
+            // return null;
+            return new ResponseBodyLWC('Error', 500, invoiceOrderAccountList + '鍑哄簱鍗曞鎴峰悕涓庡彂绁ㄥ鎴峰悕涓嶄竴鑷达紒', '');
+        } else {
+            orderRemind = '';
+        }
+        if (String.isBlank(deliveryId)) {
+            if (x < 1) {
+                // coc.addError('璇烽�夋嫨闇�瑕佸紑鍙戠エ鐨勫嚭搴撳崟锛�');
+                // return null;
+                return new ResponseBodyLWC('Error', 500, '璇烽�夋嫨闇�瑕佸紑鍙戠エ鐨勫嚭搴撳崟锛�', '');
+            }
+        }
+        Consumable_order__c invoiceHead = new Consumable_order__c();
+        List<String> outOrderIdList = new List<String>();
+        List<Consumable_order_LinkTable__c> invoiceLinkList = new List<Consumable_order_LinkTable__c>();
+        Map<String, String> insertDetMap = new Map<String, String>();
+        Map<String, String> deleteDetMap = new Map<String, String>();
+        //鏂板缓鍙戠エ鏃�
+        Savepoint sp = Database.setSavepoint();
+        try {
+            if (String.isBlank(invoiceId)) {
+                invoiceHead.Name = coc.Name;
+                if (coc.Invoice_status__c == null || String.isBlank(coc.Invoice_status__c))
+                    coc.Invoice_status__c = '鑽夋涓�';
+                invoiceHead.Order_type__c = '鍙戠エ';
+                invoiceHead.Invoice_Date__c = coc.Invoice_Date__c;
+                invoiceHead.Dealer_Info__c = accountid;
+                invoiceHead.Invoice_Note__c = coc.Invoice_Note__c;
+                invoiceHead.Invoice_status__c = '鑽夋涓�';
+                invoiceHead.Order_ProType__c = agencyProType;
+                invoiceHead.Order_ForCustomerText__c = coc.Order_ForCustomerText__c;
+                //if (deliveryId != null) {
+                if (String.isNotBlank(deliveryId)) {
+                    invoiceHead.Outbound_order__c = deliveryId;
+                }
+                invoiceHead.RecordTypeid = System.Label.RT_ConOrder_Invoice;
+                // if (SecondDealer == null) {
+                if (String.isBlank(SecondDealer)) {
+                    if (HospitalInfo != null && HospitalInfo != '') {
+                        invoiceHead.Order_ForHospital__c = HospitalInfo;
+                    }
+                } else {
+                    invoiceHead.Order_ForDealer__c = SecondDealer;
+                }
+                invoiceHead.Order_ForDealerText__c = coc.Order_ForDealerText__c;
+
+                insert invoiceHead;
+
+                invoiceId = invoiceHead.Id;
+                List<Consumable_order__c> cocinfo = new List<Consumable_order__c>();
+                cocinfo = [
+                    SELECT
+                        Id,
+                        Name,
+                        Invoice_status__c,
+                        Invoice_Date__c,
+                        Order_ForDealerText__c,
+                        Dealer_Info__c,
+                        Order_ForDealer__c,
+                        Order_ForDealer__r.Name,
+                        Order_ForHospital__c,
+                        Order_date__c,
+                        Billed_Status__c,
+                        ShipmentAccount__c,
+                        Order_Dealer_Info__c,
+                        SummonsForDirction__c,
+                        Order_ForCustomerText__c,
+                        Invoice_Note__c,
+                        Invoice_attachment__c,
+                        Invoicedet_attachment__c,
+                        Outbound_order__c
+                    FROM Consumable_order__c
+                    WHERE Id = :invoiceId
+                ];
+                if (cocinfo.size() > 0) {
+                    invoiceHead = cocinfo[0];
+                }
+                if (!String.isBlank(deliveryId)) {
+                    for (InvoiceOrderInfo ass : invoiceOrderRecoeds) {
+                        chukudanID.add(ass.esd.Id);
+                        insertDetMap.put(ass.esd.Id, invoiceHead.Id);
+                    }
+                } else {
+                    for (InvoiceOrderInfo ass : invoiceOrderRecoeds) {
+                        if (!String.isBlank(deliveryId)) {
+                            chukudanID.add(ass.esd.Id);
+                            insertDetMap.put(ass.esd.Id, invoiceHead.Id);
+                        } else {
+                            if (ass.check == true) {
+                                chukudanID.add(ass.esd.Id);
+                                insertDetMap.put(ass.esd.Id, invoiceHead.Id);
+                            } else {
+                                //deleteDetMap.put(ass.esd.Id,invoiceHead.Id);
+                            }
+                        }
+                    }
+                }
+                //鏂板缓鍙戠エ鏄庣粏1
+                if (chukudanID.size() > 0) {
+                    newinvoicedetails1(chukudanID);
+                }
+                //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'deleteDetMap +++++' + deleteDetMap.size()));
+                //return null;
+                if (insertDetMap.size() > 0) {
+                    for (String field : insertDetMap.keySet()) {
+                        List<Consumable_order_LinkTable__c> existLinkinfo = [
+                            SELECT Id
+                            FROM Consumable_order_LinkTable__c
+                            WHERE Outboundorder_Code_link__c = :field AND Invoice_Code_link__c = :insertDetMap.get(field)
+                        ];
+                        if (existLinkinfo.size() < 1) {
+                            Consumable_order_LinkTable__c invoiceLink = new Consumable_order_LinkTable__c();
+                            invoiceLink.Name = field + insertDetMap.get(field);
+                            invoiceLink.Outboundorder_Code_link__c = field;
+                            invoiceLink.Invoice_Code_link__c = insertDetMap.get(field);
+                            invoiceLinkList.add(invoiceLink);
+                        }
+                    }
+                    upsert invoiceLinkList;
+                }
+            } else if (String.isNotBlank(reopen) && reopen.equals('isreopen') && String.isNotBlank(invoiceId)) {
+                System.debug('杩涘叆reopen');
+                invoiceHead.Name = coc.Name;
+                invoiceHead.Order_type__c = '鍙戠エ';
+                invoiceHead.Invoice_Date__c = coc.Invoice_Date__c;
+                invoiceHead.Dealer_Info__c = accountid;
+                invoiceHead.Invoice_Note__c = coc.Invoice_Note__c;
+                invoiceHead.Invoice_status__c = '鑽夋涓�';
+                invoiceHead.Order_ProType__c = agencyProType;
+                invoiceHead.Order_ForCustomerText__c = coc.Order_ForCustomerText__c;
+                //if (deliveryId != null) {
+                    if (String.isNotBlank(deliveryId)) {
+                    invoiceHead.Outbound_order__c = deliveryId;
+                }
+                invoiceHead.RecordTypeid = System.Label.RT_ConOrder_Invoice;
+                //if (SecondDealer == null) {
+                    if (String.isBlank(SecondDealer)) {
+                    if (HospitalInfo != null && HospitalInfo != '') {
+                        invoiceHead.Order_ForHospital__c = HospitalInfo;
+                    }
+                } else {
+                    invoiceHead.Order_ForDealer__c = SecondDealer;
+                }
+                invoiceHead.Order_ForDealerText__c = coc.Order_ForDealerText__c;
+                insert invoiceHead;
+                System.debug('invoiceHead = ' + invoiceHead);
+
+                List<String> updateChukudanId = new List<String>();
+                //鑾峰彇鍘熷彂绁ㄧ浉鍏宠仈鐨勯摼鎺ヨ〃淇℃伅
+                List<Consumable_order_LinkTable__c> tLinkList = [
+                    SELECT Outboundorder_Code_link__c
+                    FROM Consumable_order_LinkTable__c
+                    WHERE Invoice_Code_link__c = :invoiceId
+                ];
+
+                for (Consumable_order_LinkTable__c colc : tLinkList) {
+                    colc.Invoice_Code_link__c = invoiceHead.Id;
+                    updateChukudanId.add(colc.Outboundorder_Code_link__c);
+                }
+                System.debug('tLinkList = ' + tLinkList);
+                update tLinkList;
+                //鏇存柊鍙戠エ鏄庣粏淇℃伅
+                //鑾峰彇鍑哄簱鍗�,璁剧疆鍏宠仈鍏崇郴
+                //鑾峰彇鍑哄簱鍗曟槑缁嗕俊鎭�,鍥犱负鍑哄簱鍗曚俊鎭拰鍑哄簱鍗曟槑缁嗕俊鎭槸鍏宠仈鐨�,鎵�鏈夊彧闇�瑕佸洖婊氬嚭搴撳崟鏄庣粏淇℃伅鍗冲彲
+
+                Map<String, Consumable_Orderdetails__c> tempCocMap = new Map<String, Consumable_Orderdetails__c>();
+                List<Consumable_Orderdetails__c> tempCountList = [
+                    SELECT
+                        Id,
+                        Consumable_order__c,
+                        Asset_Model_No__c,
+                        InvoicedProCost_RMB__c,
+                        Invoicedet1_OD_link__c,
+                        InvoicedProduct_RMB__c,
+                        Invoiced_Procount__c,
+                        Invoiced_BoxCount__c,
+                        Invoice_Unit__c
+                    FROM Consumable_Orderdetails__c
+                    WHERE Consumable_order__c = :invoiceId
+                ];
+                for (Consumable_Orderdetails__c cocTemp : tempCountList) {
+                    cocTemp.Consumable_order__c = invoiceHead.Id;
+                    tempCocMap.put(cocTemp.Invoicedet1_OD_link__c + cocTemp.Asset_Model_No__c, cocTemp);
+                }
+                System.debug('tempCountList = ' + tempCountList);
+                update tempCountList;
+                List<Consumable_Orderdetails__c> updateFromList = [
+                    SELECT
+                        Id,
+                        Consumable_order__c,
+                        RrturnPro_count__c,
+                        Delivery_List_RMB__c,
+                        InvoicedProCost_RMB__c,
+                        Invoiced_Procount__c,
+                        Invoice_Unitprice__c,
+                        Box_Piece__c,
+                        Invoice_Unit__c,
+                        Consumable_order__r.Invoice_total_amount__c,
+                        ProductPacking_list_manual__c,
+                        InvoicedProduct_RMB__c,
+                        Invoiced_BoxCount__c,
+                        Shipment_amount__c,
+                        Asset_Model_No__c
+                    FROM Consumable_Orderdetails__c
+                    WHERE Consumable_order__c IN :updateChukudanId
+                ];
+                for (Consumable_Orderdetails__c corderc : updateFromList) {
+                    String tempStr = corderc.Consumable_order__c + corderc.Asset_Model_No__c;
+                    if (tempCocMap.get(tempStr) != null) {
+                        //璁剧疆宸插彂绁ㄩ噾棰� 鍑哄簱鍗曠殑宸插彂绁ㄩ噾棰� - 鍙戠エ鐨勫彂绁ㄩ噾棰�
+                        corderc.InvoicedProCost_RMB__c = corderc.InvoicedProCost_RMB__c - tempCocMap.get(tempStr).InvoicedProduct_RMB__c;
+                        //璁剧疆宸插彂绁ㄦ暟閲�  鍑哄簱鍗曠殑宸插彂绁ㄦ暟閲� - 鍙戠エ鐨勫彂绁ㄦ暟閲�(鐩�)
+                        corderc.Invoiced_Procount__c = corderc.Invoiced_Procount__c - tempCocMap.get(tempStr).Invoiced_BoxCount__c;
+                        //璁剧疆鍙戠エ鍗曚环
+                        if (corderc.Box_Piece__c == '鐩�' && corderc.Invoice_Unit__c == '涓�') {
+                            corderc.Invoice_Unitprice__c = corderc.Delivery_List_RMB__c / corderc.ProductPacking_list_manual__c;
+                        } else {
+                            corderc.Invoice_Unitprice__c = corderc.Delivery_List_RMB__c;
+                        }
+                    }
+                }
+
+                Consumable_order__c tempcoc = new Consumable_order__c();
+                tempcoc.Id = invoiceId;
+                System.debug('tempcoc = ' + tempcoc);
+                delete tempcoc;
+                invoiceId = invoiceHead.Id;
+                System.debug('updateFromList = ' + updateFromList);
+                update updateFromList;
+                //--------------------------------------------------------------------------
+                for (InvoiceOrderInfo ass : invoiceOrderRecoeds) {
+                    if (ass.check == true) {
+                        insertDetMap.put(ass.esd.Id, invoiceHead.Id);
+                    } else {
+                        outOrderIdList.add(ass.esd.Id);
+                        deleteDetMap.put(ass.esd.Id, invoiceHead.Id);
+                    }
+                }
+                if (outOrderIdList.size() > 0) {
+                    getdefaultMapinfo(outOrderIdList);
+                }
+                //琚�変腑鐨勫嚭搴撳崟闆嗗悎
+                if (insertDetMap.size() > 0) {
+                    List<Consumable_order_LinkTable__c> existLinkinfo = [
+                        SELECT Id, Invoice_Code_link__c, Outboundorder_Code_link__c
+                        FROM Consumable_order_LinkTable__c
+                        WHERE Outboundorder_Code_link__c IN :insertDetMap.keySet() AND Invoice_Code_link__c = :invoiceHead.Id
+                    ];
+                    //瀛樺湪娌℃湁涓庡彂绁ㄥ缓绔嬪叧鑱斿叧绯荤殑鍑哄簱鍗�
+                    List<String> templist2 = new List<String>();
+                    for (Consumable_order_LinkTable__c colc : existLinkinfo) {
+                        templist2.add(colc.Outboundorder_Code_link__c);
+                    }
+                    //鑾峰彇璇ュ嚭搴撳崟
+                    for (String field : insertDetMap.keySet()) {
+                        if (!templist2.contains(field)) {
+                            //璇ュ嚭搴撳崟涓嶅瓨鍦ㄤ笌閾炬帴琛ㄤ腑,鏂板缓閾炬帴
+                            chukudanID.add(field);
+                            Consumable_order_LinkTable__c invoiceLink = new Consumable_order_LinkTable__c();
+                            invoiceLink.Name = field + insertDetMap.get(field);
+                            invoiceLink.Outboundorder_Code_link__c = field;
+                            invoiceLink.Invoice_Code_link__c = insertDetMap.get(field);
+                            invoiceLinkList.add(invoiceLink);
+                        }
+                    }
+                    System.debug('invoiceLinkList = ' + invoiceLinkList);
+                    upsert invoiceLinkList;
+                }
+                //鍒犻櫎鍑哄簱鍗曢摼鎺ヨ〃
+                if (deleteDetMap.size() > 0) {
+                    List<Consumable_order_LinkTable__c> invoiceLinkdetList = new List<Consumable_order_LinkTable__c>();
+                    deleteOutboundorder(deleteDetMap, invoiceHead.Id);
+                    List<Consumable_order_LinkTable__c> existLinkinfo = [
+                        SELECT Id, Outboundorder_Code_link__c, Invoice_Code_link__c
+                        FROM Consumable_order_LinkTable__c
+                        WHERE Outboundorder_Code_link__c IN :deleteDetMap.keySet() AND Invoice_Code_link__c = :invoiceHead.Id
+                    ];
+                    for (Consumable_order_LinkTable__c colctemp : existLinkinfo) {
+                        if (deleteDetMap.get(colctemp.Outboundorder_Code_link__c) == colctemp.Invoice_Code_link__c) {
+                            //瀛樺湪涓庤鍙戠エ鍏宠仈鐨勫嚭搴撳崟
+                            invoiceLinkdetList.add(colctemp);
+                        }
+                    }
+                    System.debug('invoiceLinkdetList = ' + invoiceLinkdetList);
+                    delete invoiceLinkdetList;
+                }
+                //鏂板缓鍙戠エ鏄庣粏1
+                if (chukudanID.size() > 0) {
+                    newinvoicedetails1(chukudanID);
+                }
+            } else {
+                //CHAN-BSS5SQ    update by rentongxiao 2020-09-04   end
+                //缂栬緫鍚庝繚瀛�
+                List<Consumable_order__c> cocinfo = new List<Consumable_order__c>();
+                cocinfo = [
+                    SELECT
+                        Id,
+                        Name,
+                        Invoice_status__c,
+                        Invoice_Date__c,
+                        Order_ForDealerText__c,
+                        Dealer_Info__c,
+                        Order_ForDealer__c,
+                        Order_ForDealer__r.Name,
+                        Order_ForHospital__c,
+                        Order_date__c,
+                        Billed_Status__c,
+                        ShipmentAccount__c,
+                        Order_Dealer_Info__c,
+                        SummonsForDirction__c,
+                        Order_ForCustomerText__c,
+                        Invoice_Note__c,
+                        Invoice_attachment__c,
+                        Invoicedet_attachment__c,
+                        Outbound_order__c
+                    FROM Consumable_order__c
+                    WHERE Id = :invoiceId
+                ];
+                if (cocinfo.size() > 0) {
+                    invoiceHead = cocinfo[0];
+                }
+                if (String.isNotBlank(deliveryId)) { 
+                    for (InvoiceOrderInfo ass : invoiceOrderRecoedschange) {
+                        insertDetMap.put(ass.esd.Id, invoiceHead.Id);
+                    }
+
+                    //鎹㈣揣鍗曚骇鍝佹暟閲�
+                    Map<String, Decimal> needchangeOrderProcountMap = new Map<String, Decimal>();
+                    Map<String, Decimal> returnProcountMap = new Map<String, Decimal>();
+                    Map<String, Decimal> changeOrderProcountMap = new Map<String, Decimal>();
+                    List<Consumable_Orderdetails__c> detaliFromList = [
+                        SELECT
+                            Id,
+                            Name,
+                            Consumable_order__c,
+                            Asset_Model_No__c,
+                            Shipment_Count__c,
+                            RrturnPro_count__c,
+                            Delivery_List_RMB__c,
+                            InvoicedProCost_RMB__c,
+                            Invoiced_Procount__c,
+                            Invoiced_Count__c,
+                            Invoice_Unitprice__c,
+                            InvoiceProNot_count__c,
+                            Invoice_Unit__c,
+                            Box_Piece__c,
+                            Invoice_Cost_RMB__c
+                        FROM Consumable_Orderdetails__c
+                        WHERE Consumable_order__c = :deliveryId
+                    ];
+                    for (Consumable_Orderdetails__c changeOD : detaliFromList) {
+                        needchangeOrderProcountMap.put(changeOD.Asset_Model_No__c, changeOD.Shipment_Count__c);
+                    }
+                    //鍙戠エ杩斿搧淇℃伅
+                    List<Consumable_Orderdetails__c> returnCountList = [
+                        SELECT
+                            Id,
+                            Name,
+                            Consumable_order__c,
+                            Asset_Model_No__c,
+                            Shipment_Count__c,
+                            RrturnPro_count__c,
+                            Delivery_List_RMB__c,
+                            InvoicedProCost_RMB__c,
+                            Invoiced_Procount__c,
+                            Invoiced_Count__c,
+                            Invoice_Unitprice__c,
+                            InvoiceProNot_count__c,
+                            Invoice_Cost_RMB__c,
+                            Invoice_Unit__c,
+                            Box_Piece__c
+                        FROM Consumable_Orderdetails__c
+                        WHERE Consumable_order__c = :invoiceId
+                    ];
+
+                    for (Consumable_Orderdetails__c changeOD : returnCountList) {
+                        if (changeOD.RrturnPro_count__c > 0) {
+                            if (returnProcountMap.containsKey(changeOD.Asset_Model_No__c)) {
+                                returnProcountMap.put(
+                                    changeOD.Asset_Model_No__c,
+                                    returnProcountMap.get(changeOD.Asset_Model_No__c) + changeOD.RrturnPro_count__c
+                                );
+                            } else {
+                                returnProcountMap.put(changeOD.Asset_Model_No__c, changeOD.RrturnPro_count__c);
+                            }
+                        }
+                    }
+
+                    //宸叉崲璐у崟鍑哄簱鏁伴噺淇℃伅
+                    List<Consumable_Orderdetails__c> returnCount1List = [
+                        SELECT
+                            Id,
+                            Name,
+                            Consumable_order__c,
+                            Asset_Model_No__c,
+                            Shipment_Count__c,
+                            RrturnPro_count__c,
+                            Delivery_List_RMB__c,
+                            InvoicedProCost_RMB__c,
+                            Invoiced_Procount__c,
+                            Invoiced_Count__c,
+                            Invoice_Unitprice__c,
+                            InvoiceProNot_count__c,
+                            Invoice_Cost_RMB__c,
+                            Invoice_Unit__c,
+                            Box_Piece__c
+                        FROM Consumable_Orderdetails__c
+                        WHERE
+                            Consumable_order__c IN (
+                                SELECT Outboundorder_Code_link__c
+                                FROM Consumable_order_LinkTable__c
+                                WHERE Invoice_Code_link__c = :invoiceId
+                            )
+                            AND Consumable_order__r.Onchange_order__c = TRUE
+                            AND Consumable_order__c != :deliveryId
+                    ];
+                    for (Consumable_Orderdetails__c changeOD : returnCount1List) {
+                        changeOrderProcountMap.put(changeOD.Asset_Model_No__c, changeOD.Shipment_Count__c);
+                    }
+                    outOrderchange(insertDetMap);
+                } else {
+                    invoiceHead.Name = coc.Name;
+                    invoiceHead.Invoice_status__c = coc.Invoice_status__c;
+                    invoiceHead.Invoice_Date__c = coc.Invoice_Date__c;
+                    invoiceHead.Dealer_Info__c = accountid;
+                    invoiceHead.Invoice_Note__c = coc.Invoice_Note__c;
+                    invoiceHead.Invoice_status__c = '鑽夋涓�';
+                    invoiceHead.Order_ProType__c = agencyProType;
+                    invoiceHead.Order_ForCustomerText__c = coc.Order_ForCustomerText__c;
+                    //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'HospitalName +++++'   + HospitalName));
+                    //return null;
+                    if (HospitalName != null && HospitalName != '') {
+                        if (HospitalInfo != null && HospitalInfo != '') {
+                            invoiceHead.Order_ForHospital__c = HospitalInfo;
+                        }
+                    } else {
+                        invoiceHead.Order_ForHospital__c = null;
+                    }
+                    invoiceHead.Order_ForDealer__c = SecondDealer == '' ? null : SecondDealer;
+                    invoiceHead.Order_ForDealerText__c = coc.Order_ForDealerText__c;
+                    update invoiceHead;
+                    for (InvoiceOrderInfo ass : invoiceOrderRecoeds) {
+                        if (ass.check == true) {
+                            insertDetMap.put(ass.esd.Id, invoiceHead.Id);
+                        } else {
+                            outOrderIdList.add(ass.esd.Id);
+                            deleteDetMap.put(ass.esd.Id, invoiceHead.Id);
+                        }
+                    }
+                    if (outOrderIdList.size() > 0) {
+                        getdefaultMapinfo(outOrderIdList);
+                    }
+                    //CHAN-BSS5SQ    update by rentongxiao 2020-09-08  start
+                    //琚�変腑鐨勫嚭搴撳崟闆嗗悎
+                    if (insertDetMap.size() > 0) {
+                        List<Consumable_order_LinkTable__c> existLinkinfo = [
+                            SELECT Id, Invoice_Code_link__c, Outboundorder_Code_link__c
+                            FROM Consumable_order_LinkTable__c
+                            WHERE Outboundorder_Code_link__c IN :insertDetMap.keySet() AND Invoice_Code_link__c = :invoiceHead.Id
+                        ];
+                        //瀛樺湪娌℃湁涓庡彂绁ㄥ缓绔嬪叧鑱斿叧绯荤殑鍑哄簱鍗�
+                        List<String> templist2 = new List<String>();
+                        for (Consumable_order_LinkTable__c colc : existLinkinfo) {
+                            templist2.add(colc.Outboundorder_Code_link__c);
+                        }
+                        //鑾峰彇璇ュ嚭搴撳崟
+                        for (String field : insertDetMap.keySet()) {
+                            if (!templist2.contains(field)) {
+                                //璇ュ嚭搴撳崟涓嶅瓨鍦ㄤ笌閾炬帴琛ㄤ腑,鏂板缓閾炬帴
+                                chukudanID.add(field);
+                                Consumable_order_LinkTable__c invoiceLink = new Consumable_order_LinkTable__c();
+                                invoiceLink.Name = field + insertDetMap.get(field);
+                                invoiceLink.Outboundorder_Code_link__c = field;
+                                invoiceLink.Invoice_Code_link__c = insertDetMap.get(field);
+                                invoiceLinkList.add(invoiceLink);
+                            }
+                        }
+                        upsert invoiceLinkList;
+                    }
+                    if (deleteDetMap.size() > 0) {
+                        List<Consumable_order_LinkTable__c> invoiceLinkdetList = new List<Consumable_order_LinkTable__c>();
+                        deleteOutboundorder(deleteDetMap, invoiceHead.Id);
+                        List<Consumable_order_LinkTable__c> existLinkinfo = [
+                            SELECT Id, Outboundorder_Code_link__c, Invoice_Code_link__c
+                            FROM Consumable_order_LinkTable__c
+                            WHERE Outboundorder_Code_link__c IN :deleteDetMap.keySet() AND Invoice_Code_link__c = :invoiceHead.Id
+                        ];
+                        for (Consumable_order_LinkTable__c colctemp : existLinkinfo) {
+                            if (deleteDetMap.get(colctemp.Outboundorder_Code_link__c) == colctemp.Invoice_Code_link__c) {
+                                //瀛樺湪涓庤鍙戠エ鍏宠仈鐨勫嚭搴撳崟
+                                invoiceLinkdetList.add(colctemp);
+                            }
+                        }
+                        delete invoiceLinkdetList;
+                    }
+                    //CHAN-BSS5SQ    update by rentongxiao 2020-09-08  end
+                    //鏂板缓鍙戠エ鏄庣粏1
+                    if (chukudanID.size() > 0) {
+                        newinvoicedetails1(chukudanID);
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            Database.rollback(sp);
+            // ApexPages.addMessages(ex);
+            // return null;
+            System.debug('Exception ex = ' + ex.getMessage() + ex.getLineNumber());
+            errorMsg += ex.getMessage() + ex.getLineNumber();
+        }
+        data.put('invoiceId', invoiceId);
+        res.status = 'Success';
+        res.code = 200;
+        res.msg = errorMsg;
+        System.debug('res = ' + res);
+        return res;
+    }
+
+    public static void newinvoicedetails1(List<String> chukudanID) {
+        //鏂拌鍙戠エ鏄庣粏1
+        List<Consumable_Orderdetails__c> invoiceorderList1 = new List<Consumable_Orderdetails__c>();
+        //鍑哄簱鍗曟槑缁嗘洿鏂�
+        List<Consumable_Orderdetails__c> outOrderList1 = new List<Consumable_Orderdetails__c>();
+        List<Consumable_Orderdetails__c> detaliFromList = [
+            SELECT
+                Id,
+                Name,
+                Consumable_order__c,
+                Asset_Model_No__c,
+                Consumable_Product__r.Asset_Model_No__c,
+                Shipment_Count__c,
+                RrturnPro_count__c,
+                Delivery_List_RMB__c,
+                InvoicedProCost_RMB__c,
+                Invoiced_Procount__c,
+                Invoiced_Count__c,
+                Invoice_Unitprice__c,
+                InvoiceProNot_count__c,
+                Invoice_Cost_RMB__c,
+                Invoice_No__c,
+                Consumable_Product__c,
+                Intra_Trade_List_RMB__c,
+                Dealer_Custom_Price__c,
+                Sum_of_money__c,
+                Box_Piece__c,
+                Used_date__c,
+                Send_date__c,
+                Invoice_Unit__c,
+                ProductPacking_list_manual__c
+            FROM Consumable_Orderdetails__c
+            WHERE Consumable_order__c IN :chukudanID AND InvoiceProNot_count__c > 0
+        ];
+
+        for (Integer i = 0; i < detaliFromList.size(); i++) {
+            Consumable_Orderdetails__c invoiceInsert1 = new Consumable_Orderdetails__c();
+            invoiceInsert1.Used_date__c = detaliFromList[i].Used_date__c;
+            invoiceInsert1.Send_date__c = detaliFromList[i].Send_date__c;
+            String str = string.valueOf(i + 1);
+            if (str.length() == 1) {
+                str = '0' + str;
+            }
+            invoiceInsert1.Name =
+                coc.Name +
+                '-' +
+                detaliFromList[i].Name.substring(detaliFromList[i].Name.length() - 7, detaliFromList[i].Name.length());
+            invoiceInsert1.Shipment_Count__c = detaliFromList[i].InvoiceProNot_count__c;
+            invoiceInsert1.Consumable_Product__c = detaliFromList[i].Consumable_Product__c;
+            invoiceInsert1.Intra_Trade_List_RMB__c = detaliFromList[i].Intra_Trade_List_RMB__c;
+            invoiceInsert1.Delivery_List_RMB__c = detaliFromList[i].Delivery_List_RMB__c;
+            invoiceInsert1.Dealer_Custom_Price__c = detaliFromList[i].Dealer_Custom_Price__c;
+            invoiceInsert1.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
+
+            if (detaliFromList[i].Box_Piece__c == '鐩�' && detaliFromList[i].Invoice_Unit__c == '涓�') {
+                invoiceInsert1.Invoice_Unitprice__c =
+                    detaliFromList[i].Delivery_List_RMB__c / detaliFromList[i].ProductPacking_list_manual__c;
+            } else {
+                invoiceInsert1.Invoice_Unitprice__c = detaliFromList[i].Delivery_List_RMB__c;
+            }
+            invoiceInsert1.Box_Piece__c = detaliFromList[i].Box_Piece__c;
+            invoiceInsert1.Invoice_Unit__c = detaliFromList[i].Box_Piece__c;
+            if (detaliFromList[i].Invoiced_Procount__c == null)
+                detaliFromList[i].Invoiced_Procount__c = 0;
+            if (detaliFromList[i].RrturnPro_count__c == null)
+                detaliFromList[i].RrturnPro_count__c = 0;
+            invoiceInsert1.Invoiced_Count__c = detaliFromList[i].InvoiceProNot_count__c;
+            if (detaliFromList[i].InvoicedProCost_RMB__c == null)
+                detaliFromList[i].InvoicedProCost_RMB__c = 0;
+            invoiceInsert1.InvoicedProCost_RMB__c = invoiceInsert1.Invoiced_Count__c * invoiceInsert1.Delivery_List_RMB__c;
+            invoiceInsert1.Consumable_order__c = invoiceId;
+            invoiceInsert1.Invoicedet1_OD_link__c = detaliFromList[i].Consumable_order__c;
+            invoiceorderList1.add(invoiceInsert1);
+        }
+        System.debug('invoiceorderList1 +++++' + invoiceorderList1.size());
+        Savepoint sp = Database.setSavepoint();
+        System.debug('invoiceorderList1 = ' + invoiceorderList1);
+        if (invoiceorderList1.size() > 0) {
+            try {
+                System.debug('invoiceorderList1 = ' + invoiceorderList1);
+                insert invoiceorderList1;
+            } catch (Exception ex) {
+                Database.rollback(sp);
+                //ApexPages.addMessages(ex);
+                errorMsg += ex.getMessage();
+            }
+        }
+    }
+
+    public static void getdefaultMapinfo(List<String> orderIdList) {
+        List<Consumable_Orderdetails__c> existLinkinfo = new List<Consumable_Orderdetails__c>();
+        existLinkinfo = [
+            SELECT Id, Consumable_order__c, Consumable_product__c, Asset_Model_No__c
+            FROM Consumable_Orderdetails__c
+            WHERE Consumable_order__c IN :orderIdList
+        ];
+        // for (Integer i = 0; i < existLinkinfo.size(); i++) {
+        //     ordermx1defaultMap.put(existLinkinfo[i].Consumable_order__c + existLinkinfo[i].Asset_Model_No__c, 0);
+        // }
+    }
+
+    public static void deleteOutboundorder(Map<String, String> deleteMap, String value) {
+        //鍒犻櫎鍙戠エ鏄庣粏1
+        List<Consumable_Orderdetails__c> detInvoicedet1List = [
+            SELECT Id, Consumable_Shipment_order__c, Consumable_Sale_order__c, Consumable_product__c, Asset_Model_No__c
+            FROM Consumable_Orderdetails__c
+            WHERE Consumable_order__c = :value AND Invoicedet1_OD_link__c = :deleteMap.keySet()
+        ];
+        System.debug('detInvoicedet1List = ' + detInvoicedet1List);
+        delete detInvoicedet1List;
+    }
+
+    //鎹㈣揣鍗曢摼鎺ヨ〃 鍙戠エ鏄庣粏2鍋氭垚
+    public static void outOrderchange(Map<String, String> insertDetMap) {
+        List<string> chukudanID = new List<string>();
+        List<Consumable_order_LinkTable__c> invoiceLinkList = new List<Consumable_order_LinkTable__c>();
+        Consumable_order__c p = new Consumable_order__c();
+        if (insertDetMap.size() > 0) {
+            for (String field : insertDetMap.keySet()) {
+                List<Consumable_order_LinkTable__c> existLinkinfo = [
+                    SELECT Id
+                    FROM Consumable_order_LinkTable__c
+                    WHERE Outboundorder_Code_link__c = :field
+                ];
+                if (existLinkinfo.size() > 0) {
+                    delete existLinkinfo;
+                }
+                chukudanID.add(field);
+                Consumable_order_LinkTable__c invoiceLink = new Consumable_order_LinkTable__c();
+                invoiceLink.Name = field + insertDetMap.get(field);
+                invoiceLink.Outboundorder_Code_link__c = field;
+                invoiceLink.Invoice_Code_link__c = insertDetMap.get(field);
+                invoiceLinkList.add(invoiceLink);
+                p.Id = field;
+                p.ConInvoice_Code__c = insertDetMap.get(field);
+                p.Onchange_order__c = true;
+                update p;
+            }
+            insert invoiceLinkList;
+        }
+        //鎹㈣揣鍑哄簱鍗曟槑缁�1鏇存柊
+        List<Consumable_Orderdetails__c> detaliFrom1List = [
+            SELECT
+                Id,
+                Name,
+                Consumable_order__c,
+                Asset_Model_No__c,
+                Consumable_Product__r.Asset_Model_No__c,
+                Shipment_Count__c,
+                RrturnPro_count__c,
+                Delivery_List_RMB__c,
+                InvoicedProCost_RMB__c,
+                Invoiced_Procount__c,
+                Invoiced_Count__c,
+                Invoice_Unitprice__c,
+                InvoiceProNot_count__c,
+                Invoice_Cost_RMB__c,
+                Invoice_No__c,
+                Invoice_Unit__c,
+                Box_Piece__c,
+                Consumable_Sale_order__r.Name,
+                Consumable_Shipment_order__r.Name
+            FROM Consumable_Orderdetails__c
+            WHERE Consumable_order__c IN :chukudanID
+        ];
+        for (Consumable_Orderdetails__c changeOD : detaliFrom1List) {
+            changeOD.Invoiced_Procount__c = changeOD.Shipment_Count__c;
+        }
+        update detaliFrom1List;
+    }
+
+    //涓婁紶ContentVersion闄勪欢
+    @AuraEnabled
+    public static String saveFile(Id recordId, String fileName, String base64Data) {
+        base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
+        ContentVersion cv = new ContentVersion();
+        cv.Title = fileName;
+        cv.PathOnClient = '/' + fileName;
+        cv.FirstPublishLocationId = recordId;
+        cv.VersionData = EncodingUtil.base64Decode(base64Data);
+        cv.IsMajorVersion = true;
+        insert cv;
+        System.debug('cv.Id = ' + cv.Id);
+        return cv.Id;
+    }
+
+    // 淇濆瓨闄勪欢
+    @AuraEnabled
+    public static ResponseBodyLWC saveAttachment(String attachmentRecoedsLwc,String invoiceId) {
+        System.debug('杩涘叆 saveAttachment');
+        ResponseBodyLWC res = new ResponseBodyLWC();
+        Map<String, object> data = new Map<String, object>();
+        res.entity = data;
+        invoiceId = invoiceId;
+        attachmentRecoeds = (List<InvoiceOrderInfo>)JSON.deserialize(attachmentRecoedsLwc, List<InvoiceOrderInfo>.class);
+        System.debug('attachmentRecoeds = ' + attachmentRecoeds);
+        try {
+            Consumable_accessories_invoice__c attachmentdetails = new Consumable_accessories_invoice__c();
+            for (InvoiceOrderInfo ass : attachmentRecoeds) {
+                if (!String.isBlank(ass.mailSelectOptsin)) {
+                    attachmentdetails.Id = ass.invoiceOrderId;
+                    attachmentdetails.Attachment_ID__c = ass.cvInfo.ContentDocumentId;
+                    attachmentdetails.Accessories_type__c = ass.mailSelectOptsin;
+                    if (String.isBlank(ass.invoiceOrderId)) {
+                        attachmentdetails.Invoice_code__c = invoiceId;
+                    }
+                    upsert attachmentdetails;
+                }
+            }
+        } catch (Exception e) {
+            return new ResponseBodyLWC('Error', 500, e.getMessage() + e.getLineNumber(), '');
+        }
+        res.status = 'Success';
+        res.code = 200;
+        System.debug('res = ' + res);
+        return res;
+    }
+
+    // 妫�绱�(鏈紑鍙戠エ鍑哄簱鍗�)
+    @AuraEnabled
+    public static ResponseBodyLWC InvoiceorderSearch(Consumable_order__c cocLwc,String invoiceIdLwc,String accountidLwc,String userWorkLocationLwc,String agencyProTypeLwc,String HospitalInfoLwc,String SecondDealerLwc,String invoiceOrderRecoedsLwc) {
+        System.debug('杩涘叆 InvoiceorderSearch');
+        ResponseBodyLWC res = new ResponseBodyLWC();
+        Map<String, object> data = new Map<String, object>();
+        res.entity = data;
+
+        coc = cocLwc;
+        HospitalInfo = HospitalInfoLwc;
+        SecondDealer = SecondDealerLwc;
+        errorMsg = '';
+        invoiceOrderRecoeds = (List<InvoiceOrderInfo>)JSON.deserialize(invoiceOrderRecoedsLwc, List<InvoiceOrderInfo>.class);
+        invoiceId = invoiceIdLwc;
+        accountid = accountidLwc;
+        agencyProType = agencyProTypeLwc;
+        userWorkLocation = userWorkLocationLwc;
+        
+        Map<String, String> invoiceOrderMap = new Map<String, String>();
+        String msoql = '';
+        List<InvoiceOrderInfo> invoiceOrderdetailMid = new List<InvoiceOrderInfo>();
+        if (String.isBlank(invoiceId)) {
+            msoql = makeSoql();
+        } else {
+            msoql = makeSoqlInit();
+        }
+        invoiceOrderList = new List<Consumable_order__c>();
+        invoiceOrderList = Database.query(msoql);
+        for (InvoiceOrderInfo ass : invoiceOrderRecoeds) {
+            if (ass.check == true) {
+                invoiceOrderdetailMid.add(ass);
+                invoiceOrderMap.put(ass.esd.Name, ass.esd.Name);
+            }
+        }
+        invoiceOrderRecoeds = new List<InvoiceOrderInfo>();
+        invoiceOrderRecoeds.addAll(invoiceOrderdetailMid);
+        for (Integer i = 0; i < invoiceOrderList.size(); i++) {
+            if (invoiceOrderMap.containsKey(invoiceOrderList[i].Name)) {
+            } else {
+                invoiceOrderRecoeds.add(new InvoiceOrderInfo(invoiceOrderList[i]));
+                invoiceOrderMap.put(invoiceOrderList[i].Name, invoiceOrderList[i].Name);
+            }
+        }
+        data.put('invoiceOrderRecoeds',invoiceOrderRecoeds);
+        res.status = 'Success';
+        res.code = 200;
+        System.debug('res = ' + res);
+        return res;
+    }
+
+    private static String makeSoql() {
+        String soql = 'SELECT Id, Order_ForDealer__r.Name,Order_ForHospital__r.Name,Order_ForDealerText__c, Name,Outbound_Date__c,ShipmentAccount__c,Shipment_total_amount__c,RrturnPro_total_amount__c,Total_Invoiced_Procount__c,InvoiceNotPro_total_amount__c,InvoiceNotPro_money__c,Invoice_total_amount__c,Billed_Status__c  FROM Consumable_order__c  ';
+        soql += ' WHERE Dealer_Info__c = \'' + accountid + '\'';
+        soql += ' AND (recordtypeid = \'' + System.Label.RT_ConOrder_Sale + '\'';
+        soql += ' OR recordtypeid = \'' + System.Label.RT_ConOrder_Shipment + '\'';
+        soql += ' ) ';
+        soql += ' AND Order_Owner_WorkLocal__c = \'' + userWorkLocation + '\' ';
+        soql += ' AND Order_ProType__c =\'' + agencyProType + '\'';
+        soql += ' AND Billed_Status__c != \'鍏ㄩ儴寮�绁╘'';
+        soql += ' AND SummonsForDirction__c != \'浜掔浉璋冭揣\'';
+        if (HospitalInfo != null && HospitalInfo != '') {
+            soql += ' AND Order_ForHospital__c = \'' + HospitalInfo + '\'';
+        }
+        if (String.isNotEmpty(SecondDealer)) {
+            soql += ' AND Order_ForDealer__c = \'' + SecondDealer + '\'';
+        }
+        if (coc.Order_ForDealerText__c != null) {
+            soql += ' AND ShipmentAccount__c = \'' + coc.Order_ForDealerText__c + '\'';
+        }
+        soql += ' AND InvoiceNotPro_total_amount__c > 0';
+        soql += ' AND Onchange_order__c = false limit 1000';
+        return soql;
+    }
+
+    private static String makeSoqlInit() {
+        String soql = 'SELECT Id, Order_ForDealer__r.Name,Order_ForHospital__r.Name,Order_ForDealerText__c, Name,Outbound_Date__c,ShipmentAccount__c,Shipment_total_amount__c,RrturnPro_total_amount__c,Total_Invoiced_Procount__c,InvoiceNotPro_total_amount__c,InvoiceNotPro_money__c,Invoice_total_amount__c,Billed_Status__c  FROM Consumable_order__c  ';
+        soql += ' WHERE Dealer_Info__c = \'' + accountid + '\'';
+        soql += ' AND (recordtypeid = \'' + System.Label.RT_ConOrder_Sale + '\'';
+        soql += ' OR recordtypeid = \'' + System.Label.RT_ConOrder_Shipment + '\'';
+        soql += ' ) ';
+        soql += ' AND Order_Owner_WorkLocal__c = \'' + userWorkLocation + '\' ';
+        soql += ' AND Order_ProType__c =\'' + agencyProType + '\'';
+        soql += ' AND SummonsForDirction__c != \'浜掔浉璋冭揣\' ';
+        if (HospitalInfo != null && HospitalInfo != '') {
+            soql += ' AND Order_ForHospital__c = \'' + HospitalInfo + '\'';
+        }
+        if (SecondDealer != null) {
+            soql += ' AND Order_ForDealer__c = \'' + SecondDealer + '\'';
+        }
+        if (coc.Order_ForDealerText__c != null) {
+            soql += ' AND ShipmentAccount__c = \'' + coc.Order_ForDealerText__c + '\'';
+        }
+        soql += ' AND InvoiceNotPro_total_amount__c > 0';
+        soql += ' AND Onchange_order__c = false limit 1000';
+        return soql;
+    }
+
+    // 鍒犻櫎鎸夐挳
+    @AuraEnabled
+    public static ResponseBodyLWC deleteButton(String invoiceIdLwc,Consumable_order__c cocLwc) {
+        System.debug('杩涘叆 deleteButton');
+        ResponseBodyLWC res = new ResponseBodyLWC();
+        Map<String, object> data = new Map<String, object>();
+        res.entity = data;
+
+        coc = cocLwc;
+        invoiceId = invoiceIdLwc;
+        system.debug('====invoiceId==========>' + invoiceId);
+        List<Consumable_Orderdetails__c> consList = [
+            SELECT id, Consumable_order__c
+            FROM Consumable_Orderdetails__c
+            WHERE Consumable_order__c = :invoiceId
+        ];
+        try {
+            system.debug('====cons==========>' + consList);
+            delete consList;
+            if (coc.id == invoiceId && coc.Invoice_status__c == '鑽夋涓�') {
+                delete coc;
+            }
+            res.status = 'Success';
+            res.code = 200;
+            System.debug('res = ' + res);
+            return res;
+        } catch (Exception e) {
+            // ApexPages.addMessages(e);
+            // return null;
+            return new ResponseBodyLWC('Error', 500, e.getMessage() + ' ' + e.getLineNumber(), '');
+        }
+    }
+
+    // 鎻愪氦鎸夐挳
+    @AuraEnabled
+    public static ResponseBodyLWC approval(List<String> outOrderStringListLwc,Map<String, Consumable_Orderdetails__c> outordercountMapLwc,String invoiceIdLwc) {
+        System.debug('杩涘叆 approval');
+        ResponseBodyLWC res = new ResponseBodyLWC();
+        Map<String, object> data = new Map<String, object>();
+        res.entity = data;
+
+        outOrderStringList = outOrderStringListLwc;
+        outordercountMap = outordercountMapLwc;
+        invoiceId= invoiceIdLwc;
+        //鏇存柊鍙戠エ鏄庣粏2閾炬帴
+        Consumable_order__c P = new Consumable_order__c();
+        List<Consumable_order__c> cocinfo = new List<Consumable_order__c>();
+        List<Consumable_order_details2__c> invoiceorderList2 = new List<Consumable_order_details2__c>();
+        //鏇存柊鍑哄簱鍗曟槑缁�1
+        List<Consumable_Orderdetails__c> outOrderdetUp1List = new List<Consumable_Orderdetails__c>();
+        //闇�瑕佹洿鏂板嚭搴撳崟鏄庣粏1
+        List<Consumable_Orderdetails__c> outOrderdet1List = [
+            SELECT
+                Id,
+                Name,
+                Consumable_order__c,
+                Consumable_order__r.Name,
+                Asset_Model_No__c,
+                Consumable_Product__r.Asset_Model_No__c,
+                Shipment_Count__c,
+                RrturnPro_count__c,
+                Delivery_List_RMB__c,
+                InvoicedProCost_RMB__c,
+                Invoiced_Procount__c,
+                Invoiced_Count__c,
+                Invoice_Unitprice__c,
+                InvoiceProNot_count__c,
+                Invoice_Cost_RMB__c,
+                Invoice_No__c,
+                Box_Piece__c,
+                Invoice_Unit__c
+            FROM Consumable_Orderdetails__c
+            WHERE Consumable_order__c IN :outOrderStringList
+        ];
+        for (Integer i = 0; i < outOrderdet1List.size(); i++) {
+            if (outordercountMap.containsKey(outOrderdet1List[i].Consumable_order__c + outOrderdet1List[i].Asset_Model_No__c)) {
+                Consumable_Orderdetails__c invoiceUpdte1 = new Consumable_Orderdetails__c();
+                invoiceUpdte1.Id = outOrderdet1List[i].Id;
+                invoiceUpdte1.Invoice_Unit__c = outordercountMap.get(
+                        outOrderdet1List[i].Consumable_order__c + outOrderdet1List[i].Asset_Model_No__c
+                    )
+                    .Invoice_Unit__c;
+                if (
+                    outordercountMap.get(outOrderdet1List[i].Consumable_order__c + outOrderdet1List[i].Asset_Model_No__c).Box_Piece__c ==
+                    '鐩�' &&
+                    outordercountMap.get(outOrderdet1List[i].Consumable_order__c + outOrderdet1List[i].Asset_Model_No__c).Invoice_Unit__c ==
+                    '涓�'
+                ) {
+                    Decimal OldinvoicedProcount = 0;
+                    OldinvoicedProcount = (outordercountMap.get(
+                                outOrderdet1List[i].Consumable_order__c + outOrderdet1List[i].Asset_Model_No__c
+                            )
+                            .Invoiced_Count__c /
+                        outordercountMap.get(outOrderdet1List[i].Consumable_order__c + outOrderdet1List[i].Asset_Model_No__c)
+                            .ProductPacking_list_manual__c)
+                        .setScale(2);
+                    invoiceUpdte1.Invoiced_Procount__c = outOrderdet1List[i].Invoiced_Procount__c + OldinvoicedProcount;
+                } else {
+                    invoiceUpdte1.Invoiced_Procount__c =
+                        outOrderdet1List[i].Invoiced_Procount__c +
+                        outordercountMap.get(outOrderdet1List[i].Consumable_order__c + outOrderdet1List[i].Asset_Model_No__c)
+                            .Invoiced_Count__c;
+                }
+                invoiceUpdte1.Invoice_Unitprice__c = outordercountMap.get(
+                        outOrderdet1List[i].Consumable_order__c + outOrderdet1List[i].Asset_Model_No__c
+                    )
+                    .Invoice_Unitprice__c;
+                invoiceUpdte1.InvoicedProCost_RMB__c = invoiceUpdte1.Invoiced_Procount__c * outOrderdet1List[i].Delivery_List_RMB__c;
+
+                Decimal invoicedProcount = 0;
+                if (
+                    outordercountMap.get(outOrderdet1List[i].Consumable_order__c + outOrderdet1List[i].Asset_Model_No__c).Box_Piece__c ==
+                    '鐩�' &&
+                    outordercountMap.get(outOrderdet1List[i].Consumable_order__c + outOrderdet1List[i].Asset_Model_No__c).Invoice_Unit__c ==
+                    '涓�'
+                ) {
+                    invoicedProcount = (outordercountMap.get(
+                                outOrderdet1List[i].Consumable_order__c + outOrderdet1List[i].Asset_Model_No__c
+                            )
+                            .Invoiced_Count__c /
+                        outordercountMap.get(outOrderdet1List[i].Consumable_order__c + outOrderdet1List[i].Asset_Model_No__c)
+                            .ProductPacking_list_manual__c)
+                        .setScale(2);
+                } else {
+                    invoicedProcount = outordercountMap.get(outOrderdet1List[i].Consumable_order__c + outOrderdet1List[i].Asset_Model_No__c)
+                        .Invoiced_Count__c;
+                }
+
+                if (outOrderdet1List[i].InvoiceProNot_count__c < invoicedProcount) {
+                    // ApexPages.addmessage(
+                    //     new ApexPages.message(
+                    //         ApexPages.severity.Error,
+                    //         '鍑哄簱鍗�' +
+                    //         outOrderdet1List[i].Consumable_order__r.Name +
+                    //         '涓紝' +
+                    //         outOrderdet1List[i].Asset_Model_No__c +
+                    //         '杩樻病寮�绁ㄦ暟閲忓皬浜庡彂绁ㄦ暟閲忥紝璇风‘璁ゆ槸鍚︽湁杩斿搧'
+                    //     )
+                    // );
+                    // return null;
+                    return new ResponseBodyLWC('Error', 500, '鍑哄簱鍗�' + outOrderdet1List[i].Consumable_order__r.Name + '涓紝' + outOrderdet1List[i].Asset_Model_No__c + '杩樻病寮�绁ㄦ暟閲忓皬浜庡彂绁ㄦ暟閲忥紝璇风‘璁ゆ槸鍚︽湁杩斿搧', '');
+                }
+
+                outOrderdetUp1List.add(invoiceUpdte1);
+            }
+        }
+        Savepoint sp = Database.setSavepoint();
+        try {
+            //鍑哄簱鍗曟槑缁�1鏇存柊
+            if (outOrderdetUp1List.size() > 0) {
+                ControllerUtil.updateOrderDetails1Satus(outOrderdetUp1List);
+            }
+            //鍙戠エ鐘舵�佹洿鏂�
+            cocinfo = [
+                SELECT Id, Name, Invoice_status__c, Dealer_Info__c, Order_ForHospital__c, SummonsForDirction__c
+                FROM Consumable_order__c
+                WHERE Id = :invoiceId
+            ];
+            System.debug('cocinfo = ' + cocinfo);
+            if (cocinfo.size() > 0) {
+                p = cocinfo[0];
+            }
+            System.debug('p = ' + p);
+            p.Invoice_status__c = '鎻愪氦';
+            update p;
+        } catch (Exception ex) {
+            Database.rollback(sp);
+            // ApexPages.addMessages(ex);
+            // return null;
+            return new ResponseBodyLWC('Error', 500, ex.getMessage() + ' ' + ex.getLineNumber(), '');
+        }
+        res.status = 'Success';
+        res.code = 200;
+        System.debug('res = ' + res);
+        return res;
+    }
+
+    class InvoiceOrderInfo implements Comparable {
+        @AuraEnabled
+        public Boolean check { get; set; }
+        @AuraEnabled
+        public Boolean oldCheck { get; set; }
+        @AuraEnabled
+        public Consumable_order__c esd { get; set; }
+        @AuraEnabled
+        public Consumable_Orderdetails__c esdet { get; set; }
+        @AuraEnabled
+        public Attachment attach { get; set; }
+        @AuraEnabled
+        public ContentVersion cvInfo{ get; set; }
+        @AuraEnabled
+        public String mailSelectOptsin { get; set; }
+        public List<SelectOption> mailSelectOpts { get; set; }
+        @AuraEnabled
+        public Map<String,String> mailSelectOptsMap { get; set; }
+        @AuraEnabled
+        public String invoiceOrderId { get; set; }
+        @AuraEnabled
+        public Decimal needInvoiceCount { get; set; }
+
+        // 鍑哄簱璁㈠崟
+        public InvoiceOrderInfo(Consumable_order__c e) {
+            check = false;
+            oldCheck = false;
+            esd = e;
+            needInvoiceCount = 0;
+        }
+        // 鍑哄簱璁㈠崟鏄庣粏1
+        public InvoiceOrderInfo(Consumable_Orderdetails__c e) {
+            esdet = e;
+        }
+        //闄勪欢
+        public InvoiceOrderInfo(Attachment e) {
+            attach = e;
+            mailSelectOpts = new List<SelectOption>();
+            mailSelectOpts.add(new SelectOption('鍙戠エ鍜屾槑缁�', '鍙戠エ鍜屾槑缁�'));
+            mailSelectOpts.add(new SelectOption('鍙戠エ', '鍙戠エ'));
+            mailSelectOpts.add(new SelectOption('鏄庣粏', '鏄庣粏'));
+        }
+        //ContentVersion
+        public InvoiceOrderInfo(ContentVersion cv) {
+            cvInfo = cv;
+            mailSelectOptsMap = new Map<String,String>();
+            mailSelectOptsMap.put('鍙戠エ鍜屾槑缁�', '鍙戠エ鍜屾槑缁�');
+            mailSelectOptsMap.put('鍙戠エ', '鍙戠エ');
+            mailSelectOptsMap.put('鏄庣粏', '鏄庣粏');
+        }
+        // 鎺掑簭
+        public Integer compareTo(Object compareTo) {
+            return null;
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.1