-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
InvoiceController.php
executable file
·170 lines (137 loc) · 4.25 KB
/
InvoiceController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Webkul\Admin\Http\Controllers\Sales;
use PDF;
use Webkul\Admin\Http\Controllers\Controller;
use Webkul\Sales\Repositories\OrderRepository;
use Webkul\Sales\Repositories\InvoiceRepository;
class InvoiceController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
protected $_config;
/**
* OrderRepository object
*
* @var \Webkul\Sales\Repositories\OrderRepository
*/
protected $orderRepository;
/**
* InvoiceRepository object
*
* @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);
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;
}
}