[FIX] pos_sale: add margin for pos sales in sales report

Current behavior:
In the sales report of a product, the margin was not appearing
for product that were sold in the PoS.

Steps to reproduce:
- Activate the margin option in the settings
- Create a product available in PoS, the product should have a price
  and cost set
- Start a PoS session and sell the product, then close the PoS
- Go on the product page, click the "Sold" smart button
- Add the "Margin" measure to the pivot
- There is no value in the margin column

opw-2937616

closes odoo/odoo#101168

X-original-commit: b230194d8f6a93d374a31f05e74c8fb26277d905
Signed-off-by: Trinh Jacky (trj) <trj@odoo.com>
Signed-off-by: Engels Robin (roen) <roen@odoo.com>
This commit is contained in:
roen-odoo
2022-09-26 18:00:06 +02:00
parent d80b8cbc28
commit 8dbc9f5415
7 changed files with 99 additions and 4 deletions
+13 -4
View File
@@ -75,13 +75,22 @@ class SaleReport(models.Model):
AS discount_amount,
NULL AS order_id"""
additional_fields_info = self._select_additional_fields()
additional_fields = self._select_additional_fields()
additional_fields_info = self._fill_pos_fields(additional_fields)
template = """,
NULL AS %s"""
for fname in additional_fields_info.keys():
select_ += template % fname
%s AS %s"""
for fname, value in additional_fields_info.items():
select_ += template % (value, fname)
return select_
def _fill_pos_fields(self, additional_fields):
"""Hook to fill additional fields for the pos_sale.
:param values: dictionary of values to fill
:type values: dict
"""
return {x: 'NULL' for x in additional_fields}
def _from_pos(self):
return """
pos_order_line l
+4
View File
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import report
+18
View File
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
{
'name': 'pos_sale_margin',
'version': '1.1',
'category': 'Hidden',
'summary': 'Link module between Point of Sale and Sales Margin',
'description': """
This module adds enable you to view the margin of your Point of Sale orders in the Sales Margin report.
""",
'depends': ['pos_sale', 'sale_margin'],
'installable': True,
'auto_install': True,
'license': 'LGPL-3',
}
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import sale_report
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models
class SaleReport(models.Model):
_inherit = "sale.report"
def _fill_pos_fields(self, additional_fields):
values = super()._fill_pos_fields(additional_fields)
values['margin'] = 'SUM(l.price_subtotal - l.total_cost / CASE COALESCE(pos.currency_rate, 0) WHEN 0 THEN 1.0 ELSE pos.currency_rate END)'
return values
+4
View File
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import test_pos_sale_margin_report
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import odoo
from odoo.addons.point_of_sale.tests.common import TestPoSCommon
@odoo.tests.tagged('post_install', '-at_install')
class TestPoSSaleMarginReport(TestPoSCommon):
def setUp(self):
super(TestPoSSaleMarginReport, self).setUp()
self.config = self.basic_config
def test_pos_sale_margin_report(self):
product1 = self.create_product('Product 1', self.categ_basic, 150, standard_price=50)
self.open_new_session()
session = self.pos_session
self.env['pos.order'].create({
'session_id': session.id,
'lines': [(0, 0, {
'name': "OL/0001",
'product_id': product1.id,
'price_unit': 450,
'discount': 5.0,
'qty': 1.0,
'price_subtotal': 150,
'price_subtotal_incl': 150,
'total_cost': 50,
}),],
'amount_total': 150.0,
'amount_tax': 0.0,
'amount_paid': 0.0,
'amount_return': 0.0,
})
# PoS Orders have negative IDs to avoid conflict, so reports[0] will correspond to the newest order
reports = self.env['sale.report'].sudo().search([('product_id', '=', product1.id)], order='id')
self.assertEqual(reports[0].margin, 100)