sarga/packages/Webkul/Admin/src/Http/Controllers/Sales/InvoiceController.php

174 lines
4.4 KiB
PHP
Raw Normal View History

<?php
namespace Webkul\Admin\Http\Controllers\Sales;
use PDF;
use Webkul\Admin\Http\Controllers\Controller;
2019-07-01 11:33:36 +00:00
use Webkul\Sales\Repositories\InvoiceRepository;
use Webkul\Sales\Repositories\OrderRepository;
class InvoiceController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
protected $_config;
/**
* Order repository instance.
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Sales\Repositories\OrderRepository
*/
2019-07-01 11:33:36 +00:00
protected $orderRepository;
/**
* Invoice repository instance.
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Sales\Repositories\InvoiceRepository
*/
2019-07-01 11:33:36 +00:00
protected $invoiceRepository;
/**
* 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
* @return void
*/
2019-07-01 11:33:36 +00:00
public function __construct(
OrderRepository $orderRepository,
InvoiceRepository $invoiceRepository
) {
$this->middleware('admin');
$this->_config = request('_config');
2019-07-01 11:33:36 +00:00
$this->orderRepository = $orderRepository;
2019-07-01 11:33:36 +00:00
$this->invoiceRepository = $invoiceRepository;
}
/**
* Display a listing of the resource.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
*/
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
*/
public function create($orderId)
{
2019-07-01 11:33:36 +00:00
$order = $this->orderRepository->findOrFail($orderId);
if ($order->payment->method === 'paypal_standard') {
abort(404);
}
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
* @return \Illuminate\Http\Response
*/
2019-07-01 11:33:36 +00:00
public function store($orderId)
{
2019-07-01 11:33:36 +00:00
$order = $this->orderRepository->findOrFail($orderId);
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'));
return redirect()->back();
}
$this->validate(request(), [
'invoice.items.*' => 'required|numeric|min:0',
]);
$data = request()->all();
2019-02-13 12:12:07 +00:00
$haveProductToInvoice = false;
2020-07-28 14:48:27 +00:00
foreach ($data['invoice']['items'] as $itemId => $qty) {
2019-01-15 11:54:41 +00:00
if ($qty) {
$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'));
return redirect()->back();
}
2019-07-01 11:33:36 +00:00
$this->invoiceRepository->create(array_merge($data, ['order_id' => $orderId]));
2019-02-13 12:12:07 +00:00
session()->flash('success', trans('admin::app.response.create-success', ['name' => 'Invoice']));
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
*/
public function view($id)
{
2019-07-01 11:33:36 +00:00
$invoice = $this->invoiceRepository->findOrFail($id);
return view($this->_config['view'], compact('invoice'));
}
/**
* 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);
$html = view('admin::sales.invoices.pdf', compact('invoice'))->render();
return PDF::loadHTML($this->adjustArabicAndPersianContent($html))
->setPaper('a4')
->download('invoice-' . $invoice->created_at->format('d-m-Y') . '.pdf');
}
/**
* Adjust arabic and persian content.
*
* @param string $html
* @return string
*/
private function adjustArabicAndPersianContent($html)
{
2021-01-27 05:25:00 +00:00
$arabic = new \ArPHP\I18N\Arabic();
$p = $arabic->arIdentify($html);
for ($i = count($p) - 1; $i >= 0; $i -= 2) {
$utf8ar = $arabic->utf8Glyphs(substr($html, $p[$i - 1], $p[$i] - $p[$i - 1]));
$html = substr_replace($html, $utf8ar, $p[$i - 1], $p[$i] - $p[$i - 1]);
}
return $html;
}
}