public JsonResult GeneratePreBill(BillRequest billRequest)
        {
            bool isEnglishBilling;
            var  preBill = _billRepo.GeneratePreBill(billRequest, out isEnglishBilling);
            //Report setup
            var details = preBill.BillDetails.Select(d => new
            {
                BillDetailType = Convert.ToInt32(d.BillDetailType),
                FixedAmount    = d.FixedAmount,
                UnitRate       = d.UnitRate,
                Quantity       = d.Quantity,
                Subtotal       = d.Subtotal,
                Description    = d.Description,
                TaxesAmount    = d.TaxesAmount
            }).ToList();

            string     basePath    = _hostingEnvironment.ContentRootPath;
            string     fullPath    = basePath + @"/Reports/Bill.rdlc";
            FileStream inputStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
            ReportDataSourceCollection dataSources = new ReportDataSourceCollection();

            dataSources.Add(new ReportDataSource {
                Name = "BillDataSet", Value = details
            });

            Syncfusion.ReportWriter.ReportWriter writer = new Syncfusion.ReportWriter.ReportWriter(inputStream, dataSources);
            //Setting up the parameters
            List <ReportParameter> parameters = new List <ReportParameter>();
            //IsEnglish
            ReportParameter isEnglishParam = new ReportParameter();

            isEnglishParam.Name   = "IsEnglish";
            isEnglishParam.Values = new List <string>()
            {
                isEnglishBilling ? "1" : "0"
            };
            //IsBilled
            ReportParameter isBilledParam = new ReportParameter();

            isBilledParam.Name   = "IsBilled";
            isBilledParam.Values = new List <string>()
            {
                "0"
            };
            //ClientName
            ReportParameter clientNameParam = new ReportParameter();

            clientNameParam.Name   = "ClientName";
            clientNameParam.Values = new List <string>()
            {
                preBill.BillName
            };
            //BillingPeriod

            string culture       = isEnglishBilling ? "en" : "es";
            string billingPeriod = $"{CultureInfo.GetCultureInfoByIetfLanguageTag(culture).DateTimeFormat.GetMonthName(DateTime.UtcNow.AddHours(-6).Month)} - {preBill.BillYear}";

            ReportParameter billingPeriodParam = new ReportParameter();

            billingPeriodParam.Name   = "BillingPeriod";
            billingPeriodParam.Values = new List <string>()
            {
                billingPeriod
            };
            //Subtotal
            ReportParameter subTotalExpenseParam = new ReportParameter();

            subTotalExpenseParam.Name   = "Subtotal";
            subTotalExpenseParam.Values = new List <string>()
            {
                preBill.BillSubtotal.ToString()
            };
            //Discount
            ReportParameter discountParam = new ReportParameter();

            discountParam.Name   = "Discount";
            discountParam.Values = new List <string>()
            {
                preBill.BillDiscount.ToString()
            };
            //Taxes
            ReportParameter taxesParam = new ReportParameter();

            taxesParam.Name   = "Taxes";
            taxesParam.Values = new List <string>()
            {
                preBill.Taxes.ToString()
            };
            //TotalExpense
            ReportParameter totalParam = new ReportParameter();

            totalParam.Name   = "Total";
            totalParam.Values = new List <string>()
            {
                preBill.Total.ToString()
            };

            //TotalExpense
            ReportParameter billDateParam = new ReportParameter();

            billDateParam.Name   = "BillDate";
            billDateParam.Values = new List <string>()
            {
                preBill.BillDate.ToString("dd/MM/yyyy")
            };

            parameters.Add(isEnglishParam);
            parameters.Add(isBilledParam);
            parameters.Add(clientNameParam);
            parameters.Add(billingPeriodParam);
            parameters.Add(subTotalExpenseParam);
            parameters.Add(discountParam);
            parameters.Add(taxesParam);
            parameters.Add(totalParam);
            parameters.Add(billDateParam);
            writer.SetParameters(parameters);
            writer.ReportProcessingMode = ProcessingMode.Local;
            MemoryStream memoryStream = new MemoryStream();

            writer.Save(memoryStream, WriterFormat.PDF);
            memoryStream.Position = 0;
            FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/pdf");

            fileStreamResult.FileDownloadName = $"FacturaCliente{preBill.BillName}deMes{preBill.BillMonth}YAño{preBill.BillYear}.pdf";
            return(Json(new { result = memoryStream.ConvertToBase64() }));
        }
Ejemplo n.º 2
0
        public IActionResult Report(NewActivityReport newActivityReport)
        {
            Func <Activity, decimal, decimal> GetFeeByActivity = (activity, ratePerHour) =>
            {
                switch (activity.ActivityType)
                {
                case ActivityTypeEnum.Hourly:
                    return(activity.Subtotal);

                case ActivityTypeEnum.Item:
                    return(activity.Subtotal);

                case ActivityTypeEnum.NoBillable:
                    return((decimal)(0.00));

                case ActivityTypeEnum.Package:
                    if (activity.Package.IsFinished)
                    {
                        return(ratePerHour * activity.HoursWorked);
                    }
                    else
                    {
                        return((decimal)(0.00));
                    }

                case ActivityTypeEnum.Retainer:
                    //Under working the hours
                    if (activity.BillableQuantity == 0)
                    {
                        return(ratePerHour * activity.HoursWorked);
                    }
                    //Is normal billed as excedent of the agreed hours
                    else if (activity.BillableQuantity == activity.HoursWorked)
                    {
                        return(activity.Subtotal);
                    }
                    else
                    {
                        return(activity.Subtotal + ((activity.HoursWorked - activity.BillableQuantity) * activity.BillableRate));
                    }

                default:
                    return((decimal)(0.00));
                }
            };
            var list = new List <Activity>();

            list = _activityRepo.Activities.Include(a => a.Service).ThenInclude(s => s.Category)
                   .Include(a => a.Client)
                   .Include(a => a.Package)
                   .Include(a => a.BillableRetainer)
                   .Where(a => a.RealizationDate >= newActivityReport.InitialDate && a.RealizationDate <= newActivityReport.FinalDate &&
                          (newActivityReport.UserId == null || a.CreatorId == newActivityReport.UserId) &&
                          (newActivityReport.ActivityType == null || a.ActivityType == newActivityReport.ActivityType)
                          ).ToList();

            //To get all the packages amount involved
            var involvedPackagesRates = list.Where(a => a.ActivityType == ActivityTypeEnum.Package &&
                                                   a.Package.IsFinished).Select(a => a.Package)
                                        .Distinct()
                                        .Select(package =>
                                                new { package.Id, rate = (package.Amount / (_activityRepo.Activities.Where(act => act.PackageId == package.Id).Sum(act => act.HoursWorked))) })
                                        .ToList();

            var activities = list.Select(a => new
            {
                activityClient       = a.Client.Name,
                activityService      = a.Service.Name,
                activityCategory     = a.Service.Category.Name,
                activityHoursWorked  = Math.Round(a.HoursWorked, 2),
                activityAssociatedTo = a.ActivityType == ActivityTypeEnum.Hourly ? "Horario" : a.ActivityType == ActivityTypeEnum.Item ?
                                       $"Ítem" : a.ActivityType == ActivityTypeEnum.Package ? $"Paquete - {a.Package?.Name}" : a.ActivityType == ActivityTypeEnum.Retainer ?
                                       $"Retainer - {a?.BillableRetainer?.Name}" : a.ActivityType == ActivityTypeEnum.NoBillable ?"No cobrable" :"",
                activityDate     = a.RealizationDate,
                activityTotalFee = GetFeeByActivity(a, a.ActivityType == ActivityTypeEnum.Package ?
                                                    (involvedPackagesRates.Where(ip => ip.Id == a.PackageId).FirstOrDefault()?.rate ?? (decimal)0.00)
                    : a.ActivityType == ActivityTypeEnum.Retainer ?
                                                    a.BillableRetainer.AgreedFee / a.BillableRetainer.AgreedHours
                    : (decimal)0.00)
            }).ToList();

            string     basePath    = _hostingEnvironment.ContentRootPath;
            string     fullPath    = basePath + @"/Reports/Activities.rdlc";
            FileStream inputStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
            ReportDataSourceCollection dataSources = new ReportDataSourceCollection();

            dataSources.Add(new ReportDataSource {
                Name = "ActivitiesDataSet", Value = activities
            });

            Syncfusion.ReportWriter.ReportWriter writer = new Syncfusion.ReportWriter.ReportWriter(inputStream, dataSources);
            //writer.DataSources = dataSources;
            writer.ReportProcessingMode = ProcessingMode.Local;
            //Setting up the parameters
            List <ReportParameter> parameters = new List <ReportParameter>();
            //StartDate
            ReportParameter startDateParam = new ReportParameter();

            startDateParam.Name   = "StartDate";
            startDateParam.Values = new List <string>()
            {
                newActivityReport.InitialDate.ToString("dd/MM/yyyy")
            };
            //EndDate
            ReportParameter endDateParam = new ReportParameter();

            endDateParam.Name   = "EndDate";
            endDateParam.Values = new List <string>()
            {
                newActivityReport.FinalDate.ToString("dd/MM/yyyy")
            };
            //AttorneyName
            ReportParameter attorneyParam = new ReportParameter();

            attorneyParam.Name   = "AttorneyName";
            attorneyParam.Values = new List <string>()
            {
                newActivityReport.UserId == null? "Todos"
                : _attorneyRepo.Attorneys.Where(a => a.UserId == ((int)newActivityReport.UserId)).Select(a => a.Name).First()
            };
            //ActivityType
            ReportParameter activityTypeParam = new ReportParameter();

            activityTypeParam.Name   = "ActivityType";
            activityTypeParam.Values = new List <string>()
            {
                newActivityReport.ActivityType == null ? "0" : ((int)newActivityReport.ActivityType).ToString()
            };
            //TotalHours
            ReportParameter totalHoursParam = new ReportParameter();

            totalHoursParam.Name   = "TotalHours";
            totalHoursParam.Values = new List <string>()
            {
                activities.Sum(a => a.activityHoursWorked).ToString()
            };
            //Total Amount
            ReportParameter totalAmountParam = new ReportParameter();

            totalAmountParam.Name   = "TotalAmount";
            totalAmountParam.Values = new List <string>()
            {
                activities.Sum(a => a.activityTotalFee).ToString()
            };


            parameters.Add(startDateParam);
            parameters.Add(endDateParam);
            parameters.Add(attorneyParam);
            parameters.Add(activityTypeParam);
            parameters.Add(totalHoursParam);
            parameters.Add(totalAmountParam);
            writer.SetParameters(parameters);
            MemoryStream memoryStream = new MemoryStream();

            writer.Save(memoryStream, WriterFormat.PDF);
            memoryStream.Position = 0;
            FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/pdf");

            fileStreamResult.FileDownloadName = $"ReporteDeActividadesDel{newActivityReport.InitialDate.ToString("ddMMyyyy")}Al{newActivityReport.FinalDate.ToString("ddMMyyyy")}.pdf";
            return(fileStreamResult);
        }
        public IActionResult RenderBill(int id)
        {
            var bill    = _billRepo.BillHeaders.Where(b => b.Id == id).Include(b => b.BillDetails).FirstOrDefault();
            var details = bill.BillDetails.Select(d => new
            {
                BillDetailType = Convert.ToInt32(d.BillDetailType),
                d.FixedAmount,
                d.UnitRate,
                d.Quantity,
                d.Subtotal,
                d.Description,
                d.TaxesAmount
            }).ToList();
            string     basePath    = _hostingEnvironment.ContentRootPath;
            string     fullPath    = basePath + @"/Reports/Bill.rdlc";
            FileStream inputStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
            ReportDataSourceCollection dataSources = new ReportDataSourceCollection();

            dataSources.Add(new ReportDataSource {
                Name = "BillDataSet", Value = details
            });

            Syncfusion.ReportWriter.ReportWriter writer = new Syncfusion.ReportWriter.ReportWriter(inputStream, dataSources);
            //writer.DataSources = dataSources;
            //Setting up the parameters
            List <ReportParameter> parameters = new List <ReportParameter>();
            //IsEnglish
            bool            isBillingInEnglish = _clientRepo.Clients.Where(c => c.Id == bill.ClientId).Select(c => c.BillingInEnglish).First();
            ReportParameter isEnglishParam     = new ReportParameter();

            isEnglishParam.Name   = "IsEnglish";
            isEnglishParam.Values = new List <string>()
            {
                isBillingInEnglish ? "1" : "0"
            };
            //IsBilled
            ReportParameter isBilledParam = new ReportParameter();

            isBilledParam.Name   = "IsBilled";
            isBilledParam.Values = new List <string>()
            {
                "1"
            };
            //ClientName
            ReportParameter clientNameParam = new ReportParameter();

            clientNameParam.Name   = "ClientName";
            clientNameParam.Values = new List <string>()
            {
                bill.BillName
            };
            //BillingPeriod

            string culture       = isBillingInEnglish ? "en" : "es";
            string billingPeriod = $"{CultureInfo.GetCultureInfoByIetfLanguageTag(culture).DateTimeFormat.GetMonthName(DateTime.UtcNow.AddHours(-6).Month)} - {bill.BillYear}";

            ReportParameter billingPeriodParam = new ReportParameter();

            billingPeriodParam.Name   = "BillingPeriod";
            billingPeriodParam.Values = new List <string>()
            {
                billingPeriod
            };
            //Subtotal
            ReportParameter subTotalExpenseParam = new ReportParameter();

            subTotalExpenseParam.Name   = "Subtotal";
            subTotalExpenseParam.Values = new List <string>()
            {
                bill.BillSubtotal.ToString()
            };
            //Discount
            ReportParameter discountParam = new ReportParameter();

            discountParam.Name   = "Discount";
            discountParam.Values = new List <string>()
            {
                bill.BillDiscount.ToString()
            };
            //Taxes
            ReportParameter taxesParam = new ReportParameter();

            taxesParam.Name   = "Taxes";
            taxesParam.Values = new List <string>()
            {
                bill.Taxes.ToString()
            };
            //TotalExpense
            ReportParameter totalParam = new ReportParameter();

            totalParam.Name   = "Total";
            totalParam.Values = new List <string>()
            {
                bill.Total.ToString()
            };
            //TotalExpense
            ReportParameter billDateParam = new ReportParameter();

            billDateParam.Name   = "BillDate";
            billDateParam.Values = new List <string>()
            {
                bill.BillDate.ToString()
            };

            parameters.Add(isEnglishParam);
            parameters.Add(isBilledParam);
            parameters.Add(clientNameParam);
            parameters.Add(billingPeriodParam);
            parameters.Add(subTotalExpenseParam);
            parameters.Add(discountParam);
            parameters.Add(taxesParam);
            parameters.Add(totalParam);
            parameters.Add(billDateParam);
            writer.SetParameters(parameters);
            writer.ReportProcessingMode = ProcessingMode.Local;
            MemoryStream memoryStream = new MemoryStream();

            writer.Save(memoryStream, WriterFormat.PDF);
            memoryStream.Position = 0;
            FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/pdf");

            fileStreamResult.FileDownloadName = $"FacturaCliente{bill.BillName}deMes{bill.BillMonth}YAño{bill.BillYear}.pdf";
            return(fileStreamResult);
        }