public ActionResult ListAllOrders(int distributor, int? page)
        {
            if (distributor == 0)
            {
                ViewBag.DistributorList = _listOrdersViewModelBuilder.GetDistributor();
                var ovm = _listOrdersViewModelBuilder.GetAllList();
                var orderPagedList = ovm.AsPagination(page ?? 1, 10);
                var orderListContainer = new OrderViewModel
                {
                    orderPagedList = orderPagedList,

                };
                return View(orderListContainer);
            }
            else
            {
                ViewBag.DistributorList = _listOrdersViewModelBuilder.GetDistributor();
                var ovm = _listOrdersViewModelBuilder.GetByDist(distributor);
                var orderPagedList = ovm.AsPagination(page ?? 1, 10);
                var orderListContainer = new OrderViewModel
                {
                    orderPagedList = orderPagedList,

                };
                return View(orderListContainer);
            }
        }
        public ActionResult ListAllOrders(int? page)
        {
            int pageSize = 10;

            ViewBag.DistributorList = _listOrdersViewModelBuilder.GetDistributor();

            var ovm = _listOrdersViewModelBuilder.GetAllList();
            var orderPagedList = ovm.AsPagination(page ?? 1, pageSize);
            var orderListContainer = new OrderViewModel
            {
                orderPagedList = orderPagedList,

            };
            return View(orderListContainer);
        }
        private OrderViewModel Map(Order n)
        {



            OrderViewModel ovm = new OrderViewModel
            {
                id = n.Id,
                documentReference = n.DocumentReference,
                gross = n.TotalGross,
                net = n.TotalNet,
                vat = n.TotalVat,
                status = n.Status,
                outletCode = n.DocumentIssuerCostCentre.CostCentreCode,
                orderDate = n.DocumentDateIssued,
                isseuedOnBehalf = n.IssuedOnBehalfOf == null ? null : _costCentreRepository.GetById(n.IssuedOnBehalfOf.Id).Name,

            };
            foreach (var it in n.LineItems)
            {
                //OrderViewModel.OrderLineItemViewModel oovm = new OrderViewModel.OrderLineItemViewModel 
                ovm.ProductType = it.GetType().ToString().Split('.').Last();
                ovm.productId = it.Product == null ? Guid.Empty : it.Product.Id;
                ovm.quantity = it.Qty;
                ovm.productCode = it.Product == null ? "" : it.Product.ProductCode;


            }

            return ovm;
        }
 public OrderViewModel FilterOrdersByDate(string startDate, string endDate, int CurrentPage, int PageSize)
 {
     endDate = endDate + " 23:59:59";
     OrderViewModel orderVM = new OrderViewModel
     {
         //TotalPages = (int)Math.Ceiling((double)_documentRepository.GetCount() / (double)PageSize),
         TotalPages = (int)Math.Ceiling((double)_orderRepository.GetAll().Where(n => (n.DocumentDateIssued >= DateTime.Parse(startDate) && n.DocumentDateIssued <= DateTime.Parse(endDate)) && (n.OrderType == OrderType.DistributorToProducer)).Count() / (double)PageSize),
         Items = _orderRepository.GetAll().Where(n => (n.DocumentDateIssued >= DateTime.Parse(startDate) && n.DocumentDateIssued <= DateTime.Parse(endDate)) && (n.OrderType == OrderType.DistributorToProducer)).Skip((CurrentPage - 1) * PageSize).Take(PageSize)
         .Select(n => MapSkipAndTake(n))
         .ToList()
     };
     return orderVM;
 }
 public OrderViewModel FilterOrdersByDateDistributor(Guid distributor, string startDate, string endDate, int CurrentPage, int PageSize)
 {
     endDate = endDate + " 23:59:59";
     var items =
         _mainOrderRepository.PagedPurchaseDocumentList(CurrentPage, PageSize, DateTime.Parse(startDate),
                                                        DateTime.Parse(endDate),DocumentStatus.Confirmed, distributor);
         //.Where(n =>
         //     (n.OrderType == OrderType.DistributorToProducer && 
         //     n.DocumentDateIssued >= DateTime.Parse(startDate) && n.DocumentDateIssued <= DateTime.Parse(endDate))
         //    && (distributor == Guid.Empty 
         //        || n.DocumentIssuerCostCentre.Id == distributor 
         //        || (n.DocumentIssuerCostCentre.ParentCostCentre != null 
         //            && n.DocumentIssuerCostCentre.ParentCostCentre.Id == distributor)));
     OrderViewModel orderVM = new OrderViewModel
     {
         TotalPages =items.PageCount,
         Items = items.Select(MapMainOrderSummary).ToList()
     };
     return orderVM;
 }
 public OrderViewModel SearchPOrders(string searchText, int CurrentPage, int PageSize)
 {
      DateTime sdate= DateTime.Now.AddDays(-30);
     DateTime edate= DateTime.Now.AddDays(1);
     var orders = _mainOrderRepository.PagedPurchaseDocumentList(CurrentPage, PageSize, sdate, edate,DocumentStatus.Confirmed, null,
                                                                 searchText);
     OrderViewModel orderVM = new OrderViewModel
     {
         //TotalPages = (int)Math.Ceiling((double)_documentRepository.GetCount() / (double)PageSize),
         TotalPages =orders.PageCount,
         Items =orders.Select(n=>MapMainOrderSummary(n)).ToList()
     };
     return orderVM;
 }
        public OrderViewModel GetAllApprovedOrders(int CurrentPage, int PageSize)
        {
            DateTime sdate = DateTime.Now.AddDays(-30);
            DateTime edate = DateTime.Now;
            var orders = _mainOrderRepository.PagedPurchaseDocumentList(CurrentPage, PageSize, sdate, edate, DocumentStatus.Approved);
            OrderViewModel orderVM = new OrderViewModel
            {

                TotalPages = orders.PageCount,
                Items = orders
               .Select(n => MapMainOrderSummary(n))
               .OrderByDescending(d => d.orderDate.Year)
               .ThenBy(d => d.orderDate.Month)
               .ThenBy(d => d.orderDate.Day)
               .ThenBy(d => d.orderDate.TimeOfDay)
               .ToList()

            };
            return orderVM;
        }
        public OrderViewModel GetByDist(Guid distributor, int CurrentPage, int PageSize, bool inactive = false)
        {
            var orders = new List<Order>();
            if (distributor != Guid.Empty)
            {
                orders = _orderRepository.GetAll().Where(n => (n.OrderType == OrderType.DistributorToProducer) && (n.DocumentIssuerCostCentre.Id == distributor)).ToList();
            }
            else
            {
                orders = _orderRepository.GetAll().Where(n => (n.OrderType == OrderType.DistributorToProducer)).ToList();
            }

            OrderViewModel orderVm = new OrderViewModel
            {
                TotalPages = (int)Math.Ceiling((double)_orderRepository.GetCount() / (double)PageSize),
                Items = orders.Skip((CurrentPage - 1) * PageSize).Take(PageSize).Select(MapSkipAndTake).ToList()
            };

            return orderVm;
        }
        public OrderViewModel GetOrdersSkipAndTake(int CurrentPage, int PageSize, bool inactive = false)
        {
            DateTime sdate= DateTime.Now.AddDays(-30);
            DateTime edate= DateTime.Now;
            var orders = _mainOrderRepository.PagedPurchaseDocumentList(CurrentPage,PageSize,sdate,edate,DocumentStatus.Confirmed);

            

            OrderViewModel orderVM = new OrderViewModel
            {
                TotalPages = orders.PageCount,
                Items = orders
                .Select(n => MapMainOrderSummary(n)).ToList()
            };
            return orderVM;
        }
        public void SaveOrder(OrderViewModel ovm)
        {
            ValidationResultInfo vri = ovm.BasicValidation();
            if (ovm.distributorCode != null || ovm.salesManCode != null || ovm.outletCode != null)
            {
                CostCentre distributor = _costCentreRepository.GetAll().FirstOrDefault(n => n.CostCentreCode == ovm.distributorCode);
                if (distributor == null)
                    throw new DomainValidationException(vri, "Distributor Not Found\nPlease Check Distributor Code");
                CostCentre outlet = _costCentreRepository.GetAll().FirstOrDefault(n => n.CostCentreCode == ovm.outletCode);
                if (outlet == null)
                    throw new DomainValidationException(vri, "Outlet Not Found\nPlease Check Outlet Code");
                CostCentre salesman = _costCentreRepository.GetAll().FirstOrDefault(n => n.CostCentreCode == ovm.salesManCode) as DistributorSalesman;
                if (salesman == null)

                    throw new DomainValidationException(vri, "Salesman not found\nPlease Check DistributorSalesman Code");

                User docIssuerUser = _userRepository.GetAll().FirstOrDefault(n => n.CostCentre == salesman.Id);
                Product product = _productRepository.GetAll().FirstOrDefault(n => n.ProductCode == ovm.productCode);
                if (product == null)
                    throw new DomainValidationException(vri, "Product not found");
                Order orderCC = _documentFactory.CreateDocument(Guid.NewGuid(), DocumentType.Order, salesman, distributor, docIssuerUser, Guid.NewGuid().ToString()) as Order;

            }
        }