Skip to content
This repository was archived by the owner on Mar 21, 2021. It is now read-only.

10.0 17012018 #95

Open
wants to merge 18 commits into
base: 10.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
b3fec20
[ADD] telegram_expense_manager: currency_id in expenses account.move.…
Jan 26, 2018
c5c51f6
[FIX] telegram_expense_manager: typo
Jan 26, 2018
c6e537c
[ADD] telegram_expense_manager: multicurrency expenses
Jan 29, 2018
7ada112
[ADD] telegram_expense_manager: multicurrency /income
Jan 29, 2018
18e08e6
[ADD] telegram_expense_manager: multicurrency in total row for /accou…
Jan 29, 2018
9feee23
[ADD] telegram_expense_manager: multicurrency /account_all report
Jan 29, 2018
a7b4567
[ADD] telegram_expense_manager: res.currency.alias
Jan 30, 2018
47c113d
[ADD] telegram_expense_manager: use currency aliases to get an expens…
Jan 30, 2018
ed9bec1
[FIX] telegram_expense_manager: base_currency might not have rate 1
Jan 30, 2018
050d398
[ADD] telegram_expense_manager: add em_currency_id on partner's form
Jan 30, 2018
4ac68c0
[ADD] telegram_expense_manager: allow multicurrency by default
Jan 30, 2018
87b2c0a
[FIX] telegram_expense_manager: search currency by name first, if fai…
Jan 30, 2018
ca01be3
[FIX] telegram_expense_manager: fix multicurrency /account_all report
Jan 31, 2018
4874de3
[ADD] telegram_expense_manager: multicurrency expense record report
Jan 31, 2018
e645bd0
[FIX] telegram_expense_manager: take currency from alias if there is …
Feb 6, 2018
e77fd45
[ADD] telegram_expense_manager: case-insensitive currency search
Feb 9, 2018
b3262fd
[FIX] telegram_expense_manager: don't specify base currency in accoun…
Feb 9, 2018
ab00b11
[ADD] telegram_expense_manager: data file for currency aliases
Feb 12, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions telegram_expense_manager/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
"data/account.xml",
"data/analytic.xml",
"data/cron.xml",
"data/res_currency_data.xml",
"views/schedule.xml",
"views/account_account.xml",
"views/res_currency_view.xml",
"views/res_partner_view.xml",
"security/ir.model.access.csv",
"security/base_security.xml",
],
"qweb": [
],
Expand Down
44 changes: 44 additions & 0 deletions telegram_expense_manager/data/res_currency_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<record id="base.RUB" model="res.currency">
<field name="active" eval="True"/>
</record>
<record id="rub_alias_1" model="res.currency.alias">
<field name="name">руб</field>
<field name="currency_id" eval="ref('base.RUB')"/>
</record>
<record id="rub_alias_2" model="res.currency.alias">
<field name="name">руб.</field>
<field name="currency_id" eval="ref('base.RUB')"/>
</record>
<record id="rub_alias_3" model="res.currency.alias">
<field name="name">р.</field>
<field name="currency_id" eval="ref('base.RUB')"/>
</record>
<record id="rub_alias_4" model="res.currency.alias">
<field name="name">рубль</field>
<field name="currency_id" eval="ref('base.RUB')"/>
</record>
<record id="rub_alias_5" model="res.currency.alias">
<field name="name">рубля</field>
<field name="currency_id" eval="ref('base.RUB')"/>
</record>
<record id="rub_alias_6" model="res.currency.alias">
<field name="name">рублей</field>
<field name="currency_id" eval="ref('base.RUB')"/>
</record>
<record id="usd_alias_1" model="res.currency.alias">
<field name="name">доллар</field>
<field name="currency_id" eval="ref('base.USD')"/>
</record>
<record id="usd_alias_2" model="res.currency.alias">
<field name="name">долларов</field>
<field name="currency_id" eval="ref('base.USD')"/>
</record>
<record id="eur_alias_1" model="res.currency.alias">
<field name="name">евро</field>
<field name="currency_id" eval="ref('base.EUR')"/>
</record>
</data>
</odoo>
10 changes: 7 additions & 3 deletions telegram_expense_manager/data/telegram_command.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ if not error:
data['analytic_liquidity'] = analytic_liquidity
data['analytic_payable'] = analytic_payable
data['record'] = record
data['base_currency'] = partner.em_currency_id
data['currency_ids'] = env["account.move.line"].sudo().search([('analytic_account_id', '=', analytic_liquidity.id)]).mapped('currency_id').sorted(key=lambda r: r.id)
</field>
<field name="response_template" type="xml">
<t>
Expand All @@ -98,7 +100,7 @@ if not error:
</t>
<t t-else="">New record is added! Check or edit it here: /record_<t t-esc="data['record'].id"/>
New Balance:
<b t-esc="data['analytic_liquidity'].move_balance"/> — <t t-esc="data['analytic_liquidity'].name"/>
<b><t t-esc="data['analytic_liquidity'].get_currency_balance(base_currency=data['base_currency'])"/></b> (<t t-esc="data['analytic_liquidity'].get_currency_balance(base_currency=data['base_currency'], currency=data['base_currency'])"/> <t t-esc="data['base_currency'].name"/><t t-foreach="data['analytic_liquidity'].currency_ids" t-as="currency"> <t t-esc="data['analytic_liquidity'].get_currency_balance(base_currency=data['base_currency'], currency=currency)"/> <t t-esc="currency.name"/></t>) — <t t-esc="data['analytic_liquidity'].name"/>
</t>
</t>
</field>
Expand Down Expand Up @@ -341,12 +343,14 @@ accounts = env["account.analytic.account"].sudo().search(
order='id ASC',
)
data['accounts'] = accounts
data['base_currency'] = partner.em_currency_id
data['currency_ids'] = env["account.move.line"].sudo().search([('analytic_account_id', 'in', accounts.ids)]).mapped('currency_id').sorted(key=lambda r: r.id)
data['total'] = sum((acc.move_balance for acc in accounts))
</field>
<field name="response_template" type="xml">
<t><t t-foreach="data['accounts']" t-as="acc">
/account_<t t-esc="acc.id"/> — <t t-esc="acc.move_balance"/> — <t t-esc="acc.name"/></t>
<b>Total</b>: <t t-esc="data['total']"/></t>
/account_<t t-esc="acc.id"/> — <t t-esc="acc.get_currency_balance(base_currency=data['base_currency'])"/> (<t t-esc="acc.get_currency_balance(base_currency=data['base_currency'], currency=data['base_currency'])"/> <t t-esc="data['base_currency'].name"/><t t-foreach="acc.currency_ids" t-as="currency"> <t t-esc="acc.get_currency_balance(base_currency=data['base_currency'], currency=currency)"/> <t t-esc="currency.name"/></t>) — <t t-esc="acc.name"/></t>
<b>Total</b>: <t t-esc="data['accounts'].get_currency_balance(base_currency=data['base_currency'])"/> (<t t-esc="data['accounts'].get_currency_balance(base_currency=data['base_currency'], currency=data['base_currency'])"/> <t t-esc="data['base_currency'].name"/> <t t-foreach="data['currency_ids']" t-as="currency"> <t t-esc="data['accounts'].get_currency_balance(base_currency=data['base_currency'], currency=currency)"/> <t t-esc="currency.name"/></t>)</t>
</field>
</record>

Expand Down
2 changes: 2 additions & 0 deletions telegram_expense_manager/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
from . import api
from . import schedule
from . import account_account
from . import res_currency
from . import res_partner
45 changes: 45 additions & 0 deletions telegram_expense_manager/models/account_analytic_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,48 @@ def _compute_move_debit_credit_balance(self):
move_balance = fields.Monetary(compute='_compute_move_debit_credit_balance', string='Balance')
move_debit = fields.Monetary(compute='_compute_move_debit_credit_balance', string='Debit')
move_credit = fields.Monetary(compute='_compute_move_debit_credit_balance', string='Credit')
currency_ids = fields.Many2many("res.currency", "analytic_account_currency_rel")

@api.multi
def _attach_new_currency(self, currency):
for record in self:
if currency not in record.currency_ids:
record.currency_ids = [(4, currency.id)]

@api.multi
def get_currency_balance(self, base_currency=None, currency=None):
# this method runs in sudo() mode - see /account_all telegram command respose_code (acc search)
balance = 0.0
AccountMoveLine = self.env['account.move.line']
domain = [('analytic_account_id', 'in', self.mapped('id'))]

def get_base_currency_sum():
base_currency_sum = 0.0
domain.append(('currency_id', '=', False))
line_ids = AccountMoveLine.search(domain)
base_currency_sum += sum(line_ids.mapped('balance'))
return base_currency_sum

if self._context.get('from_date', False):
domain.append(('date', '>=', self._context['from_date']))
if self._context.get('to_date', False):
domain.append(('date', '<=', self._context['to_date']))

if currency and currency != base_currency:
domain.append(('currency_id', '=', currency.id))
balance = sum(AccountMoveLine.search(domain).mapped('balance'))
elif currency and currency == base_currency:
balance = get_base_currency_sum()
else:
# this is for tatal sum
currency_domain = domain[:]
currency_domain.append(('currency_id', '!=', False))
all_currencies_line_ids = AccountMoveLine.search(currency_domain)
currency_ids = all_currencies_line_ids.mapped('currency_id')
for currency_id in currency_ids:
rate = self.env['res.currency']._get_conversion_rate(base_currency, currency_id)
filtered_line_ids = all_currencies_line_ids.filtered(lambda r: r.currency_id == currency_id)
balance += sum(all_currencies_line_ids.filtered(lambda r: r.currency_id == currency_id).mapped('balance')) * rate
balance += get_base_currency_sum()

return round(balance, 1)
19 changes: 17 additions & 2 deletions telegram_expense_manager/models/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import re

from odoo import models, api, fields
from odoo.exceptions import AccessError
from odoo.tools.translate import _
Expand Down Expand Up @@ -233,10 +235,13 @@ def em_handle_callback_data(self, callback_data, raw_text, add_record=None):
error = None

if callback_data.get('action') == ASK_AMOUNT:
m = re.match(r'([0-9][ +\-\/0-9.,]*) ?([^0-9]*)', raw_text)
amount = m.group(1)
currency = m.group(2)
if not record:
record = add_record('', raw_text)
record = add_record('', amount, currency=currency)
else:
record.em_update_amount(raw_text)
record.em_update_amount(amount, currency=currency)
elif callback_data.get('action') == ASK_NOTE:
record.em_update_note(raw_text)
elif callback_data.get('action') == ASK_ANALYTIC:
Expand Down Expand Up @@ -465,10 +470,20 @@ def _em_add_record(self,
journal_ref, from_data, to_data):

journal = self.env.ref(journal_ref)
base_currency_id = self.em_currency_id

currency_id = currency and (self.env['res.currency'].search([('name', 'ilike', currency)], limit=1) \
or self.env['res.currency.alias'].sudo().search([('name', 'ilike', currency)], limit=1).currency_id)

if currency_id and currency_id != base_currency_id:
analytic_account_lst = [from_data.get('analytic_account_id'), to_data.get('analytic_account_id')]
analytic_account_ids = self.env['account.analytic.account'].browse(analytic_account_lst)
analytic_account_ids._attach_new_currency(currency_id)

common = {
'partner_id': self.id,
'name': text or 'unknown',
'currency_id': currency_id and currency_id != base_currency_id and currency_id.id or None,
}
if isinstance(amount, basestring):
amount = float(amount.replace(',', '.'))
Expand Down
21 changes: 21 additions & 0 deletions telegram_expense_manager/models/res_currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-

from odoo import api, fields, models, tools, _


class CurrencyAlias(models.Model):
_name = "res.currency.alias"

name = fields.Char(string='Currency Alias')
_sql_constraints = [

('unique_name', 'unique (name)', 'The currency alias must be unique!'),
]

currency_id = fields.Many2one('res.currency', string='Currency', readonly=True)


class Currency(models.Model):
_inherit = "res.currency"

alias_ids = fields.One2many('res.currency.alias', 'currency_id', string='Aliases')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

15 changes: 15 additions & 0 deletions telegram_expense_manager/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-

from odoo import api, fields, models, _


class Partner(models.Model):
_inherit = "res.partner"

def _default_em_currency(self):
return self.env.user.company_id.currency_id

em_currency_id = fields.Many2one('res.currency',
string="Base currency for personal expense management",
ondelete='restrict',
default=_default_em_currency)
6 changes: 6 additions & 0 deletions telegram_expense_manager/security/base_security.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<odoo>
<record id="base.group_user" model="res.groups">
<field name="implied_ids" eval="[(4, ref('base.group_multi_currency'))]"/>
</record>
</odoo>
21 changes: 21 additions & 0 deletions telegram_expense_manager/views/res_currency_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_currency_form_inherit_expense_manager" model="ir.ui.view">
<field name="name">res.currency.form.inherit.expense_manager</field>
<field name="model">res.currency</field>
<field name="inherit_id" ref="base.view_currency_form"/>
<field name="arch" type="xml">
<xpath expr="//sheet" position="inside">
<notebook>
<page string="Aliases">
<field name="alias_ids">
<tree string="Currency Aliases" editable="bottom">
<field name="name"/>
</tree>
</field>
</page>
</notebook>
</xpath>
</field>
</record>
</odoo>
17 changes: 17 additions & 0 deletions telegram_expense_manager/views/res_partner_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_partner_form_inherit_expense_manager" model="ir.ui.view">
<field name="name">res.partner.form.inherit.expense_manager</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="//notebook" position="inside">
<page string="Personal Expenses">
<group>
<field name="em_currency_id"/>
</group>
</page>
</xpath>
</field>
</record>
</odoo>