<?php

namespace OfficeBrain\Bundle\ProductBundle\Entity;

/**
 * ProductMasterRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ProductMasterRepository extends \Doctrine\ORM\EntityRepository{
	
	 /**
	 * Emp Id - 5190 
     * To add new product
     * 
     * @param string $sku_no
     * @param \Brand $brand
     * @param string $video_url
     * @param integer $supplier_id
     * 
     * @return \OfficeBrain\Bundle\ProductBundle\Entity\ProductMaster
     */
    public function addNew($sku_no,$brand, $params, $supplier_id,$processing_days=""){
        
        $profing_req=0; ;
        $productMasterObj = new ProductMaster();
        
        $productMasterObj->setSkuNumber($sku_no);
        $productMasterObj->setMaskedSKU($sku_no);
        $productMasterObj->setSupplierId($supplier_id);
        $productMasterObj->setBrandId($brand);
        $productMasterObj->setPmsId($params['master_product_id']);
        if(!empty($params['video_url']))
            $productMasterObj->setVideoUrl($params['video_url']);
        if(!empty($params['valid_up_to'])){            
            $valid_up_to_array = explode("-",$params['valid_up_to']);
            $mktime = mktime(0,0,0,$valid_up_to_array[0],$valid_up_to_array[1],$valid_up_to_array[2]);
            $productMasterObj->setValidUpTo(date('Y-d-m',$mktime));
            
        }
        if(!empty($params['product_source']))
        	$productMasterObj->setProductSource($params['product_source']);
        if(!empty($params['migration_id']))
        	$productMasterObj->setMigrationId($params['migration_id']);
        
        $this->_em->persist($productMasterObj);
        $this->_em->flush();
        
        return $productMasterObj;
    }
    
    /**
     * Emp Id - 5190
	*/
    
    public function checkDuplicateSku($SKU,$supplier_id=1,$id_to_skip=""){
    	$queryBuilder = $this->getEntityManager()->createQueryBuilder();
    
    	$query = $queryBuilder->select('pm.id')
    	->from('OfficeBrainProductBundle:ProductMaster','pm')
    	->where(' trim(pm.maskedSKU) = :bsku_number')
    	->setParameter('bsku_number',$SKU)
    	->andWhere(' pm.supplierId = :bsupplier_id')
    	->setParameter('bsupplier_id',$supplier_id)
    	->andWhere('pm.deletedAt is NULL') //For soft delete - ProductMaster
    	;
    	if($id_to_skip){
    		$query->andWhere($queryBuilder->expr()->neq('pm.id',$id_to_skip));
    	}
    	
    	$data = $query->getQuery()->getResult();
    	if(count($data)){
    		return true;
    	}
    	return false;
    
    }
    
    /**
     * Emp Id - 5190
     * To update product from product id
     *
     * @param integer $id
     * @param string $sku_no
     * @param \Brand $brand
     * @param integer $supplier_id
     *
     * @return boolean|Ambigous <object, NULL>
     */
    public function update($id, $sku_no, $brand,$supplier_id=1,$params=array()){
    
    	$productMasterObj = $this->findOneBy(array('id'=>$id, 'deletedAt' => null));
    
    	if(!is_object($productMasterObj)) return false;
    
    	if(!empty($params)){
    		if(!empty($params['video_url']))
    			$productMasterObj->setVideoUrl($params['video_url']);
    			if(!empty($params['valid_up_to'])){
    				$valid_up_to_array = explode("-",$params['valid_up_to']);
    				$mktime = mktime(0,0,0,$valid_up_to_array[0],$valid_up_to_array[1],$valid_up_to_array[2]);
    				$productMasterObj->setValidUpTo(date('Y-d-m',$mktime));
    				 
    			}
    	}
    	$productMasterObj->setMaskedSKU($sku_no);
    	$productMasterObj->setBrandId($brand);
    	$productMasterObj->setSupplierId($supplier_id);
    	//$productMasterObj->setEditData( base64_encode(serialize($params))); 
    
    	if(!empty($params['product_source']))
    		$productMasterObj->setProductSource($params['product_source']);
    
    		$this->_em->persist($productMasterObj);
    		$this->_em->flush();
    
    		return $productMasterObj;
    }
    
    
    /**
     * Emp Id - 5190
     * 
     * get product info by product id
     * @param integer $productId
     * @return product master object
     */
    
    public function getProductInfoById($productId){
    	$em = $this->getEntityManager();
    	$em->getFilters()->disable('softdeleteable');
    	 
    	$queryBuilder = $em->createQueryBuilder();
    	$queryBuilder->select('pm')
    	->from('OfficeBrainProductBundle:ProductMaster','pm')
    	->where('pm.id=:productId')
    	->setParameter('productId',$productId)
    	->andWhere('(pm.deletedAt IS NULL OR pm.deletedAt IS NOT NULL)'); //For soft delete - ProductMaster
    	 
    	$productMasterObject = $queryBuilder->getQuery()->getResult();
    	 
    	$em->getFilters()->enable('softdeleteable');
    	return $productMasterObject;
    }
    
    public function addFromSolr($productData){
    	$productMasterObj = new ProductMaster();
    	
    	$productMasterObj->setSkuNumber($productData['sku']);
    	$productMasterObj->setMaskedSKU($productData['sku']);
    	$productMasterObj->setSupplierId($productData['supplier_id']);
    	$productMasterObj->setPmsId($productData['product_id']);
    	$this->_em->persist($productMasterObj);
    	$this->_em->flush();
    	
    	return $productMasterObj;
    }
    
    public function deleteProduct($masterProductId,$distributorProductId){
    	$em = $this->getEntityManager();
    	$productMasterObj = $this->findOneBy(array('pmsId' => $masterProductId,'id'=>$distributorProductId));
    	$em->remove($productMasterObj);
    	$em->flush();
    	return $productMasterObj;
    }
	
}
