namespace backend\controllers; | namespace backend\controllers; | ||||
use backend\controllers\BackendController; | use backend\controllers\BackendController; | ||||
use backend\forms\SharedPointSaleForm; | |||||
use backend\models\AccessUserProducerForm; | use backend\models\AccessUserProducerForm; | ||||
use domain\Feature\Feature\Feature; | use domain\Feature\Feature\Feature; | ||||
use domain\User\User\UserSearch; | use domain\User\User\UserSearch; | ||||
use yii\filters\AccessControl; | use yii\filters\AccessControl; | ||||
use yii\web\NotFoundHttpException; | |||||
/** | /** | ||||
* UserController implements the CRUD actions for User model. | * UserController implements the CRUD actions for User model. | ||||
*/ | */ | ||||
class SharedPointSaleController extends BackendController | class SharedPointSaleController extends BackendController | ||||
{ | { | ||||
public function behaviors() | public function behaviors() | ||||
{ | { | ||||
return [ | return [ | ||||
'allow' => true, | 'allow' => true, | ||||
'roles' => ['@'], | 'roles' => ['@'], | ||||
'matchCallback' => function ($rule, $action) { | 'matchCallback' => function ($rule, $action) { | ||||
$authorizationChecker = $this->getUserModule()->getAuthorizationChecker(); | $authorizationChecker = $this->getUserModule()->getAuthorizationChecker(); | ||||
$featureChecker = $this->getFeatureModule()->getChecker(); | $featureChecker = $this->getFeatureModule()->getChecker(); | ||||
public function actionIndex() | public function actionIndex() | ||||
{ | { | ||||
return $this->render('index'); | |||||
$sharedPointSaleRepository = $this->getSharedPointSaleModule()->getRepository(); | |||||
return $this->render('index', [ | |||||
'sharedPointsSaleRequestOfMe' => $sharedPointSaleRepository->findSharedPointsSaleRequestsOfMe(), | |||||
'sharedPointsSaleRequestOthers' => $sharedPointSaleRepository->findSharedPointsSaleRequestsOthers() | |||||
]); | |||||
} | } | ||||
public function actionCreate() | public function actionCreate() | ||||
{ | { | ||||
$sharedPointSale = $this->getSharedPointSaleModule()->getBuilder()->instanciateSharedPointSale(); | |||||
$sharedPointSaleModule = $this->getSharedPointSaleModule(); | |||||
$sharedPointSaleModel = $sharedPointSaleModule->getBuilder()->instanciateSharedPointSale(null, null, $this->getUserCurrent()); | |||||
if($sharedPointSale->load(\Yii::$app->request->post()) && $sharedPointSale->save()) { | |||||
if($sharedPointSaleModel->load(\Yii::$app->request->post()) && $sharedPointSaleModel->validate()) { | |||||
$sharedPointSaleModule->getManager()->createSharedPointSale( | |||||
$sharedPointSaleModel->getPointSale(), | |||||
$sharedPointSaleModel->getProducerWithSharing(), | |||||
$this->getUserCurrent() | |||||
); | |||||
$this->setFlash('success', "La demande de partage a bien été créée"); | $this->setFlash('success', "La demande de partage a bien été créée"); | ||||
return $this->redirect(['index']); | return $this->redirect(['index']); | ||||
} | } | ||||
return $this->render('create', [ | return $this->render('create', [ | ||||
'sharedPointSale' => $sharedPointSale | |||||
'sharedPointSaleModel' => $sharedPointSaleModel | |||||
]); | ]); | ||||
} | } | ||||
public function actionDecline(int $id) | |||||
{ | |||||
$sharedPointSale = $this->findSharedPointSale($id); | |||||
if($this->getSharedPointSaleModule()->getManager()->declineSharedPointSale($sharedPointSale, $this->getUserCurrent())) { | |||||
$this->addFlash('success', "La demande de partage de point de vente a bien été annulée."); | |||||
} | |||||
else { | |||||
$this->addFlash('error', "Une erreur est survenue lors de l'annulation de la demande de partage."); | |||||
} | |||||
return $this->redirect(['index']); | |||||
} | |||||
public function findSharedPointSale(int $id) | |||||
{ | |||||
$sharedPointSale = $this->getSharedPointSaleModule()->getRepository()->findOneSharedPointSaleById($id); | |||||
if(!$sharedPointSale) { | |||||
throw new NotFoundHttpException("La demande de point de vente partagé n'a pas été trouvée."); | |||||
} | |||||
return $sharedPointSale; | |||||
} | |||||
} | } |
$pointSaleModule = $this->getPointSaleModule(); | $pointSaleModule = $this->getPointSaleModule(); | ||||
$producerModule = $this->getProducerModule(); | $producerModule = $this->getProducerModule(); | ||||
$this->setTitle('Ajouter un point de vente') ; | |||||
$this->addBreadcrumb(['label' => 'Points de vente', 'url' => ['index']]) ; | |||||
$this->addBreadcrumb('Ajouter') ; | |||||
$this->setTitle('Partager un point de vente') ; | |||||
$this->addBreadcrumb(['label' => 'Points de vente partagés', 'url' => ['index']]) ; | |||||
$this->addBreadcrumb('Partager') ; | |||||
?> | ?> | ||||
<div class="shared-point-sale-create"> | <div class="shared-point-sale-create"> | ||||
<?php $form = ActiveForm::begin([ | <?php $form = ActiveForm::begin([ | ||||
'enableClientValidation' => false | 'enableClientValidation' => false | ||||
]); ?> | ]); ?> | ||||
<div class="panel panel-default"> | <div class="panel panel-default"> | ||||
<div class="panel-heading"> | |||||
<h3 class="panel-title"> | |||||
<i class="fa fa-th-list"></i> | |||||
Général | |||||
</h3> | |||||
</div> | |||||
<div class="panel-body"> | <div class="panel-body"> | ||||
<?= $form->field($sharedPointSale, 'id_point_sale') | |||||
<?= $form->field($sharedPointSaleModel, 'id_point_sale') | |||||
->dropDownList($pointSaleModule->getRepository()->populatePointSaleDropdownList()); ?> | ->dropDownList($pointSaleModule->getRepository()->populatePointSaleDropdownList()); ?> | ||||
<?php $producerPopulateDropdownArray = $producerModule->getRepository()->populateProducerDropdown(); ?> | <?php $producerPopulateDropdownArray = $producerModule->getRepository()->populateProducerDropdown(); ?> | ||||
<?= $form->field($sharedPointSale, 'id_producer_with_sharing') | |||||
<?= $form->field($sharedPointSaleModel, 'id_producer_with_sharing') | |||||
->dropDownList($producerPopulateDropdownArray['data'], ['prompt' => '--','encode' => false,'options' => $producerPopulateDropdownArray['options']]);?> | ->dropDownList($producerPopulateDropdownArray['data'], ['prompt' => '--','encode' => false,'options' => $producerPopulateDropdownArray['options']]);?> | ||||
</div> | </div> | ||||
</div> | </div> | ||||
<div class="form-group form-actions"> | <div class="form-group form-actions"> | ||||
<?= Html::a('Retour', ['index'], ['class' => 'btn btn-default']) ?> | <?= Html::a('Retour', ['index'], ['class' => 'btn btn-default']) ?> | ||||
<?= Html::submitButton($sharedPointSale->isNewRecord ? 'Créer' : 'Modifier', ['class' => 'btn btn-primary', 'name' => 'save']) ?> | |||||
<?= Html::submitButton($sharedPointSaleModel->isNewRecord ? 'Créer' : 'Modifier', ['class' => 'btn btn-primary', 'name' => 'save']) ?> | |||||
</div> | </div> | ||||
<?php ActiveForm::end(); ?> | <?php ActiveForm::end(); ?> | ||||
</div> | </div> |
'class' => 'btn btn-primary' | 'class' => 'btn btn-primary' | ||||
] | ] | ||||
); | ); | ||||
?> | ?> | ||||
<div class="shared-point-sale-index"> | <div class="shared-point-sale-index"> | ||||
<?php if(count($sharedPointsSaleRequestOfMe)): ?> | |||||
<div class="panel panel-default"> | |||||
<div class="panel-heading"> | |||||
<h3 class="panel-title"> | |||||
Demandes de partage en attente | |||||
</h3> | |||||
</div> | |||||
<div class="panel-body"> | |||||
<table class="table table-striped table-bordered"> | |||||
<thead> | |||||
<tr> | |||||
<td>Point de vente</td> | |||||
<td>Producteur</td> | |||||
<td></td> | |||||
</tr> | |||||
</thead> | |||||
<tbody> | |||||
<?php foreach($sharedPointsSaleRequestOfMe as $sharedPointSale): ?> | |||||
<tr> | |||||
<td><?= Html::encode($sharedPointSale->getPointSale()->name); ?></td> | |||||
<td><?= Html::encode($sharedPointSale->getProducerWithSharing()->name); ?></td> | |||||
<td> | |||||
<a href="<?= Yii::$app->urlManager->createUrl(['shared-point-sale/decline', 'id' => $sharedPointSale->id]) ?>" class="btn btn-default">Annuler</a> | |||||
</td> | |||||
</tr> | |||||
<?php endforeach; ?> | |||||
</tbody> | |||||
</table> | |||||
</div> | |||||
</div> | |||||
<?php endif; ?> | |||||
</div> | </div> |
<?php | |||||
use yii\db\Migration; | |||||
use yii\db\Schema; | |||||
/** | |||||
* Class m240604_093627_add_columns_shared_point_sale_created_confirmed_declined | |||||
*/ | |||||
class m240604_093627_add_columns_shared_point_sale_created_confirmed_declined extends Migration | |||||
{ | |||||
/** | |||||
* {@inheritdoc} | |||||
*/ | |||||
public function safeUp() | |||||
{ | |||||
$this->addColumn('shared_point_sale', 'created_at', Schema::TYPE_DATETIME); | |||||
$this->addColumn('shared_point_sale', 'created_by', Schema::TYPE_INTEGER); | |||||
$this->addColumn('shared_point_sale', 'confirmed_at', Schema::TYPE_DATETIME); | |||||
$this->addColumn('shared_point_sale', 'confirmed_by', Schema::TYPE_INTEGER); | |||||
$this->addColumn('shared_point_sale', 'declined_at', Schema::TYPE_DATETIME); | |||||
$this->addColumn('shared_point_sale', 'declined_by', Schema::TYPE_INTEGER); | |||||
$this->addForeignKey('fk_created_by', 'shared_point_sale', 'created_by', 'user', 'id'); | |||||
$this->addForeignKey('fk_confirmed_by', 'shared_point_sale', 'confirmed_by', 'user', 'id'); | |||||
$this->addForeignKey('fk_declined_by', 'shared_point_sale', 'declined_by', 'user', 'id'); | |||||
} | |||||
/** | |||||
* {@inheritdoc} | |||||
*/ | |||||
public function safeDown() | |||||
{ | |||||
$this->dropColumn('shared_point_sale', 'created_at'); | |||||
$this->dropColumn('shared_point_sale', 'created_by'); | |||||
$this->dropColumn('shared_point_sale', 'confirmed_at'); | |||||
$this->dropColumn('shared_point_sale', 'confirmed_by'); | |||||
$this->dropColumn('shared_point_sale', 'declined_at'); | |||||
$this->dropColumn('shared_point_sale', 'declined_by'); | |||||
$this->dropForeignKey('fk_created_by', 'shared_point_sale'); | |||||
$this->dropForeignKey('fk_confirmed_by', 'shared_point_sale'); | |||||
$this->dropForeignKey('fk_declined_by', 'shared_point_sale'); | |||||
} | |||||
} |
use common\components\ActiveRecordCommon; | use common\components\ActiveRecordCommon; | ||||
use domain\PointSale\PointSale\PointSale; | use domain\PointSale\PointSale\PointSale; | ||||
use domain\Producer\Producer\Producer; | use domain\Producer\Producer\Producer; | ||||
use domain\User\User\User; | |||||
class SharedPointSale extends ActiveRecordCommon | class SharedPointSale extends ActiveRecordCommon | ||||
{ | { | ||||
return $this; | return $this; | ||||
} | } | ||||
public function getCreatedAt(): \DateTime | |||||
{ | |||||
return new \DateTime($this->created_at); | |||||
} | |||||
public function setCreatedAt(\DateTime $createdAt): self | |||||
{ | |||||
$this->created_at = $createdAt->format('Y-m-d H:i:s'); | |||||
return $this; | |||||
} | |||||
public function getConfirmedAt(): \DateTime | |||||
{ | |||||
return new \DateTime($this->confirmed_at); | |||||
} | |||||
public function setConfirmedAt(\DateTime $confirmedAt): self | |||||
{ | |||||
$this->confirmed_at = $confirmedAt->format('Y-m-d H:i:s'); | |||||
return $this; | |||||
} | |||||
public function getDeclinedAt(): \DateTime | |||||
{ | |||||
return new \DateTime($this->declined_at); | |||||
} | |||||
public function setDeclinedAt(\DateTime $declinedAt): self | |||||
{ | |||||
$this->declined_at = $declinedAt->format('Y-m-d H:i:s'); | |||||
return $this; | |||||
} | |||||
public function getCreatedBy(): User | |||||
{ | |||||
return $this->createdByRelation; | |||||
} | |||||
public function setCreatedBy(User $createdBy): SharedPointSale | |||||
{ | |||||
$this->populateFieldObject('created_by', 'createdByRelation', $createdBy); | |||||
return $this; | |||||
} | |||||
public function getConfirmedBy(): User | |||||
{ | |||||
return $this->confirmedByRelation; | |||||
} | |||||
public function setConfirmedBy(User $confirmedBy): SharedPointSale | |||||
{ | |||||
$this->populateFieldObject('confirmed_by', 'confirmedByRelation', $confirmedBy); | |||||
return $this; | |||||
} | |||||
public function getDeclinedBy(): User | |||||
{ | |||||
return $this->declinedByRelation; | |||||
} | |||||
public function setDeclinedBy(User $declinedBy): SharedPointSale | |||||
{ | |||||
$this->populateFieldObject('declined_by', 'declinedByRelation', $declinedBy); | |||||
return $this; | |||||
} | |||||
public static function tableName() | public static function tableName() | ||||
{ | { | ||||
return 'shared_point_sale'; | return 'shared_point_sale'; | ||||
public function rules() | public function rules() | ||||
{ | { | ||||
return [ | return [ | ||||
[['id_point_sale', 'id_producer_with_sharing'], 'required'], | |||||
[['id_point_sale', 'id_producer_with_sharing', 'id_point_sale_with_sharing', 'status'], 'integer'], | |||||
[['id_point_sale', 'id_producer_with_sharing', 'created_at', 'created_by'], 'required'], | |||||
[['id_point_sale', 'id_producer_with_sharing', 'id_point_sale_with_sharing', 'status', | |||||
'created_by', 'confirmed_by', 'declined_by'], 'integer'], | |||||
[['created_at', 'confirmed_at', 'declined_at'], 'safe'], | |||||
['id_point_sale', 'exist', 'targetClass' => PointSale::class, 'targetAttribute' => 'id'], | |||||
['id_producer_with_sharing', 'exist', 'targetClass' => Producer::class, 'targetAttribute' => 'id'], | |||||
[['created_by', 'confirmed_by', 'declined_by'], 'exist', 'targetClass' => User::class, 'targetAttribute' => 'id'], | |||||
]; | ]; | ||||
} | } | ||||
return [ | return [ | ||||
'id_point_sale' => 'Point de vente que vous souhaitez partager', | 'id_point_sale' => 'Point de vente que vous souhaitez partager', | ||||
'id_producer_with_sharing' => 'Producteur avec qui vous souhaitez partager un point de vente', | 'id_producer_with_sharing' => 'Producteur avec qui vous souhaitez partager un point de vente', | ||||
'id_point_sale_with_sharing' => 'Point de vente du producteur avec qui vous souhaitez partager un point de vente', | |||||
'id_point_sale_with_sharing' => 'Point de vente du producteur avec qui vous souhaitez partager ce point de vente', | |||||
]; | ]; | ||||
} | } | ||||
{ | { | ||||
return $this->hasOne(PointSale::class, ['id' => 'id_point_sale_with_sharing']); | return $this->hasOne(PointSale::class, ['id' => 'id_point_sale_with_sharing']); | ||||
} | } | ||||
public function getCreatedByRelation() | |||||
{ | |||||
return $this->hasOne(User::class, ['id' => 'created_by']); | |||||
} | |||||
public function getConfirmedByRelation() | |||||
{ | |||||
return $this->hasOne(User::class, ['id' => 'confirmed_by']); | |||||
} | |||||
public function getDeclinedByRelation() | |||||
{ | |||||
return $this->hasOne(User::class, ['id' => 'declined_by']); | |||||
} | |||||
} | } |
use domain\_\StatusInterface; | use domain\_\StatusInterface; | ||||
use domain\PointSale\PointSale\PointSale; | use domain\PointSale\PointSale\PointSale; | ||||
use domain\Producer\Producer\Producer; | use domain\Producer\Producer\Producer; | ||||
use domain\User\User\User; | |||||
class SharedPointSaleBuilder extends AbstractBuilder | class SharedPointSaleBuilder extends AbstractBuilder | ||||
{ | { | ||||
public function instanciateSharedPointSale(PointSale $pointSale = null, Producer $producerWithSharing = null): SharedPointSale | |||||
public function instanciateSharedPointSale(PointSale $pointSale = null, Producer $producerWithSharing = null, User $createdBy = null): SharedPointSale | |||||
{ | { | ||||
$sharedPointSale = new SharedPointSale(); | $sharedPointSale = new SharedPointSale(); | ||||
$sharedPointSale->setProducerWithSharing($producerWithSharing); | $sharedPointSale->setProducerWithSharing($producerWithSharing); | ||||
} | } | ||||
if($createdBy) { | |||||
$sharedPointSale->setCreatedBy($createdBy); | |||||
} | |||||
$sharedPointSale->setCreatedAt(new \DateTime()); | |||||
$sharedPointSale->setStatus(StatusInterface::STATUS_ONLINE); | $sharedPointSale->setStatus(StatusInterface::STATUS_ONLINE); | ||||
return $sharedPointSale; | return $sharedPointSale; | ||||
} | } | ||||
public function initConfirmSharedPointSale(SharedPointSale $sharedPointSale, PointSale $pointSaleWithSharing, User $confirmedBy) | |||||
{ | |||||
$sharedPointSale->setPointSaleWithSharing($pointSaleWithSharing); | |||||
$sharedPointSale->setConfirmedBy($confirmedBy); | |||||
$sharedPointSale->setConfirmedAt(new \DateTime()); | |||||
} | |||||
public function initDeclineSharedPointSale(SharedPointSale $sharedPointSale, User $declinedBy) | |||||
{ | |||||
$sharedPointSale->setStatus(StatusInterface::STATUS_OFFLINE); | |||||
$sharedPointSale->setDeclinedBy($declinedBy); | |||||
$sharedPointSale->setDeclinedAt(new \DateTime()); | |||||
} | |||||
} | } |
use domain\_\AbstractManager; | use domain\_\AbstractManager; | ||||
use domain\PointSale\PointSale\PointSale; | use domain\PointSale\PointSale\PointSale; | ||||
use domain\Producer\Producer\Producer; | use domain\Producer\Producer\Producer; | ||||
use domain\User\User\User; | |||||
class SharedPointSaleManager extends AbstractManager | class SharedPointSaleManager extends AbstractManager | ||||
{ | { | ||||
$this->sharedPointSaleBuilder = $this->loadService(SharedPointSaleBuilder::class); | $this->sharedPointSaleBuilder = $this->loadService(SharedPointSaleBuilder::class); | ||||
} | } | ||||
public function createSharedPointSale(PointSale $pointSale, Producer $producerWithSharing): SharedPointSale | |||||
public function createSharedPointSale(PointSale $pointSale, Producer $producerWithSharing, User $createdBy): SharedPointSale | |||||
{ | { | ||||
$sharedPointSale = $this->sharedPointSaleBuilder->instanciateSharedPointSale($pointSale, $producerWithSharing); | |||||
$sharedPointSale = $this->sharedPointSaleBuilder->instanciateSharedPointSale($pointSale, $producerWithSharing, $createdBy); | |||||
$this->sharedPointSaleBuilder->create($sharedPointSale); | $this->sharedPointSaleBuilder->create($sharedPointSale); | ||||
return $sharedPointSale; | return $sharedPointSale; | ||||
} | } | ||||
public function confirmSharedPointSale(SharedPointSale $sharedPointSale, PointSale $pointSaleWithSharing, User $confirmedBy): bool | |||||
{ | |||||
$this->sharedPointSaleBuilder->initConfirmSharedPointSale($sharedPointSale, $pointSaleWithSharing, $confirmedBy); | |||||
return $this->sharedPointSaleBuilder->update($sharedPointSale); | |||||
} | |||||
public function declineSharedPointSale(SharedPointSale $sharedPointSale, User $declinedBy): bool | |||||
{ | |||||
$this->sharedPointSaleBuilder->initDeclineSharedPointSale($sharedPointSale, $declinedBy); | |||||
return $this->sharedPointSaleBuilder->update($sharedPointSale); | |||||
} | |||||
} | } |
return [ | return [ | ||||
SharedPointSaleDefinition::class, | SharedPointSaleDefinition::class, | ||||
SharedPointSaleBuilder::class, | SharedPointSaleBuilder::class, | ||||
SharedPointSaleRepository::class, | |||||
SharedPointSaleManager::class | SharedPointSaleManager::class | ||||
]; | ]; | ||||
} | } | ||||
return SharedPointSaleBuilder::getInstance(); | return SharedPointSaleBuilder::getInstance(); | ||||
} | } | ||||
public function getRepository(): SharedPointSaleRepository | |||||
{ | |||||
return SharedPointSaleRepository::getInstance(); | |||||
} | |||||
public function getManager(): SharedPointSaleManager | public function getManager(): SharedPointSaleManager | ||||
{ | { | ||||
return SharedPointSaleManager::getInstance(); | return SharedPointSaleManager::getInstance(); |
<?php | |||||
namespace domain\PointSale\SharedPointSale; | |||||
use domain\_\AbstractRepository; | |||||
class SharedPointSaleRepository extends AbstractRepository | |||||
{ | |||||
protected SharedPointSaleRepositoryQuery $sharedPointSaleRepositoryQuery; | |||||
public function loadDependencies(): void | |||||
{ | |||||
$this->loadQuery(SharedPointSaleRepositoryQuery::class); | |||||
} | |||||
public function getDefaultOptionsSearch(): array | |||||
{ | |||||
return [ | |||||
self::WITH => [], | |||||
self::JOIN_WITH => ['pointSaleRelation'], | |||||
self::ORDER_BY => '', | |||||
self::ATTRIBUTE_ID_PRODUCER => '' | |||||
]; | |||||
} | |||||
public function findOneSharedPointSaleById(int $id) | |||||
{ | |||||
return $this->createQuery() | |||||
->filterById($id) | |||||
->findOne(); | |||||
} | |||||
public function findSharedPointsSaleRequestsOfMe() | |||||
{ | |||||
return $this->createQuery() | |||||
->filterByProducerOfPointSale($this->getProducerContext()) | |||||
->filterIsStatusOnline() | |||||
->find(); | |||||
} | |||||
public function findSharedPointsSaleRequestsOthers() | |||||
{ | |||||
return $this->createQuery() | |||||
->filterByProducerWithSharing($this->getProducerContext()) | |||||
->filterIsStatusOnline() | |||||
->find(); | |||||
} | |||||
} |
<?php | |||||
namespace domain\PointSale\SharedPointSale; | |||||
use domain\_\AbstractRepositoryQuery; | |||||
use domain\_\StatusInterface; | |||||
use domain\Producer\Producer\Producer; | |||||
class SharedPointSaleRepositoryQuery extends AbstractRepositoryQuery | |||||
{ | |||||
protected SharedPointSaleDefinition $sharedPointSaleDefinition; | |||||
public function loadDependencies(): void | |||||
{ | |||||
$this->loadDefinition(SharedPointSaleDefinition::class); | |||||
} | |||||
public function filterIsStatusOnline() | |||||
{ | |||||
$this->andWhere(['shared_point_sale.status' => StatusInterface::STATUS_ONLINE]); | |||||
return $this; | |||||
} | |||||
public function filterByProducerOfPointSale(Producer $producer): self | |||||
{ | |||||
$this->andWhere(['point_sale.id_producer' => $producer->id]); | |||||
return $this; | |||||
} | |||||
public function filterByProducerWithSharing(Producer $producer): self | |||||
{ | |||||
$this->andWhere(['id_producer_with_sharing' => $producer->id]); | |||||
return $this; | |||||
} | |||||
} |