2017-09-14 19:21:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
2019-12-31 12:49:09 +00:00
|
|
|
namespace App\Http\Controllers\Sales;
|
2017-09-14 19:21:00 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
use App\Abstracts\Http\Controller;
|
2019-12-31 12:49:09 +00:00
|
|
|
use App\Exports\Sales\Customers as Export;
|
2019-11-16 07:21:14 +00:00
|
|
|
use App\Http\Requests\Common\Contact as Request;
|
|
|
|
|
use App\Http\Requests\Common\Import as ImportRequest;
|
2019-12-31 12:49:09 +00:00
|
|
|
use App\Imports\Sales\Customers as Import;
|
2019-11-16 07:21:14 +00:00
|
|
|
use App\Jobs\Common\CreateContact;
|
|
|
|
|
use App\Jobs\Common\DeleteContact;
|
2022-06-01 07:15:55 +00:00
|
|
|
use App\Jobs\Common\DuplicateContact;
|
2019-11-16 07:21:14 +00:00
|
|
|
use App\Jobs\Common\UpdateContact;
|
|
|
|
|
use App\Models\Common\Contact;
|
2022-06-01 07:15:55 +00:00
|
|
|
use App\Traits\Contacts;
|
2017-09-14 19:21:00 +00:00
|
|
|
|
|
|
|
|
class Customers extends Controller
|
|
|
|
|
{
|
2022-06-01 07:15:55 +00:00
|
|
|
use Contacts;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public $type = Contact::CUSTOMER_TYPE;
|
|
|
|
|
|
2017-09-14 19:21:00 +00:00
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
2022-06-01 07:15:55 +00:00
|
|
|
$customers = Contact::customer()->with('invoices.transactions')->collect();
|
2017-09-14 19:21:00 +00:00
|
|
|
|
2020-11-05 21:43:46 +00:00
|
|
|
return $this->response('sales.customers.index', compact('customers'));
|
2017-09-14 19:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
2018-04-07 14:14:34 +00:00
|
|
|
/**
|
|
|
|
|
* Show the form for viewing the specified resource.
|
|
|
|
|
*
|
2019-11-16 07:21:14 +00:00
|
|
|
* @param Contact $customer
|
2018-04-07 14:14:34 +00:00
|
|
|
*
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
2019-11-16 07:21:14 +00:00
|
|
|
public function show(Contact $customer)
|
2018-04-07 14:14:34 +00:00
|
|
|
{
|
2022-06-01 07:15:55 +00:00
|
|
|
return view('sales.customers.show', compact('customer'));
|
2018-04-07 14:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
2017-09-14 19:21:00 +00:00
|
|
|
/**
|
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
|
*
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
|
|
|
|
public function create()
|
|
|
|
|
{
|
2022-06-01 07:15:55 +00:00
|
|
|
return view('sales.customers.create');
|
2017-09-14 19:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
|
*
|
|
|
|
|
* @param Request $request
|
|
|
|
|
*
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
2019-11-16 07:21:14 +00:00
|
|
|
$response = $this->ajaxDispatch(new CreateContact($request));
|
2017-11-23 10:38:43 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
if ($response['success']) {
|
2021-08-18 11:37:01 +00:00
|
|
|
$response['redirect'] = route('customers.show', $response['data']->id);
|
2017-11-23 10:38:43 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
$message = trans('messages.success.added', ['type' => trans_choice('general.customers', 1)]);
|
2017-12-11 08:12:23 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
flash($message)->success();
|
|
|
|
|
} else {
|
|
|
|
|
$response['redirect'] = route('customers.create');
|
2017-09-14 19:21:00 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
$message = $response['message'];
|
2017-09-14 19:21:00 +00:00
|
|
|
|
2021-02-12 16:26:38 +00:00
|
|
|
flash($message)->error()->important();
|
2017-09-14 19:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
return response()->json($response);
|
2017-09-14 19:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-26 12:20:17 +00:00
|
|
|
/**
|
|
|
|
|
* Duplicate the specified resource.
|
|
|
|
|
*
|
2019-11-16 07:21:14 +00:00
|
|
|
* @param Contact $customer
|
2017-11-26 12:20:17 +00:00
|
|
|
*
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
2019-11-16 07:21:14 +00:00
|
|
|
public function duplicate(Contact $customer)
|
2017-11-26 12:20:17 +00:00
|
|
|
{
|
2022-06-01 07:15:55 +00:00
|
|
|
$clone = $this->dispatch(new DuplicateContact($customer));
|
2017-11-26 12:20:17 +00:00
|
|
|
|
|
|
|
|
$message = trans('messages.success.duplicated', ['type' => trans_choice('general.customers', 1)]);
|
|
|
|
|
|
|
|
|
|
flash($message)->success();
|
|
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
return redirect()->route('customers.edit', $clone->id);
|
2017-11-26 12:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-30 08:47:56 +00:00
|
|
|
/**
|
|
|
|
|
* Import the specified resource.
|
|
|
|
|
*
|
2019-11-16 07:21:14 +00:00
|
|
|
* @param ImportRequest $request
|
2017-11-30 08:47:56 +00:00
|
|
|
*
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
2019-11-16 07:21:14 +00:00
|
|
|
public function import(ImportRequest $request)
|
2017-11-30 08:47:56 +00:00
|
|
|
{
|
2021-04-15 21:59:43 +00:00
|
|
|
$response = $this->importExcel(new Import, $request, trans_choice('general.customers', 2));
|
2021-02-11 15:08:06 +00:00
|
|
|
|
|
|
|
|
if ($response['success']) {
|
|
|
|
|
$response['redirect'] = route('customers.index');
|
2017-11-30 08:47:56 +00:00
|
|
|
|
2021-04-15 21:59:43 +00:00
|
|
|
flash($response['message'])->success();
|
2021-02-11 15:08:06 +00:00
|
|
|
} else {
|
|
|
|
|
$response['redirect'] = route('import.create', ['sales', 'customers']);
|
|
|
|
|
|
2021-04-15 21:59:43 +00:00
|
|
|
flash($response['message'])->error()->important();
|
2021-02-11 15:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json($response);
|
2017-11-30 08:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
2017-09-14 19:21:00 +00:00
|
|
|
/**
|
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
|
*
|
2019-11-16 07:21:14 +00:00
|
|
|
* @param Contact $customer
|
2017-09-14 19:21:00 +00:00
|
|
|
*
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
2019-11-16 07:21:14 +00:00
|
|
|
public function edit(Contact $customer)
|
2017-09-14 19:21:00 +00:00
|
|
|
{
|
2022-06-01 07:15:55 +00:00
|
|
|
return view('sales.customers.edit', compact('customer'));
|
2017-09-14 19:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
|
*
|
2019-11-16 07:21:14 +00:00
|
|
|
* @param Contact $customer
|
|
|
|
|
* @param Request $request
|
2017-09-14 19:21:00 +00:00
|
|
|
*
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
2019-11-16 07:21:14 +00:00
|
|
|
public function update(Contact $customer, Request $request)
|
2017-09-14 19:21:00 +00:00
|
|
|
{
|
2019-11-16 07:21:14 +00:00
|
|
|
$response = $this->ajaxDispatch(new UpdateContact($customer, $request));
|
2017-11-23 10:38:43 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
if ($response['success']) {
|
|
|
|
|
$response['redirect'] = route('customers.index');
|
2017-11-23 10:38:43 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
$message = trans('messages.success.updated', ['type' => $customer->name]);
|
2017-09-14 19:21:00 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
flash($message)->success();
|
|
|
|
|
} else {
|
|
|
|
|
$response['redirect'] = route('customers.edit', $customer->id);
|
2017-09-14 19:21:00 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
$message = $response['message'];
|
2017-09-14 19:21:00 +00:00
|
|
|
|
2021-02-12 16:26:38 +00:00
|
|
|
flash($message)->error()->important();
|
2017-09-14 19:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
return response()->json($response);
|
2017-09-14 19:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-11 08:53:45 +00:00
|
|
|
/**
|
|
|
|
|
* Enable the specified resource.
|
|
|
|
|
*
|
2019-11-16 07:21:14 +00:00
|
|
|
* @param Contact $customer
|
2018-06-11 08:53:45 +00:00
|
|
|
*
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
2019-11-16 07:21:14 +00:00
|
|
|
public function enable(Contact $customer)
|
2018-06-11 08:53:45 +00:00
|
|
|
{
|
2019-11-16 07:21:14 +00:00
|
|
|
$response = $this->ajaxDispatch(new UpdateContact($customer, request()->merge(['enabled' => 1])));
|
2018-06-11 08:53:45 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
if ($response['success']) {
|
|
|
|
|
$response['message'] = trans('messages.success.enabled', ['type' => $customer->name]);
|
|
|
|
|
}
|
2018-06-11 08:53:45 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
return response()->json($response);
|
2018-06-11 08:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disable the specified resource.
|
|
|
|
|
*
|
2019-11-16 07:21:14 +00:00
|
|
|
* @param Contact $customer
|
2018-06-11 08:53:45 +00:00
|
|
|
*
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
2019-11-16 07:21:14 +00:00
|
|
|
public function disable(Contact $customer)
|
2018-06-11 08:53:45 +00:00
|
|
|
{
|
2019-11-16 07:21:14 +00:00
|
|
|
$response = $this->ajaxDispatch(new UpdateContact($customer, request()->merge(['enabled' => 0])));
|
2018-06-11 08:53:45 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
if ($response['success']) {
|
|
|
|
|
$response['message'] = trans('messages.success.disabled', ['type' => $customer->name]);
|
|
|
|
|
}
|
2018-06-11 08:53:45 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
return response()->json($response);
|
2018-06-11 08:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
2017-09-14 19:21:00 +00:00
|
|
|
/**
|
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
|
*
|
2019-11-16 07:21:14 +00:00
|
|
|
* @param Contact $customer
|
2017-09-14 19:21:00 +00:00
|
|
|
*
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
2019-11-16 07:21:14 +00:00
|
|
|
public function destroy(Contact $customer)
|
2017-09-14 19:21:00 +00:00
|
|
|
{
|
2019-11-16 07:21:14 +00:00
|
|
|
$response = $this->ajaxDispatch(new DeleteContact($customer));
|
2017-09-14 19:21:00 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
$response['redirect'] = route('customers.index');
|
2017-09-14 19:21:00 +00:00
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
if ($response['success']) {
|
|
|
|
|
$message = trans('messages.success.deleted', ['type' => $customer->name]);
|
2017-09-14 19:21:00 +00:00
|
|
|
|
|
|
|
|
flash($message)->success();
|
|
|
|
|
} else {
|
2019-11-16 07:21:14 +00:00
|
|
|
$message = $response['message'];
|
2017-09-14 19:21:00 +00:00
|
|
|
|
2021-02-12 16:26:38 +00:00
|
|
|
flash($message)->error()->important();
|
2017-09-14 19:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
2019-11-16 07:21:14 +00:00
|
|
|
return response()->json($response);
|
2017-09-14 19:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-11 00:47:32 +00:00
|
|
|
/**
|
|
|
|
|
* Export the specified resource.
|
|
|
|
|
*
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
|
|
|
|
public function export()
|
|
|
|
|
{
|
2021-01-31 20:47:36 +00:00
|
|
|
return $this->exportExcel(new Export, trans_choice('general.customers', 2));
|
2018-06-11 00:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
2021-06-28 09:47:56 +00:00
|
|
|
public function createInvoice(Contact $customer)
|
|
|
|
|
{
|
|
|
|
|
$data['contact'] = $customer;
|
|
|
|
|
|
|
|
|
|
return redirect()->route('invoices.create')->withInput($data);
|
|
|
|
|
}
|
2021-06-29 17:05:11 +00:00
|
|
|
|
2022-06-01 07:15:55 +00:00
|
|
|
public function createIncome(Contact $customer)
|
2021-06-29 17:05:11 +00:00
|
|
|
{
|
|
|
|
|
$data['contact'] = $customer;
|
|
|
|
|
|
2022-06-01 07:15:55 +00:00
|
|
|
return redirect()->route('transactions.create', ['type' => 'income'])->withInput($data);
|
2021-06-29 17:05:11 +00:00
|
|
|
}
|
2017-09-14 19:21:00 +00:00
|
|
|
}
|