File size: 5,736 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 | <?php
namespace App\Livewire\Admin\Finance\Plan;
use App\Http\Controllers\Finance\PaymentProcessController;
use App\Models\OpenAIGenerator;
use App\Models\PaymentPlans;
use Exception;
use Illuminate\Contracts\View\Factory;
use Illuminate\Foundation\Application;
use Livewire\Component;
class TokenPackPlanCreate extends Component
{
public string $currentStep = 'first';
public string $name = '';
public string $description = '';
public string $features = '';
public string $is_featured = '0';
public string $plan_type = 'All';
public float $price = 0.0;
public string $currency = 'USD';
public $is_team_plan = 0;
public $plan_allow_seat = 0;
public string $default_ai_model = 'gpt-3.5-turbo';
public $is_trial = 0;
public $trial_days = 7;
public $total_words = 0;
public $total_images = 0;
public $display_imag_count = 1;
public $display_word_count = 1;
public $can_create_ai_images = 0;
public $active = 1;
public string $type = 'prepaid';
public static $validation = [
'name' => 'required|string',
'description' => 'required|string',
'price' => 'required|numeric|min:0',
'is_team_plan' => 'required',
'plan_allow_seat' => 'required',
'is_trial' => 'required',
'trial_days' => 'required',
'total_words' => 'required',
'total_images' => 'required',
'display_imag_count' => 'sometimes|nullable',
'display_word_count' => 'sometimes|nullable',
];
public function __construct(public $plan = null) {}
public function mount($plan = null)
{
if ($plan->id) {
$this->active = $plan->active;
$this->name = $plan->name;
$this->price = $plan->price;
$this->is_free = $this->price > 0 ? 0 : 1;
$this->currency = $plan->currency;
$this->frequency = $plan->frequency;
$this->is_featured = $plan->is_featured;
$this->total_words = $plan->total_words;
$this->total_images = $plan->total_images;
$this->ai_name = $plan->ai_name;
$this->can_create_ai_images = $plan->can_create_ai_images;
$this->plan_type = $plan->plan_type;
$this->features = $plan->features;
$this->type = $plan->type;
$this->is_team_plan = $plan->is_team_plan;
$this->plan_allow_seat = $plan->plan_allow_seat;
$this->trial_days = $plan->trial_days;
$this->display_imag_count = $plan->display_imag_count;
$this->display_word_count = $plan->display_word_count;
$this->description = (string) $plan->description ?: '';
$this->default_ai_model = $plan->default_ai_model;
}
}
public function render(): Factory|Application|\Illuminate\Contracts\View\View|\Illuminate\View\View|\Illuminate\Contracts\Foundation\Application
{
return view('livewire.admin.finance.plan.token-pack-plan-create');
}
public function submit()
{
$this->validate(self::$validation);
$data = $this->data();
try {
$requireUpdate = false;
if ($this->plan->id) {
if ($this->plan->price != (float) $data['price']) {
$requireUpdate = true;
}
$this->plan->update($data);
} else {
$requireUpdate = true;
$this->plan = PaymentPlans::query()->create($data);
}
if ($requireUpdate) {
PaymentProcessController::saveGatewayProducts($this->plan);
}
return redirect(route('dashboard.admin.finance.plan.index'))->with([
'message' => 'Plan successfully updated.',
'type' => 'success',
]);
} catch (Exception $e) {
return;
}
}
private function data(): array
{
$openAiItems = [];
$openAIGenerators = OpenAIGenerator::query()
->where('active', true)
->get()
->groupBy('filters')
->filter(function ($item, $key) {
return $key;
});
foreach ($openAIGenerators as $items) {
foreach ($items as $generator) {
$openAiItems[] = $generator->slug;
}
}
return [
'active' => $this->active,
'name' => $this->name,
'price' => $this->price,
'currency' => $this->currency,
'is_featured' => $this->is_featured,
'is_free' => $this->price > 0 ? 0 : 1,
'stripe_product_id' => null,
'total_words' => $this->total_words,
'total_images' => $this->total_images,
'ai_name' => 'AI',
'can_create_ai_images' => true,
'plan_type' => $this->plan_type,
'features' => $this->features,
'type' => $this->type,
'is_team_plan' => $this->is_team_plan,
'plan_allow_seat' => $this->plan_allow_seat,
'trial_days' => 0,
'display_imag_count' => $this->display_imag_count,
'display_word_count' => $this->display_word_count,
'description' => $this->description,
'open_ai_items' => $openAiItems,
'default_ai_model' => $this->default_ai_model,
];
}
}
|