CasperSecurity
<?php
namespace App\Http\Livewire\Finance;
use App\Models\Contra;
use App\Models\FinancialTransactions;
use App\Models\FinancialTransactionsLedgerHead;
use App\Models\CashType;
use App\Models\LedgerHead;
use App\Models\LedgerheadType;
use Livewire\Component;
use App\Models\Bank;
use Livewire\WithPagination;
class ContraLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $from, $to, $from_bank,$to_bank,$from_cheque,$from_cash,$to_cash, $date, $ledger_head;
public $amount = 0;
public $modelid;
public $contra_data = [];
public $showmodal=false;
public $successmsg="The Contra Voucher has been created successfully.";
public $updatemsg="The Account Type data has been updated successfully.";
public $deletemsg="The Account Type data has been deleted successfully.";
public $duplicateerrormsg="Sorry !!! Account Type name has already exists in our database. Please Try Another Name";
public $errormsg="Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules=[
'date'=>'required',
'from'=>'required',
'to'=>'required',
'ledger_head'=>'required',
'amount'=>'required',
];
public $updatedrules=[
'ledger_head'=>'required',
];
public $messages=[
'date.required'=>'This field is required',
'from.required'=>'This field is required',
'to.required'=>'This field is required',
'ledger_head.required'=>'This field is required',
'amount.required'=>'This field is required',
];
public function showmodalclick(){
$this->resetValidation();
$this->showmodal=true;
}
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->date = date('Y-m-d');
$this->from = null;
$this->from_bank = null;
$this->from_cheque = null;
$this->from_cash = null;
$this->to = null;
$this->to_bank = null;
$this->to_cash = null;
$this->ledger_head = null;
$this->amount = 0;
}
public function loadData($id){
$response=Bank::find($id);
if($response){
$this->from_bank=$response['from_bank'];
$this->to_bank=$response['to_bank'];
$this->from_Cash_type=$response['from_Cash_type'];
$this->to_Cash_type=$response['to_Cash_type'];
$this->from_check=$response['from_check'];
$this->to_check=$response['to_check'];
$this->from_bank_id=$response['from_bank_id'];
$this->to_bank_id=$response['to_bank_id'];
$this->ledger_head=$response['ledger_head'];
$this->status=$response['status'];
}
}
public function updatedToBank(){
if($this->to_bank != null){
if($this->to_bank == $this->from_bank){
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'From Bank and To bank should not be same'
]);
$this->to_bank = null;
}
}
}
public function updatedFromBank(){
if($this->from_bank != null){
if($this->to_bank == $this->from_bank){
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'From Bank and To bank should not be same'
]);
$this->from_bank = null;
}
}
}
public function add(){
$this->validate($this->rules,$this->messages);
$from_ldgr_head_id = $to_ldgr_head_id =null;
if($this->from_bank != null){
$from_ldgr_head_id = Bank::find($this->from_bank)->ledger_head_id;
}elseif($this->from_cash != null){
$from_ldgr_head_id = CashType::find($this->from_cash)->ledger_head_id;
}
if($this->to_bank != null){
$to_ldgr_head_id = Bank::find($this->to_bank)->ledger_head_id;
}elseif ($this->to_cash != null){
$to_ldgr_head_id = CashType::find($this->to_cash)->ledger_head_id;
}
$this->contra_data = [
[
'date'=>$this->date,
'from' =>$this->from.' Payment',
'vchr_type'=>$this->from,
'from_bank'=>$this->from_bank,
'from_cheque'=>$this->from_cheque,
'from_cash'=>$this->from_cash,
'to'=>null,
'to_bank'=>null,
'to_cash'=>null,
'credit_amount'=>0,
'debit_amount'=>$this->amount,
'entries' => [
['bnk_or_cash_ledger_head'=>$from_ldgr_head_id != null?$from_ldgr_head_id:null, 'cheque_no'=>$this->from_cheque,'type'=>'Credit','amount'=>$this->amount,],
['contra_ldgr_id' =>$this->ledger_head,'type'=>'Debit','amount'=>$this->amount,],
],
],
[
'date'=>$this->date,
'from' =>null,
'from_bank'=>null,
'from_cheque'=>null,
'from_cash'=>null,
'to'=>$this->to.' Receive',
'vchr_type'=>$this->to,
'to_bank'=>$this->to_bank,
'to_cash'=>$this->to_cash,
'credit_amount'=>$this->amount,
'debit_amount'=>0,
'entries' => [
['bnk_or_cash_ledger_head'=>$to_ldgr_head_id != null?$to_ldgr_head_id:null, 'cheque_no'=>null,'type'=>'Debit','amount'=>$this->amount,],
['contra_ldgr_id' =>$this->ledger_head,'type'=>'Credit','amount'=>$this->amount,],
],
],
];
$this->cleanVar();
}
public function save()
{
$current_year = date('Y');
$contra_count =0;
$contra_voucher_no = null;
$financial_transaction = null;
if(sizeof($this->contra_data)>0){
foreach ($this->contra_data as $cnt_dt){
$count_data = FinancialTransactions::count();
$contra_count = $count_data+1;
if ($contra_count < 10) {
$contra_count = "000" . $contra_count;
} else if ($contra_count < 100) {
$contra_count = "00" . $contra_count;
}else if ($contra_count < 1000) {
$contra_count = "0" . $contra_count;
} else {
$contra_count = $contra_count;
}
$contra_voucher_no = 'CN/'. $current_year .'/'. $contra_count;
$financial_transaction = FinancialTransactions::create([
'transaction_type'=>$cnt_dt['vchr_type'],
'cash_type_id'=>$cnt_dt['from_cash'] != null?$cnt_dt['from_cash']:$cnt_dt['to_cash'],
'bank_id'=>$cnt_dt['from_bank'] != null?$cnt_dt['from_bank']:$cnt_dt['to_bank'],
'total_debit_amount'=>$cnt_dt['debit_amount'],
'total_credit_amount'=>$cnt_dt['credit_amount'],
'voucher_no'=>$contra_voucher_no,
'voucher_date'=>$cnt_dt['date'],
'voucher_type'=>'contra',
]);
if(sizeof($cnt_dt['entries'])>0 && $financial_transaction != null){
foreach ($cnt_dt['entries'] as $contra_details){
if(isset($contra_details['bnk_or_cash_ledger_head'])){
$res = FinancialTransactionsLedgerHead::create([
'finance_transaction_id'=>$financial_transaction['id'],
'ledger_head_id'=>$contra_details['bnk_or_cash_ledger_head'],
'amount'=>$contra_details['amount'],
'type'=>$contra_details['type'],
'cheque_no'=>$contra_details['cheque_no'],
]);
}elseif (isset($contra_details['contra_ldgr_id'])){
$res = FinancialTransactionsLedgerHead::create([
'finance_transaction_id'=>$financial_transaction['id'],
'ledger_head_id'=>$contra_details['contra_ldgr_id'],
'type'=>$contra_details['type'],
'amount'=>$contra_details['amount'],
]);
}
}
}
}
}
/*$response = Contra::create([
'from_bank'=>$this->from_bank,
'to_bank'=>$this->to_bank,
'from_Cash_type'=>$this->from_Cash_type,
'to_Cash_type'=>$this->to_Cash_type,
'from_check'=>$this->from_check,
'to_check'=>$this->to_check,
'from_bank_id'=>$this->from_bank_id,
'to_bank_id'=>$this->to_bank_id,
'ledger_head'=>$this->ledger_head,
]);*/
//dd($response);
if ($financial_transaction != null) {
$this->contra_data = [];
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
public function update(){
$this->validate($this->updatedrules,$this->messages);
$response=Contra::find($this->modelid);
if($response){
$response->update([
'from_bank'=>$this->from_bank,
'to_bank'=>$this->to_bank,
'from_Cash_type'=>$this->from_Cash_type,
'to_Cash_type'=>$this->to_Cash_type,
'from_check'=>$this->from_check,
'to_check'=>$this->to_check,
'from_bank_id'=>$this->from_bank_id,
'to_bank_id'=>$this->to_bank_id,
'ledger_head'=>$this->ledger_head,
'status'=>$this->status,
]);
if($response){
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->updatemsg
]);
}else{
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function delete(){
$response=Contra::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 mount(){
$this->date = date('Y-m-d');
}
public function read(){
// $this->ledgerhead=LedgerHead::where('status',1)->get();
$contras=Contra::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $contras;
}
public function render()
{
$datatable = [];
$datatable = $this->read();
$bankdetails=[];
foreach ($bankdetails as $key => $bank) {
$bankdetails['credit'] = $bank->amt;
$bankdetails['debit'] = $bank->amt;
}
/* if (sizeof($datatable) > 0) {
foreach ($datatable as $key => $data) {
$ledger = LedgerHead::where('id', $data->ledger_head)->first();
if ($ledger) {
$datatable[$key]['ledger_head_name'] = $ledger->ledger_head_name
;
} else {
$datatable[$key]['ledger_head_name'] = "NA";
}
}
}
$ledgerhead = LedgerHead::where('status', 1)->get();*/
$ledgertype=LedgerheadType::where('ledger_type','Contra')->latest()->first();
$ledger_head_data = [];
if($ledgertype != null){
$ledger_head_data = LedgerHead::where('ledger_type_id',$ledgertype['id'])->get();
}
$banks=Bank::where('status',1)->get();
$cash_type = CashType::all();
return view('livewire.finance.contra-livewire',compact('banks','datatable','ledger_head_data', 'cash_type'));
}
}