File size: 6,187 Bytes
5f32fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php

namespace App\Http\Controllers\Api;

use App\Actions\CreateActivity;
use App\Http\Controllers\Controller;
use App\Models\Currency;
use App\Models\Setting;
use App\Models\User;
use App\Models\UserAffiliate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AffiliateApiController extends Controller
{
    /**
     * Get affiliate info
     *
     * @OA\Get(
     *      path="/api/affiliates/",
     *      operationId="affiliates",
     *      tags={"Affiliates"},
     *      summary="Get affiliate totals of current user",
     *      description="Get affiliate earnings and withdrawal totals of current user.",
     *      security={{ "passport": {} }},
     *
     *      @OA\Response(
     *          response=200,
     *          description="Successful operation",
     *
     *          @OA\JsonContent(
     *              type="object",
     *          ),
     *      ),
     *
     *      @OA\Response(
     *          response=401,
     *          description="Unauthenticated",
     *      ),
     * )
     */
    public function affiliates(Request $request)
    {
        $user = Auth::user();
        $list = $user->affiliates;
        $list2 = $user->withdrawals;
        $totalEarnings = 0;
        foreach ($list as $affOrders) {
            $totalEarnings += $affOrders->orders->sum('affiliate_earnings');
        }
        $totalWithdrawal = 0;
        foreach ($list2 as $affWithdrawal) {
            $totalWithdrawal += $affWithdrawal->amount;
        }

        $earnings = $totalEarnings - $totalWithdrawal;
        if ($earnings < 0) {
            $earnings = 0;
        }

        // Get currency
        $currencyId = Setting::first()->default_currency ?? 124;
        $currency = Currency::find($currencyId);

        $affilate_code = $user->affiliate_code;

        return response()->json([
            'earnings'         => $earnings,
            'total_earnings'   => $totalEarnings,
            'total_withdrawal' => $totalWithdrawal,
            'affiliate_code'   => $affilate_code,
            'currency'         => $currency,
        ], 200);
    }

    /**
     * Get withdrawals info
     *
     * @OA\Get(
     *      path="/api/affiliates/withdrawals",
     *      operationId="withdrawals",
     *      tags={"Affiliates"},
     *      summary="Get withdrawals of current user",
     *      description="Get affiliate withdrawals list of current user.",
     *      security={{ "passport": {} }},
     *
     *      @OA\Response(
     *          response=200,
     *          description="Successful operation",
     *
     *          @OA\JsonContent(
     *              type="object",
     *          ),
     *      ),
     *
     *      @OA\Response(
     *          response=401,
     *          description="Unauthenticated",
     *      ),
     * )
     */
    public function withdrawals(Request $request)
    {
        $user = Auth::user();
        $data = UserAffiliate::where('user_id', $user->id)
            ->orderByRaw("CASE WHEN status = 'Waiting' THEN 0 ELSE 1 END")
            ->orderBy('created_at', 'desc')
            ->get();

        return response()->json($data);
    }

    /**
     * Request withdrawal
     *
     * @OA\Post(
     *      path="/api/affiliates/request-withdrawal",
     *      operationId="requestWithdrawal",
     *      tags={"Affiliates"},
     *      summary="Request withdrawal for current user",
     *      description="Request withdrawal for current user",
     *      security={{ "passport": {} }},
     *
     *      @OA\RequestBody(
     *         required=true,
     *         description="Request withdrawal data",
     *
     *         @OA\MediaType(
     *             mediaType="multipart/form-data",
     *
     *             @OA\Schema(
     *                 type="object",
     *
     *                 @OA\Property(
     *                     property="affiliate_bank_account",
     *                     description="Bank Account info",
     *                     type="string"
     *                 ),
     *                 @OA\Property(
     *                     property="amount",
     *                     description="Amount to withdraw",
     *                     type="float"
     *                 ),
     *             ),
     *         ),
     *     ),
     *
     *      @OA\Response(
     *          response=200,
     *          description="Successful operation",
     *
     *          @OA\JsonContent(
     *              type="object",
     *          ),
     *      ),
     *
     *      @OA\Response(
     *          response=401,
     *          description="Unauthenticated",
     *      ),
     *      @OA\Response(
     *          response=412,
     *          description="Precondition Failed",
     *      ),
     * )
     */
    public function requestWithdrawal(Request $request)
    {

        if ($request->affiliate_bank_account == null) {
            return response()->json(['error' => __('Bank Account missing.')], 412);
        }
        if ($request->amount == null) {
            return response()->json(['error' => __('Amount missing.')], 412);
        }

        $user = Auth::user();
        $list = $user->affiliates;
        $list2 = $user->withdrawals;

        $totalEarnings = 0;
        foreach ($list as $affOrders) {
            $totalEarnings += $affOrders->orders->sum('affiliate_earnings');
        }
        $totalWithdrawal = 0;
        foreach ($list2 as $affWithdrawal) {
            $totalWithdrawal += $affWithdrawal->amount;
        }
        if ($totalEarnings - $totalWithdrawal >= $request->amount) {
            $user->affiliate_bank_account = $request->affiliate_bank_account;
            $user->save();
            $withdrawalReq = new UserAffiliate;
            $withdrawalReq->user_id = Auth::id();
            $withdrawalReq->amount = $request->amount;
            $withdrawalReq->save();

            CreateActivity::for($user, 'Sent', 'Affiliate Withdraw Request');

            return response()->json(['message' => 'Affiliate Withdrawal Requested'], 200);
        } else {
            return response()->json(['error' => __('Not enough earnings')], 412);
        }

    }
}