CasperSecurity
<?php
namespace App\Http\Livewire\Finance;
use App\Models\Project;
use Livewire\Component;
use App\Models\Client;
use App\Models\AccountGroup;
use App\Models\FinancialTransaction;
use App\Models\FinancialTransactionLedgerHead;
use App\Models\EmployeeRegistration;
use App\Models\LedgerHead;
use App\Models\LedgerHeadType;
use App\Models\OtherPayment;
use App\Models\Vendor;
use App\Models\OrganisationUnits;
use Livewire\WithPagination;
use Illuminate\Support\Facades\Http;
class VoucherManagementLivewire extends Component
{
protected $fillable = [
'voucher_type',
'voucher_date',
'voucher_particulars',
];
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $voucher_type, $voucher_particulars, $voucher_date;
public $finance_transaction_id, $paid_to_received_from, $reference_no, $ledger_type, $ledger_head_id,$vendor_id, $data;
public $modelid;
public $add_payments_data=[];
public $journal_data=[];
public $employees=[];
public $vendors_data = [];
public $emps = [];
public $amount = 0;
public $total_amount = 0;
public $upperid;
public $user_type_id, $total_debit_amount, $total_credit_amount, $amount_type, $type, $vendor_type_id, $employee_type_id, $employee_id, $client_id;
public $sub_ledger_type;
public $showmodal = false;
public $isDisable = false;
public $modal = false;
public $totalamount;
public $successmsg = "Voucher has been saved successfully.";
public $updatemsg = "Voucher has been updated successfully.";
public $deletemsg = "Voucher data has been deleted successfully.";
public $duplicateerrormsg = "Sorry !!! Other Payments name has already exists in our database. Please Try Another Name";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $projectDatas=[];
public $project_id=[];
public $projecturl="api/project";
public $rules = [
'voucher_type' => 'required',
'voucher_particulars' => 'required',
'voucher_date' => 'required',
];
public $journal_rules = [
'type' => 'required',
'voucher_particulars' => 'required',
'voucher_date' => 'required',
];
public $transaction_rules = [
'paid_to_received_from' => 'required',
'reference_no' => 'required',
'ledger_head_id' => 'required',
'amount' => 'required',
];
public $journal_transaction_rules = [
'ledger_type'=>'required',
/*'sub_ledger_type' => 'required',*/
'amount_type' => 'required',
'ledger_head_id' => 'required',
'amount' => 'required',
];
public $journal_transaction_messages = [
'ledger_type.required'=>'This field is required',
/*'sub_ledger_type.required' => 'This field is required',*/
'amount_type.required' => 'This field is required',
'ledger_head_id.required' => 'This field is required',
'amount.required' => 'This field is required',
];
/*
public $updatedrules = [
'voucher_type' => 'required',
'voucher_particulars' => 'required',
'voucher_date' => 'required',
'paid_to_received_from' => 'required',
'reference_no' => 'required',
'ledger_head_id' =>'required',
'amount' => 'required',
];*/
public $journal_messages = [
'type.required' => 'This field is required',
'voucher_date.required' => 'This field is required',
'voucher_particulars.required' => 'This field is required',
];
public $messages = [
'voucher_type.required' => 'This field is required',
'voucher_date.required' => 'This field is required',
'voucher_particulars.required' => 'This field is required',
];
public $transaction_messages = [
'paid_to_received_from.required' => 'This field is required',
'reference_no.required' => 'This field is required',
'ledger_head_id.required' => 'This field is required',
'amount.required' => 'This field is required',
];
public function showmodalclick()
{
$this->resetValidation();
$this->showmodal = true;
}
public function resetdata()
{
$this->voucher_type = '';
$this->voucher_date = '';
$this->sectionRate = [];
$this->totalAmount = 0;
}
public function closemodalclick()
{
$this->resetValidation();
$this->showmodal = false;
}
public function updateclick($id)
{
$this->modelid = $id;
$this->loadData($this->modelid);
$this->showmodal = true;
}
public function deleteclick($id)
{
$this->modelid = $id;
$this->dispatchBrowserEvent('confirmdelete', [
'message' => 'Are you sure want to delete this data ?',
'funcname' => 'delete'
]);
}
public function cleanVar()
{
$this->modelid = null;
$this->voucher_type = null;
$this->voucher_date = date('Y-m-d');
$this->voucher_particulars = null;
$this->total_amount = 0;
$this->finance_transaction_id = null;
$this->paid_to_received_from = null;
$this->reference_no = null;
$this->ledger_type = null;
$this->ledger_head_id = null;
$this->amount = 0;
$this->add_payments_data = [];
$this->type = null;
}
/* public function loadData($id)
{
$response = FinancialTransactions::find($id);
if ($response) {
$this->voucher_type = $response['voucher_type'];
$this->voucher_date = $response['voucher_date'];
$this->voucher_particulars = $response['voucher_particulars'];
}
}*/
public function updatedSubLedgerType(){
$this->user_type_id = null;
if($this->sub_ledger_type == 'Employee'){
$this->vendors_data = [];
$this->employees = EmployeeRegistration::where('status', '>',0)->get();
$this->employees = sizeof($this->employees)>0?$this->employees->toArray():[];
}elseif ($this->sub_ledger_type == 'Vendor'){
$this->employees = [];
$this->vendors_data = Vendor::where('status', 1)->get();
}elseif ($this->sub_ledger_type == 'Others'){
$this->employees = [];
$this->vendors_data = [];
}
}
public function add_payments(){
$this->validate($this->transaction_rules, $this->transaction_messages);
$ledger_head = null;
if($this->ledger_head_id != null){
$ledger_head = LedgerHead::where('id', $this->ledger_head_id)->value('ledger_head_name');
}
$project_name = 'N/A';
if ($this->project_id) {
$project = collect($this->projectDatas)->firstWhere('id', $this->project_id);
$project_name = $project ? $project['project_name'] : 'N/A';
}
$this->add_payments_data[] = [
'paid_to_received_from' => $this->paid_to_received_from,
'reference_no' => $this->reference_no,
'ledger_type' => $this->ledger_type,
'vendor_id' => $this->vendor_id,
'client_id' => $this->client_id,
'employee_id'=>$this->employee_id,
'project_id'=>$this->project_id,
'project_name' => $project_name,
'ledger_head_id' => $this->ledger_head_id,
'ledger_head_name'=>$ledger_head != null?$ledger_head:null,
'amount' => $this->amount,
];
$this->total_amount += $this->amount;
$this->paid_to_received_from = null;
$this->reference_no = null;
$this->ledger_type = null;
$this->ledger_head_id = null;
$this->vendor_id = null;
$this->client_id = null;
$this->amount = 0;
}
public function add_journal(){
$this->validate($this->journal_transaction_rules, $this->journal_transaction_messages);
$ledger_head_name = null;
if($this->ledger_head_id != null){
$ledger_head = LedgerHead::where('id', $this->ledger_head_id)->latest()->first();
}
$vendor_name = $this->sub_ledger_type == 'Vendor'?Vendor::find($this->user_type_id)->vendor_name:null;
$employee_name = $this->sub_ledger_type == 'Employee'?EmployeeRegistration::find($this->user_type_id)->name:null;
$project_name = 'N/A';
if ($this->project_id) {
$project = collect($this->projectDatas)->firstWhere('id', $this->project_id);
$project_name = $project ? $project['project_name'] : 'N/A';
}
$this->journal_data[]=[
'ldger_hd_id'=>$this->ledger_head_id,
'ldger_type'=>$this->sub_ledger_type,
'vendor_id'=>$this->sub_ledger_type == 'Vendor'?$this->user_type_id:null,
'emp_id'=>$this->sub_ledger_type == 'Employee'?$this->user_type_id:null,
'vendor_name' => $vendor_name,
'employee_name' => $employee_name,
'project_id' => $this->project_id,
'project_name' => $project_name,
'ledger_head' => $ledger_head != null?$ledger_head['ledger_head_name']:null,
'ledger_head_code' => $ledger_head != null?$ledger_head['ledger_head']:null,
'type' =>$this->amount_type,
'debit' => $this->amount_type == 'Debit'?$this->amount:0,
'credit' => $this->amount_type == 'Credit'?$this->amount:0,
];
if($this->amount_type == 'Debit'){
$this->total_debit_amount += $this->amount;
}elseif ($this->amount_type == 'Credit'){
$this->total_credit_amount += $this->amount;
}
$this->ledger_type = null;
$this->vendor_id = null;
$this->employee_id = null;
$this->project_id = null;
$this->client_id = null;
$this->sub_ledger_type = null;
$this->amount_type = null;
$this->user_type_id = null;
$this->ledger_head_id = null;
$this->amount = 0;
}
public function delete_transaction($key){
if(sizeof($this->add_payments_data)>0){
unset($this->add_payments_data[$key]);
}
if(sizeof($this->add_payments_data)>0){
$this->total_amount = 0;
foreach ($this->add_payments_data as $pymt_dt){
$this->total_amount += $pymt_dt['amount'];
}
}else{
$this->total_amount = 0;
}
}
public function projectData(){
$url = env('CLIENT_URL') . '/' . $this->projecturl ;
$response = Http::get($url);
if($response->ok()){
if($response->json()['data'] !=null){
$this->projectDatas = $response->json()['data'];
}else{
$this->projectDatas = [];
}
}
}
public function save()
{
if ($this->type == 'payment' || $this->type == 'receive') {
$this->validate($this->rules, $this->messages);
$response = FinancialTransaction::create([
'voucher_type' => $this->type,
'transaction_type'=>$this->voucher_type,
'preparatory_date' => $this->voucher_date,
'voucher_particulars' => $this->voucher_particulars,
'total_debit_amount' => $this->type == 'payment'?$this->total_amount:0,
'total_credit_amount' => $this->type == 'receive'?$this->total_amount:0,
]);
if ($response != null) {
if($this->type == 'payment'){
$response->preparatory_voucher_no = $this->voucher_type == 'Cash'?'CP-'.$response->id:'BP-'.$response->id;
}elseif ($this->type == 'receive'){
$response->preparatory_voucher_no = $this->voucher_type == 'Cash'?'CR-'.$response->id:'BR-'.$response->id;
}
$response->save();
/* $this->validate($this->tdsrules, $this->tdsmessages);*/
if(sizeof($this->add_payments_data)>0){
foreach ($this->add_payments_data as $payment_data){
$sectionRate = FinancialTransactionLedgerHead::create([
'finance_transaction_id' => $response->id,
'paid_to_received_from' => $payment_data['paid_to_received_from'],
'reference_no' => $payment_data['reference_no'],
'ledger_type' => $payment_data['ledger_type'],
'vendor_id' => $payment_data['vendor_id'],
'customer_id'=> $payment_data['client_id'],
'employee_id' => $payment_data['employee_id'],
'project_id' => $payment_data['project_id'],
'ledger_head_id' => $payment_data['ledger_head_id'],
'amount' => $this->type == 'payment'?'-'.$payment_data['amount']:$payment_data['amount'],
'type' => $this->voucher_type,
]);
}
}
if ($sectionRate) {
$this->voucher_printing($response['id']);
// $this->isDisable = true;
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
}
public function save_journal(){
$this->validate($this->journal_rules, $this->journal_messages);
$response = FinancialTransaction::create([
'voucher_type' => $this->type,
'preparatory_date' => $this->voucher_date,
'voucher_particulars' => $this->voucher_particulars,
'total_debit_amount' => $this->total_debit_amount,
'total_credit_amount' => $this->total_credit_amount,
]);
if ($response != null) {
$response->preparatory_voucher_no = 'JNLP-'.$response->id;
$response->save();
/*$this->validate($this->tdsrules, $this->tdsmessages);*/
if(sizeof($this->journal_data)>0){
foreach ($this->journal_data as $jnl_data){
$sectionRate = FinancialTransactionLedgerHead::create([
'finance_transaction_id' => $response->id,
'ledger_type' => $jnl_data['ldger_type'],
'vendor_id' => $jnl_data['vendor_id'],
'employee_id'=>$jnl_data['emp_id'],
'project_id'=>$jnl_data['project_id'],
'ledger_head_id' => $jnl_data['ldger_hd_id'],
'amount' => $jnl_data['debit'] >0?'-'.$jnl_data['debit']:$jnl_data['credit'],
'type' => $jnl_data['type'],
]);
}
}
if ($sectionRate) {
$this->voucher_printing($response['id']);
$this->voucher_particulars = null;
$this->type = null;
$this->journal_data= [];
$this->total_debit_amount = 0;
$this->total_credit_amount = 0;
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function getIndianCurrency($number)
{
$decimal = round($number - ($no = floor($number)), 2) * 100;
$hundred = null;
$digits_length = strlen($no);
$i = 0;
$str = array();
$words = array(
0 => '',
1 => 'One',
2 => 'Two',
3 => 'Three',
4 => 'Four',
5 => 'Five',
6 => 'Six',
7 => 'Seven',
8 => 'Eight',
9 => 'Nine',
10 => 'Ten',
11 => 'Eleven',
12 => 'Twelve',
13 => 'Thirteen',
14 => 'Fourteen',
15 => 'Fifteen',
16 => 'Sixteen',
17 => 'Seventeen',
18 => 'Eighteen',
19 => 'Nineteen',
20 => 'Twenty',
30 => 'Thirty',
40 => 'Forty',
50 => 'Fifty',
60 => 'Sixty',
70 => 'Seventy',
80 => 'Eighty',
90 => 'Ninety'
);
$digits = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');
while ($i < $digits_length) {
$divider = ($i == 2) ? 10 : 100;
$number = floor($no % $divider);
$no = floor($no / $divider);
$i += $divider == 10 ? 1 : 2;
if ($number) {
$plural = (($counter = count($str)) && $number > 9) ? 's' : null;
// $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
$str[] = ($number < 21) ? $words[$number] . ' ' . $digits[$counter] . $plural . ' ' . $hundred : $words[floor($number / 10) * 10] . ' ' . $words[$number % 10] . ' ' . $digits[$counter] . $plural . ' ' . $hundred;
} else
$str[] = null;
}
$Rupees = implode('', array_reverse($str));
$paise = ($decimal > 0) ? ($words[$decimal / 10] . " " . $words[$decimal % 10]) . ' Paise' : '';
$rupeesPart = $Rupees ? 'Rupees ' . $Rupees : '';
$paisePart = $paise ? $paise : '';
$onlyPart = 'Only';
if ($rupeesPart && !$paisePart) {
return $rupeesPart . $onlyPart;
} elseif ($rupeesPart && $paisePart) {
return $rupeesPart . ' and ' . $paisePart . $onlyPart;
} elseif (!$rupeesPart && $paisePart) {
return $paisePart . $onlyPart;
} else {
return '';
}
}
public function voucher_printing($voucher_id){
if($voucher_id != null){
$voucher = FinancialTransaction::find($voucher_id);
if($voucher != null) {
$voucher = $voucher->toArray();
$voucher['transaction_type'] = $voucher['transaction_type'] != null?$voucher['transaction_type'].' '.ucwords($voucher['voucher_type']):ucwords($voucher['voucher_type']);
$word_amount = $voucher['voucher_type'] == 'payment'?$this->getIndianCurrency($voucher['total_debit_amount']):$this->getIndianCurrency($voucher['total_credit_amount']);
$voucher['amount_word'] = $word_amount != ''?$word_amount:null;
$trans = FinancialTransactionLedgerHead::where('finance_transaction_id', $voucher['id'])->get();
$voucher['transactions'] = sizeof($trans) > 0 ? $trans->toArray() : [];
if(sizeof($voucher['transactions'])>0){
foreach ($voucher['transactions'] as $key=>$ft){
$voucher['transactions'][$key]['amount'] = abs($ft['amount']);
$ledger_hd = LedgerHead::find($ft['ledger_head_id']);
$voucher['transactions'][$key]['ledger_head'] = $ledger_hd != null?$ledger_hd:null;
$voucher['transactions'][$key]['vendor'] = $ft['vendor_id']!= null?Vendor::find($ft['vendor_id']):null;
$voucher['transactions'][$key]['employee'] = $ft['employee_id'] != null?EmployeeRegistration::find($ft['employee_id']):null;
}
}
$org = OrganisationUnits::select('organisation_units.*', 'organisations.logo')->join('organisations','organisation_units.organisation_id','=', 'organisations.id')->latest()->first();
$voucher['organisation'] = $org != null?$org->toArray():null;
session(['session_voucher_data'=>$voucher]);
$this->dispatchBrowserEvent('send_to_voucher_print');
}
}
}
public function updatesub()
{
$this->validate($this->orrules, $this->ormessages);
$sectionRate = FinancialTransactionLedgerHead::create([
'finance_transaction_id' => $this->upperid,
'paid_to_received_from' => $this->paid_to_received_from,
'reference_no' => $this->reference_no,
'ledger_type' => $this->ledger_type,
'vendor_id' => $this->vendor_id,
'ledger_head_id' => $this->ledger_head_id,
'amount' => $this->amount,
]);
if ($sectionRate) {
$this->isDisable = true;
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
public function mount()
{
$this->voucher_date = date('Y-m-d');
$this->upperid == null;
/* $this->isDisable == null;*/
$this->projectData();
}
public function delete()
{
$response = FinancialTransactionLedgerHead::find($this->modelid);
if ($response) {
if ($response->delete()) {
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->deletemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function receipt()
{
$financial_transactions = FinancialTransaction::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $financial_transactions;
}
public function read($id)
{
$financial_transactions_ledger_heads = FinancialTransactionLedgerHead::where('finance_transaction_id', $id)->get();
/* orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);*/
return $financial_transactions_ledger_heads;
}
public function render()
{
if ($this->upperid == null) {
$datatable = [];
} else {
$datatable = $this->read($this->upperid);
}
if (sizeof($datatable) > 0) {
foreach ($datatable as $key => $data) {
$ledger = LedgerHead::where('id', $data->ledger_head_id)->first();
if ($ledger) {
$datatable[$key]['ledger_head_name'] = $ledger->ledger_head_name;
} else {
$datatable[$key]['ledger_head_name'] = "NA";
}
}
}
if ($this->isDisable == null) {
$othertable = [];
} else {
$othertable = $this->receipt($this->isDisable);
}
//dd($othertable);
/* $othertable = [];
$othertable = $this-> receipt();*/
/* $ledgerhead = LedgerHead::where('status', 1)->get();*/
$vendors = Vendor::where('status', 1)->get();
$totalamount = [];
$amount = 0;
foreach ($totalamount as $item) {
$amount += $item['amount'];
}
$account_group = AccountGroup::all();
// $accledgertype=LedgerheadType::where('ledger_type','Accounts Payable')->first();
// if($accledgertype != null){
// $ledgerhead=LedgerHead::where('ledger_type_id',$accledgertype->id)->get();
// }else{
// $ledgerhead = [];
// }
if($this->sub_ledger_type == 'Employee'){
$this->vendors_data = [];
}elseif ($this->sub_ledger_type == 'Vendor'){
$this->employees = [];
}
if($this->type == 'payment'){
$ledger_head_type = LedgerHeadType::whereIn('id', [1,3,7])->get();
}elseif($this->type == 'receive'){
$ledger_head_type = LedgerHeadType::whereIn('id', [2,3,7])->get();
}elseif($this->type == 'journal'){
$ledger_head_type = LedgerHeadType::whereIn('id', [1,2,3,7])->get();
}else{
$ledger_head_type = [];
}
if($this->ledger_type != null){
$ledgerhead = LedgerHead::where('ledger_type_id', $this->ledger_type)->get();
}else{
$ledgerhead = [];
}
$this->emps = EmployeeRegistration::where('status', '>',0)->get();
$this->emps = sizeof($this->emps)>0?$this->emps->toArray():[];
$clients = [];
$url=env('CLIENT_URL').'/api/client_detail';
if($url != null){
$response=Http::get($url);
if($response->ok()){
$clients=$response->json()['data'];
}else{
$clients = [];
}
}
// $clients = Client::where('status', 1)->get();
// $clients = sizeof($clients)>0?$clients->toArray():[];
return view('livewire.finance.voucher-management-livewire', compact('datatable' ,'othertable', 'vendors', 'totalamount', 'account_group','ledger_head_type', 'clients', 'ledgerhead'));
}
}