private void UpdateTopline(EditToplineViewModel model)
        {
            var uvm     = GetCurrentUserViewModel();
            var service = new BrandPeriodSalesService();
            var topline = service.GetToplineById(model.ToplineId);

            // update parent
            topline.NetSales            = ConvertHelpers.ToMoney(model.NetSales);
            topline.FranCalcRoyalty     = ConvertHelpers.ToMoney(model.FranCalcRoyalty);
            topline.FranCalcAdvertising = ConvertHelpers.ToMoney(model.FranCalcAdvertising);
            topline.TotalTickets        = ConvertHelpers.ToNumber(model.TotalTickets);

            if (model.IsApprove)
            {
                topline.BPSR_StatusID = (int)ToplineStatus.ForApproval;
                topline.SubmitterName = uvm.DisplayName;
            }
            else if (topline.BPSR_StatusID == (int)ToplineStatus.NotStarted || topline.BPSR_StatusID == (int)ToplineStatus.ForApproval)
            {
                topline.BPSR_StatusID = (int)ToplineStatus.InProgress;
            }

            // update children
            var requestedUpdates           = model.ProductGroups.ToDictionary(x => x.ProdSubGrpID, x => x);
            var potentialUpdates           = topline.BPSR_ProdGrp.ToList().ToDictionary(x => x.ProdSubGrpID, x => x);
            var availableToplinePrdSubGrps = _brandPeriodSalesService.GetToplinePrdSubGrps(topline.LocalStoreID, topline.SubBrandID, topline.PeriodEndDate).ToList();

            foreach (var current in requestedUpdates.Values)
            {
                if (current.BPSR_ProdGrpID == null && (current.NetSales != null || current.FranCalcRoyalty != null || current.FranCalcAdvertising != null || current.TotalTickets != null))
                {
                    // check if requested is valid
                    if (availableToplinePrdSubGrps.Any(a => a.ProdGrpID == current.ProdGrpID && a.ProdSubGrpID == current.ProdSubGrpID))
                    {
                        // check if already exists
                        var existingEntry = topline.BPSR_ProdGrp.FirstOrDefault(f => f.ProdGrpID == current.ProdGrpID && f.ProdSubGrpID == current.ProdSubGrpID);
                        if (existingEntry == null)
                        {
                            CreateToplineProductGroup(topline, current);
                        }
                        else
                        {
                            UpdateToplineProductGroup(existingEntry, current);
                        }
                    }
                }
                else
                {
                    // update existing entry
                    var existingEntry = topline.BPSR_ProdGrp.FirstOrDefault(f => f.BPSR_ProdGrpID == current.BPSR_ProdGrpID);
                    if (existingEntry != null)
                    {
                        UpdateToplineProductGroup(existingEntry, current);
                    }
                }
            }

            // save
            service.SaveChanges();
        }
        public ActionResult SubmitToplines(string toplines)
        {
            var success = false;
            var keyId   = string.Empty;
            var message = string.Empty;

            try
            {
                var entryCount = 0;
                var uvm        = GetCurrentUserViewModel();
                var service    = new BrandPeriodSalesService();

                foreach (var toplineId in toplines.Split(',').Select(t => int.Parse(t)).ToList())
                {
                    var topline = service.GetToplineById(toplineId);

                    if (topline != null && topline.SubBrandID == uvm.SubBrandId && CanSubmitTopline(topline.BPSR_StatusID, topline.SubmitterName, uvm.DisplayName, uvm.IsAdmin, uvm.IsApprover))
                    {
                        topline.ApproverName  = uvm.DisplayName;
                        topline.BPSR_StatusID = (int)ToplineStatus.Submitted;
                        service.SaveChanges();
                        entryCount += 1;
                    }
                }

                success = true;
                message = String.Format("Successfully approved {0} {1}.", entryCount, ((entryCount == 1) ? "Reporting Period" : "Reporting Periods"));
            }
            catch (Exception ex)
            {
                success = false;
                _log.Error(FormatException(ex));
                message = mc_ExceptionMessage_Error;
            }

            var result = BuildDialogResult(success, keyId, message);

            return(Json(result, JsonRequestBehavior.AllowGet));
        }