<?php
/**
* @version      $Id: admin_shipping_area_editor.php
* @package      Easyshop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       Richard Kammermayer <rk@ta-edv.de>
* Easyshop is a web shop system
*/

include_once './core/shipping_area.class.php';
include_once './core/deliverer.class.php';
include_once './core/customer_group.class.php';
include_once './core/customergrouppmconf.class.php';

class admin_shipping_area_editor {
	private $base_object;
	private $config;
	private $layout_object;
	private $customer_group_object;
	private $customer_group_data;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->config = $base_object->config_object;
		$this->layout_object = $layout_object;
		$this->customer_group_object = new Customer_group($base_object);
		$this->customer_group_data = $this->customer_group_object->get_all();
	}

	function run() {
		if(isset($_GET['id']) && $_GET['id']) {
			$has_id = true;
			$id = $_GET['id'];
		} else if(isset($_POST['id']) && $_POST['id']) {
			$has_id = true;
			$id = $_POST['id'];
		} else {
			$has_id = false;
		}
		
		if(isset($_POST['submit'])) {
			$is_submitted = true;
		} else {
			$is_submitted = false;
		}
		
		$customer_groups = $this->customer_group_object->get_all();
		$this->layout_object->assign('groups', $customer_groups);
	
		// get deliverer
		$deliverer_object = new Deliverer($this->base_object);
		$deliverers = $deliverer_object->get_all();
		$this->layout_object->assign('deliverers', $deliverers);
		
		// decide what CRUD method to call and call it in ListAndEdit
		// create
		$shipping_area_object = new Shipping_area($this->base_object);
		if($is_submitted && !$has_id) {
			$form_data = $shipping_area_object->data_filter($_POST['form_field']);
			$shipping_area_object->create($form_data);
			$shipping_area_data = $shipping_area_object->get_data();
// MUST BE saved first
//			$this->load_payment_method_setings($shipping_area_data);
//			$this->save_payment_method_setings();
			$this->layout_object->assign('form_data', $shipping_area_data);
		}// read
		elseif($has_id && !$is_submitted && $_GET['mod'] != 'delete') {
			// template variable name
			$shipping_area_object->id = $_GET['id'];
			$shipping_area_data = $shipping_area_object->get_data();
			$this->load_payment_method_setings($shipping_area_data);
			$this->layout_object->assign('form_data', $shipping_area_data);
		} // update
		elseif($has_id && $is_submitted && $_GET['mod'] != 'delete') {
			$shipping_area_object->id = $_POST['id'];
			$form_data = $shipping_area_object->data_filter($_POST['form_field']);
			$shipping_area_object->update($form_data);
			$this->save_payment_method_setings($id);
			$shipping_area_data = $shipping_area_object->get_data();
			$this->load_payment_method_setings($shipping_area_data);
			
			$this->layout_object->assign('form_data', $shipping_area_data);
		} // delete
		elseif($has_id && isset($_GET['mod']) && $_GET['mod'] == 'delete'){
			$shipping_area_object->id = $_GET['id'];
			$shipping_area_object->delete();
		}
		else {

		}
		
		return $this->layout_object->fetch('admin_shipping_area_editor.tpl');
	}
	
	
	private function save_payment_method_setings($id) {
		if (isset($_POST['form_field'])) {
			$pm = $_POST['form_field'];
		}
		// Vorauskasse
		if (isset($pm['active_vk']) && $pm['active_vk'] == 2) {
			$prepayment = $_POST['prepayment'];
			$data = $this->make_save_array($prepayment);
			
			$pm_id = 1;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$prepayment_conf->save_array($data);
		}
		
		// Nachnahme
		if (isset($pm['active_nn']) && $pm['active_nn'] == 2) {
			$collect_on_delivery = $_POST['collect_on_delivery'];
			$data = $this->make_save_array($collect_on_delivery);
			
			$pm_id = 2;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$prepayment_conf->save_array($data);
		}
		
		// Paypal
		if (isset($pm['active_pp']) && $pm['active_pp'] == 2) {
			$paypal = $_POST['paypal'];
			$data = $this->make_save_array($paypal);
			
			$pm_id = 4;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$prepayment_conf->save_array($data);
		}
		
		// Rechnung
		if (isset($pm['active_r']) && $pm['active_r'] == 2) {
			$invoice = $_POST['invoice'];
			$data = $this->make_save_array($invoice);
			
			$pm_id = 3;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$prepayment_conf->save_array($data);
		}
		
		// Bankeinzug
		if (isset($pm['active_be']) && $pm['active_be'] == 2) {
			$direct_debit = $_POST['direct_debit'];
			$data = $this->make_save_array($direct_debit);
			
			$pm_id = 6;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$prepayment_conf->save_array($data);
		}
		
		// Barverkauf
		if (isset($pm['active_bar']) && $pm['active_bar'] == 2) {
			$cash = $_POST['cash'];
			$data = $this->make_save_array($cash);
		
			$pm_id = 7;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$prepayment_conf->save_array($data);
		}
		
                // Klarna Rechnung
		if (isset($pm['active_klarna_rechnung']) && $pm['active_klarna_rechnung'] == 2) {
			$klarna_re = $_POST['klarna_re'];
			$data = $this->make_save_array($klarna_re);
		
			$pm_id = 8;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$prepayment_conf->save_array($data);
		}
				
		// Klarna Raten
		if (isset($pm['active_klarna_raten']) && $pm['active_klarna_raten'] == 2) {
			$klarna_ra = $_POST['klarna_ra'];
			$data = $this->make_save_array($klarna_ra);
			
			$pm_id = 9;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$prepayment_conf->save_array($data);
		}
		
		// paymorrow Rechnung
		if (isset($pm['active_paymorrow_rechnung']) && $pm['active_paymorrow_rechnung'] == 2) {
			$paymorrow_re = $_POST['paymorrow_re'];
			$data = $this->make_save_array($paymorrow_re);
		
			$pm_id = 10;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$prepayment_conf->save_array($data);
		}
				
		// paymorrow Raten
		if (isset($pm['active_paymorrow_raten']) && $pm['active_paymorrow_raten'] == 2) {
			$paymorrow_ra = $_POST['paymorrow_ra'];
			$data = $this->make_save_array($paymorrow_ra);
			
			$pm_id = 11;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$prepayment_conf->save_array($data);
		}
		
		// billpay invoice
		if (isset($pm['active_billpay_invoice']) && $pm['active_billpay_invoice'] == 2) {
			$billpay_invoice = $_POST['billpay_invoice'];
			$data = $this->make_save_array($billpay_invoice);
		
			$pm_id = 12;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$prepayment_conf->save_array($data);
		}
		
		// computop creditcard
		if (isset($pm['active_computop_creditcard']) && $pm['active_computop_creditcard'] == 2) {
			$computop_creditcard = $_POST['computop_creditcard'];
			$data = $this->make_save_array($computop_creditcard);
		
			$pm_id = 13;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$prepayment_conf->save_array($data);
		}

                // customized
		if (isset($pm['active_customized']) && $pm['active_customized'] == 2) {
			$customized = $_POST['customized'];
			$data = $this->make_save_array($customized);
		
			$pm_id = 14;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$prepayment_conf->save_array($data);
		}
	}
	
	private function make_save_array($data) {
		foreach($this->customer_group_data as $customer_group) {
			if (isset($data[$customer_group->id])) {
				$tmp['group'] = $customer_group->id;
				$tmp['active'] = '1';
				
				$return[] = $tmp;
			} else {
				$tmp['group'] = $customer_group->id;
				$tmp['active'] = '0';
				
				$return[] = $tmp;
			}
		}
		
		return $return;
	}
	
	private function load_payment_method_setings($data) {
		$id = $data->id;

		// Vorauskasse
		if ($data->active_vk == '2') {
			$pm_id = 1;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$mydata = $prepayment_conf->get_all();
			
			$this->layout_object->assign('prepayment', $mydata);
		}
		
		// Nachnahme
		if ($data->active_nn == '2') {
			$pm_id = 2;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$mydata = $prepayment_conf->get_all();
			
			$this->layout_object->assign('collect_on_delivery', $mydata);
		}
		
		// Paypal
		if ($data->active_pp == '2') {
			$pm_id = 4;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$mydata = $prepayment_conf->get_all();
			
			$this->layout_object->assign('paypal', $mydata);
		}
		
		// Rechnung
		if ($data->active_r == '2') {
			$pm_id = 3;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$mydata = $prepayment_conf->get_all();
			
			$this->layout_object->assign('invoice', $mydata);
		}
		
		// Bankeinzug
		if ($data->active_be == '2') {
			$pm_id = 6;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$mydata = $prepayment_conf->get_all();
			
			$this->layout_object->assign('direct_debit', $mydata);
		}
		
		// Barverkauf
		if ($data->active_bar == '2') {
			$pm_id = 7;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$mydata = $prepayment_conf->get_all();
			
			$this->layout_object->assign('cash', $mydata);
		}
		
		// Klarna Rechnung
		if ($data->active_klarna_rechnung == '2') {
			$pm_id = 8;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$mydata = $prepayment_conf->get_all();
		
			$this->layout_object->assign('klarna_re', $mydata);
		}
		
		// Klarna Raten
		if ($data->active_klarna_raten == '2') {
			$pm_id = 9;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$mydata = $prepayment_conf->get_all();
		
			$this->layout_object->assign('klarna_ra', $mydata);
		}
		
		// paymorrow Rechnung
		if ($data->active_paymorrow_rechnung == '2') {
			$pm_id = 10;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$mydata = $prepayment_conf->get_all();
		
			$this->layout_object->assign('paymorrow_re', $mydata);
		}
		
			// paymorrow Raten
		if ($data->active_paymorrow_raten == '2') {
			$pm_id = 11;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$mydata = $prepayment_conf->get_all();
		
			$this->layout_object->assign('paymorrow_ra', $mydata);
		}
		
			// billpay invoice
		if ($data->active_billpay_invoice == '2') {
			$pm_id = 12;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$mydata = $prepayment_conf->get_all();
		
			$this->layout_object->assign('billpay_invoice', $mydata);
		}
		
                // computop creditcard
		if ($data->active_computop_creditcard == '2') {
			$pm_id = 13;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$mydata = $prepayment_conf->get_all();
		
			$this->layout_object->assign('computop_creditcard', $mydata);
		}
		
                // customized
		if ($data->active_customized == '2') {
			$pm_id = 14;
			$prepayment_conf = new CustomerGroupPMConf($pm_id, $id);
			$mydata = $prepayment_conf->get_all();
		
			$this->layout_object->assign('customized', $mydata);
		}
		
	}
}

?>