[
'class' => VerbFilter::className(),
'actions' => [
],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action) {
return User::hasAccessBackend();
}
],
],
],
];
}
/**
* Liste les points de vente.
*
* @return mixed
*/
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()
{
$model = new PointSale();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->processPointProduction();
$model->processRestrictedAccess();
Distribution::linkPointSaleIncomingDistributions($model) ;
return $this->redirect(['index']);
} else {
return $this->render('create', array_merge($this->initForm(), [
'model' => $model,
]));
}
}
/**
* Modifie un point de vente.
*
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$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()) {
$model->processPointProduction();
$model->processRestrictedAccess();
Distribution::linkPointSaleIncomingDistributions($model) ;
Yii::$app->getSession()->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.
*
* @param integer $id
* @return mixed
*/
public function initForm($id = 0)
{
$users = User::findBy()
->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.
*
* @param integer $id
* @return mixed
*/
public function actionDelete($id, $confirm = false)
{
$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 = Distribution::getIncomingDistributions();
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) {
$order->delete(true);
}
}
Yii::$app->getSession()->setFlash('success', 'Point de vente '.Html::encode($pointSale->name).' supprimé.');
}
else {
Yii::$app->getSession()->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.
*
* @param integer $id
*/
public function actionDefault($id)
{
$pointSale = $this->findModel($id) ;
if($pointSale) {
PointSale::updateAll(['default' => 0], 'id_producer = :id_producer', [':id_producer' => GlobalParam::getCurrentProducerId()]) ;
if(!$pointSale->default) {
$pointSale->default = 1 ;
$pointSale->save() ;
Yii::$app->getSession()->setFlash('success', 'Point de vente '.Html::encode($pointSale->name).' défini par défaut.') ;
}
else {
Yii::$app->getSession()->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.
*
* @param integer $id
* @return PointVente
* @throws NotFoundHttpException si le modèle n'est pas trouvé
*/
protected function findModel($id)
{
if (($model = PointSale::findOne($id)) !== null) {
return $model;
}
else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}