added from extra-addons
[odoo/odoo.git] / addons / point_of_sale / report / pos_details.py
1 # -*- encoding: utf-8 -*-
2 ###############################################################################
3 ##
4 ## Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 ##
6 ## WARNING: This program as such is intended to be used by professional
7 ## programmers who take the whole responsability of assessing all potential
8 ## consequences resulting from its eventual inadequacies and bugs
9 ## End users who are looking for a ready-to-use solution with commercial
10 ## garantees and support are strongly adviced to contract a Free Software
11 ## Service Company
12 ##
13 ## This program is Free Software; you can redistribute it and/or
14 ## modify it under the terms of the GNU General Public License
15 ## as published by the Free Software Foundation; either version 2
16 ## of the License, or (at your option) any later version.
17 ##
18 ## This program is distributed in the hope that it will be useful,
19 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ## GNU General Public License for more details.
22 ##
23 ## You should have received a copy of the GNU General Public License
24 ## along with this program; if not, write to the Free Software
25 ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 ##
27 ###############################################################################
28
29 import time
30 from report import report_sxw
31
32
33 class pos_details(report_sxw.rml_parse):
34
35     def _get_qty_total(self, objects):
36         #code for the sum of qty_total
37         return reduce(lambda acc, object:
38                                         acc + reduce(
39                                                 lambda sum_qty, line:
40                                                         sum_qty + line.qty,
41                                                 object.lines,
42                                                 0),
43                                     objects,
44                                     0)
45
46     def _get_sum_discount(self, objects):
47         #code for the sum of discount value
48         return reduce(lambda acc, object:
49                                         acc + reduce(
50                                                 lambda sum_dis, line:
51                                                         sum_dis + ((line.price_unit * line.qty) * (line.discount / 100)),
52                                                 object.lines,
53                                                 0.0),
54                                     objects,
55                                     0.0)
56
57     def _get_payments(self, objects, ignore_gift=False):
58         gift_journal_id = None
59         if ignore_gift:
60             config_journal_ids = self.pool.get("pos.config.journal").search(self.cr, self.uid, [('code', '=', 'GIFT')])
61             if len(config_journal_ids):
62                 config_journal = self.pool.get("pos.config.journal").browse(self.cr, self.uid, config_journal_ids, {})[0]
63                 gift_journal_id = config_journal.journal_id.id
64
65         result = {}
66         for obj in objects:
67             for payment in obj.payments:
68                 if gift_journal_id and gift_journal_id == payment.journal_id.id:
69                     continue
70                 result[payment.journal_id.name] = result.get(payment.journal_id.name, 0.0) + payment.amount
71         return result
72
73     def _paid_total(self, objects):
74         return sum(self._get_payments(objects, True).values(), 0.0)
75
76     def _total_of_the_day(self, objects):
77         total_paid = sum(self._get_payments(objects, True).values(), 0.0)
78         total_invoiced = self._sum_invoice(objects)
79         return total_paid - total_invoiced
80
81     def _sum_invoice(self, objects):
82         return reduce(lambda acc, obj:
83                         acc + obj.invoice_id.amount_total,
84                         [o for o in objects if o.invoice_id and o.invoice_id.number],
85                         0.0)
86
87     def _ellipsis(self, orig_str, maxlen=100, ellipsis='...'):
88         maxlen = maxlen - len(ellipsis)
89         if maxlen <= 0:
90             maxlen = 1
91         new_str = orig_str[:maxlen]
92         return new_str
93
94     def _strip_name(self, name, maxlen=50):
95         return self._ellipsis(name, maxlen, ' ...')
96
97     def _get_tax_amount(self, objects):
98         res = {}
99         list_ids = []
100         for order in objects:
101             for line in order.lines:
102                 if len(line.product_id.taxes_id):
103                     tax = line.product_id.taxes_id[0]
104                     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)
105                     list_ids.append(tax.id)
106         return res
107
108     def _get_sales_total(self, objects):
109         return reduce(lambda x, o: x + len(o.lines), objects, 0)
110
111     def _get_period(self, objects):
112         date_orders = [obj.date_order for obj in objects]
113         min_date = min(date_orders)
114         max_date = max(date_orders)
115         if min_date == max_date:
116             return '%s' % min_date
117         else:
118             return '%s - %s' % (min_date, max_date)
119
120     def __init__(self, cr, uid, name, context):
121         super(pos_details, self).__init__(cr, uid, name, context)
122         self.total = 0.0
123         self.localcontext.update({
124             'time': time,
125             'strip_name': self._strip_name,
126             'getpayments': self._get_payments,
127             'getqtytotal': self._get_qty_total,
128             'getsumdisc': self._get_sum_discount,
129             'getpaidtotal': self._paid_total,
130             'gettotalofthaday': self._total_of_the_day,
131             'getsuminvoice': self._sum_invoice,
132             'gettaxamount': self._get_tax_amount,
133             'getsalestotal': self._get_sales_total,
134             'getperiod': self._get_period,
135         })
136
137 report_sxw.report_sxw('report.pos.details', 'pos.order', 'addons/point_of_sale/report/pos_details.rml', parser=pos_details, header=None)
138
139 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
140