Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/fluentformpro/src/Payments/Classes/CouponModel.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
3
+
namespace FluentFormPro\Payments\Classes;
4
+
5
+
use FluentForm\Framework\Helpers\ArrayHelper;
6
+
7
+
if (!defined('ABSPATH')) {
8
+
exit; // Exit if accessed directly.
9
+
}
10
+
11
+
class CouponModel
12
+
{
13
+
private $table = 'fluentform_coupons';
14
+
15
+
public function getCoupons($paginate = false)
16
+
{
17
+
$query = wpFluent()->table($this->table);
18
+
if ($paginate) {
19
+
if (!$perPage = intval($_REQUEST['per_page'])) {
20
+
$perPage = 10;
21
+
}
22
+
$coupons = $query->paginate($perPage)->toArray();
23
+
$coupons['data'] = $this->processGetCoupons($coupons['data']);
24
+
return $coupons;
25
+
}
26
+
$coupons = $query->get();
27
+
return $this->processGetCoupons($coupons);
28
+
}
29
+
30
+
public function getCouponByCode($code)
31
+
{
32
+
$coupon = wpFluent()->table($this->table)
33
+
->where('code', $code)
34
+
->first();
35
+
if (!$coupon) {
36
+
return $coupon;
37
+
}
38
+
39
+
$coupon->settings = $this->processSettings($coupon->settings);
40
+
41
+
if ($coupon->start_date == '0000-00-00') {
42
+
$coupon->start_date = '';
43
+
}
44
+
45
+
if ($coupon->expire_date == '0000-00-00') {
46
+
$coupon->expire_date = '';
47
+
}
48
+
return $coupon;
49
+
}
50
+
51
+
public function getCouponsByCodes($codes)
52
+
{
53
+
$coupons = wpFluent()->table($this->table)
54
+
->whereIn('code', $codes)
55
+
->get();
56
+
return $this->processGetCoupons($coupons);
57
+
}
58
+
59
+
public function insert($data)
60
+
{
61
+
$data['created_at'] = current_time('mysql');
62
+
$data['updated_at'] = current_time('mysql');
63
+
$data['created_by'] = get_current_user_id();
64
+
65
+
if (isset($data['settings'])) {
66
+
$data['settings'] = maybe_serialize($data['settings']);
67
+
}
68
+
69
+
return wpFluent()->table($this->table)
70
+
->insertGetId($data);
71
+
}
72
+
73
+
public function update($id, $data)
74
+
{
75
+
$data['updated_at'] = current_time('mysql');
76
+
77
+
if (isset($data['settings'])) {
78
+
$data['settings'] = maybe_serialize($data['settings']);
79
+
}
80
+
81
+
return wpFluent()->table($this->table)
82
+
->where('id', $id)
83
+
->update($data);
84
+
}
85
+
86
+
public function delete($id)
87
+
{
88
+
return wpFluent()->table($this->table)
89
+
->where('id', $id)
90
+
->delete();
91
+
}
92
+
93
+
public function getValidCoupons($coupons, $formId, $amountTotal)
94
+
{
95
+
$validCoupons = [];
96
+
97
+
$otherCouponCodes = [];
98
+
foreach ($coupons as $coupon) {
99
+
if ($coupon->status != 'active') {
100
+
continue;
101
+
}
102
+
103
+
if ($this->isDateExpire($coupon)) {
104
+
continue;
105
+
}
106
+
107
+
if ($formIds = ArrayHelper::get($coupon->settings, 'allowed_form_ids')) {
108
+
if (!in_array($formId, $formIds)) {
109
+
continue;
110
+
}
111
+
}
112
+
113
+
if ($coupon->min_amount && $coupon->min_amount > $amountTotal) {
114
+
continue;
115
+
}
116
+
117
+
if ($otherCouponCodes && $coupon->stackable != 'yes') {
118
+
continue;
119
+
}
120
+
121
+
$discountAmount = $coupon->amount;
122
+
if ($coupon->coupon_type == 'percent') {
123
+
$discountAmount = ($coupon->amount / 100) * $amountTotal;
124
+
}
125
+
126
+
$amountTotal = $amountTotal - $discountAmount;
127
+
$otherCouponCodes[] = $coupon->code;
128
+
129
+
$validCoupons[] = $coupon;
130
+
}
131
+
132
+
return $validCoupons;
133
+
}
134
+
135
+
public function migrate()
136
+
{
137
+
global $wpdb;
138
+
139
+
$charsetCollate = $wpdb->get_charset_collate();
140
+
141
+
$table = $wpdb->prefix . $this->table;
142
+
143
+
if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) {
144
+
$sql = "CREATE TABLE $table (
145
+
id int(11) NOT NULL AUTO_INCREMENT,
146
+
title varchar(192),
147
+
code varchar(192),
148
+
coupon_type varchar(255) DEFAULT 'percent',
149
+
amount decimal(10,2) NULL,
150
+
status varchar(192) DEFAULT 'active',
151
+
stackable varchar(192) DEFAULT 'no',
152
+
settings longtext,
153
+
created_by INT(11) NULL,
154
+
min_amount INT(11) NULL,
155
+
max_use INT(11) NULL,
156
+
start_date date NULL,
157
+
expire_date date NULL,
158
+
created_at timestamp NULL,
159
+
updated_at timestamp NULL,
160
+
PRIMARY KEY (id)
161
+
) $charsetCollate;";
162
+
163
+
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
164
+
165
+
dbDelta($sql);
166
+
}
167
+
}
168
+
169
+
public function isCouponCodeAvailable($code, $exceptId = false)
170
+
{
171
+
$query = wpFluent()->table($this->table)
172
+
->where('code', $code);
173
+
if ($exceptId) {
174
+
$query = $query->where('id', '!=', $exceptId);
175
+
}
176
+
return $query->first();
177
+
}
178
+
179
+
protected function processGetCoupons($coupons)
180
+
{
181
+
foreach ($coupons as $coupon) {
182
+
if (!empty($coupon->settings)) {
183
+
$coupon->settings = $this->processSettings($coupon->settings);
184
+
} else {
185
+
$coupon->settings = [
186
+
'allowed_form_ids' => [],
187
+
'coupon_limit' => 0,
188
+
];
189
+
}
190
+
191
+
if ($coupon->start_date == '0000-00-00') {
192
+
$coupon->start_date = '';
193
+
}
194
+
if ($coupon->expire_date == '0000-00-00') {
195
+
$coupon->expire_date = '';
196
+
}
197
+
}
198
+
return $coupons;
199
+
}
200
+
201
+
protected function processSettings($settings)
202
+
{
203
+
$settings = maybe_unserialize($settings);
204
+
205
+
$settings['coupon_limit'] = ArrayHelper::get($settings, 'coupon_limit', 0);
206
+
207
+
return $settings;
208
+
}
209
+
210
+
public function hasLimit($couponCode, $couponLimit, $userId)
211
+
{
212
+
$couponApplied = $this->couponAppliedCount($couponCode, $userId);
213
+
214
+
return (int) $couponLimit - $couponApplied > 0;
215
+
}
216
+
217
+
public function isDateExpire($coupon)
218
+
{
219
+
$start_date = '';
220
+
$expire_date = '';
221
+
222
+
if ($coupon->start_date && ("0000-00-00" != $coupon->start_date)) {
223
+
$start_date = strtotime($coupon->start_date);
224
+
}
225
+
if ($coupon->expire_date && ("0000-00-00" != $coupon->expire_date)) {
226
+
$expire_date = strtotime($coupon->expire_date);
227
+
}
228
+
229
+
$today = strtotime('today midnight');
230
+
if ($start_date && $expire_date) {
231
+
return !($start_date <= $today && $today <= $expire_date); // start-date<=today<=expire-date
232
+
}
233
+
if ($start_date) {
234
+
return !($start_date <= $today);
235
+
}
236
+
if ($expire_date) {
237
+
return !($today <= $expire_date);
238
+
}
239
+
return false;
240
+
}
241
+
242
+
protected function couponAppliedCount($couponCode, $userId)
243
+
{
244
+
return wpFluent()
245
+
->table('fluentform_entry_details')
246
+
->where('field_name', 'payment-coupon')
247
+
->where('field_value', $couponCode)
248
+
->join('fluentform_submissions', function ($table) use ($userId) {
249
+
$table->on('fluentform_submissions.id', '=', 'fluentform_entry_details.submission_id');
250
+
$table->on('fluentform_submissions.user_id', '=', wpFluent()->raw($userId));
251
+
})
252
+
->count();
253
+
}
254
+
}
255
+