@@ -887,6 +887,11 @@ class OrderController extends BackendController | |||
if($order && $processCredit) { | |||
$order->processCredit() ; | |||
} | |||
// lien utilisateur / point de vente | |||
if($idUser && $pointSale) { | |||
$pointSale->linkUser($idUser) ; | |||
} | |||
} | |||
return ['success'] ; |
@@ -43,6 +43,8 @@ use common\models\Producer ; | |||
use common\models\Distribution ; | |||
use backend\models\MailForm ; | |||
use common\models\UserProducer ; | |||
use common\models\UserPointSale ; | |||
use common\models\PointSale ; | |||
/** | |||
* UserController implements the CRUD actions for User model. | |||
@@ -110,6 +112,37 @@ class UserController extends BackendController | |||
]); | |||
} | |||
public function initForm($model) | |||
{ | |||
// init points de vente sélectionnés | |||
if($model->id) { | |||
$userPointSaleArray = UserPointSale::searchAll([ | |||
'id_user' => $model->id | |||
]) ; | |||
if($userPointSaleArray && count($userPointSaleArray) > 0) { | |||
foreach($userPointSaleArray as $userPointSaleArray) { | |||
$model->points_sale[] = $userPointSaleArray->id_point_sale ; | |||
} | |||
} | |||
} | |||
// points de vente | |||
$pointsSaleArray = PointSale::find() | |||
->where([ | |||
'id_producer' => Producer::getId(), | |||
]) | |||
->joinWith(['userPointSale' => function($query) use($model) { | |||
if($model->id) { | |||
$query->andOnCondition('user_point_sale.id_user = '.$model->id) ; | |||
} | |||
}]) | |||
->all(); | |||
return [ | |||
'pointsSaleArray' => $pointsSaleArray | |||
] ; | |||
} | |||
/** | |||
* Creates a new User model. | |||
* If creation is successful, the browser will be redirected to the 'view' page. | |||
@@ -141,13 +174,17 @@ class UserController extends BackendController | |||
$useProducer->save(); | |||
$model->sendMailWelcome($password) ; | |||
return $this->redirect(['index']); | |||
} else { | |||
return $this->render('create', [ | |||
'model' => $model, | |||
]); | |||
$this->processLinkPointSale($model) ; | |||
Yii::$app->getSession()->setFlash('success', 'Utilisateur créé.'); | |||
$model = new User(); | |||
} | |||
return $this->render('create', array_merge($this->initForm($model),[ | |||
'model' => $model, | |||
])); | |||
} | |||
/** | |||
@@ -174,16 +211,45 @@ class UserController extends BackendController | |||
$model->username = $model->email; | |||
$model->sendMailWelcome($password) ; | |||
} | |||
return $this->redirect(['index']); | |||
} else { | |||
return $this->render('update', [ | |||
'model' => $model, | |||
]); | |||
$this->processLinkPointSale($model) ; | |||
Yii::$app->getSession()->setFlash('success', 'Utilisateur modifié.'); | |||
} | |||
} else { | |||
} | |||
else { | |||
throw new UserException("Vous ne pouvez pas modifier cet utilisateur, soit parce qu'il appartient à plusieurs établissements, soit parce qu'il n'est pas lié au votre."); | |||
} | |||
return $this->render('update', array_merge($this->initForm($model),[ | |||
'model' => $model, | |||
])); | |||
} | |||
/** | |||
* Lie un utilisateur aux points de vente sélectionnés. | |||
* | |||
* @param User $modelUser | |||
*/ | |||
public function processLinkPointSale($modelUser) | |||
{ | |||
$posts = Yii::$app->request->post() ; | |||
UserPointSale::deleteAll([ | |||
'id_user' => $modelUser->id | |||
]) ; | |||
if(is_array($modelUser->points_sale) && count($modelUser->points_sale) > 0) { | |||
foreach($modelUser->points_sale as $pointSaleId) { | |||
$userPointSale = UserPointSale::searchOne([ | |||
'id_user' => $modelUser->id, | |||
'id_point_sale' => $pointSaleId | |||
]) ; | |||
if(!$userPointSale) { | |||
$userPointSale = new UserPointSale ; | |||
$userPointSale->id_user = $modelUser->id ; | |||
$userPointSale->id_point_sale = $pointSaleId ; | |||
$userPointSale->comment = isset($posts['User']['comment_point_sale_'.$pointSaleId]) ? $posts['User']['comment_point_sale_'.$pointSaleId] : '' ; | |||
$userPointSale->save() ; | |||
} | |||
} | |||
} | |||
} | |||
/** |
@@ -81,7 +81,8 @@ use common\models\PointSale ; | |||
<?= $form->field($model, 'auto_payment') | |||
->checkbox() | |||
->hint('Cochez cette case si vous souhaitez que le crédit pain du client soit automatiquement débité lors de la création de la commande.<br />' | |||
. 'Attention, un compte client existant doit être spécifié en haut de ce formulaire.') ?> | |||
. 'Attention, un compte client existant doit être spécifié en haut de ce formulaire.' | |||
. '<br /><strong>Pris en compte uniquement dans le cas d\'un Crédit défini comme optionnel au niveau du point de vente. Dans les autres cas, il sera automatiquement déduit.</strong>') ?> | |||
<div class="products"> | |||
<h2>Produits</h2> |
@@ -53,6 +53,18 @@ use yii\widgets\ActiveForm; | |||
<?= $form->field($model, 'phone')->textInput() ?> | |||
<?= $form->field($model, 'email')->textInput() ?> | |||
<?= $form->field($model, 'address')->textarea() ?> | |||
<?= $form->field($model, 'points_sale')->checkboxlist( | |||
ArrayHelper::map($pointsSaleArray, 'id',function($pointSale) use ($model) { | |||
$commentUserPointSale = isset($pointSale->userPointSale[0]) ? $pointSale->userPointSale[0]->comment : '' ; | |||
$html = Html::encode($pointSale->name); | |||
if($pointSale->restricted_access) { | |||
$html .= '<input type="text" placeholder="Commentaire" class="form-control" name="User[comment_point_sale_'.$pointSale->id.']" value="'.(($model->id) ? Html::encode($commentUserPointSale) : '').'" />' ; | |||
} | |||
return $html ; | |||
}), [ | |||
'encode' => false | |||
]); | |||
?> | |||
<div class="form-group"> | |||
<?= Html::submitButton($model->isNewRecord ? 'Ajouter' : 'Modifier', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> |
@@ -50,6 +50,7 @@ $this->addBreadcrumb('Ajouter') ; | |||
<?php else: ?> | |||
<?= $this->render('_form', [ | |||
'model' => $model, | |||
'pointsSaleArray' => $pointsSaleArray | |||
]) ?> | |||
<?php endif; ?> | |||
</div> |
@@ -48,5 +48,6 @@ $this->addBreadcrumb('Modifier') ; | |||
<div class="user-update"> | |||
<?= $this->render('_form', [ | |||
'model' => $model, | |||
'pointsSaleArray' => $pointsSaleArray | |||
]) ?> | |||
</div> |
@@ -1967,6 +1967,24 @@ termes. | |||
float: right; | |||
} | |||
/* line 5, ../sass/user/_form.scss */ | |||
.user-create #user-points_sale label, | |||
.user-update #user-points_sale label { | |||
font-weight: normal; | |||
margin-right: 10px; | |||
cursor: pointer; | |||
background-color: white; | |||
padding: 5px 10px; | |||
border: solid 1px #e0e0e0; | |||
} | |||
/* line 13, ../sass/user/_form.scss */ | |||
.user-create #user-points_sale label .form-control, | |||
.user-update #user-points_sale label .form-control { | |||
display: inline; | |||
width: 200px; | |||
margin-left: 15px; | |||
} | |||
/* line 4, ../sass/producer/_update.scss */ | |||
.producer-update #nav-params { | |||
margin-bottom: 30px; |
@@ -1334,5 +1334,6 @@ a.btn, button.btn { | |||
@import "distribution/_index.scss" ; | |||
@import "user/_emails.scss" ; | |||
@import "user/_credit.scss" ; | |||
@import "user/_form.scss" ; | |||
@import "producer/_update.scss" ; | |||
@import "point_sale/_index.scss" ; |
@@ -0,0 +1,20 @@ | |||
.user-create, | |||
.user-update { | |||
#user-points_sale { | |||
label { | |||
font-weight: normal ; | |||
margin-right: 10px ; | |||
cursor: pointer ; | |||
background-color: white ; | |||
padding: 5px 10px ; | |||
border: solid 1px #e0e0e0 ; | |||
.form-control { | |||
display: inline ; | |||
width: 200px; | |||
margin-left: 15px ; | |||
} | |||
} | |||
} | |||
} |
@@ -343,4 +343,27 @@ class PointSale extends ActiveRecordCommon | |||
{ | |||
return $this->credit_functioning ? $this->credit_functioning : Producer::getConfig('credit_functioning') ; | |||
} | |||
/** | |||
* Lie un utilisateur au point de vente. | |||
* | |||
* @param integer $idUser | |||
*/ | |||
public function linkUser($idUser) | |||
{ | |||
if($idUser) { | |||
$userPointSale = UserPointSale::find() | |||
->where([ | |||
'id_user' => $idUser, | |||
'id_point_sale' => $this->id | |||
])->one() ; | |||
if(!$userPointSale) { | |||
$userPointSale = new UserPointSale ; | |||
$userPointSale->id_user = $idUser ; | |||
$userPointSale->id_point_sale = $this->id ; | |||
$userPointSale->save() ; | |||
} | |||
} | |||
} | |||
} |
@@ -45,6 +45,7 @@ use common\models\PointSale; | |||
use common\models\UserPointSale; | |||
use common\models\Order; | |||
use common\models\ProductOrder; | |||
use common\models\User ; | |||
/** | |||
* This is the model class for table "commande_auto". | |||
@@ -189,7 +190,29 @@ class Subscription extends ActiveRecordCommon | |||
$order->id_point_sale = $this->id_point_sale; | |||
$order->id_distribution = $distribution->id; | |||
$order->id_subscription = $this->id; | |||
$order->auto_payment = $this->auto_payment ; | |||
$pointSale = PointSale::findOne($this->id_point_sale) ; | |||
$creditFunctioning = $pointSale->getCreditFunctioning() ; | |||
$order->auto_payment = 0 ; | |||
if($order->id_user && Producer::getConfig('credit') && $pointSale->credit) { | |||
if($creditFunctioning == Producer::CREDIT_FUNCTIONING_OPTIONAL) { | |||
$order->auto_payment = $this->auto_payment ; | |||
} | |||
elseif($creditFunctioning == Producer::CREDIT_FUNCTIONING_MANDATORY) { | |||
$order->auto_payment = 1 ; | |||
} | |||
elseif($creditFunctioning == Producer::CREDIT_FUNCTIONING_USER) { | |||
$user = User::findOne($order->id_user) ; | |||
$userProducer = UserProducer::searchOne([ | |||
'id_user' => $order->id_user, | |||
'id_producer' => $distribution->id_producer | |||
]) ; | |||
if($userProducer) { | |||
$order->auto_payment = $userProducer->credit_active ; | |||
} | |||
} | |||
} | |||
$userPointSale = UserPointSale::searchOne([ | |||
'id_point_sale' => $this->id_point_sale, | |||
@@ -202,6 +225,12 @@ class Subscription extends ActiveRecordCommon | |||
$order->save(); | |||
// liaison utilisateur / point de vente | |||
if($order->id_user) { | |||
$pointSale = PointSale::findOne($this->id_point_sale) ; | |||
$pointSale->linkUser($order->id_user) ; | |||
} | |||
// produits | |||
$amountTotal = 0; | |||
$productsAdd = false; |
@@ -70,6 +70,7 @@ class User extends ActiveRecordCommon implements IdentityInterface | |||
var $password_old; | |||
var $password_new; | |||
var $password_new_confirm; | |||
var $points_sale = [] ; | |||
/** | |||
* @inheritdoc | |||
@@ -105,7 +106,7 @@ class User extends ActiveRecordCommon implements IdentityInterface | |||
['password_old', 'verifyPasswordOld'], | |||
['password_new', 'verifyPasswordNew'], | |||
['password_new_confirm', 'verifyPasswordNewConfirm'], | |||
[['date_last_connection', 'password_old', 'password_new', 'password_new_confirm', 'password_hash'], 'safe'], | |||
[['date_last_connection', 'password_old', 'password_new', 'password_new_confirm', 'password_hash','points_sale'], 'safe'], | |||
]; | |||
} | |||
@@ -129,6 +130,7 @@ class User extends ActiveRecordCommon implements IdentityInterface | |||
'password_old' => 'Ancien mot de passe', | |||
'password_new' => 'Nouveau mot de passe', | |||
'password_new_confirm' => 'Confirmation du nouveau mot de passe', | |||
'points_sale' => 'Points de vente' | |||
]; | |||
} | |||
@@ -223,6 +225,11 @@ class User extends ActiveRecordCommon implements IdentityInterface | |||
return $this->hasMany(UserProducer::className(), ['id_user' => 'id']); | |||
} | |||
public function getOrder() | |||
{ | |||
return $this->hasMany(Order::className(), ['id_user' => 'id']) ; | |||
} | |||
/** | |||
* @inheritdoc | |||
*/ |
@@ -60,7 +60,29 @@ class UserSearch extends User | |||
{ | |||
$optionsSearch = self::defaultOptionsSearch() ; | |||
$query = User::find(); | |||
$query = User::find() | |||
->select( | |||
'`user`.id, ' | |||
.'`user`.username,' | |||
.'`user`.email, ' | |||
.'`user`.status, ' | |||
.'`user`.created_at, ' | |||
.'`user`.updated_at, ' | |||
.'`user`.lastname, ' | |||
.'`user`.name, ' | |||
.'`user`.phone, ' | |||
.'`user`.address, ' | |||
.'`user`.no_mail, ' | |||
.'`user`.mail_distribution_monday, ' | |||
.'`user`.mail_distribution_tuesday, ' | |||
.'`user`.mail_distribution_wednesday, ' | |||
.'`user`.mail_distribution_thursday, ' | |||
.'`user`.mail_distribution_friday, ' | |||
.'`user`.mail_distribution_saturday, ' | |||
.'`user`.mail_distribution_sunday, ' | |||
.'`user`.id_producer, ' | |||
.'`user`.date_last_connection, ' | |||
. '(SELECT COUNT(*) FROM `order` WHERE `user`.id = `order`.id_user) AS count_orders'); | |||
$dataProvider = new ActiveDataProvider([ | |||
'query' => $query, | |||
@@ -85,29 +107,17 @@ class UserSearch extends User | |||
return $dataProvider; | |||
} | |||
$active = (isset($this->inactive) && $this->inactive) ? 0 : 1 ; | |||
$query->innerJoin('user_producer','user.id = user_producer.id_user AND user_producer.active = '.$active.' AND user_producer.id_producer = :id_producer', [':id_producer' => Producer::getId()]) ; | |||
$query->innerJoin('user_producer','user.id = user_producer.id_user AND user_producer.id_producer = :id_producer', [':id_producer' => Producer::getId()]) ; | |||
if(isset($this->id_point_sale) && $this->id_point_sale) { | |||
$pointSale = PointSale::findOne(['id' => $this->id_point_sale]) ; | |||
if($pointSale->restricted_access) { | |||
$query->innerJoin('user_point_sale','user.id = user_point_sale.id_user AND user_point_sale.id_point_sale = :id_point_sale',[':id_point_sale' => $this->id_point_sale]) ; | |||
} | |||
else { | |||
$query->innerJoin( | |||
'order', | |||
'user.id = order.id_user AND order.id_point_sale = :id_point_sale', | |||
[':id_point_sale' => $this->id_point_sale] | |||
)->groupBy('user.id') ; | |||
} | |||
$query->innerJoin('user_point_sale','user.id = user_point_sale.id_user AND user_point_sale.id_point_sale = :id_point_sale',[':id_point_sale' => $this->id_point_sale]) ; | |||
} | |||
if(isset($this->inactive) && $this->inactive) { | |||
$query->innerJoin( | |||
'order', | |||
'user.id = order.id_user' | |||
) | |||
->groupBy('user.id'); | |||
$query->having([ | |||
'count_orders' => 0 | |||
]) ; | |||
} | |||
$query->andFilterWhere([ | |||
@@ -116,7 +126,6 @@ class UserSearch extends User | |||
['like', 'user.name', $this->username] | |||
]); | |||
return $dataProvider; | |||
} | |||
@@ -0,0 +1,51 @@ | |||
<?php | |||
use yii\db\Migration; | |||
class m190226_084104_lien_utilisateur_point_de_vente extends Migration | |||
{ | |||
public function up() | |||
{ | |||
$producersArray = \common\models\Producer::find()->all() ; | |||
foreach($producersArray as $producer) { | |||
$pointsSaleArray = common\models\PointSale::find() | |||
->where([ | |||
'id_producer' => $producer->id | |||
]) | |||
->all(); | |||
foreach($pointsSaleArray as $pointSale) { | |||
$usersArray = common\models\User::find()->innerJoin( | |||
'order', | |||
'user.id = order.id_user AND order.id_point_sale = :id_point_sale', | |||
[':id_point_sale' => $pointSale->id] | |||
) | |||
->groupBy('user.id') | |||
->all(); | |||
foreach($usersArray as $user) { | |||
$userPointSale = \common\models\UserPointSale::find() | |||
->where([ | |||
'id_user' => $user->id, | |||
'id_point_sale' => $pointSale->id | |||
])->one() ; | |||
if(!$userPointSale) { | |||
$userPointSale = new \common\models\UserPointSale() ; | |||
$userPointSale->id_user = $user->id ; | |||
$userPointSale->id_point_sale = $pointSale->id ; | |||
$userPointSale->save() ; | |||
} | |||
} | |||
} | |||
} | |||
} | |||
public function down() | |||
{ | |||
echo "m190226_084104_lien_utilisateur_point_de_vente cannot be reverted.\n"; | |||
return false; | |||
} | |||
} |
@@ -328,6 +328,9 @@ class OrderController extends ProducerBaseController | |||
$productOrder->save(); | |||
} | |||
} | |||
// lien utilisateur / point de vente | |||
$pointSale->linkUser(User::getCurrentId()) ; | |||
// credit | |||
$credit = Producer::getConfig('credit'); |