// セッション保持情報の追加
        public static bool AddSessionInf(int userId, string randomString, ExMySQLData db, ref string message)
        {
            try
            {
                DelSessionInf(userId);

                for (int i = 0; i <= sessionInf.Count - 1; i++)
                {
                    if (sessionInf[i].userId == userId)
                    {
                        return false;
                    }
                }

                sessionInf.Add(new SessionHold(userId, randomString, db));

                return true;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return false;
            }

        }
        /// <summary>
        /// 納品区分更新
        /// </summary>
        /// <param name="companyId"></param>
        /// <param name="db"></param>
        /// <param name="customerId"></param>
        /// <param name="ipAdress"></param>
        /// <param name="userId"></param>
        public static void UpdDeliverDivision(gUpdDeliverDivisionKbn kbn,
                                              string companyId,
                                              string groupId,
                                              long Id,
                                              int recNo,
                                              ExMySQLData db, 
                                              int deliverDivisionId,
                                              string pg_nm,
                                              int update_person_id,
                                              string ipAdress, 
                                              string userId)
        {
            StringBuilder sb;

            try
            {
                sb = new StringBuilder();
                sb.Length = 0;

                switch (kbn)
                { 
                    case gUpdDeliverDivisionKbn.Order:
                        sb.Append("UPDATE T_ORDER_D " + Environment.NewLine);
                        sb.Append(CommonUtl.GetUpdSQLCommonColums(pg_nm,
                                                                  update_person_id,
                                                                  ipAdress,
                                                                  userId,
                                                                  0));
                        sb.Append("      ,DELIVER_DIVISION_ID = " + deliverDivisionId.ToString() + Environment.NewLine);
                        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                        sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                        sb.Append("   AND ORDER_ID = " + Id + Environment.NewLine);
                        sb.Append("   AND REC_NO = " + recNo + Environment.NewLine);
                        break;
                    case gUpdDeliverDivisionKbn.Purchase:
                        sb.Append("UPDATE T_PURCHASE_ORDER_D " + Environment.NewLine);
                        sb.Append(CommonUtl.GetUpdSQLCommonColums(pg_nm,
                                                                  update_person_id,
                                                                  ipAdress,
                                                                  userId,
                                                                  0));
                        sb.Append("      ,DELIVER_DIVISION_ID = " + deliverDivisionId.ToString() + Environment.NewLine);
                        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                        sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                        sb.Append("   AND PURCHASE_ORDER_ID = " + Id + Environment.NewLine);
                        sb.Append("   AND REC_NO = " + recNo + Environment.NewLine);
                        break;
                }


                db.ExecuteSQL(sb.ToString(), false);

            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdDeliverDivision", ex);
                throw;
            }
        }
        /// <summary>
        /// 売掛残高更新
        /// </summary>
        /// <param name="companyId"></param>
        /// <param name="db"></param>
        /// <param name="customerId"></param>
        /// <param name="ipAdress"></param>
        /// <param name="userId"></param>
        public static void UpdSalesCredit(string companyId,
                                          string groupId,
                                          ExMySQLData db, 
                                          string customerId,
                                          double price,
                                          string pg_nm,
                                          int update_person_id,
                                          string ipAdress, 
                                          string userId)
        {
            StringBuilder sb;

            try
            {
                sb = new StringBuilder();

                sb.Length = 0;
                sb.Append("UPDATE M_CUSTOMER " + Environment.NewLine);
                sb.Append(CommonUtl.GetUpdSQLCommonColums(pg_nm,
                                                          update_person_id,
                                                          ipAdress,
                                                          userId,
                                                          0));
                sb.Append("      ,SALES_CREDIT_PRICE = SALES_CREDIT_PRICE + " + price.ToString() + Environment.NewLine);

                sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);                                // COMPANY_ID
                sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(customerId)) + Environment.NewLine);  // ID

                db.ExecuteSQL(sb.ToString(), false);

                sb.Length = 0;
                sb.Append("UPDATE M_SALES_CREDIT_BALANCE " + Environment.NewLine);

                sb.Append(CommonUtl.GetUpdSQLCommonColums(pg_nm,
                                                          update_person_id,
                                                          ipAdress,
                                                          userId,
                                                          0));
                sb.Append("      ,SALES_CREDIT_PRICE = SALES_CREDIT_PRICE + " + price.ToString() + Environment.NewLine);

                sb.Append(" WHERE DELETE_FLG = 0 " + Environment.NewLine);
                sb.Append("   AND COMPANY_ID = " + companyId + Environment.NewLine);
                sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(customerId)) + Environment.NewLine);  // ID

                db.ExecuteSQL(sb.ToString(), false);

            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdSalesCredit", ex);
                throw;
            }
        }
        /// <summary>
        /// 商品在庫更新
        /// </summary>
        /// <param name="companyId"></param>
        /// <param name="db"></param>
        /// <param name="customerId"></param>
        /// <param name="ipAdress"></param>
        /// <param name="userId"></param>
        public static void UpdCommodityInventory(string companyId,
                                                 string groupId,
                                                 ExMySQLData db, 
                                                 string customerId,
                                                 double number,
                                                 string pg_nm,
                                                 int update_person_id,
                                                 string ipAdress, 
                                                 string userId)
        {
            StringBuilder sb;

            try
            {
                sb = new StringBuilder();

                sb.Length = 0;
                sb.Append("UPDATE M_COMMODITY " + Environment.NewLine);
                sb.Append(CommonUtl.GetUpdSQLCommonColums(pg_nm,
                                                          update_person_id,
                                                          ipAdress,
                                                          userId,
                                                          0));
                sb.Append("      ,INVENTORY_NUMBER = INVENTORY_NUMBER + " + number.ToString() + Environment.NewLine);

                sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);                                // COMPANY_ID
                sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(customerId)) + Environment.NewLine);  // ID

                db.ExecuteSQL(sb.ToString(), false);

                sb.Length = 0;
                sb.Append("UPDATE M_COMMODITY_INVENTORY " + Environment.NewLine);
                sb.Append(CommonUtl.GetUpdSQLCommonColums(pg_nm,
                                                          update_person_id,
                                                          ipAdress,
                                                          userId,
                                                          0));
                sb.Append("      ,INVENTORY_NUMBER = INVENTORY_NUMBER + " + number.ToString() + Environment.NewLine);

                sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);                                           // COMPANY_ID
                sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(customerId)) + Environment.NewLine);  // ID

                db.ExecuteSQL(sb.ToString(), false);

            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdCommodityInventory", ex);
                throw;
            }
        }
        /// <summary>
        /// 支払締チェック
        /// </summary>
        /// <param name="companyId"></param>
        /// <param name="db"></param>
        /// <param name="customerId"></param>
        /// <param name="ipAdress"></param>
        /// <param name="userId"></param>
        public static bool IsPaymentClose(string companyId,
                                          ExMySQLData db,
                                          string purchaseId,
                                          string ymd)
        {
            StringBuilder sb = new StringBuilder();
            DataTable dt;

            if (ymd.Length != 10) throw new Exception();
            if (!ExCast.IsDate(ymd)) throw new Exception();

            try
            {
                sb.Length = 0;
                sb.Append("SELECT T.NO " + Environment.NewLine);
                sb.Append("  FROM T_PAYMENT AS T" + Environment.NewLine);
                sb.Append(" WHERE T.COMPANY_ID = " + companyId + Environment.NewLine);
                sb.Append("   AND T.DELETE_FLG = 0 " + Environment.NewLine);
                sb.Append("   AND T.PAYMENT_KBN = 0 " + Environment.NewLine);       // 支払区分:締処理
                sb.Append("   AND T.PAYMENT_YYYYMMDD >= " + ExEscape.zRepStr(ymd) + Environment.NewLine);
                if (purchaseId != "")
                {
                    sb.Append("   AND T.PURCHASE_ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(purchaseId)) + Environment.NewLine);
                }
                sb.Append(" LIMIT 0, 1");

                dt = db.GetDataTable(sb.ToString());
                if (dt.DefaultView.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".IsPaymentClose", ex);
                throw;
            }
        }
        /// <summary>
        /// 最大ID取得
        /// </summary>
        /// <param name="db"></param>
        /// <param name="id">ID</param>
        /// <param name="ipAdress"></param>
        /// <param name="userId"></param>
        public static void GetMaxId(ExMySQLData db,
                                    string tbl_name,
                                    string id_col_name,
                                    out string id)
        {
            StringBuilder sb;
            DataTable dt;

            bool exist = false;

            try
            {
                sb = new StringBuilder();
                sb.Length = 0;

                // 存在確認
                sb.Append("SELECT " + id_col_name + " AS MAX_ID " + Environment.NewLine);
                sb.Append("  FROM " + tbl_name + Environment.NewLine);
                sb.Append(" ORDER BY " + id_col_name + " DESC " + Environment.NewLine);
                sb.Append(" LIMIT 0, 1 " + Environment.NewLine);

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

                if (dt.DefaultView.Count > 0)
                {
                    id = (ExCast.zCLng(dt.DefaultView[0]["MAX_ID"]) + 1).ToString();
                }
                else
                    id = "1";
                }

            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".GetMaxId", ex);
                id = "";
                throw;
            }
        }
        public string UpdateSupplier(string random, int type, string CustomerId, string Id, EntitySupplier entity)
        {

            #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 != "")
                {
                    return _message;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSupplier(認証処理)", ex);
                return CLASS_NM + ".UpdateSupplier : 認証処理に失敗しました。" + Environment.NewLine + ex.Message.ToString();
            }

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;
            string _Id = "";

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSupplier(DbOpen)", ex);
                return CLASS_NM + ".UpdateSupplier(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSupplier(BeginTransaction)", ex);
                return CLASS_NM + ".UpdateSupplier(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Get Max Master ID

            if (type == 1 && (Id == "" || Id == "0"))
            {
                try
                {
                    DataMasterId.GetMaxMasterId(companyId,
                                                ExCast.zNumZeroNothingFormat(CustomerId),
                                                db,
                                                DataMasterId.geMasterMaxIdKbn.Supplier,
                                                out _Id);

                    if (_Id == "")
                    {
                        return "ID取得に失敗しました。";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSupplier(GetMaxMasterId)", ex);
                    return CLASS_NM + ".UpdateSupplier(GetMaxMasterId) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }
            else
            {
                _Id = Id;
            }

            #endregion

            #region Insert

            if (type == 1)
            {
                try
                {
                    #region Delete SQL

                    sb.Length = 0;
                    sb.Append("DELETE FROM M_SUPPLIER " + Environment.NewLine);
                    sb.Append(" WHERE DELETE_FLG = 1 " + Environment.NewLine);
                    sb.Append("   AND COMPANY_ID = " + companyId + Environment.NewLine);
                    sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(_Id)) + Environment.NewLine);
                    sb.Append("   AND CUSTOMER_ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(CustomerId)) + Environment.NewLine);

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                    #region Insert SQL

                    sb.Length = 0;
                    sb.Append("INSERT INTO M_SUPPLIER " + Environment.NewLine);
                    sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                    sb.Append("       , CUSTOMER_ID" + Environment.NewLine);
                    sb.Append("       , ID" + Environment.NewLine);
                    sb.Append("       , ID2" + Environment.NewLine);
                    sb.Append("       , NAME" + Environment.NewLine);
                    sb.Append("       , KANA" + Environment.NewLine);
                    sb.Append("       , ABOUT_NAME" + Environment.NewLine);
                    sb.Append("       , ZIP_CODE" + Environment.NewLine);
                    sb.Append("       , PREFECTURE_ID" + Environment.NewLine);
                    sb.Append("       , CITY_ID" + Environment.NewLine);
                    sb.Append("       , TOWN_ID" + Environment.NewLine);
                    sb.Append("       , ADRESS_CITY" + Environment.NewLine);
                    sb.Append("       , ADRESS_TOWN" + Environment.NewLine);
                    sb.Append("       , ADRESS1" + Environment.NewLine);
                    sb.Append("       , ADRESS2" + Environment.NewLine);
                    sb.Append("       , STATION_NAME" + Environment.NewLine);
                    sb.Append("       , POST_NAME" + Environment.NewLine);
                    sb.Append("       , PERSON_NAME" + Environment.NewLine);
                    sb.Append("       , TITLE_ID" + Environment.NewLine);
                    sb.Append("       , TITLE_NAME" + Environment.NewLine);
                    sb.Append("       , TEL" + Environment.NewLine);
                    sb.Append("       , FAX" + Environment.NewLine);
                    sb.Append("       , MAIL_ADRESS" + Environment.NewLine);
                    sb.Append("       , MOBILE_TEL" + Environment.NewLine);
                    sb.Append("       , MOBILE_ADRESS" + Environment.NewLine);
                    sb.Append("       , URL" + Environment.NewLine);
                    sb.Append("       , DIVIDE_PERMISSION_ID" + Environment.NewLine);
                    sb.Append("       , MEMO" + Environment.NewLine);
                    sb.Append("       , DISPLAY_FLG" + Environment.NewLine);
                    sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                    sb.Append("       , DELETE_FLG" + Environment.NewLine);
                    sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_DATE" + Environment.NewLine);
                    sb.Append("       , CREATE_TIME" + Environment.NewLine);
                    sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                    sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                    sb.Append(")" + Environment.NewLine);
                    sb.Append("SELECT  " + companyId + Environment.NewLine);                                                        // COMPANY_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(CustomerId)) + Environment.NewLine);       // CUSTOMER_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(_Id)) + Environment.NewLine);              // ID
                    sb.Append("       ," + ExCast.zIdForNumIndex(_Id) + Environment.NewLine);                                       // ID2
                    sb.Append("       ," + ExEscape.zRepStr(entity.name) + Environment.NewLine);                                    // NAME
                    sb.Append("       ," + ExEscape.zRepStr(entity.kana) + Environment.NewLine);                                    // KANA
                    sb.Append("       ," + ExEscape.zRepStr(entity.about_name) + Environment.NewLine);                              // ABOUT_NAME
                    sb.Append("       ," + ExCast.zNullToZero(entity.zip_code_from + entity.zip_code_to) + Environment.NewLine);    // ZIP_CODE
                    sb.Append("       ," + entity.prefecture_id + Environment.NewLine);                                             // PREFECTURE_ID
                    sb.Append("       ," + entity.city_id + Environment.NewLine);                                                   // CITY_ID
                    sb.Append("       ," + entity.town_id + Environment.NewLine);                                                   // TOWN_ID
                    sb.Append("       ," + ExEscape.zRepStr(entity.adress_city) + Environment.NewLine);                             // ADRESS_CITY
                    sb.Append("       ," + ExEscape.zRepStr(entity.adress_town) + Environment.NewLine);                             // ADRESS_TOWN
                    sb.Append("       ," + ExEscape.zRepStr(entity.adress1) + Environment.NewLine);                                 // ADRESS1
                    sb.Append("       ," + ExEscape.zRepStr(entity.adress2) + Environment.NewLine);                                 // ADRESS2
                    sb.Append("       ," + ExEscape.zRepStr(entity.station_name) + Environment.NewLine);                            // STATION_NAME
                    sb.Append("       ," + ExEscape.zRepStr(entity.post_name) + Environment.NewLine);                               // POST_NAME
                    sb.Append("       ," + ExEscape.zRepStr(entity.person_name) + Environment.NewLine);                             // PERSON_NAME
                    sb.Append("       ," + entity.title_id + Environment.NewLine);                                                  // TITLE_ID
                    sb.Append("       ," + ExEscape.zRepStr(entity.title_name) + Environment.NewLine);                              // TITLE_NAME
                    sb.Append("       ," + ExEscape.zRepStr(entity.tel) + Environment.NewLine);                                     // TEL
                    sb.Append("       ," + ExEscape.zRepStr(entity.fax) + Environment.NewLine);                                     // FAX
                    sb.Append("       ," + ExEscape.zRepStr(entity.mail_adress) + Environment.NewLine);                             // MAIL_ADRESS
                    sb.Append("       ," + ExEscape.zRepStr(entity.mobile_tel) + Environment.NewLine);                              // MOBILE_TEL
                    sb.Append("       ," + ExEscape.zRepStr(entity.mobile_adress) + Environment.NewLine);                           // MOBILE_ADRESS
                    sb.Append("       ," + ExEscape.zRepStr(entity.url) + Environment.NewLine);                                     // URL
                    sb.Append("       ," + entity.divide_permission_id + Environment.NewLine);                                      // DIVIDE_PERMISSION_ID
                    sb.Append("       ," + ExEscape.zRepStr(entity.memo) + Environment.NewLine);                                    // MEMO
                    sb.Append("       ," + entity.display_division_id + Environment.NewLine);                                       // DISPLAY_FLG
                    sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                PG_NM,
                                                                "M_SUPPLIER",
                                                                ExCast.zCInt(personId),
                                                                _Id,
                                                                ipAdress,
                                                                userId));

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSupplier(Insert)", ex);
                    return CLASS_NM + ".UpdateSupplier(Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

            }

            #endregion

            #region Update

            if (type == 0)
            {
                try
                {
                    #region SQL

                    sb.Length = 0;
                    sb.Append("UPDATE M_SUPPLIER " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                               ExCast.zCInt(personId),
                                                               ipAdress,
                                                               userId,
                                                               0));
                    sb.Append("      ,NAME = " + ExEscape.zRepStr(entity.name) + Environment.NewLine);
                    sb.Append("      ,KANA = " + ExEscape.zRepStr(entity.kana) + Environment.NewLine);
                    sb.Append("      ,ABOUT_NAME = " + ExEscape.zRepStr(entity.about_name) + Environment.NewLine);
                    sb.Append("      ,ZIP_CODE = " + ExCast.zNullToZero(entity.zip_code_from + entity.zip_code_to) + Environment.NewLine);
                    sb.Append("      ,PREFECTURE_ID = " + entity.prefecture_id + Environment.NewLine);
                    sb.Append("      ,CITY_ID = " + entity.city_id + Environment.NewLine);
                    sb.Append("      ,TOWN_ID = " + entity.town_id + Environment.NewLine);
                    sb.Append("      ,ADRESS_CITY = " + ExEscape.zRepStr(entity.adress_city) + Environment.NewLine);
                    sb.Append("      ,ADRESS_TOWN = " + ExEscape.zRepStr(entity.adress_town) + Environment.NewLine);
                    sb.Append("      ,ADRESS1 = " + ExEscape.zRepStr(entity.adress1) + Environment.NewLine);
                    sb.Append("      ,ADRESS2 = " + ExEscape.zRepStr(entity.adress2) + Environment.NewLine);
                    sb.Append("      ,STATION_NAME = " + ExEscape.zRepStr(entity.station_name) + Environment.NewLine);
                    sb.Append("      ,POST_NAME = " + ExEscape.zRepStr(entity.post_name) + Environment.NewLine);
                    sb.Append("      ,PERSON_NAME = " + ExEscape.zRepStr(entity.person_name) + Environment.NewLine);
                    sb.Append("      ,TITLE_ID = " + entity.title_id + Environment.NewLine);
                    sb.Append("      ,TITLE_NAME = " + ExEscape.zRepStr(entity.title_name) + Environment.NewLine);
                    sb.Append("      ,TEL = " + ExEscape.zRepStr(entity.tel) + Environment.NewLine);
                    sb.Append("      ,FAX = " + ExEscape.zRepStr(entity.fax) + Environment.NewLine);
                    sb.Append("      ,MAIL_ADRESS = " + ExEscape.zRepStr(entity.mail_adress) + Environment.NewLine);
                    sb.Append("      ,MOBILE_TEL = " + ExEscape.zRepStr(entity.mobile_tel) + Environment.NewLine);
                    sb.Append("      ,MOBILE_ADRESS = " + ExEscape.zRepStr(entity.mobile_adress) + Environment.NewLine);
                    sb.Append("      ,URL = " + ExEscape.zRepStr(entity.url) + Environment.NewLine);
                    sb.Append("      ,DIVIDE_PERMISSION_ID = " + entity.divide_permission_id + Environment.NewLine);
                    sb.Append("      ,MEMO = " + ExEscape.zRepStr(entity.memo) + Environment.NewLine);
                    sb.Append("      ,DISPLAY_FLG = " + entity.display_division_id + Environment.NewLine);
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);                            // COMPANY_ID
                    sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(Id)) + Environment.NewLine);      // ID
                    sb.Append("   AND CUSTOMER_ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(CustomerId)) + Environment.NewLine);

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSupplier(Update)", ex);
                    return CLASS_NM + ".UpdateSupplier(Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }

            #endregion

            #region Delete

            if (type == 2)
            {
                #region Exist Data

                try
                {
                    bool _ret = false;
                    _ret = DataExists.IsExistDataDouble(db, companyId, "", "T_ESTIMATE_H", "SUPPLIER_ID", ExCast.zNumZeroNothingFormat(Id), "CUSTOMER_ID", ExCast.zNumZeroNothingFormat(CustomerId), CommonUtl.geStrOrNumKbn.String);
                    if (_ret == true)
                    {
                        return "ID : " + Id + " は見積データの納入先に使用されている為、削除できません。";
                    }

                    _ret = DataExists.IsExistDataDouble(db, companyId, "", "T_ORDER_H", "SUPPLIER_ID", ExCast.zNumZeroNothingFormat(Id), "CUSTOMER_ID", ExCast.zNumZeroNothingFormat(CustomerId), CommonUtl.geStrOrNumKbn.String);
                    if (_ret == true)
                    {
                        return "ID : " + Id + " は受注データの納入先に使用されている為、削除できません。";
                    }

                    _ret = DataExists.IsExistDataDouble(db, companyId, "", "T_SALES_H", "SUPPLIER_ID", ExCast.zNumZeroNothingFormat(Id), "CUSTOMER_ID", ExCast.zNumZeroNothingFormat(CustomerId), CommonUtl.geStrOrNumKbn.String);
                    if (_ret == true)
                    {
                        return "ID : " + Id + " は売上データの納入先に使用されている為、削除できません。";
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSupplier(Exist Data)", ex);
                    return CLASS_NM + ".UpdateSupplier(Exist Data) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Update

                try
                {
                    sb.Length = 0;
                    sb.Append("UPDATE M_SUPPLIER " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                               ExCast.zCInt(personId),
                                                               ipAdress,
                                                               userId,
                                                               1));
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);    // COMPANY_ID
                    sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(Id)) + Environment.NewLine);            // ID
                    sb.Append("   AND CUSTOMER_ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(CustomerId)) + Environment.NewLine);

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSupplier(Delete)", ex);
                    return CLASS_NM + ".UpdateSupplier(Delete) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion
            }

            #endregion

            #region PG排他制御

            if (type == 0 || type == 2)
            {
                try
                {
                    DataPgLock.DelLockPg(companyId, userId, PG_NM, "", ipAdress, false, db);
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSupplier(DelLockPg)", ex);
                    return CLASS_NM + ".UpdateSupplier(DelLockPg) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSupplier(CommitTransaction)", ex);
                return CLASS_NM + ".UpdateSupplier(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSupplier(DbClose)", ex);
                return CLASS_NM + ".UpdateSupplier(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                switch (type)
                {
                    case 0:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Update,
                                                   "ID:" + Id.ToString());
                        break;
                    case 1:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Insert,
                                                   "ID:" + _Id.ToString());
                        break;
                    case 2:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Delete,
                                                   "ID:" + Id.ToString());
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSupplier(Add Evidence)", ex);
                return CLASS_NM + ".UpdateSupplier(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Return

            if (type == 1 && (Id == "0" || Id == ""))
            {
                return "Auto Insert success : " + "ID : " + _Id.ToString() + "で登録しました。";
            }
            else
            {
                return "";
            }

            #endregion
        }
        public string UpdateSalesCreditBalance(string random, int type, List<EntitySalesCreditBalance> entity)
        {

            #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 != "")
                {
                    return _message;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesCreditBalance(認証処理)", ex);
                return CLASS_NM + ".UpdateSalesCreditBalance : 認証処理に失敗しました。" + Environment.NewLine + ex.Message.ToString();
            }

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;
            string _Id = "";
            int _classKbn = 0;

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesCreditBalance(DbOpen)", ex);
                return CLASS_NM + ".UpdateSalesCreditBalance(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesCreditBalance(BeginTransaction)", ex);
                return CLASS_NM + ".UpdateSalesCreditBalance(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Update

            if (type == 0)
            {
                try
                {
                    for (int i = 0; i <= entity.Count - 1; i++)
                    {
                        if (entity[i].exec_flg == true)
                        {

                            double upd_balance = entity[i].before_sales_credit_balacne_upd - entity[i].before_sales_credit_balacne;

                            #region Update Sales Credit Balance

                            try
                            {
                                sb.Length = 0;
                                sb.Append("UPDATE M_SALES_CREDIT_BALANCE " + Environment.NewLine);

                                sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                                          ExCast.zCInt(personId),
                                                                          ipAdress,
                                                                          userId,
                                                                          0));

                                sb.Append("      ,SALES_CREDIT_INIT_PRICE = SALES_CREDIT_INIT_PRICE + " + upd_balance + Environment.NewLine);
                                sb.Append("      ,SALES_CREDIT_PRICE = SALES_CREDIT_PRICE + " + upd_balance + Environment.NewLine);

                                sb.Append(" WHERE DELETE_FLG = 0 " + Environment.NewLine);
                                sb.Append("   AND COMPANY_ID = " + companyId + Environment.NewLine);
                                sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                                sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entity[i].invoice_id)) + Environment.NewLine);

                                db.ExecuteSQL(sb.ToString(), false);


                            }
                            catch (Exception ex)
                            {
                                db.ExRollbackTransaction();
                                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesCreditBalance(Update Sales Credit Balance)", ex);
                                return CLASS_NM + ".UpdateSalesCreditBalance(Update Sales Credit Balance) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                            }

                            #endregion

                            #region Update Sales Credit Balance (Costumer)

                            try
                            {
                                sb.Length = 0;
                                sb.Append("UPDATE M_CUSTOMER " + Environment.NewLine);

                                sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                                          ExCast.zCInt(personId),
                                                                          ipAdress,
                                                                          userId,
                                                                          0));

                                sb.Append("      ,SALES_CREDIT_PRICE = SALES_CREDIT_PRICE + " + upd_balance + Environment.NewLine);

                                sb.Append(" WHERE DELETE_FLG = 0 " + Environment.NewLine);
                                sb.Append("   AND COMPANY_ID = " + companyId + Environment.NewLine);
                                sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entity[i].invoice_id)) + Environment.NewLine);

                                db.ExecuteSQL(sb.ToString(), false);


                            }
                            catch (Exception ex)
                            {
                                db.ExRollbackTransaction();
                                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesCreditBalance(Update Sales Credit Balance (Costumer))", ex);
                                return CLASS_NM + ".UpdateSalesCreditBalance(Update Sales Credit Balance (Costumer)) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                            }

                            #endregion

                        }
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesCreditBalance(Insert)", ex);
                    return CLASS_NM + ".UpdateSalesCreditBalance(Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

            }

            #endregion

            #region PG排他制御

            //try
            //{
            //    DataPgLock.DelLockPg(companyId, userId, PG_NM, "", ipAdress, false, db);
            //}
            //catch (Exception ex)
            //{
            //    db.ExRollbackTransaction();
            //    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesCreditBalance(DelLockPg)", ex);
            //    return CLASS_NM + ".UpdateSalesCreditBalance(DelLockPg) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            //}

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesCreditBalance(CommitTransaction)", ex);
                return CLASS_NM + ".UpdateSalesCreditBalance(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesCreditBalance(DbClose)", ex);
                return CLASS_NM + ".UpdateSalesCreditBalance(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                           companyId,
                                           userId,
                                           ipAdress,
                                           sessionString,
                                           PG_NM,
                                           DataPgEvidence.geOperationType.DeleteAndInsert,
                                           "");
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesCreditBalance(Add Evidence)", ex);
                return CLASS_NM + ".UpdateSalesCreditBalance(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            return "";

        }
        public string UpdateAuthority(string random, List<EntityAuthority> entity, int _user_id)
        {

            #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 != "")
                {
                    return _message;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(Authority_NM + ".UpdateAuthority(認証処理)", ex);
                return Authority_NM + ".UpdateAuthority : 認証処理に失敗しました。" + Environment.NewLine + ex.Message.ToString();
            }

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;
            string str_authority_kbn = "";

            #endregion

            #region 権限不可ユーザ0件チェック

            try
            {
                bool _flg = true;
                for (int i = 0; i <= entity.Count - 1; i++)
                {
                    if (entity[i].authority_kbn < 2 && entity[i].pg_id == "AuthorityMst")
                    {
                        _flg = false;
                    }
                }
                if (_flg == false)
                {
                    ExMySQLData dbSelect = ExSession.GetSessionDb(ExCast.zCInt(HttpContext.Current.Session[ExSession.USER_ID]),
                                                                  ExCast.zCStr(HttpContext.Current.Session[ExSession.SESSION_RANDOM_STR]));

                    #region SQL

                    sb.Append("SELECT MT.USER_ID " + Environment.NewLine);
                    sb.Append("  FROM M_AUTHORITY AS MT" + Environment.NewLine);

                    #region Join

                    sb.Append("  INNER JOIN SYS_M_USER AS UR" + Environment.NewLine);
                    sb.Append("    ON UR.DELETE_FLG = 0 " + Environment.NewLine);
                    sb.Append("   AND UR.DISPLAY_FLG = 1 " + Environment.NewLine);
                    sb.Append("   AND UR.COMPANY_ID = " + companyId + Environment.NewLine);
                    sb.Append("   AND UR.ID = MT.USER_ID " + Environment.NewLine);
                    sb.Append("   AND UR.ID <> " + _user_id + Environment.NewLine);

                    #endregion

                    sb.Append(" WHERE MT.DELETE_FLG = 0 " + Environment.NewLine);
                    sb.Append("   AND MT.PG_ID = 'AuthorityMst' " + Environment.NewLine);
                    sb.Append("   AND MT.AUTHORITY_KBN >= 2 " + Environment.NewLine);

                    #endregion

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

                    if (dt.DefaultView.Count == 0)
                    {
                        return "権限を付加できるユーザが0件になる為、更新できません。";
                    }
                    
                    dbSelect = null;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(Authority_NM + ".UpdateAuthority(Check)", ex);
                return Authority_NM + ".UpdateAuthority(Check) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(Authority_NM + ".UpdateAuthority(DbOpen)", ex);
                return Authority_NM + ".UpdateAuthority(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(Authority_NM + ".UpdateAuthority(BeginTransaction)", ex);
                return Authority_NM + ".UpdateAuthority(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Delete

            try
            {
                #region Delete SQL

                sb.Length = 0;
                sb.Append("DELETE FROM M_AUTHORITY " + Environment.NewLine);
                sb.Append(" WHERE USER_ID = " + _user_id + Environment.NewLine);

                #endregion

                db.ExecuteSQL(sb.ToString(), false);

            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(Authority_NM + ".UpdateAuthority(Delete)", ex);
                return Authority_NM + ".UpdateAuthority(Delete) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Insert

            try
            {
                for (int i = 0; i <= entity.Count - 1; i++)
                {
                    #region Insert SQL

                    sb.Length = 0;
                    sb.Append("INSERT INTO M_AUTHORITY " + Environment.NewLine);
                    sb.Append("       ( USER_ID" + Environment.NewLine);
                    sb.Append("       , PG_ID" + Environment.NewLine);
                    sb.Append("       , AUTHORITY_KBN" + Environment.NewLine);
                    sb.Append("       , MEMO" + Environment.NewLine);
                    sb.Append("       , DISPLAY_INDEX" + Environment.NewLine);
                    sb.Append("       , DISPLAY_FLG" + Environment.NewLine);
                    sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                    sb.Append("       , DELETE_FLG" + Environment.NewLine);
                    sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_DATE" + Environment.NewLine);
                    sb.Append("       , CREATE_TIME" + Environment.NewLine);
                    sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                    sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                    sb.Append(")" + Environment.NewLine);
                    sb.Append("SELECT  " + _user_id + Environment.NewLine);                                 // USER_ID
                    sb.Append("       ," + ExEscape.zRepStr(entity[i].pg_id) + Environment.NewLine);        // PG_ID
                    sb.Append("       ," + entity[i].authority_kbn + Environment.NewLine);                  // AUTHORITY_KBN
                    sb.Append("       ," + ExEscape.zRepStr(entity[i].memo) + Environment.NewLine);         // MEMO
                    sb.Append("       ," + entity[i].display_index + Environment.NewLine);                  // DISPLAY_INDEX
                    sb.Append("       ,1" + Environment.NewLine);                                           // DISPLAY_FLG
                    sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                PG_NM,
                                                                "M_AUTHORITY",
                                                                ExCast.zCInt(personId),
                                                                _user_id.ToString(),
                                                                ipAdress,
                                                                userId));

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                    if (entity[i].pg_id == "ReportTotal" && _user_id == ExCast.zCInt(userId))
                    {
                        str_authority_kbn = ExCast.zCStr(entity[i].authority_kbn);
                        HttpContext.Current.Session[ExSession.REPORT_TOTAL_AUTHORITY_KBN] = entity[i].authority_kbn;
                    }
                }
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(Authority_NM + ".UpdateAuthority(Insert)", ex);
                return Authority_NM + ".UpdateAuthority(Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region PG排他制御

            try
            {
                DataPgLock.DelLockPg(companyId, userId, PG_NM, "", ipAdress, false, db);
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(Authority_NM + ".UpdateAuthority(DelLockPg)", ex);
                return Authority_NM + ".UpdateAuthority(DelLockPg) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(Authority_NM + ".UpdateAuthority(CommitTransaction)", ex);
                return Authority_NM + ".UpdateAuthority(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(Authority_NM + ".UpdateAuthority(DbClose)", ex);
                return Authority_NM + ".UpdateAuthority(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                           companyId,
                                           userId,
                                           ipAdress,
                                           sessionString,
                                           PG_NM,
                                           DataPgEvidence.geOperationType.DeleteAndInsert,
                                           "");
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(Authority_NM + ".UpdateAuthority(Add Evidence)", ex);
                return Authority_NM + ".UpdateAuthority(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            if (str_authority_kbn != "")
            {
                HttpContext.Current.Session[ExSession.REPORT_TOTAL_AUTHORITY_KBN] = ExCast.zCInt(str_authority_kbn);
            }

            return "";

        }
        public string UpdateCustomer(string random, int type, string Id, EntityCustomer entity)
        {
            #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 != "")
                {
                    return _message;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(認証処理)", ex);
                return CLASS_NM + ".UpdateCustomer : 認証処理に失敗しました。" + Environment.NewLine + ex.Message.ToString();
            }

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;
            string _Id = "";

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(DbOpen)", ex);
                return CLASS_NM + ".UpdateCustomer(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(BeginTransaction)", ex);
                return CLASS_NM + ".UpdateCustomer(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Get Max Master ID

            if (type == 1 && (Id == "" || Id == "0"))
            {
                try
                {
                    DataMasterId.GetMaxMasterId(companyId,
                                                "",
                                                db,
                                                DataMasterId.geMasterMaxIdKbn.Customer,
                                                out _Id);

                    if (_Id == "")
                    {
                        return "ID取得に失敗しました。";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(GetMaxMasterId)", ex);
                    return CLASS_NM + ".UpdateCustomer(GetMaxMasterId) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }
            else
            {
                _Id = Id;
            }

            #endregion

            #region Insert

            if (type == 1)
            {
                try
                {
                    #region Delete SQL

                    sb.Length = 0;
                    sb.Append("DELETE FROM M_CUSTOMER " + Environment.NewLine);
                    sb.Append(" WHERE DELETE_FLG = 1 " + Environment.NewLine);
                    sb.Append("   AND COMPANY_ID = " + companyId + Environment.NewLine);
                    sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(_Id)) + Environment.NewLine);

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                    #region Insert SQL

                    string _invoice_id = entity.invoice_id;
                    if (ExCast.IsNumeric(_invoice_id)) _invoice_id = ExCast.zCDbl(_invoice_id).ToString();

                    sb.Length = 0;
                    sb.Append("INSERT INTO M_CUSTOMER " + Environment.NewLine);
                    sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                    sb.Append("       , ID" + Environment.NewLine);
                    sb.Append("       , ID2" + Environment.NewLine);
                    sb.Append("       , NAME" + Environment.NewLine);
                    sb.Append("       , KANA" + Environment.NewLine);
                    sb.Append("       , ABOUT_NAME" + Environment.NewLine);
                    sb.Append("       , ZIP_CODE" + Environment.NewLine);
                    sb.Append("       , PREFECTURE_ID" + Environment.NewLine);
                    sb.Append("       , CITY_ID" + Environment.NewLine);
                    sb.Append("       , TOWN_ID" + Environment.NewLine);
                    sb.Append("       , ADRESS_CITY" + Environment.NewLine);
                    sb.Append("       , ADRESS_TOWN" + Environment.NewLine);
                    sb.Append("       , ADRESS1" + Environment.NewLine);
                    sb.Append("       , ADRESS2" + Environment.NewLine);
                    sb.Append("       , STATION_NAME" + Environment.NewLine);
                    sb.Append("       , POST_NAME" + Environment.NewLine);
                    sb.Append("       , PERSON_NAME" + Environment.NewLine);
                    sb.Append("       , TITLE_ID" + Environment.NewLine);
                    sb.Append("       , TITLE_NAME" + Environment.NewLine);
                    sb.Append("       , TEL" + Environment.NewLine);
                    sb.Append("       , FAX" + Environment.NewLine);
                    sb.Append("       , MAIL_ADRESS" + Environment.NewLine);
                    sb.Append("       , MOBILE_TEL" + Environment.NewLine);
                    sb.Append("       , MOBILE_ADRESS" + Environment.NewLine);
                    sb.Append("       , URL" + Environment.NewLine);
                    sb.Append("       , INVOICE_ID" + Environment.NewLine);
                    sb.Append("       , BUSINESS_DIVISION_ID" + Environment.NewLine);
                    sb.Append("       , UNIT_KIND_ID" + Environment.NewLine);
                    sb.Append("       , CREDIT_RATE" + Environment.NewLine);
                    sb.Append("       , TAX_CHANGE_ID" + Environment.NewLine);
                    sb.Append("       , SUMMING_UP_GROUP_ID" + Environment.NewLine);
                    sb.Append("       , PRICE_FRACTION_PROC_ID" + Environment.NewLine);
                    sb.Append("       , TAX_FRACTION_PROC_ID" + Environment.NewLine);
                    sb.Append("       , CREDIT_LIMIT_PRICE" + Environment.NewLine);
                    sb.Append("       , SALES_CREDIT_PRICE" + Environment.NewLine);
                    sb.Append("       , RECEIPT_DIVISION_ID" + Environment.NewLine);
                    sb.Append("       , COLLECT_CYCLE_ID" + Environment.NewLine);
                    sb.Append("       , COLLECT_DAY" + Environment.NewLine);
                    sb.Append("       , BILL_SITE" + Environment.NewLine);
                    sb.Append("       , GROUP1_ID" + Environment.NewLine);
                    sb.Append("       , GROUP2_ID" + Environment.NewLine);
                    sb.Append("       , GROUP3_ID" + Environment.NewLine);
                    sb.Append("       , MEMO" + Environment.NewLine);
                    sb.Append("       , DISPLAY_FLG" + Environment.NewLine);
                    sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                    sb.Append("       , DELETE_FLG" + Environment.NewLine);
                    sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_DATE" + Environment.NewLine);
                    sb.Append("       , CREATE_TIME" + Environment.NewLine);
                    sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                    sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                    sb.Append(")" + Environment.NewLine);
                    sb.Append("SELECT  " + companyId + Environment.NewLine);                                                        // COMPANY_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(_Id)) + Environment.NewLine);              // ID
                    sb.Append("       ," + ExCast.zIdForNumIndex(_Id) + Environment.NewLine);                                       // ID2
                    sb.Append("       ," + ExEscape.zRepStr(entity.name) + Environment.NewLine);                                    // NAME
                    sb.Append("       ," + ExEscape.zRepStr(entity.kana) + Environment.NewLine);                                    // KANA
                    sb.Append("       ," + ExEscape.zRepStr(entity.about_name) + Environment.NewLine);                              // ABOUT_NAME
                    sb.Append("       ," + ExCast.zNullToZero(entity.zip_code_from + entity.zip_code_to) + Environment.NewLine);    // ZIP_CODE
                    sb.Append("       ," + entity.prefecture_id + Environment.NewLine);                                             // PREFECTURE_ID
                    sb.Append("       ," + entity.city_id + Environment.NewLine);                                                   // CITY_ID
                    sb.Append("       ," + entity.town_id + Environment.NewLine);                                                   // TOWN_ID
                    sb.Append("       ," + ExEscape.zRepStr(entity.adress_city) + Environment.NewLine);                             // ADRESS_CITY
                    sb.Append("       ," + ExEscape.zRepStr(entity.adress_town) + Environment.NewLine);                             // ADRESS_TOWN
                    sb.Append("       ," + ExEscape.zRepStr(entity.adress1) + Environment.NewLine);                                 // ADRESS1
                    sb.Append("       ," + ExEscape.zRepStr(entity.adress2) + Environment.NewLine);                                 // ADRESS2
                    sb.Append("       ," + ExEscape.zRepStr(entity.station_name) + Environment.NewLine);                            // STATION_NAME
                    sb.Append("       ," + ExEscape.zRepStr(entity.post_name) + Environment.NewLine);                               // POST_NAME
                    sb.Append("       ," + ExEscape.zRepStr(entity.person_name) + Environment.NewLine);                             // PERSON_NAME
                    sb.Append("       ," + entity.title_id + Environment.NewLine);                                                  // TITLE_ID
                    sb.Append("       ," + ExEscape.zRepStr(entity.title_name) + Environment.NewLine);                              // TITLE_NAME
                    sb.Append("       ," + ExEscape.zRepStr(entity.tel) + Environment.NewLine);                                     // TEL
                    sb.Append("       ," + ExEscape.zRepStr(entity.fax) + Environment.NewLine);                                     // FAX
                    sb.Append("       ," + ExEscape.zRepStr(entity.mail_adress) + Environment.NewLine);                             // MAIL_ADRESS
                    sb.Append("       ," + ExEscape.zRepStr(entity.mobile_tel) + Environment.NewLine);                              // MOBILE_TEL
                    sb.Append("       ," + ExEscape.zRepStr(entity.mobile_adress) + Environment.NewLine);                           // MOBILE_ADRESS
                    sb.Append("       ," + ExEscape.zRepStr(entity.url) + Environment.NewLine);                                     // URL
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(_invoice_id)) + Environment.NewLine);      // INVOICE_ID
                    sb.Append("       ," + entity.business_division_id + Environment.NewLine);                                      // BUSINESS_DIVISION_ID
                    sb.Append("       ," + entity.unit_kind_id + Environment.NewLine);                                              // UNIT_KIND_ID
                    sb.Append("       ," + entity.credit_rate + Environment.NewLine);                                               // CREDIT_RATE
                    sb.Append("       ," + entity.tax_change_id + Environment.NewLine);                                             // TAX_CHANGE_ID
                    sb.Append("       ," + ExEscape.zRepStr(entity.summing_up_group_id) + Environment.NewLine);                                // SUMMING_UP_GROUP_ID
                    sb.Append("       ," + entity.price_fraction_proc_id + Environment.NewLine);                                    // PRICE_FRACTION_PROC_ID
                    sb.Append("       ," + entity.tax_fraction_proc_id + Environment.NewLine);                                      // TAX_FRACTION_PROC_ID
                    sb.Append("       ," + entity.credit_limit_price + Environment.NewLine);                                        // CREDIT_LIMIT_PRICE
                    sb.Append("       ," + entity.sales_credit_price + Environment.NewLine);                                        // SALES_CREDIT_PRICE
                    sb.Append("       ," + ExEscape.zRepStr(entity.receipt_division_id) + Environment.NewLine);                                // RECEIPT_DIVISION_ID
                    sb.Append("       ," + entity.collect_cycle_id + Environment.NewLine);                                          // COLLECT_CYCLE_ID
                    sb.Append("       ," + entity.collect_day + Environment.NewLine);                                               // COLLECT_DAY
                    sb.Append("       ," + entity.bill_site + Environment.NewLine);                                                 // BILL_SITE
                    sb.Append("       ," + ExEscape.zRepStr(entity.group1_id) + Environment.NewLine);                                          // GROUP1_ID
                    sb.Append("       ," + ExEscape.zRepStr(entity.group2_id) + Environment.NewLine);                                          // GROUP2_ID
                    sb.Append("       ," + ExEscape.zRepStr(entity.group3_id) + Environment.NewLine);                                          // GROUP3_ID
                    sb.Append("       ," + ExEscape.zRepStr(entity.memo) + Environment.NewLine);                                               // MEMO
                    sb.Append("       ," + entity.display_division_id + Environment.NewLine);                                       // DISPLAY_FLG
                    sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                PG_NM,
                                                                "M_CUSTOMER",
                                                                ExCast.zCInt(personId),
                                                                _Id,
                                                                ipAdress,
                                                                userId));

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                    #region Sales Credit Balance Insert

                    try
                    {
                        if (ExCast.zNumZeroNothingFormat(_Id) == ExCast.zNumZeroNothingFormat(_invoice_id))
                        {
                            #region Delete SQL

                            sb.Length = 0;
                            sb.Append("DELETE FROM M_SALES_CREDIT_BALANCE " + Environment.NewLine);
                            sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);    // COMPANY_ID
                            sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(_invoice_id)) + Environment.NewLine);            // ID

                            #endregion

                            db.ExecuteSQL(sb.ToString(), false);
                            
                            #region SQL

                            sb.Length = 0;
                            sb.Append("SELECT MT.* " + Environment.NewLine);
                            sb.Append("  FROM SYS_M_COMPANY_GROUP AS MT" + Environment.NewLine);
                            sb.Append(" WHERE MT.COMPANY_ID = " + companyId + Environment.NewLine);
                            sb.Append("   AND MT.DELETE_FLG = 0 " + Environment.NewLine);
                            sb.Append(" ORDER BY MT.ID " + Environment.NewLine);

                            #endregion

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

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

                                    sb.Length = 0;
                                    sb.Append("INSERT INTO M_SALES_CREDIT_BALANCE " + Environment.NewLine);
                                    sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                                    sb.Append("       , GROUP_ID" + Environment.NewLine);
                                    sb.Append("       , ID" + Environment.NewLine);
                                    sb.Append("       , SALES_CREDIT_INIT_PRICE" + Environment.NewLine);
                                    sb.Append("       , SALES_CREDIT_PRICE" + Environment.NewLine);
                                    sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                                    sb.Append("       , DELETE_FLG" + Environment.NewLine);
                                    sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                                    sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                                    sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                                    sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                                    sb.Append("       , CREATE_DATE" + Environment.NewLine);
                                    sb.Append("       , CREATE_TIME" + Environment.NewLine);
                                    sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                                    sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                                    sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                                    sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                                    sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                                    sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                                    sb.Append(")" + Environment.NewLine);
                                    sb.Append("SELECT  " + companyId + Environment.NewLine);                                                        // COMPANY_ID
                                    sb.Append("       ," + ExCast.zCStr(dt.DefaultView[i]["ID"]) + Environment.NewLine);                            // GROUP_ID
                                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(_invoice_id)) + Environment.NewLine);      // ID
                                    sb.Append("       ,0" + Environment.NewLine);                                                                   // SALES_CREDIT_INIT_PRICE
                                    sb.Append("       ,0" + Environment.NewLine);                                                                   // SALES_CREDIT_PRICE
                                    sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                                PG_NM,
                                                                                "M_SALES_CREDIT_BALANCE",
                                                                                ExCast.zCInt(personId),
                                                                                _Id,
                                                                                ipAdress,
                                                                                userId));

                                    #endregion

                                    db.ExecuteSQL(sb.ToString(), false);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        db.ExRollbackTransaction();
                        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(Sales Credit Balance Insert)", ex);
                        return CLASS_NM + ".UpdateCustomer(Sales Credit Balance Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                    }

                    #endregion

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(Insert)", ex);
                    return CLASS_NM + ".UpdateCustomer(Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

            }

            #endregion

            #region Update

            if (type == 0)
            {
                try
                {
                    #region SQL

                    string _invoice_id = entity.invoice_id;
                    if (ExCast.IsNumeric(_invoice_id)) _invoice_id = ExCast.zCDbl(_invoice_id).ToString();

                    sb.Length = 0;
                    sb.Append("UPDATE M_CUSTOMER " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                               ExCast.zCInt(personId),
                                                               ipAdress,
                                                               userId,
                                                               0));
                    sb.Append("      ,NAME = " + ExEscape.zRepStr(entity.name) + Environment.NewLine);
                    sb.Append("      ,KANA = " + ExEscape.zRepStr(entity.kana) + Environment.NewLine);
                    sb.Append("      ,ABOUT_NAME = " + ExEscape.zRepStr(entity.about_name) + Environment.NewLine);
                    sb.Append("      ,ZIP_CODE = " + ExCast.zNullToZero(entity.zip_code_from + entity.zip_code_to) + Environment.NewLine);
                    sb.Append("      ,PREFECTURE_ID = " + entity.prefecture_id + Environment.NewLine);
                    sb.Append("      ,CITY_ID = " + entity.city_id + Environment.NewLine);
                    sb.Append("      ,TOWN_ID = " + entity.town_id + Environment.NewLine);
                    sb.Append("      ,ADRESS_CITY = " + ExEscape.zRepStr(entity.adress_city) + Environment.NewLine);
                    sb.Append("      ,ADRESS_TOWN = " + ExEscape.zRepStr(entity.adress_town) + Environment.NewLine);
                    sb.Append("      ,ADRESS1 = " + ExEscape.zRepStr(entity.adress1) + Environment.NewLine);
                    sb.Append("      ,ADRESS2 = " + ExEscape.zRepStr(entity.adress2) + Environment.NewLine);
                    sb.Append("      ,STATION_NAME = " + ExEscape.zRepStr(entity.station_name) + Environment.NewLine);
                    sb.Append("      ,POST_NAME = " + ExEscape.zRepStr(entity.post_name) + Environment.NewLine);
                    sb.Append("      ,PERSON_NAME = " + ExEscape.zRepStr(entity.person_name) + Environment.NewLine);
                    sb.Append("      ,TITLE_ID = " + entity.title_id + Environment.NewLine);
                    sb.Append("      ,TITLE_NAME = " + ExEscape.zRepStr(entity.title_name) + Environment.NewLine);
                    sb.Append("      ,TEL = " + ExEscape.zRepStr(entity.tel) + Environment.NewLine);
                    sb.Append("      ,FAX = " + ExEscape.zRepStr(entity.fax) + Environment.NewLine);
                    sb.Append("      ,MAIL_ADRESS = " + ExEscape.zRepStr(entity.mail_adress) + Environment.NewLine);
                    sb.Append("      ,MOBILE_TEL = " + ExEscape.zRepStr(entity.mobile_tel) + Environment.NewLine);
                    sb.Append("      ,MOBILE_ADRESS = " + ExEscape.zRepStr(entity.mobile_adress) + Environment.NewLine);
                    sb.Append("      ,URL = " + ExEscape.zRepStr(entity.url) + Environment.NewLine);
                    sb.Append("      ,INVOICE_ID = " + ExEscape.zRepStr(_invoice_id) + Environment.NewLine);
                    sb.Append("      ,BUSINESS_DIVISION_ID = " + entity.business_division_id + Environment.NewLine);
                    sb.Append("      ,UNIT_KIND_ID = " + entity.unit_kind_id + Environment.NewLine);
                    sb.Append("      ,CREDIT_RATE = " + entity.credit_rate + Environment.NewLine);
                    sb.Append("      ,TAX_CHANGE_ID = " + entity.tax_change_id + Environment.NewLine);
                    sb.Append("      ,SUMMING_UP_GROUP_ID = " + ExEscape.zRepStr(entity.summing_up_group_id) + Environment.NewLine);
                    sb.Append("      ,PRICE_FRACTION_PROC_ID = " + entity.price_fraction_proc_id + Environment.NewLine);
                    sb.Append("      ,TAX_FRACTION_PROC_ID = " + entity.tax_fraction_proc_id + Environment.NewLine);
                    sb.Append("      ,CREDIT_LIMIT_PRICE = " + entity.credit_limit_price + Environment.NewLine);
                    sb.Append("      ,SALES_CREDIT_PRICE = " + entity.sales_credit_price + Environment.NewLine);
                    sb.Append("      ,RECEIPT_DIVISION_ID = " + ExEscape.zRepStr(entity.receipt_division_id) + Environment.NewLine);
                    sb.Append("      ,COLLECT_CYCLE_ID = " + entity.collect_cycle_id + Environment.NewLine);
                    sb.Append("      ,COLLECT_DAY = " + entity.collect_day + Environment.NewLine);
                    sb.Append("      ,BILL_SITE = " + entity.bill_site + Environment.NewLine);
                    sb.Append("      ,GROUP1_ID = " + ExEscape.zRepStr(entity.group1_id) + Environment.NewLine);
                    sb.Append("      ,GROUP2_ID = " + ExEscape.zRepStr(entity.group2_id) + Environment.NewLine);
                    sb.Append("      ,GROUP3_ID = " + ExEscape.zRepStr(entity.group3_id) + Environment.NewLine);
                    sb.Append("      ,MEMO = " + ExEscape.zRepStr(entity.memo) + Environment.NewLine);
                    sb.Append("      ,DISPLAY_FLG = " + entity.display_division_id + Environment.NewLine);
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);                            // COMPANY_ID
                    sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(Id)) +  Environment.NewLine);     // ID

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                    #region Sales Credit Balance Insert

                    try
                    {
                        if (ExCast.zNumZeroNothingFormat(_Id) == ExCast.zNumZeroNothingFormat(_invoice_id))
                        {
                            #region SQL

                            sb.Length = 0;
                            sb.Append("SELECT MT.* " + Environment.NewLine);
                            sb.Append("  FROM SYS_M_COMPANY_GROUP AS MT" + Environment.NewLine);
                            sb.Append(" WHERE MT.COMPANY_ID = " + companyId + Environment.NewLine);
                            sb.Append("   AND MT.DELETE_FLG = 0 " + Environment.NewLine);
                            sb.Append(" ORDER BY MT.ID " + Environment.NewLine);

                            #endregion

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

                            if (dt.DefaultView.Count > 0)
                            {
                                for (int i = 0; i <= dt.DefaultView.Count - 1; i++)
                                {
                                    bool flg = DataExists.IsExistData(db, 
                                                                      companyId,
                                                                      ExCast.zCStr(dt.DefaultView[i]["ID"]), 
                                                                      "M_SALES_CREDIT_BALANCE", 
                                                                      "ID", 
                                                                      ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(_invoice_id)), 
                                                                      CommonUtl.geStrOrNumKbn.String);
                                    if (flg == false)
                                    {
                                        #region Insert SQL

                                        sb.Length = 0;
                                        sb.Append("INSERT INTO M_SALES_CREDIT_BALANCE " + Environment.NewLine);
                                        sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                                        sb.Append("       , GROUP_ID" + Environment.NewLine);
                                        sb.Append("       , ID" + Environment.NewLine);
                                        sb.Append("       , SALES_CREDIT_INIT_PRICE" + Environment.NewLine);
                                        sb.Append("       , SALES_CREDIT_PRICE" + Environment.NewLine);
                                        sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                                        sb.Append("       , DELETE_FLG" + Environment.NewLine);
                                        sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                                        sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                                        sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                                        sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                                        sb.Append("       , CREATE_DATE" + Environment.NewLine);
                                        sb.Append("       , CREATE_TIME" + Environment.NewLine);
                                        sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                                        sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                                        sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                                        sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                                        sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                                        sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                                        sb.Append(")" + Environment.NewLine);
                                        sb.Append("SELECT  " + companyId + Environment.NewLine);                                                        // COMPANY_ID
                                        sb.Append("       ," + ExCast.zCStr(dt.DefaultView[i]["ID"]) + Environment.NewLine);                            // GROUP_ID
                                        sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(_invoice_id)) + Environment.NewLine);      // ID
                                        sb.Append("       ,0" + Environment.NewLine);                                                                   // SALES_CREDIT_INIT_PRICE
                                        sb.Append("       ,0" + Environment.NewLine);                                                                   // SALES_CREDIT_PRICE
                                        sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                                    PG_NM,
                                                                                    "M_SALES_CREDIT_BALANCE",
                                                                                    ExCast.zCInt(personId),
                                                                                    _Id,
                                                                                    ipAdress,
                                                                                    userId));

                                        #endregion

                                        db.ExecuteSQL(sb.ToString(), false);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        db.ExRollbackTransaction();
                        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(Sales Credit Balance Insert)", ex);
                        return CLASS_NM + ".UpdateCustomer(Sales Credit Balance Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                    }

                    #endregion

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(Update)", ex);
                    return CLASS_NM + ".UpdateCustomer(Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }

            #endregion

            #region Delete

            if (type == 2)
            {
                #region Exist Data

                try
                {
                    bool _ret = false;

                    // 得意先チェック
                    _ret = DataExists.IsExistData(db, companyId, "", "T_ESTIMATE_H", "CUSTOMER_ID", ExCast.zNumZeroNothingFormat(Id), CommonUtl.geStrOrNumKbn.String);
                    if (_ret == true)
                    {
                        return "ID : " + Id + " は見積データの得意先に使用されている為、削除できません。";
                    }

                    _ret = DataExists.IsExistData(db, companyId, "", "T_ORDER_H", "CUSTOMER_ID", ExCast.zNumZeroNothingFormat(Id), CommonUtl.geStrOrNumKbn.String);
                    if (_ret == true)
                    {
                        return "ID : " + Id + " は受注データの得意先に使用されている為、削除できません。";
                    }

                    _ret = DataExists.IsExistData(db, companyId, "", "T_SALES_H", "CUSTOMER_ID", ExCast.zNumZeroNothingFormat(Id), CommonUtl.geStrOrNumKbn.String);
                    if (_ret == true)
                    {
                        return "ID : " + Id + " は売上データの得意先に使用されている為、削除できません。";
                    }

                    if (ExCast.zNumZeroNothingFormat(Id) != ExCast.zNumZeroNothingFormat(entity.invoice_id))
                    {
                        _ret = DataExists.IsExistData(db, companyId, "", "M_CUSTOMER", "INVOICE_ID", ExCast.zNumZeroNothingFormat(Id), CommonUtl.geStrOrNumKbn.String);
                        if (_ret == true)
                        {
                            return "ID : " + Id + " は得意先マスタの請求IDに使用されている為、削除できません。";
                        }
                    }

                    // 請求先チェック
                    if (ExCast.zNumZeroNothingFormat(Id) == ExCast.zNumZeroNothingFormat(entity.invoice_id))
                    {
                        _ret = DataExists.IsExistData(db, companyId, "", "T_SALES_H", "INVOICE_ID", ExCast.zNumZeroNothingFormat(Id), CommonUtl.geStrOrNumKbn.String);
                        if (_ret == true)
                        {
                            return "ID : " + Id + " は売上データの請求先に使用されている為、削除できません。";
                        }

                        _ret = DataExists.IsExistData(db, companyId, "", "T_INVOICE", "INVOICE_ID", ExCast.zNumZeroNothingFormat(Id), CommonUtl.geStrOrNumKbn.String);
                        if (_ret == true)
                        {
                            return "ID : " + Id + " は請求データの請求先に使用されている為、削除できません。";
                        }

                        _ret = DataExists.IsExistData(db, companyId, "", "T_RECEIPT_H", "INVOICE_ID", ExCast.zNumZeroNothingFormat(Id), CommonUtl.geStrOrNumKbn.String);
                        if (_ret == true)
                        {
                            return "ID : " + Id + " は入金データの請求先に使用されている為、削除できません。";
                        }
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(Exist Data)", ex);
                    return CLASS_NM + ".UpdateCustomer(Exist Data) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Delete Sales Credit Balance

                try
                {
                    if (ExCast.zNumZeroNothingFormat(Id) == ExCast.zNumZeroNothingFormat(entity.invoice_id))
                    {
                        sb.Length = 0;
                        sb.Append("UPDATE M_SALES_CREDIT_BALANCE " + Environment.NewLine);
                        sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                                   ExCast.zCInt(personId),
                                                                   ipAdress,
                                                                   userId,
                                                                   1));
                        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);    // COMPANY_ID
                        sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entity.invoice_id)) + Environment.NewLine);            // ID

                        db.ExecuteSQL(sb.ToString(), false);
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(Delete Sales Credit Balance)", ex);
                    return CLASS_NM + ".UpdateCustomer(Delete Sales Credit Balance) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Update

                try
                {
                    sb.Length = 0;
                    sb.Append("UPDATE M_CUSTOMER " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                               ExCast.zCInt(personId),
                                                               ipAdress,
                                                               userId,
                                                               1));
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);    // COMPANY_ID
                    sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(Id)) + Environment.NewLine);            // ID

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(Delete)", ex);
                    return CLASS_NM + ".UpdateCustomer(Delete) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

            }

            #endregion

            #region PG排他制御

            if (type == 0 || type == 2)
            {
                try
                {
                    DataPgLock.DelLockPg(companyId, userId, PG_NM, "", ipAdress, false, db);
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(DelLockPg)", ex);
                    return CLASS_NM + ".UpdateCustomer(DelLockPg) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(CommitTransaction)", ex);
                return CLASS_NM + ".UpdateCustomer(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(DbClose)", ex);
                return CLASS_NM + ".UpdateCustomer(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                switch (type)
                {
                    case 0:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Update,
                                                   "ID:" + Id.ToString());
                        break;
                    case 1:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Insert,
                                                   "ID:" + _Id.ToString());
                        break;
                    case 2:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Delete,
                                                   "ID:" + Id.ToString());
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCustomer(Add Evidence)", ex);
                return CLASS_NM + ".UpdateCustomer(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Return

            if (type == 1 && (Id == "0" || Id == ""))
            {
                return "Auto Insert success : " + "ID : " + _Id.ToString() + "で登録しました。";
            }
            else
            {
                return "";
            }

            #endregion
        }
        public static string DelLockPg(string companyId,
                                       string userId,
                                       string pgId,
                                       string lockId,
                                       string ipAdress,
                                       bool commitFlg,
                                       ExMySQLData _db)
        {
            StringBuilder sb;
            DataTable dt;
            ExMySQLData db = null;

            string ret = "err";

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".DelLockPg(DbOpen)", ex);
                return CLASS_NM + ".DelLockPg(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".DelLockPg", ex);
                return CLASS_NM + ".DelLockPg(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            try
            {
                sb = new StringBuilder();
                sb.Length = 0;

                sb.Append("DELETE FROM S_PG_LOCK " + Environment.NewLine);
                sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                sb.Append("   AND USER_ID = " + userId + Environment.NewLine);
                if (pgId != "")
                {
                    sb.Append("   AND PG_ID = " + ExEscape.zRepStr(pgId) + Environment.NewLine);
                }
                if (lockId != "")
                {
                    sb.Append("   AND LOCK_ID = " + ExEscape.zRepStr(lockId) + Environment.NewLine);
                }

                db.ExecuteSQL(sb.ToString(), false);

                ret = "";
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                ret = CLASS_NM + ".DelLockPg : " + ex.Message;
                CommonUtl.ExLogger.Error(CLASS_NM + ".DelLockPg", ex);
            }

            #region CommitTransaction

            try
            {
                if (commitFlg == true) db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".DelLockPg(CommitTransaction)", ex);
                return CLASS_NM + ".DelLockPg(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".DelLockPg(DbClose)", ex);
                return CLASS_NM + ".DelLockPg(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            return ret;
        }    
        public static bool IsExistData(ExMySQLData db,
                               string companyId,
                               string groupId,
                               string tblName,
                               string col1Name,
                               string col1Value,
                               string col2Name,
                               string col2Value,
                               CommonUtl.geStrOrNumKbn col1Type)
        {
            StringBuilder sb;
            DataTable dt;

            try
            {
                sb = new StringBuilder();

                #region SQL

                sb.Append("SELECT TBL.* " + Environment.NewLine);
                sb.Append("  FROM " + tblName + " AS TBL" + Environment.NewLine);

                sb.Append(" WHERE TBL.DELETE_FLG = 0 " + Environment.NewLine);
                if (companyId != "")
                {
                    sb.Append("   AND TBL.COMPANY_ID = " + companyId + Environment.NewLine);
                }
                if (groupId != "")
                {
                    sb.Append("   AND TBL.GROUP_ID = " + groupId + Environment.NewLine);
                }
                if (col1Type == CommonUtl.geStrOrNumKbn.String)
                {
                    sb.Append("   AND TBL." + col1Name + " = " + ExEscape.zRepStr(col1Value) + Environment.NewLine);
                    sb.Append("   AND TBL." + col2Name + " <> " + ExEscape.zRepStr(col2Value) + Environment.NewLine);
                }
                else
                {
                    sb.Append("   AND TBL." + col1Name + " = " + ExCast.zCDbl(col1Value) + Environment.NewLine);
                    sb.Append("   AND TBL." + col2Name + " <> " + ExCast.zCDbl(col2Value) + Environment.NewLine);
                }
                sb.Append(" LIMIT 0, 1" + Environment.NewLine);

                #endregion

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

                if (dt.DefaultView.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".IsExistData", ex);
                throw;
            }
            finally
            {
            }
        }
        public static string SetLockPg(string companyId,
                                     string userId,
                                     string pgId,
                                     string lockId,
                                     string ipAdress,
                                     ExMySQLData _db,
                                     out geLovkFlg lockFlg)
        {
            StringBuilder sb;
            DataTable dt;
            ExMySQLData db = null;

            string ret = "err";
            lockFlg = geLovkFlg.UnLock;

            if (CommonUtl.gDemoKbn == 1)
            {
                lockFlg = DataPgLock.geLovkFlg.UnLock;
                return "";
            }

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".SetLockPg(DbOpen)", ex);
                return CLASS_NM + ".SetLockPg(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".SetLockPg(BeginTransaction)", ex);
                return CLASS_NM + ".SetLockPg(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            try
            {
                sb = new StringBuilder();
                sb.Length = 0;

                ret = DataPgLock.DelLockPg(companyId, userId, pgId, "", ipAdress, false, db);
                if (ret != "") return ret;

                #region SQL

                // 存在確認
                sb.Append("SELECT TBL.* " + Environment.NewLine);
                sb.Append("  FROM S_PG_LOCK AS TBL" + Environment.NewLine);
                sb.Append(" WHERE TBL.COMPANY_ID = " + companyId + Environment.NewLine);
                sb.Append("   AND TBL.PG_ID = " + ExEscape.zRepStr(pgId) + Environment.NewLine);
                sb.Append("   AND TBL.LOCK_ID = " + ExEscape.zRepStr(lockId) + Environment.NewLine);
                sb.Append(" LIMIT 0, 1" + Environment.NewLine);
                sb.Append(" FOR UPDATE");

                #endregion

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

                if (dt.DefaultView.Count > 0)
                {
                    if (!string.IsNullOrEmpty(userId))
                    {
                        if (userId != ExCast.zCStr(dt.DefaultView[0]["USER_ID"]))
                        {
                            lockFlg = DataPgLock.geLovkFlg.Lock;
                        }
                        else
                        {
                            lockFlg = DataPgLock.geLovkFlg.UnLock;
                        }
                    }
                    else
                    {
                        lockFlg = DataPgLock.geLovkFlg.Lock;
                    }
                }
                else
                {
                    #region SQL

                    sb.Length = 0;
                    sb.Append("INSERT INTO S_PG_LOCK " + Environment.NewLine);
                    sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                    sb.Append("       , USER_ID" + Environment.NewLine);
                    sb.Append("       , PG_ID" + Environment.NewLine);
                    sb.Append("       , LOCK_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                    sb.Append("       , DELETE_FLG" + Environment.NewLine);
                    sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_DATE" + Environment.NewLine);
                    sb.Append("       , CREATE_TIME" + Environment.NewLine);
                    sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                    sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                    sb.Append(")" + Environment.NewLine);
                    sb.Append("VALUES (" + Environment.NewLine);
                    sb.Append("        " + companyId + Environment.NewLine);            // COMPANY_ID
                    sb.Append("       ," + userId + Environment.NewLine);               // USER_ID
                    sb.Append("       ," + ExEscape.zRepStr(pgId) + Environment.NewLine);          // PG_ID
                    sb.Append("       ," + ExEscape.zRepStr(lockId) + Environment.NewLine);        // LOCK_ID
                    sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins, "pgId", "S_PG_LOCK", 9999, "0", ipAdress, userId));
                    sb.Append(")" + Environment.NewLine);

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                    lockFlg = DataPgLock.geLovkFlg.UnLock;
                }

                ret = "";
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                ret = CLASS_NM + ".SetLockPg(Insert) : " + ex.Message;
                CommonUtl.ExLogger.Error(CLASS_NM + ".SetLockPg(Insert)", ex);
                throw;
            }

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".SetLockPg(CommitTransaction)", ex);
                return CLASS_NM + ".SetLockPg(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".SetLockPg(DbClose)", ex);
                return CLASS_NM + ".SetLockPg(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            return ret;
        }
        public void UpdatePaymentPrint(string random, int type, List<EntityPaymentClose> entity)
        {
            #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 != "")
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentPrint(認証処理)" + _message);
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentPrint(認証処理)", ex);
            }

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;
            string _Id = "";
            int _classKbn = 0;

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentPrint(DbOpen)", ex);
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentPrint(BeginTransaction)", ex);
            }

            #endregion

            #region Update (発行区分更新)

            try
            {
                for (int i = 0; i <= entity.Count - 1; i++)
                {
                    #region Update Payment

                    sb.Length = 0;
                    sb.Append("UPDATE T_PAYMENT " + Environment.NewLine);
                    sb.Append("   SET PAYMENT_PRINT_FLG = 1" + Environment.NewLine);
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                    sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                    sb.Append("   AND DELETE_FLG = 0 " + Environment.NewLine);
                    sb.Append("   AND NO = " + ExCast.zCLng(entity[i].no) + Environment.NewLine);

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);
                }
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentPrint(Update)", ex);
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentPrint(CommitTransaction)", ex);
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentPrint(DbClose)", ex);
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                           companyId,
                                           userId,
                                           ipAdress,
                                           sessionString,
                                           PG_NM,
                                           DataPgEvidence.geOperationType.DeleteAndInsert,
                                           "");
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentPrint(Add Evidence)", ex);
            }

            #endregion
        }
        public string UpdatePaymentCash(string random, int type, long PaymentCashNo, EntityPaymentCashH entityH, List<EntityPaymentCashD> entityD, EntityPaymentCashH before_entityH)
        {

            #region 認証処理

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

            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]);

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

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;

            string accountPeriod = "";
            long id = 0;
            long no = PaymentCashNo;

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(DbOpen)", ex);
                return "UpdatePaymentCash(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(BeginTransaction)", ex);
                return "UpdatePaymentCash(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Payment Close Check

            try
            {
                if (type <= 1)
                {
                    // 支払締切済チェック
                    if (DataClose.IsPaymentClose(companyId, db, entityH.purchase_id, entityH.payment_cash_ymd))
                    {
                        db.DbClose();
                        return "出金日 : " + entityH.payment_cash_ymd + " は支払締切済の為、計上できません。";
                    }
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(Payment Close Check)", ex);
                return "UpdatePaymentCash(Payment Close Check) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Update or Insert

            if (type <= 1)
            {
                #region Get Accout Period

                try
                {
                    accountPeriod = DataAccount.GetAccountPeriod(ExCast.zCStr(HttpContext.Current.Session[ExSession.ACCOUNT_BEGIN_PERIOD]), entityH.payment_cash_ymd);
                    if (accountPeriod == "")
                    {
                        return "会計年の取得に失敗しました。(期首月日 : " + ExCast.zCStr(HttpContext.Current.Session[ExSession.ACCOUNT_BEGIN_PERIOD]) +
                                                             " 出金日 : " + entityH.payment_cash_ymd + ")";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(GetAccountPeriod)", ex);
                    return "UpdatePaymentCash(GetAccountPeriod) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Get Slip No

                try
                {
                    DataSlipNo.GetSlipNo(companyId,
                                         groupId,
                                         db,
                                         DataSlipNo.geSlipKbn.PaymentCash,
                                         accountPeriod,
                                         ref no,
                                         out id,
                                         ExCast.zCStr(HttpContext.Current.Session[ExSession.IP_ADRESS]),
                                         ExCast.zCStr(HttpContext.Current.Session[ExSession.USER_ID]));
                    if (no == 0 || id == 0)
                    {
                        return "伝票番号の取得に失敗しました。(会計年 : " + accountPeriod + ")";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(GetSlipNo)", ex);
                    return "UpdatePaymentCash(GetSlipNo) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Head Insert

                try
                {
                    #region SQL

                    sb.Length = 0;
                    sb.Append("INSERT INTO T_PAYMENT_CASH_H " + Environment.NewLine);
                    sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                    sb.Append("       , GROUP_ID" + Environment.NewLine);
                    sb.Append("       , ID" + Environment.NewLine);
                    sb.Append("       , NO" + Environment.NewLine);
                    sb.Append("       , PURCHASE_ID" + Environment.NewLine);
                    sb.Append("       , PAYMENT_YYYYMMDD" + Environment.NewLine);
                    sb.Append("       , PERSON_ID" + Environment.NewLine);
                    sb.Append("       , PAYMENT_CASH_YMD" + Environment.NewLine);
                    sb.Append("       , SUM_PRICE" + Environment.NewLine);
                    sb.Append("       , PAYMENT_NO" + Environment.NewLine);
                    sb.Append("       , MEMO" + Environment.NewLine);
                    sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                    sb.Append("       , DELETE_FLG" + Environment.NewLine);
                    sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_DATE" + Environment.NewLine);
                    sb.Append("       , CREATE_TIME" + Environment.NewLine);
                    sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                    sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                    sb.Append(")" + Environment.NewLine);
                    sb.Append("SELECT  " + companyId + Environment.NewLine);                                                                // COMPANY_ID
                    sb.Append("       ," + groupId + Environment.NewLine);                                                                  // GROUP_ID
                    sb.Append("       ," + id.ToString() + Environment.NewLine);                                                            // ID
                    sb.Append("       ," + no.ToString() + Environment.NewLine);                                                            // NO
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityH.purchase_id)) + Environment.NewLine);      // PURCHASE_ID
                    sb.Append("       ," + ExEscape.zRepStr(entityH.payment_yyyymmdd) + Environment.NewLine);                               // PAYMENT_YYYYMMDD
                    sb.Append("       ," + entityH.person_id + Environment.NewLine);                                                        // PERSON_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zDateNullToDefault(entityH.payment_cash_ymd)) + Environment.NewLine);    // PAYMENT_CASH_YMD
                    sb.Append("       ," + entityH.sum_price + Environment.NewLine);                                                        // SUM_PRICE
                    sb.Append("       ," + entityH.payment_no + Environment.NewLine);                                                       // PAYMENT_NO
                    sb.Append("       ," + ExEscape.zRepStr(entityH.memo) + Environment.NewLine);                                           // MEMO
                    if (type == 0)
                    {
                        sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Upd,
                                                                  PG_NM,
                                                                  "T_PAYMENT_CASH_H",
                                                                  entityH.update_person_id,
                                                                  PaymentCashNo.ToString(),
                                                                  ipAdress,
                                                                  userId));
                    }
                    else
                    {
                        sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                  PG_NM,
                                                                  "T_PAYMENT_CASH_H",
                                                                  entityH.update_person_id,
                                                                  "0",
                                                                  ipAdress,
                                                                  userId));
                    }

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(Head Insert)", ex);
                    return "UpdatePaymentCash(Head Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Detail Insert

                try
                {
                    for (int i = 0; i <= entityD.Count - 1; i++)
                    {
                        if ((!string.IsNullOrEmpty(entityD[i].payment_cash_division_id)) || (!string.IsNullOrEmpty(entityD[i].payment_cash_division_nm)) ||
                             (entityD[i].price != 0) || (!string.IsNullOrEmpty(entityD[i].bill_site_day)) || (!string.IsNullOrEmpty(entityD[i].memo)))
                        {
                            #region SQL

                            sb.Length = 0;
                            sb.Append("INSERT INTO T_PAYMENT_CASH_D " + Environment.NewLine);
                            sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                            sb.Append("       , GROUP_ID" + Environment.NewLine);
                            sb.Append("       , PAYMENT_CASH_ID" + Environment.NewLine);
                            sb.Append("       , REC_NO" + Environment.NewLine);
                            sb.Append("       , PAYMENT_CASH_DIVISION_ID" + Environment.NewLine);
                            sb.Append("       , DESCRIPTION" + Environment.NewLine);
                            sb.Append("       , BILL_DUE_DATE" + Environment.NewLine);
                            sb.Append("       , PRICE" + Environment.NewLine);
                            sb.Append("       , MEMO" + Environment.NewLine);
                            sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                            sb.Append("       , DELETE_FLG" + Environment.NewLine);
                            sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                            sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                            sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                            sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                            sb.Append("       , CREATE_DATE" + Environment.NewLine);
                            sb.Append("       , CREATE_TIME" + Environment.NewLine);
                            sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                            sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                            sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                            sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                            sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                            sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                            sb.Append(")" + Environment.NewLine);
                            sb.Append("SELECT  " + companyId + Environment.NewLine);                                                                    // COMPANY_ID
                            sb.Append("       ," + groupId + Environment.NewLine);                                                                      // COMPANY_ID
                            sb.Append("       ," + id.ToString() + Environment.NewLine);                                                                // PAYMENT_CASH_ID
                            sb.Append("       ," + entityD[i].rec_no + Environment.NewLine);                                                            // REC_NO
                            sb.Append("       ," + ExEscape.zRepStr(entityD[i].payment_cash_division_id) + Environment.NewLine);                        // PAYMENT_CASH_DIVISION_ID
                            sb.Append("       ," + ExEscape.zRepStr(entityD[i].payment_cash_division_nm) + Environment.NewLine);                        // DESCRIPTION
                            sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityD[i].bill_site_day)) + Environment.NewLine);     // BILL_DUE_DATE
                            sb.Append("       ," + entityD[i].price + Environment.NewLine);                                                             // PRICE
                            sb.Append("       ," + ExEscape.zRepStr(entityD[i].memo) + Environment.NewLine);                                            // MEMO

                            if (type == 0)
                            {
                                sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Upd,
                                                                          PG_NM,
                                                                          "T_PAYMENT_CASH_D",
                                                                          entityH.update_person_id,
                                                                          ExCast.zCStr(entityH.id),
                                                                          ipAdress,
                                                                          userId));
                            }
                            else
                            {
                                sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                          PG_NM,
                                                                          "T_PAYMENT_CASH_D",
                                                                          entityH.update_person_id,
                                                                          "0",
                                                                          ipAdress,
                                                                          userId));
                            }

                            #endregion

                            db.ExecuteSQL(sb.ToString(), false);
                        }
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(Detail Insert)", ex);
                    return "UpdatePaymentCash(Detail Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Head Update

                if (type == 0)
                {
                    try
                    {
                        sb.Length = 0;
                        sb.Append("UPDATE T_PAYMENT_CASH_H " + Environment.NewLine);
                        sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                                  entityH.update_person_id,
                                                                  ipAdress,
                                                                  userId,
                                                                  1));
                        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);        // COMPANY_ID
                        sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);            // GROUP_ID
                        sb.Append("   AND ID = " + entityH.id.ToString() + Environment.NewLine);    // ID

                        db.ExecuteSQL(sb.ToString(), false);

                    }
                    catch (Exception ex)
                    {
                        db.ExRollbackTransaction();
                        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(Head Update)", ex);
                        return "UpdatePaymentCash(Head Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                    }
                }

                #endregion

                #region Detail Update

                if (type == 0)
                {
                    try
                    {
                        sb.Length = 0;
                        sb.Append("UPDATE T_PAYMENT_CASH_D " + Environment.NewLine);
                        sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                                  entityH.update_person_id,
                                                                  ipAdress,
                                                                  userId,
                                                                  1));
                        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                        sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                        sb.Append("   AND PAYMENT_CASH_ID = " + entityH.id.ToString() + Environment.NewLine);

                        db.ExecuteSQL(sb.ToString(), false);

                    }
                    catch (Exception ex)
                    {
                        db.ExRollbackTransaction();
                        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(Detail Update)", ex);
                        return "UpdatePaymentCash(Detail Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                    }
                }

                #endregion

                #region Update Slip No

                try
                {
                    if (type == 0)
                    {
                        DataSlipNo.UpdateSlipNo(companyId, groupId, db, DataSlipNo.geSlipKbn.PaymentCash, accountPeriod, 0, id);
                    }
                    else
                    {
                        DataSlipNo.UpdateSlipNo(companyId, groupId, db, DataSlipNo.geSlipKbn.PaymentCash, accountPeriod, no, id);
                    }

                    if (no == 0 || id == 0)
                    {
                        return "伝票番号の更新に失敗しました。(会計年 : " + accountPeriod + ")";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(UpdateSlipNo)", ex);
                    return "UpdatePaymentCash(UpdateSlipNo) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

            }

            #endregion

            #region Delete

            if (type == 2)
            {
                #region Head Delete

                try
                {
                    sb.Length = 0;
                    sb.Append("UPDATE T_PAYMENT_CASH_H " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                              entityH.update_person_id,
                                                              ipAdress,
                                                              userId,
                                                              1));
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);        // COMPANY_ID
                    sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);            // GROUP_ID
                    sb.Append("   AND ID = " + entityH.id.ToString() + Environment.NewLine);    // ID

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(Head Delete)", ex);
                    return "UpdatePaymentCash(Head Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Detail Delete

                try
                {
                    sb.Length = 0;
                    sb.Append("UPDATE T_PAYMENT_CASH_D " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                              entityH.update_person_id,
                                                              ipAdress,
                                                              userId,
                                                              1));
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                    sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                    sb.Append("   AND PAYMENT_CASH_ID = " + entityH.id.ToString() + Environment.NewLine);

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(Detail Delete)", ex);
                    return "UpdatePaymentCash(Detail Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion
            }

            #endregion

            #region Update Payment Credit

            try
            {
                double _price = 0;
                switch (type)
                {
                    case 0:         // Update
                        _price = before_entityH.sum_price;
                        DataCredit.UpdPaymentCredit(companyId, groupId, db, before_entityH.purchase_id, _price, PG_NM, ExCast.zCInt(entityH.update_person_id), ipAdress, userId);
                        _price = entityH.sum_price * -1;
                        DataCredit.UpdPaymentCredit(companyId, groupId, db, entityH.purchase_id, _price, PG_NM, ExCast.zCInt(entityH.update_person_id), ipAdress, userId);
                        break;
                    case 1:         // Insert
                        _price = entityH.sum_price * -1;
                        DataCredit.UpdPaymentCredit(companyId, groupId, db, entityH.purchase_id, _price, PG_NM, ExCast.zCInt(entityH.update_person_id), ipAdress, userId);
                        break;
                    case 2:         // Delete
                        _price = before_entityH.sum_price;
                        DataCredit.UpdPaymentCredit(companyId, groupId, db, before_entityH.purchase_id, _price, PG_NM, ExCast.zCInt(entityH.update_person_id), ipAdress, userId);
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(Update Payment Credit)", ex);
                return "UpdatePaymentCash(Update Payment Credit) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(CommitTransaction)", ex);
                return "UpdatePaymentCash(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(DbClose)", ex);
                return "UpdatePaymentCash(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                switch (type)
                {
                    case 0:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.PaymentCash.PaymentCashInp,
                                                   DataPgEvidence.geOperationType.Update,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    case 1:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.PaymentCash.PaymentCashInp,
                                                   DataPgEvidence.geOperationType.Insert,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    case 2:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.PaymentCash.PaymentCashInp,
                                                   DataPgEvidence.geOperationType.Delete,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePaymentCash(Add Evidence)", ex);
                return "UpdatePaymentCash(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Return

            if (type == 1 && PaymentCashNo == 0)
            {
                return "Auto Insert success : " + "出金番号 : " + no.ToString().ToString() + "で登録しました。";
            }
            else
            {
                return "";
            }

            #endregion

        }
        public string UpdateCondition(string random, List<EntityCondition> entity)
        {

            #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 != "")
                {
                    return _message;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCondition(認証処理)", ex);
                return CLASS_NM + ".UpdateCondition : 認証処理に失敗しました。" + Environment.NewLine + ex.Message.ToString();
            }

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;
            string _Id = "";

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCondition(DbOpen)", ex);
                return CLASS_NM + ".UpdateCondition(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCondition(BeginTransaction)", ex);
                return CLASS_NM + ".UpdateCondition(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Delete

            try
            {
                #region データ存在チェック

                sb = new StringBuilder();

                #region SQL

                sb.Length = 0;
                sb.Append("SELECT MT.* " + Environment.NewLine);
                sb.Append("  FROM M_CONDITION AS MT" + Environment.NewLine);
                sb.Append(" WHERE MT.COMPANY_ID = " + companyId + Environment.NewLine);
                sb.Append("   AND MT.CONDITION_DIVISION_ID = 1 " + Environment.NewLine);
                sb.Append("   AND MT.DELETE_FLG = 0 " + Environment.NewLine);

                #endregion

                db = ExSession.GetSessionDb(ExCast.zCInt(HttpContext.Current.Session[ExSession.USER_ID]),
                                            ExCast.zCStr(HttpContext.Current.Session[ExSession.SESSION_RANDOM_STR]));
                dt = db.GetDataTable(sb.ToString());

                string ret_msg = "";
                if (dt.DefaultView.Count > 0)
                {
                    for (int i = 0; i <= dt.DefaultView.Count - 1; i++)
                    {
                        long lngId = ExCast.zCLng(dt.DefaultView[i]["ID"]);
                        bool exists_flg = false;
                        for (int i_ = 0; i_ <= entity.Count - 1; i_++)
                        {
                            if (ExCast.zCLng(entity[i_].id) == lngId)
                            {
                                exists_flg = true;
                            }
                        }

                        string strId = string.Format("{0:00}", ExCast.zCDbl(lngId));

                        if (exists_flg == false)
                        {
                            bool _ret = false;

                            switch (strId)
                            { 
                                case "05":
                                    ret_msg += "締区分ID : " + strId + " は削除できません。" + Environment.NewLine;
                                    break;
                                case "10":
                                    ret_msg += "締区分ID : " + strId + " は削除できません。" + Environment.NewLine;
                                    break;
                                case "15":
                                    ret_msg += "締区分ID : " + strId + " は削除できません。" + Environment.NewLine;
                                    break;
                                case "25":
                                    ret_msg += "締区分ID : " + strId + " は削除できません。" + Environment.NewLine;
                                    break;
                                case "31":
                                    ret_msg += "締区分ID : " + strId + " は削除できません。" + Environment.NewLine;
                                    break;
                                default:
                                    _ret = DataExists.IsExistData(db, companyId, "", "M_CUSTOMER", "SUMMING_UP_GROUP_ID", strId, CommonUtl.geStrOrNumKbn.String);
                                    if (_ret == true)
                                    {
                                        ret_msg += "締区分ID : " + strId + " は得意先マスタで使用されている為、削除できません。" + Environment.NewLine;
                                    }
                                    break;
                            }
                        }
                    }

                    if (!string.IsNullOrEmpty(ret_msg))
                    {
                        return ret_msg;
                    }
                }


                #endregion

                #region Delete SQL

                sb.Length = 0;
                sb.Append("DELETE FROM M_CONDITION " + Environment.NewLine);
                sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);

                #endregion

                db.ExecuteSQL(sb.ToString(), false);
                
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCondition(Delete)", ex);
                return CLASS_NM + ".UpdateCondition(Delete) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Insert

            try
            {
                for (int i = 0; i <= entity.Count - 1; i++)
                {
                    // 空行は無視する
                    if (!string.IsNullOrEmpty(entity[i].id))
                    {
                        #region Insert SQL

                        sb.Length = 0;
                        sb.Append("INSERT INTO M_CONDITION " + Environment.NewLine);
                        sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                        sb.Append("       , CONDITION_DIVISION_ID" + Environment.NewLine);
                        sb.Append("       , ID" + Environment.NewLine);
                        sb.Append("       , NAME" + Environment.NewLine);
                        sb.Append("       , MEMO" + Environment.NewLine);
                        sb.Append("       , DISPLAY_FLG" + Environment.NewLine);
                        sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                        sb.Append("       , DELETE_FLG" + Environment.NewLine);
                        sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                        sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                        sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                        sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                        sb.Append("       , CREATE_DATE" + Environment.NewLine);
                        sb.Append("       , CREATE_TIME" + Environment.NewLine);
                        sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                        sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                        sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                        sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                        sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                        sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                        sb.Append(")" + Environment.NewLine);
                        sb.Append("SELECT  " + companyId + Environment.NewLine);                                                // COMPANY_ID
                        sb.Append("       ,1" + Environment.NewLine);                                                           // CONDITION_DIVISION_ID
                        sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroFormat("{0:00}", entity[i].id)) + Environment.NewLine);     // ID
                        sb.Append("       ," + ExEscape.zRepStr(entity[i].name) + Environment.NewLine);                                    // NAME
                        sb.Append("       ," + ExEscape.zRepStr(entity[i].memo) + Environment.NewLine);                                    // MEMO
                        sb.Append("       ," + entity[i].display_division_id + Environment.NewLine);                            // DISPLAY_FLG
                        sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                    PG_NM,
                                                                    "M_Condition",
                                                                    ExCast.zCInt(personId),
                                                                    _Id,
                                                                    ipAdress,
                                                                    userId));

                        #endregion

                        db.ExecuteSQL(sb.ToString(), false);
                    }                    
                }
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCondition(Insert)", ex);
                return CLASS_NM + ".UpdateCondition(Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCondition(CommitTransaction)", ex);
                return CLASS_NM + ".UpdateCondition(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCondition(DbClose)", ex);
                return CLASS_NM + ".UpdateCondition(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                           companyId,
                                           userId,
                                           ipAdress,
                                           sessionString,
                                           PG_NM,
                                           DataPgEvidence.geOperationType.DeleteAndInsert,
                                           "");
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCondition(Add Evidence)", ex);
                return CLASS_NM + ".UpdateCondition(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            return "";

        }
        public string UpdateStockInventory(string random, int type, string ymd, List<EntityStockInventory> entity)
        {

            #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 != "")
                {
                    return _message;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateStockInventory(認証処理)", ex);
                return CLASS_NM + ".UpdateStockInventory : 認証処理に失敗しました。" + Environment.NewLine + ex.Message.ToString();
            }

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;
            string _Id = "";
            int _classKbn = 0;

            long rec_cnt = 0;
            string str_message = "";

            EntityInOutDeliveryH _entityInOutDeliveryH = new EntityInOutDeliveryH();
            List<EntityInOutDeliveryD> _entityInOutDeliveryListD_Plus = new List<EntityInOutDeliveryD>();
            List<EntityInOutDeliveryD> _entityInOutDeliveryListD_Minus = new List<EntityInOutDeliveryD>();

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateStockInventory(DbOpen)", ex);
                return CLASS_NM + ".UpdateStockInventory(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateStockInventory(BeginTransaction)", ex);
                return CLASS_NM + ".UpdateStockInventory(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Update

            if (type == 0)
            {
                try
                {
                    for (int i = 0; i <= entity.Count - 1; i++)
                    {
                        if (entity[i].exec_flg == true && entity[i].diff_number != 0)
                        {
                            #region Update Commodity Inventory

                            try
                            {
                                DataCommodityInventory.UpdCommodityInventory(companyId,
                                                                             groupId,
                                                                             db,
                                                                             ExCast.zNumZeroNothingFormat(entity[i].commodity_id),
                                                                             entity[i].diff_number * -1,
                                                                             PG_NM,
                                                                             ExCast.zCInt(personId),
                                                                             ipAdress,
                                                                             userId);
                            }
                            catch (Exception ex)
                            {
                                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(Update Commodity Inventory)", ex);
                                return "UpdateSales(Update Commodity Inventory) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                            }

                            #endregion

                            #region Set Entity InOutDelivery

                            rec_cnt += 1;

                            EntityInOutDeliveryD _entityInOutDeliveryD = new EntityInOutDeliveryD();
                            _entityInOutDeliveryD.rec_no = rec_cnt;
                            _entityInOutDeliveryD.commodity_id = entity[i].commodity_id;
                            _entityInOutDeliveryD.commodity_name = entity[i].commodity_name;
                            _entityInOutDeliveryD.unit_id = 0;
                            _entityInOutDeliveryD.enter_number = 0;
                            _entityInOutDeliveryD.case_number = 0;

                            if (entity[i].diff_number > 0)
                            {
                                _entityInOutDeliveryD.number = entity[i].diff_number;
                                _entityInOutDeliveryListD_Minus.Add(_entityInOutDeliveryD);
                            }
                            else
                            {
                                _entityInOutDeliveryD.number = entity[i].diff_number * - 1;
                                _entityInOutDeliveryListD_Plus.Add(_entityInOutDeliveryD);
                            }

                            #endregion

                        }
                    }

                    #region Update InOutDelivery

                    try
                    {
                        
                        svcInOutDelivery _svcInOutDelivery = new svcInOutDelivery();

                        if (_entityInOutDeliveryListD_Minus.Count > 0)
                        {
                            _entityInOutDeliveryH.in_out_delivery_ymd = ymd;
                            _entityInOutDeliveryH.in_out_delivery_kbn = 2;          // 入出庫区分:出庫
                            _entityInOutDeliveryH.in_out_delivery_proc_kbn = 4;     // 入出庫処理区分:棚卸
                            _entityInOutDeliveryH.in_out_delivery_to_kbn = 1;       // 入出庫先区分:グループ
                            _entityInOutDeliveryH.update_person_id = ExCast.zCInt(personId);
                            _entityInOutDeliveryH.group_id_to = groupId;

                            for (int i = 0; i <= _entityInOutDeliveryListD_Minus.Count - 1; i++)
                            {
                                _entityInOutDeliveryH.sum_enter_number += _entityInOutDeliveryListD_Minus[i].enter_number;
                                _entityInOutDeliveryH.sum_case_number += _entityInOutDeliveryListD_Minus[i].case_number;
                                _entityInOutDeliveryH.sum_number += _entityInOutDeliveryListD_Minus[i].number;
                            }

                            // random
                            // update type     1:Insert
                            // procKbn         4:棚卸
                            // InOutDeliveryNo 入出庫番号
                            // CauseNo         元伝票番号
                            str_message = _svcInOutDelivery.UpdateInOutDeliveryExcExc(random,
                                                                                      1,
                                                                                      4,
                                                                                      0,
                                                                                      0,
                                                                                      _entityInOutDeliveryH,
                                                                                      _entityInOutDeliveryListD_Minus,
                                                                                      null,
                                                                                      null);
                            if (str_message.IndexOf("Auto Insert success : ") == -1 && !string.IsNullOrEmpty(str_message))
                            {
                                return "UpdateStockInventory(Update InOutDelivery) : " + str_message;
                            }
                        }

                        _svcInOutDelivery = new svcInOutDelivery();

                        _entityInOutDeliveryH = null;
                        _entityInOutDeliveryH = new EntityInOutDeliveryH();
                        if (_entityInOutDeliveryListD_Plus.Count > 0)
                        {
                            _entityInOutDeliveryH.in_out_delivery_ymd = ymd;
                            _entityInOutDeliveryH.in_out_delivery_kbn = 1;          // 入出庫区分:入庫
                            _entityInOutDeliveryH.in_out_delivery_proc_kbn = 4;     // 入出庫処理区分:棚卸
                            _entityInOutDeliveryH.in_out_delivery_to_kbn = 1;       // 入出庫先区分:グループ
                            _entityInOutDeliveryH.update_person_id = ExCast.zCInt(personId);
                            _entityInOutDeliveryH.group_id_to = groupId;

                            for (int i = 0; i <= _entityInOutDeliveryListD_Plus.Count - 1; i++)
                            {
                                _entityInOutDeliveryH.sum_enter_number += _entityInOutDeliveryListD_Plus[i].enter_number;
                                _entityInOutDeliveryH.sum_case_number += _entityInOutDeliveryListD_Plus[i].case_number;
                                _entityInOutDeliveryH.sum_number += _entityInOutDeliveryListD_Plus[i].number;
                            }

                            // random
                            // update type     1:Insert
                            // procKbn         4:棚卸
                            // InOutDeliveryNo 入出庫番号
                            // CauseNo         元伝票番号
                            str_message = _svcInOutDelivery.UpdateInOutDeliveryExcExc(random,
                                                                                      1,
                                                                                      4,
                                                                                      0,
                                                                                      0,
                                                                                      _entityInOutDeliveryH,
                                                                                      _entityInOutDeliveryListD_Plus,
                                                                                      null,
                                                                                      null);
                            if (str_message.IndexOf("Auto Insert success : ") == -1 && !string.IsNullOrEmpty(str_message))
                            {
                                return "UpdateStockInventory(Update InOutDelivery) : " + str_message;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        db.ExRollbackTransaction();
                        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateStockInventory(Update InOutDelivery)", ex);
                        return "UpdateStockInventory(Update InOutDelivery) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                    }

                    #endregion


                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateStockInventory(Insert)", ex);
                    return CLASS_NM + ".UpdateStockInventory(Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

            }

            #endregion

            #region PG排他制御

            //try
            //{
            //    DataPgLock.DelLockPg(companyId, userId, PG_NM, "", ipAdress, false, db);
            //}
            //catch (Exception ex)
            //{
            //    db.ExRollbackTransaction();
            //    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateStockInventory(DelLockPg)", ex);
            //    return CLASS_NM + ".UpdateStockInventory(DelLockPg) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            //}

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateStockInventory(CommitTransaction)", ex);
                return CLASS_NM + ".UpdateStockInventory(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateStockInventory(DbClose)", ex);
                return CLASS_NM + ".UpdateStockInventory(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                           companyId,
                                           userId,
                                           ipAdress,
                                           sessionString,
                                           PG_NM,
                                           DataPgEvidence.geOperationType.DeleteAndInsert,
                                           "");
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateStockInventory(Add Evidence)", ex);
                return CLASS_NM + ".UpdateStockInventory(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            return "";

        }
        public string UpdateCompany(string random, int type, EntityCompany entity)
        {
            #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 != "")
                {
                    return _message;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCompany(認証処理)", ex);
                return CLASS_NM + ".UpdateCompany : 認証処理に失敗しました。" + Environment.NewLine + ex.Message.ToString();
            }

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;
            string _Id = "";

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData();
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCompany(DbOpen)", ex);
                return CLASS_NM + ".UpdateCompany(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCompany(BeginTransaction)", ex);
                return CLASS_NM + ".UpdateCompany(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Update

            if (type == 0)
            {
                try
                {
                    #region Company SQL

                    sb.Length = 0;
                    sb.Append("UPDATE SYS_M_COMPANY " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                               ExCast.zCInt(personId),
                                                               ipAdress,
                                                               userId,
                                                               0));
                    sb.Append("      ,NAME = " + ExEscape.zRepStr(entity.name) + Environment.NewLine);
                    sb.Append("      ,KANA = " + ExEscape.zRepStr(entity.kana) + Environment.NewLine);
                    sb.Append("      ,ZIP_CODE = " + ExCast.zNullToZero(entity.zip_code_from + entity.zip_code_to) + Environment.NewLine);
                    sb.Append("      ,PREFECTURE_ID = " + entity.prefecture_id + Environment.NewLine);
                    sb.Append("      ,CITY_ID = " + entity.city_id + Environment.NewLine);
                    sb.Append("      ,TOWN_ID = " + entity.town_id + Environment.NewLine);
                    sb.Append("      ,ADRESS_CITY = " + ExEscape.zRepStr(entity.adress_city) + Environment.NewLine);
                    sb.Append("      ,ADRESS_TOWN = " + ExEscape.zRepStr(entity.adress_town) + Environment.NewLine);
                    sb.Append("      ,ADRESS1 = " + ExEscape.zRepStr(entity.adress1) + Environment.NewLine);
                    sb.Append("      ,ADRESS2 = " + ExEscape.zRepStr(entity.adress2) + Environment.NewLine);
                    sb.Append("      ,TEL = " + ExEscape.zRepStr(entity.tel) + Environment.NewLine);
                    sb.Append("      ,FAX = " + ExEscape.zRepStr(entity.fax) + Environment.NewLine);
                    sb.Append("      ,MAIL_ADRESS = " + ExEscape.zRepStr(entity.mail_adress) + Environment.NewLine);
                    sb.Append("      ,MOBILE_TEL = " + ExEscape.zRepStr(entity.mobile_tel) + Environment.NewLine);
                    sb.Append("      ,MOBILE_ADRESS = " + ExEscape.zRepStr(entity.mobile_adress) + Environment.NewLine);
                    sb.Append("      ,URL = " + ExEscape.zRepStr(entity.url) + Environment.NewLine);
                    sb.Append("      ,MEMO = " + ExEscape.zRepStr(entity.memo) + Environment.NewLine);
                    sb.Append(" WHERE ID = " + companyId + Environment.NewLine);                            // COMPANY_ID

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                    #region System Setting SQL

                    sb.Length = 0;
                    sb.Append("UPDATE SYS_M_SETTING " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                              ExCast.zCInt(personId),
                                                              ipAdress,
                                                              userId,
                                                              0));
                    sb.Append("      ,GROUP_DISPLAY_NAME = " + ExEscape.zRepStr(entity.group_display_name) + Environment.NewLine);
                    sb.Append("      ,ID_FIGURE_SLIP_NO = " + entity.id_figure_slip_no + Environment.NewLine);
                    sb.Append("      ,ID_FIGURE_CUSTOMER = " + entity.id_figure_customer + Environment.NewLine);
                    sb.Append("      ,ID_FIGURE_PURCHASE = " + entity.id_figure_purchase + Environment.NewLine);
                    sb.Append("      ,ID_FIGURE_GOODS = " + entity.id_figure_commodity + Environment.NewLine);
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);                            // COMPANY_ID

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCompany(Update)", ex);
                    return CLASS_NM + ".UpdateCompany(Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }

            #endregion

            #region Delete

            if (type == 2)
            {
                //#region Exist Data

                //try
                //{
                //    bool _ret = false;
                //    _ret = DataExists.IsExistData(db, companyId, "", "SYS_M_COMPANY", "INVOICE_ID", ExCast.zNumZeroNothingFormat(Id), "ID", ExCast.zNumZeroNothingFormat(Id), CommonUtl.geStrOrNumKbn.String);
                //    if (_ret == true)
                //    {
                //        return "得意先ID : " + Id + " は得意先マスタの請求IDにが使用されている為、削除できません。";
                //    }

                //    _ret = DataExists.IsExistData(db, companyId, "", "T_ORDER_H", "Company_ID", ExCast.zNumZeroNothingFormat(Id), CommonUtl.geStrOrNumKbn.String);
                //    if (_ret == true)
                //    {
                //        return "得意先ID : " + Id + " は受注データの得意先に使用されている為、削除できません。";
                //    }

                //}
                //catch (Exception ex)
                //{
                //    db.ExRollbackTransaction();
                //    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCompany(Exist Data)", ex);
                //    return CLASS_NM + ".UpdateCompany(Exist Data) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                //}

                //#endregion

                //#region Update

                //try
                //{
                //    sb.Length = 0;
                //    sb.Append("UPDATE SYS_M_COMPANY " + Environment.NewLine);
                //    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                //                                               ExCast.zCInt(personId),
                //                                               ipAdress,
                //                                               userId,
                //                                               1));
                //    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);    // COMPANY_ID
                //    sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(Id)) + Environment.NewLine);            // ID

                //    db.ExecuteSQL(sb.ToString(), false);

                //}
                //catch (Exception ex)
                //{
                //    db.ExRollbackTransaction();
                //    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCompany(Delete)", ex);
                //    return CLASS_NM + ".UpdateCompany(Delete) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                //}

                //#endregion
            }

            #endregion

            #region PG排他制御

            if (type == 0 || type == 2)
            {
                try
                {
                    DataPgLock.DelLockPg(companyId, userId, PG_NM, "", ipAdress, false, db);
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCompany(DelLockPg)", ex);
                    return CLASS_NM + ".UpdateCompany(DelLockPg) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCompany(CommitTransaction)", ex);
                return CLASS_NM + ".UpdateCompany(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCompany(DbClose)", ex);
                return CLASS_NM + ".UpdateCompany(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                switch (type)
                {
                    case 0:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Update,
                                                   "ID:" + companyId.ToString());
                        break;
                    case 1:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Insert,
                                                   "ID:" + companyId.ToString());
                        break;
                    case 2:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Delete,
                                                   "ID:" + companyId.ToString());
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateCompany(Add Evidence)", ex);
                return CLASS_NM + ".UpdateCompany(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Return

            return "";

            #endregion
        }
        public string UpdateInOutDeliveryExcExc(string random, 
                                                int type, 
                                                int procKbn,
                                                long InOutDeliveryNo,
                                                long CauseNo,
                                                EntityInOutDeliveryH entityH,
                                                List<EntityInOutDeliveryD> entityD,
                                                EntityInOutDeliveryH before_entityH,
                                                List<EntityInOutDeliveryD> before_entityD)
        {

            #region 認証処理

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

            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]);

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

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;

            string accountPeriod = "";
            long id = 0;
            long no = InOutDeliveryNo;

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(DbOpen)", ex);
                return "UpdateInOutDeliveryExc(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(BeginTransaction)", ex);
                return "UpdateInOutDeliveryExc(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Update or Insert

            if (type <= 1)
            {
                #region Get Accout Period

                try
                {
                    accountPeriod = DataAccount.GetAccountPeriod(ExCast.zCStr(HttpContext.Current.Session[ExSession.ACCOUNT_BEGIN_PERIOD]), entityH.in_out_delivery_ymd);
                    if (accountPeriod == "")
                    {
                        return "会計年の取得に失敗しました。(期首月日 : " + ExCast.zCStr(HttpContext.Current.Session[ExSession.ACCOUNT_BEGIN_PERIOD]) +
                                                             " 入出庫日 : " + entityH.in_out_delivery_ymd + ")";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(GetAccountPeriod)", ex);
                    return "UpdateInOutDeliveryExc(GetAccountPeriod) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Get Slip No

                try
                {
                    DataSlipNo.GetSlipNo(companyId,
                                         groupId,
                                         db,
                                         DataSlipNo.geSlipKbn.InOutDelivery,
                                         accountPeriod,
                                         ref no,
                                         out id,
                                         ExCast.zCStr(HttpContext.Current.Session[ExSession.IP_ADRESS]),
                                         ExCast.zCStr(HttpContext.Current.Session[ExSession.USER_ID]));
                    if (no == 0 || id == 0)
                    {
                        return "伝票番号の取得に失敗しました。(会計年 : " + accountPeriod + ")";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(GetSlipNo)", ex);
                    return "UpdateInOutDeliveryExc(GetSlipNo) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Head Insert

                try
                {
                    #region SQL

                    sb.Length = 0;
                    sb.Append("INSERT INTO T_IN_OUT_DELIVERY_H " + Environment.NewLine);
                    sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                    sb.Append("       , GROUP_ID" + Environment.NewLine);
                    sb.Append("       , ID" + Environment.NewLine);
                    sb.Append("       , NO" + Environment.NewLine);
                    sb.Append("       , CAUSE_NO" + Environment.NewLine);
                    sb.Append("       , IN_OUT_DELIVERY_YMD" + Environment.NewLine);
                    sb.Append("       , IN_OUT_DELIVERY_KBN" + Environment.NewLine);
                    sb.Append("       , IN_OUT_DELIVERY_PROC_KBN" + Environment.NewLine);
                    sb.Append("       , PERSON_ID" + Environment.NewLine);
                    sb.Append("       , IN_OUT_DELIVERY_TO_KBN" + Environment.NewLine);
                    sb.Append("       , GROUP_ID_TO" + Environment.NewLine);
                    sb.Append("       , CUSTOMER_ID" + Environment.NewLine);
                    sb.Append("       , PURCHASE_ID" + Environment.NewLine);
                    sb.Append("       , SUM_ENTER_NUMBER" + Environment.NewLine);
                    sb.Append("       , SUM_CASE_NUMBER" + Environment.NewLine);
                    sb.Append("       , SUM_NUMBER" + Environment.NewLine);
                    sb.Append("       , MEMO" + Environment.NewLine);
                    sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                    sb.Append("       , DELETE_FLG" + Environment.NewLine);
                    sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_DATE" + Environment.NewLine);
                    sb.Append("       , CREATE_TIME" + Environment.NewLine);
                    sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                    sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                    sb.Append(")" + Environment.NewLine);
                    sb.Append("SELECT  " + companyId + Environment.NewLine);                                                    // COMPANY_ID
                    sb.Append("       ," + groupId + Environment.NewLine);                                                      // GROUP_ID
                    sb.Append("       ," + id.ToString() + Environment.NewLine);                                                // ID
                    sb.Append("       ," + no.ToString() + Environment.NewLine);                                                // NO
                    sb.Append("       ," + CauseNo.ToString() + Environment.NewLine);                                           // CAUSE_NO
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zDateNullToDefault(entityH.in_out_delivery_ymd)) + Environment.NewLine); // IN_OUT_DELIVERY_YMD
                    sb.Append("       ," + entityH.in_out_delivery_kbn + Environment.NewLine);                                              // IN_OUT_DELIVERY_KBN
                    sb.Append("       ," + procKbn.ToString() + Environment.NewLine);                                                       // IN_OUT_DELIVERY_PROC_KBN
                    sb.Append("       ," + entityH.update_person_id + Environment.NewLine);                                                 // PERSON_ID
                    sb.Append("       ," + entityH.in_out_delivery_to_kbn + Environment.NewLine);                                           // IN_OUT_DELIVERY_TO_KBN
                    sb.Append("       ," + ExCast.zCInt(entityH.group_id_to) + Environment.NewLine);                                        // GROUP_ID_TO
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityH.customer_id)) + Environment.NewLine);      // CUSTOMER_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityH.purchase_id)) + Environment.NewLine);      // PURCHASE_ID
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_enter_number) + Environment.NewLine);                                   // SUM_ENTER_NUMBER
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_case_number) + Environment.NewLine);                                    // SUM_CASE_NUMBER
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_number) + Environment.NewLine);                                         // SUM_NUMBER
                    sb.Append("       ," + ExEscape.zRepStr(entityH.memo) + Environment.NewLine);                                           // MEMO
                    if (type == 0)
                    {
                        sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Upd,
                                                                  PG_NM,
                                                                  "T_IN_OUT_DELIVERY_H",
                                                                  entityH.update_person_id,
                                                                  InOutDeliveryNo.ToString(),
                                                                  ipAdress,
                                                                  userId));
                    }
                    else
                    {
                        sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                  PG_NM,
                                                                  "T_IN_OUT_DELIVERY_H",
                                                                  entityH.update_person_id,
                                                                  "0",
                                                                  ipAdress,
                                                                  userId));
                    }

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(Head Insert)", ex);
                    return "UpdateInOutDeliveryExc(Head Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Detail Insert

                try
                {
                    for (int i = 0; i <= entityD.Count - 1; i++)
                    {
                        if (!string.IsNullOrEmpty(entityD[i].commodity_id))
                        {
                            #region SQL

                            sb.Length = 0;
                            sb.Append("INSERT INTO T_IN_OUT_DELIVERY_D " + Environment.NewLine);
                            sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                            sb.Append("       , GROUP_ID" + Environment.NewLine);
                            sb.Append("       , IN_OUT_DELIVERY_ID" + Environment.NewLine);
                            sb.Append("       , REC_NO" + Environment.NewLine);
                            sb.Append("       , COMMODITY_ID" + Environment.NewLine);
                            sb.Append("       , COMMODITY_NAME" + Environment.NewLine);
                            sb.Append("       , UNIT_ID" + Environment.NewLine);
                            sb.Append("       , ENTER_NUMBER" + Environment.NewLine);
                            sb.Append("       , CASE_NUMBER" + Environment.NewLine);
                            sb.Append("       , NUMBER" + Environment.NewLine);
                            sb.Append("       , MEMO" + Environment.NewLine);
                            sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                            sb.Append("       , DELETE_FLG" + Environment.NewLine);
                            sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                            sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                            sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                            sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                            sb.Append("       , CREATE_DATE" + Environment.NewLine);
                            sb.Append("       , CREATE_TIME" + Environment.NewLine);
                            sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                            sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                            sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                            sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                            sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                            sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                            sb.Append(")" + Environment.NewLine);
                            sb.Append("SELECT  " + companyId + Environment.NewLine);                                                        // COMPANY_ID
                            sb.Append("       ," + groupId + Environment.NewLine);                                                          // GROUP_ID
                            sb.Append("       ," + id.ToString() + Environment.NewLine);                                                    // IN_OUT_DELIVERY_ID
                            sb.Append("       ," + entityD[i].rec_no + Environment.NewLine);                                                // REC_NO
                            sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityD[i].commodity_id)) + Environment.NewLine);     // COMMODITY_ID
                            sb.Append("       ," + ExEscape.zRepStr(entityD[i].commodity_name) + Environment.NewLine);                                 // COMMODITY_NAME
                            sb.Append("       ," + entityD[i].unit_id + Environment.NewLine);                                               // UNIT_ID
                            sb.Append("       ," + entityD[i].enter_number + Environment.NewLine);                                          // ENTER_NUMBER
                            sb.Append("       ," + entityD[i].case_number + Environment.NewLine);                                           // CASE_NUMBER
                            sb.Append("       ," + entityD[i].number + Environment.NewLine);                                                // NUMBER
                            sb.Append("       ," + ExEscape.zRepStr(entityD[i].memo) + Environment.NewLine);                                // MEMO

                            if (type == 0)
                            {
                                sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Upd,
                                                                          PG_NM,
                                                                          "T_IN_OUT_DELIVERY_D",
                                                                          entityH.update_person_id,
                                                                          ExCast.zCStr(entityH.id),
                                                                          ipAdress,
                                                                          userId));
                            }
                            else
                            {
                                sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                          PG_NM,
                                                                          "T_IN_OUT_DELIVERY_D",
                                                                          entityH.update_person_id,
                                                                          "0",
                                                                          ipAdress,
                                                                          userId));
                            }

                            #endregion

                            db.ExecuteSQL(sb.ToString(), false);

                            #region Update Commodity Inventory

                            // 入出庫時
                            if (procKbn == 1)
                            {
                                try
                                {
                                    double _number = entityD[i].number;
                                    if (entityH.in_out_delivery_kbn == 2)
                                    {
                                        // 出庫時
                                        _number = _number * -1;
                                    }
                                    if (entityD[i].inventory_management_division_id == 1)
                                    {
                                        DataCommodityInventory.UpdCommodityInventory(companyId,
                                                                                     groupId,
                                                                                     db,
                                                                                     ExCast.zNumZeroNothingFormat(entityD[i].commodity_id),
                                                                                     _number,
                                                                                     PG_NM,
                                                                                     ExCast.zCInt(entityH.update_person_id),
                                                                                     ipAdress,
                                                                                     userId);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(Update Commodity Inventory)", ex);
                                    return "UpdateInOutDeliveryExc(Update Commodity Inventory) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                                }
                            }

                            #endregion
                        }
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(Detail Insert)", ex);
                    return "UpdateInOutDeliveryExc(Detail Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Head Update

                if (type == 0)
                {
                    try
                    {
                        sb.Length = 0;
                        sb.Append("UPDATE T_IN_OUT_DELIVERY_H " + Environment.NewLine);
                        sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                                  entityH.update_person_id,
                                                                  ipAdress,
                                                                  userId,
                                                                  1));
                        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);        // COMPANY_ID
                        sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);            // GROUP_ID
                        sb.Append("   AND ID = " + entityH.id.ToString() + Environment.NewLine);    // ID

                        db.ExecuteSQL(sb.ToString(), false);

                    }
                    catch (Exception ex)
                    {
                        db.ExRollbackTransaction();
                        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(Head Update)", ex);
                        return "UpdateInOutDeliveryExc(Head Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                    }
                }

                #endregion

                #region Detail Update

                if (type == 0)
                {
                    try
                    {
                        sb.Length = 0;
                        sb.Append("UPDATE T_IN_OUT_DELIVERY_D " + Environment.NewLine);
                        sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                                  entityH.update_person_id,
                                                                  ipAdress,
                                                                  userId,
                                                                  1));
                        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                        sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                        sb.Append("   AND IN_OUT_DELIVERY_ID = " + entityH.id.ToString() + Environment.NewLine);

                        db.ExecuteSQL(sb.ToString(), false);

                    }
                    catch (Exception ex)
                    {
                        db.ExRollbackTransaction();
                        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(Detail Update)", ex);
                        return "UpdateInOutDeliveryExc(Detail Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                    }
                }

                #endregion

                #region Update Slip No

                try
                {
                    if (type == 0)
                    {
                        DataSlipNo.UpdateSlipNo(companyId, groupId, db, DataSlipNo.geSlipKbn.InOutDelivery, accountPeriod, 0, id);
                    }
                    else
                    {
                        DataSlipNo.UpdateSlipNo(companyId, groupId, db, DataSlipNo.geSlipKbn.InOutDelivery, accountPeriod, no, id);
                    }

                    if (no == 0 || id == 0)
                    {
                        return "伝票番号の更新に失敗しました。(会計年 : " + accountPeriod + ")";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(UpdateSlipNo)", ex);
                    return "UpdateInOutDeliveryExc(UpdateSlipNo) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

            }

            #endregion

            #region Delete

            if (type == 2)
            {
                #region Head Delete

                try
                {
                    sb.Length = 0;
                    sb.Append("UPDATE T_IN_OUT_DELIVERY_H " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                              entityH.update_person_id,
                                                              ipAdress,
                                                              userId,
                                                              1));
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);        // COMPANY_ID
                    sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);            // GROUP_ID
                    sb.Append("   AND ID = " + entityH.id.ToString() + Environment.NewLine);    // ID

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(Head Delete)", ex);
                    return "UpdateInOutDeliveryExc(Head Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Detail Delete

                try
                {
                    sb.Length = 0;
                    sb.Append("UPDATE T_IN_OUT_DELIVERY_D " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                              entityH.update_person_id,
                                                              ipAdress,
                                                              userId,
                                                              1));
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                    sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                    sb.Append("   AND IN_OUT_DELIVERY_ID = " + entityH.id.ToString() + Environment.NewLine);

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(Detail Delete)", ex);
                    return "UpdateInOutDeliveryExc(Detail Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion
            }

            #endregion

            #region Update Commodity Inventory

            // 入出庫時
            if (procKbn == 1)
            {
                try
                {
                    // Update or Delete
                    if (type == 0 || type == 2)
                    {

                        for (int i = 0; i <= before_entityD.Count - 1; i++)
                        {
                            double _number = before_entityD[i].number;
                            if (before_entityH.in_out_delivery_kbn == 1)
                            {
                                // 入庫時
                                _number = _number * -1;
                            }
                            if (before_entityD[i].inventory_management_division_id == 1)
                            {
                                DataCommodityInventory.UpdCommodityInventory(companyId,
                                                                             groupId,
                                                                             db,
                                                                             ExCast.zNumZeroNothingFormat(before_entityD[i].commodity_id),
                                                                             _number,
                                                                             PG_NM,
                                                                             ExCast.zCInt(entityH.update_person_id),
                                                                             ipAdress,
                                                                             userId);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(UpdateCommodityInventory)", ex);
                    return "UpdateInOutDeliveryExc(UpdateCommodityInventory) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(CommitTransaction)", ex);
                return "UpdateInOutDeliveryExc(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(DbClose)", ex);
                return "UpdateInOutDeliveryExc(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                switch (type)
                {
                    case 0:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.InOutDeliver.InOutDeliverInp,
                                                   DataPgEvidence.geOperationType.Update,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    case 1:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.InOutDeliver.InOutDeliverInp,
                                                   DataPgEvidence.geOperationType.Insert,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    case 2:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.InOutDeliver.InOutDeliverInp,
                                                   DataPgEvidence.geOperationType.Delete,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDeliveryExc(Add Evidence)", ex);
                return "UpdateInOutDeliveryExc(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Return

            if (type == 1 && InOutDeliveryNo == 0)
            {
                return "Auto Insert success : " + "入出庫番号 : " + no.ToString().ToString() + "で登録しました。";
            }
            else
            {
                return "";
            }

            #endregion

        }
        public string UpdatePurchaseOrder(string random, 
                                          int type, 
                                          long PurchaseOrderNo, 
                           EntityPurchaseOrderH entityH, 
                                         List<EntityPurchaseOrderD> entityD)
        {

            #region 認証処理

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

            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]); 

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

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;

            string accountPeriod = "";
            long id = 0;
            long no = PurchaseOrderNo;

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(DbOpen)", ex);
                return "UpdatePurchaseOrder(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Invoice Close Check

            try
            {
                if (type <= 1)
                {
                    // 掛仕入
                    if (entityH.business_division_id == 1)
                    {
                        //// 請求締切済チェック
                        //if (DataClose.IsInvoiceClose(companyId, db, entityH.invoice_id, entityH.sales_ymd))
                        //{
                        //    db.DbClose();
                        //    return "仕入日 : " + entityH.sales_ymd + " は請求締切済の為、計上できません。";
                        //}
                    }
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(Payment Close Check)", ex);
                return "UpdatePurchaseOrder(Payment Close Check) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(BeginTransaction)", ex);
                return "UpdatePurchaseOrder(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Update or Insert

            if (type <= 1)
            {

                #region Get Accout Period

                try
                {
                    accountPeriod = DataAccount.GetAccountPeriod(ExCast.zCStr(HttpContext.Current.Session[ExSession.ACCOUNT_BEGIN_PERIOD]), entityH.purchase_order_ymd);
                    if (accountPeriod == "")
                    {
                        return "会計年の取得に失敗しました。(期首月日 : " + ExCast.zCStr(HttpContext.Current.Session[ExSession.ACCOUNT_BEGIN_PERIOD]) +
                                                             " 仕入日 : " + entityH.purchase_order_ymd + ")";
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(GetAccountPeriod)", ex);
                    return "UpdatePurchaseOrder(GetAccountPeriod) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Get Slip No

                try
                {
                    DataSlipNo.GetSlipNo(companyId,
                                         groupId,
                                         db,
                                         DataSlipNo.geSlipKbn.Purchase,
                                         accountPeriod,
                                         ref no,
                                         out id,
                                         ExCast.zCStr(HttpContext.Current.Session[ExSession.IP_ADRESS]),
                                         ExCast.zCStr(HttpContext.Current.Session[ExSession.USER_ID]));
                    if (no == 0 || id == 0)
                    {
                        db.ExRollbackTransaction();
                        return "伝票番号の取得に失敗しました。(会計年 : " + accountPeriod + ")";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(GetSlipNo)", ex);
                    db.ExRollbackTransaction();
                    return "UpdatePurchaseOrder(GetSlipNo) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Head Insert

                try
                {
                    #region SQL

                    sb.Length = 0;
                    sb.Append("INSERT INTO T_PURCHASE_ORDER_H " + Environment.NewLine);
                    sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                    sb.Append("       , GROUP_ID" + Environment.NewLine);
                    sb.Append("       , ID" + Environment.NewLine);
                    sb.Append("       , NO" + Environment.NewLine);
                    sb.Append("       , PURCHASE_ORDER_YMD" + Environment.NewLine);
                    sb.Append("       , PERSON_ID" + Environment.NewLine);
                    sb.Append("       , PURCHASE_ID" + Environment.NewLine);
                    sb.Append("       , TAX_CHANGE_ID" + Environment.NewLine);
                    sb.Append("       , BUSINESS_DIVISION_ID" + Environment.NewLine);
                    sb.Append("       , SEND_ID" + Environment.NewLine);
                    sb.Append("       , CUSTOMER_ID" + Environment.NewLine);
                    sb.Append("       , SUPPLIER_ID" + Environment.NewLine);
                    sb.Append("       , SUPPLY_YMD" + Environment.NewLine);
                    sb.Append("       , DELIVER_DIVISION_ID" + Environment.NewLine);
                    sb.Append("       , SUM_ENTER_NUMBER" + Environment.NewLine);
                    sb.Append("       , SUM_CASE_NUMBER" + Environment.NewLine);
                    sb.Append("       , SUM_NUMBER" + Environment.NewLine);
                    sb.Append("       , SUM_UNIT_PRICE" + Environment.NewLine);
                    sb.Append("       , SUM_TAX" + Environment.NewLine);
                    sb.Append("       , SUM_NO_TAX_PRICE" + Environment.NewLine);
                    sb.Append("       , SUM_PRICE" + Environment.NewLine);
                    sb.Append("       , MEMO" + Environment.NewLine);
                    sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                    sb.Append("       , DELETE_FLG" + Environment.NewLine);
                    sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_DATE" + Environment.NewLine);
                    sb.Append("       , CREATE_TIME" + Environment.NewLine);
                    sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                    sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                    sb.Append(")" + Environment.NewLine);
                    sb.Append("SELECT  " + companyId + Environment.NewLine);                                                                // COMPANY_ID
                    sb.Append("       ," + groupId + Environment.NewLine);                                                                  // GROUP_ID
                    sb.Append("       ," + id.ToString() + Environment.NewLine);                                                            // ID
                    sb.Append("       ," + no.ToString() + Environment.NewLine);                                                            // NO
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zDateNullToDefault(entityH.purchase_order_ymd)) + Environment.NewLine);  // PURCHASE_ORDER_YMD
                    sb.Append("       ," + entityH.update_person_id + Environment.NewLine);                                                 // PERSON_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityH.purchase_id)) + Environment.NewLine);      // PURCHASE_ID
                    sb.Append("       ," + entityH.tax_change_id + Environment.NewLine);                                                    // TAX_CHANGE_ID
                    sb.Append("       ," + entityH.business_division_id + Environment.NewLine);                                             // BUSINESS_DIVISION_ID
                    sb.Append("       ," + entityH.send_kbn_id + Environment.NewLine);                                                      // SEND_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityH.customer_id)) + Environment.NewLine);      // CUSTOMER_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityH.supplier_id)) + Environment.NewLine);      // SUPPLIER_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zTimeNullToDefault(entityH.supply_ymd)) + Environment.NewLine);          // SUPPLY_YMD
                    sb.Append("       ,1" + Environment.NewLine);                                                                           // DELIVER_DIVISION_ID
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_enter_number) + Environment.NewLine);                                   // SUM_ENTER_NUMBER
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_case_number) + Environment.NewLine);                                    // SUM_CASE_NUMBER
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_number) + Environment.NewLine);                                         // SUM_NUMBER
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_unit_price) + Environment.NewLine);                                     // SUM_UNIT_PRICE
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_tax) + Environment.NewLine);                                            // SUM_TAX
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_no_tax_price) + Environment.NewLine);                                   // SUM_NO_TAX_PRICE
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_price) + Environment.NewLine);                                          // SUM_PRICE
                    sb.Append("       ," + ExEscape.zRepStr(entityH.memo) + Environment.NewLine);                                           // MEMO
                    if (type == 0)
                    {
                        sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Upd,
                                                                  PG_NM,
                                                                  "T_PURCHASE_ORDER_H",
                                                                  entityH.update_person_id,
                                                                  PurchaseOrderNo.ToString(),
                                                                  ipAdress,
                                                                  userId));
                    }
                    else
                    {
                        sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                  PG_NM,
                                                                  "T_PURCHASE_ORDER_H",
                                                                  entityH.update_person_id,
                                                                  "0",
                                                                  ipAdress,
                                                                  userId));
                    }

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(Head Insert)", ex);
                    return "UpdatePurchaseOrder(Head Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Detail Insert

                try
                {
                    for (int i = 0; i <= entityD.Count - 1; i++)
                    {
                        #region SQL

                        sb.Length = 0;
                        sb.Append("INSERT INTO T_PURCHASE_ORDER_D " + Environment.NewLine);
                        sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                        sb.Append("       , GROUP_ID" + Environment.NewLine);
                        sb.Append("       , PURCHASE_ORDER_ID" + Environment.NewLine);
                        sb.Append("       , REC_NO" + Environment.NewLine);
                        sb.Append("       , BREAKDOWN_ID" + Environment.NewLine);
                        sb.Append("       , DELIVER_DIVISION_ID" + Environment.NewLine);
                        sb.Append("       , COMMODITY_ID" + Environment.NewLine);
                        sb.Append("       , COMMODITY_NAME" + Environment.NewLine);
                        sb.Append("       , UNIT_ID" + Environment.NewLine);
                        sb.Append("       , ENTER_NUMBER" + Environment.NewLine);
                        sb.Append("       , CASE_NUMBER" + Environment.NewLine);
                        sb.Append("       , NUMBER" + Environment.NewLine);
                        sb.Append("       , UNIT_PRICE" + Environment.NewLine);
                        sb.Append("       , TAX" + Environment.NewLine);
                        sb.Append("       , NO_TAX_PRICE" + Environment.NewLine);
                        sb.Append("       , PRICE" + Environment.NewLine);
                        sb.Append("       , TAX_DIVISION_ID" + Environment.NewLine);
                        sb.Append("       , TAX_PERCENT" + Environment.NewLine);
                        sb.Append("       , MEMO" + Environment.NewLine);
                        sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                        sb.Append("       , DELETE_FLG" + Environment.NewLine);
                        sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                        sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                        sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                        sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                        sb.Append("       , CREATE_DATE" + Environment.NewLine);
                        sb.Append("       , CREATE_TIME" + Environment.NewLine);
                        sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                        sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                        sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                        sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                        sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                        sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                        sb.Append(")" + Environment.NewLine);
                        sb.Append("SELECT  " + companyId + Environment.NewLine);                                                                    // COMPANY_ID
                        sb.Append("       ," + groupId + Environment.NewLine);                                                                      // GROUP_ID
                        sb.Append("       ," + id.ToString() + Environment.NewLine);                                                                // PURCHASE_ORDER_ID
                        sb.Append("       ," + entityD[i].rec_no + Environment.NewLine);                                                            // REC_NO
                        sb.Append("       ," + entityD[i].breakdown_id + Environment.NewLine);                                                      // BREAKDOWN_ID
                        sb.Append("       ," + entityD[i].deliver_division_id + Environment.NewLine);                                               // DELIVER_DIVISION_ID
                        sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityD[i].commodity_id)) + Environment.NewLine);      // COMMODITY_ID
                        sb.Append("       ," + ExEscape.zRepStr(entityD[i].commodity_name) + Environment.NewLine);                                  // COMMODITY_NAME
                        sb.Append("       ," + entityD[i].unit_id + Environment.NewLine);                                                           // UNIT_ID
                        sb.Append("       ," + entityD[i].enter_number + Environment.NewLine);                                                      // ENTER_NUMBER
                        sb.Append("       ," + entityD[i].case_number + Environment.NewLine);                                                       // CASE_NUMBER
                        sb.Append("       ," + entityD[i].number + Environment.NewLine);                                                            // NUMBER
                        sb.Append("       ," + entityD[i].unit_price + Environment.NewLine);                                                        // UNIT_PRICE
                        sb.Append("       ," + entityD[i].tax + Environment.NewLine);                                                               // TAX
                        sb.Append("       ," + entityD[i].no_tax_price + Environment.NewLine);                                                      // NO_TAX_PRICE
                        sb.Append("       ," + entityD[i].price + Environment.NewLine);                                                             // PRICE
                        sb.Append("       ," + entityD[i].tax_division_id + Environment.NewLine);                                                   // TAX_DIVISION_ID
                        sb.Append("       ," + entityD[i].tax_percent + Environment.NewLine);                                                       // TAX_PERCENT
                        sb.Append("       ," + ExEscape.zRepStr(entityD[i].memo) + Environment.NewLine);                                            // MEMO

                        if (type == 0)
                        {
                            sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Upd,
                                                                      PG_NM,
                                                                      "T_PURCHASE_ORDER_D",
                                                                      entityH.update_person_id,
                                                                      ExCast.zCStr(entityH.id),
                                                                      ipAdress,
                                                                      userId));
                        }
                        else
                        {
                            sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                      PG_NM,
                                                                      "T_PURCHASE_ORDER_D",
                                                                      entityH.update_person_id,
                                                                      "0",
                                                                      ipAdress,
                                                                      userId));
                        }
                        
                        #endregion

                        if (entityD[i].breakdown_id <= 3 && string.IsNullOrEmpty(entityD[i].commodity_id))
                        {
                            // 摘要以外で商品未選択の場合、登録しない
                        }
                        else
                        {
                            db.ExecuteSQL(sb.ToString(), false);
                        }
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(Detail Insert)", ex);
                    return "UpdatePurchaseOrder(Detail Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Head Update

                if (type == 0)
                {
                    try
                    {
                        sb.Length = 0;
                        sb.Append("UPDATE T_PURCHASE_ORDER_H " + Environment.NewLine);
                        sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                                  entityH.update_person_id,
                                                                  ipAdress,
                                                                  userId,
                                                                  1));
                        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);        // COMPANY_ID
                        sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);            // GROUP_ID
                        sb.Append("   AND ID = " + entityH.id.ToString() + Environment.NewLine);    // ID

                        db.ExecuteSQL(sb.ToString(), false);

                    }
                    catch (Exception ex)
                    {
                        db.ExRollbackTransaction();
                        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(Head Update)", ex);
                        return "UpdatePurchaseOrder(Head Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                    }
                }

                #endregion

                #region Detail Update

                if (type == 0)
                {
                    try
                    {
                        sb.Length = 0;
                        sb.Append("UPDATE T_PURCHASE_ORDER_D " + Environment.NewLine);
                        sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                                  entityH.update_person_id,
                                                                  ipAdress,
                                                                  userId,
                                                                  1));
                        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                        sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                        sb.Append("   AND PURCHASE_ORDER_ID = " + entityH.id.ToString() + Environment.NewLine);

                        db.ExecuteSQL(sb.ToString(), false);

                    }
                    catch (Exception ex)
                    {
                        db.ExRollbackTransaction();
                        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(Detail Update)", ex);
                        return "UpdatePurchaseOrder(Detail Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                    }
                }

                #endregion

                #region Update Slip No

                try
                {
                    if (type == 0)
                    {
                        DataSlipNo.UpdateSlipNo(companyId, groupId, db, DataSlipNo.geSlipKbn.Purchase, accountPeriod, 0, id);
                    }
                    else
                    {
                        DataSlipNo.UpdateSlipNo(companyId, groupId, db, DataSlipNo.geSlipKbn.Purchase, accountPeriod, no, id);
                    }

                    if (no == 0 || id == 0)
                    {
                        return "伝票番号の更新に失敗しました。(会計年 : " + accountPeriod + ")";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(UpdateSlipNo)", ex);
                    return "UpdatePurchaseOrder(UpdateSlipNo) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

            }

            #endregion

            #region Delete

            if (type == 2)
            {
                #region Head Delete

                try
                {
                    sb.Length = 0;
                    sb.Append("UPDATE T_PURCHASE_ORDER_H " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                              entityH.update_person_id,
                                                              ipAdress,
                                                              userId,
                                                              1));
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);        // COMPANY_ID
                    sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);            // GROUP_ID
                    sb.Append("   AND ID = " + entityH.id.ToString() + Environment.NewLine);    // ID

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(Head Delete)", ex);
                    return "UpdatePurchaseOrder(Head Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Detail Delete

                try
                {
                    sb.Length = 0;
                    sb.Append("UPDATE T_PURCHASE_ORDER_D " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                              entityH.update_person_id,
                                                              ipAdress,
                                                              userId,
                                                              1));
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                    sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                    sb.Append("   AND PURCHASE_ORDER_ID = " + entityH.id.ToString() + Environment.NewLine);

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(Detail Delete)", ex);
                    return "UpdatePurchaseOrder(Detail Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(CommitTransaction)", ex);
                return "UpdatePurchaseOrder(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(DbClose)", ex);
                return "UpdatePurchaseOrder(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                switch (type)
                {
                    case 0:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.PurchaseOrder.PurchaseOrderInp,
                                                   DataPgEvidence.geOperationType.Update,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    case 1:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.PurchaseOrder.PurchaseOrderInp,
                                                   DataPgEvidence.geOperationType.Insert,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    case 2:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.PurchaseOrder.PurchaseOrderInp,
                                                   DataPgEvidence.geOperationType.Delete,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchaseOrder(Add Evidence)", ex);
                return "UpdatePurchaseOrder(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Return

            if (type == 1 && PurchaseOrderNo == 0)
            {
                return "Auto Insert success : " + "仕入番号 : " + no.ToString().ToString() + "で登録しました。";
            }
            else
            {
                return "";
            }

            #endregion

        }
 public SessionHold(int userId, string randomString, ExMySQLData db)
 {
     this.userId = userId;
     this.randomString = randomString;
     this.db = db;
 }
        public void UpdateSalesPrint(string random, int type, string _no)
        {

            #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 != "")
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesPrint(認証処理) " + _message);
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesPrint(認証処理)", ex);
            }

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesPrint(DbOpen)", ex);
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesPrint(BeginTransaction)", ex);
            }

            #endregion

            #region Update (発行区分更新)

            try
            {
                sb.Length = 0;
                sb.Append("UPDATE T_SALES_H " + Environment.NewLine);
                sb.Append("   SET DELIVERY_PRINT_FLG = 1" + Environment.NewLine);
                sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);        // COMPANY_ID
                sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);            // GROUP_ID
                sb.Append("   AND DELETE_FLG = 0 " + Environment.NewLine);
                sb.Append("   AND NO IN (" + _no.Replace("<<@escape_comma@>>", ",") + ")" + Environment.NewLine);          // ID

                db.ExecuteSQL(sb.ToString(), false);
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesPrint", ex);
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesPrint(CommitTransaction)", ex);
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesPrint(DbClose)", ex);
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                switch (type)
                {
                    case 0:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.Sales.SalesPrint,
                                                   DataPgEvidence.geOperationType.Update,
                                                   "NO:" + _no.Replace("<<@escape_comma@>>", ","));
                        break;
                    case 1:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.Sales.SalesPrint,
                                                   DataPgEvidence.geOperationType.Insert,
                                                   "NO:" + _no.Replace("<<@escape_comma@>>", ","));
                        break;
                    case 2:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.Sales.SalesPrint,
                                                   DataPgEvidence.geOperationType.Delete,
                                                   "NO:" + _no.Replace("<<@escape_comma@>>", ","));
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSalesPrint(Add Evidence)", ex);
            }

            #endregion

        }
        public string UpdateUser(string random, int type, long Id, EntityUser entity)
        {

            #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 != "")
                {
                    return _message;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateUser(認証処理)", ex);
                return CLASS_NM + ".UpdateUser : 認証処理に失敗しました。" + Environment.NewLine + ex.Message.ToString();
            }

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;
            string _Id = "";
            _Id = Id.ToString();

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData();
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateUser(DbOpen)", ex);
                return CLASS_NM + ".UpdateUser(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateUser(BeginTransaction)", ex);
                return CLASS_NM + ".UpdateUser(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Update

            if (type == 0)
            {
                try
                {
                    #region SQL

                    sb.Length = 0;
                    sb.Append("UPDATE SYS_M_USER " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                               ExCast.zCInt(personId),
                                                               ipAdress,
                                                               userId,
                                                               0));
                    sb.Append("      ,GROUP_ID = " + entity.group_id + Environment.NewLine);
                    sb.Append("      ,LOGIN_ID = " + ExEscape.zRepStr(entity.after_login_id) + Environment.NewLine);
                    sb.Append("      ,NAME = " + ExEscape.zRepStr(entity.name) + Environment.NewLine);
                    sb.Append("      ,PASSWORD = "******"      ,PERSON_ID = " + entity.person_id + Environment.NewLine);
                    sb.Append("      ,MEMO = " + ExEscape.zRepStr(entity.memo) + Environment.NewLine);
                    sb.Append("      ,DISPLAY_FLG = 1" + Environment.NewLine);
                    sb.Append(" WHERE ID = " + Id.ToString() + Environment.NewLine);            // ID

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateUser(Update)", ex);
                    return CLASS_NM + ".UpdateUser(Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }

            #endregion

            #region Delete

            if (type == 2)
            {
                //#region Exist Data

                //try
                //{
                //    bool _ret = false;
                //    _ret = DataExists.IsExistData(db, companyId, "", "T_ORDER_H", "User_ID", Id.ToString(), CommonUtl.geStrOrNumKbn.Number);
                //    if (_ret == true)
                //    {
                //        return "担当ID : " + Id + " は受注データの入力担当者に使用されている為、削除できません。";
                //    }
                //}
                //catch (Exception ex)
                //{
                //    db.ExRollbackTransaction();
                //    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateUser(Exist Data)", ex);
                //    return CLASS_NM + ".UpdateUser(Exist Data) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                //}

                //#endregion

                //#region Update

                //try
                //{
                //    sb.Length = 0;
                //    sb.Append("UPDATE SYS_M_USER " + Environment.NewLine);
                //    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                //                                               ExCast.zCInt(personId),
                //                                               ipAdress,
                //                                               userId,
                //                                               1));
                //    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);    // COMPANY_ID
                //    sb.Append("   AND ID = " + Id.ToString() + Environment.NewLine);        // ID

                //    db.ExecuteSQL(sb.ToString(), false);

                //}
                //catch (Exception ex)
                //{
                //    db.ExRollbackTransaction();
                //    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateUser(Delete)", ex);
                //    return CLASS_NM + ".UpdateUser(Delete) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                //}

                //#endregion
            }

            #endregion

            #region PG排他制御

            if (type == 0 || type == 2)
            {
                try
                {
                    DataPgLock.DelLockPg(companyId, userId, PG_NM, "", ipAdress, false, db);
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateUser(DelLockPg)", ex);
                    return CLASS_NM + ".UpdateUser(DelLockPg) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateUser(CommitTransaction)", ex);
                return CLASS_NM + ".UpdateUser(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateUser(DbClose)", ex);
                return CLASS_NM + ".UpdateUser(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                switch (type)
                {
                    case 0:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Update,
                                                   "ID:" + Id.ToString());
                        break;
                    case 1:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Insert,
                                                   "ID:" + _Id.ToString());
                        break;
                    case 2:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Delete,
                                                   "ID:" + Id.ToString());
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateUser(Add Evidence)", ex);
                return CLASS_NM + ".UpdateUser(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Return

            if (type == 1 && Id == 0)
            {
                return "Auto Insert success : " + "ID : " + _Id.ToString() + "で登録しました。";
            }
            else
            {
                return "";
            }

            #endregion

        }
        public static void GetData(ExMySQLData db,
                                   string companyId,
                                   string groupId,
                                   string tblName,
                                   string col1Name,
                                   string col1Value,
                                   CommonUtl.geStrOrNumKbn col1Type,
                                   string get_col1Name,
                                   string get_col2Name,
                                   string get_col3Name,
                                   string get_col4Name,
                                   string get_col5Name,
                                   string get_another_col1Name,
                                   string get_another_col2Name,
                                   string get_another_col3Name,
                                   string get_another_col4Name,
                                   string get_another_col5Name,
                                   ref string get_col1Value,
                                   ref string get_col2Value,
                                   ref string get_col3Value,
                                   ref string get_col4Value,
                                   ref string get_col5Value)
        {
            StringBuilder sb;
            DataTable dt;

            try
            {
                sb = new StringBuilder();

                #region SQL

                sb.Append("SELECT " + get_col1Name + Environment.NewLine);
                if (!string.IsNullOrEmpty(get_col2Name)) sb.Append("      ," + get_col2Name + Environment.NewLine);
                if (!string.IsNullOrEmpty(get_col3Name)) sb.Append("      ," + get_col3Name + Environment.NewLine);
                if (!string.IsNullOrEmpty(get_col4Name)) sb.Append("      ," + get_col4Name + Environment.NewLine);
                if (!string.IsNullOrEmpty(get_col5Name)) sb.Append("      ," + get_col5Name + Environment.NewLine);
                sb.Append("  FROM " + tblName + " AS TBL" + Environment.NewLine);

                sb.Append(" WHERE TBL.DELETE_FLG = 0 " + Environment.NewLine);
                if (companyId != "")
                {
                    sb.Append("   AND TBL.COMPANY_ID = " + companyId + Environment.NewLine);
                }
                if (groupId != "")
                {
                    sb.Append("   AND TBL.GROUP_ID = " + groupId + Environment.NewLine);
                }
                if (col1Type == CommonUtl.geStrOrNumKbn.String)
                {
                    sb.Append("   AND TBL." + col1Name + " = " + ExEscape.zRepStr(col1Value) + Environment.NewLine);
                }
                else
                {
                    sb.Append("   AND TBL." + col1Name + " = " + ExCast.zCDbl(col1Value) + Environment.NewLine);
                }
                sb.Append(" LIMIT 0, 1" + Environment.NewLine);

                #endregion

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

                if (dt.DefaultView.Count > 0)
                {
                    get_col1Value = ExCast.zCStr(dt.DefaultView[0][get_another_col1Name]);
                    if (!string.IsNullOrEmpty(get_col2Name)) get_col2Value = ExCast.zCStr(dt.DefaultView[0][get_another_col2Name]);
                    if (!string.IsNullOrEmpty(get_col3Name)) get_col3Value = ExCast.zCStr(dt.DefaultView[0][get_another_col3Name]);
                    if (!string.IsNullOrEmpty(get_col4Name)) get_col4Value = ExCast.zCStr(dt.DefaultView[0][get_another_col4Name]);
                    if (!string.IsNullOrEmpty(get_col5Name)) get_col5Value = ExCast.zCStr(dt.DefaultView[0][get_another_col5Name]);
                }
                else
                {
                    get_col1Value = "";
                    get_col2Value = "";
                    get_col3Value = "";
                    get_col4Value = "";
                    get_col5Value = "";
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".GetData", ex);
                throw;
            }
            finally
            {
            }
        }
        public string UpdatePayment(string random, int type, List<EntityPaymentClose> entity)
        {

            #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 != "")
                {
                    return _message;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePayment(認証処理)", ex);
                return CLASS_NM + ".UpdatePayment : 認証処理に失敗しました。" + Environment.NewLine + ex.Message.ToString();
            }

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;
            string _Id = "";
            int _classKbn = 0;

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePayment(DbOpen)", ex);
                return CLASS_NM + ".UpdatePayment(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePayment(BeginTransaction)", ex);
                return CLASS_NM + ".UpdatePayment(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Delete

            if (type == 2)
            {
                try
                {
                    for (int i = 0; i <= entity.Count - 1; i++)
                    {
                        if (entity[i].exec_flg == true)
                        {
                            #region 支払データ存在確認

                            #region SQL

                            sb.Length = 0;
                            sb.Append("SELECT T.NO " + Environment.NewLine);
                            sb.Append("  FROM T_PAYMENT 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.PAYMENT_KBN = 0 " + Environment.NewLine);       // 締支払分のみ
                            sb.Append("   AND T.PAYMENT_YYYYMMDD > " + ExEscape.zRepStr(entity[i].payment_close_yyyymmdd) + Environment.NewLine);
                            sb.Append("   AND T.PURCHASE_ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entity[i].purchase_id)) + Environment.NewLine);
                            sb.Append(" LIMIT 0, 1");

                            #endregion

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

                            if (dt.DefaultView.Count > 0)
                            {
                                db.ExRollbackTransaction();
                                return CLASS_NM + "仕入先 " + entity[i].purchase_id + ":" + entity[i].purchase_nm + Environment.NewLine +
                                                  "支払番号 : " + entity[i].no + "は" + Environment.NewLine + 
                                                  "支払締日 : " + ExCast.zFormatToDate_YYYYMM(entity[0].payment_close_yyyymmdd) + " 以降のデータが存在する為、削除できません。";
                            }

                            #endregion

                            #region 出金データ存在確認

                            //#region SQL

                            //sb.Length = 0;
                            //sb.Append("SELECT T.ID " + Environment.NewLine);
                            //sb.Append("  FROM T_PAYMENT_CASH_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.PURCHASE_ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entity[i].purchase_id)) + Environment.NewLine);
                            //sb.Append("   AND T.PAYMENT_CASH_YMD <= " + ExEscape.zRepStr(entity[i].payment_close_yyyymmdd) + Environment.NewLine);

                            //#endregion

                            //dt = db.GetDataTable(sb.ToString());
                            //if (dt.DefaultView.Count > 0)
                            //{
                            //    return CLASS_NM + ".UpdatePayment(Delete Check) : 仕入先 " + entity[i].purchase_id + ":" + entity[i].purchase_nm +
                            //                      " 支払締日 : " + entity[0].payment_close_yyyymmdd + " 以前の出金データが存在する為、削除できません。";
                            //}

                            #endregion

                            #region 出金消込データ存在確認

                            #region SQL

                            sb.Length = 0;
                            sb.Append("SELECT T.ID " + Environment.NewLine);
                            sb.Append("  FROM T_PAYMENT_CASH_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.PAYMENT_NO = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entity[i].no)) + Environment.NewLine);

                            #endregion

                            dt = db.GetDataTable(sb.ToString());
                            if (dt.DefaultView.Count > 0)
                            {
                                db.ExRollbackTransaction();
                                return "仕入先 " + entity[i].purchase_id + ":" + entity[i].purchase_nm + Environment.NewLine +
                                       "支払番号 : " + entity[i].no + "は出金消込済の為、削除できません。";
                            }

                            #endregion

                            #region Delete SQL

                            sb.Length = 0;
                            sb.Append("DELETE FROM T_PAYMENT " + Environment.NewLine);
                            sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                            sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                            sb.Append("   AND PAYMENT_KBN = 0 " + Environment.NewLine);       // 締支払分のみ
                            sb.Append("   AND NO = " + ExCast.zCLng(entity[i].no) + Environment.NewLine);

                            #endregion

                            db.ExecuteSQL(sb.ToString(), false);

                            #region Update Purchase

                            sb.Length = 0;
                            sb.Append("UPDATE T_PURCHASE_H " + Environment.NewLine);
                            sb.Append("   SET PAYMENT_NO = 0" + Environment.NewLine);
                            sb.Append(" WHERE DELETE_FLG = 0 " + Environment.NewLine);
                            sb.Append("   AND COMPANY_ID = " + companyId + Environment.NewLine);
                            sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                            sb.Append("   AND BUSINESS_DIVISION_ID = 1 " + Environment.NewLine);        // 掛仕入のみ
                            sb.Append("   AND PURCHASE_YMD <= " + ExEscape.zRepStr(entity[i].payment_close_yyyymmdd) + Environment.NewLine);
                            sb.Append("   AND PURCHASE_ID = " + ExEscape.zRepStr(entity[i].purchase_id) + Environment.NewLine);

                            #endregion

                            db.ExecuteSQL(sb.ToString(), false);

                        }
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePayment(Delete)", ex);
                    return CLASS_NM + ".UpdatePayment(Delete) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }

            #endregion

            #region Insert

            if (type == 1)
            {
                #region Get Max Payment No

                long no = 0;
                try
                {
                    no = DataSlipNo.GetMaxSlipNo(companyId, groupId, db, "T_PAYMENT", "NO");
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePayment(Get Max No)", ex);
                    return CLASS_NM + ".UpdatePayment(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                try
                {
                    for (int i = 0; i <= entity.Count - 1; i++)
                    {
                        if (entity[i].exec_flg == true)
                        {
                            #region 支払データ存在確認

                            #region SQL

                            sb.Length = 0;
                            sb.Append("SELECT T.NO " + Environment.NewLine);
                            sb.Append("  FROM T_PAYMENT 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.PAYMENT_KBN = 0 " + Environment.NewLine);       // 締支払分のみ
                            sb.Append("   AND T.PAYMENT_YYYYMMDD > " + ExEscape.zRepStr(entity[i].payment_close_yyyymmdd) + Environment.NewLine);
                            sb.Append("   AND T.PURCHASE_ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entity[i].purchase_id)) + Environment.NewLine);
                            sb.Append(" LIMIT 0, 1");

                            #endregion

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

                            if (dt.DefaultView.Count > 0)
                            {
                                db.ExRollbackTransaction();
                                return CLASS_NM + "仕入先 " + entity[i].purchase_id + ":" + entity[i].purchase_nm + "は" + Environment.NewLine +
                                                  "支払締日 : " + ExCast.zFormatToDate_YYYYMM(entity[0].payment_close_yyyymmdd) + " 以降のデータが存在する為、削除できません。";
                            }

                            #endregion

                            if (entity[i].no != "未")
                            {
                                #region 出金消込データ存在確認

                                #region SQL

                                sb.Length = 0;
                                sb.Append("SELECT T.ID " + Environment.NewLine);
                                sb.Append("  FROM T_PAYMENT_CASH_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.PAYMENT_NO = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entity[i].no)) + Environment.NewLine);

                                #endregion

                                dt = db.GetDataTable(sb.ToString());
                                if (dt.DefaultView.Count > 0)
                                {
                                    db.ExRollbackTransaction();
                                    return "仕入先 " + entity[i].purchase_id + ":" + entity[i].purchase_nm + Environment.NewLine +
                                           "支払番号 : " + entity[i].no + "は出金消込済の為、削除できません。";
                                }

                                #endregion

                                #region Delete SQL

                                sb.Length = 0;
                                sb.Append("DELETE FROM T_PAYMENT " + Environment.NewLine);
                                sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                                sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                                sb.Append("   AND PAYMENT_KBN = 0 " + Environment.NewLine);       // 締支払分のみ
                                sb.Append("   AND NO = " + ExCast.zCLng(entity[i].no) + Environment.NewLine);

                                #endregion

                                db.ExecuteSQL(sb.ToString(), false);
                            }

                            #region Insert SQL

                            sb.Length = 0;
                            sb.Append("INSERT INTO T_PAYMENT " + Environment.NewLine);
                            sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                            sb.Append("       , GROUP_ID" + Environment.NewLine);
                            sb.Append("       , NO" + Environment.NewLine);
                            sb.Append("       , PURCHASE_ID" + Environment.NewLine);
                            sb.Append("       , PAYMENT_YYYYMMDD" + Environment.NewLine);
                            sb.Append("       , SUMMING_UP_GROUP_ID" + Environment.NewLine);
                            sb.Append("       , BEFORE_PAYMENT_YYYYMMDD" + Environment.NewLine);
                            sb.Append("       , PERSON_ID" + Environment.NewLine);
                            sb.Append("       , PAYMENT_CYCLE_ID" + Environment.NewLine);
                            sb.Append("       , PAYMENT_DAY" + Environment.NewLine);
                            sb.Append("       , PAYMENT_PLAN_DAY" + Environment.NewLine);
                            sb.Append("       , BEFORE_PAYMENT_PRICE" + Environment.NewLine);
                            sb.Append("       , PAYMENT_CASH_PRICE" + Environment.NewLine);
                            sb.Append("       , TRANSFER_PRICE" + Environment.NewLine);
                            sb.Append("       , PURCHASE_PRICE" + Environment.NewLine);
                            sb.Append("       , NO_TAX_PURCHASE_PRICE" + Environment.NewLine);
                            sb.Append("       , TAX" + Environment.NewLine);
                            sb.Append("       , PAYMENT_PRICE" + Environment.NewLine);
                            sb.Append("       , MEMO" + Environment.NewLine);
                            sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                            sb.Append("       , DELETE_FLG" + Environment.NewLine);
                            sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                            sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                            sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                            sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                            sb.Append("       , CREATE_DATE" + Environment.NewLine);
                            sb.Append("       , CREATE_TIME" + Environment.NewLine);
                            sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                            sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                            sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                            sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                            sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                            sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                            sb.Append(")" + Environment.NewLine);
                            sb.Append("SELECT  " + companyId + Environment.NewLine);                                                // COMPANY_ID
                            sb.Append("       ," + groupId + Environment.NewLine);                                                  // GROUP_ID

                            if (entity[i].no != "未")
                            {
                                sb.Append("       ," + ExCast.zCLng(entity[i].no) + Environment.NewLine);                           // NO
                            }
                            else
                            {
                                sb.Append("       ," + no.ToString() + Environment.NewLine);                                        // NO
                            }

                            sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entity[i].purchase_id)) + Environment.NewLine);    // PURCHASE_ID
                            sb.Append("       ," + ExEscape.zRepStr(entity[i].payment_close_yyyymmdd) + Environment.NewLine);                       // PAYMENT_YYYYMMDD
                            sb.Append("       ," + ExEscape.zRepStr(entity[i].summing_up_group_id) + Environment.NewLine);                          // SUMMING_UP_DAY
                            sb.Append("       ," + ExEscape.zRepStr(entity[i].before_payment_yyyymmdd) + Environment.NewLine);                      // BEFORE_PAYMENT_YYYYMMDD
                            sb.Append("       ," + entity[i].person_id + Environment.NewLine);                                                      // PERSON_ID
                            sb.Append("       ," + entity[i].payment_cycle_id + Environment.NewLine);                                               // PAYMENT_CYCLE_ID
                            sb.Append("       ," + entity[i].payment_day + Environment.NewLine);                                                    // PAYMENT_DAY
                            sb.Append("       ," + ExEscape.zRepStr(entity[i].payment_plan_day) + Environment.NewLine);                             // PAYMENT_PLAN_DAY
                            sb.Append("       ," + entity[i].before_payment_price + Environment.NewLine);                                           // BEFORE_PAYMENT_PRICE
                            sb.Append("       ," + entity[i].payment_cash_price + Environment.NewLine);                                             // PAYMENT_CASH_PRICE
                            sb.Append("       ," + entity[i].transfer_price + Environment.NewLine);                                                 // TRANSFER_PRICE
                            sb.Append("       ," + entity[i].purchase_price + Environment.NewLine);                                                 // PURCHASE_PRICE
                            sb.Append("       ," + entity[i].no_tax_purchase_price + Environment.NewLine);                                          // NO_TAX_PURCHASE_PRICE
                            sb.Append("       ," + entity[i].tax + Environment.NewLine);                                                            // TAX
                            sb.Append("       ," + entity[i].payment_price + Environment.NewLine);                                                  // PAYMENT_PRICE
                            sb.Append("       ," + ExEscape.zRepStr(entity[i].memo) + Environment.NewLine);                                         // MEMO
                            sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                        PG_NM,
                                                                        "T_PAYMENT",
                                                                        ExCast.zCInt(personId),
                                                                        "",
                                                                        ipAdress,
                                                                        userId));

                            #endregion

                            db.ExecuteSQL(sb.ToString(), false);

                            #region Update Purchase

                            sb.Length = 0;
                            sb.Append("UPDATE T_PURCHASE_H " + Environment.NewLine);

                            if (entity[i].no != "未")
                            {
                                sb.Append("   SET PAYMENT_NO = " + ExCast.zCLng(entity[i].no) + Environment.NewLine);
                            }
                            else
                            {
                                sb.Append("   SET PAYMENT_NO = " + no.ToString() + Environment.NewLine);
                            }

                            sb.Append(" WHERE DELETE_FLG = 0 " + Environment.NewLine);
                            sb.Append("   AND COMPANY_ID = " + companyId + Environment.NewLine);
                            sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                            sb.Append("   AND BUSINESS_DIVISION_ID = 1 " + Environment.NewLine);        // 掛仕入のみ
                            sb.Append("   AND PURCHASE_YMD <= " + ExEscape.zRepStr(entity[i].payment_close_yyyymmdd) + Environment.NewLine);
                            sb.Append("   AND PURCHASE_ID = " + ExEscape.zRepStr(entity[i].purchase_id) + Environment.NewLine);

                            #endregion

                            db.ExecuteSQL(sb.ToString(), false);

                            no += 1;
                        }
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePayment(Insert)", ex);
                    return CLASS_NM + ".UpdatePayment(Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

            }

            #endregion

            #region PG排他制御

            //try
            //{
            //    DataPgLock.DelLockPg(companyId, userId, PG_NM, "", ipAdress, false, db);
            //}
            //catch (Exception ex)
            //{
            //    db.ExRollbackTransaction();
            //    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePayment(DelLockPg)", ex);
            //    return CLASS_NM + ".UpdatePayment(DelLockPg) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            //}

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePayment(CommitTransaction)", ex);
                return CLASS_NM + ".UpdatePayment(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePayment(DbClose)", ex);
                return CLASS_NM + ".UpdatePayment(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                           companyId,
                                           userId,
                                           ipAdress,
                                           sessionString,
                                           PG_NM,
                                           DataPgEvidence.geOperationType.DeleteAndInsert,
                                           "");
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePayment(Add Evidence)", ex);
                return CLASS_NM + ".UpdatePayment(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            if (type == 2)
            {
                return "削除しました。";
            }
            else if (type == 1)
            {
                return "支払締切を実行しました。";
            }
            else
            {
                return "";
            }

        }
        public string UpdateReportSetting(string random, int type, string Id, EntityReportSetting entity)
        {
            #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 != "")
                {
                    return _message;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateReportSetting(認証処理)", ex);
                return CLASS_NM + ".UpdateReportSetting : 認証処理に失敗しました。" + Environment.NewLine + ex.Message.ToString();
            }

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;
            string _Id = Id;

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateReportSetting(DbOpen)", ex);
                return CLASS_NM + ".UpdateReportSetting(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateReportSetting(BeginTransaction)", ex);
                return CLASS_NM + ".UpdateReportSetting(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Insert

            if (type <= 1)
            {
                try
                {
                    #region Delete SQL

                    sb.Length = 0;
                    sb.Append("DELETE FROM M_REPORT_SETTING " + Environment.NewLine);
                    sb.Append(" WHERE USER_ID = " + userId + Environment.NewLine);
                    sb.Append("   AND PG_ID = " + ExEscape.zRepStr(Id) + Environment.NewLine);

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                    #region Insert SQL

                    sb.Length = 0;
                    sb.Append("INSERT INTO M_REPORT_SETTING " + Environment.NewLine);
                    sb.Append("       ( USER_ID" + Environment.NewLine);
                    sb.Append("       , PG_ID" + Environment.NewLine);
                    sb.Append("       , SIZE" + Environment.NewLine);
                    sb.Append("       , ORIENTATION" + Environment.NewLine);
                    sb.Append("       , LEFT_MARGIN" + Environment.NewLine);
                    sb.Append("       , RIGHT_MARGIN" + Environment.NewLine);
                    sb.Append("       , TOP_MARGIN" + Environment.NewLine);
                    sb.Append("       , BOTTOM_MARGIN" + Environment.NewLine);
                    sb.Append("       , GROUP_ID_FROM" + Environment.NewLine);
                    sb.Append("       , GROUP_ID_TO" + Environment.NewLine);
                    sb.Append("       , GROUP_TOTAL" + Environment.NewLine);
                    sb.Append("       , TOTAL_KBN" + Environment.NewLine);
                    sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                    sb.Append("       , DELETE_FLG" + Environment.NewLine);
                    sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_DATE" + Environment.NewLine);
                    sb.Append("       , CREATE_TIME" + Environment.NewLine);
                    sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                    sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                    sb.Append(")" + Environment.NewLine);
                    sb.Append("SELECT  " + userId + Environment.NewLine);                       // USER_ID
                    sb.Append("       ," + ExEscape.zRepStr(_Id) + Environment.NewLine);        // PG_ID
                    sb.Append("       ," + entity.size + Environment.NewLine);                  // SIZE
                    sb.Append("       ," + entity.orientation + Environment.NewLine);           // ORIENTATION
                    sb.Append("       ," + entity.left_margin + Environment.NewLine);           // LEFT_MARGIN
                    sb.Append("       ," + entity.right_margin + Environment.NewLine);          // RIGHT_MARGIN
                    sb.Append("       ," + entity.top_margin + Environment.NewLine);            // TOP_MARGIN
                    sb.Append("       ," + entity.bottom_margin + Environment.NewLine);         // BOTTOM_MARGIN
                    sb.Append("       ," + entity.group_id_from + Environment.NewLine);         // GROUP_ID_FROM
                    sb.Append("       ," + entity.group_id_to + Environment.NewLine);           // GROUP_ID_TO
                    sb.Append("       ," + entity.group_total + Environment.NewLine);           // GROUP_TOTAL
                    sb.Append("       ," + entity.total_kbn + Environment.NewLine);             // TOTAL_KBN
                    sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                PG_NM,
                                                                "M_REPORT_SETTING",
                                                                ExCast.zCInt(personId),
                                                                _Id,
                                                                ipAdress,
                                                                userId));

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateReportSetting(Insert)", ex);
                    return CLASS_NM + ".UpdateReportSetting(Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

            }

            #endregion

            #region Update

            if (type == 0)
            {
                //try
                //{
                //    #region SQL

                //    string _invoice_id = entity.invoice_id;
                //    if (ExCast.IsNumeric(_invoice_id)) _invoice_id = ExCast.zCDbl(_invoice_id).ToString();

                //    sb.Length = 0;
                //    sb.Append("UPDATE M_REPORT_SETTING " + Environment.NewLine);
                //    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                //                                               ExCast.zCInt(personId),
                //                                               ipAdress,
                //                                               userId,
                //                                               0));
                //    sb.Append("      ,NAME = " + ExEscape.zRepStr(entity.name) + Environment.NewLine);
                //    sb.Append("      ,KANA = " + ExEscape.zRepStr(entity.kana) + Environment.NewLine);
                //    sb.Append("      ,ABOUT_NAME = " + ExEscape.zRepStr(entity.about_name) + Environment.NewLine);
                //    sb.Append("      ,ZIP_CODE = " + ExCast.zNullToZero(entity.zip_code_from + entity.zip_code_to) + Environment.NewLine);
                //    sb.Append("      ,PREFECTURE_ID = " + entity.prefecture_id + Environment.NewLine);
                //    sb.Append("      ,CITY_ID = " + entity.city_id + Environment.NewLine);
                //    sb.Append("      ,TOWN_ID = " + entity.town_id + Environment.NewLine);
                //    sb.Append("      ,ADRESS_CITY = " + ExEscape.zRepStr(entity.adress_city) + Environment.NewLine);
                //    sb.Append("      ,ADRESS_TOWN = " + ExEscape.zRepStr(entity.adress_town) + Environment.NewLine);
                //    sb.Append("      ,ADRESS1 = " + ExEscape.zRepStr(entity.adress1) + Environment.NewLine);
                //    sb.Append("      ,ADRESS2 = " + ExEscape.zRepStr(entity.adress2) + Environment.NewLine);
                //    sb.Append("      ,STATION_NAME = " + ExEscape.zRepStr(entity.station_name) + Environment.NewLine);
                //    sb.Append("      ,POST_NAME = " + ExEscape.zRepStr(entity.post_name) + Environment.NewLine);
                //    sb.Append("      ,PERSON_NAME = " + ExEscape.zRepStr(entity.person_name) + Environment.NewLine);
                //    sb.Append("      ,TITLE_ID = " + entity.title_id + Environment.NewLine);
                //    sb.Append("      ,TITLE_NAME = " + ExEscape.zRepStr(entity.title_name) + Environment.NewLine);
                //    sb.Append("      ,TEL = " + ExEscape.zRepStr(entity.tel) + Environment.NewLine);
                //    sb.Append("      ,FAX = " + ExEscape.zRepStr(entity.fax) + Environment.NewLine);
                //    sb.Append("      ,MAIL_ADRESS = " + ExEscape.zRepStr(entity.mail_adress) + Environment.NewLine);
                //    sb.Append("      ,MOBILE_TEL = " + ExEscape.zRepStr(entity.mobile_tel) + Environment.NewLine);
                //    sb.Append("      ,MOBILE_ADRESS = " + ExEscape.zRepStr(entity.mobile_adress) + Environment.NewLine);
                //    sb.Append("      ,URL = " + ExEscape.zRepStr(entity.url) + Environment.NewLine);
                //    sb.Append("      ,INVOICE_ID = " + ExEscape.zRepStr(_invoice_id) + Environment.NewLine);
                //    sb.Append("      ,BUSINESS_DIVISION_ID = " + entity.business_division_id + Environment.NewLine);
                //    sb.Append("      ,UNIT_KIND_ID = " + entity.unit_kind_id + Environment.NewLine);
                //    sb.Append("      ,CREDIT_RATE = " + entity.credit_rate + Environment.NewLine);
                //    sb.Append("      ,TAX_CHANGE_ID = " + entity.tax_change_id + Environment.NewLine);
                //    sb.Append("      ,SUMMING_UP_GROUP_ID = " + ExEscape.zRepStr(entity.summing_up_group_id) + Environment.NewLine);
                //    sb.Append("      ,PRICE_FRACTION_PROC_ID = " + entity.price_fraction_proc_id + Environment.NewLine);
                //    sb.Append("      ,TAX_FRACTION_PROC_ID = " + entity.tax_fraction_proc_id + Environment.NewLine);
                //    sb.Append("      ,CREDIT_LIMIT_PRICE = " + entity.credit_limit_price + Environment.NewLine);
                //    sb.Append("      ,SALES_CREDIT_PRICE = " + entity.sales_credit_price + Environment.NewLine);
                //    sb.Append("      ,RECEIPT_DIVISION_ID = " + ExEscape.zRepStr(entity.receipt_division_id) + Environment.NewLine);
                //    sb.Append("      ,COLLECT_CYCLE_ID = " + entity.collect_cycle_id + Environment.NewLine);
                //    sb.Append("      ,COLLECT_DAY = " + entity.collect_day + Environment.NewLine);
                //    sb.Append("      ,BILL_SITE = " + entity.bill_site + Environment.NewLine);
                //    sb.Append("      ,GROUP1_ID = " + ExEscape.zRepStr(entity.group1_id) + Environment.NewLine);
                //    sb.Append("      ,GROUP2_ID = " + ExEscape.zRepStr(entity.group2_id) + Environment.NewLine);
                //    sb.Append("      ,GROUP3_ID = " + ExEscape.zRepStr(entity.group3_id) + Environment.NewLine);
                //    sb.Append("      ,MEMO = " + ExEscape.zRepStr(entity.memo) + Environment.NewLine);
                //    sb.Append("      ,DISPLAY_FLG = " + entity.display_division_id + Environment.NewLine);
                //    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);                            // COMPANY_ID
                //    sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(Id)) + Environment.NewLine);     // ID

                //    #endregion

                //    db.ExecuteSQL(sb.ToString(), false);

                //}
                //catch (Exception ex)
                //{
                //    db.ExRollbackTransaction();
                //    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateReportSetting(Update)", ex);
                //    return CLASS_NM + ".UpdateReportSetting(Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                //}
            }

            #endregion

            #region Delete

            //if (type == 2)
            //{
            //    #region Exist Data

            //    try
            //    {
            //        bool _ret = false;
            //        _ret = DataExists.IsExistData(db, companyId, "", "M_REPORT_SETTING", "INVOICE_ID", ExCast.zNumZeroNothingFormat(Id), "ID", ExCast.zNumZeroNothingFormat(Id), CommonUtl.geStrOrNumKbn.String);
            //        if (_ret == true)
            //        {
            //            return "得意先ID : " + Id + " は得意先マスタの請求IDにが使用されている為、削除できません。";
            //        }

            //        _ret = DataExists.IsExistData(db, companyId, "", "T_ORDER_H", "ReportSetting_ID", ExCast.zNumZeroNothingFormat(Id), CommonUtl.geStrOrNumKbn.String);
            //        if (_ret == true)
            //        {
            //            return "得意先ID : " + Id + " は受注データの得意先に使用されている為、削除できません。";
            //        }

            //    }
            //    catch (Exception ex)
            //    {
            //        db.ExRollbackTransaction();
            //        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateReportSetting(Exist Data)", ex);
            //        return CLASS_NM + ".UpdateReportSetting(Exist Data) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            //    }

            //    #endregion

            //    #region Update

            //    try
            //    {
            //        sb.Length = 0;
            //        sb.Append("UPDATE M_REPORT_SETTING " + Environment.NewLine);
            //        sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
            //                                                   ExCast.zCInt(personId),
            //                                                   ipAdress,
            //                                                   userId,
            //                                                   1));
            //        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);    // COMPANY_ID
            //        sb.Append("   AND ID = " + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(Id)) + Environment.NewLine);            // ID

            //        db.ExecuteSQL(sb.ToString(), false);

            //    }
            //    catch (Exception ex)
            //    {
            //        db.ExRollbackTransaction();
            //        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateReportSetting(Delete)", ex);
            //        return CLASS_NM + ".UpdateReportSetting(Delete) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            //    }

            //    #endregion
            //}

            #endregion

            #region PG排他制御

            //if (type == 0 || type == 2)
            //{
            //    try
            //    {
            //        DataPgLock.DelLockPg(companyId, userId, PG_NM, "", ipAdress, false, db);
            //    }
            //    catch (Exception ex)
            //    {
            //        db.ExRollbackTransaction();
            //        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateReportSetting(DelLockPg)", ex);
            //        return CLASS_NM + ".UpdateReportSetting(DelLockPg) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            //    }
            //}

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateReportSetting(CommitTransaction)", ex);
                return CLASS_NM + ".UpdateReportSetting(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateReportSetting(DbClose)", ex);
                return CLASS_NM + ".UpdateReportSetting(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                switch (type)
                {
                    case 0:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Update,
                                                   "ID:" + _Id.ToString());
                        break;
                    case 1:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Insert,
                                                   "ID:" + _Id.ToString());
                        break;
                    case 2:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Delete,
                                                   "ID:" + Id.ToString());
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateReportSetting(Add Evidence)", ex);
                return CLASS_NM + ".UpdateReportSetting(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Return

            return "";
            //if (type == 1 && (Id == "0" || Id == ""))
            //{
            //    return "Auto Insert success : " + "ID : " + _Id.ToString() + "で登録しました。";
            //}
            //else
            //{
            //    return "";
            //}

            #endregion
        }
        public string UpdateDuties(string random, int type, long no, EntityDuties entity)
        {

            #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 != "")
                {
                    return _message;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateDuties(認証処理)", ex);
                return CLASS_NM + ".UpdateDuties : 認証処理に失敗しました。" + Environment.NewLine + ex.Message.ToString();
            }

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;
            string _no = "";

            DateTime now = DateTime.Now;
            string date = now.ToString("yyyy/MM/dd");
            string time = now.ToString("HH:mm:ss");

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateDuties(DbOpen)", ex);
                return CLASS_NM + ".UpdateDuties(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
                if (CommonUtl.gSysDbKbn == 0) db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateDuties(BeginTransaction)", ex);
                return CLASS_NM + ".UpdateDuties(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Get Max ID

            if (type == 1)
            {
                try
                {
                    DataMaxId.GetMaxId(db,
                                       "T_DUTIES_COMMUNICATION",
                                       "NO",
                                       out _no);

                    if (_no == "")
                    {
                        return "業務連絡NOの取得に失敗しました。";
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateDuties(GetMaxId)", ex);
                    return CLASS_NM + ".UpdateDuties(GetMaxId) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }
            else
            {
                _no = no.ToString();
            }

            #endregion

            #region Upload File Set

            if (type <= 1)
            {
                if (!string.IsNullOrEmpty(entity.upload_file_path1))
                {
                    if (System.IO.Directory.Exists(CommonUtl.gstrFileUpLoadDutiesDir + entity.upload_file_path1) && !System.IO.Directory.Exists(CommonUtl.gstrFileUpLoadDutiesDir + _no + "-" + date.Replace("/", "") + time.Replace(":", "")))
                    {
                        System.IO.Directory.Move(CommonUtl.gstrFileUpLoadDutiesDir + entity.upload_file_path1, CommonUtl.gstrFileUpLoadDutiesDir + _no + "-" + date.Replace("/", "") + time.Replace(":", ""));
                        entity.upload_file_path1 = CommonUtl.gstrFileUpLoadDutiesDir + _no + "-" + date.Replace("/", "") + time.Replace(":", "") + "\\" + entity.upload_file_name1;
                    }
                }
            }

            #endregion

            #region Insert

            if (type == 1)
            {
                try
                {
                    #region Insert SQL

                    sb.Length = 0;
                    sb.Append("INSERT INTO T_DUTIES_COMMUNICATION " + Environment.NewLine);
                    sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                    sb.Append("       , NO" + Environment.NewLine);
                    sb.Append("       , GROUP_ID" + Environment.NewLine);
                    sb.Append("       , DATE" + Environment.NewLine);
                    sb.Append("       , TIME" + Environment.NewLine);
                    sb.Append("       , PERSON_ID" + Environment.NewLine);
                    sb.Append("       , DUTIES_LEVEL_ID" + Environment.NewLine);
                    sb.Append("       , DUTIES_STATE_ID" + Environment.NewLine);
                    sb.Append("       , TITLE" + Environment.NewLine);
                    sb.Append("       , CONTENT" + Environment.NewLine);
                    sb.Append("       , UPLOAD_FILE_PATH1" + Environment.NewLine);
                    sb.Append("       , UPLOAD_FILE_PATH2" + Environment.NewLine);
                    sb.Append("       , UPLOAD_FILE_PATH3" + Environment.NewLine);
                    sb.Append("       , UPLOAD_FILE_PATH4" + Environment.NewLine);
                    sb.Append("       , UPLOAD_FILE_PATH5" + Environment.NewLine);
                    sb.Append("       , MEMO" + Environment.NewLine);
                    sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                    sb.Append("       , DELETE_FLG" + Environment.NewLine);
                    sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_DATE" + Environment.NewLine);
                    sb.Append("       , CREATE_TIME" + Environment.NewLine);
                    sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                    sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                    sb.Append(")" + Environment.NewLine);
                    sb.Append("SELECT  " + companyId + Environment.NewLine);                                        // COMPANY_ID
                    sb.Append("       ," + _no + Environment.NewLine);                                              // NO
                    sb.Append("       ," + entity.gropu_id + Environment.NewLine);                                  // GROUP_ID
                    sb.Append("       ," + ExEscape.zRepStr(date) + Environment.NewLine);                           // DATE
                    sb.Append("       ," + ExEscape.zRepStr(time) + Environment.NewLine);                           // TIME
                    sb.Append("       ," + entity.person_id + Environment.NewLine);                                 // PERSON_ID
                    sb.Append("       ," + entity.duties_level_id + Environment.NewLine);                           // DUTIES_LEVEL_ID
                    sb.Append("       ," + entity.duties_state_id + Environment.NewLine);                           // DUTIES_STATE_ID
                    sb.Append("       ," + ExEscape.zRepStr(entity.title) + Environment.NewLine);                   // TITLE
                    sb.Append("       ," + ExEscape.zRepStr(entity.content) + Environment.NewLine);                 // CONTENT
                    sb.Append("       ," + ExEscape.zRepStr(entity.upload_file_path1) + Environment.NewLine);       // UPLOAD_FILE_PATH1
                    sb.Append("       ," + ExEscape.zRepStr(entity.upload_file_path2) + Environment.NewLine);       // UPLOAD_FILE_PATH2
                    sb.Append("       ," + ExEscape.zRepStr(entity.upload_file_path3) + Environment.NewLine);       // UPLOAD_FILE_PATH3
                    sb.Append("       ," + ExEscape.zRepStr(entity.upload_file_path4) + Environment.NewLine);       // UPLOAD_FILE_PATH4
                    sb.Append("       ," + ExEscape.zRepStr(entity.upload_file_path5) + Environment.NewLine);       // UPLOAD_FILE_PATH5
                    sb.Append("       ," + ExEscape.zRepStr(entity.memo) + Environment.NewLine);                    // MEMO
                    sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                PG_NM,
                                                                "T_DUTIES_COMMUNICATION",
                                                                ExCast.zCInt(personId),
                                                                _no,
                                                                ipAdress,
                                                                userId));

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);
                    if (CommonUtl.gSysDbKbn == 0) db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateDuties(Insert)", ex);
                    return CLASS_NM + ".UpdateDuties(Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }

            #endregion

            #region Update

            if (type == 0)
            {
                #region Update

                try
                {
                    #region SQL

                    sb.Length = 0;
                    sb.Append("UPDATE T_DUTIES_COMMUNICATION " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                               ExCast.zCInt(personId),
                                                               ipAdress,
                                                               userId,
                                                               0));
                    sb.Append("      ,GROUP_ID = " + entity.gropu_id + Environment.NewLine);
                    sb.Append("      ,DATE = " + ExEscape.zRepStr(date) + Environment.NewLine);
                    sb.Append("      ,TIME = " + ExEscape.zRepStr(time) + Environment.NewLine);
                    sb.Append("      ,PERSON_ID = " + entity.person_id + Environment.NewLine);
                    sb.Append("      ,DUTIES_LEVEL_ID = " + entity.duties_level_id + Environment.NewLine);
                    sb.Append("      ,DUTIES_STATE_ID = " + entity.duties_state_id + Environment.NewLine);
                    sb.Append("      ,TITLE = " + ExEscape.zRepStr(entity.title) + Environment.NewLine);
                    sb.Append("      ,CONTENT = " + ExEscape.zRepStr(entity.content) + Environment.NewLine);
                    sb.Append("      ,UPLOAD_FILE_PATH1 = " + ExEscape.zRepStr(entity.upload_file_path1) + Environment.NewLine);
                    sb.Append("      ,UPLOAD_FILE_PATH2 = " + ExEscape.zRepStr(entity.upload_file_path2) + Environment.NewLine);
                    sb.Append("      ,UPLOAD_FILE_PATH3 = " + ExEscape.zRepStr(entity.upload_file_path3) + Environment.NewLine);
                    sb.Append("      ,UPLOAD_FILE_PATH4 = " + ExEscape.zRepStr(entity.upload_file_path4) + Environment.NewLine);
                    sb.Append("      ,UPLOAD_FILE_PATH5 = " + ExEscape.zRepStr(entity.upload_file_path5) + Environment.NewLine);
                    sb.Append("      ,MEMO = " + ExEscape.zRepStr(entity.memo) + Environment.NewLine);
                    sb.Append(" WHERE NO = " + _no + Environment.NewLine);

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);
                    if (CommonUtl.gSysDbKbn == 0) db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateDuties(Update Head)", ex);
                    return CLASS_NM + ".UpdateDuties(Update Head) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion
            }

            #endregion

            #region Delete

            if (type == 2)
            {
            }

            #endregion

            #region PG排他制御

            if (type == 0 || type == 2)
            {
                try
                {
                    DataPgLock.DelLockPg(companyId, userId, PG_NM, "", ipAdress, false, db);
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateDuties(DelLockPg)", ex);
                    return CLASS_NM + ".UpdateDuties(DelLockPg) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateDuties(CommitTransaction)", ex);
                return CLASS_NM + ".UpdateDuties(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateDuties(DbClose)", ex);
                return CLASS_NM + ".UpdateDuties(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                switch (type)
                {
                    case 0:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Update,
                                                   "NO:" + _no);
                        break;
                    case 1:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Insert,
                                                   "NO:" + _no);
                        break;
                    case 2:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   PG_NM,
                                                   DataPgEvidence.geOperationType.Delete,
                                                   "NO:" + _no);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateDuties(Add Evidence)", ex);
                return CLASS_NM + ".UpdateDuties(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Return

            if (type == 1)
            {
                return "Auto Insert success : " + "NO : " + _no + "で登録しました。";
            }
            else
            {
                return "";
            }

            #endregion
        }
        public string UpdatePurchase(string random, int type, long PurchaseNo, EntityPurchaseH entityH, List<EntityPurchaseD> entityD, EntityPurchaseH before_entityH, List<EntityPurchaseD> before_entityD)
        {

            #region 認証処理

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

            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]);

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

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;

            string accountPeriod = "";
            long id = 0;
            long no = PurchaseNo;
            string ret_message = "";

            EntityInOutDeliveryH _entityInOutDeliveryH = new EntityInOutDeliveryH();
            List<EntityInOutDeliveryD> _entityInOutDeliveryListD = new List<EntityInOutDeliveryD>();

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(DbOpen)", ex);
                return "UpdatePurchase(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Payment Close Check

            try
            {
                if (type <= 1)
                {
                    // 掛仕入
                    if (entityH.business_division_id == 1)
                    {
                        // 支払締切済チェック
                        if (DataClose.IsPaymentClose(companyId, db, entityH.purchase_id, entityH.purchase_ymd))
                        {
                            db.DbClose();
                            return "仕入日 : " + entityH.purchase_ymd + " は支払締切済の為、計上できません。";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(Payment Close Check)", ex);
                return "UpdatePurchase(Payment Close Check) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(BeginTransaction)", ex);
                return "UpdatePurchase(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Update or Insert

            if (type <= 1)
            {
                #region Get Accout Period

                try
                {
                    accountPeriod = DataAccount.GetAccountPeriod(ExCast.zCStr(HttpContext.Current.Session[ExSession.ACCOUNT_BEGIN_PERIOD]), entityH.purchase_ymd);
                    if (accountPeriod == "")
                    {
                        return "会計年の取得に失敗しました。(期首月日 : " + ExCast.zCStr(HttpContext.Current.Session[ExSession.ACCOUNT_BEGIN_PERIOD]) +
                                                             " 仕入日 : " + entityH.purchase_ymd + ")";
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(GetAccountPeriod)", ex);
                    return "UpdatePurchase(GetAccountPeriod) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Get Slip No

                try
                {
                    DataSlipNo.GetSlipNo(companyId,
                                         groupId,
                                         db,
                                         DataSlipNo.geSlipKbn.Vendor,
                                         accountPeriod,
                                         ref no,
                                         out id,
                                         ExCast.zCStr(HttpContext.Current.Session[ExSession.IP_ADRESS]),
                                         ExCast.zCStr(HttpContext.Current.Session[ExSession.USER_ID]));
                    if (no == 0 || id == 0)
                    {
                        db.ExRollbackTransaction();
                        return "伝票番号の取得に失敗しました。(会計年 : " + accountPeriod + ")";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(GetSlipNo)", ex);
                    db.ExRollbackTransaction();
                    return "UpdatePurchase(GetSlipNo) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Head Insert

                try
                {
                    #region SQL

                    sb.Length = 0;
                    sb.Append("INSERT INTO T_PURCHASE_H " + Environment.NewLine);
                    sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                    sb.Append("       , GROUP_ID" + Environment.NewLine);
                    sb.Append("       , ID" + Environment.NewLine);
                    sb.Append("       , NO" + Environment.NewLine);
                    sb.Append("       , PURCHASE_ORDER_NO" + Environment.NewLine);
                    sb.Append("       , PURCHASE_YMD" + Environment.NewLine);
                    sb.Append("       , PERSON_ID" + Environment.NewLine);
                    sb.Append("       , PURCHASE_ID" + Environment.NewLine);
                    sb.Append("       , TAX_CHANGE_ID" + Environment.NewLine);
                    sb.Append("       , BUSINESS_DIVISION_ID" + Environment.NewLine);
                    sb.Append("       , SEND_ID" + Environment.NewLine);
                    sb.Append("       , CUSTOMER_ID" + Environment.NewLine);
                    sb.Append("       , SUPPLIER_ID" + Environment.NewLine);
                    sb.Append("       , SUPPLY_YMD" + Environment.NewLine);
                    sb.Append("       , DELIVER_DIVISION_ID" + Environment.NewLine);
                    sb.Append("       , SUM_ENTER_NUMBER" + Environment.NewLine);
                    sb.Append("       , SUM_CASE_NUMBER" + Environment.NewLine);
                    sb.Append("       , SUM_NUMBER" + Environment.NewLine);
                    sb.Append("       , SUM_UNIT_PRICE" + Environment.NewLine);
                    sb.Append("       , SUM_TAX" + Environment.NewLine);
                    sb.Append("       , SUM_NO_TAX_PRICE" + Environment.NewLine);
                    sb.Append("       , SUM_PRICE" + Environment.NewLine);
                    sb.Append("       , MEMO" + Environment.NewLine);
                    sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                    sb.Append("       , DELETE_FLG" + Environment.NewLine);
                    sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_DATE" + Environment.NewLine);
                    sb.Append("       , CREATE_TIME" + Environment.NewLine);
                    sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                    sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                    sb.Append(")" + Environment.NewLine);
                    sb.Append("SELECT  " + companyId + Environment.NewLine);                                                                // COMPANY_ID
                    sb.Append("       ," + groupId + Environment.NewLine);                                                                  // GROUP_ID
                    sb.Append("       ," + id.ToString() + Environment.NewLine);                                                            // ID
                    sb.Append("       ," + no.ToString() + Environment.NewLine);                                                            // NO
                    sb.Append("       ," + entityH.purchase_order_no + Environment.NewLine);                                                // PURCHASE_ORDER_NO
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zDateNullToDefault(entityH.purchase_ymd)) + Environment.NewLine);        // PURCHASE_YMD
                    sb.Append("       ," + entityH.update_person_id + Environment.NewLine);                                                 // PERSON_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityH.purchase_id)) + Environment.NewLine);      // PURCHASE_ID
                    sb.Append("       ," + entityH.tax_change_id + Environment.NewLine);                                                    // TAX_CHANGE_ID
                    sb.Append("       ," + entityH.business_division_id + Environment.NewLine);                                             // BUSINESS_DIVISION_ID
                    sb.Append("       ," + entityH.send_kbn_id + Environment.NewLine);                                                      // SEND_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityH.customer_id)) + Environment.NewLine);      // CUSTOMER_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityH.supplier_id)) + Environment.NewLine);      // SUPPLIER_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zTimeNullToDefault(entityH.supply_ymd)) + Environment.NewLine);          // SUPPLY_YMD
                    sb.Append("       ,1" + Environment.NewLine);                                                                           // DELIVER_DIVISION_ID
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_enter_number) + Environment.NewLine);                                   // SUM_ENTER_NUMBER
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_case_number) + Environment.NewLine);                                    // SUM_CASE_NUMBER
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_number) + Environment.NewLine);                                         // SUM_NUMBER
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_unit_price) + Environment.NewLine);                                     // SUM_UNIT_PRICE
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_tax) + Environment.NewLine);                                            // SUM_TAX
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_no_tax_price) + Environment.NewLine);                                   // SUM_NO_TAX_PRICE
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_price) + Environment.NewLine);                                          // SUM_PRICE
                    sb.Append("       ," + ExEscape.zRepStr(entityH.memo) + Environment.NewLine);                                           // MEMO
                    if (type == 0)
                    {
                        sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Upd,
                                                                  PG_NM,
                                                                  "T_PURCHASE_H",
                                                                  entityH.update_person_id,
                                                                  PurchaseNo.ToString(),
                                                                  ipAdress,
                                                                  userId));
                    }
                    else
                    {
                        sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                  PG_NM,
                                                                  "T_PURCHASE_H",
                                                                  entityH.update_person_id,
                                                                  "0",
                                                                  ipAdress,
                                                                  userId));
                    }

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(Head Insert)", ex);
                    return "UpdatePurchase(Head Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Detail Insert

                try
                {
                    for (int i = 0; i <= entityD.Count - 1; i++)
                    {
                        #region SQL

                        sb.Length = 0;
                        sb.Append("INSERT INTO T_PURCHASE_D " + Environment.NewLine);
                        sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                        sb.Append("       , GROUP_ID" + Environment.NewLine);
                        sb.Append("       , PURCHASE_ID" + Environment.NewLine);
                        sb.Append("       , REC_NO" + Environment.NewLine);
                        sb.Append("       , BREAKDOWN_ID" + Environment.NewLine);
                        sb.Append("       , DELIVER_DIVISION_ID" + Environment.NewLine);
                        sb.Append("       , COMMODITY_ID" + Environment.NewLine);
                        sb.Append("       , COMMODITY_NAME" + Environment.NewLine);
                        sb.Append("       , UNIT_ID" + Environment.NewLine);
                        sb.Append("       , ENTER_NUMBER" + Environment.NewLine);
                        sb.Append("       , CASE_NUMBER" + Environment.NewLine);
                        sb.Append("       , NUMBER" + Environment.NewLine);
                        sb.Append("       , UNIT_PRICE" + Environment.NewLine);
                        sb.Append("       , TAX" + Environment.NewLine);
                        sb.Append("       , NO_TAX_PRICE" + Environment.NewLine);
                        sb.Append("       , PRICE" + Environment.NewLine);
                        sb.Append("       , TAX_DIVISION_ID" + Environment.NewLine);
                        sb.Append("       , TAX_PERCENT" + Environment.NewLine);
                        sb.Append("       , PURCHASE_ORDER_ID" + Environment.NewLine);
                        sb.Append("       , MEMO" + Environment.NewLine);
                        sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                        sb.Append("       , DELETE_FLG" + Environment.NewLine);
                        sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                        sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                        sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                        sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                        sb.Append("       , CREATE_DATE" + Environment.NewLine);
                        sb.Append("       , CREATE_TIME" + Environment.NewLine);
                        sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                        sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                        sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                        sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                        sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                        sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                        sb.Append(")" + Environment.NewLine);
                        sb.Append("SELECT  " + companyId + Environment.NewLine);                                                        // COMPANY_ID
                        sb.Append("       ," + groupId + Environment.NewLine);                                                          // GROUP_ID
                        sb.Append("       ," + id.ToString() + Environment.NewLine);                                                    // PURCHASE_ID
                        sb.Append("       ," + entityD[i].rec_no + Environment.NewLine);                                                // REC_NO
                        sb.Append("       ," + entityD[i].breakdown_id + Environment.NewLine);                                          // BREAKDOWN_ID
                        sb.Append("       ," + entityD[i].deliver_division_id + Environment.NewLine);                                   // DELIVER_DIVISION_ID
                        sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityD[i].commodity_id)) + Environment.NewLine);     // COMMODITY_ID
                        sb.Append("       ," + ExEscape.zRepStr(entityD[i].commodity_name) + Environment.NewLine);                                 // COMMODITY_NAME
                        sb.Append("       ," + entityD[i].unit_id + Environment.NewLine);                                               // UNIT_ID
                        sb.Append("       ," + entityD[i].enter_number + Environment.NewLine);                                          // ENTER_NUMBER
                        sb.Append("       ," + entityD[i].case_number + Environment.NewLine);                                           // CASE_NUMBER
                        sb.Append("       ," + entityD[i].number + Environment.NewLine);                                                // NUMBER
                        sb.Append("       ," + entityD[i].unit_price + Environment.NewLine);                                            // UNIT_PRICE
                        sb.Append("       ," + entityD[i].tax + Environment.NewLine);                                                   // TAX
                        sb.Append("       ," + entityD[i].no_tax_price + Environment.NewLine);                                          // NO_TAX_PRICE
                        sb.Append("       ," + entityD[i].price + Environment.NewLine);                                                 // PRICE
                        sb.Append("       ," + entityD[i].tax_division_id + Environment.NewLine);                                       // TAX_DIVISION_ID
                        sb.Append("       ," + entityD[i].tax_percent + Environment.NewLine);                                           // TAX_PERCENT
                        sb.Append("       ," + entityD[i].purchase_order_id + Environment.NewLine);                                     // PURCHASE_ORDER_ID
                        sb.Append("       ," + ExEscape.zRepStr(entityD[i].memo) + Environment.NewLine);                                // MEMO

                        if (type == 0)
                        {
                            sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Upd,
                                                                      PG_NM,
                                                                      "T_PURCHASE_D",
                                                                      entityH.update_person_id,
                                                                      ExCast.zCStr(entityH.id),
                                                                      ipAdress,
                                                                      userId));
                        }
                        else
                        {
                            sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                      PG_NM,
                                                                      "T_PURCHASE_D",
                                                                      entityH.update_person_id,
                                                                      "0",
                                                                      ipAdress,
                                                                      userId));
                        }

                        #endregion

                        if (entityD[i].breakdown_id <= 3 && string.IsNullOrEmpty(entityD[i].commodity_id))
                        {
                            // 摘要以外で商品未選択の場合、登録しない
                        }
                        else
                        {
                            db.ExecuteSQL(sb.ToString(), false);

                            #region Update Commodity Inventory

                            try
                            {
                                if (entityD[i].inventory_management_division_id == 1)
                                {
                                    DataCommodityInventory.UpdCommodityInventory(companyId, 
                                                                                 groupId,
                                                                                 db,
                                                                                 ExCast.zNumZeroNothingFormat(entityD[i].commodity_id),
                                                                                 entityD[i].number,
                                                                                 PG_NM,
                                                                                 ExCast.zCInt(entityH.update_person_id),
                                                                                 ipAdress,
                                                                                 userId);
                                }
                            }
                            catch (Exception ex)
                            {
                                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(UpdatePurchaseCredit)", ex);
                                return "UpdatePurchase(UpdatePurchaseCredit) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                            }

                            #endregion

                            #region Update Deliver Division

                            try
                            {
                                DataDeliverDivision.UpdDeliverDivision(DataDeliverDivision.gUpdDeliverDivisionKbn.Purchase, 
                                                                       companyId,
                                                                       groupId,
                                                                       ExCast.zCLng(entityD[i].purchase_order_id),
                                                                       entityD[i].rec_no,
                                                                       db,
                                                                       entityD[i].deliver_division_id,
                                                                       PG_NM,
                                                                       ExCast.zCInt(entityH.update_person_id),
                                                                       ipAdress,
                                                                       userId);
                            }
                            catch (Exception ex)
                            {
                                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(UpdateDeliverDivisio)", ex);
                                return "UpdatePurchase(UpdateDeliverDivisio) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                            }

                            #endregion

                            #region Set Entity InOutDelivery

                            EntityInOutDeliveryD _entityInOutDeliveryD = new EntityInOutDeliveryD();
                            _entityInOutDeliveryD.rec_no = entityD[i].rec_no;
                            _entityInOutDeliveryD.commodity_id = entityD[i].commodity_id;
                            _entityInOutDeliveryD.commodity_name = entityD[i].commodity_name;
                            _entityInOutDeliveryD.unit_id = entityD[i].unit_id;
                            _entityInOutDeliveryD.enter_number = entityD[i].enter_number;
                            _entityInOutDeliveryD.case_number = entityD[i].case_number;
                            _entityInOutDeliveryD.number = entityD[i].number;
                            _entityInOutDeliveryListD.Add(_entityInOutDeliveryD);

                            #endregion

                        }
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(Detail Insert)", ex);
                    return "UpdatePurchase(Detail Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Head Update

                if (type == 0)
                {
                    try
                    {
                        sb.Length = 0;
                        sb.Append("UPDATE T_PURCHASE_H " + Environment.NewLine);
                        sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                                  entityH.update_person_id,
                                                                  ipAdress,
                                                                  userId,
                                                                  1));
                        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);        // COMPANY_ID
                        sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);            // GROUP_ID
                        sb.Append("   AND ID = " + entityH.id.ToString() + Environment.NewLine);    // ID

                        db.ExecuteSQL(sb.ToString(), false);

                    }
                    catch (Exception ex)
                    {
                        db.ExRollbackTransaction();
                        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(Head Update)", ex);
                        return "UpdatePurchase(Head Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                    }
                }

                #endregion

                #region Detail Update

                if (type == 0)
                {
                    try
                    {
                        sb.Length = 0;
                        sb.Append("UPDATE T_PURCHASE_D " + Environment.NewLine);
                        sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                                  entityH.update_person_id,
                                                                  ipAdress,
                                                                  userId,
                                                                  1));
                        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                        sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                        sb.Append("   AND PURCHASE_ID = " + entityH.id.ToString() + Environment.NewLine);

                        db.ExecuteSQL(sb.ToString(), false);

                    }
                    catch (Exception ex)
                    {
                        db.ExRollbackTransaction();
                        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(Detail Update)", ex);
                        return "UpdatePurchase(Detail Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                    }
                }

                #endregion

                #region Update InOutDelivery

                try
                {
                    svcInOutDelivery _svcInOutDelivery = new svcInOutDelivery();
                    _entityInOutDeliveryH.in_out_delivery_ymd = entityH.purchase_ymd;
                    _entityInOutDeliveryH.in_out_delivery_kbn = 1;          // 入出庫区分:入庫
                    _entityInOutDeliveryH.in_out_delivery_proc_kbn = 3;     // 入出庫処理区分:売上
                    _entityInOutDeliveryH.in_out_delivery_to_kbn = 2;       // 入出庫先区分:仕入先
                    _entityInOutDeliveryH.update_person_id = entityH.update_person_id;
                    _entityInOutDeliveryH.purchase_id = entityH.purchase_id;

                    for (int i = 0; i <= _entityInOutDeliveryListD.Count - 1; i++)
                    {
                        _entityInOutDeliveryH.sum_enter_number += _entityInOutDeliveryListD[i].enter_number;
                        _entityInOutDeliveryH.sum_case_number += _entityInOutDeliveryListD[i].case_number;
                        _entityInOutDeliveryH.sum_number += _entityInOutDeliveryListD[i].number;
                    }

                    long in_out_delivery_no = 0;
                    long in_out_delivery_id = 0;

                    if (type == 0)
                    {
                        DataSlipNo.GetInOutDeliveryNo(companyId, groupId, db, DataSlipNo.geInOutDeliverySlipKbn.Purchase, no, ref in_out_delivery_no, ref in_out_delivery_id);
                        _entityInOutDeliveryH.no = in_out_delivery_no;
                        _entityInOutDeliveryH.id = in_out_delivery_id;
                    }

                    ret_message = _svcInOutDelivery.UpdateInOutDeliveryExcExc(random, type, 3, in_out_delivery_no, no, _entityInOutDeliveryH, _entityInOutDeliveryListD, null, null);
                    if (ret_message.IndexOf("Auto Insert success : ") == -1 && !string.IsNullOrEmpty(ret_message))
                    {
                        return "UpdatePurchase(Update InOutDelivery) : " + ret_message;
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(Update InOutDelivery)", ex);
                    return "UpdatePurchase(Update InOutDelivery) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Update Slip No

                try
                {
                    if (type == 0)
                    {
                        DataSlipNo.UpdateSlipNo(companyId, groupId, db, DataSlipNo.geSlipKbn.Vendor, accountPeriod, 0, id);
                    }
                    else
                    {
                        DataSlipNo.UpdateSlipNo(companyId, groupId, db, DataSlipNo.geSlipKbn.Vendor, accountPeriod, no, id);
                    }

                    if (no == 0 || id == 0)
                    {
                        return "伝票番号の更新に失敗しました。(会計年 : " + accountPeriod + ")";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(UpdateSlipNo)", ex);
                    return "UpdatePurchase(UpdateSlipNo) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

            }

            #endregion

            #region Delete

            if (type == 2)
            {
                #region Delete InOutDelivery

                try
                {
                    svcInOutDelivery _svcInOutDelivery = new svcInOutDelivery();

                    long in_out_delivery_no = 0;
                    long in_out_delivery_id = 0;
                    DataSlipNo.GetInOutDeliveryNo(companyId, groupId, db, DataSlipNo.geInOutDeliverySlipKbn.Purchase, no, ref in_out_delivery_no, ref in_out_delivery_id);
                    _entityInOutDeliveryH.no = in_out_delivery_no;
                    _entityInOutDeliveryH.id = in_out_delivery_id;

                    ret_message = _svcInOutDelivery.UpdateInOutDeliveryExcExc(random, type, 3, in_out_delivery_no, no, _entityInOutDeliveryH, _entityInOutDeliveryListD, null, null);
                    if (ret_message.IndexOf("Auto Insert success : ") == -1 && !string.IsNullOrEmpty(ret_message))
                    {
                        return "UpdatePurchase(Delete InOutDelivery) : " + ret_message;
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(Delete InOutDelivery)", ex);
                    return "UpdatePurchase(Delete InOutDelivery) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Head Delete

                try
                {
                    sb.Length = 0;
                    sb.Append("UPDATE T_PURCHASE_H " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                              entityH.update_person_id,
                                                              ipAdress,
                                                              userId,
                                                              1));
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);        // COMPANY_ID
                    sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);            // GROUP_ID
                    sb.Append("   AND ID = " + entityH.id.ToString() + Environment.NewLine);    // ID

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(Head Delete)", ex);
                    return "UpdatePurchase(Head Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Detail Delete

                try
                {
                    sb.Length = 0;
                    sb.Append("UPDATE T_PURCHASE_D " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                              entityH.update_person_id,
                                                              ipAdress,
                                                              userId,
                                                              1));
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                    sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                    sb.Append("   AND PURCHASE_ID = " + entityH.id.ToString() + Environment.NewLine);

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(Detail Delete)", ex);
                    return "UpdatePurchase(Detail Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion
            }

            #endregion

            #region Update Payment Credit

            try
            {
                double _price = 0;
                switch (type)
                {
                    case 0:         // Update
                        // 前回「掛仕入」の場合
                        if (before_entityH.business_division_id == 1)
                        {
                            _price = (before_entityH.sum_no_tax_price + before_entityH.sum_tax) * -1;
                            DataCredit.UpdPaymentCredit(companyId, groupId, db, before_entityH.purchase_id, _price, PG_NM, ExCast.zCInt(entityH.update_person_id), ipAdress, userId);
                        }
                        // 今回「掛仕入」の場合
                        if (entityH.business_division_id == 1)
                        {
                            _price = entityH.sum_no_tax_price + entityH.sum_tax;
                            DataCredit.UpdPaymentCredit(companyId, groupId, db, entityH.purchase_id, _price, PG_NM, ExCast.zCInt(entityH.update_person_id), ipAdress, userId);
                        }
                        break;
                    case 1:         // Insert
                        // 今回「掛仕入」、または、「都度請求」の場合
                        if (entityH.business_division_id == 1)
                        {
                            _price = entityH.sum_no_tax_price + entityH.sum_tax;
                            DataCredit.UpdPaymentCredit(companyId, groupId, db, entityH.purchase_id, _price, PG_NM, ExCast.zCInt(entityH.update_person_id), ipAdress, userId);
                        }
                        break;
                    case 2:         // Delete
                        // 前回「掛仕入」の場合
                        if (before_entityH.business_division_id == 1)
                        {
                            _price = (before_entityH.sum_no_tax_price + before_entityH.sum_tax) * -1;
                            DataCredit.UpdPaymentCredit(companyId, groupId, db, before_entityH.purchase_id, _price, PG_NM, ExCast.zCInt(entityH.update_person_id), ipAdress, userId);
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(UpdatePurchaseCredit)", ex);
                return "UpdatePurchase(UpdatePurchaseCredit) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Update Commodity Inventory

            try
            {
                // Update or Delete
                if (type == 0 || type == 2)
                {

                    for (int i = 0; i <= before_entityD.Count - 1; i++)
                    {
                        if (before_entityD[i].inventory_management_division_id == 1)
                        {
                            DataCommodityInventory.UpdCommodityInventory(companyId,
                                                                         groupId,
                                                                         db,
                                                                         ExCast.zNumZeroNothingFormat(before_entityD[i].commodity_id),
                                                                         before_entityD[i].number * -1,
                                                                         PG_NM,
                                                                         ExCast.zCInt(entityH.update_person_id),
                                                                         ipAdress,
                                                                         userId);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(UpdateCommodityInventory)", ex);
                return "UpdatePurchase(UpdateCommodityInventory) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Update Deliver Division

            try
            {
                // Delete
                if (type == 2)
                {
                    for (int i = 0; i <= before_entityD.Count - 1; i++)
                    {
                        // 未納に戻す
                        DataDeliverDivision.UpdDeliverDivision(DataDeliverDivision.gUpdDeliverDivisionKbn.Purchase,
                                                               companyId,
                                                               groupId,
                                                               ExCast.zCLng(before_entityD[i].purchase_order_id),
                                                               before_entityD[i].rec_no,
                                                               db,
                                                               1,
                                                               PG_NM,
                                                               ExCast.zCInt(entityH.update_person_id),
                                                               ipAdress,
                                                               userId);
                    }
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(UpdateDeliverDivisio)", ex);
                return "UpdatePurchase(UpdateDeliverDivisio) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(CommitTransaction)", ex);
                return "UpdatePurchase(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(DbClose)", ex);
                return "UpdatePurchase(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                switch (type)
                {
                    case 0:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.Purchase.PurchaseInp,
                                                   DataPgEvidence.geOperationType.Update,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    case 1:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.Purchase.PurchaseInp,
                                                   DataPgEvidence.geOperationType.Insert,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    case 2:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.Purchase.PurchaseInp,
                                                   DataPgEvidence.geOperationType.Delete,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdatePurchase(Add Evidence)", ex);
                return "UpdatePurchase(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Return

            if (type == 1 && PurchaseNo == 0)
            {
                return "Auto Insert success : " + "仕入番号 : " + no.ToString().ToString() + "で登録しました。";
            }
            else
            {
                return "";
            }

            #endregion

        }
        public string UpdateSales(string random, int type, long SalesNo, EntitySalesH entityH, List<EntitySalesD> entityD, EntitySalesH before_entityH, List<EntitySalesD> before_entityD)
        {

            #region 認証処理

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

            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]); 

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

            #endregion

            #region Field

            StringBuilder sb = new StringBuilder();
            DataTable dt;
            ExMySQLData db = null;

            string accountPeriod = "";
            long id = 0;
            long no = SalesNo;
            string ret_message = "";

            EntityInOutDeliveryH _entityInOutDeliveryH = new EntityInOutDeliveryH();
            List<EntityInOutDeliveryD> _entityInOutDeliveryListD = new List<EntityInOutDeliveryD>();

            #endregion

            #region Databese Open

            try
            {
                db = new ExMySQLData(ExCast.zCStr(HttpContext.Current.Session[ExSession.DB_CONNECTION_STR]));
                db.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(DbOpen)", ex);
                return "UpdateSales(DbOpen) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Invoice Close Check

            try
            {
                if (type <= 1)
                {
                    // 掛売上
                    if (entityH.business_division_id == 1)
                    {
                        // 請求締切済チェック
                        if (DataClose.IsInvoiceClose(companyId, db, entityH.invoice_id, entityH.sales_ymd))
                        {
                            db.DbClose();
                            return "売上日 : " + entityH.sales_ymd + " は請求締切済の為、計上できません。";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(Invoice Close Check)", ex);
                return "UpdateSales(Invoice Close Check) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region BeginTransaction

            try
            {
                db.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(BeginTransaction)", ex);
                return "UpdateSales(BeginTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Update or Insert

            if (type <= 1)
            {

                #region Get Accout Period

                try
                {
                    accountPeriod = DataAccount.GetAccountPeriod(ExCast.zCStr(HttpContext.Current.Session[ExSession.ACCOUNT_BEGIN_PERIOD]), entityH.sales_ymd);
                    if (accountPeriod == "")
                    {
                        return "会計年の取得に失敗しました。(期首月日 : " + ExCast.zCStr(HttpContext.Current.Session[ExSession.ACCOUNT_BEGIN_PERIOD]) +
                                                             " 売上日 : " + entityH.sales_ymd + ")";
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(GetAccountPeriod)", ex);
                    return "UpdateSales(GetAccountPeriod) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Get Slip No

                try
                {
                    DataSlipNo.GetSlipNo(companyId,
                                         groupId,
                                         db,
                                         DataSlipNo.geSlipKbn.Sales,
                                         accountPeriod,
                                         ref no,
                                         out id,
                                         ExCast.zCStr(HttpContext.Current.Session[ExSession.IP_ADRESS]),
                                         ExCast.zCStr(HttpContext.Current.Session[ExSession.USER_ID]));
                    if (no == 0 || id == 0)
                    {
                        db.ExRollbackTransaction();
                        return "伝票番号の取得に失敗しました。(会計年 : " + accountPeriod + ")";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(GetSlipNo)", ex);
                    db.ExRollbackTransaction();
                    return "UpdateSales(GetSlipNo) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Set Invoice Data

                long set_invoice = entityH.invoice_no;
                bool max_invoice = false;

                // 追加で取引区分が都度請求
                if (type == 1 && entityH.business_division_id == 3)
                {
                    max_invoice = true;
                }
                // 更新で取引区分が都度請求で請求番号が未設定
                else if (type == 0 && entityH.business_division_id == 3 && entityH.invoice_no == 0)
                {
                    max_invoice = true;
                }

                #region Get Max Invoice No

                int get_cnt = 0;
                long invoice_no = 0;

                if (max_invoice == true)
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        try
                        {
                            invoice_no = DataSlipNo.GetMaxSlipNo(companyId, groupId, db, "T_INVOICE", "NO");
                            break;
                        }
                        catch (Exception ex)
                        {
                            System.Threading.Thread.Sleep(100);
                        }
                    }
                }

                if (max_invoice == true && invoice_no == 0)
                {
                    db.ExRollbackTransaction();
                    return "請求書番号の取得に失敗しました。";
                }

                #endregion

                #region Invoice Data Insert

                if (max_invoice == true)
                {
                    #region 回収予定日設定

                    string _collect_plan_day = "";
                    int _collect_day = 0;
                    DataPlanDay.GetPlanDay(entityH.collect_cycle_id,
                                                         entityH.collect_day,
                                                         entityH.sales_ymd,
                                                         ref _collect_plan_day,
                                                         ref _collect_day);



                    #endregion

                    #region Insert SQL

                    sb.Length = 0;
                    sb.Append("INSERT INTO T_INVOICE " + Environment.NewLine);
                    sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                    sb.Append("       , GROUP_ID" + Environment.NewLine);
                    sb.Append("       , NO" + Environment.NewLine);
                    sb.Append("       , INVOICE_ID" + Environment.NewLine);
                    sb.Append("       , INVOICE_YYYYMMDD" + Environment.NewLine);
                    sb.Append("       , SUMMING_UP_GROUP_ID" + Environment.NewLine);
                    //sb.Append("       , BEFORE_INVOICE_YYYYMMDD" + Environment.NewLine);
                    sb.Append("       , PERSON_ID" + Environment.NewLine);
                    sb.Append("       , COLLECT_CYCLE_ID" + Environment.NewLine);
                    sb.Append("       , COLLECT_DAY" + Environment.NewLine);
                    sb.Append("       , COLLECT_PLAN_DAY" + Environment.NewLine);
                    sb.Append("       , BEFORE_INVOICE_PRICE" + Environment.NewLine);
                    sb.Append("       , RECEIPT_PRICE" + Environment.NewLine);
                    sb.Append("       , TRANSFER_PRICE" + Environment.NewLine);
                    sb.Append("       , SALES_PRICE" + Environment.NewLine);
                    sb.Append("       , NO_TAX_SALES_PRICE" + Environment.NewLine);
                    sb.Append("       , TAX" + Environment.NewLine);
                    sb.Append("       , INVOICE_PRICE" + Environment.NewLine);
                    sb.Append("       , INVOICE_KBN" + Environment.NewLine);
                    sb.Append("       , MEMO" + Environment.NewLine);
                    sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                    sb.Append("       , DELETE_FLG" + Environment.NewLine);
                    sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_DATE" + Environment.NewLine);
                    sb.Append("       , CREATE_TIME" + Environment.NewLine);
                    sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                    sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                    sb.Append(")" + Environment.NewLine);
                    sb.Append("SELECT  " + companyId + Environment.NewLine);                                                                // COMPANY_ID
                    sb.Append("       ," + groupId + Environment.NewLine);                                                                  // GROUP_ID
                    sb.Append("       ," + invoice_no + Environment.NewLine);                                                               // NO
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityH.invoice_id)) + Environment.NewLine);       // INVOICE_ID
                    sb.Append("       ," + ExEscape.zRepStr(entityH.sales_ymd) + Environment.NewLine);                                      // INVOICE_YYYYMMDD
                    sb.Append("       ,''" + Environment.NewLine);                                                                          // SUMMING_UP_DAY
                    //sb.Append("       ,'0000/00/00'" + Environment.NewLine);                                                                // BEFORE_INVOICE_YYYYMMDD
                    sb.Append("       ," + entityH.update_person_id + Environment.NewLine);                                                 // PERSON_ID
                    sb.Append("       ," + entityH.collect_cycle_id + Environment.NewLine);                                                 // COLLECT_CYCLE_ID
                    sb.Append("       ," + _collect_day + Environment.NewLine);                                                             // COLLECT_DAY
                    sb.Append("       ," + ExEscape.zRepStr(_collect_plan_day) + Environment.NewLine);                                      // COLLECT_PLAN_DAY
                    sb.Append("       ,0" + Environment.NewLine);                                                                           // BEFORE_INVOICE_PRICE
                    sb.Append("       ,0" + Environment.NewLine);                                                                           // RECEIPT_PRICE
                    sb.Append("       ,0" + Environment.NewLine);                                                                           // TRANSFER_PRICE
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_price) + Environment.NewLine);                                          // SALES_PRICE
                    sb.Append("       ," + entityH.sum_no_tax_price + Environment.NewLine);                                                 // NO_TAX_SALES_PRICE
                    sb.Append("       ," + entityH.sum_tax + Environment.NewLine);                                                          // TAX
                    sb.Append("       ," + (ExCast.zCDbl(entityH.sum_no_tax_price) + ExCast.zCDbl(entityH.sum_tax)) + Environment.NewLine); // INVOICE_PRICE
                    sb.Append("       ,1" + Environment.NewLine);                                                                           // INVOICE_KBN
                    sb.Append("       ," + ExEscape.zRepStr(entityH.memo) + Environment.NewLine);                                           // MEMO
                    sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                              PG_NM,
                                                              "T_INVOICE",
                                                              ExCast.zCInt(entityH.update_person_id),
                                                              "",
                                                              ipAdress,
                                                              userId));

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                    set_invoice = invoice_no;
                }

                #endregion

                #region Invoice Data Delete

                // 更新で取引区分が都度請求以外で請求番号が設定(都度請求から都度請求以外に更新)
                if (type == 0 && entityH.business_division_id != 3 && entityH.invoice_no != 0)
                {
                    #region Delete SQL

                    sb.Length = 0;
                    sb.Append("DELETE FROM T_INVOICE " + Environment.NewLine);
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                    sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                    sb.Append("   AND INVOICE_KBN = 0 " + Environment.NewLine);       // 都度請求分のみ
                    sb.Append("   AND NO = " + entityH.invoice_no + Environment.NewLine);

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                    set_invoice = 0;
                }

                #endregion

                #endregion

                #region Head Insert

                try
                {
                    #region SQL

                    sb.Length = 0;
                    sb.Append("INSERT INTO T_SALES_H " + Environment.NewLine);
                    sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                    sb.Append("       , GROUP_ID" + Environment.NewLine);
                    sb.Append("       , ID" + Environment.NewLine);
                    sb.Append("       , NO" + Environment.NewLine);
                    sb.Append("       , RED_BEFORE_NO" + Environment.NewLine);
                    sb.Append("       , ORDER_NO" + Environment.NewLine);
                    sb.Append("       , ESTIMATENO" + Environment.NewLine);
                    sb.Append("       , SALES_YMD" + Environment.NewLine);
                    sb.Append("       , PERSON_ID" + Environment.NewLine);
                    sb.Append("       , CUSTOMER_ID" + Environment.NewLine);
                    sb.Append("       , INVOICE_ID" + Environment.NewLine);
                    sb.Append("       , TAX_CHANGE_ID" + Environment.NewLine);
                    sb.Append("       , BUSINESS_DIVISION_ID" + Environment.NewLine);
                    sb.Append("       , SUPPLIER_ID" + Environment.NewLine);
                    sb.Append("       , SUPPLY_YMD" + Environment.NewLine);
                    sb.Append("       , DELIVER_DIVISION_ID" + Environment.NewLine);
                    sb.Append("       , SUM_ENTER_NUMBER" + Environment.NewLine);
                    sb.Append("       , SUM_CASE_NUMBER" + Environment.NewLine);
                    sb.Append("       , SUM_NUMBER" + Environment.NewLine);
                    sb.Append("       , SUM_UNIT_PRICE" + Environment.NewLine);
                    sb.Append("       , SUM_SALES_COST" + Environment.NewLine);
                    sb.Append("       , SUM_TAX" + Environment.NewLine);
                    sb.Append("       , SUM_NO_TAX_PRICE" + Environment.NewLine);
                    sb.Append("       , SUM_PRICE" + Environment.NewLine);
                    sb.Append("       , SUM_PROFITS" + Environment.NewLine);
                    sb.Append("       , PROFITS_PERCENT" + Environment.NewLine);
                    sb.Append("       , INVOICE_NO" + Environment.NewLine);
                    sb.Append("       , MEMO" + Environment.NewLine);
                    sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                    sb.Append("       , DELETE_FLG" + Environment.NewLine);
                    sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , CREATE_DATE" + Environment.NewLine);
                    sb.Append("       , CREATE_TIME" + Environment.NewLine);
                    sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                    sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                    sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                    sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                    sb.Append(")" + Environment.NewLine);
                    sb.Append("SELECT  " + companyId + Environment.NewLine);                                                    // COMPANY_ID
                    sb.Append("       ," + groupId + Environment.NewLine);                                                      // GROUP_ID
                    sb.Append("       ," + id.ToString() + Environment.NewLine);                                                // ID
                    sb.Append("       ," + no.ToString() + Environment.NewLine);                                                // NO
                    sb.Append("       ," + entityH.red_before_no + Environment.NewLine);                                        // RED_BEFORE_NO
                    sb.Append("       ," + entityH.order_no + Environment.NewLine);                                             // ESTIMATENO
                    sb.Append("       ," + entityH.estimateno + Environment.NewLine);                                           // ESTIMATENO
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zDateNullToDefault(entityH.sales_ymd)) + Environment.NewLine);           // Sales_YMD
                    sb.Append("       ," + entityH.update_person_id + Environment.NewLine);                                                 // PERSON_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityH.customer_id)) + Environment.NewLine);      // CUSTOMER_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityH.invoice_id)) + Environment.NewLine);       // INVOICE_ID
                    sb.Append("       ," + entityH.tax_change_id + Environment.NewLine);                                                    // TAX_CHANGE_ID
                    sb.Append("       ," + entityH.business_division_id + Environment.NewLine);                                             // BUSINESS_DIVISION_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityH.supplier_id)) + Environment.NewLine);      // SUPPLIER_ID
                    sb.Append("       ," + ExEscape.zRepStr(ExCast.zTimeNullToDefault(entityH.supply_ymd)) + Environment.NewLine);          // SUPPLY_YMD
                    sb.Append("       ,1" + Environment.NewLine);                                                               // DELIVER_DIVISION_ID
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_enter_number) + Environment.NewLine);                       // SUM_ENTER_NUMBER
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_case_number) + Environment.NewLine);                        // SUM_CASE_NUMBER
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_number) + Environment.NewLine);                             // SUM_NUMBER
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_unit_price) + Environment.NewLine);                         // SUM_UNIT_PRICE
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_sales_cost) + Environment.NewLine);                         // SUM_SALES_COST
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_tax) + Environment.NewLine);                                // SUM_TAX
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_no_tax_price) + Environment.NewLine);                       // SUM_NO_TAX_PRICE
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_price) + Environment.NewLine);                              // SUM_PRICE
                    sb.Append("       ," + ExCast.zCDbl(entityH.sum_profits) + Environment.NewLine);                            // SUM_PROFITS
                    sb.Append("       ," + ExCast.zCDbl(entityH.profits_percent) + Environment.NewLine);                        // PROFITS_PERCENT
                    sb.Append("       ," + set_invoice + Environment.NewLine);                                                  // INVOICE_NO
                    sb.Append("       ," + ExEscape.zRepStr(entityH.memo) + Environment.NewLine);                               // MEMO
                    if (type == 0)
                    {
                        sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Upd,
                                                                  PG_NM,
                                                                  "T_SALES_H",
                                                                  entityH.update_person_id,
                                                                  SalesNo.ToString(),
                                                                  ipAdress,
                                                                  userId));
                    }
                    else
                    {
                        sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                  PG_NM,
                                                                  "T_SALES_H",
                                                                  entityH.update_person_id,
                                                                  "0",
                                                                  ipAdress,
                                                                  userId));
                    }

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(Head Insert)", ex);
                    return "UpdateSales(Head Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Detail Insert

                try
                {
                    for (int i = 0; i <= entityD.Count - 1; i++)
                    {
                        #region SQL

                        sb.Length = 0;
                        sb.Append("INSERT INTO T_SALES_D " + Environment.NewLine);
                        sb.Append("       ( COMPANY_ID" + Environment.NewLine);
                        sb.Append("       , GROUP_ID" + Environment.NewLine);
                        sb.Append("       , SALES_ID" + Environment.NewLine);
                        sb.Append("       , REC_NO" + Environment.NewLine);
                        sb.Append("       , BREAKDOWN_ID" + Environment.NewLine);
                        sb.Append("       , DELIVER_DIVISION_ID" + Environment.NewLine);
                        sb.Append("       , COMMODITY_ID" + Environment.NewLine);
                        sb.Append("       , COMMODITY_NAME" + Environment.NewLine);
                        sb.Append("       , UNIT_ID" + Environment.NewLine);
                        sb.Append("       , ENTER_NUMBER" + Environment.NewLine);
                        sb.Append("       , CASE_NUMBER" + Environment.NewLine);
                        sb.Append("       , NUMBER" + Environment.NewLine);
                        sb.Append("       , UNIT_PRICE" + Environment.NewLine);
                        sb.Append("       , SALES_COST" + Environment.NewLine);
                        sb.Append("       , TAX" + Environment.NewLine);
                        sb.Append("       , NO_TAX_PRICE" + Environment.NewLine);
                        sb.Append("       , PRICE" + Environment.NewLine);
                        sb.Append("       , PROFITS" + Environment.NewLine);
                        sb.Append("       , PROFITS_PERCENT" + Environment.NewLine);
                        sb.Append("       , TAX_DIVISION_ID" + Environment.NewLine);
                        sb.Append("       , TAX_PERCENT" + Environment.NewLine);
                        sb.Append("       , ORDER_ID" + Environment.NewLine);
                        sb.Append("       , MEMO" + Environment.NewLine);
                        sb.Append("       , UPDATE_FLG" + Environment.NewLine);
                        sb.Append("       , DELETE_FLG" + Environment.NewLine);
                        sb.Append("       , CREATE_PG_ID" + Environment.NewLine);
                        sb.Append("       , CREATE_ADRESS" + Environment.NewLine);
                        sb.Append("       , CREATE_USER_ID" + Environment.NewLine);
                        sb.Append("       , CREATE_PERSON_ID" + Environment.NewLine);
                        sb.Append("       , CREATE_DATE" + Environment.NewLine);
                        sb.Append("       , CREATE_TIME" + Environment.NewLine);
                        sb.Append("       , UPDATE_PG_ID" + Environment.NewLine);
                        sb.Append("       , UPDATE_ADRESS" + Environment.NewLine);
                        sb.Append("       , UPDATE_USER_ID" + Environment.NewLine);
                        sb.Append("       , UPDATE_PERSON_ID" + Environment.NewLine);
                        sb.Append("       , UPDATE_DATE" + Environment.NewLine);
                        sb.Append("       , UPDATE_TIME" + Environment.NewLine);
                        sb.Append(")" + Environment.NewLine);
                        sb.Append("SELECT  " + companyId + Environment.NewLine);                                                        // COMPANY_ID
                        sb.Append("       ," + groupId + Environment.NewLine);                                                          // GROUP_ID
                        sb.Append("       ," + id.ToString() + Environment.NewLine);                                                    // Sales_ID
                        sb.Append("       ," + entityD[i].rec_no + Environment.NewLine);                                                // REC_NO
                        sb.Append("       ," + entityD[i].breakdown_id + Environment.NewLine);                                          // BREAKDOWN_ID
                        sb.Append("       ," + entityD[i].deliver_division_id + Environment.NewLine);                                   // DELIVER_DIVISION_ID
                        sb.Append("       ," + ExEscape.zRepStr(ExCast.zNumZeroNothingFormat(entityD[i].commodity_id)) + Environment.NewLine);     // COMMODITY_ID
                        sb.Append("       ," + ExEscape.zRepStr(entityD[i].commodity_name) + Environment.NewLine);                                 // COMMODITY_NAME
                        sb.Append("       ," + entityD[i].unit_id + Environment.NewLine);                                               // UNIT_ID
                        sb.Append("       ," + entityD[i].enter_number + Environment.NewLine);                                          // ENTER_NUMBER
                        sb.Append("       ," + entityD[i].case_number + Environment.NewLine);                                           // CASE_NUMBER
                        sb.Append("       ," + entityD[i].number + Environment.NewLine);                                                // NUMBER
                        sb.Append("       ," + entityD[i].unit_price + Environment.NewLine);                                            // UNIT_PRICE
                        sb.Append("       ," + entityD[i].sales_cost + Environment.NewLine);                                            // SALES_COST
                        sb.Append("       ," + entityD[i].tax + Environment.NewLine);                                                   // TAX
                        sb.Append("       ," + entityD[i].no_tax_price + Environment.NewLine);                                          // NO_TAX_PRICE
                        sb.Append("       ," + entityD[i].price + Environment.NewLine);                                                 // PRICE
                        sb.Append("       ," + entityD[i].profits + Environment.NewLine);                                               // PROFITS
                        sb.Append("       ," + ExCast.zCDbl(entityD[i].profits_percent) + Environment.NewLine);                         // PROFITS_PERCENT
                        sb.Append("       ," + entityD[i].tax_division_id + Environment.NewLine);                                       // TAX_DIVISION_ID
                        sb.Append("       ," + entityD[i].tax_percent + Environment.NewLine);                                           // TAX_PERCENT
                        sb.Append("       ," + entityD[i].order_id + Environment.NewLine);                                              // ORDER_ID
                        sb.Append("       ," + ExEscape.zRepStr(entityD[i].memo) + Environment.NewLine);                                           // MEMO

                        if (type == 0)
                        {
                            sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Upd,
                                                                      PG_NM,
                                                                      "T_SALES_D",
                                                                      entityH.update_person_id,
                                                                      ExCast.zCStr(entityH.id),
                                                                      ipAdress,
                                                                      userId));
                        }
                        else
                        {
                            sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins,
                                                                      PG_NM,
                                                                      "T_SALES_D",
                                                                      entityH.update_person_id,
                                                                      "0",
                                                                      ipAdress,
                                                                      userId));
                        }
                        
                        #endregion

                        if (entityD[i].breakdown_id <= 3 && string.IsNullOrEmpty(entityD[i].commodity_id))
                        {
                            // 摘要以外で商品未選択の場合、登録しない
                        }
                        else
                        {
                            db.ExecuteSQL(sb.ToString(), false);

                            #region Update Commodity Inventory

                            try
                            {
                                if (entityD[i].inventory_management_division_id == 1)
                                {
                                    DataCommodityInventory.UpdCommodityInventory(companyId,
                                                                                 groupId,
                                                                                 db,
                                                                                 ExCast.zNumZeroNothingFormat(entityD[i].commodity_id),
                                                                                 entityD[i].number * -1,
                                                                                 PG_NM,
                                                                                 ExCast.zCInt(entityH.update_person_id),
                                                                                 ipAdress,
                                                                                 userId);
                                }
                            }
                            catch (Exception ex)
                            {
                                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(Update Commodity Inventory)", ex);
                                return "UpdateSales(Update Commodity Inventory) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                            }

                            #endregion

                            #region Update Deliver Division

                            try
                            {
                                DataDeliverDivision.UpdDeliverDivision(DataDeliverDivision.gUpdDeliverDivisionKbn.Order, 
                                                                       companyId,
                                                                       groupId,
                                                                       ExCast.zCLng(entityD[i].order_id),
                                                                       entityD[i].rec_no,
                                                                       db,
                                                                       entityD[i].deliver_division_id,
                                                                       PG_NM,
                                                                       ExCast.zCInt(entityH.update_person_id),
                                                                       ipAdress,
                                                                       userId);
                            }
                            catch (Exception ex)
                            {
                                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(UpdateDeliverDivisio)", ex);
                                return "UpdateSales(UpdateDeliverDivisio) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                            }

                            #endregion

                            #region Set Entity InOutDelivery

                            EntityInOutDeliveryD _entityInOutDeliveryD = new EntityInOutDeliveryD();
                            _entityInOutDeliveryD.rec_no = entityD[i].rec_no;
                            _entityInOutDeliveryD.commodity_id = entityD[i].commodity_id;
                            _entityInOutDeliveryD.commodity_name = entityD[i].commodity_name;
                            _entityInOutDeliveryD.unit_id = entityD[i].unit_id;
                            _entityInOutDeliveryD.enter_number = entityD[i].enter_number;
                            _entityInOutDeliveryD.case_number = entityD[i].case_number;
                            _entityInOutDeliveryD.number = entityD[i].number;
                            _entityInOutDeliveryListD.Add(_entityInOutDeliveryD);

                            #endregion
                        }
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(Detail Insert)", ex);
                    return "UpdateSales(Detail Insert) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Head Update

                if (type == 0)
                {
                    try
                    {
                        sb.Length = 0;
                        sb.Append("UPDATE T_SALES_H " + Environment.NewLine);
                        sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                                  entityH.update_person_id,
                                                                  ipAdress,
                                                                  userId,
                                                                  1));
                        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);        // COMPANY_ID
                        sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);            // GROUP_ID
                        sb.Append("   AND ID = " + entityH.id.ToString() + Environment.NewLine);    // ID

                        db.ExecuteSQL(sb.ToString(), false);

                    }
                    catch (Exception ex)
                    {
                        db.ExRollbackTransaction();
                        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(Head Update)", ex);
                        return "UpdateSales(Head Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                    }
                }

                #endregion

                #region Detail Update

                if (type == 0)
                {
                    try
                    {
                        sb.Length = 0;
                        sb.Append("UPDATE T_SALES_D " + Environment.NewLine);
                        sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                                  entityH.update_person_id,
                                                                  ipAdress,
                                                                  userId,
                                                                  1));
                        sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                        sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                        sb.Append("   AND Sales_ID = " + entityH.id.ToString() + Environment.NewLine);

                        db.ExecuteSQL(sb.ToString(), false);

                    }
                    catch (Exception ex)
                    {
                        db.ExRollbackTransaction();
                        CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(Detail Update)", ex);
                        return "UpdateSales(Detail Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                    }
                }

                #endregion

                #region Update InOutDelivery

                try
                {
                    svcInOutDelivery _svcInOutDelivery = new svcInOutDelivery();
                    _entityInOutDeliveryH.in_out_delivery_ymd = entityH.sales_ymd;
                    _entityInOutDeliveryH.in_out_delivery_kbn = 2;          // 入出庫区分:出庫
                    _entityInOutDeliveryH.in_out_delivery_proc_kbn = 2;     // 入出庫処理区分:売上
                    _entityInOutDeliveryH.in_out_delivery_to_kbn = 2;       // 入出庫先区分:得意先
                    _entityInOutDeliveryH.update_person_id = entityH.update_person_id;
                    _entityInOutDeliveryH.customer_id = entityH.customer_id;

                    for (int i = 0; i <= _entityInOutDeliveryListD.Count - 1; i++)
                    {
                        _entityInOutDeliveryH.sum_enter_number += _entityInOutDeliveryListD[i].enter_number;
                        _entityInOutDeliveryH.sum_case_number += _entityInOutDeliveryListD[i].case_number;
                        _entityInOutDeliveryH.sum_number += _entityInOutDeliveryListD[i].number;
                    }

                    long in_out_delivery_no = 0;
                    long in_out_delivery_id = 0;

                    if (type == 0)
                    {
                        DataSlipNo.GetInOutDeliveryNo(companyId, groupId, db, DataSlipNo.geInOutDeliverySlipKbn.Sales, no, ref in_out_delivery_no, ref in_out_delivery_id);
                        _entityInOutDeliveryH.no = in_out_delivery_no;
                        _entityInOutDeliveryH.id = in_out_delivery_id;
                    }

                    ret_message = _svcInOutDelivery.UpdateInOutDeliveryExcExc(random, type, 2, in_out_delivery_no, no, _entityInOutDeliveryH, _entityInOutDeliveryListD, null, null);
                    if (ret_message.IndexOf("Auto Insert success : ") == -1 && !string.IsNullOrEmpty(ret_message))
                    {
                        return "UpdateSales(Update InOutDelivery) : " + ret_message;
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(Update InOutDelivery)", ex);
                    return "UpdateSales(Update InOutDelivery) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Update Slip No

                try
                {
                    if (type == 0)
                    {
                        DataSlipNo.UpdateSlipNo(companyId, groupId, db, DataSlipNo.geSlipKbn.Sales, accountPeriod, 0, id);
                    }
                    else
                    {
                        DataSlipNo.UpdateSlipNo(companyId, groupId, db, DataSlipNo.geSlipKbn.Sales, accountPeriod, no, id);
                    }

                    if (no == 0 || id == 0)
                    {
                        return "伝票番号の更新に失敗しました。(会計年 : " + accountPeriod + ")";
                    }
                }
                catch (Exception ex)
                {
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(UpdateSlipNo)", ex);
                    return "UpdateSales(UpdateSlipNo) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

            }

            #endregion

            #region Delete

            if (type == 2)
            {
                #region Invoice Data Delete

                // 請求番号が設定時
                if (entityH.invoice_no != 0)
                {
                    #region Delete SQL

                    sb.Length = 0;
                    sb.Append("DELETE FROM T_INVOICE " + Environment.NewLine);
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                    sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                    sb.Append("   AND INVOICE_KBN = 1 " + Environment.NewLine);       // 締請求分のみ
                    sb.Append("   AND NO = " + entityH.invoice_no + Environment.NewLine);

                    #endregion

                    db.ExecuteSQL(sb.ToString(), false);
                }

                #endregion

                #region Delete InOutDelivery

                try
                {
                    svcInOutDelivery _svcInOutDelivery = new svcInOutDelivery();

                    long in_out_delivery_no = 0;
                    long in_out_delivery_id = 0;
                    DataSlipNo.GetInOutDeliveryNo(companyId, groupId, db, DataSlipNo.geInOutDeliverySlipKbn.Sales, no, ref in_out_delivery_no, ref in_out_delivery_id);
                    _entityInOutDeliveryH.no = in_out_delivery_no;
                    _entityInOutDeliveryH.id = in_out_delivery_id;

                    ret_message = _svcInOutDelivery.UpdateInOutDeliveryExcExc(random, type, 2, in_out_delivery_no, no, _entityInOutDeliveryH, _entityInOutDeliveryListD, null, null);
                    if (ret_message.IndexOf("Auto Insert success : ") == -1 && !string.IsNullOrEmpty(ret_message))
                    {
                        return "UpdateSales(Delete InOutDelivery) : " + ret_message;
                    }
                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(Delete InOutDelivery)", ex);
                    return "UpdateSales(Delete InOutDelivery) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Head Delete

                try
                {
                    sb.Length = 0;
                    sb.Append("UPDATE T_SALES_H " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                              entityH.update_person_id,
                                                              ipAdress,
                                                              userId,
                                                              1));
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);        // COMPANY_ID
                    sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);            // GROUP_ID
                    sb.Append("   AND ID = " + entityH.id.ToString() + Environment.NewLine);    // ID

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(Head Delete)", ex);
                    return "UpdateSales(Head Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion

                #region Detail Delete

                try
                {
                    sb.Length = 0;
                    sb.Append("UPDATE T_SALES_D " + Environment.NewLine);
                    sb.Append(CommonUtl.GetUpdSQLCommonColums(PG_NM,
                                                              entityH.update_person_id,
                                                              ipAdress,
                                                              userId,
                                                              1));
                    sb.Append(" WHERE COMPANY_ID = " + companyId + Environment.NewLine);
                    sb.Append("   AND GROUP_ID = " + groupId + Environment.NewLine);
                    sb.Append("   AND Sales_ID = " + entityH.id.ToString() + Environment.NewLine);

                    db.ExecuteSQL(sb.ToString(), false);

                }
                catch (Exception ex)
                {
                    db.ExRollbackTransaction();
                    CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(Detail Delete)", ex);
                    return "UpdateSales(Detail Update) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
                }

                #endregion
            }

            #endregion

            #region Update Sales Credit

            try
            {
                double _price = 0;
                switch (type)
                {
                    case 0:         // Update
                        // 前回「掛売上」、または、「都度請求」の場合
                        if (before_entityH.business_division_id == 1 || before_entityH.business_division_id == 3)
                        {
                            _price = (before_entityH.sum_no_tax_price + before_entityH.sum_tax) * -1;
                            DataCredit.UpdSalesCredit(companyId, groupId, db, before_entityH.invoice_id, _price, PG_NM, ExCast.zCInt(entityH.update_person_id), ipAdress, userId);
                        }
                        // 今回「掛売上」、または、「都度請求」の場合
                        if (entityH.business_division_id == 1 || entityH.business_division_id == 3)
                        {
                            _price = entityH.sum_no_tax_price + entityH.sum_tax;
                            DataCredit.UpdSalesCredit(companyId, groupId, db, entityH.invoice_id, _price, PG_NM, ExCast.zCInt(entityH.update_person_id), ipAdress, userId);
                        }
                        break;
                    case 1:         // Insert
                        // 今回「掛売上」、または、「都度請求」の場合
                        if (entityH.business_division_id == 1 || entityH.business_division_id == 3)
                        {
                            _price = entityH.sum_no_tax_price + entityH.sum_tax;
                            DataCredit.UpdSalesCredit(companyId, groupId, db, entityH.invoice_id, _price, PG_NM, ExCast.zCInt(entityH.update_person_id), ipAdress, userId);
                        }
                        break;
                    case 2:         // Delete
                        // 前回「掛売上」、または、「都度請求」の場合
                        if (before_entityH.business_division_id == 1 || before_entityH.business_division_id == 3)
                        {
                            _price = (before_entityH.sum_no_tax_price + before_entityH.sum_tax) * -1;
                            DataCredit.UpdSalesCredit(companyId, groupId, db, before_entityH.invoice_id, _price, PG_NM, ExCast.zCInt(entityH.update_person_id), ipAdress, userId);
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(UpdateSalesCredit)", ex);
                return "UpdateSales(UpdateSalesCredit) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Update Commodity Inventory

            try
            {
                // Update or Delete
                if (type == 0 || type == 2)
                {

                    for (int i = 0; i <= before_entityD.Count - 1; i++)
                    {
                        if (before_entityD[i].inventory_management_division_id == 1)
                        {
                            DataCommodityInventory.UpdCommodityInventory(companyId,
                                                                         groupId,
                                                                         db,
                                                                         ExCast.zNumZeroNothingFormat(before_entityD[i].commodity_id),
                                                                         before_entityD[i].number,
                                                                         PG_NM,
                                                                         ExCast.zCInt(entityH.update_person_id),
                                                                         ipAdress,
                                                                         userId);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(UpdateCommodityInventory)", ex);
                return "UpdateSales(UpdateCommodityInventory) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Update Deliver Division

            try
            {
                // Delete
                if (type == 2)
                {
                    for (int i = 0; i <= before_entityD.Count - 1; i++)
                    {
                        // 未納に戻す
                        DataDeliverDivision.UpdDeliverDivision(DataDeliverDivision.gUpdDeliverDivisionKbn.Order,
                                                               companyId,
                                                               groupId,
                                                               ExCast.zCLng(before_entityD[i].order_id),
                                                               before_entityD[i].rec_no,
                                                               db,
                                                               1,
                                                               PG_NM,
                                                               ExCast.zCInt(entityH.update_person_id),
                                                               ipAdress,
                                                               userId);
                    }
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(UpdateDeliverDivisio)", ex);
                return "UpdateSales(UpdateDeliverDivisio) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region CommitTransaction

            try
            {
                db.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(CommitTransaction)", ex);
                return "UpdateSales(CommitTransaction) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Database Close

            try
            {
                db.DbClose();
            }
            catch (Exception ex)
            {
                db.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(DbClose)", ex);
                return "UpdateSales(DbClose) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
            finally
            {
                db = null;
            }

            #endregion

            #region Add Evidence

            try
            {
                switch (type)
                {
                    case 0:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.Sales.SalesInp,
                                                   DataPgEvidence.geOperationType.Update,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    case 1:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.Sales.SalesInp,
                                                   DataPgEvidence.geOperationType.Insert,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    case 2:
                        svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                                   companyId,
                                                   userId,
                                                   ipAdress,
                                                   sessionString,
                                                   DataPgEvidence.PGName.Sales.SalesInp,
                                                   DataPgEvidence.geOperationType.Delete,
                                                   "NO:" + no.ToString() + ",ID:" + id.ToString());
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateSales(Add Evidence)", ex);
                return "UpdateSales(Add Evidence) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }

            #endregion

            #region Return

            if (type == 1 && SalesNo == 0)
            {
                return "Auto Insert success : " + "売上番号 : " + no.ToString().ToString() + "で登録しました。";
            }
            else
            {
                return "";
            }

            #endregion

        }
        private bool pvtLogoff(string ipAdress, string userId, string sessionString, string personId)
        {
            #region Field

            string date = "";
            string time = "";
            DataTable dt;
            ExMySQLData sysdb = null;
            StringBuilder sb = new StringBuilder();

            #endregion

            #region Init

            // 日時取得
            DateTime now = DateTime.Now;
            date = now.ToString("yyyy/MM/dd");
            time = now.ToString("HH:mm:ss");

            #endregion

            #region System Databese Open

            try
            {
                sysdb = new ExMySQLData();
                sysdb.DbOpen();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".pvtLogoff(DbOpen)", ex);
                CommonUtl.gstrErrMsg = CLASS_NM + ".pvtLogoff(DbOpen)" + Environment.NewLine + ex.Message;
                return false;
            }

            #endregion

            #region BeginTransaction

            try
            {
                sysdb.ExBeginTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".pvtLogoff(BeginTransaction)", ex);
                CommonUtl.gstrErrMsg = CLASS_NM + ".pvtLogoff(BeginTransaction)" + Environment.NewLine + ex.Message;
                return false;
            }

            #endregion

            #region Insert

            // ログイン履歴登録
            try
            {

                sb.Length = 0;
                sb.Append("INSERT INTO SYS_H_USER_LOGIN_HISTORY " + Environment.NewLine);
                sb.Append("       (USER_ID" + Environment.NewLine);
                sb.Append("       ,LOGIN_DIVISION" + Environment.NewLine);
                sb.Append("       ,LOGIN_DATE" + Environment.NewLine);
                sb.Append("       ,LOGIN_TIME" + Environment.NewLine);
                sb.Append("       ,SESSION_STRING" + Environment.NewLine);
                sb.Append("       ,IP_ADRESS" + Environment.NewLine);
                sb.Append("       ,UPDATE_FLG" + Environment.NewLine);
                sb.Append("       ,DELETE_FLG" + Environment.NewLine);
                sb.Append("       ,CREATE_PG_ID" + Environment.NewLine);
                sb.Append("       ,CREATE_ADRESS" + Environment.NewLine);
                sb.Append("       ,CREATE_USER_ID" + Environment.NewLine);
                sb.Append("       ,CREATE_PERSON_ID" + Environment.NewLine);
                sb.Append("       ,CREATE_DATE" + Environment.NewLine);
                sb.Append("       ,CREATE_TIME" + Environment.NewLine);
                sb.Append("       ,UPDATE_PG_ID" + Environment.NewLine);
                sb.Append("       ,UPDATE_ADRESS" + Environment.NewLine);
                sb.Append("       ,UPDATE_USER_ID" + Environment.NewLine);
                sb.Append("       ,UPDATE_PERSON_ID" + Environment.NewLine);
                sb.Append("       ,UPDATE_DATE" + Environment.NewLine);
                sb.Append("       ,UPDATE_TIME" + Environment.NewLine);
                sb.Append(")" + Environment.NewLine);
                sb.Append("VALUES (" + userId + Environment.NewLine);                       // USER_ID
                sb.Append("       ,2" + Environment.NewLine);                               // LOGIN_DIVISION
                sb.Append("       ," + ExEscape.zRepStr(date) + Environment.NewLine);                  // LOGIN_DATE
                sb.Append("       ," + ExEscape.zRepStr(time) + Environment.NewLine);                  // LOGIN_TIME
                sb.Append("       ," + ExEscape.zRepStr(sessionString) + Environment.NewLine);         // SESSION_STRING
                sb.Append("       ," + ExEscape.zRepStr(ipAdress) + Environment.NewLine);              // IP_ADRESS
                sb.Append(CommonUtl.GetInsSQLCommonColums(CommonUtl.UpdKbn.Ins, 
                                                          "SYSTEM", 
                                                          "",
                                                          ExCast.zCInt(personId), 
                                                          "0",
                                                          ipAdress,
                                                          userId));
                sb.Append(")");

                sysdb.ExecuteSQL(sb.ToString(), false);

            }
            catch (Exception ex)
            {
                sysdb.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".pvtLogoff(Insert)", ex);
                CommonUtl.gstrErrMsg = CLASS_NM + ".pvtLogoff(Insert)" + Environment.NewLine + ex.Message;
                return false;
            }

            #endregion

            #region CommitTransaction

            try
            {
                sysdb.ExCommitTransaction();
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".pvtLogoff(CommitTransaction)", ex);
                CommonUtl.gstrErrMsg = CLASS_NM + ".pvtLogoff(CommitTransaction)" + Environment.NewLine + ex.Message;
                return false;
            }

            #endregion

            #region System Database Close

            try
            {
                sysdb.DbClose();
            }
            catch (Exception ex)
            {
                sysdb.ExRollbackTransaction();
                CommonUtl.ExLogger.Error(CLASS_NM + ".pvtLogoff(DbClose)", ex);
                CommonUtl.gstrErrMsg = CLASS_NM + ".pvtLogoff(DbClose)" + Environment.NewLine + ex.Message;
                return false;
            }
            finally
            {
                sysdb = null;
            }

            #endregion

            // グローバルセッション情報削除
            try
            {
                if (ExSession.DelSessionInf(ExCast.zCInt(HttpContext.Current.Session[ExSession.USER_ID])) == false)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".pvtLogoff(Delete Session)", ex);
                CommonUtl.gstrErrMsg = CLASS_NM + ".pvtLogoff(Delete Session)" + Environment.NewLine + ex.Message;
                return false;
            }

        }