[FIX] Specify the name of the argument for the context, to avoir a critical bug
[odoo/odoo.git] / addons / point_of_sale / report / pos_details_summary.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
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 import time
24 from report import report_sxw
25
26
27 class pos_details_summary(report_sxw.rml_parse):
28
29     def _get_qty_total(self, objects):
30         #code for the sum of qty_total
31         return reduce(lambda acc, object:
32                                         acc + reduce(
33                                                 lambda sum_qty, line:
34                                                         sum_qty + line.qty,
35                                                 object.lines,
36                                                 0 ),
37                                     objects,
38                                     0)
39
40     def _get_sum_discount(self, objects):
41         #code for the sum of discount value
42         return reduce(lambda acc, object:
43                                         acc + reduce(
44                                                 lambda sum_dis, line:
45                                                         sum_dis + ((line.price_unit * line.qty ) * (line.discount / 100)),
46                                                 object.lines,
47                                                 0.0),
48                                     objects,
49                                     0.0 )
50
51     def _get_payments(self, objects, ignore_gift=False):
52         gift_journal_id = None
53         if ignore_gift:
54             config_journal_ids = self.pool.get("pos.config.journal").search(self.cr, self.uid, [('code', '=', 'GIFT')])
55             if len(config_journal_ids):
56                 config_journal = self.pool.get("pos.config.journal").browse(self.cr, self.uid, config_journal_ids, {})[0]
57                 gift_journal_id = config_journal.journal_id.id
58
59         result = {}
60         for obj in objects:
61             for payment in obj.payments:
62                 if gift_journal_id and gift_journal_id == payment.journal_id.id:
63                     continue
64                 result[payment.journal_id.name] = result.get(payment.journal_id.name, 0.0) + payment.amount
65         return result
66
67     def _paid_total(self, objects):
68         return sum(self._get_payments(objects, True).values(), 0.0)
69
70     def _total_of_the_day(self, objects):
71         total_paid = sum(self._get_payments(objects, True).values(), 0.0)
72         total_invoiced = self._sum_invoice(objects)
73         return total_paid - total_invoiced
74
75     def _sum_invoice(self, objects):
76         return reduce(lambda acc, obj:
77                                                 acc + obj.invoice_id.amount_total,
78                                     [o for o in objects if o.invoice_id and o.invoice_id.number],
79                                     0.0)
80
81     def _ellipsis(self, string, maxlen=100, ellipsis = '...'):
82         ellipsis = ellipsis or ''
83         return string[:maxlen - len(ellipsis) ] + (ellipsis, '')[len(string) < maxlen]
84
85     def _strip_name(self, name, maxlen=50):
86         return self._ellipsis(name, maxlen, ' ...')
87
88     def _get_tax_amount(self, objects):
89         res = {}
90         list_ids = []
91         for order in objects:
92             for line in order.lines:
93                 if len(line.product_id.taxes_id):
94                     tax = line.product_id.taxes_id[0]
95                     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)
96                     list_ids.append(tax.id)
97         return res
98
99     def _get_sales_total(self, objects):
100         return reduce(lambda x, o: x + len(o.lines), objects, 0)
101
102     def _get_period(self, objects):
103         date_orders = [obj.date_order for obj in objects]
104         min_date = min(date_orders)
105         max_date = max(date_orders)
106         if min_date == max_date:
107             return '%s' % min_date
108         else:
109             return '%s - %s' % (min_date, max_date)
110
111     def __init__(self, cr, uid, name, context):
112         super(pos_details_summary, self).__init__(cr, uid, name, context=context)
113         self.total = 0.0
114         self.localcontext.update({
115             'time': time,
116             'strip_name': self._strip_name,
117             'getpayments': self._get_payments,
118             'getqtytotal': self._get_qty_total,
119             'getsumdisc': self._get_sum_discount,
120             'getpaidtotal': self._paid_total,
121             'gettotalofthaday': self._total_of_the_day,
122             'getsuminvoice': self._sum_invoice,
123             'gettaxamount': self._get_tax_amount,
124             'getsalestotal': self._get_sales_total,
125             'getperiod': self._get_period,
126         })
127
128 report_sxw.report_sxw('report.pos.details_summary',
129                                             'pos.order',
130                                             'addons/point_of_sale/report/pos_details_summary.rml',
131                                             parser=pos_details_summary,
132                                             header=None)
133
134 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
135