Completed
Push — main ( 890fd9...e70aa0 )
by Emmanuel
08:21
created

Queries::getBillingHistoryByStatus()   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::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
    /** Function to get given user invoices */
118
    public function getUserInvoices($userid)
119
    {
120
        return Invoice::where('userid', $userid)->get();
121
    }
122
123
124
    /** Function that returns total number of user invoices */
125
    public function totalUserInvoices($userid): int
126
    {
127
        return Invoice::where('userid', $userid)->count();
128
    }
129
130
131
132
133
    /** Function to get given user invoices by status */
134
    public function getUserInvoiceByStatus($userid, $status)
135
    {
136
        return Invoice::where('userid', $userid)->where('status', $status)->get();
137
    }
138
139
140
141
142
    /**
143
     * @param $userid
144
     * @param $status
145
     * @return mixed
146
     * Function to get given user invoices by status
147
     */
148
    public function sumUserInvoiceByStatus($userid, $status) : int
149
    {
150
        return Invoice::where('userid', $userid)->where('status', $status)->sum('amount');
151
    }
152
153
154
155
156
157
    /**
158
     * @param $userid
159
     * @param $status
160
     * @return mixed
161
     *  Function to get given user invoices by status
162
     */
163
    public function totalUserInvoiceByStatus($userid, $status)
164
    {
165
        return Invoice::where('userid', $userid)->where('status', $status)->count();
166
    }
167
168
    /** Function that gets full  billing history */
169
170
    public function getAllBillingHistory()
171
    {
172
173
        return BillingPayment::all();
174
    }
175
176
177
    /** Function that gets full user billing history*/
178
179
    public function getUserBillingHistory($userid)
180
    {
181
182
        return BillingPayment::where('userid', $userid)->get();
183
    }
184
185
186
187
188
    public function getUserBillingHistoryByStartDate($userid, $start_date)
189
    {
190
191
        return BillingPayment::where('userid', $userid)->where('date', $start_date)->get();
192
    }
193
194
195
    /** Function to get payment history of a given user for a given period */
196
197
    public function getUserBillingHistoryBetweenDates($userid, $start_date, $enddate)
198
    {
199
200
        return BillingPayment::where('userid', $userid)->whereBetween('date', [$start_date, $enddate])->get();
201
    }
202
203
204
    /**
205
     * Returns the model instance to be updated
206
     */
207
    public function updateBillingHistory($invoiceid): BillingPayment
208
    {
209
        return BillingPayment::find(BillingPayment::where('invoiceid', $invoiceid)->first()->id);
210
    }
211
212
213
    /**
214
     * Returns the model instance to be updated
215
     */
216
    public function updateInvoiceByInstance($invoiceid): Invoice
217
    {
218
        return Invoice::find($invoiceid);
219
    }
220
221
    /**
222
     *
223
     * Returns updated invoice with new due_date
224
     */
225
    public function updateDueDate($date, $invoiceid): Invoice
226
    {
227
        $invoice = $this->updateInvoiceByInstance($invoiceid);
228
        $invoice->due_date = $date;
229
        $invoice->save();
230
        return $invoice;
231
    }
232
233
234
235
 public function getBillingHistoryByStatus($status)
236
    {
237
238
        return BillingPayment::where('status', $status)->get();
239
    }
240
241
242
    public function getUserBillingHistoryByStatus($userid, $status)
243
    {
244
245
        return BillingPayment::where('userid', $userid)->where('status', $status)->get();
246
    }
247
248
    /** Return OrderItems for particular order
249
     *@param $orderid
250
     **/
251
    public function getOrderItems($orderid)
252
    {
253
254
        return OrderItem::where('order_id', $orderid)->get();
255
    }
256
257
258
    /** Return OrderItems for particular order gets invoiceid
259
     *@param $invoiceid
260
     **/
261
    public function getOrderItemsByInvoiceId($invoiceid)
262
    {
263
264
        return OrderItem::where('order_id', Invoice::where('id', $invoiceid)->first()->orderid)->get();
265
    }
266
}
267