Ejemplo n.º 1
0
        public ActionResult Invoice(InvoiceViewModel invoiceModel, HttpPostedFileBase file, FormCollection fc)
        {
            BCBSClient client = new BCBSClient();
            //ModelState.Where(m => m.Key == "CustomerCode").FirstOrDefault().Value.Errors.Clear();
            //if (ModelState.IsValid)
            //{
            long Id = 0;
            string fname = string.Empty;
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(file.FileName);
                string ext = Path.GetExtension(file.FileName);
                Guid g = Guid.NewGuid();

                fname = g.ToString() + ext;
                // store the file inside ~/UploadDocuments/uploads folder
                var path = Path.Combine(Server.MapPath("~/UploadDocuments/uploads"), fname);
                file.SaveAs(path);
                invoiceModel.SupportingDocuments = fname;
            }

            //string csv = string.Empty;
            Id = client.InsertCustomerInvoice(invoiceModel.InvoiceNumber, invoiceModel.CustomerId, invoiceModel.InvoiceDate, invoiceModel.PrepareBy, invoiceModel.PrepareByExt, invoiceModel.AuthorizedBy, invoiceModel.AuthorizedByExt, invoiceModel.Division, invoiceModel.IsDeffered, invoiceModel.DefferedAccount, invoiceModel.FromDate, invoiceModel.ToDate, invoiceModel.SpecialInstuction, invoiceModel.SupportingDocuments, invoiceModel.TotalAmount);
            if (Id > 0)
            {
                //csv += "Invoice Number," + invoiceModel.InvoiceNumber + "\n";
                //csv += "Invoice Date," + invoiceModel.InvoiceDate.ToString("MM/dd/yyyy") + "\n";
                //csv += "Prepared by," + invoiceModel.PrepareBy + ", Authorized By," + invoiceModel.AuthorizedBy + "\n";
                //csv += "Devision," + invoiceModel.Division + "\n\n";

                //csv += "Customer Name," + invoiceModel.Customer.Name + "\n";
                //csv += "Address\n";
                //csv += invoiceModel.Customer.CustomerAddress + " " + invoiceModel.Customer.City + " " + invoiceModel.Customer.State + " " + invoiceModel.Customer.PostalCode + "\n";
                //csv += "Contact Name," + invoiceModel.Customer.FirstName + " " + invoiceModel.Customer.LastName + "\n";
                //csv += "Phone," + invoiceModel.Customer.Phone + "\n\n";

                SBFEmailViewModel sbfemail = new SBFEmailViewModel();
                //mailer.SBF();
                string fcActivities = fc["Activities"].ToString();
                List<string> activities = new List<string>();
                if (!string.IsNullOrEmpty(fcActivities))
                {

                    bool isbilled = client.SetActivityBilled(fcActivities);
                    if (isbilled)
                    {
                        var activityIds = fcActivities.Split(',');
                        foreach (var x in activityIds)
                        {
                            if (!string.IsNullOrEmpty(x))
                            {
                                long activityId = Convert.ToInt64(x);
                                long sbfActivityId = client.InsertSBFActivity(Id, activityId);
                                if (sbfActivityId > 0)
                                {
                                    activities.Add(activityId.ToString());

                                    continue;
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                if (activities != null && activities.Count > 0)
                {
                    string ids = string.Join(",", activities.ToArray());
                    string result = client.GetActivitiesByActivityIds(ids);
                    List<ActivityListModel> activity = JsonConvert.DeserializeObject<List<ActivityListModel>>(result);
                    if (sbfemail.ActivityList == null)
                    {
                        //csv += "Contract -Activity\n\n";
                        //csv += "Project Name,Service Type,From Date,End Date,Charges,Value,Amount\n";
                        sbfemail.ActivityList = new List<ActivityListModel>();
                    }
                    if (activity != null)
                    {
                        foreach (ActivityListModel x in activity)
                        {
                            string charge = string.Empty;
                            if (x.Charges == true) { charge = "Expense"; } else { charge = "Revenue"; }
                            string estimate = string.Empty;

                            if (x.Estimate == true) { estimate = "Real"; } else { estimate = "Estimate"; }

                            //csv += x.ProjectName + "," + x.Service + "," + x.FromDate.ToString("MM/dd/yyyy") + "," + x.EndDate.ToString("MM/dd/yyyy") + "," + charge + "," + estimate + "," + x.Amount + "\n";
                        }
                        //csv += ",,,,,Total," + invoiceModel.TotalAmount + "\n\n";
                        //if (invoiceModel.IsDeffered == true)
                        //{
                        //csv += "Deffered Account," + invoiceModel.DefferedAccount + "\n";
                        //}
                        //csv += "From Date," + invoiceModel.FromDate.ToString("MM/dd/yyyy") + ",,End Date," + invoiceModel.ToDate.ToString("MM/dd/yyyy") + "\n";

                        sbfemail.ActivityList = activity;
                    }
                }

                IUserMailer mailer = new UserMailer();

                sbfemail.Invoice = invoiceModel;
                string CustomerData = client.GetcustomerById(invoiceModel.CustomerId);
                if (!string.IsNullOrEmpty(CustomerData))
                {
                    sbfemail.Customer = JsonConvert.DeserializeObject<CustomerModel>(CustomerData);
                }
                string ContractData = client.GetcontractById(invoiceModel.ContractId);
                ContractModel contract = new ContractModel();
                if (!string.IsNullOrEmpty(ContractData))
                {
                    contract = JsonConvert.DeserializeObject<ContractModel>(ContractData);
                    sbfemail.Contract = contract;
                }
                string fileName = "SBF_" + sbfemail.Invoice.InvoiceNumber + "_" + DateTime.Now.ToString("MM_dd_yyyy_HH_mm_ss");
                string excelFileName = CreatExcel(sbfemail, fileName);
                mailer.SBF(sbfemail, file, "SBF Invoice - BCBS Acc Sys").Send();
                if (!string.IsNullOrEmpty(excelFileName))
                {
                    string path = Path.Combine(Server.MapPath("~/UploadDocuments/uploads/xlsx"), excelFileName);
                    return File(path, "text/csv", excelFileName);
                }
                else
                {

                    TempData["Message"] = "Customer Invoice generated successfully..!";

                    //string fileName = "SBF_" + invoiceModel.InvoiceNumber + "_" + DateTime.Now.ToString("MM-dd-yyyy");
                    //
                    return RedirectToAction("Index", "Customer");
                }
            }
            else
            {
                string fullPath = Request.MapPath("~/UploadDocuments/uploads/" + fname);
                if (System.IO.File.Exists(fullPath))
                {
                    System.IO.File.Delete(fullPath);
                }
                TempData["Error"] = "Customer Invoice generation failed..!";
                return View(invoiceModel);
            }
            ModelState.Clear();

            //}
            //else
            //{
            //    string customerlist = client.GetcustomerList();
            //    if (!string.IsNullOrEmpty(customerlist))
            //    {
            //        ViewBag.Customers = JsonConvert.DeserializeObject<List<CustomerModel>>(customerlist).Select(x => new { x.Id, x.Name });
            //    }
            //    else
            //    {
            //        ViewBag.Customers = "";
            //    }
            //    return View(invoiceModel);
            //}
        }