Here I try to reduce the number of leave limit by the amount of leave 4-12, but every time I want to reduce the amount is always only the previous number is reduced
controller
public function approved($id)
{
$data = array(
'status'=> 'approved'
);
$this->Mcuti->update($id, $data);
$operasi = array(
'jumlah_bc'=> 'jumlah_bc'-'jumlah_cuti'
);
$this->Mcuti->pengurangan($id, $operasi);
redirect('backend/cuti/preview');
}
Model
public function pengurangan($id, $operasi)
{
$this->db->query('UPDATE batas_cuti JOIN cuti ON batas_cuti.nik=cuti.nik SET batas_cuti.jumlah_bc=batas_cuti.jumlah_bc-cuti.jumlah_cuti');
$query = $this->db->where($id);
return $query;
}
Related
I have column total leave in table user and column days taken and status in table leave. I tried to deduct the total leave with the leave days taken when the leave is approved but it is not updated. I've tried with this code.
This function is for the applicant to apply for leave.
public function store(Request $request){
$dateFrom = Carbon::createFromFormat('Y-m-d', $request->date_from);
$dateTo = Carbon::createFromFormat('Y-m-d', $request->date_to);
$days_taken = $dateFrom->diffInDays($dateTo)+1;
if($request->leave_id == 1){
if($days_taken > auth()->user()->total_annual){
return redirect()->route('admin.leaverequest.create')->with('error', 'Leave exceed');
}
}
else{
return redirect()->route('admin.leaverequest.index')
->with('success', 'Leave Application Submitted');
}
Application::create([
'user_id'=>auth()->user()->id,
'leave_id'=>$request->leave_id,
'date_from'=>$request->date_from,
'date_to'=>$request->date_to,
'days_taken'=> $dateFrom->diffInDays($dateTo)+1,
]);
return redirect()->route('admin.leaverequest.index')
->with('success', 'Leave Application Submitted');
}
This is the function for approve/rejecting the leave request.
public function approval(Request $request, $id){
$leaverequest = Application::find($id);
if($leaverequest->status = $request->approve){
$leaverequest->save();
return redirect()->back();
}
elseif($leaverequest->status = $request->reject){
$leaverequest->save();
return redirect()->back();
}
}
This function is for the calculation to deduct the total leave everytime if the leave request is approved. If not, the total leave will stay as it is.
public function leaveBalance(Request $request, $id){
$leaverequest = Application::all();
$days_taken = $request->days_taken;
$annual_balance = User::find($id);
if($request->status == 1){
$annual_balance->total_annual = $annual_balance->total_annual - $days_taken;
$annual_balance->save();
return redirect()->route('home');
}
}
The other functions seems to function well except for the leaveBalance function. I've tried using this code but also didn't work.
auth()->user()->total_annual = auth()->user(->total_annual - $days_taken;
auth()->user()->save();
First of code looks fine , but if you getting error or not updating record then i suggests to debug your code first that all variables have values like $annual_balance = User::find($id); did you get data in this? and if you get all things then try to store $annual_balance->total_annual in one variable and then subtract code from variable.
function leaveBalance(Request $request, $id){
$leaverequest = Application::all();
$days_taken = $request->days_taken;
$annual_balance = User::find($id);
if($request->status == 1){
$total_annual = $annual_balance->total_annual;
$annual_balance->total_annual = $total_annual - $days_taken;
$annual_balance->save();
return redirect()->route('home');
}
}
// Or you can try using DB if above not work don't forget to add Use DB on top of Controller.
$affected = DB::table('users')
->where('id', $id)
->update(['total_annual' => $total_annual]);
You could also try this
I would say return a single total_annual (total_annual refers to the total leave field in the users table) for a certain user from the user table and note the keyword value() in the query which indicates one value.
$annual_balance variable store a total annual leave and $days_taken store the number of leave days taken and do some calculation as below to set the total annual which is the remaining total annual leave after days taken
$annual_balance->total_annual = $annual_balance - $days_taken;
public function leaveBalance(Request $request, $id){
$days_taken = $request->days_taken;
$annual_balance = User::where('user_id', $id)->select('total_annual')->value('total_annual');
if($request->status == 1)
{
$annual_balance->total_annual = $annual_balance - $days_taken;
$annual_balance->save();
return redirect()->route('home');
}
}
I have 3 tables (sales, sales_detail, and bicycle). I don't know how to get back my quantity (sales) to unit balance (bicycle) and then delete the sales_detail entry because I'm going to update new sales.
public function edit(Request $request, $id) {
$sales = Sales::find($id);
$sales_details = SalesDetail::where('sales_id', $id)->get();
$bicycles = Bicycle::where('sales_id', $id)->get();
foreach ($bicycles as $bc && $sales_details as $sd) {
$bc->unit_balance = $sd->quantity + $bc->unit_balance;
//then delete sales_detail
}
return view('sales/edit', array(
'sales' => $sales,
'sales_details' => $sales_details,
'bicycles' => $bicycles
));
}
I suggest the use of Elequents relations in your Models such as 'belongsto' and 'hasmany' to better prepare data. Following this practice would allow you to simplify your queries during development. Your approach is very messy/novice and procedural.
Checkout https://laravel.com/docs/5.8/eloquent-relationships
public function edit(Request $request, $id) {
$sales = Sales::find($id); // get sales where sales_id = 40
$sales_details = SalesDetail::where('sales_id', $id)->get();
return view('sales/edit', array( 'sales' => $sales,
'sales_details' => $sales_details ));
}
public function update(Request $request, $id) {
$sales = Sales::find($id);
$sales_details = SalesDetail::where('sales_id',$id)->get();
foreach ($sales_details as $sales_dtl) {
$bicycle = Bicycle::find($sales_dtl->bicycle_id);
$bicycle->unit_balance = $bicycle->unit_balance + $sales_dtl['quantity'];
$bicycle->save();
$sales_dtl->delete();
}
$this->saveData($sales,$request);
return redirect()->route('sales.index');
}
i get my answer already
I am just new to Codeigniter. I want to display the total sum of bill_rpt of this SQL query in the view page. here's the code
this is my Model
public function total() {
$sql="SELECT bill_rpt
FROM amilyar_bill
WHERE bill_status = 1";
return $this->db->query($sql);
}
this is my Controller
public function payments($token=''){
$this->isLoggedIn();
$data = array(
// get data using email
'token' => $token,
'admin_info' => $this->model->getAdminInfo($this->session->userdata('email'))->row(),
'notifications' => $this->model->notification_admin($this->session->userdata('email'))->result_array(),
'notification' => $this->model->all_notification_admin($this->session->userdata('email'))->result_array(),
'total' => $this->model->total($this->session->userdata('email'))->result_array(),
);
if ($this->session->userdata('position_id') == 2) {
$this->load->view('includes/admin_header',$data);
$this->load->view('admin/reports/payments',$data);
} else {
$this->logout();
}
}
this is my View
<h4 class="pull-right">Total:</h4>
Thanks in advance
Hope this will help you :
Use ci select_sum() to get the sum
Your total method should be like this :
public function total()
{
$this->db->select_sum('bill_rpt');
$this->db->where('bill_status' , '1');
$query = $this->db->get('amilyar_bill');
return $query->row()->bill_rpt;
}
Your $data part in controller payments method should be like this :
$data = array(
'token' => $token,
'admin_info' => $this->model->getAdminInfo($this->session->userdata('email'))->row(),
'notifications' => $this->model->notification_admin($this->session->userdata('email'))->result_array(),
'notification' => $this->model->all_notification_admin($this->session->userdata('email'))->result_array(),
'total' => $this->model->total()
);
In payments view part use $total like this :
<h4 class="pull-right">Total: <?=$total;?></h4>
You can use num_rows() method of Active Class.
$query->num_rows();
public function total(){
$sql="SELECT bill_rpt FROM amilyar_bill WHERE bill_status = 1";
$query = $this->db->query($sql);
return $query->num_rows();
}
And in View,
<h4 class="pull-right">Total: <?php echo $total;?></h4>
Try SUM function of MySql as:
In Model
public function total(){
$sql="SELECT SUM(bill_rpt ) as total_sum
FROM amilyar_bill
WHERE bill_status = 1";
return $this->db->query($sql);
}
it's my controller
function set_hargabesar($id){
$this->load->library('cart');
$condition['id'] = 'id';
$get = $this->myigniter_model->getharga('harga_satuan','id');
$data = array(
'rowid' => $id,
'qty' => 5,
'price' => $get,
);
$this->cart->update_all($data);
}
it's my model
public function getharga($harga, $id){
$this->db->select($harga);
$this->db->from('barang');
$this->db->where('id',$id);
$query=$this->db->get();
}
How can I get a 'harga_satuan' (price cart) from database
please help me.
I can get the data(harga_satuan) from database with this code.
You've only executed your query, you need to retrieve data.
Model:
public function getharga($harga, $id){
$this->db->select($harga);
$this->db->from('barang');
$this->db->where('id',$id);
$query=$this->db->get();
$row = $query->row_array();
return $row;
}
In your controller just change this:
'price' => $get['harga_satuan'],
And, of course, you need to pass id to your model:
$get = $this->myigniter_model->getharga('harga_satuan', $id);
(but I hope you are doing it)
I've recently been using the controller to pass in a couple of values into my view. In my controller this is what it looks like when I pass in values from the controller to the view.
public function actionProcessPayment($id)
{
// if the account id was passed in
if(isset($_POST['listF']))
{
$account_id = $_POST['listF'];
// total due before payment
$totaldue = Recipient::model()->totaldue($id);
$result = Recipient::model()->MakePayment($id, $account_id);
if($result == 'not enough money')
{
Yii::app()->user->setFlash('not enough money', "This account does not have enough money
to make that transaction!");
$this->redirect(array('recipient/index', 'id' => $id));
}
else
$this->render('paysummary',array(
'id'=>$id,
'totaldue'=>$totaldue,
'numpeople'=>Paylist::model()->NumIndv($id),
'user_id'=>Login::model()->getUserId(),
'accountname'=>Account::model()->AccountName($account_id),
'accountbalance'=>Account::model()->AccountBalance($account_id),
''
));
}
Now, in order to get the account name (for example), I've created a function in the model called AccountName that takes in the account id as a parameter.
Like so
public function AccountName($id)
{
$model = Account::model()->findByPk($id);
return $name = $model->name;
}
This is working fine, but I feel like I am programming in a very roundabout way. Is there a way to change this to be one line of code? Is this how it should be called in the controller?
public function actionProcessPayment($id)
{
// if the account id was passed in
if(isset($_POST['listF']))
{
$account_id = $_POST['listF'];
// total due before payment
$totaldue = Recipient::model()->totaldue($id);
$result = Recipient::model()->MakePayment($id, $account_id);
$model = Account::model()->findByPk( $account_id );// <<---
if($result == 'not enough money')
{
Yii::app()->user->setFlash('not enough money', "This account does not have enough money
to make that transaction!");
$this->redirect(array('recipient/index', 'id' => $id));
}
else
$this->render('paysummary',array(
'id'=>$id,
'totaldue'=>$totaldue,
'numpeople'=>Paylist::model()->NumIndv($id),
'user_id'=>Login::model()->getUserId(),
'accountname'=>$model->name,// <<---
'accountbalance'=>$model->balance, // <<---
''
));
}
You can simply pass model to view as a param:
$account = Account::model()->findByPk($account_id);
$this->render('paysummary',array(
'id'=>$id,
'totaldue'=>$totaldue,
'numpeople'=>Paylist::model()->NumIndv($id),
'user_id'=>Login::model()->getUserId(),
'account'=>$account,
''
));
Then in the view:
<?php echo $account->name; ?>