public function actionDelGood()
 {
     if (Yii::$app->request->isAjax) {
         $data = Yii::$app->request->post();
         $goods_id = $data['goods_id'];
         \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         $session = new Sitesession();
         $user_id = $session->getUserId();
         if (!$user_id) {
             return ['goods_id' => 0, 'count' => 0];
         }
         $goods = $session->getGoods($user_id);
         $shopping_car = $goods ? $goods : array();
         if (in_array($goods_id, $shopping_car)) {
             foreach ($shopping_car as $key => $gid) {
                 if ($gid == $goods_id) {
                     unset($shopping_car[$key]);
                 }
             }
         }
         $session->storeGoods($user_id, $shopping_car);
         $count = count($shopping_car);
         return ['goods_id' => $goods_id, 'shopping_car' => $shopping_car, 'count' => $count];
     }
 }
 public function actionValiderOrder()
 {
     $data = Yii::$app->request->get();
     if (isset($data['order_id'])) {
         $order_id = $data['order_id'];
         $connection = \Yii::$app->db;
         $command = $connection->createCommand('SELECT * FROM orders WHERE order_id=' . $order_id);
         $order = $command->queryOne();
         $user = $connection->createCommand('SELECT * FROM customer WHERE id =' . $order['action_user'])->queryOne();
         $ordergoods = $connection->createCommand('SELECT * FROM order_goods WHERE order_id =' . $order_id)->queryAll();
         return $this->render('orderlist', ['order' => $order, 'ordergoods' => $ordergoods, 'user' => $user]);
     } else {
         $datapost = Yii::$app->request->post();
         $count = $datapost['goods_count'];
         $type = $datapost['type'];
         $order_id = $datapost['order_id'];
         $remise = $datapost['remise'];
         $str = '';
         $goods_nums_arr = $goods_price_arr = array();
         for ($i = 0; $i < $count; $i++) {
             $good_id = $datapost['good_id'][$i];
             $good_num = $datapost['number'][$i];
             $good_price = $datapost['pprice'][$i];
             $str = empty($str) ? $str . $good_id : $str . "," . $good_id;
             $goods_nums_arr[$good_id] = $good_num;
             $goods_price_arr[$good_id] = $good_price;
         }
         $session = new Sitesession();
         $user_id = $session->getUserId();
         $connection = \Yii::$app->db;
         $transaction = $connection->beginTransaction();
         try {
             if ($order_id != 0) {
                 $insert_order_id = $order_id;
                 //删除order_id原来对应的goods
                 OrderGoods::deleteAll('order_id = :order_id', [':order_id' => $order_id]);
             } else {
                 //添加orders
                 $insert_order_id = 0;
                 $orders = new Orders();
                 $orders->action_user = $user_id;
                 $orders->type = $type;
                 if ($orders->save()) {
                     $insert_order_id = $orders->order_id;
                 }
             }
             // 所有的查询都在主服务器上执行
             $goods = $connection->createCommand('SELECT * FROM goods WHERE goods_id in (' . $str . ')')->queryAll();
             //$connection->createCommand("UPDATE user SET username='******' WHERE id=1")->execute();
             $total = 0;
             //添加goods
             foreach ($goods as $i => $good) {
                 //处理商品数量,1.包含'|',前面是数量,后面是赠送。。 2.不包含'|',就是数量
                 $num_str = $goods_nums_arr[$good['goods_id']];
                 $num_buy = 1;
                 $num_free = 0;
                 if (strstr($num_str, "|")) {
                     $num_arr = explode("|", $num_str);
                     $num_buy = isset($num_arr[0]) && is_numeric($num_arr[0]) && intval($num_arr[0]) >= 0 ? intval($num_arr[0]) : 1;
                     $num_free = isset($num_arr[1]) && is_numeric($num_arr[1]) && intval($num_arr[1]) >= 0 ? intval($num_arr[1]) : 0;
                 } else {
                     $num_buy = is_numeric($num_str) && intval($num_str) > 0 ? intval($num_str) : 1;
                 }
                 if ($num_buy) {
                     $price = is_numeric($goods_price_arr[$good['goods_id']]) && $goods_price_arr[$good['goods_id']] != 0.0 ? $goods_price_arr[$good['goods_id']] : $good['shop_price'];
                     $order_goods = new OrderGoods();
                     $order_goods->order_id = $insert_order_id;
                     $order_goods->goods_id = $good['goods_id'];
                     $order_goods->goods_name = $good['goods_name'];
                     $order_goods->goods_sn = $good['goods_sn'];
                     $order_goods->goods_number = $num_buy;
                     $order_goods->market_price = $price;
                     $sum = $price * $num_buy;
                     $order_goods->sum_price = $sum;
                     $total += $sum;
                     $order_goods->save();
                 }
                 if ($num_free) {
                     $order_free_goods = new OrderGoods();
                     $order_free_goods->order_id = $insert_order_id;
                     $order_free_goods->goods_id = $good['goods_id'];
                     $order_free_goods->goods_name = $good['goods_name'];
                     $order_free_goods->goods_sn = $good['goods_sn'];
                     $order_free_goods->goods_number = $num_free;
                     $order_free_goods->market_price = 0;
                     $order_free_goods->sum_price = 0;
                     $order_free_goods->save();
                 }
             }
             $connection->createCommand()->update('orders', ['sum_price' => $total, 'remise' => $remise], 'order_id = ' . $insert_order_id)->execute();
             $transaction->commit();
             //情况session:一定要先清空goods再清空user
             $session->clearGoodsSession();
             $session->clearUserSession();
             $session->clearOrderSession();
             $this->redirect(array('/order/valider-order', 'order_id' => $insert_order_id));
         } catch (\Exception $e) {
             $transaction->rollBack();
             throw $e;
         }
     }
 }
Exemple #3
0
if (is_null($user) || !$user) {
    ?>
	    <?php 
    echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], 'id', 'username', 'title', 'address', ['label' => '选择', 'format' => 'raw', 'value' => function ($data) {
        $url = "index.php?r=order/fix-user&user_id=" . $data->id;
        return Html::a('选择用户', $url, ['title' => '选择']);
    }]]]);
    ?>

	<?php 
} else {
    ?>
		
		<?php 
    echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], 'goods_id', 'goods_sn', 'goods_name', 'shop_price', 'promote_price', ['label' => '添加到购物列表', 'format' => 'raw', 'value' => function ($data) {
        $session = new Sitesession();
        $user_id = $session->getUserId();
        $goods = $session->getGoods($user_id);
        if (!empty($goods) && in_array($data->goods_id, $goods)) {
            $text = "已添加";
            $actionadd = 0;
        } else {
            $text = "添加";
            $actionadd = 1;
        }
        return Html::a($text, null, ['title' => '添加', 'goods_id' => $data->goods_id, 'actionad' => $actionadd, 'class' => 'jsAddGoods']);
    }]]]);
    ?>
	<?php 
}
?>