[
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action) {
return $this->getUserModule()
->getAuthorizationChecker()
->isGrantedAsProducer($this->getUserCurrent());
}
],
],
],
];
}
/**
* 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()
{
$pointSaleModule = $this->getPointSaleModule();
$distributionModule = $this-> getDistributionModule();
$pointSale = $pointSaleModule->instanciatePointSale();
if ($pointSale->load(\Yii::$app->request->post()) && $pointSale->save()) {
$pointSaleModule->updatePointSalePointProduction($pointSale);
$pointSaleModule->processRestrictedAccess($pointSale);
$this->initPaymentMethodsByCreditFunctioning($pointSale);
$distributionModule->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)
{
$distributionModule = $this-> getDistributionModule();
$pointSaleModule = $this->getPointSaleModule();
$model = $this->findModel($id);
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()) {
$pointSaleModule->updatePointSalePointProduction($model);
$pointSaleModule->processRestrictedAccess($model);
$this->initPaymentMethodsByCreditFunctioning($model);
$distributionModule->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,
]));
}
}
public function initPaymentMethodsByCreditFunctioning(PointSale $pointSale)
{
if($this->getPointSaleModule()->getBuilder()->initPaymentMethodsByCreditFunctioning($pointSale)) {
$this->addFlash('info', 'Le paiement sur place a été ajusté par rapport au fonctionnement de la cagnotte.');
}
}
/**
* Initialise le formulaire de création/modification.
*/
public function initForm(int $id = 0)
{
$userModule = $this->getUserModule();
$users = $userModule->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)
{
$orderModule = $this->getOrderModule();
$distributionModule = $this-> getDistributionModule();
$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 = $distributionModule->findDistributionsIncoming(true);
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) {
$orderModule->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)
{
$pointSaleModule = $this->getPointSaleModule();
$pointSale = $this->findModel($id);
if ($pointSale) {
PointSale::updateAll(['default' => 0], 'id_producer = :id_producer', [':id_producer' => GlobalParam::getCurrentProducerId()]);
if (!$pointSale->default) {
$pointSale->default = 1;
$pointSaleModule->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)
{
$model = PointSale::find()
->with('userPointSale')
->where(['id' => $id])
->one();
if(!$model || $model->id_producer != $this->getProducerCurrent()->id) {
throw new NotFoundHttpException('Le point de vente demandé est introuvable.');
}
return $model;
}
}