2024-04-24 18:02:58 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace common\services;
|
|
|
|
|
|
|
|
use common\models\Company;
|
2024-05-24 15:27:07 +03:00
|
|
|
use common\models\ProductCategory;
|
2024-04-24 18:02:58 +03:00
|
|
|
use Yii;
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
|
|
|
class CompanyService
|
|
|
|
{
|
|
|
|
public Company $model;
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
$this->model = new Company();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCompaniesByUser(int $id = null): array
|
|
|
|
{
|
|
|
|
return $this->model->find()->where(['user_id' => $id ?? Yii::$app->user->id])->all();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCompaniesByUserArr(int $id = null): array
|
|
|
|
{
|
|
|
|
return ArrayHelper::map($this->getCompaniesByUser($id), 'id', 'name');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
* @return Company|null
|
|
|
|
*/
|
|
|
|
public function getCompany(int $id): ?Company
|
|
|
|
{
|
|
|
|
return $this->model->findOne($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAddressesByUser(int $id = null): array
|
|
|
|
{
|
|
|
|
$companies = $this->getCompaniesByUser($id);
|
|
|
|
return $this->model->find()->where(['id' => ArrayHelper::getColumn($companies, 'id')])->all();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAddressesByUserArr(int $id = null): array
|
|
|
|
{
|
|
|
|
return ArrayHelper::map($this->getAddressesByUser($id), 'id', 'address');
|
|
|
|
}
|
|
|
|
|
2024-05-24 15:27:07 +03:00
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCategoryByCompanyId(int $id): array
|
|
|
|
{
|
|
|
|
return ProductCategory::find()->where(['company_id' => $id])->all();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-24 18:02:58 +03:00
|
|
|
}
|