Generating Total from Seeds? (ActiveRecord) - ruby

Right now, I am learning ruby and activerecord, and haven't touched upon rails just yet. I am trying to create a Cart instance with attributes of item, price, quantity, order, and total. I have three tables [carts (which belongs to items and order), items, and order].
While Order is just information of the customer, the Item contains the price while the Cart contains the quantity. The problem is that the item attribute is randomized, and total is a float number. I am trying to figure out how I can get the total from the price. Here is how the seeds look:
One of the Item instances:
Item.create(item: "Pencil", price: 1.99, stock: 300)
Cart instance:
Cart.find_or_create_by(item: Item.all.sample, order: Order.all.sample, quantity: rand(1..10), total: )
I appreciate the help!

item = Item.all.sample
quantity = rand(1..10)
total = item.price * quantity.to_f
Cart.find_or_create_by(item: item, order: Order.all.sample, quantity: quantity, total: total)
You could save the item and the quantity to variables and then find the total my multiplying Item#price by quantity, save that to variable, and use that to set the total when you .find_or_create_by

Related

rasa-core Map entities to other entites

I am building a simple food ordering bot. In this I have a take_order intent in which two entities will be extracted food_item and quantity, both of these entities have list types in slots, for example if a user message like this comes:
I would like to have [one] (quantity) [chicken burger] (food_item) and [two] (quantity) [fries] (food_item)
slot for this example will be: slot{“quantity”: [“one”, “two”], “food_item”: [“chicken burger”, “fries”]}
in the action user_take_order I will be multiplying quantity of each item to its price and giving total bill to the user.
But I have a problem, in a complex case when user do not provide a quantity for the food_item, I will be assuming the default quantity to one but the problem occurs when user orders three items and do not provide quantity for only the second item, for example:
I would like to have [one] (quantity) [chicken burger] (food_item), [fries] (food_item) and [two] (quantity) [soft drinks] (food_item)
in this example no quantity is provided for the fries and slots filled: slot{“quantity”: [“one”, “two”], “food_item”: [“chicken burger”, “fries”, “cold drink”]}
in the action user_take_order I would like to do this:
1 x price_of_chicken_burger
1 x price_of_fries
2 x price_of_cold_drink
but the problem is that in quantity slot I have only quantities of chicken burger and cold drink and I do not have a clue that user did not mention the quantity of fries( I want to set quantity of fries as 1 “default case”)
have I choose the wrong types for slots quantity and food_item?
slots:
food_item:
type: list
quantity:
type: list
One of the possible solutions is to extract quantity and food item as one entity:
I would like to have [one chicken burger] (quantity_food_item), [fries] (quantity_food_item) and [two soft drinks] (quantity_food_item)
then differentiate them inside an action.

Complicated Cube Query

I'm working on a fairly complicated view, which calculates the total cost of a guest's stayed based on data pulled from four different tables. The output however is not exactly what I want. My code is
CREATE OR REPLACE VIEW Price AS
SELECT UNIQUE
Booking.Booking_ID AS "Booking",
Booking.GuestID AS "Guest ID",
Room.Room_Price*(Booking.CheckOutDate-Booking.CheckInDate) AS "Room Price",
Add_Ons.Price AS "Add ons Price",
Room.Room_Price*(Booking.CheckOutDate-Booking.CheckInDate) + (Add_Ons.Price) AS "Total Price"
FROM Booking JOIN Room ON Room.Room_Num = Booking.Room_Num
JOIN Booking_Add_Ons ON Booking.Booking_ID = Booking_Add_Ons.Booking_ID
JOIN Add_ons ON Booking_Add_Ons.Add_On_ID = Add_Ons.Add_On_ID
ORDER BY Booking.Booking_ID;
Now, I'm trying to get this to return the total cost of all Addons, plus the cost of the hotel rooms as the total price, however it is returning the cost of the rooms + each of the addons on separate lines. As follows:
My question is, is it possible to use something like CUBE, or SUM to add up the rows, so that there is only one entry for each of the Bookings with the total price of all add-ons accounted for?

How to recalculate tax on new row total in magento?

I’m making a call to $item->calcRowTotal() on a Mage_Sales_Model_Quote_Item object.
This works great to reset the row total and base row total, but it does not affect the row total including tax (row_total_incl_tax) attribute on the item.
I assume I have to manually do this after I have the new row total but I can’t figure out how to properly calculate the tax and populate the row_total_incl_tax attribute on the item.
Any suggestions would be much appreciated.
Get the tax rate and recalculate row_total_incl_tax. Here's how you can get tax rate.
Assuming your order no. is 101.
$sale = Mage::getModel('sales/sale)->load(101)
$taxModel = Mage::getModel('sales/order_tax')
->load($sale->getId(), 'order_id');
$taxRate = $taxModel->getPercent()
I think you know how to calculate the tax amount based on row total value.
Edit:
Get tax based on product:
$product = Mage::getModel('catalog/product')->load($item-getProductId());
$request = Mage::getSingleton('tax/calculation')
->getRateRequest()
->setProductClassId($product->getTaxClassId());
$taxRate = Mage::getSingleton('tax/calculation')
->getRate($request);
So you know the tax rate, row_total_incl_tax is sum of tax * qty + total amount (or you can calculate anyway you want). May be you need to check if item price is included tax or not, you can do this by Mage::getModel('Tax/Config')->priceIncludesTax()

Magento how update product price in cart

I want to update the product price depend on different sizes selected and quantity entered for the size (I am using the custom options for different sizes).
for example:
I have one product with two different size options
size 1: S
size 2: M
where size 2 have cost with $2 extra. so if the product price is $14 and user select this size then this will charge user extra $2 for each M size quantity entered.
Another example:
Small size quantity: 5
Medium size quantity: 1
Then the product price will be
5 * 14 = $70
1 * 16 = $16
Total price: $86
But according to magento this effect overall price:
6 * 16 = $96
so the product cost is $96 instead of $86.
Which types do the products have? Configurable products should exist of a set Simple products which are or are not visible individually.
Since the configurable products is just a shell to keep it all together you can still alter the prices of the individual products.
For more info on configurable products, attributes and simple products check: http://www.magentocommerce.com/knowledge-base/entry/tutorial-creating-a-configurable-product/
Good luck!

Modify Magento Subtotal Calculation: Tier prices

I am using Tier Prices and have a set price for quantities that fall between 1 and 4 and a per item price for anything over 4.
I want to modify my cart to have a set price for items where Qty >= 1 && Qty <=4
How do I do that?
Here is the item i testing this with: http://stjamestutorials.com/ppptransportation/index.php/mayfield-falls-tours-negril-jamaica-tours.html
Try and create an observer for this event:
catalog_product_get_final_price
which is found in the class :
app\code\core\Mage\Catalog\Model\Product\Type\Price.php
The event comes with the product and the qty, so you can write your logic and then set your custom price in the final_price attribute of the product.

Resources