Best way to store differing tax rates for countries in SQL (then how to use it in a basket situation) - sql

I have raw data that I'm trying to sort; I have the tables (I think) decided on with a basket including a basket_id, customer_id, basket_date, total_price, tax and payment_type.
Tax is a set portion of the total price, so if the total price were 512.99 and the seller came from the UK (which would denote a tax percentage of 10%, different for each country and a total of 6 countries) then the tax would be 51.299. I need to store that tax figure, NOT the percentage, in this basket table by somehow referencing the tax percentage in another tax table so that SQL can automatically calculate the figure from the percentage*total_price. Is this possible?
I don't need anything unnecessary in examples, JUST what I need to do, no need to overcomplicate it!

Related

Bigcommerce - Update Price of Products in Cart Based on Price List & Custom Fields

With Bigcommerce Price Lists, we obviously have the ability to give discounts based on bulk buys/qty.
We want to be able to combine the quantities of items in the cart if the products have a common custom field (or similar) and give the bulk price list discounts based on the combined qty.
For example:
Product A has a bulk purchase discount - buy 5+ and get 10% discount
Product B has a bulk purchase discount - buy 10+ and get 15% discount
Both these products would have a custom field/meta field at the variant level that = "Allow Combined Discount A"
So that means if Product A has a cart qty of 4, and product B has a cart qty of 6, the combined qty = 10
Product A should receive 10% discount (as combined qty >=5)
Product B should receive 15% discount (as combined qty >=10)
How can we achieve this? Ideally, the price updates/discounts would apply immediately when the cart quantities reach the correct levels. There could also be other products in the cart with different combining custom field/meta field values, which would need to calculate their own total - e.g. "Allow Combined Discount B"
Promotions on categories will not work as there are can be varients on products a and b where the bulk discounts do not apply, or the qty of some variants should not count towards a combined total to apply price list bulk buy discounts.

Choosing a value based on a ranking of another column

I have decent Google-Fu skills, but they've utterly failed me on this.
I'm working in PowerPivot and I'm trying to match up a product with a price point in another table. Sounds easy, right? Well each product has several price points, based on the source of the price, with a hierarchy of importance.
For Example:
Product 1 has three prices living in a Pricing Ledger:
Price 1 has an account # of A22011
Price 2 has an account # of B22011
Price 3 has an account # of C22011
Price A overrides Price B which overrides Price C
What I want to do is be able to pull the most relevant price (i.e, that with the highest rank in the hierarchy) when not all price points are being used.
I'd originally used a series of IF statements, but that's when there were only four points. We now have ten points, and that might grow, so the IF statements are an untenable solution.
I'd appreciate any help.
-Thanks-

Sell numbered seats in Prestashop

Im looking for a way to sell numbered Tickets on Prestashop. For example I have a ticket for a Hockey Game. Seats are numbered, so when I sell a ticket for seat A8, this seat is gone and can not be sold again. The seats are all the same price for a given Game or Season. A customer should be able to choose his seat from a dropdown list.
You will need to add product quantities and enable out of stock feature.
Then set the product quantity to 1.
And not allowing orders when the product quantity is less than 1.
You need to make Attribute "Seats" with value of all seat numbers and allow add one quantity for each of Seats values

OpenCart - how to calculate VAT correctly?

