1

I am trying to run my console application in yii2 but I am getting error.

Exception 'yii\base\UnknownPropertyException' with message 'Setting unknown property: yii\console\Response::formatters' in F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Component.php:209

Stack trace:
#0 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\BaseYii.php(558): yii\base\Component->__set('formatters', Array)
#1 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\BaseObject.php(107): yii\BaseYii::configure(Object(yii\console\Response), Array)
#2 [internal function]: yii\base\BaseObject->__construct(Array)
#3 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\Container.php(400): ReflectionClass->newInstanceArgs(Array)
#4 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\Container.php(159): yii\di\Container->build('yii\\console\\Res...', Array, Array)
#5 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\BaseYii.php(365): yii\di\Container->get('yii\\console\\Res...', Array, Array)
#6 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\ServiceLocator.php(137): yii\BaseYii::createObject(Array)
#7 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Module.php(742): yii\di\ServiceLocator->get('response', true)
#8 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\Instance.php(164): yii\base\Module->get('response')
#9 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\Instance.php(137): yii\di\Instance->get(NULL)
#10 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Controller.php(109): yii\di\Instance::ensure(Object(yii\di\Instance), 'yii\\base\\Respon...')
#11 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\BaseObject.php(109): yii\base\Controller->init()
#12 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Controller.php(98): yii\base\BaseObject->__construct(Array)
#13 [internal function]: yii\base\Controller->__construct('cron', Object(yii\console\Application), Array)
#14 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\Container.php(392): ReflectionClass->newInstanceArgs(Array)
#15 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\di\Container.php(159): yii\di\Container->build('console\\control...', Array, Array)
#16 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\BaseYii.php(345): yii\di\Container->get('console\\control...', Array)
#17 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Module.php(642): yii\BaseYii::createObject('console\\control...', Array)
#18 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Module.php(596): yii\base\Module->createControllerByID('cron')
#19 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Module.php(522): yii\base\Module->createController('monthlybill')
#20 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\console\Application.php(180): yii\base\Module->runAction('cron/monthlybil...', Array)
#21 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\console\Application.php(147): yii\console\Application->runAction('cron/monthlybil...', Array)
#22 F:\xampp\htdocs\mdc\vendor\yiisoft\yii2\base\Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request))
#23 F:\xampp\htdocs\mdc\yii(27): yii\base\Application->run()
#24 {main}
Press any key to continue . . .

Common\config\main-local.php

'components' => [
 'mycomponent' => [

        'class' => 'common\components\MyComponent',

    ],
 'response' => [
        'formatters' => [
            'pdf' => [
                'class' => 'robregonm\pdf\PdfResponseFormatter',
            ],
        ]
    ],

],

CronController

namespace console\controllers;
use yii\console\Controller;
use Yii;
use yii\filters\AccessControl;
class CronController extends Controller {

  public function actionMonthlybill()
  {

     Yii::$app->mycomponent->billing();
  }

}

Update 1

Below is my component code

