1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Author: Emmanuel Paul Mnzava |
5
|
|
|
* Twitter: @epmnzava |
6
|
|
|
* Github: https://github.com/dbrax/bill-me |
7
|
|
|
* Email: [email protected] |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Epmnzava\BillMe; |
12
|
|
|
|
13
|
|
|
use Epmnzava\BillMe\Models\Order; |
14
|
|
|
use Epmnzava\BillMe\Models\Receipt; |
15
|
|
|
use Epmnzava\BillMe\Models\Invoice; |
16
|
|
|
use Epmnzava\BillMe\Models\OrderItem; |
17
|
|
|
use Epmnzava\BillMe\Mail\Client\Invoices\InvoiceCreated; |
18
|
|
|
use Epmnzava\BillMe\Mail\Client\OrderReceived; |
19
|
|
|
use Epmnzava\BillMe\Mail\Merchant\NewOrder; |
20
|
|
|
use Carbon\Carbon; |
21
|
|
|
use Epmnzava\BillMe\Models\BillingPayment; |
22
|
|
|
use Mail; |
23
|
|
|
|
24
|
|
|
class BillMe extends Queries |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* A function that triggers order creation |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
public function createOrder( |
34
|
|
|
string $firstname, |
35
|
|
|
string $lastname, |
36
|
|
|
string $email, |
37
|
|
|
string $mobile_number, |
38
|
|
|
int $amount, |
39
|
|
|
string $payment_method, |
40
|
|
|
string $notes, |
41
|
|
|
string $address, |
42
|
|
|
array $orderitems, |
43
|
|
|
$userid = null |
44
|
|
|
): Order { |
45
|
|
|
|
46
|
|
|
$order = new Order; |
47
|
|
|
$order->userid = $userid; |
48
|
|
|
$order->firstname = $firstname; |
49
|
|
|
$order->lastname = $lastname; |
50
|
|
|
$order->email = $email; |
51
|
|
|
$order->mobile_number = $mobile_number; |
52
|
|
|
$order->amount = $amount; |
53
|
|
|
$order->payment_method = $payment_method; |
54
|
|
|
$order->status = "pending"; |
55
|
|
|
$order->notes = $notes; |
56
|
|
|
$order->address = $address; |
57
|
|
|
$order->date = date("Y-m-d"); |
58
|
|
|
|
59
|
|
|
$order->save(); |
60
|
|
|
|
61
|
|
|
// Loop through order items here |
62
|
|
|
if (!empty($orderitems)) { |
63
|
|
|
|
64
|
|
|
for ($i = 0; $i < count($orderitems); $i++) { |
|
|
|
|
65
|
|
|
$orderItem = new OrderItem(); |
66
|
|
|
$orderItem->order_id = $order->id; |
67
|
|
|
$orderItem->amount = $orderitems[$i]->amount; |
68
|
|
|
$orderItem->quantity = $orderitems[$i]->quantity; |
69
|
|
|
$orderItem->item = $orderitems[$i]->item; |
70
|
|
|
$orderItem->extra_details = serialize($orderitems[$i]->extra_details); |
71
|
|
|
$orderItem->save(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
$invoice = $this->createInvoice($order); |
77
|
|
|
|
78
|
|
|
if (config('bill-me.send_mail') == 1) |
79
|
|
|
$this->sendMailNotifications($order, $invoice); |
80
|
|
|
|
81
|
|
|
$billing_record = $this->add_billing_record($order, $invoice); |
|
|
|
|
82
|
|
|
|
83
|
|
|
return $order; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
|
88
|
|
|
public function add_billing_record(Order $order, Invoice $invoice) |
89
|
|
|
{ |
90
|
|
|
|
91
|
|
|
$bill_payment = new BillingPayment; |
92
|
|
|
$bill_payment->userid = $order->userid; |
93
|
|
|
$bill_payment->invoiceid = $invoice->id; |
94
|
|
|
$bill_payment->orderid = $order->id; |
95
|
|
|
$bill_payment->amount = $invoice->amount; |
96
|
|
|
$bill_payment->date = $order->date; |
97
|
|
|
$bill_payment->save(); |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
return $bill_payment; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Function that triggers sending of email notification for orders |
106
|
|
|
*/ |
107
|
|
|
|
108
|
|
|
public function sendMailNotifications(Order $order, Invoice $invoice) |
109
|
|
|
{ |
110
|
|
|
Mail::to(["email" => $order->email, "name" => $order->email])->send(new OrderReceived($order)); |
111
|
|
|
Mail::to(["email" => $order->email, "name" => $order->email])->send(new NewOrder($order)); |
112
|
|
|
Mail::to(["address" => $invoice->email, "name" => $invoice->email])->send(new InvoiceCreated($invoice)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Function that creates an invoie from an order |
119
|
|
|
*/ |
120
|
|
|
|
121
|
|
|
public function createInvoice(Order $order): Invoice |
122
|
|
|
{ |
123
|
|
|
|
124
|
|
|
$invoice = new Invoice; |
125
|
|
|
$invoice->orderid = $order->id; |
126
|
|
|
$invoice->userid = $order->userid; |
127
|
|
|
$invoice->firstname = $order->firstname; |
128
|
|
|
$invoice->lastname = $order->lastname; |
129
|
|
|
$invoice->mobile_number = $order->mobile_number; |
130
|
|
|
$invoice->email = $order->email; |
131
|
|
|
$invoice->amount = $order->amount; |
132
|
|
|
$invoice->status = $order->status; |
133
|
|
|
$invoice->address = $order->address; |
134
|
|
|
$invoice->date = date('Y-m-d'); |
135
|
|
|
$invoice->save(); |
136
|
|
|
|
137
|
|
|
$order_update = Order::find($order->id); |
138
|
|
|
$order_update->invoiceid = $invoice->id; |
139
|
|
|
$order_update->save(); |
140
|
|
|
|
141
|
|
|
return $invoice; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
public function order_paid($orderid) |
146
|
|
|
{ |
147
|
|
|
|
148
|
|
|
$invoiceid = Invoice::where('orderid', $orderid)->first()->id; |
149
|
|
|
|
150
|
|
|
$this->invoice_paid($invoiceid); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Function gets @param invoiceid and updates order , invoice and billing record that the user has paid |
156
|
|
|
*/ |
157
|
|
|
public function invoice_paid($invoiceid): void |
158
|
|
|
{ |
159
|
|
|
|
160
|
|
|
$invoice = Invoice::find($invoiceid); |
161
|
|
|
$invoice->status = "paid"; |
162
|
|
|
$invoice->save(); |
163
|
|
|
|
164
|
|
|
$order = Order::find($invoice->orderid); |
165
|
|
|
$order->status = "completed"; |
166
|
|
|
$order->save(); |
167
|
|
|
|
168
|
|
|
$billingid = $this->paid_billing_record($invoiceid); |
169
|
|
|
|
170
|
|
|
$receiptid=$this->create_receipt($invoiceid, $billingid); |
|
|
|
|
171
|
|
|
|
172
|
|
|
// create email notification invoice paid order paid.. |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Function gets @param invoiceid and @param billingid and creates receipt |
177
|
|
|
*/ |
178
|
|
|
public function create_receipt($invoiceid, $billingid) : int |
179
|
|
|
{ |
180
|
|
|
|
181
|
|
|
$receipt = new Receipt; |
182
|
|
|
$receipt->invoiceid = $invoiceid; |
183
|
|
|
$receipt->paymentid = $billingid; |
184
|
|
|
$receipt->save(); |
185
|
|
|
|
186
|
|
|
return $receipt->id; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
public function paid_billing_record($invoiceid) |
191
|
|
|
{ |
192
|
|
|
|
193
|
|
|
$billing_record = BillingPayment::find(BillingPayment::where('invoiceid', $invoiceid)->first()->id); |
194
|
|
|
$billing_record->status = "completed"; |
195
|
|
|
$billing_record->save(); |
196
|
|
|
|
197
|
|
|
return $billing_record->id; |
198
|
|
|
} |
199
|
|
|
/** |
200
|
|
|
* Function that gets you invoice details by using orderid |
201
|
|
|
*/ |
202
|
|
|
|
203
|
|
|
public function getInvoiceByOrderId($orderid) |
204
|
|
|
{ |
205
|
|
|
return Invoice::where('orderid', $orderid)->first(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Function that gets you invoice details by using invoiceid |
210
|
|
|
*/ |
211
|
|
|
|
212
|
|
|
public function getInvoiceByInvoiceId($invoiceid) |
213
|
|
|
{ |
214
|
|
|
|
215
|
|
|
return Invoice::find($invoiceid); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Function that gets you order details by using orderid |
222
|
|
|
*/ |
223
|
|
|
|
224
|
|
|
public function getOrderByOrderId($orderid) |
225
|
|
|
{ |
226
|
|
|
return Order::find($orderid); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Function that gets you order details by using invoiceid |
231
|
|
|
*/ |
232
|
|
|
|
233
|
|
|
public function getOrderByInvoiceId($invoiceid) |
234
|
|
|
{ |
235
|
|
|
return Order::find(Invoice::where('id', $invoiceid)->first()->orderid); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Function that updates invoice status and returns void |
241
|
|
|
*/ |
242
|
|
|
public function update_invoice_status(string $invoiceid, string $status): void |
243
|
|
|
{ |
244
|
|
|
$invoice = Invoice::find($invoiceid); |
245
|
|
|
$invoice->status = $status; |
246
|
|
|
$invoice->save(); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
public function update_invoice(string $invoiceid, Invoice $invoice): void |
|
|
|
|
251
|
|
|
{ |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
|
255
|
|
|
|
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Function that updates order status and returns void |
259
|
|
|
*/ |
260
|
|
|
public function update_order_status(string $orderid, string $status): void |
261
|
|
|
{ |
262
|
|
|
$order = Order::find($orderid); |
263
|
|
|
$order->status = $status; |
264
|
|
|
$order->save(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Function that updates an order returns void |
270
|
|
|
*/ |
271
|
|
|
public function update_order(string $order_id, Order $order): void |
|
|
|
|
272
|
|
|
{ |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Function that updates and status to cancelled returns void |
278
|
|
|
*/ |
279
|
|
|
public function cancel_order(string $orderid): void |
280
|
|
|
{ |
281
|
|
|
$order = Order::find($orderid); |
282
|
|
|
$order->status = "cancelled"; |
283
|
|
|
$order->save(); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Function that deletes an order returns void |
289
|
|
|
*/ |
290
|
|
|
public function delete_order(string $orderid) |
291
|
|
|
{ |
292
|
|
|
|
293
|
|
|
$order = Order::find($orderid); |
294
|
|
|
$order->delete(); |
295
|
|
|
$this->delete_invoice($orderid); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Function that deletes an invoice returns void |
300
|
|
|
*/ |
301
|
|
|
public function delete_invoice(string $orderid): void |
302
|
|
|
{ |
303
|
|
|
|
304
|
|
|
$invoice = Invoice::where('orderid', $orderid)->delete(); |
|
|
|
|
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: