Beispiel #1
0
        public ActionResult CreateCustomer(ERPAPD_Customer cus, HttpPostedFileBase file)
        {
            if (asset.Create)
            {
                ERPAPD_Customer item = new ERPAPD_Customer();
                try
                {
                    using (IDbConnection dbConn = ERPAPD.Helpers.OrmliteConnection.openConn())
                    {
                        item.CustomerID   = getLastIdCustomer();
                        item.ShortName    = cus.ShortName;
                        item.CustomerName = cus.CustomerName;
                        item.TaxCode      = cus.TaxCode;
                        item.UnitId       = cus.UnitId;
                        item.Website      = cus.Website;
                        item.Source       = cus.Source;
                        item.CompanyType  = cus.CompanyType;
                        item.Category     = cus.Category;
                        item.CustomerType = cus.CustomerType;
                        //BaoHV
                        item.AgencyType = cus.AgencyType;
                        item.AgencyRule = cus.AgencyRule;
                        //End
                        item.Area     = cus.Area;
                        item.Country  = cus.Country;
                        item.Province = cus.Province;
                        item.District = cus.District;
                        item.Wards    = cus.Wards;
                        item.Address  = cus.Address;
                        item.Phone    = cus.Phone;
                        item.Address2 = cus.Address2;
                        item.Phone2   = cus.Phone2;
                        item.Fax      = cus.Fax;
                        item.Account  = cus.Account;
                        //item.BankCode = cus.BankCode;
                        item.BankName       = cus.BankName;
                        item.BankBranch     = cus.BankBranch;
                        item.Sponsor        = cus.Sponsor;
                        item.Position       = cus.Position;
                        item.StaffId        = cus.StaffId;
                        item.Status         = "MOI";
                        item.RowCreatedUser = currentUser.UserName;
                        item.RowCreatedAt   = DateTime.Now;

                        if (file != null && file.ContentLength > 0)
                        {
                            var fileName = Path.GetFileName(file.FileName);
                            Helpers.UploadFile.CreateFolder(Server.MapPath("~/Images/" + item.CustomerID + "/"));
                            var path = Path.Combine(Server.MapPath("~/Images/" + item.CustomerID + "/"), fileName);
                            file.SaveAs(path);
                            item.FileName = fileName;
                        }

                        dbConn.Insert <ERPAPD_Customer>(item);
                    }
                }
                catch (Exception e)
                {
                    return(Json(new { success = false, message = e.Message }));
                }
                //return RedirectToAction("Index", "MnDetailEmployer", new { CustomerID = item.CustomerID });
                return(Json(new { success = true }));
            }
            else
            {
                return(Json(new { success = false, message = "Bạn không có quyền." }));
            }
        }
