예제 #1
0
 public function actionCheckout()
 {
     $user_id = 0;
     if (Yii::$app->user->isGuest) {
         return $this->redirect('/login');
     }
     if (!Yii::$app->user->isGuest) {
         $user_id = Yii::$app->user->identity->id;
     }
     $cart_json = [];
     if (isset($_COOKIE["cart_{$user_id}"])) {
         $cart_json = Json::decode($_COOKIE["cart_{$user_id}"]);
     } elseif (isset($_COOKIE["cart_0"])) {
         $cart_json = Json::decode($_COOKIE["cart_0"]);
     } else {
         return $this->redirect('/shop');
         Yii::$app->end();
     }
     $ar_product_id = array_keys($cart_json);
     $cart_items = Product::find()->where(['in', 'id', $cart_json])->all();
     if (isset($_POST) && !empty($_POST['place_order'])) {
         $grand_total = 0.0;
         $grand_total_purchase_price = 0.0;
         foreach ($cart_items as $ci) {
             $unit_price = floatval($ci->selling_price);
             $qty = intval($cart_json[$ci->id]['qty']);
             $row_total = $unit_price * $qty;
             $grand_total += $row_total;
             $unit_purchase_price = floatval($ci->purchase_price);
             $row_total_purchase_price = $unit_purchase_price * $qty;
             $grand_total_purchase_price += $row_total_purchase_price;
         }
         $order = new Order();
         $order->bill_number = Custom::getUniqueId(0, 6);
         $order->member_id = $user_id;
         $order->total_amount = $grand_total;
         $order->total_payable = $grand_total;
         $order->payment_method = $_POST['payment_method'];
         $order->beforeSave(true);
         if ($order->validate() && $order->insert()) {
             $product = '';
             foreach ($cart_items as $ci) {
                 $unit_price = floatval($ci->selling_price);
                 $qty = intval($cart_json[$ci->id]['qty']);
                 $row_total = $unit_price * $qty;
                 $product .= "{$ci->id},";
                 $cart = new Cart();
                 $cart->order_id = $order->id;
                 $cart->product_id = $ci->id;
                 $cart->unit_selling_price = $ci->selling_price;
                 $cart->quantity_sold = $qty;
                 $cart->subtotal_payable = $row_total;
                 $cart->subtotal_paid = $row_total;
                 $cart->beforeSave(true);
                 $cart->insert();
             }
         }
         $sale_amount = $grand_total - $grand_total_purchase_price;
         $app_root = \Yii::getAlias('@approot');
         include "{$app_root}/affiliate/controller/record-sale.php";
         $cookies = Yii::$app->response->cookies;
         if (isset($_COOKIE['cart_' . $user_id])) {
             $cookies->remove('cart_' . $user_id);
             unset($cookies['cart_' . $user_id]);
         }
         if (isset($_COOKIE['cart_0'])) {
             $cookies->remove('cart_0');
             unset($cookies['cart_0']);
         }
         return $this->redirect(['/member/order', 'id' => $order->id]);
     }
     return $this->render('checkout', ['cart_items' => $cart_items, 'cookie_data' => $cart_json]);
 }