Example #1
0
 private Boolean Save()
 {
     try
     {
         var x = new order_hiv_rapid_test_kit_PhysicalCount
         {
             FacilityCode        = mFacilityCode,
             DateOfPhysicalCount = mDateOfPhysicalCount,
             // string Expiry_Date = temp.ExpiryDate.ToString();
             //DateTime ExpiryTime = DateTime.Parse(Expiry_Date);
             //obj.ExpiryDate = ExpiryTime;
             product_code        = mproduct_code,
             BatchNo             = mBatchNo,
             Quantity_Dispensary = mQuantity_Dispensary,
             Quantity_Store      = mQuantity_Store,
             ExpiryDate          = mExpiryDate,
             Total         = (mQuantity_Dispensary + mQuantity_Store),
             Comment       = mComment,
             AddedBy       = new UserManagement().getCurrentuser(),
             DateAdded     = DateTime.Now,
             record_status = true
         };
         context.order_hiv_rapid_test_kit_PhysicalCount.Add(x);
         context.SaveChanges();
         return(true);
     }
     //catch (Exception x)
     //{
     //    throw (x);
     //}
     catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
     {
         Exception raise = dbEx;
         foreach (var validationErrors in dbEx.EntityValidationErrors)
         {
             foreach (var validationError in validationErrors.ValidationErrors)
             {
                 string message = string.Format("{0}:{1}",
                                                validationErrors.Entry.Entity.ToString(),
                                                validationError.ErrorMessage);
                 // raise a new exception nesting
                 // the current instance as InnerException
                 raise = new InvalidOperationException(message, raise);
             }
         }
         throw raise;
     }
 }
        public ActionResult BatchUpdateHIV(string key, List <order_hiv_rapid_test_kit_PhysicalCount> changed, List <order_hiv_rapid_test_kit_PhysicalCount> added, List <order_hiv_rapid_test_kit_PhysicalCount> deleted)
        {
            mascisEntities dbCase = new mascisEntities();

            //Performing insert operation
            if (added != null && added.Count() > 0)
            {
                foreach (var temp in added)
                {
                    string   iDate = temp.DateOfPhysicalCount.ToString();
                    DateTime oDate = DateTime.Parse(iDate);
                    temp.DateOfPhysicalCount = oDate;

                    order_hiv_rapid_test_kit_PhysicalCount old = dbCase.order_hiv_rapid_test_kit_PhysicalCount.
                                                                 FirstOrDefault(o => o.FacilityCode == temp.FacilityCode && o.DateOfPhysicalCount == temp.DateOfPhysicalCount && o.product_code == temp.product_code && o.BatchNo == temp.BatchNo);
                    if (old != null)
                    {
                        dbCase.order_hiv_rapid_test_kit_PhysicalCount.Add(temp);
                        temp.Total         = temp.Quantity_Dispensary + temp.Quantity_Store;
                        temp.DateAdded     = DateTime.Now;
                        temp.AddedBy       = new UserManagement().getCurrentuser();
                        temp.record_status = true;
                    }
                    else
                    {
                        dbCase.order_hiv_rapid_test_kit_PhysicalCount.Add(temp);
                        temp.Total         = temp.Quantity_Dispensary + temp.Quantity_Store;
                        temp.DateAdded     = DateTime.Now;
                        temp.AddedBy       = new UserManagement().getCurrentuser();
                        temp.record_status = true;
                    }
                }
            }
            //dbCase.SaveChanges();

            if (changed != null && changed.Count() > 0)
            {
                foreach (var temp in changed)
                {
                    string   iDate = temp.DateOfPhysicalCount.ToString();
                    DateTime oDate = DateTime.Parse(iDate);
                    temp.DateOfPhysicalCount = oDate;

                    order_hiv_rapid_test_kit_PhysicalCount old = dbCase.order_hiv_rapid_test_kit_PhysicalCount.
                                                                 FirstOrDefault(o => o.FacilityCode == temp.FacilityCode && o.DateOfPhysicalCount == temp.DateOfPhysicalCount && o.product_code == temp.product_code && o.BatchNo == temp.BatchNo);
                    if (old != null)
                    {
                        dbCase.Entry(old).CurrentValues.SetValues(temp);
                        temp.Total         = temp.Quantity_Dispensary + temp.Quantity_Store;
                        temp.DateAdded     = DateTime.Now;
                        temp.AddedBy       = new UserManagement().getCurrentuser();
                        temp.record_status = true;
                    }
                    else
                    {
                        dbCase.order_hiv_rapid_test_kit_PhysicalCount.Add(temp);
                        temp.Total         = temp.Quantity_Dispensary + temp.Quantity_Store;
                        temp.DateAdded     = DateTime.Now;
                        temp.AddedBy       = new UserManagement().getCurrentuser();
                        temp.record_status = true;
                    }
                }
            }
            //dbCase.SaveChanges();
            //Performing delete operation
            if (deleted != null && deleted.Count() > 0)
            {
                foreach (var temp in deleted)
                {
                    dbCase.order_hiv_rapid_test_kit_PhysicalCount.Remove(dbCase.order_hiv_rapid_test_kit_PhysicalCount.FirstOrDefault(o => o.FacilityCode == temp.FacilityCode && o.DateOfPhysicalCount == temp.DateOfPhysicalCount && o.product_code == temp.product_code && o.BatchNo == temp.BatchNo));
                }
            }

            dbCase.SaveChanges();
            return(RedirectToAction("BatchDataHIV"));
            //var data = 0;
            //return Json(data, JsonRequestBehavior.AllowGet);
        }
        //public ActionResult BatchDataKount(DataManager dataManager)
        //{
        //    IEnumerable DataSource = context.WebTemplate_ARV_PhysicalCount.ToList();
        //    DataResult result = new DataResult();
        //    result.result = DataSource;
        //    result.count = context.WebTemplate_ARV_PhysicalCount.Count();

        //    return Json(result.count, JsonRequestBehavior.AllowGet);
        //}
        public int SavePC(int FacilityCode, DateTime DateOfPhysicalCount, int product_code, string BatchNo,
                          string Quantity_Dispensary, string Quantity_Store, DateTime?ExpiryDate, string Total, string Comment)
        {
            order_hiv_rapid_test_kit_PhysicalCount obj = new order_hiv_rapid_test_kit_PhysicalCount();
            int recstat = 0;

            obj.FacilityCode        = FacilityCode;
            obj.DateOfPhysicalCount = DateOfPhysicalCount;
            obj.product_code        = System.Convert.ToInt32(product_code);
            obj.BatchNo             = BatchNo;
            if (Quantity_Dispensary != null && !string.IsNullOrEmpty(Quantity_Dispensary))
            {
                obj.Quantity_Dispensary = System.Convert.ToDouble(Quantity_Dispensary);
            }
            if (Quantity_Store != null && !string.IsNullOrEmpty(Quantity_Store))
            {
                obj.Quantity_Store = System.Convert.ToDouble(Quantity_Store);
            }
            obj.ExpiryDate = ExpiryDate;
            if (Total != null && !string.IsNullOrEmpty(Total))
            {
                obj.Total = System.Convert.ToDouble(Total);
            }
            obj.Comment = Comment;

            try
            {
                //Check for the existance of the record
                var ca = context.order_hiv_rapid_test_kit_PhysicalCount.FirstOrDefault(c => c.FacilityCode == FacilityCode && c.DateOfPhysicalCount == DateOfPhysicalCount && c.product_code == product_code && c.BatchNo == BatchNo);

                if (ca == null)
                {
                    context.order_hiv_rapid_test_kit_PhysicalCount.Add(obj);
                    obj.DateAdded = DateTime.Now;
                    obj.AddedBy   = new UserManagement().getCurrentuser();
                    recstat       = context.SaveChanges();
                    // recstat = obj.household_number;
                }
                else
                {
                    order_hiv_rapid_test_kit_PhysicalCount table = context.order_hiv_rapid_test_kit_PhysicalCount.FirstOrDefault(c => c.FacilityCode == FacilityCode && c.DateOfPhysicalCount == DateOfPhysicalCount && c.product_code == product_code && c.BatchNo == BatchNo);
                    // obj.household_number = ca.household_number ;
                    context.Entry(table).CurrentValues.SetValues(obj);
                    obj.DateAdded = DateTime.Now;
                    obj.AddedBy   = new UserManagement().getCurrentuser();
                    context.Entry(table).State = EntityState.Modified;
                    recstat = context.SaveChanges();
                    //recstat = ca.household_number;
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                                                       validationErrors.Entry.Entity.ToString(),
                                                       validationError.ErrorMessage);
                        // raise a new exception nesting
                        // the current instance as InnerException
                        raise = new InvalidOperationException(message, raise);
                    }
                }
                throw raise;
            }

            return(recstat);
        }