~vrenaville-c2c/oerpscenario/base_db_testing

« back to all changes in this revision

Viewing changes to lib/Helpers/AccountBankStatement.rb

  • Committer: Vincent Renaville
  • Date: 2010-01-14 11:03:35 UTC
  • mfrom: (33.1.19 OERPScenario)
  • Revision ID: vincent.renaville@camptocamp.com-20100114110335-os6ro1cz03y3ukc2
Merge from trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
include Ooor
24
24
require 'pp'
25
25
 
26
 
# Add useful methode on bank statement handling
27
 
##############################################################################
28
 
AccountBankStatement.class_eval do 
29
 
  ############################################################################
30
 
  # Import invoices into the given bank statements
31
 
  # Input :
32
 
  #  - invoices :  A valid dict of AccountInvoice instance
33
 
  #  - statement : A valid AccountBankStatement instance
34
 
  #  - options {
35
 
  #      date (Default today) : date used into the first screen of the wizard (date
36
 
  #                             imported in lines)
37
 
  #    }
38
 
  # Return
39
 
  #  - The AccountBankStatement as a instance of the class¨
40
 
  # Usage Example:
41
 
  # statement = AccountBankStatement.import_invoice(invoices,statement)
42
 
  #
43
 
  # TODO implement journals
44
 
  # TODO filter acc_move_line_ids to take the right one
45
 
  # TODO Remove self to use it from an instance of the class
46
 
  def import_invoice(invoices, options={},*args)
47
 
    o = {:date => false, :journals => []}.merge(options)
48
 
    if o[:date] :
49
 
        o[:date] = Date.parse(str=o[:date]).to_s
50
 
    else
51
 
        o[:date] = Date.today.to_s
52
 
    end
53
 
    
54
 
    # Parse the given journal ids if provided
55
 
    journal_ids=[]
56
 
    o[:journals].each do |journal|
57
 
      journal_ids.push journal.id
58
 
    end
59
 
    invoice_move_line_ids=[]
60
 
    inv_total=0.0
61
 
    self.balance_start=inv_total
62
 
    
63
 
    # # For each invoices, add the right account.move.line and compute total
64
 
    invoices.each do |inv|
65
 
      
66
 
      unless inv.class  == AccountInvoice :
67
 
        raise "!!! --- HELPER ERROR :import_invoice received a #{inv.class.to_s} instead of AccountInvoice" 
68
 
      end
69
 
      # Take the move ids from concerned invoice
70
 
      # TODO add journal support and (journal_ids.include? move_line.journal_id or journal_ids == []
71
 
      # TODO : Debug the problem of undefined method `[]' for nil:NilClass (NoMethodError)
72
 
      # pp inv.move_id.line_id
73
 
      #       inv.move_id.line_id.each do |move_line|
74
 
      #         
75
 
      #         # if move_line.reconcile_id == false
76
 
      #         if move_line.account_id.reconcile == true
77
 
      #           invoice_move_line_ids.push move_line.id
78
 
      #         end
79
 
      #       end
80
 
      
81
 
      inv.move_id.line_id.each do |move_line|
82
 
        if move_line.attributes['reconcile_id'] == false and move_line.account_id.reconcile == true
83
 
          invoice_move_line_ids.push move_line.id
84
 
          inv_total = inv_total + move_line.amount_currency
 
26
 
 
27
begin
 
28
    if Object.const_defined?'AccountBankStatement':
 
29
 
 
30
        # Add useful methode on bank statement handling
 
31
        ##############################################################################
 
32
        AccountBankStatement.class_eval do 
 
33
          ############################################################################
 
34
          # Import invoices into the given bank statements
 
35
          # Input :
 
36
          #  - invoices :  A valid dict of AccountInvoice instance
 
37
          #  - statement : A valid AccountBankStatement instance
 
38
          #  - options {
 
39
          #      date (Default today) : date used into the first screen of the wizard (date
 
40
          #                             imported in lines)
 
41
          #    }
 
42
          # Return
 
43
          #  - The AccountBankStatement as a instance of the class¨
 
44
          # Usage Example:
 
45
          # statement = AccountBankStatement.import_invoice(invoices,statement)
 
46
          #
 
