include product option pricing in the main dist
authorTony Cook <tony@develop-help.com>
Tue, 21 Apr 2020 09:45:32 +0000 (19:45 +1000)
committerTony Cook <tony@develop-help.com>
Tue, 21 Apr 2020 09:45:32 +0000 (19:45 +1000)
under the plugins directory

MANIFEST
plugins/optionpricing/lib/BSECustom/ProductOptionPricing.pm [new file with mode: 0644]
plugins/optionpricing/templates/admin/edit_prodopt_custom.tmpl [new file with mode: 0644]
plugins/optionpricing/templates/admin/prodopt_edit_custom.tmpl [new file with mode: 0644]

index 97df6f8c5e2fd79d04d9cfcc1c2f94c8eef98ea2..0f8fe11c19b7e6506b99e8cd9ed6b2cfe4b4b2e0 100644 (file)
--- 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 (file)
index 0000000..36da3d2
--- /dev/null
@@ -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 (file)
index 0000000..8d44198
--- /dev/null
@@ -0,0 +1,18 @@
+<:.define addform_value_head:>
+<div class="valueentry"><span>Values</span><span>Price offset</span><span>Default</span></div>
+<:.end define :>
+<:.define addform_value_entry:>
+<div class="valueentry<:= index mod 2 == 0 ? "" : " odd":>">
+  <span><input type="text" name="value<:= index :>" value="<:= cgi.param("value" _ index) :>" maxlength="255" class="editor_field" title="Enter some values here" /><:.call "error_img", field: "value" _ index:></span>
+  <span><input type="text" name="price<:= index :>" value="<:= cgi.param("price" _ index) :>" maxlength="10" /><:.call "error_img", field: "price" _ index:></span>
+  <span><input type="radio" name="default_value" value="value<:= index :>"></span>
+</div>
+<:.end define:>
+<:.define value_entry:>
+<div id="valentry<:= dboptionvalue.id:>" class="valueentry<:= loop.index mod 2 == 1 ? " odd" : "":>">
+<span id="prodoptvalue<:= dboptionvalue.id:>"><:= dboptionvalue.value:></span>
+<span><:= bse.number("money", dboptionvalue.get_custom("value_price") or 0) :></span>
+<:.if dboptionvalue.id == dboption.default_value:>(default)<:.end if:>
+<:.call "value_entry_menu":>
+</div>
+<:.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 (file)
index 0000000..2a2d4c6
--- /dev/null
@@ -0,0 +1,19 @@
+<:.define value_head :>
+<tr>
+  <th>Value</th>
+  <th>Price Offset</th>
+  <th>Default<:.call "error_img", field:"default_value":></th>
+</tr>
+<:.end define:>
+<:.define value_entry:>
+<tr>
+  <td><input type="text" name="value<:= value.id:>" value="<:= cgi.param("save_enabled") ? cgi.param("value" _ value.id) : value.value:>" /><:.call "error_img", field:"value" _ value.id:></td>
+  <td><input type="text" name="price<:= value.id:>" value="<:= cgi.param("save_enabled") ? cgi.param("price" _ value.id) : ((value.get_custom("value_price") or 0) / 100).format("%.2f") :>" /><:.call "error_img", field:"price" _ value.id:></td>
+  <td class="check"><input type="radio" name="default_value" value="<:= value.id:>" <:.if value.id == option.default_value:>checked="checked"<:.end if:> /></td>
+</tr>
+<:.end define:>
+<:-.define newvalue_entry -:>
+<div><label for="newvalue<:= index:>">Value:</label>
+<input type="text" name="newvalue<:= index:>" value="<:=cgi.param("newvalue" _ index) :>:>" /><:.call "error_img", field:"newvalue" _ index :>
+<label for="newprice<:= index:>">Price:</label><input type="text" name="newprice<:= index:>" value="<:=cgi.param("newprice" _ index) :>:>" /><:.call "error_img", field:"newprice" _ index :></div>
+<:.end define-:>