Beispiel #2
0
        public ActionResult ImportFromExcel()
        {
            try
            {
                using (IDbConnection dbConn = Helpers.OrmliteConnection.openConn())
                {
                    if (Request.Files["FileUpload"] != null && Request.Files["FileUpload"].ContentLength > 0)
                    {
                        string fileExtension =
                            System.IO.Path.GetExtension(Request.Files["FileUpload"].FileName);

                        if (fileExtension == ".xlsx")
                        {
                            string fileLocation      = string.Format("{0}/{1}", Server.MapPath("~/Excel"), "[" + currentUser.UserName + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "]" + Request.Files["FileUpload"].FileName);
                            string errorFileLocation = string.Format("{0}/{1}", Server.MapPath("~/Excel"), "[" + currentUser.UserName + "-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "-Error]" + Request.Files["FileUpload"].FileName);

                            if (System.IO.File.Exists(fileLocation))
                            {
                                System.IO.File.Delete(fileLocation);
                            }

                            Request.Files["FileUpload"].SaveAs(fileLocation);

                            var rownumber = 2;
                            var total     = 0;

                            FileInfo fileInfo = new FileInfo(fileLocation);
                            var      excelPkg = new ExcelPackage(fileInfo);

                            FileInfo template = new FileInfo(Server.MapPath(@"~\ExportExcelFile\TW_Goods_Category.xlsx"));
                            template.CopyTo(errorFileLocation);
                            FileInfo _fileInfo = new FileInfo(errorFileLocation);
                            var      _excelPkg = new ExcelPackage(_fileInfo);

                            ExcelWorksheet oSheet = excelPkg.Workbook.Worksheets["TW_Goods_Category"];
                            ExcelWorksheet eSheet = _excelPkg.Workbook.Worksheets["TW_Goods_Category"];

                            //remove row
                            int totalRows = oSheet.Dimension.End.Row;
                            for (int i = 2; i <= totalRows; i++)
                            {
                                string CatID = oSheet.Cells[i, 1].Value != null ? oSheet.Cells[i, 1].Value.ToString() : "0";
                                try
                                {
                                    var write          = new ERPAPD_Customer();
                                    var checkCatNumber = dbConn.FirstOrDefault <ERPAPD_Customer>("CatId={0}", CatID);
                                    if (checkCatNumber != null)
                                    {
                                        //write.Note = Note;
                                        dbConn.Update(write);
                                        total++;
                                    }
                                    else
                                    {
                                        //write.Note = Note;
                                        dbConn.Insert(write);
                                        total++;
                                    }
                                }
                                catch (Exception e)
                                {
                                    //eSheet.Cells[rownumber, 2].Value = CatName;
                                    //eSheet.Cells[rownumber, 3].Value = Status;
                                    //eSheet.Cells[rownumber, 5].Value = CatNumber;
                                    eSheet.Cells[rownumber, 10].Value = e.Message;
                                    rownumber++;
                                    continue;
                                }
                            }
                            _excelPkg.Save();

                            return(Json(new { success = true, total = total, totalError = rownumber - 2, link = errorFileLocation }));
                        }
                        else
                        {
                            return(Json(new { success = false, error = "File extension is not valid. *.xlsx please." }));
                        }
                    }
                    else
                    {
                        return(Json(new { success = false, error = "File upload null" }));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Json(new { success = false, error = ex.Message }));
            }
        }
        public ActionResult Customer_Update(ERPAPD_Customer request)
        {
            using (IDbConnection dbConn = ERPAPD.Helpers.OrmliteConnection.openConn())
            {
                try
                {
                    if (!string.IsNullOrEmpty(request.CustomerID))
                    {
                        var customer = dbConn.FirstOrDefault <ERPAPD_Customer>("CustomerID={0}", request.CustomerID);

                        if (!string.IsNullOrEmpty(request.ShortName))
                        {
                            customer.ShortName = request.ShortName.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.CustomerName))
                        {
                            customer.CustomerName = request.CustomerName.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.TaxCode))
                        {
                            customer.TaxCode = request.TaxCode.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.Source))
                        {
                            customer.Source = request.Source.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.CompanyType))
                        {
                            customer.CompanyType = request.CompanyType.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.Category))
                        {
                            customer.Category = request.Category.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.CustomerType))
                        {
                            customer.CustomerType = request.CustomerType.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.Area))
                        {
                            customer.Area = request.Area.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.Country))
                        {
                            customer.Country = request.Country.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.District))
                        {
                            customer.District = request.District.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.Province))
                        {
                            customer.Province = request.Province.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.Wards))
                        {
                            customer.Wards = request.Wards.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.Address))
                        {
                            customer.Address = request.Address.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.Address2))
                        {
                            customer.Address2 = request.Address2.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.Phone))
                        {
                            customer.Phone = request.Phone.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.Phone2))
                        {
                            customer.Phone2 = request.Phone2.Trim();
                        }

                        if (!string.IsNullOrEmpty(request.StaffId.ToString()))
                        {
                            customer.StaffId = int.Parse(request.StaffId.ToString());
                        }
                        if (!string.IsNullOrEmpty(request.Fax))
                        {
                            customer.Fax = request.Fax.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.BankCode))
                        {
                            customer.BankCode = request.BankCode.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.BankBranch))
                        {
                            customer.BankBranch = request.BankBranch.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.BankName))
                        {
                            customer.BankName = request.BankName.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.Sponsor))
                        {
                            customer.Sponsor = request.Sponsor.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.Position))
                        {
                            customer.Position = request.Position.Trim();
                        }
                        if (!string.IsNullOrEmpty(request.Website))
                        {
                            //Array arr = request.Website.Trim().Split(',');

                            customer.Website = request.Website;
                        }
                        //BaoHV add
                        customer.AgencyType     = !string.IsNullOrEmpty(request.AgencyType)? request.AgencyType: customer.AgencyType;
                        customer.AgencyRule     = request.AgencyRule;
                        customer.RowUpdatedAt   = DateTime.Now;
                        customer.RowUpdatedUser = currentUser.UserName;
                        dbConn.Update(customer);
                        return(Json(new { success = true }));
                    }
                    else
                    {
                        ModelState.AddModelError("error", "");
                        return(Json(new { success = false }));
                    }
                }
                catch (Exception e)
                {
                    return(Json(new { success = false, message = e.Message }));
                }
            }
        }