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\Invoice; |
15
|
|
|
use Epmnzava\BillMe\Models\OrderItem; |
16
|
|
|
use Epmnzava\BillMe\Mail\Client\Invoices\InvoiceCreated; |
17
|
|
|
use Epmnzava\BillMe\Mail\Client\OrderReceived; |
18
|
|
|
use Epmnzava\BillMe\Mail\Merchant\NewOrder; |
19
|
|
|
use Carbon\Carbon; |
20
|
|
|
use Epmnzava\BillMe\Models\BillingPayment; |
21
|
|
|
use Mail; |
22
|
|
|
|
23
|
|
|
class BillMe extends Queries |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* A function that triggers order creation |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
|
|
public function createOrder( |
33
|
|
|
int $userid = null, |
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
|
|
|
): Order { |
44
|
|
|
|
45
|
|
|
$order = new Order; |
46
|
|
|
$order->userid = $userid; |
47
|
|
|
$order->firstname = $firstname; |
48
|
|
|
$order->lastname = $lastname; |
49
|
|
|
$order->email = $email; |
50
|
|
|
$order->mobile_number = $mobile_number; |
51
|
|
|
$order->amount = $amount; |
52
|
|
|
$order->payment_method = $payment_method; |
53
|
|
|
$order->status = "pending"; |
54
|
|
|
$order->notes = $notes; |
55
|
|
|
$order->address = $address; |
56
|
|
|
$order->date = date("Y-m-d"); |
57
|
|
|
|
58
|
|
|
$order->save(); |
59
|
|
|
|
60
|
|
|
// Loop through order items here |
61
|
|
|
if (!empty($orderitems)) { |
62
|
|
|
|
63
|
|
|
for ($i = 0; $i < count($orderitems); $i++) { |
|
|
|
|
64
|
|
|
$orderItem = new OrderItem(); |
65
|
|
|
$orderItem->order_id = $order->id; |
66
|
|
|
$orderItem->amount = $orderitems[$i]->amount; |
67
|
|
|
$orderItem->quantity = $orderitems[$i]->quantity; |
68
|
|
|
$orderItem->item = $orderitems[$i]->item; |
69
|
|
|
$orderItem->extra_details = serialize($orderitems[$i]->extra_details); |
70
|
|
|
$orderItem->save(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
$invoice = $this->createInvoice($order); |
76
|
|
|
|
77
|
|
|
if (config('bill-me.send_mail') == 1) |
78
|
|
|
$this->sendMailNotifications($order, $invoice); |
79
|
|
|
|
80
|
|
|
$billing_record = $this->add_billing_record($order, $invoice); |
|
|
|
|
81
|
|
|
|
82
|
|
|
return $order; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
|
87
|
|
|
public function add_billing_record(Order $order, Invoice $invoice) |
88
|
|
|
{ |
89
|
|
|
|
90
|
|
|
$bill_payment = new BillingPayment; |
91
|
|
|
$bill_payment->userid = $order->userid; |
92
|
|
|
$bill_payment->invoiceid = $invoice->id; |
93
|
|
|
$bill_payment->orderid = $order->id; |
94
|
|
|
$bill_payment->amount = $invoice->amount; |
95
|
|
|
$bill_payment->date = $order->date; |
96
|
|
|
$bill_payment->save(); |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
return $bill_payment; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Function that triggers sending of email notification for orders |
105
|
|
|
*/ |
106
|
|
|
|
107
|
|
|
public function sendMailNotifications(Order $order, Invoice $invoice) |
108
|
|
|
{ |
109
|
|
|
Mail::to(["email" => $order->email, "name" => $order->email])->send(new OrderReceived($order)); |
110
|
|
|
Mail::to(["email" => $order->email, "name" => $order->email])->send(new NewOrder($order)); |
111
|
|
|
Mail::to(["address" => $invoice->email, "name" => $invoice->email])->send(new InvoiceCreated($invoice)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Function that creates an invoie from an order |
118
|
|
|
*/ |
119
|
|
|
|
120
|
|
|
public function createInvoice(Order $order): Invoice |
121
|
|
|
{ |
122
|
|
|
|
123
|
|
|
$invoice = new Invoice; |
124
|
|
|
$invoice->orderid = $order->id; |
125
|
|
|
$invoice->userid = $order->userid; |
126
|
|
|
$invoice->firstname = $order->firstname; |
127
|
|
|
$invoice->lastname = $order->lastname; |
128
|
|
|
$invoice->mobile_number = $order->mobile_number; |
129
|
|
|
$invoice->email = $order->email; |
130
|
|
|
$invoice->amount = $order->amount; |
131
|
|
|
$invoice->status = $order->status; |
132
|
|
|
$invoice->address = $order->address; |
133
|
|
|
$invoice->date = date('Y-m-d'); |
134
|
|
|
$invoice->save(); |
135
|
|
|
|
136
|
|
|
$order_update = Order::find($order->id); |
137
|
|
|
$order_update->invoiceid = $invoice->id; |
138
|
|
|
$order_update->save(); |
139
|
|
|
|
140
|
|
|
return $invoice; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Function that gets you invoice details by using orderid |
146
|
|
|
*/ |
147
|
|
|
|
148
|
|
|
public function getInvoiceByOrderId($orderid) |
149
|
|
|
{ |
150
|
|
|
return Invoice::where('orderid', $orderid)->first(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Function that gets you invoice details by using invoiceid |
155
|
|
|
*/ |
156
|
|
|
|
157
|
|
|
public function getInvoiceByInvoiceId($invoiceid) |
158
|
|
|
{ |
159
|
|
|
|
160
|
|
|
return Invoice::find($invoiceid); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Function that gets you order details by using orderid |
167
|
|
|
*/ |
168
|
|
|
|
169
|
|
|
public function getOrderByOrderId($orderid) |
170
|
|
|
{ |
171
|
|
|
return Order::find($orderid); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Function that gets you order details by using invoiceid |
176
|
|
|
*/ |
177
|
|
|
|
178
|
|
|
public function getOrderByInvoiceId($invoiceid) |
179
|
|
|
{ |
180
|
|
|
return Order::find(Invoice::where('id', $invoiceid)->first()->orderid); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Function that updates invoice status and returns void |
186
|
|
|
*/ |
187
|
|
|
public function update_invoice_status(string $invoiceid, string $status): void |
188
|
|
|
{ |
189
|
|
|
$invoice = Invoice::find($invoiceid); |
190
|
|
|
$invoice->status = $status; |
191
|
|
|
$invoice->save(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
public function update_invoice(string $invoiceid, Invoice $invoice): void |
|
|
|
|
196
|
|
|
{ |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Function that updates order status and returns void |
204
|
|
|
*/ |
205
|
|
|
public function update_order_status(string $orderid, string $status): void |
206
|
|
|
{ |
207
|
|
|
$order = Order::find($orderid); |
208
|
|
|
$order->status = $status; |
209
|
|
|
$order->save(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Function that updates an order returns void |
215
|
|
|
*/ |
216
|
|
|
public function update_order(string $order_id, Order $order): void |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Function that updates and status to cancelled returns void |
223
|
|
|
*/ |
224
|
|
|
public function cancel_order(string $orderid): void |
225
|
|
|
{ |
226
|
|
|
$order = Order::find($orderid); |
227
|
|
|
$order->status = "cancelled"; |
228
|
|
|
$order->save(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Function that deletes an order returns void |
234
|
|
|
*/ |
235
|
|
|
public function delete_order(string $orderid) |
236
|
|
|
{ |
237
|
|
|
|
238
|
|
|
$order = Order::find($orderid); |
239
|
|
|
$order->delete(); |
240
|
|
|
$this->delete_invoice($orderid); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Function that deletes an invoice returns void |
245
|
|
|
*/ |
246
|
|
|
public function delete_invoice(string $orderid): void |
247
|
|
|
{ |
248
|
|
|
|
249
|
|
|
$invoice = Invoice::where('orderid', $orderid)->delete(); |
|
|
|
|
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
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: