|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Order; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use App\Http\Controllers\Controller; |
|
7
|
|
|
use App\Model\Product\Subscription; |
|
8
|
|
|
use App\Model\Order\Order; |
|
9
|
|
|
use App\Model\Product\Product; |
|
10
|
|
|
use App\Model\Payment\Plan; |
|
11
|
|
|
use App\Model\Order\Invoice; |
|
12
|
|
|
use App\Model\Order\InvoiceItem; |
|
13
|
|
|
|
|
14
|
|
|
class BaseRenewController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
public function invoiceBySubscriptionId($id, $planid, $cost) |
|
17
|
|
|
{ |
|
18
|
|
|
try { |
|
19
|
|
|
$sub = Subscription::find($id); |
|
20
|
|
|
$order_id = $sub->order_id; |
|
21
|
|
|
|
|
22
|
|
|
return $this->getInvoiceByOrderId($order_id, $planid, $cost); |
|
23
|
|
|
} catch (Exception $ex) { |
|
|
|
|
|
|
24
|
|
|
throw new Exception($ex->getMessage()); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getInvoiceByOrderId($orderid, $planid, $cost) |
|
29
|
|
|
{ |
|
30
|
|
|
try { |
|
31
|
|
|
$order = Order::find($orderid); |
|
32
|
|
|
$invoice_item_id = $order->invoice_item_id; |
|
33
|
|
|
$invoice_id = $order->invoice_id; |
|
34
|
|
|
$invoice = Invoice::find($invoice_id); |
|
35
|
|
|
if ($invoice_item_id == 0) { |
|
36
|
|
|
$invoice_item_id = $invoice->invoiceItem()->first()->id; |
|
37
|
|
|
} |
|
38
|
|
|
$item = InvoiceItem::find($invoice_item_id); |
|
39
|
|
|
$product = $this->getProductByName($item->product_name); |
|
40
|
|
|
//dd($product); |
|
41
|
|
|
$user = $this->getUserById($order->client); |
|
42
|
|
|
if (!$user) { |
|
43
|
|
|
throw new Exception('User has removed from database'); |
|
44
|
|
|
} |
|
45
|
|
|
if (!$product) { |
|
46
|
|
|
throw new Exception('Product has removed from database'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->generateInvoice($product, $user, $orderid, $planid, $cost, $code = ''); |
|
50
|
|
|
} catch (Exception $ex) { |
|
51
|
|
|
throw new Exception($ex->getMessage()); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
public function getProductByName($name) |
|
57
|
|
|
{ |
|
58
|
|
|
try { |
|
59
|
|
|
$product = Product::where('name', $name)->first(); |
|
60
|
|
|
if ($product) { |
|
61
|
|
|
return $product; |
|
62
|
|
|
} |
|
63
|
|
|
} catch (Exception $ex) { |
|
64
|
|
|
throw new Exception($ex->getMessage()); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getCost(Request $request) |
|
69
|
|
|
{ |
|
70
|
|
|
try { |
|
71
|
|
|
$planid = $request->input('plan'); |
|
72
|
|
|
$userid = $request->input('user'); |
|
73
|
|
|
|
|
74
|
|
|
return $this->planCost($planid, $userid); |
|
75
|
|
|
} catch (Exception $ex) { |
|
76
|
|
|
throw new \Exception($ex->getMessage()); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function planCost($planid, $userid) |
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
|
|
$currency = $this->getUserCurrencyById($userid); |
|
84
|
|
|
$plan = Plan::find($planid); |
|
85
|
|
|
$price = $plan->planPrice()->where('currency', $currency)->first()->renew_price; |
|
86
|
|
|
|
|
87
|
|
|
return $price; |
|
88
|
|
|
} catch (Exception $ex) { |
|
89
|
|
|
throw new \Exception($ex->getMessage()); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
public function generateInvoice($product, $user, $orderid, $planid, $cost, $code = '') |
|
95
|
|
|
{ |
|
96
|
|
|
try { |
|
97
|
|
|
$controller = new InvoiceController(); |
|
98
|
|
|
$currency = \Auth::user()->currency; |
|
99
|
|
|
if ($code != '') { |
|
100
|
|
|
$product_cost = $controller->checkCode($code, $product->id, $currency); |
|
101
|
|
|
} |
|
102
|
|
|
if ($cost != '') { |
|
103
|
|
|
$product_cost = $this->planCost($planid, $user->id); |
|
104
|
|
|
} |
|
105
|
|
|
$cost = $this->tax($product, $product_cost, $user->id); |
|
106
|
|
|
$currency = $this->getUserCurrencyById($user->id); |
|
107
|
|
|
$number = rand(11111111, 99999999); |
|
108
|
|
|
$date = \Carbon\Carbon::now(); |
|
109
|
|
|
$invoice = Invoice::create([ |
|
110
|
|
|
'user_id' => $user->id, |
|
111
|
|
|
'number' => $number, |
|
112
|
|
|
'date' => $date, |
|
113
|
|
|
'grand_total' => $cost, |
|
114
|
|
|
'currency' => $currency, |
|
115
|
|
|
'status' => 'pending', |
|
116
|
|
|
]); |
|
117
|
|
|
$this->createOrderInvoiceRelation($orderid, $invoice->id); |
|
118
|
|
|
$items = $controller->createInvoiceItemsByAdmin($invoice->id, $product->id, $code, $product_cost, $currency, $qty = 1); |
|
119
|
|
|
|
|
120
|
|
|
return $items; |
|
121
|
|
|
} catch (Exception $ex) { |
|
122
|
|
|
throw new Exception($ex->getMessage()); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|