private static Order getEntityByModel(OrderModel model)
        {
            if (model == null) return null;

            Order entity = new Order();

            if (model.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.CustomerId = model.CustomerId;
            entity.CustomerSiteId = model.CustomerSiteId;
            entity.Id = model.Id;
            entity.OrderDate = model.OrderDate;
            entity.OrderNo = model.OrderNo;
            entity.OrderTypeId = model.OrderTypeId;
            entity.Remarks = model.Remarks;
            entity.SOBId = SessionHelper.SOBId;
            entity.Status = model.Status;
            entity.UpdateBy = AuthenticationHelper.UserId;
            entity.UpdateDate = DateTime.Now;

            return entity;
        }
        public ActionResult Create()
        {
            OrderModel order = SessionHelper.Order;
            if (order == null)
            {
                order = new OrderModel
                {
                    CompanyId = AuthenticationHelper.CompanyId.Value,
                    OrderDate = DateTime.Now,
                    OrderDetail = new List<OrderDetailModel>(),
                    OrderNo = "New",
                    Status = "Booked"
                };
                SessionHelper.Order = order;
            }

            order.Customers = CustomerHelper.GetActiveCustomersCombo(order.OrderDate);
            if (order.Customers != null && order.Customers.Count() > 0)
            {
                order.CustomerId = order.CustomerId > 0 ? order.CustomerId : Convert.ToInt64(order.Customers.FirstOrDefault().Value);
                order.CustomerSites = CustomerHelper.GetCustomerSitesCombo(order.CustomerId);
            }

            order.OrderTypes = OrderTypeHelper.GetOrderTypesCombo(order.OrderDate);
            if (order.OrderTypes != null && order.OrderTypes.Count() > 0)
                order.OrderTypeId = order.OrderTypeId > 0 ? order.OrderTypeId : Convert.ToInt64(order.OrderTypes.FirstOrDefault().Value);

            return View("Edit", order);
        }
        public static void Save(OrderModel model)
        {
            Order entity = getEntityByModel(model);

            string result = string.Empty;
            if (entity.IsValid())
            {
                if (model.Id > 0)
                    result = service.Update(entity);
                else
                    result = service.Insert(entity);

                if (!string.IsNullOrEmpty(result))
                {
                    var savedLines = getDetailByOrderId(Convert.ToInt64(result));
                    if (savedLines.Count() > model.OrderDetail.Count())
                    {
                        var tobeDeleted = savedLines.Take(savedLines.Count() - model.OrderDetail.Count());
                        foreach (var item in tobeDeleted)
                        {
                            service.DeleteOrderDetail(item.Id);
                        }
                        savedLines = getDetailByOrderId(Convert.ToInt64(result));
                    }

                    foreach (var detail in model.OrderDetail)
                    {
                        OrderDetail detailEntity = getEntityByModel(detail);
                        if (detailEntity.IsValid())
                        {
                            detailEntity.OrderId = Convert.ToInt64(result);
                            if (savedLines.Count() > 0)
                            {
                                detailEntity.Id = savedLines.FirstOrDefault().Id;
                                savedLines.Remove(savedLines.FirstOrDefault(rec => rec.Id == detailEntity.Id));
                                service.Update(detailEntity);
                            }
                            else
                                service.Insert(detailEntity);
                        }
                    }
                }
            }
        }
 public static string UpdateOrder(OrderModel model)
 {
     Order entity = getEntityByModel(model);
     return service.Update(entity);
 }
        public static string GenerateOrderNum(OrderModel model)
        {
            var currentDocument = service.GetAll(SessionHelper.SOBId).OrderByDescending(rec => rec.Id).FirstOrDefault();
            string newDocNo = "";
            if (currentDocument != null)
            {
                int outVal;
                bool isNumeric = int.TryParse(currentDocument.OrderNo, out outVal);
                if (isNumeric && currentDocument.OrderNo.Length == 8)
                {
                    newDocNo = (int.Parse(currentDocument.OrderNo) + 1).ToString();
                    return newDocNo;
                }
            }

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

            return yearDigit + monthDigit + docNo;
        }
        public ActionResult ChangeCombos(DateTime orderDate)
        {
            if(orderDate < DateTime.Now.Date)
                return Json("Order date cannot be the past date!.");

            OrderModel order = new OrderModel();
            order.Customers = CustomerHelper.GetActiveCustomersCombo(orderDate);
            if (order.Customers != null && order.Customers.Count() > 0)
            {
                order.CustomerId = order.CustomerId > 0 ? order.CustomerId : Convert.ToInt64(order.Customers.FirstOrDefault().Value);
                order.CustomerSites = CustomerHelper.GetCustomerSitesCombo(order.CustomerId);
            }

            order.OrderTypes = OrderTypeHelper.GetOrderTypesCombo(orderDate);
            if (order.OrderTypes != null && order.OrderTypes.Count() > 0)
                order.OrderTypeId = order.OrderTypeId > 0 ? order.OrderTypeId : Convert.ToInt64(order.OrderTypes.FirstOrDefault().Value);

            return Json(order);
        }