ActiveWAFL Crash Course

The WAFL Cones Ice Cream Shop, Version 1.0

6.1 ProcessSale API Handler

Wafl Cones uses a single API call, ProcessSale, to process new sale submissions.
Not only do you need to create the API Handler, but you also need to add the API's definition.

Define the API call

Edit the file Config/More/ApiCalls.syrp and replace it's contents with the following syrup.

Config/More/ApiCalls.syrp: API definitions file.
  1. ProcessSale
  2.     ApiCall         =   ProcessSale
  3.     HandlerFunction =   ProcessSale
  4.     HandlerFile     =   General.php
  5.     HandlerClass    =   \\WaflCones\\Api\\General

Create the API Handler

Create the file WaflCones/Api/General.php and replace it's contents with the following code.

WaflCones/Api/General.php: General API with one handler for processing new sale submissions.
  1. <?php
  2.  
  3. namespace WaflCones\Api;
  4.  
  5. use Exception,
  6.     Wafl\Core,
  7.     WaflCones\FunctionalModel\Flavor,
  8.     WaflCones\FunctionalModel\Sale,
  9.     WaflCones\FunctionalModel\SaleItem;
  10.  
  11. class General
  12. {
  13.     public static function ProcessSale($args, $headers, $files)
  14.     {
  15.         if (Core::$CURRENT_USER->Get_UserId())
  16.         {
  17.             $itemFlavors    = $args["Flavor"];
  18.             $itemWeights    = $args["Ounces"];
  19.             $amountTendered = $args["AmountTendered"];
  20.  
  21.             $allFlavors = Flavor::Filter();
  22.             $totalPrice = 0;
  23.             foreach ($itemFlavors as $itemNumber=>$itemFlavorId)
  24.             {
  25.                 $flavor = $allFlavors[$itemFlavorId];
  26.                 $totalPrice += $flavor->Get_PricePerOunce() * $itemWeights[$itemNumber];
  27.             }
  28.  
  29.             //record the sale
  30.             $sale = new Sale();
  31.             $sale->Set_EmployeeId(Core::$CURRENT_USER->Get_UserId());
  32.             $sale->Set_Price($totalPrice);
  33.             $sale->Set_SaleDate(time());
  34.             $sale->Save();
  35.  
  36.             foreach ($itemFlavors as $itemNumber=>$itemFlavorId)
  37.             {
  38.                 //reduce the stock for the sold flavor
  39.                 $flavor = $allFlavors[$itemFlavorId];
  40.                 $flavor->Set_OuncesInStock($flavor->Get_OuncesInStock()-$itemWeights[$itemNumber]);
  41.                 $flavor->Save();
  42.  
  43.  
  44.                 //record the sale item
  45.                 $saleItem = new SaleItem();
  46.                 $saleItem->Set_SaleId($sale->Get_SaleId());
  47.                 $saleItem->Set_FlavorId($itemFlavorId);
  48.                 $saleItem->Set_Price($flavor->Get_PricePerOunce() * $itemWeights[$itemNumber]);
  49.                 $saleItem->Set_Ounces($itemWeights[$itemNumber]);
  50.                 $saleItem->Save();
  51.             }
  52.  
  53.             $changeDue = $amountTendered - $totalPrice;
  54.  
  55.             return ["Sale" => $sale, "ChangeDue" => $changeDue];
  56.         } else {
  57.             throw new Exception("You must be logged in to use this api");
  58.         }
  59.     }
  60. }

Your API should be all set.

Great job!
You have now performed every basic task involved in developing an MVC Web Application with ActiveWAFL.
You're almost finished. You just need to setup the remaining eight pages.
The process will be roughly the same as what you did for the first three pages.

Next: Create (or simply download) the presentation and logic for the remaining pages.