Completed
Push — main ( 3409a6...9d9395 )
by Emmanuel
01:27
created

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