~pexego/openobject-addons/6.1-pexego-sale_commission

« back to all changes in this revision

Viewing changes to stock_block_prodlots/block_prodlots_plpgsql_functions.py

  • Committer: Omar (pexego)
  • Date: 2012-07-27 08:40:22 UTC
  • Revision ID: omar@pexego.es-20120727084022-qp3ludpr3vsuyuf6
[ADD] Traceability modules ported to 6.1

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-2011 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
"""Creates functions to search prodlots to block"""
 
24
 
 
25
from osv import osv
 
26
 
 
27
class plpgsql_block_prodlots(osv.osv):
 
28
    """Creates functions to search prodlots to block"""
 
29
    _name = "plpgsql.block.prodlots"
 
30
    _description = "Creates functions to search prodlots to block"
 
31
    _auto = False
 
32
 
 
33
    def init(self, cr):
 
34
        """creates functions when install"""
 
35
        #check if exists the language plpgsql in bbdd, if not, is created for can create functions in this language
 
36
        cr.execute("select * from pg_language where lanname = 'plpgsql'")
 
37
        if not cr.rowcount:
 
38
            cr.execute("create language 'plpgsql'")
 
39
 
 
40
        #creates a function. From int parameter (prodlot_id) returns all prodlots child of specific
 
41
        cr.execute("""CREATE OR REPLACE FUNCTION block_prodlots_up(integer)
 
42
        RETURNS SETOF stock_production_lot AS
 
43
        $BODY$
 
44
            declare parent_move record;
 
45
                    child_prodlot record;
 
46
                    result record;
 
47
            BEGIN
 
48
                 FOR parent_move IN select distinct stock_move.* from stock_move
 
49
                 inner join stock_move_history_ids on stock_move.id = parent_id
 
50
                 where prodlot_id = $1 and state != 'cancel'
 
51
                 LOOP
 
52
                    FOR child_prodlot IN select distinct stock_production_lot.* from stock_move
 
53
                    inner join stock_move_history_ids on child_id = stock_move.id
 
54
                    inner join stock_production_lot on stock_production_lot.id = stock_move.prodlot_id
 
55
                    where parent_id = parent_move.id and stock_move.state != 'cancel' and stock_production_lot.id != $1
 
56
                    LOOP
 
57
                        FOR result in select distinct * from block_prodlots_up(child_prodlot.id)
 
58
                        LOOP
 
59
                            return next result;
 
60
                        END LOOP;
 
61
                        return next child_prodlot;
 
62
                    END LOOP;
 
63
                END LOOP;
 
64
            END; $BODY$
 
65
        LANGUAGE 'plpgsql';""")
 
66
 
 
67
        #creates a function. From int parameter return the stock_move last childs for this paramenter
 
68
        cr.execute("""CREATE OR REPLACE FUNCTION block_prodlots_down(integer)
 
69
        RETURNS SETOF stock_production_lot AS
 
70
        $BODY$
 
71
            declare child_move record;
 
72
                    parent_prodlot record;
 
73
                    result record;
 
74
            BEGIN
 
75
                 FOR child_move IN select distinct stock_move.* from stock_move
 
76
                 inner join stock_move_history_ids on stock_move.id = child_id
 
77
                 where prodlot_id = $1 and state != 'cancel'
 
78
                 LOOP
 
79
                    FOR parent_prodlot IN select distinct stock_production_lot.* from stock_move
 
80
                    inner join stock_move_history_ids on parent_id = stock_move.id
 
81
                    inner join stock_production_lot on stock_production_lot.id = stock_move.prodlot_id
 
82
                    where child_id = child_move.id and stock_move.state != 'cancel' and stock_production_lot.id != $1
 
83
                    LOOP
 
84
                        FOR result in select distinct * from block_prodlots_down(parent_prodlot.id)
 
85
                        LOOP
 
86
                            return next result;
 
87
                        END LOOP;
 
88
                        return next parent_prodlot;
 
89
                    END LOOP;
 
90
                END LOOP;
 
91
            END; $BODY$
 
92
        LANGUAGE 'plpgsql';""")
 
93
 
 
94
plpgsql_block_prodlots()
 
 
b'\\ No newline at end of file'