Calculate the discount rate - vb.net

I have the original price of a commodity and its discounted price.
I currently have some code but it is not working properly.
For example: The price of a pre-discounted commodity is 853.2 $.
The discounted price is 349 $.
That is a 59% discount, but my code shows the wrong numbers.
Dim a1 As String = (Val(pre-discounted_price) - Val(discount_price) * 100 / Val(discount_price)).ToString
Dim a2 As Integer = (853.2 - 349) * 100 / 349

The formula for calculating how much you need to discount something by, to reach a new price is:
100 * (originalprice - newprice) / originalprice
100 * (852.3 - 349) / 852.3 = 59 (0 decimal places)
Checking:
852.3 - (852.3 * 0.59) = 349 (approx)
Your code went wrong because you divided by the newprice

Related

Profit maximization using non linear optimization in pyomo problem framing

Background
We have 5 items in our portfolio along with the quantity sold, margin and prices of every item from last month. Predicted quantity sold of every item is dependent on its own price (through own price elasticity) and price of every other item (cross price elasticity). I am trying to write an pyomo script to increase prices of every item optimally and maximize the profit.
Example for 5 items after calculating a 5*5 matrix for elasticities.
Predicted quantity sold for every item is following function: Pred(Q1) = exp(p1 * e11 + p2 * e12 + ...... + p5 * e15)
Margin for every item is available.
Current Net profit : P1 * Q1 * Margin1 + ......... + P5 * Q5 * Margin5 = 1000
Objective : Max(P1 * (1+change p1) * Pred(Q1) * Margin 1 + ......... + P5 * (1+change p5) * Pred(Q5) * Margin 5
Constraints:
We can change the price of every item by [-5% , +10%]
Predicted quantity sold of every item with new prices should not change by
more than [-5% , +10%] of current quantities
Maximum profit achieved should be greater than 1000
All prices and quantities should be positive
Need help with programming it in pyomo with n variables and constarints, ipopt is what I think of using but open for recommendation from experts.
#Parameters
model.N = pyo.Param(model.a, initialize = qty_dict)
#Function that returns bounds on variable x
def bounds_for_y(model,l):
return (round(model.N[l]*0.95,0),round(model.N[l]*1.1,0))
#Here l is just a placeholder for set a
#Variable Declaration
model.qty = pyo.Var(model.a, domain = pyo.Integers, bounds = bounds_for_y )
Similarly for price:
#Variable declaration
model.prices = pyo.Var(model.a, domain = pyo.Reals, bounds = bounds_for_x, initialize = price_dict)
enter code here

Multiplication price and profit with 2 number percentage in vb.net

I have a product table where I save the price and the amount of profit.
I want to display the amount of time displayed differently
for example :
price : 8.08
profit : 8
Presentable : 8.08 + (8.08 * 8 / 100) = 8.7264
Now I want the answer to be two decimal places and rounded up.
And the answer should be as follows:
Presentable : 8.08 + (8.08 * 8 / 100) = 8.73
The "C" currency format specifier should do the trick for you.
Dim amount As Decimal = 123.456
System.Console.WriteLine(amount.ToString("C", System.Globalization.CultureInfo.CurrentCulture))
' Prints : $123.46
More info here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#CFormatString
There is a also a Round method, where you can specify to how many decimal places you would like to round the number. So in your case of rounding to 2 decimal places you could make a call like:
System.Math.Round(amount, 2, System.MidPointRounding.AwayFromZero)

How to compute debit, credit amount in Vendor Bill in Odoo?

I am new to Odoo Accounting. I have been encountering a problem in calculating debit and credit amount of Vendor Bill. I have added two new fields in purchase order line, discount_type and discount_amt. The subtotal value must be (price_unit * quantity) - discount. I could compute the subtotal amount. But when I check the journal items, the debit and credit amount are not changed. I mean the discount amount are not subtracted. But when I saved the form, I got the error which said debit and credit were not balanced. How can I do that?
def compute_price_subtotal(self):
for line in self:
line.discount_type = line.purchase_line_id.discount_type
line.discount_amt = line.purchase_line_id.discount_amt
qty = line.quantity or 0
price_unit = line.price_unit or 0
subtotal = price_unit * qty
discount_type = line.discount_type
discount_amount = line.discount_amt
if discount_type == 'fixed':
discount = discount_amount * qty
line.price_subtotal = subtotal - discount
elif discount_type == 'percentage':
discount = subtotal * (discount_amount / 100)
line.price_subtotal = subtotal - discount
else:
line.price_subtotal = subtotal
if line.move_id.type in line.move_id.get_outbound_types():
sign = 1
elif line.move_id.type in line.move_id.get_inbound_types():
sign = -1
else:
sign = 1
price_subtotal = sign * line.price_subtotal
line.update({
'debit': price_subtotal > 0.0 and price_subtotal or 0.0,
'credit': price_subtotal < 0.0 and -price_subtotal or 0.0,
})
The above method is to calculate the price_subtotal, debit and credit.
In the picture, the untaxed amount is 13800 and tax is 690. So the total amount will be 13800 + 690 = 14490. But in the Journal items, it shows 15000 and the subtotal values are different.
This is because you only modify the line you are interested in.
This tries to modify the debit/credit but it is leaving the account move unbalanced.
As this violate a constrains, the modification of the accounting is prevented.
You need to balance the move before updating anything in it.
This involves updating the tax line and the payable/receivable line too.
Once you have all of those lines, you can update the whole account move.
This means that you have to make something like this:
(assuming the calculation is already done)
lines_to_write = [
(1, line_A_id, line_A_values),
(1, tax_line_id, tax_line_values),
(1, receivable_line_id, receivable_line_values)
]
move.write({'line_ids': lines_to_write})
You can get the list of command here
Btw, to recompute the debit and credit when a business field is changed, you can (should) call the method _get_fields_onchange_subtotal_model to get the new values and then to update the account move line with those new values.
One of the reason is the fact that the accounting and the invoice could be in different currency.
Disclaimer: you should modify taxes only if you are sure of what you are doing.
This can have an impact on the user's tax report.

Sum of list item value in GenericList in Vb.net

I have generic list -GasPropList - in that i want the sum of the all individual items value.
All Varaibles are in Double datatype.
For Each gasprop In GasPropList
gasprop.OUTGi = gasprop.NameVal * gasprop.GasGi / 100
gasprop.OUTPci = gasprop.NameVal * gasprop.Pci / 100
gasprop.OUTTci = gasprop.NameVal * gasprop.Tci / 100
gasprop.OUTVi = gasprop.NameVal * gasprop.NHVi / 100
gasprop.OUTGVi = gasprop.NameVal * gasprop.GHDVi / 100
Next
From this i want to calculate sum of OUTGi, OUTPi etc.
I saw Linq Sum function, but don't know how to use for my case.
E.g.
Dim OUTGiSum = GasPropList.Sum(Function(gp) gp.OUTGi)
I'll leave it to you to do the others.

Advice and feedback on dividing cash amounts into actual counts of various bills and coinage

So I need an idea of how to divide out an amount of money into actual counts of various bills and coinage. I know this is confusing, so let me give an example:
$16.32 - Sixteen dollars and thirty-two cents
One $10 bill
One $5 bill
One $1 bill
One Quarter ($0.25)
One Nickel ($0.05)
Two Pennies ($0.01)
So as you can see, we're just getting the number of bills and coinage that goes into a value, which will change according to user input.
Here's my current setup (Visual Basic):
If 100 Mod amount < 0 Then
If 50 Mod amount < 0 Then
' Continue this pattern until you get all the way down to the end ($0.01)
Else
While amount > 50
fiftiesAmount += 1
amount -= 50
End If
Else
While amount > 100
hundredsAmount += 1
amount -= 100
End If
Basically, each If statement determines whether or not your total amount needs an extra billing amount of that type, and then either adds to the amount of bills/coinage already created or moves on to the next amount.
Is this an efficient way of doing things, or am I missing out on an easier/faster algorithm/pattern that would make my life, and whoever is reading my code's life easier?
If you need extra details, I'll be happy to edit the question as needed.
Convert your amount to cents (it's easier). Divide by the currency value being tested, and then deduct that amount from the balance (pseudo-code)
Value = 16.32 * 100 ' Convert to cents
If Value > 10000 ' Hundreds
Hundreds = Value / 10000 ' How many?
Value = Value - (Hundreds * 10000) ' Reduce amount accordingly
End If
If Value > 5000 ' Fifties
Fifties = Value / 5000
Value = Value - (Fifties * 5000)
End If
If Value > 2000 ' Twenties
Twenties = Value / 2000
Value = Value - (Twenties * 2000)
End If
Repeat until you have less than 100, at which point you start with coins (50, 25, 10, 5)
Once you've got > 10, you've reached pennies; save them, reduce Value by that amount, and
Value is zero, so you're finished.