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