Browse Source

Points de vente partagés : distributions de plusieurs producteurs sur un même point de vente

feature/rotating_product
Guillaume Bourgeois 5 months ago
parent
commit
2fd02429ae
10 changed files with 336 additions and 29 deletions
  1. +41
    -6
      backend/controllers/SharedPointSaleController.php
  2. +6
    -16
      backend/views/shared-point-sale/create.php
  3. +32
    -1
      backend/views/shared-point-sale/index.php
  4. +42
    -0
      console/migrations/m240604_093627_add_columns_shared_point_sale_created_confirmed_declined.php
  5. +90
    -3
      domain/PointSale/SharedPointSale/SharedPointSale.php
  6. +21
    -1
      domain/PointSale/SharedPointSale/SharedPointSaleBuilder.php
  7. +15
    -2
      domain/PointSale/SharedPointSale/SharedPointSaleManager.php
  8. +6
    -0
      domain/PointSale/SharedPointSale/SharedPointSaleModule.php
  9. +48
    -0
      domain/PointSale/SharedPointSale/SharedPointSaleRepository.php
  10. +35
    -0
      domain/PointSale/SharedPointSale/SharedPointSaleRepositoryQuery.php

+ 41
- 6
backend/controllers/SharedPointSaleController.php View File

@@ -39,17 +39,18 @@
namespace backend\controllers;

use backend\controllers\BackendController;
use backend\forms\SharedPointSaleForm;
use backend\models\AccessUserProducerForm;
use domain\Feature\Feature\Feature;
use domain\User\User\UserSearch;
use yii\filters\AccessControl;
use yii\web\NotFoundHttpException;

/**
* UserController implements the CRUD actions for User model.
*/
class SharedPointSaleController extends BackendController
{

public function behaviors()
{
return [
@@ -60,7 +61,6 @@ class SharedPointSaleController extends BackendController
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action) {

$authorizationChecker = $this->getUserModule()->getAuthorizationChecker();
$featureChecker = $this->getFeatureModule()->getChecker();

@@ -75,20 +75,55 @@ class SharedPointSaleController extends BackendController

public function actionIndex()
{
return $this->render('index');
$sharedPointSaleRepository = $this->getSharedPointSaleModule()->getRepository();

return $this->render('index', [
'sharedPointsSaleRequestOfMe' => $sharedPointSaleRepository->findSharedPointsSaleRequestsOfMe(),
'sharedPointsSaleRequestOthers' => $sharedPointSaleRepository->findSharedPointsSaleRequestsOthers()
]);
}

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");
return $this->redirect(['index']);
}

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;
}
}

+ 6
- 16
backend/views/shared-point-sale/create.php View File

@@ -43,38 +43,28 @@ use yii\widgets\ActiveForm;
$pointSaleModule = $this->getPointSaleModule();
$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">

<?php $form = ActiveForm::begin([
'enableClientValidation' => false
]); ?>

<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">
<?= $form->field($sharedPointSale, 'id_point_sale')
<?= $form->field($sharedPointSaleModel, 'id_point_sale')
->dropDownList($pointSaleModule->getRepository()->populatePointSaleDropdownList()); ?>
<?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']]);?>
</div>
</div>

<div class="form-group form-actions">
<?= 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>

<?php ActiveForm::end(); ?>
</div>

+ 32
- 1
backend/views/shared-point-sale/index.php View File

@@ -52,9 +52,40 @@ $this->addButton(
'class' => 'btn btn-primary'
]
);

?>

<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>

+ 42
- 0
console/migrations/m240604_093627_add_columns_shared_point_sale_created_confirmed_declined.php View File

@@ -0,0 +1,42 @@
<?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');
}
}

+ 90
- 3
domain/PointSale/SharedPointSale/SharedPointSale.php View File

@@ -5,6 +5,7 @@ namespace domain\PointSale\SharedPointSale;
use common\components\ActiveRecordCommon;
use domain\PointSale\PointSale\PointSale;
use domain\Producer\Producer\Producer;
use domain\User\User\User;

class SharedPointSale extends ActiveRecordCommon
{
@@ -56,6 +57,72 @@ class SharedPointSale extends ActiveRecordCommon
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()
{
return 'shared_point_sale';
@@ -64,8 +131,13 @@ class SharedPointSale extends ActiveRecordCommon
public function rules()
{
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'],
];
}

@@ -74,7 +146,7 @@ class SharedPointSale extends ActiveRecordCommon
return [
'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_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',
];
}

@@ -94,4 +166,19 @@ class SharedPointSale extends ActiveRecordCommon
{
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']);
}
}

+ 21
- 1
domain/PointSale/SharedPointSale/SharedPointSaleBuilder.php View File

@@ -6,10 +6,11 @@ use domain\_\AbstractBuilder;
use domain\_\StatusInterface;
use domain\PointSale\PointSale\PointSale;
use domain\Producer\Producer\Producer;
use domain\User\User\User;

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();

@@ -21,8 +22,27 @@ class SharedPointSaleBuilder extends AbstractBuilder
$sharedPointSale->setProducerWithSharing($producerWithSharing);
}

if($createdBy) {
$sharedPointSale->setCreatedBy($createdBy);
}

$sharedPointSale->setCreatedAt(new \DateTime());
$sharedPointSale->setStatus(StatusInterface::STATUS_ONLINE);

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());
}
}

+ 15
- 2
domain/PointSale/SharedPointSale/SharedPointSaleManager.php View File

@@ -5,6 +5,7 @@ namespace domain\PointSale\SharedPointSale;
use domain\_\AbstractManager;
use domain\PointSale\PointSale\PointSale;
use domain\Producer\Producer\Producer;
use domain\User\User\User;

class SharedPointSaleManager extends AbstractManager
{
@@ -15,10 +16,22 @@ class SharedPointSaleManager extends AbstractManager
$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);
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);
}
}

+ 6
- 0
domain/PointSale/SharedPointSale/SharedPointSaleModule.php View File

@@ -11,6 +11,7 @@ class SharedPointSaleModule extends AbstractModule
return [
SharedPointSaleDefinition::class,
SharedPointSaleBuilder::class,
SharedPointSaleRepository::class,
SharedPointSaleManager::class
];
}
@@ -25,6 +26,11 @@ class SharedPointSaleModule extends AbstractModule
return SharedPointSaleBuilder::getInstance();
}

public function getRepository(): SharedPointSaleRepository
{
return SharedPointSaleRepository::getInstance();
}

public function getManager(): SharedPointSaleManager
{
return SharedPointSaleManager::getInstance();

+ 48
- 0
domain/PointSale/SharedPointSale/SharedPointSaleRepository.php View File

@@ -0,0 +1,48 @@
<?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();
}
}

+ 35
- 0
domain/PointSale/SharedPointSale/SharedPointSaleRepositoryQuery.php View File

@@ -0,0 +1,35 @@
<?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;
}
}

Loading…
Cancel
Save