Note: The following bugs were also impacting the e-commerce.
This commit fixes some bugs related to promotion program offering free product:
- Create a promotion program that offers an ipad if you buy 3 of them, so the
fourth is free (reduced in total)
1. Buying 4, getting a free one, removing one, free one remains
- Create a quotation with 3 ipad, save and update promotions, you don't have
a free one, which is 'correct'
- Edit, change the qty to 4 ipad, save, update promotions, you have a free
ipad (reduced from total), still correct
- Edit, change the qty back to 3 ipad, update ptomotions, you still have a
free ipad even if you only bought 3 of them (the way it is designed, the
free ipad should only appears when you order your fourth).
2. Exponential bug: buy a lot of ipad, get a lot of free ipad then set the qty
to a lower amount, you will have a lot more free ipad than you should.
It can even lead to a negative amount (more free ipad than you paid ipad, but
in that case you won't be able to confirm the sale).
- Create a quotation with 100 ipad, you get 31 free ipad (which is incorrect,
see bug 3)
- Edit and change the qty back to 10 ipad and save
- Update promotions, you will have 10 ipad to pay and 10 free ipad resulting
in a null total.
3. Buy X ipad, get Y+Z free instead of Y (lets illustrate with 10)
- Create a quotation with 10 ipad, you get 3 free ipad instead of 2
- Indeed, your first 4 ipad should give you a free one
- Your next 4 ipad should give you a second one (you got 8)
- The last 2 (to reach 10) are not enough to give you a third free ipad
This is because free products are reward lines in the SO (is_reward_line) but
not linked to an applied_coupon_ids
Now, these reward lines will be considered as such and the coupon computing
will return correct results
This closes#1643
Before this commit:
Coupon error on cart would always show "Invalid or Expired coupon code"
instead of a more accurate error (eg: already applied, can't use 2 code..).
Plus it would still show the error on page refresh because of the error being
stored in URL param.
Now:
It will show the exact error and since the error is not stored in URL, it won't
be still displayed on page refresh.
This closes#1601
The default invoicing policy should always be by ordered quantity.
Otherwise, if the user changes the default invoicing policy to
"delivered quantity", coupon won't be applied to the invoice.
opw-749632
Purpose
=======
Currently we are updating the coupon rewards on the fly when creating/modifying/deleting a sales order line.
To avoid multiple searches on res_partners and product to know whether the customer/product is valid on the sale coupon program, we store the applicable customers and products on the program according to the domain defined on it.
This brings 2 issues:
- The customers/products stored on the program are not updated (Was the original spec)
- The checks are numerous and maybe overkill for the given results
Specification
=============
- Don't recompute the reward lines on SO modifications. Add a button to compute the line by hand and make an automatic compute on the SO validation or on the page shop/payment.
- When recomputing the reward lines, make a search on the res_partners and products instead. Now that the action is not triggered so often we can afford to make a search.
- Don't use the fields rule_partners_domain and rule_products_domain. This should be removed in master.
- The field is_public_included is useless now. And we decided to make the coupon available for all the partners if they fit the domain on the program.
Purpose
=======
People doesn't understand that the reward line product is the product that will be used in the discount line, not the product the be rewarded
If they set another product before the program is created, the product they set will be renamed, which is wrong.
Specification
=============
- Don't display the field before it's set
- Make the field readonly on the program form view
Purpose
=======
On a sale coupon program, the allowed partners are the customers by default.
That way, users like the Administrator aren't allowed to use coupon on a fresh database
Specification
=============
Allow all the partners by default and the user restrict the partners as he wishes.
Before this commit:
Coupon Program: Only customers have access to rewards
Promotional Program: Only customers have access to reward and this can be retricted with a domain
After this commit:
Coupon Program: All partners have access to rewards
Promotional Program: All partners have access to reward and this can be retricted with a domain
This is possible that `unlink` is called without any record in self.
In the given case, this is because of
`self.env['sale.order.line'].search([('order_id', 'in', self.ids), ('is_delivery', '=', True)]).unlink()`
in the `undelivery_set` method of the module delivery.
`Unlink` on an empty set is supported, and it should therefore be the case here as well.
This is not because `self.order_id.no_code_promo_program_ids` expects a singleton
for the `self.order_id` part, and this is actually an empty set as `self` is an empty set.
Besides, it makes sense to loop on the lines passed to `self`,
as you could pass several lines, not all having the same
`product_id`, for the above line
`related_program = self.env['sale.coupon.program'].search([('discount_line_product_id', '=', self.product_id.id)])`
opw-746084
The methods delivery_set and delivery_unset are called everywhere (mostly on the ecommerce). The reasons for that is that we do not want several shipping lines on the same order. A better way to achieve this would have been to create the line if it doesn't exist or write on it to modify the 'price_unit' instead.
The side effect of that behavior is:
- A delivery line is set on the order.
- We apply a coupon to get a free shipping
- We go on the payment page. The method _cart_update is called, which calls delivery_set, so delivery_unset. The shipping line is unlinked to create again a new shipping line with the price updated. As the shipping line is removed, a recomputation of the reward lines is made. The program isn't applicable anymore as there's no shipping line on the order. The reward is thus removed.
That way, it's impossible to have a free shipping reward on the ecommerce and achieve the full checkout process.
What's done in this commit:
If a free shipping is applicable by code or if a free shipping can be automatically applied. It will be done with a price of 0.0 if there's no shipping line or the price_unit on the shipping line if set. If the shipping line is removed (every 2 seconds on a checkout process), the reward line is not unlinked and the price is set to 0.0.
That way, it's now possible to make a full flow.
NB: If a line is unlinked on an order, we unlink the reward line if it's the last one set on the order. This is to avoid free shipping line coming back on a new order in the ecommerce even if not applicable.
Purpose
=======
It's hard to find all the menu items dedicated to products & pricing:
- create a product
- set up a discount
- change the pricing
such recurring actions are related to each other but you need to go to different menus: e.g. to edit pricelists you have to go to settings while editing your pricing is a business operation.
Specification
=============
make one menu item like in ecommerce: CATALOG
Also clean reporting and configurion menus a bit...
Details
=======
Sales menu rearrangement :
Dashboard
Sales
Invoicing
Orders to Invoice
Orders to Upsell
Leads Management
Catalog (takes straight to products if no access to variant, pricelist or coupons)
Products (move from Sales)
Product Variants (move from Sales)
Pricelists (move from Config)
Promotion Programs (move from Config)
Coupon Programs (move from Config)
Reporting (reorder items, from leads to sales)
Leads
Scoring Page Views
Opp. Assignment Analysis
Partnerships
Pipeline
Activities
Phonecalls
Sales
Configuration
Settings
Sales Channels (move up)
Leads & Opportunities
Sales
Quotation Templates
Report Layout Categories
Payment Methods (if online quotation installed)
Delivery Methods (move from Config > Delivery)
Products
Attributes
Attribute Values
Internal Categories (rename from Product Categories)
Units of Measure
Unit of Measure Categories
Pricelists (remove)
Delivery (remove)
Use Case
========
In the coupon module, a call to a write, a create or an unlink on a sales order line may trigger a write, a create or an unlink on another line. Example: I have a promotional offer "If you buy for more than 100 euros, you got a 10 percent discount on the sales order. On my sales order, I buy 2 imac at 999 euros. At that moment my total amount is over 1000 euros and I got another line, the discount. I write on the sales order line that I only buy one imac, a unlink is trigger on the reward line to remove it from the sales order.
Current Solution
================
At the beginning, I was overwriting the write, the create and the unlink methods on the sales order line to perform the correct actions according to the promotional offers that were still applicable, not applicable anymore of newly applicable. This solution was followed by side effects on the computed fields. For example, a write on the SO line that was triggering an unlink on another line was triggering a computed field that depended on the sales order line. In that case, the environment I received on the compute method was the environment before the line unlink. As the compute method was looping on the sales order line, it was taking values on a line that wasn't existing anymore and that way it was triggering an 'This record doesn't exist or has been deleted' error.
After a discussion with @rco-odoo, we realized that the only way to achieve this was to override the write method on a sales order to avoid quadratic computation loop when creating/editing/deleting sale order lines. As coupon are recorded as sale.order.line, it may effectively blow up.
We also overrode the create method on a sales order line to make an ORM command on the sales order instead of a simple create on the sales order. See: https://github.com/odoo/enterprise/blob/saas-14/sale_coupon/models/sale_order.py#L253
For the write and the unlink, this is a little more complicated because a write may trigger a write and an unlink may trigger an unlink, resulting in an infinite loop.
New Solution
============
Having a method _recompute_coupon_lines on sale.order. This method do nothing if there is a key coupon_recompute in the context.
```python
@multi
def _recompute_coupon_lines(self):
if self.env.context.get('coupon_recompute'):
return
self = self.with_context(coupon_recompute=True) # avoid loop
[...do actual stuff...]
```
The create/write/unlink method on sale.order.line call super before calling the method _recompute_coupon_lines
```python
@multi
def write(self, values):
result = super(SaleOrderLine, self).write(values)
self.mapped('order_id')._recompute_coupon_lines()
return result
```
In order (pun intended) to accelerate mass creation/edition of lines, the method create/write/unlink of sale.order should call recompute themself
```python
@mutli
def write(self, values):
result = super(SaleOrder, self.with_context(coupon_recompute=True)).write(values)
self._recompute_coupon_lines()
return result
```
Use case
========
If apply base on a product, it takes only the first line of this product !
Eg: 1 free if 3 buy,
- add 2 line of 2 will not work,
- add 1 line of 1 (eg: discounted, descr, ...) and one ligne with 10... will be ignored