Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚑 access error for poses created for different companies #1147

Open
wants to merge 9 commits into
base: 11.0
Choose a base branch
from
5 changes: 3 additions & 2 deletions pos_multi_session/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"category": "Point Of Sale",
# "live_test_url": 'http://apps.it-projects.info/shop/product/pos-multi-session?version=11.0',
"images": ["images/pos-multi-session.png"],
"version": "11.0.4.2.10",
"version": "11.0.4.2.12",
"application": False,

"author": "IT-Projects LLC, Ivan Yelizariev",
Expand All @@ -28,7 +28,8 @@
"data/pos_multi_session_data.xml",
"security/ir.model.access.csv",
"views/pos_multi_session_views.xml",
"multi_session_view.xml"
"multi_session_view.xml",
"security/ir_rule.xml",
],
"qweb": [
"static/src/xml/pos_multi_session.xml",
Expand Down
15 changes: 3 additions & 12 deletions pos_multi_session/data/pos_multi_session_data.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2018 Dinar Gabbasov <https://it-projects.info/team/GabbasovDinar>
<!-- Copyright 2018-2019 Dinar Gabbasov <https://it-projects.info/team/GabbasovDinar>
License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -->

<odoo noupdate="1">

<record id="default_multi_session" model="pos.multi_session">
<field name="name">Default Multi Session</field>
<field name="multi_session_active">False</field>
</record>

<record id="point_of_sale.pos_config_main" model="pos.config">
<field name="multi_session_id" ref="default_multi_session"/>
</record>

<!-- Default multi session for exist POSes -->
<function model="pos.multi_session" name="action_set_default_multi_session" eval="[[ref('default_multi_session')]]"/>
<!-- Create default multi sessions for each company -->
<function model="pos.multi_session" name="action_set_default_multi_session" />

</odoo>
4 changes: 1 addition & 3 deletions pos_multi_session/demo/demo.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2016 Ilyas Rakhimkulov
Copyright 2016,2018 Dinar Gabbasov <https://it-projects.info/team/GabbasovDinar>
Copyright 2016,2018-2019 Dinar Gabbasov <https://it-projects.info/team/GabbasovDinar>
Copyright 2016 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
Copyright 2017 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -->
Expand All @@ -26,7 +26,6 @@

<record id="partner_demo2" model="res.partner">
<field name="name">Demo User2</field>
<field name="company_id" ref="base.main_company"/>
<field name="customer" eval="False"/>
<field name="email">[email protected]</field>
<field name="street">Avenue des Dessus-de-Lives2, 2</field>
Expand All @@ -38,7 +37,6 @@
<field name="login">demo2</field>
<field name="password">demo2</field>
<field name="signature">--Mr Demo2</field>
<field name="company_id" ref="base.main_company"/>
<field name="groups_id" eval="[(6,0,[ref('base.group_user'), ref('base.group_partner_manager'), ref('point_of_sale.group_pos_user')])]"/>
<field name="image" type="base64" file="base/static/img/user_demo-image.jpg"/>
<field name="pos_security_pin">0410100000013</field>
Expand Down
12 changes: 12 additions & 0 deletions pos_multi_session/doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
`4.2.12`
-------

- **Fix:** Expected singleton error on adding POSes to a multisession

`4.2.11`
--------

- **Fix:** Access error for poses created for different companies
- **Fix:** Error during installation in multi-company mode
- **Improvement:** Company rule for multi-session model

Copy link
Contributor

Choose a reason for hiding this comment

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

😍

`4.2.10`
--------

Expand Down
55 changes: 42 additions & 13 deletions pos_multi_session/models/pos_multi_session_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2015-2016 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
# Copyright 2016 Ilyas Rakhimkulov
# Copyright 2017 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
# Copyright 2016-2018 Dinar Gabbasov <https://it-projects.info/team/GabbasovDinar>
# Copyright 2017,2019 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
# Copyright 2016-2019 Dinar Gabbasov <https://it-projects.info/team/GabbasovDinar>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

import logging
Expand All @@ -18,16 +18,14 @@ class PosConfig(models.Model):
multi_session_id = fields.Many2one('pos.multi_session', 'Multi-session',
help='Set the same value for POSes where orders should be synced.'
'Uncheck the box "Active" if the POS should not use syncing.'
'Before updating you need to close active session',
default=lambda self: self.env.ref('pos_multi_session.default_multi_session', raise_if_not_found=False))
'Before updating you need to close active session')
multi_session_accept_incoming_orders = fields.Boolean('Accept incoming orders', default=True)
multi_session_replace_empty_order = fields.Boolean('Replace empty order', default=True, help='Empty order is deleted whenever new order is come from another POS')
multi_session_deactivate_empty_order = fields.Boolean('Deactivate empty order', default=False, help='POS is switched to new foreign Order, if current order is empty')
current_session_state = fields.Char(search='_search_current_session_state')
sync_server = fields.Char(related='multi_session_id.sync_server')
autostart_longpolling = fields.Boolean(default=False)
fiscal_position_ids = fields.Many2many(related='multi_session_id.fiscal_position_ids')
company_id = fields.Many2one(related='multi_session_id.company_id')

def _search_current_session_state(self, operator, value):
ids = map(lambda x: x.id, self.env["pos.config"].search([]))
Expand All @@ -41,10 +39,28 @@ def _search_current_session_state(self, operator, value):
else:
return [('id', 'in', [])]

@api.multi
def _write(self, vals):
# made to prevent 'expected singleton' errors in *pos.config* constraints
result = False
if 'multi_session_id' in vals:
multi_session_id = vals.get('multi_session_id')
if multi_session_id:
Copy link
Contributor

