I
I
ilysion_in_life2018-11-12 10:43:16
Yii
ilysion_in_life, 2018-11-12 10:43:16

How to formulate a discount system correctly?

Good afternoon, can you advise? i have an array so i output from the model

$model = Yii::app()->db->createCommand()-> 
select( '*' )-> 
from('DiscountRegular')-> 
where('active = "1"')-> 
queryAll();

data such
array(2) { 
[0]=> 
array(7) { 
["id"]=> 
string(1) "1" 
["name"]=> 
string(6) "7roses" 
["active"]=> 
string(1) "1" 
["sum"]=> 
string(2) "10" 
["start_date"]=> 
string(19) "2018-10-25 13:40:07" 
["end_date"]=> 
string(19) "2018-11-30 09:45:28" 
["price"]=> 
string(3) "351" 
} 
[1]=> 
array(7) { 
["id"]=> 
string(1) "2" 
["name"]=> 
string(1) "2" 
["active"]=> 
string(1) "1" 
["sum"]=> 
string(2) "20" 
["start_date"]=> 
string(19) "2018-10-30 10:25:40" 
["end_date"]=> 
string(19) "2018-11-30 10:25:40" 
["price"]=> 
string(3) "250" 
} 
}

how can I go through such an example
if the total amount paid to the buyer is 250 or more then he will have a % discount of 20% and if more than 351 or equal then 10% condition I did this
if($result == $model[0]['price'] or $result > $model[0]['price']){

i.e. I need to do something so that the discount by the ["price"] value would work, but for some reason now it doesn’t work correctly for me, although the indicated amount of purchases is 351, the discount for 250 doesn’t work,
I did it all like this
public function checkDiscount($price) 
{ 
$model = Yii::app()->db->createCommand()-> 
select( '*' )-> 
from('DiscountRegular')-> 
where('active = "1"')-> 
queryAll(); 

$regular_discount = DiscountRegular::calculateDiscount(); 
$result = preg_replace("/\..+/", "", $regular_discount); 

if($result == $model[0]['price'] or $result > $model[0]['price']){ 
$timestamp_start = strtotime($model[0]['start_date']); 
$timestamp_stop = strtotime($model[0]['end_date']); 


if($timestamp_start <= time() && $timestamp_stop >= time() && $model[0]['active'] == '1') 
{ 
$percent = $model[0]['sum']; 
$number_percent = $price / 100 * $percent; 
$result = $price - $number_percent; 

return $result; 

}else{ 
return false; 
} 
}else{ 
return false; 
} 
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xtress, 2018-11-12
@xtress

First, you can tell what's going on here (and what happens at this stage):

$regular_discount = DiscountRegular::calculateDiscount(); 
$result = preg_replace("/\..+/", "", $regular_discount);

Secondly, if we take your condition:
the way in which
although the indicated amount of purchases is 351, the discount for 250 does not work
should this apply? Your condition says unequivocally - discount = 20%, at 250<=amount<351; discount = 10%, if the amount>=351. Ie, kmk, you formulated the problem incorrectly.
ps
Well, and, thirdly: and the data at you all in lines is stored in a DB? (what type of fields?) If not, then why not immediately execute a general query like
Naturally transferring it to your ORM used in the project. And then:
$applicableDiscounts = $queryResults; //результаты запроса выше
$currentPercentage = 0;
$prevMinOrderSum = 0; //нужно для запоминания текущих границ скидки, чтобы не применялась тупо последняя выбранная из бд скидка
foreach ($applicableDiscounts as $discountData) {
    if ($price >= $discountData['minOrderSum'] && $discountData['minOrderSum'] > $prevMinOrderSum) { //где $price - сумма вашего заказа
        $currentPercentage = $discountData['percentage'];
        $prevMinOrderSum = $discountData['minOrderSum'];
    }
}
$finalPrice = $price * (1 - $currentPercentage / 100);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question