Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //статически заданные параметры:
- $products=array('discount'=>0,'A'=>3,'B'=>2,'C'=>1,'D'=>1); //'discount'=>0 - текущая скидка (накапливаемый параметр, изначально равный нулю),
- // 'A'=>3 и т.п. - статически заданные группы товара в заказе с указанным количеством
- $discounts=array('d1','d2','d3','d4','d5','d6','d7'); //список действующих скидок
- $price_products=array('A'=>1,'B'=>10,'C'=>5,'D'=>7,'E'=>5,'F'=>4,'G'=>4,'H'=>3,'I'=>5,'J'=>10,'K'=>6,'L'=>10,'M'=>4);//прайс-лист
- //все вышеперечисленные данные целесообразно получать из БД ($discounts, $price_products) или POSTом/GETом($products) и для этого использовать типичные классы connectDB или getData
- class DiscountManager { //класс управляющий пакетом действующих скидок
- private $discounts=array();
- private $products=array();
- private $price_products=array();
- public $obj_discount=array();
- public $res_discount=array();
- public $total_discounts;
- function __construct($discounts,$products,$price_products){
- $this->discounts=$discounts;
- $this->products=$products;
- $this->price_products=$price_products;
- }
- function create_obj_discount(){ //функция, управляющая созданием и накопдением скидок
- foreach($this->discounts as $value){
- $this->obj_discount=new $value($this->products,$this->price_products); //вызываем классы скидок
- $this->products=$this->obj_discount->calculate_discount(); //получаем результаты выполнения функций классов скидок
- }
- $this->total_discount=$this->products['discount'];
- }
- }
- class order_Manager{ //класс, управляющий суммой заказа
- protected $price_products=array();
- protected $total_discount;
- protected $products;
- protected $sum;
- protected $sum_without_discount;
- function __construct($price_products,$total_discount,$products){
- $this->price_products=$price_products;
- $this->total_discount=$total_discount;
- $this->products=$products;
- }
- function sum_order(){ //находит общую сумму заказа
- $this->sum=0;
- foreach($this->products as $key=>$value){
- foreach($this->price_products as $key1=>$value1){
- if($key==$key1){
- $this->sum+=$value*$value1;
- }
- }
- }
- $this->sum_without_discount=$this->sum-$this->total_discount;
- $this->show_order_price();
- }
- function show_order_price(){ //выводим представление
- $view='<h2>Заказ</h2> Товары: <ul>';
- foreach($this->products as $key => $value){
- if($key!='discount'){
- $view.='<li>'.$key.'</li>';
- }
- }
- echo $view.='</ul>Общая сумма: '.$this->sum.'<br />Сумма скидки: '.$this->total_discount.'<br />Окончательная цена; '.$this->sum_without_discount;
- }
- }
- // далее - классы скидок
- class d1 {
- private $products;
- private $price_products;
- function __construct($products,$price_products){
- $this->products=$products;
- $this->price_products=$price_products;
- }
- function calculate_discount(){
- if(array_key_exists('A',$this->products)&& array_key_exists('B',$this->products)
- && $this->products['A']!=0 && $this->products['B']!=0){
- $pair=min(array($this->products['A'],$this->products['B']));
- $this->products['discount']=$this->products['discount']+((($pair*($this->price_products['A']+$this->price_products['B']))/100)*10);
- $this->products['A']=$this->products['A']-$pair;
- $this->products['B']=$this->products['B']-$pair;
- return $this->products;
- }else{
- return $this->products;
- }
- }
- }
- class d2 {
- private $products;
- private $price_products;
- function __construct($products,$price_products){
- $this->products=$products;
- $this->price_products=$price_products;
- }
- function calculate_discount(){
- if(array_key_exists('D',$this->products)&& array_key_exists('E',$this->products)
- && $this->products['D']!=0 && $this->products['E']!=0){
- $pair=min(array($this->products['D'],$this->products['E']));
- $this->products['discount']=$this->products['discount']+((($pair*($this->price_products['D']+$this->price_products['E']))/100)*10);
- $this->products['D']=$this->products['D']-$pair;
- $this->products['E']=$this->products['E']-$pair;
- return $this->products;
- }else{
- return $this->products;
- }
- }
- }
- class d3 {
- private $products;
- private $price_products;
- function __construct($products,$price_products){
- $this->products=$products;
- $this->price_products=$price_products;
- }
- function calculate_discount(){
- if(array_key_exists('E',$this->products)&& array_key_exists('F',$this->products)&& array_key_exists('G',$this->products)
- && $this->products['E']!=0 && $this->products['F']!=0 && $this->products['G']!=0){
- $pair=min(array($this->products['E'],$this->products['F'],$this->products['G']));
- $this->products['discount']=$this->products['discount']+((($pairEFG*($this->price_products['E']+$this->price_products['F']+$this->price_products['G']))/100)*5);
- $this->products['E']=$this->products['E']-$pair;
- $this->products['F']=$this->products['F']-$pair;
- $this->products['G']=$this->products['G']-$pair;
- return $this->products;
- }else{
- return $this->products;
- }
- }
- }
- class d4 {
- private $products;
- private $price_products;
- function __construct($products,$price_products){
- $this->products=$products;
- $this->price_products=$price_products;
- }
- function calculate_discount(){
- if(array_key_exists('A',$this->products)&& $this->products['A']!=0
- && ((array_key_exists('K',$this->products)&& $this->products !=0)
- xor (array_key_exists('L',$this->products)&& $this->products['L']!=0)
- xor (array_key_exists('M',$this->products) && $this->products['M']!=0))){
- if($this->products['K']){
- $key='K';
- }elseif($this->products['L']){
- $key='L';
- }else{
- $key='M';
- }
- $this->products['A']=$this->products['A']-$pair;
- $this->products[$key]=$this->products[$key]-$pair;
- $this->products['discount']=$this->products['discount']+($this->price_products[$key]/100*5);
- return $this->products;
- }else{
- return $this->products;
- }
- }
- }
- class d5 {
- private $products;
- private $price_products;
- function __construct($products,$price_products){
- $this->products=$products;
- $this->price_products=$price_products;
- }
- function calculate_discount(){
- if(!array_key_exists('A',$this->products)&& !array_key_exists('C',$this->products)&& count($this->products)==4 && $this->products['discount']!=0 ){
- $price=0;
- foreach($this->products as $key=>$value){
- if($key!='discount'){
- foreach($this->price_products as $key1=>$value1){
- if($key==$key1){
- $price_part=$value*$value1;
- }
- $prise+=$price_part;
- }
- }
- }
- $this->products['discount']=$this->products['discount']+((array_sum($price))/100*5);
- return $this->discount5;
- }else{
- return $this->products;
- }
- }
- }
- class d6 {
- private $products;
- private $price_products;
- function __construct($products,$price_products){
- $this->products=$products;
- $this->price_products=$price_products;
- }
- function calculate_discount(){
- if(!array_key_exists('A',$this->products)&& !array_key_exists('C',$this->products)&& count($this->products)==5 && $this->products['discount']!=0 ){
- $price=0;
- foreach($this->products as $key=>$value){
- if($key!='discount'){
- foreach($this->price_products as $key1=>$value1){
- if($key==$key1){
- $price_part=$value*$value1;
- }
- $prise+=$price_part;
- }
- }
- }
- $this->products['discount']=$this->products['discount']+((array_sum($this->products))/100*10);
- return $this->products;
- }else{
- return $this->products;
- }
- }
- }
- class d7 {
- private $products;
- private $price_products;
- function __construct($products,$price_products){
- $this->products=$products;
- $this->price_products=$price_products;
- }
- function calculate_discount(){
- if(!array_key_exists('A',$this->products)&& !array_key_exists('C',$this->products)&& count($this->products)==6 && $this->products['discount']!=0){
- $price=0;
- foreach($this->products as $key=>$value){
- if($key!='discount'){
- foreach($this->price_products as $key1=>$value1){
- if($key==$key1){
- $price_part=$value*$value1;
- }
- $prise+=$price_part;
- }
- }
- }
- $this->products['discount']=$this->products['discount']+((array_sum($this->products))/100*20);
- return $this->products;;
- }else{
- return $this->products;
- }
- }
- }
- $disc=new DiscountManager($discounts,$products,$price_products);
- $disc->create_obj_discount();
- $order=new order_Manager($price_products,$disc->total_discount,$products);
- echo $order->sum_order();
- ?>
Add Comment
Please, Sign In to add comment