|
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
|
|
|
|
|
21
|
|
|
use Mail; |
|
22
|
|
|
|
|
23
|
|
|
class BillMe extends Queries |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** A function that triggers order creation */ |
|
29
|
|
|
public function createOrder( |
|
30
|
|
|
string $firstname, |
|
31
|
|
|
string $lastname, |
|
32
|
|
|
string $email, |
|
33
|
|
|
string $mobile_number, |
|
34
|
|
|
int $amount, |
|
35
|
|
|
string $payment_method, |
|
36
|
|
|
string $notes, |
|
37
|
|
|
string $address, |
|
38
|
|
|
array $orderitems |
|
39
|
|
|
) { |
|
40
|
|
|
|
|
41
|
|
|
$order = new Order; |
|
42
|
|
|
$order->firstname = $firstname; |
|
43
|
|
|
$order->lastname = $lastname; |
|
44
|
|
|
$order->email = $email; |
|
45
|
|
|
$order->mobile_number = $mobile_number; |
|
46
|
|
|
$order->amount = $amount; |
|
47
|
|
|
$order->payment_method = $payment_method; |
|
48
|
|
|
$order->status = "pending"; |
|
49
|
|
|
$order->notes = $notes; |
|
50
|
|
|
$order->address = $address; |
|
51
|
|
|
$order->date = date("Y-m-d"); |
|
52
|
|
|
|
|
53
|
|
|
$order->save(); |
|
54
|
|
|
|
|
55
|
|
|
// order items ... here |
|
56
|
|
|
|
|
57
|
|
|
if (!empty($orderitems)) { |
|
58
|
|
|
/* $orderItem=new OrderItem(); |
|
59
|
|
|
$orderItem->order_id=$order->id; |
|
60
|
|
|
$orderItem->amount=$orderitems->amount; |
|
61
|
|
|
$orderItem->quantity=$orderitems->quantity; |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
*/ |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
//perform checks if the user needs email service use a separate function here add bulk sms functionality ... |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$this->createInvoice($order); |
|
71
|
|
|
|
|
72
|
|
|
if (config('bill-me.send_mail') == 1) |
|
73
|
|
|
$this->sendMailNotifications($order); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
public function sendMailNotifications(Order $order) |
|
78
|
|
|
{ |
|
79
|
|
|
Mail::to(["email" => $order->email, "name" => $order->email])->send(new OrderReceived($order)); |
|
80
|
|
|
Mail::to(["email" => $order->email, "name" => $order->email])->send(new NewOrder($order)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
public function createInvoice(Order $order) |
|
87
|
|
|
{ |
|
88
|
|
|
|
|
89
|
|
|
$invoice = new Invoice; |
|
90
|
|
|
$invoice->orderid = $order->id; |
|
91
|
|
|
$invoice->firstname = $order->firstname; |
|
92
|
|
|
$invoice->lastname = $order->lastname; |
|
93
|
|
|
$invoice->mobile_number = $order->mobile_number; |
|
94
|
|
|
$invoice->email = $order->email; |
|
95
|
|
|
$invoice->amount = $order->amount; |
|
96
|
|
|
$invoice->status = $order->status; |
|
97
|
|
|
$invoice->address = $order->address; |
|
98
|
|
|
$invoice->date = date('Y-m-d'); |
|
99
|
|
|
$invoice->save(); |
|
100
|
|
|
|
|
101
|
|
|
if (config('bill-me.send_mail')) |
|
102
|
|
|
Mail::to(["address" => $invoice->email, "name" => $invoice->email])->send(new InvoiceCreated($invoice)); |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
$order_update = Order::find($order->id); |
|
108
|
|
|
$order_update->invoiceid = $invoice->id; |
|
109
|
|
|
$order_update->save(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Function that gets you invoice details by using orderid |
|
115
|
|
|
*/ |
|
116
|
|
|
|
|
117
|
|
|
public function getInvoiceByOrderId($orderid) |
|
118
|
|
|
{ |
|
119
|
|
|
return Invoice::where('orderid', $orderid)->first(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Function that gets you invoice details by using invoiceid |
|
124
|
|
|
*/ |
|
125
|
|
|
|
|
126
|
|
|
public function getInvoiceByInvoiceId($invoiceid) |
|
127
|
|
|
{ |
|
128
|
|
|
|
|
129
|
|
|
return Invoice::find($invoiceid); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Function that gets you order details by using orderid |
|
136
|
|
|
*/ |
|
137
|
|
|
|
|
138
|
|
|
public function getOrderByOrderId($orderid) |
|
139
|
|
|
{ |
|
140
|
|
|
return Order::find($orderid); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Function that gets you order details by using invoiceid |
|
145
|
|
|
*/ |
|
146
|
|
|
|
|
147
|
|
|
public function getOrderByInvoiceId($invoiceid) |
|
148
|
|
|
{ |
|
149
|
|
|
return Order::find(Invoice::where('id', $invoiceid)->first()->orderid); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Function that updates invoice status and returns void |
|
155
|
|
|
*/ |
|
156
|
|
|
public function update_invoice_status(string $invoiceid, string $status): void |
|
157
|
|
|
{ |
|
158
|
|
|
$invoice = Invoice::find($invoiceid); |
|
159
|
|
|
$invoice->status = $status; |
|
160
|
|
|
$invoice->save(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
public function update_invoice(string $invoiceid, Invoice $invoice): void |
|
|
|
|
|
|
165
|
|
|
{ |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Function that updates order status and returns void |
|
173
|
|
|
*/ |
|
174
|
|
|
public function update_order_status(string $orderid, string $status): void |
|
175
|
|
|
{ |
|
176
|
|
|
$order = Order::find($orderid); |
|
177
|
|
|
$order->status = $status; |
|
178
|
|
|
$order->save(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
public function update_order(string $order_id, Order $order): void |
|
|
|
|
|
|
184
|
|
|
{ |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function cancel_order(string $orderid): void |
|
188
|
|
|
{ |
|
189
|
|
|
$order = Order::find($orderid); |
|
190
|
|
|
$order->status = "cancelled"; |
|
191
|
|
|
$order->save(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
public function delete_order(string $orderid) |
|
195
|
|
|
{ |
|
196
|
|
|
|
|
197
|
|
|
$order = Order::find($orderid); |
|
198
|
|
|
$order->delete(); |
|
199
|
|
|
$this->delete_invoice($orderid); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function delete_invoice(string $orderid): void |
|
203
|
|
|
{ |
|
204
|
|
|
|
|
205
|
|
|
$invoice = Invoice::where('orderid', $orderid)->delete(); |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.