Is there a way to bulk print invoices instead of going to each order, opening up each PDF and printing it out?
Also, is there an easy way for printing Shipping Labels?
Any help is appreciated!
public ActionResult PrintInvoices(string selectedIds)
{
int[] orderIds = null;
if (selectedIds != null)
{
var ids = selectedIds
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => Convert.ToInt32(x))
.ToArray();
orderIds = ids;
}
if (orderIds != null && !String.IsNullOrEmpty(selectedIds))
{
var invoicesStream = new List<byte[]>();
foreach (var orderId in orderIds.AsParallel().WithDegreeOfParallelism(4))
{
invoicesStream.Add(GetInvoice(orderId));
}
byte[] invoicesReady = MergeInvoices(invoicesStream);
foreach (var orderId in orderIds.AsParallel().WithDegreeOfParallelism(4))
{
var order = _orderService.GetOrderById(orderId);
order.OrderStatus = OrderStatus.Processing;
_orderService.UpdateOrder(order);
}
return File(invoicesReady, "application/pdf", "Invoices.pdf");
}
else
{
///TODO define in resources
ErrorNotification("Orders not selected for printing", true);
return RedirectToAction("List", "Order", new { area = "Admin" });
}
}
private byte[] MergeInvoices(List<byte[]> invoiceBytes)
{
PdfMerge merge = new PdfMerge();
foreach (var content in invoiceBytes)
{
merge.AddDocument(content);
}
MemoryStream stream = new MemoryStream();
merge.Merge(stream);
var result = stream.ToArray();
stream.Close();
return result;
}
private byte[] GetInvoice(int orderId)
{
here is your invoice generation code
}
$('#printInvoices').click(function (e) {
e.preventDefault();
//redirect to required URL
setLocation('@(Url.Action("PrintInvoices", "Invoicing", new { area = ""}))?selectedIds=' + selectedIds.join(","));
return false;
});