~shineit/openerp-china/pdf-font-for-any-language

« back to all changes in this revision

Viewing changes to shineit_mm/report/mm_report.py

  • Committer: JoshuaJan
  • Date: 2011-12-20 19:20:05 UTC
  • Revision ID: popkar77@gmail.com-20111220192005-hnf0tba7y7jip4bu
Add two Report

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding:utf-8 -*-
 
2
 
 
3
import time
 
4
import calendar
 
5
 
 
6
from report import report_sxw
 
7
 
 
8
class mm_report(report_sxw.rml_parse):
 
9
    def __init__(self, cr, uid, name, context=None):
 
10
        super(mm_report, self).__init__(cr, uid, name, context=context)
 
11
        self.localcontext.update({
 
12
            'time':time,
 
13
            'get_categories':self._get_categories,
 
14
            })
 
15
 
 
16
    def _get_inv_total(self, product_id,year_start,year_stop):
 
17
        so_obj = self.pool.get('sale.order')
 
18
        po_obj = self.pool.get('purchase.order')
 
19
        sol_obj = self.pool.get('sale.order.line')
 
20
        pol_obj = self.pool.get('purchase.order.line')
 
21
        inv_obj = self.pool.get('account.invoice')
 
22
        invl_obj = self.pool.get('account.invoice.line')
 
23
 
 
24
        result = {
 
25
                'c_inv_total':0.0,
 
26
                's_inv_total':0.0,
 
27
                }
 
28
        so_ids = so_obj.search(self.cr, self.uid, [('date_order','>=',year_start),('date_order','<=',year_stop),('state','=','done')])
 
29
        #print 'soid--->>>',so_ids
 
30
        for so in so_obj.browse(self.cr, self.uid, so_ids):
 
31
            so_inv_ids = inv_obj.search(self.cr,self.uid,[('origin','ilike',so.name),('state','in',['paid'])])
 
32
            so_invl_ids = invl_obj.search(self.cr, self.uid, [('product_id','=',product_id),('invoice_id','in',so_inv_ids)])
 
33
            #print 'so_invl--->',so_invl_ids
 
34
            for so_invl in invl_obj.browse(self.cr, self.uid, so_invl_ids):
 
35
                #print 'so_invl--->>',so_invl
 
36
                result['c_inv_total'] += so_invl.price_subtotal or 0.0
 
37
            #Find the PO invoice
 
38
            po_ids = po_obj.search(self.cr, self.uid,[('origin','ilike',so.name)])
 
39
            #print 'po_ids:',po_ids
 
40
            if po_ids:
 
41
                po = po_obj.browse(self.cr, self.uid, po_ids)
 
42
                #print 'po:',po
 
43
                po_inv_ids = inv_obj.search(self.cr,self.uid,[('origin','ilike',po[0].name),('state','in',['paid','open'])])
 
44
                #print 'po_inv-->>>',po_inv_ids
 
45
                po_invl_ids = invl_obj.search(self.cr, self.uid, [('product_id','=',product_id),('invoice_id','in',po_inv_ids)])
 
46
                #print 'po_invl--->',po_invl_ids
 
47
                for po_invl in invl_obj.browse(self.cr, self.uid, po_invl_ids):
 
48
                    #print 'invl--->>',po_invl
 
49
                    result['s_inv_total'] += po_invl.price_subtotal or 0.0
 
50
        #print 'result--->>>',result
 
51
        return result
 
52
            
 
53
        
 
54
    def _get_month_line(self, year, product_id, month):
 
55
        self.cr.execute('''SELECT count(sol.id),sum(sol.product_uom_qty) \
 
56
                            FROM sale_order_line AS sol \
 
57
                            LEFT JOIN sale_order AS so on(sol.order_id = so.id) \
 
58
                            WHERE '%s-%s-1'<=so.date_confirm \
 
59
                                AND so.date_confirm<='%s-%s-%s' \
 
60
                                AND product_id = %s'''%(year,month,year,month,calendar.monthrange(int(year),month)[1],product_id))
 
61
        res = self.cr.fetchall()
 
62
        #print 'res--->>>>',res
 
63
        month={
 
64
                'item':res[0][0] or 0.0,
 
65
                'sqm':res[0][1] or 0.0,
 
66
                }
 
67
        #print 'month--->>>',month
 
68
        return month
 
69
 
 
70
    def _get_categories(self,data):
 
71
        cat_ids=[]
 
72
        res=[]
 
73
        pro_ids=[]
 
74
        product_obj = self.pool.get('product.product')
 
