From 036a0af8dffd9a66321f1d3c97d8570f76975f77 Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Tue, 21 Apr 2020 19:45:32 +1000 Subject: [PATCH] include product option pricing in the main dist under the plugins directory --- MANIFEST | 3 + .../lib/BSECustom/ProductOptionPricing.pm | 150 ++++++++++++++++++ .../templates/admin/edit_prodopt_custom.tmpl | 18 +++ .../templates/admin/prodopt_edit_custom.tmpl | 19 +++ 4 files changed, 190 insertions(+) create mode 100644 plugins/optionpricing/lib/BSECustom/ProductOptionPricing.pm create mode 100644 plugins/optionpricing/templates/admin/edit_prodopt_custom.tmpl create mode 100644 plugins/optionpricing/templates/admin/prodopt_edit_custom.tmpl diff --git a/MANIFEST b/MANIFEST index 97df6f8c..0f8fe11c 100644 --- a/MANIFEST +++ b/MANIFEST @@ -11,6 +11,9 @@ Makefile makehtmldocs.pl MANIFEST MANIFEST.SKIP +plugins/optionpricing/lib/BSECustom/ProductOptionPricing.pm +plugins/optionpricing/templates/admin/edit_prodopt_custom.tmpl +plugins/optionpricing/templates/admin/prodopt_edit_custom.tmpl README schema/article.txt schema/bse.sql diff --git a/plugins/optionpricing/lib/BSECustom/ProductOptionPricing.pm b/plugins/optionpricing/lib/BSECustom/ProductOptionPricing.pm new file mode 100644 index 00000000..36da3d20 --- /dev/null +++ b/plugins/optionpricing/lib/BSECustom/ProductOptionPricing.pm @@ -0,0 +1,150 @@ +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; diff --git a/plugins/optionpricing/templates/admin/edit_prodopt_custom.tmpl b/plugins/optionpricing/templates/admin/edit_prodopt_custom.tmpl new file mode 100644 index 00000000..8d441986 --- /dev/null +++ b/plugins/optionpricing/templates/admin/edit_prodopt_custom.tmpl @@ -0,0 +1,18 @@ +<:.define addform_value_head:> +
ValuesPrice offsetDefault
+<:.end define :> +<:.define addform_value_entry:> +
"> + " maxlength="255" class="editor_field" title="Enter some values here" /><:.call "error_img", field: "value" _ index:> + " maxlength="10" /><:.call "error_img", field: "price" _ index:> + +
+<:.end define:> +<:.define value_entry:> +
"> +<:= dboptionvalue.value:> +<:= bse.number("money", dboptionvalue.get_custom("value_price") or 0) :> +<:.if dboptionvalue.id == dboption.default_value:>(default)<:.end if:> +<:.call "value_entry_menu":> +
+<:.end define:> diff --git a/plugins/optionpricing/templates/admin/prodopt_edit_custom.tmpl b/plugins/optionpricing/templates/admin/prodopt_edit_custom.tmpl new file mode 100644 index 00000000..2a2d4c65 --- /dev/null +++ b/plugins/optionpricing/templates/admin/prodopt_edit_custom.tmpl @@ -0,0 +1,19 @@ +<:.define value_head :> + + Value + Price Offset + Default<:.call "error_img", field:"default_value":> + +<:.end define:> +<:.define value_entry:> + + " /><:.call "error_img", field:"value" _ value.id:> + " /><:.call "error_img", field:"price" _ value.id:> + checked="checked"<:.end if:> /> + +<:.end define:> +<:-.define newvalue_entry -:> +
+:>" /><:.call "error_img", field:"newvalue" _ index :> +:>" /><:.call "error_img", field:"newprice" _ index :>
+<:.end define-:> -- 2.30.2