public List<EntityInvoiceClose> GetInvoiceList(string random, string strWhereSql, string strOrderBySql)
        {
            List<EntityInvoiceClose> entityList = new List<EntityInvoiceClose>();

            #region 認証処理

            string companyId = "";
            string groupId = "";
            string userId = "";
            string ipAdress = "";
            string sessionString = "";
            int idFigureCommodity = 0;
            int idFigureCustomer = 0;
            int idFigurePurchase = 0;
            int idFigureSlipNo = 0;

            try
            {
                companyId = ExCast.zCStr(HttpContext.Current.Session[ExSession.COMPANY_ID]);
                groupId = ExCast.zCStr(HttpContext.Current.Session[ExSession.GROUP_ID]);
                userId = ExCast.zCStr(HttpContext.Current.Session[ExSession.USER_ID]);
                ipAdress = ExCast.zCStr(HttpContext.Current.Session[ExSession.IP_ADRESS]);
                sessionString = ExCast.zCStr(HttpContext.Current.Session[ExSession.SESSION_RANDOM_STR]);
                idFigureCommodity = ExCast.zCInt(HttpContext.Current.Session[ExSession.ID_FIGURE_GOODS]);
                idFigureCustomer = ExCast.zCInt(HttpContext.Current.Session[ExSession.ID_FIGURE_CUSTOMER]);
                idFigurePurchase = ExCast.zCInt(HttpContext.Current.Session[ExSession.ID_FIGURE_PURCHASE]);
                idFigureSlipNo = ExCast.zCInt(HttpContext.Current.Session[ExSession.ID_FIGURE_SLIP_NO]);

                string _message = ExSession.SessionUserUniqueCheck(random, ExCast.zCStr(HttpContext.Current.Session[ExSession.SESSION_RANDOM_STR]), ExCast.zCInt(HttpContext.Current.Session[ExSession.USER_ID]));
                if (_message != "")
                {
                    EntityInvoiceClose entity = new EntityInvoiceClose();
                    entity.MESSAGE = _message;
                    entityList.Add(entity);
                    return entityList;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".GetInvoiceList(認証処理)", ex);
                EntityInvoiceClose entity = new EntityInvoiceClose();
                entity.MESSAGE = "認証処理に失敗しました。" + Environment.NewLine + ex.Message.ToString(); ;
                entityList.Add(entity);
                return entityList;
            }
            #endregion

            StringBuilder sb;
            DataTable dt;
            ExMySQLData db;

            try
            {
                db = ExSession.GetSessionDb(ExCast.zCInt(HttpContext.Current.Session[ExSession.USER_ID]),
                                            ExCast.zCStr(HttpContext.Current.Session[ExSession.SESSION_RANDOM_STR]));

                sb = new StringBuilder();

                ExReportManeger rptMgr = new ExReportManeger();
                rptMgr.idFigureCommodity = idFigureCommodity;
                rptMgr.idFigureCustomer = idFigureCustomer;
                rptMgr.idFigurePurchase = idFigurePurchase;
                rptMgr.idFigureSlipNo = idFigureSlipNo;
                sb.Append(rptMgr.GetInvoiceListReportSQL(companyId, groupId, strWhereSql, strOrderBySql));

                dt = db.GetDataTable(sb.ToString());

                if (dt.DefaultView.Count > 0)
                {
                    for (int i = 0; i <= dt.DefaultView.Count - 1; i++)
                    {
                        #region Set Entity

                        EntityInvoiceClose entity = new EntityInvoiceClose();
                        entity.no = ExCast.zCStr(dt.DefaultView[i]["NO"]);
                        entity.invoice_id = ExCast.zCStr(dt.DefaultView[i]["INVOICE_ID"]);
                        entity.invoice_nm = ExCast.zCStr(dt.DefaultView[i]["INVOICE_NM"]);
                        entity.invoice_yyyymmdd = ExCast.zDateNullToDefault(dt.DefaultView[i]["INVOICE_YYYYMMDD"]);
                        entity.summing_up_group_id = ExCast.zCStr(dt.DefaultView[i]["SUMMING_UP_GROUP_ID"]);
                        entity.summing_up_group_nm = ExCast.zCStr(dt.DefaultView[i]["SUMMING_UP_GROUP_NM"]);
                        entity.person_id = ExCast.zCInt(dt.DefaultView[i]["INPUT_PERSON"]);
                        entity.person_nm = ExCast.zCStr(dt.DefaultView[i]["INPUT_PERSON_NM"]);

                        entity.collect_plan_day = ExCast.zDateNullToDefault(dt.DefaultView[i]["COLLECT_PLAN_DAY"]);
                        entity.collect_day = ExCast.zCInt(dt.DefaultView[i]["COLLECT_DAY"]);

                        entity.before_invoice_yyyymmdd = ExCast.zDateNullToDefault(dt.DefaultView[i]["BEFORE_INVOICE_YYYYMMDD"]);

                        entity.before_invoice_price = ExCast.zCDbl(dt.DefaultView[i]["BEFORE_INVOICE_PRICE"]);
                        entity.before_invoice_price_upd = ExCast.zCDbl(dt.DefaultView[i]["BEFORE_INVOICE_PRICE"]);
                        entity.receipt_price = ExCast.zCDbl(dt.DefaultView[i]["RECEIPT_PRICE"]);
                        entity.transfer_price = ExCast.zCDbl(dt.DefaultView[i]["TRANSFER_PRICE"]);
                        entity.sales_price = ExCast.zCDbl(dt.DefaultView[i]["SALES_PRICE"]);
                        entity.no_tax_sales_price = ExCast.zCDbl(dt.DefaultView[i]["NO_TAX_SALES_PRICE"]);
                        entity.tax = ExCast.zCDbl(dt.DefaultView[i]["TAX"]);

                        entity.invoice_price = ExCast.zCDbl(dt.DefaultView[i]["INVOICE_PRICE"]);

                        // 外税額=税抜金額+消費税額-金額(外税を含めない金額)
                        entity.out_tax = ExCast.zCDbl(dt.DefaultView[i]["OUT_TAX"]);

                        entity.invoice_kbn = ExCast.zCInt(dt.DefaultView[i]["INVOICE_KBN"]);
                        entity.invoice_kbn_nm = ExCast.zCStr(dt.DefaultView[i]["INVOICE_KBN_NM"]);

                        entity.invoice_print_flg = ExCast.zCInt(dt.DefaultView[i]["INVOICE_PRINT_FLG"]);
                        entity.invoice_print_flg_nm = ExCast.zCStr(dt.DefaultView[i]["INVOICE_PRINT_FLG_NM"]);

                        entity.this_receipt_price = ExCast.zCDbl(dt.DefaultView[i]["THIS_RECEIPT_PRICE"]);

                        entity.receipt_receivable_kbn = ExCast.zCInt(dt.DefaultView[i]["RECEIPT_RECEIVABLE_KBN"]);
                        entity.receipt_receivable_kbn_nm = ExCast.zCStr(dt.DefaultView[i]["RECEIPT_RECEIVABLE_KBN_NM"]);

                        //if (entity.this_receipt_price == 0)
                        //{
                        //    if (entity.invoice_price == 0)
                        //    {
                        //        entity.receipt_receivable_kbn = 3;
                        //        entity.receipt_receivable_kbn_nm = "消込済";
                        //    }
                        //    else
                        //    {
                        //        entity.receipt_receivable_kbn = 1;
                        //        entity.receipt_receivable_kbn_nm = "消込未";
                        //    }
                        //}
                        //else if (entity.this_receipt_price < entity.invoice_price)
                        //{
                        //    entity.receipt_receivable_kbn = 2;
                        //    entity.receipt_receivable_kbn_nm = "一部消込";
                        //}
                        //else if (entity.this_receipt_price == entity.invoice_price)
                        //{
                        //    entity.receipt_receivable_kbn = 3;
                        //    entity.receipt_receivable_kbn_nm = "消込済";
                        //}
                        //else if (entity.this_receipt_price > entity.invoice_price)
                        //{
                        //    entity.receipt_receivable_kbn = 4;
                        //    entity.receipt_receivable_kbn_nm = "超過消込";
                        //}
                        //else
                        //{
                        //    entity.receipt_receivable_kbn = 1;
                        //    entity.receipt_receivable_kbn_nm = "消込未";
                        //}

                        entity.invoice_zan_price = ExCast.zCDbl(dt.DefaultView[i]["INVOICE_ZAN_PRICE"]);
                        //entity.invoice_zan_price = entity.invoice_price - entity.this_receipt_price;

                        entity.memo = ExCast.zCStr(dt.DefaultView[i]["MEMO"]);

                        entity.invoice_exists_flg = 0;
                        entity.exec_flg = false;

                        entity.lock_flg = 0;

                        entityList.Add(entity);

                        #endregion

                    }
                }

            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".GetInvoiceList", ex);
                entityList.Clear();
                EntityInvoiceClose entity = new EntityInvoiceClose();
                entity.MESSAGE = CLASS_NM + ".GetInvoiceList : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message.ToString();
                entityList.Add(entity);
            }
            finally
            {
                db = null;
            }

            svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                       companyId,
                                       userId,
                                       ipAdress,
                                       sessionString,
                                       DataPgEvidence.PGName.Order.OrderList,
                                       DataPgEvidence.geOperationType.Select,
                                       "Where:" + strWhereSql + ",Orderby:" + strOrderBySql);

            return entityList;

        }
        public List<EntityInvoiceClose> GetInvoiceTotal(string random, EntityInvoiceClosePrm entityPrm)
        {

            EntityInvoiceClose entity;
            List<EntityInvoiceClose> entityList = new List<EntityInvoiceClose>();

            #region 認証処理

            string companyId = "";
            string groupId = "";
            string userId = "";
            string ipAdress = "";
            string sessionString = "";
            string personId = "";

            try
            {
                companyId = ExCast.zCStr(HttpContext.Current.Session[ExSession.COMPANY_ID]);
                groupId = ExCast.zCStr(HttpContext.Current.Session[ExSession.GROUP_ID]);
                userId = ExCast.zCStr(HttpContext.Current.Session[ExSession.USER_ID]);
                ipAdress = ExCast.zCStr(HttpContext.Current.Session[ExSession.IP_ADRESS]);
                sessionString = ExCast.zCStr(HttpContext.Current.Session[ExSession.SESSION_RANDOM_STR]);
                personId = ExCast.zCStr(HttpContext.Current.Session[ExSession.PERSON_ID]);

                string _message = ExSession.SessionUserUniqueCheck(random, ExCast.zCStr(HttpContext.Current.Session[ExSession.SESSION_RANDOM_STR]), ExCast.zCInt(HttpContext.Current.Session[ExSession.USER_ID]));
                if (_message != "")
                {
                    entity = new EntityInvoiceClose();
                    entity.MESSAGE = _message;
                    entityList.Add(entity);
                    return entityList;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".GetInvoiceTotal(認証処理)", ex);
                entity = new EntityInvoiceClose();
                entity.MESSAGE = CLASS_NM + ".GetInvoiceTotal : 認証処理に失敗しました。" + Environment.NewLine + ex.Message.ToString(); ;
                entityList.Add(entity);
                return entityList;
            }

            #endregion

            StringBuilder sb;
            DataTable dt;
            DataTable dt2;
            ExMySQLData db;

            try
            {
                db = ExSession.GetSessionDb(ExCast.zCInt(HttpContext.Current.Session[ExSession.USER_ID]),
                                            ExCast.zCStr(HttpContext.Current.Session[ExSession.SESSION_RANDOM_STR]));

                sb = new StringBuilder();

                string yyyymmdd = entityPrm.invoice_yyyymmdd;

                #region SQL

                sb.Length = 0;
                sb.Append("SELECT CM.ID " + Environment.NewLine);
                sb.Append("      ,CM.TAX_FRACTION_PROC_ID " + Environment.NewLine);
                sb.Append("      ,CM.NAME " + Environment.NewLine);
                sb.Append("      ,CM.SUMMING_UP_GROUP_ID " + Environment.NewLine);
                sb.Append("      ,CM.COLLECT_DAY " + Environment.NewLine);
                sb.Append("      ,CM.COLLECT_CYCLE_ID " + Environment.NewLine);
                sb.Append("      ,SL.SUM_INVOICE_OUT_PRICE " + Environment.NewLine);
                sb.Append("      ,SL.SUM_INVOICE_IN_PRICE " + Environment.NewLine);
                sb.Append("      ,SL.SUM_PRICE " + Environment.NewLine);
                sb.Append("      ,SL.SUM_TAX " + Environment.NewLine);
                sb.Append("      ,SL.SUM_NO_TAX_PRICE " + Environment.NewLine);
                sb.Append("  FROM M_CUSTOMER AS CM" + Environment.NewLine);

                #region Join

                // 今回売上
                sb.Append("  INNER JOIN (SELECT T.INVOICE_ID" + Environment.NewLine);
                sb.Append("                    ,SUM(CASE WHEN T.TAX_CHANGE_ID = 3 THEN T.SUM_PRICE" + Environment.NewLine);
                sb.Append("                              ELSE 0 END) AS SUM_INVOICE_OUT_PRICE" + Environment.NewLine);
                sb.Append("                    ,SUM(CASE WHEN T.TAX_CHANGE_ID = 6 THEN T.SUM_PRICE" + Environment.NewLine);
                sb.Append("                              ELSE 0 END) AS SUM_INVOICE_IN_PRICE" + Environment.NewLine);
                sb.Append("                    ,SUM(T.SUM_PRICE) AS SUM_PRICE" + Environment.NewLine);
                sb.Append("                    ,SUM(T.SUM_TAX) AS SUM_TAX" + Environment.NewLine);
                sb.Append("                    ,SUM(T.SUM_NO_TAX_PRICE) AS SUM_NO_TAX_PRICE" + Environment.NewLine);
                sb.Append("                FROM T_SALES_H AS T " + Environment.NewLine);
                sb.Append("               WHERE T.DELETE_FLG = 0 " + Environment.NewLine);
                sb.Append("                 AND T.COMPANY_ID = " + companyId + Environment.NewLine);
                sb.Append("                 AND T.GROUP_ID = " + groupId + Environment.NewLine);
                sb.Append("                 AND T.SALES_YMD <= " + ExEscape.zRepStr(yyyymmdd) + Environment.NewLine);
                sb.Append("                 AND T.BUSINESS_DIVISION_ID = 1 " + Environment.NewLine);        // 掛売上のみ

                // 前回請求分以外の売上
                sb.Append("                 AND NOT EXISTS (SELECT IV.NO" + Environment.NewLine);
                sb.Append("                                   FROM T_INVOICE AS IV" + Environment.NewLine);
                sb.Append("                                  WHERE IV.DELETE_FLG = 0 " + Environment.NewLine);
                sb.Append("                                    AND IV.COMPANY_ID = T.COMPANY_ID " + Environment.NewLine);
                sb.Append("                                    AND IV.GROUP_ID = T.GROUP_ID " + Environment.NewLine);
                sb.Append("                                    AND IV.NO = T.INVOICE_NO " + Environment.NewLine);
                sb.Append("                                    AND IV.INVOICE_YYYYMMDD < " + ExEscape.zRepStr(yyyymmdd) + Environment.NewLine);
                sb.Append("                                    AND IV.INVOICE_KBN = 0) " + Environment.NewLine);

                sb.Append("               GROUP BY T.INVOICE_ID " + Environment.NewLine);
                sb.Append("             ) AS SL" + Environment.NewLine);
                sb.Append("     ON SL.INVOICE_ID = CM.ID" + Environment.NewLine);

                                            

                #endregion

                sb.Append(" WHERE CM.COMPANY_ID = " + companyId + Environment.NewLine);
                sb.Append("   AND CM.DELETE_FLG = 0 " + Environment.NewLine);
                if (!string.IsNullOrEmpty(entityPrm.summing_up_group_id))
                {
                    sb.Append("   AND CM.SUMMING_UP_GROUP_ID = " + ExEscape.zRepStr(entityPrm.summing_up_group_id) + Environment.NewLine);
                }
                if (!string.IsNullOrEmpty(entityPrm.invoice_id))
                {
                    sb.Append("   AND CM.ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityPrm.invoice_id)) + Environment.NewLine);
                }

                sb.Append(" ORDER BY CM.ID2 " + Environment.NewLine);
                sb.Append("         ,CM.ID " + Environment.NewLine);

                #endregion

                dt = db.GetDataTable(sb.ToString());

                // 排他制御
                DataPgLock.geLovkFlg lockFlg;
                string strErr = DataPgLock.SetLockPg(companyId, userId, PG_NM, groupId, ipAdress, db, out lockFlg);

                if (strErr != "")
                {
                    entity = new EntityInvoiceClose();
                    entity.MESSAGE = CLASS_NM + ".GetInvoiceTotal : 排他制御(ロック情報取得)に失敗しました。" + Environment.NewLine + strErr;
                    entityList.Add(entity);
                    return entityList;
                }

                if (dt.DefaultView.Count > 0)
                {
                    for (int i = 0; i <= dt.DefaultView.Count - 1; i++)
                    {
                        #region Set Entity

                        entity = new EntityInvoiceClose();
                        entity.no = "未";
                        entity.invoice_id = ExCast.zCStr(dt.DefaultView[i]["ID"]);
                        entity.invoice_nm = ExCast.zCStr(dt.DefaultView[i]["NAME"]);
                        entity.invoice_yyyymmdd = yyyymmdd;
                        entity.summing_up_group_id = ExCast.zCStr(dt.DefaultView[i]["SUMMING_UP_GROUP_ID"]);
                        entity.person_id = ExCast.zCInt(personId);

                        #region 再集計チェック

                        int _invoieReTotalFlg = 0;
                        double _before_invoice_price = 0;

                        #region SQL

                        sb.Length = 0;
                        sb.Append("SELECT T.NO " + Environment.NewLine);
                        sb.Append("      ,T.BEFORE_INVOICE_PRICE " + Environment.NewLine);
                        sb.Append("      ,date_format(T.COLLECT_PLAN_DAY , " + ExEscape.SQL_YMD + ") AS COLLECT_PLAN_DAY" + Environment.NewLine);
                        sb.Append("      ,T.COLLECT_DAY " + Environment.NewLine);
                        sb.Append("  FROM T_INVOICE AS T" + Environment.NewLine);
                        sb.Append(" WHERE T.COMPANY_ID = " + companyId + Environment.NewLine);
                        sb.Append("   AND T.GROUP_ID = " + groupId + Environment.NewLine);
                        sb.Append("   AND T.DELETE_FLG = 0 " + Environment.NewLine);
                        sb.Append("   AND T.INVOICE_KBN = 0 " + Environment.NewLine);       // 締請求分のみ
                        sb.Append("   AND T.INVOICE_ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entity.invoice_id)) + Environment.NewLine);
                        sb.Append("   AND T.INVOICE_YYYYMMDD = " + ExEscape.zRepStr(yyyymmdd) + Environment.NewLine);
                        sb.Append(" LIMIT 0, 1");

                        #endregion

                        dt2 = db.GetDataTable(sb.ToString());
                        if (dt2.DefaultView.Count > 0)
                        {
                            _invoieReTotalFlg = 1;
                            entity.no = ExCast.zCStr(dt2.DefaultView[0]["NO"]);
                            entity.collect_plan_day = ExCast.zDateNullToDefault(dt2.DefaultView[0]["COLLECT_PLAN_DAY"]);
                            entity.collect_day = ExCast.zCInt(dt2.DefaultView[0]["COLLECT_DAY"]);
                            _before_invoice_price = ExCast.zCDbl(dt2.DefaultView[0]["BEFORE_INVOICE_PRICE"]);
                        }

                        #endregion

                        #region 前回請求取得

                        int _invoieExistsFlg = 0;
                        int _before_invoice_yyyyymm = 0;
                        string _before_invoice_yyyyymmdd = "";

                        #region SQL

                        sb.Length = 0;
                        sb.Append("SELECT T.NO " + Environment.NewLine);
                        sb.Append("      ,date_format(T.INVOICE_YYYYMMDD , " + ExEscape.SQL_YMD + ") AS INVOICE_YYYYMMDD" + Environment.NewLine);
                        sb.Append("      ,T.INVOICE_PRICE " + Environment.NewLine);
                        sb.Append("  FROM T_INVOICE AS T " + Environment.NewLine);

                        sb.Append(" WHERE T.DELETE_FLG = 0 " + Environment.NewLine);
                        sb.Append("   AND T.COMPANY_ID = " + companyId + Environment.NewLine);
                        sb.Append("   AND T.GROUP_ID = " + groupId + Environment.NewLine);
                        sb.Append("   AND T.INVOICE_ID = " +  ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(dt.DefaultView[i]["ID"])) + Environment.NewLine);
                        sb.Append("   AND T.INVOICE_KBN = 0 " + Environment.NewLine);               // 締請求分のみ
                        sb.Append("   AND T.INVOICE_YYYYMMDD < " + ExEscape.zRepStr(yyyymmdd) + Environment.NewLine);
                        sb.Append(" LIMIT 0, 1");

                        #endregion

                        dt2 = db.GetDataTable(sb.ToString());
                        if (dt2.DefaultView.Count > 0)
                        {
                            if (_invoieReTotalFlg == 0)
                            {
                                //entity.no = ExCast.zCStr(dt2.DefaultView[0]["NO"]);
                                _before_invoice_price = ExCast.zCDbl(dt2.DefaultView[0]["INVOICE_PRICE"]);
                            }
                            _invoieExistsFlg = 1;

                            _before_invoice_yyyyymmdd = ExCast.zDateNullToDefault(dt2.DefaultView[0]["INVOICE_YYYYMMDD"]);
                            if (_before_invoice_yyyyymmdd != "")
                            {
                                entity.before_invoice_yyyymmdd = _before_invoice_yyyyymmdd;
                            }
                            else
                            {
                                entity.before_invoice_yyyymmdd = "請求無し";
                            }
                        }
                        else
                        {
                            entity.before_invoice_yyyymmdd = "請求無し";
                        }


                        #region 回収予定日

                        string _collect_plan_day = "";
                        int _collect_day = 0;
                        DataPlanDay.GetPlanDay(entity.collect_cycle_id,
                                                             ExCast.zCInt(dt.DefaultView[i]["COLLECT_DAY"]),
                                                             entityPrm.invoice_yyyymmdd,
                                                             ref _collect_plan_day,
                                                             ref _collect_day);

                        if (_invoieReTotalFlg == 0)
                        {
                            entity.collect_plan_day = _collect_plan_day;
                            entity.collect_day = _collect_day;

                            if (!string.IsNullOrEmpty(entityPrm.collect_plan_yyyymmdd))
                            {
                                entity.collect_plan_day = entityPrm.collect_plan_yyyymmdd;
                            }
                        }

                        #endregion

                        #endregion

                        #region 入金取得

                        double _receipt_price = 0;

                        #region SQL

                        sb.Length = 0;
                        sb.Append("SELECT SUM(T.SUM_PRICE) AS SUM_PRICE " + Environment.NewLine);
                        sb.Append("  FROM T_RECEIPT_H AS T" + Environment.NewLine);
                        sb.Append(" WHERE T.COMPANY_ID = " + companyId + Environment.NewLine);
                        sb.Append("   AND T.GROUP_ID = " + groupId + Environment.NewLine);
                        sb.Append("   AND T.DELETE_FLG = 0 " + Environment.NewLine);
                        sb.Append("   AND T.INVOICE_ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entity.invoice_id)) + Environment.NewLine);
                        sb.Append("   AND T.RECEIPT_YMD <= " + ExEscape.zRepStr(yyyymmdd) + Environment.NewLine);
                        sb.Append("   AND IFNULL(T.INVOICE_NO, 0) > 0 " + Environment.NewLine);
                        // 締請求分のみ
                        sb.Append("   AND EXISTS (SELECT IV.NO" + Environment.NewLine);
                        sb.Append("                 FROM T_INVOICE AS IV" + Environment.NewLine);
                        sb.Append("                WHERE IV.DELETE_FLG = 0 " + Environment.NewLine);
                        sb.Append("                  AND IV.COMPANY_ID = T.COMPANY_ID " + Environment.NewLine);
                        sb.Append("                  AND IV.GROUP_ID = T.GROUP_ID " + Environment.NewLine);
                        sb.Append("                  AND IV.NO = T.INVOICE_NO " + Environment.NewLine);
                        sb.Append("                  AND IV.INVOICE_KBN = 0) " + Environment.NewLine);

                        #endregion

                        dt2 = db.GetDataTable(sb.ToString());
                        if (dt2.DefaultView.Count > 0)
                        {
                            _receipt_price = ExCast.zCDbl(dt2.DefaultView[0]["SUM_PRICE"]);
                        }

                        #endregion

                        entity.before_invoice_yyyymmdd = _before_invoice_yyyyymmdd;
                        entity.before_invoice_price = _before_invoice_price;
                        entity.receipt_price = _receipt_price;
                        entity.transfer_price = _before_invoice_price - _receipt_price;
                        entity.sales_price = ExCast.zCDbl(dt.DefaultView[i]["SUM_PRICE"]);                  // 外税額含めない金額
                        entity.no_tax_sales_price = ExCast.zCDbl(dt.DefaultView[i]["SUM_NO_TAX_PRICE"]);
                        entity.tax = ExCast.zCDbl(dt.DefaultView[i]["SUM_TAX"]);                            // 内税・外税計

                        #region 請求時消費税

                        double _invoice_out_tax = 0;
                        double _invoice_in_tax = 0;
                        int _tax_fraction_proc_id = ExCast.zCInt(dt.DefaultView[i]["TAX_FRACTION_PROC_ID"]);
                        double _sum_invoice_out_price = ExCast.zCDbl(dt.DefaultView[i]["SUM_INVOICE_OUT_PRICE"]);       // 外税/請求時 金額
                        double _sum_invoice_in_price = ExCast.zCDbl(dt.DefaultView[i]["SUM_INVOICE_IN_PRICE"]);         // 内税/請求時 金額

                        if (_sum_invoice_out_price != 0 || _sum_invoice_in_price != 0)
                        {
                            // 外税/請求時 消費税
                            if (_sum_invoice_out_price != 0)
                            {
                                _invoice_out_tax = ExMath.zCalcTax(_sum_invoice_out_price, 3, 0, _tax_fraction_proc_id);
                            }
                            // 内税/請求時 消費税
                            if (_sum_invoice_in_price != 0)
                            {
                                _invoice_in_tax = ExMath.zCalcTax(_sum_invoice_out_price, 6, 0, _tax_fraction_proc_id);
                            }

                            // 税額の加算
                            entity.tax += _invoice_out_tax;
                            entity.tax += _invoice_in_tax;

                            // 税抜額の減算
                            entity.no_tax_sales_price -= _invoice_in_tax;
                        }

                        #endregion

                        // 今回請求額=繰越金額+税抜金額+消費税額
                        entity.invoice_price = entity.transfer_price + entity.no_tax_sales_price + entity.tax;

                        // 外税額=税抜金額+消費税額-金額(外税を含めない金額)
                        entity.out_tax = entity.no_tax_sales_price + entity.tax - entity.sales_price;

                        //entity.memo = ExCast.zCStr(dt.DefaultView[i]["MEMO"]);

                        entity.invoice_exists_flg = _invoieExistsFlg;
                        entity.exec_flg = false;

                        entity.lock_flg = (int)lockFlg;

                        entityList.Add(entity);

                        #endregion
                    }
                }

            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".GetInvoiceTotal", ex);
                entity = new EntityInvoiceClose();
                entity.MESSAGE = CLASS_NM + ".GetInvoiceTotal : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message.ToString();
                entityList.Clear();
                entityList.Add(entity);
            }
            finally
            {
                db = null;
            }

            svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                           companyId,
                           userId,
                           ipAdress,
                           sessionString,
                           PG_NM,
                           DataPgEvidence.geOperationType.Select,
                           "");

            return entityList;

        }