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

174 lines
4.4 KiB
PHP
Executable File

<?php
namespace Webkul\Admin\Http\Controllers\Sales;
use PDF;
use Webkul\Admin\Http\Controllers\Controller;
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.
*
* @var \Webkul\Sales\Repositories\OrderRepository
*/
protected $orderRepository;
/**
* Invoice repository instance.
*
* @var \Webkul\Sales\Repositories\InvoiceRepository
*/
protected $invoiceRepository;
/**
* Create a new controller instance.
*
* @param \Webkul\Sales\Repositories\OrderRepository $orderRepository
* @param \Webkul\Sales\Repositories\InvoiceRepository $invoiceRepository
* @return void
*/
public function __construct(
OrderRepository $orderRepository,
InvoiceRepository $invoiceRepository
) {
$this->middleware('admin');
$this->_config = request('_config');
$this->orderRepository = $orderRepository;
$this->invoiceRepository = $invoiceRepository;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\View\View
*/
public function index()
{
return view($this->_config['view']);
}
/**
* Show the form for creating a new resource.
*
* @param int $orderId
* @return \Illuminate\View\View
*/
public function create($orderId)
{
$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.
*
* @param int $orderId
* @return \Illuminate\Http\Response
*/
public function store($orderId)
{
$order = $this->orderRepository->findOrFail($orderId);
if (! $order->canInvoice()) {
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();
$haveProductToInvoice = false;
foreach ($data['invoice']['items'] as $itemId => $qty) {
if ($qty) {
$haveProductToInvoice = true;
break;
}
}
if (! $haveProductToInvoice) {
session()->flash('error', trans('admin::app.sales.invoices.product-error'));
return redirect()->back();
}
$this->invoiceRepository->create(array_merge($data, ['order_id' => $orderId]));
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
* @return \Illuminate\View\View
*/
public function view($id)
{
$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)
{
$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)
{
$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;
}
}