1
###############################################################################
3
# OERPScenario, OpenERP Functional Tests
4
# Author Nicolas Bessi & Joel Grand-Guillaume 2009
5
# Copyright Camptocamp SA
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.
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.
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/>.
20
##############################################################################
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
36
# - name : Name of the invoice
37
# - partner : A valid ResPartner instance
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
46
# - The created AccountInvoice as a instance of the class¨
48
# part = ResPartner.find(:first)
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)
54
date_invoice = Date.parse(str=o[:date]).to_s
56
date_invoice = Date.today.to_s
58
toreturn = AccountInvoice.new()
60
unless partner.class == ResPartner
61
raise "!!! --- HELPER ERROR :create_cust_invoice_with_currency received a #{partner.class.to_s} instead of ResPartner"
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
68
raise "!!! --- HELPER ERROR :create_cust_invoice_with_currency received a partner : #{partner.name} without adresses"
70
toreturn.on_change('onchange_partner_id', :partner_id, partner.id, o[:type], partner.id, date_invoice, false, false)
74
toreturn.date_invoice=date_invoice
77
toreturn.type = o[:type]
78
curr = ResCurrency.find(:first, :domain => [['name', '=', o[:currency_code]]], :fields => ['id'])
81
toreturn.currency_id = curr.id
83
raise "!!! --- HELPER ERROR :#{o[:currency_code]} currency not found"
86
# Set amount and line if asked for
91
if ['in_invoice', 'in_refund'].include? o[:type]
92
toreturn.check_total = o[:amount]
95
unless account.class == AccountAccount
96
raise "!!! --- HELPER ERROR :create_cust_invoice_with_currency received a #{o[:account].class.to_s} instead of AccountAccount"
98
account_id = o[:account].id
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,
106
:name => name+' line',
107
:price_unit => o[:amount],
108
:invoice_id => toreturn.id
118
$utils.log.debug("AccountInvoice helper not initialized")
120
rescue Exception => e
121
$utils.log.fatal("ERROR : #{e.to_s}")