public ActionResult UpdateRecurringReceipt(RecurringReceipt model)
 {
     if (!User.Identity.IsAuthenticated)
         return RedirectToAction("LogOn", "Account");
     var scope = ObjectScopeProvider1.GetNewObjectScope();
     if (CheckAdminauthorization(scope, User.Identity.Name))
     {
         var donationReceiver = (from c in scope.GetOqlQuery<User>().ExecuteEnumerable()
                                 where
                                     c.IsheDonationReceiver.Equals(true) &&
                                     c.Username.Equals(Request.Form["CmbDonationReceivedBy"])
                                 select c).ToList();
         if (donationReceiver.Count > 0)
         {
             List<Receipt> receipts = (from c in scope.GetOqlQuery<Receipt>().ExecuteEnumerable()
                                       where c.ReceiptNumber.ToLower().Equals(model.ReceiptNumber.ToLower())
                                       select c).ToList();
             if (receipts.Count > 0)
             {
                 scope.Transaction.Begin();
                 var receipt = receipts[0];
                 receipt.Address = model.Address;
                 receipt.Address2 = model.Address2;
                 receipt.Contact = model.Contact;
                 receipt.ReceiptNumber = model.ReceiptNumber;
                 receipt.DonationReceiver = donationReceiver[0];
                 receipt.Email = model.Email;
                 receipt.FirstName = model.FirstName;
                 receipt.Mi = model.Mi;
                 receipt.LastName = model.LastName;
                 receipt.City = model.City;
                 receipt.State = model.State;
                 receipt.ZipCode = model.ZipCode;
                 receipt.ReceiptType = ReceiptType.RecurringReceipt;
                 receipt.IssuedDate = model.IssuedDate;
                 receipt.DonationAmountinWords = model.DonationAmountinWords;
                 receipt.RecurringDetails.Clear();
                 for (int i = 0; i < model.RecurrenceDates.Count(); i++)
                 {
                     try
                     {
                         var recurringDetails = new RecurringDetails
                                                    {
                                                        DueDate = Convert.ToDateTime(model.RecurrenceDates[i]),
                                                        Amount = model.RecurrenceAmount[i]
                                                    };
                         switch (model.RecurrenceModeofPayment[i])
                         {
                             case "Cash":
                                 {
                                     recurringDetails.ModeOfPayment = ModeOfPayment.Cash;
                                     break;
                                 }
                             case "Cheque":
                                 {
                                     recurringDetails.ModeOfPayment = ModeOfPayment.Cheque;
                                     break;
                                 }
                             case "Mobile":
                                 {
                                     recurringDetails.ModeOfPayment = ModeOfPayment.Mobile;
                                     break;
                                 }
                             case "Goods":
                                 {
                                     recurringDetails.ModeOfPayment = ModeOfPayment.Goods;
                                     break;
                                 }
                             case "Online":
                                 {
                                     recurringDetails.ModeOfPayment = ModeOfPayment.Online;
                                     break;
                                 }
                         }
                         receipt.RecurringDetails.Add(recurringDetails);
                     }
                     catch (Exception)
                     {
                         continue;
                     }
                 }
                 if (model.SignatureImage != null)
                 {
                     // saving image here
                     try
                     {
                         var signature = new SignatureImage { Filename = model.SignatureImage.FileName };
                         Stream fileStream = model.SignatureImage.InputStream;
                         int fileLength = model.SignatureImage.ContentLength;
                         signature.Filedata = new byte[fileLength];
                         fileStream.Read(signature.Filedata, 0, fileLength);
                         signature.MimeType = model.SignatureImage.ContentType;
                         signature.ID = Guid.NewGuid();
                         receipt.SignatureImage = signature;
                     }
                     catch
                     { }
                 }
                 scope.Add(receipt);
                 scope.Transaction.Commit();
                 ViewData["Status"] = "Updated successfully.";
                 return View("Status");
             }
         }
         ViewData["Status"] = "Unable to generate receipt due to invalid parameter passed.";
         return View("Status");
     }
     ViewData["Status"] = "You are not authorized to do this operation";
     return View("Status");
 }
 public ActionResult RecurringReceipt(RecurringReceipt model)
 {
     if (!User.Identity.IsAuthenticated)
         return RedirectToAction("LogOn", "Account");
     var scope = ObjectScopeProvider1.GetNewObjectScope();
     if (Checkauthorization(scope, User.Identity.Name))
     {
         if (model.SignatureImage != null)
         {
             var donationReceiver = (from c in scope.GetOqlQuery<User>().ExecuteEnumerable()
                                     where
                                         c.IsheDonationReceiver.Equals(true) &&
                                         c.Username.Equals(Request.Form["CmbDonationReceivedBy"])
                                     select c).ToList();
             if (donationReceiver.Count > 0)
             {
                 scope.Transaction.Begin();
                 var receipt = new Receipt
                                   {
                                       Address = model.Address,
                                       Address2 = model.Address2,
                                       Contact = model.Contact,
                                       ReceiptNumber = model.ReceiptNumber,
                                       DonationReceiver = donationReceiver[0],
                                       DonationAmountinWords = model.DonationAmountinWords,
                                       Email = model.Email,
                                       DateReceived = DateTime.Now,
                                       FirstName = model.FirstName,
                                       City = model.City,
                                       LastName = model.LastName,
                                       Mi = model.Mi,
                                       State = model.State,
                                       ZipCode = model.ZipCode,
                                       IssuedDate = model.IssuedDate,
                                       ReceiptType = ReceiptType.RecurringReceipt
                                   };
                 for (int i = 0; i < model.RecurrenceDates.Count(); i++)
                 {
                     try
                     {
                         var recurringDetails = new RecurringDetails
                                                    {
                                                        DueDate = Convert.ToDateTime(model.RecurrenceDates[i]),
                                                        Amount = model.RecurrenceAmount[i]
                                                    };
                         switch (model.RecurrenceModeofPayment[i])
                         {
                             case "Cash":
                                 {
                                     recurringDetails.ModeOfPayment = ModeOfPayment.Cash;
                                     break;
                                 }
                             case "Cheque":
                                 {
                                     recurringDetails.ModeOfPayment = ModeOfPayment.Cheque;
                                     break;
                                 }
                             case "Mobile":
                                 {
                                     recurringDetails.ModeOfPayment = ModeOfPayment.Mobile;
                                     break;
                                 }
                             case "Goods":
                                 {
                                     recurringDetails.ModeOfPayment = ModeOfPayment.Goods;
                                     break;
                                 }
                             case "Online":
                                 {
                                     recurringDetails.ModeOfPayment = ModeOfPayment.Online;
                                     break;
                                 }
                         }
                         receipt.RecurringDetails.Add(recurringDetails);
                     }
                     catch (Exception)
                     {
                         continue;
                     }
                 }
                 // saving image here
                 try
                 {
                     var signature = new SignatureImage { Filename = model.SignatureImage.FileName };
                     Stream fileStream = model.SignatureImage.InputStream;
                     int fileLength = model.SignatureImage.ContentLength;
                     signature.Filedata = new byte[fileLength];
                     fileStream.Read(signature.Filedata, 0, fileLength);
                     signature.MimeType = model.SignatureImage.ContentType;
                     signature.ID = Guid.NewGuid();
                     receipt.SignatureImage = signature;
                 }
                 catch
                 {
                 }
                 scope.Add(receipt);
                 scope.Transaction.Commit();
                 ViewData["ReceiptID"] = receipt.ReceiptNumber;
                 return View("Printoptions");
             }
         }
         LoadReceiptValuesFromDb(scope);
         ViewData["PostAction"] = "RecurringReceipt";
         ViewData["selectedModeOfPayment"] = Request.Form["cmbModeOfPayment"];
         ViewData["selectedDonationReceivedBy"] = Request.Form["CmbDonationReceivedBy"];
         ModelState.AddModelError("", "Unable to generate receipt due to invalid parameter passed.");
         return View();
     }
     ViewData["Status"] = "You are not authorized to do this operation";
     return View("PartialViewStatus");
 }
        public ActionResult ImportfromExcel(ExcelModels model, HttpPostedFileBase excelFile, HttpPostedFileBase signatureFile)
        {
            if (User.Identity.IsAuthenticated)
            {
                ViewData["Status"] = string.Empty;
                var scope = ObjectScopeProvider1.GetNewObjectScope();
                if (CheckAdminauthorization(scope, User.Identity.Name))
                {
                    if (ModelState.IsValid)
                    {
                        OleDbConnection connection = null;
                        try
                        {
                            // saving image here
                            SignatureImage signature;
                            try
                            {
                                signature = new SignatureImage { Filename = model.SignatureFile.FileName };
                                Stream fileStream = model.SignatureFile.InputStream;
                                int fileLength = model.SignatureFile.ContentLength;
                                signature.Filedata = new byte[fileLength];
                                fileStream.Read(signature.Filedata, 0, fileLength);
                                signature.MimeType = model.SignatureFile.ContentType;
                                signature.ID = Guid.NewGuid();
                            }
                            catch
                            {
                                signature = null;
                            }
                            if (signature != null)
                            {
                                string directory = Server.MapPath("/App_Data");
                                if (Directory.Exists(directory))
                                    Directory.CreateDirectory(directory);
                                string filePath = Path.Combine(directory, excelFile.FileName);
                                if (System.IO.File.Exists(filePath))
                                    System.IO.File.Delete(filePath);
                                excelFile.SaveAs(filePath);
                                string connectionString = string.Empty;
                                if (Path.GetExtension(filePath) == ".xls")
                                    connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath +
                                                       ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                                else if (Path.GetExtension(filePath) == ".xlsx")
                                    connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath +
                                                       ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                                bool isReceptsGenerated = false;
                                var groupId = DateTime.Now.ToString("%s%MM%mm%yy%dd%HH");
                                if (!string.IsNullOrEmpty(connectionString))
                                {
                                    connection = new OleDbConnection(connectionString);
                                    var cmd = new OleDbCommand { CommandType = CommandType.Text, Connection = connection };
                                    var dAdapter = new OleDbDataAdapter(cmd);
                                    var dtExcelRecords = new DataTable();
                                    connection.Open();
                                    DataTable dtExcelSheetName = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
                                                                                                null);
                                    if (dtExcelSheetName != null && dtExcelSheetName.Rows.Count > 0)
                                    {
                                        string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
                                        cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
                                        dAdapter.SelectCommand = cmd;
                                        dAdapter.Fill(dtExcelRecords);

                                        // Started to create recepts now)
                                        foreach (DataRow dataRow in dtExcelRecords.Rows)
                                        {
                                            string receiptType = string.Empty,
                                                   firstname = string.Empty,
                                                   mi = string.Empty,
                                                   lastname = string.Empty,
                                                   address = string.Empty,
                                                   address2 = string.Empty,
                                                   city = string.Empty,
                                                   state = string.Empty,
                                                   zipcode = string.Empty,
                                                   email = string.Empty,
                                                   contact = string.Empty,
                                                   datereceived = string.Empty,
                                                   issueddate = string.Empty,
                                                   donationamount = string.Empty,
                                                   donationAmountinwords = string.Empty,
                                                   recurringDetails = string.Empty,
                                                   merchandiseItem = string.Empty,
                                                   quantity = string.Empty,
                                                   value = string.Empty,
                                                   servicetype = string.Empty,
                                                   hoursServed = string.Empty,
                                                   rateperhour = string.Empty,
                                                   fmvvalue = string.Empty,
                                                   modeOfPayment = string.Empty,
                                                   receivedBy = string.Empty;
                                            if (dataRow[0] != null && !string.IsNullOrEmpty(dataRow[0].ToString()))
                                                receiptType = dataRow[0].ToString();
                                            if (dataRow[1] != null && !string.IsNullOrEmpty(dataRow[1].ToString()))
                                                firstname = dataRow[1].ToString();
                                            if (dataRow[2] != null && !string.IsNullOrEmpty(dataRow[2].ToString()))
                                                mi = dataRow[2].ToString();
                                            if (dataRow[3] != null && !string.IsNullOrEmpty(dataRow[3].ToString()))
                                                lastname = dataRow[3].ToString();
                                            if (dataRow[4] != null && !string.IsNullOrEmpty(dataRow[4].ToString()))
                                                address = dataRow[4].ToString();
                                            if (dataRow[5] != null && !string.IsNullOrEmpty(dataRow[5].ToString()))
                                                address2 = dataRow[5].ToString();
                                            if (dataRow[6] != null && !string.IsNullOrEmpty(dataRow[6].ToString()))
                                                city = dataRow[6].ToString();
                                            if (dataRow[7] != null && !string.IsNullOrEmpty(dataRow[7].ToString()))
                                                state = dataRow[7].ToString();
                                            if (dataRow[8] != null && !string.IsNullOrEmpty(dataRow[8].ToString()))
                                                zipcode = dataRow[8].ToString();
                                            if (dataRow[9] != null && !string.IsNullOrEmpty(dataRow[9].ToString()))
                                                email = dataRow[9].ToString();
                                            if (dataRow[10] != null && !string.IsNullOrEmpty(dataRow[10].ToString()))
                                                contact = dataRow[10].ToString();
                                            if (dataRow[11] != null && !string.IsNullOrEmpty(dataRow[11].ToString()))
                                                datereceived = dataRow[11].ToString();
                                            if (dataRow[12] != null && !string.IsNullOrEmpty(dataRow[12].ToString()))
                                                issueddate = dataRow[12].ToString();
                                            if (dataRow[13] != null && !string.IsNullOrEmpty(dataRow[13].ToString()))
                                                donationamount = dataRow[13].ToString();
                                            if (dataRow[14] != null && !string.IsNullOrEmpty(dataRow[14].ToString()))
                                                donationAmountinwords = dataRow[14].ToString();
                                            if (dataRow[15] != null && !string.IsNullOrEmpty(dataRow[15].ToString()))
                                                recurringDetails = dataRow[15].ToString();
                                            if (dataRow[16] != null && !string.IsNullOrEmpty(dataRow[16].ToString()))
                                                merchandiseItem = dataRow[16].ToString();
                                            if (dataRow[17] != null && !string.IsNullOrEmpty(dataRow[17].ToString()))
                                                quantity = dataRow[17].ToString();
                                            if (dataRow[18] != null && !string.IsNullOrEmpty(dataRow[18].ToString()))
                                                value = dataRow[18].ToString();
                                            if (dataRow[19] != null && !string.IsNullOrEmpty(dataRow[19].ToString()))
                                                servicetype = dataRow[19].ToString();
                                            if (dataRow[20] != null && !string.IsNullOrEmpty(dataRow[20].ToString()))
                                                hoursServed = dataRow[20].ToString();
                                            if (dataRow[21] != null && !string.IsNullOrEmpty(dataRow[21].ToString()))
                                                rateperhour = dataRow[21].ToString();
                                            if (dataRow[22] != null && !string.IsNullOrEmpty(dataRow[22].ToString()))
                                                fmvvalue = dataRow[22].ToString();
                                            if (dataRow[23] != null && !string.IsNullOrEmpty(dataRow[23].ToString()))
                                                modeOfPayment = dataRow[23].ToString();
                                            if (dataRow[24] != null && !string.IsNullOrEmpty(dataRow[24].ToString()))
                                                receivedBy = dataRow[24].ToString();
                                            if (!string.IsNullOrEmpty(receiptType) && !string.IsNullOrEmpty(firstname) &&
                                                !string.IsNullOrEmpty(receivedBy))
                                            {
                                                List<saibabacharityreceiptorDL.User> receiver =
                                                    (from c in scope.GetOqlQuery<saibabacharityreceiptorDL.User>().ExecuteEnumerable()
                                                     where c.Username.ToLower().Equals(receivedBy.ToLower().Trim())
                                                     select c).ToList();
                                                if (receiver.Count > 0)
                                                {
                                                    var receipt = new Receipt
                                                                      {
                                                                          ReceiptNumber = Utilities.GenerateReceiptId(),
                                                                          FirstName = firstname,
                                                                          Mi = mi,
                                                                          LastName = lastname,
                                                                          Address = address,
                                                                          Address2 = address2,
                                                                          City = city,
                                                                          State = state,
                                                                          ZipCode = zipcode,
                                                                          Email = email,
                                                                          Contact = contact,
                                                                          IssuedDate = Convert.ToDateTime(issueddate),
                                                                          DonationReceiver = receiver[0],
                                                                          GroupId = groupId,
                                                                          SignatureImage = signature
                                                                      };
                                                    if (receiptType.ToLower().Trim() != "recurring receipt")
                                                        receipt.DateReceived = Convert.ToDateTime(datereceived);
                                                    switch (receiptType.ToLower().Trim())
                                                    {
                                                        case "regular receipt":
                                                            {
                                                                if (string.IsNullOrEmpty(modeOfPayment) ||
                                                                    string.IsNullOrEmpty(donationamount) ||
                                                                    string.IsNullOrEmpty(donationAmountinwords))
                                                                    continue;
                                                                receipt.DonationAmount = donationamount;
                                                                receipt.DonationAmountinWords = donationAmountinwords;
                                                                switch (modeOfPayment.ToLower().Trim())
                                                                {
                                                                    case "cash":
                                                                        {
                                                                            receipt.ModeOfPayment =
                                                                                ModeOfPayment.Cash;
                                                                            break;
                                                                        }
                                                                    case "cheque":
                                                                        {
                                                                            receipt.ModeOfPayment =
                                                                                ModeOfPayment.
                                                                                    Cheque;
                                                                            break;
                                                                        }
                                                                    case "goods":
                                                                        {
                                                                            receipt.ModeOfPayment =
                                                                                ModeOfPayment.
                                                                                    Goods;
                                                                            break;
                                                                        }
                                                                    case "online":
                                                                        {
                                                                            receipt.ModeOfPayment =
                                                                                ModeOfPayment.
                                                                                    Online;
                                                                            break;
                                                                        }
                                                                    case "mobile":
                                                                        {
                                                                            receipt.ModeOfPayment =
                                                                                ModeOfPayment.
                                                                                    Mobile;
                                                                            break;
                                                                        }
                                                                }
                                                                break;
                                                            }
                                                        case "recurring receipt":
                                                            {
                                                                if (string.IsNullOrEmpty(donationAmountinwords))
                                                                    continue;
                                                                receipt.DonationAmountinWords = donationAmountinwords;
                                                                break;
                                                            }
                                                    }
                                                    switch (receiptType.ToLower().Trim())
                                                    {
                                                        case "regular receipt":
                                                            {
                                                                receipt.ReceiptType = ReceiptType.GeneralReceipt;
                                                                break;
                                                            }
                                                        case "recurring receipt":
                                                            {
                                                                receipt.ReceiptType = ReceiptType.RecurringReceipt;
                                                                string[] recurrenceDetials = recurringDetails.Split(')');
                                                                foreach (string recurrenceDetial in recurrenceDetials)
                                                                {
                                                                    try
                                                                    {
                                                                        var values =
                                                                            recurrenceDetial.Replace("(", "").Split('-');
                                                                        if (values.Count() > 2)
                                                                        {
                                                                            var recurring = new RecurringDetails
                                                                                                {
                                                                                                    DueDate =
                                                                                                        Convert.
                                                                                                        ToDateTime(
                                                                                                            values[0]),
                                                                                                    Amount = values[2]
                                                                                                };
                                                                            switch (values[1].ToLower().Trim())
                                                                            {
                                                                                case "cash":
                                                                                    {
                                                                                        recurring.ModeOfPayment =
                                                                                            ModeOfPayment.Cash;
                                                                                        break;
                                                                                    }
                                                                                case "cheque":
                                                                                    {
                                                                                        recurring.ModeOfPayment =
                                                                                            ModeOfPayment.Cheque;
                                                                                        break;
                                                                                    }
                                                                                case "goods":
                                                                                    {
                                                                                        recurring.ModeOfPayment =
                                                                                            ModeOfPayment.Goods;
                                                                                        break;
                                                                                    }
                                                                                case "online":
                                                                                    {
                                                                                        recurring.ModeOfPayment =
                                                                                            ModeOfPayment.Online;
                                                                                        break;
                                                                                    }
                                                                                case "mobile":
                                                                                    {
                                                                                        recurring.ModeOfPayment =
                                                                                            ModeOfPayment.Mobile;
                                                                                        break;
                                                                                    }
                                                                            }
                                                                            receipt.RecurringDetails.Add(recurring);
                                                                        }
                                                                    }
                                                                    catch (Exception)
                                                                    {
                                                                        continue;
                                                                    }
                                                                }
                                                                break;
                                                            }
                                                        case "merchandise receipt":
                                                            {
                                                                if (string.IsNullOrEmpty(merchandiseItem) ||
                                                                    string.IsNullOrEmpty(value) ||
                                                                    string.IsNullOrEmpty(quantity))
                                                                    continue;
                                                                receipt.ReceiptType = ReceiptType.MerchandiseReceipt;
                                                                receipt.MerchandiseItem = merchandiseItem;
                                                                receipt.Quantity = quantity;
                                                                receipt.FmvValue = value;
                                                                break;
                                                            }
                                                        case "services receipt":
                                                            {
                                                                if (string.IsNullOrEmpty(servicetype) ||
                                                                    string.IsNullOrEmpty(hoursServed) ||
                                                                    string.IsNullOrEmpty(rateperhour) ||
                                                                    string.IsNullOrEmpty(fmvvalue))
                                                                    continue;
                                                                receipt.ReceiptType = ReceiptType.ServicesReceipt;
                                                                receipt.ServiceType = servicetype;
                                                                try
                                                                {
                                                                    receipt.HoursServed = Convert.ToDouble(hoursServed);
                                                                }
                                                                catch (Exception)
                                                                {
                                                                    continue;
                                                                }
                                                                double rateperHour;
                                                                double fmvValue;
                                                                try
                                                                {
                                                                    rateperHour = Convert.ToDouble(rateperhour);
                                                                    fmvValue = Convert.ToDouble(fmvvalue);
                                                                }
                                                                catch (Exception)
                                                                {
                                                                    continue;
                                                                }
                                                                receipt.RatePerHrOrDay = rateperHour.ToString();
                                                                receipt.FmvValue = fmvValue.ToString();
                                                                break;
                                                            }
                                                        default:
                                                            {
                                                                continue;
                                                            }
                                                    }
                                                    scope.Transaction.Begin();
                                                    scope.Add(receipt);
                                                    scope.Transaction.Commit();
                                                    isReceptsGenerated = true;
                                                    Thread.Sleep(100);
                                                }
                                            }
                                        }
                                    }
                                    connection.Close();
                                }
                                if (isReceptsGenerated)
                                {
                                    ViewData["ReceiptID"] = groupId;
                                    return View("Printoptions");
                                }
                                else
                                {
                                    ViewData["Status"] =
                                        "Excel import process completed successfully. There is some invalid entry found in your excel file.";
                                    return View();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            ViewData["Status"] = "Unable to read data from file, please input your data with the specified format.";
                            ModelState.AddModelError("", "Unable to import from excel due to " + ex.Message);
                        }
                        finally
                        {
                            try
                            {
                                if (connection != null)
                                {
                                    connection.Close();
                                    connection.Dispose();
                                }
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                    // If we got this far, something failed, redisplay form
                    return View(model);
                }
                ViewData["Status"] = "You are not authorized to do this operation";
                return View("PartialViewStatus");
            }
            ViewData["Status"] = "Your session has been expired, please login again and try.";
            return View("PartialViewStatus");
        }