async Task LoadGrid()
        {
            fromDate = FromDate.DateTime;
            toDate   = ToDate.DateTime;

            var filters = new List <PropValuePair>();

            if (fromDate != DateTime.MinValue)
            {
                filters.Add(PropValuePair.GenereteParameter("FromDate", typeof(string), Convert.ToString(fromDate.Ticks)));
            }
            if (toDate != DateTime.MinValue)
            {
                filters.Add(PropValuePair.GenereteParameter("ToDate", typeof(string), Convert.ToString(toDate.Ticks)));
            }

            busyIndicator.IsBusy = true;
            var transSum = await api.Query(new FAMTransSumClient(), null, filters);

            if (transSum == null)
            {
                dgFAMTransSumGrid.ItemsSource = null;
            }
            else
            {
                Array.Sort(transSum, new FAMTransSumSort());
                dgFAMTransSumGrid.SetSource(transSum);
            }
            busyIndicator.IsBusy = false;
        }
Example #2
0
 public override Task InitQuery()
 {
     PropValuePair[] propValuePair = null;
     if (master != null)
     {
         // we do this to select all lines. also the hidden ones.
         propValuePair = new PropValuePair[] { PropValuePair.GenereteParameter("ShowAllTrans", typeof(int), "1") };
     }
     return(dgInvLines.Filter(propValuePair));
 }
Example #3
0
        Task LoadGrid()
        {
            List <PropValuePair> filter;

            if (includeSubProject)
            {
                filter = new List <PropValuePair>()
                {
                    PropValuePair.GenereteParameter("IncludeSubProject", typeof(string), "1")
                }
            }
            ;
            else
            {
                filter = null;
            }
            return(dgProjectTransaction.Filter(filter));
        }
        async Task LoadGrid()
        {
            List <PropValuePair> filter;

            if (includeSubProject)
            {
                filter = new List <PropValuePair>()
                {
                    PropValuePair.GenereteParameter("IncludeSubProject", typeof(string), "1")
                }
            }
            ;
            else
            {
                filter = null;
            }

            await dgProjectTransaction.Filter(filter);

            ClearTimeTrans();
            IncludeTimeJournals();
        }
        async void AddAttachment(string actionType, ProductionOrderLineClient productionOrderLineClient)
        {
            var invBomResult = await api.Query <InvBOMClient>(productionOrderLineClient, new[] { PropValuePair.GenereteParameter("ItemPart", typeof(string), "1") });

            if (invBomResult != null && invBomResult.Length > 0)
            {
                AddDockItem(actionType == "AddNote" ? TabControls.UserNotesPage : TabControls.UserDocsPage, invBomResult[0]);
            }
        }
        async Task LoadGrid()
        {
            fromDate    = txtFromDate.DateTime;
            toDate      = txtToDate.DateTime;
            budgetGroup = cmbBudgetGroup.Text;
            grpWeek     = chkGroupWeek.IsChecked.Value;
            grpPrevYear = chkGroupPrevYear.IsChecked.Value;

            fieldQtyPrev.Visible                   = grpPrevYear;
            fieldCostPrev.Visible                  = grpPrevYear;
            fieldSalesPrev.Visible                 = grpPrevYear;
            fieldQtyActualPrevBudDiff.Visible      = grpPrevYear;
            fieldSalesActualPrevBudgetDiff.Visible = grpPrevYear;
            fieldCostActualPrevBudgetDiff.Visible  = grpPrevYear;

            fieldAnchorBudgetQty.Visible                 = false;
            fieldAnchorBudgetCost.Visible                = false;
            fieldAnchorBudgetSales.Visible               = false;
            fieldQtyNormAnchorBudDiff.Visible            = false;
            fieldQtyActualAnchorBudDiff.Visible          = false;
            fieldQtyActualPrevAnchorBudDiff.Visible      = false;
            fieldQtyAnchorBudBudDiff.Visible             = false;
            fieldSalesActualAnchorBudgetDiff.Visible     = false;
            fieldCostActualAnchorBudgetDiff.Visible      = false;
            fieldSalesActualPrevAnchorBudgetDiff.Visible = false;
            fieldCostActualPrevAnchorBudgetDiff.Visible  = false;
            fieldSalesAnchorBudBudDiff.Visible           = false;
            fieldCostAnchorBudBudDiff.Visible            = false;

            if (string.IsNullOrEmpty(budgetGroup))
            {
                var msgText = FieldCannotBeEmpty(Uniconta.ClientTools.Localization.lookup("BudgetGroup"));
                UnicontaMessageBox.Show(msgText, Uniconta.ClientTools.Localization.lookup("Warning"), MessageBoxButton.OK);
                return;
            }

            if (fromDate == DateTime.MinValue)
            {
                var msgText = FieldCannotBeEmpty(Uniconta.ClientTools.Localization.lookup("FromDate"));
                UnicontaMessageBox.Show(msgText, Uniconta.ClientTools.Localization.lookup("Warning"), MessageBoxButton.OK);
                return;
            }

            if (toDate == DateTime.MinValue)
            {
                var msgText = FieldCannotBeEmpty(Uniconta.ClientTools.Localization.lookup("ToDate"));
                UnicontaMessageBox.Show(msgText, Uniconta.ClientTools.Localization.lookup("Warning"), MessageBoxButton.OK);
                return;
            }

            busyIndicator.IsBusy = true;

            List <PropValuePair> filter = new List <PropValuePair>();

            if (fromDate != DateTime.MinValue)
            {
                var propValuePairFolder = PropValuePair.GenereteParameter("FromDate", typeof(string), Convert.ToString(fromDate.Ticks));
                filter.Add(propValuePairFolder);
            }
            if (toDate != DateTime.MinValue)
            {
                var propValuePairFolder = PropValuePair.GenereteParameter("ToDate", typeof(string), Convert.ToString(toDate.Ticks));
                filter.Add(propValuePairFolder);
            }
            if (budgetGroup != null)
            {
                var propValuePairFolder = PropValuePair.GenereteParameter("BudgetGroup", typeof(string), budgetGroup);
                filter.Add(propValuePairFolder);
            }

            var api       = this.api;
            var CompanyId = api.CompanyId;

            var trans = await api.Query(new ProjectTransPivotClient(), new [] { master }, filter);

            if (trans == null)
            {
                return;
            }

            var len  = trans.Length;
            var sort = new ProjectTransBudgetPivotSort();

            Array.Sort(trans, sort);

            List <ProjectTransPivotClient> extras = null;

            if (showBudget)
            {
                var budget = await api.Query(new ProjectBudgetPivotClient(), new List <UnicontaBaseEntity>() { master }, filter);

                var key = new ProjectTransPivotClient();
                foreach (var bc in budget)
                {
                    key._Project         = bc._Project;
                    key._Date            = bc._Date;
                    key._PrCategory      = bc._PrCategory;
                    key._PayrollCategory = bc._PayrollCategory;
                    key._Employee        = bc.Employee;
                    var idx = Array.BinarySearch(trans, key, sort);
                    if (idx >= 0 && idx < len)
                    {
                        var t = trans[idx];
                        if (bc._AnchorBudget)
                        {
                            t._AnchorBudgetSales += bc._Sales;
                            t._AnchorBudgetCost  += bc._Cost;
                            t._AnchorBudgetQty   += bc._Qty;
                        }
                        else
                        {
                            t._BudgetSales += bc._Sales;
                            t._BudgetCost  += bc._Cost;
                            t._BudgetQty   += bc._Qty;
                        }
                    }
                    else
                    {
                        var prTrans = new ProjectTransPivotClient()
                        {
                            _CompanyId       = CompanyId,
                            _PrCategory      = bc._PrCategory,
                            _Project         = bc._Project,
                            _Date            = bc._Date,
                            _Employee        = bc._Employee,
                            _PayrollCategory = bc._PayrollCategory,
                        };

                        if (bc._AnchorBudget)
                        {
                            prTrans._AnchorBudgetSales = bc._Sales;
                            prTrans._AnchorBudgetCost  = bc._Cost;
                            prTrans._AnchorBudgetQty   = bc._Qty;
                        }
                        else
                        {
                            prTrans._BudgetSales = bc._Sales;
                            prTrans._BudgetCost  = bc._Cost;
                            prTrans._BudgetQty   = bc._Qty;
                        }

                        if (extras == null)
                        {
                            extras = new List <ProjectTransPivotClient>();
                        }
                        extras.Add(prTrans);
                    }
                }
            }

            if (grpPrevYear)
            {
                foreach (var p in filter)
                {
                    if (p.Prop == "FromDate")
                    {
                        p.Arg = Convert.ToString(fromDate.AddYears(-1).Ticks);
                    }
                    if (p.Prop == "ToDate")
                    {
                        p.Arg = Convert.ToString(toDate.AddYears(-1).Ticks);
                    }
                }

                var transTaskPrev = api.Query(new ProjectTransPivotClient(), new [] { master }, filter);
                var transPrev     = await transTaskPrev;

                foreach (var y in transPrev)
                {
                    var prTrans = new ProjectTransPivotClient()
                    {
                        _CompanyId       = CompanyId,
                        _SalesPrev       = y._Sales,
                        _CostPrev        = y._Cost,
                        _QtyPrev         = y._Qty,
                        _PrCategory      = y._PrCategory,
                        _Project         = y._Project,
                        _Date            = y._Date,
                        _Employee        = y._Employee,
                        _PayrollCategory = y._PayrollCategory
                    };

                    if (extras == null)
                    {
                        extras = new List <ProjectTransPivotClient>();
                    }
                    extras.Add(prTrans);
                }
            }

            if (extras != null)
            {
                Array.Resize(ref trans, len + extras.Count);
                foreach (var sum in extras)
                {
                    trans[len++] = sum;
                }
                Array.Sort(trans, sort);
                extras = null;
            }

            int start = 0;

            while (start < len)
            {
                int    end;
                string ProjectNumber;
                if (master == null)
                {
                    ProjectNumber = trans[start]._Project;
                    for (end = start; (end < len && trans[end]._Project == ProjectNumber); end++)
                    {
                        ;
                    }
                }
                else
                {
                    ProjectNumber = ((Uniconta.DataModel.Project)master)._Number;
                    end           = len;
                }

                start = end;
            }

            #region Norm Calendar

            CalenderNormLst[] normLst = null;

            var pairCalendarLine = new PropValuePair[]
            {
                PropValuePair.GenereteWhereElements(nameof(TMEmpCalendarLineClient.Date), typeof(DateTime), String.Format("{0:d}..{1:d}", fromDate, toDate))
            };

            var tmEmpCalenderLineLst = await api.Query <TMEmpCalendarLineClient>(pairCalendarLine);

            if (tmEmpCalenderLineLst.Length > 0)
            {
                if (grpWeek)
                {
                    var grpCalendarLst = tmEmpCalenderLineLst.GroupBy(x => new { x.Calendar, PeriodFirstDate = x.WeekMonday }).Select(x => new { x.Key, Hours = x.Sum(y => y.Hours) });
                    foreach (var rec in grpCalendarLst)
                    {
                        var normTrans = new CalenderNormLst()
                        {
                            CalendarId = rec.Key.Calendar,
                            Date       = rec.Key.PeriodFirstDate,
                            NormQty    = rec.Hours
                        };

                        if (normLst == null)
                        {
                            normLst = new CalenderNormLst[] { normTrans }
                        }
                        ;
                        else
                        {
                            Array.Resize(ref normLst, normLst.Length + 1);
                            normLst[normLst.Length - 1] = normTrans;
                        }
                    }
                }
                else
                {
                    var grpCalendarLst = tmEmpCalenderLineLst.GroupBy(x => new { x.Calendar, x.FirstDayOfMonth }).Select(x => new { Key = x.Key, Hours = x.Sum(y => y.Hours) }).ToList();

                    foreach (var rec in grpCalendarLst)
                    {
                        var normTrans = new CalenderNormLst()
                        {
                            CalendarId = rec.Key.Calendar,
                            Date       = rec.Key.FirstDayOfMonth,
                            NormQty    = rec.Hours
                        };

                        if (normLst == null)
                        {
                            normLst = new CalenderNormLst[] { normTrans }
                        }
                        ;
                        else
                        {
                            Array.Resize(ref normLst, normLst.Length + 1);
                            normLst[normLst.Length - 1] = normTrans;
                        }
                    }
                }

                var normCalSort = new SortNormCalendar();
                Array.Sort(normLst, normCalSort);

                var lstCalendarSetup = await api.Query <TMEmpCalendarSetupClient>();

                var calSetupSort = new SortCalendarSetup();
                Array.Sort(lstCalendarSetup, calSetupSort);

                var calenders      = new List <TMEmpCalendarSetupClient>(10);
                var searchCalSetup = new TMEmpCalendarSetupClient();

                var empNormLst = new List <ProjectTransPivotClient>();
                var searchNorm = new CalenderNormLst();

                foreach (var empl in employeeCache)
                {
                    var curEmployee = empl._Number;

                    calenders.Clear();

                    searchCalSetup.Employee = curEmployee;
                    var posCalSetup = Array.BinarySearch(lstCalendarSetup, searchCalSetup, calSetupSort);
                    if (posCalSetup < 0)
                    {
                        posCalSetup = ~posCalSetup;
                    }
                    while (posCalSetup < lstCalendarSetup.Length)
                    {
                        var s = lstCalendarSetup[posCalSetup++];
                        if (s.Employee != curEmployee)
                        {
                            break;
                        }

                        calenders.Add(s);
                    }

                    if (calenders.Count == 0)
                    {
                        continue;
                    }

                    if (grpWeek)
                    {
                        foreach (var rec in calenders.OrderBy(s => s.ValidFrom))
                        {
                            var newStartDate = rec._ValidFrom != DateTime.MinValue && rec._ValidFrom > fromDate ? rec._ValidFrom : fromDate;
                            var empStartDate = empl._Hired == DateTime.MinValue ? newStartDate : empl._Hired > newStartDate ? empl._Hired : newStartDate;

                            var newEndDate = rec._ValidTo != DateTime.MinValue && rec._ValidTo < toDate ? rec._ValidTo : toDate;
                            var empEndDate = empl._Terminated == DateTime.MinValue ? newEndDate : empl._Terminated < newEndDate ? empl._Terminated : newEndDate;

                            var empFirstDayOfWk = FirstDayOfWeek(empStartDate);

                            searchNorm.CalendarId = rec._Calendar;
                            searchNorm.Date       = empFirstDayOfWk;

                            var pos = Array.BinarySearch(normLst, searchNorm, normCalSort);
                            if (pos < 0)
                            {
                                pos = ~pos;
                            }
                            while (pos < normLst.Length)
                            {
                                var s = normLst[pos++];
                                if (s.CalendarId != rec._Calendar || s.Date > empEndDate)
                                {
                                    break;
                                }

                                if (s.Date >= empStartDate && s.Date <= empEndDate)
                                {
                                    var newTrans = new ProjectTransPivotClient()
                                    {
                                        _CompanyId = CompanyId,
                                        _Employee  = curEmployee,
                                        _Date      = s.Date,
                                        _NormQty   = s.NormQty
                                    };

                                    empNormLst.Add(newTrans);
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (var rec in calenders.OrderBy(s => s.ValidFrom))
                        {
                            var newStartDate = rec._ValidFrom != DateTime.MinValue && rec._ValidFrom > fromDate ? rec._ValidFrom : fromDate;
                            var empStartDate = empl._Hired == DateTime.MinValue ? newStartDate : empl._Hired > newStartDate ? empl._Hired : newStartDate;

                            var newEndDate = rec._ValidTo != DateTime.MinValue && rec._ValidTo < toDate ? rec._ValidTo : toDate;
                            var empEndDate = empl._Terminated == DateTime.MinValue ? newEndDate : empl._Terminated < newEndDate ? empl._Terminated : newEndDate;

                            var empFirstDayOfMonth = FirstDayOfMonth(empStartDate);
                            var empLastDayOfMonth  = LastDayOfMonth(empEndDate);

                            int empFirstMth = 0, empLastMth = 0;

                            if (empFirstDayOfMonth != empStartDate)
                            {
                                empFirstMth = empStartDate.Month;
                            }

                            if (empLastDayOfMonth != empEndDate)
                            {
                                empLastMth = empEndDate.Month;
                            }

                            searchNorm.CalendarId = rec._Calendar;
                            searchNorm.Date       = empFirstDayOfMonth;

                            var pos = Array.BinarySearch(normLst, searchNorm, normCalSort);
                            if (pos < 0)
                            {
                                pos = ~pos;
                            }
                            while (pos < normLst.Length)
                            {
                                var s = normLst[pos++];
                                if (s.CalendarId != rec._Calendar || s.Date > empEndDate)
                                {
                                    break;
                                }


                                if (empFirstMth != 0 && s.Date.Month == empFirstMth)
                                {
                                    var firstDayOfMonth = FirstDayOfMonth(empStartDate);
                                    var lastDayOfMonth  = firstDayOfMonth.AddMonths(1).AddDays(-1);

                                    var hours = tmEmpCalenderLineLst.Where(x => x._Calendar == s.CalendarId && x.Date >= empStartDate && x.Date <= lastDayOfMonth).Sum(y => y._Hours);

                                    var newTrans = new ProjectTransPivotClient()
                                    {
                                        _CompanyId = CompanyId,
                                        _Employee  = curEmployee,
                                        _Date      = firstDayOfMonth,
                                        _NormQty   = hours
                                    };

                                    empNormLst.Add(newTrans);
                                }
                                else if (empLastMth != 0 && s.Date.Month == empLastMth)
                                {
                                    var firstDayOfMonth = FirstDayOfMonth(empEndDate);
                                    var lastDayOfMonth  = firstDayOfMonth.AddMonths(1).AddDays(-1);

                                    var hours = tmEmpCalenderLineLst.Where(x => x._Calendar == s.CalendarId && x.Date >= firstDayOfMonth && x.Date <= empEndDate).Sum(y => y._Hours);

                                    var newTrans = new ProjectTransPivotClient()
                                    {
                                        _CompanyId = CompanyId,
                                        _Employee  = curEmployee,
                                        _Date      = firstDayOfMonth,
                                        _NormQty   = hours
                                    };

                                    empNormLst.Add(newTrans);
                                }
                                else if (s.Date >= empStartDate && s.Date <= empEndDate)
                                {
                                    var newTrans = new ProjectTransPivotClient()
                                    {
                                        _CompanyId = CompanyId,
                                        _Employee  = curEmployee,
                                        _Date      = s.Date,
                                        _NormQty   = s.NormQty
                                    };

                                    empNormLst.Add(newTrans);
                                }
                            }
                        }
                    }
                }
                if (empNormLst.Count > 0)
                {
                    Array.Resize(ref trans, len + empNormLst.Count);
                    foreach (var norm in empNormLst)
                    {
                        trans[len++] = norm;
                    }

                    Array.Sort(trans, sort);
                }
            }
            #endregion Norm Calendar

            pivotDgProjectPlanning.DataSource = trans as IList;
            if (!isPivotIsVisible)
            {
                pivotDgProjectPlanning.EndUpdate();
                pivotDgProjectPlanning.Visibility = Visibility.Visible;
                isPivotIsVisible = true;
            }
            pivotDgProjectPlanning.RefreshData();
            busyIndicator.IsBusy = false;
        }
        async Task LoadGrid()
        {
            fromDate = FromDate.DateTime;
            toDate   = ToDate.DateTime;

            List <PropValuePair> filter = new List <PropValuePair>();

            if (includeSubProject)
            {
                var propValuePairFolder = PropValuePair.GenereteParameter("IncludeSubProject", typeof(string), "1");
                filter.Add(propValuePairFolder);
            }
            if (InvoicedTrans > 0)
            {
                var propValuePairFolder = PropValuePair.GenereteParameter("InvoicedTrans", typeof(string), NumberConvert.ToString(InvoicedTrans));
                filter.Add(propValuePairFolder);
            }
            if (fromDate != DateTime.MinValue)
            {
                var propValuePairFolder = PropValuePair.GenereteParameter("FromDate", typeof(string), NumberConvert.ToString(fromDate.Ticks));
                filter.Add(propValuePairFolder);
            }
            if (toDate != DateTime.MinValue)
            {
                var propValuePairFolder = PropValuePair.GenereteParameter("ToDate", typeof(string), NumberConvert.ToString(toDate.Ticks));
                filter.Add(propValuePairFolder);
            }

            var api       = this.api;
            var CompanyId = api.CompanyId;

            var cats = api.CompanyEntity.GetCache(typeof(PrCategory)) ?? await api.CompanyEntity.LoadCache(typeof(PrCategory), api);

            var transTask = api.Query(new ProjectTransCategorySumClientLocal(), dgProjectTransCategorySum.masterRecords, filter);

            var trans = await transTask;

            if (trans == null)
            {
                dgProjectTransCategorySum.SetSource(null);
                return;
            }

            var len  = trans.Length;
            var sort = new ProjectTransCategorySort();

            Array.Sort(trans, sort);

            List <ProjectTransCategorySumClientLocal> extras = null, sums = new List <ProjectTransCategorySumClientLocal>();

            if (showBudget)
            {
                var budget = await api.Query(new ProjectBudgetCategorySumClient(), dgProjectTransCategorySum.masterRecords, filter);

                var key = new ProjectTransCategorySumClientLocal();
                foreach (var bc in budget)
                {
                    key._Project    = bc._Project;
                    key._PrCategory = bc._PrCategory;
                    var idx = Array.BinarySearch(trans, key, sort);
                    if (idx >= 0 && idx < len)
                    {
                        var t = trans[idx];
                        t._BudgetSales += bc._Sales;
                        t._BudgetCost  += bc._Cost;
                        t._BudgetQty   += bc._Qty;
                    }
                    else
                    {
                        var prTrans = new ProjectTransCategorySumClientLocal()
                        {
                            _CompanyId = CompanyId, _BudgetSales = bc._Sales, _BudgetCost = bc._Cost, _BudgetQty = bc._Qty, _PrCategory = bc._PrCategory, _Project = bc._Project
                        };

                        var cat = (PrCategory)cats.Get(bc._PrCategory);
                        prTrans._CatType = cat._CatType;
                        if (cat._CatType == CategoryType.Sum || cat._CatType == CategoryType.Header)
                        {
                            sums.Add(prTrans);
                        }
                        else
                        {
                            if (extras == null)
                            {
                                extras = new List <ProjectTransCategorySumClientLocal>();
                            }
                            extras.Add(prTrans);
                        }
                    }
                }
                if (extras != null)
                {
                    Array.Resize(ref trans, len + extras.Count);
                    foreach (var sum in extras)
                    {
                        trans[len++] = sum;
                    }
                    Array.Sort(trans, sort);
                    extras = null;
                }
            }

            foreach (var t in trans)
            {
                var cat = (PrCategory)cats.Get(t._PrCategory);
                if (cat != null && (cat._CatType == CategoryType.Revenue || cat._CatType == CategoryType.OnAccountInvoicing))
                {
                    t._InvoicedQty       = -t._Qty;
                    t._Invoiced          = -t._Sales;
                    t._BudgetInvoicedQty = -t._BudgetQty;
                    t._BudgetInvoiced    = -t._BudgetSales;

                    t._Qty         = 0;
                    t._BudgetQty   = 0;
                    t._Cost        = 0;
                    t._Sales       = 0;
                    t._BudgetCost  = 0;
                    t._BudgetSales = 0;
                }
            }

            int start = 0;

            while (start < len)
            {
                int    end;
                string ProjectNumber;
                if (master == null)
                {
                    ProjectNumber = trans[start]._Project;
                    for (end = start; (end < len && trans[end]._Project == ProjectNumber); end++)
                    {
                        ;
                    }
                }
                else
                {
                    ProjectNumber = ((Uniconta.DataModel.Project)master)._Number;
                    end           = len;
                }

                int headerAddedLast = 0;
                foreach (var cat in (PrCategory[])cats.GetKeyStrRecords)
                {
                    if (cat != null && (cat._CatType == CategoryType.Sum || cat._CatType == CategoryType.Header))
                    {
                        PropValuePair SumList = AccountSum.Generate(cat._Sum);
                        if (SumList != null)
                        {
                            double Sales = 0, Cost = 0, Qty = 0, BudgetSales = 0, BudgetQty = 0, BudgetCost = 0, Invoiced = 0, InvoicedBudget = 0, InvoicedQty = 0, InvoicedBudgetQty = 0;
                            for (int j = start; j < end; j++)
                            {
                                var Acc2 = trans[j];
                                if (AccountSum.IsIncluded(SumList, Acc2._PrCategory))
                                {
                                    Sales       += Acc2._Sales;
                                    Cost        += Acc2._Cost;
                                    Qty         += Acc2._Qty;
                                    BudgetCost  += Acc2._BudgetCost;
                                    BudgetSales += Acc2._BudgetSales;
                                    BudgetQty   += Acc2._BudgetQty;

                                    Invoiced          += Acc2._Invoiced;
                                    InvoicedBudget    += Acc2._BudgetInvoiced;
                                    InvoicedQty       += Acc2._InvoicedQty;
                                    InvoicedBudgetQty += Acc2._BudgetInvoicedQty;
                                }
                            }

                            var sum = new ProjectTransCategorySumClientLocal()
                            {
                                _CompanyId = CompanyId, _Project = ProjectNumber, _PrCategory = cat._Number, _CatType = cat._CatType
                            };

                            sum._Qty               = Math.Round(Qty, 2);
                            sum._BudgetQty         = Math.Round(BudgetQty, 2);
                            sum._Sales             = Math.Round(Sales, 2);
                            sum._Cost              = Math.Round(Cost, 2);
                            sum._BudgetSales       = Math.Round(BudgetSales, 2);
                            sum._BudgetCost        = Math.Round(BudgetCost, 2);
                            sum._Invoiced          = Math.Round(Invoiced, 2);
                            sum._BudgetInvoiced    = Math.Round(InvoicedBudget, 2);
                            sum._InvoicedQty       = Math.Round(InvoicedQty, 2);
                            sum._BudgetInvoicedQty = Math.Round(InvoicedBudgetQty, 2);
                            if (sum._Qty != 0 || sum._BudgetQty != 0 || sum._Sales != 0 || sum._Cost != 0 || sum._BudgetSales != 0 ||
                                sum._BudgetCost != 0 || sum._Invoiced != 0 || sum._BudgetInvoiced != 0 || sum._InvoicedQty != 0 || sum._BudgetInvoicedQty != 0)
                            {
                                sums.Add(sum);
                                headerAddedLast = 0;
                            }
                            else if (cat._CatType == CategoryType.Header)
                            {
                                sums.Add(sum);
                                headerAddedLast++;
                            }
                        }
                        else if (cat._CatType == CategoryType.Header)
                        {
                            sums.Add(new ProjectTransCategorySumClientLocal()
                            {
                                _CompanyId = CompanyId, _Project = ProjectNumber, _PrCategory = cat._Number, _CatType = CategoryType.Header
                            });
                            headerAddedLast++;
                        }
                    }
                }
                if (headerAddedLast > 0)
                {
                    sums.RemoveRange(sums.Count - headerAddedLast, headerAddedLast);
                }
                start = end;
            }

            if (sums.Count > 0)
            {
                Array.Resize(ref trans, len + sums.Count);
                foreach (var sum in sums)
                {
                    trans[len++] = sum;
                }
                Array.Sort(trans, sort);
            }

            dgProjectTransCategorySum.SetSource(trans);
        }