Here's an example of the issue at hand:
A product costs 81.25 (that is price including 20% VAT, in other words, 81.25 is equal to 67.71+13.54). I've set the price calculations to the best of my belief (from Extensions->Order Totals) and within Shopping Cart, we'd see the following break-down:
A table of the product with thumbnail, description, quantity, price: and last cell says 81.25. Below that:
Sub-total: 67.71 (that is price without VAT)
+20% VAT: 13.54 (that is the VAT, achieved through Taxes)
-10% Discount: -8.13 (all products over $50 receive 10% discount, that is, 10% off of 81.25)
TOTAL: 73.12
Unfortunately, our customers are very sensitive to VAT, so the calculation should break-down to:
-10% discount: 8.12 (that is 10% off the full price of 81.25)
Sub-total: 60.94 (that would be the full price,81.25, minus the 10% discount or 8.12 first and excluding the 20% VAT second)
+20% VAT: 12.19
TOTAL: 73.12
As you can see, the Total is the same, but the VAT is different. I've tried a different Total Order from the OpenCart admin, but if I start with Discount, it does not have a previous value to discount in the first place and returns 0.
A possible theoretical solution in my mind, perhaps if Total could be duplicated somehow, renamed and inserted hidden in the very first position (so that it would equal 81.25 at the beginning, then discount etc.) that might work. I would really appreciate your ideas.
I see what's Your point. But If I buy something from Your store for the end-price of 73.125 (that is 81.25 - 10% discount) while there is 20% VAT, the price without VAT is 60.9375 and the VAT is indeed 12.1875. This is simply because the discount is applied to the sub-total price (end-price of the product including VAT) and not to the price without VAT.
If You would like to do it Your way, thus subtracting 10% discount from the netto price of the product (excluding VAT), You would have to apply 10% discount on the VAT as well:
price excl. VAT -10% discount: 81.25/1.2 * 0.9 = 67.708333 * 0.9 = 60.9375
VAT = 13.541666 (BUT SEE point 3)
VAT - 10% discount = 13.541666 * 0.9 = 12.1875
Your counting is incorrect simply because if we count Your results (that were very wrongly rounded), we would end up with the final price of 60.9375 (price excl. VAT - 10% discount) + 13.541666 (VAT w/o 10% discount) = 74.4791666 ~ 74.48
AND
74.48 !== 73.12
BUT counting the discounted price + discounted VAT
60.9375 + 12.1875 = 73.125 which is correct result!
I hope now is it clear that OC counts the result correctly while Your idea is wrong...
It should really go
Subtotal (67.71)
Discount (-6.77)
VAT (13.54)
Total (73.12)
The subtotal is actually the "total" you mentioned you wanted to have and rename. It's not possible to have the total show before the taxes etc are applied for the simple fact that the order total is calculated in order of those totals
Uncle Fester, I think you are right.
We have to have in mind two groups of buyers, retail and wholesale.
If you tell a guy in retail that he has 10% discount, it should mean that if the final price (w/ VAT) was, lets say USD 100, he would expect that the discounted price should be USD 90.
On the other hand, for a wholesale customer, it is a bit different. Wholesale customers are interested in prices w/o VAT only, so, a price for him is USD 90 + VAT and when you tell him he has 10% discount, he counts on USD 81 w/o VAT.
Back to the VAT calculation, lets look at the same price of a product in amount of USD 90 w/o VAT and USD 100 w/ VAT. Lets say we are giving 10% discount to both customer groups.
Retail customer is counting on a price of USD 90 (USD 75 + VAT), while
Wholesale customer counts on a price of USD 97.2 (USD 81 + VAT).
So, that is the initial approach to the issue.
Next, something that I do not understand is the following:
basically, any tax is to be paid in a certain percentage of the final sum, dont you agree?
So, if the final price of the product (w/o VAT) is USD 100, then the VAT is USD 20 (if rate is 20%), and final amount is USD 120. If however, one receives 10% discount, his bill is now USD 90 + VAT, or USD 99 is the final sum. In other words, VAT amount in Invoice should always be the exact percentage of the goods that is precised in the law.
If it stays written that it is USD 13,54 - it is NOT 20% of the net, but the gross amount of the goods. In this case, VAT grows to 22,2%!?! And all of you are saying that is how it should be???
I really do not understand this...
There are 2 ways of calculating VAT-
Method 1) Applying VAT on each item
Method 2) Applying VAT on the subtotal
For my country in Malaysia, the VAT has to be calculated using Method 1. Consequently, the accounting (XERO) and billing (TradeGecko) software I use also uses Method 1. Unfortunately, OpenCart uses Method 2, and I am still searching for the solution for OpenCart that allows me to use Method 1.
Method 1 and Method 2 will give you different VAT amount, simply because of where the rounding of the VAT amount is taking place. For Method 1, the rounding of VAT takes place at the item level, while in Method 2, the rounding takes place at the sub-total level.
If only OpenCart has a simple radio button that allows us to choose either one, that should please almost 99% of the users.

Optimal selection for ordering multiple items (parts) from multiple suppliers (vendors)

