BillsController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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', 'update', '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. if($type == 'guarantee'){
  158. $percent_g = 0.015;
  159. $percent_r = 0.0015;
  160. $amount_vat = $amount = $type == 'guarantee'
  161. ? $percent_g*(isset($model->auction->lot) ? $model->auction->lot->start_price : 0 )
  162. : $percent_r*(isset($model->auction->lot) ? $model->auction->lot->start_price : 0 );
  163. if (isset($model->auction->lot) ? $model->auction->lot->nds : 0) {
  164. $amount = $amount/1.2*1;
  165. } else {
  166. $amount_vat *= 1.2;
  167. }
  168. $data = [
  169. 'billid' => $model->id,
  170. 'date' => date('m.d.Y'),
  171. 'userfio' => (empty($model->profile->fio) ? $model->profile->member : $model->profile->fio),
  172. 'summ' => $amount_vat,
  173. 'letters' => Yii::$app->num2text->moneyFormatted($amount_vat),
  174. 'letterpdv' => Yii::$app->num2text->moneyFormatted(($amount_vat / 100 * 20)),
  175. 'aukinfo' => "№" . (empty($model->auction) ? 'Не знайдено' : $model->auction->id) . " " . (empty($model->auction) ? 'Не знайдено' : $model->auction->name),
  176. 'nopdv' => ($amount_vat - ($amount_vat / 100 * 20)),
  177. 'ypdv' => ($amount_vat / 100 * 20)
  178. // 'auctionID' => $model->auction->id,
  179. // 'date' => date('d-m-Y'),
  180. // 'inn' => $model->profile->inn,
  181. // 'zkpo' => $model->profile->zkpo,
  182. // 'uAddress' => $model->profile->u_address,
  183. // 'bank' => $model->requisite->bank,
  184. // 'payAccount' => $model->requisite->account,
  185. // 'phone' => $model->profile->phone,
  186. // 'fax' => $model->profile->fax,
  187. // 'firmaFull' => $model->profile->firma_full,
  188. // 'billType' => $type == 'guarantee'? 'Гарантійний внесок': 'Регістраційний внесок',
  189. // 'amountString' => num2str($amount_vat),
  190. // 'amountVat' => $amount_vat,
  191. // 'amount' => $amount,
  192. // 'vat' => $model->auction->lot->nds ? 'з ПДВ' : 'без ПДВ',
  193. ];
  194. } else {
  195. $amount_vat = 100.00;
  196. $data = [
  197. 'billid' => $model->id,
  198. 'date' => date('m.d.Y'),
  199. 'userfio' => (empty($model->profile->fio) ? $model->profile->member : $model->profile->fio),
  200. 'summ' => $amount_vat + 20,
  201. 'letters' => Yii::$app->num2text->moneyFormatted($amount_vat+20),
  202. 'letterpdv' => Yii::$app->num2text->moneyFormatted(($amount_vat / 100 * 20)),
  203. 'aukinfo' => "№" . (empty($model->auction) ? 'Не знайдено' : $model->auction->id) . " " . (empty($model->auction) ? 'Не знайдено' : $model->auction->name),
  204. 'nopdv' => $amount_vat,
  205. 'ypdv' => ($amount_vat / 100 * 20)
  206. ];
  207. }
  208. $filename = templater()->process(
  209. $data,
  210. $type
  211. );
  212. return Yii::$app->response->sendFile($filename);
  213. }
  214. }