private ReceivingDetail getEntityByModel(ReceivingDetailModel model)
        {
            if (model == null) return null;

            ReceivingDetail entity = new ReceivingDetail();

            if (model.Id == 0)
            {
                entity.CreateBy = AuthenticationHelper.UserId;
                entity.CreateDate = DateTime.Now;
            }
            else
            {
                entity.CreateBy = model.CreateBy;
                entity.CreateDate = model.CreateDate;
            }

            entity.Id = model.Id;
            entity.ItemId = model.ItemId;
            entity.LocatorId = model.LocatorId;
            entity.LotNoId = model.LotNoId;
            entity.Quantity = model.ThisPurchaseQty;
            entity.ReceiptId = model.ReceiptId;
            entity.SerialNo = model.SerialNo;
            entity.UpdateBy = model.UpdateBy;
            entity.UpdateDate = model.UpdateDate;
            entity.WarehouseId = model.WarehouseId;
            entity.PODetailId = model.PODetailId;

            return entity;
        }
        public ActionResult DeletePartial(ReceivingDetailModel model)
        {
            try
            {
                ReceivingModel receiving = SessionHelper.Receiving;
                ReceivingDetailModel receivingDetail = receiving.ReceivingDetail.FirstOrDefault(rec => rec.Id == model.Id);

                SessionHelper.Receiving.ReceivingDetail.Remove(receivingDetail);
            }
            catch (Exception ex)
            {
                ViewData["EditError"] = ex.Message;
            }

            return PartialView("_Detail", getReceivingDetail());
        }
        public ActionResult UpdatePartial(ReceivingDetailModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    ReceivingDetailModel completeModel = SessionHelper.Receiving.ReceivingDetail.FirstOrDefault(x => x.Id == model.Id);

                    Item item = itemService.GetSingle(completeModel.ItemId.ToString(), AuthenticationHelper.CompanyId.Value);
                    if (item.LotControl)
                    {
                        if (string.IsNullOrEmpty(model.LotNo))
                        {
                            ViewData["EditError"] = "Please provide Lot #!";
                            return PartialView("_Detail", getReceivingDetail());
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(model.LotNo))
                        {
                            ViewData["EditError"] = "Item does not support Lot!";
                            return PartialView("_Detail", getReceivingDetail());
                        }
                    }

                    if (item.SerialControl)
                    {
                        if (string.IsNullOrEmpty(model.SerialNo))
                        {
                            ViewData["EditError"] = "Please provide Serial #!";
                            return PartialView("_Detail", getReceivingDetail());
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(model.LotNo))
                            {
                                ViewData["EditError"] = "Please provide Lot #!";
                                return PartialView("_Detail", getReceivingDetail());
                            }
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(model.SerialNo))
                        {
                            ViewData["EditError"] = "Item does not support Serials!";
                            return PartialView("_Detail", getReceivingDetail());
                        }
                    }

                    if (model.WarehouseId == 0)
                    {
                        ViewData["EditError"] = "Please select Warehouse!";
                        return PartialView("_Detail", getReceivingDetail());
                    }
                    if (model.LocatorId == 0)
                    {
                        ViewData["EditError"] = "Please select Locator!";
                        return PartialView("_Detail", getReceivingDetail());
                    }

                    if (model.ThisPurchaseQty > completeModel.BalanceQty + model.ThisPurchaseQty)
                    {
                        ViewData["EditError"] = "Quantity can not be more than balance!";
                        return PartialView("_Detail", getReceivingDetail());
                    }
                    if (!string.IsNullOrEmpty(model.SerialNo))
                    {
                        if (string.IsNullOrEmpty(model.LotNo))
                        {
                            ViewData["EditError"] = "Serials can not be defined without Lot!";
                            return PartialView("_Detail", getReceivingDetail());
                        }
                        else
                        {
                            List<string> serials = model.SerialNo.Split(new char[] { ',' }).ToList();
                            if (serials.Count() != model.ThisPurchaseQty)
                            {
                                ViewData["EditError"] = "Serials must be according to the Quantity!";
                                return PartialView("_Detail", getReceivingDetail());
                            }
                        }
                    }
                    ReceivingDetailModel currentModel = SessionHelper.Receiving.ReceivingDetail.FirstOrDefault(rec => rec.Id == model.Id);

                    currentModel.Id = model.Id;
                    currentModel.LocatorId = model.LocatorId;
                    currentModel.LotNo = model.LotNo;
                    currentModel.SerialNo = model.SerialNo;
                    currentModel.WarehouseId = model.WarehouseId;
                    currentModel.ThisPurchaseQty = model.ThisPurchaseQty;
                    currentModel.BalanceQty = completeModel.OrderQty - (completeModel.PurchaseQty + model.ThisPurchaseQty);
                    currentModel.OrderQty = completeModel.OrderQty;
                    currentModel.LotNoId = completeModel.LotNoId;
                    currentModel.ItemId = completeModel.ItemId;
                    currentModel.ItemName = completeModel.ItemName;
                    currentModel.PODetailId = completeModel.PODetailId;
                    currentModel.ReceiptId = completeModel.ReceiptId;

                    SessionHelper.Receiving.ReceivingDetail.Remove(SessionHelper.Receiving.ReceivingDetail.FirstOrDefault(rec => rec.Id == model.Id));
                    SessionHelper.Receiving.ReceivingDetail.Add(currentModel);
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_Detail", getReceivingDetail());
        }
        public ActionResult AddNewPartial(ReceivingDetailModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (model.ThisPurchaseQty > model.BalanceQty)
                    {
                        ViewData["EditError"] = "Quantity can not be more than balance!";
                    }
                    if (!string.IsNullOrEmpty(model.SerialNo))
                    {
                        if (string.IsNullOrEmpty(model.LotNo))
                            ViewData["EditError"] = "Serials can not be defined without Lot!";
                    }
                    else
                    {
                        if (SessionHelper.Receiving != null)
                        {
                            if (SessionHelper.Receiving.ReceivingDetail != null && SessionHelper.Receiving.ReceivingDetail.Count() > 0)
                                model.Id = SessionHelper.Receiving.ReceivingDetail.LastOrDefault().Id + 1;
                            else
                                model.Id = 1;
                        }
                        else
                            model.Id = 1;

                        SessionHelper.Receiving.ReceivingDetail.Add(model);
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_Detail", getReceivingDetail());
        }
        private string updateSerials(ReceivingDetailModel model)
        {
            if (!string.IsNullOrEmpty(model.SerialNo))
            {
                List<string> newSerials = model.SerialNo.Trim().Split(new char[] { ',' }).ToList();
                if (newSerials.Count() != model.ThisPurchaseQty)
                    return "Serials must be according to the Quantity";
            }
            if (model.Id > 0)
            {
                ReceivingDetail savedDetail = service.GetSingleReceivingDetail(model.Id);
                if (!string.IsNullOrEmpty(model.SerialNo))
                {
                    List<string> unsavedSerials = model.SerialNo.Trim().Split(new char[] { ',' }).ToList();
                    if (!string.IsNullOrEmpty(savedDetail.SerialNo))
                    {
                        List<string> savedSerials = savedDetail.SerialNo.Split(new char[] { ',' }).ToList();
                        bool isAvailable = true;
                        foreach (var serial in savedSerials)
                        {
                            isAvailable = lotService.CheckSerialNumAvailability(AuthenticationHelper.CompanyId.Value, savedDetail.LotNoId.Value, serial);
                            if (!isAvailable)
                                return "Serial is in use!";
                        }

                        if (isAvailable)
                        {
                            if (savedSerials.Count() > unsavedSerials.Count())
                            {
                                List<string> tobeDeleted = savedSerials.Take(savedSerials.Count() - unsavedSerials.Count()).ToList();
                                if (tobeDeleted != null && tobeDeleted.Count() > 0)
                                {
                                    foreach (var item in tobeDeleted)
                                    {
                                        SerialNumber serialNum = lotService.GetSerialNo(item, savedDetail.LotNoId.Value, AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId);
                                        if (serialNum != null)
                                            lotService.DeleteSerialNum(serialNum.Id.ToString(), AuthenticationHelper.CompanyId.Value);
                                    }
                                }
                            }
                            foreach (var serial in unsavedSerials)
                            {
                                SerialNumber entity = lotService.GetSerialNo(serial, savedDetail.LotNoId.Value, AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId);
                                if (entity != null)
                                {
                                    entity.LotNo = model.LotNo;
                                    entity.SerialNo = serial;
                                    lotService.UpdateSerialNum(entity);
                                }
                                else
                                {
                                    lotService.InsertSerialNum(new SerialNumber
                                    {
                                        CompanyId = AuthenticationHelper.CompanyId.Value,
                                        CreateBy = AuthenticationHelper.UserId,
                                        CreateDate = DateTime.Now,
                                        LotNo = model.LotNo,
                                        LotNoId = savedDetail.LotNoId.Value,
                                        SerialNo = serial,
                                        UpdateBy = null,
                                        UpdateDate = null
                                    });
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (var serial in unsavedSerials)
                        {
                            lotService.InsertSerialNum(new SerialNumber
                            {
                                CompanyId = AuthenticationHelper.CompanyId.Value,
                                CreateBy = AuthenticationHelper.UserId,
                                CreateDate = DateTime.Now,
                                LotNo = model.LotNo,
                                LotNoId = savedDetail.Id,
                                SerialNo = serial,
                                UpdateBy = null,
                                UpdateDate = null
                            });
                        }
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(model.SerialNo))
                {
                    if (!string.IsNullOrEmpty(model.LotNo))
                    {
                        LotNumber lot = lotService.GetLotbyItem(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, model.ItemId, model.LotNo);
                        if (lot != null)
                        {
                            List<string> serials = model.SerialNo.Trim().Split(new char[] { ',' }).ToList();
                            bool notAvailable = true;
                            foreach (var serial in serials)
                            {
                                notAvailable = lotService.CheckSerialNumAvailability(AuthenticationHelper.CompanyId.Value, lot.Id, serial);
                                if (notAvailable)
                                    return "Serial # " + serial + " is already defined";
                            }
                            if (!notAvailable)
                            {
                                foreach (var serial in serials)
                                {
                                    lotService.InsertSerialNum(new SerialNumber
                                    {
                                        CompanyId = AuthenticationHelper.CompanyId.Value,
                                        CreateBy = AuthenticationHelper.UserId,
                                        CreateDate = DateTime.Now,
                                        LotNo = lot.LotNo,
                                        LotNoId = lot.Id,
                                        SerialNo = serial,
                                        UpdateBy = null,
                                        UpdateDate = null
                                    });
                                }
                            }
                        }
                        else
                            return "Lot not found!";
                    }
                    else
                        return "Serials can not be defined without lot!";
                }
            }

            return "";
        }
        private string deleteSerials(ReceivingDetailModel model)
        {
            ReceivingDetail savedDetail = service.GetSingleReceivingDetail(model.Id);

            if (!string.IsNullOrEmpty(savedDetail.SerialNo))
            {
                List<string> savedSerials = savedDetail.SerialNo.Split(new char[] { ',' }).ToList();
                bool isAllowed = true;
                List<SerialNumber> tobeDeleted = new List<SerialNumber>();
                foreach (var serial in savedSerials)
                {
                    SerialNumber currentSerial = lotService.GetSerialNo(serial, savedDetail.LotNoId.Value, AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId);
                    if (currentSerial != null)
                    {
                        isAllowed = lotService.CheckSerialNumAvailability(AuthenticationHelper.CompanyId.Value, savedDetail.LotNoId.Value, serial);
                        if (!isAllowed)
                            return "Record can not be deleted!";
                        else
                            tobeDeleted.Add(lotService.GetSerialNo(serial, savedDetail.LotNoId.Value, AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId));
                    }
                }
                if (isAllowed)
                {
                    if (tobeDeleted.Count() > 0)
                    {
                        foreach (var item in tobeDeleted)
                        {
                            lotService.DeleteSerialNum(item.Id.ToString(), AuthenticationHelper.CompanyId.Value);
                        }
                    }
                }
            }
            return "";
        }
 private string updateLot(ReceivingDetailModel model)
 {
     if (!string.IsNullOrEmpty(model.LotNo))
     {
         if (model.Id > 0)
         {
             ReceivingDetail savedDetail = service.GetSingleReceivingDetail(model.Id);
             LotNumber savedLot = lotService.GetSingle(model.LotNoId.ToString(), AuthenticationHelper.CompanyId.Value);
             if (savedLot.LotNo == model.LotNo)
             {
                 savedLot.Qty = savedLot.Qty - savedDetail.Quantity + model.ThisPurchaseQty;
                 return lotService.Update(savedLot);
             }
             else
             {
                 List<LotNumber> savedLots = lotService.GetAllbyLotNo(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, savedLot.LotNo, savedLot.ItemId).ToList();
                 if (savedLots.Count() > 1)
                     return "Lot can not be edited!";
                 else
                 {
                     savedLot.Qty = savedLot.Qty - savedDetail.Quantity + model.ThisPurchaseQty;
                     savedLot.LotNo = model.LotNo;
                     return lotService.Update(savedLot);
                 }
             }
         }
         else
         {
             LotNumber savedLot = lotService.GetLotbyItem(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, model.ItemId, model.LotNo);
             if (savedLot != null)
             {
                 savedLot.Qty = savedLot.Qty + model.ThisPurchaseQty;
                 return lotService.Update(savedLot);
             }
             else
             {
                 return lotService.Insert(new LotNumber
                 {
                     CompanyId = AuthenticationHelper.CompanyId.Value,
                     CreateBy = AuthenticationHelper.UserId,
                     CreateDate = DateTime.Now,
                     ItemId = model.ItemId,
                     LotNo = model.LotNo,
                     Qty = model.ThisPurchaseQty,
                     SOBId = SessionHelper.SOBId,
                     SourceId = 0,
                     SourceType = "Receiving",
                     UpdateBy = null,
                     UpdateDate = null
                 });
             }
         }
     }
     else
     {
         if (model.Id > 0)
         {
             ReceivingDetail savedDetail = service.GetSingleReceivingDetail(model.Id);
             LotNumber lot = lotService.GetSingle(savedDetail.LotNoId.Value.ToString(), AuthenticationHelper.CompanyId.Value);
             List<LotNumber> savedLots = lotService.GetAllbyLotNo(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, lot.LotNo, lot.ItemId).ToList();
             if (savedDetail.LotNoId != null)
             {
                 if (savedLots.Count() > 1)
                 {
                     return "Lot can not be deleted!";
                 }
                 else
                     lotService.Delete(savedDetail.LotNoId.Value.ToString(), AuthenticationHelper.CompanyId.Value);
             }
             return "";
         }
         return "";
     }
 }
        private string deleteLot(ReceivingDetailModel model)
        {
            LotNumber lot = lotService.GetSingle(model.LotNoId.Value.ToString(), AuthenticationHelper.CompanyId.Value);
            List<LotNumber> savedLots = lotService.GetAllbyLotNo(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, lot.LotNo, lot.ItemId).ToList();
            if (model.LotNoId != null)
            {
                List<SerialNumber> savedSerials = lotService.GetSerialsbyLotNo(model.LotNoId.Value, AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId);
                if (savedSerials != null && savedSerials.Count() > 0)
                {
                    if (!string.IsNullOrEmpty(model.SerialNo))
                    {
                        List<string> currentSerials = model.SerialNo.Trim().Split(new char[] { ',' }).ToList();
                        if (savedSerials.Any(rec => rec.SerialNo != currentSerials.FirstOrDefault(x => x == rec.SerialNo)))
                        {
                            lot.Qty = lot.Qty - model.ThisPurchaseQty;
                            lotService.Update(lot);
                            return "";
                        }
                        else
                            lotService.Delete(model.LotNoId.Value.ToString(), AuthenticationHelper.CompanyId.Value);
                    }
                    else
                    {
                        lot.Qty = lot.Qty - model.ThisPurchaseQty;
                        lotService.Update(lot);
                        return "";
                    }
                }
                else
                    lotService.Delete(model.LotNoId.Value.ToString(), AuthenticationHelper.CompanyId.Value);
            }

            return "";
        }