2

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.

1
  • 1
    Not quite sure what you want to do, but it looks like you should take a look at TimeSpan Commented May 29, 2013 at 8:01

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

For Min I'd do something like this: DateTime endDate = new DateTime(Math.Min(invoiceDate.Ticks, endOfStorageDate.Ticks));
Or simply DateTime endDate = invoiceDate < endOfStorageDate ? invoiceDate : endOfStorageDate;
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.