|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Order; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
6
|
|
|
use App\Http\Controllers\Front\CartController; |
|
7
|
|
|
use App\Model\Order\Invoice; |
|
8
|
|
|
use App\Model\Payment\Tax; |
|
9
|
|
|
use App\Model\Payment\TaxClass; |
|
10
|
|
|
use App\Model\Product\Product; |
|
11
|
|
|
use App\User; |
|
12
|
|
|
use Bugsnag; |
|
13
|
|
|
use Illuminate\Http\Request; |
|
14
|
|
|
|
|
15
|
|
|
class BaseInvoiceController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
public function invoiceGenerateByForm(Request $request, $user_id = '') |
|
18
|
|
|
{ |
|
19
|
|
|
$qty = 1; |
|
20
|
|
|
|
|
21
|
|
|
try { |
|
22
|
|
|
if ($user_id == '') { |
|
23
|
|
|
$user_id = \Request::input('user'); |
|
24
|
|
|
} |
|
25
|
|
|
$productid = $request->input('product'); |
|
26
|
|
|
$code = $request->input('code'); |
|
27
|
|
|
$total = $request->input('price'); |
|
28
|
|
|
$plan = $request->input('plan'); |
|
29
|
|
|
$description = $request->input('description'); |
|
30
|
|
|
if ($request->has('domain')) { |
|
31
|
|
|
$domain = $request->input('domain'); |
|
32
|
|
|
$this->setDomain($productid, $domain); |
|
33
|
|
|
} |
|
34
|
|
|
$controller = new \App\Http\Controllers\Front\CartController(); |
|
35
|
|
|
$currency = $controller->currency($user_id); |
|
36
|
|
|
$number = rand(11111111, 99999999); |
|
37
|
|
|
$date = \Carbon\Carbon::now(); |
|
38
|
|
|
$product = Product::find($productid); |
|
39
|
|
|
$cost = $controller->cost($productid, $user_id, $plan); |
|
40
|
|
|
if ($cost != $total) { |
|
41
|
|
|
$grand_total = $total; |
|
42
|
|
|
} |
|
43
|
|
|
$grand_total = $this->getGrandTotal($code, $total, $cost, $productid, $currency); |
|
44
|
|
|
$grand_total = $qty * $grand_total; |
|
45
|
|
|
|
|
46
|
|
|
$tax = $this->checkTax($product->id, $user_id); |
|
47
|
|
|
$tax_name = ''; |
|
48
|
|
|
$tax_rate = ''; |
|
49
|
|
|
if (!empty($tax)) { |
|
50
|
|
|
$tax_name = $tax[0]; |
|
51
|
|
|
$tax_rate = $tax[1]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$grand_total = $this->calculateTotal($tax_rate, $grand_total); |
|
55
|
|
|
$grand_total = \App\Http\Controllers\Front\CartController::rounding($grand_total); |
|
56
|
|
|
|
|
57
|
|
|
$invoice = Invoice::create(['user_id' => $user_id, 'number' => $number, 'date' => $date, 'grand_total' => $grand_total, 'currency' => $currency, 'status' => 'pending', 'description' => $description]); |
|
58
|
|
|
|
|
59
|
|
|
$items = $this->createInvoiceItemsByAdmin($invoice->id, $productid, $code, $total, $currency, $qty, $plan, $user_id, $tax_name, $tax_rate); |
|
60
|
|
|
$result = $this->getMessage($items, $user_id); |
|
61
|
|
|
} catch (\Exception $ex) { |
|
62
|
|
|
app('log')->useDailyFiles(storage_path().'/laravel.log'); |
|
63
|
|
|
app('log')->info($ex->getMessage()); |
|
64
|
|
|
Bugsnag::notifyException($ex); |
|
65
|
|
|
$result = ['fails' => $ex->getMessage()]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return response()->json(compact('result')); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
*Tax When state is not empty. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getTaxWhenState($user_state, $productid, $origin_state) |
|
75
|
|
|
{ |
|
76
|
|
|
$cartController = new CartController(); |
|
77
|
|
|
$c_gst = $user_state->c_gst; |
|
78
|
|
|
$s_gst = $user_state->s_gst; |
|
79
|
|
|
$i_gst = $user_state->i_gst; |
|
80
|
|
|
$ut_gst = $user_state->ut_gst; |
|
81
|
|
|
$state_code = $user_state->state_code; |
|
82
|
|
|
if ($state_code == $origin_state) {//If user and origin state are same |
|
83
|
|
|
$taxClassId = TaxClass::where('name', 'Intra State GST')->pluck('id')->toArray(); //Get the class Id of state |
|
84
|
|
|
if ($taxClassId) { |
|
85
|
|
|
$taxes = $cartController->getTaxByPriority($taxClassId); |
|
86
|
|
|
$value = $cartController->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes); |
|
87
|
|
|
} else { |
|
88
|
|
|
$taxes = [0]; |
|
89
|
|
|
} |
|
90
|
|
|
} elseif ($state_code != $origin_state && $ut_gst == 'NULL') {//If user is from other state |
|
91
|
|
|
|
|
92
|
|
|
$taxClassId = TaxClass::where('name', 'Inter State GST')->pluck('id')->toArray(); //Get the class Id of state |
|
93
|
|
|
if ($taxClassId) { |
|
94
|
|
|
$taxes = $cartController->getTaxByPriority($taxClassId); |
|
95
|
|
|
$value = $cartController->getValueForOtherState($productid, $i_gst, $taxClassId, $taxes); |
|
96
|
|
|
} else { |
|
97
|
|
|
$taxes = [0]; |
|
98
|
|
|
} |
|
99
|
|
|
} elseif ($state_code != $origin_state && $ut_gst != 'NULL') {//if user from Union Territory |
|
100
|
|
|
$taxClassId = TaxClass::where('name', 'Union Territory GST')->pluck('id')->toArray(); //Get the class Id of state |
|
101
|
|
|
if ($taxClassId) { |
|
102
|
|
|
$taxes = $cartController->getTaxByPriority($taxClassId); |
|
103
|
|
|
$value = $cartController->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes); |
|
104
|
|
|
} else { |
|
105
|
|
|
$taxes = [0]; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return ['taxes'=>$taxes, 'value'=>$value]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
*Tax When from other Country. |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid) |
|
116
|
|
|
{ |
|
117
|
|
|
$cartController = new CartController(); |
|
118
|
|
|
$taxClassId = Tax::where('state', $geoip_state)->orWhere('country', $geoip_country)->pluck('tax_classes_id')->first(); |
|
119
|
|
|
$value = ''; |
|
120
|
|
|
$rate = ''; |
|
121
|
|
|
if ($taxClassId) { //if state equals the user State |
|
122
|
|
|
|
|
123
|
|
|
$taxes = $cartController->getTaxByPriority($taxClassId); |
|
124
|
|
|
|
|
125
|
|
|
// $taxes = $this->cartController::getTaxByPriority($taxClassId); |
|
126
|
|
|
$value = $cartController->getValueForOthers($productid, $taxClassId, $taxes); |
|
127
|
|
|
$rate = $value; |
|
128
|
|
|
} else {//if Tax is selected for Any State Any Country |
|
129
|
|
|
$taxClassId = Tax::where('country', '')->where('state', 'Any State')->pluck('tax_classes_id')->first(); |
|
130
|
|
|
if ($taxClassId) { |
|
131
|
|
|
$taxes = $cartController->getTaxByPriority($taxClassId); |
|
132
|
|
|
$value = $cartController->getValueForOthers($productid, $taxClassId, $taxes); |
|
133
|
|
|
$rate = $value; |
|
134
|
|
|
} else { |
|
135
|
|
|
$taxes = [0]; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return ['taxes'=>$taxes, 'value'=>$value, 'rate'=>$rate]; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
public function whenDateNotSet($start, $end) |
|
145
|
|
|
{ |
|
146
|
|
|
//both not set, always true |
|
147
|
|
|
if (($start == null || $start == '0000-00-00 00:00:00') && |
|
148
|
|
|
($end == null || $end == '0000-00-00 00:00:00')) { |
|
149
|
|
|
return 'success'; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function whenStartDateSet($start, $end, $now) |
|
154
|
|
|
{ |
|
155
|
|
|
//only starting date set, check the date is less or equel to today |
|
156
|
|
|
if (($start != null || $start != '0000-00-00 00:00:00') |
|
157
|
|
|
&& ($end == null || $end == '0000-00-00 00:00:00')) { |
|
158
|
|
|
if ($start <= $now) { |
|
159
|
|
|
return 'success'; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function whenEndDateSet($start, $end, $now) |
|
165
|
|
|
{ |
|
166
|
|
|
//only ending date set, check the date is greater or equel to today |
|
167
|
|
|
if (($end != null || $end != '0000-00-00 00:00:00') && ($start == null || $start == '0000-00-00 00:00:00')) { |
|
168
|
|
|
if ($end >= $now) { |
|
169
|
|
|
return 'success'; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function whenBothSet($start, $end, $now) |
|
175
|
|
|
{ |
|
176
|
|
|
//both set |
|
177
|
|
|
if (($end != null || $start != '0000-00-00 00:00:00') && ($start != null || $start != '0000-00-00 00:00:00')) { |
|
178
|
|
|
if ($end >= $now && $start <= $now) { |
|
179
|
|
|
return 'success'; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function postPayment($invoiceid, Request $request) |
|
185
|
|
|
{ |
|
186
|
|
|
$this->validate($request, [ |
|
187
|
|
|
'payment_method' => 'required', |
|
188
|
|
|
'amount' => 'required|numeric', |
|
189
|
|
|
'payment_date' => 'required|date_format:Y-m-d', |
|
190
|
|
|
]); |
|
191
|
|
|
|
|
192
|
|
|
try { |
|
193
|
|
|
$payment_method = $request->input('payment_method'); |
|
194
|
|
|
$payment_status = 'success'; |
|
195
|
|
|
$payment_date = $request->input('payment_date'); |
|
196
|
|
|
$amount = $request->input('amount'); |
|
197
|
|
|
$payment = $this->updateInvoicePayment($invoiceid, $payment_method, $payment_status, $payment_date, $amount); |
|
198
|
|
|
|
|
199
|
|
|
return redirect()->back()->with('success', 'Payment Accepted Successfully'); |
|
200
|
|
|
} catch (\Exception $ex) { |
|
201
|
|
|
Bugsnag::notifyException($ex); |
|
202
|
|
|
|
|
203
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
public function setDomain($productid, $domain) |
|
210
|
|
|
{ |
|
211
|
|
|
try { |
|
212
|
|
|
if (\Session::has('domain'.$productid)) { |
|
213
|
|
|
\Session::forget('domain'.$productid); |
|
214
|
|
|
} |
|
215
|
|
|
\Session::put('domain'.$productid, $domain); |
|
216
|
|
|
} catch (\Exception $ex) { |
|
217
|
|
|
Bugsnag::notifyException($ex); |
|
218
|
|
|
|
|
219
|
|
|
throw new \Exception($ex->getMessage()); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function domain($id) |
|
224
|
|
|
{ |
|
225
|
|
|
try { |
|
226
|
|
|
if (\Session::has('domain'.$id)) { |
|
227
|
|
|
$domain = \Session::get('domain'.$id); |
|
228
|
|
|
} else { |
|
229
|
|
|
$domain = ''; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
return $domain; |
|
233
|
|
|
} catch (\Exception $ex) { |
|
234
|
|
|
Bugsnag::notifyException($ex); |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|