/// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (DataItem != null && !(DataItem is GridInsertionObject))
            {
                PaymentPassRule = (tbl_PaymentPassRule)DataItem;
            }

            ddlPaymentType.Items.Clear();
            foreach (var mode in EnumHelper.EnumToList <PaymentType>())
            {
                ddlPaymentType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(mode), ((int)mode).ToString()));
            }

            if (PaymentPassRule != null)
            {
                edsPaymentPassRulePass.Where    = string.Format("it.PaymentPassRuleID = GUID '{0}'", PaymentPassRule.ID);
                edsPaymentPassRuleCompany.Where = string.Format("it.PaymentPassRuleID = GUID '{0}'", PaymentPassRule.ID);

                //if (int.Parse(ddlPaymentType.SelectedValue) != PaymentPassRule.PaymentTypeID && ddlPaymentType.SelectedValue == "0")
                //{
                //    dcbPaymentPassCategory.SelectedId = PaymentCFO.PaymentPassCategoryID;
                //    var paymentPassCategory =
                //        DataManager.PaymentPassCategory.SelectById(PaymentCFO.PaymentPassCategoryID);
                //    dcbPaymentPassCategory.SelectedText = paymentPassCategory != null
                //                                              ? paymentPassCategory.Title
                //                                              : "";
                //}
            }
        }
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            rgPaymentPassRulePass.Culture    = new CultureInfo("ru-RU");
            rgPaymentPassRuleCompany.Culture = new CultureInfo("ru-RU");

            edsPaymentArticle.Where      = string.Format("it.SiteID = GUID '{0}'", CurrentUser.Instance.SiteID);
            edsPaymentCFO.Where          = string.Format("it.SiteID = GUID '{0}'", CurrentUser.Instance.SiteID);
            edsPaymentPassCategory.Where = string.Format("it.SiteID = GUID '{0}'", CurrentUser.Instance.SiteID);
            edsPaymentPassRulePass.Where = string.Format("it.SiteID = GUID '{0}'", CurrentUser.Instance.SiteID);

            edsPayer.Where                 = string.Format("it.SiteID = GUID '{0}'", CurrentUser.Instance.SiteID);
            edsPayerLegalAccount.Where     = string.Format("it.SiteID = GUID '{0}'", CurrentUser.Instance.SiteID);
            edsRecipient.Where             = string.Format("it.SiteID = GUID '{0}'", CurrentUser.Instance.SiteID);
            edsRecipientLegalAccount.Where = string.Format("it.SiteID = GUID '{0}'", CurrentUser.Instance.SiteID);

            if (DataItem != null && !(DataItem is GridInsertionObject))
            {
                PaymentPassRule = (tbl_PaymentPassRule)DataItem;
            }

            if (PaymentPassRule != null)
            {
                edsPaymentPassRulePass.Where    = string.Format("it.PaymentPassRuleID = GUID '{0}'", PaymentPassRule.ID);
                edsPaymentPassRuleCompany.Where = string.Format("it.PaymentPassRuleID = GUID '{0}'", PaymentPassRule.ID);
            }
        }
        /// <summary>
        /// Adds the specified service level client.
        /// </summary>
        /// <param name="paymentPassRule">The payment pass rule.</param>
        /// <returns></returns>
        public tbl_PaymentPassRule Add(tbl_PaymentPassRule paymentPassRule)
        {
            paymentPassRule.ID = Guid.NewGuid();

            _dataContext.tbl_PaymentPassRule.AddObject(paymentPassRule);
            _dataContext.SaveChanges();

            return(paymentPassRule);
        }
        /// <summary>
        /// Handles the OnClick event of the btnInsert control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnInsert_OnClick(object sender, EventArgs e)
        {
            var paymentPassRule = new tbl_PaymentPassRule
            {
                ID            = Guid.NewGuid(),
                SiteID        = CurrentUser.Instance.SiteID,
                Title         = txtTitle.Text,
                PaymentTypeID = int.Parse(ddlPaymentType.SelectedValue),
                IsActive      = chxIsActive.Checked,
                IsAutomatic   = chxIsAutomatic.Checked
            };

            DataItem = paymentPassRule;

            DataManager.PaymentPassRule.Add(paymentPassRule);
        }
        public void BindData()
        {
            ddlPaymentType.Items.Clear();
            foreach (var mode in EnumHelper.EnumToList <PaymentType>())
            {
                ddlPaymentType.Items.Add(new ListItem(EnumHelper.GetEnumDescription(mode), ((int)mode).ToString()));
            }

            if (PaymentPassRuleId != null)
            {
                PaymentPassRule             = DataManager.PaymentPassRule.SelectById(CurrentUser.Instance.SiteID, (Guid)PaymentPassRuleId);
                txtTitle.Text               = PaymentPassRule.Title;
                chxIsActive.Checked         = PaymentPassRule.IsActive ?? false;
                chxIsAutomatic.Checked      = PaymentPassRule.IsAutomatic ?? false;
                ucPaymentPass.PaymentPasses = PaymentPassRule.tbl_PaymentPassRulePass.Select(op => new PaymentPass.PaymentPassMap()
                {
                    ID = op.ID,
                    OutgoPaymentPassCategoryID = op.OutgoPaymentPassCategoryID,
                    OutgoPaymentPassCategory   = op.tbl_PaymentPassCategory != null ? op.tbl_PaymentPassCategory.Title : "",
                    OutgoCFOID                  = op.OutgoCFOID,
                    OutgoCFO                    = op.tbl_PaymentCFO != null ? op.tbl_PaymentCFO.Title : "",
                    OutgoPaymentArticleID       = op.OutgoPaymentArticleID,
                    OutgoPaymentArticle         = op.tbl_PaymentArticle != null ? op.tbl_PaymentArticle.Title : "",
                    IncomePaymentPassCategoryID = op.IncomePaymentPassCategoryID,
                    IncomePaymentPassCategory   = op.tbl_PaymentPassCategory1 != null ? op.tbl_PaymentPassCategory1.Title : "",
                    IncomeCFOID                 = op.IncomeCFOID,
                    IncomeCFO                   = op.tbl_PaymentCFO1 != null ? op.tbl_PaymentCFO1.Title : "",
                    IncomePaymentArticleID      = op.IncomePaymentArticleID,
                    IncomePaymentArticle        = op.tbl_PaymentArticle1 != null ? op.tbl_PaymentArticle1.Title : "",
                    FormulaID                   = op.FormulaID,
                    Value = op.Value
                }).ToList();

                ucPaymentPassRuleCompany.PaymentPassRuleCompanies =
                    PaymentPassRule.tbl_PaymentPassRuleCompany.Select(op => new PaymentPassRuleCompany.PaymentPassRuleCompanyMap()
                {
                    ID                      = op.ID,
                    PayerID                 = op.PayerID,
                    Payer                   = op.tbl_Company != null ? op.tbl_Company.Name : "",
                    PayerLegalAccountID     = op.PayerLegalAccountID,
                    PayerLegalAccount       = op.tbl_CompanyLegalAccount != null ? op.tbl_CompanyLegalAccount.Title : "",
                    RecipientID             = op.RecipientID,
                    Recipient               = op.tbl_Company1 != null ? op.tbl_Company1.Name : "",
                    RecipientLegalAccountID = op.RecipientLegalAccountID,
                    RecipientLegalAccount   = op.tbl_CompanyLegalAccount1 != null ? op.tbl_CompanyLegalAccount1.Title : ""
                }).ToList();
            }
            else
            {
                txtTitle.Text               = "";
                chxIsActive.Checked         = false;
                chxIsAutomatic.Checked      = false;
                ucPaymentPass.PaymentPasses = new List <PaymentPass.PaymentPassMap>();
                ucPaymentPassRuleCompany.PaymentPassRuleCompanies = new List <PaymentPassRuleCompany.PaymentPassRuleCompanyMap>();
                if (PaymentPassRuleCompanies != null)
                {
                    ucPaymentPassRuleCompany.PaymentPassRuleCompanies = PaymentPassRuleCompanies;
                }
            }
            ucPaymentPassRuleCompany.BindData();
            ucPaymentPass.BindData();
        }
        /// <summary>
        /// Handles the OnClick event of the btnUpdate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnUpdate_OnClick(object sender, EventArgs e)
        {
            var paymentPassRule = DataManager.PaymentPassRule.SelectById(CurrentUser.Instance.SiteID, PaymentPassRule.ID);

            if (paymentPassRule != null)
            {
                paymentPassRule.Title         = txtTitle.Text;
                paymentPassRule.PaymentTypeID = int.Parse(ddlPaymentType.SelectedValue);
                paymentPassRule.IsActive      = chxIsActive.Checked;
                paymentPassRule.IsAutomatic   = chxIsAutomatic.Checked;
                DataManager.PaymentPassRule.Update(paymentPassRule);
            }
            else
            {
                paymentPassRule = new tbl_PaymentPassRule
                {
                    ID            = Guid.NewGuid(),
                    SiteID        = CurrentUser.Instance.SiteID,
                    Title         = txtTitle.Text,
                    PaymentTypeID = int.Parse(ddlPaymentType.SelectedValue),
                    IsActive      = chxIsActive.Checked,
                    IsAutomatic   = chxIsAutomatic.Checked
                };

                DataManager.PaymentPassRule.Add(paymentPassRule);
            }


            #region Save Payment Pass Rule Pass
            var oldPaymentPasses = DataManager.PaymentPassRulePass.SelectAll(CurrentUser.Instance.SiteID, paymentPassRule.ID);
            var ids = new List <tbl_PaymentPassRulePass>();
            foreach (var old in oldPaymentPasses)
            {
                if (ucPaymentPass.PaymentPasses.Where(a => a.ID == old.ID).Select(a => a.ID).SingleOrDefault() == Guid.Empty)
                {
                    ids.Add(old);
                }
            }
            foreach (var tblPaymentPass in ids)
            {
                DataManager.PaymentPassRulePass.Delete(tblPaymentPass);
            }
            foreach (var paymentPass in ucPaymentPass.PaymentPasses)
            {
                var oldPaymentPass = DataManager.PaymentPassRulePass.SelectById(paymentPassRule.ID, paymentPass.ID);
                if (oldPaymentPass != null)
                {
                    oldPaymentPass.SiteID                      = CurrentUser.Instance.SiteID;
                    oldPaymentPass.PaymentPassRuleID           = paymentPassRule.ID;
                    oldPaymentPass.IncomePaymentPassCategoryID = paymentPass.IncomePaymentPassCategoryID;
                    oldPaymentPass.IncomeCFOID                 = paymentPass.IncomeCFOID;
                    oldPaymentPass.IncomePaymentArticleID      = paymentPass.IncomePaymentArticleID;

                    oldPaymentPass.OutgoPaymentPassCategoryID = paymentPass.OutgoPaymentPassCategoryID;
                    oldPaymentPass.OutgoCFOID            = paymentPass.OutgoCFOID;
                    oldPaymentPass.OutgoPaymentArticleID = paymentPass.OutgoPaymentArticleID;

                    oldPaymentPass.FormulaID = paymentPass.FormulaID;
                    oldPaymentPass.Value     = paymentPass.Value;
                    DataManager.PaymentPassRulePass.Update(oldPaymentPass);
                }
                else
                {
                    DataManager.PaymentPassRulePass.Add(new tbl_PaymentPassRulePass()
                    {
                        SiteID = CurrentUser.Instance.SiteID, PaymentPassRuleID = paymentPassRule.ID, IncomePaymentPassCategoryID = paymentPass.IncomePaymentPassCategoryID, IncomeCFOID = paymentPass.IncomeCFOID, IncomePaymentArticleID = paymentPass.IncomePaymentArticleID, OutgoPaymentPassCategoryID = paymentPass.OutgoPaymentPassCategoryID, OutgoCFOID = paymentPass.OutgoCFOID, OutgoPaymentArticleID = paymentPass.OutgoPaymentArticleID, FormulaID = paymentPass.FormulaID, Value = paymentPass.Value
                    });
                }
            }
            #endregion


            #region Save Payment Pass Rule Company
            var oldPaymentPassRuleCompanies = DataManager.PaymentPassRuleCompany.SelectAll(CurrentUser.Instance.SiteID, paymentPassRule.ID);
            var ids2 = new List <tbl_PaymentPassRuleCompany>();
            foreach (var old in oldPaymentPassRuleCompanies)
            {
                if (ucPaymentPassRuleCompany.PaymentPassRuleCompanies.Where(a => a.ID == old.ID).Select(a => a.ID).SingleOrDefault() == Guid.Empty)
                {
                    ids2.Add(old);
                }
            }
            foreach (var tblPaymentPass in ids2)
            {
                DataManager.PaymentPassRuleCompany.Delete(tblPaymentPass);
            }
            foreach (var paymentPass in ucPaymentPassRuleCompany.PaymentPassRuleCompanies)
            {
                var oldPaymentPass = DataManager.PaymentPassRuleCompany.SelectById(paymentPassRule.ID, paymentPass.ID);
                if (oldPaymentPass != null)
                {
                    oldPaymentPass.SiteID              = CurrentUser.Instance.SiteID;
                    oldPaymentPass.PaymentPassRuleID   = paymentPassRule.ID;
                    oldPaymentPass.PayerID             = paymentPass.PayerID;
                    oldPaymentPass.PayerLegalAccountID = paymentPass.PayerLegalAccountID;
                    oldPaymentPass.RecipientID         = paymentPass.RecipientID;

                    oldPaymentPass.RecipientLegalAccountID = paymentPass.RecipientLegalAccountID;
                    DataManager.PaymentPassRuleCompany.Update(oldPaymentPass);
                }
                else
                {
                    DataManager.PaymentPassRuleCompany.Add(new tbl_PaymentPassRuleCompany()
                    {
                        SiteID = CurrentUser.Instance.SiteID, PaymentPassRuleID = paymentPassRule.ID, PayerID = paymentPass.PayerID, PayerLegalAccountID = paymentPass.PayerLegalAccountID, RecipientID = paymentPass.RecipientID, RecipientLegalAccountID = paymentPass.RecipientLegalAccountID
                    });
                }
            }
            #endregion
        }
 /// <summary>
 /// Deletes the specified service level client.
 /// </summary>
 /// <param name="paymentPassRule">The payment pass rule.</param>
 public void Delete(tbl_PaymentPassRule paymentPassRule)
 {
     _dataContext.tbl_PaymentPassRule.DeleteObject(paymentPassRule);
     _dataContext.SaveChanges();
 }
 /// <summary>
 /// Updates the specified service level client.
 /// </summary>
 /// <param name="paymentPassRule">The payment pass rule.</param>
 public void Update(tbl_PaymentPassRule paymentPassRule)
 {
     _dataContext.SaveChanges();
 }