/** * Creates a new Orders model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Orders(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
public function actionCopy($id) { $model = $this->findModel($id); $newModel = new Orders(); //копируем параметры заказа $newModel->customers_customer_id = $model->customers_customer_id; $newModel->user_id = $model->user_id; $newModel->order_amount = $model->order_amount; $newModel->save(); //тут копируем список заказаных товаров //которые пользователь сможет править т.к. копия создается со статусом "Черновик" $list = Listofgoods::find()->where(['orders_order_id' => $model->order_id])->all(); foreach ($list as $item) { $newList = new Listofgoods(); $newList->orders_order_id = $newModel->order_id; $newList->goods_good_1c_id = $item->goods_good_1c_id; $newList->good_count = $item->good_count; $newList->save(); } return $this->redirect(['view', 'id' => $newModel->order_id]); }
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; } } }
/** * goodsIdList = [goodID => quantity] * @param array $goodsIdList * @return int $orderID */ public static function saveOrder($goodsIdList) { /* all the goods in $goodsIdList are ordered from the same firm */ $goodID = key($goodsIdList); $firmID = Goods::findOne($goodID)->firm_id; $totalGoods = $priceWithVat = $priceWithoutVat = $incrementPrice = 0; foreach ($goodsIdList as $id => $quantity) { $totalGoods += $quantity; $good = Goods::findOne($id); $priceWithVat += $good->price_with_vat * $quantity; $priceWithoutVat += $good->price_without_vat * $quantity; $incrementPrice += $good->increment_price * $quantity; } $order = new Orders(); $order->firm_id = $firmID; $order->price_with_vat = $priceWithVat; $order->price_without_vat = $priceWithoutVat; $order->increment_price = $incrementPrice; $order->total_types = count($goodsIdList); $order->total_goods = $totalGoods; $order->save(); $orderID = $order->getPrimaryKey(); return $orderID; }