BillsController.php 6.4 KB

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