~akretion-team/oerpscenario/core

« back to all changes in this revision

Viewing changes to lib/cucumber/lib/Helpers/AccountInvoice.rb

  • Committer: nicolas.bessi at camptocamp
  • Date: 2012-04-05 15:31:36 UTC
  • Revision ID: nicolas.bessi@camptocamp.com-20120405153136-v92eas3ais6zgs45
[IMP] remove dead code + fix @openerp property

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
###############################################################################
2
 
#
3
 
#    OERPScenario, OpenERP Functional Tests
4
 
#    Author Nicolas Bessi & Joel Grand-Guillaume 2009 
5
 
#    Copyright Camptocamp SA
6
 
#
7
 
#    This program is free software: you can redistribute it and/or modify
8
 
#    it under the terms of the GNU General Public License as published by
9
 
#    the Free Software Foundation, either version 3 Afero of the License, or
10
 
#    (at your option) any later version.
11
 
#
12
 
#    This program is distributed in the hope that it will be useful,
13
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
#    GNU General Public License for more details.
16
 
#
17
 
#    You should have received a copy of the GNU General Public License
18
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
#
20
 
##############################################################################
21
 
require 'rubygems'
22
 
require 'ooor'
23
 
require 'pp'
24
 
 
25
 
 
26
 
begin
27
 
  if Object.const_defined? 'AccountInvoice'
28
 
    # Add useful methode on invoice handling
29
 
    ##############################################################################
30
 
    AccountInvoice.class_eval do
31
 
      $utils.log.debug("Extending  #{self.class} #{self.name}")
32
 
      ##########################################################################
33
 
      # Create an invoice with given informations
34
 
      # Add a line if amount <> false, the account could be provided or not
35
 
      # Input :
36
 
      #  - name : Name of the invoice
37
 
      #  - partner : A valid ResPartner instance
38
 
      #  - option {
39
 
      #    currency_code (Default : EUR) : An ISO code for currency
40
 
      #    date (Default : false) : A date in this text format : 1 jan 2009
41
 
      #    amount (Default : false) : An amount for the invoice => this will create a line 
42
 
      #    account (Default : false) : An valide AccountAccount
43
 
      #    type (Default : out_invoice) : the invoice type
44
 
      #  }
45
 
      # Return
46
 
      #  - The created AccountInvoice as a instance of the class¨
47
 
      # Usage Example:
48
 
      # part = ResPartner.find(:first)
49
 
      # puts part.id
50
 
      # inv = AccountInvoice.create_cust_invoice_with_currency('my name',part,{currency_code =>'CHF'})
51
 
      def self.create_invoice_with_currency(name, partner, options={}, *args)
52
 
        o = {:type => 'out_invoice', :currency_code => 'EUR', :date => false, :amount => false, :account => false}.merge(options)
53
 
        if o[:date]
54
 
          date_invoice = Date.parse(str=o[:date]).to_s
55
 
        else
56
 
          date_invoice = Date.today.to_s
57
 
        end
58
 
        toreturn = AccountInvoice.new()
59
 
 
60
 
        unless partner.class == ResPartner
61
 
          raise "!!! --- HELPER ERROR :create_cust_invoice_with_currency received a #{partner.class.to_s} instead of ResPartner"
62
 
        end
63
 
        # Set partner
64
 
        #We use this syntax for optimisation reason, helper have to be fast
65
 
        if  ResPartnerAddress.find(:first, :domain => [['partner_id', '=', partner.id]], :fields => ['id'])
66
 
          toreturn.partner_id = partner.id
67
 
        else
68
 
          raise "!!! --- HELPER ERROR :create_cust_invoice_with_currency received a partner : #{partner.name} without adresses"
69
 
        end
70
 
        toreturn.on_change('onchange_partner_id', :partner_id, partner.id, o[:type], partner.id, date_invoice, false, false)
71
 
        toreturn.save
72
 
        # Set name & date
73
 
        toreturn.name = name
74
 
        toreturn.date_invoice=date_invoice
75
 
 
76
 
        # Set type of invoice
77
 
        toreturn.type = o[:type]
78
 
        curr = ResCurrency.find(:first, :domain => [['name', '=', o[:currency_code]]], :fields => ['id'])
79
 
        # Set currency
80
 
        if curr
81
 
          toreturn.currency_id = curr.id
82
 
        else
83
 
          raise "!!! --- HELPER ERROR :#{o[:currency_code]} currency not found"
84
 
        end
85
 
 
86
 
        # Set amount and line if asked for
87
 
        toreturn.save
88
 
 
89
 
        if o[:amount]
90
 
 
91
 
          if ['in_invoice', 'in_refund'].include? o[:type]
92
 
            toreturn.check_total = o[:amount]
93
 
          end
94
 
          if o[:account]
95
 
            unless account.class == AccountAccount
96
 
              raise "!!! --- HELPER ERROR :create_cust_invoice_with_currency received a #{o[:account].class.to_s} instead of AccountAccount"
97
 
            end
98
 
            account_id = o[:account].id
99
 
          else
100
 
            # If no account, take on of type 'other' and a non-reconciliable account
101
 
            account_id = AccountAccount.find(:first, :domain => [['type', '=', 'other'], ['reconcile', '=', false]], :fields => ['id']).id
102
 
            # Create a line = amount for the created invoice
103
 
            line=AccountInvoiceLine.new(
104
 
                :account_id => account_id,
105
 
                :quantity => 1,
106
 
                :name => name+' line',
107
 
                :price_unit => o[:amount],
108
 
                :invoice_id => toreturn.id
109
 
            )
110
 
            line.create
111
 
          end
112
 
        end
113
 
        toreturn.save
114
 
        return toreturn
115
 
      end
116
 
    end
117
 
  else
118
 
    $utils.log.debug("AccountInvoice helper not initialized")
119
 
  end
120
 
rescue Exception => e
121
 
  $utils.log.fatal("ERROR : #{e.to_s}")
122
 
end