diff --git a/app/Http/Controllers/Expenses/Vendors.php b/app/Http/Controllers/Expenses/Vendors.php index 76aa73dc1..0cd4fb7fb 100644 --- a/app/Http/Controllers/Expenses/Vendors.php +++ b/app/Http/Controllers/Expenses/Vendors.php @@ -4,10 +4,16 @@ namespace App\Http\Controllers\Expenses; use App\Http\Controllers\Controller; use App\Http\Requests\Expense\Vendor as Request; +use App\Models\Expense\Bill; +use App\Models\Expense\Payment; use App\Models\Expense\Vendor; use App\Models\Setting\Currency; use App\Traits\Uploads; use App\Utilities\ImportFile; +use Date; +use Illuminate\Pagination\Paginator; +use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Support\Collection; class Vendors extends Controller { @@ -25,6 +31,80 @@ class Vendors extends Controller return view('expenses.vendors.index', compact('vendors')); } + /** + * Show the form for viewing the specified resource. + * + * @param Vendor $vendor + * + * @return Response + */ + public function show(Vendor $vendor) + { + $amounts = [ + 'paid' => 0, + 'open' => 0, + 'overdue' => 0, + ]; + + $counts = [ + 'bills' => 0, + 'payments' => 0, + ]; + + // Handle bills + $bills = Bill::with(['status', 'payments'])->where('vendor_id', $vendor->id)->get(); + + $counts['bills'] = $bills->count(); + + $bill_payments = []; + + $today = Date::today()->toDateString(); + + foreach ($bills as $item) { + $payments = 0; + + foreach ($item->payments as $payment) { + $payment->category = new \stdClass(); + $payment->category->id = 0; + $payment->category->name = trans_choice('general.bills', 2); + + $bill_payments[] = $payment; + + $amount = $payment->getConvertedAmount(); + + $amounts['paid'] += $amount; + + $payments += $amount; + } + + if ($item->bill_status_code == 'paid') { + continue; + } + + // Check if it's open or overdue invoice + if ($item->due_at > $today) { + $amounts['open'] += $item->getConvertedAmount() - $payments; + } else { + $amounts['overdue'] += $item->getConvertedAmount() - $payments; + } + } + + // Handle payments + $payments = Payment::with(['account', 'category'])->where('vendor_id', $vendor->id)->get(); + + $counts['payments'] = $payments->count(); + + // Prepare data + $items = collect($payments)->each(function ($item) use (&$amounts) { + $amounts['paid'] += $item->getConvertedAmount(); + }); + + $limit = request('limit', setting('general.list_limit', '25')); + $transactions = $this->paginate($items->merge($bill_payments)->sortByDesc('paid_at'), $limit); + + return view('expenses.vendors.show', compact('vendor', 'counts', 'amounts', 'transactions')); + } + /** * Show the form for creating a new resource. * @@ -206,4 +286,23 @@ class Vendors extends Controller return response()->json($vendor); } + + /** + * Generate a pagination collection. + * + * @param array|Collection $items + * @param int $perPage + * @param int $page + * @param array $options + * + * @return LengthAwarePaginator + */ + public function paginate($items, $perPage = 15, $page = null, $options = []) + { + $page = $page ?: (Paginator::resolveCurrentPage() ?: 1); + + $items = $items instanceof Collection ? $items : Collection::make($items); + + return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options); + } } diff --git a/app/Http/Controllers/Incomes/Customers.php b/app/Http/Controllers/Incomes/Customers.php index 5fc0f692b..273030037 100644 --- a/app/Http/Controllers/Incomes/Customers.php +++ b/app/Http/Controllers/Incomes/Customers.php @@ -4,11 +4,17 @@ namespace App\Http\Controllers\Incomes; use App\Http\Controllers\Controller; use App\Http\Requests\Income\Customer as Request; -use Illuminate\Http\Request as FRequest; use App\Models\Auth\User; use App\Models\Income\Customer; +use App\Models\Income\Invoice; +use App\Models\Income\Revenue; use App\Models\Setting\Currency; use App\Utilities\ImportFile; +use Date; +use Illuminate\Http\Request as FRequest; +use Illuminate\Pagination\Paginator; +use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Support\Collection; class Customers extends Controller { @@ -25,6 +31,80 @@ class Customers extends Controller return view('incomes.customers.index', compact('customers', 'emails')); } + /** + * Show the form for viewing the specified resource. + * + * @param Customer $customer + * + * @return Response + */ + public function show(Customer $customer) + { + $amounts = [ + 'paid' => 0, + 'open' => 0, + 'overdue' => 0, + ]; + + $counts = [ + 'invoices' => 0, + 'revenues' => 0, + ]; + + // Handle invoices + $invoices = Invoice::with(['status', 'payments'])->where('customer_id', $customer->id)->get(); + + $counts['invoices'] = $invoices->count(); + + $invoice_payments = []; + + $today = Date::today()->toDateString(); + + foreach ($invoices as $item) { + $payments = 0; + + foreach ($item->payments as $payment) { + $payment->category = new \stdClass(); + $payment->category->id = 0; + $payment->category->name = trans_choice('general.invoices', 2); + + $invoice_payments[] = $payment; + + $amount = $payment->getConvertedAmount(); + + $amounts['paid'] += $amount; + + $payments += $amount; + } + + if ($item->invoice_status_code == 'paid') { + continue; + } + + // Check if it's open or overdue invoice + if ($item->due_at > $today) { + $amounts['open'] += $item->getConvertedAmount() - $payments; + } else { + $amounts['overdue'] += $item->getConvertedAmount() - $payments; + } + } + + // Handle revenues + $revenues = Revenue::with(['account', 'category'])->where('customer_id', $customer->id)->get(); + + $counts['revenues'] = $revenues->count(); + + // Prepare data + $items = collect($revenues)->each(function ($item) use (&$amounts) { + $amounts['paid'] += $item->getConvertedAmount(); + }); + + $limit = request('limit', setting('general.list_limit', '25')); + $transactions = $this->paginate($items->merge($invoice_payments)->sortByDesc('paid_at'), $limit); + + return view('incomes.customers.show', compact('customer', 'counts', 'amounts', 'transactions')); + } + /** * Show the form for creating a new resource. * @@ -266,4 +346,23 @@ class Customers extends Controller return response()->json($json); } + + /** + * Generate a pagination collection. + * + * @param array|Collection $items + * @param int $perPage + * @param int $page + * @param array $options + * + * @return LengthAwarePaginator + */ + public function paginate($items, $perPage = 15, $page = null, $options = []) + { + $page = $page ?: (Paginator::resolveCurrentPage() ?: 1); + + $items = $items instanceof Collection ? $items : Collection::make($items); + + return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options); + } } diff --git a/resources/views/expenses/vendors/index.blade.php b/resources/views/expenses/vendors/index.blade.php index 0a66e11ac..4f479f4a1 100644 --- a/resources/views/expenses/vendors/index.blade.php +++ b/resources/views/expenses/vendors/index.blade.php @@ -42,7 +42,7 @@
@foreach($vendors as $item)