Completed
Push — main ( 5773b9...467ced )
by Emmanuel
01:34
created

Queries::orders_thisYearByStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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