pmtpenza

Untitled

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