protected void ButtonDestroyOrderAndBack_Click(object sender, EventArgs e) { bool Success = false; if (LabelCurrentOrderId.Text != Guid.Empty.ToString()) { ModelTMSContainer _ControlObjectContext = new ModelTMSContainer(Session["CustomerConnectString"].ToString(), Session); // start transaction using (TransactionScope TS = new TransactionScope()) { try { // roll back order // correct invoice if there if (LabelCurrentInvoiceId.Text != "") { Invoice CorrInvoice = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.InvoiceSet", "Id", Guid.Parse(LabelCurrentInvoiceId.Text))) as Invoice; // unprocess CorrInvoice.UnprocessInvoice(_ControlObjectContext, CorrInvoice.GroupCode, Common.CurrentClientDateTime(Session)); } // destroy rentledger RentLedger TempRentLedger = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RentLedgerSet", "Id", Guid.Parse(LabelCurrentOrderId.Text))) as RentLedger; TempRentLedger.DestroyRentalItemActivities(_ControlObjectContext); _ControlObjectContext.DeleteObject(TempRentLedger); // and save to persistent storage _ControlObjectContext.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave); // commit TS.Complete(); Success = true; } catch (Exception ex) { // rollback transaction TS.Dispose(); // inform user Common.InformUserOnTransactionFail(ex, Page); } } } if (Success) { // when success revert CurrentPageNr--; EnableCorrectScreenElements(); } }
protected void ButtonPrintAndProcess_Click(object sender, EventArgs e) { bool Success = false; ModelTMSContainer _ControlObjectContext = new ModelTMSContainer(Session["CustomerConnectString"].ToString(), Session); // start transaction using (TransactionScope TS = new TransactionScope()) { try { // expand rented items if required WebUserControlRentMaterials1.ExpandOrderLines(); // by default we have no invoice LabelCurrentInvoiceId.Text = ""; // create rentledger entry and link up the rentalitemactivities RentLedger TempRentLedger = new RentLedger(); _ControlObjectContext.AddToRentLedgerSet(TempRentLedger); if (LabelGroupId.Text == "") { LabelGroupId.Text = TempRentLedger.Id.ToString(); } TempRentLedger.Relation = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationSet", "Id", Guid.Parse(DropDownListCustomers.SelectedValue))) as Relation; TempRentLedger.Location = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.LocationSet", "Id", Guid.Parse(DropDownListLocations.SelectedValue))) as Location; TempRentLedger.Description = TextBox_Description.Text; WebUserControlRentMaterials1.AddRentMaterialsToRentLedgerAndRentalItemActivitySet(_ControlObjectContext, TempRentLedger); // create invoice if required and link this up to the rentledger if ((LabelRentMode.Text == "Cash") || (CheckBoxProFormaInvoice.Checked)) { Invoice TempInvoice = new Invoice(); // copy the group code if this is a correction from an old invoice Guid OldGuid = Guid.Parse(LabelCurrentOrderId.Text); if (OldGuid != Guid.Empty) { try { Invoice OldInvoice = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.InvoiceSet", "Id", OldGuid)) as Invoice; TempInvoice.GroupCode = OldInvoice.GroupCode; } catch { }; } // reset the invoice LabelCurrentOrderId.Text = Guid.Empty.ToString(); // continue processing TempInvoice.Relation = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationSet", "Id", Guid.Parse(DropDownListCustomers.SelectedValue))) as Relation; TempInvoice.Location = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.LocationSet", "Id", Guid.Parse(DropDownListLocations.SelectedValue))) as Location; TempInvoice.Description = TextBox_Description.Text; TempInvoice.InvoiceType = "Sell"; TempInvoice.InvoiceSubType = "Rent"; TempInvoice.BookingDateTime = Common.CurrentClientDateTime(Session); if (LabelRentMode.Text == "Cash") { TempInvoice.Ledger = TempInvoice.Location.CashLedger; } else { TempInvoice.Ledger = TempInvoice.Location.BankLedger; } TempInvoice.GroupCode = new Guid(LabelGroupId.Text); TempInvoice.DiscountPercentage = Convert.ToDouble(TextBoxInvoiceDiscount.Text); if (LabelRentMode.Text != "Cash") { TempInvoice.Ledger = TempInvoice.Location.BankLedger; } TempInvoice.InvoiceNote = TextBoxInvoiceNote.Text; _ControlObjectContext.AddToInvoiceSet(TempInvoice); TempRentLedger.Invoice.Add(TempInvoice); // add the order to the invoice TempRentLedger.AddRentMaterialsToInvoice(_ControlObjectContext, TempInvoice); // 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 = TempInvoice; iline.LineNumber = iline.Invoice.InvoiceLine.Count; iline.LedgerBookingCode = iline.Invoice.Location.DefaultBailPriceBookingCode; } // all invoice lines to the same ledger TempInvoice.AllInvoiceLinesToSameLedger(TempInvoice.Ledger); TempInvoice.RecalcTotals(); // process order & invoice if this is a cash invoice if (LabelRentMode.Text == "Cash") { TempInvoice.ProcessInvoice(_ControlObjectContext, TempInvoice.GroupCode, true, Common.CurrentClientDateTime(Session)); } else { TempInvoice.GenerateInvoiceNumber(_ControlObjectContext); } LabelCurrentInvoiceId.Text = TempInvoice.Id.ToString(); } TempRentLedger.CheckRentLedgerBeforeSave(_ControlObjectContext); // and save to persistent storage _ControlObjectContext.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave); // and save this invoice LabelCurrentOrderId.Text = TempRentLedger.Id.ToString(); // commit the transaciton TS.Complete(); Success = true; } catch (Exception ex) // commit or procedure failed somewhere { // rollback transaction TS.Dispose(); // inform user Common.InformUserOnTransactionFail(ex, Page); } } if (Success) { // when success advance panel CurrentPageNr = CurrentPageNr + 1; EnableCorrectScreenElements(); } }
public void AddRentMaterialsToRentLedgerAndRentalItemActivitySet(ModelTMSContainer ControlObjectContext, RentLedger rl) { LoadOrderLines(); // add each rental item line foreach (RentalItemActivityListItem ria in OrderLines) { RentalItemActivity riaactivity = new RentalItemActivity(); // hookup items riaactivity.RentalItem = ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RentalItemSet", "Id", ria.RentalItemId)) as RentalItem; // create rentalitemactivity line riaactivity.RentStartDateTime = StartRentDate; riaactivity.RentEndStartDateTime = EndRentDate; riaactivity.Description = riaactivity.RentalItem.Description; riaactivity.InvoiceStatus = "Open"; riaactivity.CalculatedRentPrice = ria.RentPrice; riaactivity.BaseRentPrice = ria.RentPrice; riaactivity.DiscountPercentage = ria.DiscountPercentage; riaactivity.VATRentPrice = ria.Vat; riaactivity.TotalRentPrice = ria.TotalRentPrice; riaactivity.IsTreatedAsAdvancePayment = ria.TreatAsAdvancePayment; riaactivity.GenerateDescription(); riaactivity.UpdateAdvancePaymentStatus(ControlObjectContext, true, ria.TreatAsAdvancePayment); rl.RentalItemActivity.Add(riaactivity); } }