|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Soap\Invoices\Services; |
|
5
|
|
|
|
|
6
|
|
|
use Dompdf\Dompdf; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
9
|
|
|
use Illuminate\Database\Query\Builder; |
|
10
|
|
|
use Illuminate\Support\Facades\View; |
|
11
|
|
|
use Soap\Invoices\Interfaces\BillServiceInterface; |
|
12
|
|
|
use Soap\Invoices\Models\Bill; |
|
13
|
|
|
use Soap\Invoices\MoneyFormatter; |
|
14
|
|
|
use Soap\Invoices\Scopes\InvoiceScope; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
|
|
17
|
|
|
class BillService implements BillServiceInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var Bill $bill |
|
21
|
|
|
*/ |
|
22
|
|
|
private $bill; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool $is_free |
|
26
|
|
|
*/ |
|
27
|
|
|
private $is_free = false; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var bool $is_comp |
|
31
|
|
|
*/ |
|
32
|
|
|
private $is_comp = false; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array $taxes |
|
36
|
|
|
*/ |
|
37
|
|
|
private $taxes = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @inheritDoc |
|
41
|
|
|
*/ |
|
42
|
|
|
public function create(Model $model, ?array $bill = []): BillServiceInterface |
|
43
|
|
|
{ |
|
44
|
|
|
$this->bill = $model->bills()->create($bill); |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritDoc |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getBill(): Bill |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->bill; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritDoc |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getLines(): Collection |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->getBill()->lines()->get(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritDoc |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setFree(): BillServiceInterface |
|
69
|
|
|
{ |
|
70
|
|
|
$this->is_free = true; |
|
71
|
|
|
$this->is_comp = false; |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritDoc |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setComplimentary(): BillServiceInterface |
|
80
|
|
|
{ |
|
81
|
|
|
$this->is_comp = true; |
|
82
|
|
|
$this->is_free = false; |
|
83
|
|
|
|
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @inheritDoc |
|
89
|
|
|
*/ |
|
90
|
|
|
public function addTaxPercentage(string $identifier, float $taxPercentage = 0): BillServiceInterface |
|
91
|
|
|
{ |
|
92
|
|
|
array_push($this->taxes, [ |
|
93
|
|
|
'identifier' => $identifier, |
|
94
|
|
|
'tax_fixed' => null, |
|
95
|
|
|
'tax_percentage' => $taxPercentage, |
|
96
|
|
|
]); |
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @inheritDoc |
|
102
|
|
|
*/ |
|
103
|
|
|
public function addTaxFixed(string $identifier, int $taxAmount = 0): BillServiceInterface |
|
104
|
|
|
{ |
|
105
|
|
|
array_unshift($this->taxes, [ |
|
106
|
|
|
'identifier' => $identifier, |
|
107
|
|
|
'tax_fixed' => $taxAmount, |
|
108
|
|
|
'tax_percentage' => null, |
|
109
|
|
|
]); |
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @inheritDoc |
|
115
|
|
|
*/ |
|
116
|
|
|
public function addAmountExclTax(Model $model, int $amount, string $description): BillServiceInterface |
|
117
|
|
|
{ |
|
118
|
|
|
$tax = 0; |
|
119
|
|
|
foreach ($this->taxes as $each) { |
|
120
|
|
|
$tax += (null === $each['tax_fixed']) ? $amount * $each['tax_percentage'] : $each['tax_fixed']; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$this->bill->lines()->create([ |
|
124
|
|
|
'amount' => $amount + $tax, |
|
125
|
|
|
'description' => $description, |
|
126
|
|
|
'tax' => $tax, |
|
127
|
|
|
'tax_details' => $this->taxes, |
|
128
|
|
|
'invoiceable_id' => $model->id, |
|
129
|
|
|
'invoiceable_type' => get_class($model), |
|
130
|
|
|
'is_free' => $this->is_free, |
|
131
|
|
|
'is_complimentary' => $this->is_comp, |
|
132
|
|
|
]); |
|
133
|
|
|
|
|
134
|
|
|
$this->recalculate(); |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @inheritDoc |
|
141
|
|
|
*/ |
|
142
|
|
|
public function addAmountInclTax(Model $model, int $amount, string $description): BillServiceInterface |
|
143
|
|
|
{ |
|
144
|
|
|
$exc = $amount; |
|
145
|
|
|
$tax = 0; |
|
146
|
|
|
foreach ($this->taxes as $each) { |
|
147
|
|
|
if (null !== $each['tax_fixed']) { |
|
148
|
|
|
$exc -= $each['tax_fixed']; |
|
149
|
|
|
$tax += $each['tax_fixed']; |
|
150
|
|
|
} else { |
|
151
|
|
|
$tax += ($exc * $each['tax_percentage']) / (1 + $each['tax_percentage']); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$this->bill->lines()->create([ |
|
156
|
|
|
'amount' => $amount, |
|
157
|
|
|
'description' => $description, |
|
158
|
|
|
'tax' => $tax, |
|
159
|
|
|
'tax_details' => $this->taxes, |
|
160
|
|
|
'invoiceable_id' => $model->id, |
|
161
|
|
|
'invoiceable_type' => get_class($model), |
|
162
|
|
|
'is_free' => $this->is_free, |
|
163
|
|
|
'is_complimentary' => $this->is_comp, |
|
164
|
|
|
]); |
|
165
|
|
|
|
|
166
|
|
|
$this->recalculate(); |
|
167
|
|
|
|
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @inheritDoc |
|
173
|
|
|
*/ |
|
174
|
|
|
public function recalculate(): Bill |
|
175
|
|
|
{ |
|
176
|
|
|
$lines = $this->bill->lines()->get(); |
|
177
|
|
|
$free = $lines->where('is_free', true)->toBase(); |
|
178
|
|
|
$complimentary = $lines->where('is_complimentary', true)->toBase(); |
|
179
|
|
|
$other = $lines |
|
180
|
|
|
->where('is_free', false) |
|
181
|
|
|
->where('is_complimentary', false) |
|
182
|
|
|
->toBase(); |
|
183
|
|
|
|
|
184
|
|
|
$this->bill->total = $other->sum('amount'); |
|
185
|
|
|
$this->bill->tax = $other->sum('tax'); |
|
186
|
|
|
$this->bill->discount = $free->sum('amount') + $complimentary->sum('amount') + $other->sum('discount'); |
|
187
|
|
|
|
|
188
|
|
|
$this->bill->save(); |
|
189
|
|
|
|
|
190
|
|
|
$this->is_free = false; |
|
191
|
|
|
$this->is_comp = false; |
|
192
|
|
|
$this->taxes = []; |
|
193
|
|
|
|
|
194
|
|
|
return $this->bill; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @inheritDoc |
|
199
|
|
|
*/ |
|
200
|
|
|
public function view(array $data = []): \Illuminate\Contracts\View\View |
|
201
|
|
|
{ |
|
202
|
|
|
return View::make('invoices::receipt', array_merge($data, [ |
|
203
|
|
|
'invoice' => $this->bill, |
|
204
|
|
|
'moneyFormatter' => new MoneyFormatter( |
|
205
|
|
|
$this->bill->currency, |
|
206
|
|
|
config('soap.invoices.locale') |
|
207
|
|
|
), |
|
208
|
|
|
])); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @inheritDoc |
|
213
|
|
|
*/ |
|
214
|
|
|
public function pdf(array $data = []): string |
|
215
|
|
|
{ |
|
216
|
|
|
if (! defined('DOMPDF_ENABLE_AUTOLOAD')) { |
|
217
|
|
|
define('DOMPDF_ENABLE_AUTOLOAD', false); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if (file_exists($configPath = base_path().'/vendor/dompdf/dompdf/dompdf_config.inc.php')) { |
|
221
|
|
|
require_once $configPath; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
$dompdf = new Dompdf; |
|
225
|
|
|
$dompdf->loadHtml($this->view($data)->render()); |
|
226
|
|
|
$dompdf->render(); |
|
227
|
|
|
return $dompdf->output(); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @inheritDoc |
|
232
|
|
|
*/ |
|
233
|
|
|
public function download(array $data = []): Response |
|
234
|
|
|
{ |
|
235
|
|
|
$filename = $this->bill->reference . '.pdf'; |
|
236
|
|
|
|
|
237
|
|
|
return new Response($this->pdf($data), 200, [ |
|
238
|
|
|
'Content-Description' => 'File Transfer', |
|
239
|
|
|
'Content-Disposition' => 'attachment; filename="'.$filename.'"', |
|
240
|
|
|
'Content-Transfer-Encoding' => 'binary', |
|
241
|
|
|
'Content-Type' => 'application/pdf', |
|
242
|
|
|
]); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @inheritDoc |
|
247
|
|
|
*/ |
|
248
|
|
|
public function findByReference(string $reference): ?Bill |
|
249
|
|
|
{ |
|
250
|
|
|
return Bill::where('reference', $reference)->withoutGlobalScope(InvoiceScope::class)->first(); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @inheritDoc |
|
255
|
|
|
*/ |
|
256
|
|
|
public function findByReferenceOrFail(string $reference): Bill |
|
257
|
|
|
{ |
|
258
|
|
|
return Bill::where('reference', $reference)->withoutGlobalScope(InvoiceScope::class)->firstOrFail(); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @inheritDoc |
|
263
|
|
|
*/ |
|
264
|
|
|
public function findByInvoiceable(Model $model): Collection |
|
265
|
|
|
{ |
|
266
|
|
|
/* |
|
267
|
|
|
* In order to receive invoices by polymorphic relationship, we pluck invoice ids to find invoices. |
|
268
|
|
|
*/ |
|
269
|
|
|
$bills = $model->invoiceLines()->get('invoice_id')->pluck('invoice_id')->unique(); |
|
270
|
|
|
|
|
271
|
|
|
return Bill::find($bills); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @inheritDoc |
|
276
|
|
|
*/ |
|
277
|
|
|
public function findByRelated(Model $model): Collection |
|
278
|
|
|
{ |
|
279
|
|
|
return $model->bills()->get(); |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|