public ActionResult DeletePartial(OrderShipmentLine model)
        {
            try
            {
                model.Id = currentId;
                string result = ShipmentHelper.Delete(model);
                if (!string.IsNullOrEmpty(result))
                    ViewData["EditError"] = result;
            }
            catch (Exception ex)
            {
                ViewData["EditError"] = ex.Message;
            }

            return PartialView("_Detail", SessionHelper.Shipment.OrderShipments);
        }
        public ActionResult AddNewPartial(OrderShipmentLine model)
        {
            //Never be in this case..
            if (ModelState.IsValid)
            {
                try
                {
                    if (SessionHelper.Order.OrderDetail != null && SessionHelper.Order.OrderDetail.Count() > 0)
                        model.Id = SessionHelper.Invoice.InvoiceDetail.LastOrDefault().Id + 1;
                    else
                        model.Id = 1;

                    string result = ShipmentHelper.Insert(model);
                    if (!string.IsNullOrEmpty(result))
                        ViewData["EditError"] = result;
                }
                catch (Exception ex)
                {
                    ViewData["EditError"] = ex.Message;
                }
            }
            else
                ViewData["EditError"] = "Please correct all errors!";
            return PartialView("_Detail", SessionHelper.Shipment.OrderShipments);
        }
        public ActionResult UpdatePartial(OrderShipmentLine model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //Temporary, because unable to pass Grid column as a parameter to fill combo in grid
                    model.ItemId = SessionHelper.ItemId;
                    model.Id = currentId;
                    string result = ShipmentHelper.Update(model);
                    if (!string.IsNullOrEmpty(result))
                        ViewData["EditError"] = result;
                }
                catch (Exception ex)
                {
                    ViewData["EditError"] = ex.Message;
                }
            }
            else
                ViewData["EditError"] = "Please correct all errors!";

            return PartialView("_Detail", SessionHelper.Shipment.OrderShipments);
        }
        private static string validateShipment(OrderShipmentLine model)
        {
            decimal totalQty = model.ThisShipQuantity;
            OrderDetailModel orderDetail = OrderHelper.GetSingleOrderDetail(model.LineId);
            List<Shipment> shiped = new List<Shipment>();
            if (model.Id > 0)
                shiped = service.GetAllByLineId(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, model.LineId)
                    .Where(rec => rec.Id < model.Id).ToList();
            else
                shiped = service.GetAllByLineId(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, model.LineId).ToList();

            if (shiped != null && shiped.Count() > 0)
                totalQty += shiped.Sum(rec => rec.Quantity);
            if (totalQty > orderDetail.Quantity)
                return "Quantity is exceeding than order!";

            string lotAndSerialValidation = checkLotandSerials(model);
            if (!string.IsNullOrEmpty(lotAndSerialValidation))
                return lotAndSerialValidation;

            //Other validations if any..

            OrderShipmentModel orderShipment = SessionHelper.Shipment;
            if (model.Id > 0)
            {
                orderShipment.OrderShipments.First(x => x.LineId == model.LineId).BalanceQuantity = orderDetail.Quantity - (shiped.Sum(rec => rec.Quantity) + model.ThisShipQuantity);
                orderShipment.OrderShipments.First(x => x.LineId == model.LineId).Id = model.Id;
                orderShipment.OrderShipments.First(x => x.LineId == model.LineId).LocatorId = model.LocatorId;
                orderShipment.OrderShipments.First(x => x.LineId == model.LineId).LotNoId = model.LotNoId;
                orderShipment.OrderShipments.First(x => x.LineId == model.LineId).OrderQuantity = orderDetail.Quantity;
                orderShipment.OrderShipments.First(x => x.LineId == model.LineId).SerialNo = model.SerialNo;
                orderShipment.OrderShipments.First(x => x.LineId == model.LineId).ShipedQuantity = shiped.Sum(rec => rec.Quantity);
                orderShipment.OrderShipments.First(x => x.LineId == model.LineId).ThisShipQuantity = model.ThisShipQuantity;
            }
            else
            {
                if (orderShipment.OrderShipments.Any(x => x.LineId == model.LineId))
                {
                    orderShipment.OrderShipments.First(x => x.LineId == model.LineId).BalanceQuantity = orderDetail.Quantity - (shiped.Sum(rec => rec.Quantity) + model.ThisShipQuantity);
                    orderShipment.OrderShipments.First(x => x.LineId == model.LineId).Id = model.Id;
                    orderShipment.OrderShipments.First(x => x.LineId == model.LineId).LocatorId = model.LocatorId;
                    orderShipment.OrderShipments.First(x => x.LineId == model.LineId).LotNoId = model.LotNoId;
                    orderShipment.OrderShipments.First(x => x.LineId == model.LineId).OrderQuantity = orderDetail.Quantity;
                    orderShipment.OrderShipments.First(x => x.LineId == model.LineId).SerialNo = model.SerialNo;
                    orderShipment.OrderShipments.First(x => x.LineId == model.LineId).ShipedQuantity = shiped.Sum(rec => rec.Quantity);
                    orderShipment.OrderShipments.First(x => x.LineId == model.LineId).ThisShipQuantity = model.ThisShipQuantity;
                }
                else
                {
                    //Never be in this case..
                    orderShipment.OrderShipments.Add(new OrderShipmentLine
                        {
                            BalanceQuantity = orderDetail.Quantity - (shiped.Sum(rec => rec.Quantity) + model.ThisShipQuantity),
                            Id = model.Id,
                            ItemName = model.ItemName,
                            LineId = model.LineId,
                            LocatorId = model.LocatorId,
                            LotNoId = model.LotNoId,
                            OrderQuantity = orderDetail.Quantity,
                            SerialNo = model.SerialNo,
                            ShipedQuantity = shiped.Sum(rec => rec.Quantity),
                            ThisShipQuantity = model.ThisShipQuantity
                        });
                }
            }
            return "";
        }
        public static string Delete(OrderShipmentLine model)
        {
            OrderShipmentModel orderShipment = SessionHelper.Shipment;
            OrderShipmentLine shipment = orderShipment.OrderShipments.FirstOrDefault(x => x.LineId == model.LineId);
            SessionHelper.Shipment.OrderShipments.Remove(shipment);

            return "";
        }
 public static string Update(OrderShipmentLine model)
 {
     return validateShipment(model);
 }
        public static OrderShipmentModel GetShipmentEdit(string deliveryNo, DateTime date)
        {
            List<ShipmentModel> shipmentDetail = service.GetDelivery(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, deliveryNo, date).Select(x => new ShipmentModel(x)).ToList();

            OrderModel order = OrderHelper.GetOrder(shipmentDetail.First().OrderId.ToString());

            //Can be multiple, showing the first one on the header..
            OrderShipmentModel orderShipment = new OrderShipmentModel(shipmentDetail.First());
            orderShipment.CreateBy = shipmentDetail.First().CreateBy;
            orderShipment.CreateDate = shipmentDetail.First().CreateDate;
            orderShipment.CustomerId = order.CustomerId;
            orderShipment.CustomerSiteId = order.CustomerSiteId;
            orderShipment.UpdateBy = shipmentDetail.First().UpdateBy;
            orderShipment.UpdateDate = shipmentDetail.First().UpdateDate;

            orderShipment.OrderShipments = new List<OrderShipmentLine>();
            foreach (var item in shipmentDetail)
            {
                OrderDetailModel orderDetail = OrderHelper.GetSingleOrderDetail(item.LineId);

                OrderShipmentLine shipmentLine = new OrderShipmentLine(item);
                decimal shippedQty = GetShipments(item.LineId).Where(rec => rec.Id < item.Id).Sum(x => x.Quantity);
                shipmentLine.BalanceQuantity = orderDetail.Quantity - (shippedQty + item.Quantity);
                shipmentLine.OrderQuantity = orderDetail.Quantity;
                shipmentLine.ShipedQuantity = shippedQty;
                shipmentLine.ThisShipQuantity = item.Quantity;

                shipmentLine.ItemName = ItemHelper.GetItem(orderDetail.ItemId.ToString()).ItemName;
                shipmentLine.CustomerId = OrderHelper.GetSingleOrder(item.OrderId.ToString()).CustomerId;
                shipmentLine.CustomerName = OrderHelper.GetSingleOrder(item.OrderId.ToString()).CustomerName;
                shipmentLine.CustomerSiteId = OrderHelper.GetSingleOrder(item.OrderId.ToString()).CustomerSiteId;
                shipmentLine.CustomerSiteName = OrderHelper.GetSingleOrder(item.OrderId.ToString()).CustomerSiteName;

                orderShipment.OrderShipments.Add(shipmentLine);
            }

            return orderShipment;
        }
        private static string checkLotandSerials(OrderShipmentLine model)
        {
            //Selection check..
            if (model.LotNoId == null || model.LotNoId == 0)
            {
                ItemModel item = ItemHelper.GetItem(model.ItemId.ToString());
                if (item.LotControl)
                    return "Please provide item lot!";

                if (!string.IsNullOrEmpty(model.SerialNo))
                    return "Serial no. can not be defined without its lot!";
                else
                    return "";
            }
            else
            {
                List<SerialNumber> serialExist = LotNumberHelper.GetSerialsbyLotNo(model.LotNoId.Value).ToList();
                List<SerialNumber> availableSerials = LotNumberHelper.GetAvailableSerials(model.LotNoId.Value);
                List<string> serialonGrid = SessionHelper.Shipment.OrderShipments.Where(rec => rec.LotNoId == model.LotNoId && rec.LineId != model.LineId).Select(x => x.SerialNo).ToList();

                if (!string.IsNullOrEmpty(model.SerialNo))
                {
                    List<string> serials = model.SerialNo.Split(new char[] { ',' }).ToList();

                    //Check duplication within the same record..
                    if (serials.GroupBy(rec => rec).Any(d => d.Count() > 1))
                        return "Serial can not be duplicate!";

                    //Quantity check..
                    if (serials.Count() < model.ThisShipQuantity || serials.Count() > model.ThisShipQuantity)
                        return "Serials are not matching with the quantity!";

                    //Serial availablity within shipment..
                    if (serialonGrid != null && serialonGrid.Count() > 0)
                    {
                        foreach (var unsaved in serialonGrid)
                        {
                            if (!string.IsNullOrEmpty(unsaved))
                            {
                                //Check by another entries on grid..
                                List<string> unsavedSerial = unsaved.Split(new char[] { ',' }).ToList();
                                if (serials.Any(rec => rec == unsavedSerial.FirstOrDefault(x => x == rec)))
                                    return "One of your serial no is already defined!";
                            }
                        }
                    }

                    //Lot serial existence check..
                    if (serialExist != null && serialExist.Count() > 0)
                    {
                        if (serials.Count() > availableSerials.Count())
                            return "Lot is exceeding the available qty!";
                    }
                    else
                        return "This lot does not support serial.";

                    //Serial availablity within db..
                    foreach (var serial in serials)
                    {
                        bool isAvailable = LotNumberHelper.CheckSerialNumAvailability(model.LotNoId.Value, serial);
                        if (isAvailable)
                            continue;
                        else
                        {
                            if (model.Id > 0)
                            {
                                LotNumber savedLot = LotNumberHelper.GetLotNumber(LotNumberHelper.GetSerialNo(serial, model.LotNoId.Value).LotNoId);
                                if (savedLot.SourceId == model.Id)
                                    continue;
                                else
                                    return "Serial No. " + serial + " does not exist or may have been already shipped";
                            }
                            else
                                return "Serial No. " + serial + " does not exist or may have been already shipped";
                        }
                    }
                }
                else
                {
                    //Lot serial existence check..
                    if (serialExist != null && serialExist.Count() > 0)
                    {
                        //Serial Generation
                        if (model.ThisShipQuantity > availableSerials.Count() - serialonGrid.Count())
                            return "Quantity is exceeding!";
                        else
                        {
                            //Generate from available serials..
                            //Qty can not be decimal..
                            List<SerialNumber> useAvailable = new List<SerialNumber>();
                            if (serialonGrid != null && serialonGrid.Count() > 0)
                            {
                                List<string> gridSerial = new List<string>();
                                foreach (var serial in serialonGrid)
                                {
                                    List<string> currentLine = serial.Split(new char[] { ',' }).ToList();
                                    gridSerial.AddRange(currentLine);
                                }

                                useAvailable = availableSerials.Where(rec => rec.SerialNo != gridSerial.FirstOrDefault(x => x == rec.SerialNo)).Take(Convert.ToInt32(model.ThisShipQuantity)).ToList();
                            }
                            else
                            {
                                useAvailable = availableSerials.Take(Convert.ToInt32(model.ThisShipQuantity)).ToList();
                            }

                            List<string> autoGeneratedSerial = useAvailable.Select(rec => rec.SerialNo).ToList();
                            string newSerial = string.Join(",", autoGeneratedSerial.ToArray());

                            SessionHelper.Shipment.OrderShipments.FirstOrDefault(x => x.LineId == model.LineId && x.Id == model.Id).SerialNo = newSerial;
                        }
                    }
                    else
                    {
                        //Lot can be used only one time..
                        if (SessionHelper.Shipment.OrderShipments.Any(re => re.LotNoId == model.LotNoId && re.LineId != model.LineId))
                            return "Lot is in use in the current shipment";
                    }
                }
            }

            return "";
        }