1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Affero General Public License as
9
# published by the Free Software Foundation, either version 3 of the
10
# License, or (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Affero General Public License for more details.
17
# You should have received a copy of the GNU Affero General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
##############################################################################
23
from osv import fields
26
class purchase_order(osv.osv):
27
_name = 'purchase.order'
28
_inherit = 'purchase.order'
30
def _shipped_rate(self, cr, uid, ids, name, arg, context=None):
31
uom_obj = self.pool.get('product.uom')
35
for order in self.browse(cr, uid, ids, context=context):
36
# Direct PO is 100.00% received when a user confirm the reception at customer side
37
if order.order_type == 'direct' and order.state == 'done':
38
res[order.id] = 100.00
40
elif order.order_type == 'direct' and order.state != 'done':
45
amount_received = 0.00
46
for line in order.order_line:
47
amount_total += line.product_qty*line.price_unit
48
for move in line.move_ids:
49
if move.state == 'done':
50
move_qty = uom_obj._compute_qty(cr, uid, move.product_uom.id, move.product_qty, line.product_uom.id)
51
if move.type == 'out':
52
amount_received -= move_qty*line.price_unit
53
elif move.type == 'in':
54
amount_received += move_qty*line.price_unit
55
elif move.type == 'internal':
56
# not taken into account
60
res[order.id] = (amount_received/amount_total)*100
65
'shipped_rate': fields.function(_shipped_rate, method=True, string='Received', type='float'),
68
def name_search(self, cr, uid, name='', args=None, operator='ilike', context=None, limit=80):
70
Search all PO by internal or customer reference
74
if context.get('from_followup'):
76
if name and len(name) > 1:
77
ids.extend(self.search(cr, uid, [('partner_ref', operator, name)], context=context))
78
return self.name_get(cr, uid, ids, context=context)
79
elif context.get('from_followup2'):
80
# receive input name as a customer name, get customer ids by operator
81
# then search customer ids in PO
83
if name and len(name) > 1:
85
customer_ids = self.pool.get('res.partner').search(cr, uid,
86
[('name', operator, name)], context=context)
88
# search for m2o 'dest_partner_id' dest_customer in PO (direct PO)
89
po1_ids = ids.extend(self.search(cr, uid,
90
[('dest_partner_id', 'in', customer_ids)],
92
# search for m2m 'dest_partner_ids' dest_customer in PO (sourcing PO)
93
query = "SELECT purchase_order_id FROM res_partner_purchase_order_rel"
94
query += " WHERE partner_id in (" + ",".join(map(str, customer_ids)) + ")"
97
po2_ids = cr.fetchall()
99
# po1_ids, po2_ids union
100
for po_id in po1_ids:
101
if po_id not in po2_ids:
102
po2_ids.append(po_id)
106
('rfq_ok', '=', False),
109
ids = self.search(cr, uid, domain, context=context)
110
return self.name_get(cr, uid, ids, context=context)
112
return super(purchase_order, self).name_search(cr, uid, name, args, operator, context, limit)
114
def name_get(self, cr, uid, ids, context=None):
116
If the method is called from followup wizard, set the supplier ref in brackets
120
if context.get('from_followup'):
122
for r in self.browse(cr, uid, ids, context=context):
124
res.append((r.id, '%s' % r.partner_ref))
126
res.append((r.id, '%s' % r.name))
128
elif context.get('from_followup2'):
130
for r in self.browse(cr, uid, ids, context=context):
133
if r.dest_partner_id:
135
customer_names.append(r.dest_partner_id.name)
136
if r.dest_partner_ids:
137
# customer from sourcing
138
for customer in r.dest_partner_ids:
139
if r.dest_partner_id and not customer.id == r.dest_partner_id.id:
140
customer_names.append(customer.name)
142
customer_names.append(customer.name)
144
# display PO and Customers
145
name += " (%s)" % ("; ".join(customer_names),)
146
res.append((r.id, name))
149
return super(purchase_order, self).name_get(cr, uid, ids, context=context)
154
class sale_order(osv.osv):
156
_inherit = 'sale.order'
158
def _picked_rate(self, cr, uid, ids, name, arg, context=None):
159
uom_obj = self.pool.get('product.uom')
164
for order in self.browse(cr, uid, ids, context=context):
167
amount_received = 0.00
168
for line in order.order_line:
169
amount_total += line.product_uom_qty*line.price_unit
170
for move in line.move_ids:
171
if move.state == 'done' and move.location_dest_id.usage == 'customer':
172
move_qty = uom_obj._compute_qty(cr, uid, move.product_uom.id, move.product_qty, line.product_uom.id)
173
amount_received += move_qty*line.price_unit
176
res[order.id] = (amount_received/amount_total)*100
181
'picked_rate': fields.function(_picked_rate, method=True, string='Picked', type='float'),
186
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: