1

I have a database with some prices in it. For example I have milk = 1€. I want the price not to be modify for more or less than 10%. So in this case you can set the price only between 0.90 or 1.10. It's easy to do that, however, when the price is modified, the user can modify it again right after. (if he sets it to 0.9, then he can set it between 0.81 and 0.99 and so on...)So I want to affect in a variable the price for example each day at 9 a.m, so in one day it can't change for more or less than 10%.

DataClassesDataContext db = new DataClassesDataContext();

List<ARTICLE> article = db.ARTICLE.ToList();

for (int i = 0; i < article.Count; i++)
{
    switch (article[i].Name)
    {
        case "Milk":
            if (decimal.Parse(Milk.Text, CultureInfo.InvariantCulture) <= 
                (article[i].Price/ 10) * 11 && decimal.Parse(Milk.Text,
                 CultureInfo.InvariantCulture) >= (article[i].Price/ 10) * 9)
            {
                if (decimal.Parse(Milk.Text, CultureInfo.InvariantCulture) != article[i].Price)
                {
                    article[i].Price= decimal.Parse(Milk.Text, CultureInfo.InvariantCulture);
                }
            }
            break;
        case "Other thing":
            //... etc
     }
}
EpicKip
  • 4,015
  • 1
  • 20
  • 37
Alexandre
  • 125
  • 9
  • Have you tried anything? There are probably thousands of ways to achive that. Are you telling me you didn't come up with any? – Pikoh Jun 15 '17 at 08:14
  • There are many solutions to this problem, for example some service with timer.. and many more – Daniel Tshuva Jun 15 '17 at 08:14
  • I have added what I did, i just wanna know how to set a global variable for example to the price in database each day so I can use it for my if statements – Alexandre Jun 15 '17 at 08:19
  • Ok,just a quick idea. Add a column to your database with the date of the prices change. Before updating the table,check if that date is the same as today. In that case, don't update. Otherwise, update and set the date to today... – Pikoh Jun 15 '17 at 08:22
  • @mjwills The user can udpate prices as many times as he wants in a day, but I want the price not to change for more or less than 10% in one day. We could consider day starts from 00:00 a.m to 23.59 pm. It's to prevent a massive loose of money if someone cracked his password or something. – Alexandre Jun 15 '17 at 08:32

5 Answers5

3

So you want to:

  • Save the start price for each day
  • Allow the user to change the price for that day, within a threshold of N%
  • Allow the user to change the price multiple times a day (to fix corrections, or to increase or decrease the price as long as the total difference is < N%)

Then upon changing the daily price, check that it doesn't vary from the base price by more than N%.

To solve this, you'd use three columns: DailyPrice, CurrentPrice, PriceModified. Then upon editing a price:

if (record.PriceModified.Date != DateTime.Now.Date)
{
    // First edit for today. 
    if (PriceWithinChangeThreshold(formData.NewPrice, record.CurrentPrice))
    {       
        // Save the old price as the new start price for today
        record.DailyPrice = record.CurrentPrice;
        record.CurrentPrice = formData.NewPrice;
        record.PriceModified = DateTime.Now;
    }
}
else
{
    // This price has already been edited today.
    if (PriceWithinChangeThreshold(formData.NewPrice, record.DailyPrice))
    {
        // But it's within the threshold for today
        record.CurrentPrice = formData.NewPrice;
        record.PriceModified = DateTime.Now;
    }   
}

But as discussed in comments, saving prices as mere decimal columns is generally a bad idea. Think about (historical) reporting, invoicing, taxes, currency and whatnot: a "product price" is way more than a number. So I'd suggest storing your prices in a different manner altogether. See mjwlls' answer for that.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 1
    Let's say the base price was $1. Would this solution allow the price to be $1.21 after one day, $1.33 after two days etc etc? – mjwills Jun 15 '17 at 08:24
  • No worries. An easy mistake to make. – mjwills Jun 15 '17 at 08:27
  • But just saving the modified date isn't a solution either, as this disallows you to change the price more than once a day, even when those changes are within the threshold (or if you want to correct an incorrect price change). So you'll need both. Am editing the answer. – CodeCaster Jun 15 '17 at 08:28
  • I can't still agree with this answer.Using an "offline process" when you could easily just have a "lastModified" column seems overcomplicated to me... – Pikoh Jun 15 '17 at 08:34
  • What is this LastModified column going to do? When is the base price for the day going to be set? Upon the first modification for a day? I don't like price columns anyway, prices have way more information than fits in just one decimal column, I'd use a junction table with pricinghistory (especially for reporting, invoicing, and so on). There will be no link between the date and the price for that day. If you modify it once, you can't modify it again. Feel free to write it out, I can't wrap my head around it ATM. – CodeCaster Jun 15 '17 at 08:35
  • As it name suggest,store the date of the last modification. If you try an update within the same date, the price variation only could be of,lets say, +-10%. It the date in there is before todays date, the price variation can be anything – Pikoh Jun 15 '17 at 08:38
  • And I don't like price columns neither, i agree with you in that – Pikoh Jun 15 '17 at 08:40
  • Yes, that's what i meant :) Anyway, the best answer for me is using a price history table,as in @mjwills answer – Pikoh Jun 15 '17 at 08:45
  • 1
    @Pikoh yes, that's definitely better, but I'm generally hesistant to say "change everything." – CodeCaster Jun 15 '17 at 08:46
2

One possible solution for this would be to add a PriceHistory table.

Then whenever you save a price, you check whether the new price is valid and (if it is) add a record to the PriceHistory table.

The check would be something like:

SELECT TOP 1 MostRecentPrice
FROM PriceHistory
WHERE Item = 'Milk' and PriceDateTime < 'Today's Date (with midnight as the time)'
ORDER BY PriceDateTime DESC

Then verifying that the new price you are saving is within 10% of MostRecentPrice. Also, make sure you handle the scenario where there is no recent price (i.e. this is the first time you have saved the price for an item).

Another advantage of this is that not only does it solve your current problem, it gives you valuable information over time. "What was the price of milk 3 weeks ago?" That kind of thing.

Plus, you can alter the rules of the validation reasonably simply - for example you could say 'no price changes of 10% per day or 20% per week' - which you can't do as easily if you don't store the full history.

mjwills
  • 23,389
  • 6
  • 40
  • 63
  • So If I understood well I have a new table with all the price changes and their date. When I modify the price, I check the first price of the day and see if it fits with the constraint ? – Alexandre Jun 15 '17 at 08:43
  • Almost. **Before** you write the price of the day you check for what the price was *yesterday* (that is what my suggested query does). And then you check that the price you are about to write is within 10% of the number returned from the query. – mjwills Jun 15 '17 at 08:45
  • Also, if possible, you should ask the database for the date time rather than get it from the local machine. Otherwise you can just change the local date of your machine to easily change prices at will. :) See https://stackoverflow.com/a/12106050/34092 – mjwills Jun 15 '17 at 08:52
1

You can save last modification date time and can validate it upon every change.

Harsh Sharma
  • 910
  • 1
  • 7
  • 20
0

You could introduce a lastModified-member in all your Articles. Thus you know if you may or may not modify the current product:

class Article
{
    DateTime LastModified { get; set; }
}

...

foreach(var article in articles)
{
    var timespan = DateTime.Now - article.LastModified;
    if(timeSpan.TotalDays >= 1) ...
}
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
0

Create a column like "LastModified" as DateTime in your table. And enable the button for modification after checking if the day and month of "LastModified" is not the same as today's date.

It can't be done in the application itself or you would need something that runs on a server that you can query with a webservice or what ever.

If you can't modify the table maybe creating a local file with a last modified date but then the user can modify it so be careful.

user5014677
  • 694
  • 6
  • 22