Example #1
0
 public function buy(Request $request, $store)
 {
     $stores = config('omnomcom.stores');
     if (array_key_exists($store, $stores)) {
         $storedata = $stores[$store];
         if (!in_array($request->ip(), $storedata->addresses) && !Auth::user()->can($storedata->roles)) {
             return "<span style='color: red;'>You are not authorized to do this.</span>";
         }
     } else {
         return "<span style='color: red;'>This store doesn't exist.</span>";
     }
     switch ($request->input('credentialtype')) {
         case 'account':
             $credentials = $request->input('credentials');
             $user = AuthController::verifyCredentials($credentials['username'], $credentials['password']);
             if (!$user) {
                 return "<span style='color: red;'>Invalid credentials.</span>";
             }
             break;
         case 'card':
             $card = RfidCard::where('card_id', $request->input('credentials'))->first();
             if (!$card) {
                 return "<span style='color: red;'>Unknown card.</span>";
             }
             $card->touch();
             $user = $card->user;
             if (!$user) {
                 return "<span style='color: red;'>Unknown user.</span>";
             }
             break;
         default:
             return "<span style='color: red;'>Invalid credential type.</span>";
             break;
     }
     if (!$user->member) {
         return "<span style='color: red;'>Only members can use the OmNomCom.</span>";
     }
     $withCash = $request->input('cash');
     if ($withCash == "true" && !$storedata->cash_allowed) {
         return "<span style='color: red;'>You cannot use cash in this store.</span>";
     }
     $cart = $request->input('cart');
     foreach ($cart as $id => $amount) {
         if ($amount > 0) {
             $product = Product::find($id);
             if (!$product) {
                 return "<span style='color: red;'>You tried to buy a product that didn't exist!</span>";
             }
             if (!$product->isVisible()) {
                 return "<span style='color: red;'>You tried to buy a product that is not available!</span>";
             }
             if ($product->stock < $amount) {
                 return "<span style='color: red;'>You tried to buy more of a product than was in stock!</span>";
             }
             if ($product->is_alcoholic && $user->age() < 18) {
                 return "<span style='color: red;'>You tried to buy alcohol, youngster!</span>";
             }
         }
     }
     foreach ($cart as $id => $amount) {
         if ($amount > 0) {
             $product = Product::find($id);
             $orderline = OrderLine::create(['user_id' => $withCash == "true" ? null : $user->id, 'cashier_id' => $withCash == "true" ? $user->id : null, 'product_id' => $product->id, 'original_unit_price' => $product->price, 'units' => $amount, 'total_price' => $amount * $product->price, 'payed_with_cash' => $withCash == "true" ? date('Y-m-d H:i:s') : null]);
             $orderline->save();
             $product->stock -= $amount;
             $product->save();
         }
     }
     return "OK";
 }
Example #2
0
 public function bulkUpdate(Request $request)
 {
     $input = preg_split('/\\r\\n|\\r|\\n/', $request->input('update'));
     $feedback = "";
     foreach ($input as $line) {
         $line = explode(',', $line);
         $product = Product::find($line[0]);
         if ($product) {
             $oldstock = $product->stock;
             $newstock = $oldstock + $line[1];
             $product->stock = $newstock;
             $feedback .= "<strong>" . $product->name . "</strong> updated with delta <strong>" . $line[1] . "</strong>. Stock changed from {$oldstock} to <strong>{$newstock}</strong>.<br>";
             $product->save();
         } else {
             $feedback .= "Product ID <strong>" . $line[0] . "</strong> not recognized.<br>";
         }
     }
     $request->session()->flash('flash_message', $feedback);
     return Redirect::back();
 }