BillsController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use app\models\Bills;
  5. use app\models\BillsSearch;
  6. use yii\filters\AccessControl;
  7. use yii\web\Controller;
  8. use yii\web\NotFoundHttpException;
  9. use yii\filters\VerbFilter;
  10. use yii\web\Response;
  11. /**
  12. * BillsController implements the CRUD actions for Bills model.
  13. */
  14. class BillsController extends Controller
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function behaviors()
  20. {
  21. return [
  22. 'verbs' => [
  23. 'class' => VerbFilter::className(),
  24. 'actions' => [
  25. 'delete' => ['POST'],
  26. ],
  27. ],
  28. 'access' => [
  29. 'class' => AccessControl::className(),
  30. 'rules' => [
  31. [
  32. 'allow' => true,
  33. 'actions' => ['payed', 'download-template'],
  34. 'roles' => ['admin'],
  35. ],
  36. [
  37. 'allow' => true,
  38. 'actions' => ['index', 'create', 'update', 'view', 'delete', 'download-requisites'],
  39. 'roles' => ['@'],
  40. ],
  41. ]
  42. ]
  43. ];
  44. }
  45. /**
  46. * Lists all Bills models.
  47. * @return mixed
  48. */
  49. public function actionIndex()
  50. {
  51. $searchModel = new BillsSearch();
  52. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  53. return $this->render('index', [
  54. 'searchModel' => $searchModel,
  55. 'dataProvider' => $dataProvider,
  56. ]);
  57. }
  58. /**
  59. * Displays a single Bills model.
  60. * @param integer $id
  61. * @return mixed
  62. * @throws NotFoundHttpException if the model cannot be found
  63. */
  64. public function actionView($id)
  65. {
  66. return $this->render('view', [
  67. 'model' => $this->findModel($id),
  68. ]);
  69. }
  70. /**
  71. * Creates a new Bills 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 Bills();
  78. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  79. return $this->redirect(['view', 'id' => $model->id]);
  80. }
  81. return $this->render('create', [
  82. 'model' => $model,
  83. ]);
  84. }
  85. /**
  86. * Updates an existing Bills model.
  87. * If update is successful, the browser will be redirected to the 'view' page.
  88. * @param integer $id
  89. * @return mixed
  90. * @throws NotFoundHttpException if the model cannot be found
  91. */
  92. public function actionUpdate($id)
  93. {
  94. $model = $this->findModel($id);
  95. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  96. return $this->redirect(['view', 'id' => $model->id]);
  97. }
  98. return $this->render('update', [
  99. 'model' => $model,
  100. ]);
  101. }
  102. /**
  103. * Deletes an existing Bills model.
  104. * If deletion is successful, the browser will be redirected to the 'index' page.
  105. * @param integer $id
  106. * @return mixed
  107. * @throws NotFoundHttpException if the model cannot be found
  108. */
  109. public function actionDelete($id)
  110. {
  111. $this->findModel($id)->delete();
  112. return $this->redirect(['index']);
  113. }
  114. public function actionPayed($id, $payed)
  115. {
  116. Yii::$app->response->format = Response::FORMAT_JSON;
  117. return $this->findModel($id)->updateAttributes(['payed' => $payed]);
  118. }
  119. /**
  120. * Finds the Bills model based on its primary key value.
  121. * If the model is not found, a 404 HTTP exception will be thrown.
  122. * @param integer $id
  123. * @return Bills the loaded model
  124. * @throws NotFoundHttpException if the model cannot be found
  125. */
  126. protected function findModel($id)
  127. {
  128. if (($model = Bills::findOne($id)) !== null) {
  129. return $model;
  130. }
  131. throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
  132. }
  133. public function actionDownloadTemplate()
  134. {
  135. $filename = templater()->process([
  136. 'billID' => 'номер рахунку',
  137. 'auctionID' => 'номер аукціону',
  138. 'date' => date('d-m-Y'),
  139. 'inn' => 'ІПН',
  140. 'zkpo' => 'ЭКПО',
  141. 'uAddress' => 'юр. адреса',
  142. 'bank' => 'банк',
  143. 'payAccount' => 'розрахунковий рахунок',
  144. 'phone' => 'телефон',
  145. 'fax' => 'факс',
  146. 'firmaFull' => 'повна назва фірми',
  147. 'billType' => 'тип рахунку',
  148. 'amountString' => num2str(121.34),
  149. 'amount' => 121.34,
  150. 'amountVat' => 121.34,
  151. ]);
  152. return Yii::$app->response->sendFile($filename);
  153. }
  154. public function actionDownloadRequisites($id) {
  155. $model = $this->findModel($id);
  156. $type = $model->type;
  157. $percent_g = 0.015;
  158. $percent_r = 0.0015;
  159. $amount_vat = $amount = $type == 'guarantee'
  160. ? $percent_g*$model->auction->lot->start_price
  161. : $percent_r*$model->auction->lot->start_price;
  162. if ($model->auction->lot->nds) {
  163. $amount = $amount/1.2*1;
  164. } else {
  165. $amount_vat *= 1.2;
  166. }
  167. $filename = templater()->process(
  168. [
  169. 'billID' => $model->id,
  170. 'auctionID' => $model->auction->id,
  171. 'date' => date('d-m-Y'),
  172. 'inn' => $model->profile->inn,
  173. 'zkpo' => $model->profile->zkpo,
  174. 'uAddress' => $model->profile->u_address,
  175. 'bank' => $model->requisite->bank,
  176. 'payAccount' => $model->requisite->account,
  177. 'phone' => $model->profile->phone,
  178. 'fax' => $model->profile->fax,
  179. 'firmaFull' => $model->profile->firma_full,
  180. 'billType' => $type == 'guarantee'? 'Гарантійний внесок': 'Регістраційний внесок',
  181. 'amountString' => num2str($amount_vat),
  182. 'amountVat' => $amount_vat,
  183. 'amount' => $amount,
  184. 'vat' => $model->auction->lot->nds ? 'з ПДВ' : 'без ПДВ',
  185. ],
  186. $type
  187. );
  188. return Yii::$app->response->sendFile($filename);
  189. }
  190. }