public ActionResult Create()
        {
            OrderShipmentModel newShipment = SessionHelper.Shipment;
            if (newShipment == null)
            {
                newShipment = new OrderShipmentModel
                {
                    DeliveryDate = DateTime.Now,
                    CompanyId = AuthenticationHelper.CompanyId.Value,
                    OrderShipments = new List<OrderShipmentLine>()
                };
                SessionHelper.Shipment = newShipment;
            }

            newShipment.Customers = CustomerHelper.GetActiveCustomersCombo(newShipment.DeliveryDate);
            if (newShipment.Customers != null && newShipment.Customers.Count() > 0)
            {
                newShipment.CustomerId = newShipment.CustomerId > 0 ? newShipment.CustomerId : 0;
                newShipment.Customers.Add(new SelectListItem { Text = "-- Select --", Value = "0" });
                newShipment.CustomerSites = CustomerHelper.GetCustomerSitesCombo(newShipment.CustomerId);
                if (newShipment.CustomerSites != null && newShipment.CustomerSites.Count() > 0)
                {
                    newShipment.CustomerSiteId = newShipment.CustomerSiteId > 0 ? newShipment.CustomerSiteId : 0;
                    newShipment.CustomerSites.Add(new SelectListItem { Text = "-- Select --", Value = "0" });
                }
            }

            newShipment.Orders = OrderHelper.GetOrdersCombo();
            if (newShipment.Orders != null && newShipment.Orders.Count() > 0)
            {
                newShipment.OrderId = newShipment.OrderId > 0 ? newShipment.OrderId : 0;
                newShipment.Orders.Add(new SelectListItem { Text = "-- Select --", Value = "0" });
            }

            newShipment.Warehouses = WarehouseHelper.GetWarehousesCombo(SessionHelper.SOBId);
            if (newShipment.Warehouses != null && newShipment.Warehouses.Count() > 0)
                newShipment.WarehouseId = newShipment.WarehouseId > 0 ? newShipment.WarehouseId : Convert.ToInt64(newShipment.Warehouses.First().Value);

            newShipment.DeliveryNo = "New";
            SessionHelper.Shipment = newShipment;
            SessionHelper.Shipment.OrderShipments = ShipmentHelper.GetShipment(newShipment.WarehouseId, newShipment.CustomerId, newShipment.CustomerSiteId, newShipment.OrderId).OrderShipments;

            return View("Edit", newShipment);
        }
        private static List<Shipment> getEntitiesByModel(OrderShipmentModel model)
        {
            if (model == null) return null;
            List<Shipment> entityList = new List<Shipment>();

            if (model.OrderShipments == null || model.OrderShipments.Count == 0)
                return null;
            foreach (var item in model.OrderShipments)
            {
                Shipment entity = new Shipment();
                if (item.Id == 0)
                {
                    entity.CreateBy = AuthenticationHelper.UserId;
                    entity.CreateDate = DateTime.Now;
                    entity.CompanyId = AuthenticationHelper.CompanyId.Value;
                }
                else
                {
                    entity.CreateBy = model.CreateBy;
                    entity.CreateDate = model.CreateDate;
                    entity.CompanyId = model.CompanyId;
                }
                entity.DeliveryDate = model.DeliveryDate;
                entity.SOBId = SessionHelper.SOBId;
                entity.WarehouseId = model.WarehouseId;
                entity.OrderId = item.OrderId;
                entity.UpdateBy = model.UpdateBy;
                entity.UpdateDate = model.UpdateDate;
                entity.Id = item.Id;
                entity.LineId = item.LineId;
                entity.LocatorId = item.LocatorId;
                entity.LotNoId = item.LotNoId == 0 ? null : item.LotNoId;
                entity.Quantity = item.ThisShipQuantity;
                entity.SerialNo = item.SerialNo;
                entity.DeliveryNo = model.DeliveryNo;

                entityList.Add(entity);
            }
            return entityList;
        }
        public static string Save(OrderShipmentModel model)
        {
            List<long> orderIds = new List<long>();
            string result = "";
            if (model.OrderShipments != null && model.OrderShipments.Count() > 0)
                model.OrderShipments = model.OrderShipments.Where(rec => rec.LocatorId > 0).ToList();

            List<Shipment> currentShipments = getEntitiesByModel(model);

            if (currentShipments != null && currentShipments.Count() > 0)
            {
                if (model.DeliveryNo != "New")
                {
                    List<Shipment> savedShipment = service.GetDelivery(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId, model.DeliveryNo, model.DeliveryDate).ToList();
                    if (savedShipment != null && savedShipment.Count() > 0)
                    {
                        if (savedShipment.Count() > currentShipments.Count())
                        {
                            List<Shipment> tobeDeleted = savedShipment.Take(savedShipment.Count() - currentShipments.Count()).ToList();
                            foreach (var deleteSingle in tobeDeleted)
                            {
                                if (!string.IsNullOrEmpty(deleteSingle.SerialNo))
                                {
                                    List<string> serials = deleteSingle.SerialNo.Split(new char[] { ',' }).ToList();
                                    deleteSerials(serials, deleteSingle.LotNoId.Value);
                                }

                                orderIds.Add(deleteSingle.OrderId);
                                service.Delete(deleteSingle.Id.ToString(), AuthenticationHelper.CompanyId.Value);
                            }
                        }
                    }
                }
                foreach (var item in currentShipments)
                {
                    List<ShipmentModel> shipments = GetShipments(item.LineId);
                    OrderDetailModel orderDetail = OrderHelper.GetSingleOrderDetail(item.LineId);
                    decimal savedQty = 0;
                    if (shipments != null && shipments.Count() > 0)
                        savedQty = shipments.Sum(x => x.Quantity);

                    orderIds.Add(item.OrderId);

                    if (item.LotNoId != null && item.LotNoId > 0)
                    {
                        long lotNoId = 0;
                        string lotNumResult = updateLots(item);
                        int outVal;
                        bool isNumeric = int.TryParse(lotNumResult, out outVal);
                        if (isNumeric)
                            lotNoId = int.Parse(lotNumResult);

                        item.LotNoId = lotNoId;
                    }

                    updateSerials(item);

                    if (item.Id > 0)
                        result = service.Update(item);
                    else
                        result = service.Insert(item);

                    //Update lot num to set the sourceId..
                    if (item.LotNoId != null && item.LotNoId > 0)
                    {
                        LotNumber tobeUpdated = LotNumberHelper.GetLotNumber(item.LotNoId.Value);
                        tobeUpdated.SourceId = item.Id;
                        LotNumberHelper.Update(tobeUpdated);
                    }
                }

                updateOrders(orderIds);

            }
            else
                result = "Please select order to ship!";
            return result;
        }
        public static OrderShipmentModel GetShipment(long warehouseId, long customerId, long customerSiteId, long orderId)
        {
            OrderShipmentModel newShipment = new OrderShipmentModel
            {
                CustomerId = customerId,
                CustomerSiteId = customerSiteId,
                DeliveryDate = DateTime.Now,
                Id = 0,
                OrderId = orderId,
                OrderShipments = new List<OrderShipmentLine>(),
                WarehouseId = warehouseId
            };

            List<OrderModel> orders = OrderHelper.GetOrders(orderId, customerId, customerSiteId);
            if (orders.Count() > 0)
            {
                foreach (var order in orders)
                {
                    List<OrderDetailModel> orderDetail = OrderHelper.GetOrderDetail(order.Id.ToString()).ToList();
                    if (orderDetail != null && orderDetail.Count() > 0)
                    {
                        orderDetail = orderDetail.Where(rec => rec.WarehouseId == warehouseId).ToList();
                        if (orderDetail != null && orderDetail.Count() > 0)
                        {
                            foreach (var detailItem in orderDetail)
                            {
                                List<ShipmentModel> shipments = GetShipments(detailItem.Id);
                                if (shipments != null && shipments.Count() > 0)
                                {
                                    if (shipments.Sum(rec => rec.Quantity) < detailItem.Quantity)
                                    {
                                        OrderShipmentLine current = fromOrderDetailtoShipment(detailItem, shipments);
                                        current.CustomerName = order.CustomerName;
                                        current.CustomerSiteName = order.CustomerSiteName;
                                        current.OrderNo = order.OrderNo;

                                        if (!SessionHelper.Shipment.OrderShipments.Any(rec => rec.LineId == detailItem.Id))
                                            newShipment.OrderShipments.Add(current);
                                    }
                                }
                                else
                                {
                                    OrderShipmentLine current = fromOrderDetailtoShipment(detailItem, shipments);
                                    current.CustomerName = order.CustomerName;
                                    current.CustomerSiteName = order.CustomerSiteName;
                                    current.OrderNo = order.OrderNo;

                                    newShipment.OrderShipments.Add(current);
                                }
                            }
                        }
                    }
                }
            }

            return newShipment;
        }
        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;
        }
        public static string GenerateDeliveryNum(OrderShipmentModel model)
        {
            var currentDocument = service.GetAll(AuthenticationHelper.CompanyId.Value, SessionHelper.SOBId).OrderByDescending(rec => rec.Id).FirstOrDefault();
            string newDocNo = "";
            if (currentDocument != null)
            {
                int outVal;
                bool isNumeric = int.TryParse(currentDocument.DeliveryNo, out outVal);
                if (isNumeric && currentDocument.DeliveryNo.Length == 8)
                {
                    newDocNo = (int.Parse(currentDocument.DeliveryNo) + 1).ToString();
                    return newDocNo;
                }
            }

            //Create New DocNum..
            string yearDigit = model.DeliveryDate.ToString("yy");
            string monthDigit = model.DeliveryDate.ToString("MM");
            string docNo = int.Parse("1").ToString().PadLeft(4, '0');

            return yearDigit + monthDigit + docNo;
        }