protected void ButtonNew_Click(object sender, EventArgs e) { // create new empty invoice Invoice NewInvoice = new Invoice(); NewInvoice.GenerateInvoiceNumber(ControlObjectContext); NewInvoice.Description = "Factuur " + NewInvoice.InvoiceNumber.ToString() + " dd " + NewInvoice.BookingDateTime.ToString(); NewInvoice.InvoiceType = LabelInvoiceType.Text; NewInvoice.InvoiceSubType = LabelSubInvoiceType.Text; NewInvoice.Ledger = ControlObjectContext.LedgerSet.First(); ControlObjectContext.AddToInvoiceSet(NewInvoice); // save ControlObjectContext.SaveChanges(); // show ShowSelectedInvoice(NewInvoice.Id.ToString()); }
public void CreateNewInvoice() { bool Success = false; // start transaction using (TransactionScope TS = new TransactionScope()) { try { Invoice inv = new Invoice(); ControlObjectContext.AddToInvoiceSet(inv); foreach (GridViewRow gvr in GridViewSelectedRentOuts.Rows) { if ((gvr.Cells[0].Controls[1] as CheckBox).Checked) { String RentID = (gvr.Cells[0].Controls[1] as CheckBox).ToolTip; RentalItemActivity ria = ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RentalItemActivitySet", "Id", new System.Guid(RentID))) as RentalItemActivity; ria.AddRentToInvoice(ControlObjectContext, inv); inv.Location = ria.RentLedger.Location; } } inv.GenerateInvoiceNumber(ControlObjectContext); inv.Relation = ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationSet", "Id", Guid.Parse(DropDownListCustomers.SelectedValue))) as Relation; inv.Description = TextBoxDescription.Text; inv.InvoiceType = "Sell"; inv.InvoiceSubType = "Rent"; inv.BookingDateTime = Common.CurrentClientDateTime(Session); inv.Ledger = inv.Location.BankLedger; try { inv.DiscountPercentage = Convert.ToDouble("0" + TextBoxInvoiceDiscount.Text); } catch {} // add bail to the invoice double Bail = Convert.ToDouble(TextBoxBail.Text); // except if this fails, this should be a valid number if (Bail != 0) { InvoiceLine iline = new InvoiceLine(); iline.Description = "Borg"; iline.OriginalPrice = -Bail; iline.AllowDiscount = false; iline.VATPercentage = 0; iline.VATPrice = 0; iline.TotalPrice = -Bail; iline.Invoice = inv; iline.LineNumber = iline.Invoice.InvoiceLine.Count; iline.LedgerBookingCode = iline.Invoice.Location.DefaultBailPriceBookingCode; } LabelGeneratedInvoiceNr.Text = inv.InvoiceNumber.ToString(); LabelGeneratedInvoiceId.Text = inv.Id.ToString(); if (LabelGroupId.Text == "") { LabelGroupId.Text = inv.GroupCode.ToString(); } inv.GroupCode = new Guid(LabelGroupId.Text); inv.InvoiceNote = TextBoxInvoiceNote.Text; // all invoice lines to the same ledger inv.AllInvoiceLinesToSameLedger(inv.Ledger); inv.RecalcTotals(); // save the data ControlObjectContext.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave); // commit the transaciton TS.Complete(); Success = true; } catch (Exception ex) { // rollback transaction TS.Dispose(); // inform user Common.InformUserOnTransactionFail(ex, Page); } } if (!Success) { CurrentPageNr--; EnableCurrentPageElements(); } }