<?php
namespace ModulesAbstract\PluginReports\Report\Model\Builder\Helpers;

class FakeInvoice
{

	/**
	 * Stores the singleton instance for the current object.
	 * @var \ModulesAbstract\PluginReports\Report\Model\Builder\Helpers\FakeInvoice
	 */
	static private $instance;

	/**
	 * Returns the singleton instatiation of the class.
	 * @return \ModulesAbstract\PluginReports\Report\Model\Builder\Helpers\FakeInvoice
	 */
	static public function getInstance()
	{
		if (!self::$instance) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Create fake invoice from service and populate some fields of the invoice
	 * @return \Entity\Invoice
	 */
	public function createInvoiceEntityFromExistingService(\Entity\Service $serviceEntity, $reportAllComponents = true)
	{
		$invoiceEntity = \Entity\Invoice::select(0);
		$offerEntity = 	\Entity\Offer::select($serviceEntity->chosenOfferId);

		$invoiceEntity->setField('idCorporate', $serviceEntity->order->corporateOwner->getId());
		$invoiceEntity->setField('currency', $offerEntity->totalCurrency);
		$invoiceEntity->setField('invoiceDate', $serviceEntity->addDate->formatDateIso());
		$invoiceEntity->setField('invoiceNumber', $serviceEntity->getId());
		$invoiceEntity->setField('idUser', $serviceEntity->order->idUser);
		$invoiceEntity->setField('formOfPayment', 'cc');
		$invoiceEntity->setField('voidTime', 0);

		$invoicedPricesGroupedByServiceAndRefund = [];
		$components = [
			'price' => [
				'pricePrintValue' => 	$offerEntity->supplierWorkValue,
				'vatPrintValue' => 		$offerEntity->supplierWorkVat,
				'total' => 				$offerEntity->supplierWorkValue + $offerEntity->supplierWorkVat,
			],
			'tax' => [
				'pricePrintValue' => 	$offerEntity->taxWorkValue,
				'vatPrintValue' => 		$offerEntity->taxWorkVat,
				'total' => 				$offerEntity->taxWorkValue + $offerEntity->taxWorkVat,
				
			],
			'serviceFee' => [
				'pricePrintValue' => 	$offerEntity->serviceFeeWorkValue,
				'vatPrintValue' => 		$offerEntity->serviceFeeWorkVat,
				'total' => 				$offerEntity->serviceFeeWorkValue + $offerEntity->serviceFeeWorkVat,
				
			],
			'commission' => [
				'pricePrintValue' => 	$offerEntity->commissionWorkValue,
				'vatPrintValue' => 		$offerEntity->commissionWorkVat,
				'total' => 				$offerEntity->commissionWorkValue + $offerEntity->commissionWorkVat,
				
			],
			'discount' => [
				'pricePrintValue' => 	(-1) * $offerEntity->discountWorkValue,
				'vatPrintValue' => 		(-1) * $offerEntity->discountWorkVat,
				'total' => 				(-1) * ($offerEntity->discountWorkValue + $offerEntity->discountWorkVat),
			],
		];
		
		if (!$reportAllComponents) {
			$components = array_filter($components, function ($c) { return ($c['pricePrintValue'] or $c['vatPrintValue'] or $c['total']); });
		}

		$invoicedPricesGroupedByServiceAndRefund['byServiceId'][$serviceEntity->getId()] = [
			'sale' => [
				'isRefund' => 	false,
				'isStorno' => 	false,
				'isSale' =>		true,
				'total' =>		$offerEntity->offerWorkTotal,
				'supplier' => 	$offerEntity->supplierWorkValue + $offerEntity->supplierWorkVat,
				'commission' =>	$offerEntity->commissionWorkValue + $offerEntity->commissionWorkVat,
				'components' => $components,
			]
		];

		$invoiceEntity->setField('invoicedPricesGroupedByServiceAndRefund', $invoicedPricesGroupedByServiceAndRefund);

		return $invoiceEntity;
	}

}