from openerp.osv import fields, osv
from openerp.tools.translate import _
import genshi
from genshi.template import TemplateLoader
import os
import datetime
import base64

TEMPLATE = "sepa_tpl.xml"

class account_voucher(osv.Model):
    _name = "account.voucher"
    _inherit = "account.voucher"

    def generate_sepa(self, cr, uid, ids, context=None):
        items = self.browse(cr, uid, ids, context=context)

        ir_attachment_osv = self.pool.get("ir.attachment")
        payment_mode_osv = self.pool.get("payment.mode")

        template_loader = TemplateLoader([os.path.dirname(os.path.abspath(__file__))])
        tpl = template_loader.load(TEMPLATE)

        now = datetime.datetime.now()
        now_str = now.strftime("%Y%m%d%H%M%S")

        for voucher in items:
            if not len(voucher.partner_id.bank_ids):
                raise osv.except_osv(_("No bank account for Partner"),
                                     _("This partner should have a bank account."))
            
            payment_mode_ids = payment_mode_osv.search(cr, uid,
                                                    [("journal", "=", voucher.journal_id.id)],
                                                    context=context)
            payment_modes = payment_mode_osv.browse(cr, uid, payment_mode_ids, context=context)
            banks = [pm.bank_id for pm in payment_modes]
            
            if not len(banks):
                banks = [bank
                         for bank in voucher.company_id.bank_ids
                         if bank.journal_id.id==voucher.journal_id.id]

            if len(banks):
                bank = banks[0]
            else:
                raise osv.except_osv(_("No origin bank account found"),
                                     _("Bank account for journal %s is not found" % voucher.journal_id.name))


            lines = voucher.line_ids
            content = str(tpl.generate(bank=bank,
                                       voucher=voucher,
                                       lines=lines))
            fname = "PAYMENT_%s_%s.xml" % (voucher.partner_id.name.replace(" ", "_"), now_str)
            #print content
            att_values = dict(datas=base64.encodestring(content),
                              datas_fname=fname,
                              name=fname,
                              res_id = voucher.id,
                              res_model = "account.voucher")
            ir_attachment_osv.create(cr, uid, att_values, context=context)
            # don't write for now (testing)

@@ -20,16 +20,8 @@
 port
- fields,
  osv
@@ -63,22 +63,8 @@
 t _%0A
-import genshi%0A
 from
@@ -170,16 +170,17 @@
 l.xml%22%0A%0A
+%0A
 class ac
@@ -544,16 +544,29 @@
 eLoader(
+%0A            
 %5Bos.path
@@ -851,24 +851,45 @@
 .except_osv(
+%0A                    
 _(%22No bank a
@@ -910,33 +910,16 @@
 tner%22),%0A
-                 
         
@@ -977,28 +977,16 @@
 unt.%22))%0A
-            
 %0A       
@@ -1033,24 +1033,16 @@
 .search(
-cr, uid,
 %0A       
@@ -1050,36 +1050,25 @@
         
-                    
+cr, uid,%0A
         
@@ -1122,44 +1122,8 @@
 )%5D,%0A
-                                    
     
@@ -1203,24 +1203,57 @@
 .browse(
-cr, uid,
+%0A                cr, uid,%0A               
  payment
@@ -1258,24 +1258,40 @@
 nt_mode_ids,
+%0A               
  context=con
@@ -1288,32 +1288,97 @@
 ontext=context)%0A
+            # We get the bank account of the payment mode setted%0A
             bank
@@ -1418,16 +1418,17 @@
 _modes%5D%0A
+%0A
         
@@ -1435,99 +1435,441 @@
     
-%0A            if not len(banks):%0A                banks = %5Bbank%0A                         for 
+# if no payment mode setted, we get company bank account%0A            if not len(banks):%0A                if not voucher.company_id or not voucher.company_id.bank_ids:%0A                    raise osv.except_osv(%0A                        _(%22No bank account for your Company%22),%0A                        _(%22Your company should have a bank account.%22))%0A                banks = %5B%0A                    company_bank%0A                    for company_
 bank
@@ -1920,24 +1920,27 @@
         
-     if 
+if company_
 bank.jou
@@ -1953,10 +1953,12 @@
 d.id
+ 
 ==
+ 
 vouc
@@ -2016,24 +2016,32 @@
             
+company_
 bank = banks
@@ -2099,16 +2099,37 @@
 ept_osv(
+%0A                    
 _(%22No or
@@ -2175,33 +2175,16 @@
         
-            
-     
 _(%22Bank 
@@ -2221,16 +2221,38 @@
 found%22 %25
+%0A                     
  voucher
@@ -2271,17 +2271,16 @@
 name))%0A%0A
-%0A
         
@@ -2304,24 +2304,118 @@
 er.line_ids%0A
+            print %22*%22 * 35%0A            print company_bank.bank_bic%0A            print %22*%22 * 35%0A
             
@@ -2446,16 +2446,24 @@
 te(bank=
+company_
 bank,%0A  
@@ -2612,16 +2612,33 @@
 xml%22 %25 (
+%0A                
 voucher.
@@ -2901,19 +2901,17 @@
   res_id
- = 
+=
 voucher.
@@ -2953,19 +2953,17 @@
 es_model
- = 
+=
 %22account

Correct some typo