Ruby / Rails calculation - ruby-on-rails

I have rails 3 application and the following problem. I need to calculate a price based on the following:
Term range from 1 to 365 days (1 year).
Tariffs in a tables which presented in two sections: for one day and for half a month (15 days)
Example 1:
Prices: 1 day price = 0.5 and 15 days = 6
Term : 45 days.
Price: To get the price we devide the number of days by 15 (45/15 = 3) and multiply the result by the tariff for 15 days (3*6 = 18). Final price 18.
Example 2:
Prices: 1 day price = 0.5 and 15 days = 6
Term : 79 days.
Price: To get the price we find the half month period in this case is 45 and again we devide that number by 15 (75/15 = 5) and multiply the result by the tariff for 15 days (5*6 = 30). However there are 4 more days to account for, so we multiple them by the price for a day (4*0.5 = 2). The sum of the two results forms the final price (30+2 = 32).
The period is submited through a from and can be anything from 1 day to 365 days, prices per day and 15 days are stored in a database. My question is how to make the calculations in ruby/rails, so the code always calculates the half month and the reminder if any?
Any help is appreciated.

use the modulo operator:
79 / 15 # dividing integers performs an euclidian division
=> 5
79 % 15 # gives you the rest from an euclidian division.
=> 4

Related

Is it possible to sum a value if the substraction of two value on the same row equals something?

I'm trying to build a sheet where I can see how much I have to pay each month.
Let's say I have the following table
Current installment (CI)
Total installments (TI)
Installment amount (IA)
1
3
$100
1
1
$200
2
3
$150
1
3
$75
2
4
$150
1
1
$50
So, the first month would be if TI-CI >= 1, then I will sum that value. For the following month I would do the same but TI-CI >= 2
And the result would be something like this
-
-
1st month debt
$475 (the result of 100+150+75+100)
2nd month debt
$325 (the result of 100+75+150)
3rd month debt
$100
Is this possible at all?
try:
=IFNA(SUM(FILTER(C$2:C, (B$2:B-A$2:A)>=ROW(A1))))
and drag down

Need to return production finish date of an order based on the weekly production Qty in excel

I have 2 tables
Table-1 = Order details
Table-2 = Production details.
Explanation of color inside table:
Yellow color = Output Qty week wise and product wise.
Green color = My expectation. Example- The second order of shirt(Qty-10) delivery date is 14 Jan & there are 2 more orders (order num 1 & 4) of shirt which have delivery earlier than 14 Jan. So the finish week will be 4 as the order num 1 & 4 (total Qty 6) will be produced till week 2 as per the Table-2 (total Qty =7 (3+4).
Thanks to help me write the formula in E 2 to E6 cells.
Table1:
Table2:
Work out the sum of quantities for the same product and dates including this one using sumifs.
Compare it to the cumulative sum of the numbers produced for this product using match.
=ArrayFormula(match(true,sumifs(C$2:C$6,B$2:B$6,B2,D$2:D$6,"<="&D2)<=sumif(column(H:K),"<="&column(H:K),index(H$3:K$4,match(B2,G$3:G$4,0),0)),0))
I'm assuming for the time being that you couldn't have two rows with the same product and delivery date. If this could happen, you could refine the formula for the situation where (say) the first delivery could be sent in week 2 but the next delivery would be in week 3.

Spreading cost per month over duration of months in Sheets

I have a tricky problem in Sheets.
I need a formula to calculate running total costs for each month from Nov 2019 on wards (column B).
Currently, my formula for B2 is:
=SUMIFS($F$2:$F$6,$E$2:$E$6,">="&A2,$E$2:$E$6,"<="&(EDATE(A2,1)-1))
Basically, this finds all values in cells F2:F6 whose dates (in column E) match that of A2.
E.g. Cell B3 is the total cost for December, so cells F3 and F6 are a match (200 + 300 = 500)
However, this does not take into account the duration of the cost (column G).
This means that the total cost for December 2019 (cell B3) should actually be 600 (because the November cost duration lasts 12 months). Meaning there is a cumulative cost for the duration of months the cost lasts for.
I am pretty much stuck on this. If anyone could help that would be great!
So as long as the date given in column A intersect between "start date" and "start date" + x months the cost per month should be applied?
So according to this, Jan-2020 will have a cost of 600 in this example:
Sant/Falskt = True/False
Green rows = date intersect between Start and End date
If that is correct then this formula should do the job, paste in B2:
=SUMPRODUCT((--(A2>=DATE(YEAR($E$2:$E$6),MONTH($E$2:$E$6),1))*--(A2<DATE(YEAR($E$2:$E$6),MONTH($E$2:$E$6)+$G$2:$G$6,DAY($E$2:$E$6))))*$F$2:$F$6)

Conditional sum contigous columns

I'd like to sum the total time allocated per task. Time has been provided either in minutes or in hours.
For example Task A should be:
30 minutes + 2 hours x 60 minutes/hour + 150 minutes = 300
What formula should I use on B2 to get such a result, considering that I have many more than 4 people?
Try
=sumproduct(n(C2:I2)*if(D2:J2="minutes",1,60))

Return an Integer between 1 and 52, based on a given Date

I have a client that's giving me data sets that are broken down into quarters, periods (a block of four weeks in a quarter), and weeks. I'm writing a quick reference algorithm to return the quarter, period, week given a date and year and vise versa.
Their data is always broken down into 52 weeks, where week 1 always contains Jan 1st and starts with the Monday before or at Jan 1st. This is how they handle the 365 / 7 = 52.142857 conundrum.
So, is there a gem or built in function (cweek returns 1-53), that would give me a week number based on the premise that week 1 always contains Jan 1st or do I need to design something additional?
Way 1. Date#strftime
Date.new(2016,1,1).strftime("%U").to_i + 1 # week starts with Sunday
Date.new(2016,1,1).strftime("%W").to_i + 1 # week starts with Monday
Way 2. Date#cweek
Date.new(2016,1,1).cweek % 53 + 1 # week starts with Monday

Resources