1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
23 from openerp.report import report_sxw
25 class pos_details(report_sxw.rml_parse):
27 def _get_invoice(self, 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'
36 def _get_all_users(self):
37 user_obj = self.pool.get('res.users')
38 return user_obj.search(self.cr, self.uid, [])
40 def _pos_sales_details(self, form):
41 pos_obj = self.pool.get('pos.order')
42 user_obj = self.pool.get('res.users')
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):
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,
56 'discount': pol.discount,
57 'total': (pol.price_unit * pol.qty * (1 - (pol.discount) / 100.0)),
58 'date_order': pos.date_order,
60 'uom': pol.product_id.uom_id.name
63 self.total += result['total']
64 self.qty += result['qty']
65 self.discount += result['discount']
71 def _get_qty_total_2(self):
74 def _get_sales_total_2(self):
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):
85 self.total_invoiced += (pol.price_unit * pol.qty * (1 - (pol.discount) / 100.0))
86 return self.total_invoiced or False
88 def _paid_total_2(self):
89 return self.total or 0.0
91 def _get_sum_dis_2(self):
92 return self.discount or 0.0
94 def _get_sum_discount(self, form):
95 #code for the sum of discount value
96 pos_obj = self.pool.get('pos.order')
97 user_obj = self.pool.get('res.users')
98 user_ids = form['user_ids'] or self._get_all_users()
99 company_id = user_obj.browse(self.cr, self.uid, self.uid).company_id.id
100 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)])
101 for pos in pos_obj.browse(self.cr, self.uid, pos_ids):
102 for pol in pos.lines:
103 self.total_discount += ((pol.price_unit * pol.qty) * (pol.discount / 100))
104 return self.total_discount or False
106 def _get_payments(self, form):
107 statement_line_obj = self.pool.get("account.bank.statement.line")
108 pos_order_obj = self.pool.get("pos.order")
109 user_ids = form['user_ids'] or self._get_all_users()
110 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)])
113 st_line_ids = statement_line_obj.search(self.cr, self.uid, [('pos_statement_id', 'in', pos_ids)])
115 st_id = statement_line_obj.browse(self.cr, self.uid, st_line_ids)
119 self.cr.execute("select aj.name,sum(amount) from account_bank_statement_line as absl,account_bank_statement as abs,account_journal as aj " \
120 "where absl.statement_id = abs.id and abs.journal_id = aj.id and absl.id IN %s " \
121 "group by aj.name ",(tuple(a_l),))
123 data = self.cr.dictfetchall()
128 def _total_of_the_day(self, objects):
130 if self.total == self.total_invoiced:
133 return ((self.total or 0.00) - (self.total_invoiced or 0.00))
137 def _sum_invoice(self, objects):
138 return reduce(lambda acc, obj:
139 acc + obj.invoice_id.amount_total,
140 [o for o in objects if o.invoice_id and o.invoice_id.number],
143 def _ellipsis(self, orig_str, maxlen=100, ellipsis='...'):
144 maxlen = maxlen - len(ellipsis)
147 new_str = orig_str[:maxlen]
150 def _strip_name(self, name, maxlen=50):
151 return self._ellipsis(name, maxlen, ' ...')
153 def _get_tax_amount(self, form):
155 account_tax_obj = self.pool.get('account.tax')
156 user_ids = form['user_ids'] or self._get_all_users()
157 pos_order_obj = self.pool.get('pos.order')
158 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)])
159 for order in pos_order_obj.browse(self.cr, self.uid, pos_ids):
160 for line in order.lines:
161 line_taxes = account_tax_obj.compute_all(self.cr, self.uid, line.product_id.taxes_id, line.price_unit, line.qty, product=line.product_id, partner=line.order_id.partner_id or False)
162 for tax in line_taxes['taxes']:
163 taxes.setdefault(tax['id'], {'name': tax['name'], 'amount':0.0})
164 taxes[tax['id']]['amount'] += tax['amount']
165 return taxes.values()
167 def _get_user_names(self, user_ids):
168 user_obj = self.pool.get('res.users')
169 return ', '.join(map(lambda x: x.name, user_obj.browse(self.cr, self.uid, user_ids)))
171 def __init__(self, cr, uid, name, context):
172 super(pos_details, self).__init__(cr, uid, name, context=context)
175 self.total_invoiced = 0.0
177 self.total_discount = 0.0
178 self.localcontext.update({
180 'strip_name': self._strip_name,
181 'getpayments': self._get_payments,
182 'getsumdisc': self._get_sum_discount,
183 'gettotalofthaday': self._total_of_the_day,
184 'gettaxamount': self._get_tax_amount,
185 'pos_sales_details':self._pos_sales_details,
186 'getqtytotal2': self._get_qty_total_2,
187 'getsalestotal2': self._get_sales_total_2,
188 'getsuminvoice2':self._get_sum_invoice_2,
189 'getpaidtotal2': self._paid_total_2,
190 'getinvoice':self._get_invoice,
191 'get_user_names': self._get_user_names,
194 report_sxw.report_sxw('report.pos.details', 'pos.order', 'addons/point_of_sale_singer/report/pos_details.rml', parser=pos_details, header='internal')
196 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: