11# Copyright 2015-2016 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
22# Copyright 2016 Ilyas Rakhimkulov
33# Copyright 2017 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
4- # Copyright 2016-2018 Dinar Gabbasov <https://it-projects.info/team/GabbasovDinar>
4+ # Copyright 2016-2019 Dinar Gabbasov <https://it-projects.info/team/GabbasovDinar>
55# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
66
77import logging
@@ -18,16 +18,16 @@ class PosConfig(models.Model):
1818 multi_session_id = fields .Many2one ('pos.multi_session' , 'Multi-session' ,
1919 help = 'Set the same value for POSes where orders should be synced.'
2020 'Uncheck the box "Active" if the POS should not use syncing.'
21- 'Before updating you need to close active session' ,
22- default = lambda self : self .env .ref ('pos_multi_session.default_multi_session' , raise_if_not_found = False ))
21+ 'Before updating you need to close active session' )
2322 multi_session_accept_incoming_orders = fields .Boolean ('Accept incoming orders' , default = True )
2423 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' )
2524 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' )
2625 current_session_state = fields .Char (search = '_search_current_session_state' )
2726 sync_server = fields .Char (related = 'multi_session_id.sync_server' )
2827 autostart_longpolling = fields .Boolean (default = False )
2928 fiscal_position_ids = fields .Many2many (related = 'multi_session_id.fiscal_position_ids' )
30- company_id = fields .Many2one (related = 'multi_session_id.company_id' )
29+ company_id = fields .Many2one (related = 'multi_session_id.company_id' , store = True , default = lambda self : self .env .user .company_id )
30+ stock_location_id = fields .Many2one (related = 'multi_session_id.stock_location_id' , store = True )
3131
3232 def _search_current_session_state (self , operator , value ):
3333 ids = map (lambda x : x .id , self .env ["pos.config" ].search ([]))
@@ -45,6 +45,9 @@ def _search_current_session_state(self, operator, value):
4545class PosMultiSession (models .Model ):
4646 _name = 'pos.multi_session'
4747
48+ def _get_default_location (self ):
49+ return self .env ['stock.warehouse' ].search ([('company_id' , '=' , self .env .user .company_id .id )], limit = 1 ).lot_stock_id
50+
4851 name = fields .Char ('Name' )
4952 multi_session_active = fields .Boolean (string = "Active" , help = "Select the checkbox to enable synchronization for POSes" , default = True )
5053 pos_ids = fields .One2many ('pos.config' , 'multi_session_id' , string = 'POSes in Multi-session' )
@@ -56,18 +59,34 @@ class PosMultiSession(models.Model):
5659 "It's used to prevent synchronization of old orders" )
5760 fiscal_position_ids = fields .Many2many ('account.fiscal.position' , string = 'Fiscal Positions' , ondelete = "restrict" )
5861 company_id = fields .Many2one ('res.company' , string = 'Company' , required = True , default = lambda self : self .env .user .company_id )
62+ stock_location_id = fields .Many2one (
63+ 'stock.location' , string = 'Stock Location' ,
64+ domain = [('usage' , '=' , 'internal' )], required = True , default = _get_default_location )
5965
60- @api .multi
66+ @api .model
6167 def action_set_default_multi_session (self ):
6268 """
63- during installation of the module set default multi-session
69+ during installation of the module set default multi-sessions (separate default multi- session for every company)
6470 for all POSes for which multi_session_id is not specified
6571 """
66- self .ensure_one ()
67- configs = self .env ['pos.config' ].search ([('multi_session_id' , '=' , False )])
68- configs .write ({
69- 'multi_session_id' : self .id
70- })
72+ companies = self .env ['res.company' ].search ([])
73+ for company in companies :
74+ configs = self .env ['pos.config' ].search ([('multi_session_id' , '=' , False ), ('company_id' , '=' , company .id )])
75+
76+ # If exist POSes with the company then we need to create default multi-session
77+ if configs :
78+ # Create default multi-session for current company
79+ stock_location = self .env ['stock.warehouse' ].search ([('company_id' , '=' , company .id )], limit = 1 ).lot_stock_id
80+ multi_session = self .create ({
81+ 'name' : 'Default Multi Session (%s)' % company .name ,
82+ 'multi_session_active' : False ,
83+ 'company_id' : company .id ,
84+ 'stock_location_id' : stock_location .id
85+ })
86+ for c in configs :
87+ c .write ({
88+ 'multi_session_id' : multi_session .id
89+ })
7190
7291 @api .multi
7392 def name_get (self ):
0 commit comments