plumwd

Untitled

Jan 17th, 2018
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.22 KB | None | 0 0
  1. <?php
  2. class Basket extends Controller {
  3.  
  4.     var $CustomerId;
  5.     var $ColsolidatedShippingCharges;
  6.    
  7.     function Basket()
  8.     {
  9.         parent::Controller();
  10.         $this->load->model('ProductOption');
  11.         $this->load->model('ProductCustomiseArea');
  12.         $this->load->model('OptionValue');
  13.         $this->load->model('Product');
  14.         $this->load->model('Option');
  15.         $this->load->model('Currency');
  16.         $this->Currency->Load();
  17.         $this->load->helper('date');
  18.  
  19.  
  20.         if( $this->session->userdata('logged_in')==true )  
  21.         {
  22.             $this->load->model('CustomerBasket');
  23.             $this->load->model('CustomerBasketProductOption');
  24.             $this->load->model('CustomerBasketCustomiseArea');
  25.             $this->Basket = $this->CustomerBasket;
  26.             $this->BasketProductOption = $this->CustomerBasketProductOption;
  27.             $this->BasketCustomiseArea = $this->CustomerBasketCustomiseArea;
  28.            
  29.             $this->CustomerId = $this->session->userdata('customer_id');
  30.         }
  31.         else
  32.         {
  33.             $this->load->model('VisitorBasket');
  34.             $this->load->model('VisitorBasketProductOption');
  35.             $this->load->model('VisitorBasketCustomiseArea');
  36.             $this->Basket = $this->VisitorBasket;
  37.             $this->BasketProductOption = $this->VisitorBasketProductOption;
  38.             $this->BasketCustomiseArea = $this->VisitorBasketCustomiseArea;
  39.            
  40.             $this->CustomerId = $this->session->userdata('session_id');
  41.         }
  42.        
  43.     }
  44.    
  45.     function index()
  46.     {
  47.         redirect('/home/index');
  48. //      $this->layout->render('welcome_message');
  49.     }
  50.    
  51.     function add()
  52.     {
  53.         if(empty($_POST['product_id']))
  54.             die('Invalid Data');
  55.          $post_count=count($_POST);
  56.                  
  57.                  
  58.         $total_price_show = 0;
  59.         $total_quantity_show = 0;
  60.         $pro_data=$this->Product->Getprice($_POST['product_id']);
  61.         //if($pro_data['price_type']== "regular" || $pro_data['price_type']== "sale")
  62.         {
  63.             $productdata =  $this->Basket->GetCartValues($this->CustomerId);
  64.             if(!empty($productdata))
  65.             {
  66.                 foreach($productdata as $index=>$value)
  67.                 {
  68.                     $total_price_show += $value["price"];
  69.                     $total_quantity_show++;
  70.                     if($value['product_id']==$_POST['product_id'])
  71.                     {
  72.                         $newdata = array(
  73.                        'themessage'  => 'The product is already in the basket. You can update the quantity from below.',
  74.                        'condition'     => 'Y'
  75.                          );
  76.  
  77.                     //$this->session->set_flashdata($newdata); 
  78.                     //redirect('basket/view');
  79.                     }
  80.                 }  
  81.             }
  82.         }
  83.        
  84.         if( $this->session->userdata('logged_in') == true ){  
  85.             $rec = $this->CustomerBasket->new_record();
  86.             $rec->customer_id = $this->CustomerId;
  87.         }
  88.         else{
  89.             $rec = $this->VisitorBasket->new_record();
  90.             $rec->session_id = $this->session->userdata('session_id');
  91.         }
  92.         $rec->product_id = $_POST["product_id"];
  93.         $quantity = ($_POST["quantity"] == '' || $_POST["quantity"] == 0 || !is_numeric($_POST["quantity"])) ? 1 : $_POST["quantity"];
  94.         $rec->quantity = $quantity;
  95.         $totalprice=0;
  96.         $unit_price=0;
  97.  
  98.                 $groupValues='';
  99.                 $group_Price=array();
  100.                 $groupPrice=0;
  101.                 foreach ($_POST as $key => $value) {
  102.                     if(strpos($key, 'groupOption_') !== false){
  103.                         if(empty($groupValues))
  104.                            $groupValues = $value;
  105.                         else
  106.                            $groupValues .= ",".$value;
  107.                       }
  108.  
  109.                 }
  110.                if(!empty($groupValues)){
  111.                $group_Price = $this->ProductOption->getOptionsValuePrice($groupValues);
  112.                }
  113.                if(!empty($group_Price)){
  114.                if($pro_data['price_type'] == "sale"){
  115.                     $groupPrice = $group_Price[0]['option_values_sale_price'];            
  116.                  }
  117.                  else{
  118.                      $groupPrice = $group_Price[0]['option_values_price'];
  119.                  }
  120.                }
  121.         $rec->price = $totalprice+$groupPrice;
  122.         $rec->unit_price = $unit_price;
  123.         $rec->date_updated = mdate("%Y-%m-%d %h:%i:%s");
  124.         $rec->date_added = mdate("%Y-%m-%d %h:%i:%s");
  125.         $rec->save();
  126.         $sizelist = $this->ProductOption->GetSizeList();
  127.         $attachedAreaId = NULL;
  128. //      if ($pro_data["price_type"]=="option_based")
  129. //      {
  130.             $this->ProductOption->group_by('option_id');
  131.             $values=$this->ProductOption->find_all_by('product_id',$_POST["product_id"]);                      
  132.             $order_total = 0;
  133.             $pro = $this->Product->find($_POST["product_id"]);
  134.             $base_price =  ($pro->price_type == "regular" || $pro->price_type == "option_based") ?  $pro->regular_price : $pro->sale_price;
  135.             $order_total += $base_price;
  136.             $basketid = $this->db->insert_id();
  137.             $_SESSION['basketid'] = $basketid;
  138.             $i = 0;
  139.             // Add non percent product options
  140.             foreach($values as $value)
  141.             {
  142.                 // combo box, radiobox
  143.                 if( isset($_POST["option_" . $value->option_id]) && is_numeric($_POST["option_" . $value->option_id]) ){
  144.                     $opt = $this->OptionValue->find($_POST["option_" . $value->option_id]);
  145.                     if(in_array($opt->option_id, $sizelist))
  146.                         $attachedAreaId = $opt->option_id;
  147.                    
  148.                     if($opt->is_percent == 'N'){
  149.                         $order_total += ($this->addProductOptionValue($basketid, $value->option_id, $_POST["option_" . $value->option_id], $order_total,$pro_data['price_type']));
  150.                     }
  151.                 }
  152.                 // checkbox
  153.                 else if (!empty($_POST["option_".$value->option_id])  && is_array($_POST["option_".$value->option_id]))
  154.                 {
  155.                     $opt = $this->OptionValue->find($value->option_id);
  156.                     if($opt->is_percent == 'N'){                           
  157.                         foreach($_POST["option_".$value->option_id] as $OptionValueId){
  158.                             if(is_numeric($OptionValueId))
  159.                                 $order_total += ($this->addProductOptionValue($basketid, $value->option_id, $OptionValueId, $order_total,$pro_data['price_type']));
  160.                                
  161.                         }
  162.                     }
  163.                 }
  164.  
  165.             } // end product options
  166.                         if($groupValues !=''){
  167.                             $opt_name=$this->ProductOption->get_option_name($groupValues);
  168.                             $totl_pr=$_POST['regular_price'];
  169.                             foreach ($opt_name as  $value) {
  170.                              $totl_pr += ($this->addProductOptionValue($basketid, $value->option_id, $value->option_value_id, $totl_pr,$pro_data['price_type']));    
  171.                             }
  172.                         }
  173.             // insert customise area information****************************************
  174.             // Add customize areas
  175.             $areas =$this->ProductCustomiseArea->find_all_by('product_id',$_POST["product_id"]);
  176.  
  177.             foreach($areas as $value)
  178.             {
  179.                 if(!empty($_POST["length_" . $value->id]) && !empty($_POST["width_" . $value->id]))
  180.                 {
  181.                     if( $this->session->userdata('logged_in') == true ){
  182.                         $recarea = $this->BasketCustomiseArea->new_record();
  183.                         $recarea->customer_basket_id = $basketid;
  184.                     }
  185.                     else{
  186.                         $recarea = $this->BasketCustomiseArea->new_record();
  187.                         $recarea->visitor_basket_id = $basketid;
  188.                     }
  189.                     $recarea->customise_area_id = $value->id;
  190.                     $recarea->title = $value->title;
  191.                     $recarea->unit = $value->unit;
  192.                     $recarea->width = $_POST["width_" . $value->id];
  193.                     $recarea->height = $_POST["length_" . $value->id];
  194.                     $recarea->first_label = $value->first_label;
  195.                     $recarea->second_label = $value->second_label;
  196.                    
  197.                     if($value->is_calculateable == 'Y'){
  198.                             $recarea->unit_price = $value->unit_price;
  199.                             $area_price = $this->ProductCustomiseArea->getAreaPrice($attachedAreaId, $_POST["length_" . $value->id], $_POST["width_" . $value->id], $value->unit_price, $order_total);
  200.                             $recarea->total_price = $area_price;
  201.                     }
  202.                     else{
  203.                         $recarea->unit_price = '0';
  204.                         $recarea->total_price = '0';
  205.                     }
  206.                     $order_total += $recarea->total_price;
  207.                     $recarea->save();
  208.                 }
  209. //              $a++;  
  210.             }// `end customise area insertion ***********************************************************
  211.            
  212. //      }
  213.         // Add percentage product options
  214.         $this->ProductOption->group_by('option_id');
  215.         $values2=$this->ProductOption->find_all_by('product_id',$_POST["product_id"]);                     
  216.        
  217.                 foreach($values2 as $value)
  218.         {
  219.             // combo box, radiobox
  220.             if( isset($_POST["option_" . $value->option_id]) && is_numeric($_POST["option_" . $value->option_id]) ){
  221.                 $opt2 = $this->OptionValue->find($_POST["option_" . $value->option_id]);
  222.                 if($opt2->is_percent == 'Y'){
  223.                     $order_total += ($this->addProductOptionValue($basketid, $value->option_id, $_POST["option_" . $value->option_id], $order_total,$pro_data['price_type']));
  224.                 }
  225.             }
  226.             // checkbox
  227.             else if (!empty($_POST["option_".$value->option_id])  && is_array($_POST["option_".$value->option_id]))
  228.             {
  229.                 $opt2 = $this->OptionValue->find($value->option_id);
  230.                 if($opt2->is_percent == 'Y'){                          
  231.                     foreach($_POST["option_".$value->option_id] as $OptionValueId){
  232.                         if(is_numeric($OptionValueId))
  233.                         $order_total += ($this->addProductOptionValue($basketid, $value->option_id, $OptionValueId, $order_total,$pro_data['price_type']));
  234.                     }
  235.                 }
  236.             }
  237.  
  238.         } // end product options
  239.         // update Basket *****************
  240.         $rec_update = $this->Basket->find($basketid);
  241.         $rec_update->price = $order_total * $quantity+$groupPrice;
  242.         $rec_update->unit_price = $order_total+$groupPrice;
  243.        
  244.                
  245.         if($rec_update->save())
  246.         {          
  247.             $this->view(true);
  248.             global $CURRENCY;
  249.             $total_quantity_show++;
  250.             $total_price_show += $rec_update->price;
  251.             $return_data['html']= '<p>You have '.$total_quantity_show.' items in your basket</p>
  252.             <p><strong>Total Cost: '.current($CURRENCY).number_format($total_price_show, 2, '.', '').'</strong></p>
  253.             ';
  254.             $return_data['quantity']=$total_quantity_show;
  255.             $return_data['price']=number_format($total_price_show,2,'.','');
  256.             echo json_encode($return_data);
  257.         }
  258.         else
  259.             echo 'error';
  260.         exit;
  261.         //redirect('basket/view');
  262.     }  
  263.    
  264.     private function addProductOptionValue($basketid, $OptionId, $selectedOptionValue, $TotalOrder=0, $price_type='Regular')
  265.     {
  266.        
  267.         if( $this->session->userdata('logged_in') == true )
  268.             $recrd = $this->CustomerBasketProductOption->new_record();
  269.         else
  270.             $recrd = $this->VisitorBasketProductOption->new_record();
  271.            
  272.         $recrd->basket_id = $basketid;
  273.         $sizelist = $this->ProductOption->GetSizeList();
  274.         $data_opt = $this->ProductOption->GetProductValueDetail($selectedOptionValue, $_POST["product_id"]);
  275.         $isAreaSelected =false;
  276.         $areas =$this->ProductCustomiseArea->find_all_by(array('product_id','is_calculateable','is_active', 'is_deleted'),array($_POST["product_id"],'Y', 'Y', 'N'));
  277.         foreach($areas as $value)
  278.         {
  279.             if(!empty($_POST["length_" . $value->id]) && !empty($_POST["width_" . $value->id]) )
  280.                 $isAreaSelected =true;
  281.         }
  282.  
  283.        
  284.                 if(empty($data_opt))    return 0;
  285.         if(($isAreaSelected && !in_array($data_opt[0]['option_id'], $sizelist)) || (!$isAreaSelected))
  286.         {
  287.            
  288.             $recrd->option_id = $data_opt[0]['option_id'];
  289.             $recrd->option_value_id = $data_opt[0]['option_value_id'];
  290.                         if($price_type == "sale"){
  291.                             $recrd->price =  ($data_opt[0]['is_percent']=='Y') ? ($data_opt[0]['price']/100)* $TotalOrder: $data_opt[0]['sale_price'];
  292.                         }
  293.                         else{
  294.                 $recrd->price =  ($data_opt[0]['is_percent']=='Y') ? ($data_opt[0]['price']/100)* $TotalOrder: $data_opt[0]['price'];
  295.                         }
  296.                        
  297.             $recrd->prefix = $data_opt[0]['prefix'];
  298.         }
  299.         else
  300.             return 0;
  301.            
  302.        
  303.        
  304.                 $option_name = $this->Option->find($OptionId);
  305.                 $recrd->name = $option_name->name;
  306.  
  307.         $option_value = $this->OptionValue->find($selectedOptionValue);
  308.         $recrd->value = $option_value->value;
  309.         $recrd->save();    
  310.         return ''.$recrd->prefix.$recrd->price;
  311.  
  312.     }
  313.    
  314.     function view($returnData=false)
  315.     {
  316.  
  317.         global $CURRENCY;
  318.         if( $this->session->userdata('logged_in') == true )
  319.             $data = $this->Basket->GetCartValues($this->CustomerId);
  320.         else
  321.             $data = $this->Basket->GetCartValues($this->session->userdata('session_id'));
  322.         $data_array=array();
  323.         $j=0;
  324.        
  325.         $totalQuantity = $totalProducts = $totalPrice = 0;
  326.         $currencyCode = '';
  327.        
  328.        
  329.         $currencyCode = (current($CURRENCY));
  330.         foreach($data as $key=>$value)
  331.         {  
  332.                         $product_p=$this->Product->Getprice($value['product_id']);
  333.                         if($product_p['price_type']=='sale') { $product_price=$product_p['sale_price']; }
  334.                         else { $product_price=$product_p['regular_price']; }
  335.                         $url_slug=$this->ProductOption->get_slug('products','id',$value['product_id']);
  336.             $data_array['rec'][$j]['name'] = $value['name'];
  337.             $data_array['rec'][$j]['code'] = $value['code'];
  338.                         $data_array['rec'][$j]['url_slug'] = $url_slug;
  339.                         $data_array['rec'][$j]['base_price'] = $product_price;
  340.             $data_array['rec'][$j]['currency_code'] = $CURRENCY[$value['currency_id']] ;
  341.             $data_array['rec'][$j]['unit_price'] = $value['unit_price'];
  342.             $data_array['rec'][$j]['quantity'] = $value['quantity'];
  343.             $data_array['rec'][$j]['price'] = $value['price'];
  344.             $data_array['rec'][$j]['price_type'] = $value['price_type'];
  345.             $data_array['rec'][$j]['id'] = $value['id'];
  346.             $data_array['rec'][$j]['product_id'] = $value['product_id'];   
  347.             $data_array['rec'][$j]['currency_id'] = $value['currency_id'];
  348.             $data_array['rec'][$j]['option_values'] = $value['values'];
  349.             $data_array['rec'][$j]['area_values'] = $value['customise_areas'];
  350.            
  351.             $totalQuantity += $value['quantity'];
  352.             $totalPrice += $value['price'];
  353.             $totalProducts++;
  354.             $j++;
  355.         }
  356.  
  357.         $this->session->set_userdata('quantity', $totalQuantity);
  358.         $this->session->set_userdata('total_price', $totalPrice);
  359.         $this->session->set_userdata('currency_code', $currencyCode);
  360.         $this->session->set_userdata('totalpurchase', $totalProducts);
  361.         $data_array['currency_code'] = $currencyCode;
  362.         $data_array['meta']['title'] = HOME_META_TITLE;
  363.         $data_array['meta']['keywords'] =   HOME_META_KEYWORD;
  364.         $data_array['meta']['description'] = HOME_META_DESCRIPTION;
  365.        
  366.         if($returnData)
  367.             return $data_array;
  368.         else
  369.             $this->layout->render('basket/view', $data_array,false,'header_less_layout');  
  370.     }
  371.            
  372.  
  373. function update()
  374. {
  375.     foreach($_POST["quantity"] as $basket_id=>$quantity)
  376.     {  
  377.         $allprodutcs = "";
  378.         $myBaskets = $this->Basket->find_by(array('id'=> $basket_id));
  379.         $quantity_response = $this->Product->VerifyQuantity($myBaskets->product_id, $quantity);
  380.         if($quantity_response === true)
  381.             $this->Basket->Update_Basket($basket_id, $quantity);       
  382.         else
  383.         {  
  384.             $allproducts .= $quantity_response . ", ";
  385.         }      
  386.     }  
  387.     if(!empty($allproducts))
  388.     {
  389.         $newdata = array(
  390.                        'showmessage'  => 'Y',
  391.                        'products'     => $allproducts
  392.                    );
  393.         $this->session->set_flashdata($newdata);
  394.     }  
  395.     redirect('basket/view');       
  396. }      
  397.     function remove($basket_id)
  398.     {
  399.         if( $this->session->userdata('logged_in') == true ){
  400.             $this->Basket->Delete_Basket($basket_id);
  401.             //$this->BasketProductOption->Delete_Basket($basket_id);
  402.         }
  403.         else{
  404.             $this->Basket->Delete_Basket($basket_id);
  405.             //$this->BasketProductOption->Delete_Basket($basket_id);
  406.         }
  407.        
  408.         redirect('basket/view');
  409.     }  
  410.     function listing()
  411.     {
  412.         if( $this->session->userdata('logged_in') == true )
  413.             $data = $this->Basket->GetCartValues($this->CustomerId);
  414.         else
  415.             $data = $this->Basket->GetCartValues($this->CustomerId);
  416.         print_r($data);
  417.  
  418.     }
  419.  
  420. }
  421.  
  422.  
  423. ?>
Advertisement
Add Comment
Please, Sign In to add comment