Subtracting fields from two different tables and updating the value during update in Laravel - mysql

I have two different tables, table_employee and table_leave. table_employee contains fields such as emp_id, paid_vacation(In Hours), used_vacation(in Hours), while table_leave contains id, hours_spent(in Hours). please how can i deduct the hours_spent in the table_leave from used from the paid_vacation in the table_employee and then update the used_vacation in table_employee with the new value
for instance. paid_vacation =150 hours, used_vacation = 0, hours_spent= 8 hours, with this, this used_vacation should be updated to 150 -8 hours = 142 hours left. I want deduct and update back in the table.
private function saveEmployeeLeave(Request $request, $id=null){
if($id != NULL){
$employeeId = DB::table('as_tbl_employee_master')
->where('employee_id', $request->employee_id)
->get();
}
$employee_id = $request->employee_id;
$paid_vacation = $request->paid_vacation;
$used_vacation = $request->used_vacation;
$empData = new AsEmployee();
if($id !== null) {
$empData = AsEmployee::find($id);
if($empData == null) {
$errors = array('Id: ' . $id . ' not exist');
return json_encode([], 400, 'Failed', $errors);
}
}
$empData->employee_id = $employee_id;
$empData->paid_vacation = $paid_vacation;
$empData->used_vacation = $used_vacation;
$empData->save();
// $emp_id = $request->emp_id;
$emp_id = $request->emp_id;
$dept = $request->department_id;
$english_name = $request->english_name;
$department = $request->department;
$selectedLeaveType = $request->selectedLeaveType;
$application_time = date('Y-m-d H:i:s');
$date_from = $request->date_from;
$date_to = $request->date_to;
$comment = $request->comment;
$contact = $request->contact;
$hours = $request->hours;
$approval_emp_id = $request->reports_to;
$manager = $request->manager;
$status = $request->status;
$empInfo = new AsEmployeeLeave();
if($id !== null) {
$empInfo = AsEmployeeLeave::find($id);
if($empInfo == null) {
$errors = array('Id: ' . $id . ' not exist');
return json_encode([], 400, 'Failed', $errors);
}
}
$empInfo->emp_id = $emp_id;
$empInfo->department_id = $dept;
$empInfo->application_time = $application_time;
$empInfo->type = $selectedLeaveType;
$empInfo->date_from = $date_from;
$empInfo->date_to = $date_to;
$empInfo->comment = $comment;
$empInfo->requester_emp_id = $emp_id;
$empInfo->requester_sign = $english_name;
$empInfo->approval_emp_id = $approval_emp_id;
$empInfo->approval_sign = $manager;
$empInfo->contact = $contact;
$empInfo->hours = $hours;
$empInfo->status = $status;
$empInfo->save();
return $empData;
}
I called it inside this function
public function addEmpLeave(Request $request){
$responseData = $this->saveEmployeeLeave($request);
return ($responseData instanceof JsonResponse) ? $responseData : $this->sendResponse($responseData);
}

Try this :
private function saveEmployeeLeave(Request $request, $id=null){
if($id != NULL){
$employeeId = DB::table('as_tbl_employee_master')
->where('employee_id', $request->employee_id)
->get();
}
$employee_id = $request->employee_id;
$paid_vacation = $request->paid_vacation;
$used_vacation = $request->used_vacation;
$empData = new AsEmployee();
if($id !== null) {
$empData = AsEmployee::find($id);
if($empData == null) {
$errors = array('Id: ' . $id . ' not exist');
return json_encode([], 400, 'Failed', $errors);
}
}
$empData->employee_id = $employee_id;
$empData->paid_vacation = $paid_vacation;
$empData->used_vacation = $used_vacation;
$empData->save();
// $emp_id = $request->emp_id;
$emp_id = $request->emp_id;
$dept = $request->department_id;
$english_name = $request->english_name;
$department = $request->department;
$selectedLeaveType = $request->selectedLeaveType;
$application_time = date('Y-m-d H:i:s');
$date_from = $request->date_from;
$date_to = $request->date_to;
$comment = $request->comment;
$contact = $request->contact;
$hours = $request->hours;
$approval_emp_id = $request->reports_to;
$manager = $request->manager;
$status = $request->status;
$empInfo = new AsEmployeeLeave();
if($id !== null) {
$empInfo = AsEmployeeLeave::find($id);
if($empInfo == null) {
$errors = array('Id: ' . $id . ' not exist');
return json_encode([], 400, 'Failed', $errors);
}
}
$empInfo->emp_id = $emp_id;
$empInfo->department_id = $dept;
$empInfo->application_time = $application_time;
$empInfo->type = $selectedLeaveType;
$empInfo->date_from = $date_from;
$empInfo->date_to = $date_to;
$empInfo->comment = $comment;
$empInfo->requester_emp_id = $emp_id;
$empInfo->requester_sign = $english_name;
$empInfo->approval_emp_id = $approval_emp_id;
$empInfo->approval_sign = $manager;
$empInfo->contact = $contact;
$empInfo->hours = $hours;
$empInfo->status = $status;
$empInfo->save();
$empData = AsEmployee::find($empData->id);
$empData->used_vacation = (int)$paid_vacation - (int)$hours;
$empData->save();
return $empData;
}

Related

How to use loop for following Laravel code?

$query1 = User::where('sponser_id',$_SESSION['ruserid'])->get('userid');
$count1= count($query1);
$query2 = User::whereIn('sponser_id',$query1)->get('userid');
$count2= count($query2);
$query3 = User::whereIn('sponser_id',$query2)->get('userid');
$count3= count($query3);
$query4 = User::whereIn('sponser_id',$query3)->get('userid');
$count4= count($query4);
$query5 = User::whereIn('sponser_id',$query4)->get('userid');
$count5= count($query5);
$query6 = User::whereIn('sponser_id',$query5)->get('userid');
$count6= count($query6);
$query7 = User::whereIn('sponser_id',$query6)->get('userid');
$count7= count($query7);
$allcount=$count1+$count2+$count3+$count4+$count5+$count6+$count7;
I want to calculate the total downlines of users. This code works fine. But How to simplifies this code using a loop?
$allcount = 0;
$query1 = User::where('sponser_id',$_SESSION['ruserid'])->get('userid');
$count1 = count($query1);
$allcount = $allcount + $count1;
for ($i=2; $i<8; $i++) {
if ($i>2){
$j = $i-1;
${"query".$j} = User::whereIn('sponser_id',${"query".($j-1)})->get('userid');
}
${"query".$i} = User::whereIn('sponser_id',${"query".($i-1)})->get('userid');
${"count".$i} = count(${"query".$i});
$allcount = $allcount + ${"count".$i};
}
return $allcount;

Cakephp 3 how do i save individual date in database

I have leave request form.If users takes 2/3 days leaves, then the leave date should be stored into the database separately.I have applied for 2 days, suppose i have selected the date 20-10-2019 and 21-10-2019 for leave request. But it stored into database only one entry. date_form and date_to are my column names.
I want to separate the dates. Example : date_from: 20-10-2019 date_to : 20-10-2019 and date_from: 21-10-2019 date_to : 21-10-2019.
But now it stored date_from : 20-10-2019 date_to : 21-10-2019. Can anyone help me on this?
I have tried some code:
else if($leaveRequest->no_of_days > 1 && $leaveRequest->no_of_days < 7)
{
for($i = 0 ; $i < $leaveRequest->no_of_days; $i++)
{
$day = $i. ' day';
$var = $this->EmpLeaves->newEntity();
if($i == 0)
{
$var->date_from = date('Y-m-d', strtotime($this->request->getData(['date_from'])));
$var->date_to = date('Y-m-d', strtotime($this->request->getData(['date_from'])));
}
else
{
$var->date_from = date('Y-m-d', strtotime($day,strtotime($this->request->getData(['date_from']))));
$var->date_to = date('Y-m-d', strtotime($day,strtotime($this->request->getData(['date_from']))));
}
$var->employee_id = $this->Auth->user('id');
$var->employee_name = $this->request->getData(['employee_name']);
$var->reporting_managerId = $this->request->getData(['reporting_managerId']);
$var->leave_type = $this->request->getData(['leave_type']);
// $var->no_of_days = $this->request->getData(['no_of_days']);
$var->no_of_days = 1;
$var->half_day = $this->request->getData(['half_day']);
$var->leave_reason = $this->request->getData(['leave_reason']);
$var->reliever = $this->request->getData(['reliever']);
$valueSaved = $this->EmpLeaves->save($var);
}
if($valueSaved)
{
//newcode for sending email
$applierEmail = $this->Auth->user('office_email');
$applierName = $this->Auth->user('first_name') . ' ' . $this->Auth->user('last_name');
}
else
{
$this->Flash->error(__('Please check the message'));
}
}
else if($leaveRequest->no_of_days > 1 && $leaveRequest->no_of_days < 7)
{
for($i = 0 ; $i < $leaveRequest->no_of_days; $i++)
{
$day = $i. ' day';
$var = $this->EmpLeaves->newEntity();
else
$var->date_from = date('Y-m-d', strtotime($day,strtotime($this->request->getData(['date_from']))));
$var->date_to = date('Y-m-d', strtotime($day,strtotime($this->request->getData(['date_to']))));
$var->employee_id = $this->Auth->user('id');
$var->employee_name = $this->request->getData(['employee_name']);
$var->reporting_managerId = $this->request->getData(['reporting_managerId']);
$var->leave_type = $this->request->getData(['leave_type']);
$var->no_of_days = $this->request->getData(['no_of_days']);
$var->half_day = $this->request->getData(['half_day']);
$var->leave_reason = $this->request->getData(['leave_reason']);
$var->reliever = $this->request->getData(['reliever']);
$valueSaved = $this->EmpLeaves->save($var);
}
if($valueSaved)
{
//newcode for sending email
$applierEmail = $this->Auth->user('office_email');
$applierName = $this->Auth->user('first_name') . ' ' . $this->Auth->user('last_name');
}
else
{
$this->Flash->error(__('Please check the message'));
}
}

Woocommerce Subscriptions update next payment date from functions.php

I would like to change the next payment date of active subscriptions after the Initial (first) Subscription payment is completed after the trial period.
<?php global $woocommerce;
$all_meta_for_user = get_user_meta( 53 );
$order = new WC_Order(4843);
$order_id=4846;//PUT YOUR ORDER ID HERE
$subscription = wcs_get_subscription( 4846 );
add_action('woocommerce_subscription_payment_complete', 'nextpaymentdatechange', 10, 1);
function nextpaymentdatechange($order_id){
if (WC_Subscriptions_Order::order_contains_subscription($order_id)) {
$nextdate = get_post_meta( $order_id, '_schedule_next_payment', true );
$threedays_ago = date('Y-m-d H:i:s', strtotime('-15 days', strtotime($nextdate)));
update_post_meta($order_id , '_schedule_next_payment', $threedays_ago);
}
} ?>
I got a solution for the subscription-related question which posted. It may help others.
function pro_subscription($subscription){
$sid = $subscription->id;
$uid = $subscription->customer_user;
$subscription = wcs_get_subscription( $sid );
$items = $subscription->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$order_id = $subscription->get_parent_id();
}
$subscription_key = WC_Subscriptions_Manager::get_subscription_key( $order_id, $product_id);
$td = new DateTime($subscription->get_date( 'trial_end_date' ));
$try_date = $td->format('Y-m-d');
$sd = new DateTime($subscription->get_date( 'next_payment_date' ));
$pay_date = $sd->format('Y-m-d');
$sdt = new DateTime($subscription->get_date( 'start_date' ));
$sst = $sd->format('Y-m-d H:i:s');
$tst = $td->format('Y-m-d H:i:s');
$date1=date_create($tst);
$date2=date_create($sst);
$diff=date_diff($date1,$date2);
$df = 30 - $diff->format("%a");
$next_pay = date('Y-m-d H:i:s', strtotime($ts. ' + ' . $df . ' days'));
if($try_date == $pay_date){
WC_Subscriptions_Manager::set_next_payment_date( $subscription_key, $uid, $next_pay );
}
}

How to update table in redbeanphp

