Hello I am using Yii2 basic.
I have a groupsavingdetails table. There are fields as TotalValueOfLoanOutstanding and LoanRecovery.
Now when the TotalValueOfLoanOutstanding is 500 and Loan recovery is 500 then there is error as
Bad Request (#400)
Missing required parameters: id
Following is the model and controller of groupsavingdetails
- model
<?php
namespace app\models;
use Yii;
class Groupsavingdetails extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'groupsavingdetails';
}
public function rules()
{
return [
[['UserId', 'GroupId', 'Year', 'Month', 'OpeningBalance', 'MonthlySaving', 'LoanRecovery', 'LoanInterest', 'Fine', 'BankInterest', 'Expenses', 'LoanGiven', 'BankLoan','TotalSaving', 'TotalValueofLoanGiven', 'LoanRepaidUptilNow', 'TotalValueOfLoanOutstanding', 'CashInHand', 'CashInBank', 'ClosingBalance'], 'required'],
[['UserId', 'GroupId', 'Year', 'Month'], 'integer'],
[['Year'],'validateyear'],
[[ 'Month'], 'integer'],
[['Month'],'validatemonth'],
[['LoanGiven'],'validateloangiven'],
[['LoanRecovery'],'validateloanrecovery'],
[['MonthlySaving'],'validatemonthlysaving'],
[['LoanInterest'],'validateloaninterest'],
[['Fine'],'validatefine'],
[['BankInterest'],'validatebankinterest'],
[['BankLoan'],'validatebankloan'],
[['Expenses'],'validateexpense'],
[['CashInHand'],'validatecashinhand'],
[['CashInBank'],'validatecashinbank'],
[['OpeningBalance', 'MonthlySaving', 'LoanRecovery', 'LoanInterest', 'Fine', 'BankInterest', 'Expenses', 'LoanGiven', 'BankLoan', 'TotalValueofLoanGiven', 'LoanRepaidUptilNow', 'TotalValueOfLoanOutstanding', 'CashInHand', 'CashInBank', 'ClosingBalance'], 'number'],
[['UserId'], 'exist', 'skipOnError' => true, 'targetClass' => Employee::className(), 'targetAttribute' => ['UserId' => 'UserId']],
[['GroupId'], 'exist', 'skipOnError' => true, 'targetClass' => Groupdetails::className(), 'targetAttribute' => ['GroupId' => 'GroupId']],
];
}
public function attributeLabels()
{
return [
'GroupSavingDetailsId' => 'Group Saving Details ID',
'UserId' => 'Emp ID',
'GroupId' => 'Group',
'Year' => 'Year',
'Month' => 'Month',
'OpeningBalance' => 'Opening Balance',
'MonthlySaving' => 'Monthly Saving',
'LoanRecovery' => 'Loan Recovery',
'LoanInterest' => 'Loan Interest',
'Fine' => 'Fine',
'BankInterest' => 'Bank Interest',
'Expenses' => 'Expenses',
'LoanGiven' => 'Loan Given',
'BankLoan' => 'Bank Loan',
'TotalSaving'=>'Total Saving',
'TotalValueofLoanGiven' => 'Total Value of Loan Given',
'LoanRepaidUptilNow' => 'Loan Repaid Uptil Now',
'TotalValueOfLoanOutstanding' => 'Total Value Of Loan Outstanding',
'CashInHand' => 'Cash In Hand',
'CashInBank' => 'Cash In Bank',
'ClosingBalance' => 'Closing Balance',
];
}
public function getEmp()
{
return $this->hasOne(Employee::className(), ['UserId' => 'UserId']);
}
public function getGroup()
{
return $this->hasOne(Groupdetails::className(), ['GroupId' => 'GroupId']);
}
public function validateyear($attribute,$params,$validator)
{
$group = Groupdetails::find()->where(['GroupId' => $this->GroupId])->all();
$year;
if ($group !=null) {
foreach($group as $groups)
{
$year= $groups['FormationDate'];
}if($this->$attribute>date('Y'))
$this->addError($attribute,'Year should not be greater than current year');
$year1= explode('-', $year);
if($this->$attribute<$year1[0])
$this->addError($attribute,'Year should be greater than or = to '.$year1[0]);
}
else
{
$this->addError($attribute,'Such group not there');
}
}
public function validatemonth($attribute, $params, $validator)
{
$group = Groupdetails::find()->where(['GroupId' => $this->GroupId])->all();
$year;
if ($group !=null) {
foreach($group as $groups)
{
$year= $groups['FormationDate'];
}
$year1= explode('-', $year);
if($this->Year==$year1[0]&&$this->$attribute<$year1[1])
$this->addError($attribute,'Month should be greater than or = to '.date("F",strtotime($year)));
}
else
{
$this->addError($attribute,'Such group not there');
}
}
public function validateloangiven($attribute, $params, $validator)
{
$opening_balance=$this->OpeningBalance;
if( $opening_balance==0)
{
if($this->$attribute>0)
$this->addError($attribute,'Loan could not be given');
}
if( $opening_balance!=0)
{
if($this->$attribute> $opening_balance)
$this->addError($attribute,'Loan amount should not be greater than Opening Balance');
}
if($this->$attribute<0)
$this->addError($attribute,'Loan amount should not be negative');
}
public function validateloanrecovery($attribute, $params, $validator)
{
$TotalValueLoanGiven=$this->TotalValueofLoanGiven;
$totalvalueofloanoutstanding=$this->TotalValueOfLoanOutstanding;
if($totalvalueofloanoutstanding==0)
{
if($this->$attribute>0)
$this->addError($attribute,'Loan recovery should be 0');
}
if($this->$attribute>$TotalValueLoanGiven)
$this->addError($attribute,'Loan recovery should not be greater than or equal to total loan given');
if($this->$attribute<0)
$this->addError($attribute,'Loan recovery should not be less than 0');
if($TotalValueLoanGiven==0)
{
if($this->$attribute>0)
$this->addError($attribute,'Loan recovery cannot be given');
}
}
public function validatemonthlysaving($attribute, $params, $validator)
{
if($this->$attribute<0)
$this->addError($attribute,'Monthly saving should not be negative');
}
public function validateloaninterest($attribute, $params, $validator)
{
if($this->$attribute<0)
$this->addError($attribute,'Loan interest cannot be negative');
}
public function validatefine($attribute, $params, $validator)
{
if($this->$attribute<0)
$this->addError($attribute,'Fine not less than 0');
}
public function validatebankinterest($attribute, $params, $validator)
{
if($this->$attribute<0)
$this->addError($attribute,'Bank interest cannot be less than 0');
}
public function validatebankloan($attribute, $params, $validator)
{
if($this->$attribute<0)
$this->addError($attribute,'Bank loan cannot be less than 0');
}
public function validateexpense($attribute, $params, $validator)
{
if($this->$attribute<0)
$this->addError($attribute,'Expenses cannot be less than 0');
}
public function validatecashinhand($attribute, $params, $validator)
{
if($this->$attribute<0)
$this->addError($attribute,'Cash in hand cannot be less than 0');
}
public function validatecashinbank($attribute, $params, $validator)
{
if($this->$attribute<0)
$this->addError($attribute,'Cash In Bank should not be negative');
}
}
- controller actionCreate()
<?php
public function actionCreate()
{
if(\Yii::$app->user->can('creategroupsaving'))
{
$model = new Groupsavingdetails();
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())) {
$record = Yii::$app->db->createCommand('SELECT *
FROM groupsavingdetails
where GroupId=:id
and Year=:year
and Month=:month'
)->bindValues([':id' => $model->GroupId, ':year' => $model->Year, ':month' => $model->Month])
->queryAll();
if ($record != null) {
echo "<script language='javascript'>";
echo "alert('Saving data already exists for group')";
echo "</script>";
return $this->render('create', [
'model' => $model,
]);
} else {
$month = $model->Month;
$count = Yii::$app->db->createCommand('SELECT count(*)
FROM groupsavingdetails
where groupsavingdetails.GroupId=:id
and (groupsavingdetails.Year<=:year OR groupsavingdetails.Year>=:year )'
)->bindValues([':id' => $model->GroupId, ':year' => $model->Year])
->queryScalar();
if ($count == 0) {
$model->OpeningBalance = 0;
$model->TotalValueofLoanGiven = 0;
$model->LoanRepaidUptilNow = 0;
$model->TotalValueOfLoanOutstanding = 0;
$identity = Yii::$app->user->identity->getonlyid();
$model->UserId = $identity;
$model->TotalSaving = ($model->OpeningBalance + $model->MonthlySaving + $model->LoanRecovery + $model->LoanInterest + $model->Fine + $model->BankInterest + $model->BankLoan - $model->Expenses - $model->LoanGiven);
$model->ClosingBalance=$model->TotalSaving;
$value=$model->CashInHand+$model->CashInBank;
if($model->ClosingBalance!=$value)
{
echo "<script language='javascript'>";
echo "alert('Sum of CashInHand and CashInBank are not equal to Closing Balance')";
echo "</script>";
return $this->render('create', [
'model' => $model,
]);
}
else
{
$model->save();
}
}
if ($count > 0) {
$group = Yii::$app->db->createCommand('SELECT *
FROM groupsavingdetails
where groupsavingdetails.GroupId=:id
ORDER BY groupsavingdetails.GroupSavingDetailsId DESC LIMIT 1')
->bindValues([
':id' => $model->GroupId,
])
->queryAll();
$year2 = 0;
$month3 = 0;
foreach ($group as $groups) {
$model->OpeningBalance = $groups['ClosingBalance'];
$model->TotalValueofLoanGiven = $groups['TotalValueofLoanGiven'];
$model->LoanRepaidUptilNow = $groups['LoanRepaidUptilNow'];
$model->TotalValueOfLoanOutstanding = $groups['TotalValueOfLoanOutstanding'];
$year2 = $groups['Year'];
$month3 = $groups['Month'];
}
$identity = \Yii::$app->user->identity->getonlyid();
$model->UserId = $identity;
if ($model->LoanGiven != 0) {
$model->TotalValueofLoanGiven += $model->LoanGiven;
}
if ($model->LoanRecovery != 0) {
$model->LoanRepaidUptilNow += $model->LoanRecovery;
}
$model->TotalValueOfLoanOutstanding = $model->TotalValueofLoanGiven - $model->LoanRepaidUptilNow;
if ($model->Year == $year2) {
if ($model->Month > $month3) {
$model->TotalSaving = ($model->OpeningBalance + $model->MonthlySaving + $model->LoanRecovery + $model->LoanInterest + $model->Fine + $model->BankInterest + $model->BankLoan - $model->Expenses - $model->LoanGiven);
$model->ClosingBalance=$model->TotalSaving;
$value=$model->CashInHand+$model->CashInBank;
if($model->ClosingBalance!=$value)
{
echo "<script language='javascript'>";
echo "alert('Sum of CashInHand and CashInBank are not equal to Closing Balance')";
echo "</script>";
return $this->render('create', [
'model' => $model,
]);
}
else
{
$model->save();
}
} else {
echo "<script language='javascript'>";
echo "alert('Records greater than selected month exists.')";
echo "</script>";
return $this->render('create', [
'model' => $model,
]);
}
}
if ($model->Year > $year2) {
$month3 = 0;
if ($model->Month > $month3) {
$model->TotalSaving = ($model->OpeningBalance + $model->MonthlySaving + $model->LoanRecovery + $model->LoanInterest + $model->Fine + $model->BankInterest + $model->BankLoan - $model->Expenses - $model->LoanGiven);
$model->ClosingBalance=$model->TotalSaving;
$value=$model->CashInHand+$model->CashInBank;
if($model->ClosingBalance!=$value)
{
echo "<script language='javascript'>";
echo "alert(' Sum of CashInHand and CashInBank are not equal to Closing Balance')";
echo "</script>";
return $this->render('create', [
'model' => $model,
]);
}
else
{
$model->save();
}
} else {
echo "<script language='javascript'>";
echo "alert('Records greater than selected month exists.')";
echo "</script>";
return $this->render('create', [
'model' => $model,
]);
}
}
if ($model->Year < $year2) {
echo "<script language='javascript'>";
echo "alert('Records greater than entered year exists.')";
echo "</script>";
return $this->render('create', [
'model' => $model,
]);
}
}
return $this->redirect(['view', 'id' => $model->GroupSavingDetailsId]);
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
else
{
throw new ForbiddenHttpException("You have no access to create groupsaving data");
}
}