Beispiel #1
0
        //SAVE
        private void btnSave_Click(object sender, EventArgs e)
        {
            var timer = new Timer();

            timer.Interval = 5000;
            timer.Tick    += (s, a) => { statusLabel.Text = ""; timer.Stop(); };

            //set defaults if not set
            selectedCard.Preferred          = selectedCard.Preferred == null ? 0 : selectedCard.Preferred;
            selectedCard.Status             = selectedCard.Status == null ? 1 : selectedCard.Status;
            selectedCard.CodedCCNo          = string.IsNullOrEmpty(selectedCard.CodedCCNo.TrimEnd()) ? selectedCard.CrCardNoDecrypted.TrimEnd() : selectedCard.CodedCCNo.TrimEnd();
            selectedCard.CrCardNo           = "************" + selectedCard.CodedCCNo.TrimEnd().Substring(selectedCard.CodedCCNo.TrimEnd().Length - 4, 4);
            selectedCard.CrCardStreetNbrZip = string.IsNullOrEmpty(selectedCard.CrCardStreetNbrZip) ? "877 Monterey Pass Rd" : selectedCard.CrCardStreetNbrZip;
            selectedCard.CrCardZipCode      = string.IsNullOrEmpty(selectedCard.CrCardZipCode) ? "91754" : selectedCard.CrCardZipCode;

            var visaCardType = cardTypes.First(c => c.CrCardTypeName.Contains("Visa"));

            selectedCard.CrCardTypeKey = selectedCard.CrCardTypeKey == null ? visaCardType.Key : selectedCard.CrCardTypeKey;

            //update old preferred card
            var  oldPrefCard             = creditCards.FirstOrDefault(c => c.Preferred == -1 && c.CCKey != selectedCard.CCKey);
            bool selectedCardIsPreferred = selectedCard.Preferred == -1;

            if ((selectedCardIsPreferred) && (oldPrefCard != null))
            {
                oldPrefCard.CodedCCNo = oldPrefCard.CrCardNoDecrypted.TrimEnd();
                oldPrefCard.CrCardNo  = "************" + oldPrefCard.CodedCCNo.TrimEnd().Substring(oldPrefCard.CodedCCNo.TrimEnd().Length - 4, 4);
                oldPrefCard.Preferred = 0;
                service.UpdateCreditCard(oldPrefCard);
            }

            //add or update selected card
            if (selectedCard.CCKey == 0)
            {
                service.AddNewCreditCard(selectedCard, customer.Key);
                statusLabel.Text = "Credit card was saved successfully.";
                timer.Start();
            }
            else
            {
                service.UpdateCreditCard(selectedCard);
                statusLabel.Text = "Credit card was updated successfully.";
                timer.Start();
            }

            //refresh cards
            creditCards = service.LoadCreditCardsByCustKey(custKey);


            //set UI
            loading = true;
            UnsubscribeToStateChangedEvents();

            SetDataGrid();

            if (selectedCard.CCKey != 0)
            {
                SelectCurrentCard();
                SelectCurrentCardDGV();
            }
            else
            {
                SelectPreferredCard();
                SelectPreferredCardDGV();
            }

            SetFormControlValuesFromSelectedCard();
            btnSave.Enabled = false;
            isDirty         = false;

            SubscribeToStateChangedEvents();
            loading = false;

            dgvCreditCard.Focus();
        }