Beispiel #1
0
        //public string GenerateRandom()
        //{
        //    Random generator = new Random();
        //    String r = generator.Next(100000, 999999).ToString("D6");
        //    return r;
        //}
        public int SaveData()
        {
            int      rc, count4 = 0;
            CashCard cashCard = new CashCard();

            CashCard[] cashCards = CashCard.GetCashCards(new CashCardParameters {
                SerialNumber = SerialNUM
            }, out rc);

            if (cashCards.Length == 0)
            {
                count4 = 1;
            }

            else
            {
                cashCard = cashCards[0];
            }
            CashCard[] cashCards1 = CashCard.GetCashCards(new CashCardParameters {
            }, out rc);
            int result = 0;
            int count = 0, count2 = 0, count3 = 1;

            Meter[] meters = Meter.GetMeters(new MeterParameters {
                Meterid = MeterId
            }, out rc);                                               //user for meter
            Customer customer = new Customer(meters[0].UserId.Value); //cardid for meter user

            foreach (CashCard cashCard1 in cashCards1)                //card exist
            {
                if (SerialNUM == cashCard1.SerialNumber && count4 == 0)
                {
                    count3 = 0;
                }
            }

            if (cashCard.Id != customer.CardId && count3 == 0 && count4 == 0)//card not for meter
            {
                count2 = 1;
            }


            if (cashCard.Amount < Amount && count4 == 0) //not suffecient amount
            {
                count = 1;
            }


            if (count == 0 && count2 == 0 && count3 == 0 && count4 == 0)
            {
                //OTP = 0;
                ChargeDate = DateTime.Now;
                Status     = "0";

                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection  = new SqlConnection(cstr.con);
                    cmd.Connection.Open();
                    cmd.CommandText = "SaveTopupData";


                    if (MeterId != null)
                    {
                        cmd.Parameters.AddWithValue("meter_id", MeterId);
                    }
                    if (Amount != null)
                    {
                        cmd.Parameters.AddWithValue("amount", Amount);
                    }
                    if (SerialNUM != null)
                    {
                        cmd.Parameters.AddWithValue("card_serialnum", cashCard.SerialNumber);
                    }
                    // if (OTP != null) cmd.Parameters.AddWithValue("otp", OTP);
                    if (ChargeDate != null)
                    {
                        cmd.Parameters.AddWithValue("chargeDate", ChargeDate);
                    }
                    if (ActivationDate != null)
                    {
                        cmd.Parameters.AddWithValue("activationDate", ActivationDate);
                    }
                    if (Status != null)
                    {
                        cmd.Parameters.AddWithValue("status", Status);
                    }

                    SqlParameter idParam = cmd.Parameters.Add("@id", SqlDbType.Int);
                    idParam.Direction = ParameterDirection.InputOutput;
                    idParam.Value     = this.Id;

                    SqlParameter otpParam = cmd.Parameters.Add("@otp", SqlDbType.Int);
                    otpParam.Direction = ParameterDirection.InputOutput;

                    SqlParameter resultParam = cmd.Parameters.Add("@result", SqlDbType.Int);
                    resultParam.Direction = ParameterDirection.InputOutput;


                    //try
                    // {
                    int c = cmd.ExecuteNonQuery();
                    //}
                    //catch (Exception ex)
                    //{
                    //    return 0;
                    //}

                    this.Id  = Convert.ToInt32(idParam.Value);
                    this.OTP = Convert.ToInt32(otpParam.Value);
                    result   = Convert.ToInt32(resultParam.Value);
                    cmd.Connection.Close();
                }
                decimal? amount = cashCard.Amount - Amount;
                CashCard cash   = new CashCard(cashCard.Id, amount, cashCard.SerialNumber);
                cash.SaveData();
            }
            else if (count == 1)//un sufficient amount
            {
                result = Convert.ToInt32(cashCard.Amount);
            }

            else if (count3 == 0 && count2 == 1)//not for meter
            {
                result = 4;
            }
            else if (count4 == 1)//card not valid
            {
                result = 2;
            }
            return(result);
        }