Completed
Push — main ( 8ae71e...a8a380 )
by Emmanuel
01:33
created

BillMe::create_receipt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
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++) {
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...
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);
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...
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);
0 ignored issues
show
Unused Code introduced by
$receiptid 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...
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
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...
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
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...
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();
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...
305
    }
306
}
307