public function createSales() { $data = json_decode($this->data); $cart = isset($data->cart) ? json_decode($data->cart) : null; $res = true; foreach ($cart as $p) { $remarks = ""; foreach ($p as $k => $v) { if (substr($k, 0, 5) == "data_") { $remarks .= ($remarks == "" ? "" : ", ") . substr($k, 5) . ": " . $v; } } $sale = Sale::findOne(['product_id' => $p->id, 'order_id' => $this->id, 'data' => $remarks]); if (!$sale) { $sale = new Sale(); } $sale->time = date("Y-m-d H:i:s"); $sale->isdel = 0; $sale->order_id = $this->id; $sale->product_id = $p->id; $sale->quantity = $p->quantity; $sale->amount = $p->quantity * $p->price; $sale->data = $remarks; if (!$sale->save()) { $res = false; } else { $customer = Customer::findOne($this->customer_id); if ($customer) { $customer->last_action = 2; $customer->last_time = date("Y-m-d H:i:s"); if (!$customer->save()) { $res = false; } } } } return $res; }
/** * Finds the Sale model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Sale the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Sale::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }