protected void ButtonPrintAndProcess_Click(object sender, EventArgs e) { bool Success = false; // start transaction using (TransactionScope TS = new TransactionScope()) { try { // process order // create order Order TempOrder = new Order(); TempOrder.Relation = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationSet", "Id", Guid.Parse(DropDownListCustomers.SelectedValue))) as Relation; TempOrder.Location = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.LocationSet", "Id", Guid.Parse(DropDownListLocations.SelectedValue))) as Location; TempOrder.Description = TextBox_Description.Text; TempOrder.OrderType = LabelInvoiceType.Text; TempOrder.BookingDateTime = Common.CurrentClientDateTime(Session); TempOrder.YourDriverName = TextBox_YourDriverName.Text; TempOrder.YourTruckPlate = TextBox_YourTruckPlate.Text; TempOrder.FreightID = TextBoxFreightID.Text; TempOrder.LinkToSingleFreight(null); if (LabelFreightGuid.Text != "") { Freight frg = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.FreightSet", "Id", Guid.Parse(LabelFreightGuid.Text))) as Freight; TempOrder.LinkToSingleFreight(frg); } // assign the order number if not known if (LabelOrderNr.Text == "") { TempOrder.AssignOrderNumber(_ControlObjectContext); } else { TempOrder.OrderNumber = Convert.ToInt64(LabelOrderNr.Text); } if (DropDownListProjects.SelectedValue != "") { TempOrder.RelationProject = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationProjectSet", "Id", Guid.Parse(DropDownListProjects.SelectedValue))) as RelationProject; } if (DropDownListCustomerFreights.SelectedValue != "") { TempOrder.Freight.Clear(); TempOrder.Freight.Add(_ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.FreightSet", "Id", Guid.Parse(DropDownListCustomerFreights.SelectedValue))) as Freight); } if (DropDownListCustomerLocations.SelectedValue != "") { TempOrder.RelationLocation = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationLocationSet", "Id", Guid.Parse(DropDownListCustomerLocations.SelectedValue))) as RelationLocation; } // if (Session["CurrentUserID"] != null) // { // TempOrder.StaffMemberPurchaser = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.StaffMemberSet", "Id", Guid.Parse(Session["CurrentUserID"].ToString()))) as StaffMember; // } TempOrder.StaffMemberPurchaser = Common.CurrentLoggedInUser(Session, _ControlObjectContext); //add the order to the controlobjectcontext _ControlObjectContext.AddToOrderSet(TempOrder); // add order lines for (int i = 0; i < WebUserControlEditOrderMaterials1.OrderLines.Count; i++) { OrderLine TempLine = ((WebUserControlEditOrderMaterials1.OrderLines[i]) as OrderLine); TempLine.Order = TempOrder; TempLine.Id = Guid.NewGuid(); //_ControlObjectContext.AddToOrderLineSet(TempLine); } TempOrder.RecalcTotals(); // add dirt if required Double AddDirtAmount; try { AddDirtAmount = System.Convert.ToDouble(TextBoxAddDirt.Text); } catch { AddDirtAmount = 0; } AddDirtAmount = (AddDirtAmount - TempOrder.TotalAmount); if (AddDirtAmount > 0) { OrderLine TempLine = new OrderLine(); TempLine.Order = TempOrder; TempLine.Material = TempOrder.Location.MaterialForDirt; TempLine.PricePerUnit = TempLine.Material.PurchasePrice; if (LabelInvoiceType.Text == "Sell") { TempLine.PricePerUnit = TempLine.Material.SalesPrice; } TempLine.Amount = AddDirtAmount * TempOrder.Location.MaterialForDirt.MaterialUnit.StockKgMultiplier; TempLine.Description = "- " + TempOrder.Location.MaterialForDirt.Description; TempLine.RecalcTotals(); _ControlObjectContext.AddToOrderLineSet(TempLine); } // create invoice Invoice TempInvoice = new Invoice(); // copy the group code if this is a correction from an old invoice Guid OldGuid = Guid.Parse(LabelGeneratedInvoiceId.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 LabelGeneratedInvoiceId.Text = Guid.Empty.ToString(); // continue processing TempInvoice.Relation = TempOrder.Relation; TempInvoice.Location = TempOrder.Location; //TempInvoice.Order.Add(TempOrder); TempInvoice.Description = TempOrder.Description; TempInvoice.InvoiceType = LabelInvoiceType.Text; TempInvoice.BookingDateTime = Common.CurrentClientDateTime(Session); TempInvoice.Ledger = TempInvoice.Location.CashLedger; TempInvoice.InvoiceNote = TextBoxInvoiceNote.Text; _ControlObjectContext.AddToInvoiceSet(TempInvoice); // add the order to the invoice TempInvoice.AddOrderToInvoice(_ControlObjectContext, TempOrder); // subtract advance payments WebUserControlEditAdvancePayments1.LoadAPLines(); for (int i = 0; i < WebUserControlEditAdvancePayments1.AdvancePaymentLines.Count; i++) { Guid TempGuid; TempGuid = (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Id; // load the work or advance payment object RelationAdvancePayment CurrAP = null; RelationWork CurrWork = null; try { CurrAP = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationAdvancePaymentSet", "Id", TempGuid)) as RelationAdvancePayment; TempInvoice.AddAdvancePaymentCorrection(_ControlObjectContext, CurrAP, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Amount, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Description); } catch (Exception) { }; try { CurrWork = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationWorkSet", "Id", TempGuid)) as RelationWork; TempInvoice.AddWorkCorrection(_ControlObjectContext, CurrWork, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Amount, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Description); } catch (Exception) { }; } // process order & invoice TempInvoice.ProcessInvoice(_ControlObjectContext, TempInvoice.GroupCode, true, Common.CurrentClientDateTime(Session)); // and save to persistent storage _ControlObjectContext.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave); // and save this invoice LabelGeneratedInvoiceId.Text = TempInvoice.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 CurrentPanelLevel = CurrentPanelLevel + 1; ShowCorrectPanels(); } }
protected void ButtonPrintAndProcess_Click(object sender, EventArgs e) { ModelTMSContainer _ControlObjectContext = new ModelTMSContainer(Session["CustomerConnectString"].ToString(), Session); // start transaction using (TransactionScope TS = new TransactionScope()) { try { // create new invoice Invoice NewInvoice = new Invoice(); NewInvoice.Relation = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationSet", "Id", Guid.Parse(DropDownListCustomers.SelectedValue))) as Relation; if (DropDownListLocations.SelectedValue != "") { NewInvoice.Location = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.LocationSet", "Id", Guid.Parse(DropDownListLocations.SelectedValue))) as Location; } NewInvoice.Description = TextBoxInvoiceDescription.Text; NewInvoice.InvoiceNote = TextBoxInvoiceNote.Text; NewInvoice.InvoiceType = LabelInvoiceType.Text; NewInvoice.BookingDateTime = Common.CurrentClientDateTime(Session); NewInvoice.InvoiceNote = TextBoxInvoiceNote.Text; _ControlObjectContext.AddToInvoiceSet(NewInvoice); // add order lines for (int i = 0; i < GridViewOpenOrders.Rows.Count; i++) { if ((GridViewOpenOrders.Rows[i].Cells[0].Controls[1] as CheckBox).Checked) { // load order string Query = "SELECT VALUE it FROM OrderSet as it WHERE it.OrderNumber = @OrderNumber"; ObjectQuery <Order> query = new ObjectQuery <Order>(Query, _ControlObjectContext); query.Parameters.Add(new ObjectParameter("OrderNumber", Convert.ToInt64(GridViewOpenOrders.Rows[i].Cells[2].Text))); ObjectResult <Order> ilines = query.Execute(MergeOption.AppendOnly); Order TempOrder = ilines.First <Order>(); // save location in the invoice if this was unknown yet if ((TempOrder.Location != null) && (NewInvoice.Location == null)) { NewInvoice.Location = TempOrder.Location; if (NewInvoice.Ledger == null) // set ledger for this invoice (default bank) { NewInvoice.Ledger = NewInvoice.Location.BankLedger; } } // add to invoice NewInvoice.AddOrderToInvoice(_ControlObjectContext, TempOrder); } } // subtract advance payments WebUserControlEditAdvancePayments1.LoadAPLines(); for (int i = 0; i < WebUserControlEditAdvancePayments1.AdvancePaymentLines.Count; i++) { Guid TempGuid; TempGuid = (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Id; // load the work or advance payment object RelationAdvancePayment CurrAP = null; RelationWork CurrWork = null; try { CurrAP = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationAdvancePaymentSet", "Id", TempGuid)) as RelationAdvancePayment; NewInvoice.AddAdvancePaymentCorrection(_ControlObjectContext, CurrAP, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Amount, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Description); } catch (Exception) { }; try { CurrWork = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationWorkSet", "Id", TempGuid)) as RelationWork; NewInvoice.AddWorkCorrection(_ControlObjectContext, CurrWork, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Amount, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Description); } catch (Exception) { }; } // process the invoice if (LabelGeneratedInvoiceId.Text != "") { // use old invoice id as group code NewInvoice.GroupCode = new Guid(LabelGeneratedInvoiceId.Text); } NewInvoice.GenerateInvoiceNumber(_ControlObjectContext); //NewInvoice.ProcessInvoice(_ControlObjectContext, NewInvoice.GroupCode); DO NOT PROCESS INVOICE !!! CUSTOMER HAS TO DO THAT SEPERATELY !!! LabelGeneratedInvoiceId.Text = NewInvoice.Id.ToString(); // and save to persistent storage _ControlObjectContext.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave); // commit the transaciton TS.Complete(); // when success advance panel CurrentPage = 5; EnableCurrentPageElements(); } catch (Exception ex) { // rollback transaction TS.Dispose(); // inform user Common.InformUserOnTransactionFail(ex, Page); } } }