binxie
2023-06-26 1b3fb93f787b8b546a307bf063183f5295d183f8
force-app/main/default/classes/ConInvoiceViewController.cls
@@ -1,17 +1,17 @@
public without sharing class ConInvoiceViewController {
    //经销商产品分类
    public String agencyProType {get;set;}
    public String agencyProType { get; set; }
    //发票使用
    public Consumable_order__c coc { get; set; }
    //出库单使用
    public Consumable_order__c outbound { get; set; }
    //发票ID
    public String invoiceId {get;set;}
    public String orderRemind {get;set;}
    public String deliveryId {get;set;}
    public String reopen {get;set;}
    public Decimal sumPrice {get;set;}
    public Boolean isChange {get;set;}
    public String invoiceId { get; set; }
    public String orderRemind { get; set; }
    public String deliveryId { get; set; }
    public String reopen { get; set; }
    public Decimal sumPrice { get; set; }
    public Boolean isChange { get; set; }
    //用户信息
    private String accountid = null;
    private String userId = '';
@@ -28,20 +28,17 @@
        return (coc.Name);
    }
    // 精琢科技   zxk  2021-11-03  存放消耗品明细  end
    public List < Consumable_Orderdetails__c > conoList {
        get;
        set;
    }
    public List<Consumable_Orderdetails__c> conoList { get; set; }
    // 精琢科技   zxk  2021-11-03  存放消耗品明细  end
    public String shipmentAccount{
    public String shipmentAccount {
        get {
            return (coc.ShipmentAccount__c);
        }
    }
    //编辑状态
    public String statusEdit = '';
    public Boolean editAble {get;set;}
    public Boolean editAble { get; set; }
    public List<InvoiceOrderInfo> invoiceOrderRecoeds { get; set; }
    //出库单明细
@@ -54,21 +51,19 @@
    //发票明细1(出库单)
    private List<Consumable_order__c> invoiceOrderSelectedList = new List<Consumable_order__c>();
    Map<String,Decimal> ordermx1defaultMap = new Map<String,Decimal>();
    Map<String, Decimal> ordermx1defaultMap = new Map<String, Decimal>();
    //出库单汇总金额 from 发票明细1
    Map<String,Decimal> outorderMoneytMap = new Map<String,Decimal>();
    Map<String, Decimal> outorderMoneytMap = new Map<String, Decimal>();
    //发票明细1关联产品数量
    Map<String,Consumable_Orderdetails__c> outordercountMap = new Map<String,Consumable_Orderdetails__c>();
    Map<String, Consumable_Orderdetails__c> outordercountMap = new Map<String, Consumable_Orderdetails__c>();
    //发票关联出库单取得
    List<String> outOrderStringList = new List<String>();
    //二级经销商选择框
    public List<SelectOption> provinceOpts  { get; set; }
    public String SecondDealer {get;set;}
    public List<SelectOption> provinceOpts { get; set; }
    public String SecondDealer { get; set; }
    //public List<SelectOption> orderForHospitalOpts { get; set; }
    public String HospitalInfo {get;set;}
    public String HospitalName {get;set;}
    public String HospitalInfo { get; set; }
    public String HospitalName { get; set; }
    //明细数量
    public Integer invoiceOrderRecoedsCount {
@@ -77,9 +72,9 @@
        }
    }
    list<Dealer_elationship__c> Dealerelationship = new list<Dealer_elationship__c>();
    list<Dealer_elationship__c> Dealerelationship = new List<Dealer_elationship__c>();
    //排序使用
    private String[] orderby = new String[]{ 'Outbound_Date__c','Name','ShipmentAccount__c'};
    private String[] orderby = new List<String>{ 'Outbound_Date__c', 'Name', 'ShipmentAccount__c' };
    public String sortKey { get; set; }
    public String preSortKey { get; private set; }
    public Boolean sortOrderAsc { get; private set; }
@@ -100,7 +95,7 @@
        invoiceOrderRecoedschange = new List<InvoiceOrderInfo>();
        orderRemind = '';
        isChange = false;
        conoList = new List < Consumable_Orderdetails__c > ();
        conoList = new List<Consumable_Orderdetails__c>();
    }
    //页面初始化
    public void init() {
@@ -108,103 +103,57 @@
        this.sortKey = '1';
        this.preSortKey = '1';
        this.sortOrderAsc = false;
        this.sortOrder = new String[2];
        this.sortOrder = new String[]{' ',' ',' ',' '};
        this.sortOrder = new List<String>(2);
        this.sortOrder = new List<String>{ ' ', ' ', ' ', ' ' };
        if(invoiceId!=null && invoiceId!='' && (statusEdit==''|| statusEdit==null)){
        if (invoiceId != null && invoiceId != '' && (statusEdit == '' || statusEdit == null)) {
            editAble = false;
        }else if((invoiceId == null||invoiceId=='') && (statusEdit==''||statusEdit==null)){
        } else if ((invoiceId == null || invoiceId == '') && (statusEdit == '' || statusEdit == null)) {
            editAble = true;
        }else if(invoiceId != null && invoiceId !='' && statusEdit !='' && statusEdit!=null){
        } else if (invoiceId != null && invoiceId != '' && statusEdit != '' && statusEdit != null) {
            editAble = true;
        }
        userId = UserInfo.getUserId();
        //userId = '00510000005QQ35';
        List<user> Useracc = New List<user>();
        Useracc = [SELECT accountid, Work_Location__c,UserPro_Type__c FROM user WHERE id = :userId ];
        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)){
        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 ];
        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=='') {
        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++){
                for (Integer i = 0; i < invoiceOrderList.size(); i++) {
                    invoiceOrderRecoeds.add(new InvoiceOrderInfo(invoiceOrderList[i]));
                }
            }else{
                if(statusEdit=='' || statusEdit==null){
            } else {
                if (statusEdit == '' || statusEdit == null) {
                    EditAble = false;
                }else if(statusEdit!=''&&statusEdit!=null){
                } 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,
                coc = [
                    SELECT
                        Id,
                        Name,
                        Invoice_status__c,
                        Invoice_Date__c,
                        Order_ForDealerText__c,
@@ -224,124 +173,205 @@
                        Invoicedet_attachment__c,
                        Outbound_order__c
                    FROM Consumable_order__c
                    WHERE Id =:invoiceId
                    AND Order_type__c = '发票'];
            if(coc.Order_ForDealer__c != null){
                    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){
            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++){
            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++){
            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)){
            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)){
                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){
            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);
                    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)){
            for (InvoiceOrderInfo ass : invoiceOrderRecoeds) {
                if (outorderMoneytMap.containsKey(ass.esd.id)) {
                    ass.needInvoiceCount = outorderMoneytMap.get(ass.esd.id);
                }
                ass.check = true;
            }
        }
        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'outordercountMap +++++' + outordercountMap.size() + outordercountMap));
        if(SecondDealer==null||SecondDealer==''){
        if (SecondDealer == null || SecondDealer == '') {
            provinceOpts.add(new SelectOption('', '-无-'));
            for(Integer i = 0; i < Dealerelationship.size(); i++){
                provinceOpts.add(new SelectOption(Dealerelationship[i].Dealer_subordinate__c, Dealerelationship[i].Dealer_subordinate__r.Name));
            for (Integer i = 0; i < Dealerelationship.size(); i++) {
                provinceOpts.add(
                    new SelectOption(Dealerelationship[i].Dealer_subordinate__c, Dealerelationship[i].Dealer_subordinate__r.Name)
                );
            }
        }else{
        } else {
            provinceOpts.add(new SelectOption('', '-无-'));
            for(Integer i = 0; i < Dealerelationship.size(); i++){
                if(Dealerelationship[i].Dealer_subordinate__r.Name == 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, 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));
            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)
                    );
                }
            }
        }
