From 561829849f2c354136390bf9da2c487c4bddcac7 Mon Sep 17 00:00:00 2001 From: Ravi Gadhia Date: Fri, 21 Jun 2019 09:22:21 +0000 Subject: [PATCH] [IMP] purchase: add section and note on order line --- .../views/report_purchase_order.xml | 25 +++++-- addons/purchase/models/account_invoice.py | 2 +- addons/purchase/models/purchase.py | 30 ++++++-- .../report/purchase_order_templates.xml | 75 +++++++++++++------ .../report/purchase_quotation_templates.xml | 33 +++++--- addons/purchase/views/portal_templates.xml | 28 ++++++- addons/purchase/views/purchase_views.xml | 69 +++++++++++------ 7 files changed, 194 insertions(+), 68 deletions(-) diff --git a/addons/l10n_in_purchase/views/report_purchase_order.xml b/addons/l10n_in_purchase/views/report_purchase_order.xml index 398e9fcf068..f2ffc0844ff 100644 --- a/addons/l10n_in_purchase/views/report_purchase_order.xml +++ b/addons/l10n_in_purchase/views/report_purchase_order.xml @@ -2,15 +2,30 @@ diff --git a/addons/purchase/models/account_invoice.py b/addons/purchase/models/account_invoice.py index 9e1f867a7ea..18fb4b4f9de 100644 --- a/addons/purchase/models/account_invoice.py +++ b/addons/purchase/models/account_invoice.py @@ -45,7 +45,7 @@ class AccountMove(models.Model): # Copy purchase lines. po_lines = self.purchase_id.order_line - self.line_ids.mapped('purchase_line_id') new_lines = self.env['account.move.line'] - for line in po_lines: + for line in po_lines.filtered(lambda l: not l.display_type): new_line = new_lines.new(line._prepare_account_move_line(self)) new_line.account_id = new_line._get_computed_account() new_line._onchange_price_subtotal() diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index 3ab10e2d3b2..7c68c906954 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -36,7 +36,7 @@ class PurchaseOrder(models.Model): for order in self: min_date = False for line in order.order_line: - if not min_date or line.date_planned < min_date: + if not min_date or line.date_planned and line.date_planned < min_date: min_date = line.date_planned if min_date: order.date_planned = min_date @@ -360,7 +360,7 @@ class PurchaseOrder(models.Model): for line in self.order_line: # Do not add a contact as a supplier partner = self.partner_id if not self.partner_id.parent_id else self.partner_id.parent_id - if partner not in line.product_id.seller_ids.mapped('name') and len(line.product_id.seller_ids) <= 10: + if line.product_id and partner not in line.product_id.seller_ids.mapped('name') and len(line.product_id.seller_ids) <= 10: currency = partner.property_purchase_currency_id or self.env.company.currency_id supplierinfo = { 'name': partner.id, @@ -431,11 +431,11 @@ class PurchaseOrderLine(models.Model): sequence = fields.Integer(string='Sequence', default=10) product_qty = fields.Float(string='Quantity', digits='Product Unit of Measure', required=True) product_uom_qty = fields.Float(string='Total Quantity', compute='_compute_product_uom_qty', store=True) - date_planned = fields.Datetime(string='Scheduled Date', required=True, index=True) + date_planned = fields.Datetime(string='Scheduled Date', index=True) taxes_id = fields.Many2many('account.tax', string='Taxes', domain=['|', ('active', '=', False), ('active', '=', True)]) - product_uom = fields.Many2one('uom.uom', string='Unit of Measure', required=True, domain="[('category_id', '=', product_uom_category_id)]") + product_uom = fields.Many2one('uom.uom', string='Unit of Measure', domain="[('category_id', '=', product_uom_category_id)]") product_uom_category_id = fields.Many2one(related='product_id.uom_id.category_id') - product_id = fields.Many2one('product.product', string='Product', domain=[('purchase_ok', '=', True)], change_default=True, required=True) + product_id = fields.Many2one('product.product', string='Product', domain=[('purchase_ok', '=', True)], change_default=True) product_type = fields.Selection(related='product_id.type', readonly=True) price_unit = fields.Float(string='Unit Price', required=True, digits='Product Price') @@ -465,6 +465,19 @@ class PurchaseOrderLine(models.Model): currency_id = fields.Many2one(related='order_id.currency_id', store=True, string='Currency', readonly=True) date_order = fields.Datetime(related='order_id.date_order', string='Order Date', readonly=True) + display_type = fields.Selection([ + ('line_section', "Section"), + ('line_note', "Note")], default=False, help="Technical field for UX purpose.") + + _sql_constraints = [ + ('accountable_required_fields', + "CHECK(display_type IS NOT NULL OR (product_id IS NOT NULL AND product_uom IS NOT NULL AND date_planned IS NOT NULL))", + "Missing required fields on accountable purchase order line."), + ('non_accountable_null_fields', + "CHECK(display_type IS NULL OR (product_id IS NULL AND price_unit = 0 AND product_uom_qty = 0 AND product_uom IS NULL AND date_planned is NULL))", + "Forbidden values on non-accountable purchase order line"), + ] + @api.depends('product_qty', 'price_unit', 'taxes_id') def _compute_amount(self): for line in self: @@ -545,6 +558,9 @@ class PurchaseOrderLine(models.Model): @api.model def create(self, values): + if values.get('display_type', self.default_get(['display_type'])['display_type']): + values.update(product_id=False, price_unit=0, product_uom_qty=0, product_uom=False, date_planned=False) + line = super(PurchaseOrderLine, self).create(values) if line.order_id.state == 'purchase': msg = _("Extra line with %s ") % (line.product_id.display_name,) @@ -553,6 +569,9 @@ class PurchaseOrderLine(models.Model): @api.multi def write(self, values): + if 'display_type' in values and self.filtered(lambda line: line.display_type != values.get('display_type')): + raise UserError("You cannot change the type of a purchase order line. Instead you should delete the current line and create a new line of the proper type.") + if 'product_qty' in values: for line in self: if line.order_id.state == 'purchase': @@ -714,4 +733,5 @@ class PurchaseOrderLine(models.Model): 'analytic_account_id': self.account_analytic_id.id, 'analytic_tag_ids': [(6, 0, self.analytic_tag_ids.ids)], 'tax_ids': [(6, 0, self.taxes_id.ids)], + 'display_type': self.display_type, } diff --git a/addons/purchase/report/purchase_order_templates.xml b/addons/purchase/report/purchase_order_templates.xml index 8351d401d8b..4a473309480 100644 --- a/addons/purchase/report/purchase_order_templates.xml +++ b/addons/purchase/report/purchase_order_templates.xml @@ -53,28 +53,59 @@ - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Subtotal + + + + + diff --git a/addons/purchase/report/purchase_quotation_templates.xml b/addons/purchase/report/purchase_quotation_templates.xml index eec1465e0ca..536a7c50f77 100644 --- a/addons/purchase/report/purchase_quotation_templates.xml +++ b/addons/purchase/report/purchase_quotation_templates.xml @@ -29,18 +29,27 @@ - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + diff --git a/addons/purchase/views/portal_templates.xml b/addons/purchase/views/portal_templates.xml index b6a9a0a0847..6d723766665 100644 --- a/addons/purchase/views/portal_templates.xml +++ b/addons/purchase/views/portal_templates.xml @@ -97,8 +97,11 @@ Subtotal + -
+ + +
Product
@@ -115,6 +118,29 @@
+ +
+ +
+ + + + +
+ +
+
+ +
+
Subtotal
+
+ +
+
+

diff --git a/addons/purchase/views/purchase_views.xml b/addons/purchase/views/purchase_views.xml index d5b320c6d33..04735497d18 100644 --- a/addons/purchase/views/purchase_views.xml +++ b/addons/purchase/views/purchase_views.xml @@ -181,16 +181,31 @@ - - + + + + + + + + - - - + + + @@ -199,25 +214,31 @@ - +
- - + + - + + + + + + + + + + + - - - - - - - - - -
+