2018-10-09 12:02:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Admin\Http\Controllers\Sales;
|
|
|
|
|
|
2020-07-28 14:48:27 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
2018-10-09 12:02:22 +00:00
|
|
|
use Webkul\Admin\Http\Controllers\Controller;
|
2019-07-01 11:33:36 +00:00
|
|
|
use Webkul\Sales\Repositories\OrderRepository;
|
|
|
|
|
use Webkul\Sales\Repositories\InvoiceRepository;
|
2020-07-28 14:48:27 +00:00
|
|
|
|
2018-11-01 13:48:59 +00:00
|
|
|
use PDF;
|
2018-10-09 12:02:22 +00:00
|
|
|
|
|
|
|
|
class InvoiceController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
protected $_config;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* OrderRepository object
|
|
|
|
|
*
|
2020-03-05 13:37:08 +00:00
|
|
|
* @var \Webkul\Sales\Repositories\OrderRepository
|
2018-10-09 12:02:22 +00:00
|
|
|
*/
|
2019-07-01 11:33:36 +00:00
|
|
|
protected $orderRepository;
|
2018-10-09 12:02:22 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* InvoiceRepository object
|
|
|
|
|
*
|
2020-03-05 13:37:08 +00:00
|
|
|
* @var \Webkul\Sales\Repositories\InvoiceRepository
|
2018-10-09 12:02:22 +00:00
|
|
|
*/
|
2019-07-01 11:33:36 +00:00
|
|
|
protected $invoiceRepository;
|
2018-10-09 12:02:22 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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(
|
|
|
|
|
OrderRepository $orderRepository,
|
|
|
|
|
InvoiceRepository $invoiceRepository
|
|
|
|
|
)
|
2018-10-09 12:02:22 +00:00
|
|
|
{
|
|
|
|
|
$this->middleware('admin');
|
|
|
|
|
|
|
|
|
|
$this->_config = request('_config');
|
|
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->orderRepository = $orderRepository;
|
2018-10-09 12:02:22 +00:00
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->invoiceRepository = $invoiceRepository;
|
2018-10-09 12:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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()
|
|
|
|
|
{
|
|
|
|
|
return view($this->_config['view']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-10-09 12:02:22 +00:00
|
|
|
$haveProductToInvoice = false;
|
2020-07-28 14:48:27 +00:00
|
|
|
|
2018-10-09 12:02:22 +00:00
|
|
|
foreach ($data['invoice']['items'] as $itemId => $qty) {
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($qty) {
|
2018-10-09 12:02:22 +00:00
|
|
|
$haveProductToInvoice = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $haveProductToInvoice) {
|
2019-02-13 12:12:07 +00:00
|
|
|
session()->flash('error', trans('admin::app.sales.invoices.product-error'));
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Print and download the for the specified resource.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function print($id)
|
|
|
|
|
{
|
2019-07-01 11:33:36 +00:00
|
|
|
$invoice = $this->invoiceRepository->findOrFail($id);
|
2018-11-01 13:48:59 +00:00
|
|
|
|
|
|
|
|
$pdf = PDF::loadView('admin::sales.invoices.pdf', compact('invoice'))->setPaper('a4');
|
|
|
|
|
|
|
|
|
|
return $pdf->download('invoice-' . $invoice->created_at->format('d-m-Y') . '.pdf');
|
|
|
|
|
}
|
2020-07-28 14:48:27 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the invoice state.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function updateState($id, Request $request)
|
|
|
|
|
{
|
|
|
|
|
$invoice = $this->invoiceRepository->findOrFail($id);
|
|
|
|
|
$task = $this->invoiceRepository->updateInvoiceState($invoice, $request->state);
|
|
|
|
|
|
|
|
|
|
if ($task){
|
|
|
|
|
session()->flash('success', trans('admin::app.sales.orders.invoice-status-confirmed'));
|
|
|
|
|
} else {
|
|
|
|
|
session()->flash('success', trans('admin::app.sales.orders.invoice-status-error'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return back();
|
|
|
|
|
}
|
2018-10-09 12:02:22 +00:00
|
|
|
}
|