2018-10-09 12:02:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Admin\Http\Controllers\Sales;
|
|
|
|
|
|
2022-01-31 12:13:35 +00:00
|
|
|
use Illuminate\Http\Request;
|
2022-02-07 10:36:38 +00:00
|
|
|
use Webkul\Admin\DataGrids\InvoicesTransactionsDatagrid;
|
|
|
|
|
use Webkul\Admin\DataGrids\OrderInvoicesDataGrid;
|
2018-10-09 12:02:22 +00:00
|
|
|
use Webkul\Admin\Http\Controllers\Controller;
|
2022-01-31 12:13:35 +00:00
|
|
|
use Webkul\Admin\Traits\Mails;
|
2022-01-19 18:06:04 +00:00
|
|
|
use Webkul\Core\Traits\PDFHandler;
|
2019-07-01 11:33:36 +00:00
|
|
|
use Webkul\Sales\Repositories\InvoiceRepository;
|
2021-08-18 09:21:09 +00:00
|
|
|
use Webkul\Sales\Repositories\OrderRepository;
|
2018-10-09 12:02:22 +00:00
|
|
|
|
|
|
|
|
class InvoiceController extends Controller
|
|
|
|
|
{
|
2022-01-31 12:13:35 +00:00
|
|
|
use Mails, PDFHandler;
|
2022-01-19 18:06:04 +00:00
|
|
|
|
2018-10-09 12:02:22 +00:00
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
protected $_config;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new controller instance.
|
|
|
|
|
*
|
2020-03-05 14:19:14 +00:00
|
|
|
* @param \Webkul\Sales\Repositories\OrderRepository $orderRepository
|
2020-03-05 13:37:08 +00:00
|
|
|
* @param \Webkul\Sales\Repositories\InvoiceRepository $invoiceRepository
|
2018-10-09 12:02:22 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2019-07-01 11:33:36 +00:00
|
|
|
public function __construct(
|
2022-04-01 14:57:11 +00:00
|
|
|
protected OrderRepository $orderRepository,
|
|
|
|
|
protected InvoiceRepository $invoiceRepository
|
|
|
|
|
)
|
|
|
|
|
{
|
2018-10-09 12:02:22 +00:00
|
|
|
$this->_config = request('_config');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
2019-08-19 09:30:24 +00:00
|
|
|
* @return \Illuminate\View\View
|
2018-10-09 12:02:22 +00:00
|
|
|
*/
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
2022-02-07 10:36:38 +00:00
|
|
|
if (request()->ajax()) {
|
|
|
|
|
return app(OrderInvoicesDataGrid::class)->toJson();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 12:02:22 +00:00
|
|
|
return view($this->_config['view']);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 10:36:38 +00:00
|
|
|
/**
|
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function invoiceTransactions($id)
|
|
|
|
|
{
|
|
|
|
|
return app(InvoicesTransactionsDatagrid::class)->toJson();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 12:02:22 +00:00
|
|
|
/**
|
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @param int $orderId
|
2019-08-19 09:30:24 +00:00
|
|
|
* @return \Illuminate\View\View
|
2018-10-09 12:02:22 +00:00
|
|
|
*/
|
|
|
|
|
public function create($orderId)
|
|
|
|
|
{
|
2019-07-01 11:33:36 +00:00
|
|
|
$order = $this->orderRepository->findOrFail($orderId);
|
2018-10-09 12:02:22 +00:00
|
|
|
|
2021-08-18 09:21:09 +00:00
|
|
|
if ($order->payment->method === 'paypal_standard') {
|
|
|
|
|
abort(404);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 12:02:22 +00:00
|
|
|
return view($this->_config['view'], compact('order'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @param int $orderId
|
2018-10-09 12:02:22 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2019-07-01 11:33:36 +00:00
|
|
|
public function store($orderId)
|
2018-10-09 12:02:22 +00:00
|
|
|
{
|
2019-07-01 11:33:36 +00:00
|
|
|
$order = $this->orderRepository->findOrFail($orderId);
|
2018-10-09 12:02:22 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $order->canInvoice()) {
|
2019-02-13 12:12:07 +00:00
|
|
|
session()->flash('error', trans('admin::app.sales.invoices.creation-error'));
|
2018-10-09 12:02:22 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->validate(request(), [
|
|
|
|
|
'invoice.items.*' => 'required|numeric|min:0',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$data = request()->all();
|
2019-02-13 12:12:07 +00:00
|
|
|
|
2021-12-17 07:14:38 +00:00
|
|
|
if (! $this->invoiceRepository->haveProductToInvoice($data)) {
|
|
|
|
|
session()->flash('error', trans('admin::app.sales.invoices.product-error'));
|
2020-07-28 14:48:27 +00:00
|
|
|
|
2021-12-17 07:14:38 +00:00
|
|
|
return redirect()->back();
|
2018-10-09 12:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-17 07:14:38 +00:00
|
|
|
if (! $this->invoiceRepository->isValidQuantity($data)) {
|
|
|
|
|
session()->flash('error', trans('admin::app.sales.invoices.invalid-qty'));
|
2018-10-09 12:02:22 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->invoiceRepository->create(array_merge($data, ['order_id' => $orderId]));
|
2018-10-09 12:02:22 +00:00
|
|
|
|
2019-02-13 12:12:07 +00:00
|
|
|
session()->flash('success', trans('admin::app.response.create-success', ['name' => 'Invoice']));
|
2018-10-09 12:02:22 +00:00
|
|
|
|
|
|
|
|
return redirect()->route($this->_config['redirect'], $orderId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the view for the specified resource.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
2019-08-19 09:30:24 +00:00
|
|
|
* @return \Illuminate\View\View
|
2018-10-09 12:02:22 +00:00
|
|
|
*/
|
|
|
|
|
public function view($id)
|
|
|
|
|
{
|
2019-07-01 11:33:36 +00:00
|
|
|
$invoice = $this->invoiceRepository->findOrFail($id);
|
2018-10-09 12:02:22 +00:00
|
|
|
|
|
|
|
|
return view($this->_config['view'], compact('invoice'));
|
|
|
|
|
}
|
2018-11-01 13:48:59 +00:00
|
|
|
|
2022-01-31 12:13:35 +00:00
|
|
|
/**
|
|
|
|
|
* Send duplicate invoice.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function sendDuplicateInvoice(Request $request, $id)
|
|
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'email' => 'required|email',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$invoice = $this->invoiceRepository->findOrFail($id);
|
|
|
|
|
|
|
|
|
|
if ($invoice) {
|
|
|
|
|
$this->sendDuplicateInvoiceMail($invoice, $request->email);
|
|
|
|
|
|
|
|
|
|
session()->flash('success', __('admin::app.sales.invoices.invoice-sent'));
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session()->flash('error', __('admin::app.response.something-went-wrong'));
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-01 13:48:59 +00:00
|
|
|
/**
|
|
|
|
|
* Print and download the for the specified resource.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2022-01-19 18:06:04 +00:00
|
|
|
public function printInvoice($id)
|
2021-01-08 14:53:01 +00:00
|
|
|
{
|
2022-01-19 18:06:04 +00:00
|
|
|
$invoice = $this->invoiceRepository->findOrFail($id);
|
2018-11-01 13:48:59 +00:00
|
|
|
|
2022-01-19 18:06:04 +00:00
|
|
|
return $this->downloadPDF(
|
|
|
|
|
view('admin::sales.invoices.pdf', compact('invoice'))->render(),
|
|
|
|
|
'invoice-' . $invoice->created_at->format('d-m-Y')
|
|
|
|
|
);
|
2018-11-01 13:48:59 +00:00
|
|
|
}
|
2018-10-09 12:02:22 +00:00
|
|
|
}
|