2012-07-20 127 views
0

需要一些严重的脑力!我有一个问题,我试图从两个给定日期范围中计算多次收费的价格。基本上我有如下发票行:C#比较日期范围

BillID  AccountID  BilledFrom  BillTo  Price 
34   3456   10/10/2012  10/12/2012 86p 

我然后有一个定价矩阵可以是:

AccountID  EffectiveFrom  EffectiveTo  Price 
3456   09/09/2009   10/11/2012  86p 
3456   11/11/2012   20/11/2012  56p 
3456   21/11/2012   10/12/2013  24p 
3456   11/12/2013   null   18p -- null never expires 

目前被取最初的价格,其中BillFrom日期生效于和生效至日期和之间每天计费。现在我想弄清楚账单上的滥收费用,因为您可以看到Bill到目前为止超过了86p系列的EffectiveTo日期。所以有凡客已被过度的线2或者定价应该踢1个月的时期,我需要无论是在SQL或C#中的函数将结果:

OverchargeID type  BillID  Reason  Days  Price 
    1   Credit  34  PriceChange  10  36p  (86p-56p) 
    2   Credit  34  PriceChange  19  64p  (86p-24p) 

我将处理56000记录可以有多达10个价格范围。我有下面的代码来工作它的服务器端,但它是不实际的,大约需要10分钟。任何解决方案或此代码的修改将不胜感激。

foreach (InvoiceOverchargePriceChange_SelectResult line in result) { 

      //Get the invoiceLineRecord 
      InvoiceLine invoiceLine = _dataContext.InvoiceLines.Where(m=>m.InvoiceLineID == line.InvoiceLineID).FirstOrDefault(); 


      // get the prices for each line 
      List<Pricing_SelectResult> prices = ctrl.Select(line.AccountID, null, null, null, null) 
                .Where(m=>m.BillingMethodID == line.BillingMethodID) 
                .OrderByDescending(m=>m.EffectiveFrom).ToList(); 

      // if the price count is greater than 1 then need to check if overcharges occurred 
      if (prices.Count > 1) { 

       DateTime date = new DateTime(); 
       int days = 0; 
       decimal charge = 0; ; 
       for (int i = 0; i < prices.Count(); i++) { 
        days = 0; 
        charge = 0; 

        //if it goes in we found our price that we used 
        if (invoiceLine.BillFrom >= prices[i].EffectiveFrom && prices[i].EffectiveTo != null) { 

         date = invoiceLine.BillTo; 
         if (prices[i].EffectiveTo == null) break; 
         //check the Bill to date does not exceed the effective To date, if it does go in 
         while (date >= prices[i].EffectiveTo) { 



          if (date == invoiceLine.BillTo) { 

           //if its first go set the price 
           charge = prices.Where(m => date >= m.EffectiveFrom).FirstOrDefault().Price; 


          } 

          if (charge == prices.Where(m => date >= m.EffectiveFrom).FirstOrDefault().Price) { 

           //increment the days counter 
           days++; 
           date = date.AddDays(-1); 

          } 
          else { 

           //insert the days and price into db here... 


           //reset the days 
           days = 0; 
           charge = prices.Where(m => date >= m.EffectiveFrom).FirstOrDefault().Price; 




          } 


         } 

        } 

       } 

      } 


     } 

在此先感谢

+1

请注意,这些都是DD/MM/YYYY日期,美国开发者... – 2012-07-20 14:09:14

+0

哪里'result'从何而来?另一个Linq to SQL查询? – 2012-07-20 14:15:34

+0

是结果是不同帐单ID和帐户ID – CR41G14 2012-07-20 14:46:19

回答

0

尝试......

select 
     ROW_NUMBER() over (order by BillID, StartDate), 
     case when (billprice>pricingprice) then 'Credit' else 'Debit' end, 
     BillID, 
     DATEDIFF(d, startdate,enddate)+1 as days, 
     billprice-pricingprice 
from 
(
     select 
      BillID, 
      case when Effectivefrom>BilledFrom then Effectivefrom else BilledFrom end startdate, 
      case when isnull(effectiveto,GETDATE())<BillTo 
       then isnull(effectiveto,GETDATE()) else billto-1 end enddate, 
      bill.price as billprice, 
      pricing.price as pricingprice 
     from pricing 
    inner join bill 
       on bill.accountid= pricing.accountid 
       and (billedfrom<isnull(effectiveto,getdate())) and (BillTo>Effectivefrom) 
       and bill.price <> pricing.price 
) v 
+0

消息206,级别16,状态2,行2 操作数类型冲突:日期与int – CR41G14 2012-07-20 15:26:54

+0

@ user1401320不兼容您可以发布您的数据表结构 – podiluska 2012-07-20 15:30:46

+0

确定修正它修改“billto-1”to dateadd(day,-1,billedto)但我没有从中得到正确的计算结果,我的数据库表如下: – CR41G14 2012-07-20 15:39:11