pmtpenza

Untitled

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