257 lines
6.4 KiB
PHP
Raw Normal View History

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