2
votes

I have previously posted this question. However I was not specific enough, so I am trying to explain it better in this post.

I am currently writing a small program to create invoices. The invoices should be calculated on account of storage time of items e.g. costPerDay * numberOfDaysInStorage.

The invoice is created on a monthly basis, e.g. InvoiceDate = 31/05/2013.

The items start date and end date (in storage) is extracted from a database.

So, I have:

Variables:

 DateTime StartInStorageDate; 
 DateTime EndOfStorageDate;
 DateTime InvoiceDate; //(always last date of month)

Rules

  • The first five days in storage should always be free.
  • Calculation of days, should only be made for a given invoice date.
  • If the date prior to the InvoiceDate exist, the days should be taken into account, in order to calculate the "free days". If month date prior has 5 or more days, to the end of its month, we have to assume that these have been calculated, and therefore not take them into account in the invoice month.

Examples

  • Example 1:

    DateTime StartInStorageDate = 07/04/2013;

    DateTime EndOfStorageDate = 08/06/2013;

    DateTime InvoiceDate = 31/05/2013;

    Calculation of days = 31 days (Because Invoice date has a date prior, which has more than five days, the "free days" are already subtracted in that month)

  • Example 2:

    DateTime StartInStorageDate = 28/04/2013

    DateTime EndOfStorageDate = 08/06/2013

    DateTime InvoiceDate = 31/05/2013

    Calculation of days = (11 days total - 5 "free days") = 6 days (Because Invoice date has a date prior, which does not have 5 days to end of its month, we have to add these days to the invoice month, and subtract 5 "free days" from the total)

  • Example 3:

    DateTime StartInStorageDate = 28/04/2013

    DateTime EndOfStorageDate = 08/05/2013

    DateTime InvoiceDate = 31/04/2013

    Calculation of days = 0 (Because the invoiceDate does not have more than 5 days, and does not have a month prior)

I hope someone can provide me with some pointers, or some code to help calculate the days. The part I find tricky, is to "look into" a previous month (if one exists) from the invoice date, to check for days.

Thank you.

2
Not quite sure what you want to do, but it looks like you should take a look at TimeSpan - Corak

2 Answers

2
votes

It requires only a few simple actions:

DateTime endDate = Min(invoiceDate, endOfStorageDate );    // Min() is pseudo code
int daysInStorage = (endDate - StartInStorageDate).Days;
daysInStorage -= 5;
if (daysInStorage < 0) daysInStorage = 0;
0
votes

You need to calculate two dates: the start and end of the invoice period:

DateTime invoiceStart = StartInStorageDate.AddDays(5);
DateTime invoiceEnd = InvoiceDate < EndOfStorageDate ? InvoiceDate : EndOfStorageDate; 

double billedDays = Math.Max(0, (invoiceEnd - invoiceStart).TotalDays);