Пример #1
0
        public HttpResponseMessage Report()
        {
            try
            {
                string   tempPath    = ConfigurationManager.AppSettings["TempPath"];
                long     ticks       = DateTime.Now.Ticks;
                string   fileName    = $"SalesReport-{ticks}.xls";
                string   fullName    = $"{tempPath}\\" + fileName;
                List <M> allAsync    = service.GetAll();
                string   headerValue = "Sale Report \n Printed " + DateTime.Now.ToString("dd-MM-yyyy");
                //List<SaleReportModel> reportdata = allAsync.Select(x => new SaleReportModel (x)).ToList();
                List <SaleReportModel> reportdata = allAsync.Select(x => new SaleReportModel(x)).ToList();
                SaleReportModel        item       = new SaleReportModel
                {
                    Memo         = "Total",
                    BarcodePrice = reportdata.Sum(x => x.BarcodePrice),
                    Discount     = reportdata.Sum(x => x.Discount),
                    Total        = reportdata.Sum(x => x.Total),
                    CostPrice    = reportdata.Sum(x => x.CostPrice),
                    CashReceived = reportdata.Where(x => x.SaleType.Equals("Cash")).Sum(x => x.Total),
                    CardReceived = reportdata.Where(x => x.SaleType.Equals("Card")).Sum(x => x.Total),
                    InvoiceDate  = DateTime.Now
                };

                reportdata.Add(item);
                GenericReportGenerator <SaleReportModel> .WriteExcel(reportdata, headerValue, fullName);

                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                var stream = new FileStream(fullName, FileMode.Open);
                result.Content = new StreamContent(stream);
                result.Content.Headers.ContentType        = new MediaTypeHeaderValue("application/octet-stream");
                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = fileName
                };
                return(result);
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
        public HttpResponseMessage Report()
        {
            try
            {
                string tempPath = ConfigurationManager.AppSettings["TempPath"];
                long   ticks    = DateTime.Now.Ticks;
                string fileName = $"PurchasesReport-{ticks}.xls";
                string fullName = $"{tempPath}\\" + fileName;
                List <PurchaseViewModel> allAsync = service.GetAll();
                string headerValue = "Purchase Report \n Printed " + DateTime.Now.ToString("dd-MM-yyyy");
                List <PurchaseReportModel> reportdata = allAsync.Select(x => new PurchaseReportModel {
                    Created = x.Created, Supplier = x.Supplier.Name, Memo = x.Memo, Total = x.Total, Modified = x.Modified
                }).ToList();
                PurchaseReportModel item = new PurchaseReportModel
                {
                    Supplier = "Total",
                    Total    = reportdata.Sum(x => x.Total),
                    Modified = DateTime.Now,
                    Created  = DateTime.Now
                };

                reportdata.Add(item);
                GenericReportGenerator <PurchaseReportModel> .WriteExcel(reportdata, headerValue, fullName);

                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                var stream = new FileStream(fullName, FileMode.Open);
                result.Content = new StreamContent(stream);
                result.Content.Headers.ContentType        = new MediaTypeHeaderValue("application/octet-stream");
                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = fileName
                };
                return(result);
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }