~vauxoo/addons-vauxoo/6.0-trunk

« back to all changes in this revision

Viewing changes to partner_effective_sale/model/partner.py

[MERGE] from local .bzrignore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- encoding: utf-8 -*-
 
3
###########################################################################
 
4
#    Module Writen to OpenERP, Open Source Management Solution
 
5
#    Copyright (C) OpenERP Venezuela (<http://openerp.com.ve>).
 
6
#    All Rights Reserved
 
7
###############Credits######################################################
 
8
#    Coded by: Humberto Arocha <hbto@vauxoo.com>           
 
9
#    Audited by: Nhomar Hernandez <nhomar@vauxoo.com>
 
10
#############################################################################
 
11
#    This program is free software: you can redistribute it and/or modify
 
12
#    it under the terms of the GNU Affero General Public License as published by
 
13
#    the Free Software Foundation, either version 3 of the License, or
 
14
#    (at your option) any later version.
 
15
#
 
16
#    This program is distributed in the hope that it will be useful,
 
17
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
#    GNU Affero General Public License for more details.
 
20
#
 
21
#    You should have received a copy of the GNU Affero General Public License
 
22
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
23
################################################################################
 
24
 
 
25
import time
 
26
from osv import fields, osv
 
27
from tools.translate import _
 
28
 
 
29
class Partner(osv.osv):
 
30
    _inherit = 'res.partner'
 
31
 
 
32
    def _fnct_search_date(self, cr, uid, obj, name, args, context=None):
 
33
        if not len(args):                                                       
 
34
            return []                                                           
 
35
        context = context or {}
 
36
        res = []
 
37
        ids = self.search(cr,uid,[],context=context)
 
38
        if not ids:                                                                
 
39
            return [('id', '=', 0)]                                                
 
40
        model = name=='sale_order_date' and 'sale_order' or 'account_invoice'
 
41
        fieldname = name=='sale_order_date' and 'date_order' or 'date_invoice'
 
42
        query='''
 
43
            SELECT partner_id as id, '''+fieldname+'''
 
44
            FROM (
 
45
                SELECT t2.partner_id, t2.'''+fieldname+'''
 
46
                FROM '''+model+''' t2
 
47
                WHERE (t2.partner_id,t2.'''+fieldname+''') IN (
 
48
                    SELECT t1.partner_id,t1.'''+fieldname+'''
 
49
                    FROM '''+model+''' t1
 
50
                    WHERE t1.partner_id = t2.partner_id
 
51
                    ORDER BY t1.'''+fieldname+'''
 
52
                    LIMIT 1)
 
53
            ) v
 
54
            WHERE partner_id IN %s
 
55
                AND '''+fieldname+'''%s\'%s\';'''
 
56
 
 
57
        for arg in args:                                                        
 
58
            if arg[1] in ('=','>=','<=','>','<') and arg[2]:
 
59
                cr.execute(query%(str(tuple(ids)),arg[1],arg[2]))
 
60
                res += [i[0] for i in cr.fetchall()]
 
61
        if not res:                                                                
 
62
            return [('id', '=', 0)]                                                
 
63
        return [('id', 'in', res)]    
 
64
 
 
65
    def _fnct_get_date(self, cr, uid, ids, fieldname, arg, context=None):
 
66
        context = context or {}
 
67
        res = {}.fromkeys(ids,None)
 
68
        so_obj = self.pool.get('sale.order')
 
69
        inv_obj = self.pool.get('account.invoice')
 
70
        for id in ids:
 
71
            s_id = so_obj.search(cr, uid,
 
72
                    [('partner_id','=',id)],order='date_order asc',limit=1) or []
 
73
            i_id = inv_obj.search(cr, uid,
 
74
                    #TODO: in the future set args for state in ['open','paid']
 
75
                    [('partner_id','=',id),('type','=','out_invoice')],
 
76
                    order='date_invoice asc',limit=1) or []
 
77
            res[id]= {
 
78
                'sale_order_date' : s_id and \
 
79
                    so_obj.browse(cr,uid,s_id[0],context=context).date_order \
 
80
                    or None,
 
81
                'invoice_date':  i_id and \
 
82
                    inv_obj.browse(cr,uid,i_id[0],context=context).date_invoice \
 
83
                    or None,}
 
84
        return res
 
85
 
 
86
    _columns = {
 
87
        'sale_order_date':fields.function(
 
88
            _fnct_get_date,
 
89
            method = True,
 
90
            type = 'date',
 
91
            string = 'First Sale Order',
 
92
            multi='sale_invoice',
 
93
            fnct_search=_fnct_search_date,
 
94
            ),
 
95
        'invoice_date':fields.function(
 
96
            _fnct_get_date,
 
97
            method = True,
 
98
            type = 'date',
 
99
            string = 'First Sale Invoice',
 
100
            multi='sale_invoice',
 
101
            fnct_search=_fnct_search_date,
 
102
            ),
 
103
    }
 
104
Partner()