2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2009-11-13 05:41:16 +00:00
#
2009-01-28 11:42:54 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2008-06-16 11:00:21 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +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.
2006-12-07 13:41:40 +00:00
#
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
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2006-12-07 13:41:40 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2009-11-13 05:41:16 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
import datetime
2007-08-02 14:33:25 +00:00
import time
2006-12-07 13:41:40 +00:00
2008-06-02 13:19:37 +00:00
import tools
from osv import fields , osv
2008-07-08 08:13:12 +00:00
from tools . translate import _
2008-06-02 13:19:37 +00:00
2010-04-29 04:49:02 +00:00
class account_followup_print ( osv . osv_memory ) :
_name = ' account.followup.print '
_description = ' Print Followup & Send Mail to Customers '
_columns = {
' date ' : fields . date ( ' Follow-up Sending Date ' , required = True , help = " This field allow you to select a forecast date to plan your follow-ups " ) ,
' followup_id ' : fields . many2one ( ' account_followup.followup ' , ' Follow-up ' , required = True )
}
2009-01-29 10:41:06 +00:00
2010-04-29 04:49:02 +00:00
def _get_followup ( self , cr , uid , context = None ) :
2010-07-03 09:30:47 +00:00
if context is None :
context = { }
if context . get ( ' active_model ' , ' ir.ui.menu ' ) == ' account_followup.followup ' :
return context . get ( ' active_id ' , False )
2010-04-29 04:49:02 +00:00
company_id = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid ) . company_id . id
2010-07-03 09:30:47 +00:00
followp_id = self . pool . get ( ' account_followup.followup ' ) . search ( cr , uid , [ ( ' company_id ' , ' = ' , company_id ) ] , context = context )
return followp_id and followp_id [ 0 ] or False
2009-01-29 10:41:06 +00:00
2010-04-29 04:49:02 +00:00
def do_continue ( self , cr , uid , ids , context = None ) :
mod_obj = self . pool . get ( ' ir.model.data ' )
2010-07-03 09:30:47 +00:00
if context is None :
context = { }
2010-04-29 04:49:02 +00:00
data = self . read ( cr , uid , ids , [ ] ) [ 0 ]
model_data_ids = mod_obj . search ( cr , uid , [ ( ' model ' , ' = ' , ' ir.ui.view ' ) , ( ' name ' , ' = ' , ' view_account_followup_print_all ' ) ] , context = context )
resource_id = mod_obj . read ( cr , uid , model_data_ids , fields = [ ' res_id ' ] , context = context ) [ 0 ] [ ' res_id ' ]
context . update ( { ' followup_id ' : data [ ' followup_id ' ] , ' date ' : data [ ' date ' ] } )
return {
2010-07-03 09:30:47 +00:00
' name ' : _ ( ' Select Partners ' ) ,
' view_type ' : ' form ' ,
' context ' : context ,
' view_mode ' : ' tree,form ' ,
' res_model ' : ' account.followup.print.all ' ,
' views ' : [ ( resource_id , ' form ' ) ] ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
2010-04-29 04:49:02 +00:00
}
2007-08-29 04:47:45 +00:00
2010-04-29 04:49:02 +00:00
_defaults = {
' date ' : time . strftime ( ' % Y- % m- %d ' ) ,
' followup_id ' : _get_followup ,
}
2007-08-29 04:47:45 +00:00
2010-04-29 04:49:02 +00:00
account_followup_print ( )
2008-06-02 13:19:37 +00:00
2010-04-29 04:49:02 +00:00
class account_followup_print_all ( osv . osv_memory ) :
_name = ' account.followup.print.all '
_description = ' Print Followup & Send Mail to Customers '
_columns = {
' partner_ids ' : fields . many2many ( ' account_followup.stat ' , ' partner_stat_rel ' , ' followup_id ' , ' stat_id ' , ' Partners ' , required = True ) ,
' email_conf ' : fields . boolean ( ' Send email confirmation ' ) ,
' email_subject ' : fields . char ( ' Email Subject ' , size = 64 ) ,
2010-08-14 12:12:31 +00:00
' partner_lang ' : fields . boolean ( ' Send Email in Partner Language ' , help = ' Do not change message text, if you want to send email in partner language, or configure from company ' ) ,
2010-04-29 04:49:02 +00:00
' email_body ' : fields . text ( ' Email body ' ) ,
' summary ' : fields . text ( ' Summary ' , required = True , readonly = True )
}
def _get_summary ( self , cr , uid , context = None ) :
2010-07-03 09:30:47 +00:00
if context is None :
context = { }
2010-04-29 04:49:02 +00:00
return context . get ( ' summary ' , ' ' )
2008-06-02 13:19:37 +00:00
2010-04-29 04:49:02 +00:00
def _get_partners ( self , cr , uid , context = None ) :
2010-07-03 09:30:47 +00:00
return self . _get_partners_followp ( cr , uid , [ ] , context = context ) [ ' partner_ids ' ]
2008-06-02 13:19:37 +00:00
2010-04-29 04:49:02 +00:00
def _get_msg ( self , cr , uid , context = None ) :
return self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context ) . company_id . follow_up_msg
2006-12-07 13:41:40 +00:00
2010-04-29 04:49:02 +00:00
_defaults = {
' email_body ' : _get_msg ,
' email_subject ' : ' Invoices Reminder ' ,
' partner_lang ' : True ,
' partner_ids ' : _get_partners ,
' summary ' : _get_summary ,
# 'email_body':'''
#Date : %(date)s
#
#Dear %(partner_name)s,
#
#Please find in attachment a reminder of all your unpaid invoices, for a total amount due of:
#
#%(followup_amount).2f %(company_currency)s
#
#
#Thanks,
#--
#%(user_signature)s
#%(company_name)s
# '''
}
2008-06-02 13:19:37 +00:00
2010-04-29 04:49:02 +00:00
def _get_partners_followp ( self , cr , uid , ids , context = None ) :
data = { }
if context is None :
context = { }
if ids :
data = self . read ( cr , uid , ids , [ ] ) [ 0 ]
cr . execute (
" SELECT l.partner_id, l.followup_line_id,l.date_maturity, l.date, l.id " \
" FROM account_move_line AS l " \
" LEFT JOIN account_account AS a " \
" ON (l.account_id=a.id) " \
" WHERE (l.reconcile_id IS NULL) " \
" AND (a.type= ' receivable ' ) " \
" AND (l.state<> ' draft ' ) " \
" AND (l.partner_id is NOT NULL) " \
" AND (a.active) " \
" ORDER BY l.date " )
move_lines = cr . fetchall ( )
old = None
fups = { }
fup_id = ' followup_id ' in context and context [ ' followup_id ' ] or data [ ' followup_id ' ]
date = ' date ' in context and context [ ' date ' ] or data [ ' date ' ]
2007-06-26 08:26:35 +00:00
2010-04-29 04:49:02 +00:00
current_date = datetime . date ( * time . strptime ( date ,
' % Y- % m- %d ' ) [ : 3 ] )
cr . execute (
" SELECT * " \
" FROM account_followup_followup_line " \
" WHERE followup_id= %s " \
" ORDER BY sequence " , ( fup_id , ) )
for result in cr . dictfetchall ( ) :
delay = datetime . timedelta ( days = result [ ' delay ' ] )
fups [ old ] = ( current_date - delay , result [ ' id ' ] )
if result [ ' start ' ] == ' end_of_month ' :
fups [ old ] [ 0 ] . replace ( day = 1 )
old = result [ ' id ' ]
2007-06-26 08:26:35 +00:00
2010-04-29 04:49:02 +00:00
fups [ old ] = ( datetime . date ( datetime . MAXYEAR , 12 , 31 ) , old )
2007-06-26 08:26:35 +00:00
2010-04-29 04:49:02 +00:00
partner_list = [ ]
to_update = { }
for partner_id , followup_line_id , date_maturity , date , id in move_lines :
if not partner_id :
continue
if followup_line_id not in fups :
continue
if date_maturity :
if date_maturity < = fups [ followup_line_id ] [ 0 ] . strftime ( ' % Y- % m- %d ' ) :
if partner_id not in partner_list :
partner_list . append ( partner_id )
to_update [ str ( id ) ] = { ' level ' : fups [ followup_line_id ] [ 1 ] , ' partner_id ' : partner_id }
elif date and date < = fups [ followup_line_id ] [ 0 ] . strftime ( ' % Y- % m- %d ' ) :
if partner_id not in partner_list :
partner_list . append ( partner_id )
to_update [ str ( id ) ] = { ' level ' : fups [ followup_line_id ] [ 1 ] , ' partner_id ' : partner_id }
2008-06-02 13:19:37 +00:00
2010-04-29 04:49:02 +00:00
return { ' partner_ids ' : partner_list , ' to_update ' : to_update }
2006-12-07 13:41:40 +00:00
2010-04-29 04:49:02 +00:00
def do_mail ( self , cr , uid , ids , context = None ) :
mod_obj = self . pool . get ( ' ir.model.data ' )
move_obj = self . pool . get ( ' account.move.line ' )
user_obj = self . pool . get ( ' res.users ' )
line_obj = self . pool . get ( ' account_followup.stat ' )
2010-07-03 09:30:47 +00:00
if context is None :
context = { }
2010-04-29 04:49:02 +00:00
data = self . read ( cr , uid , ids , [ ] ) [ 0 ]
model_data_ids = mod_obj . search ( cr , uid , [ ( ' model ' , ' = ' , ' ir.ui.view ' ) , ( ' name ' , ' = ' , ' view_account_followup_print_all_msg ' ) ] , context = context )
resource_id = mod_obj . read ( cr , uid , model_data_ids , fields = [ ' res_id ' ] , context = context ) [ 0 ] [ ' res_id ' ]
if data [ ' email_conf ' ] :
2009-01-29 10:41:06 +00:00
mail_notsent = ' '
msg_sent = ' '
msg_unsent = ' '
count = 0
2010-04-29 04:49:02 +00:00
data_user = user_obj . browse ( cr , uid , uid )
2010-06-18 13:39:57 +00:00
move_lines = line_obj . browse ( cr , uid , data [ ' partner_ids ' ] )
2009-01-29 10:41:06 +00:00
partners = [ ]
dict_lines = { }
for line in move_lines :
partners . append ( line . name )
dict_lines [ line . name . id ] = line
for partner in partners :
2010-04-29 04:49:02 +00:00
ids_lines = move_obj . search ( cr , uid , [ ( ' partner_id ' , ' = ' , partner . id ) , ( ' reconcile_id ' , ' = ' , False ) , ( ' account_id.type ' , ' in ' , [ ' receivable ' ] ) ] )
2010-06-16 11:51:39 +00:00
data_lines = move_obj . browse ( cr , uid , ids_lines )
2009-01-29 10:41:06 +00:00
followup_data = dict_lines [ partner . id ]
dest = False
if partner . address :
for adr in partner . address :
if adr . type == ' contact ' :
if adr . email :
dest = [ adr . email ]
if ( not dest ) and adr . type == ' default ' :
if adr . email :
dest = [ adr . email ]
2009-10-09 07:42:53 +00:00
src = tools . config . options [ ' email_from ' ]
2010-04-29 04:49:02 +00:00
if not data [ ' partner_lang ' ] :
body = data [ ' email_body ' ]
2009-09-15 10:23:46 +00:00
else :
cxt = context . copy ( )
cxt [ ' lang ' ] = partner . lang
2010-04-29 04:49:02 +00:00
body = user_obj . browse ( cr , uid , uid , context = cxt ) . company_id . follow_up_msg
2009-11-13 05:41:16 +00:00
2009-01-29 10:41:06 +00:00
total_amt = followup_data . debit - followup_data . credit
move_line = ' '
subtotal_due = 0.0
subtotal_paid = 0.0
subtotal_maturity = 0.0
balance = 0.0
l = ' -------------------------------------------------------------------------------------------------------------------------- '
head = l + ' \n ' + ' Date ' . rjust ( 10 ) + ' \t ' + ' Description ' . rjust ( 10 ) + ' \t ' + ' Ref ' . rjust ( 10 ) + ' \t ' + ' Maturity date ' . rjust ( 10 ) + ' \t ' + ' Due ' . rjust ( 10 ) + ' \t ' + ' Paid ' . rjust ( 10 ) + ' \t ' + ' Maturity ' . rjust ( 10 ) + ' \t ' + ' Litigation ' . rjust ( 10 ) + ' \n ' + l
for i in data_lines :
2009-09-18 06:09:13 +00:00
maturity = 0.00
2009-01-29 10:41:06 +00:00
if i . date_maturity < time . strftime ( ' % Y- % m- %d ' ) and ( i . debit - i . credit ) :
maturity = i . debit - i . credit
subtotal_due = subtotal_due + i . debit
subtotal_paid = subtotal_paid + i . credit
subtotal_maturity = subtotal_maturity + int ( maturity )
balance = balance + ( i . debit - i . credit )
move_line = move_line + ( i . date ) . rjust ( 10 ) + ' \t ' + ( i . name ) . rjust ( 10 ) + ' \t ' + ( i . ref or ' ' ) . rjust ( 10 ) + ' \t ' + ( i . date_maturity or ' ' ) . rjust ( 10 ) + ' \t ' + str ( i . debit ) . rjust ( 10 ) + ' \t ' + str ( i . credit ) . rjust ( 10 ) + ' \t ' + str ( maturity ) . rjust ( 10 ) + ' \t ' + str ( i . blocked ) . rjust ( 10 ) + ' \n '
move_line = move_line + l + ' \n ' + ' \t \t \t ' + ' Sub total ' . rjust ( 35 ) + ' \t ' + ( str ( subtotal_due ) or ' ' ) . rjust ( 10 ) + ' \t ' + ( str ( subtotal_paid ) or ' ' ) . rjust ( 10 ) + ' \t ' + ( str ( subtotal_maturity ) or ' ' ) . rjust ( 10 ) + ' \n '
move_line = move_line + ' \t \t \t ' + ' Balance ' . rjust ( 33 ) + ' \t ' + str ( balance ) . rjust ( 10 ) + ' \n ' + l
val = {
' partner_name ' : partner . name ,
' followup_amount ' : total_amt ,
' user_signature ' : data_user . name ,
' company_name ' : data_user . company_id . name ,
' company_currency ' : data_user . company_id . currency_id . name ,
' line ' : move_line ,
' heading ' : head ,
' date ' : time . strftime ( ' % Y- % m- %d ' ) ,
}
body = body % val
2010-04-29 04:49:02 +00:00
sub = tools . ustr ( data [ ' email_subject ' ] )
2009-01-29 10:41:06 +00:00
msg = ' '
if dest :
tools . email_send ( src , dest , sub , body )
msg_sent + = partner . name + ' \n '
else :
msg + = partner . name + ' \n '
msg_unsent + = msg
if not msg_unsent :
2009-09-18 06:09:13 +00:00
summary = _ ( " All E-mails have been successfully sent to Partners:. \n \n " ) + msg_sent
2009-01-27 16:35:52 +00:00
else :
2009-09-18 06:09:13 +00:00
msg_unsent = _ ( " E-Mail not sent to following Partners, Email not available ! \n \n " ) + msg_unsent
msg_sent = msg_sent and _ ( " \n \n E-Mail sent to following Partners successfully. ! \n \n " ) + msg_sent
2009-01-29 10:41:06 +00:00
line = ' ========================================================================== '
summary = msg_unsent + line + msg_sent
2010-04-29 04:49:02 +00:00
context . update ( { ' summary ' : summary } )
2009-01-29 10:41:06 +00:00
else :
2010-04-29 04:49:02 +00:00
context . update ( { ' summary ' : ' \n \n \n E-Mail has not been sent to any partner. If you want to send it, please tick send email confirmation on wizard. ' } )
2010-07-03 09:30:47 +00:00
2010-04-29 04:49:02 +00:00
return {
2010-07-03 09:30:47 +00:00
' name ' : _ ( ' Follwoup Summary ' ) ,
2010-04-29 04:49:02 +00:00
' view_type ' : ' form ' ,
' context ' : context ,
' view_mode ' : ' tree,form ' ,
' res_model ' : ' account.followup.print.all ' ,
' views ' : [ ( resource_id , ' form ' ) ] ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
' nodestroy ' : True
}
2008-06-02 13:19:37 +00:00
2010-04-29 04:49:02 +00:00
def do_print ( self , cr , uid , ids , context = None ) :
2010-07-03 09:30:47 +00:00
if context is None :
context = { }
2010-04-29 04:49:02 +00:00
data = self . read ( cr , uid , ids , [ ] ) [ 0 ]
res = self . _get_partners_followp ( cr , uid , ids , context ) [ ' to_update ' ]
to_update = res
data [ ' followup_id ' ] = ' followup_id ' in context and context [ ' followup_id ' ] or False
date = ' date ' in context and context [ ' date ' ] or data [ ' date ' ]
for id in to_update . keys ( ) :
2010-06-24 11:48:39 +00:00
if to_update [ id ] [ ' partner_id ' ] in data [ ' partner_ids ' ] :
2010-04-29 04:49:02 +00:00
cr . execute (
" UPDATE account_move_line " \
" SET followup_line_id= %s , followup_date= %s " \
" WHERE id= %s " ,
( to_update [ id ] [ ' level ' ] ,
date , int ( id ) , ) )
2006-12-07 13:41:40 +00:00
2010-04-29 04:49:02 +00:00
datas = {
' ids ' : [ ] ,
' model ' : ' account_followup.followup ' ,
' form ' : data
}
2010-07-03 09:30:47 +00:00
2010-04-29 04:49:02 +00:00
return {
' type ' : ' ir.actions.report.xml ' ,
' report_name ' : ' account_followup.followup.print ' ,
' datas ' : datas ,
}
2008-07-23 14:41:47 +00:00
2010-04-29 04:49:02 +00:00
account_followup_print_all ( )
2008-07-23 14:41:47 +00:00
2010-04-29 04:49:02 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: