/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Suspense::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'customer_id' => $this->customer_id, 'timestamp' => $this->timestamp]);
     $query->andFilterWhere(['like', 'mpesa_id', $this->mpesa_id])->andFilterWhere(['like', 'original', $this->original])->andFilterWhere(['like', 'destination', $this->destination])->andFilterWhere(['like', 'test', $this->test])->andFilterWhere(['like', 'mpesa_code', $this->mpesa_code])->andFilterWhere(['like', 'mpesa_acc', $this->mpesa_acc])->andFilterWhere(['like', 'mpesa_msidn', $this->mpesa_msidn])->andFilterWhere(['like', 'mpesa_amount', $this->mpesa_amount])->andFilterWhere(['like', 'mpesa_sender', $this->mpesa_sender])->andFilterWhere(['like', 'actual_uri', $this->actual_uri]);
     return $dataProvider;
 }
 /**
  * Finds the Suspense model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Suspense the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Suspense::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 public function actionCollection()
 {
     //                  TEST URL
     //test/mpesa/?id=152352061&orig=MPESA&dest=254702170201&tstamp=2015-09-03+10%3A41%3A35&text=JI33ZTR59X+Confirmed.+on+3%2F9%2F15+at+10%3A42+AM+Ksh3%2C000.00+received+from+MARK+CHIIRA+254722294820.++Account+Number+0200000656031+New+Utility+balance+is+Ksh3&customer_id=7759&user=default&pass=default&routemethod_id=2&routemethod_name=HTTP&mpesa_code=JI33ZTR59X&mpesa_acc=0200000656031&mpesa_msisdn=254722294820&mpesa_trx_date=3%2F9%2F15&mpesa_trx_time=10%3A42+AM&mpesa_amt=3000.0&mpesa_sender=MARK+CHIIRA&business_number=332500
     $payment = new Payments();
     $payment->mpesa_id = $_REQUEST['id'];
     $payment->original = $_REQUEST['orig'];
     $payment->destination = $_REQUEST['dest'];
     $payment->customer_id = $_REQUEST['customer_id'];
     $payment->test = $_REQUEST['text'];
     $payment->mpesa_code = $_REQUEST['mpesa_code'];
     $payment->mpesa_acc = $_REQUEST['mpesa_acc'];
     $payment->mpesa_msidn = $_REQUEST['mpesa_msisdn'];
     $payment->mpesa_amount = $_REQUEST['mpesa_amt'];
     $payment->mpesa_sender = $_REQUEST['mpesa_sender'];
     $payment->timestamp = $_REQUEST['tstamp'];
     try {
         //FIND CUSTOMER RECORDS
         $customer = Clients::findOne(['clientAcc' => $payment->mpesa_acc]);
         //HOW MUCH MONEY CLIENTS SHOULD PAY A MONTH
         $billing = Bills::findOne(['id' => 1]);
         //IF NO CLIENT FOUND, SUBMIT THIS TO SUSPENSE ACCOUNT
         if (count($customer) >= 1) {
             // IF A CLIENT IS FOUND, THEN DO THE FOLLOWING AND SAVE
             $payment->save();
             //IS THE CLIENT ACTIVE OR INACTIVE? BASED ON THE PROFILE
             $customer_radreply = Radreply::findOne(['username' => $customer->username]);
             //print_r($customer_radgroup);
             //COLLECT ALL RADGROUPS
             if ($payment->mpesa_amount >= $billing->monthly_charge) {
                 //IF USER WAS DISCONNECTED, AND HAS PAID MORE THAN THE BILLING AMOUNT, SAY: 3000, THEN ACTIVATE THAT USER
                 if (count($customer_radreply) >= 1) {
                     if ($customer_radreply->username == $customer->username) {
                         try {
                             //Yii::$app->db->createCommand()->delete('radusergroup', ['username' => $customer->username])->execute();
                             if ($customer_radreply->delete()) {
                                 echo "Deleted";
                             }
                         } catch (Exception $e) {
                         }
                     }
                 }
                 if ($customer->arrears < 0 && $customer->balances >= $billing->monthly_charge) {
                     $customer->arrears += $payment->mpesa_amount;
                     if ($customer->arrears > 0) {
                         $customer->balances += $customer->arrears;
                     }
                 } elseif ($customer->arrears < 0 && $customer->balances < $billing->monthly_charge) {
                     $difference = $billing->monthly_charge - $customer->balances;
                     $topup = $billing->monthly_charge - $customer->balances;
                     //$customer->balances = 2500;
                     //$billing->monthly_charge = 3000;
                     //$difference = 1000;
                     //$payment->mpesa_amount = 2000
                     if ($payment->mpesa_amount > $topup) {
                         $remainder = $payment->mpesa_amount - $topup;
                         $customer->balances += $billing->monthly_charge;
                         $customer->arrears += $remainder;
                         if ($customer->arrears > 0) {
                             $customer->balances += $customer->arrears;
                         }
                     } else {
                         $customer->balances += $payment->mpesa_amount;
                     }
                 } else {
                     $customer->balances += $payment->mpesa_amount;
                 }
             } else {
                 if ($customer->balances >= $billing->monthly_charge && $customer->arrears < 0) {
                     //$customer->balances = 2000;
                     //$billing->monthly_charge = 3000;
                     //$difference = 1000;
                     //$payment->mpesa_amount = 2000
                     $customer->arrears += $payment->mpesa_amount;
                     if ($customer->arrears > 0) {
                         $customer->balances += $customer->arrears;
                     }
                 } elseif ($customer->balances <= $billing->monthly_charge && $customer->arrears < 0) {
                     if ($payment->mpesa_amount + $customer->balances > $billing->monthly_charge) {
                         $topup = $billing->monthly_charge - $customer->balances;
                         $customer->balances += $topup;
                         //
                         $excess = $payment->mpesa_amount - $topup;
                         $customer->arrears += $excess;
                     } else {
                         $customer->balances += $payment->mpesa_amount;
                     }
                 }
                 if ($customer->balances >= $billing->monthly_charge && count($customer_radgroup) >= 1) {
                     if ($customer_radgroup->groupname == "daloRADIUS-Disabled-Users") {
                         try {
                             //Yii::$app->db->createCommand()->delete('radusergroup', ['username' => $customer->username])->execute();
                             if ($customer_radgroup->delete()) {
                                 echo "Deleted";
                             }
                         } catch (Exception $e) {
                         }
                     }
                 }
             }
             try {
                 $customer->balances = (string) $customer->balances;
                 $customer->arrears = (string) $customer->arrears;
                 $customer->save();
             } catch (Exception $e) {
             }
         } elseif (empty($customer)) {
             $suspense = new Suspense();
             $suspense->mpesa_id = $_REQUEST['id'];
             $suspense->original = $_REQUEST['orig'];
             $suspense->destination = $_REQUEST['dest'];
             $suspense->customer_id = $_REQUEST['customer_id'];
             $suspense->test = $_REQUEST['text'];
             $suspense->mpesa_code = $_REQUEST['mpesa_code'];
             $suspense->mpesa_acc = $_REQUEST['mpesa_acc'];
             $suspense->mpesa_msidn = $_REQUEST['mpesa_msisdn'];
             $suspense->mpesa_amount = $_REQUEST['mpesa_amt'];
             $suspense->mpesa_sender = $_REQUEST['mpesa_sender'];
             $suspense->timestamp = $_REQUEST['tstamp'];
             try {
                 if ($suspense->save()) {
                     echo "<hr><pre>";
                     print_r("Records saved in suspense account");
                     echo "</pre>";
                 }
             } catch (Exception $e) {
                 echo "Suspense not saved";
                 var_dump($errors);
                 exit;
             }
         }
         //return $this->render('collection', ["status"=>"Records saved"] );
     } catch (Exception $e) {
         //return $this->render('collection', ["status"=>"Records not saved"] );
     }
     return $this->render('collection');
 }