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