コード例 #1
0
        public ActionResult Index(string submitButton, PurchaseViewModels model, HttpPostedFileBase file)
        {
            try
            {
                ApplicationUser currentUser = ApplicationUserManager.GetApplicationUser(User.Identity.Name, HttpContext.GetOwinContext());

                if (submitButton == "Save")
                {
                    //vendor = Request.Form["PurchaseQuotation.Vendor_Code"].ToString();
                    //model.PurchaseQuotation.Vendor_Code = Convert.ToInt32(vendor.Remove(0,1));
                    model.PurchaseQuotation.Quotation_Status   = "Open";
                    model.PurchaseQuotation.Created_Branc_Id   = currentUser.Created_Branch_Id;
                    model.PurchaseQuotation.Created_Date       = DateTime.Now;
                    model.PurchaseQuotation.Created_User_Id    = currentUser.Created_User_Id;    //GetUserId()
                    model.PurchaseQuotation.Creating_Branch    = currentUser.Created_Branch_Id;; //GetBranch
                    model.PurchaseQuotation.Modified_User_Id   = currentUser.Modified_User_Id;
                    model.PurchaseQuotation.Modified_Date      = DateTime.Now;
                    model.PurchaseQuotation.Modified_Branch_Id = currentUser.Modified_Branch_Id;


                    var QuotationList = model.PurchaseQuotationItemList.Where(x => x.IsDummy == 0);
                    model.PurchaseQuotationItemList = QuotationList.ToList();

                    for (int i = 0; i < model.PurchaseQuotationItemList.Count; i++)
                    {
                        model.PurchaseQuotationItemList[i].Quoted_qty  = model.PurchaseQuotationItemList[i].Required_qty;
                        model.PurchaseQuotationItemList[i].Quoted_date = model.PurchaseQuotationItemList[i].Required_date;
                    }

                    if (purchaseDb.AddNewQuotation(model.PurchaseQuotation, model.PurchaseQuotationItemList, ref ErrorMessage))
                    {
                        XMLGenerate_SAPInsert(model);
                        //for (int i = 0; i < model.PurchaseQuotationItemList.Count; i++)
                        //{
                        //    XMLGenerate_Quotation_SAPInsert(model.PurchaseQuotationItemList[i]);
                        //}
                        return(RedirectToAction("Index", "Purchase"));
                    }
                    else
                    {
                        ViewBag.AppErrorMessage = ErrorMessage;
                        return(View("Error"));
                    }
                }
                else if (submitButton == "Update")
                {
                    //vendor = Request.Form["PurchaseQuotation.Vendor_Code"].ToString();
                    //model.PurchaseQuotation.Vendor_Code = Convert.ToInt32(vendor.Remove(0, 1));
                    Temp_Purchase = Convert.ToString(TempData["oldPurchaseQuotation_Name"]);
                    model.PurchaseQuotation.Created_Branc_Id   = currentUser.Created_Branch_Id;
                    model.PurchaseQuotation.Created_Date       = DateTime.Now;
                    model.PurchaseQuotation.Created_User_Id    = currentUser.Created_User_Id;   //GetUserId()
                    model.PurchaseQuotation.Creating_Branch    = currentUser.Created_Branch_Id; //GetBranch
                    model.PurchaseQuotation.Modified_User_Id   = currentUser.Modified_User_Id;
                    model.PurchaseQuotation.Modified_Date      = DateTime.Now;
                    model.PurchaseQuotation.Modified_Branch_Id = currentUser.Modified_Branch_Id;

                    var QuotationList = model.PurchaseQuotationItemList.Where(x => x.IsDummy == 0);
                    model.PurchaseQuotationItemList = QuotationList.ToList();

                    //for (int i = 0; i < model.PurchaseQuotationItemList.Count; i++)
                    //{
                    //    model.PurchaseQuotationItemList[i].Quoted_qty = 10; //GetQuantity()
                    //    model.PurchaseQuotationItemList[i].Quoted_date = DateTime.Now;
                    //}

                    if (purchaseDb.UpdateQuotation(model.PurchaseQuotation, model.PurchaseQuotationItemList, ref ErrorMessage))
                    {
                        XMLGenerate_SAPUpdate(model);

                        return(RedirectToAction("Index", "Purchase"));
                    }
                    else
                    {
                        ViewBag.AppErrorMessage = ErrorMessage;
                        return(View("Error"));
                    }
                }
                else if (submitButton == "Export")
                {
                    _ExporttoExcel();
                }

                if (Convert.ToString(Request.Files["FileUpload"]).Length > 0)
                {
                    try
                    {
                        string fileExtension = System.IO.Path.GetExtension(Request.Files["FileUpload"].FileName);

                        string fileName = System.IO.Path.GetFileName(Request.Files["FileUpload"].FileName.ToString());

                        if (fileExtension == ".xls" || fileExtension == ".xlsx")
                        {
                            if (UploadExcelData(fileExtension, fileName, ref ErrorMessage))
                            {
                                return(Json(new { success = true, Message = "File Uploaded Successfully" }, JsonRequestBehavior.AllowGet));
                            }
                            else
                            {
                                return(Json(new { success = false, Message = ErrorMessage }, JsonRequestBehavior.AllowGet));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        return(Json(new { success = false, Error = "File Upload failed :" + ex.Message }, JsonRequestBehavior.AllowGet));
                    }
                }

                return(RedirectToAction("Index", "Purchase"));
            }
            catch (Exception ex)
            {
                ExceptionHandler.LogException(ex);
                ViewBag.AppErrorMessage = ex.Message;
                return(View("Error"));
            }
        }