Launchpad automatic translations update.
[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 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, objects):
95         #code for the sum of discount value
96         return reduce(lambda acc, object:
97                                         acc + reduce(
98                                                 lambda sum_dis, line:
99                                                         sum_dis + ((line.price_unit * line.qty) * (line.discount / 100)),
100                                                 object.lines,
101                                                 0.0),
102                                     objects,
103                                     0.0)
104
105     def _get_payments(self, form):
106         statement_line_obj = self.pool.get("account.bank.statement.line")
107         pos_order_obj = self.pool.get("pos.order")
108         user_ids = form['user_ids'] or self._get_all_users()
109         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)])
110         data={}
111         if pos_ids:
112             st_line_ids = statement_line_obj.search(self.cr, self.uid, [('pos_statement_id', 'in', pos_ids)])
113             if st_line_ids:
114                 st_id = statement_line_obj.browse(self.cr, self.uid, st_line_ids)
115                 a_l=[]
116                 for r in st_id:
117                     a_l.append(r['id'])
118                 self.cr.execute("select aj.name,sum(amount) from account_bank_statement_line as absl,account_bank_statement as abs,account_journal as aj " \
119                                 "where absl.statement_id = abs.id and abs.journal_id = aj.id  and absl.id IN %s " \
120                                 "group by aj.name ",(tuple(a_l),))
121
122                 data = self.cr.dictfetchall()
123                 return data
124         else:
125             return {}
126
127     def _total_of_the_day(self, objects):
128         if self.total:
129              if self.total == self.total_invoiced:
130                  return self.total
131              else:
132                  return ((self.total or 0.00) - (self.total_invoiced or 0.00))
133         else:
134             return False
135
136     def _sum_invoice(self, objects):
137         return reduce(lambda acc, obj:
138                         acc + obj.invoice_id.amount_total,
139                         [o for o in objects if o.invoice_id and o.invoice_id.number],
140                         0.0)
141
142     def _ellipsis(self, orig_str, maxlen=100, ellipsis='...'):
143         maxlen = maxlen - len(ellipsis)
144         if maxlen <= 0:
145             maxlen = 1
146         new_str = orig_str[:maxlen]
147         return new_str
148
149     def _strip_name(self, name, maxlen=50):
150         return self._ellipsis(name, maxlen, ' ...')
151
152     def _get_tax_amount(self, form):
153         res = {}
154         temp = {}
155         list_ids = []
156         temp2 = 0.0
157         user_ids = form['user_ids'] or self._get_all_users()
158         pos_order_obj = self.pool.get('pos.order')
159         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)])
160         temp.update({'name': ''})
161         for order in pos_order_obj.browse(self.cr, self.uid, pos_ids):
162             temp2 += order.amount_tax
163             for line in order.lines:
164                 if len(line.product_id.taxes_id):
165                     tax = line.product_id.taxes_id[0]
166                     res[tax.name] = (line.price_unit * line.qty * (1-(line.discount or 0.0) / 100.0)) + (tax.id in list_ids and res[tax.name] or 0)
167                     list_ids.append(tax.id)
168                     temp.update({'name': tax.name})
169         temp.update({'amount': temp2})
170         return [temp] or False
171
172     def _get_user_names(self, user_ids):
173         user_obj = self.pool.get('res.users')
174         return ', '.join(map(lambda x: x.name, user_obj.browse(self.cr, self.uid, user_ids)))
175
176     def __init__(self, cr, uid, name, context):
177         super(pos_details, self).__init__(cr, uid, name, context=context)
178         self.total = 0.0
179         self.qty = 0.0
180         self.total_invoiced = 0.0
181         self.discount = 0.0
182         self.localcontext.update({
183             'time': time,
184             'strip_name': self._strip_name,
185             'getpayments': self._get_payments,
186             'getsumdisc': self._get_sum_dis_2,
187             'gettotalofthaday': self._total_of_the_day,
188             'gettaxamount': self._get_tax_amount,
189             'pos_sales_details':self._pos_sales_details,
190             'getqtytotal2': self._get_qty_total_2,
191             'getsalestotal2': self._get_sales_total_2,
192             'getsuminvoice2':self._get_sum_invoice_2,
193             'getpaidtotal2': self._paid_total_2,
194             'getinvoice':self._get_invoice,
195             'get_user_names': self._get_user_names,
196         })
197
198 report_sxw.report_sxw('report.pos.details', 'pos.order', 'addons/point_of_sale_singer/report/pos_details.rml', parser=pos_details, header='internal')
199
200 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: