|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Payment; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use App\Http\Controllers\Controller; |
|
7
|
|
|
use App\Model\Payment\Promotion; |
|
8
|
|
|
use App\Model\Payment\PromotionType; |
|
9
|
|
|
use App\Model\Product\Product; |
|
10
|
|
|
|
|
11
|
|
|
class BasePromotionController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
public function getCode() |
|
15
|
|
|
{ |
|
16
|
|
|
try { |
|
17
|
|
|
$code = str_random(6); |
|
18
|
|
|
echo strtoupper($code); |
|
19
|
|
|
} catch (\Exception $ex) { |
|
20
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
public function findCost($type, $value, $price, $productid) |
|
26
|
|
|
{ |
|
27
|
|
|
try { |
|
28
|
|
|
switch ($type) { |
|
29
|
|
|
case 1: |
|
30
|
|
|
$percentage = $price * ($value / 100); |
|
31
|
|
|
return $price - $percentage; |
|
32
|
|
|
case 2: |
|
33
|
|
|
return $price - $value; |
|
34
|
|
|
case 3: |
|
35
|
|
|
\Cart::update($productid, [ |
|
36
|
|
|
'price' => $value, |
|
37
|
|
|
]); |
|
38
|
|
|
|
|
39
|
|
|
return '-0'; |
|
40
|
|
|
case 4: |
|
41
|
|
|
return '-'.$price; |
|
42
|
|
|
} |
|
43
|
|
|
} catch (\Exception $ex) { |
|
44
|
|
|
throw new \Exception(\Lang::get('message.find-cost-error')); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
public function findCostAfterDiscount($promoid, $productid) |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
|
|
$promotion = Promotion::findOrFail($promoid); |
|
53
|
|
|
$product = Product::findOrFail($productid); |
|
54
|
|
|
$promotion_type = $promotion->type; |
|
55
|
|
|
$promotion_value = $promotion->value; |
|
56
|
|
|
$product_price = 0; |
|
57
|
|
|
$userid = \Auth::user()->id; |
|
58
|
|
|
$control = new \App\Http\Controllers\Order\RenewController(); |
|
59
|
|
|
$cart_control = new \App\Http\Controllers\Front\CartController(); |
|
60
|
|
|
$currency = $cart_control->checkCurrencySession(); |
|
61
|
|
|
if ($cart_control->checkPlanSession() == true) { |
|
|
|
|
|
|
62
|
|
|
$planid = \Session::get('plan'); |
|
63
|
|
|
} |
|
64
|
|
|
if ($product->subscription != 1) { |
|
65
|
|
|
$planId = Plan::where('product', $productid)->pluck('id')->first(); |
|
66
|
|
|
$product_price = PlanPrice::where('plan_id', $planId)->where('currency', $currency)->pluck('add_price')->first(); |
|
67
|
|
|
} else { |
|
68
|
|
|
$product_price = $control->planCost($planid, $userid); |
|
69
|
|
|
} |
|
70
|
|
|
if (count(\Cart::getContent())) { |
|
71
|
|
|
$product_price = \Cart::getSubTotalWithoutConditions(); |
|
72
|
|
|
// dd($product_price); |
|
73
|
|
|
} |
|
74
|
|
|
$updated_price = $this->findCost($promotion_type, $promotion_value, $product_price, $productid); |
|
75
|
|
|
// dd($updated_price); |
|
76
|
|
|
//dd([$product_price,$promotion_type,$updated_price]); |
|
77
|
|
|
return $updated_price; |
|
78
|
|
|
} catch (\Exception $ex) { |
|
79
|
|
|
throw new \Exception(\Lang::get('message.find-discount-error')); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.