<?php

namespace OfficeBrain\CustomBundle\ServiceGroupBundle\Manager\Extended;

use OfficeBrain\Bundle\ServiceGroupBundle\Manager\Extended\ServiceRuleChargesCalculationExtended as BaseServiceRuleChargesCalculationExtended;

/**
 * @author OfficeBrain 4273 <info@officebrain.com>
 *
 * Description : Service Group Extended Class
 */
class ServiceRuleChargesCalculationExtended extends BaseServiceRuleChargesCalculationExtended
{
    public function __construct($entityManager, $request, $session, $serviceContainer, $translatorService, $countries, $languages, $coreManager, $productTagManager, $serviceGroupAPiClient, $twigExtension)
    {
       
    	parent::__construct($entityManager, $request, $session, $serviceContainer, $translatorService, $countries, $languages, $coreManager, $productTagManager, $serviceGroupAPiClient, $twigExtension);
        
    }

    public function __destruct()
    {
        parent::__destruct();
    } 
    
    /**
     * @author Employee Id: 4273
     * Description : setup charge 
     *
     * @param 1 : product Detail
     * @param 2 : imprint location
     * @param 3 : imprint method and location array
     * @param 4 : setup charge 
     * @param 5 : user service group ID
     * @param 6 : no. of selected color
     * @param 7 : no. of selected location
     *
     * @return type : setup charge 
     *
     * @throws Exception: Null
     */

    public function applySetupChargeRule($productDetail, $imprintLocation, $imprintMethodLocationArray, $setupChargeRate, $userServiceGroupId, $noOfSelectedColor, $noOfSelectedLocation)
    {
    	$finalSetupCharge = 0;
    
    	if ($setupChargeRate > 0) {
    		//Rule information
    		//setup charge id 1
    		$screenPrintMethods = $embroideryMethods = array();
    		 
    		 
    		if ($this->serviceContainer->hasParameter('screen_printing_method_id') && is_array($this->serviceContainer->getParameter('screen_printing_method_id'))) {
    			$screenPrintMethods = array_merge($screenPrintMethods, $this->serviceContainer->getParameter('screen_printing_method_id'));
    		}
    		 
    		if ($this->serviceContainer->hasParameter('embroidery_method_id') && is_array($this->serviceContainer->getParameter('embroidery_method_id'))) {
    			$embroideryMethods = array_merge($embroideryMethods, $this->serviceContainer->getParameter('embroidery_method_id'));
    		}
    		 
    		 
    		$serviceRuleChargeProductPriceArray = $this->serviceRuleChargeProductPriceRepository->findByProductIdAndType($productDetail['product_id'], 'setup', $userServiceGroupId);
    
    		if (!$serviceRuleChargeProductPriceArray || in_array($imprintMethodLocationArray['imprint_method_id'], $embroideryMethods)) {
    			if((isset($productDetail['imprint_information']['imprint_method_information'][$imprintMethodLocationArray['imprint_method_id']]['full_color'])
    					&& $productDetail['imprint_information']['imprint_method_information'][$imprintMethodLocationArray['imprint_method_id']]['full_color'])
    					 || in_array($imprintMethodLocationArray['imprint_method_id'], $embroideryMethods))
    				return $setupChargeRate ; 
    			else
    				return $setupChargeRate * $noOfSelectedColor;    			
    		}
    
    		foreach ($serviceRuleChargeProductPriceArray as $serviceRuleChargeProductPrice) {
    			//get service Rule Master
    			$serviceRuleMappingMaster = $serviceRuleChargeProductPrice->getRuleMappingId();
    
    			//check is setup charge rule satisfied or not
    			if (!$this->checkRuleForCharges($serviceRuleMappingMaster, $imprintLocation, $imprintMethodLocationArray)) {
    				return $finalSetupCharge;
    			}
    
    			//get service rule charge equation array
    			$serviceRuleChargesEquationArray = $serviceRuleMappingMaster->getServiceRuleChargesEquationId();
    
    			if ($serviceRuleChargesEquationArray && $serviceRuleChargesEquationArray->count() > 0) {
    				//check all realted equations
    				foreach ($serviceRuleChargesEquationArray as $serviceRuleChargesEquation) {
    					//TODO: overwrite base setup charge if in rule equation
    					//$baseCharge = ($serviceRuleChargesEquation->getBaseCharge1()) ? $serviceRuleChargesEquation->getBaseCharge1() : $setupChargeRate;
    					$firstOperator = $this->getConditionOperator($serviceRuleChargesEquation->getConditionOperator1());
    					$secoundOperator = $this->getConditionOperator($serviceRuleChargesEquation->getConditionOperator2());
    					$amountToCalculate = ($serviceRuleChargesEquation->getAmountType() === 1) ? $serviceRuleChargesEquation->getAmount() : $serviceRuleChargesEquation->getAmount()/100;
    
    					//make equation based on setup charge rule
    					$finalSetupCharge += eval('return '.$setupChargeRate . $firstOperator .  $setupChargeRate . $secoundOperator . $amountToCalculate.';');
    				}
    			}
    		}
    	}
    	if(isset($productDetail['imprint_information']['imprint_method_information'][$imprintMethodLocationArray['imprint_method_id']]['full_color'])
    					&& $productDetail['imprint_information']['imprint_method_information'][$imprintMethodLocationArray['imprint_method_id']]['full_color'])
    		return $finalSetupCharge ;
    	else
    		return $finalSetupCharge * $noOfSelectedColor;
    	
    }
    
    
    /**
     * @author Employee Id: 4273
     * Description :­ User input base charge - apply only user select split shipping option
     *
     * @param 1 : input post Detail
     * @param 2 : product Detail
     * @param 3 : total quantity selected
     * @param 4 : total PMS color selected
     * @param 5 : user service group ID
     *
     * @return type : split shipping charge Array
     *
     * @throws Exception: Null
     */
    public function SPLIT_SHIPPING_CHARGE_CALCULATION($inputDetail, $productDetail,  $totalQtySelected, $totalPMSColorSelected, $userServiceGroupId)
    {   
    	 
    	$splitShippingCharges = array();
    
    	if ($this->serviceContainer->hasParameter('static_charges') ) {
    				 
    		$staticData = $this->serviceContainer->getParameter('static_charges');
    				
    		if (isset($staticData['split']['rate']) && $staticData['split']['rate'] > 0 && isset($inputDetail['shipping_method']['shipping_type']) &&
    			$inputDetail['shipping_method']['shipping_type']=='split' && count($inputDetail['shipping_method']['shipping_detail']) > $staticData['split']['free_address']) 
    		{    		
    			$rate = $staticData['split']['rate'] * (count($inputDetail['shipping_method']['shipping_detail']) - $staticData['split']['free_address']);
    			
    			$splitShippingCharges["charges_amount"]= $this->TwigExtension->priceFilter($rate);
    			$splitShippingCharges["charges_amount_js"]= $this->TwigExtension->currencyFilter($rate);
    			$splitShippingCharges["charges_type"]= ucfirst($this->translatorService->trans('admin.service_group_manager.charge_type.split'));
    			$splitShippingCharges['currency_symbol'] = $productDetail['currency']['symbol'];
    		}
    				 
    	}
    			 
    	return $splitShippingCharges;
    }
        
        
    
}
