implement coupon on web cart
authorKonrad Rosenbaum <konrad@silmor.de>
Sun, 1 Jan 2017 22:28:41 +0000 (23:28 +0100)
committerKonrad Rosenbaum <konrad@silmor.de>
Sun, 1 Jan 2017 22:28:41 +0000 (23:28 +0100)
Change-Id: Icf98fa691b7883e1cc28b14cdf82c1368c055d5b

wob/db/cart.wolf
wob/transact/cart.wolf
www/inc/classes/basevars.php
www/inc/classes/eventplan.php
www/inc/rendering/cart_listing.php
www/inc/wext/webcart.php
www/index.php
www/template/en/cart.html

index dc36358..6f04dce 100644 (file)
@@ -38,7 +38,7 @@
                <!--tickets in the cart-->
                <Column name="eventid" type="int32" notnull="yes" foreignkey="event:eventid" primarykey="yes">the event the ticket is for</Column>
                <Column name="pricecategoryid" type="int32" foreignkey="pricecategory:pricecategoryid" primarykey="yes">the price category of the ticket</Column>
-               <Column name="origpricecategoryid" type="int32" foreignkey="pricecategory:pricecategoryid" primarykey="yes">the price category of the ticket</Column>
+               <Column name="origpricecategoryid" type="int32" foreignkey="pricecategory:pricecategoryid">the price category of the ticket</Column>
                <Column name="amount" type="int32" notnull="yes">amount of tickets of this type in the cart</Column>
                <Column name="seating" type="string:255" null="yes" version="01.05"/>
        </Table>
index 6e394b9..f201699 100644 (file)
                <Call lang="php" method="WOWebCart::removeVoucher($this);"/>
                <Output/>
        </Transaction>
-</Wolf>
\ No newline at end of file
+
+       <Transaction name="WebCartAddCoupon">
+               <Doc>Called from Web UI only: adds a coupon to the cart</Doc>
+               <Input>
+                       <Var name="cartid" type="astring">The cart to add into</Var>
+                       <Var name="couponid" type="astring">The coupon to add</Var>
+               </Input>
+               <Call lang="php" method="WOWebCart::addCoupon($this);"/>
+               <Output/>
+       </Transaction>
+</Wolf>
index 385926c..4abed0d 100644 (file)
@@ -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
 }
 
index 31d59bc..7c0c5d4 100644 (file)
@@ -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();
+               }
+       }
 };
 
 
index e2126f3..09fce8a 100644 (file)
@@ -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);
index 8418fcc..f2d1499 100644 (file)
@@ -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;
index f903fcc..2cca618 100644 (file)
@@ -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;
index e987007..5ebe97f 100644 (file)
   
   {# end of the table #}
   </table>
+
+  {% if invalidcoupon is defined %}
+  <p>The coupon could not be added to your cart.
+  {% endif %}
+  {% if cart.couponid is null %}
+  <p><form action="{{script.addcoupon}}" method="get"><input type="hidden" name="mode" value="addcoupon"></input>
+  Coupon: <input type="text" name="{{inputnames.coupon}}"></input><input type="submit" value="Add Coupon"></input>
+  {% else %}
+  <p>Coupon: {{cart.couponid}}
+  {% endif %}
   
   {# actions the user may want to take #}
   <p><a href="{{script.orderLogin}}">Checkout</a></p>