<?php

/**
 * 
 * @author Peter Csubak (chuby)
 * @copyright Peter Csubak (chuby)
 * 
 * //Annotations:
 * @Stateful
 */
class TransporterSF {
	
	
	/**
	 * 
	 * @return array
	 * @XmlMethod(schemaName="mobile", methodName="gosl")
	 * @XmlArray(schemaName="mobile", elementName="e", valueType="Order")
	 * @GrantTo("transporter")
	 */
	public function getOrdersList(){
		$where = new Where("Status", "=", "before");
		return PersistenceHandler::getInstance()->selectEntities("Order", $where);
	}
	
	/**
	 * 
	 * @XmlMethod(schemaName="mobile", methodName="lgn", paramsName={"un" = "userName", "pc" = "passCode"})
	 */
	public function login($userName, $passCode){
		$where = new Where("UserName", "=", $userName);
		$where->_and("PassCode", "=", $passCode);
		$res = PersistenceHandler::getInstance()->selectEntities("Transporter", $where);
		
		if (is_array($res) && (count($res) > 0)){
			SecureSession::getInstance()->setLoggedInUser($res[0]);
		}else{
			throw new Exception("Hibás felhasználói név, vagy jelszó.");
		}
	}
	
	
	/**
	 * 
	 * @XmlMethod(schemaName="mobile", methodName="rd", paramsName={"ori" = "orderid"})
	 * @GrantTo("transporter")
	 */
	public function registerDelivering($orderid){
		$nOrder = new Order();
		$nOrder->autoLoad($orderid);
		$nOrder->status = "in";
		$nOrder->save();
		$custid = $nOrder->customerId;
		$where = new Where("ID", "=", $custid);
		$arr = PersistenceHandler::getInstance()->selectEntities("Customer", $where);
		return $arr[0];
	}
	
	/**
	 * 
	 * @XmlMethod(schemaName="mobile", methodName="rdd", paramsName={"ori" = "orderid"})
	 * @XmlArray(schemaName="mobile", elementName="e", valueType="Order")
	 * @GrantTo("transporter")
	 */
	public function registerDeliverDone($orderid){
		$nOrder = new Order();
		$nOrder->autoLoad($orderid);
		$nOrder->status = "after";
		$nOrder->save();
		$where = new Where("CustomerId", "=", $nOrder->customerId);
		$where->_and("Status", "=", "after")->_or("Status", "=", "need");
		$where->_and("ID", "<>", $nOrder->ID);
		return PersistenceHandler::getInstance()->selectEntities("Order", $where);
	}
	
	/**
	 * 
	 * @XmlMethod(schemaName="mobile", methodName="gnl")
	 * @XmlArray(schemaName="mobile", elementName="e", valueType="Order")
	 * @GrantTo("transporter")
	 */
	public function getNeededList(){
		$where = new Where("Status", "=", "need");
		return PersistenceHandler::getInstance()->selectEntities("Order", $where);
	}
	
	/**
	 * 
	 * @XmlMethod(schemaName="mobile", methodName="rro", paramsName={"ori" = "orderid"})
	 * @GrantTo("transporter")
	 */
	public function registerRemovableOrder($orderid){
		$nOrder = new Order();
		$nOrder->autoLoad($orderid);
		foreach ($nOrder->orderedDvds as $value){
			$value->orderId = null;
			$value->save();
		}
		$nOrder->remove();
	}
	
	
}
?>