@@ -365,26 +395,29 @@
            }
        }*/
        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  ];
        if(consumableaccessories.size()>0){
        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);
                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,CreatedDate FROM Attachment WHERE  parentid =:invoiceId  ];
        if(attachmentinfo.size()>0){
        List<Attachment> attachmentinfo = [SELECT Id, Name, OwnerId, CreatedDate FROM Attachment WHERE parentid = :invoiceId];
        if (attachmentinfo.size() > 0) {
            for (Integer i = 0; i < attachmentinfo.size(); i++) {
                attachmentRecoeds.add(new InvoiceOrderInfo(attachmentinfo[i]));
            }
        }
        for (InvoiceOrderInfo ass : attachmentRecoeds)  {
            if(consumableaccessoriesMap.containsKey(ass.attach.Id)){
        for (InvoiceOrderInfo ass : attachmentRecoeds) {
            if (consumableaccessoriesMap.containsKey(ass.attach.Id)) {
                ass.mailSelectOptsin = consumableaccessoriesMap.get(ass.attach.Id);
                ass.invoiceOrderId = invorceOrderIdMap.get(ass.attach.Id);
            }
@@ -395,27 +428,27 @@
    // 检索(未开发票出库单)
    public void InvoiceorderSearch() {
        Map<String,String> invoiceOrderMap = new Map<String,String>();
        String msoql ='';
        Map<String, String> invoiceOrderMap = new Map<String, String>();
        String msoql = '';
        List<InvoiceOrderInfo> invoiceOrderdetailMid = new List<InvoiceOrderInfo>();
        if(String.isBlank(invoiceId)){
        if (String.isBlank(invoiceId)) {
            msoql = makeSoql();
        }else{
        } else {
            msoql = makeSoqlInit();
        }
        //String msoql = makeSoql();
        invoiceOrderList = Database.query(msoql);
        for (InvoiceOrderInfo ass : invoiceOrderRecoeds)  {
            if(ass.check == true){
        for (InvoiceOrderInfo ass : invoiceOrderRecoeds) {
            if (ass.check == true) {
                invoiceOrderdetailMid.add(ass);
                invoiceOrderMap.put(ass.esd.Name,ass.esd.Name);
                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{
        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);
            }
@@ -424,15 +457,15 @@
    public void nvoiceorderremind() {
        List<String> invoiceOrderAccountList = new List<String>();
        for (InvoiceOrderInfo ass : invoiceOrderRecoeds)  {
            if(ass.check == true && ass.esd.ShipmentAccount__c != shipmentAccount){
        for (InvoiceOrderInfo ass : invoiceOrderRecoeds) {
            if (ass.check == true && ass.esd.ShipmentAccount__c != shipmentAccount) {
                invoiceOrderAccountList.add(ass.esd.Name);
            }
        }
        if(invoiceOrderAccountList.size() > 0 ){
            orderRemind = '出库单'+ invoiceOrderAccountList + '客户名与发票客户名不一致!';
        if (invoiceOrderAccountList.size() > 0) {
            orderRemind = '出库单' + invoiceOrderAccountList + '客户名与发票客户名不一致!';
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, orderRemind));
        }else{
        } else {
            orderRemind = '';
            //return '';
        }
@@ -440,12 +473,13 @@
    // 保存附件
    public PageReference saveAttachment() {
        Consumable_accessories_invoice__c attachmentdetails = new Consumable_accessories_invoice__c();
        for (InvoiceOrderInfo ass : AttachmentRecoeds)  {
            if(!String.isBlank(ass.mailSelectOptsin)){
        System.debug('AttachmentRecoeds = ' + AttachmentRecoeds);
        for (InvoiceOrderInfo ass : AttachmentRecoeds) {
            if (!String.isBlank(ass.mailSelectOptsin)) {
                attachmentdetails.Id = ass.invoiceOrderId;
                attachmentdetails.Attachment_ID__c = ass.attach.Id;
                attachmentdetails.Accessories_type__c = ass.mailSelectOptsin;
                if(String.isBlank(ass.invoiceOrderId)){
                if (String.isBlank(ass.invoiceOrderId)) {
                    attachmentdetails.Invoice_code__c = invoiceId;
                }
                upsert attachmentdetails;
@@ -455,15 +489,13 @@
    }
    // 保存按钮
    public PageReference save() {
        System.debug('enter ConInvoiceViewController.save success');
        List<String> chukudanID = new List<String>();
        //  画面录入项判断
        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'HospitalInfo +++++' + HospitalInfo + '-----' + HospitalName));
        //return null;
        if(coc.Name  ==''||coc.Name  == null){
        if (coc.Name == '' || coc.Name == null) {
            coc.Name.addError('请录入发票号。');
            return null;
        }
        if(coc.Invoice_Date__c  == null){
        if (coc.Invoice_Date__c == null) {
            coc.Invoice_Date__c.addError('请录入发票日期。');
            return null;
        }
@@ -473,131 +505,130 @@
            coc.addError('必须输入客户名或者经销商。');
            return null;
        }*/
        if(String.isBlank(HospitalInfo) && SecondDealer  == null){
        if (String.isBlank(HospitalInfo) && SecondDealer == null) {
            coc.addError('必须输入客户名或者二级经销商。');
            return null;
        }
        /*if(String.isBlank(HospitalInfo) && SecondDealer  != null && coc.Order_ForDealerText__c != null){
            coc.Order_ForDealerText__c.addError('请输入二级经销商或者经销商(录入)。');
            return null;
        }*/
        //CHAN-BSS5SQ    update by rentongxiao 2020-08-26  start
/*        if(HospitalInfo  !=null && HospitalInfo  !='' && SecondDealer  != null && coc.Order_ForDealerText__c != null){
            coc.addError('请输入客户名、二级经销商和经销商(录入)只可输入一个。');
            return null;
        }
*/
        if (HospitalInfo != null && HospitalInfo != '' && SecondDealer != null) {
            coc.addError('请输入客户名、二级经销商只可输入一个。');
        }
/*        if(HospitalInfo  !=null && HospitalInfo  !='' && (SecondDealer  != null || coc.Order_ForDealerText__c != null)){
        /*        if(HospitalInfo  !=null && HospitalInfo  !='' && (SecondDealer  != null || coc.Order_ForDealerText__c != null)){
            coc.addError('请输入客户名、二级经销商和经销商(录入)只可输入一个。');
            return null;
        }*/
        //CHAN-BSS5SQ    update by rentongxiao 2020-08-26   end
        if(HospitalInfo  !=null && HospitalInfo  !=''){
            if(coc.Order_ForCustomerText__c ==null){
        if (HospitalInfo != null && HospitalInfo != '') {
            if (coc.Order_ForCustomerText__c == null) {
                coc.Order_ForCustomerText__c.addError('请输入科室。');
                return null;
            }
        }else{
            if(coc.Order_ForCustomerText__c !=null){
        } else {
            if (coc.Order_ForCustomerText__c != null) {
                coc.Order_ForCustomerText__c.addError('不需要输入科室。');
                return null;
            }
        }
        //update  by rentx 2021-02-24 CHAN-BYH9PK start
        //因为之前默认 医院 二级经销商 经销商录入只能出入一个值,所以不会出错,但是现在的情况是经销商有值 经销商录入也有值 所以会报错,因此修改 shipmentAccountString 只能存放一个值即可
        String shipmentAccountString ='';
        String shipmentAccountString = '';
        // String a = HospitalInfo == null? '' : HospitalInfo;
        // String b = SecondDealer == null? '' : SecondDealer;
        // String c = coc.Order_ForDealerText__c == null? '' : coc.Order_ForDealerText__c;
        // shipmentAccountString = a + b + c;
        if (HospitalInfo != null ) {
        if (HospitalInfo != null) {
            shipmentAccountString = HospitalInfo;
        }else if (SecondDealer != null) {
        } else if (SecondDealer != null) {
            shipmentAccountString = SecondDealer;
        }else {
            shipmentAccountString = coc.Order_ForDealerText__c == null? '' : coc.Order_ForDealerText__c;
        } else {
            shipmentAccountString = coc.Order_ForDealerText__c == null ? '' : coc.Order_ForDealerText__c;
        }
        //update by rentx 2021-02-24 CHAN-BYH9PK end
        Integer x = 0;
        List<String> invoiceOrderAccountList = new List<String>();
        for (InvoiceOrderInfo ass : invoiceOrderRecoeds)  {
            if(String.isBlank(deliveryId)){
                if(ass.check == true){
        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){
                    if (HospitalInfo != null && HospitalInfo != '') {
                        if (ass.esd.Order_ForHospital__c != shipmentAccountString) {
                            invoiceOrderAccountList.add(ass.esd.Name);
                        }
                    }
                    if(SecondDealer != null){
                        if(ass.esd.Order_ForDealer__c != shipmentAccountString && ass.esd.Order_ForDealerText__c != shipmentAccountString){
                    if (SecondDealer != null) {
                        if (
                            ass.esd.Order_ForDealer__c != shipmentAccountString &&
                            ass.esd.Order_ForDealerText__c != shipmentAccountString
                        ) {
                            //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, shipmentAccountString  + ass.esd.Order_ForDealer__c + ass.esd.Order_ForDealerText__c));
                            invoiceOrderAccountList.add(ass.esd.Name);
                        }
                    }
                    //update by rentx 2021-2-24 CHAN-BYH9PK start
                    // if(coc.Order_ForDealerText__c != null){
                    if(coc.Order_ForDealerText__c != null && (HospitalInfo == null || HospitalInfo == '' )){
                    //update by rentx 2021-2-24 CHAN-BYH9PK end
                        if((ass.esd.Order_ForDealerText__c != null || ass.esd.Order_ForDealerText__c != '') && ass.esd.ShipmentAccount__c != shipmentAccountString){
                    if (coc.Order_ForDealerText__c != null && (HospitalInfo == null || HospitalInfo == '')) {
                        //update by rentx 2021-2-24 CHAN-BYH9PK end
                        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){
            } else {
                if (HospitalInfo != null && HospitalInfo != '') {
                    System.debug('++++++++++++' + ass.esd.Order_ForHospital__c + '   ' + shipmentAccountString);
                    if (ass.esd.Order_ForHospital__c != shipmentAccountString) {
                        invoiceOrderAccountList.add(ass.esd.Name);
                    }
                }
                if(SecondDealer != null){
                    if(ass.esd.Order_ForDealer__c != shipmentAccountString){
                if (SecondDealer != null) {
                    if (ass.esd.Order_ForDealer__c != shipmentAccountString) {
                        invoiceOrderAccountList.add(ass.esd.Name);
                    }
                }
                //update by rentx 2021-2-24 CHAN-BYH9PK start
                // if(coc.Order_ForDealerText__c != null){
                if(coc.Order_ForDealerText__c != null && (HospitalInfo == null || HospitalInfo == '' )){
                //update by rentx 2021-2-24 CHAN-BYH9PK end
                    if((ass.esd.Order_ForDealerText__c != null || ass.esd.Order_ForDealerText__c != '') && ass.esd.ShipmentAccount__c != shipmentAccountString){
                if (coc.Order_ForDealerText__c != null && (HospitalInfo == null || HospitalInfo == '')) {
                    //update by rentx 2021-2-24 CHAN-BYH9PK end
                    if (
                        (ass.esd.Order_ForDealerText__c != null || ass.esd.Order_ForDealerText__c != '') &&
                        ass.esd.ShipmentAccount__c != shipmentAccountString
                    ) {
                        invoiceOrderAccountList.add(ass.esd.Name);
                    }
                }
            }
        }
        // invoiceOrderAccountList.add('12345');
        if(invoiceOrderAccountList.size() > 0 ){
        if (invoiceOrderAccountList.size() > 0) {
            orderRemind = invoiceOrderAccountList + '出库单客户名与发票客户名不一致!';
            coc.addError(invoiceOrderAccountList + '出库单客户名与发票客户名不一致!');
            return null;
        }else{
        } else {
            orderRemind = '';
            //return '';
        }
        if(String.isBlank(deliveryId)){
            if(x < 1){
        if (String.isBlank(deliveryId)) {
            if (x < 1) {
                coc.addError('请选择需要开发票的出库单!');
                return null;
            }
        }
        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>();
        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)){
        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 ='草案中';
                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;
@@ -605,78 +636,84 @@
                invoiceHead.Invoice_status__c = '草案中';
                invoiceHead.Order_ProType__c = agencyProType;
                invoiceHead.Order_ForCustomerText__c = coc.Order_ForCustomerText__c;
                if(deliveryId != null){
                if (deliveryId != null) {
                    invoiceHead.Outbound_order__c = deliveryId;
                }
                invoiceHead.RecordTypeid = System.Label.RT_ConOrder_Invoice;
                if(SecondDealer==null){
                    if(HospitalInfo != null && HospitalInfo  !=''){
                if (SecondDealer == null) {
                    if (HospitalInfo != null && HospitalInfo != '') {
                        invoiceHead.Order_ForHospital__c = HospitalInfo;
                    }
                }else {
                } 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){
                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)  {
                if (!String.isBlank(deliveryId)) {
                    for (InvoiceOrderInfo ass : invoiceOrderRecoeds) {
                        chukudanID.add(ass.esd.Id);
                        insertDetMap.put(ass.esd.Id,invoiceHead.Id);
                        insertDetMap.put(ass.esd.Id, invoiceHead.Id);
                    }
                }else{
                    for (InvoiceOrderInfo ass : invoiceOrderRecoeds)  {
                        if(!String.isBlank(deliveryId)){
                } 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){
                            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{
                                insertDetMap.put(ass.esd.Id, invoiceHead.Id);
                            } else {
                                //deleteDetMap.put(ass.esd.Id,invoiceHead.Id);
                            }
                        }
                    }
                }
                //新建发票明细1
                if(chukudanID.size() > 0 ){
                if (chukudanID.size() > 0) {
                    newinvoicedetails1(chukudanID);
                }
                //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'deleteDetMap +++++' + deleteDetMap.size()));
                //return null;
                if(insertDetMap.size() > 0 ){
                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){
                        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;
@@ -700,9 +737,8 @@
                    }
                    delete invoiceLinkdetList;
                }*/
            }
            //CHAN-BSS5SQ    update by rentongxiao 2020-09-04   start
            else if (String.isNotBlank(reopen) && reopen.equals('isreopen') && String.isNotBlank(invoiceId)) {
            } else if (String.isNotBlank(reopen) && reopen.equals('isreopen') && String.isNotBlank(invoiceId)) {
                //CHAN-BSS5SQ    update by rentongxiao 2020-09-04   start
                //如果是 作废重开
                //1. 设置新发票信息
                invoiceHead.Name = coc.Name;
@@ -713,67 +749,83 @@
                invoiceHead.Invoice_status__c = '草案中';
                invoiceHead.Order_ProType__c = agencyProType;
                invoiceHead.Order_ForCustomerText__c = coc.Order_ForCustomerText__c;
                if(deliveryId != null){
                if (deliveryId != null) {
                    invoiceHead.Outbound_order__c = deliveryId;
                }
                invoiceHead.RecordTypeid = System.Label.RT_ConOrder_Invoice;
                if(SecondDealer==null){
                    if(HospitalInfo != null && HospitalInfo  !=''){
                if (SecondDealer == null) {
                    if (HospitalInfo != null && HospitalInfo != '') {
                        invoiceHead.Order_ForHospital__c = HospitalInfo;
                    }
                }else {
                } else {
                    invoiceHead.Order_ForDealer__c = SecondDealer;
                }
                invoiceHead.Order_ForDealerText__c = coc.Order_ForDealerText__c;
                System.debug('invoiceHead = ' + invoiceHead);
                insert 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];
                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){
                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>();
                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){
                    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);
                    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
                    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){
                for (Consumable_Orderdetails__c corderc : updateFromList) {
                    String tempStr = corderc.Consumable_order__c + corderc.Asset_Model_No__c;
                    if (tempCocMap.get(tempStr) != null ) {
                    if (tempCocMap.get(tempStr) != null) {
                        //设置已发票金额 出库单的已发票金额 - 发票的发票金额
                        corderc.InvoicedProCost_RMB__c = corderc.InvoicedProCost_RMB__c - tempCocMap.get(tempStr).InvoicedProduct_RMB__c;
                        //设置已发票数量  出库单的已发票数量 - 发票的发票数量(盒)
@@ -781,159 +833,173 @@
                        //设置发票单价
                        if (corderc.Box_Piece__c == '盒' && corderc.Invoice_Unit__c == '个') {
                            corderc.Invoice_Unitprice__c = corderc.Delivery_List_RMB__c / corderc.ProductPacking_list_manual__c;
                        }else{
                        } 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);
                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);
                        }
                    }
                    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);
                                }
                            }
                            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('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);
                        }
                        delete invoiceLinkdetList;
                    }
                    //新建发票明细1
                    if(chukudanID.size() > 0 ){
                        newinvoicedetails1(chukudanID);
                    }
            }
            //CHAN-BSS5SQ    update by rentongxiao 2020-09-04   end
            //编辑后保存
            else{
                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){
                    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);
                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>();
                    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){
                        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];
                        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{
                    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);
                            }
                        }
@@ -941,31 +1007,37 @@
                    //已换货单出库数量信息
                    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){
                        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{
                } else {
                    invoiceHead.Name = coc.Name;
                    invoiceHead.Invoice_status__c = coc.Invoice_status__c;
                    invoiceHead.Invoice_Date__c = coc.Invoice_Date__c;
@@ -976,53 +1048,53 @@
                    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  !=''){
                    if (HospitalName != null && HospitalName != '') {
                        if (HospitalInfo != null && HospitalInfo != '') {
                            invoiceHead.Order_ForHospital__c = HospitalInfo;
                        }
                    }else{
                    } else {
                        invoiceHead.Order_ForHospital__c = null;
                    }
                    invoiceHead.Order_ForDealer__c = 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{
                    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);
                            deleteDetMap.put(ass.esd.Id, invoiceHead.Id);
                        }
                    }
                    if(outOrderIdList.size() > 0){
                    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];
                    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);
                        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);
                            }
                            //获取该出库单
                            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;
                        }
                        upsert invoiceLinkList;
                    }
                    /*if(insertDetMap.size() > 0 ){
                        for (String field : insertDetMap.keySet()) {
@@ -1057,16 +1129,17 @@
                        delete invoiceLinkdetList;
                    }*/
                    //删除出库单链接表
                    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.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);
                            }
                        }
@@ -1074,12 +1147,12 @@
                    }
                    //CHAN-BSS5SQ    update by rentongxiao 2020-09-08  end
                    //新建发票明细1
                    if(chukudanID.size() > 0 ){
                    if (chukudanID.size() > 0) {
                        newinvoicedetails1(chukudanID);
                    }
                }
            }
        }catch (Exception ex) {
        } catch (Exception ex) {
            Database.rollback(sp);
            ApexPages.addMessages(ex);
            return null;
@@ -1088,15 +1161,18 @@
    }
    //换货单链接表 发票明细2做成
    public void outOrderchange(Map<String,String> insertDetMap){
    public void outOrderchange(Map<String, String> insertDetMap) {
        List<string> chukudanID = new List<string>();
        List<Consumable_order_LinkTable__c> invoiceLinkList = New List<Consumable_order_LinkTable__c>();
        List<Consumable_order_LinkTable__c> invoiceLinkList = new List<Consumable_order_LinkTable__c>();
        Consumable_order__c p = new Consumable_order__c();
        if(insertDetMap.size() > 0 ){
        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){
                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);
@@ -1114,28 +1190,30 @@
        }
        //换货出库单明细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){
            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;
@@ -1163,18 +1241,19 @@
            changeOD.Invoice_Date__c = Date.today();
        }
        update detaliFromList;*/
    }
    //取得更新map初始化数据
    //获取 订单中产品的初始化 Map
    public void getdefaultMapinfo(List<String> orderIdList){
    public 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);
        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);
        }
    }
    //删除发票明细1
@@ -1191,53 +1270,54 @@
            delete detInvoicedet1List;
        }
    }*/
    public void deleteOutboundorder(Map<String,String> deleteMap,String value){
    public 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()];
            delete detInvoicedet1List;
        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;
    }
    //CHAN-BSS5SQ    update by rentongxiao 2020-09-08  end
    public void newinvoicedetails1(List<String> chukudanID){
    public void newinvoicedetails1(List<String> chukudanID) {
        //新规发票明细1
        List<Consumable_Orderdetails__c> invoiceorderList1 = New List<Consumable_Orderdetails__c>();
        List<Consumable_Orderdetails__c> invoiceorderList1 = new List<Consumable_Orderdetails__c>();
        //出库单明细更新
        List<Consumable_Orderdetails__c> outOrderList1 = 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
            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++){
        for (Integer i = 0; i < detaliFromList.size(); i++) {
            Consumable_Orderdetails__c invoiceInsert1 = new Consumable_Orderdetails__c();
            //Consumable_Orderdetails__c orderUpdate1 = new Consumable_Orderdetails__c();
            //orderUpdate1.id = detaliFromList[i].Id;
@@ -1245,30 +1325,37 @@
            //invoiceInsert1.Asset_Model_No__c = detaliFromList[i].Asset_Model_No__c;
            invoiceInsert1.Send_date__c = detaliFromList[i].Send_date__c;
            String str = string.valueOf(i + 1);
            if(str.length() == 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.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{
            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;
            //orderUpdate1.Invoice_Unitprice__c = detaliFromList[i].Delivery_List_RMB__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;
            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_Procount__c = detaliFromList[i].Invoiced_Procount__c;
            invoiceInsert1.Invoiced_Count__c = detaliFromList[i].InvoiceProNot_count__c;
            if (detaliFromList[i].InvoicedProCost_RMB__c == null) detaliFromList[i].InvoicedProCost_RMB__c = 0;
            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.Sum_of_money__c = invoiceInsert1.InvoicedProCost_RMB__c;
            invoiceInsert1.Consumable_order__c = invoiceId;
@@ -1281,28 +1368,34 @@
        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'invoiceorderList1 +++++' + invoiceorderList1.size()));
        //return ;
        Savepoint sp = Database.setSavepoint();
        if(invoiceorderList1.size() > 0){
        if (invoiceorderList1.size() > 0) {
            try {
                System.debug('invoiceorderList1 = ' + invoiceorderList1);
                insert invoiceorderList1;
            }catch (Exception ex) {
            } catch (Exception ex) {
                Database.rollback(sp);
                ApexPages.addMessages(ex);
            }
        }
    }
    //上传附件
    public PageReference FilesUpload(){
        PageReference ref = new Pagereference('/p/attach/NoteAttach?pid='+invoiceId+'&retURL=%2F' + '/ConInvoiceView?invoiceId=' +invoiceId);
    public PageReference FilesUpload() {
        PageReference ref = new Pagereference(
            '/p/attach/NoteAttach?pid=' +
            invoiceId +
            '&retURL=%2F' +
            '/ConInvoiceView?invoiceId=' +
            invoiceId
        );
        ref.setRedirect(true);
        return ref;
    }
    //排序
    public void SortLimited(){
    public void SortLimited() {
        List<Id> orderIdList = new List<Id>();
        for (InvoiceOrderInfo ass : invoiceOrderRecoeds)  {
        for (InvoiceOrderInfo ass : invoiceOrderRecoeds) {
            orderIdList.add(ass.esd.Id);
        }
        if (this.sortKey == this.preSortKey) {
@@ -1323,35 +1416,39 @@
        soql += ' ' + (this.sortOrderAsc == true ? 'asc nulls first' : 'desc nulls last');
        List<Consumable_order__c> queryList = Database.query(soql);
        invoiceOrderRecoeds = new List<InvoiceOrderInfo>();
        for(Integer i = 0; i < queryList.size(); i++){
        for (Integer i = 0; i < queryList.size(); i++) {
            invoiceOrderRecoeds.add(new InvoiceOrderInfo(queryList[i]));
        }
        for (InvoiceOrderInfo ass : invoiceOrderRecoeds)  {
            if(outorderMoneytMap.containsKey(ass.esd.id)){
        for (InvoiceOrderInfo ass : invoiceOrderRecoeds) {
            if (outorderMoneytMap.containsKey(ass.esd.id)) {
                ass.needInvoiceCount = outorderMoneytMap.get(ass.esd.id);
            }
        }
    }
    // 编辑按钮
    public PageReference setEditAble(){
    public PageReference setEditAble() {
        statusEdit = 'Redirect';
        PageReference ref = new Pagereference('/ConInvoiceView?invoiceId='+ invoiceId +'&KeyWords='+statusEdit);
        PageReference ref = new Pagereference('/ConInvoiceView?invoiceId=' + invoiceId + '&KeyWords=' + statusEdit);
        ref.setRedirect(true);
        return ref;
    }
    // 返回不可编辑状态
    public PageReference UnabletoEdit(){
        PageReference ref = new Pagereference('/ConInvoiceView?invoiceId='+ invoiceId);
    public PageReference UnabletoEdit() {
        PageReference ref = new Pagereference('/ConInvoiceView?invoiceId=' + invoiceId);
        ref.setRedirect(true);
        return ref;
    }
     // 精琢科技  zxk  2021-11-03  CHAN-C8D86K 添加删除按钮  start
    // 精琢科技  zxk  2021-11-03  CHAN-C8D86K 添加删除按钮  start
    // 删除按钮
    public PageReference deleteButton() {
        system.debug('====invoiceId==========>' + invoiceId);
        List < Consumable_Orderdetails__c > consList = [select id, Consumable_order__c from Consumable_Orderdetails__c where Consumable_order__c = : invoiceId];
        List<Consumable_Orderdetails__c> consList = [
            SELECT id, Consumable_order__c
            FROM Consumable_Orderdetails__c
            WHERE Consumable_order__c = :invoiceId
        ];
        // Consumable_order__c consumable = [select id from Consumable_order__c where id =: invoiceId AND Invoice_status__c = '草案中'];
        try {
            system.debug('====cons==========>' + consList);
@@ -1373,11 +1470,10 @@
    }
    // 精琢科技  zxk  2021-11-03  CHAN-C8D86K 添加删除按钮  end
    // 作废重开按钮
    public PageReference setreopen(){
    public PageReference setreopen() {
        statusEdit = 'Redirect';
        PageReference ref = new Pagereference('/ConInvoiceView?invoiceId='+ invoiceId + '&reopen=isreopen' +'&KeyWords='+statusEdit);
        PageReference ref = new Pagereference('/ConInvoiceView?invoiceId=' + invoiceId + '&reopen=isreopen' + '&KeyWords=' + statusEdit);
        ref.setRedirect(true);
        return ref;
    }
@@ -1385,78 +1481,123 @@
    public PageReference approval() {
        //更新发票明细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>();
        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> 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
            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)){
        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 == '个'){
                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;
                    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.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 (
                    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 +'还没开票数量小于发票数量,请确认是否有返品。'));
                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;
                }
                outOrderdetUp1List.add(invoiceUpdte1);
            }
        }
        Savepoint sp = Database.setSavepoint();
        try {
            //出库单明细1更新
            if(outOrderdetUp1List.size() > 0){
            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];
            if (cocinfo.size()>0){
            cocinfo = [
                SELECT Id, Name, Invoice_status__c, Dealer_Info__c, Order_ForHospital__c, SummonsForDirction__c
                FROM Consumable_order__c
                WHERE Id = :invoiceId
            ];
            if (cocinfo.size() > 0) {
                p = cocinfo[0];
            }
            p.Invoice_status__c = '提交';
            update p;
        }catch (Exception ex) {
        } catch (Exception ex) {
            Database.rollback(sp);
            ApexPages.addMessages(ex);
            return null;
@@ -1465,23 +1606,23 @@
    }
    // 做成检索SQL文
    private String makeSoqlInit(){
    private 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 += ' 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 Order_ProType__c =\'' + agencyProType + '\'';
        soql += ' AND SummonsForDirction__c != \'互相调货\' ';
        if(HospitalInfo != null && HospitalInfo  !=''){
            soql += ' AND Order_ForHospital__c = \'' +  HospitalInfo + '\'';
        if (HospitalInfo != null && HospitalInfo != '') {
            soql += ' AND Order_ForHospital__c = \'' + HospitalInfo + '\'';
        }
        if(SecondDealer != null){
            soql += ' AND Order_ForDealer__c = \'' +  SecondDealer + '\'';
        if (SecondDealer != null) {
            soql += ' AND Order_ForDealer__c = \'' + SecondDealer + '\'';
        }
        if(coc.Order_ForDealerText__c != null){
            soql += ' AND ShipmentAccount__c = \'' +  coc.Order_ForDealerText__c + '\'';
        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';
@@ -1489,29 +1630,28 @@
    }
    // 做成检索SQL文
    private String makeSoql(){
    private 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 += ' 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 Order_ProType__c =\'' + agencyProType + '\'';
        soql += ' AND Billed_Status__c != \'全部开票\'';
        soql += ' AND SummonsForDirction__c != \'互相调货\'';
        if(HospitalInfo != null && HospitalInfo  !=''){
            soql += ' AND Order_ForHospital__c = \'' +  HospitalInfo + '\'';
        if (HospitalInfo != null && HospitalInfo != '') {
            soql += ' AND Order_ForHospital__c = \'' + HospitalInfo + '\'';
        }
        if(SecondDealer != null){
            soql += ' AND Order_ForDealer__c = \'' +  SecondDealer + '\'';
        if (SecondDealer != null) {
            soql += ' AND Order_ForDealer__c = \'' + SecondDealer + '\'';
        }
        if(coc.Order_ForDealerText__c != null){
            soql += ' AND ShipmentAccount__c = \'' +  coc.Order_ForDealerText__c + '\'';
        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;
    }
    // Data Bean
@@ -1521,9 +1661,9 @@
        public Consumable_order__c esd { get; set; }
        public Consumable_Orderdetails__c esdet { get; set; }
        public Attachment attach { get; set; }
        public String mailSelectOptsin {get;set;}
        public List<SelectOption> mailSelectOpts  { get; set; }
        public String invoiceOrderId {get;set;}
        public String mailSelectOptsin { get; set; }
        public List<SelectOption> mailSelectOpts { get; set; }
        public String invoiceOrderId { get; set; }
        public Decimal needInvoiceCount { get; set; }
        // 出库订单