~camptocamp/c2c-rd-addons/8.0a

« back to all changes in this revision

Viewing changes to sale_shipped_rate/sale.py

  • Committer: ferdinand
  • Date: 2011-04-30 20:34:01 UTC
  • Revision ID: office@chricar.at-20110430203401-eqfv4au4tv3faj93
[ADD] initial

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
#    Copyright (C) 2010-2010 Camptocamp Austria (<http://www.camptocamp.at>)
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU Affero General Public License as
 
10
#    published by the Free Software Foundation, either version 3 of the
 
11
#    License, or (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU Affero General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU Affero General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
 
 
24
from osv import fields, osv
 
25
 
 
26
class sale_order(osv.osv):
 
27
    _inherit = "sale.order"
 
28
 
 
29
    def _shipped_rate(self, cr, uid, ids, name, arg, context=None):
 
30
        if not ids:
 
31
            return {}
 
32
        res = {}
 
33
        for id in ids:
 
34
            res[id] = [0.0, 0.0]
 
35
        cr.execute('''SELECT
 
36
                p.sale_id, sum(m.product_qty), mp.state as mp_state
 
37
            FROM
 
38
                stock_move m
 
39
            LEFT JOIN
 
40
                stock_picking p on (p.id=m.picking_id)
 
41
            LEFT JOIN
 
42
                procurement_order mp on (mp.move_id=m.id)
 
43
            WHERE
 
44
                p.type = 'internal' AND
 
45
                p.sale_id IN %s GROUP BY mp.state, p.sale_id''', (tuple(ids),))
 
46
        for oid, nbr, mp_state in cr.fetchall():
 
47
            if mp_state == 'cancel':
 
48
                continue
 
49
            if mp_state == 'done':
 
50
                res[oid][0] += nbr or 0.0
 
51
                res[oid][1] += nbr or 0.0
 
52
            else:
 
53
                res[oid][1] += nbr or 0.0
 
54
        for r in res:
 
55
            if not res[r][1]:
 
56
                res[r] = 0.0
 
57
            else:
 
58
                res[r] = 100.0 * res[r][0] / res[r][1]
 
59
        for order in self.browse(cr, uid, ids, context=context):
 
60
            if order.shipped:
 
61
                res[order.id] = 100.0
 
62
        return res
 
63
 
 
64
    def _picked_rate(self, cr, uid, ids, name, arg, context=None):
 
65
        if not ids:
 
66
            return {}
 
67
        res = {}
 
68
        for id in ids:
 
69
            res[id] = [0.0, 0.0]
 
70
        cr.execute('''SELECT
 
71
                p.sale_id, sum(m.product_qty), mp.state as mp_state
 
72
            FROM
 
73
                stock_move m
 
74
            LEFT JOIN
 
75
                stock_picking p on (p.id=m.picking_id)
 
76
            LEFT JOIN
 
77
                procurement_order mp on (mp.move_id=m.id)
 
78
            WHERE
 
79
                p.type != 'internal' AND
 
80
                p.sale_id IN %s GROUP BY mp.state, p.sale_id''', (tuple(ids),))
 
81
        for oid, nbr, mp_state in cr.fetchall():
 
82
            if mp_state == 'cancel':
 
83
                continue
 
84
            if mp_state == 'done':
 
85
                res[oid][0] += nbr or 0.0
 
86
                res[oid][1] += nbr or 0.0
 
87
            else:
 
88
                res[oid][1] += nbr or 0.0
 
89
        for r in res:
 
90
            if not res[r][1]:
 
91
                res[r] = 0.0
 
92
            else:
 
93
                res[r] = 100.0 * res[r][0] / res[r][1]
 
94
        for order in self.browse(cr, uid, ids, context=context):
 
95
            if order.shipped:
 
96
                res[order.id] = 100.0
 
97
        return res
 
98
 
 
99
    _columns = {
 
100
        'shipped_rate': fields.function(_shipped_rate, method=True, string='Shipped', type='float'),
 
101
    }
 
102
 
 
103
sale_order()
 
 
b'\\ No newline at end of file'