added vendor management

fixed a few privilege statements in some other code
This commit is contained in:
JT Smith 2008-04-29 21:25:15 +00:00
parent 175b977e4e
commit 6616504077
11 changed files with 610 additions and 19 deletions

View file

@ -46,10 +46,29 @@ mergeProductsWithCommerce($session);
addCaptchaToDataForm( $session );
addArchiveEnabledToCollaboration( $session );
addShelf( $session );
addVendors($session);
modifyThingyPossibleValues( $session );
finish($session); # this line required
#----------------------------------------------------------------------------
sub addVendors {
my $session = shift;
print "\tAdding vendors... " unless $quiet;
$session->db->write(q{
create table vendor (
vendorId varchar(22) binary not null primary key,
dateCreated datetime,
name varchar(255)
)
});
$session->db->write(q{
insert into vendor (vendorId,name,dateCreated) values ('defaultvendor000000000','Default Vendor',now())
});
print "DONE!\n" unless $quiet;
}
#----------------------------------------------------------------------------
# Add the archiveEnabled field to Collaboration assets
sub addArchiveEnabledToCollaboration {
@ -355,7 +374,9 @@ sub convertTransactionLog {
shippingDate datetime,
quantity int not null default 1,
price float,
index transactionId (transactionId)
vendorId varchar(22) binary not null default 'defaultvendor000000000',
index transactionId (transactionId),
index vendorId (vendorId)
)");
$session->setting->add('shopMyPurchasesTemplateId','');
$session->setting->add('shopMyPurchaseDetailTemplateId','');
@ -429,13 +450,13 @@ sub createSkuAsset {
revisionDate bigint not null,
description mediumtext,
sku varchar(35) binary not null,
salesAgentId varchar(22) binary,
vendorId varchar(22) binary not null default 'defaultvendor000000000',
displayTitle bool not null default 1,
overrideTaxRate bool not null default 0,
taxRateOverride float not null default 0.00,
primary key (assetId, revisionDate),
index sku (sku),
index salesAgentId (salesAgentId)
index vendorId (vendorId)
)");
}

View file

@ -137,12 +137,12 @@ sub definition {
label => $i18n->get("tax rate override"),
hoverHelp => $i18n->get("tax rate override help")
},
salesAgentId => {
vendorId => {
tab => "shop",
fieldType => "hidden",
defaultValue => undef,
label => $i18n->get("sales agent"),
hoverHelp => $i18n->get("sales agent help")
fieldType => "vendor",
defaultValue => 'defaultvendor000000000',
label => $i18n->get("vendor"),
hoverHelp => $i18n->get("vendor help")
},
);
push(@{$definition}, {

View file

@ -22,6 +22,7 @@ use WebGUI::Shop::Pay;
use WebGUI::Shop::Ship;
use WebGUI::Shop::Tax;
use WebGUI::Shop::Transaction;
use WebGUI::Shop::Vendor;
=head1 NAME
@ -81,8 +82,8 @@ sub www_address {
my $output = undef;
my $method = "www_". ( $session->form->get("method") || "view");
my $cart = WebGUI::Shop::AddressBook->newBySession($session);
if ($cart->can($method)) {
$output = $cart->$method();
if (my $sub = $cart->can($method)) {
$output = $sub->();
}
return $output;
}
@ -200,6 +201,24 @@ sub www_transaction {
return $output;
}
#-------------------------------------------------------------------
=head2 www_vendor ()
Hand off to the vendor system.
=cut
sub www_vendor {
my $session = shift;
my $output = undef;
my $method = "www_".$session->form->get("method");
if ($method ne "www_" && WebGUI::Shop::Vendor->can($method)) {
$output = WebGUI::Shop::Vendor->$method($session);
}
return $output;
}
1;

189
lib/WebGUI/Form/Vendor.pm Normal file
View file

@ -0,0 +1,189 @@
package WebGUI::Form::Vendor;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2008 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use base 'WebGUI::Form::SelectList';
use WebGUI::International;
use WebGUI::Shop::Admin;
use WebGUI::Shop::Vendor;
=head1 NAME
Package WebGUI::Form::Vendor
=head1 DESCRIPTION
Creates a vendor chooser field.
=head1 SEE ALSO
This is a subclass of WebGUI::Form::SelectList.
=head1 METHODS
The following methods are specifically available from this class. Check the superclass for additional methods.
=cut
#-------------------------------------------------------------------
=head2 areOptionsSettable ( )
Returns 0.
=cut
sub areOptionsSettable {
return 0;
}
#-------------------------------------------------------------------
=head2 definition ( [ additionalTerms ] )
See the super class for additional details.
=head3 additionalTerms
The following additional parameters have been added via this sub class.
=head4 defaultValue
This will be used if no value is specified. Defaults to 'defaultvendor000000000' which is "Default Vendor".
=cut
sub definition {
my $class = shift;
my $session = shift;
my $definition = shift || [];
push(@{$definition}, {
size=>{
defaultValue=>1
},
multiple=>{
defaultValue=>0
},
defaultValue=>{
defaultValue=>'defaultvendor000000000'
},
});
return $class->SUPER::definition($session, $definition);
}
#-------------------------------------------------------------------
=head2 getDatabaseFieldType ( )
Returns "VARCHAR(22) BINARY".
=cut
sub getDatabaseFieldType {
return "VARCHAR(22) BINARY";
}
#-------------------------------------------------------------------
=head2 getName ( session )
Returns the human readable name of this control.
=cut
sub getName {
my ($self, $session) = @_;
return WebGUI::International->new($session, 'Shop')->get('vendors');
}
#-------------------------------------------------------------------
=head2 getValueAsHtml ( )
Formats as a name.
=cut
sub getValueAsHtml {
my $self = shift;
my $vendor = eval{WebGUI::Shop::Vendor->new($self->session, $self->getValue)};
if (!$@ && defined $vendor) {
return $vendor->get('name');
}
return undef;
}
#-------------------------------------------------------------------
=head2 isDynamicCompatible ( )
A class method that returns a boolean indicating whether this control is compatible with the DynamicField control.
=cut
sub isDynamicCompatible {
return 1;
}
#-------------------------------------------------------------------
=head2 toHtml ( )
Returns a group pull-down field. A group pull down provides a select list that provides name value pairs for all the vendors in the WebGUI system.
=cut
sub toHtml {
my $self = shift;
$self->set('options', WebGUI::Shop::Vendor->getVendors($self->session, {asHashRef=>1}));
return $self->SUPER::toHtml();
}
#-------------------------------------------------------------------
=head2 toHtmlAsHidden ( )
Creates a series of hidden fields representing the data in the list.
=cut
sub toHtmlAsHidden {
my $self = shift;
$self->set("options", WebGUI::Shop::Vendor->getVendors($self->session, {asHashRef=>1}));
return $self->SUPER::toHtmlAsHidden();
}
#-------------------------------------------------------------------
=head2 toHtmlWithWrapper ( )
Renders the form field to HTML as a table row complete with labels, subtext, hoverhelp, etc. Also adds a manage icon next to the field if the current user is in the admins group.
=cut
sub toHtmlWithWrapper {
my $self = shift;
if (WebGUI::Shop::Admin->new($self->session)->canManage) {
my $subtext = $self->session->icon->manage("shop=vendor;method=manage");
$self->set("subtext",$subtext . $self->get("subtext"));
}
return $self->SUPER::toHtmlWithWrapper;
}
1;

View file

@ -67,6 +67,7 @@ sub getAdminConsole {
$ac->addSubmenuItem($url->page("shop=pay;method=manage"), $i18n->get("payment methods"));
$ac->addSubmenuItem($url->page("shop=ship;method=manage"), $i18n->get("shipping methods"));
$ac->addSubmenuItem($url->page("shop=transaction;method=manage"), $i18n->get("transactions"));
$ac->addSubmenuItem($url->page("shop=vendor;method=manage"), $i18n->get("vendors"));
return $ac;
}

View file

@ -268,9 +268,9 @@ sub www_manage {
my $self = shift;
my $session = $self->session;
my $admin = WebGUI::Shop::Admin->new($session);
my $i18n = WebGUI::International->new($session, "Pay");
my $i18n = WebGUI::International->new($session, "Shop");
return $session->privilege->adminOnly() unless ($session->user->isInGroup("3"));
return $session->privilege->adminOnly() unless ($admin->canManage);
# Button for adding a payment gateway
my $output = WebGUI::Form::formHeader($session)

View file

@ -247,8 +247,8 @@ The main management screen for shippers.
sub www_manage {
my ($self) = @_;
my $session = $self->session;
return $session->privilege->adminOnly() unless ($session->user->isInGroup("3"));
my $admin = WebGUI::Shop::Admin->new($session);
return $session->privilege->adminOnly() unless ($admin->canManage);
my $i18n = WebGUI::International->new($session, "Shop");
my $output = WebGUI::Form::formHeader($session)
.WebGUI::Form::hidden($session, {name=>"shop", value=>"ship"})

View file

@ -245,7 +245,7 @@ A hash reference that contains one of the following:
A reference to a WebGUI::Shop::CartItem. Alternatively you can manually pass in any of the following
fields that would be created automatically by this object: assetId configuredTitle options shippingAddressId
shippingName shippingAddress1 shippingAddress2 shippingAddress3 shippingCity shippingState shippingCountry
shippingCode shippingPhoneNumber quantity price
shippingCode shippingPhoneNumber quantity price vendorId
=head4 shippingTrackingNumber
@ -284,7 +284,7 @@ sub update {
}
my @fields = (qw(assetId configuredTitle options shippingAddressId shippingTrackingNumber shippingStatus
shippingName shippingAddress1 shippingAddress2 shippingAddress3 shippingCity shippingState
shippingCountry shippingCode shippingPhoneNumber quantity price));
shippingCountry shippingCode shippingPhoneNumber quantity price vendorId));
foreach my $field (@fields) {
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};
}

343
lib/WebGUI/Shop/Vendor.pm Normal file
View file

@ -0,0 +1,343 @@
package WebGUI::Shop::Vendor;
use strict;
use Class::InsideOut qw{ :std };
use WebGUI::Shop::Admin;
use WebGUI::Exception::Shop;
use WebGUI::International;
=head1 NAME
Package WebGUI::Shop::Vendor
=head1 DESCRIPTION
Keeps track of vendors that sell merchandise in the store.
=head1 SYNOPSIS
use WebGUI::Shop::Vendor;
my $vendor = WebGUI::Shop::Vendor->new($session, $vendord);
=head1 METHODS
These subroutines are available from this package:
=cut
readonly session => my %session;
readonly properties => my %properties;
#-------------------------------------------------------------------
=head2 create ( session, properties )
Constructor. Creates a new vendor.
=head3 session
A reference to the current session.
=head3 properties
A hash reference containing the properties for this vendor. See update() for details.
=cut
sub create {
my ($class, $session, $properties) = @_;
unless (defined $session && $session->isa("WebGUI::Session")) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
}
my $id = $session->id->generate;
$session->db->write("insert into vendor (vendorId, dateCreated) values (?, now())",[$id]);
my $self = $class->new($session, $id);
$self->update($properties);
return $self;
}
#-------------------------------------------------------------------
=head2 delete ()
Deletes this vendor.
=cut
sub delete {
my ($self) = @_;
$self->session->db->deleteRow("vendor","vendorId",$self->getId);
}
#-------------------------------------------------------------------
=head2 get ( [ property ] )
Returns a duplicated hash reference of this objectÕs data. See update() for details.
=head3 property
Any field returns the value of a field rather than the hash reference.
=cut
sub get {
my ($self, $name) = @_;
if (defined $name) {
return $properties{id $self}{$name};
}
my %copyOfHashRef = %{$properties{id $self}};
return \%copyOfHashRef;
}
#-------------------------------------------------------------------
=head2 getId ()
Returns the unique id of this item.
=cut
sub getId {
my $self = shift;
return $self->get("vendorId");
}
#-------------------------------------------------------------------
=head2 getVendors ( session, options )
Class method. Returns an array reference of WebGUI::Shop::Vendor objects.
=head3 session
A reference to the current session.
=head3 options
A hash reference of optional flags.
=head4 asHashRef
A boolean indicating that the vendors should be returned as a hash reference of id/names rather than objects.
=cut
sub getVendors {
my ($class, $session, $options) = @_;
my $vendorList = $session->db->buildHashRef("select vendorId,name from vendor order by name");
if ($options->{asHashRef}) {
return $vendorList;
}
my @vendors = ();
foreach my $id (keys %{$vendorList}) {
push @vendors, $class->new($session, $id);
}
return \@vendors;
}
#-------------------------------------------------------------------
=head2 new ( session, vendorId )
Constructor.
=head3 session
A reference to the current session.
=head3 vendorId
A unique id for a vendor.
=cut
sub new {
my ($class, $session, $vendorId) = @_;
unless (defined $session && $session->isa("WebGUI::Session")) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
}
unless (defined $vendorId) {
WebGUI::Error::InvalidParam->throw( param=>$vendorId, error=>"Need a vendorId.");
}
my $vendor = $session->db->quickHashRef("select * from vendor where vendorId=?",[$vendorId]);
if ($vendor->{vendorId} eq "") {
WebGUI::Error::ObjectNotFound->throw(error=>"Vendor not found.", id=>$vendorId);
}
my $self = register $class;
my $id = id $self;
$session{ $id } = $session;
$properties{ $id } = $vendor;
return $self;
}
#-------------------------------------------------------------------
=head2 session ()
Returns a reference to the current session.
=cut
#-------------------------------------------------------------------
=head2 update ( properties )
Sets properties of the vendor
=head3 properties
A hash reference that contains one of the following:
=head4 name
The name of the vendor.
=cut
sub update {
my ($self, $newProperties) = @_;
my $id = id $self;
my @fields = (qw(name));
foreach my $field (@fields) {
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};
}
$self->session->db->setRow("vendor","vendorId",$properties{$id});
}
#-------------------------------------------------------------------
=head2 www_delete ( )
Deletes a vendor.
=cut
sub www_delete {
my ($class, $session) = @_;
my $admin = WebGUI::Shop::Admin->new($session);
return $session->privilege->adminOnly() unless ($admin->canManage);
my $self = $class->new($session, $session->form->get("vendorId"));
if (defined $self) {
$self->delete;
}
return $class->www_manage($session);
}
#-------------------------------------------------------------------
=head2 www_edit ( )
Displays an edit form for a vendor.
=cut
sub www_edit {
my ($class, $session) = @_;
my $admin = WebGUI::Shop::Admin->new($session);
return $session->privilege->adminOnly() unless ($admin->canManage);
# get properties
my $self = eval{$class->new($session, $session->form->get("vendorId"))};
my $properties = {};
if (!WebGUI::Error->caught && defined $self) {
$properties = $self->get;
}
# draw form
my $i18n = WebGUI::International->new($session, "Shop");
my $f = WebGUI::HTMLForm->new($session);
$f->hidden(name=>'shop',value=>'vendor');
$f->hidden(name=>'method',value=>'editSave');
$f->hidden(name=>'vendorId',value=>$properties->{vendorId});
$f->readOnly(label=>$i18n->get('date created'),value=>$properties->{dateCreated});
$f->text(name=>'name', label=>$i18n->get('name'),defaultValue=>$properties->{name});
$f->submit();
# Wrap in admin console
my $console = $admin->getAdminConsole;
return $console->render($f->print, $i18n->get("vendors"));
}
#-------------------------------------------------------------------
=head2 www_editSave ( )
Saves the results of www_edit()
=cut
sub www_editSave {
my ($class, $session) = @_;
my $admin = WebGUI::Shop::Admin->new($session);
return $session->privilege->adminOnly() unless ($admin->canManage);
my $form = $session->form;
my $properties = {
name => $form->get("name","text"),
};
my $self = eval{$class->new($session, $form->get("vendorId"))};
if (!WebGUI::Error->caught && defined $self) {
$self->update($properties);
}
else {
$class->create($session, $properties);
}
return $class->www_manage($session);
}
#-------------------------------------------------------------------
=head2 www_manage ( )
Displays the list of vendors.
=cut
sub www_manage {
my ($class, $session) = @_;
my $admin = WebGUI::Shop::Admin->new($session);
my $i18n = WebGUI::International->new($session, "Shop");
return $session->privilege->adminOnly() unless ($admin->canManage);
# Button for adding a vendor
my $output = WebGUI::Form::formHeader($session)
.WebGUI::Form::hidden($session, { name => "shop", value => "vendor" })
.WebGUI::Form::hidden($session, { name => "method", value => "edit" })
.WebGUI::Form::submit($session, { value => $i18n->get("add a vendor") })
.WebGUI::Form::formFooter($session);
# Add a row with edit/delete buttons for each
foreach my $vendor (@{$class->getVendors($session)}) {
$output .= '<div style="clear: both;">'
# Delete button
.WebGUI::Form::formHeader($session, {extras=>'style="float: left;"' })
.WebGUI::Form::hidden($session, { name => "shop", value => "vendor" })
.WebGUI::Form::hidden($session, { name => "method", value => "delete" })
.WebGUI::Form::hidden($session, { name => "vendorId", value => $vendor->getId })
.WebGUI::Form::submit($session, { value => $i18n->get("delete"), extras => 'class="backwardButton"' })
.WebGUI::Form::formFooter($session)
# Edit button
.WebGUI::Form::formHeader($session, {extras=>'style="float: left;"' })
.WebGUI::Form::hidden($session, { name => "shop", value => "vendor" })
.WebGUI::Form::hidden($session, { name => "method", value => "edit" })
.WebGUI::Form::hidden($session, { name => "vendorId", value => $vendor->getId })
.WebGUI::Form::submit($session, { value => $i18n->get("edit"), extras => 'class="normalButton"' })
.WebGUI::Form::formFooter($session)
# Append name
.' '. $vendor->get("name")
.'</div>';
}
# Wrap in admin console
my $console = $admin->getAdminConsole;
return $console->render($output, $i18n->get("vendors"));
}
1;

View file

@ -33,16 +33,16 @@ our $I18N = {
context => q|help for sku field|
},
'sales agent' => {
message => q|sales agent|,
'vendor' => {
message => q|Vendor|,
lastUpdated => 0,
context => q|asset field relating to who is selling this product|
},
'sales agent help' => {
'vendor help' => {
message => q|Which person/company defined in the commerce system should get credit for selling this item, if any?|,
lastUpdated => 0,
context => q|help for sales agent field|
context => q|help for vendor field|
},
'override tax rate' => {

View file

@ -39,6 +39,12 @@ our $I18N = {
context => q|field label|
},
'vendors' => {
message => q|Vendors|,
lastUpdated => 0,
context => q|admin function label|
},
'date' => {
message => q|Date|,
lastUpdated => 0,
@ -201,12 +207,24 @@ our $I18N = {
context => q|a label in the address editor|
},
'date created' => {
message => q|Date Created|,
lastUpdated => 0,
context => q|a label in the vendor manager|
},
'add a new address' => {
message => q|Add A New Address|,
lastUpdated => 0,
context => q|a button in the address book|
},
'add a vendor' => {
message => q|Add A Vendor|,
lastUpdated => 0,
context => q|a button in the vendor manager|
},
'delete' => {
message => q|Delete|,
lastUpdated => 0,