Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace backend\controllers;
- use Yii;
- use backend\models\Deals;
- use backend\models\DealsSearch;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- use backend\models\Client;
- use yii\web\Response;
- use backend\models\Order;
- use backend\models\OrderForm;
- use backend\models\TypeZakaz;
- use backend\models\OplataForm;
- use backend\models\Oplata;
- /**
- * DealsController implements the CRUD actions for Deals model.
- */
- class DealsController extends Controller
- {
- /**
- * {@inheritdoc}
- */
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['POST'],
- ],
- ],
- ];
- }
- /**
- * Lists all Deals models.
- * @return mixed
- */
- public function actionIndex()
- {
- $searchModel = new DealsSearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
- /**
- * Displays a single Deals model.
- * @param integer $id
- * @return mixed
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionView($id)
- {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
- /**
- * Creates a new Deals model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @return mixed
- */
- public function actionCreate()
- {
- $model = new Deals();
- $orders = [new OrderForm()];
- $oplatas = [new OplataForm()];
- if ($model->load(Yii::$app->request->post()) && $model->save())
- {
- foreach(Yii::$app->request->post('OrderForm', []) as $form) {
- $order = new Order();
- $order->type_zakaz_id =$form['type_zakaz_id'];
- $order->link('deals', $model);
- }
- foreach(Yii::$app->request->post('OplataForm', []) as $form) {
- $oplata = new Oplata();
- $oplata->deals_id =$form['deals_id'];
- $oplata->summa =$form['summa'];
- $oplata->date_oplata =$form['date_oplata'];
- $oplata->link('deals', $model);
- }
- return $this->redirect(['view', 'id' => $model->id]);
- }
- return $this->render('create', [
- 'model' => $model,
- 'orders' => $orders,
- 'oplatas' => $oplatas,
- ]);
- }
- /**
- * Updates an existing Deals model.
- * If update is successful, the browser will be redirected to the 'view' page.
- * @param integer $id
- * @return mixed
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionUpdate($id)
- {
- $model = $this->findModel($id);
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- $orders = [];
- $i = 0;
- foreach ($model->orders as $order) {
- $orders[$i] = new OrderForm(ArrayHelper::toArray($order));
- $i ++;
- }
- $oplatas = [];
- $i = 0;
- foreach ($model->oplatas as $oplata) {
- $orders[$i] = new OplataForm(ArrayHelper::toArray($oplata));
- $i ++;
- }
- return $this->redirect(['view', 'id' => $model->id]);
- }
- return $this->render('update', [
- 'model' => $model,
- 'orders' => $orders,
- 'oplatas' => $oplatas,
- ]);
- }
- /**
- * Deletes an existing Deals model.
- * If deletion is successful, the browser will be redirected to the 'index' page.
- * @param integer $id
- * @return mixed
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionDelete($id)
- {
- $this->findModel($id)->delete();
- return $this->redirect(['index']);
- }
- /**
- * Finds the Deals model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param integer $id
- * @return Deals the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- protected function findModel($id)
- {
- if (($model = Deals::findOne($id)) !== null) {
- return $model;
- }
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- public function actionGetClient($q = null)
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- $query = Client::find()
- ->asArray()
- ->select(['id', 'CONCAT(client.company, " , ", client.firstname) AS text']);
- $query->andFilterWhere(['or',
- ['like', 'client.firstname', $q],
- ['like', 'client.company', $q],
- ]);
- $query->orderBy('client.company ASC');
- $data = $query->limit(10)->all();
- $out['results'] = array_values($data);
- return $out;
- }
- public function actionGetType($q = null)
- {
- $response = Yii::$app->response;
- $response->format = \yii\web\Response::FORMAT_JSON;
- $query = TypeZakaz::find()
- ->asArray()
- ->select(['type_zakaz.id', 'type_zakaz.type AS text']);
- $query->andFilterWhere(['or',
- ['like', 'type_zakaz.type', $q],
- ]);
- $query->orderBy('type_zakaz.type ASC');
- $data = $query->limit(10)->all();
- $out['results'] = array_values($data);
- return $out;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment