Browse Source

[ADD] Dividend Engine

this module allow to calculate the amount to give to the cooperator
based on the purcentage to attribute
pull/1/head
houssine 6 years ago
parent
commit
9de43b804b
  1. 2
      easy_my_coop_dividend/__init__.py
  2. 51
      easy_my_coop_dividend/__openerp__.py
  3. 2
      easy_my_coop_dividend/models/__init__.py
  4. 200
      easy_my_coop_dividend/models/dividend.py
  5. 1
      easy_my_coop_dividend/security/ir.model.access.csv
  6. 164
      easy_my_coop_dividend/views/dividend_views.xml

2
easy_my_coop_dividend/__init__.py

@ -0,0 +1,2 @@
# -*- coding: utf8 -*-
from . import models

51
easy_my_coop_dividend/__openerp__.py

@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2013-2018 Open Architects Consulting SPRL.
# Copyright (C) 2013-2018 Coop IT Easy SCRLfs.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
##############################################################################
{
'name': 'Easy My Coop Dividend Engine',
'summary': """
Manage the dividend calculation for a fiscal year.
""",
'description': """
This module allows to calculate the dividend to give to a cooperator base
on the amount of his shares, the percentage allocated and for how long the
shares have been owned on prorata temporis calculation.
""",
'author': 'Houssine BAKKALI, <houssine@coopiteasy.be>',
'license': 'AGPL-3',
'version': '9.0.1.0',
'website': "www.coopiteasy.be",
'category': 'Cooperative Management',
'depends': [
'base',
'web',
'mail',
],
'data': [
'security/ir.model.access.csv',
'views/dividend_views.xml',
]
}

2
easy_my_coop_dividend/models/__init__.py

@ -0,0 +1,2 @@
# -*- coding: utf8 -*-
from . import dividend

200
easy_my_coop_dividend/models/dividend.py

@ -0,0 +1,200 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (C) 2013-2018 Open Architects Consulting SPRL.
# Copyright (C) 2013-2018 Coop IT Easy SCRLfs.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from __future__ import division
from datetime import datetime
import openerp.addons.decimal_precision as dp
from openerp import fields, models, api
class dividend_year(models.Model):
_name = 'dividend.year'
@api.multi
def _compute_dividend_info(self):
res = {}
for dividend in self:
res[dividend.id] = {'grand_total_dividend': 0.0,
'grand_total_taxes': 0.0}
for line in dividend.dividend_ids:
res[dividend.id]['grand_total_dividend'] += line.dividend_amount
res[dividend.id]['grand_total_taxes'] += line.dividend_taxes
return res
name = fields.Char(string='Code')
date_from = fields.Date(string='Date from')
date_to = fields.Date(string='Date to')
# fiscal_year_id = fields.Many2one('account.fiscalyear',
# string='Fiscal year')
percentage = fields.Float(string='Percentage')
withholding_tax = fields.Float(string='Withholding tax')
detailed_dividend_ids = fields.One2many('detailed.dividend.line',
'dividend_year_id',
string='Dividend lines')
dividend_ids = fields.One2many('dividend.line',
'dividend_year_id',
string='Dividend lines')
grand_total_dividend = fields.Float(
compute=_compute_dividend_info,
string='Grand total dividend',
digits_compute=dp.get_precision('Account'))
grand_total_taxes = fields.Float(
compute=_compute_dividend_info,
string='Grand total taxes',
digits_compute=dp.get_precision('Account'))
@api.multi
def compute_dividend(self):
self.ensure_one()
det_div_line_obj = self.env['detailed.dividend.line']
div_line_obj = self.env['dividend.line']
res_partner_obj = self.env['res.partner']
# delete lines if any
detailed_dividend_ids = det_div_line_obj.search([('dividend_year_id', '=', self.id)])
detailed_dividend_ids.unlink()
dividend_ids = div_line_obj.search([('dividend_year_id', '=', self.id)])
dividend_ids.unlink()
partner_ids = res_partner_obj.search([
('cooperator', '=', True),
('member', '=', True)],
order='cooperator_register_number')
number_of_days = (datetime.strptime(self.date_to, '%Y-%m-%d')
- datetime.strptime(self.date_from, '%Y-%m-%d')
).days + 1
print number_of_days
for partner in partner_ids:
total_amount_dividend = 0.0
for line in partner.share_ids:
vals = {}
vals2 = {}
line_id = False
if line.effective_date >= self.date_from \
and line.effective_date <= self.date_to:
date_res = (datetime.strptime(self.date_to, '%Y-%m-%d')
- datetime.strptime(line.effective_date, '%Y-%m-%d')).days
coeff = (date_res / number_of_days) * self.percentage
dividend_amount = line.total_amount_line * coeff
vals['days'] = date_res
vals['dividend_year_id'] = self.id
vals['coop_number'] = line.partner_id.cooperator_register_number
vals['partner_id'] = partner.id
vals['share_line_id'] = line.id
vals['coeff'] = coeff
vals['dividend_amount'] = dividend_amount
total_amount_dividend += dividend_amount
line_id = det_div_line_obj.create(vals)
elif line.effective_date < self.date_from:
dividend_amount = line.total_amount_line * self.percentage
vals['days'] = number_of_days
vals['dividend_year_id'] = self.id
vals['coop_number'] = line.partner_id.cooperator_register_number
vals['partner_id'] = partner.id
vals['share_line_id'] = line.id
vals['coeff'] = self.percentage
vals['dividend_amount'] = dividend_amount
total_amount_dividend += dividend_amount
line_id = det_div_line_obj.create(vals)
if line_id:
vals2['coop_number'] = line.partner_id.cooperator_register_number
vals2['dividend_year_id'] = self.id
vals2['partner_id'] = line.partner_id.id
vals2['dividend_amount_net'] = total_amount_dividend
vals2['dividend_amount'] = total_amount_dividend
# TODO set as a parameter on dividend year object
if total_amount_dividend <= 190.00:
vals2['dividend_taxes'] = 0.0
else:
div_tax = (total_amount_dividend - 190) * self.withholding_tax
vals2['dividend_taxes'] = div_tax
vals2['dividend_amount_net'] = total_amount_dividend - div_tax
div_line_obj.create(vals2)
return True
class DetailedDividendLine(models.Model):
_name = 'detailed.dividend.line'
@api.multi
def _compute_total_line(self):
res = {}
for line in self:
res[line.id] = line.share_unit_price * line.share_number
return res
dividend_year_id = fields.Many2one('dividend.year',
string='Dividend year')
coop_number = fields.Integer(string='Cooperator Number')
days = fields.Integer(string='Days')
partner_id = fields.Many2one('res.partner',
string='Cooperator',
readonly=True)
share_line_id = fields.Many2one('share.line',
string='Share line',
readonly=True)
share_number = fields.Integer(related='share_line_id.share_number',
string='Number of Share')
share_unit_price = fields.Float(related='share_line_id.share_unit_price',
string='Share unit price')
effective_date = fields.Date(related='share_line_id.effective_date',
string='Effective date')
total_amount_line = fields.Float(compute=_compute_total_line,
string="Total value of share",
readonly=True)
coeff = fields.Float(string='Coefficient to apply',
digits=(2, 4))
dividend_amount = fields.Float(string='Gross Dividend')
dividend_amount_net = fields.Float(string='Dividend net')
dividend_taxes = fields.Float(string='Taxes')
class dividend_line(models.Model):
_name = 'dividend.line'
@api.multi
def _get_account_number(self):
res = {}
for line in self:
bank_accounts = self.env['res.partner.bank'].search(
[('partner_id', '=', line.partner_id.id)])
res[line.id] = bank_accounts[0].acc_number
return res
coop_number = fields.Integer(string='Coop Number')
dividend_year_id = fields.Many2one('dividend.year',
string='Dividend year')
partner_id = fields.Many2one('res.partner',
string='Cooperator',
readonly=True)
account_number = fields.Char(compute=_get_account_number,
string='Account Number')
dividend_amount = fields.Float(string='Gross Dividend')
dividend_amount_net = fields.Float(string='Dividend net')
dividend_taxes = fields.Float(string='Taxes')

1
easy_my_coop_dividend/security/ir.model.access.csv

@ -0,0 +1 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink

164
easy_my_coop_dividend/views/dividend_views.xml

@ -0,0 +1,164 @@
<odoo>
<data>
<menuitem name="Dividend" id="menu_easy_my_coop_main_dividend" parent="easy_my_coop.menu_main_easy_my_coop" sequence="10" />
<record id="dividend_year_form" model="ir.ui.view">
<field name="name">dividend.year.form</field>
<field name="model">dividend.year</field>
<field name="arch" type="xml">
<form string="Dividend year">
<header>
<button name="compute_dividend" string="Compute dividend" type="object" class="oe_highlight" groups="base.group_user"/>
</header>
<sheet>
<group>
<group>
<field name="name"/>
<field name="percentage"/>
<field name="grand_total_dividend"/>
<field name="grand_total_taxes"/>
</group>
<group>
<field name="date_from"/>
<field name="date_to"/>
<field name="withholding_tax"/>
</group>
</group>
<notebook>
<page string="Dividend line">
<field name="dividend_ids">
<tree>
<field name="coop_number"/>
<field name="partner_id"/>
<field name="dividend_amount" sum="Total dividend"/>
<field name="dividend_amount_net" sum="Total dividend net"/>
<field name="dividend_taxes" sum="Total dividend taxes"/>
</tree>
</field>
</page>
<page string="Detailed dividend line">
<field name="detailed_dividend_ids">
<tree>
<field name="coop_number"/>
<field name="partner_id"/>
<field name="share_number"/>
<field name="share_unit_price"/>
<field name="effective_date"/>
<field name="days"/>
<field name="total_amount_line"/>
<field name="coeff"/>
<field name="dividend_amount" string="Dividend" sum="Total dividend"/>
</tree>
</field>
</page>
</notebook>
</sheet>
</form>
</field>
</record>
<record id="dividend_year_tree" model="ir.ui.view">
<field name="name">dividend.year.tree</field>
<field name="model">dividend.year</field>
<field name="arch" type="xml">
<tree string="Dividend year">
<field name="name"/>
<field name="percentage"/>
</tree>
</field>
</record>
<record id="dividend_year_action" model="ir.actions.act_window">
<field name="name">Dividend year</field>
<field name="res_model">dividend.year</field>
<field name="view_type">form</field>
<field name="view_id" ref="dividend_year_tree"/>
</record>
<menuitem action="dividend_year_action" name="Dividend" id="menu_dividend_year" parent="menu_easy_my_coop_main_dividend" sequence="10"/>
<record id="view_detailed_dividend_line_filter" model="ir.ui.view">
<field name="name">detailed.dividend.line.select</field>
<field name="model">detailed.dividend.line</field>
<field name="arch" type="xml">
<search string="Search detailed dividend line">
<field name="partner_id"/>
<field name="coop_number"/>
<filter name="dividend_year_id" string="Dividend Year"/>
<separator/>
<filter string="Partner" domain="[]" context="{'group_by':'partner_id'}"/>
<filter string="Dividend Year" domain="[]" context="{'group_by':'dividend_year_id'}"/>
</search>
</field>
</record>
<record id="detailed_dividend_line_tree" model="ir.ui.view">
<field name="name">detailed.dividend.line.tree</field>
<field name="model">detailed.dividend.line</field>
<field name="arch" type="xml">
<tree string="Detailed dividend lines">
<field name="dividend_year_id"/>
<field name="coop_number"/>
<field name="partner_id"/>
<field name="share_number"/>
<field name="share_unit_price"/>
<field name="effective_date"/>
<field name="days"/>
<field name="total_amount_line"/>
<field name="coeff"/>
<field name="dividend_amount" string="Dividend" sum="Total dividend"/>
</tree>
</field>
</record>
<record id="detailed_dividend_line_action" model="ir.actions.act_window">
<field name="name">Detailed dividend lines</field>
<field name="res_model">detailed.dividend.line</field>
<field name="view_type">form</field>
<field name="view_id" ref="detailed_dividend_line_tree"/>
</record>
<menuitem action="detailed_dividend_line_action" name="Detailed dividend lines" id="menu_det_dividend_line" parent="menu_easy_my_coop_main_dividend" sequence="20"/>
<record id="view_dividend_line_filter" model="ir.ui.view">
<field name="name">dividend.line.select</field>
<field name="model">dividend.line</field>
<field name="arch" type="xml">
<search string="Search Subscription">
<field name="partner_id"/>
<field name="coop_number"/>
<filter name="dividend_year_id" string="Dividend Year"/>
<separator/>
<group expand="0" string="Group By...">
<filter string="Partner" domain="[]" context="{'group_by':'partner_id'}"/>
<filter string="Dividend Year" domain="[]" context="{'group_by':'dividend_year_id'}"/>
</group>
</search>
</field>
</record>
<record id="dividend_line_tree" model="ir.ui.view">
<field name="name">dividend.line.tree</field>
<field name="model">dividend.line</field>
<field name="arch" type="xml">
<tree string="dividend lines">
<field name="dividend_year_id"/>
<field name="coop_number"/>
<field name="partner_id"/>
<field name="account_number"/>
<field name="dividend_amount" sum="Total dividend"/>
<field name="dividend_amount_net" sum="Total dividend net"/>
<field name="dividend_taxes" sum="Total dividend taxes"/>
</tree>
</field>
</record>
<record id="dividend_line_action" model="ir.actions.act_window">
<field name="name">Dividend lines</field>
<field name="res_model">dividend.line</field>
<field name="view_type">form</field>
<field name="view_id" ref="dividend_line_tree"/>
</record>
<menuitem action="dividend_line_action" name="Dividend lines" id="menu_dividend_line" parent="menu_easy_my_coop_main_dividend" sequence="30"/>
</data>
</odoo>
Loading…
Cancel
Save