Completed
Push — main ( 7b6bf3...02d2be )
by Emmanuel
02:03
created

BillMe::getOrderByOrderId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Mail\Client\Invoices\InvoicePaid;
22
use Epmnzava\BillMe\Models\BillingPayment;
23
use Mail;
24
25
class BillMe extends Queries
26
{
27
28
29
30
    /** 
31
     * A function that triggers order creation
32
     * 
33
     */
34
    public function createOrder(
35
        string $firstname,
36
        string $lastname,
37
        string $email,
38
        string $mobile_number,
39
        int $amount,
40
        string $payment_method,
41
        string $notes,
42
        string $ordertitle="",
43
        string $address,
44
        array $orderitems,
45
        $userid = null
46
    ): Order {
47
48
        $order = new Order;
49
        $order->userid = $userid;
50
        $order->firstname = $firstname;
51
        $order->lastname = $lastname;
52
        $order->email = $email;
53
        $order->mobile_number = $mobile_number;
54
        $order->amount = $amount;
55
        $order->payment_method = $payment_method;
56
        $order->status = "pending";
57
        $order->notes = $notes;
58
        $order->ordertitle=$ordertitle;
59
        $order->address = $address;
60
        $order->date = date("Y-m-d");
61
62
        $order->save();
63
64
        // Loop through order items here
65
        if (!empty($orderitems)) {
66
67
            for ($i = 0; $i < count($orderitems); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

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:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
68
                $orderItem = new OrderItem();
69
                $orderItem->order_id = $order->id;
70
                $orderItem->amount = $orderitems[$i]['amount'];
71
                $orderItem->quantity = $orderitems[$i]['quantity'];
72
                $orderItem->item = $orderitems[$i]['item'];
73
                $orderItem->extra_details = serialize($orderitems[$i]['extra_details']);
74
                $orderItem->date = date("Y-m-d");
75
76
                $orderItem->save();
77
            }
78
        }
79
80
81
        $invoice = $this->createInvoice($order);
82
83
        if (config('bill-me.send_mail') == 1)
84
            $this->sendMailNotifications($order, $invoice);
85
86
        $billing_record = $this->add_billing_record($order, $invoice);
0 ignored issues
show
Unused Code introduced by
$billing_record is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87
88
        return $order;
89
    }
90
91
92
93
    public function add_billing_record(Order $order, Invoice $invoice): BillingPayment
94
    {
95
96
        $bill_payment = new BillingPayment;
97
        $bill_payment->userid = $order->userid;
98
        $bill_payment->invoiceid = $invoice->id;
99
        $bill_payment->orderid = $order->id;
100
        $bill_payment->amount = $invoice->amount;
101
        $bill_payment->date = $order->date;
102
        $bill_payment->save();
103
104
105
        return $bill_payment;
106
    }
107
108
109
    /**
110
     * Function that triggers sending of email notification for orders
111
     */
112
113
    public function sendMailNotifications(Order $order, Invoice $invoice): void
114
    {
115
        Mail::to(["email" => $order->email])->send(new OrderReceived($order));
116
        Mail::to(["email" => config('bill-me.your_mail_address')])->send(new NewOrder($order));
117
        Mail::to(["address" => $invoice->email])->send(new InvoiceCreated($invoice));
118
    }
119
120
121
122
    /**
123
     * Function that creates an invoie from an order
124
     */
125
126
    public function createInvoice(Order $order): Invoice
127
    {
128
129
        $invoice = new Invoice;
130
        $invoice->orderid = $order->id;
131
        $invoice->userid = $order->userid;
132
        $invoice->firstname = $order->firstname;
133
        $invoice->lastname = $order->lastname;
134
        $invoice->mobile_number = $order->mobile_number;
135
        $invoice->email = $order->email;
136
        $invoice->invoice_title=$order->ordertitle;
137
        $invoice->amount = $order->amount;
138
        $invoice->status = $order->status;
139
        $invoice->address = $order->address;
140
        $invoice->date = date('Y-m-d');
141
        $invoice->due_date = Carbon::now()->addDays(config('bill-me.due_date_duration'))->format('Y-m-d');
142
        $invoice->save();
143
144
        $order_update = Order::find($order->id);
145
        $order_update->invoiceid = $invoice->id;
146
        $order_update->save();
147
148
        return $invoice;
149
    }
150
151
152
    public function order_paid($orderid): void
153
    {
154
155
        $invoiceid = Invoice::where('orderid', $orderid)->first()->id;
156
157
        $this->invoice_paid($invoiceid);
158
    }
159
160
161
    /**
162
     * Function gets @param invoiceid and updates order , invoice and billing record that the user has paid
163
     */
164
    public function invoice_paid($invoiceid): Invoice
165
    {
166
167
        $invoice = Invoice::find($invoiceid);
168
        $invoice->status = "paid";
169
        $invoice->save();
170
171
        $order = Order::find($invoice->orderid);
172
        $order->status = "completed";
173
        $order->save();
174
175
        $billing = $this->paid_billing_record($invoiceid);
0 ignored issues
show
Unused Code introduced by
$billing is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
176
177
        //$receiptid = $this->create_receipt($invoiceid, $billingid);
178
179
        // create email notification invoice paid order paid..
180
181
        if (config('bill-me.send_mail') == 1)
182
            Mail::to(["email" => $order->email])->send(new InvoicePaid($invoice));
183
184
185
        return  $invoice;
186
    }
187
188
    /**
189
     * Function gets @param invoiceid and @param billingid and creates receipt
190
     */
191
    public function create_receipt($invoiceid, $billingid): int
192
    {
193
194
        $receipt = new Receipt;
195
        $receipt->invoiceid = $invoiceid;
196
        $receipt->paymentid = $billingid;
197
        $receipt->save();
198
199
        return $receipt->id;
200
    }
201
202
203
204
    public function paid_billing_record($invoiceid): BillingPayment
205
    {
206
207
        $billing_record = BillingPayment::find(BillingPayment::where('invoiceid', $invoiceid)->first()->id);
208
        $billing_record->status = "paid";
209
        $billing_record->save();
210
211
        return  $billing_record;
212
    }
213
    /**
214
     * Function that gets you invoice details by using orderid
215
     */
216
217
    public function getInvoiceByOrderId($orderid)
218
    {
219
        return Invoice::where('orderid', $orderid)->first();
220
    }
221
222
    /**
223
     * Function that gets you invoice details by using invoiceid
224
     */
225
226
    public function getInvoiceByInvoiceId($invoiceid)
227
    {
228
229
        return Invoice::find($invoiceid);
230
    }
231
232
233
234
235
236
    /**
237
     * Function that gets you order details by using orderid
238
     */
239
240
    public function getOrderByOrderId($orderid)
241
    {
242
        return Order::find($orderid);
243
    }
244
245
    /**
246
     * Function that gets you order details by using invoiceid
247
     */
248
249
    public function getOrderByInvoiceId($invoiceid)
250
    {
251
        return Order::find(Invoice::where('id', $invoiceid)->first()->orderid);
252
    }
253
254
255
    /**
256
     * Function that updates invoice status and returns void
257
     */
258
    public function update_invoice_status(string $invoiceid, string $status): void
259
    {
260
        $invoice = Invoice::find($invoiceid);
261
        $invoice->status = $status;
262
        $invoice->save();
263
    }
264
265
    /**
266
     * Function to update invoice
267
     */
268
269
    public function update_invoice(string $invoiceid, Invoice $invoice): Invoice
0 ignored issues
show
Unused Code introduced by
The parameter $invoiceid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $invoice is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
270
    {
271
    }
272
273
274
275
276
    /**
277
     * Function that updates order status and returns void
278
     */
279
    public function update_order_status(string $orderid, string $status): void
280
    {
281
        $order = Order::find($orderid);
282
        $order->status = $status;
283
        $order->save();
284
    }
285
286
287
    /**
288
     * Function that updates an  order  returns void
289
     */
290
    public function update_order(string $order_id, Order $order): void
0 ignored issues
show
Unused Code introduced by
The parameter $order_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $order is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
291
    {
292
    }
293
294
295
    /**
296
     * Function that updates and  status to cancelled  returns void
297
     */
298
    public function cancel_order(string $orderid): void
299
    {
300
        $order = Order::find($orderid);
301
        $order->status = "cancelled";
302
        $order->save();
303
    }
304
305
306
    /**
307
     * Function that deletes an order returns void
308
     */
309
    public function delete_order(string $orderid)
310
    {
311
312
        $order = Order::find($orderid);
313
        $order->delete();
314
        $this->delete_invoice($orderid);
315
    }
316
317
    /**
318
     * Function that deletes an invoice returns void
319
     */
320
    public function delete_invoice(string $orderid): void
321
    {
322
323
        $invoice = Invoice::where('orderid', $orderid)->delete();
0 ignored issues
show
Unused Code introduced by
$invoice is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
324
    }
325
}
326