<?php
namespace frontend\components;

use Yii;
use common\models\Order;
use yii\helpers\Html;
use common\models\MoneyMovement;
use yii\db\Query;
use common\models\AccountState;
use yii\web\Controller;
use yii\base\Object;
use frontend\models\ReceptionForm;
use common\models\UserSoldItem;
use common\models\ShoppingCart;
use common\models\Transfer;
use common\models\User;

class FrontendController extends  Controller{
	
	 
	
	protected function mkReceptionForm($number){
		$receptionForm = new ReceptionForm();
		$receptionForm->number = $number;
		$receptionForm->readCard();
		return $receptionForm;
	}
	
	
	protected function payoutCustomerCart($receptionForm){
		$result = false;
		if ( isset($receptionForm->customer)){
			$connection = \Yii::$app->db;
			$transaction = $connection->beginTransaction();
			try {
				ShoppingCart::payout($receptionForm->customer);
				$transaction->commit();
				Yii::$app->session->setFlash('success', Yii::t('frontend/ticket', 'Customer cart paid!') );
				$result = true;
			} catch(Exception $e) {
				$transaction->rollback();
			}
		}
		return $result;
	}
	
	protected function payoutUserCart($receptionForm){
		$connection = \Yii::$app->db;
		$transaction = $connection->beginTransaction();
		try {
			$user = User::findOne(Yii::$app->user->id );
			UserSoldItem::payout($user);
			$transaction->commit();
			Yii::$app->session->setFlash('success', Yii::t('frontend/ticket', 'User cart paid!') );
			return $this->redirect(['customer/reception'   ]);
		} catch(Exception $e) {
			$transaction->rollback();
		}
		return true;
	}
	
}