<?php
/**
 * @link         http://www.openzc.cn/
 * @author       hexipeng | e-mail:2017412656@qq.com
 * @copyright    Copyright (c) 2020, Openzc, Inc.
 * @license      http://www.openzc.cn/license.txt
 */
class modulesModel{
	function Run(&$atts, &$refObj, &$fields){
		global $payment_modules,$order_total_modules,$order,$shipping_modules,$quotes,$shipping_modules,$currencies;
		$attlist = "item=,type=,field=fields,active=false,code=,nocode=,flag=";
		FillAtts($atts,$attlist);
		FillFields($atts,$fields,$refObj);
		extract($atts, EXTR_OVERWRITE);
		switch($item){
			case "payment":
				if(!$payment_modules){
					require(DIR_WS_CLASSES . 'payment.php');
	  				$payment_modules = new payment;
					
				}
				$selection = $payment_modules->selection();
				
				if(isset($_SESSION['payment'])){
					foreach($selection as $k => $v){
						if($v['id']==$_SESSION['payment']){
							$selection[$k]['status']="active";
						}
					}
				}else{
					$selection[0]['status']="active";
				}
				if($type=="son" && $field){
					return $fields[$field];
				}
			break;
			case "shipping":
				if($shipping_modules){
					$quotes=$shipping_modules->quote();
				}
				if($active=="true" && isset($_SESSION['shipping'])){
					$selection=$_SESSION['shipping']['title']." ".$currencies->format($_SESSION['shipping']['cost']);
					return $selection;
				}
				$_SESSION['quotes']=$quotes;
				$selection=$quotes;
				if($type=="son"){
					foreach($fields['methods'] as $k => $v){
						$fields['methods'][$k]['pid']=$fields['id'];
					}
					return $fields['methods'];
				}
				foreach($selection as $k => $v){
					foreach($v['methods'] as $a => $b){
						$selection[$k]['methods'][$a]['cost']=$currencies->format(zen_add_tax($b['cost'], (isset($v['tax']) ? $v['tax'] : 0)));
						$thisid=$v['id']."_".$b['id'];
						if($_SESSION["shipping"]['id']==$thisid || $GLOBALS['selected_shipping']['id']==$thisid){
							$selection[$k]['methods'][$a]['status']="active";
						}else{
							$selection[$k]['methods'][$a]['status']="";
						}
						$selection[$k]['sort_index']='"'.$k.'"';
						$selection[$k]['methods'][$a]['module']=$v['module'];
					}
				}
			break;
			case "order_total":
				
				if(!isset($GLOBALS['shipping_modules'])){
					require(DIR_WS_CLASSES . 'shipping.php');
  					$shipping_modules = new shipping;
					$shipping_modules->quote();
				}
				if ((!isset($_SESSION['shipping']) || (!isset($_SESSION['shipping']['id']) || $_SESSION['shipping']['id'] == '') && zen_count_shipping_modules() >= 1)){
				    $_SESSION['shipping'] = $shipping_modules->cheapest();
				}
				$included=false;
				foreach(get_included_files() as $k => $v){
					if(strstr($v,"order.php")){
						$included=true;
					}
				}
				if($included==false){require(DIR_WS_CLASSES . 'order.php');}
  				$order = new order;
				
				if(!isset($GLOBALS['order_total_modules']) ){
					require(DIR_WS_CLASSES . 'order_total.php');
					$order_total_modules = new order_total;
				}
				$string=$order_total_modules->output();

				
				if($GLOBALS['order_total']){
					$selection=$GLOBALS['order_total'];
				}else{
					$GLOBALS['order_total']=array_filter($order_total_modules->process());

					$selection=$GLOBALS['order_total'];
				}

				$check=[];
				foreach($selection as $k => $v){
					if(isset($check[$v['code']]) || $nocode==$v['code']){
						unset($selection[$k]);
					}
					$check[$v['code']]=true;
				}
				if($code){
					foreach($selection as $k => $v){
						if($v['code']==$code){
							$result[]=$v;
						}
					}
					$selection=$result;
				}

			break;
		}
		foreach($selection as $k => $v){
			$selection[$k]['sort_index']=$k+1;
		}
		return $selection;
	}
}
  
  
?>