Your IP : 216.73.216.91


Current Path : /var/www/html/tax/api/controller/
Upload File :
Current File : /var/www/html/tax/api/controller/business.php

<?php 
require_once('../api/autoload.php');
class business extends pdocrudhandler{
	private $startUpConfig;
	private $baseUrl;
	public function __construct(){
		$this->_pdo = $this->connect();
		if (session_status() == PHP_SESSION_NONE) {
		    session_start();
		}
	}

	public function getBusinessType($data){
		$companyId = (isset($_SESSION['companyid'])) ? $_SESSION['companyid'] : $data['bs_company_id'];
		$userId = (isset($_SESSION['userid'])) ? $_SESSION['userid'] : $data['bs_user_id'];
		$response = $this->select($this->getTable('businesstype'),["*"], "WHERE idCompany = ? and isActive = ?", [$companyId, 1]);
		return $response;
	}

	public function getBusiness($data){
		$companyId = (isset($_SESSION['companyid'])) ? $_SESSION['companyid'] : $data['bs_company_id'];
		$userId = (isset($_SESSION['userid'])) ? $_SESSION['userid'] : $data['bs_user_id'];

		$qry = "SELECT 
			bs.idBusiness, 
			bs.name, 
			bs.label, 
			bs.address, 
			bst.type as businessType,
			bst.idBusinessType as idBusinessType,
			CASE
			    WHEN bsn.name is null THEN 'N/A'
				ELSE bsn.name
			END AS sponsorBusinessName,
			CASE
			    WHEN bs.sponsorIdBusiness is null THEN -1
				ELSE bs.sponsorIdBusiness
			END AS sponsorIdBusiness
			FROM billing.business bs
			inner join businesstype bst on bst.idBusinessType = bs.idBusinessType
			LEFT join business bsn on bsn.idBusiness = bs.sponsorIdBusiness
			WHERE bs.idCompany = ? and bs.isActive = ?";
		$response = $this->customSelect($qry, [$companyId, 1]);
		return $response;
	}

	public function deleteBusiness($data){
		$response = []; 
		$companyId = (isset($_SESSION['companyid'])) ? $_SESSION['companyid'] : $data['bs_company_id'];
		$userId = (isset($_SESSION['userid'])) ? $_SESSION['userid'] : $data['bs_user_id'];
		$helper = new helper();
		$collectionsToBeChecked = [
			$this->getTable('bill'),
			$this->getTable('payment')
		];
		$fieldToBeChecked = ["idBusiness" => $data['bs_id']];
		$helperRes = $helper->checkTransactions($collectionsToBeChecked, $fieldToBeChecked);
		if($helperRes){
			$response = $this->delete($this->getTable('business'), "WHERE idBusiness = ? and idCompany = ?", [$data['bs_id'], $companyId]);
		}else{
			$response = [
				"status" => "failed",
				"msg" => "Business can't be deleted, they either have billing or payment history"
			];
		}
		return $response;
	}

	public function createBusiness($data){
		$business = [];
		$response = []; 
		$companyId = (isset($_SESSION['companyid'])) ? $_SESSION['companyid'] : $data['bs_company_id'];
		$userId = (isset($_SESSION['userid'])) ? $_SESSION['userid'] : $data['bs_user_id'];
		$res = $this->select($this->getTable('business'), ["*"], "WHERE name = ? and idCompany = ? and isActive = ?", [$data['bs_name'], $companyId, 1]);
		if($res['rowsAffected'] == 1){
			$response = [
				"status" => "info",
				"msg" => "Business with the same name already exists, please use different name",
			];
		}else{
			$business = [
				"name" => $data['bs_name'], 
				"label" => $data['bs_label'], 
				"address" => $data['bs_address'], 
				"idBusinesstype" => $data['bs_type'], 
				"idCompany" => $companyId,
				"idUser" => $userId,
			];
			if(strtolower($data['bs_type']) == 1){
				$additionalFields = [
					"sponsorIdBusiness" => $data['bs_sponsor'],
				];
				$business = array_merge($business, $additionalFields);
			}
			$response = $this->insert($this->getTable('business'), $business);
		}
		return $response;
	}

	public function updateBusiness($data){
		$business = [];
		$response = []; 
		$companyId = (isset($_SESSION['companyid'])) ? $_SESSION['companyid'] : $data['bs_company_id'];
		$userId = (isset($_SESSION['userid'])) ? $_SESSION['userid'] : $data['bs_user_id'];
		$res = $this->select($this->getTable('business'), ["*"], "WHERE name = ? and idCompany = ? and isActive = ?", [$data['bs_name'], $companyId, 1]);
		if($res['rowsAffected'] == 0){
			$additionalFields = ["name" => $data['bs_name']];
			$business = array_merge($business, $additionalFields);
		}
		// echo "<pre>"; print_r($data); echo $data['bs_type'];exit;
		if($data['bs_type'] == 1){
			$additionalFields = ["sponsorIdBusiness" => $data['bs_sponsor']];
			$business = array_merge($business, $additionalFields);
		}
		$additionalFields = [
			"label" => $data['bs_label'], 
			"address" => $data['bs_address'], 
			"idBusinesstype" => $data['bs_type'], 
			"idCompany" => $companyId,
			"idUser" => $userId,
		];
		$business = array_merge($business, $additionalFields);
		$response = $this->update($this->getTable('business'), $business, "where idBusiness = ?", [$data['bs_id']]);
		return $response;
	}

	public function validateRequestsParams(){
		$this->mendetoryParamsAgainstEachMethod = [
			'getBusiness' => [],
			'deleteBusiness' => ["bs_id"],
			'getBusinessType' => [],
			'createBusiness' => [
				"bs_name", 
				"bs_label", 
				"bs_address", 
				"bs_type",
			],
			'updateBusiness' => [
				"bs_name", 
				"bs_label", 
				"bs_address", 
				"bs_type",
				"bs_id"
			],
		];
		return $this->mendetoryParamsAgainstEachMethod;
	}
}
?>