Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

<?php
App::uses('AppController', 'Controller');
/**
 * Preferences Controller
 *
 * @property Preference $Preference
 * @property PaginatorComponent $Paginator
 */
class CashbacksController extends AppController {

/**
 * Components
 *
 * @var array
 */
        public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
        public function index($store_id = null,$product_code = null) {
                $this->layout = 'ajax';
                $this->response->type('json');
                $this->loadModel('Api');
                if(!$store_id || !$product_code){
                        $result = array('success'=>false,'message'=>'Either Source or Product Code is missing','cashback_type'=>'value','cashback'=>0);
                }else{
                        //First check if sku wise cashback is configured
                        $this->loadModel('ProductCashback');
                        $options = array('conditions'=>array('product_id'=>$product_code,'store_id'=>$store_id),'recursive'=>-1);
                        $productcashback = $this->ProductCashback->find('first',$options);
                        if(!empty($productcashback)){
                                if(!empty($productcashback['ProductCashback']['cashback_percantage'])){
                                        $cashback_type = 'percentage';
                                        $value = $productcashback['ProductCashback']['cashback_percantage'];
                                }else{
                                        $cashback_type = 'value';
                                        $value = $productcashback['ProductCashback']['cashback_amount'];
                                }
                                $result = array('success'=>true,'message'=>'Sku level cashback configured','cashback_type'=>$cashback_type,'cashback'=>$value);
                        }else{
                                //Check if category wise cashback is configured                         
                                $this->loadModel('StoreProduct');
                                $options = array('conditions'=>array('store_id'=>$store_id,'StoreProduct.url LIKE'=>"%$product_code%"),'fields'=>array('category_id'),'recursive'=>-1);
                                $storeproduct = $this->StoreProduct->find('first',$options);
                                if(empty($storeproduct)){
                                        //Category couldn't be determined, so no cashback can be offered
                                        $result = array('success'=>false,'message'=>'Unable to determine category','cashback_type'=>'value','cashback'=>0);
                                }else{
                                        $category_id = $storeproduct['StoreProduct']['category_id'];
                                        //Fetch category discount
                                        $this->loadModel('StoreCashback');
                                        $options = array('conditions'=>array('store_id'=>$store_id,'category_id'=>$category_id),'recursive'=>-1);
                                        $storecashback = $this->StoreCashback->find('first',$options);
                                        if(!empty($storecashback)){
                                                $result = array('success'=>true,'message'=>'Store level cashback configured','cashback_type'=>'percentage','cashback'=>$storecashback['StoreCashback']['cashback_percantage']);
                                        }else{
                                                $result = array('success'=>false,'message'=>'Cashback has not been configured for this store & category combination','cashback_type'=>'value','cashback'=>0);
                                        }
                                }       
                        }
                }
                $this->set(array(
                    'result' => $result,
                    '_serialize' => array('result')
                ));             
                $this->render('/Elements/json');
        }
}