Ejemplo n.º 1
0
        public HttpResponseMessage CreatePDFforInvoice(string invoicekey)
        {
            var             viewmodel = new InvoiceViewModel();
            DeliveryOrderDL orderDl   = new DeliveryOrderDL();
            InvoiceDL       dl        = new InvoiceDL();

            string orderkey = dl.getOrderkeybyInvoicekey(invoicekey);

            if (!string.IsNullOrEmpty(orderkey))
            {
                var orderBO = orderDl.GetDeliveryOrder(orderkey);
                viewmodel.Order         = orderBO;
                viewmodel.InvoiceHeader = dl.GetInvoicebyinvoiceKey(invoicekey);
                viewmodel.InvoiceDetail = dl.GetInvoiceDetail(invoicekey);

                var orderdetailBO = orderDl.GetOrderDetailsByKey(orderkey);

                if (orderdetailBO != null && orderdetailBO.Count > 0)
                {
                    var orderdtlList = new List <ThinOrderDetailViewModel>();
                    orderdetailBO.ForEach(d =>
                    {
                        orderdtlList.Add(new ThinOrderDetailViewModel()
                        {
                            Chassis        = d.Chassis,
                            ContainerNo    = d.ContainerNo,
                            OrderDetailKey = d.OrderDetailKey,
                            //InvoiceHeader = d.OrderDetailKey != null ? dl.GetInvoicebyinvoiceKey(Convert.ToString(invoicekey)) : null,
                            //InvoiceDetail = d.OrderDetailKey != null ? dl.GetInvoiceDetail(invoicekey) : null,
                        });
                    });
                    viewmodel.OrderDtl = orderdtlList;
                }
            }
            //if (orderdetailBO != null && orderdetailBO.Count > 0)
            //{
            //    var orderdtlList = new List<ThinOrderDetailViewModel>();
            //    orderdetailBO.ForEach(d =>
            //    {
            //        orderdtlList.Add(new ThinOrderDetailViewModel()
            //        {
            //            Chassis = d.Chassis,
            //            ContainerNo = d.ContainerNo,
            //            OrderDetailKey = d.OrderDetailKey,
            //            InvoiceHeader = d.OrderDetailKey != null ? dl.GetInvoicebyOrderDetailKey(Convert.ToString(d.OrderDetailKey)) : null,
            //            InvoiceDetail = d.OrderDetailKey != null ? dl.GetInvoiceDetail(Convert.ToString(d.OrderDetailKey)) : null,

            //        });
            //    });
            //    viewmodel.OrderDtl = orderdtlList;
            //}
            //else
            //{
            //    return Request.CreateResponse(HttpStatusCode.InternalServerError, "Order not found", Configuration.Formatters.JsonFormatter);
            //}

            if (viewmodel == null)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Order not found", Configuration.Formatters.JsonFormatter));
            }

            var stringified = new InvoiceViewController().RenderRazorViewToString("~/Views/InvoiceView/Invoice.cshtml", viewmodel);
            var pdf         = IronPdf.HtmlToPdf.StaticRenderHtmlAsPdf(stringified);
            var basepath    = HttpContext.Current.Server.MapPath("~/App_Data/Files/");
            var doccreated  = pdf.SaveAs(Path.Combine(basepath, $"{viewmodel.Order.OrderNo}.pdf"));

            if (doccreated.PageCount > 0)
            {
                return(Request.CreateResponse(HttpStatusCode.OK, "Invoice generated", Configuration.Formatters.JsonFormatter));
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Error occurred while creating Invoice", Configuration.Formatters.JsonFormatter));
            }
        }