--- /dev/null
+package BSECustom::ProductOptionPricing;
+use strict;
+
+our $VERSION = "1.000";
+
+sub init {
+ my ($class) = @_;
+
+ my $self = bless {}, $class;
+
+ BSE::PubSub->handle("*", [ $self ]);
+}
+
+sub product_edit_variables {
+ my ($self, $params) = @_;
+
+ $params->{req}->set_variable(
+ option_value_price => sub {
+ $_[0]->get_custom("value_price");
+ });
+}
+
+sub product_option_edit_variables {
+ my ($self, $params) = @_;
+
+ $params->{req}->set_variable(
+ option_value_price => sub {
+ $_[0]->get_custom("value_price");
+ });
+}
+
+my $price_re1 = qr/^\s*-?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\s*$/;
+my $price_re2 = qr/^\s*-?(?:[0-9]+(?:\.[0-9]{0,2})?|\.[0-9]{1,2})\s*$/;
+
+sub product_option_add_validate {
+ my ($self, $params) = @_;
+
+ my $cgi = $params->{req}->cgi;
+ my $error = $params->{errors};
+ for my $value_field (grep /^value[1-9][0-9]*$/, $cgi->param) {
+ my $val = $cgi->param($value_field);
+ if (defined $val && $val =~ /\S/) {
+ (my $price_field = $value_field) =~ s/^value/price/;
+ my $price = $cgi->param($price_field);
+ if (defined $price && $price =~ /\S/) {
+ if ($price !~ $price_re1) {
+ $error->{$price_field} = "Invalid price";
+ }
+ elsif ($price !~ $price_re2) {
+ $error->{$price_field} = "Invalid price, only two cents digits allowed";
+ }
+ }
+ }
+ }
+}
+
+sub product_option_add {
+ my ($self, $params) = @_;
+
+ my $values = $params->{values};
+ my $cgi = $params->{req}->cgi;
+ for my $key (keys %$values) {
+ (my $price_key = $key) =~ s/^value/price/;
+ my $price = ($cgi->param($price_key))[0] || 0;
+ my $option = $values->{$key};
+ $option->set_custom(value_price => 0+sprintf("%.0f", $price * 100));
+ $option->save;
+ }
+}
+
+sub product_option_edit_validate {
+ my ($self, $params) = @_;
+
+ my $cgi = $params->{req}->cgi;
+ my $error = $params->{errors};
+ for my $value ($params->{option}->values) {
+ my $price_key = "price".$value->id;
+ my $price = ($cgi->param($price_key))[0] || 0;
+ if (defined $price && $price =~ /\S/) {
+ if ($price !~ $price_re1) {
+ $error->{$price_key} = "Invalid price";
+ }
+ elsif ($price !~ $price_re2) {
+ $error->{$price_key} = "Invalid price, only two cents digits allowed";
+ }
+ }
+ }
+
+ my $newvalues = $params->{newvalues};
+ for my $newkey (keys %$newvalues) {
+ my $newvalue = $newvalues->{$newkey};
+ (my $price_key = $newkey) =~ s/value/price/;
+ my $price = ($cgi->param($price_key))[0] || 0;
+ if (defined $price) {
+ if ($price !~ $price_re1) {
+ $error->{$price_key} = "Invalid price";
+ }
+ elsif ($price !~ $price_re2) {
+ $error->{$price_key} = "Invalid price, only two cents digits allowed";
+ }
+ }
+ }
+}
+
+sub product_option_edit_save {
+ my ($self, $params) = @_;
+
+ my $cgi = $params->{req}->cgi;
+ for my $value ($params->{option}->values) {
+ my $price_key = "price".$value->id;
+ my $price = ($cgi->param($price_key))[0] || 0;
+ $value->set_custom(value_price => 0+sprintf("%.0f", $price * 100));
+ $value->save;
+ }
+
+ my $newvalues = $params->{newvalues};
+ for my $newkey (keys %$newvalues) {
+ my $newvalue = $newvalues->{$newkey};
+ (my $price_key = $newkey) =~ s/value/price/;
+ my $price = ($cgi->param($price_key))[0] || 0;
+ $newvalue->set_custom(value_price => sprintf("%.0f", $price * 100));
+ $newvalue->save;
+ }
+}
+
+sub cart_price {
+ my ($self, $params) = @_;
+
+ my $item = $params->{cartitem};
+ my $rprice = $params->{price};
+
+ for my $option ($item->option_list) {
+ if (my $value = $option->{valueobj}) {
+ $$rprice += $value->get_custom("value_price") || 0;
+ }
+ }
+}
+
+sub order_item_option {
+ my ($self, $params) = @_;
+
+ my $cartitem = $params->{cartitem};
+ my $ooption = $params->{orderitemoption};
+ my $value = $params->{cartoption};
+
+ $ooption->set_custom(value_price => $value->get_custom("value_price") || 0);
+ $ooption->save;
+}
+
+1;