public static void GetTotalCalculNormReport(int seriaFrom, int productChipher, ref DataTable dttemp)
        {
            var currentDetList = from infdet in FillTrudoyomkostDB.infDetList
                                 join whereuse in FillTrudoyomkostDB.whereUseList
                                 on infdet.ID equals whereuse.InfDetID
                                 where whereuse.SeriaFrom <= seriaFrom && seriaFrom <= whereuse.SeriaTo && whereuse.InfProductsCipher == productChipher
                                 select new
                                 {
                                     infdet.ID,
                                     whereuse.CountPerProduct
                                 };

            var result = from labourNorm in FillTrudoyomkostDB.LabourNormList
                         join det in currentDetList
                         on labourNorm.InfDetID equals det.ID
                         join whereOperUse in FillTrudoyomkostDB.WhereOperationUseList
                         on labourNorm.ID equals whereOperUse.LabourNormID
                         where labourNorm.WorkerRate > 0.0 && labourNorm.ProfCode != 0 && whereOperUse.SeriaFrom <= seriaFrom && seriaFrom <= whereOperUse.SeriaTo && whereOperUse.InfProductsChipher == productChipher
                         orderby labourNorm.KindPay
                         orderby labourNorm.WorkerRate
                         select new
                         {
                             det.CountPerProduct,
                             labourNorm.InfDetID,
                             OperNum = labourNorm.OperNum,
                             labourNorm.WorkerRate,
                             labourNorm.KindPay,
                             labourNorm.ProfCode,
                             NameKindWork = labourNorm.NameKindWork,
                             labourNorm.TariffNetNum,
                             labourNorm.ItemCTN,
                             labourNorm.PreparTimeCTN,
                             labourNorm.ItemPayNorm,
                             labourNorm.PreparTimePayNorm,
                             labourNorm.Valuation,
                             labourNorm.ValPreparTime,
                             labourNorm.CoeffCTN,
                             DocNum = labourNorm.DocNum,
                             labourNorm.DepRegion,
                             labourNorm.Date,
                             TaskNumber = labourNorm.TaskNumber,

                         }
                             into TotalRes
                             group TotalRes by TotalRes.ProfCode
                                 into groupAllinfo
                                 select new
                                 {
                                     Key = groupAllinfo.Key,
                                     Value = groupAllinfo
                                 };

            NormTotal kindProfAndRateTotal = new NormTotal();
            NormTotal ProffCodeTotal = new NormTotal();
            NormTotal TotalForProductByTheJob = new NormTotal();
            NormTotal TotalForProductByTheTime = new NormTotal();

            dttemp.Clear();
            foreach (var item in result)
            {
                DataRow rowdttemp = dttemp.NewRow();

                rowdttemp["ProfCode"] = item.Key;
                rowdttemp["NameKindWork"] = item.Value.First().NameKindWork;

                string beginKindPay = item.Value.First().KindPay;
                double beginWorkRate = item.Value.First().WorkerRate;

                dttemp.Rows.Add(rowdttemp);

                string t = item.Key.ToString();
                foreach (var labour in item.Value)
                {

                    if ((beginKindPay != labour.KindPay || beginWorkRate != Math.Round(labour.WorkerRate, 2)))
                    {

                        AddRowIntoDgTotal(dttemp, ref kindProfAndRateTotal, ref rowdttemp, beginKindPay, beginWorkRate);

                        kindProfAndRateTotal.Clear();
                        beginKindPay = labour.KindPay;
                        beginWorkRate = Math.Round(labour.WorkerRate, 2);
                    }

                    kindProfAndRateTotal.CalcTotal(labour.ItemCTN * labour.CountPerProduct,
                                                    labour.PreparTimeCTN,
                                                    labour.ItemPayNorm * labour.CountPerProduct,
                                                    labour.PreparTimePayNorm,
                                                    labour.Valuation,
                                                    labour.ValPreparTime ?? 0
                                                    );

                    if (labour == item.Value.Last())
                    {
                        AddRowIntoDgTotal(dttemp, ref kindProfAndRateTotal, ref rowdttemp, beginKindPay, beginWorkRate);

                        kindProfAndRateTotal.Clear();
                        rowdttemp = dttemp.NewRow();
                        dttemp.Rows.Add(rowdttemp);
                        //item.Value.
                        rowdttemp = dttemp.NewRow();
                        beginKindPay = "С";

                        ProffCodeTotal.ItemCTNSum = item.Value.Where(lbn => lbn.KindPay == beginKindPay).Sum(lbn => lbn.ItemCTN * lbn.CountPerProduct);
                        ProffCodeTotal.PrTimeCTNSum = item.Value.Where(lbn => lbn.KindPay == beginKindPay).Sum(lbn => lbn.PreparTimeCTN);
                        ProffCodeTotal.ItemPaySum = item.Value.Where(lbn => lbn.KindPay == beginKindPay).Sum(lbn => lbn.ItemPayNorm * lbn.CountPerProduct);
                        ProffCodeTotal.PrTimePaySum = item.Value.Where(lbn => lbn.KindPay == beginKindPay).Sum(lbn => lbn.PreparTimePayNorm);
                        ProffCodeTotal.ValuateSum = item.Value.Where(lbn => lbn.KindPay == beginKindPay).Sum(lbn => lbn.Valuation);
                        ProffCodeTotal.ValuatePrTimeSum = item.Value.Where(lbn => lbn.KindPay == beginKindPay).Sum(lbn => lbn.ValPreparTime);
                        TotalForProductByTheJob.CalcTotal(ProffCodeTotal.ItemCTNSum, ProffCodeTotal.PrTimeCTNSum, ProffCodeTotal.ItemPaySum, ProffCodeTotal.PrTimePaySum, ProffCodeTotal.ValuateSum, ProffCodeTotal.ValuatePrTimeSum ?? 0);
                        AddRowIntoDgTotal(dttemp, ref ProffCodeTotal, ref rowdttemp, beginKindPay);

                        beginKindPay = "П";
                        ProffCodeTotal.ItemCTNSum = item.Value.Where(lbn => lbn.KindPay == beginKindPay).Sum(lbn => lbn.ItemCTN * lbn.CountPerProduct);
                        ProffCodeTotal.PrTimeCTNSum = item.Value.Where(lbn => lbn.KindPay == beginKindPay).Sum(lbn => lbn.PreparTimeCTN);
                        ProffCodeTotal.ItemPaySum = item.Value.Where(lbn => lbn.KindPay == beginKindPay).Sum(lbn => lbn.ItemPayNorm * lbn.CountPerProduct);
                        ProffCodeTotal.PrTimePaySum = item.Value.Where(lbn => lbn.KindPay == beginKindPay).Sum(lbn => lbn.PreparTimePayNorm);
                        ProffCodeTotal.ValuateSum = item.Value.Where(lbn => lbn.KindPay == beginKindPay).Sum(lbn => lbn.Valuation);
                        ProffCodeTotal.ValuatePrTimeSum = item.Value.Where(lbn => lbn.KindPay == beginKindPay).Sum(lbn => lbn.ValPreparTime);
                        TotalForProductByTheTime.CalcTotal(ProffCodeTotal.ItemCTNSum, ProffCodeTotal.PrTimeCTNSum, ProffCodeTotal.ItemPaySum, ProffCodeTotal.PrTimePaySum, ProffCodeTotal.ValuateSum, ProffCodeTotal.ValuatePrTimeSum ?? 0);
                        AddRowIntoDgTotal(dttemp, ref ProffCodeTotal, ref rowdttemp, beginKindPay);

                    }

                }
                dttemp.Rows.Add(dttemp.NewRow());
            }
            AddRowIntoDgTotal(dttemp, TotalForProductByTheJob, "Итог для изделия Сдельно");
            AddRowIntoDgTotal(dttemp, TotalForProductByTheTime, "Итог для изделия Повременно");
        }
        public static void FilldtNormViewer(int seriaFrom, int seriaTo, int infProdChipher, ref TrudoyomkostDBDataSet.LabourNormDataTable dttemp, bool timeInHours, ref NormTotal normTotalbyTheJob, ref NormTotal normTotalbyTheTime)
        {
            var tempResult = from labourNorm in FillTrudoyomkostDB.CurrentLabourNormList
                             join whereOpeationUse in FillTrudoyomkostDB.WhereOperationUseList on labourNorm.ID
                                 equals whereOpeationUse.LabourNormID
                             where
                                 whereOpeationUse.InfProductsChipher == infProdChipher &&
                                 ((whereOpeationUse.SeriaFrom == seriaFrom && seriaFrom <= whereOpeationUse.SeriaTo) || (whereOpeationUse.SeriaFrom <= seriaFrom && whereOpeationUse.SeriaTo >= seriaFrom))

                             select labourNorm

                                 into allInfo
                                 orderby allInfo.OperNum
                                 select allInfo;

            dttemp.Clear();
            normTotalbyTheJob.Clear();
            normTotalbyTheTime.Clear();
            normTotalbyTheTime.ValuatePrTimeSum = 0;
            normTotalbyTheJob.ValuatePrTimeSum = 0;

            foreach (var item in tempResult)
            {

                DataRow rowdttemp = dttemp.NewRow();
                rowdttemp[0] = item.ID;
                rowdttemp[1] = item.InfDetID;
                rowdttemp[2] = item.OperNum;
                rowdttemp[3] = item.DepRegion;
                rowdttemp[8] = item.WorkerRate;
                rowdttemp[6] = item.TariffNetNum;
                rowdttemp[7] = item.KindPay;
                rowdttemp[4] = item.ProfCode;
                rowdttemp[5] = item.NameKindWork;
                rowdttemp[9] = (timeInHours) ? (Decimal)item.ItemCTN : (Decimal)Math.Round((item.ItemCTN * 60), TrudoyomkostSettings.RoundNum);
                rowdttemp[10] = (timeInHours) ? (Decimal)item.PreparTimeCTN : (Decimal)Math.Round(item.PreparTimeCTN * 60, TrudoyomkostSettings.RoundNum);
                rowdttemp[11] = (timeInHours) ? (Decimal)item.ItemPayNorm : (Decimal)Math.Round(item.ItemPayNorm * 60, TrudoyomkostSettings.RoundNum);
                rowdttemp[12] = (timeInHours) ? (Decimal)item.PreparTimePayNorm : (Decimal)Math.Round((item.PreparTimePayNorm * 60), TrudoyomkostSettings.RoundNum);
                rowdttemp[13] = item.Valuation;
                rowdttemp[14] = item.ValPreparTime;
                rowdttemp[15] = Math.Round(item.CoeffCTN, TrudoyomkostSettings.RoundNum);
                rowdttemp[16] = item.DocNum;
                rowdttemp[17] = item.Date;
                rowdttemp[18] = item.TaskNumber;
                dttemp.Rows.Add(rowdttemp);
                if (item.ProfCode != 2322 && item.ProfCode != 2351)
                {
                    if (item.KindPay.Equals("С") || item.KindPay.Equals("C"))
                    {
                        normTotalbyTheJob.ItemCTNSum += item.ItemCTN;
                        normTotalbyTheJob.PrTimeCTNSum += item.PreparTimeCTN;
                        normTotalbyTheJob.ItemPaySum += item.ItemPayNorm;
                        normTotalbyTheJob.ValuateSum += item.Valuation;
                        normTotalbyTheJob.PrTimePaySum += item.PreparTimePayNorm;
                        normTotalbyTheJob.ValuatePrTimeSum += item.ValPreparTime;

                    }
                    else
                    {
                        normTotalbyTheTime.ItemCTNSum += item.ItemCTN;
                        normTotalbyTheTime.PrTimeCTNSum += item.PreparTimeCTN;
                        normTotalbyTheTime.ItemPaySum += item.ItemPayNorm;
                        normTotalbyTheTime.ValuateSum += item.Valuation;
                        normTotalbyTheTime.PrTimePaySum += item.PreparTimePayNorm;
                        normTotalbyTheTime.ValuatePrTimeSum += item.ValPreparTime;
                    }
                }

            }
            normTotalbyTheJob.ReCalcInMinutes(timeInHours);
            normTotalbyTheTime.ReCalcInMinutes(timeInHours);
            normTotalbyTheJob.SetCoeff();
            normTotalbyTheTime.SetCoeff();
        }