public JsonResult Update([FromBody]MaterialSBatchModel obj)
 {
     var msg = new JMessage() { Error = false };
     try
     {
         var item = _context.MaterialStoreBatchGoodss.FirstOrDefault(x => x.Code == obj.Code);
         item.Name = obj.Name;
         item.ProductCode = obj.ProductCode;
         item.StoreId = obj.StoreId;
         item.Quantity = obj.Quantity;
         item.Unit = obj.Unit;
         item.Vat = obj.Vat;
         item.Cost = obj.Cost;
         item.Currency = obj.Currency;
         item.Barcode = obj.Barcode;
         item.SupplierId = obj.SupplierId;
         item.Madein = obj.Madein;
         item.Packing = obj.Packing;
         item.Sale = obj.Sale;
         item.Description = obj.Description;
         item.DateProduce = !string.IsNullOrEmpty(obj.DateProduce) ? DateTime.ParseExact(obj.DateProduce, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null;
         item.DateExpire = !string.IsNullOrEmpty(obj.DateExpire) ? DateTime.ParseExact(obj.DateExpire, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null;
         item.DateReiceive = !string.IsNullOrEmpty(obj.DateReiceive) ? DateTime.ParseExact(obj.DateReiceive, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null;
         item.UpdatedBy = ESEIM.AppContext.UserName;
         item.UpdatedTime = DateTime.Now;
         //double? total = 0;
         //if (obj.Quantity != null && obj.Cost != null)
         //{
         //    total = obj.Quantity * obj.Cost;
         //    if (obj.Sale != null)
         //    {
         //        total = (total * obj.Sale) / 100;
         //    }
         //    if (obj.Vat != null)
         //    {
         //        total = (total * obj.Vat) / 100;
         //    }
         //}
         item.Total = obj.Total;
         _context.MaterialStoreBatchGoodss.Update(item);
         _context.SaveChanges();
         msg.Title = "Cập nhật lô hàng thành công";
     }
     catch (Exception ex)
     {
         msg.Error = true;
         msg.Title = "Cập nhật lô hàng lỗi!";
         msg.Object = ex;
     }
     return Json(msg);
 }
        public JsonResult Insert([FromBody]MaterialSBatchModel obj)
        {
            var msg = new JMessage() { Error = false, Title = "" };
            try
            {
                var query = from a in _context.MaterialStoreBatchGoodss
                            where a.Code == obj.Code
                            select a;
                if (query.Count() == 0)
                {
                    var temp = new MaterialStoreBatchGoods
                    {
                        Code = obj.Code,
                        Name = obj.Name,
                        ProductCode = obj.ProductCode,
                        StoreId = obj.StoreId,
                        Quantity = obj.Quantity,
                        Unit = obj.Unit,
                        Vat = obj.Vat,
                        Cost = obj.Cost,
                        Currency = obj.Currency,
                        Barcode = obj.Barcode,
                        SupplierId = obj.SupplierId,
                        Madein = obj.Madein,
                        Packing = obj.Packing,
                        Sale = obj.Sale,
                        DateProduce = !string.IsNullOrEmpty(obj.DateProduce) ? DateTime.ParseExact(obj.DateProduce, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null,
                        DateExpire = !string.IsNullOrEmpty(obj.DateExpire) ? DateTime.ParseExact(obj.DateExpire, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null,
                        DateReiceive = !string.IsNullOrEmpty(obj.DateReiceive) ? DateTime.ParseExact(obj.DateReiceive, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null,
                        Description = obj.Description,
                        CreatedBy = ESEIM.AppContext.UserName,
                        CreatedTime = DateTime.Now
                    };
                    //double? total = 0;
                    //if (obj.Quantity != null && obj.Cost != null)
                    //{
                    //    total = obj.Quantity * obj.Cost;
                    //    if (obj.Sale != null)
                    //    {
                    //        total = (total * obj.Sale) / 100;
                    //    }
                    //    if (obj.Vat != null)
                    //    {
                    //        total = (total * obj.Vat) / 100;
                    //    }
                    //}
                    temp.Total = obj.Total;
                    _context.MaterialStoreBatchGoodss.Add(temp);
                    _context.SaveChanges();
                    msg.Title = "Thêm lô hàng thành công";
                }
                else
                {
                    msg.Error = true;
                    msg.Title = "Mã lô hàng đã tồn tại";
                    //return msg;
                }
                return Json(msg);


                //var temp = new MaterialStoreBatchGoods
                //{
                //    Code = obj.Code,
                //    Name = obj.Name,
                //    ProductCode = obj.ProductCode,
                //    StoreId = obj.StoreId,
                //    Quantity = obj.Quantity,
                //    Unit = obj.Unit,
                //    Vat = obj.Vat,
                //    Cost = obj.Cost,
                //    Currency = obj.Currency,
                //    Barcode = obj.Barcode,
                //    SupplierId = obj.SupplierId,
                //    Madein = obj.Madein,
                //    Packing = obj.Packing,
                //    Sale = obj.Sale,
                //    DateProduce = !string.IsNullOrEmpty(obj.DateProduce) ? DateTime.ParseExact(obj.DateProduce, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null,
                //    DateExpire = !string.IsNullOrEmpty(obj.DateExpire) ? DateTime.ParseExact(obj.DateExpire, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null,
                //    DateReiceive = !string.IsNullOrEmpty(obj.DateReiceive) ? DateTime.ParseExact(obj.DateReiceive, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null,
                //    Description = obj.Description,
                //    CreatedBy = ESEIM.AppContext.UserName,
                //    CreatedTime = DateTime.Now
                //};
                //double? total = 0;
                //if (obj.Quantity != null && obj.Cost != null)
                //{
                //    total = obj.Quantity * obj.Cost;
                //    if (obj.Sale != null)
                //    {
                //        total = (total * obj.Sale) / 100;
                //    }
                //    if (obj.Vat != null)
                //    {
                //        total = (total * obj.Vat) / 100;
                //    }
                //}
                //temp.Total = total;
                //_context.MaterialStoreBatchGoodss.Add(temp);
                //_context.SaveChanges();
                //msg.Title = "Thêm lô hàng thành công";
            }
            catch (Exception ex)
            {
                msg.Error = true;
                msg.Title = "Thêm lô hàng lỗi!";
            }
            return Json(msg);
        }