From c61890392cfa47bb85eeeacbb6019b913c8f6df1 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Mon, 17 Mar 2008 17:33:54 +0000 Subject: [PATCH] added basic quantity checking for inventory management, and a dateAdded field to the cart in case we need to monitor how long items have been in the cart --- docs/upgrades/upgrade_7.5.2-7.5.3.pl | 5 ++- lib/WebGUI/Asset/Sku.pm | 64 +++++++++++++++++++++++++++- lib/WebGUI/Shop/Cart.pm | 2 +- lib/WebGUI/Shop/CartItem.pm | 13 +++--- 4 files changed, 74 insertions(+), 10 deletions(-) diff --git a/docs/upgrades/upgrade_7.5.2-7.5.3.pl b/docs/upgrades/upgrade_7.5.2-7.5.3.pl index 9e31858fa..ffd445339 100644 --- a/docs/upgrades/upgrade_7.5.2-7.5.3.pl +++ b/docs/upgrades/upgrade_7.5.2-7.5.3.pl @@ -205,15 +205,16 @@ sub migrateToNewCart { couponId varchar(22) binary, index sessionId (sessionId) )"); - $session->db->write("create table cartItems ( + $session->db->write("create table cartItem ( itemId varchar(22) binary not null primary key, cartId varchar(22) binary not null, assetId varchar(22) binary not null, + dateAdded datetime not null, options mediumtext, configuredTitle varchar(255), shippingAddressId varchar(22) binary, quantity integer not null default 1, - index cartId_assetId (cartId,assetId) + index cartId_assetId_dateAdded (cartId,assetId,dateAdded) )"); $session->db->write("drop table shoppingCart"); $session->setting->add('shopCartTemplateId','aIpCmr9Hi__vgdZnDTz1jw'); diff --git a/lib/WebGUI/Asset/Sku.pm b/lib/WebGUI/Asset/Sku.pm index bdb9bffa8..7622cf389 100644 --- a/lib/WebGUI/Asset/Sku.pm +++ b/lib/WebGUI/Asset/Sku.pm @@ -72,6 +72,23 @@ sub addToCart { #------------------------------------------------------------------- +=head2 adjustQuantityAvailable ( amount ) + +Adjust the quantity of this product that is available. Send a negative number to decrease, or a positive number to increase. Returns getQuantityAvailable. + +=head3 amount + +A signed integer that represents the amount to adjust + +=cut + +sub adjustQuantityAvailable { + my ($self, $amount) = @_; + return $self->setQuantityAvailable($self->getQuantityAvailable + $amount); +} + +#------------------------------------------------------------------- + =head2 applyOptions ( options ) Accepts a configuration data hash reference that configures a sku a certain way. For example to turn "a t-shirt" into "an XL red t-shirt". See also getOptions(). @@ -206,12 +223,13 @@ sub getOptions { =head2 getMaxAllowedInCart ( ) -Returns 99999999. Should be overriden by subclasses that have a specific value. Subclasses that are unique should return 1. Subclasses that have an inventory count should return the amount in inventory. +Returns getQuantityAvailable(). Should be overriden by subclasses that have a specific value. Subclasses that are unique should return 1. Subclasses that have an inventory count should return the amount in inventory. =cut sub getMaxAllowedInCart { - return 99999999; + my $self = shift; + return $self->getQuantityAvailable; } #------------------------------------------------------------------- @@ -228,6 +246,18 @@ sub getPrice { #------------------------------------------------------------------- +=head2 getQuantityAvailable ( ) + +Returns 99999999. Needs to be overriden by subclasses. Tells the commerce system how many of this item is on hand. + +=cut + +sub getQuantityAvailable { + return 99999999; +} + +#------------------------------------------------------------------- + =head2 getTaxRate ( ) Returns undef unless the "Override tax rate?" switch is set to yes. If it is, then it returns the value of the "Tax Rate Override" field. @@ -267,6 +297,19 @@ sub indexContent { } +#------------------------------------------------------------------- + +=head2 isRecurring + +Returns a boolean indicating whether this sku is recurring. Defaultly returns 0. Needs to be overriden by subclasses that do recurring transactions, because not all payment gateways can process recurring transactions. + +=cut + +sub isRecurring { + return 0; +} + + #------------------------------------------------------------------- =head2 isShippingRequired @@ -323,6 +366,23 @@ sub processStyle { #------------------------------------------------------------------- +=head2 setQuantityAvailable ( amount ) + +Set the quantity of this product that is available. Returns getQuantityAvailable(). Should be overridden by skus that keep track of quantity. + +=head3 amount + +A signed integer that represents the quantity to set it to. + +=cut + +sub setQuantityAvailable { + my ($self, $amount) = @_; + return $self->getQuantityAvailable; +} + +#------------------------------------------------------------------- + =head2 www_view ( ) Renders self->view based upon current style, subject to timeouts. Returns Privilege::noAccess() if canView is False. diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index ffadd3f67..a653b5ffd 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -238,7 +238,7 @@ Returns an array reference of WebGUI::Asset::Sku objects that are in the cart. sub getItems { my ($self) = @_; my @itemsObjects = (); - my $items = $self->session->db->read("select itemId from cartItems where cartId=?",[$self->getId]); + my $items = $self->session->db->read("select itemId from cartItem where cartId=?",[$self->getId]); while (my ($itemId) = $items->array) { push(@itemsObjects, $self->getItem($itemId)); } diff --git a/lib/WebGUI/Shop/CartItem.pm b/lib/WebGUI/Shop/CartItem.pm index 66262eaf5..4bf10fd6b 100644 --- a/lib/WebGUI/Shop/CartItem.pm +++ b/lib/WebGUI/Shop/CartItem.pm @@ -62,9 +62,10 @@ sub create { WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Asset::Sku", got=>(ref $sku), error=>"Need a SKU item."); } my $itemId = $cart->session->id->generate; - $cart->session->db->write('insert into cartItems (quantity, cartId, assetId, itemId) values (1,?,?,?)', [$cart->getId, $sku->getId, $itemId]); + $cart->session->db->write('insert into cartItem (quantity, cartId, assetId, itemId, dateAdded) values (1,?,?,?,now())', [$cart->getId, $sku->getId, $itemId]); my $self = $class->new($cart, $itemId); $self->update({asset=>$sku}); + $sku->adjustQuantityAvailable(-1); return $self; } @@ -185,7 +186,7 @@ sub new { unless (defined $itemId) { WebGUI::Error::InvalidParam->throw(error=>"Need an itemId."); } - my $item = $cart->session->db->quickHashRef('select * from cartItems where itemId=?', [$itemId]); + my $item = $cart->session->db->quickHashRef('select * from cartItem where itemId=?', [$itemId]); if ($item->{itemId} eq "") { WebGUI::Error::ObjectNotFound->throw(error=>"Item not found.", id=>$itemId); } @@ -209,7 +210,7 @@ Removes this item from the cart. sub remove { my $self = shift; - $self->cart->session->db->deleteRow("cartItems","itemId",$self->getId); + $self->cart->session->db->deleteRow("cartItem","itemId",$self->getId); undef $self; return undef; } @@ -230,6 +231,7 @@ The number to set the quantity to. Zero or less will remove the item from cart. sub setQuantity { my ($self, $quantity) = @_; my $id = id $self; + my $currentQuantity = $self->get("quantity"); if ($quantity > $self->getSku->getMaxAllowedInCart) { WebGUI::Error::Shop::MaxOfItemInCartReached->throw(error=>"Cannot have that many of this item in cart."); } @@ -237,7 +239,8 @@ sub setQuantity { return $self->remove; } $properties{$id}{quantity} = $quantity; - $self->cart->session->db->setRow("cartItems","itemId", $properties{$id}); + $self->getSku->adjustQuantityAvailable($currentQuantity + $quantity); + $self->cart->session->db->setRow("cartItem","itemId", $properties{$id}); } #------------------------------------------------------------------- @@ -286,7 +289,7 @@ sub update { if (exists $newProperties->{options} && ref($newProperties->{options}) eq "HASH") { $properties{$id}{options} = JSON::to_json($newProperties->{options}); } - $self->cart->session->db->setRow("cartItems","itemId",$properties{$id}); + $self->cart->session->db->setRow("cartItem","itemId",$properties{$id}); }