47
          # TODO implement journals
 
48
          # TODO filter acc_move_line_ids to take the right one
 
49
          # TODO Remove self to use it from an instance of the class
 
50
          def import_invoice(invoices, options={},*args)
 
51
            o = {:date => false, :journals => []}.merge(options)
 
52
            if o[:date] :
 
53
                o[:date] = Date.parse(str=o[:date]).to_s
 
54
            else
 
55
                o[:date] = Date.today.to_s
 
56
            end
 
57
 
 
58
            # Parse the given journal ids if provided
 
59
            journal_ids=[]
 
60
            o[:journals].each do |journal|
 
61
              journal_ids.push journal.id
 
62
            end
 
63
            invoice_move_line_ids=[]
 
64
            inv_total=0.0
 
65
            self.balance_start=inv_total
 
66
 
 
67
            # # For each invoices, add the right account.move.line and compute total
 
68
            invoices.each do |inv|
 
69
              unless inv.class  == AccountInvoice :
 
70
                raise "!!! --- HELPER ERROR :import_invoice received a #{inv.class.to_s} instead of AccountInvoice" 
 
71
              end
 
72
              # Take the move ids from concerned invoice
 
73
              # TODO add journal support and (journal_ids.include? move_line.journal_id or journal_ids == []
 
74
              # TODO : Debug the problem of undefined method `[]' for nil:NilClass (NoMethodError)
 
75
              # pp inv.move_id.line_id
 
76
              #       inv.move_id.line_id.each do |move_line|
 
77
              #         
 
78
              #         # if move_line.reconcile_id == false
 
79
              #         if move_line.account_id.reconcile == true
 
80
              #           invoice_move_line_ids.push move_line.id
 
81
              #         end
 
82
              #       end
 
83
 
 
84
              inv.move_id.line_id.each do |move_line|
 
85
                if (not move_line.attributes['reconcile_id']) && move_line.account_id.reconcile
 
86
                  invoice_move_line_ids.push move_line.id
 
87
                  inv_total = inv_total + move_line.amount_currency
 
88
                end
 
89
              end
 
90
            end
 
91
            # Save the start and end values
 
92
            self.balance_end_real=inv_total
 
93
            self.save
 
94
 
 
95
            unless self.class  == AccountBankStatement :
 
96
              raise "!!! --- HELPER ERROR :import_invoice received a #{self.class.to_s} instead of AccountBankStatement" 
 
97
            end
 
98
 
 
99
            # Call the wizard
 
100
            wizard = self.old_wizard_step('populate_statement_from_inv')
 
101
            # Set the wizard with given values
 
102
            step_dict = wizard.datas.merge({:date=>o[:date]})
 
103
            step_dict=step_dict.merge({:journal_id=> [[[],[],journal_ids]]})
 
104
            # Search the invoices and update values
 
105
            res=wizard.go(step_dict)
 
106
            step_dict=res.datas.merge(step_dict)
 
107
            # Update the step_dict with invoice we want
 
108
            step_dict=step_dict.merge({:lines=> [[[],[],invoice_move_line_ids]]})
 
109
            # Ask to populate the statement with given invoice linked account move
 
110
            res=wizard.finish(step_dict)
 
111
            # step_dict = step_dict.merge({:writeoff_acc_id => @journal.default_debit_account_id.id, :writeoff_journal_id=>@journal.id})
 
112
            # return res
 
113
          end
 
114
          ############################################################################
 
115
          # Create an statement with given informations
 
116
          # Input :
 
117
          #  - options {
 
118
          #    journal : A valid AccountJournal instance
 
119
          #    currency_code (Default : EUR) : An ISO code for currency
 
120
          #    name (Default : computed by OpenERP sequence) : A valid name if you wanna set it
 
121
          #  }
 
122
          # Return
 
123
          #  - The created AccountBankStatement as a instance of the class¨
 
124
          # Usage Example:
 
125
          # statement = AccountBankStatement.create_statement_with_currency({currency_code =>'CHF'})
 
126
          def self.create_statement_with_currency(options={}, *args)
 
127
            o = {:currency_code=>'EUR', :journal=>false, :name=>false}.merge(options)
 
128
            if not o[:journal] :
 
129
              # Take the currency
 
130
              currency_id = ResCurrency.find(:first, :domain=>[['code','=',o[:currency_code]]]).id
 
131
              if currency_id : 
 
132
                # Look for the asked cash journal currency
 
133
                journal = AccountJournal.find(:first, :domain=>[['type','=','cash'],['currency','=',currency_id]])
 
134
              else
 
135
                raise "!!! --- HELPER ERROR : create_statement_with_currency #{o[:currency_code]} currency not found"
 
136
              end
 
137
            else
 
138
              journal = o[:journal]
 
139
            end
 
140
            toreturn = AccountBankStatement.new()
 
141
            toreturn.journal_id = journal.id
 
142
            toreturn.create
 
143
            if o[:name]:
 
144
              toreturn.name=o[:name]
 
145
              toreturn.save
 
146
            end
 
147
            return toreturn
 
148
          end
 
149
 
85
150
        end
86
 
      end
87
 
    end
88
 
    # Save the start and end values
89
 
    self.balance_end_real=inv_total
90
 
    self.save
91
 
 
92
 
    unless self.class  == AccountBankStatement :
93
 
      raise "!!! --- HELPER ERROR :import_invoice received a #{self.class.to_s} instead of AccountBankStatement" 
94
 
    end
95
 
 
96
 
    # Call the wizard
97
 
    wizard = self.old_wizard_step('populate_statement_from_inv')
98
 
    # Set the wizard with given values
99
 
    step_dict = wizard.datas.merge({:date=>o[:date]})
100
 
    step_dict=step_dict.merge({:journal_id=> [[[],[],journal_ids]]})
101
 
    # Search the invoices and update values
102
 
    res=wizard.go(step_dict)
103
 
    step_dict=res.datas.merge(step_dict)
104
 
    # Update the step_dict with invoice we want
105
 
    step_dict=step_dict.merge({:lines=> [[[],[],invoice_move_line_ids]]})
106
 
    # Ask to populate the statement with given invoice linked account move
107
 
    res=wizard.finish(step_dict)
108
 
    # step_dict = step_dict.merge({:writeoff_acc_id => @journal.default_debit_account_id.id, :writeoff_journal_id=>@journal.id})
109
 
    # return res
110
 
  end
111
 
  ############################################################################
112
 
  # Create an statement with given informations
113
 
  # Input :
114
 
  #  - options {
115
 
  #    journal : A valid AccountJournal instance
116
 
  #    currency_code (Default : EUR) : An ISO code for currency
117
 
  #    name (Default : computed by OpenERP sequence) : A valid name if you wanna set it
118
 
  #  }
119
 
  # Return
120
 
  #  - The created AccountBankStatement as a instance of the class¨
121
 
  # Usage Example:
122
 
  # statement = AccountBankStatement.create_statement_with_currency({currency_code =>'CHF'})
123
 
  def self.create_statement_with_currency(options={}, *args)
124
 
    o = {:currency_code=>'EUR', :journal=>false, :name=>false}.merge(options)
125
 
    if not o[:journal] :
126
 
      # Take the currency
127
 
      currency_id = ResCurrency.find(:first, :domain=>[['code','=',o[:currency_code]]]).id
128
 
      if currency_id : 
129
 
        # Look for the asked cash journal currency
130
 
        journal = AccountJournal.find(:first, :domain=>[['type','=','cash'],['currency','=',currency_id]])
131
 
      else
132
 
        raise "!!! --- HELPER ERROR : create_statement_with_currency #{o[:currency_code]} currency not found"
133
 
      end
134
151
    else
135
 
      journal = o[:journal]
136
 
    end
137
 
    toreturn = AccountBankStatement.new()
138
 
    toreturn.journal_id = journal.id
139
 
    toreturn.create
140
 
    if o[:name]:
141
 
      toreturn.name=o[:name]
142
 
      toreturn.save
143
 
    end
144
 
    return toreturn
145
 
  end
146
 
  
147
 
end
 
 
b'\\ No newline at end of file'
 
152
        puts "WARNING : Account Helpers can't be initialized -> account module isn't installed !!!"
 
153
    end
 
154
rescue Exception => e
 
155
    puts e.to_s
 
156
end