How to perform operation based on when user hass changed a value in text box in Yii2


(Jamess) #1

Hi,
I am using Yii2 basic.

Now when I update a record, I enter Loan Given and click update. It gets updated. Now when second time i click on update for same record and whether I change some values or not , TotalValueofLoanGive , LoanRepaidUptilNow and TotalValueOfLoanOutstanding fields are getting changed as I have applied condition. This condition is necessary. How should I do?

How should I detect if textbox content has changed. If it is changed then only it should consider for operation else not

I have below action Update.

public function actionUpdate($id)
    {
        $model = $this->findModel($id);

		if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
    Yii::$app->response->format = Response::FORMAT_JSON;
    return ActiveForm::validate($model);
		}
		if ($model->load(Yii::$app->request->post())) {
			$count = Yii::$app->db->createCommand('SELECT count(*) FROM groupsavingdetails WHERE 
groupsavingdetails.GroupId=:Id 
and ( Year > :year or (Year>=:year and Month > :month) )
    ')

->bindValues([

    ':Id'=>$model->GroupId,':year'=>$model->Year,':month'=>$model->Month
])
	 
 ->queryAll();


 if($count==0)
			{
$groups  = Groupsavingdetails::find()
        ->andWhere(['and',
            ["GroupId" => $model->GroupId],
            ['>=', "Year", $model->Year],
            ['>', "Month", $model->Month],
        ])
        ->orWhere(['and',
            ["GroupId" => $model->GroupId],
            ['>', "Year", $model->Year],
            ['>', "Month",0],
        ])->all(); 
foreach ($groups as $k => $group) {

    if($k==0)
	{
	$group->OpeningBalance = $model->ClosingBalance;
        $group->TotalValueofLoanGiven = $model->TotalValueofLoanGiven;
        $group->LoanRepaidUptilNow = $model->LoanRepaidUptilNow;
        $group->TotalValueOfLoanOutstanding = $model->TotalValueOfLoanOutstanding;
		if($group->LoanGiven!=0)
				{
$group->TotalValueofLoanGiven+=$group->LoanGiven;

}



if($group->LoanRecovery!=0)
				{
$group->LoanRepaidUptilNow+=$group->LoanRecovery;

}


$group->TotalValueOfLoanOutstanding=$group->TotalValueofLoanGiven-$group->LoanRepaidUptilNow;

$group->ClosingBalance=($group->OpeningBalance+$group->TotalSaving+$group->LoanRecovery+$group->LoanInterest+$group->Fine+$group->BankInterest+$group->BankLoan-$group->Expenses-$group->LoanGiven);

        $group->save();
	
	}
 }
 $model->save();

return $this->redirect(['view', 'id' => $model->GroupSavingDetailsId]);
			}

 if($count==1)
			{
 
if($model->LoanGiven!=0)
				{
$model->TotalValueofLoanGiven+=$model->LoanGiven;

}



if($model->LoanRecovery!=0)
				{
$model->LoanRepaidUptilNow+=$model->LoanRecovery;

}


$model->TotalValueOfLoanOutstanding=$model->TotalValueofLoanGiven-$model->LoanRepaidUptilNow;

$model->ClosingBalance=($model->OpeningBalance+$model->TotalSaving+$model->LoanRecovery+$model->LoanInterest+$model->Fine+$model->BankInterest+$model->BankLoan-$model->Expenses-$model->LoanGiven);

 
 

$groups  = Groupsavingdetails::find()
        ->andWhere(['and',
            ["GroupId" => $model->GroupId],
            ['>=', "Year", $model->Year],
            ['>', "Month", $model->Month],
        ])
        ->orWhere(['and',
            ["GroupId" => $model->GroupId],
            ['>', "Year", $model->Year],
            ['>', "Month",0],
        ])->all();


foreach ($groups as $k => $group) {

    if($k==0)
	{
	$group->OpeningBalance = $model->ClosingBalance;
        $group->TotalValueofLoanGiven = $model->TotalValueofLoanGiven;
        $group->LoanRepaidUptilNow = $model->LoanRepaidUptilNow;
        $group->TotalValueOfLoanOutstanding = $model->TotalValueOfLoanOutstanding;
		if($group->LoanGiven!=0)
				{
$group->TotalValueofLoanGiven+=$group->LoanGiven;

}



if($group->LoanRecovery!=0)
				{
$group->LoanRepaidUptilNow+=$group->LoanRecovery;

}


$group->TotalValueOfLoanOutstanding=$group->TotalValueofLoanGiven-$group->LoanRepaidUptilNow;

$group->ClosingBalance=($group->OpeningBalance+$group->TotalSaving+$group->LoanRecovery+$group->LoanInterest+$group->Fine+$group->BankInterest+$group->BankLoan-$group->Expenses-$group->LoanGiven);

        $group->save();
	
	}

$model->save();

return $this->redirect(['view', 'id' => $model->GroupSavingDetailsId]);
 }
			}

 if($count>1)
			{
	 


if($model->LoanGiven!=0)
				{
$model->TotalValueofLoanGiven+=$model->LoanGiven;

}



if($model->LoanRecovery!=0)
				{
$model->LoanRepaidUptilNow+=$model->LoanRecovery;

}


$model->TotalValueOfLoanOutstanding=$model->TotalValueofLoanGiven-$model->LoanRepaidUptilNow;

$model->ClosingBalance=($model->OpeningBalance+$model->TotalSaving+$model->LoanRecovery+$model->LoanInterest+$model->Fine+$model->BankInterest+$model->BankLoan-$model->Expenses-$model->LoanGiven);

 
 

$groups  = Groupsavingdetails::find()
        ->andWhere(['and',
            ["GroupId" => $model->GroupId],
            ['>=', "Year", $model->Year],
            ['>', "Month", $model->Month],
        ])
        ->orWhere(['and',
            ["GroupId" => $model->GroupId],
            ['>', "Year", $model->Year],
            ['>', "Month",0],
        ])->all();


foreach ($groups as $k => $group) {

    if($k==0)
	{
	$group->OpeningBalance = $model->ClosingBalance;
        $group->TotalValueofLoanGiven = $model->TotalValueofLoanGiven;
        $group->LoanRepaidUptilNow = $model->LoanRepaidUptilNow;
        $group->TotalValueOfLoanOutstanding = $model->TotalValueOfLoanOutstanding;
		if($group->LoanGiven!=0)
				{
$group->TotalValueofLoanGiven+=$group->LoanGiven;

}



if($group->LoanRecovery!=0)
				{
$group->LoanRepaidUptilNow+=$group->LoanRecovery;

}


$group->TotalValueOfLoanOutstanding=$group->TotalValueofLoanGiven-$group->LoanRepaidUptilNow;

$group->ClosingBalance=($group->OpeningBalance+$group->TotalSaving+$group->LoanRecovery+$group->LoanInterest+$group->Fine+$group->BankInterest+$group->BankLoan-$group->Expenses-$group->LoanGiven);

        $group->save();
	
	}


     if($k!=0)
	{
       $group['OpeningBalance'] = $groups[$k-1]->ClosingBalance;
	   $group['TotalValueofLoanGiven'] = $groups[$k-1]->TotalValueofLoanGiven;
	   	   $group['LoanRepaidUptilNow'] = $groups[$k-1]->LoanRepaidUptilNow;
		   	   	   $group['TotalValueOfLoanOutstanding'] = $groups[$k-1]->TotalValueOfLoanOutstanding;

				   if($group->LoanGiven!=0)
				{
$group->TotalValueofLoanGiven+=$group->LoanGiven;

}
if($group->LoanRecovery!=0)
				{
$group->LoanRepaidUptilNow+=$group->LoanRecovery;
				}
$group->TotalValueOfLoanOutstanding=$group->TotalValueofLoanGiven-$group->LoanRepaidUptilNow;


$group->ClosingBalance=($group->OpeningBalance+$group->TotalSaving+$group->LoanRecovery+$group->LoanInterest+$group->Fine+$group->BankInterest+$group->BankLoan-$group->Expenses-$group->LoanGiven);

        $group->save();


       
    }
}





$model->save();

return $this->redirect(['view', 'id' => $model->GroupSavingDetailsId]);
	}
	
 
 }	
		
		else
		{
		 return $this->render('update', [
                'model' => $model,
            ]);
		}
    }

Where should I make changes in following code

if($group->LoanGiven!=0)
{
$group->TotalValueofLoanGiven+=$group->LoanGiven;
}
if($group->LoanRecovery!=0)
{
$group->LoanRepaidUptilNow+=$group->LoanRecovery;
}