customer/vendor show page
This commit is contained in:
		@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user