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

        }
        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 UpdateInOutDelivery(string random, int type, long InOutDeliveryNo, EntityInOutDeliveryH entityH, List<EntityInOutDeliveryD> entityD, EntityInOutDeliveryH before_entityH, List<EntityInOutDeliveryD> before_entityD)
        {

            try
            {
                return UpdateInOutDeliveryExcExc(random, type, 1, InOutDeliveryNo, 0, entityH, entityD, before_entityH, before_entityD);
            }
            catch (Exception ex)
            {
                CommonUtl.ExLogger.Error(CLASS_NM + ".UpdateInOutDelivery(UpdateInOutDeliveryExcExc)", ex);
                return "UpdateInOutDelivery(UpdateInOutDeliveryExcExc) : 予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message;
            }
        }
        public EntityInOutDeliveryH GetInOutDeliveryH(string random, long IDFrom, long IDTo)
        {

            EntityInOutDeliveryH entity = null;

            #region 認証処理

            string companyId = "";
            string groupId = "";
            string userId = "";
            string ipAdress = "";
            string sessionString = "";
            int idFigureCustomer = 0;
            int idFigurePurchase = 0;
            try
            {
                companyId = ExCast.zCStr(HttpContext.Current.Session[ExSession.COMPANY_ID]);
                groupId = ExCast.zCStr(HttpContext.Current.Session[ExSession.GROUP_ID]);
                userId = ExCast.zCStr(HttpContext.Current.Session[ExSession.USER_ID]);
                ipAdress = ExCast.zCStr(HttpContext.Current.Session[ExSession.IP_ADRESS]);
                sessionString = ExCast.zCStr(HttpContext.Current.Session[ExSession.SESSION_RANDOM_STR]);
                idFigureCustomer = ExCast.zCInt(HttpContext.Current.Session[ExSession.ID_FIGURE_CUSTOMER]);
                idFigurePurchase = ExCast.zCInt(HttpContext.Current.Session[ExSession.ID_FIGURE_PURCHASE]);

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

            #endregion

            StringBuilder sb;
            DataTable dt;
            ExMySQLData db;

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

                sb = new StringBuilder();

                #region SQL

                sb.Append("SELECT T.COMPANY_ID " + Environment.NewLine);
                sb.Append("      ,T.GROUP_ID " + Environment.NewLine);
                sb.Append("      ,T.ID " + Environment.NewLine);
                sb.Append("      ,lpad(CAST(T.NO as char), 10, '0') AS NO" + Environment.NewLine);
                sb.Append("      ,date_format(T.IN_OUT_DELIVERY_YMD , " + ExEscape.SQL_YMD + ") AS IN_OUT_DELIVERY_YMD" + Environment.NewLine);

                sb.Append("      ,T.IN_OUT_DELIVERY_KBN " + Environment.NewLine);
                sb.Append("      ,NM1.DESCRIPTION AS IN_OUT_DELIVERY_KBN_NM " + Environment.NewLine);

                sb.Append("      ,T.IN_OUT_DELIVERY_PROC_KBN " + Environment.NewLine);
                sb.Append("      ,NM2.DESCRIPTION AS IN_OUT_DELIVERY_PROC_KBN_NM " + Environment.NewLine);

                sb.Append("      ,T.CAUSE_NO " + Environment.NewLine);

                sb.Append("      ,T.PERSON_ID " + Environment.NewLine);
                sb.Append("      ,PS.NAME AS UPDATE_PERSON_NM " + Environment.NewLine);

                sb.Append("      ,T.IN_OUT_DELIVERY_TO_KBN " + Environment.NewLine);
                sb.Append("      ,NM3.DESCRIPTION AS IN_OUT_DELIVERY_TO_KBN_NM " + Environment.NewLine);

                sb.Append("      ,T.GROUP_ID_TO " + Environment.NewLine);
                sb.Append("      ,SG.NAME AS GROUP_ID_TO_NM " + Environment.NewLine);

                sb.Append("      ,T.CUSTOMER_ID " + Environment.NewLine);
                sb.Append("      ,CS.NAME AS CUSTOMER_NM " + Environment.NewLine);

                sb.Append("      ,T.PURCHASE_ID " + Environment.NewLine);
                sb.Append("      ,PU.NAME AS PURCHASE_NM " + Environment.NewLine);

                sb.Append("      ,T.SUM_ENTER_NUMBER " + Environment.NewLine);
                sb.Append("      ,T.SUM_CASE_NUMBER " + Environment.NewLine);
                sb.Append("      ,T.SUM_NUMBER " + Environment.NewLine);

                sb.Append("      ,T.MEMO " + Environment.NewLine);
                sb.Append("      ,date_format(T.UPDATE_DATE , " + ExEscape.SQL_YMD + ") AS UPDATE_DATE" + Environment.NewLine);
                sb.Append("      ,T.UPDATE_TIME " + Environment.NewLine);

                sb.Append("  FROM T_IN_OUT_DELIVERY_H AS T" + Environment.NewLine);

                #region Join

                // 更新担当者
                sb.Append("  LEFT JOIN M_PERSON AS PS" + Environment.NewLine);
                sb.Append("    ON PS.DELETE_FLG = 0 " + Environment.NewLine);
                sb.Append("   AND PS.DISPLAY_FLG = 1 " + Environment.NewLine);
                sb.Append("   AND PS.COMPANY_ID = " + companyId + Environment.NewLine);
                sb.Append("   AND PS.GROUP_ID = " + groupId + Environment.NewLine);
                sb.Append("   AND PS.ID = T.PERSON_ID" + Environment.NewLine);

                // グループ
                sb.Append("  LEFT JOIN SYS_M_COMPANY_GROUP AS SG" + Environment.NewLine);
                sb.Append("    ON SG.DELETE_FLG = 0 " + Environment.NewLine);
                sb.Append("   AND SG.DISPLAY_FLG = 1 " + Environment.NewLine);
                sb.Append("   AND SG.COMPANY_ID = T.COMPANY_ID" + Environment.NewLine);
                sb.Append("   AND SG.ID = T.GROUP_ID_TO" + Environment.NewLine);

                // 得意先
                sb.Append("  LEFT JOIN M_CUSTOMER AS CS" + Environment.NewLine);
                sb.Append("    ON CS.DELETE_FLG = 0 " + Environment.NewLine);
                sb.Append("   AND CS.DISPLAY_FLG = 1 " + Environment.NewLine);
                sb.Append("   AND CS.COMPANY_ID = " + companyId + Environment.NewLine);
                sb.Append("   AND T.CUSTOMER_ID = CS.ID" + Environment.NewLine);

                // 仕入先
                sb.Append("  LEFT JOIN M_PURCHASE AS PU" + Environment.NewLine);
                sb.Append("    ON PU.DELETE_FLG = 0 " + Environment.NewLine);
                sb.Append("   AND PU.DISPLAY_FLG = 1 " + Environment.NewLine);
                sb.Append("   AND PU.COMPANY_ID = " + companyId + Environment.NewLine);
                sb.Append("   AND T.PURCHASE_ID = PU.ID" + Environment.NewLine);

                // 入出庫区分
                sb.Append("  LEFT JOIN SYS_M_NAME AS NM1" + Environment.NewLine);
                sb.Append("    ON NM1.DELETE_FLG = 0 " + Environment.NewLine);
                sb.Append("   AND NM1.DISPLAY_FLG = 1 " + Environment.NewLine);
                sb.Append("   AND NM1.DIVISION_ID = " + (int)CommonUtl.geNameKbn.IN_OUT_DELIVERY_KBN + Environment.NewLine);
                sb.Append("   AND T.IN_OUT_DELIVERY_KBN = NM1.ID" + Environment.NewLine);

                // 入出庫処理区分
                sb.Append("  LEFT JOIN SYS_M_NAME AS NM2" + Environment.NewLine);
                sb.Append("    ON NM2.DELETE_FLG = 0 " + Environment.NewLine);
                sb.Append("   AND NM2.DISPLAY_FLG = 1 " + Environment.NewLine);
                sb.Append("   AND NM2.DIVISION_ID = " + (int)CommonUtl.geNameKbn.IN_OUT_DELIVERY_PROC_KBN + Environment.NewLine);
                sb.Append("   AND T.IN_OUT_DELIVERY_PROC_KBN = NM2.ID" + Environment.NewLine);

                // 入出庫先区分
                sb.Append("  LEFT JOIN SYS_M_NAME AS NM3" + Environment.NewLine);
                sb.Append("    ON NM3.DELETE_FLG = 0 " + Environment.NewLine);
                sb.Append("   AND NM3.DISPLAY_FLG = 1 " + Environment.NewLine);
                sb.Append("   AND NM3.DIVISION_ID = " + (int)CommonUtl.geNameKbn.IN_OUT_DELIVERY_TO_KBN + Environment.NewLine);
                sb.Append("   AND T.IN_OUT_DELIVERY_TO_KBN = NM3.ID" + Environment.NewLine);

                #endregion

                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.NO >= " + IDFrom.ToString() + Environment.NewLine);
                sb.Append("   AND T.NO <= " + IDTo.ToString() + Environment.NewLine);

                sb.Append(" ORDER BY T.COMPANY_ID " + Environment.NewLine);
                sb.Append("         ,T.GROUP_ID " + Environment.NewLine);
                sb.Append("         ,T.IN_OUT_DELIVERY_YMD DESC " + Environment.NewLine);
                sb.Append("         ,T.NO DESC " + Environment.NewLine);
                sb.Append(" LIMIT 0, 1");

                #endregion

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

                if (dt.DefaultView.Count > 0)
                {
                    entity = new EntityInOutDeliveryH();

                    // 排他制御
                    DataPgLock.geLovkFlg lockFlg = DataPgLock.geLovkFlg.UnLock;
                    string strErr = DataPgLock.SetLockPg(companyId, userId, PG_NM, groupId + "-" + ExCast.zNumZeroNothingFormat(IDFrom.ToString()), ipAdress, db, out lockFlg);
                    if (strErr != "")
                    {
                        entity.message = CLASS_NM + ".GetInOutDeliveryH : 排他制御(ロック情報取得)に失敗しました。" + Environment.NewLine + strErr;
                    }

                    #region Set Entity

                    entity.id = ExCast.zCLng(dt.DefaultView[0]["ID"]);
                    entity.no = ExCast.zCLng(dt.DefaultView[0]["NO"]);
                    entity.in_out_delivery_ymd = ExCast.zDateNullToDefault(dt.DefaultView[0]["IN_OUT_DELIVERY_YMD"]);
                    entity.in_out_delivery_kbn = ExCast.zCInt(dt.DefaultView[0]["IN_OUT_DELIVERY_KBN"]);
                    entity.in_out_delivery_kbn_nm = ExCast.zCStr(dt.DefaultView[0]["IN_OUT_DELIVERY_KBN_NM"]);

                    entity.in_out_delivery_proc_kbn = ExCast.zCInt(dt.DefaultView[0]["IN_OUT_DELIVERY_PROC_KBN"]);
                    entity.in_out_delivery_proc_kbn_nm = ExCast.zCStr(dt.DefaultView[0]["IN_OUT_DELIVERY_PROC_KBN_NM"]);

                    entity.cause_no = ExCast.zCLng(dt.DefaultView[0]["CAUSE_NO"]);

                    entity.update_person_id = ExCast.zCInt(dt.DefaultView[0]["PERSON_ID"]);
                    entity.update_person_nm = ExCast.zCStr(dt.DefaultView[0]["UPDATE_PERSON_NM"]);

                    entity.in_out_delivery_to_kbn = ExCast.zCInt(dt.DefaultView[0]["IN_OUT_DELIVERY_TO_KBN"]);
                    entity.in_out_delivery_to_kbn_nm = ExCast.zCStr(dt.DefaultView[0]["IN_OUT_DELIVERY_TO_KBN_NM"]);

                    entity.group_id_to = string.Format("{0:000}", ExCast.zCInt(dt.DefaultView[0]["GROUP_ID_TO"]));
                    entity.group_id_to_nm = ExCast.zCStr(dt.DefaultView[0]["GROUP_ID_TO_NM"]);

                    entity.customer_id = ExCast.zFormatForID(dt.DefaultView[0]["CUSTOMER_ID"], idFigureCustomer);
                    entity.customer_name = ExCast.zCStr(dt.DefaultView[0]["CUSTOMER_NM"]);

                    entity.purchase_id = ExCast.zFormatForID(dt.DefaultView[0]["PURCHASE_ID"], idFigurePurchase);
                    entity.purchase_name = ExCast.zCStr(dt.DefaultView[0]["PURCHASE_NM"]);

                    entity.sum_enter_number = ExCast.zCDbl(dt.DefaultView[0]["SUM_ENTER_NUMBER"]);
                    entity.sum_case_number = ExCast.zCDbl(dt.DefaultView[0]["SUM_CASE_NUMBER"]);
                    entity.sum_number = ExCast.zCDbl(dt.DefaultView[0]["SUM_NUMBER"]);

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

                    entity.lock_flg = (int)lockFlg;

                    #endregion

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

            svcPgEvidence.gAddEvidence(ExCast.zCInt(HttpContext.Current.Session[ExSession.EVIDENCE_SAVE_FLG]),
                                       companyId,
                                       userId,
                                       ipAdress,
                                       sessionString,
                                       DataPgEvidence.PGName.InOutDeliver.InOutDeliverInp,
                                       DataPgEvidence.geOperationType.Select,
                                       "NO:" + IDFrom.ToString() + ",NO:" + IDTo.ToString());

            return entity;


        }
        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 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 "";

        }