[FIX] stock: use correct vals in grouped invoice (opw 606535)
[odoo/odoo.git] / addons / point_of_sale / report / pos_details.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import time
23 from openerp.report import report_sxw
24
25 class pos_details(report_sxw.rml_parse):
26
27     def _get_invoice(self, inv_id):
28         res={}
29         if inv_id:
30             self.cr.execute("select number from account_invoice as ac where id = %s", (inv_id,))
31             res = self.cr.fetchone()
32             return res[0] or 'Draft'
33         else:
34             return  ''
35
36     def _get_all_users(self):
37         user_obj = self.pool.get('res.users')
38         return user_obj.search(self.cr, self.uid, [])
39
40     def _pos_sales_details(self, form):
41         pos_obj = self.pool.get('pos.order')
42         user_obj = self.pool.get('res.users')
43         data = []
44         result = {}
45         user_ids = form['user_ids'] or self._get_all_users()
46         company_id = user_obj.browse(self.cr, self.uid, self.uid).company_id.id
47         pos_ids = pos_obj.search(self.cr, self.uid, [('date_order','>=',form['date_start'] + ' 00:00:00'),('date_order','<=',form['date_end'] + ' 23:59:59'),('user_id','in',user_ids),('state','in',['done','paid','invoiced']),('company_id','=',company_id)])
48         for pos in pos_obj.browse(self.cr, self.uid, pos_ids):
49             for pol in pos.lines:
50                 result = {
51                     'code': pol.product_id.default_code,
52                     'name': pol.product_id.name,
53                     'invoice_id': pos.invoice_id.id, 
54                     'price_unit': pol.price_unit, 
55                     'qty': pol.qty, 
56                     'discount': pol.discount, 
57                     'total': (pol.price_unit * pol.qty * (1 - (pol.discount) / 100.0)), 
58                     'date_order': pos.date_order, 
59                     'pos_name': pos.name, 
60                     'uom': pol.product_id.uom_id.name
61                 }
62                 data.append(result)
63                 self.total += result['total']
64                 self.qty += result['qty']
65                 self.discount += result['discount']
66         if data:
67             return data
68         else:
69             return {}
70
71     def _get_qty_total_2(self):
72         return self.qty
73
74     def _get_sales_total_2(self):
75         return self.total
76
77     def _get_sum_invoice_2(self, form):
78         pos_obj = self.pool.get('pos.order')
79         user_obj = self.pool.get('res.users')
80         user_ids = form['user_ids'] or self._get_all_users()
81         company_id = user_obj.browse(self.cr, self.uid, self.uid).company_id.id
82         pos_ids = pos_obj.search(self.cr, self.uid, [('date_order','>=',form['date_start'] + ' 00:00:00'),('date_order','<=',form['date_end'] + ' 23:59:59'),('user_id','in',user_ids),('company_id','=',company_id),('invoice_id','<>',False)])
83         for pos in pos_obj.browse(self.cr, self.uid, pos_ids):
84             for pol in pos.lines:
85                 self.total_invoiced += (pol.price_unit * pol.qty * (1 - (pol.discount) / 100.0))
86         return self.total_invoiced or False
87
88     def _paid_total_2(self):
89         return self.total or 0.0
90
91     def _get_sum_dis_2(self):
92         return self.discount or 0.0
93
94     def _get_sum_discount(self, form):
95         #code for the sum of discount value
96         pos_obj = self.pool.get('pos.order')
97         user_obj = self.pool.get('res.users')
98         user_ids = form['user_ids'] or self._get_all_users()
99         company_id = user_obj.browse(self.cr, self.uid, self.uid).company_id.id
100         pos_ids = pos_obj.search(self.cr, self.uid, [('date_order','>=',form['date_start'] + ' 00:00:00'),('date_order','<=',form['date_end'] + ' 23:59:59'),('user_id','in',user_ids),('company_id','=',company_id)])
101         for pos in pos_obj.browse(self.cr, self.uid, pos_ids):
102             for pol in pos.lines:
103                 self.total_discount += ((pol.price_unit * pol.qty) * (pol.discount / 100))
104         return self.total_discount or False
105
106     def _get_payments(self, form):
107         statement_line_obj = self.pool.get("account.bank.statement.line")
108         pos_order_obj = self.pool.get("pos.order")
109         user_ids = form['user_ids'] or self._get_all_users()
110         pos_ids = pos_order_obj.search(self.cr, self.uid, [('date_order','>=',form['date_start'] + ' 00:00:00'),('date_order','<=',form['date_end'] + ' 23:59:59'),('state','in',['paid','invoiced','done']),('user_id','in',user_ids)])
111         data={}
112         if pos_ids:
113             st_line_ids = statement_line_obj.search(self.cr, self.uid, [('pos_statement_id', 'in', pos_ids)])
114             if st_line_ids:
115                 st_id = statement_line_obj.browse(self.cr, self.uid, st_line_ids)
116                 a_l=[]
117                 for r in st_id:
118                     a_l.append(r['id'])
119                 self.cr.execute("select aj.name,sum(amount) from account_bank_statement_line as absl,account_bank_statement as abs,account_journal as aj " \
120                                 "where absl.statement_id = abs.id and abs.journal_id = aj.id  and absl.id IN %s " \
121                                 "group by aj.name ",(tuple(a_l),))
122
123                 data = self.cr.dictfetchall()
124                 return data
125         else:
126             return {}
127
128     def _total_of_the_day(self, objects):
129         if self.total:
130              if self.total == self.total_invoiced:
131                  return self.total
132              else:
133                  return ((self.total or 0.00) - (self.total_invoiced or 0.00))
134         else:
135             return False
136
137     def _sum_invoice(self, objects):
138         return reduce(lambda acc, obj:
139                         acc + obj.invoice_id.amount_total,
140                         [o for o in objects if o.invoice_id and o.invoice_id.number],
141                         0.0)
142
143     def _ellipsis(self, orig_str, maxlen=100, ellipsis='...'):
144         maxlen = maxlen - len(ellipsis)
145         if maxlen <= 0:
146             maxlen = 1
147         new_str = orig_str[:maxlen]
148         return new_str
149
150     def _strip_name(self, name, maxlen=50):
151         return self._ellipsis(name, maxlen, ' ...')
152
153     def _get_tax_amount(self, form):
154         taxes = {}
155         account_tax_obj = self.pool.get('account.tax')
156         user_ids = form['user_ids'] or self._get_all_users()
157         pos_order_obj = self.pool.get('pos.order')
158         pos_ids = pos_order_obj.search(self.cr, self.uid, [('date_order','>=',form['date_start'] + ' 00:00:00'),('date_order','<=',form['date_end'] + ' 23:59:59'),('state','in',['paid','invoiced','done']),('user_id','in',user_ids)])
159         for order in pos_order_obj.browse(self.cr, self.uid, pos_ids):
160             for line in order.lines:
161                 line_taxes = account_tax_obj.compute_all(self.cr, self.uid, line.product_id.taxes_id, line.price_unit, line.qty, product=line.product_id, partner=line.order_id.partner_id or False)
162                 for tax in line_taxes['taxes']:
163                     taxes.setdefault(tax['id'], {'name': tax['name'], 'amount':0.0})
164                     taxes[tax['id']]['amount'] += tax['amount']
165         return taxes.values()
166
167     def _get_user_names(self, user_ids):
168         user_obj = self.pool.get('res.users')
169         return ', '.join(map(lambda x: x.name, user_obj.browse(self.cr, self.uid, user_ids)))
170
171     def __init__(self, cr, uid, name, context):
172         super(pos_details, self).__init__(cr, uid, name, context=context)
173         self.total = 0.0
174         self.qty = 0.0
175         self.total_invoiced = 0.0
176         self.discount = 0.0
177         self.total_discount = 0.0
178         self.localcontext.update({
179             'time': time,
180             'strip_name': self._strip_name,
181             'getpayments': self._get_payments,
182             'getsumdisc': self._get_sum_discount,
183             'gettotalofthaday': self._total_of_the_day,
184             'gettaxamount': self._get_tax_amount,
185             'pos_sales_details':self._pos_sales_details,
186             'getqtytotal2': self._get_qty_total_2,
187             'getsalestotal2': self._get_sales_total_2,
188             'getsuminvoice2':self._get_sum_invoice_2,
189             'getpaidtotal2': self._paid_total_2,
190             'getinvoice':self._get_invoice,
191             'get_user_names': self._get_user_names,
192         })
193
194 report_sxw.report_sxw('report.pos.details', 'pos.order', 'addons/point_of_sale_singer/report/pos_details.rml', parser=pos_details, header='internal')
195
196 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: