[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',
'author': 'Youssef El Ouahby, Fabien Bourgeois',
'license': 'AGPL-3',
'application': True,
'application': False,
'installable': True,
'depends': ['golem_resource'],
'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
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from . import golem_resource_pack, \
golem_resource_reservation
from . import golem_resource_pack, golem_resource_reservation

View File

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

View File

@ -25,4 +25,5 @@ class GolemResourceReservation(models.Model):
""" GOLEM Resource Reservation Model """
_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
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

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="model">golem.resource.pack</field>
<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="partner_id" />
<field name="state" />
@ -55,7 +55,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</header>
<sheet>
<group>
<group name="general" string="Genaral">
<group name="general" string="Pack" colspan="2">
<field name="id" invisible="1"/>
<field name="user_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"
attrs="{'invisible': [('state', '!=', 'rejected')]}"/>
</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>
<field name="reservation_ids" context="{'default_partner_id' : partner_id}" />
</sheet>
<div class="oe_chatter">
<field name="message_follower_ids" widget="mail_followers" />

View File

@ -31,8 +31,7 @@ class GolemReservationRejectionWizard(models.TransientModel):
def reject(self):
""" Sets pack status to rejected and add reason """
self.ensure_one()
rejection = self[0]
for reservation in rejection.pack_id.reservation_ids:
if reservation.state == "confirmed":
reservation.write({'state' :'rejected'})
rejection.pack_id.write({'rejection_reason': rejection.reason})
rdata = {'state': 'rejected',
'rejection_reason': self[0].reason}
self[0].pack_id.reservation_ids.filtered(lambda r: r.state == 'confirmed').write(rdata)
self[0].pack_id.rejection_reason = self[0].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="arch" type="xml">
<form string="Rejection reason">
<group>
<field name="pack_id" readonly="1" />
<field name="reason" />
</group>
<sheet>
<group>
<field name="pack_id" readonly="1" />
<field name="reason" />
</group>
</sheet>
<footer>
<button name="reject" string="Reject" type="object"
class="oe_highlight" />

View File

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

View File

@ -32,21 +32,17 @@ class GolemResourcePack(models.Model):
copy=False)
invoice_amount_total = fields.Monetary(related='invoice_id.amount_total')
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
def _compute_is_products_set(self):
""" compute is_products_set """
@api.depends('reservation_ids.resource_product_id')
def _compute_are_products_set(self):
""" Computes are_products_set """
for pack in self:
if len(filter(lambda x: x.resource_product_id.id is False, pack.reservation_ids)) \
> 0:
pack.is_products_set = False
else:
pack.is_products_set = True
pack.are_products_set = all([r.resource_product_id.id for r in pack.reservation_ids])
@api.multi
def chek_pack_to_invoice(self):
""" chek pack before invoicing """
""" Cheks pack before invoicing """
for pack in self:
if pack.state != 'validated':
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 '
'is already one.'))
@api.multi
def create_invoice(self):
""" Invoice creation """
self.chek_pack_to_invoice()
for pack in self:
pack.chek_pack_to_invoice()
pack.reservation_ids.check_before_invoicing()
inv_obj = self.env['account.invoice']
partner_id = pack.partner_id
invoice_id = inv_obj.create({
invoice_id = self.env['account.invoice'].create({
'origin': pack.name,
'type': 'out_invoice',
'reference': False,
'account_id': partner_id.property_account_receivable_id.id,
'partner_id': partner_id.id
})
})
pack.invoice_id = invoice_id.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>
<data>
<!-- Tree -->
<record model="ir.ui.view" id="golem_resource_pack_inherit_account_tree">
<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>
</record>
<!-- Forms -->
<record model="ir.ui.view" id="golem_resource_pack_inherit_account_form">
<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">
<button name="state_rejected" position="after">
<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"
attrs="{'invisible': [('invoice_id', '=', False)]}" />
</button>
<group name="general" position="after">
<group name="invoicing" string="Invoicing"
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_state" />
<field name="invoice_amount_total" />
@ -52,5 +54,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</group>
</field>
</record>
</data>
</odoo>