public static Creditnote Create(Invoice Invoice, bool Simulate) { List<CreditnoteLine> lines = new List<CreditnoteLine> (); // Customer customer = Customer.Load (Invoice.CustomerId); foreach (InvoiceLine invoiceline1 in Invoice.Lines) { try { Item item = Item.Load (invoiceline1.ItemId); Bid bid = Bid.Load (item.CurrentBidId); List<Invoice> invoices = Invoice.List (item); invoices.Sort (delegate (Invoice i1, Invoice i2) { return i1.No.CompareTo (i2.No);}); invoices.Reverse (); if (bid.CustomerId == Invoice.CustomerId) { if (item.Invoiced) { bool ok = true; foreach (Invoice invoice in invoices) { foreach (InvoiceLine invoiceline2 in invoice.Lines) { if (invoiceline2.ItemId == invoiceline1.ItemId) { if (invoice.No > Invoice.No) { ok = false; } } } } if (ok) { lines.Add (new CreditnoteLine (invoiceline1)); } } } } catch (Exception exception) { // LOG: LogDebug.ExceptionUnknown SorentoLib.Services.Logging.LogDebug (string.Format (SorentoLib.Strings.LogDebug.ExceptionUnknown, "DIDIUS.CREDITNOTE", exception.Message)); } } return Create (Customer.Load (Invoice.CustomerId), lines, Simulate); }