75
        self.cr.execute('''SELECT sol.product_id FROM sale_order_line as sol LEFT JOIN sale_order AS so on (so.id = sol.order_id) WHERE (('%s'<=so.date_confirm and so.date_confirm<='%s') and so.state='done' and so.invoice_type_id='%s')  GROUP BY(sol.product_id) '''%(data['year_start'],data['year_stop'],data['inv_type']))
 
76
        product_ids = [pid[0] for pid in self.cr.fetchall()]
 
77
        print 'prodcut_ids---->>>>',product_ids
 
78
        products = product_obj.browse(self.cr, self.uid , list(product_ids)) 
 
79
        for product in products:
 
80
            pro_ids.append(product.id)
 
81
            if product.categ_id.id not in cat_ids:
 
82
                cat_ids.append(product.categ_id.id)
 
83
 
 
84
        cats = self.pool.get('product.category').name_get(self.cr, self.uid, cat_ids, context=self.localcontext)
 
85
        if not cats:
 
86
            return res
 
87
        year_total_month = {}
 
88
        year_total_item = 0.0
 
89
        year_total_sqm = 0.0
 
90
        year_total_c_inv = 0.0
 
91
        year_total_s_inv = 0.0
 
92
        for cat in cats:
 
93
            product_ids=product_obj.search(self.cr, self.uid, [('id', 'in', pro_ids), ('categ_id', '=', cat[0])], context=self.localcontext)
 
94
            products = []
 
95
            cat_total_item = 0
 
96
            cat_total_sqm = 0
 
97
            cat_total_month ={}
 
98
            cat_total_c_inv = 0.0
 
99
            cat_total_s_inv = 0.0
 
100
            for product in product_obj.read(self.cr, self.uid, product_ids, ['name', 'code'], context=self.localcontext):
 
101
                inv_total = self._get_inv_total(product['id'], data['year_start'], data['year_stop'])
 
102
                val = {
 
103
                     'id':product['id'],
 
104
                     'name':product['name'],
 
105
                     'product_total_item':0.0,
 
106
                     'product_total_sqm':0.0,
 
107
                     'product_total_month_sqm':0.0,
 
108
                     'c_inv_total':inv_total['c_inv_total'],
 
109
                     's_inv_total':inv_total['s_inv_total'],
 
110
                 }
 
111
                for month in range(1,13):
 
112
                    val[month]=self._get_month_line(data['year'], product['id'], month)
 
113
                    val['product_total_item'] += int(val[month].get('item',0))
 
114
                    val['product_total_sqm'] += int(val[month].get('sqm',0.0))
 
115
                    if not cat_total_month.get(month,False):
 
116
                        cat_total_month.update({month:{'item':0,'sqm':0}})
 
117
                    cat_total_month[month]['item'] += int(val[month].get('item',0)) 
 
118
                    cat_total_month[month]['sqm'] += int(val[month].get('sqm',0.0))
 
119
                    if not year_total_month.get(month,False):
 
120
                        year_total_month.update({month:{'item':0,'sqm':0}})
 
121
                    year_total_month[month]['item'] += int(val[month].get('item',0))
 
122
                    year_total_month[month]['sqm'] += int(val[month].get('sqm',0.0)) 
 
123
                    print 'year_total_month[month]-->',year_total_month 
 
124
                    cat_total_item += int(val[month].get('item',0))
 
125
                    cat_total_sqm += int(val[month].get('sqm',0.0))
 
126
                cat_total_c_inv += inv_total['c_inv_total']
 
127
                cat_total_s_inv += inv_total['s_inv_total']
 
128
                print 'year_month:',year_total_month
 
129
                products.append(val)
 
130
            year_total_item += cat_total_item
 
131
            year_total_sqm += cat_total_sqm
 
132
            year_total_c_inv += cat_total_c_inv
 
133
            year_total_s_inv += cat_total_s_inv
 
134
            res.append({
 
135
                        'name':cat[1],
 
136
                        'products':products,
 
137
                        'cat_total_item':cat_total_item,
 
138
                        'cat_total_sqm':cat_total_sqm,
 
139
                        'cat_total_month':cat_total_month,
 
140
                        'cat_total_c_inv':cat_total_c_inv,
 
141
                        'cat_total_s_inv':cat_total_s_inv,
 
142
                    })
 
143
        #print 'cat---->>>>',res
 
144
        self.localcontext.update({
 
145
            'year_total_month':year_total_month,
 
146
            'year_total_c_inv':year_total_c_inv,
 
147
            'year_total_s_inv':year_total_s_inv,
 
148
            'year_total_item':year_total_item,
 
149
            'year_total_sqm':year_total_sqm,
 
150
        })
 
151
        return res
 
152
 
 
153
 
 
154
report_sxw.report_sxw('report.mm','sale.order','my-addons/shineit_mm/report/mm.rml',parser=mm_report,header=False)
 
155
 
 
156
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: