[WIP][REF]GOLEM Resource Pack : 1rst pass of refactoring on models and views

This commit is contained in:
Fabien BOURGEOIS 2018-04-16 19:16:53 +02:00
parent 21f5b6789a
commit b855e1436e
11 changed files with 72 additions and 82 deletions

View File

@ -24,7 +24,7 @@
'category': 'GOLEM', 'category': 'GOLEM',
'author': 'Youssef El Ouahby, Fabien Bourgeois', 'author': 'Youssef El Ouahby, Fabien Bourgeois',
'license': 'AGPL-3', 'license': 'AGPL-3',
'application': True, 'application': False,
'installable': True, 'installable': True,
'depends': ['golem_resource'], 'depends': ['golem_resource'],
'data': ['views/golem_resource_pack_views.xml', 'data': ['views/golem_resource_pack_views.xml',

View File

@ -16,5 +16,4 @@
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from . import golem_resource_pack, \ from . import golem_resource_pack, golem_resource_reservation
golem_resource_reservation

View File

@ -28,47 +28,54 @@ class GolemResourcePack(models.Model):
_description = 'GOLEM Resource Pack Model' _description = 'GOLEM Resource Pack Model'
_inherit = 'mail.thread' _inherit = 'mail.thread'
name = fields.Char(compute='_compute_name', store=True) name = fields.Char(required=True)
reservation_ids = fields.One2many('golem.resource.reservation', 'pack_id', reservation_ids = fields.One2many('golem.resource.reservation', 'pack_id',
readonly=True, track_visibility='onchange', readonly=True, string='Reservations',
states={'draft': [('readonly', False)], states={'draft': [('readonly', False)]})
False : [('readonly', False)]})
note = fields.Text(help='Notes, optional subject for the reservation, reason', note = fields.Text(help='Notes, optional subject for the reservation, reason',
track_visibility='onchange', readonly=True, track_visibility='onchange', readonly=True,
states={'draft': [('readonly', False)], states={'draft': [('readonly', False)]})
False : [('readonly', False)]})
user_id = fields.Many2one('res.users', required=True, index=True, readonly=True, user_id = fields.Many2one('res.users', required=True, index=True, readonly=True,
string='User', default=lambda self: self.env.user) string='User', default=lambda self: self.env.user)
partner_id = fields.Many2one('res.partner', string='On behalf of', required=True, partner_id = fields.Many2one('res.partner', string='On behalf of', required=True,
index=True, track_visibility='onchange', readonly=True, index=True, track_visibility='onchange', readonly=True,
states={'draft': [('readonly', False)], states={'draft': [('readonly', False)]})
False : [('readonly', False)]})
state = fields.Selection([('canceled', 'Canceled'), state = fields.Selection([('canceled', 'Canceled'),
('draft', 'Draft'), ('draft', 'Draft'),
('confirmed', 'Confirmed'), ('confirmed', 'Confirmed'),
('validated', 'Validated'), ('validated', 'Validated'),
('rejected', 'Rejected')], ('rejected', 'Rejected')],
default='draft', compute="_compute_pack_state", default='draft', compute='_compute_pack_state',
track_visibility='onchange') track_visibility='onchange')
reservation_count = fields.Integer(compute="_compute_reservation_count", reservation_count = fields.Integer(compute='_compute_reservation_count')
string="Reservation count")
rejection_reason = fields.Text(readonly=True, track_visibility='onchange') rejection_reason = fields.Text(readonly=True, track_visibility='onchange')
@api.multi
@api.constrains('partner_id')
def set_reservation_partner(self):
""" Set reservation partner """
for pack in self:
pack.reservation_ids.write({'partner_id': pack.partner_id.id})
@api.multi
@api.depends('reservation_ids') @api.depends('reservation_ids')
def _compute_reservation_count(self): def _compute_reservation_count(self):
for pack in self: for pack in self:
pack.reservation_count = len(pack.reservation_ids) pack.reservation_count = len(pack.reservation_ids)
@api.depends('reservation_ids', 'reservation_ids.state')
def _compute_pack_state(self):
""" get pack state """
for pack in self:
if not pack.reservation_ids:
pack.state = 'draft'
else:
reservation_states = pack.mapped('reservation_ids.state')
if 'rejected' in reservation_states:
pack.state = 'rejected'
elif 'canceled' in reservation_states:
pack.state = 'canceled'
elif 'draft' in reservation_states:
pack.state = 'draft'
elif 'confirmed' in reservation_states:
pack.state = 'confirmed'
elif 'validated' in reservation_states:
pack.state = 'validated'
@api.multi @api.multi
def state_confirm(self): def state_confirm(self):
""" pack confirm """ """ pack confirm """
@ -105,34 +112,16 @@ class GolemResourcePack(models.Model):
'view_mode': 'form', 'view_mode': 'form',
'target': 'new'} 'target': 'new'}
@api.constrains('partner_id')
@api.depends('partner_id') def set_reservation_partner(self):
def _compute_name(self): """ Set reservation partner """
""" Compute pack name """
for pack in self: for pack in self:
pack.name = u'{}/{}'.format(pack.partner_id.name, pack.reservation_ids.write({'partner_id': pack.partner_id.id})
pack.create_date)
@api.multi
@api.constrains('reservation_ids') @api.constrains('reservation_ids')
def check_reservation_partner(self): def check_reservation_partner(self):
""" Check reservation partner """ """ Check reservation partner """
for pack in self: for pack in self:
if len(filter(lambda x: x.partner_id == pack.partner_id, pack.reservation_ids)) < len(pack.reservation_ids): if len(pack.reservation_ids.mapped('partner_id')) > 1:
raise ValidationError(_('Pack client should be the same for all reservations')) raise ValidationError(_('Pack partner should be the same for '
'all reservations'))
@api.multi
@api.depends('reservation_ids')
def _compute_pack_state(self):
""" get pack state """
for pack in self:
reservation_states = list(map(lambda x: x.state, pack.reservation_ids))
if "rejected" in reservation_states:
pack.state = 'rejected'
elif "canceled" in reservation_states:
pack.state = 'canceled'
elif "draft" in reservation_states:
pack.state = 'draft'
elif "confirmed" in reservation_states:
pack.state = 'confirmed'
elif "validated" in reservation_states:
pack.state = 'validated'

View File

@ -25,4 +25,5 @@ class GolemResourceReservation(models.Model):
""" GOLEM Resource Reservation Model """ """ GOLEM Resource Reservation Model """
_inherit = 'golem.resource.reservation' _inherit = 'golem.resource.reservation'
pack_id = fields.Many2one('golem_resource_reservation', 'Reservation Pack') pack_id = fields.Many2one('golem.resource.pack', 'Reservation Pack',
index=True)

View File

@ -1,3 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_golem_resource_pack_user,Access GOLEM Resource Pack User,model_golem_resource_pack,golem_base.group_golem_user,1,0,0,0 access_golem_resource_pack_user,Access GOLEM Resource Pack User,model_golem_resource_pack,golem_base.group_golem_user,1,1,1,0
access_golem_resource_pack_manager,Access GOLEM Resource Pack Manager,model_golem_resource_pack,golem_base.group_golem_manager,1,1,1,1 access_golem_resource_pack_manager,Access GOLEM Resource Pack Manager,model_golem_resource_pack,golem_base.group_golem_manager,1,1,1,1

1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_golem_resource_pack_user Access GOLEM Resource Pack User model_golem_resource_pack golem_base.group_golem_user 1 0 1 0 1 0
3 access_golem_resource_pack_manager Access GOLEM Resource Pack Manager model_golem_resource_pack golem_base.group_golem_manager 1 1 1 1

View File

@ -23,7 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<field name="name">GOLEM Resource Pack Tree</field> <field name="name">GOLEM Resource Pack Tree</field>
<field name="model">golem.resource.pack</field> <field name="model">golem.resource.pack</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<tree colors="black: state=='draft'; blue: state=='validated'; gree : state=='validated'; grey: state=='canceled'; orange: state=='rejected'"> <tree colors="black: state=='draft'; blue: state=='validated'; green : state=='validated'; grey: state=='canceled'; orange: state=='rejected'">
<field name="name" /> <field name="name" />
<field name="partner_id" /> <field name="partner_id" />
<field name="state" /> <field name="state" />
@ -55,7 +55,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</header> </header>
<sheet> <sheet>
<group> <group>
<group name="general" string="Genaral"> <group name="general" string="Pack" colspan="2">
<field name="id" invisible="1"/> <field name="id" invisible="1"/>
<field name="user_id" /> <field name="user_id" />
<field name="partner_id" /> <field name="partner_id" />
@ -63,8 +63,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<field name="rejection_reason" <field name="rejection_reason"
attrs="{'invisible': [('state', '!=', 'rejected')]}"/> attrs="{'invisible': [('state', '!=', 'rejected')]}"/>
</group> </group>
<group colspan="2" name="reservations">
<field name="reservation_ids" widget="many2many"
context="{'default_partner_id' : partner_id, 'default_pack_id': active_id, 'search_default_state_draft': 1, 'search_default_partner_id': partner_id}" />
</group>
</group> </group>
<field name="reservation_ids" context="{'default_partner_id' : partner_id}" />
</sheet> </sheet>
<div class="oe_chatter"> <div class="oe_chatter">
<field name="message_follower_ids" widget="mail_followers" /> <field name="message_follower_ids" widget="mail_followers" />

View File

@ -31,8 +31,7 @@ class GolemReservationRejectionWizard(models.TransientModel):
def reject(self): def reject(self):
""" Sets pack status to rejected and add reason """ """ Sets pack status to rejected and add reason """
self.ensure_one() self.ensure_one()
rejection = self[0] rdata = {'state': 'rejected',
for reservation in rejection.pack_id.reservation_ids: 'rejection_reason': self[0].reason}
if reservation.state == "confirmed": self[0].pack_id.reservation_ids.filtered(lambda r: r.state == 'confirmed').write(rdata)
reservation.write({'state' :'rejected'}) self[0].pack_id.rejection_reason = self[0].reason
rejection.pack_id.write({'rejection_reason': rejection.reason})

View File

@ -25,10 +25,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<field name="model">golem.pack.rejection.wizard</field> <field name="model">golem.pack.rejection.wizard</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form string="Rejection reason"> <form string="Rejection reason">
<group> <sheet>
<field name="pack_id" readonly="1" /> <group>
<field name="reason" /> <field name="pack_id" readonly="1" />
</group> <field name="reason" />
</group>
</sheet>
<footer> <footer>
<button name="reject" string="Reject" type="object" <button name="reject" string="Reject" type="object"
class="oe_highlight" /> class="oe_highlight" />

View File

@ -20,13 +20,13 @@
'name': 'GOLEM resources pack invoicing', 'name': 'GOLEM resources pack invoicing',
'summary': 'GOLEM resources pack invoicing', 'summary': 'GOLEM resources pack invoicing',
'description': ''' GOLEM resources pack invoicing ''', 'description': ''' GOLEM resources pack invoicing ''',
'version': '10.0.0.0.1', 'version': '10.0.0.1.0',
'category': 'GOLEM', 'category': 'GOLEM',
'author': 'Youssef El Ouahby, Fabien Bourgeois', 'author': 'Youssef El Ouahby, Fabien Bourgeois',
'license': 'AGPL-3', 'license': 'AGPL-3',
'application': True, 'application': False,
'installable': True, 'installable': True,
'auto_install': True,
'depends': ['golem_resource_pack', 'golem_resource_account'], 'depends': ['golem_resource_pack', 'golem_resource_account'],
'data': ['views/golem_resource_pack_views.xml'] 'data': ['views/golem_resource_pack_views.xml']
#'security/ir.model.access.csv']
} }

View File

@ -32,21 +32,17 @@ class GolemResourcePack(models.Model):
copy=False) copy=False)
invoice_amount_total = fields.Monetary(related='invoice_id.amount_total') invoice_amount_total = fields.Monetary(related='invoice_id.amount_total')
currency_id = fields.Many2one(related='invoice_id.currency_id') currency_id = fields.Many2one(related='invoice_id.currency_id')
is_products_set = fields.Boolean(compute="_compute_is_products_set") are_products_set = fields.Boolean(compute="_compute_are_products_set")
@api.multi @api.depends('reservation_ids.resource_product_id')
def _compute_is_products_set(self): def _compute_are_products_set(self):
""" compute is_products_set """ """ Computes are_products_set """
for pack in self: for pack in self:
if len(filter(lambda x: x.resource_product_id.id is False, pack.reservation_ids)) \ pack.are_products_set = all([r.resource_product_id.id for r in pack.reservation_ids])
> 0:
pack.is_products_set = False
else:
pack.is_products_set = True
@api.multi @api.multi
def chek_pack_to_invoice(self): def chek_pack_to_invoice(self):
""" chek pack before invoicing """ """ Cheks pack before invoicing """
for pack in self: for pack in self:
if pack.state != 'validated': if pack.state != 'validated':
raise ValidationError(_('The current pack is not validated, please validate ' raise ValidationError(_('The current pack is not validated, please validate '
@ -58,22 +54,20 @@ class GolemResourcePack(models.Model):
raise ValidationError(_('You can not create an invoice as there ' raise ValidationError(_('You can not create an invoice as there '
'is already one.')) 'is already one.'))
@api.multi @api.multi
def create_invoice(self): def create_invoice(self):
""" Invoice creation """ """ Invoice creation """
self.chek_pack_to_invoice()
for pack in self: for pack in self:
pack.chek_pack_to_invoice()
pack.reservation_ids.check_before_invoicing() pack.reservation_ids.check_before_invoicing()
inv_obj = self.env['account.invoice']
partner_id = pack.partner_id partner_id = pack.partner_id
invoice_id = inv_obj.create({ invoice_id = self.env['account.invoice'].create({
'origin': pack.name, 'origin': pack.name,
'type': 'out_invoice', 'type': 'out_invoice',
'reference': False, 'reference': False,
'account_id': partner_id.property_account_receivable_id.id, 'account_id': partner_id.property_account_receivable_id.id,
'partner_id': partner_id.id 'partner_id': partner_id.id
}) })
pack.invoice_id = invoice_id.id pack.invoice_id = invoice_id.id
pack.reservation_ids.create_invoice_line(invoice_id) pack.reservation_ids.create_invoice_line(invoice_id)

View File

@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
--> -->
<odoo> <odoo>
<data> <data>
<!-- Tree --> <!-- Tree -->
<record model="ir.ui.view" id="golem_resource_pack_inherit_account_tree"> <record model="ir.ui.view" id="golem_resource_pack_inherit_account_tree">
<field name="name">GOLEM Resource Pack Account extention Tree</field> <field name="name">GOLEM Resource Pack Account extention Tree</field>
@ -29,6 +30,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</field> </field>
</field> </field>
</record> </record>
<!-- Forms --> <!-- Forms -->
<record model="ir.ui.view" id="golem_resource_pack_inherit_account_form"> <record model="ir.ui.view" id="golem_resource_pack_inherit_account_form">
<field name="name">GOLEM Resource Pack Account extention Form</field> <field name="name">GOLEM Resource Pack Account extention Form</field>
@ -37,14 +39,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<field name="arch" type="xml"> <field name="arch" type="xml">
<button name="state_rejected" position="after"> <button name="state_rejected" position="after">
<button name="create_invoice" type="object" string="Create Invoice" class="oe_highlight" <button name="create_invoice" type="object" string="Create Invoice" class="oe_highlight"
attrs="{'invisible': ['|', ('state', '!=', 'validated'), '|', ('invoice_id', '!=', False), ('is_products_set', '=', False)]}" /> attrs="{'invisible': ['|', ('state', '!=', 'validated'), '|', ('invoice_id', '!=', False), ('are_products_set', '=', False)]}" />
<button name="show_invoice" type="object" string="Show invoice" class="oe_highlight" <button name="show_invoice" type="object" string="Show invoice" class="oe_highlight"
attrs="{'invisible': [('invoice_id', '=', False)]}" /> attrs="{'invisible': [('invoice_id', '=', False)]}" />
</button> </button>
<group name="general" position="after"> <group name="general" position="after">
<group name="invoicing" string="Invoicing" <group name="invoicing" string="Invoicing"
attrs="{'invisible': [('invoice_id', '=', False)]}"> attrs="{'invisible': [('invoice_id', '=', False)]}">
<field name="is_products_set" invisible="1"/> <field name="are_products_set" invisible="1"/>
<field name="invoice_id" /> <field name="invoice_id" />
<field name="invoice_state" /> <field name="invoice_state" />
<field name="invoice_amount_total" /> <field name="invoice_amount_total" />
@ -52,5 +54,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</group> </group>
</field> </field>
</record> </record>
</data> </data>
</odoo> </odoo>