Guest User

Untitled

a guest
Nov 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. <?php
  2. /////////// FORM
  3. echo $form->field($model, 'rk_invoice_id')->widget(\kartik\select2\Select2::className(), [
  4. 'initValueText' => \d3modules\lietvediba\dictionary\RkInvoiceDict::forSelect2Label($model->rk_invoice_id),
  5. 'pluginOptions' => [
  6. 'allowClear' => true,
  7. 'minimumInputLength' => 0,
  8. 'language' => [
  9. 'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
  10. ],
  11. 'ajax' => [
  12. 'url' => $url,
  13. 'dataType' => 'json',
  14. 'data' => new JsExpression('function(params) { return {q:params.term}; }')
  15. ],
  16. 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
  17. 'templateResult' => new JsExpression('function(city) { return city.text; }'),
  18. 'templateSelection' => new JsExpression('function (city) { return city.text; }'),
  19. ],
  20. ]);
  21.  
  22. // controller
  23. /**
  24. * for Select2
  25. * @param null $q
  26. * @param null $id
  27. * @return array
  28. */
  29. public function actionList($q = null, $id = null) {
  30. \Yii::$app->response->format = Response::FORMAT_JSON;
  31.  
  32. if($id){
  33. return ['results' => RkInvoiceDict::forSelect2Item($id)];
  34. }
  35.  
  36. return ['results' => RkInvoiceDict::forSelect2List($q)];
  37. }
  38. ///////dictionary
  39.  
  40. namespace d3modules\lietvediba\dictionary;
  41.  
  42.  
  43. use d3modules\lietvediba\models\RkInvoice;
  44.  
  45.  
  46. class RkInvoiceDict
  47. {
  48.  
  49. public static function forSelect2Item($id): array
  50. {
  51.  
  52. $query = self::forSelect2Query();
  53.  
  54. $invoice = $query
  55. ->andWhere([
  56. 'rk_invoice.id' => $id
  57. ])
  58. ->asArray()
  59. ->one();
  60. if (!$invoice) {
  61. return [
  62. 'id' => '',
  63. 'text' => ''
  64. ];
  65. }
  66.  
  67. return self::forSelect2Row($invoice);
  68. }
  69.  
  70. public static function forSelect2Label($id): string
  71. {
  72. if(!$id){
  73. return '';
  74. }
  75. $row = self::forSelect2Item($id);
  76. return $row['text'];
  77. }
  78.  
  79. private static function forSelect2Query()
  80. {
  81. $sqlInnerOn = '
  82. `partner_company`.`id` = CASE
  83. `rk_invoice`.`from_company_id`
  84. WHEN ' . \Yii::$app->SysCmp->getActiveCompanyId() . '
  85. THEN `rk_invoice`.`to_company_id`
  86. ELSE `rk_invoice`.`from_company_id`
  87. END
  88. ';
  89. return RkInvoice::find()
  90. ->select([
  91. 'rk_invoice.id',
  92. 'partner_company.name companyName',
  93. 'rk_invoice.number',
  94. 'rk_invoice.invoice_date',
  95. 'rk_invoice.amount',
  96. ])
  97. ->innerJoin('d3c_company partner_company', $sqlInnerOn);
  98. }
  99.  
  100. public static function forSelect2List($q = ''): array
  101. {
  102.  
  103. // if ($q === null) {
  104. // return [
  105. // 'id' => '',
  106. // 'text' => ''
  107. // ];
  108. // }
  109.  
  110. $query = self::forSelect2Query();
  111. $query
  112. ->orderBy('rk_invoice.invoice_date')
  113. ->limit(20);
  114.  
  115. if ($q !== null) {
  116. $query->andWhere(['like', 'CONCAT(
  117. partner_company.name,
  118. \' \',
  119. rk_invoice.number,
  120. \' \',
  121. rk_invoice.invoice_date
  122. )', $q]);
  123. }
  124.  
  125. $data = [];
  126. foreach ($query->asArray()->all() as $row) {
  127. $data[] = self::forSelect2Row($row);
  128. }
  129.  
  130. return $data;
  131.  
  132. }
  133.  
  134. private static function forSelect2Row($row): array
  135. {
  136. return [
  137. 'id' => $row['id'],
  138. 'text' => \Yii::$app->formatter->asDate($row['invoice_date']) .
  139. ' | ' . $row['number'] .
  140. ' | ' . number_format($row['amount'], 2, '.', ' ') .
  141. ' EUR' .
  142. ' | ' . $row['companyName']
  143. ];
  144. }
  145.  
  146. }
Add Comment
Please, Sign In to add comment