Here is my function Insert.
What should i write after findOne to UPDATE the table. If field name exist update all other fields.
function insert_data_score($name, $clickcountscore, $levelofitems, $bonuscounter, $date, $timeofcasinobutton, $timeofcasinovideo){
$repeatChecker = R:: findOne('clicker', 'name = ?', array ($name));
if (isset($repeatChecker))
// ????????
return " User $name exist";
$scoreboard = R::xdispense( 'clicker' );
$scoreboard->name = $name;
$scoreboard->clickcountscore = $clickcountscore;
$scoreboard->levelofitems = $levelofitems;
$scoreboard->bonuscounter = $bonuscounter;
$scoreboard->date = $date;
$scoreboard->timeofcasinobutton = $timeofcasinobutton;
R::store($scoreboard);
return "User: $name, created";
$repeatChecker->clickcountscore = $clickcountscore;
$repeatChecker->levelofitems = $levelofitems;
$repeatChecker->bonuscounter = $bonuscounter;
$repeatChecker->date = $date;
$repeatChecker->timeofcasinobutton = $timeofcasinobutton;
$repeatChecker->timeofcasinovideo = $timeofcasinovideo;
R::store($repeatChecker);
return " User $name Updated";
all wokrs if add this code after "if"

Mysql - Slow query output

I got the slowquery log running from my amazon RDS instances and I'm able to retrieve them using a custom script in the "sloquery.log" format.
My question is quite simple, is there any formatting program out there able to make thoses reports more "readable".
Current output is something like this :
Thanks
Yes, mysqldumpslow is available in all standard MySQL distributions.
I knocked up this crude php script to extract the log into CSV format, which means I can then easily manipulate it with a spreadsheet.
You will have to change the input and output locations probably (I just drag the log to my localhost and then run the script against it there).
<?php
set_time_limit(240);
$handle = fopen('C:\\Users\\kwalker\\Downloads\\mysql-slow.log', "rb");
$fp = fopen('file.csv', 'w');
$inline = '';
$inline = fgets($handle, 8192);
$OutLine = array();
$OutLine['Time'] = 'Time';
$OutLine['Timestamp'] = 'Timestamp';
$OutLine['User'] = 'User';
$OutLine['Query_time'] = 'Query_time';
$OutLine['Lock_time'] = 'Lock_time';
$OutLine['Rows_sent'] = 'Rows_sent';
$OutLine['Rows_examined'] = 'Rows_examined';
$OutLine['Database'] = 'Database';
$OutLine['SqlOut'] = 'SqlOut';
WriteOut($fp, $OutLine);
$OutLine = array();
$OutLine['Time'] = '';
$OutLine['Timestamp'] = '';
$OutLine['User'] = '';
$OutLine['Query_time'] = '';
$OutLine['Lock_time'] = '';
$OutLine['Rows_sent'] = '';
$OutLine['Rows_examined'] = '';
$OutLine['Database'] = '';
$OutLine['SqlOut'] = '';
$PossibleUse = true;
$TimeTriggeredOut = true;
$CurrentTime = '';
$CurrentDatabase = '';
while (!feof($handle))
{
switch (true)
{
case substr($inline, 0, 8) == '# Time: ' :
WriteOut($fp, $OutLine);
$PossibleUse = true;
$Timings = explode(': ', $inline);
$CurrentTime = $Timings[1];
$OutLine = array();
$OutLine['Time'] = $CurrentTime;
$OutLine['Timestamp'] = '';
$OutLine['User'] = '';
$OutLine['Query_time'] = '';
$OutLine['Lock_time'] = '';
$OutLine['Rows_sent'] = '';
$OutLine['Rows_examined'] = '';
$OutLine['Database'] = $CurrentDatabase;
$OutLine['SqlOut'] = '';
$TimeTriggeredOut = true;
break;
case substr($inline, 0, 6) == '# User' :
if (!$TimeTriggeredOut)
{
WriteOut($fp, $OutLine);
$PossibleUse = true;
$OutLine = array();
$OutLine['Time'] = $CurrentTime;
$OutLine['Timestamp'] = '';
$OutLine['User'] = '';
$OutLine['Query_time'] = '';
$OutLine['Lock_time'] = '';
$OutLine['Rows_sent'] = '';
$OutLine['Rows_examined'] = '';
$OutLine['Database'] = $CurrentDatabase;
$OutLine['SqlOut'] = '';
}
$OutLine['User'] = $inline;
$TimeTriggeredOut = false;
break;
case substr($inline, 0, 12) == '# Query_time' :
$Timings = explode(' ', $inline);
//print_r($Timings);
$OutLine['Query_time'] = $Timings[2];
$OutLine['Lock_time'] = $Timings[5];
$OutLine['Rows_sent'] = $Timings[7];
$OutLine['Rows_examined'] = $Timings[10];
$PossibleUse = true;
break;
case substr($inline, 0, 14) == 'SET timestamp=' :
$Timings = explode('=', $inline);
$OutLine['Timestamp'] = $Timings[1];
$PossibleUse = true;
break;
case $PossibleUse AND substr($inline, 0, 4) == 'use ' :
$Timings = explode(' ', $inline);
$CurrentDatabase = $Timings[1];
$OutLine['Database'] = $CurrentDatabase;
$PossibleUse = false;
break;
default;
$OutLine['SqlOut'] .= $inline;
}
$inline = fgets($handle, 8192);
}
fclose($fp);
fclose($handle);
function WriteOut($fp, $OutLine)
{
foreach($OutLine as &$aOutLine)
{
$aOutLine = str_replace("\n", " ", $aOutLine);
$aOutLine = str_replace("\r", " ", $aOutLine);
$aOutLine = str_replace("\t", " ", $aOutLine);
}
fputcsv($fp, $OutLine);
}
?>