flectra/addons/base_branch_company/models/res_branch.py

168 lines
5.8 KiB
Python
Raw Normal View History

2018-01-16 13:51:36 +01:00
# -*- coding: utf-8 -*-
from flectra import api, fields, models
2018-06-29 11:57:41 +02:00
from flectra import SUPERUSER_ID
2018-01-29 12:35:08 +01:00
# from flectra.exceptions import ValidationError
2018-01-16 13:51:36 +01:00
2018-06-29 11:57:41 +02:00
def migrate_company_branch(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})
company = env.ref('base.main_company')
company.write({'branch_id': env.ref('base_branch_company.data_branch_1').id})
cr.commit()
user_ids = env['res.users'].search([])
for user_id in user_ids:
if not user_id.user_has_groups('base_branch_company.group_multi_branch'):
user_id.sudo().write({'default_branch_id': user_id.company_id.branch_id.id,
'branch_ids': [(6, 0, [user_id.company_id.branch_id.id])]})
cr.commit()
2018-01-16 13:51:36 +01:00
2018-01-16 13:51:36 +01:00
class Company(models.Model):
_name = "res.company"
_inherit = ["res.company"]
branch_id = fields.Many2one('res.branch', 'Branch', ondelete="cascade")
2018-01-16 13:51:36 +01:00
@api.model
def create(self, vals):
branch = self.env['res.branch'].create({
'name': vals['name'],
'code': vals['name'],
})
vals['branch_id'] = branch.id
self.clear_caches()
company = super(Company, self).create(vals)
branch.write({'partner_id': company.partner_id.id,
'company_id': company.id})
return company
2018-01-29 12:35:08 +01:00
2018-01-16 13:51:36 +01:00
class ResBranch(models.Model):
_name = "res.branch"
name = fields.Char(string='Name', required=True)
code = fields.Char(string='Code', required=True)
active = fields.Boolean(string='Active', default=True)
partner_id = fields.Many2one('res.partner', string='Partner',
ondelete='restrict')
company_id = fields.Many2one(
'res.company', string="Company",
default=lambda self: self.env.user.company_id, required=True)
street = fields.Char()
street2 = fields.Char()
zip = fields.Char(change_default=True)
city = fields.Char()
state_id = fields.Many2one("res.country.state", string='State',
ondelete='restrict')
country_id = fields.Many2one('res.country', string='Country',
ondelete='restrict')
email = fields.Char()
phone = fields.Char()
mobile = fields.Char()
2018-01-29 12:35:08 +01:00
_sql_constraints = [('branch_code_company_uniq',
'unique (code,company_id)',
2018-01-16 13:51:36 +01:00
'The branch code must be unique per company!')]
@api.model
def create(self, vals):
res = super(ResBranch, self).create(vals)
vals.pop("name", None)
vals.pop("code", None)
vals.pop("partner_id", None)
vals.update({'branch_id': res.id})
res.partner_id.write(vals)
return res
@api.multi
def write(self, vals):
res = super(ResBranch, self).write(vals)
vals.pop("name", None)
vals.pop("code", None)
vals.pop("company_id", None)
vals.pop("partner_id", None)
ctx = self.env.context.copy()
if 'branch' not in ctx:
for record in self:
record.partner_id.write(vals)
2018-01-16 13:51:36 +01:00
return res
class Users(models.Model):
_inherit = "res.users"
@api.model
def branch_default_get(self, user):
if not user:
user = self._uid
branch_id = self.env['res.users'].browse(user).default_branch_id
if not branch_id:
branch_id = \
self.env['res.users'].browse(user).company_id.branch_id
return branch_id
@api.model
def _get_branch(self):
return self.env.user.default_branch_id
2018-01-16 13:51:36 +01:00
@api.model
def _get_default_branch(self):
return self.branch_default_get(self._uid)
def _branches_count(self):
return self.env['res.branch'].sudo().search_count([])
2018-01-16 13:51:36 +01:00
branch_ids = fields.Many2many('res.branch',
'res_branch_users_rel',
'user_id',
'branch_id',
'Branches', default=_get_branch,
domain="[('company_id','=',company_id)]")
2018-01-16 13:51:36 +01:00
default_branch_id = fields.Many2one('res.branch', 'Default branch',
default=_get_branch,
2018-01-16 13:51:36 +01:00
domain="[('company_id','=',company_id)"
"]")
2018-01-29 12:35:08 +01:00
branches_count = fields.Integer(
compute='_compute_branches_count',
string="Number of Companies", default=_branches_count)
2018-06-29 11:57:41 +02:00
@api.onchange('company_id')
def _onchange_company_id(self):
if self.company_id.branch_id:
self.default_branch_id = self.company_id.branch_id.id
self.branch_ids = [(4, self.company_id.branch_id.id)]
2018-06-29 11:57:41 +02:00
2018-01-29 12:35:08 +01:00
# To do : Check with all base module test cases
# @api.multi
# @api.constrains('default_branch_id', 'branch_ids')
# def _check_branches(self):
# for user in self:
# if user.branch_ids \
# and user.default_branch_id not in user.branch_ids:
# raise ValidationError(_('The selected Default Branch (%s) '
# 'is not in the Branches!') % (
# user.default_branch_id.name))
2018-01-16 13:51:36 +01:00
@api.multi
def _compute_branches_count(self):
branches_count = self._branches_count()
for user in self:
user.branches_count = branches_count
@api.model
def create(self, vals):
res = super(Users, self).create(vals)
if 'company_id' in vals:
vals.update({
'default_branch_id': self.company_id.branch_id.id,
})
return res
@api.multi
def write(self, vals):
res = super(Users, self).write(vals)
if 'company_id' in vals:
self.default_branch_id = self.company_id.branch_id.id
return res