protected void CreateInvoiceImageButton_Command(object sender, CommandEventArgs e) { ImageButton ib = (ImageButton)sender; int purchaseOrderId = Convert.ToInt32(ib.CommandArgument); try { PurchaseOrder foundOrder = PurchaseOrder.GetPurchaseOrder(purchaseOrderId); AllowedStore permission = AllowedStore.GetAllowedStoreByCustomerSupplier(foundOrder.customer_id, homeCompany.company_id); Invoice newInvoice = foundOrder.CreateInvoice(); newInvoice.customer_id = foundOrder.customer_id; newInvoice.supplier_id = foundOrder.supplier_id; newInvoice.freight = 10; newInvoice.total = 0; newInvoice.creation_datetime = DateTime.UtcNow; newInvoice.Save(); newInvoice.Refresh(); foreach (var item in foundOrder.PurchaseOrderItemsBypurchaseorder_) { string barcode = item.barcode; double quantity = item.quantity; var foundProduct = SupplierProduct.findProductByBarcode(homeCompany.company_id, barcode); if (foundProduct != null) { InvoiceItem newItem = newInvoice.CreateInvoiceItem(); newItem.barcode = foundProduct.barcode; newItem.cost_price = foundProduct.CalculateApplicableCost(quantity, permission.is_member); newItem.description = foundProduct.description; newItem.product_code = foundProduct.product_code; newItem.quantity = quantity; newItem.is_GST = foundProduct.is_GST; newItem.RRP = foundProduct.RRP; newItem.Save(); } } newInvoice.total = newInvoice.InvoiceItemsByinvoice_.Sum(i => i.subTotal) + newInvoice.freight; newInvoice.tax = newInvoice.InvoiceItemsByinvoice_.Sum(i => i.taxAmount); if (!newInvoice.customer_.has_POSSystem) { newInvoice.is_downloaded = true; newInvoice.downloaded_datetime = newInvoice.creation_datetime; } else { newInvoice.is_downloaded = false; } newInvoice.Save(); newInvoice.Refresh(); EmailHelper.SendInvoice(newInvoice); PopulateOrderLists(); } catch { throw; } }