/// <summary>
        /// GET Run ProformaInvoice from DB
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Run(int?id)
        {
            RunInvoiceHeaderDTO serviceModel;

            try
            {
                //this should trigger the WCF to prepare billing items after being invoked
                //the mechanism to call is EXACTLY SAME with the mechanism to call service from SAP (in the beginning)
                serviceModel = WCFClientManager.SAPServiceClient.QueryRunInvoiceHeaderFromDB(
                    UserManagementHelper.GetSessionId(), id ?? 0);
            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
                return(RedirectToAction("Index", "Error"));
            }

            if (serviceModel == null)
            {
                ErrorSignal.FromCurrentContext().Raise(new Exception(ErrorResource.WCFNullObject));
                return(RedirectToAction("Index", "Error", new { errorKey = "WCFNullObject" }));
            }

            PopulateCategories();
            var viewModel = ProformaInvoiceHelper.GenerateRunProformaInvoiceViewModel(serviceModel);

            return(View(viewModel));
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public ActionResult Main()
        {
            // Clear existing session variables
            System.Web.HttpContext.Current.Session["RunInvoiceHeaderStacks"] = null;

            var viewModel = ProformaInvoiceHelper.GenerateMainProformaInvoiceViewModel();

            return(View(viewModel));
        }
        /// <summary>
        /// Populate dropdownlist which is diplayed inside of Kendo Grid
        /// </summary>
        private void PopulateCategories()
        {
            var categories = ProformaInvoiceHelper.GenerateReasonForRejectionListViewModel()
                             .Select(c => new CategoryViewModel
            {
                CategoryID   = c.CategoryID,
                CategoryName = c.CategoryName
            })
                             .OrderBy(e => e.CategoryID).Take(7);

            ViewData["categories"]      = categories;
            ViewData["defaultCategory"] = categories.FirstOrDefault();
        }
        public JsonResult BillingDocument(MainProformaInvoiceViewModel iModel)
        {
            BillingNumberItemDTO[] billingNumber;
            try
            {
                var dateFrom = DictionaryHelper.KendoDatePickerDateStringToDateTime(iModel.DateFromValue);
                var dateTo   = DictionaryHelper.KendoDatePickerDateStringToDateTime(iModel.DateToValue);
                billingNumber = WCFClientManager.SAPServiceClient.QueryBillingNumbers(UserManagementHelper.GetSessionId(),
                                                                                      iModel.SoldToFromValue, dateFrom, dateTo);
            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
                return(Json(new { html = ErrorResource.WCFCannotGetObjectHTML }));
            }

            var viewModel    = ProformaInvoiceHelper.GeneratePopupBillingDocumentViewModel(billingNumber, iModel);
            var renderedHtml = ViewHelper.RenderPartialViewToString(this, "_PartialPopupBillingDocumentList", viewModel);

            return(Json(new { html = renderedHtml }));
        }
        /// <summary>
        /// Asynchronus method to get Kendo Grid Datasource for Billing Items from DB
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult BillingItemsFromDB_Read([DataSourceRequest] DataSourceRequest request)
        {
            IEnumerable <InvoiceProformaItemDTO> serviceModels;

            try
            {
                serviceModels = WCFClientManager.SAPServiceClient.QueryRunInvoiceBillingsAll(UserManagementHelper.GetSessionId());
            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
                ModelState.AddModelError("Name", ex.Message);
                return(Json(ModelState.ToDataSourceResult()));
            }

            // Map to the following model.
            var result = ProformaInvoiceHelper.GenerateBillingItemsViewModelJSON(request, serviceModels);
            var json   = Json(result, JsonRequestBehavior.AllowGet);

            json.MaxJsonLength = int.MaxValue;
            return(json);
        }
        public ActionResult Run(PopupBillingDocumentViewModel iModel)
        {
            RunInvoiceHeaderDTO serviceModel;
            int numberOfRows = 0;

            try
            {
                serviceModel = WCFClientManager.SAPServiceClient.QueryRunInvoiceHeader(UserManagementHelper.GetSessionId(), iModel.Lines.SelectedValue, iModel.IsBlockedDocSelectedValue, iModel.IsBlockedDocSelectedValue, iModel.IsActiveDocSelectedValue);
                numberOfRows = WCFClientManager.SAPServiceClient.QueryTotalBillingRecords(UserManagementHelper.GetSessionId());
            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
                return(Json(new { urlToRedirect = "Error/" }, JsonRequestBehavior.AllowGet));
            }

            if (serviceModel == null)
            {
                ErrorSignal.FromCurrentContext().Raise(new Exception(ErrorResource.WCFNullObject));
                return(Json(new { urlToRedirect = "Error/" }, JsonRequestBehavior.AllowGet));
            }

            var viewModel = ProformaInvoiceHelper.GenerateRunProformaInvoiceViewModel(serviceModel);

            viewModel.EstimatedProcessTime = numberOfRows / 11;

            // Push to temporary stack in order that posted data can be accessed through GET
            var runInvoiceHeaderStacks = System.Web.HttpContext.Current.Session["RunInvoiceHeaderStacks"] != null ?
                                         System.Web.HttpContext.Current.Session["RunInvoiceHeaderStacks"] as Dictionary <int, RunProformaInvoiceHeaderViewModel>
                : new Dictionary <int, RunProformaInvoiceHeaderViewModel>();

            var indexMax = runInvoiceHeaderStacks.Count();

            runInvoiceHeaderStacks.Add(++indexMax, viewModel);
            System.Web.HttpContext.Current.Session["RunInvoiceHeaderStacks"] = runInvoiceHeaderStacks;

            return(Json(new { urlToRedirect = "CreateNew/" + indexMax }, JsonRequestBehavior.AllowGet));
        }
        /// <summary>
        /// Redirect to View CreateIncorrect
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateIncorrectExternalData()
        {
            var viewModel = ProformaInvoiceHelper.GenerateCreateIncorrectExternalDataViewModel();

            return(View(viewModel));
        }