~zaber/openobject-addons/stable_5.0-extra-addons

4925 by Omar (pexego)
[ADD] Adds new modules that wasn't published: wiki_parser_scrum, serial_number, sale_rappel, report_notes, pxgo_early_payment_discount, purchase_sale_delivery, product_variant_properties, intrastat_extend, full_merge_picking, wiki_parser, pxgo_project_hg, pxgo_product_sale_description, partner_discount, pxgo_scrum_trac
1
# -*- coding: utf-8 -*-
2
##############################################################################
3
#
4
#    OpenERP, Open Source Management Solution
5
#    Copyright (C) 2004-TODAY Pexego (<www.pexego.es>). All Rights Reserved
6
#    $Omar CastiƱeira Saavedra$
7
#
8
#    This program is free software: you can redistribute it and/or modify
9
#    it under the terms of the GNU General Public License as published by
10
#    the Free Software Foundation, either version 3 of the License, or
11
#    (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 General Public License for more details.
17
#
18
#    You should have received a copy of the GNU General Public License
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
#
21
##############################################################################
22
23
"""Add new relationship with stock_picking on purchases"""
24
25
from osv import osv, fields
26
import netsvc
27
from tools.translate import _
28
29
class purchase_order(osv.osv):
30
    """Add new relationship with stock_picking on purchases"""
31
32
    _inherit = "purchase.order"
33
34
    def _shipped_rate(self, cr, uid, ids, name, arg, context=None):
35
        """compute the shipped rate from picking moves"""
36
        if not ids: return {}
37
        res = {}
38
        for order in self.browse(cr, uid, ids):
39
            res[order.id] = [0.0,0.0]
40
41
        cr.execute('''SELECT
42
                p.id,sum(m.product_qty), m.state
43
            FROM
44
                stock_move m
45
            INNER JOIN
46
                purchase_order_line pl on (pl.id=m.purchase_line_id)
47
            INNER JOIN
48
                purchase_order p on (p.id=pl.order_id)
49
            WHERE
50
                p.id in %s
51
            GROUP BY m.state, p.id''',
52
                   (tuple(ids),))
53
54
        for oid,nbr,state in cr.fetchall():
55
            if state=='cancel':
56
                continue
57
            if state=='done':
58
                res[oid][0] += nbr or 0.0
59
                res[oid][1] += nbr or 0.0
60
            else:
61
                res[oid][1] += nbr or 0.0
62
        for r in res:
63
            if not res[r][1]:
64
                res[r] = 0.0
65
            else:
66
                res[r] = 100.0 * res[r][0] / res[r][1]
67
        return res
68
69
    def _invoiced_rate(self, cursor, user, ids, name, arg, context=None):
70
        res = {}
71
        for purchase in self.browse(cursor, user, ids, context=context):
72
            tot = 0.0
73
            tot_pamount = 0.0
74
            if purchase.invoice_id and purchase.invoice_id.state not in ('draft','cancel'):
75
                tot += purchase.invoice_id.amount_untaxed
76
77
            purchase_ids = self.search(cursor, user, [('invoice_id', '=', purchase.invoice_id.id),('state', '!=', 'cancel')])
78
            for pur in self.browse(cursor, user, purchase_ids):
79
                tot_pamount += pur.amount_untaxed
80
81
            if purchase.amount_untaxed and tot:
82
                result = tot * 100.0 / tot_pamount
83
                res[purchase.id] = result > 100 and 100.0 or result
84
            else:
85
                res[purchase.id] = 0.0
86
        return res
87
88
89
    _columns = {
90
        'group_picking_ids': fields.many2many('stock.picking', 'purchase_order_picking_group_rel', 'purchase_id', 'picking_id', 'Picking group'),
91
        'shipped_rate': fields.function(_shipped_rate, method=True, string='Received', type='float'),
92
        'invoiced_rate': fields.function(_invoiced_rate, method=True, string='Invoiced', type='float')
93
    }
94
95
96
    def action_merge_picking_assign(self, cr, uid, ids, *args):
97
        """assigns the group picking to active picking"""
98
        picking_id = False
99
100
        for order in self.browse(cr, uid, ids):
101
            if order.group_picking_ids:
102
                for pick in order.group_picking_ids:
103
                    if pick.state != 'cancel':
104
                        picking_id = pick.id
105
106
        return picking_id
107
108
    def check_can_cancel(self, cr, uid, ids, *args):
109
        """Checks if was created new group picking or relly the first group picking was cancelled"""
110
        for order in self.browse(cr, uid, ids):
111
            if order.group_picking_ids:
112
                for pick in order.group_picking_ids:
113
                    if pick.state != 'cancel':
114
                        return True
115
116
        return False
117
    
118
    def action_cancel(self, cr, uid, ids, context={}):
119
        """cancel lines on group picking if exists"""
120
        if context is None: context = {}
121
        res = super(purchase_order, self).action_cancel(cr, uid, ids, context=context)
122
123
        pickings = []
124
125
        for order in self.browse(cr, uid, ids):
126
            if order.group_picking_ids:
127
                for line in order.order_line:
128
                    moves = self.pool.get('stock.move').search(cr, uid, [('purchase_line_id', '=', line.id)])
129
                    for move in self.pool.get('stock.move').browse(cr, uid, moves):
130
                        if move.picking_id and move.picking_id.state in ('draft','cancel'):
131
                            pickings.append(move.picking_id.id)
132
                        elif move.picking_id:
133
                            raise osv.except_osv(
134
                                _('Could not cancel purchase order !'),
135
                                _('You must first cancel all packing attached to this purchase order.'))
136
                    if moves:
137
                        self.pool.get('stock.move').action_cancel(cr, uid, moves)
138
139
        if pickings:
140
            pickings = list(set(pickings))
141
142
            for picking in self.pool.get('stock.picking').browse(cr, uid, pickings):
143
                cancel = True
144
                for move in picking.move_lines:
145
                    if move.state != 'cancel':
146
                        cancel = False
147
                        break
148
149
                if cancel:
150
                    wf_service = netsvc.LocalService("workflow")
151
                    wf_service.trg_validate(uid, 'stock.picking', picking.id, 'button_cancel', cr)
152
153
        return res
154
    
155
purchase_order()