Skip to content

Commit

Permalink
Fix error plugin discount
Browse files Browse the repository at this point in the history
  • Loading branch information
lanhktc committed Jul 26, 2021
1 parent 2ea927d commit 127e536
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 45 deletions.
20 changes: 9 additions & 11 deletions app/Plugins/Total/Discount/AppConfig.php
Expand Up @@ -138,11 +138,9 @@ public function getData()
$totalMethod = session('totalMethod', []);
$discount = $totalMethod['Discount']??'';

$check = json_decode((new FrontController)->check($discount, $uID), true);
$check = (new FrontController)->check($discount, $uID);

if (!empty($discount) && !$check['error']) {
$storeID = $check['content']['store_id'];
//Get cart item with group store id
$subtotalWithTax = ShopCurrency::sumCartCheckout()['subTotalWithTax'] ?? null;
if (!$subtotalWithTax) {
return $arrData;
Expand All @@ -152,8 +150,13 @@ public function getData()
} else {
$value = sc_currency_value($check['content']['reward']);
}
//Add info for earch store
$dataStore[$storeID]['value'] = $value;
if (sc_config_global('MultiVendorPro') || sc_config_global('MultiStorePro')) {
//Add info for earch store
$storeID = $check['content']['store_id'];
$dataStore[$storeID]['value'] = $value;
} else {
$dataStore = [];
}

$arrData = array(
'title' => '<b>' . $this->title . ':</b> ' . $discount . '',
Expand All @@ -179,11 +182,6 @@ public function getData()
*
*/
public function endApp($data = []) {
$customer = session('customer');
$orderID = $data['orderID'] ?? '';
$code = $data['code'] ?? '';
$uID = $customer->id ?? 0;
$msg = 'Order #'.$orderID;
return (new FrontController)->apply($code, $uID, $msg);
return;
}
}
42 changes: 13 additions & 29 deletions app/Plugins/Total/Discount/Controllers/FrontController.php
Expand Up @@ -96,19 +96,19 @@ public function check($code, $uID = null)
$uID = (int) $uID;
$promocode = (new Discount)->getPromotionByCode($code);
if ($promocode === null) {
return json_encode(['error' => 1, 'msg' => "error_code_not_exist"]);
return ['error' => 1, 'msg' => sc_language_render($this->plugin->pathPlugin.'::lang.process.invalid')];
}
//Check user login
if ($promocode->login && !$uID) {
return json_encode(['error' => 1, 'msg' => "error_login"]);
return ['error' => 1, 'msg' => sc_language_render($this->plugin->pathPlugin.'::lang.process.must_login')];
}

if ($promocode->limit == 0 || $promocode->limit <= $promocode->used) {
return json_encode(['error' => 1, 'msg' => "error_code_cant_use"]);
return ['error' => 1, 'msg' => sc_language_render($this->plugin->pathPlugin.'::lang.process.over')];
}

if ($promocode->status == 0 || $promocode->isExpired()) {
return json_encode(['error' => 1, 'msg' => "error_code_expired_disabled"]);
return ['error' => 1, 'msg' => sc_language_render($this->plugin->pathPlugin.'::lang.process.expire')];
}
if ($promocode->login) {
//check if this user has already used this code already
Expand All @@ -117,11 +117,10 @@ public function check($code, $uID = null)
$arrUsers[] = $value->pivot->customer_id;
}
if (in_array($uID, $arrUsers)) {
return json_encode(['error' => 1, 'msg' => "error_user_used"]);
return ['error' => 1, 'msg' => sc_language_render($this->plugin->pathPlugin.'::lang.process.used')];
}
}

return json_encode(['error' => 0, 'content' => $promocode]);
return ['error' => 0, 'content' => $promocode];
}

/**
Expand All @@ -135,11 +134,10 @@ public function apply($code, $uID = null, $msg = null)
{
//check code valid
$checkCode = $this->check($code, $uID);
$check = json_decode($checkCode, true);

if ($check['error'] === 0) {
if ($checkCode['error'] === 0) {
$promocode = (new Discount)->getPromotionByCode($code);
if($promocode) {
if ($promocode) {
try {
// increment used
$promocode->used += 1;
Expand All @@ -149,12 +147,12 @@ public function apply($code, $uID = null, $msg = null)
'used_at' => Carbon::now(),
'log' => $msg,
]);
return json_encode(['error' => 0, 'content' => $promocode->load('users')]);
return ['error' => 0, 'content' => $promocode->load('users')];
} catch (\Throwable $e) {
return json_encode(['error' => 1, 'msg' => $e->getMessage()]);
return ['error' => 1, 'msg' => $e->getMessage()];
}
} else {
return json_encode(['error' => 1, 'msg' => 'error_code_not_exist']);
return ['error' => 1, 'msg' => sc_language_render($this->plugin->pathPlugin.'::lang.process.undefined')];
}

} else {
Expand Down Expand Up @@ -272,24 +270,10 @@ public function useDiscount()
$html = '';
$code = request('code');
$uID = request('uID');
$check = json_decode($this->check($code, $uID), true);
$check = $this->check($code, $uID);
if ($check['error'] == 1) {
$error = 1;
if ($check['msg'] == 'error_code_not_exist') {
$msg = sc_language_render($this->plugin->pathPlugin.'::lang.process.invalid');
} elseif ($check['msg'] == 'error_code_cant_use') {
$msg = sc_language_render($this->plugin->pathPlugin.'::lang.process.over');
} elseif ($check['msg'] == 'error_code_expired_disabled') {
$msg = sc_language_render($this->plugin->pathPlugin.'::lang.process.expire');
} elseif ($check['msg'] == 'error_user_used') {
$msg = sc_language_render($this->plugin->pathPlugin.'::lang.process.used');
} elseif ($check['msg'] == 'error_uID_input') {
$msg = sc_language_render($this->plugin->pathPlugin.'::lang.process.customer_id_invalid');
} elseif ($check['msg'] == 'error_login') {
$msg = sc_language_render($this->plugin->pathPlugin.'::lang.process.must_login');
} else {
$msg = sc_language_render($this->plugin->pathPlugin.'::lang.process.undefined');
}
$msg = $check['msg'];
} else {
$content = $check['content'];
if ($content['type'] === 1) {
Expand Down
5 changes: 2 additions & 3 deletions app/Plugins/Total/Discount/Models/PluginModel.php
Expand Up @@ -63,11 +63,10 @@ public function install()
});

Schema::create($this->table_related, function (Blueprint $table) {
$table->integer('customer_id');
$table->integer('discount_id');
$table->integer('customer_id')->index();
$table->integer('discount_id')->index();
$table->text('log')->nullable();
$table->timestamp('used_at')->nullable();
$table->primary(['customer_id', 'discount_id']);
});


Expand Down
4 changes: 2 additions & 2 deletions app/Plugins/Total/Discount/Views/Admin.blade.php
Expand Up @@ -167,10 +167,10 @@

<div class="form-group row {{ $errors->has('shop_store') ? ' text-red' : '' }}">
<label for="shop_store"
class="col-sm-2 col-form-label">{{ sc_language_render('multi_store.select_store') }}</label>
class="col-sm-2 col-form-label">{{ sc_language_render('admin.select_store') }}</label>
<div class="col-sm-8">
<select class="form-control shop_store select2" multiple="multiple"
data-placeholder="{{ sc_language_render('multi_store.select_store') }}" style="width: 100%;"
data-placeholder="{{ sc_language_render('admin.select_store') }}" style="width: 100%;"
name="shop_store[]">
<option value=""></option>
@foreach (sc_get_list_code_store() as $k => $v)
Expand Down

0 comments on commit 127e536

Please sign in to comment.