One Hat Cyber Team
Your IP :
3.142.131.16
Server IP :
104.21.32.1
Server :
Linux agrigation-prod 5.15.0-67-generic #74-Ubuntu SMP Wed Feb 22 14:14:39 UTC 2023 x86_64
Server Software :
nginx/1.24.0
PHP Version :
7.4.33
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
forge
/
app.gftag.com
/
app
/
Http
/
Controllers
/
View File Name :
CashBookController.php
<?php namespace App\Http\Controllers; use App\Models\Sale; use Illuminate\Http\Request; use App\Models\PurchaseOrder; use App\Models\CashBookTransaction; use App\Models\CashTransactionHeader; use App\Services\OrganisationService; use App\Models\Customer; use Carbon\Carbon; class CashBookController extends Controller { public function createIncome(Request $request) { $amount = 0; $type = ''; $reference = 0; $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); if ($request->has('source') && $request->has('id')) { $reference = $request->id; $sale = Sale::find($request->id); $amount = $sale->total; $transactionAmount = CashBookTransaction::where('organisation_id', $org_id)->where('type', 'income')->where('reference_id', $sale->id)->sum('amount'); if ($amount <= $transactionAmount) { return redirect()->back()->with('message', 'The income of this sale is already recorded'); } $amount = $amount - $transactionAmount; $type = $sale->sale_type; } $incomeHeaders = CashTransactionHeader::where('type', 'income')->get(); return view('cashbook.income', ['headers' => $incomeHeaders, 'amount' => $amount, 'type' => $type, 'reference' => $reference]); } public function storeIncome(Request $request) { $request->validate([ 'amount' => 'required', 'header' => 'required_without:sale_type', 'sale_type' => 'required_without:header', 'transaction_date' => 'required_without:sale_date|date', 'sale_date' => 'required_without:transaction_date|date', 'comments' => 'required' ]); $data = $request->all(); $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); $reference_type = null; $customer_id = null; if ($request->reference) { $sale = Sale::find($request->reference); $reference_type = get_class($sale); $customer_id = $sale->customer_id; } if(isset($request->sale_type) && $request->sale_type == 'product'){ $header_id = 1; } elseif(isset($request->sale_type) && $request->sale_type == 'service'){ $header_id = 2; } CashBookTransaction::create([ 'organisation_id' => $org_id, 'header_id' => isset($request->header) ? $request->header : $header_id, 'amount' => $request->amount, 'type' => 'income', 'transaction_date' => isset($request->transaction_date) ? date('Y-m-d', strtotime($request->transaction_date)) : date('Y-m-d', strtotime($request->sale_date)), 'reference_id' => $request->reference ? $request->reference : null, 'reference_type' => $reference_type, 'customer_id' => $customer_id, 'comments' => $request->comments, 'cash_flow_type' => $request->payment_mode ]); return redirect()->route('create-income')->with('message', 'Income recorded successfully !'); } public function createExpense(Request $request) { $amount = 0; $type = ''; $reference = 0; $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); if ($request->has('source') && $request->has('id')) { $reference = $request->id; $po = PurchaseOrder::find($request->id); $amount = $po->amount; $transactionAmount = CashBookTransaction::where('organisation_id', $org_id)->where('type', 'expense')->where('reference_id', $po->id)->sum('amount'); if ($amount <= $transactionAmount) { return redirect()->back()->with('message', 'The expense of this Purchase is already recorded'); } $amount = $amount - $transactionAmount; $type = 'purchase_order'; } $incomeHeaders = CashTransactionHeader::where('type', 'expense')->get(); return view('cashbook.expense', ['headers' => $incomeHeaders, 'amount' => $amount, 'type' => $type, 'reference' => $reference]); } public function storeExpense(Request $request) { $request->validate([ 'amount' => 'required', 'header' => 'required', 'transaction_date' => 'required', 'comments' => 'required' ]); $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); $reference_type = null; if ($request->reference) { $purchase_order = PurchaseOrder::find($request->reference); $reference_type = get_class($purchase_order); } CashBookTransaction::create([ 'organisation_id' => $org_id, 'header_id' => $request->header, 'amount' => $request->amount, 'type' => 'expense', 'transaction_date' => date('Y-m-d', strtotime($request->transaction_date)), 'reference_id' => $request->reference ? $request->reference : null, 'reference_type' => $reference_type, 'comments' => $request->comments, 'cash_flow_type' => $request->payment_mode ]); return redirect()->route('create-expense')->with('message', 'Expense recorded successfully !'); } public function cashbookReport(Request $request) { $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); $headers = CashTransactionHeader::get(); $cashBookReport = []; $startDate = Carbon::today()->startOfDay(); $endDate = Carbon::today()->endOfDay(); $cashBook = CashBookTransaction::where('organisation_id', $org_id)->orderBy('transaction_date', 'DESC'); if ($request->has('filter') && $request->filter != "") { if ($request->has('head') && $request->head != "") { if ($request->head != "All") { $cashBook->where('header_id', $request->head); } } if ($request->has('start_date') && $request->has('end_date')) { if ($request->start_date != "" && $request->end_date != "") { $startDate = $request->start_date; $endDate = $request->end_date; } } } $cashBook->whereBetween('transaction_date', [$startDate, $endDate]); $cashBookReport = $cashBook->get(); return view('cashbook.report', ['cashBookReport' => $cashBookReport, 'headers' => $headers]); } public function editCashbook(Request $request, $id) { $cashBook = CashBookTransaction::find($id); if ($cashBook->type == 'income') { $headers = CashTransactionHeader::where('type', 'income')->get(); } else { $headers = CashTransactionHeader::where('type', 'expense')->get(); } return view('cashbook.edit', ['headers' => $headers, 'cashBook' => $cashBook]); } public function updateCashbook(Request $request) { $cashBook = CashBookTransaction::find($request->id); $cashBook->header_id = $request->header; $cashBook->type = $request->type; $cashBook->amount = $request->amount; $cashBook->transaction_date = $request->date; $cashBook->save(); return redirect()->back()->with('message', 'Transaction updated successfully !'); } public function updateCustomersInCashbook() { $cashBook = CashBookTransaction::where('type', 'income')->whereNull('customer_id')->get(); foreach ($cashBook as $transaction) { $sale = Sale::find($transaction->reference_id); if ($sale) { echo $sale->customer_id; CashBookTransaction::where('id', $transaction->id)->update(['customer_id' => $sale->customer_id]); } } } public function getCustomerOutstandings() { $current_date = date('Y-m-d'); $before_30_date = date('Y-m-d', strtotime('-30 day')); $before_31_date = date('Y-m-d', strtotime('-31 day')); $before_60_date = date('Y-m-d', strtotime($before_31_date . ' -30 day')); $before_61_date = date('Y-m-d', strtotime($before_60_date . ' -1 day')); $before_90_date = date('Y-m-d', strtotime($before_61_date . ' -30 day')); $before_91_date = date('Y-m-d', strtotime($before_90_date . ' -1 day')); $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); $customers = Customer::where('organisation_id', $org_id)->get(); $customerArr = []; foreach ($customers as $customer) { $invoice_30_days = Sale::where('customer_id', $customer->id)->whereBetween('created_at', [$before_30_date, $current_date])->get(); $invoice_30_days_outstanding = 0; foreach ($invoice_30_days as $sale) { $income = CashBookTransaction::where('type', 'income')->where('reference_id', $sale->id)->sum('amount'); $invoice_30_days_outstanding += ($sale->total - ($sale->total >= $income ? $income : $sale->total)); } $customerArr[$customer->name]['0'] = $invoice_30_days_outstanding; $invoice_30_days = Sale::where('customer_id', $customer->id)->whereBetween('created_at', [$before_60_date, $before_31_date])->get(); $invoice_30_days_outstanding = 0; foreach ($invoice_30_days as $sale) { $income = CashBookTransaction::where('type', 'income')->where('reference_id', $sale->id)->sum('amount'); $invoice_30_days_outstanding += ($sale->total - ($sale->total >= $income ? $income : $sale->total)); } $customerArr[$customer->name]['1'] = $invoice_30_days_outstanding; $invoice_30_days = Sale::where('customer_id', $customer->id)->whereBetween('created_at', [$before_90_date, $before_61_date])->get(); $invoice_30_days_outstanding = 0; foreach ($invoice_30_days as $sale) { $income = CashBookTransaction::where('type', 'income')->where('reference_id', $sale->id)->sum('amount'); $invoice_30_days_outstanding += ($sale->total - ($sale->total >= $income ? $income : $sale->total)); } $customerArr[$customer->name]['2'] = $invoice_30_days_outstanding; $invoice_30_days = Sale::where('customer_id', $customer->id)->where('created_at', '<=', $before_91_date)->get(); $invoice_30_days_outstanding = 0; foreach ($invoice_30_days as $sale) { $income = CashBookTransaction::where('type', 'income')->where('reference_id', $sale->id)->sum('amount'); $invoice_30_days_outstanding += ($sale->total - ($sale->total >= $income ? $income : $sale->total)); } $customerArr[$customer->name]['3'] = $invoice_30_days_outstanding; } return view('cashbook.outstanding', ['customers' => $customerArr]); } }