2010-08-09 11:14:53 +00:00
# -*- coding: utf-8 -*-
2008-11-03 19:18:56 +00:00
##############################################################################
2010-08-27 09:14:13 +00:00
#
2009-10-14 11:15:34 +00:00
# OpenERP, Open Source Management Solution
2010-08-09 11:14:53 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2008-11-03 19:18:56 +00:00
#
# This program is free software: you can redistribute it and/or modify
2010-08-09 11:14:53 +00:00
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
2008-11-03 19:18:56 +00:00
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2010-08-09 11:14:53 +00:00
# GNU Affero General Public License for more details.
2008-11-03 19:18:56 +00:00
#
2010-08-09 11:14:53 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-08-27 09:14:13 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-11-03 19:18:56 +00:00
#
##############################################################################
2008-08-24 14:45:43 +00:00
import time
from report import report_sxw
class pos_details ( report_sxw . rml_parse ) :
2011-09-13 06:20:36 +00:00
def _get_invoice ( self , inv_id ) :
2010-01-18 08:38:26 +00:00
res = { }
2010-08-10 05:58:45 +00:00
if inv_id :
2010-10-01 09:26:28 +00:00
self . cr . execute ( " select name from account_invoice as ac where id = %s " , ( inv_id , ) )
2010-08-10 05:58:45 +00:00
res = self . cr . fetchone ( )
2010-01-18 08:38:26 +00:00
return res [ 0 ]
else :
return ' '
2011-11-03 13:27:46 +00:00
def _get_all_users ( self ) :
user_obj = self . pool . get ( ' res.users ' )
return user_obj . search ( self . cr , self . uid , [ ] )
2011-09-13 06:20:36 +00:00
def _pos_sales_details ( self , form ) :
data = { }
2011-11-03 13:27:46 +00:00
user_ids = form [ ' user_ids ' ] or self . _get_all_users ( )
2010-10-27 12:07:01 +00:00
self . cr . execute ( " select po.name as pos_name,po.date_order,pt.name, pp.default_code as code,pol.qty,pu.name as uom,pol.price_unit,pol.discount,po.invoice_id,sum((pol.price_unit * pol.qty * (1 - (pol.discount) / 100.0))) as Total " \
2010-10-12 12:04:47 +00:00
" from pos_order as po,pos_order_line as pol,product_product as pp,product_template as pt,product_uom as pu,res_users as ru,res_company as rc " \
" where pt.id=pp.product_tmpl_id and pu.id=pt.uom_id and pp.id=pol.product_id and po.id = pol.order_id and po.state IN ( ' done ' , ' paid ' , ' invoiced ' ) " \
2010-01-18 08:38:26 +00:00
" 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 " \
2011-09-13 06:20:36 +00:00
" and po.user_id = ru.id and rc.id = po.company_id and ru.id IN %s " \
2010-10-12 12:04:47 +00:00
" group by po.name,pol.qty,po.date_order,pt.name,pp.default_code,pu.name,pol.price_unit,pol.discount,po.invoice_id " \
2011-09-13 06:20:36 +00:00
, ( form [ ' date_start ' ] , form [ ' date_end ' ] , tuple ( user_ids ) ) )
2010-01-18 08:38:26 +00:00
data = self . cr . dictfetchall ( )
if data :
for d in data :
self . total + = d [ ' total ' ]
self . qty + = d [ ' qty ' ]
return data
else :
return { }
2011-09-13 06:20:36 +00:00
def _get_qty_total_2 ( self , form ) :
2010-01-18 08:38:26 +00:00
qty = [ ]
2011-11-03 13:27:46 +00:00
user_ids = form [ ' user_ids ' ] or self . _get_all_users ( )
2010-01-18 08:38:26 +00:00
self . cr . execute ( " select sum(pol.qty) as qty " \
" 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 " \
2010-06-16 11:51:39 +00:00
" 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 ' ) " \
2010-01-18 08:38:26 +00:00
" 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 " \
2011-09-13 06:20:36 +00:00
" and po.user_id = ru.id and rc.id = po.company_id and ru.id IN %s " \
, ( form [ ' date_start ' ] , form [ ' date_end ' ] , tuple ( user_ids ) ) )
2010-01-18 08:38:26 +00:00
qty = self . cr . fetchone ( )
return qty [ 0 ] or 0.00
2011-09-13 06:20:36 +00:00
def _get_sales_total_2 ( self , form ) :
2011-11-09 05:35:00 +00:00
user_ids = form [ ' user_ids ' ] or self . _get_all_users ( )
2010-06-16 11:51:39 +00:00
self . cr . execute ( " select sum((pol.price_unit * pol.qty * (1 - (pol.discount) / 100.0))) as Total " \
2010-10-27 12:07:01 +00:00
" from pos_order_line as pol, pos_order po, product_product as pp,product_template as pt " \
2011-11-09 05:35:00 +00:00
" where 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 and po.user_id in %s " \
, ( form [ ' date_start ' ] , form [ ' date_end ' ] , tuple ( user_ids ) ) )
2010-01-18 08:38:26 +00:00
res2 = self . cr . fetchone ( )
return res2 and res2 [ 0 ] or 0.0
2011-09-13 06:20:36 +00:00
def _get_sum_invoice_2 ( self , form ) :
2010-01-18 08:38:26 +00:00
res2 = [ ]
2011-11-03 13:27:46 +00:00
user_ids = form [ ' user_ids ' ] or self . _get_all_users ( )
2010-01-18 08:38:26 +00:00
self . cr . execute ( " select sum(pol.price_unit * pol.qty * (1 - (pol.discount) / 100.0)) " \
2010-10-27 12:07:01 +00:00
" 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,account_invoice as ai " \
2010-10-14 13:13:54 +00:00
" where pt.id=pp.product_tmpl_id and pp.id=pol.product_id and po.id = pol.order_id and ai.id=po.invoice_id " \
2010-01-18 08:38:26 +00:00
" 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 " \
2011-09-13 06:20:36 +00:00
" and po.user_id = ru.id and rc.id = po.company_id and ru.id IN %s " \
, ( form [ ' date_start ' ] , form [ ' date_end ' ] , tuple ( user_ids ) ) )
2010-01-18 08:38:26 +00:00
res2 = self . cr . fetchone ( )
self . total_invoiced = res2 [ 0 ]
return res2 [ 0 ] or False
2011-09-13 06:20:36 +00:00
def _paid_total_2 ( self , form ) :
2010-01-18 08:38:26 +00:00
res3 = [ ]
2011-11-03 13:27:46 +00:00
user_ids = form [ ' user_ids ' ] or self . _get_all_users ( )
2010-01-18 08:38:26 +00:00
self . cr . execute ( " select sum(pol.price_unit * pol.qty * (1 - (pol.discount) / 100.0)) " \
" 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 " \
2010-10-14 12:57:19 +00:00
" 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 ' ) " \
2010-01-18 08:38:26 +00:00
" 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 " \
2011-09-13 06:20:36 +00:00
" and po.user_id = ru.id and rc.id = po.company_id and ru.id IN %s " \
, ( form [ ' date_start ' ] , form [ ' date_end ' ] , tuple ( user_ids ) ) )
2010-01-18 08:38:26 +00:00
res3 = self . cr . fetchone ( )
self . total_paid = res3 [ 0 ]
return res3 [ 0 ] or False
2011-09-13 06:20:36 +00:00
def _get_sum_dis_2 ( self , form ) :
2010-10-14 12:57:19 +00:00
res4 = [ ]
2011-11-03 13:27:46 +00:00
user_ids = form [ ' user_ids ' ] or self . _get_all_users ( )
2011-11-09 06:53:13 +00:00
self . cr . execute ( " select sum(pol.discount) " \
2010-10-27 12:07:01 +00:00
" 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 " \
2011-11-09 06:53:13 +00:00
" where pt.id=pp.product_tmpl_id and pp.id=pol.product_id and po.id = pol.order_id and po.state IN ( ' paid ' , ' done ' , ' invoiced ' ) " \
2010-10-14 12:57:19 +00:00
" 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 " \
2011-09-13 06:20:36 +00:00
" and po.user_id = ru.id and rc.id = po.company_id and ru.id IN %s " \
, ( form [ ' date_start ' ] , form [ ' date_end ' ] , tuple ( user_ids ) ) )
2010-10-14 12:57:19 +00:00
res4 = self . cr . fetchone ( )
self . total_invoiced = res4 [ 0 ]
return res4 [ 0 ] or False
2008-08-24 14:45:43 +00:00
def _get_sum_discount ( self , objects ) :
#code for the sum of discount value
return reduce ( lambda acc , object :
acc + reduce (
lambda sum_dis , line :
sum_dis + ( ( line . price_unit * line . qty ) * ( line . discount / 100 ) ) ,
object . lines ,
0.0 ) ,
objects ,
0.0 )
2011-09-13 06:20:36 +00:00
def _get_payments ( self , form ) :
2010-01-18 08:38:26 +00:00
statement_line_obj = self . pool . get ( " account.bank.statement.line " )
2010-10-27 12:07:01 +00:00
pos_order_obj = self . pool . get ( " pos.order " )
2011-11-03 13:27:46 +00:00
user_ids = form [ ' user_ids ' ] or self . _get_all_users ( )
2011-09-13 06:20:36 +00:00
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 ) ] )
2010-01-18 08:38:26 +00:00
data = { }
if pos_ids :
st_line_ids = statement_line_obj . search ( self . cr , self . uid , [ ( ' pos_statement_id ' , ' in ' , pos_ids ) ] )
if st_line_ids :
st_id = statement_line_obj . browse ( self . cr , self . uid , st_line_ids )
a_l = [ ]
2010-10-27 12:07:01 +00:00
for r in st_id :
2010-01-18 08:38:26 +00:00
a_l . append ( r [ ' id ' ] )
self . cr . execute ( " select aj.name,sum(amount) from account_bank_statement_line as absl,account_bank_statement as abs,account_journal as aj " \
2010-06-16 11:51:39 +00:00
" where absl.statement_id = abs.id and abs.journal_id = aj.id and absl.id IN %s " \
" group by aj.name " , ( tuple ( a_l ) , ) )
2010-01-18 08:38:26 +00:00
data = self . cr . dictfetchall ( )
return data
else :
return { }
2008-08-24 14:45:43 +00:00
def _total_of_the_day ( self , objects ) :
2010-01-18 08:38:26 +00:00
if self . total_paid :
2010-10-27 12:07:01 +00:00
if self . total_paid == self . total_invoiced :
2010-01-18 08:38:26 +00:00
return self . total_paid
else :
return ( ( self . total_paid or 0.00 ) - ( self . total_invoiced or 0.00 ) )
else :
return False
2008-08-24 14:45:43 +00:00
def _sum_invoice ( self , objects ) :
return reduce ( lambda acc , obj :
acc + obj . invoice_id . amount_total ,
[ o for o in objects if o . invoice_id and o . invoice_id . number ] ,
0.0 )
def _ellipsis ( self , orig_str , maxlen = 100 , ellipsis = ' ... ' ) :
maxlen = maxlen - len ( ellipsis )
if maxlen < = 0 :
maxlen = 1
new_str = orig_str [ : maxlen ]
return new_str
def _strip_name ( self , name , maxlen = 50 ) :
return self . _ellipsis ( name , maxlen , ' ... ' )
2011-09-13 06:20:36 +00:00
def _get_tax_amount ( self , form ) :
2008-08-24 14:45:43 +00:00
res = { }
2010-01-18 08:38:26 +00:00
temp = { }
2008-08-24 14:45:43 +00:00
list_ids = [ ]
2010-01-18 08:38:26 +00:00
temp2 = 0.0
2011-11-03 13:27:46 +00:00
user_ids = form [ ' user_ids ' ] or self . _get_all_users ( )
2010-10-27 12:07:01 +00:00
pos_order_obj = self . pool . get ( " pos.order " )
2011-09-13 06:20:36 +00:00
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 ) ] )
2010-01-18 08:38:26 +00:00
temp . update ( { ' name ' : ' ' } )
2010-10-27 12:07:01 +00:00
for order in pos_order_obj . browse ( self . cr , self . uid , pos_ids ) :
2010-01-18 08:38:26 +00:00
temp2 + = order . amount_tax
2008-08-24 14:45:43 +00:00
for line in order . lines :
if len ( line . product_id . taxes_id ) :
tax = line . product_id . taxes_id [ 0 ]
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 )
list_ids . append ( tax . id )
2010-01-18 08:38:26 +00:00
temp . update ( { ' name ' : tax . name } )
temp . update ( { ' amount ' : temp2 } )
return [ temp ] or False
def _get_period ( self , form ) :
return form [ ' date_start ' ]
def _get_period2 ( self , form ) :
return form [ ' date_end ' ]
2008-08-24 14:45:43 +00:00
2011-11-03 13:27:46 +00:00
def _get_user_names ( self , user_ids ) :
user_obj = self . pool . get ( ' res.users ' )
return ' , ' . join ( map ( lambda x : x . name , user_obj . browse ( self . cr , self . uid , user_ids ) ) )
2008-08-24 14:45:43 +00:00
def __init__ ( self , cr , uid , name , context ) :
2010-10-06 05:27:56 +00:00
super ( pos_details , self ) . __init__ ( cr , uid , name , context = context )
2008-08-24 14:45:43 +00:00
self . total = 0.0
2010-01-18 08:38:26 +00:00
self . qty = 0.0
self . invoice_id = ' '
self . total_paid = 0.0
self . total_invoiced = 0.0
2008-08-24 14:45:43 +00:00
self . localcontext . update ( {
' time ' : time ,
' strip_name ' : self . _strip_name ,
' getpayments ' : self . _get_payments ,
2010-10-14 12:57:19 +00:00
' getsumdisc ' : self . _get_sum_dis_2 ,
2008-08-24 14:45:43 +00:00
' gettotalofthaday ' : self . _total_of_the_day ,
' gettaxamount ' : self . _get_tax_amount ,
' getperiod ' : self . _get_period ,
2010-01-18 08:38:26 +00:00
' getperiod2 ' : self . _get_period2 ,
' pos_sales_details ' : self . _pos_sales_details ,
' getqtytotal2 ' : self . _get_qty_total_2 ,
' getsalestotal2 ' : self . _get_sales_total_2 ,
' getsuminvoice2 ' : self . _get_sum_invoice_2 ,
' getpaidtotal2 ' : self . _paid_total_2 ,
' getinvoice ' : self . _get_invoice ,
2011-11-03 13:27:46 +00:00
' get_user_names ' : self . _get_user_names ,
2008-08-24 14:45:43 +00:00
} )
2010-08-31 13:12:50 +00:00
report_sxw . report_sxw ( ' report.pos.details ' , ' pos.order ' , ' addons/point_of_sale_singer/report/pos_details.rml ' , parser = pos_details , header = ' internal ' )
2008-08-24 14:45:43 +00:00
2011-09-25 14:09:30 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: