Browse Source

[Global] Prix dégressifs : évolution méthode Product::getPrice() #249

refactoring
Guillaume 2 years ago
parent
commit
56820bf5cb
5 changed files with 199 additions and 161 deletions
  1. +3
    -3
      backend/controllers/DistributionController.php
  2. +2
    -1
      backend/web/js/vuejs/distribution-index.js
  3. +16
    -50
      common/models/Product.php
  4. +176
    -106
      common/models/ProductPrice.php
  5. +2
    -1
      common/models/Subscription.php

+ 3
- 3
backend/controllers/DistributionController.php View File

]; ];
} }


public function actionAjaxUpdateProductOrder($idDistribution, $idUser = false, $idPointSale = false)
public function actionAjaxUpdateProductOrder($idDistribution, $idUser = false, $idPointSale = false, $idOrder = false)
{ {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;


'price' => $product->getPriceWithTax([ 'price' => $product->getPriceWithTax([
'user' => $user, 'user' => $user,
'user_producer' => $userProducer, 'user_producer' => $userProducer,
'point_sale' => $pointSale
'point_sale' => $pointSale,
]), ]),
'active' => $product->productDistribution[0]->active 'active' => $product->productDistribution[0]->active
&& (!$pointSale || $product->isAvailableOnPointSale($pointSale)) && (!$pointSale || $product->isAvailableOnPointSale($pointSale))
]); ]);


$returnTillerObject = json_decode($returnTiller); $returnTillerObject = json_decode($returnTiller);
$order->tiller_external_id = ''.$returnTillerObject->id;
$order->tiller_external_id = '' . $returnTillerObject->id;
$order->save(); $order->save();


$return[] = $returnTiller; $return[] = $returnTiller;

+ 2
- 1
backend/web/js/vuejs/distribution-index.js View File

params: { params: {
idDistribution: app.distribution.id, idDistribution: app.distribution.id,
idUser: order.id_user, idUser: order.id_user,
idPointSale: order.id_point_sale
idPointSale: order.id_point_sale,
idOrder: order.id
} }
}) })
.then(function (response) { .then(function (response) {

+ 16
- 50
common/models/Product.php View File

$user = isset($params['user']) ? $params['user'] : false ; $user = isset($params['user']) ? $params['user'] : false ;
$userProducer = isset($params['user_producer']) ? $params['user_producer'] : false ; $userProducer = isset($params['user_producer']) ? $params['user_producer'] : false ;
$pointSale = isset($params['point_sale']) ? $params['point_sale'] : false ; $pointSale = isset($params['point_sale']) ? $params['point_sale'] : false ;
$quantity = isset($params['quantity']) ? $params['quantity'] : 1 ;


if($specificPrices && ($user || $pointSale)) { if($specificPrices && ($user || $pointSale)) {


$specificPricesArray = [
'user' => false,
'pointsale' => false,
'user_pointsale' => false,
'usergroup' => false,
] ;
$priorityMatchSpecificPrice = ProductPrice::getPriorityMatchOfSpecificPriceArray($specificPrices, $user, $pointSale);


foreach($specificPrices as $specificPrice) {
if($user
&& $specificPrice->id_user
&& !$specificPrice->id_point_sale
&& !$specificPrice->id_user_group
&& $specificPrice->id_user == $user->id) {

$specificPricesArray['user'] = $specificPrice->price ;
}
if($user
&& $specificPrice->id_user_group
&& !$specificPrice->id_point_sale
&& !$specificPrice->id_user
&& $user->belongsToUserGroup($specificPrice->id_user_group)) {

$specificPricesArray['usergroup'] = $specificPrice->price ;
}
if($pointSale
&& $specificPrice->id_point_sale
&& !$specificPrice->id_user
&& !$specificPrice->id_user_group
&& $specificPrice->id_point_sale == $pointSale->id) {

$specificPricesArray['pointsale'] = $specificPrice->price ;
}

if($pointSale && $user
&& $specificPrice->id_point_sale
&& $specificPrice->id_user
&& $specificPrice->id_point_sale == $pointSale->id
&& $specificPrice->id_user == $user->id) {

$specificPricesArray['user_pointsale'] = $specificPrice->price ;
}
foreach($specificPrices as $keySpecificPrice => $specificPrice) {
if(!$specificPrice->$priorityMatchSpecificPrice($user, $pointSale)) {
unset($specificPrices[$keySpecificPrice]);
}
} }


if($specificPricesArray['user_pointsale']) {
return $specificPricesArray['user_pointsale'] ;
}
elseif($specificPricesArray['user']) {
return $specificPricesArray['user'] ;
}
elseif($specificPricesArray['usergroup']) {
return $specificPricesArray['usergroup'] ;
$bestPrice = 9999;
foreach($specificPrices as $specificPrice) {
$fromQuantity = $specificPrice->from_quantity;
if((($fromQuantity && $fromQuantity <= $quantity) || !$fromQuantity)
&& $specificPrice->price < $bestPrice) {
$bestPrice = $specificPrice->price;
}
} }
elseif($specificPricesArray['pointsale']) {
return $specificPricesArray['pointsale'] ;

if($bestPrice != 9999) {
return $bestPrice;
} }
} }



+ 176
- 106
common/models/ProductPrice.php View File

class ProductPrice extends ActiveRecordCommon class ProductPrice extends ActiveRecordCommon
{ {


/**
* @inheritdoc
*/
public static function tableName()
{
return 'product_price';
/**
* @inheritdoc
*/
public static function tableName()
{
return 'product_price';
}

/**
* @inheritdoc
*/
public function rules()
{
return [
[
'id_user',
'required',
'when' => function ($model) {
return !$model->id_point_sale && !$model->id_user_group;
},
'message' => 'Vous devez renseigner au moins un utilisateur, un point de vente ou un groupe d\'utilisateur'
],
[
'id_point_sale',
'required',
'when' => function ($model) {
return !$model->id_user && !$model->id_user_group;
},
'message' => 'Vous devez renseigner au moins un utilisateur, un point de vente ou un groupe d\'utilisateur'
],
[
'id_user_group',
'required',
'when' => function ($model) {
return !$model->id_user && !$model->id_point_sale;
},
'message' => 'Vous devez renseigner au moins un utilisateur, un point de vente ou un groupe d\'utilisateur'
],
[['id_product', 'price'], 'required'],
[['id_product', 'id_user', 'id_point_sale', 'id_user_group', 'percent'], 'integer'],
[['price', 'from_quantity'], 'double'],
];
}

/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'id_product' => 'Produit',
'id_user' => 'Utilisateur',
'id_point_sale' => 'Point de vente',
'id_user_group' => "Groupe d'utilisateur",
'price' => 'Prix (HT)',
'percent' => 'Pourcentage',
'from_quantity' => 'À partir de la quantité',
];
}

/*
* Relations
*/

public function getProduct()
{
return $this->hasOne(
Product::className(),
['id' => 'id_product']
);
}

public function getPointSale()
{
return $this->hasOne(
PointSale::className(),
['id' => 'id_point_sale']
);
}

public function getUserGroup()
{
return $this->hasOne(
UserGroup::className(),
['id' => 'id_user_group']
);
}

public function getUser()
{
return $this->hasOne(
User::className(),
['id' => 'id_user']
);
}

/**
* Retourne les options de base nécessaires à la fonction de recherche.
*
* @return array
*/
public static function defaultOptionsSearch()
{
return [
'with' => ['user', 'pointSale'],
'join_with' => ['product'],
'orderby' => '',
'attribute_id_producer' => 'product.id_producer'
];
}

public static function percentValues()
{
$percentValues = [
'' => 'Aucun'
];

for ($i = -50; $i < 51; $i = $i + 5) {
$percentValues[$i] = $i . ' %';
} }


/**
* @inheritdoc
*/
public function rules()
{
return [
['id_user', 'required', 'when' => function($model) {
return !$model->id_point_sale && !$model->id_user_group ;
}, 'message' => 'Vous devez renseigner au moins un utilisateur, un point de vente ou un groupe d\'utilisateur'],
['id_point_sale', 'required', 'when' => function($model) {
return !$model->id_user && !$model->id_user_group ;
}, 'message' => 'Vous devez renseigner au moins un utilisateur, un point de vente ou un groupe d\'utilisateur'],
['id_user_group', 'required', 'when' => function($model) {
return !$model->id_user && !$model->id_point_sale ;
}, 'message' => 'Vous devez renseigner au moins un utilisateur, un point de vente ou un groupe d\'utilisateur'],
[['id_product', 'price'], 'required'],
[['id_product', 'id_user', 'id_point_sale', 'id_user_group', 'percent'], 'integer'],
[['price', 'from_quantity'], 'double'],
];
}

/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'id_product' => 'Produit',
'id_user' => 'Utilisateur',
'id_point_sale' => 'Point de vente',
'id_user_group' => "Groupe d'utilisateur",
'price' => 'Prix (HT)',
'percent' => 'Pourcentage',
'from_quantity' => 'À partir de la quantité',
];
}

/*
* Relations
*/

public function getProduct()
{
return $this->hasOne(
Product::className(),
['id' => 'id_product']
);
}

public function getPointSale()
{
return $this->hasOne(
PointSale::className(),
['id' => 'id_point_sale']
);
}

public function getUserGroup()
{
return $this->hasOne(
UserGroup::className(),
['id' => 'id_user_group']
);
}

public function getUser()
{
return $this->hasOne(
User::className(),
['id' => 'id_user']
) ;
}

/**
* Retourne les options de base nécessaires à la fonction de recherche.
*
* @return array
*/
public static function defaultOptionsSearch()
{
return [
'with' => ['user', 'pointSale'],
'join_with' => ['product'],
'orderby' => '',
'attribute_id_producer' => 'product.id_producer'
];
}

public static function percentValues()
{
$percentValues = [
'' => 'Aucun'
] ;

for($i = -50 ; $i < 51 ; $i = $i + 5) {
$percentValues[$i] = $i.' %' ;
}

return $percentValues ;
return $percentValues;
}

public static function getPriorityMatchOfSpecificPriceArray($specificPriceArray, $user, $pointSale)
{
foreach($specificPriceArray as $specificPrice) {
if($specificPrice->matchUser($user, $pointSale)) {
return 'matchUser';
}
if($specificPrice->matchUserGroup($user, $pointSale)) {
return 'matchUserGroup';
}
if($specificPrice->matchPointSale($user, $pointSale)) {
return 'matchPointSale';
}
if($specificPrice->matchUserPointSale($user, $pointSale)) {
return 'matchUserPointSale';
}
} }


return null;
}

public function matchUser($user, $pointSale)
{
return $user
&& $this->id_user
&& !$this->id_point_sale
&& !$this->id_user_group
&& $this->id_user == $user->id;
}

public function matchUserGroup($user, $pointSale)
{
return $user
&& $this->id_user_group
&& !$this->id_point_sale
&& !$this->id_user
&& $user->belongsToUserGroup($this->id_user_group);
}

public function matchPointSale($user, $pointSale)
{
return $pointSale
&& $this->id_point_sale
&& !$this->id_user
&& !$this->id_user_group
&& $this->id_point_sale == $pointSale->id;
}

public function matchUserPointSale($user, $pointSale)
{
return $pointSale && $user
&& $this->id_point_sale
&& $this->id_user
&& $this->id_point_sale == $pointSale->id
&& $this->id_user == $user->id;
}
} }

+ 2
- 1
common/models/Subscription.php View File

$productOrder->quantity = $productSubscription->quantity; $productOrder->quantity = $productSubscription->quantity;
$productOrder->price = $productSubscription->product->getPrice([ $productOrder->price = $productSubscription->product->getPrice([
'user' => $user, 'user' => $user,
'point_sale' => $pointSale
'point_sale' => $pointSale,
'quantity' => $productSubscription->quantity
]); ]);
$productOrder->unit = $productSubscription->product->unit; $productOrder->unit = $productSubscription->product->unit;
$productOrder->step = $productSubscription->product->step; $productOrder->step = $productSubscription->product->step;

Loading…
Cancel
Save