ce7145278bddcab077a549498d0af115d18d0ec6
[freeside.git] / FS / FS / part_pkg / currency_fixed.pm
1 package FS::part_pkg::currency_fixed;
2 #can't discount yet
3 #use base qw( FS::part_pkg::discount_Mixin FS::part_pkg::recur_Common );
4 use base qw( FS::part_pkg::recur_Common );
5
6 use strict;
7 use vars qw( %info );
8 use FS::Record qw(qsearchs); # qsearch qsearchs);
9 use FS::currency_exchange;
10
11 %info = (
12   'name' => 'Per-currency pricing from package definitions',
13   'shortname' => 'Per-currency pricing',
14   'inherit_fields' => [ 'prorate_Mixin', 'global_Mixin' ],
15   'fields' => {
16     'cutoff_day'    => { 'name' => 'Billing Day (1 - 28) for prorating or '.
17                                    'subscription',
18                          'default' => '1',
19                        },
20
21     'recur_method'  => { 'name' => 'Recurring fee method',
22                          #'type' => 'radio',
23                          #'options' => \%recur_method,
24                          'type' => 'select',
25                          'select_options' => \%FS::part_pkg::recur_Common::recur_method,
26                        },
27   },
28   'fieldorder' => [qw( recur_method cutoff_day ),
29                    FS::part_pkg::prorate_Mixin::fieldorder,
30                   ],
31   'weight' => '59',
32 );
33
34 sub price_info {
35     my $self = shift;
36     my $str = $self->SUPER::price_info;
37     $str .= " (or local currency pricing)" if $str;
38     $str;
39 }
40
41 sub base_setup {
42   my($self, $cust_pkg, $sdate, $details, $param ) = @_;
43
44   $self->calc_currency_option('setup_fee', $cust_pkg, $sdate, $details, $param);
45 }
46
47 sub calc_setup {
48   my($self, $cust_pkg, $sdate, $details, $param) = @_;
49
50   return 0 if $self->prorate_setup($cust_pkg, $sdate);
51
52   $self->base_setup($cust_pkg, $sdate, $details, $param);
53 }
54
55 use FS::Conf;
56 sub calc_currency_option {
57   my($self, $optionname, $cust_pkg, $sdate, $details, $param) = @_;
58
59   my($currency, $amount) = $cust_pkg->part_pkg_currency_option($optionname);
60   return sprintf('%.2f', $amount ) unless $currency;
61
62   $param->{'billed_currency'} = $currency;
63   $param->{'billed_amount'}   = $amount;
64
65   my $currency_exchange = qsearchs('currency_exchange', {
66     'from_currency' => $currency,
67     'to_currency'   => ( FS::Conf->new->config('currency') || 'USD' ),
68   }) or die "No exchange rate from $currency\n";
69
70   #XXX do we want the rounding here to work differently?
71   #my $recognized_amount =
72   sprintf('%.2f', $amount * $currency_exchange->rate);
73 }
74
75 sub base_recur {
76   my( $self, $cust_pkg, $sdate, $details, $param ) = @_;
77   $param ||= {};
78   $self->calc_currency_option('recur_fee', $cust_pkg, $sdate, $details, $param);
79 }
80
81 sub can_discount { 0; } #can't discount yet (percentage would work, but amount?)
82 sub calc_recur {
83   my $self = shift;
84   #my($cust_pkg, $sdate, $details, $param ) = @_;
85   #$self->calc_recur_Common($cust_pkg,$sdate,$details,$param);
86   $self->calc_recur_Common(@_);
87 }
88
89 sub is_free { 0; }
90
91 sub can_currency_exchange { 1; }
92
93 1;