mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
206 lines
8.8 KiB
PHP
206 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\Payroll\Http\Controllers;
|
|
|
|
use Barryvdh\Snappy\Facades\SnappyPdf;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Streamline\Models\ChartOfAccount;
|
|
use Streamline\Models\PayrollAllowancesOrDeduction;
|
|
use Streamline\Models\PayrollCategory;
|
|
use Streamline\Models\PayrollSchedule;
|
|
|
|
class PayrollScheduleController extends Controller {
|
|
public function index() {
|
|
$schedules = PayrollSchedule::get();
|
|
|
|
return view('payroll::payroll_schedule.index', compact('schedules'));
|
|
}
|
|
|
|
public function create(Request $request) {
|
|
$employees = [];
|
|
|
|
if (isset($request->payroll_category)) {
|
|
if ($request->payroll_category == 0) {
|
|
$employees = DB::table('payrolls')
|
|
->join('patients', 'payrolls.patient_id', '=', 'patients.id')
|
|
->get(['payrolls.*', 'patients.first_name', 'patients.last_name', 'patients.number']);
|
|
} else {
|
|
$employees = DB::table('payrolls')
|
|
->join('patients', 'payrolls.patient_id', '=', 'patients.id')
|
|
->where('payroll_category', $request->payroll_category)
|
|
->get(['payrolls.*', 'patients.first_name', 'patients.last_name', 'patients.number']);
|
|
}
|
|
}
|
|
|
|
$months = array(
|
|
'-select-', 'January' => 'January', 'February' => 'February', 'March' => 'March',
|
|
'April' => 'April', 'May' => 'May', 'June' => 'June', 'July' => 'July',
|
|
'August' => 'August', 'September' => 'September', 'October' => 'October',
|
|
'November' => 'November', 'December' => 'December'
|
|
);
|
|
|
|
$years = ['' => '-select-'];
|
|
|
|
$end_year = date('Y');
|
|
$start_year = $end_year - 10;
|
|
|
|
// create array with all the years in between
|
|
$years_range = range($end_year, $start_year);
|
|
|
|
// add to years array to be returned to view
|
|
foreach ($years_range as $year) {
|
|
$years[$year] = $year;
|
|
}
|
|
|
|
$payroll_categories = PayrollCategory::pluck('name', 'id')->toArray();
|
|
$payroll_categories = [0 => 'All Categories'] + $payroll_categories;
|
|
|
|
$expense_accounts = ChartOfAccount::where('type', 2)->pluck('name', 'id')->toArray();
|
|
$expense_accounts = ['' => '- select -'] + $expense_accounts;
|
|
|
|
$payable_accounts = ChartOfAccount::whereIn('type', [6, 9])->pluck('name', 'id')->toArray();
|
|
$payable_accounts = ['' => '- select -'] + $payable_accounts;
|
|
|
|
return view('payroll::payroll_schedule.create', compact('employees', 'years', 'months', 'payroll_categories', 'expense_accounts', 'payable_accounts'));
|
|
}
|
|
|
|
public function save_schedule(Request $request) {
|
|
request()->validate([
|
|
'name' => 'required',
|
|
'pay_year' => 'required',
|
|
'pay_month' => 'required',
|
|
'due_date' => 'required',
|
|
'employee_id' => 'required'
|
|
]);
|
|
|
|
$logged_in_user_id = Auth::user()->id;
|
|
|
|
$basic_pay_arr = $request->basic_pay;
|
|
$allowance_arr = $request->allowance;
|
|
$gross_pay_arr = $request->gross_pay;
|
|
$tax_arr = $request->tax;
|
|
$social_security_arr = $request->social_security;
|
|
$deductions_arr = $request->deductions;
|
|
$total_deductions_arr = $request->total_deductions;
|
|
$net_pay_arr = $request->net_pay;
|
|
$employee_id_arr = $request->employee_id;
|
|
$payroll_category_arr = $request->payroll_category;
|
|
$allow_deduct_ids_arr = array_merge($request->allowances_arr, $request->deductions_arr);
|
|
|
|
foreach ($allow_deduct_ids_arr as $records) {
|
|
if ($records) {
|
|
$exploded_records = explode(",", $records);
|
|
foreach ($exploded_records as $record) {
|
|
$allow_deduct = DB::table('payroll_allowances_or_deductions')->where('id', $record)->first();
|
|
|
|
$are_dates_past = !$allow_deduct->recurring_final_date || Carbon::today()->isAfter(Carbon::createFromFormat('Y-m-d', $allow_deduct->recurring_final_date));
|
|
|
|
if ($allow_deduct && ($allow_deduct->recurring == 0 || $are_dates_past)) {
|
|
DB::table('payroll_allowances_or_deductions')->where('id', $record)->update(['billed' => 1]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$schedule = new PayrollSchedule();
|
|
|
|
$schedule->name = $request->name;
|
|
$schedule->pay_year = $request->pay_year;
|
|
$schedule->pay_month = $request->pay_month;
|
|
$schedule->salaries_expense_account = $request->salaries_expense_account;
|
|
$schedule->salaries_payable_account = $request->salaries_payable_account;
|
|
$schedule->tax_expense_account = $request->tax_expense_account;
|
|
$schedule->tax_payable_account = $request->tax_payable_account;
|
|
$schedule->due_date = Carbon::createFromFormat('d/m/Y', $request->due_date)->toDateString();
|
|
$schedule->social_security_expense_account = $request->social_security_expense_account;
|
|
$schedule->social_security_payable_account = $request->social_security_payable_account;
|
|
$schedule->memo = $request->memo;
|
|
$schedule->employee_id = implode(',', $employee_id_arr);
|
|
$schedule->payroll_category = implode(',', $payroll_category_arr);
|
|
$schedule->basic_pay = implode(',', $basic_pay_arr);
|
|
$schedule->allowances = implode(',', $allowance_arr);
|
|
$schedule->gross_pay = implode(',', $gross_pay_arr);
|
|
$schedule->tax_amount = implode(',', $tax_arr);
|
|
$schedule->social_security_amount = implode(',', $social_security_arr);
|
|
$schedule->deductions = implode(',', $deductions_arr);
|
|
$schedule->total_deductions = implode(',', $total_deductions_arr);
|
|
$schedule->net_pay = implode(',', $net_pay_arr);
|
|
$schedule->created_by = $logged_in_user_id;
|
|
$schedule->updated_by = $logged_in_user_id;
|
|
|
|
try {
|
|
$schedule->save();
|
|
flash("Payroll Schedule has been saved")->success();
|
|
return redirect("/payroll_schedules/index");
|
|
} catch (QueryException $e) {
|
|
flash('An error occurred. Please try again later')->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
|
|
public function get_allowance_deductions_details(Request $request) {
|
|
$items = explode(',', $request->ids);
|
|
$records = PayrollAllowancesOrDeduction::whereIn('id', $items)->get();
|
|
$html = '';
|
|
|
|
foreach ($records as $record) {
|
|
$html .= "<tr>";
|
|
|
|
if($record->type == 0) {
|
|
$html .= '<td>' . __('payroll.allowance') . '</td>';
|
|
} else {
|
|
$html .= '<td>' . __('payroll.deduction') . '</td>';
|
|
}
|
|
|
|
$html .= '<td>' . ugandan_shillings($record->amount) . '</td>';
|
|
$html .= '<td>' . ($record->recurring == 0 ? 'No' : 'Yes') . '</td>';
|
|
$html .= '<td>' . ($record->recurring_final_date ? streamline_date($record->recurring_final_date) : 'N/A') . '</td>';
|
|
$html .= '<td>' . $record->reason . '</td>';
|
|
$html .= '<td>' . get_full_name($record->created_by, 'id', 'first_name', 'last_name', 'users') . '</td>';
|
|
$html .= '<td>' . streamline_date_time($record->created_at) . '</td>';
|
|
|
|
$html .= "</tr>";
|
|
}
|
|
|
|
return $html;
|
|
}
|
|
|
|
public function details($id) {
|
|
$schedule = PayrollSchedule::find($id);
|
|
|
|
$payroll_categories = PayrollCategory::pluck('name', 'id')->toArray();
|
|
$expense_accounts = ChartOfAccount::where('type', 2)->pluck('name', 'id')->toArray();
|
|
$payable_accounts = ChartOfAccount::whereIn('type', [6, 9])->pluck('name', 'id')->toArray();
|
|
|
|
return view('payroll::payroll_schedule.details', compact('schedule', 'payroll_categories', 'payable_accounts', 'expense_accounts'));
|
|
}
|
|
|
|
public function print_details($id) {
|
|
$schedule = PayrollSchedule::find($id);
|
|
|
|
$payroll_categories = PayrollCategory::pluck('name', 'id')->toArray();
|
|
$expense_accounts = ChartOfAccount::where('type', 2)->pluck('name', 'id')->toArray();
|
|
$payable_accounts = ChartOfAccount::whereIn('type', [6, 9])->pluck('name', 'id')->toArray();
|
|
|
|
$data = [
|
|
'schedule' => $schedule,
|
|
'payroll_categories' => $payroll_categories,
|
|
'expense_accounts' => $expense_accounts,
|
|
'payable_accounts' => $payable_accounts,
|
|
];
|
|
|
|
$pdf = SnappyPDF::loadView("payroll_schedule.print_details", $data)
|
|
->setOrientation('portrait')
|
|
->setPaper('a4')
|
|
->setOption('margin-bottom', 5)
|
|
->setOption('margin-top', 5)
|
|
->setOption('footer-html', '<i>Printed On ' . date('d-M-Y h:ia') . '</i>');
|
|
|
|
return $pdf->inline('Payroll Schedule Details.pdf');
|
|
}
|
|
}
|