CasperSecurity
<?php
namespace App\Http\Livewire\Finance;
use App\Models\FinancialTransaction;
use App\Models\FinancialTransactionTemp;
use App\Models\FinancialTransactionLedgerHead;
use App\Models\FinancialTransactionLedgerHeadTemp;
use App\Models\SalaryJournalConfiguration;
use App\Models\SalaryJournalComponent;
use App\Models\AssignEmployeePayrole;
use Livewire\Component;
use Livewire\WithPagination;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class SalaryJournalLivewire extends Component
{
use WithPagination;
public $generate_month;
public $date_error = false;
public $financial_temp;
public $temp;
public function closeGenerate(){
$this->dispatchBrowserEvent('hide_gen_modal');
}
public function closeSubmit(){
$this->dispatchBrowserEvent('hide_submit_modal');
}
public function closeCancel(){
$this->dispatchBrowserEvent('hide_cancel_modal');
}
public function showmodalclick(){
if($this->generate_month != null){
$this->dispatchBrowserEvent('show_gen_modal');
}else{
$this->date_error = true;
}
}
public function generate(){
if($this->generate_month != null) {
$chk_salary_generation = FinancialTransactionTemp::where('generated_month', Carbon::parse($this->generate_month)->format('Y-m'))->where('status', '>', 0)->latest()->first();
if ($chk_salary_generation != null) {
$year = Carbon::parse($this->generate_month)->format('Y');
$mnth = Carbon::parse($this->generate_month)->format('m');
$salary_config = SalaryJournalConfiguration::whereYear('created_at', $year)->whereMonth('created_at', $mnth)->get();
$total_component_amt = 0;
if (sizeof($salary_config) > 0) {
foreach ($salary_config as $key => $sal_con) {
$component = SalaryJournalComponent::where('salary_journal_configuration_id', $sal_con['id'])->where('status', 1)->get();
if (sizeof($component) > 0) {
foreach ($component as $key1 => $cmpt) {
$amt = AssignEmployeePayrole::where('payrole_item_id', $cmpt['salary_component_id'])->sum('amount');
if($amt != null && $amt >0) {
$component[$key1]['amount'] = $amt;
$total_component_amt += $amt;
}else{
$component[$key1]['amount'] = 0;
}
}
}
$salary_config[$key]['components'] = sizeof($component) > 0 ? $component->toArray() : [];
$salary_config[$key]['sum_of_component_amt'] = $total_component_amt;
}
$financial_transaction = FinancialTransaction::count();
$store_salary_confg = FinancialTransactionTemp::create([
'voucher_type' => 'journal',
'transaction_related' => 'Salary Journal',
'preparatory_voucher_no' => 'JNLP' . $financial_transaction,
'preparatory_date' => date('Y-m-d'),
'voucher_particulars' => 'Salary Journal for month ' . Carbon::parse($this->generate_month)->format('F'),
'generated_month' => Carbon::parse($this->generate_month)->format('Y-m'),
'total_debit_amount' => 0,
'total_credit_amount' => 0,
]);
if ($store_salary_confg != null) {
$total_credit_amt = $total_debit_amt = 0;
foreach ($salary_config as $slr) {
$transaction_ldgr_head = FinancialTransactionLedgerHeadTemp::create([
'finance_transaction_id' => $store_salary_confg['id'],
'ledger_head_id' => $slr['ledger_head_id'],
'paid_to_received_from'=>$slr['description'],
'amount' => $slr['type'] == 'Debit'? '-'.$slr['sum_of_component_amt']:$slr['sum_of_component_amt'],
'type' => $slr['type'],
]);
if ($slr['type'] == 'Debit') {
$total_debit_amt += $slr['sum_of_component_amt'];
} elseif ($slr['type'] == 'Credit') {
$total_credit_amt += $slr['sum_of_component_amt'];
}
}
$financial_trans_data = FinancialTransactionTemp::find($store_salary_confg['id']);
if ($financial_trans_data != null) {
$financial_trans_data->update([
'total_debit_amount' => $total_debit_amt,
'total_credit_amount' => $total_credit_amt,
]);
}
}
$this->dispatchBrowserEvent('hide_gen_modal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => 'Salary Journal Generate Successfully.'
]);
}
}else{
$this->dispatchBrowserEvent('hide_gen_modal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => Carbon::parse($this->generate_month)->format('F').'Month Salary Journal Already Generated.'
]);
}
}
}
public function submitClick($id){
$this->temp=$id;
$this->dispatchBrowserEvent('show_submit_modal');
}
public function cancelClick($id){
$this->temp=$id;
$this->dispatchBrowserEvent('show_cancel_modal');
}
public function cancel(){
if($this->temp != null){
$ftlhtmp = FinancialTransactionLedgerHeadTemp::where('finance_transaction_id', $this->temp)->delete();
$fttmp = FinancialTransactionTemp::find($this->temp)->delete();
$this->dispatchBrowserEvent('hide_cancel_modal');
}
}
public function submit(){
$fnc_tmp = null;
if($this->temp != null){
$fnc_tmp = FinancialTransactionTemp::find($this->temp);
if($fnc_tmp != null){
$fnc_dtls_tmp = FinancialTransactionLedgerHeadTemp::where('finance_transaction_id', $fnc_tmp['id'])->where('status', 1)->get();
if(sizeof($fnc_dtls_tmp)>0){
$fnc_tmp['finance_ldgr_hd_tmp'] = $fnc_dtls_tmp->toArray();
}
}
}
if($fnc_tmp != null){
$current_year = date('Y');
$count_data = FinancialTransaction::where('voucher_type', 'journal')->where('voucher_status','>', 0)->count();
$jnl_count = $count_data+1;
if ($jnl_count < 10) {
$jnl_count = "000" . $jnl_count;
} else if ($jnl_count < 100) {
$jnl_count = "00" . $jnl_count;
}else if ($jnl_count < 1000) {
$jnl_count = "0" . $jnl_count;
} else {
$jnl_count = $jnl_count;
}
$jnl_voucher_no = 'JV/'.$current_year.'/'.$jnl_count;
$store_salary_confg = FinancialTransaction::create([
'voucher_type' => $fnc_tmp['voucher_type'],
'transaction_related' => $fnc_tmp['transaction_related'],
'voucher_no' => $jnl_voucher_no,
'voucher_date' => date('Y-m-d'),
'voucher_particulars' => $fnc_tmp['voucher_particulars'],
'total_debit_amount' => $fnc_tmp['total_debit_amount'],
'total_credit_amount' => $fnc_tmp['total_credit_amount'],
]);
if ($store_salary_confg != null && sizeof($fnc_tmp['finance_ldgr_hd_tmp'])>0) {
foreach ($fnc_tmp['finance_ldgr_hd_tmp'] as $details_data) {
$transaction_ldgr_head = FinancialTransactionLedgerHead::create([
'finance_transaction_id' => $store_salary_confg['id'],
'ledger_head_id' => $details_data['ledger_head_id'],
'amount' => $details_data['amount'],
'type' => $details_data['type'],
]);
}
$financial_trans_data = FinancialTransactionTemp::find($store_salary_confg['id']);
if ($financial_trans_data != null) {
$financial_trans_data->update([
'status' => 0,
]);
$financial_details = FinancialTransactionLedgerHeadTemp::where('finance_transaction_id', $financial_trans_data['id'])->where('status', 1)->update(['status'=>0]);
$this->temp = null;
$this->dispatchBrowserEvent('hide_submit_modal');
}
}
}
}
public function render()
{
$financial_temp = null;
$debit_amount = 0;
$credit_amount = 0;
if($this->generate_month != null){
$this->financial_temp = FinancialTransactionTemp::where('generated_month', Carbon::parse($this->generate_month)->format('Y-m'))->where('status', 1)->latest()->first();
if($this->financial_temp != null){
$trans_ldgr=FinancialTransactionLedgerHeadTemp::select('financial_transaction_ledger_head_temps.*', 'ledger_heads.ledger_head_name')->join('ledger_heads', 'financial_transaction_ledger_head_temps.ledger_head_id','=', 'ledger_heads.id')->where('financial_transaction_ledger_head_temps.finance_transaction_id', $this->financial_temp['id'])->where('financial_transaction_ledger_head_temps.status', 1)->get();
$this->financial_temp['financial_transaction_ledgerhead_temp'] = sizeof($trans_ldgr)>0?$trans_ldgr->toArray():[];
foreach ($trans_ldgr as $ldgr){
if($ldgr['type'] == 'Debit'){
$debit_amount += $ldgr['amount'];
}elseif($ldgr['type'] == 'Credit'){
$credit_amount += $ldgr['amount'];
}
}
}
}
return view('livewire.finance.salary-journal-livewire', compact('debit_amount', 'credit_amount'));
}
}