The task here is to define the optimal (as detailed below) way of ordering items (parts) from suppliers.
The relevant parts of the table schema (with some sample data) are
Items
ID NUMBER
1 Item0001
2 Item0002
3 Item0003
Suppliers
ID NAME DELIVERY DISCOUNT
1 Supplier0001 0 0
2 Supplier0002 0 0.025
3 Supplier0003 20 0
DELIVERY is the delivery charge (in dollars) levied by that supplier on each delivery. DISCOUNT is the settlement discount (as a percentage i.e. 2.5% for ID=2 above) allowed by that supplier for on time payment.
SupplierItems
SUPPLIER_ID ITEM_ID PRICE
1 2 21.67
1 5 45.54
1 7 32.97
This is the many-to-many join between suppliers and items with the price that supplier charges for that item (in dollars). Every item has at least 1 supplier but some have more than one. A supplier may have no items.
PartsRequests
ID ITEM_ID QUANTITY LOCATION_ID ORDER_ID
1 59 4 2 (null)
2 89 5 2 (null)
3 42 4 2 (null)
This table is a request from a field site for parts to be ordered and delivered by the supplier to that site. A delivery of any number of items to a site attracts a delivery charge. When the parts are ordered, the ORDER_ID is inserted into the table so we are only concerned with those where ORDER_ID IS NULL
The question is, what is the optimal way to order these parts for each `LOCATION' where there are 3 optimal solutions that need to be presented to the user for selection.
The combination of orders with the least number of suppliers
The combination of orders with the lowest total cost i.e. The sum of QUANTITY*PRICE for each item plus the DELIVERY for each order summed over all orders ignoring DISCOUNT
As item 2 but accounting for DISCOUNT
Clearly I need to determine the combinations of orders that are available and then determining the optimal ones becomes trivial but I am a bit stuck on an efficient way to deal with building the combinations.
I have built some SQL fiddles in SQL Server 2008 with random data. This one has 100 items, 10 suppliers and 100 requests. This one has 1000 items, 50 suppliers and 250 requests. The table schema is the same.
Update
I reasoned that the solution had to be recursive and I built a nice table valued function to get but I ran into the 32 hard limit on recursion in SQL Server. I was uncomfortable with it anyway because it hinted more of a procedural language solution than a RDMS.
So I am now playing with CTE recursion.
The root query is:
SELECT DISTINCT
'' SOLUTION_ID
,LOCATION_ID
,SUPPLIER_ID
,(subquery I haven't quite worked out) SOLE_SUPPLIER
FROM PartsRequests pr
INNER JOIN
SupplierItems si ON pr.ITEM_ID=si.ITEM_ID
WHERE pr.ORDER_ID IS NULL
This gets all the suppliers that can supply the required items and is certainly a solution, probably not optimal. The subquery sets a flag if the supplier is the sole supplier of any product required for that location; if so they must be part of any solution.
The recursive part is to remove suppliers one by one by means of CTE.SUPPLIER_ID<>CTE.SUPPLIER_ID and add them if they still cover all the items. The SOLUTION_ID will be a CSV list of the suppliers removed, partly to uniquely identify each solution and partly to check against so I get combinations instead of permutations.
Still working on the details, the purpose of this update was to allow the Community to say "Yay, looks like that will work" or, alternatively "You moron, that won't work because ..."
Thanks
This is a more general answer (as in, not sql) as I think solving this problem will require something more powerful. Your first scenario is to select a minimum number of suppliers. This problem can be seen as a set cover problem as you are trying to cover all demands per site with the suppliers. This problem is already NP-complete.
Your third scenario seems to be basically the same as the second. You just have to take the discount into account in the prices, assuming you pay on time for every order.
The second scenario is at least NP-hard as I see a lot of resemblance with the facility location problem. You are trying to decide which suppliers (facilities) to use (open) to cover your orders (demands) based on their prices and delivery costs (opening costs).
Enumerating your possible solutions seems infeasible as with 10 suppliers, you have 2^10 possibilities of using them, further complicated by the distribution of demands internally.
I would suggest some dynamic programming to first select the suppliers that you have to use (=they are the only ones that deliver a specific thing), eliminating some possibilities (if the cost for supplier A +delivery cost A< cost for supplier B) and then trying to expand your set of possible solutions. Linear programming is also a valid train of thought.