<?php

namespace Modules\Orders\Cancel;

class Model extends \App\Mvc\ModelAbstract
{

	/**
	 * Returns an object wich will manage the cancelling of services on orders.
	 * @param Int $idEntity
	 * @return \Entity\Service
	 */
 	public function getServiceEntity($idEntity, $input)
 	{

		$servObj=\Entity\Service::cancel($idEntity);
		$servObj->cancelReason=$input['serviceCancelReason'];
		$servObj->cancelReasonDetails=$input['serviceCancelDetails'];
		return $servObj;

 	}


 	/**
 	 * Returns the list of not-issued invoices associated to this service.
 	 * @param Int $idService
 	 * @return Array
 	 */
 	public function getNotIssuedServiceInvoices($idService)
 	{

 		$invRowsList=\Entity\Service::select($idService)->invoiceRowsList;
		$return=[];
 		foreach($invRowsList as $row) {
 			$inv=\Entity\Invoice::select($row['idInvoice']);
 			if (!$inv->issueTime and !$return[$row['idInvoice']]) {
				$return[$row['idInvoice']]=['id'=>$row['idInvoice'], 'invoiceNumber'=>$inv->invoiceNumber, 'invoiceDate'=>$inv->invoiceDate];
 			}
 		}

 		return $return;

 	}

 	/**
 	 * Returns the list of not-issued invoices associated to this order.
 	 * @param Int $idOrder
 	 * @return Array
 	 */
 	public function getNotIssuedOrderInvoices($idOrder)
 	{

 		// get the list of services
 		$servList=\Entity\Order::select($idOrder)->servicesList;

 		$return=[];

 		foreach($servList as $idServ=>$servDet) {
 			if ($servDet['statusOp']!='XX') {
 				$return += $this->getNotIssuedServiceInvoices($idServ);
 			}
 		}

 		return $return;

 	}


}
