1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
21 ##############################################################################
24 from report import report_sxw
27 class pos_details(report_sxw.rml_parse):
29 def _get_invoice(self,inv_id,user):
31 self.cr.execute("select name from account_invoice as ac where id = %d" %(inv_id))
32 res = self.cr.fetchone()
38 def _pos_sales_details(self,form,user):
40 self.cr.execute ("select po.name as pos_name,po.date_order,pt.name ,pol.qty,pol.price_unit,pol.discount,po.invoice_id,sum((pol.price_unit * pol.qty * (1 - (pol.discount) / 100.0))) as Total " \
41 "from pos_order as po,pos_order_line as pol,product_product as pp,product_template as pt,res_users as ru,res_company as rc " \
42 "where pt.id=pp.product_tmpl_id and pp.id=pol.product_id and po.id = pol.order_id and po.state in ('done','paid','invoiced') " \
43 "and to_char(date_trunc('day',po.date_order),'YYYY-MM-DD')::date >= %s and to_char(date_trunc('day',po.date_order),'YYYY-MM-DD')::date <= %s " \
44 "and po.user_id = ru.id and rc.id = %s and ru.id = %s " \
45 "group by po.name,pol.qty,po.date_order,pt.name,pol.price_unit,pol.discount,po.invoice_id " \
46 ,(form['date_start'],form['date_end'],str(user.company_id.id),str(self.uid)))
47 data=self.cr.dictfetchall()
50 self.total += d['total']
56 def _get_qty_total_2(self, form,user):
58 self.cr.execute("select sum(pol.qty) as qty " \
59 "from pos_order as po,pos_order_line as pol,product_product as pp,product_template as pt,res_users as ru,res_company as rc " \
60 "where pt.id=pp.product_tmpl_id and pp.id=pol.product_id and po.id = pol.order_id and po.state in ('done','paid','invoiced') " \
61 " and to_char(date_trunc('day',po.date_order),'YYYY-MM-DD')::date >= %s and to_char(date_trunc('day',po.date_order),'YYYY-MM-DD')::date <= %s " \
62 "and po.user_id = ru.id and rc.id = %s and ru.id = %s " \
63 ,(form['date_start'],form['date_end'],str(user.company_id.id),str(self.uid)))
64 qty = self.cr.fetchone()
67 def _get_sales_total_2(self, form,user):
68 self.cr.execute("sELECT sum((pol.price_unit * pol.qty * (1 - (pol.discount) / 100.0))) as Total from pos_order_line as pol , pos_order po, product_product as pp,product_template as pt where po.company_id='%s' and po.id=pol.order_id and to_char(date_trunc('day',po.date_order),'YYYY-MM-DD')::date >= '%s' and to_char(date_trunc('day',po.date_order),'YYYY-MM-DD')::date <= '%s' and po.state in ('paid','invoiced','done') and pt.id=pp.product_tmpl_id and pol.product_id=pp.id"% (str(user.company_id.id),form['date_start'],form['date_end']))
69 res2=self.cr.fetchone()
70 return res2 and res2[0] or 0.0
72 def _get_sum_invoice_2(self,form,user):
74 self.cr.execute ("select sum(pol.price_unit * pol.qty * (1 - (pol.discount) / 100.0))" \
75 "from pos_order as po,pos_order_line as pol,product_product as pp,product_template as pt ,res_users as ru,res_company as rc " \
76 "where pt.id=pp.product_tmpl_id and pp.id=pol.product_id and po.id = pol.order_id and po.state in('invoiced') " \
77 "and to_char(date_trunc('day',po.date_order),'YYYY-MM-DD')::date >= %s and to_char(date_trunc('day',po.date_order),'YYYY-MM-DD')::date <= %s " \
78 "and po.user_id = ru.id and rc.id = %s and ru.id = %s " \
79 ,(form['date_start'],form['date_end'],str(user.company_id.id),str(self.uid)))
80 res2=self.cr.fetchone()
81 self.total_invoiced=res2[0]
82 return res2[0] or False
84 def _paid_total_2(self,form,user):
86 self.cr.execute ("select sum(pol.price_unit * pol.qty * (1 - (pol.discount) / 100.0))" \
87 "from pos_order as po,pos_order_line as pol,product_product as pp,product_template as pt, res_users as ru,res_company as rc " \
88 "where pt.id=pp.product_tmpl_id and pp.id=pol.product_id and po.id = pol.order_id and po.state in('paid','invoiced','done') " \
89 "and to_char(date_trunc('day',po.date_order),'YYYY-MM-DD')::date >= %s and to_char(date_trunc('day',po.date_order),'YYYY-MM-DD')::date <= %s " \
90 "and po.user_id = ru.id and rc.id = %s and ru.id = %s " \
91 ,(form['date_start'],form['date_end'],str(user.company_id.id),str(self.uid)))
92 res3=self.cr.fetchone()
93 self.total_paid=res3[0]
94 return res3[0] or False
96 # def _get_qty_total(self, objects):
97 # #code for the sum of qty_total
98 # return reduce(lambda acc, object:
100 # lambda sum_qty, line:
101 # sum_qty + line.qty,
107 def _get_sum_discount(self, objects):
108 #code for the sum of discount value
109 return reduce(lambda acc, object:
111 lambda sum_dis, line:
112 sum_dis + ((line.price_unit * line.qty) * (line.discount / 100)),
118 def _get_payments(self, form,user, ignore_gift=False):
119 # gift_journal_id = None
121 # config_journal_ids = self.pool.get("pos.config.journal").search(self.cr, self.uid, [('code', '=', 'GIFT')])
122 # if len(config_journal_ids):
123 # config_journal = self.pool.get("pos.config.journal").browse(self.cr, self.uid, config_journal_ids, {})[0]
124 # gift_journal_id = config_journal.journal_id.id
127 # for obj in objects:
128 # for payment in obj.statement_ids:
129 # result[payment.journal_id] = result.get(payment.journal_id, 0.0) + payment.amount
131 statement_line_obj = self.pool.get("account.bank.statement.line")
132 gift_journal_id = None
134 config_journal_ids = self.pool.get("pos.config.journal").search(self.cr, self.uid, [('code', '=', 'GIFT')])
135 if len(config_journal_ids):
136 config_journal = self.pool.get("pos.config.journal").browse(self.cr, self.uid, config_journal_ids, {})[0]
137 gift_journal_id = config_journal.journal_id.id
138 pos_ids=self.pool.get("pos.order").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','=',self.uid)])
141 st_line_ids = statement_line_obj.search(self.cr, self.uid, [('pos_statement_id', 'in', pos_ids)])
143 st_id = statement_line_obj.browse(self.cr, self.uid, st_line_ids)
147 a = ','.join(map(str,a_l))
148 self.cr.execute("select aj.name,sum(amount) from account_bank_statement_line as absl,account_bank_statement as abs,account_journal as aj " \
149 "where absl.statement_id = abs.id and abs.journal_id = aj.id and absl.id in (%s) " \
150 "group by aj.name " \
153 data=self.cr.dictfetchall()
158 def _total_of_the_day(self, objects):
160 if self.total_paid == self.total_invoiced :
161 return self.total_paid
163 return ((self.total_paid or 0.00) - (self.total_invoiced or 0.00))
167 def _sum_invoice(self, objects):
168 return reduce(lambda acc, obj:
169 acc + obj.invoice_id.amount_total,
170 [o for o in objects if o.invoice_id and o.invoice_id.number],
173 def _ellipsis(self, orig_str, maxlen=100, ellipsis='...'):
174 maxlen = maxlen - len(ellipsis)
177 new_str = orig_str[:maxlen]
180 def _strip_name(self, name, maxlen=50):
181 return self._ellipsis(name, maxlen, ' ...')
183 def _get_tax_amount(self, form,user):
189 pos_ids=self.pool.get("pos.order").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','=',self.uid)])
190 temp.update({'name':''})
191 for order in self.pool.get("pos.order").browse(self.cr, self.uid, pos_ids):
192 temp2 +=order.amount_tax
193 for line in order.lines:
194 if len(line.product_id.taxes_id):
195 tax = line.product_id.taxes_id[0]
196 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)
197 list_ids.append(tax.id)
198 temp.update({'name':tax.name})
199 temp.update({'amount':temp2})
200 return [temp] or False
202 # def _get_period(self, form):
203 # min_date = form['date_start']
204 # max_date = form['date_end']
205 # if min_date == max_date:
206 # return '%s' % min_date
208 # return '%s - %s' % (min_date, max_date)
209 def _get_period(self, form):
210 return form['date_start']
212 def _get_period2(self,form):
213 return form['date_end']
215 def __init__(self, cr, uid, name, context):
216 super(pos_details, self).__init__(cr, uid, name, context)
220 self.total_paid = 0.0
221 self.total_invoiced = 0.0
222 self.localcontext.update({
224 'strip_name': self._strip_name,
225 'getpayments': self._get_payments,
226 'getsumdisc': self._get_sum_discount,
227 'gettotalofthaday': self._total_of_the_day,
228 'gettaxamount': self._get_tax_amount,
229 'getperiod': self._get_period,
230 'getperiod2':self._get_period2,
231 'pos_sales_details':self._pos_sales_details,
232 'getqtytotal2': self._get_qty_total_2,
233 'getsalestotal2': self._get_sales_total_2,
234 'getsuminvoice2':self._get_sum_invoice_2,
235 'getpaidtotal2': self._paid_total_2,
236 'getinvoice':self._get_invoice,
239 report_sxw.report_sxw('report.pos.details', 'pos.order', 'addons/point_of_sale_singer/report/pos_details.rml', parser=pos_details, header=None)
241 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: