From 273ce97cf3ecdf92a899d81473c1d4875fc5da3a Mon Sep 17 00:00:00 2001 From: Konrad Rosenbaum Date: Sun, 1 Jan 2017 23:28:41 +0100 Subject: [PATCH] implement coupon on web cart Change-Id: Icf98fa691b7883e1cc28b14cdf82c1368c055d5b --- wob/db/cart.wolf | 2 +- wob/transact/cart.wolf | 12 ++++++++- www/inc/classes/basevars.php | 2 + www/inc/classes/eventplan.php | 50 ++++++++++++++++++++++++++++++++++- www/inc/rendering/cart_listing.php | 32 +++++++++++++++++++++- www/inc/wext/webcart.php | 25 ++++++++++++++++++ www/index.php | 4 +++ www/template/en/cart.html | 10 +++++++ 8 files changed, 131 insertions(+), 6 deletions(-) diff --git a/wob/db/cart.wolf b/wob/db/cart.wolf index dc36358..6f04dce 100644 --- a/wob/db/cart.wolf +++ b/wob/db/cart.wolf @@ -38,7 +38,7 @@ the event the ticket is for the price category of the ticket - the price category of the ticket + the price category of the ticket amount of tickets of this type in the cart diff --git a/wob/transact/cart.wolf b/wob/transact/cart.wolf index 6e394b9..f201699 100644 --- a/wob/transact/cart.wolf +++ b/wob/transact/cart.wolf @@ -47,4 +47,14 @@ - \ No newline at end of file + + + Called from Web UI only: adds a coupon to the cart + + The cart to add into + The coupon to add + + + + + diff --git a/www/inc/classes/basevars.php b/www/inc/classes/basevars.php index 385926c..4abed0d 100644 --- a/www/inc/classes/basevars.php +++ b/www/inc/classes/basevars.php @@ -111,6 +111,7 @@ public static function initPriv(){ $basevars['script']['changeDeliveryAddress']=$BaseUrl."?mode=changeDeliveryAddress"; $basevars['script']['customerResetLogin']=$BaseUrl."?mode=customerResetLogin"; $basevars['script']['setlanguage']=$BaseUrl."?mode=setlanguage&lang="; + $basevars['script']['addcoupon']=$BaseUrl."?mode=addcoupon"; // form elements $basevars['inputnames']['amountTickets']="amountTickets"; $basevars['inputnames']['event']="event"; @@ -132,6 +133,7 @@ public static function initPriv(){ $basevars['inputnames']['address']['zip']='addr_zip'; $basevars['inputnames']['address']['state']='addr_state'; $basevars['inputnames']['address']['country']='addr_country'; + $basevars['inputnames']['coupon']='coupon'; //end of basevars } diff --git a/www/inc/classes/eventplan.php b/www/inc/classes/eventplan.php index 31d59bc..7c0c5d4 100644 --- a/www/inc/classes/eventplan.php +++ b/www/inc/classes/eventplan.php @@ -71,13 +71,13 @@ class EventPlan ///checks whether the event has already started at the given timestamp public function eventHasStarted($timestamp) { - return $timestamp>=$this->event->getstarttime(); + return $timestamp>=$this->event->getstart(); } ///checks whether the event has already ended at the given timestamp public function eventHasEnded($timestamp) { - return $timestamp>=$this->event->getendtime(); + return $timestamp>=$this->event->getend(); } ///adds the ticket to the list of new tickets and validates whether the sale is possible; @@ -183,6 +183,7 @@ class CouponVerifier private $couponid=null; private $coupon=null; + ///create a new verifier object public function __construct($couponid) { $this->couponid=$couponid; @@ -190,6 +191,7 @@ class CouponVerifier $this->coupon=WOCoupon::fromTablecoupon(WTcoupon::getFromDB($couponid)); } + ///checks that the coupon can be used public function isUseableOrNull() { if($this->coupon===null)return true; @@ -204,6 +206,7 @@ class CouponVerifier return true; } + ///verify the contents of the event plan public function verify($eventplan) { if(!$this->isUseableOrNull())return false; @@ -234,6 +237,7 @@ class CouponVerifier return $vret; } + ///verification if the coupon is NULL private function verifyNull($eventplan) { $vret=true; @@ -248,6 +252,48 @@ class CouponVerifier } return $vret; } + + ///applies the coupon to the WebCart + //TODO: check limits, check time + static public function applyToCart($cartid) + { + //get cart + $dcart=WTcart::getFromDB($cartid); + if(!is_a($dcart,"WTcart"))return; + //get coupon + $coupon=WOCoupon::fromTablecoupon(WTcoupon::getFromDB($dcart->couponid)); + if(!is_a($coupon,"WOCoupon"))return; + //collect + global $db; + $tlines=WTcartticket::selectFromDB("cartid=".$db->escapeString($cartid)); + foreach($tlines as $tick){ + //get event + $event=WOEvent::fromTableevent(WTevent::getFromDB($tick->eventid)); + //get price ID + if($db->isNull($tick->origpricecategoryid)) + $pid=$tick->pricecategoryid; + else + $pid=$tick->origpricecategoryid; + //check for rule + $npid=$pid; + foreach($coupon->getrules() as $rule){ + if($rule->getfromcategory()==$pid){ + $n=$rule->gettocategory(); + foreach($event->getprice() as $prc){ + if($prc->getpricecategoryid()==$n){ + $npid=$n; + break; + } + } + if($npid!=$pid)break; + } + } + //TODO: this may fail if the user adds more tickets later + $tick->pricecategoryid=$npid; + $tick->origpricecategoryid=$pid; + $tick->update(); + } + } }; diff --git a/www/inc/rendering/cart_listing.php b/www/inc/rendering/cart_listing.php index e2126f3..09fce8a 100644 --- a/www/inc/rendering/cart_listing.php +++ b/www/inc/rendering/cart_listing.php @@ -66,6 +66,31 @@ static public function addVoucher() redirectHome(array("mode"=>"cart","cartid"=>$cartid)); } +///called from index.php - adds a coupon to the cart +static public function addCoupon() +{ + global $HTTPARGS,$basevars; + //get the cart + $cartid=self::getOrCreateCart(); + //find event +// echo "checking"; + $vi=$basevars['inputnames']['coupon']; + if(!isset($HTTPARGS[$vi])){ + $basevars['invalidcoupon']='none'; + return; + } + //price + $cpid=strtoupper(trim($HTTPARGS[$vi])); + //add +// echo "adding"; + try{ + WTrWebCartAddCoupon::execute($cartid,$cpid); + }catch(WobTransactionError $e){ + $basevars['invalidcoupon']=$cpid; + } + //rendering is called after this... +} + /**called from index.php - removes tickets/vouchers from cart*/ static public function removeItem() { @@ -236,6 +261,9 @@ static public function createCartOverview() $p=$twig->loadTemplate("carterror.html"); return $p->render($basevars); } + + //apply coupon + CouponVerifier::applyToCart($cartid); //cart is ok, now get the object $cart = WOWebCart::fromTablecart(WTcart::getFromDB($cartid)); @@ -416,8 +444,8 @@ static public function placeOrder() $p=$twig->loadTemplate("carterror.html"); return $p->render($basevars); } - //push it to order - $trans=WTrCreateOrder::execute($cart,false); + //push it to order; TODO: add voucher feature (arg #3) + $trans=WTrCreateOrder::execute($cart,false,array()); //delete cart if($trans->resultorder()!==false){ $where="cartid = ".$db->escapeString($cartid); diff --git a/www/inc/wext/webcart.php b/www/inc/wext/webcart.php index 8418fcc..f2d1499 100644 --- a/www/inc/wext/webcart.php +++ b/www/inc/wext/webcart.php @@ -74,12 +74,37 @@ class WOWebCart extends WOWebCartAbstract // die ("hallo"); } + ///transaction to add coupon to cart + static public function addCoupon($trans) + { + $cartid=$trans->getcartid(); + $cpid=$trans->getcouponid(); + //get cart + $dcart=WTcart::getFromDB($cartid); + $cart=WOWebCart::fromTablecart($dcart); + if(!is_a($cart,"WOWebCart")){ + $trans->abortWithError("no cart"); + return; + } + //find coupon + $dcoup=WTcoupon::getFromDB($cpid); + if(!is_a($dcoup,"WTcoupon")){ + $trans->abortWithError("no coupon"); + return; + } + //TODO: check validity + //update + $dcart->couponid=$cpid; + $dcart->update(); + } + /**transaction to remove tickets from cart*/ static public function removeTickets($trans) { $cartid=$trans->getcartid(); $evid=$trans->geteventid(); $pcid=$trans->getpricecategoryid(); + error_log("hallo ".$cartid." ".$evid." ".$pcid); //get cart $cart=WOWebCart::fromTablecart(WTcart::getFromDB($cartid)); if(!is_a($cart,"WOWebCart"))return; diff --git a/www/index.php b/www/index.php index f903fcc..2cca618 100644 --- a/www/index.php +++ b/www/index.php @@ -63,6 +63,10 @@ try{ case "cart": // show current cart (can be called from anywhere in order process) $page=WebCart::createCartOverview(); break; + case "addcoupon"://add a coupon to the cart + WebCart::addCoupon(); + $page=WebCart::createCartOverview(); + break; case "removeItem": // remove something from cart WebCart::removeItem(); break; diff --git a/www/template/en/cart.html b/www/template/en/cart.html index e987007..5ebe97f 100644 --- a/www/template/en/cart.html +++ b/www/template/en/cart.html @@ -61,6 +61,16 @@ {# end of the table #} + + {% if invalidcoupon is defined %} +

The coupon could not be added to your cart. + {% endif %} + {% if cart.couponid is null %} +

+ Coupon: + {% else %} +

Coupon: {{cart.couponid}} + {% endif %} {# actions the user may want to take #}

Checkout

-- 1.7.2.5