<?php
namespace ModulesAbstract\PluginReports\Report\Model\Gatherer\ExportData;

/**
 * Helper class for exported invoice xml files
 * @author cristiana.matache
 */
class Invoice extends AbstractExportDataFile
{

	/**
	 * get the type of invoice partner it is
	 * @return string
	 */
	public function getPartnerType()
	{
		if ($this->client['type'] == 'individual') {
			return 'customer';
		}
		elseif ($this->client['type'] == 'legalEntity') {
			return 'corporate';
		}
		else {
			throw new \Exception("Unknown client type: {$this->client['type']}");
		}
	}

	/**
	 * get the details about the partner
	 * @return string[]
	 */
	public function getPartnerDetails()
	{
		$partnerType = $this->getPartnerType();

		if ($partnerType == 'customer') {

			return [
				'id'	=>				(String)$this->client['id'],
				'type'	=>				$partnerType,
				'firstName'	=>			(String)$this->client->individualDetails->firstName,
				'lastName'	=>			(String)$this->client->individualDetails->lastName,
				'PNC'	=>				(String)$this->client->individualDetails->PNC,
				'address'	=>			(String)$this->client->individualDetails->address,
				'zipCode'	=>			(String)$this->client->individualDetails->zipCode,
				'country'	=>			(String)$this->client->individualDetails->country,
				'countryCode'	=>		(String)$this->client->individualDetails->country['code'],
				'city'	=>				(String)$this->client->individualDetails->city,
				'cityId'	=>			(String)$this->client->individualDetails->city['id'],
				'email'	=>				(String)$this->client->individualDetails->email,
				'bankName'	=>			(String)$this->client->individualDetails->bankName,
				'idBank'	=>			(String)$this->client->individualDetails->bankName['id'],
				'bankOfficeName'	=>	(String)$this->client->individualDetails->bankOfficeName,
				'idBankOffice'	=>		(String)$this->client->individualDetails->bankOfficeName['id'],
				'bankAccount'	=>		(String)$this->client->individualDetails->bankAccount,

			];

		}
		elseif ($partnerType == 'corporate') {

			$mainOffice = "";

			foreach ($this->client->corporateDetails->offices as $offices) {
				if ((String)$offices->office['type'] == "central") {
					$mainOffice = $offices->office;
					break;
				}
			}

			return [
				'id'	=>				(String)$this->client['id'],
				'type'	=>				$partnerType,
				'alias'	=>					(String)$this->client->corporateDetails->alias,
				'company'	=>				(String)$this->client->corporateDetails->company,
				'companyPrefix'	=>			(String)$this->client->corporateDetails->company['prefix'],
				'companySuffix'	=>			(String)$this->client->corporateDetails->company['suffix'],
				'fiscalCode'	=>			(String)$this->client->corporateDetails->fiscalCode,
				'email'	=>					(String)$this->client->corporateDetails->email,
				'mainOffice'	=>
					[
						'id'	=>			(String)$mainOffice['id'],
						'name'	=>			(String)$mainOffice->name,
						'country'	=>		(String)$mainOffice->country,
						'countryCode'	=>	(String)$mainOffice->country['code'],
						'city'	=>			(String)$mainOffice->city,
						'cityId'	=>		(String)$mainOffice->city['id'],
					]
			];

		}
		else {
			throw new \Exception('Unknown partner type : '.$partnerType);
		}
	}



	/**
	 * Return  array with all rows from an invoice
	 */
	public function getInvoiceRows()
	{
		$rows = [];

		if (isset($this->invoiceRows->row)) {
			foreach ($this->invoiceRows->row as $row) {
				$rows[(String)$row['id']] = $row;
			}
		}

		return $rows;
	}

	public function getDate()
	{
		return (String)$this->invoiceDate;
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \ModulesAbstract\PluginReports\Report\Model\Gatherer\ExportData\AbstractExportDataFile::isVoid()
	 */
	public function isVoid()
	{
		return ((String)$this['operation'] == 'void');
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \ModulesAbstract\PluginReports\Report\Model\Gatherer\ExportData\AbstractExportDataFile::getVoidDate()
	 */
	public function getVoidDate()
	{
		return \Engine\DateTime::create((String)$this->voidTime)->formatDateIso();
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \ModulesAbstract\PluginReports\Report\Model\Gatherer\ExportData\AbstractExportDataFile::isConfirmed()
	 */
	public function isIssue()
	{
		return ((String)$this['operation'] == 'issue');

	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \ModulesAbstract\PluginReports\Report\Model\Gatherer\ExportData\AbstractExportDataFile::getIssueDate()
	 */
	public function getIssueDate()
	{
		return $this->getDate();
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \ModulesAbstract\PluginReports\Report\Model\Gatherer\ExportData\AbstractExportDataFile::isCoreDocument()
	 */
	public function isCoreDocument()
	{
		return true;
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \ModulesAbstract\PluginReports\Report\Model\Gatherer\ExportData\AbstractExportDataFile::isCashbookDocument()
	 */
	public function isCashbookDocument()
	{
		return false;
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \ModulesAbstract\PluginReports\Report\Model\Gatherer\ExportData\AbstractExportDataFile::isImportedDocument()
	 */
	public function isImportedDocument()
	{
		return false;
	}

}
