multi-currency, RT#21565
authorIvan Kohler <ivan@freeside.biz>
Sat, 8 Jun 2013 12:14:19 +0000 (05:14 -0700)
committerIvan Kohler <ivan@freeside.biz>
Sat, 8 Jun 2013 12:14:19 +0000 (05:14 -0700)
FS/FS/Schema.pm
FS/FS/cust_main.pm
FS/FS/cust_pkg.pm
FS/FS/part_pkg.pm
FS/FS/part_pkg/currency_fixed.pm [new file with mode: 0644]

index 0487186..f1cc09c 100644 (file)
@@ -1081,6 +1081,7 @@ sub tables_hashref {
         'ship_night',    'varchar', 'NULL', 20, '', '', 
         'ship_fax',      'varchar', 'NULL', 12, '', '', 
         'ship_mobile',   'varchar', 'NULL', 12, '', '', 
+        'currency',         'char', 'NULL',  3, '', '',
         'payby',    'char', '',     4, '', '', 
         'payinfo',  'varchar', 'NULL', 512, '', '', 
         'paycvv',   'varchar', 'NULL', 512, '', '', 
index f4c3beb..3fbee15 100644 (file)
@@ -1757,6 +1757,7 @@ sub check {
     || $self->ut_flag('invoice_noemail')
     || $self->ut_flag('message_noemail')
     || $self->ut_enum('locale', [ '', FS::Locales->locales ])
+    || $self->ut_currency('currency')
   ;
 
   my $company = $self->company;
index 65bfc80..8357386 100644 (file)
@@ -2343,6 +2343,24 @@ sub num_cust_event {
   $sth->fetchrow_arrayref->[0];
 }
 
+=item part_pkg_currency_option OPTIONNAME
+
+Returns the option value for the given name and the currency of this customer
+(see L<FS::part_pkg_currency>).  If this customer has no currency, returns
+the regular option value for the given name (see L<FS::part_pkg_option>).
+
+=cut
+
+sub part_pkg_currency_option {
+  my( $self, $optionname ) = @_;
+  my $part_pkg = $self->part_pkg;
+  if ( my $currency = $self->cust_main->currency ) {
+    $part_pkg->part_pkg_currency_option($currency, $optionname);
+  } else {
+    $part_pkg->option($optionname);
+  }
+}
+
 =item cust_svc [ SVCPART ] (old, deprecated usage)
 
 =item cust_svc [ OPTION => VALUE ... ] (current usage)
index 67372ac..22e8828 100644 (file)
@@ -1076,6 +1076,8 @@ sub can_discount { 0; }
 
 sub can_start_date { 1; }
 
+sub can_currency_exchange { 0; }
+
 sub freqs_href {
   # moved to FS::Misc to make this accessible to other packages
   # at initialization
@@ -1270,6 +1272,28 @@ sub part_pkg_currency_options {
   map { $_->optionname => $_->optionvalue } $self->part_pkg_currency(shift);
 }
 
+=item part_pkg_currency_option CURRENCY OPTIONNAME
+
+Returns the option value for the given name and currency.
+
+=cut
+
+sub part_pkg_currency_option {
+  my( $self, $currency, $optionname ) = @_; 
+  my $part_pkg_currency =
+    qsearchs('part_pkg_currency', { 'pkgpart'    => $self->pkgpart,
+                                    'currency'   => $currency,
+                                    'optionname' => $optionname,
+                                  }
+            )#;
+  #fatal if not found?  that works for our use cases from
+  #part_pkg/currency_fixed, but isn't how we would typically/expect the method
+  #to behave.  have to catch it there if we change it here...
+    or die "Unknown price for ". $self->pkg_comment. " in $currency\n";
+
+  $part_pkg_currency->optionvalue;
+}
+
 =item bill_part_pkg_link
 
 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
diff --git a/FS/FS/part_pkg/currency_fixed.pm b/FS/FS/part_pkg/currency_fixed.pm
new file mode 100644 (file)
index 0000000..bee6c5b
--- /dev/null
@@ -0,0 +1,67 @@
+package FS::part_pkg::currency_fixed;
+#can't discount yet
+#use base qw( FS::part_pkg::discount_Mixin FS::part_pkg::recur_Common );
+use base qw( FS::part_pkg::recur_Common );
+
+use strict;
+use vars qw( %info );
+#use FS::Record qw(qsearch qsearchs);
+
+%info = (
+  'name' => 'Per-currency pricing from package definitions',
+  'shortname' => 'Per-currency pricing',
+  'inherit_fields' => [ 'prorate_Mixin', 'global_Mixin' ],
+  'fields' => {
+    'cutoff_day'    => { 'name' => 'Billing Day (1 - 28) for prorating or '.
+                                   'subscription',
+                         'default' => '1',
+                       },
+
+    'recur_method'  => { 'name' => 'Recurring fee method',
+                         #'type' => 'radio',
+                         #'options' => \%recur_method,
+                         'type' => 'select',
+                         'select_options' => \%FS::part_pkg::recur_Common::recur_method,
+                       },
+  },
+  'fieldorder' => [qw( recur_method cutoff_day ),
+                   FS::part_pkg::prorate_Mixin::fieldorder,
+                  )],
+  'weight' => '59',
+);
+
+sub price_info {
+    my $self = shift;
+    my $str = $self->SUPER::price_info;
+    $str .= " (or local currency pricing)" if $str;
+    $str;
+}
+
+#some false laziness w/recur_Common, could have been better about it.. pry when
+# we do discounting
+sub calc_setup {
+  my($self, $cust_pkg, $sdate, $details, $param) = @_;
+
+  return 0 if $self->prorate_setup($cust_pkg, $sdate);
+
+  sprintf('%.2f', $cust_pkg->part_pkg_currency_option('setup_fee') );
+}
+
+sub base_recur {
+  my( $self, $cust_pkg ) = @_;
+  sprintf('%.2f', $cust_pkg->part_pkg_currency_option('recur_fee') );
+}
+
+sub can_discount { 0; } #can't discount yet (percentage would work, but amount?)
+sub calc_recur {
+  my $self = shift;
+  #my($cust_pkg, $sdate, $details, $param ) = @_;
+  #$self->calc_recur_Common($cust_pkg,$sdate,$details,$param);
+  $self->calc_recur_Common(@_);
+}
+
+sub is_free { 0; }
+
+sub can_currency_exchange { 1; }
+
+1;