[
'class' => VerbFilter::class,
'actions' => [
],
],
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action) {
return $this->getUserManager()->hasAccessBackend();
}
],
],
],
];
}
/**
* Liste les points de vente.
*/
public function actionIndex()
{
$searchModel = new PointSaleSearch();
$dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Crée un point de vente.
*
* @return mixed
*/
public function actionCreate()
{
$pointSaleManager = $this->getPointSaleManager();
$distributionManager = $this->getDistributionManager();
$pointSale = $pointSaleManager->instanciatePointSale();
if ($pointSale->load(\Yii::$app->request->post()) && $pointSale->save()) {
$pointSaleManager->updatePointSalePointProduction($pointSale);
$pointSaleManager->processRestrictedAccess($pointSale);
$distributionManager->addPointSaleIncomingDistributions($pointSale);
return $this->redirect(['index']);
} else {
return $this->render('create', array_merge($this->initForm(), [
'model' => $pointSale,
]));
}
}
/**
* Modifie un point de vente.
*/
public function actionUpdate(int $id)
{
$distributionManager = $this->getDistributionManager();
$pointSaleManager = $this->getPointSaleManager();
$model = PointSale::find()
->with('userPointSale')
->where(['id' => $id])
->one();
foreach ($model->userPointSale as $userPointSale) {
$model->users[] = $userPointSale->id_user;
$model->users_comment[$userPointSale->id_user] = $userPointSale->comment;
}
if ($model->load(\Yii::$app->request->post()) && $model->save()) {
$pointSaleManager->updatePointSalePointProduction($model);
$pointSaleManager->processRestrictedAccess($model);
$distributionManager->addPointSaleIncomingDistributions($model);
$this->setFlash('success', 'Point de vente modifié.');
return $this->redirect(['index']);
} else {
return $this->render('update', array_merge($this->initForm($id), [
'model' => $model,
]));
}
}
/**
* Initialise le formulaire de création/modification.
*/
public function initForm(int $id = 0)
{
$userManager = $this->getUserManager();
$users = $userManager->queryUsersBy()
->leftJoin('user_point_sale', 'user_point_sale.id_user = user.id AND user_point_sale.id_point_sale = :id_point_sale', [':id_point_sale' => $id])
->orderBy('user_point_sale.id_point_sale DESC, lastname ASC, name ASC')
->all();
return [
'users' => $users
];
}
/**
* Supprime un point de vente et redirige vers la liste des points de vente.
*/
public function actionDelete(int $id, $confirm = false)
{
$orderManager = $this->getOrderManager();
$distributionManager = $this->getDistributionManager();
$pointSale = $this->findModel($id);
if ($confirm) {
$pointSale->status = -1;
$pointSale->save();
// Suppression du lien entre les utilisateurs et le point de vente
UserPointSale::deleteAll(['id_point_sale' => $id]);
// Suppression du lien PointSaleDistribution pour toutes les distributions à venir
$incomingDistributions = $distributionManager->findDistributionsIncoming();
foreach ($incomingDistributions as $distribution) {
PointSaleDistribution::deleteAll(['id_point_sale' => $id, 'id_distribution' => $distribution->id]);
}
// Suppression de toutes les commandes à venir de ce point de vente
$ordersArray = Order::searchAll(
[
'id_point_sale' => $id,
],
[
'conditions' => 'date_delete IS NULL AND distribution.date > :today',
'params' => [':today' => date('Y-m-d')]
]
);
if ($ordersArray) {
foreach ($ordersArray as $order) {
$orderManager->deleteOrder($order, true);
}
}
$this->setFlash('success', 'Point de vente ' . Html::encode($pointSale->name) . ' supprimé.');
} else {
$this->setFlash('info', 'Souhaitez-vous vraiment supprimer le point de vente ' . Html::encode($pointSale->name) . ' ? '
. Html::a('Oui', ['point-sale/delete', 'id' => $id, 'confirm' => 1], ['class' => 'btn btn-default']) . ' ' . Html::a('Non', ['point-sale/index'], ['class' => 'btn btn-default']));
}
return $this->redirect(['index']);
}
/**
* Définit un point de vente par défaut.
*/
public function actionDefault(int $id)
{
$pointSaleManager = $this->getPointSaleManager();
$pointSale = $this->findModel($id);
if ($pointSale) {
PointSale::updateAll(['default' => 0], 'id_producer = :id_producer', [':id_producer' => GlobalParam::getCurrentProducerId()]);
if (!$pointSale->default) {
$pointSale->default = 1;
$pointSaleManager->saveUpdate($pointSale);
$this->setFlash('success', 'Point de vente ' . Html::encode($pointSale->name) . ' défini par défaut.');
} else {
$this->setFlash('success', 'Aucun point de vente défini par défaut');
}
}
return $this->redirect(['index']);
}
/**
* Recherche un point de vente en fonction de son ID.
*/
protected function findModel(int $id)
{
if (($model = PointSale::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}