Example #1
0
 public function sendReceipt(Cart $cart)
 {
     $mg = new Mailgun(self::API_KEY);
     $domain = "mg.fullprintcamping.com";
     $user = $cart->user()->first();
     $response = $mg->sendMessage($domain, array('from' => '*****@*****.**', 'to' => $user->email, 'subject' => "Your order at FullPrintCamping.com", 'text' => "fullprintcamping.com/order-details/" . $cart->id));
     var_dump($response);
 }
 /**
  * @param $cart
  * @param $product_id
  *
  * @return array
  */
 public function getExistingProductQuantity(Cart $cart, $product_id)
 {
     if (empty($cart)) {
         return 0;
     }
     $data = $cart->products()->get()->where('id', $product_id)->fetch('pivot')->implode('quantity');
     if (empty($data)) {
         return 0;
     }
     return (int) $data;
 }
Example #3
0
 public static function getCurrentCart()
 {
     if (($cart = self::findOne(['customer_id' => \Yii::$app->user->identity->id, 'status' => self::CART_PENDING])) !== null) {
         $cart->cleanUp();
         return $cart;
     }
     $cart = new Cart();
     $cart->customer_id = \Yii::$app->user->identity->id;
     $cart->session_id = session_id();
     $cart->created = date('Y-m-d H:i:s');
     $cart->status = self::CART_PENDING;
     $cart->save();
     return $cart;
 }
Example #4
0
 public function actionProceedToCheckout()
 {
     $model = new CheckoutForm();
     $user = Yii::$app->user->identity;
     $total = 0;
     $session = Yii::$app->session;
     $products = $session['cart'] ?: [];
     foreach ($products as $product) {
         $total += $product['price'] * $product['quantity'];
     }
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         if ($order = $model->proceedToCheckout($total)) {
             $ordersProductsForm = new OrdersProductsForm();
             foreach ($products as $product) {
                 $orderProducts = ['OrdersProductsForm' => ['orderId' => $order->id, 'productId' => $product['id'], 'price' => $product['price'], 'quantity' => $product['quantity']]];
                 if ($ordersProductsForm->load($orderProducts) && $ordersProductsForm->validate()) {
                     if ($ordersProductsForm->saveOrdersProducts()) {
                         Cart::delete($product['id']);
                     }
                 }
             }
             return $this->redirect('/product/index');
         }
     }
     return $this->render('proceedToCheckout', ['model' => $model, 'user' => $user, 'total' => $total]);
 }
 /**
  * Finds the Cart model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Cart the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     $where = ['id' => $id, 'status' => [Cart::CART_SOLD, Cart::CART_REFUNDED]];
     $user = \Yii::$app->user->identity;
     if (!$user->admin) {
         $where['customer_id'] = $user->getId();
     }
     return Cart::findOne($where);
 }
 public function removeFromCart(Request $request, $itemId)
 {
     if ($request->session()->has(self::CART_ID)) {
         $cart = Cart::find($request->session()->get(self::CART_ID));
         $item = $cart->items()->where('id', $itemId)->first();
         $item->delete();
     }
     return $this->success();
 }
Example #7
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     for ($i = 1; $i < 10; $i++) {
         \App\Models\Cart::create(['table_id' => $i, 'status' => 'active']);
     }
     $faker = Faker\Factory::create();
     for ($i = 1; $i < 10; $i++) {
         for ($k = 1; $k < 6; $k++) {
             \App\Models\CartItem::create(['cart_id' => $i, 'product_id' => $k, 'quantity' => $faker->numberBetween(5, 20), 'status' => 'active']);
         }
     }
 }
Example #8
0
 public function getTopProducts(Request $request)
 {
     $input = $request->all();
     $startDate = Carbon::today('Asia/Manila')->subDays(30);
     $endDate = Carbon::today('Asia/Manila');
     if (isset($input['from'])) {
         $startDate = Carbon::createFromTimestamp(strtotime($input['from']));
         $endDate = Carbon::createFromTimestamp(strtotime($input['to']));
     }
     $cartItems = Cart::join('cart_items', 'carts.id', '=', 'cart_items.cart_id')->join('products', 'cart_items.product_id', '=', 'products.id')->select(\DB::raw('COUNT(quantity) as totalQuantity'), 'cart_items.product_id', 'products.name')->whereDate('carts.created_at', '>=', $startDate->toDateString())->whereDate('carts.created_at', '<=', $endDate->toDateString())->orderBy('totalQuantity', 'DESC')->groupBy('cart_items.product_id')->take(5)->get();
     return $this->responseOk($cartItems);
 }
Example #9
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Cart::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'product_id' => $this->product_id, 'number' => $this->number, 'price' => $this->price, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'session_id', $this->session_id])->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
Example #10
0
 public function thankYou(Request $request)
 {
     $cartId = $request->input('cart-id');
     $cart = Cart::find($cartId);
     $token = $request->input('token-id');
     if (!$cartId || !$cart || !$token) {
         Log::error("Thank you page accessed with no token-id or invalid cart");
         return redirect('/');
     }
     if ($cart->status != Cart::STATUS_CLOSED) {
         $util = new PaylineUtility();
         $errors = $util->stepThree($token);
         if (!is_null($errors)) {
             return redirect('/my-cart?section=billing&error=' . urlencode($errors));
         }
         $cart->status = Cart::STATUS_CLOSED;
         $cart->save();
         $mailer = new EmailUtil();
         $mailer->sendReceipt($cart);
     }
     return response()->view('thankYou', array('cartId' => $cartId, 'cartItemCount' => 0));
 }
Example #11
0
 public function actionAjaxAdd()
 {
     Yii::$app->response->format = Response::FORMAT_JSON;
     $request = Yii::$app->request;
     $id = $request->get('id');
     $color = $request->get('color');
     $size = $request->get('size');
     $number = $request->get('num');
     $model = Product::findOne($id);
     if (!Yii::$app->session->isActive) {
         Yii::$app->session->open();
     }
     $cart = new Cart();
     $cart->session_id = Yii::$app->session->id;
     $cart->user_id = Yii::$app->user->isGuest ? 0 : Yii::$app->user->id;
     $cart->product_id = $id;
     $cart->name = $model->name;
     $cart->color = $color;
     $cart->size = $size;
     $cart->number = $number;
     $cart->price = $model->price;
     if ($cart->save()) {
         return ['status' => 1, 'productId' => $id, 'size' => $size, 'color' => $color];
     } else {
         return ['status' => -2, 'productId' => $id, 'size' => $size, 'color' => $color];
     }
 }
Example #12
0
 public function actionLoadCart()
 {
     $return = [];
     $total = 0;
     if (isset(Yii::$app->user->id)) {
         $cart = Cart::findOne(['user_id' => Yii::$app->user->id, 'status' => 'CREATED']);
         if ($cart) {
             foreach ($cart->productscarts as $k => $productCart) {
                 $aux = [];
                 $aux['name'] = $productCart->product->name;
                 $aux['value'] = $productCart->quantity;
                 $aux['won'] = $productCart->won;
                 $return[$productCart->product->id] = $aux;
                 $total += $productCart->product->price * $productCart->quantity;
             }
             $return['total'] = $total;
         }
     } else {
         $cart = Yii::$app->session['cart'];
         if ($cart) {
             foreach ($cart as $k => $ca) {
                 $aux = [];
                 $product = Product::findOne($k);
                 $aux['name'] = $product->name;
                 $aux['value'] = $ca;
                 $aux['won'] = 'NO';
                 $return[$product->id] = $aux;
                 $total += $product->price * $ca;
             }
             $return['total'] = $total;
         }
     }
     echo json_encode($return);
 }
Example #13
0
 /**
  * Create the cart
  *
  * @param $tableId
  * @return static
  */
 private function createCart($data)
 {
     return Cart::create($data);
 }
