Commit f42b52db by François C.

Prevent user to order the same product twice in an a single purchase order

parent 57044e23
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from openerp import api, models, fields from openerp import api, models, fields
from openerp.tools.translate import _
from openerp.exceptions import UserError
class PurchaseOrder(models.Model): class PurchaseOrder(models.Model):
_inherit = 'purchase.order' _inherit = 'purchase.order'
def verify_product_line_unicity(self):
pdts = {}
for order in self:
for line in order.order_line:
if not line.product_id.id in pdts:
pdts[line.product_id.id] = 0
pdts[line.product_id.id] += 1
pids = []
for pid, nb in pdts.items():
if nb > 1:
pids.append(pid)
if len(pids) > 0:
products = self.env['product.product'].search([('id', 'in', pids)])
message = ''
for p in products:
message += p.display_name + ' (' + str(p.product_tmpl_id.id) + ')'
message += ' --> ' + str(pdts[p.id]) + " lines\n"
raise UserError(_(message))
@api.multi @api.multi
def write(self, vals): def write(self, vals):
res = super(PurchaseOrder, self).write(vals) res = super(PurchaseOrder, self).write(vals)
if 'state' in vals and vals['state'] == 'purchase': if 'state' in vals and vals['state'] == 'purchase':
import requests import requests
# First of all, verify that no product has been added twice or more
# an error is raised if a product appears more than once
self.verify_product_line_unicity()
conf = self.env['ir.config_parameter'] conf = self.env['ir.config_parameter']
export_url = str(conf.get_param('cagette_purchase.orders_proxy_url')).strip() export_url = str(conf.get_param('cagette_purchase.orders_proxy_url')).strip()
export_url += '/export/' + str(self.id) export_url += '/export/' + str(self.id)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment