/// <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));
        }
        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));
        }