Completed
Push — main ( f6f9de...8ae71e )
by Emmanuel
01:45
created

BillMe::invoice_paid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
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\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
        string $firstname,
34
        string $lastname,
35
        string $email,
36
        string $mobile_number,
37
        int $amount,
38
        string $payment_method,
39
        string $notes,
40
        string $address,
41
        array $orderitems,
42
        $userid = null
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++) {
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...
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);
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...
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
    public function order_paid($orderid){
145
146
        $invoiceid=Invoice::where('orderid',$orderid)->first()->id;
147
       
148
        $this->invoice_paid($invoiceid);
149
150
151
    }
152
    public function invoice_paid($invoiceid) : void
153
    {
154
155
        $invoice = Invoice::find($invoiceid);
156
        $invoice->status = "paid";
157
        $invoice->save();
158
        
159
        $order=Order::find($invoice->orderid);
160
        $order->status="completed";
161
        $order->save();
162
163
        $this->paid_billing_record($invoiceid);
164
165
166
167
168
    }
169
170
171
    public function paid_billing_record($invoiceid){
172
        
173
      $billing_record=BillingPayment::find(BillingPayment::where('invoiceid',$invoiceid)->first()->id);
174
      $billing_record->status="completed"; 
175
      $billing_record->save();
176
177
    }
178
    /**
179
     * Function that gets you invoice details by using orderid
180
     */
181
182
    public function getInvoiceByOrderId($orderid)
183
    {
184
        return Invoice::where('orderid', $orderid)->first();
185
    }
186
187
    /**
188
     * Function that gets you invoice details by using invoiceid
189
     */
190
191
    public function getInvoiceByInvoiceId($invoiceid)
192
    {
193
194
        return Invoice::find($invoiceid);
195
    }
196
197
198
199
    /**
200
     * Function that gets you order details by using orderid
201
     */
202
203
    public function getOrderByOrderId($orderid)
204
    {
205
        return Order::find($orderid);
206
    }
207
208
    /**
209
     * Function that gets you order details by using invoiceid
210
     */
211
212
    public function getOrderByInvoiceId($invoiceid)
213
    {
214
        return Order::find(Invoice::where('id', $invoiceid)->first()->orderid);
215
    }
216
217
218
    /**
219
     * Function that updates invoice status and returns void
220
     */
221
    public function update_invoice_status(string $invoiceid, string $status): void
222
    {
223
        $invoice = Invoice::find($invoiceid);
224
        $invoice->status = $status;
225
        $invoice->save();
226
    }
227
228
229
    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...
230
    {
231
    }
232
233
234
235
236
    /**
237
     * Function that updates order status and returns void
238
     */
239
    public function update_order_status(string $orderid, string $status): void
240
    {
241
        $order = Order::find($orderid);
242
        $order->status = $status;
243
        $order->save();
244
    }
245
246
247
    /**
248
     * Function that updates an  order  returns void
249
     */
250
    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...
251
    {
252
    }
253
254
255
    /**
256
     * Function that updates and  status to cancelled  returns void
257
     */
258
    public function cancel_order(string $orderid): void
259
    {
260
        $order = Order::find($orderid);
261
        $order->status = "cancelled";
262
        $order->save();
263
    }
264
265
266
    /**
267
     * Function that deletes an order returns void
268
     */
269
    public function delete_order(string $orderid)
270
    {
271
272
        $order = Order::find($orderid);
273
        $order->delete();
274
        $this->delete_invoice($orderid);
275
    }
276
277
    /**
278
     * Function that deletes an invoice returns void
279
     */
280
    public function delete_invoice(string $orderid): void
281
    {
282
283
        $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...
284
    }
285
}
286