Example #14
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getCarts()
 {
     return $this->hasMany(Cart::className(), ['id' => 'cart_id'])->viaTable('productscart', ['product_id' => 'id']);
 }
 public function actionDelcart($id)
 {
     $cart = new Cart();
     $cart->delFromCart($id);
     return $this->redirect(Yii::$app->request->referrer);
 }
Example #16
0
 /**
  * @return Cart|array|static[]
  */
 public function retrieveCart()
 {
     // get the shopping cart
     $this->cart = $this->cart->whereUserId($this->id)->get(['id']);
     return $this->cart;
 }
Example #17
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getCart()
 {
     return $this->hasOne(Cart::className(), ['id' => 'cart_id']);
 }
    public function actionSave()
    {
        $session = new Session();
        $user = Yii::$app->user->identity;
        $cart = Cart::getCurrentCart();
        $cart->processCart();
        if ($cart->total > 0) {
            return $this->actionCharge();
        }
        $cart->status = Cart::CART_SOLD;
        $cart->save();
        $session->addSuccess(Yii::t('app', 'Congratulations, you\'ve completed your order!'));
        $cart_lines = [];
        foreach ($cart->items as $item) {
            $cart_lines[] = $item->ticket->group->event->name . ': ' . $item->ticket->name . ' x' . $item->quantity . ' @ ' . $item->ticket->ticket_price . ' each';
        }
        $cart_details = implode("\n", $cart_lines);
        $email = new Email();
        $email->to_name = $user->name;
        $email->to_email = $user->email;
        $email->subject = "Your Tixty Purchase";
        $message = <<<EOT
Hi {$user->name}!!

You just bought {$cart->quantity} tickets for a total of {$cart->total} - details below.

Thanks,

Tixty

---
{$cart_details}
EOT;
        $email->body = nl2br($message);
        $email->save();
        $email->send();
        $email = new Email();
        $email->to_name = "Tixty";
        $email->to_email = \Yii::$app->params['adminEmail'];
        $email->subject = "Tixty Purchase #{$cart->id}";
        $message = <<<EOT
{$user->name} just bought {$cart->quantity} tickets for a total of {$cart->total} - details below.

Tixty

---
{$cart_details}
EOT;
        $email->body = nl2br($message);
        $email->save();
        $email->send();
        return $this->redirect('index');
    }
 /**
  * 结算,生成订单,删除购物车
  */
 public function setorder()
 {
     $address = Address::where('user_id', $this->user->id)->first();
     if ($address) {
         $carts = Cart::with('good')->where('user_id', $this->user->id)->get();
         $order = new Order();
         $order->user_id = $this->user->id;
         $order->status = 0;
         $order->address_id = $address->id;
         $order->express_id = 1;
         $order->express_code = 918682605098;
         $order->total_price = $this->total_price();
         $order->save();
         foreach ($carts as $cart) {
             $order_good = new Order_good();
             $order_good->good_id = $cart->good_id;
             $order_good->order_id = $order->id;
             $order_good->number = $cart->num;
             $order_good->save();
         }
         $carts = Cart::where('user_id', $this->user->id)->delete();
         $order_goods = Order_good::with('good')->where('order_id', $order->id)->get();
         return view('wechat.setorder', ['address' => $address, 'carts' => $order_goods, 'total_price' => $order->total_price, 'cart_number' => 0, 'user_id' => $this->user->id]);
     } else {
         return redirect(url('/address', $this->user->id));
     }
 }
Example #20
0
 public function getCarts()
 {
     return $this->hasMany(Cart::className(), ['user_id' => 'id']);
 }
 public static function new($user_id = null)
 {
     $cart = new Cart(['user_id' => $user_id]);
     $cart->save();
     return $cart;
 }
 /**
  * @Authorized('/login')
  * @param CheckOutBindingModel $input
  * @param Cart $cart
  */
 public function doCheckout(CheckOutBindingModel $input, Cart $cart)
 {
     $this->validation->validate($input, ['names' => 'required|min:5', 'email' => 'required|min:5|email', 'mobile' => 'required', 'address' => 'required']);
     if ($this->validation->getErrors() == false) {
         $cart->checkout($input->names, $input->email, $input->mobile, $input->address);
         $this->emptyCart();
         return $this->redirect->to('/products')->withSuccess("You have sent your order successfully.")->go();
     }
     $this->redirect->to('/cart/checkout')->withErrors($this->validation->getErrors())->withInput($input)->go();
 }
Example #23
0
<?php

/* @var $this \yii\web\View */
/* @var $content string */
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use app\assets\AppAsset;
use app\models\Cart;
AppAsset::register($this);
$navItems[] = ['label' => 'Home', 'url' => ['/']];
if (Yii::$app->user->isGuest) {
    $navItems[] = ['label' => 'Login', 'url' => ['/site/login']];
} else {
    $cart = Cart::getCurrentCart();
    $cart->processCart();
    $navItems[] = ['label' => 'Cart' . ($cart->quantity ? ' (' . $cart->quantity . ')' : ''), 'url' => ['/cart']];
    $navItems[] = ['label' => 'My Account', 'url' => ['/account']];
    $navItems[] = ['label' => 'Logout (' . Yii::$app->user->identity->name . ')', 'url' => ['/site/logout'], 'linkOptions' => ['data-method' => 'post']];
}
$this->beginPage();
?>
<!DOCTYPE html>
<html lang="<?php 
echo Yii::$app->language;
?>
">
    <head>
        <meta charset="<?php 
echo Yii::$app->charset;
Example #24
0
<?php

use app\models\Event;
use app\models\Cart;
$_event = Event::findOne(2);
$carts = Cart::findAll(['status' => Cart::CART_SOLD]);
$sold_tickets = [];
foreach ($carts as $cart) {
    $items = $cart->items;
    foreach ($items as $i) {
        if (!isset($sold_tickets[$i->ticket_id])) {
            $sold_tickets[$i->ticket_id] = 0;
        }
        $sold_tickets[$i->ticket_id] += $i->quantity;
    }
}
$sold_total = 0;
$total_tickets_sold = 0;
$this->title = Yii::t('app', 'Event Summary');
?>
<div class="foxden-default-index">
    <h1><?php 
echo $this->title;
?>
</h1>
    <?php 
foreach ($_event->ticketGroups as $group) {
    if (sizeof($_event->ticketGroups) > 1) {
        ?>
<h2><?php 
        echo $group->name;
Example #25
0
 private function total_price()
 {
     $total_price = 0;
     $carts = Cart::with('good')->where('user_id', $this->user->id)->get();
     foreach ($carts as $cart) {
         $total_price += $cart->number * $cart->good->price;
     }
     return $total_price;
 }