~unifield-team/unifield-wm/us-826

« back to all changes in this revision

Viewing changes to purchase_followup/sale_purchase.py

  • Committer: Olivier DOSSMANN
  • Date: 2014-03-31 09:31:46 UTC
  • mto: This revision was merged to the branch mainline in revision 2086.
  • Revision ID: od@tempo-consulting.fr-20140331093146-tgvxnly1kc1hbv1s
UF-2171 [ADD] Analytic distribution reset button for recurring models

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#    
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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/>.     
 
19
#
 
20
##############################################################################
 
21
 
 
22
from osv import osv
 
23
from osv import fields
 
24
 
 
25
 
 
26
class purchase_order(osv.osv):
 
27
    _name = 'purchase.order'
 
28
    _inherit = 'purchase.order'
 
29
    
 
30
    def _shipped_rate(self, cr, uid, ids, name, arg, context=None):
 
31
        uom_obj = self.pool.get('product.uom')
 
32
        if not ids: return {}
 
33
        res = {}
 
34
        
 
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
 
39
                continue
 
40
            elif order.order_type == 'direct' and order.state != 'done':
 
41
                res[order.id] = 0.00
 
42
                continue
 
43
            res[order.id] = 0.00
 
44
            amount_total = 0.00
 
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
 
57
                            pass
 
58
                    
 
59
            if amount_total:
 
60
                res[order.id] = (amount_received/amount_total)*100
 
61
            
 
62
        return res
 
63
    
 
64
    _columns = {
 
65
            'shipped_rate': fields.function(_shipped_rate, method=True, string='Received', type='float'),
 
66
    }
 
67
 
 
68
    def name_search(self, cr, uid, name='', args=None, operator='ilike', context=None, limit=80):
 
69
        '''
 
70
        Search all PO by internal or customer reference
 
71
        '''
 
72
        if context is None:
 
73
            context = {}
 
74
        if context.get('from_followup'):
 
75
            ids = []
 
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
 
82
            ids = []
 
83
            if name and len(name) > 1:
 
84
                # search for customer
 
85
                customer_ids = self.pool.get('res.partner').search(cr, uid,
 
86
                    [('name', operator, name)], context=context)
 
87
                if customer_ids:
 
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)],
 
91
                        context=context))
 
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)) + ")"
 
95
                    cr.execute(query)
 
96
                    if cr.rowcount:
 
97
                        po2_ids = cr.fetchall()
 
98
                        if po1_ids:
 
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)
 
103
                        ids = po2_ids
 
104
                    if ids:
 
105
                        domain = [
 
106
                            ('rfq_ok', '=', False),
 
107
                            ('id', 'in', ids),
 
108
                        ]
 
109
                        ids = self.search(cr, uid, domain, context=context)
 
110
            return self.name_get(cr, uid, ids, context=context)
 
111
        else:
 
112
            return super(purchase_order, self).name_search(cr, uid, name, args, operator, context, limit)
 
113
 
 
114
    def name_get(self, cr, uid, ids, context=None):
 
115
        '''
 
116
        If the method is called from followup wizard, set the supplier ref in brackets
 
117
        '''
 
118
        if context is None:
 
119
            context = {}
 
120
        if context.get('from_followup'):
 
121
            res = []
 
122
            for r in self.browse(cr, uid, ids, context=context):
 
123
                if r.partner_ref:
 
124
                    res.append((r.id, '%s' % r.partner_ref))
 
125
                else:
 
126
                    res.append((r.id, '%s' % r.name))
 
127
            return res
 
128
        elif context.get('from_followup2'):
 
129
            res = []
 
130
            for r in self.browse(cr, uid, ids, context=context):
 
131
                name = r.name
 
132
                customer_names = []
 
133
                if r.dest_partner_id:
 
134
                    # direct customer
 
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)
 
141
                        else:
 
142
                            customer_names.append(customer.name)
 
143
                if customer_names:
 
144
                    # display PO and Customers
 
145
                    name += " (%s)" % ("; ".join(customer_names),)
 
146
                res.append((r.id, name))
 
147
            return res
 
148
        else:
 
149
            return super(purchase_order, self).name_get(cr, uid, ids, context=context)
 
150
    
 
151
purchase_order()
 
152
 
 
153
 
 
154
class sale_order(osv.osv):
 
155
    _name = 'sale.order'
 
156
    _inherit = 'sale.order'
 
157
    
 
158
    def _picked_rate(self, cr, uid, ids, name, arg, context=None):
 
159
        uom_obj = self.pool.get('product.uom')
 
160
        if not ids:
 
161
            return {}
 
162
        res = {}
 
163
        
 
164
        for order in self.browse(cr, uid, ids, context=context):
 
165
            res[order.id] = 0.00
 
166
            amount_total = 0.00
 
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
 
174
                    
 
175
            if amount_total:
 
176
                res[order.id] = (amount_received/amount_total)*100   
 
177
        
 
178
        return res
 
179
    
 
180
    _columns = {
 
181
        'picked_rate': fields.function(_picked_rate, method=True, string='Picked', type='float'),
 
182
    }
 
183
    
 
184
sale_order()
 
185
 
 
186
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: