Skip to content

Commit

Permalink
Enhance pos_margin module with v12 version
Browse files Browse the repository at this point in the history
  • Loading branch information
fjg committed Nov 19, 2020
1 parent d75b7f6 commit c376b08
Show file tree
Hide file tree
Showing 96 changed files with 4,633 additions and 22 deletions.
7 changes: 5 additions & 2 deletions pos_margin/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@

{
'name': 'POS Margin',
'version': '9.0.1.0.0',
'version': '9.0.2.0.0',
'category': 'Point Of Sale',
'sequence': 1,
'author': "GRAP,"
'author': "Adaptation pour Supercoop (basée sur la v12),"
"GRAP,"
"Odoo Community Association (OCA)",
'summary': 'Margin on PoS Order',
'depends': [
'point_of_sale',
'sale_margin',
],
'data': [
'views/view_pos_order.xml',
'report/report_pos_order_margin.xml',
],
'installable': True,
}
1 change: 1 addition & 0 deletions pos_margin/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from . import pos_order
from . import pos_order_line
from . import report_pos_order
31 changes: 27 additions & 4 deletions pos_margin/models/pos_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,37 @@ class PosOrder(models.Model):

# Columns Section
margin = fields.Float(
'Margin', compute='_compute_margin', store=True,
'Margin',
compute='_compute_margin',
store=True,
digits_compute=dp.get_precision('Product Price'),
help="It gives profitability by calculating the difference between"
" the Unit Price and the cost price.")
" the Unit Price and the cost price."
)

margin_percent = fields.Float(
'Margin (%)',
compute='_compute_margin',
store=True,
digits_compute=dp.get_precision('Product Price')
)

# # Compute Section
# @api.multi
# @api.depends('lines.margin')
# def _compute_margin(self):
# for order in self:
# order.margin = sum(order.mapped('lines.margin'))

# Compute Section
@api.multi
@api.depends('lines.margin')
@api.depends('lines.margin', 'lines.price_subtotal')
def _compute_margin(self):
for order in self:
order.margin = sum(order.mapped('lines.margin'))
tmp_margin = sum(order.mapped('lines.margin'))
tmp_price_subtotal = sum(order.mapped('lines.price_subtotal'))
order.update({
'margin': tmp_margin,
'margin_percent': tmp_price_subtotal and (
tmp_margin / tmp_price_subtotal * 100) or 0.0,
})
53 changes: 41 additions & 12 deletions pos_margin/models/pos_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,53 @@ class PosOrderLine(models.Model):

# Columns Section
margin = fields.Float(
'Margin', compute='_compute_multi_margin', store=True,
'Margin',
compute='_compute_multi_margin',
store=True,
multi='multi_margin',
digits_compute=dp.get_precision('Product Price'))
digits_compute=dp.get_precision('Product Price')
)

margin_percent = fields.Float(
'Margin (%)',
compute='_compute_multi_margin',
store=True,
multi='multi_margin',
digits_compute=dp.get_precision('Product Price'),
)

purchase_price = fields.Float(
'Cost Price', compute='_compute_multi_margin', store=True,
'Cost Price',
compute='_compute_multi_margin',
store=True,
multi='multi_margin',
digits_compute=dp.get_precision('Product Price'))
digits_compute=dp.get_precision('Product Price')
)

# Compute Section
@api.multi
@api.depends('product_id', 'qty', 'price_subtotal')
def _compute_multi_margin(self):
for line in self:
if not line.product_id:
line.purchase_price = 0
line.margin = 0
else:
line.purchase_price = line.product_id.standard_price
line.margin = line.price_subtotal - (
line.product_id.standard_price * line.qty)
for line in self.filtered('product_id'):
purchase_price = self._get_purchase_price(line)
margin = line.price_subtotal - (purchase_price * line.qty)
line.update({
'purchase_price': purchase_price,
'margin': margin,
'margin_percent': line.price_subtotal and (
margin / line.price_subtotal * 100.0),
})
# for line in self:
# if not line.product_id:
# line.purchase_price = 0
# line.margin = 0
# else:
# line.purchase_price = line.product_id.standard_price
# line.margin = line.price_subtotal - (
# line.product_id.standard_price * line.qty)

@api.model
def _get_purchase_price(self, line):
# We call _get_purchase_price from sale_margin module, to reuse
# computation that handles multi currencies context and UoM
SaleOrderLine = self.env['sale.order.line']
64 changes: 64 additions & 0 deletions pos_margin/models/report_pos_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
from openerp import fields, models
from openerp import tools


class ReportPosOrder(models.Model):
_inherit = "report.pos.order"

margin_total = fields.Float(string='Margin Total')
margin_rate = fields.Float(string='Margin Rate', group_operator='avg')

