Completed
Push — main ( 84805f...ba9f1d )
by Emmanuel
01:22
created

Queries::editPaymentMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
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 Carbon\Carbon;
14
use Epmnzava\BillMe\Models\BillingPayment;
15
use Epmnzava\BillMe\Models\Order;
16
use Epmnzava\BillMe\Models\Invoice;
17
use Epmnzava\BillMe\Models\OrderItem;
18
use Epmnzava\BillMe\Mail\Client\Invoices\InvoiceCreated;
19
use Epmnzava\BillMe\Mail\Client\OrderReceived;
20
use Epmnzava\BillMe\Mail\Merchant\NewOrder;
21
use Epmnzava\BillMe\Models\PaymentMethod;
22
23
24
use Mail;
25
26
class Queries extends Stats
27
{
28
29
    public function orders()
30
    {
31
32
        return Order::all();
33
    }
34
35
36
    public function orders_orderby($orderby)
0 ignored issues
show
Unused Code introduced by
The parameter $orderby 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...
37
    {
38
    }
39
40
41
42
    public function orders_today()
43
    {
44
        return Order::whereDate('date', date('Y-m-d'))->get();
45
    }
46
47
48
49
    public function orders_todayByStatus($status)
50
    {
51
        return Order::whereDate('date', date('Y-m-d'))->where('status',$status)->get();
52
    }
53
54
55
56
    public function orders_thisMonth()
57
    {
58
        return Order::whereYear('date', date('Y'))->whereMonth('date', date('m'))->get();
59
    }
60
61
     public function orders_thisMonthByStatus($status)
62
    {
63
        return Order::whereYear('date', date('Y'))->whereMonth('date', date('m'))->where('status',$status)->get();
64
    }
65
66
67
68
    public function orders_thisYear()
69
    {
70
        return Order::whereYear('date', date('Y'))->get();
71
72
    }
73
74
75
     public function orders_thisYearByStatus($status)
76
    {
77
        return Order::whereYear('date', date('Y'))->where('status',$status)->get();
78
79
    }
80
81
82
    public function get_orders_with_status($status)
83
    {
84
        return Order::where('status', $status)->get();
85
    }
86
87
    public function pending_orders()
88
    {
89
        return Order::where('status', "pending")->get();
90
    }
91
92
    public function cancelled_orders()
93
    {
94
        return Order::where('status', "cancelled")->get();
95
    }
96
97
    public function completed_orders()
98
    {
99
        return Order::where('status', "completed")->get();
100
    }
101
102
    public function getOrderById($orderid)
103
    {
104
        return Order::find($orderid);
105
    }
106
107
    public function getOrdersOnDate($date)
108
    {
109
        return Order::where('date', $date)->get();
110
    }
111
112
113
    public function getOrdersOnDateRange($startdate, $enddate)
0 ignored issues
show
Unused Code introduced by
The parameter $startdate 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 $enddate 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...
114
    {
115
    }
116
117
118
    /** Function to get an invoice */
119
    public function getInvoiceById($invoiceid)
120
    {
121
        return Invoice::find($invoiceid);
122
    }
123
124
125
    /** Function to get an invoice */
126
    public function getInvoiceByOrderId($orderid)
127
    {
128
        return Invoice::find(Order::where('id', $orderid)->first()->invoiceid);
129
    }
130
131
132
133
    /**
134
     *
135
     * User Queries
136
     */
137
138
139
    /** Function to get given user orders */
140
    public function getUserOrders($userid)
141
    {
142
        return Order::where('userid', $userid)->get();
143
    }
144
145
146
    /** Function to get given user orders by status */
147
    public function getUserOrdersByStatus($userid, $status)
148
    {
149
        return Order::where('userid', $userid)->where('status', $status)->get();
150
    }
151
152
153
154
155
    /** Function to get  orders by status */
156
    public function getOrdersByStatus( $status)
157
    {
158
        return Order::where('status', $status)->get();
159
    }
160
161
162
163
    /** Function to get given user invoices */
164
    public function getUserInvoices($userid)
165
    {
166
        return Invoice::where('userid', $userid)->get();
167
    }
168
169
170
    /** Function that returns total number of user invoices */
171
    public function totalUserInvoices($userid): int
172
    {
173
        return Invoice::where('userid', $userid)->count();
174
    }
175
176
177
178
179
    /** Function to get given user invoices by status */
180
    public function getUserInvoiceByStatus($userid, $status)
181
    {
182
        return Invoice::where('userid', $userid)->where('status', $status)->get();
183
    }
184
185
186
187
188
    /** Function to get given all  invoices by status */
189
    public function getInvoiceByStatus( $status)
190
    {
191
        return Invoice::where('status', $status)->get();
192
    }
193
194
195
    /**
196
     * @param $userid
197
     * @param $status
198
     * @return mixed
199
     * Function to get given user invoices by status
200
     */
201
    public function sumUserInvoiceByStatus($userid, $status) : int
202
    {
203
        return Invoice::where('userid', $userid)->where('status', $status)->sum('amount');
204
    }
205
206
207
208
209
210
    /**
211
     * @param $userid
212
     * @param $status
213
     * @return mixed
214
     *  Function to get given user invoices by status
215
     */
216
    public function totalUserInvoiceByStatus($userid, $status)
217
    {
218
        return Invoice::where('userid', $userid)->where('status', $status)->count();
219
    }
220
221
    /** Function that gets full  billing history */
222
223
    public function getAllBillingHistory()
224
    {
225
226
        return BillingPayment::all();
227
    }
228
229
230
    /** Function that gets full user billing history*/
231
232
    public function getUserBillingHistory($userid)
233
    {
234
235
        return BillingPayment::where('userid', $userid)->get();
236
    }
237
238
239
240
241
    public function getUserBillingHistoryByStartDate($userid, $start_date)
242
    {
243
244
        return BillingPayment::where('userid', $userid)->where('date', $start_date)->get();
245
    }
246
247
248
    /** Function to get payment history of a given user for a given period */
249
250
    public function getUserBillingHistoryBetweenDates($userid, $start_date, $enddate)
251
    {
252
253
        return BillingPayment::where('userid', $userid)->whereBetween('date', [$start_date, $enddate])->get();
254
    }
255
256
257
    /**
258
     * Returns the model instance to be updated
259
     */
260
    public function updateBillingHistory($invoiceid): BillingPayment
261
    {
262
        return BillingPayment::find(BillingPayment::where('invoiceid', $invoiceid)->first()->id);
263
    }
264
265
266
    /**
267
     * Returns the model instance to be updated
268
     */
269
    public function updateInvoiceByInstance($invoiceid): Invoice
270
    {
271
        return Invoice::find($invoiceid);
272
    }
273
274
    /**
275
     *
276
     * Returns updated invoice with new due_date
277
     */
278
    public function updateDueDate($date, $invoiceid): Invoice
279
    {
280
        $invoice = $this->updateInvoiceByInstance($invoiceid);
281
        $invoice->due_date = $date;
282
        $invoice->save();
283
        return $invoice;
284
    }
285
286
287
288
 public function getBillingHistoryByStatus($status)
289
    {
290
291
        return BillingPayment::where('status', $status)->get();
292
    }
293
294
295
    public function getUserBillingHistoryByStatus($userid, $status)
296
    {
297
298
        return BillingPayment::where('userid', $userid)->where('status', $status)->get();
299
    }
300
301
    /** Return OrderItems for particular order
302
     *@param $orderid
303
     **/
304
    public function getOrderItems($orderid)
305
    {
306
307
        return OrderItem::where('order_id', $orderid)->get();
308
    }
309
310
311
    /** Return OrderItems for particular order gets invoiceid
312
     *@param $invoiceid
313
     **/
314
    public function getOrderItemsByInvoiceId($invoiceid)
315
    {
316
317
        return OrderItem::where('order_id', Invoice::where('id', $invoiceid)->first()->orderid)->get();
318
    }
319
320
    public function getPaymentMethods(){
321
322
        return PaymentMethod::all();
323
    }
324
325
      public function getPaymentMethodById($pid){
0 ignored issues
show
Unused Code introduced by
The parameter $pid 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...
326
327
        return PaymentMethod::find($id);
0 ignored issues
show
Bug introduced by
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
328
    }
329
330
        public function editPaymentMethod($pmethod_id, $pmethod)
331
    {
332
        $pmethodObj = PaymentMethod::find($pmethod_id);
333
        $pmethodObj->pmethod = $pmethod;
334
        $pmethodObj->save();
335
        return $pmethodObj;
336
    }
337
338
    
339
    public function addPaymentMethod(string $pmethod)
340
    {
341
342
343
        $pmethodObj = new PaymentMethod;
344
        $pmethodObj->pmethod=$pmethod;
345
        $pmethodObj->save();
346
347
      
348
        return $pmethodObj;
349
    }
350
351
    
352
}
353