Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /////////// FORM
- echo $form->field($model, 'rk_invoice_id')->widget(\kartik\select2\Select2::className(), [
- 'initValueText' => \d3modules\lietvediba\dictionary\RkInvoiceDict::forSelect2Label($model->rk_invoice_id),
- 'pluginOptions' => [
- 'allowClear' => true,
- 'minimumInputLength' => 0,
- 'language' => [
- 'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
- ],
- 'ajax' => [
- 'url' => $url,
- 'dataType' => 'json',
- 'data' => new JsExpression('function(params) { return {q:params.term}; }')
- ],
- 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
- 'templateResult' => new JsExpression('function(city) { return city.text; }'),
- 'templateSelection' => new JsExpression('function (city) { return city.text; }'),
- ],
- ]);
- // controller
- /**
- * for Select2
- * @param null $q
- * @param null $id
- * @return array
- */
- public function actionList($q = null, $id = null) {
- \Yii::$app->response->format = Response::FORMAT_JSON;
- if($id){
- return ['results' => RkInvoiceDict::forSelect2Item($id)];
- }
- return ['results' => RkInvoiceDict::forSelect2List($q)];
- }
- ///////dictionary
- namespace d3modules\lietvediba\dictionary;
- use d3modules\lietvediba\models\RkInvoice;
- class RkInvoiceDict
- {
- public static function forSelect2Item($id): array
- {
- $query = self::forSelect2Query();
- $invoice = $query
- ->andWhere([
- 'rk_invoice.id' => $id
- ])
- ->asArray()
- ->one();
- if (!$invoice) {
- return [
- 'id' => '',
- 'text' => ''
- ];
- }
- return self::forSelect2Row($invoice);
- }
- public static function forSelect2Label($id): string
- {
- if(!$id){
- return '';
- }
- $row = self::forSelect2Item($id);
- return $row['text'];
- }
- private static function forSelect2Query()
- {
- $sqlInnerOn = '
- `partner_company`.`id` = CASE
- `rk_invoice`.`from_company_id`
- WHEN ' . \Yii::$app->SysCmp->getActiveCompanyId() . '
- THEN `rk_invoice`.`to_company_id`
- ELSE `rk_invoice`.`from_company_id`
- END
- ';
- return RkInvoice::find()
- ->select([
- 'rk_invoice.id',
- 'partner_company.name companyName',
- 'rk_invoice.number',
- 'rk_invoice.invoice_date',
- 'rk_invoice.amount',
- ])
- ->innerJoin('d3c_company partner_company', $sqlInnerOn);
- }
- public static function forSelect2List($q = ''): array
- {
- // if ($q === null) {
- // return [
- // 'id' => '',
- // 'text' => ''
- // ];
- // }
- $query = self::forSelect2Query();
- $query
- ->orderBy('rk_invoice.invoice_date')
- ->limit(20);
- if ($q !== null) {
- $query->andWhere(['like', 'CONCAT(
- partner_company.name,
- \' \',
- rk_invoice.number,
- \' \',
- rk_invoice.invoice_date
- )', $q]);
- }
- $data = [];
- foreach ($query->asArray()->all() as $row) {
- $data[] = self::forSelect2Row($row);
- }
- return $data;
- }
- private static function forSelect2Row($row): array
- {
- return [
- 'id' => $row['id'],
- 'text' => \Yii::$app->formatter->asDate($row['invoice_date']) .
- ' | ' . $row['number'] .
- ' | ' . number_format($row['amount'], 2, '.', ' ') .
- ' EUR' .
- ' | ' . $row['companyName']
- ];
- }
- }
Add Comment
Please, Sign In to add comment