pmtpenza

Untitled

Jul 17th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. <?php
  2.  
  3. namespace backend\controllers;
  4.  
  5. use Yii;
  6. use backend\models\Deals;
  7. use backend\models\DealsSearch;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11. use backend\models\Client;
  12. use yii\web\Response;
  13. use backend\models\Order;
  14. use backend\models\OrderForm;
  15. use backend\models\TypeZakaz;
  16.  
  17.  
  18.  
  19. /**
  20. * DealsController implements the CRUD actions for Deals model.
  21. */
  22. class DealsController extends Controller
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function behaviors()
  28. {
  29. return [
  30. 'verbs' => [
  31. 'class' => VerbFilter::className(),
  32. 'actions' => [
  33. 'delete' => ['POST'],
  34. ],
  35. ],
  36. ];
  37. }
  38.  
  39. /**
  40. * Lists all Deals models.
  41. * @return mixed
  42. */
  43. public function actionIndex()
  44. {
  45. $searchModel = new DealsSearch();
  46. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  47.  
  48. return $this->render('index', [
  49. 'searchModel' => $searchModel,
  50. 'dataProvider' => $dataProvider,
  51. ]);
  52. }
  53.  
  54. /**
  55. * Displays a single Deals model.
  56. * @param integer $id
  57. * @return mixed
  58. * @throws NotFoundHttpException if the model cannot be found
  59. */
  60. public function actionView($id)
  61. {
  62. return $this->render('view', [
  63. 'model' => $this->findModel($id),
  64. ]);
  65. }
  66.  
  67. /**
  68. * Creates a new Deals model.
  69. * If creation is successful, the browser will be redirected to the 'view' page.
  70. * @return mixed
  71. */
  72. public function actionCreate()
  73. {
  74. $model = new Deals();
  75. $orders = [new OrderForm()];
  76. if ($model->load(Yii::$app->request->post()) && $model->save())
  77. {
  78.  
  79. foreach(Yii::$app->request->post('OrderForm', []) as $form) {
  80.  
  81. $order = new Order();
  82. $order->type_zakaz_id =$form['type_zakaz_id'];
  83. $order->link('deals', $model);
  84. }
  85.  
  86. $oplatas = [new OplataForm()];
  87. if ($model->load(Yii::$app->request->post()) && $model->save())
  88. {
  89.  
  90. foreach(Yii::$app->request->post('OplataForm', []) as $form) {
  91.  
  92. $oplata = new Oplata();
  93. $oplata->deals_id =$form['deals_id'];
  94. $oplata->link('deals', $model);
  95. }
  96.  
  97. return $this->redirect(['view', 'id' => $model->id]);
  98. }
  99. return $this->render('create', [
  100. 'model' => $model,
  101. 'orders' => $orders,
  102. 'oplatas' => $oplatas,
  103. ]);
  104.  
  105. }
  106. }
  107.  
  108. /**
  109. * Updates an existing Deals model.
  110. * If update is successful, the browser will be redirected to the 'view' page.
  111. * @param integer $id
  112. * @return mixed
  113. * @throws NotFoundHttpException if the model cannot be found
  114. */
  115. public function actionUpdate($id)
  116. {
  117. $model = $this->findModel($id);
  118.  
  119. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  120. return $this->redirect(['view', 'id' => $model->id]);
  121. }
  122.  
  123. return $this->render('update', [
  124. 'model' => $model,
  125. 'orders' => $orders,
  126. ]);
  127. }
  128.  
  129. /**
  130. * Deletes an existing Deals model.
  131. * If deletion is successful, the browser will be redirected to the 'index' page.
  132. * @param integer $id
  133. * @return mixed
  134. * @throws NotFoundHttpException if the model cannot be found
  135. */
  136. public function actionDelete($id)
  137. {
  138. $this->findModel($id)->delete();
  139.  
  140. return $this->redirect(['index']);
  141. }
  142.  
  143. /**
  144. * Finds the Deals model based on its primary key value.
  145. * If the model is not found, a 404 HTTP exception will be thrown.
  146. * @param integer $id
  147. * @return Deals the loaded model
  148. * @throws NotFoundHttpException if the model cannot be found
  149. */
  150. protected function findModel($id)
  151. {
  152. if (($model = Deals::findOne($id)) !== null) {
  153. return $model;
  154. }
  155.  
  156. throw new NotFoundHttpException('The requested page does not exist.');
  157. }
  158. public function actionGetClient($q = null)
  159. {
  160. Yii::$app->response->format = Response::FORMAT_JSON;
  161. $query = Client::find()
  162. ->asArray()
  163. ->select(['id', 'CONCAT(client.company, " , ", client.firstname) AS text']);
  164. $query->andFilterWhere(['or',
  165. ['like', 'client.firstname', $q],
  166. ['like', 'client.company', $q],
  167. ]);
  168.  
  169. $query->orderBy('client.company ASC');
  170.  
  171. $data = $query->limit(10)->all();
  172.  
  173. $out['results'] = array_values($data);
  174.  
  175. return $out;
  176. }
  177. public function actionGetType($q = null)
  178. {
  179. $response = Yii::$app->response;
  180. $response->format = \yii\web\Response::FORMAT_JSON;
  181.  
  182. $query = TypeZakaz::find()
  183. ->asArray()
  184. ->select(['type_zakaz.id', 'type_zakaz.type AS text']);
  185. $query->andFilterWhere(['or',
  186. ['like', 'type_zakaz.type', $q],
  187. ]);
  188.  
  189. $query->orderBy('type_zakaz.type ASC');
  190.  
  191. $data = $query->limit(10)->all();
  192.  
  193. $out['results'] = array_values($data);
  194.  
  195. return $out;
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment