2008-07-23 14:41:47 +00:00
# -*- encoding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
#
2009-01-28 11:42:54 +00:00
# OpenERP, Open Source Management Solution
2009-01-04 22:12:50 +00:00
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
2008-11-03 19:18:56 +00:00
# $Id$
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
# it under the terms of the GNU 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
# GNU General Public License for more details.
2006-12-07 13:41:40 +00:00
#
2008-11-03 19:18:56 +00:00
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
import wizard
import datetime
import pooler
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
2009-01-29 10:41:06 +00:00
_email_summary_form = """ <?xml version= " 1.0 " ?>
< form string = " Summary " >
< field name = " summary " height = " 300 " width = " 800 " / >
< / form > """
_email_summary_fields = {
' summary ' : { ' string ' : ' Summary ' , ' type ' : ' text ' , ' required ' : False , ' readonly ' : True } ,
}
2009-01-27 16:35:52 +00:00
_followup_wizard_screen1_form = """ <?xml version= " 1.0 " ?>
< form string = " Follow-up and Date Selection " >
< field name = " followup_id " / >
2008-07-22 15:11:28 +00:00
< field name = " date " / >
2007-08-29 04:47:45 +00:00
< / form > """
2009-01-27 16:35:52 +00:00
_followup_wizard_screen1_fields = {
' date ' : { ' string ' : ' Follow-up Sending Date ' , ' type ' : ' date ' , ' required ' : True , ' help ' : " This field allow you to select a forecast date to plan your follow-ups " } ,
' followup_id ' : { ' string ' : ' Follow-up ' , ' type ' : ' many2one ' , ' relation ' : ' account_followup.followup ' , ' required ' : True , } ,
2007-08-29 04:47:45 +00:00
}
2008-06-02 13:19:37 +00:00
2007-06-26 08:00:30 +00:00
_followup_wizard_all_form = """ <?xml version= " 1.0 " ?>
< form string = " Select partners " colspan = " 4 " >
2008-07-22 15:11:28 +00:00
< notebook >
2009-01-27 16:35:52 +00:00
< page string = " Partner Selection " >
2008-07-22 15:11:28 +00:00
< separator string = " Select partners to remind " colspan = " 4 " / >
< field name = " partner_ids " colspan = " 4 " nolabel = " 1 " / >
< / page >
2009-01-27 16:35:52 +00:00
< page string = " Email Settings " >
2008-07-22 15:11:28 +00:00
< field name = " email_conf " colspan = " 4 " / >
< field name = " email_subject " colspan = " 4 " / >
< separator string = " Email body " colspan = " 4 " / >
< field name = " email_body " colspan = " 4 " nolabel = " 1 " / >
< separator string = " Legend " colspan = " 4 " / >
2008-06-02 13:19:37 +00:00
2008-07-22 15:11:28 +00:00
< label string = " %(partner_name)s : Partner name " colspan = " 2 " / >
< label string = " %(user_signature)s : User name " colspan = " 2 " / >
2009-01-27 16:35:52 +00:00
< label string = " %(followup_amount)s : Total Amount Due " colspan = " 2 " / >
< label string = " %(date)s : Current Date " colspan = " 2 " / >
2008-07-22 15:11:28 +00:00
< label string = " %(company_name)s : User ' s Company name " colspan = " 2 " / >
2009-01-27 16:35:52 +00:00
< label string = " %(company_currency)s : User ' s Company Currency " colspan = " 2 " / >
2008-07-22 15:11:28 +00:00
< label string = " %(heading)s : Move line header " colspan = " 2 " / >
2009-01-27 16:35:52 +00:00
< label string = " %(line)s : Account Move lines " colspan = " 2 " / >
2008-07-22 15:11:28 +00:00
< / page >
< / notebook >
2007-06-26 08:00:30 +00:00
< / form > """
2006-12-07 13:41:40 +00:00
2007-06-26 08:00:30 +00:00
_followup_wizard_all_fields = {
2008-07-22 15:11:28 +00:00
' partner_ids ' : {
' string ' : " Partners " ,
' type ' : ' many2many ' ,
2009-01-27 16:35:52 +00:00
' required ' : True ,
2008-07-22 15:11:28 +00:00
' relation ' : ' account_followup.stat ' ,
} ,
' email_conf ' : {
' string ' : " Send email confirmation " ,
' type ' : ' boolean ' ,
} ,
' email_subject ' : {
' string ' : " Email Subject " ,
' type ' : " char " ,
' size ' : 64 ,
2009-01-27 16:35:52 +00:00
' default ' : ' Invoices Reminder '
2008-07-22 15:11:28 +00:00
} ,
' email_body ' : {
' string ' : " Email body " ,
' type ' : ' text ' ,
' default ' : '''
2008-06-02 13:19:37 +00:00
Date : % ( date ) s
2007-06-26 08:26:35 +00:00
Dear % ( partner_name ) s ,
2009-01-27 16:35:52 +00:00
Please find in attachment a reminder of all your unpaid invoices , for a total amount due of :
2007-06-26 08:26:35 +00:00
2009-01-27 16:35:52 +00:00
% ( followup_amount ) .2 f % ( company_currency ) s
2007-06-26 08:26:35 +00:00
2008-06-02 13:19:37 +00:00
Thanks ,
- -
2007-06-26 08:26:35 +00:00
% ( user_signature ) s
2008-06-02 13:19:37 +00:00
% ( company_name ) s
2008-07-22 15:11:28 +00:00
'''
}
2007-06-26 08:00:30 +00:00
}
2006-12-07 13:41:40 +00:00
2007-06-26 08:00:30 +00:00
class followup_all_print ( wizard . interface ) :
2008-07-22 15:11:28 +00:00
def _update_partners ( self , cr , uid , data , context ) :
to_update = data [ ' form ' ] [ ' to_update ' ]
for id in to_update . keys ( ) :
cr . execute (
" UPDATE account_move_line " \
2008-12-10 14:29:55 +00:00
" SET followup_line_id= %s , followup_date= %s " \
" WHERE id= %s " ,
2008-07-22 15:11:28 +00:00
( to_update [ id ] ,
data [ ' form ' ] [ ' date ' ] , int ( id ) , ) )
return { }
2007-08-02 14:33:25 +00:00
2008-07-22 15:11:28 +00:00
def _sendmail ( self , cr , uid , data , context ) :
2009-01-29 10:41:06 +00:00
if data [ ' form ' ] [ ' email_conf ' ] :
mail_notsent = ' '
msg_sent = ' '
msg_unsent = ' '
count = 0
pool = pooler . get_pool ( cr . dbname )
data_user = pool . get ( ' res.users ' ) . browse ( cr , uid , uid )
line_obj = pool . get ( ' account_followup.stat ' )
move_lines = line_obj . browse ( cr , uid , data [ ' form ' ] [ ' partner_ids ' ] [ 0 ] [ 2 ] )
partners = [ ]
dict_lines = { }
for line in move_lines :
partners . append ( line . name )
dict_lines [ line . name . id ] = line
for partner in partners :
ids_lines = pool . get ( ' account.move.line ' ) . search ( cr , uid , [ ( ' partner_id ' , ' = ' , partner . id ) , ( ' reconcile_id ' , ' = ' , False ) , ( ' account_id.type ' , ' in ' , [ ' receivable ' ] ) ] )
data_lines = pool . get ( ' account.move.line ' ) . browse ( cr , uid , ids_lines )
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 ]
src = tools . config . options [ ' smtp_user ' ]
body = data [ ' form ' ] [ ' email_body ' ]
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 :
maturity = ' '
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
sub = str ( data [ ' form ' ] [ ' email_subject ' ] )
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 :
summary = _ ( " All emails have been successfully sent to Partners:. \n \n " ) + msg_sent
2009-01-27 16:35:52 +00:00
else :
2009-01-29 10:41:06 +00:00
msg_unsent = _ ( " Mail not sent to following Partners, Email not available ! \n \n " ) + msg_unsent
msg_sent = msg_sent and _ ( " \n \n Mail sent to following Partners successfully, ! \n \n " ) + msg_sent
line = ' ========================================================================== '
summary = msg_unsent + line + msg_sent
return { ' summary ' : summary }
else :
2009-02-02 13:00:33 +00:00
return { ' summary ' : ' \n \n \n Mail not sent to any partner if you want to sent it please tick send email confirmation on wizard ' }
2008-06-02 13:19:37 +00:00
2008-07-22 15:11:28 +00:00
def _get_partners ( self , cr , uid , data , context ) :
pool = pooler . get_pool ( cr . dbname )
cr . execute (
2008-09-24 12:02:40 +00:00
" SELECT l.partner_id, l.followup_line_id,l.date_maturity, l.date, l.id " \
2008-07-22 15:11:28 +00:00
" 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 ' ) " \
2009-01-27 16:35:52 +00:00
" AND (l.partner_id is NOT NULL) " \
" AND (a.active) " \
2008-07-22 15:11:28 +00:00
" ORDER BY l.date " )
move_lines = cr . fetchall ( )
old = None
fups = { }
2009-01-27 16:35:52 +00:00
fup_id = data [ ' form ' ] [ ' followup_id ' ]
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
current_date = datetime . date ( * time . strptime ( data [ ' form ' ] [ ' date ' ] ,
' % Y- % m- %d ' ) [ : 3 ] )
cr . execute (
" SELECT * " \
" FROM account_followup_followup_line " \
2008-12-10 14:29:55 +00:00
" WHERE followup_id= %s " \
2008-07-22 15:11:28 +00:00
" 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 ' ]
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
fups [ old ] = ( datetime . date ( datetime . MAXYEAR , 12 , 31 ) , old )
2006-12-07 13:41:40 +00:00
2008-07-22 15:11:28 +00:00
partner_list = [ ]
to_update = { }
2008-09-24 11:46:07 +00:00
for partner_id , followup_line_id , date_maturity , date , id in move_lines :
2008-07-22 15:11:28 +00:00
if not partner_id :
continue
if followup_line_id not in fups :
continue
2009-01-27 16:35:52 +00:00
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 ) ] = fups [ followup_line_id ] [ 1 ]
2008-09-24 11:46:07 +00:00
elif date and date < = fups [ followup_line_id ] [ 0 ] . strftime ( ' % Y- % m- %d ' ) :
2009-01-27 16:35:52 +00:00
if partner_id not in partner_list :
partner_list . append ( partner_id )
2008-07-22 15:11:28 +00:00
to_update [ str ( id ) ] = fups [ followup_line_id ] [ 1 ]
2009-01-27 16:35:52 +00:00
2008-07-22 15:11:28 +00:00
return { ' partner_ids ' : partner_list , ' to_update ' : to_update }
2007-08-02 14:33:25 +00:00
2009-01-27 16:35:52 +00:00
def _get_screen1_values ( self , cr , uid , data , context ) :
pool = pooler . get_pool ( cr . dbname )
company_id = pool . get ( ' res.users ' ) . browse ( cr , uid , uid ) . company_id . id
tmp = pool . get ( ' account_followup.followup ' ) . search ( cr , uid , [ ( ' company_id ' , ' = ' , company_id ) ] )
followup = tmp and tmp [ 0 ] or False
return { ' date ' : time . strftime ( ' % Y- % m- %d ' ) , ' followup_id ' : followup }
2007-08-29 04:47:45 +00:00
2008-07-22 15:11:28 +00:00
states = {
' init ' : {
2009-01-27 16:35:52 +00:00
' actions ' : [ _get_screen1_values ] ,
2008-07-22 15:11:28 +00:00
' result ' : { ' type ' : ' form ' ,
2009-01-27 16:35:52 +00:00
' arch ' : _followup_wizard_screen1_form ,
' fields ' : _followup_wizard_screen1_fields ,
2008-07-22 15:11:28 +00:00
' state ' : [
( ' end ' , ' Cancel ' ) ,
( ' next ' , ' Continue ' ) ,
]
} ,
} ,
' next ' : {
' actions ' : [ _get_partners ] ,
' result ' : { ' type ' : ' form ' ,
' arch ' : _followup_wizard_all_form ,
' fields ' : _followup_wizard_all_fields ,
' state ' : [
( ' end ' , ' Cancel ' ) ,
2009-01-27 16:35:52 +00:00
( ' print ' , ' Print Follow Ups & Send Mails ' ) ,
2008-07-22 15:11:28 +00:00
]
} ,
} ,
' print ' : {
' actions ' : [ _update_partners ] ,
' result ' : { ' type ' : ' print ' ,
' report ' : ' account_followup.followup.print ' ,
2009-01-29 10:41:06 +00:00
' state ' : ' summary ' } ,
} ,
' summary ' : {
' actions ' : [ _sendmail ] ,
' result ' : { ' type ' : ' form ' ,
' arch ' : _email_summary_form ,
' fields ' : _email_summary_fields ,
' state ' : [ ( ' end ' , ' Ok ' ) ]
} ,
2008-07-22 15:11:28 +00:00
} ,
}
2007-07-26 08:30:27 +00:00
2006-12-07 13:41:40 +00:00
followup_all_print ( ' account_followup.followup.print.all ' )
2008-07-23 14:41:47 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: