/*
* Coupon true does not mean it is valid
* COUPON TRUE = access, published, available quantity, quantity user (basicCheck)
* COUPON VALID = COUPON TRUE + total amount, total quantity, product id, category id checked (advancedCheck)
*/
public function checkCoupon($basicCheck = 0, $id = 0, $catid = 0, $quantity = 0, $amount = 0, $subtotalAmount = 0)
{
if (!empty($this->coupon)) {
$paramsC = PhocacartUtils::getComponentParameters();
// $discount_priority = $paramsC->get( 'discount_priority', 1 );
$discount_subtotal_amount = $paramsC->get('discount_subtotal_amount', 1);
// -----------
// BASIC CHECK
// 1. ACCESS, EXISTS, PUBLISHED, CUSTOMER GROUP, TYPE
// Checked in SQL
// 2. VALID DATE FROM TO CHECK
if (isset($this->coupon['valid_from']) && isset($this->coupon['valid_to'])) {
$valid = PhocacartDate::getActiveDate($this->coupon['valid_from'], $this->coupon['valid_to']);
if ($valid != 1) {
PhocacartLog::add(4, 'Message - Coupon not valid (Valid From - Valid To)', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
} else {
PhocacartLog::add(4, 'Message - Coupon not valid (Valid From - Valid To) - Empty data', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
// 3. AVAILABLE QUANTITY
if (isset($this->coupon['available_quantity'])) {
if ((int) $this->coupon['available_quantity'] == 0) {
// OK we don't check the quantity as zero means, no quantity limit
} else {
if ((int) $this->coupon['available_quantity'] > 0 && (int) $this->coupon['count'] == (int) $this->coupon['available_quantity'] || (int) $this->coupon['count'] > (int) $this->coupon['available_quantity']) {
PhocacartLog::add(4, 'Message - Coupon not valid (Available Quantity)', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
}
// OK
} else {
PhocacartLog::add(4, 'Message - Coupon not valid (Available Quantity) - Empty data', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
// 4. QUANTITY USER
if (isset($this->coupon['available_quantity_user'])) {
if ((int) $this->coupon['available_quantity_user'] == 0) {
// OK we don't check the quantity as zero means, no quantity limit
} else {
if ((int) $this->coupon['available_quantity_user'] > 0 && (int) $this->coupon['countuser'] == (int) $this->coupon['available_quantity_user'] || (int) $this->coupon['countuser'] > (int) $this->coupon['available_quantity_user']) {
PhocacartLog::add(4, 'Message - Coupon not valid (Available Quantity - User)', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
}
// OK
} else {
PhocacartLog::add(4, 'Message - Coupon not valid (Available Quantity - User) - Empty data', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
if ($basicCheck) {
return true;
}
// 5. VALID TOTAL AMOUNT
if (isset($this->coupon['total_amount'])) {
$currentAmount = $amount;
if ($discount_subtotal_amount == 2) {
$currentAmount = $subtotalAmount;
}
if ($this->coupon['total_amount'] == 0) {
// OK we don't check the total amount as zero means, no total amount limit
} else {
if ($this->coupon['total_amount'] > 0 && $currentAmount < $this->coupon['total_amount']) {
PhocacartLog::add(4, 'Message - Coupon not valid (Total Amount)', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
}
} else {
PhocacartLog::add(4, 'Message - Coupon not valid (Total Amount) - Empty data', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
// 6. VALID QUANTITY
if (isset($this->coupon['quantity_from'])) {
if ((int) $this->coupon['quantity_from'] == 0) {
// OK we don't check the quantity as zero means, no quantity limit
} else {
if ((int) $this->coupon['quantity_from'] > 0 && (int) $quantity < (int) $this->coupon['quantity_from']) {
PhocacartLog::add(4, 'Message - Coupon not valid (Quantity From)', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
}
} else {
PhocacartLog::add(4, 'Message - Coupon not valid (Quantity From) - Empty data', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
// 7. VALID PRODUCT
if (!empty($this->coupon['product'])) {
$ids = explode(',', $this->coupon['product']);
if (empty($ids)) {
// OK we don't check the quantity as zero means, no quantity limit
} else {
if ($this->coupon['product_filter'] == 0) {
// All except the selected
if (in_array($id, $ids)) {
PhocacartLog::add(4, 'Message - Coupon not valid (Valid Product)', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
} else {
// All selected
if (!in_array($id, $ids)) {
PhocacartLog::add(4, 'Message - Coupon not valid (Valid Product) - Empty data', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
}
}
}
// 8. VALID CATEGORY
if (!empty($this->coupon['category'])) {
$catids = explode(',', $this->coupon['category']);
if (empty($catids)) {
// OK we don't check the quantity as zero means, no quantity limit
} else {
if ($this->coupon['category_filter'] == 0) {
// All except the selected
if (in_array($catid, $catids)) {
PhocacartLog::add(4, 'Message - Coupon not valid (Valid Category)', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
} else {
// All selected
if (!in_array($catid, $catids)) {
PhocacartLog::add(4, 'Message - Coupon not valid (Valid Category) - Empty data', $this->coupon['id'], 'Coupon title: ' . $this->coupon['title']);
return false;
}
}
}
}
// Seems like everything is Ok
$this->coupon['valid'] = 1;
return true;
}
return false;
}