~jo0thlt/sfsoluciones/mtf

« back to all changes in this revision

Viewing changes to sfs_product_qty_available/product.py

  • Committer: contact at zbeanztech
  • Date: 2014-11-07 18:54:14 UTC
  • Revision ID: contact@zbeanztech.com-20141107185414-tzybvn7293tju4ce
 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Copyright (c) 2014 SF Soluciones.
 
5
#    (http://www.sfsoluciones.com)
 
6
#    contacto@sfsoluciones.com
 
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
from osv import osv
 
24
 
 
25
class product_product(osv.osv):
 
26
    _inherit = 'product.product'
 
27
    
 
28
    def set_qty_available_function(self, cr, uid, context=None):
 
29
        cr.execute("""drop function if exists get_qty_onhand(product_id integer);
 
30
                   CREATE OR REPLACE FUNCTION get_qty_onhand
 
31
                   (x_product_id integer)
 
32
                   RETURNS TABLE(qty float) AS
 
33
                   $BODY$
 
34
                   BEGIN
 
35
                   return query
 
36
                   select round(coalesce(sum(x.product_qty)::float, 0.00))
 
37
                   from((SELECT
 
38
                        coalesce(sum(-m.product_qty * pu.factor / pu2.factor)::float, 0.0) as product_qty
 
39
                   FROM
 
40
                    stock_move m
 
41
                        LEFT JOIN stock_picking p ON (m.picking_id=p.id)
 
42
                        LEFT JOIN product_product pp ON (m.product_id=pp.id)
 
43
                        LEFT JOIN product_template pt ON (pp.product_tmpl_id=pt.id)
 
44
                        LEFT JOIN product_uom pu ON (pt.uom_id=pu.id)
 
45
                        LEFT JOIN product_uom pu2 ON (m.product_uom=pu2.id)
 
46
                        LEFT JOIN product_uom u ON (m.product_uom=u.id)
 
47
                        LEFT JOIN stock_location l ON (m.location_id=l.id)
 
48
                        where m.product_id = x_product_id and l.usage = 'internal' and m.state = 'done'
 
49
                   GROUP BY
 
50
                        m.id, m.product_id, m.product_uom, pt.categ_id, m.location_id,  m.location_dest_id,
 
51
                        m.prodlot_id, m.date, m.state, l.usage, m.company_id, pt.uom_id)
 
52
                   UNION ALL (
 
53
                   SELECT
 
54
                   coalesce(sum(m.product_qty * pu.factor / pu2.factor)::decimal, 0.0) as product_qty
 
55
                   FROM
 
56
                    stock_move m
 
57
                        LEFT JOIN stock_picking p ON (m.picking_id=p.id)
 
58
                        LEFT JOIN product_product pp ON (m.product_id=pp.id)
 
59
                        LEFT JOIN product_template pt ON (pp.product_tmpl_id=pt.id)
 
60
                        LEFT JOIN product_uom pu ON (pt.uom_id=pu.id)
 
61
                        LEFT JOIN product_uom pu2 ON (m.product_uom=pu2.id)
 
62
                        LEFT JOIN product_uom u ON (m.product_uom=u.id)
 
63
                        LEFT JOIN stock_location l ON (m.location_dest_id=l.id)
 
64
                        where m.product_id = x_product_id and l.usage='internal' and m.state = 'done'
 
65
                   GROUP BY
 
66
                        m.id, m.product_id, m.product_uom, pt.categ_id, m.location_id, m.location_dest_id,
 
67
                        m.prodlot_id, m.date, m.state, l.usage, m.company_id, pt.uom_id
 
68
                    ))x;
 
69
                   END
 
70
                   $BODY$
 
71
                   LANGUAGE 'plpgsql';""")
 
72
        return True
 
73
 
 
74
product_product()
 
75
 
 
76
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: