- Timestamp:
- 10/01/13 13:18:19 (11 years ago)
- Location:
- branches/OaaS/HeuristicLab.Services.Optimization.Billing
- Files:
-
- 5 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OaaS/HeuristicLab.Services.Optimization.Billing/BillingEngine/BillingEngine.cs
r9653 r10013 6 6 using System.Linq; 7 7 using System.Threading; 8 using System.Transactions; 8 9 using HeuristicLab.Services.Optimization.Billing.Business; 9 10 using HeuristicLab.Services.Optimization.Billing.Interfaces; … … 51 52 public void Run() { 52 53 while (!stop) { 54 TransactionOptions options = new TransactionOptions(); 55 options.IsolationLevel = IsolationLevel.ReadUncommitted; 56 TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, options); 53 57 try { 54 58 Trace.WriteLine("[BillingEngine] Start billing cycle"); … … 58 62 Trace.WriteLine(string.Format("[BillingEngine] Processing {0} active orders.", activeOrders.Count)); 59 63 foreach (Order order in activeOrders) { 64 Trace.WriteLine(string.Format("[BillingEngine] Processing order with id: {0}", order.OrderId)); 65 60 66 if (!order.NextBillableDay.HasValue) { 61 67 // An invoice has never been generated from this order … … 64 70 } 65 71 66 if (order.NextBillableDay >= currentDate) { 67 // TODO: Embed everything in a transaction 68 72 if (order.NextBillableDay <= currentDate) { 69 73 // Collect Usage Data 74 Trace.WriteLine("[BillingEngine] Collecting usage data"); 70 75 //IList<UsageRecord> usageRecrods = BillingService.GetUsageRecords(order.User.Name, order.LastBillableDay.Value, currentDate); 71 76 72 77 // Collect Invoice Data 78 Trace.WriteLine("[BillingEngine] Collecting invoice data"); 73 79 Invoice invoice = new Invoice(); 74 80 invoice.Order = order; … … 96 102 // Bill Post Processing: Apply Discounts or Promotions 97 103 // add discounts, sum up items to sub-total, calc. tax -> total current charges 98 // maybe use windows workflow foundation for this task 104 // maybe use rule execution feature from the windows workflow foundation for this task 105 Trace.WriteLine("[BillingEngine] Bill post preccessing"); 106 double subTotal = 0.0; 107 foreach (InvoiceLine invoiceLine in invoice.InvoiceLines) { 108 subTotal += invoiceLine.ProductPrice; 109 } 110 invoice.SubTotal = subTotal; 111 invoice.Tax = invoice.SubTotal * 0.2; 112 invoice.Total = invoice.SubTotal + invoice.Tax; 99 113 100 // Invoice Formatting Engine: Generate Invoice 114 // Invoice Formatting Engine: Generate Invoice 115 Trace.WriteLine("[BillingEngine] Generating invoice document"); 101 116 invoice.InvoiceDocument = InvoiceFormattingEngine.Generate(invoice); 102 117 … … 104 119 billingService.UpdateOrder(order); 105 120 billingService.SaveInvoice(invoice); 121 122 transaction.Complete(); 106 123 } 107 124 } … … 112 129 Trace.WriteLine(string.Format("[BillingEngine] The following exception occured: {0}", e.ToString())); 113 130 } 131 finally { 132 transaction.Dispose(); 133 } 134 114 135 waitHandle.WaitOne(interval); 115 136 } -
branches/OaaS/HeuristicLab.Services.Optimization.Billing/BillingEngine/PlainTextInvoiceFormattingEngine.cs
r9653 r10013 3 3 using System.IO; 4 4 using System.Linq; 5 using System.Text.RegularExpressions; 5 6 using System.Windows.Forms; 6 7 using HeuristicLab.Services.Optimization.Billing.Interfaces; … … 20 21 private PlainTextInvoiceFormat invoiceFormat; 21 22 private string invoiceOutputPath; 23 private IInvoiceDao invoiceDao; 24 25 private IInvoiceDao InvoiceDao { 26 get { 27 if (invoiceDao == null) { 28 invoiceDao = new FileSystemInvoiceDao(); 29 } 30 return invoiceDao; 31 } 32 } 22 33 23 34 public PlainTextInvoiceFormattingEngine() … … 82 93 invoiceText = invoiceText.Replace("{INVOICE_TOTAL}", invoice.Total.ToString("C2").PadLeft(14)); 83 94 95 // remove all escape sequences and replace directory separator with a hyphen 96 string customerName = Regex.Replace(invoice.User.ContactInformation.OrganizationName, "\\r\\n|\\[bnrt]", ""); 97 customerName = customerName.Replace("/", "-"); 98 84 99 if (invoiceFormat == PlainTextInvoiceFormat.RTF) { 85 invoiceOutputPath = string.Format("{0}_{1}_{2}.rtf", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), "{CUSTOMER}");100 invoiceOutputPath = string.Format("{0}_{1}_{2}.rtf", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), customerName); 86 101 87 102 RichTextBox richTextBox = new RichTextBox(); … … 90 105 richTextBox.SelectionFont = new Font("Consolas", 10, FontStyle.Regular); 91 106 92 File.WriteAllText(invoiceOutputPath, richTextBox.Rtf); 107 //File.WriteAllText(invoiceOutputPath, richTextBox.Rtf); 108 InvoiceDao.SaveDocument(invoiceOutputPath, richTextBox.Rtf); 93 109 } else if (invoiceFormat == PlainTextInvoiceFormat.TXT) { 94 invoiceOutputPath = string.Format("{0}_{1}_{2}.txt", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), "{CUSTOMER}");110 invoiceOutputPath = string.Format("{0}_{1}_{2}.txt", invoice.InvoiceId, invoice.InvoiceDate.ToString("yyyMMdd"), customerName); 95 111 96 File.WriteAllText(invoiceOutputPath, invoiceText); 112 //File.WriteAllText(invoiceOutputPath, invoiceText); 113 InvoiceDao.SaveDocument(invoiceOutputPath, invoiceText); 97 114 } else if (invoiceFormat == PlainTextInvoiceFormat.PDF) { 98 115 throw new Exception("PDF invoice format is currently not supported"); -
branches/OaaS/HeuristicLab.Services.Optimization.Billing/Business/BillingService.cs
r9645 r10013 19 19 } 20 20 21 private ITransactionManager transaction; 22 private ITransactionManager Transaction { 23 get { 24 if (transaction == null) transaction = new TransactionManager(); 25 return transaction; 26 } 27 } 28 21 29 #endregion 22 30 … … 25 33 public IList<Product> GetProducts() { 26 34 //TODO: Authentication 27 //TODO: Enable Transaction 28 return BillingDao.FindAllProducts().ToList(); 35 return Transaction.UseTransaction(() => { 36 return BillingDao.FindAllProducts().ToList(); 37 }); 29 38 } 30 39 31 40 public IList<Product> GetProductByName(string productName) { 32 41 //TODO: Authentication 33 //TODO: Enable Transaction 34 return BillingDao.FindProdcutBy(prod => prod.Name == productName).ToList(); 42 return Transaction.UseTransaction(() => { 43 return BillingDao.FindProdcutBy(prod => prod.Name == productName).ToList(); 44 }); 35 45 } 36 46 … … 41 51 public void SaveOrder(Order order) { 42 52 //TODO: Authentication 43 //TODO: Enable Transaction 44 BillingDao.InsertOrUpdateOrder(order); 53 Transaction.UseTransaction(() => { 54 BillingDao.InsertOrUpdateOrder(order); 55 }); 45 56 } 46 57 47 58 public IList<Order> GetOrdersByState(OrderState state) { 48 59 //TODO: Authentication 49 //TODO: Enable Transaction 50 return BillingDao.FindOrderBy(order => order.StateValue == (int)state).ToList(); 60 return Transaction.UseTransaction(() => { 61 return BillingDao.FindOrderBy(order => order.StateValue == (int)state).ToList(); 62 }); 51 63 } 52 64 53 65 public IList<Order> GetOrders(string userName) { 54 66 //TODO: Authentication 55 //TODO: Enable Transaction 56 User user = GetUser(userName); 57 return BillingDao.FindOrderBy(order => order.User == user).ToList(); 67 return Transaction.UseTransaction(() => { 68 User user = GetUser(userName); 69 return BillingDao.FindOrderBy(order => order.User == user).ToList(); 70 }); 58 71 } 59 72 60 73 public void UpdateOrder(Order order) { 61 74 //TODO: Authentication 62 //TODO: Enable Transaction 63 BillingDao.InsertOrUpdateOrder(order); 75 Transaction.UseTransaction(() => { 76 BillingDao.InsertOrUpdateOrder(order); 77 }); 64 78 } 65 79 … … 70 84 public User GetUser(string userName) { 71 85 //TODO: Authentication 72 //TODO: Enable Transaction 73 return BillingDao.FindUserBy(u => u.Name == userName).First(); 86 return Transaction.UseTransaction(() => { 87 return BillingDao.FindUserBy(u => u.Name == userName).First(); 88 }); 74 89 } 75 90 76 91 public void SaveUser(User user) { 77 92 //TODO: Authentication 78 //TODO: Enable Transaction 79 BillingDao.InsertOrUpdateUser(user); 93 Transaction.UseTransaction(() => { 94 BillingDao.InsertOrUpdateUser(user); 95 }); 80 96 } 81 97 … … 86 102 public IList<UsageRecord> GetUsageRecords(string userName) { 87 103 //TODO: Authentication 88 //TODO: Enable Transaction 89 User user = GetUser(userName); 90 return BillingDao.FindUsageRecordsBy(ur => ur.User == user).ToList(); 104 return Transaction.UseTransaction(() => { 105 User user = GetUser(userName); 106 return BillingDao.FindUsageRecordsBy(ur => ur.User == user).ToList(); 107 }); 91 108 } 92 109 … … 101 118 public IList<Invoice> GetInvoices(string userName) { 102 119 //TODO: Authentication 103 //TODO: Enable Transaction 104 User user = GetUser(userName); 105 return BillingDao.FindInvoiceBy(inv => inv.User == user).ToList(); 120 return Transaction.UseTransaction(() => { 121 User user = GetUser(userName); 122 return BillingDao.FindInvoiceBy(inv => inv.User == user).ToList(); 123 }); 106 124 } 107 125 108 126 public void SaveInvoice(Invoice invoice) { 109 127 //TODO: Authentication 110 //TODO: Enable Transaction 111 BillingDao.InsertOrUpdateInvoice(invoice); 128 Transaction.UseTransaction(() => { 129 BillingDao.InsertOrUpdateInvoice(invoice); 130 }); 112 131 } 113 132 -
branches/OaaS/HeuristicLab.Services.Optimization.Billing/HeuristicLab.Services.Optimization.Billing.csproj
r9645 r10013 35 35 <HintPath>..\packages\EntityFramework.5.0.0\lib\net40\EntityFramework.dll</HintPath> 36 36 </Reference> 37 <Reference Include="Microsoft.WindowsAzure.StorageClient, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> 37 38 <Reference Include="System" /> 38 39 <Reference Include="System.ComponentModel.DataAnnotations" /> … … 42 43 <Reference Include="System.Drawing" /> 43 44 <Reference Include="System.ServiceModel" /> 45 <Reference Include="System.Transactions" /> 44 46 <Reference Include="System.Windows.Forms" /> 45 47 <Reference Include="System.Xml.Linq" /> … … 50 52 </ItemGroup> 51 53 <ItemGroup> 54 <Compile Include="BillingEngine\AzureInvoiceDao.cs" /> 52 55 <Compile Include="BillingEngine\BillingEngine.cs" /> 56 <Compile Include="BillingEngine\FileSystemInvoiceDao.cs" /> 53 57 <Compile Include="BillingEngine\PlainTextInvoiceFormattingEngine.cs" /> 54 58 <Compile Include="Business\BillingService.cs" /> 55 59 <Compile Include="Business\BillingServiceProvider.cs" /> 60 <Compile Include="Business\TransactionManager.cs" /> 56 61 <Compile Include="DataAccess\BillingContext.cs" /> 57 62 <Compile Include="DataAccess\BillingDao.cs" /> 58 63 <Compile Include="Interfaces\IEntity.cs" /> 59 64 <Compile Include="Interfaces\IGenericDao.cs" /> 65 <Compile Include="Interfaces\IInvoiceDao.cs" /> 60 66 <Compile Include="Interfaces\IInvoiceFormattingEngine.cs" /> 61 67 <Compile Include="Interfaces\IObjectWithState.cs" /> 62 68 <Compile Include="Interfaces\IOptimizationBilling.cs" /> 63 69 <Compile Include="Business\MockupBillingService.cs" /> 70 <Compile Include="Interfaces\ITransactionManager.cs" /> 64 71 <Compile Include="Model\Model.cs" /> 65 72 <Compile Include="Properties\AssemblyInfo.cs" /> -
branches/OaaS/HeuristicLab.Services.Optimization.Billing/Model/Model.cs
r9645 r10013 151 151 public DateTime Due { get; set; } 152 152 public int StatusValue { get; set; } 153 public double SubTotal { get; set; } 153 154 public double Total { get; set; } 154 155 public double Tax { get; set; }
Note: See TracChangeset
for help on using the changeset viewer.