def init(self, cr):
tools.drop_view_if_exists(cr, 'report_pos_order')
cr.execute("""
create or replace view report_pos_order as (
select
min(l.id) as id,
count(*) as nbr,
s.date_order as date,
s.id as order_id,
sum(l.qty) as product_qty,
sum(l.price_subtotal) as price_total_vat_excl,
sum(l.qty * l.price_unit) as price_sub_total,
sum((l.qty * l.price_unit) * (100 - l.discount) / 100)
as price_total,
sum((l.qty * l.price_unit) * (l.discount / 100))
as total_discount,
(sum(l.qty*l.price_unit)/sum(l.qty * u.factor))::decimal
as average_price,
sum(cast(to_char(date_trunc('day',s.date_order)
- date_trunc('day',s.create_date),'DD') as int))
as delay_validation,
s.partner_id as partner_id,
s.state as state,
s.user_id as user_id,
s.location_id as location_id,
s.company_id as company_id,
s.sale_journal as journal_id,
l.product_id as product_id,
pt.categ_id as product_categ_id,
p.product_tmpl_id,
ps.config_id,
pt.pos_categ_id,
pc.stock_location_id,
s.pricelist_id,
s.invoice_id IS NOT NULL AS invoiced,
sum(l.margin) as margin_total,
(sum(l.margin / nullif(l.qty,0)) * 100 /
sum(nullif(l.purchase_price,0)))::decimal as margin_rate
from pos_order_line as l
left join pos_order s on (s.id=l.order_id)
left join product_product p on (l.product_id=p.id)
left join product_template pt on (p.product_tmpl_id=pt.id)
left join product_uom u on (u.id=pt.uom_id)
left join pos_session ps on (s.session_id=ps.id)
left join pos_config pc on (ps.config_id=pc.id)
group by
s.id, s.date_order, s.partner_id,s.state, pt.categ_id,
s.user_id,s.location_id,s.company_id,s.sale_journal,
s.pricelist_id,s.invoice_id,l.product_id,s.create_date,
pt.categ_id,pt.pos_categ_id,p.product_tmpl_id,
ps.config_id,pc.stock_location_id
having
sum(l.qty * u.factor) != 0)""")
16 changes: 16 additions & 0 deletions pos_margin/report/report_pos_order_margin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<odoo>
<record id="view_report_pos_order_pivot_margin_inherit" model="ir.ui.view">
<field name="name">report.pos.order.pivot.inherit</field>
<field name="model">report.pos.order</field>
<field name="inherit_id" ref="point_of_sale.view_report_pos_order_pivot"/>
<field name="arch" type="xml">
<field name="price_total" position="after">
<field name="margin_total" type="measure"/>
<field name="margin_rate" type="measure"/>
</field>
</field>
</record>
</odoo>
</openerp>
20 changes: 16 additions & 4 deletions pos_margin/views/view_pos_order.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,27 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<field name="model">pos.order</field>
<field name="inherit_id" ref="point_of_sale.view_pos_pos_form"/>
<field name="arch" type="xml">
<xpath expr="//group[@name='order_total']" position="after">
<group name="margin">
<field name="margin" widget='monetary'/>
</group>
<xpath expr="//group[@name='order_total']/field[@name='amount_total']" position="after">
<field name="margin" widget='monetary'/>
<field name="margin_percent" widget="monetary"/>
</xpath>
<xpath expr="//field[@name='lines']/tree/field[@name='price_unit']" position="after">
<field name="purchase_price"/>
<field name="margin"/>
<field name="margin_percent"/>
</xpath>
</field>
</record>

<record id="view_pos_order_tree" model="ir.ui.view">
<field name="model">pos.order</field>
<field name="inherit_id" ref="point_of_sale.view_pos_order_tree"/>
<field name="arch" type="xml">
<field name="amount_total" position="before">
<field name="margin" widget="monetary" sum="Total"/>
<field name="margin_percent" widget="monetary"/>
</field>
</field>
</record>

</data></openerp>
19 changes: 19 additions & 0 deletions sale_margin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from functools import partial
import openerp
from openerp import api, SUPERUSER_ID

import sale_margin # noqa
import report # noqa


def uninstall_hook(cr, registry):
def recreate_view(dbname):
db_registry = openerp.modules.registry.RegistryManager.new(dbname)
with api.Environment.manage(), db_registry.cursor() as cr:
env = api.Environment(cr, SUPERUSER_ID, {})
if 'sale.report' in env:
env['sale.report'].init()

cr.after("commit", partial(recreate_view, cr.dbname))
22 changes: 22 additions & 0 deletions sale_margin/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

{
'name': 'Margins in Sales Orders',
'version':'1.0',
'category': 'Sales',
'description': """
This module adds the 'Margin' on sales order.
=============================================
This gives the profitability by calculating the difference between the Unit
Price and Cost Price.
""",
'author':'OpenERP SA',
'depends':['sale'],
'demo':['sale_margin_demo.xml'],
'test': ['test/sale_margin.yml'],
'data':['security/ir.model.access.csv','sale_margin_view.xml'],
'auto_install': False,
'installable': True,
'uninstall_hook': "uninstall_hook",
}
52 changes: 52 additions & 0 deletions sale_margin/i18n/af.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * sale_margin
#
# Translators:
msgid ""
msgstr ""
"Project-Id-Version: Odoo 9.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2015-09-07 14:41+0000\n"
"PO-Revision-Date: 2015-08-25 10:24+0000\n"
"Last-Translator: <>\n"
"Language-Team: Afrikaans (http://www.transifex.com/odoo/odoo-9/language/af/)\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Language: af\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#. module: sale_margin
#: model:ir.model.fields,field_description:sale_margin.field_sale_order_line_purchase_price
msgid "Cost"
msgstr "Koste"

#. module: sale_margin
#: model:ir.model.fields,help:sale_margin.field_sale_order_margin
msgid ""
"It gives profitability by calculating the difference between the Unit Price "
"and the cost."
msgstr ""

#. module: sale_margin
#: model:ir.model.fields,field_description:sale_margin.field_sale_order_line_margin
#: model:ir.model.fields,field_description:sale_margin.field_sale_order_margin
#: model:ir.model.fields,field_description:sale_margin.field_sale_report_margin
msgid "Margin"
msgstr ""

#. module: sale_margin
#: model:ir.model,name:sale_margin.model_sale_order
msgid "Sales Order"
msgstr ""

#. module: sale_margin
#: model:ir.model,name:sale_margin.model_sale_order_line
msgid "Sales Order Line"
msgstr ""

#. module: sale_margin
#: model:ir.model,name:sale_margin.model_sale_report
msgid "Sales Orders Statistics"
msgstr ""
Loading

0 comments on commit c376b08

Please sign in to comment.