public static function billing()
{
    $date = date('Y-m-d 23:59:59');;

    $rel = MdcmetercustRel::find()->all();
    if(count($rel)>0)
    {
        foreach ($rel as $item) {
            $tariff_id = $item->tariff_id;
            $tariff_name = $item->tariff_name;
            $consumer_no = $item->cust_id;
            $consumer_name =  $item->name;
            $msn = $item->msn;
            $meter_type = $item->meter_type;
            $consumer_address = $item->cust_address;
            $prev_read = 0;
            $p_data = \Yii::$app->db->createCommand(/** @lang text */ "SELECT m.`curr_read` FROM `mdc_bill_details` 
             m WHERE m.`consumer_no` = '$consumer_no' ORDER BY m.`id` DESC LIMIT 1;")->queryOne();

            if ($p_data)
            {
                $prev_read = $p_data['curr_read'];
            }
            else
            {
                $prev_read = 0;
            }

            $mData = \Yii::$app->db->createCommand(/** @lang text */ "SELECT s.`slab_start`, s.`slab_end`, s.`rate` FROM `mdc_tariff_slabs` s 
            INNER JOIN `mdc_meter_cust_rel` r ON s.`t_id` = r.`tariff_id`
            WHERE r.`cust_id` = $consumer_no")->queryAll();
            $array = ArrayHelper::map($mData, 'slab_end', 'rate');


            $data = MdcmetersData::find()->where(['and',['cust_id'=>$consumer_no],['<','data_date_time',$date]])->orderBy(['id' => SORT_DESC])->one();
            $current_read =  $data['kwh_t'];
            $slabs = [100 => 10, 150 => 12, PHP_INT_MAX => 14];
            $bill = self::billCalc($current_read,$slabs);
            //echo substr ($x, 0, 3);  // outputs 123
            $read_diff = $current_read - $prev_read;
            $avg_units = 0;
            $bill_month =  date('Y-m-d h:i:s');
            $m_read_date =  date('Y-m-d h:i:s');
            $issue_date =  date('Y-m-d h:i:s');
            $due_date = date('Y-m-d h:i:s', strtotime('+ 8 days'));
            $e_charges = $bill;
            $e_arrears = 0;
            $i_tax = 0;
            $gst = 0;
            $e_tax = 0;
            $f_tax = 0;
            $r_g_s_t = 0;
            $e_total_amt = $e_charges + $e_arrears + $i_tax + $gst + $e_tax + $f_tax + $r_g_s_t;
            $e_amt_w_due_date = $e_total_amt;
            $e_amt_a_due_date = $e_amt_w_due_date + 300;
            $c_duty = 0;
            $rd_maint = 0;
            $mosque = 0;
            $conserve = 0;
            $sewerage = 0;
            $misc = 0;
            $sanitation = 0;
            $tv_fee = 0;
            $rent = 0;
            $rent_arrear = 0;
            $ot_amt_w_due_date = $c_duty + $mosque  + $rd_maint
                + $conserve + $sewerage + $misc + $sanitation + $tv_fee + $rent + $rent_arrear;
            $ot_amt_a_due_date = $ot_amt_w_due_date;
            $f_mrc = 0;
            $f_mrc_arrear = 0;
            $water_charge = 0;
            $water_arrear = 0;
            $wat_total_amt = $f_mrc + $f_mrc_arrear + $water_charge ;
            $wat_amt_w_due_date =$wat_total_amt;
            $wat_amt_a_due_date = $wat_amt_w_due_date + 10;
            $g_total_w_due_date = $e_amt_w_due_date + $ot_amt_w_due_date +$wat_amt_w_due_date;
            $g_total_a_due_date = $e_amt_a_due_date + $ot_amt_a_due_date + $wat_amt_a_due_date;

            $m = new MdcBillDetails;

            $m->t_id = $tariff_id;
            $m->t_name = $tariff_name;
            $m->consumer_no = $consumer_no;
            $m->consumer_name = $consumer_name;
            $m->consumer_address = $consumer_address;
            $m->meter_no = $msn;
            $m->meter_type =$meter_type;
            $m->prev_read = $prev_read;
            $m->curr_read = $current_read;
            $m->read_diff = $read_diff;
            $m->avg_units = $avg_units;
            $m->total_units = $avg_units + $read_diff;
            $m->bill_month = $bill_month;
            $m->m_r_date = $m_read_date;
            $m->issue_date = $issue_date;
            $m->due_date = $due_date;
            $m->e_charges = $e_charges;
            $m->e_arrears = $e_arrears;
            $m->i_tax = $i_tax;
            $m->gst = $gst;
            $m->e_tax = $e_tax;
            $m->f_tax = $f_tax;
            $m->r_g_s_t = $r_g_s_t;
            $m->e_total_amt = $e_total_amt;
            $m->e_amt_w_due_date = $e_amt_w_due_date;
            $m->e_amt_a_due_date = $e_amt_a_due_date;
            $m->c_duty = $c_duty;
            $m->rd_maint = $rd_maint;
            $m->mosque = $mosque;
            $m->conserve = $conserve;
            $m->sewerage = $sewerage;
            $m->misc = $misc;
            $m->sanitation = $sanitation;
            $m->tv_fee = $tv_fee;
            $m->rent = $rent;
            $m->rent_arrear = $rent_arrear;
            $m->ot_amt_w_due_date = $ot_amt_w_due_date;
            $m->ot_amt_a_due_date = $ot_amt_a_due_date;
            $m->f_mrc = $f_mrc;
            $m->f_mrc_arrear = $f_mrc_arrear;
            $m->water_charge = $water_charge;
            $m->water_arrear = $water_arrear;
            $m->wat_total_amt = $wat_total_amt;
            $m->wat_amt_w_due_date = $wat_amt_w_due_date;
            $m->wat_amt_a_due_date = $wat_amt_a_due_date;
            $m->g_total_w_due_date = $g_total_w_due_date;
            $m->g_total_a_due_date = $g_total_a_due_date;


            if($m->save(false))
            {
                $hist = new MdcBillHist;
                $hist->month_year = date('Y-m-d h:i:s');
                $hist->units = $current_read;
                $hist->bill_amt = $bill;
                $hist->payment = 0;
                $hist->customer_id = $consumer_no;
                $hist->save(false);

            }

        }
    }
    else{
        print_r('no data available');
        exit();
    }

}
public static function billCalc($input, $slabs)
{
    $result = [];
    $bill = 0;
    $previous_slab = 0;

    foreach($slabs as $slab => $rate)
    {
        if($slab=='')
            $slab = PHP_INT_MAX;
        // calculate distance between current and previous slab
        $slab_distance = $slab - $previous_slab;
        // if current remainder of input value is >= distance, add distance to result,
        // and subtract distance from remainder of input
        if( $input >= $slab_distance )
        {
            $result[] = $slab_distance;
            $bill += $slab_distance * $rate;
            $input -= $slab_distance;
        }// otherwise, add remainder as last item of result, and break out of the loop here
        else
        {
            $result[] = $input;
            $bill += $input * $rate;
            break;
        }
        $previous_slab = $slab;
    }
    return $bill;
}

I don't know what is the main issue as I have tried to check the issue but couldn't find the solution.

Moeez
  • 494
  • 9
  • 55
  • 147

1 Answers1

2

I can only guess (because you are not showing your configuration) that you have your console application configured with response component like:

'components' => [
    'response' => [
        'formatters' => [/* some stuff here */],
        // ...other settings
    ],
],

This is a mistake, most probably caused by copy-pasting the config for web application where this makes sense because web applications automatically wire yii\web\Response (which has formatters option available) and console is using yii\console\Response (which doesn't have formatters). If this is the case - just remove the unnecessary configuration. If you are using the same config for both web and console you must separate it.

Bizley
  • 17,392
  • 5
  • 49
  • 59
  • Ok. I have added the `response` part as well. Kindly have a look – Moeez Aug 04 '20 at 08:52
  • Well, yes, exactly what I said. You cannot set `formatters` for console default response component. – Bizley Aug 04 '20 at 09:08
  • Ok so how can I set it then? – Moeez Aug 04 '20 at 09:23
  • 1
    This is a different question, ask it separately. I have answered your question about why you are getting the error. Console application cannot return the response as pdf but you can for example save the file and grab it later on. – Bizley Aug 04 '20 at 09:29
  • I have added a new [question](https://stackoverflow.com/q/63258051/8919244) – Moeez Aug 05 '20 at 03:38