Choose a reason for hiding this comment

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

Я правильно понимаю, что multi_session_id может быть задан в словаре, но иметь значение вида 0, False или None? Меня тут смутило то, что мы сначала проверяем наличие multi_session_id, потом вызываем .get который не выкинет исключение, если его там нет.

Copy link
Collaborator

Choose a reason for hiding this comment

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

get по привычке наверное Саша написал.

multi_session_id = self.multi_session_id.browse(multi_session_id)
vals['company_id'] = multi_session_id.company_id.id
else:
vals['company_id'] = self.env.user.company_id.id
for config in self:
result = super(PosConfig, config)._write(vals)
return result


class PosMultiSession(models.Model):
_name = 'pos.multi_session'

def _get_default_location(self):
return self.env['stock.warehouse'].search([('company_id', '=', self.env.user.company_id.id)], limit=1).lot_stock_id

name = fields.Char('Name')
multi_session_active = fields.Boolean(string="Active", help="Select the checkbox to enable synchronization for POSes", default=True)
pos_ids = fields.One2many('pos.config', 'multi_session_id', string='POSes in Multi-session')
Expand All @@ -57,17 +73,30 @@ class PosMultiSession(models.Model):
fiscal_position_ids = fields.Many2many('account.fiscal.position', string='Fiscal Positions', ondelete="restrict")
company_id = fields.Many2one('res.company', string='Company', required=True, default=lambda self: self.env.user.company_id)

@api.multi
@api.model
def action_set_default_multi_session(self):
"""
during installation of the module set default multi-session
for all POSes for which multi_session_id is not specified
during installation of the module set default multi-sessions
(separate default multi-session for each company)
for all POSes with not set multi_session_id
"""
self.ensure_one()
configs = self.env['pos.config'].search([('multi_session_id', '=', False)])
configs.write({
'multi_session_id': self.id
})
companies = self.env['res.company'].search([])
for company in companies:
configs = self.env['pos.config'].search([('multi_session_id', '=', False), ('company_id', '=', company.id)])

# If there are POSes with the company then we need to create default multi-session
if configs:
# Create default multi-session for current company
multi_session = self.create({
'name': 'Default Multi Session (%s)' % company.name,
'multi_session_active': False,
'company_id': company.id,
})
# odoo.exceptions.ValidationError: ('Error while validating constraint\n\nExpected singleton: pos.config(1, 2, 3)', None)
for c in configs:
c.write({
'multi_session_id': multi_session.id
})

@api.multi
def name_get(self):
Expand Down
1 change: 1 addition & 0 deletions pos_multi_session/multi_session_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<label for="name" class="oe_edit_only"/>
<h1><field name="name"/></h1>
<group string="Settings">
<field name="company_id"/>
KolushovAlexandr marked this conversation as resolved.
Show resolved Hide resolved
<field name="pos_ids" widget="many2many_tags" domain="[('current_session_state', '!=', 'opened')]" options="{'not_delete': True}"/>
<field name="multi_session_active"/>
<field name="sync_server" placeholder="http://yourhost"/>
Expand Down
12 changes: 12 additions & 0 deletions pos_multi_session/security/ir_rule.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2019 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -->
<odoo>
<record id="pos_multi_session_rule" model="ir.rule">
<field name="name">Multi Session Rule</field>
<field name="model_id" ref="pos_multi_session.model_pos_multi_session"/>
<field name="global" eval="False"/>
<field name="groups" eval="[(6, 0, [ref('base.group_user')])]"/>
<field name="domain_force">[('company_id','child_of',[user.company_id.id])]</field>
</record>
</odoo>
25 changes: 24 additions & 1 deletion pos_multi_session/views/pos_multi_session_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<label for="multi_session_id"/>
<div class="content-group">
<div class="mt16 row">
<field name="multi_session_id" class="col-xs-3 col-md-3" attrs="{'readonly':[('current_session_state', '=', 'opened')]}"></field>
<field name="multi_session_id" class="col-xs-3 col-md-3" attrs="{'readonly':[('current_session_state', '=', 'opened')]}" required="True"></field>
</div>
</div>
</div>
Expand Down Expand Up @@ -88,6 +88,29 @@
</div>
</div>
</xpath>
<xpath expr="//field[@name='journal_id']/../../../../.." position="after">
Copy link
Collaborator

Choose a reason for hiding this comment

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

Попроще точно ничего нельзя сделать?)

<h2 attrs="{'invisible': [('is_installed_account_accountant', '=', True)]}">Accounting</h2>
<div class="row mt16 o_settings_container" attrs="{'invisible': [('is_installed_account_accountant', '=', True)]}">
<div class="col-xs-12 col-md-6 o_setting_box">
<div class="o_setting_right_pane">
<label string="Journal Entries"/>
<div class="text-muted">
Configuration for journal entries of PoS orders
</div>
<div class="content-group">
<div class="row mt16" title="Whenever you close a session, one entry is generated in the following accounting journal for all the orders not invoiced. Invoices are recorded in accounting separately.">
<label string="Sales Journal" for="journal_id" class="col-md-3 o_light_label" widget="selection"/>
<field name="journal_id"/>
</div>
<div class="row" groups="account.group_account_user" title="Get one journal item per product rather than one journal item per receipt line. This works for any anonymous order. If the customer is set on the order, one journal item is created for each receipt line. This option is recommended for an easy review of your journal entries when managing lots of orders.">
<label string="Group Journal Items" for="group_by" class="col-md-3 o_light_label"/>
<field name="group_by"/>
</div>
</div>
</div>
</div>
</div>
</xpath>
</field>
</record>

Expand Down