Просмотр исходного кода

[Backend] Prix dégressifs : ajustement maj des prix lors de l'ajout / édition d'une commande #261

refactoring
Guillaume 2 лет назад
Родитель
Сommit
555ff8518e
3 измененных файлов: 75 добавлений и 52 удалений
  1. +29
    -32
      backend/web/js/vuejs/distribution-index.js
  2. +24
    -8
      common/models/Product.php
  3. +22
    -12
      common/models/ProductPrice.php

+ 29
- 32
backend/web/js/vuejs/distribution-index.js Просмотреть файл

@@ -602,6 +602,7 @@ var app = new Vue({
Vue.set(app.orderCreate.productOrder[idProduct], 'prices', response.data[idProduct].prices);
Vue.set(app.orderCreate.productOrder[idProduct], 'active', response.data[idProduct].active);
Vue.set(app.orderCreate.productOrder[idProduct], 'unit_coefficient', response.data[idProduct].unit_coefficient);
Vue.set(app.orderCreate.productOrder[idProduct], 'price', app.getBestProductPrice(app.orderCreate, idProduct, app.orderCreate.productOrder[idProduct].quantity));
}

if (app.showModalFormOrderUpdate && app.idOrderUpdate) {
@@ -619,7 +620,31 @@ var app = new Vue({
app.loadingUpdateProductOrder = false;
});
}
}
},
getBestProductPrice: function(order, idProduct, theQuantity) {
var thePriceWithTax = 9999;
var pricesArray = order.productOrder[idProduct].prices;
var unitCoefficient = order.productOrder[idProduct].unit_coefficient;
if(theQuantity) {
theQuantity = theQuantity / unitCoefficient;
}

for(var i = 0; i < pricesArray.length ; i++) {
var priceWithTax = pricesArray[i].price_with_tax;
var fromQuantity = pricesArray[i].from_quantity;

if(priceWithTax < thePriceWithTax && fromQuantity <= theQuantity) {
thePriceWithTax = priceWithTax;
}
}

if(thePriceWithTax == 9999) {
return 0;
}
else {
return thePriceWithTax;
}
},
},
});

@@ -629,6 +654,7 @@ Vue.component('modal', {

Vue.component('order-form',{
props: ['date', 'pointsSale','meansPayment', 'users', 'products', 'order', 'producer', 'loadingUpdateProductOrder'],
emits: ['updateProductPrice'],
data: function() {
return {
errors: [],
@@ -736,37 +762,8 @@ Vue.component('order-form',{
theQuantity = parseInt(theQuantity);
}

Vue.set(this.order.productOrder, id_product, {
quantity: theQuantity,
unit: this.order.productOrder[id_product].unit,
unit_coefficient: this.order.productOrder[id_product].unit_coefficient,
prices: this.order.productOrder[id_product].prices,
price: this.getBestProductPrice(id_product, theQuantity),
active: this.order.productOrder[id_product].active
});
}
},
getBestProductPrice: function(id_product, theQuantity) {
var thePriceWithTax = 9999;
var pricesArray = this.order.productOrder[id_product].prices;
var unitCoefficient = this.order.productOrder[id_product].unit_coefficient;
if(theQuantity) {
theQuantity = theQuantity / unitCoefficient;
}

for(var i = 0; i < pricesArray.length ; i++) {
var priceWithTax = pricesArray[i].price_with_tax;
var fromQuantity = pricesArray[i].from_quantity;

if(priceWithTax < thePriceWithTax && fromQuantity <= theQuantity) {
thePriceWithTax = priceWithTax;
}
}
if(thePriceWithTax == 9999) {
return 0;
}
else {
return thePriceWithTax;
Vue.set(this.order.productOrder[id_product], 'quantity', theQuantity);
Vue.set(this.order.productOrder[id_product], 'price', app.getBestProductPrice(this.order, id_product, theQuantity));
}
},
productPriceChange: function(event) {

+ 24
- 8
common/models/Product.php Просмотреть файл

@@ -392,9 +392,12 @@ class Product extends ActiveRecordCommon
{
$priceArray = [];

$userProducer = UserProducer::searchOne([
'id_user' => $user->id,
]);
$userProducer = null;
if($user) {
$userProducer = UserProducer::searchOne([
'id_user' => $user->id,
]);
}

// specific prices
$specificPriceArray = $this->getSpecificPricesFilterByPriorityMatch(
@@ -415,11 +418,13 @@ class Product extends ActiveRecordCommon
];
}

// base price
$priceArray[] = [
'from_quantity' => 0,
'price_with_tax' => $this->getPriceWithTax(),
];
if(!$this->hasPriceWithQuantityZero($priceArray)) {
// base price
$priceArray[] = [
'from_quantity' => 0,
'price_with_tax' => $this->getPriceWithTax(),
];
}

usort($priceArray, function($a, $b) {
if($a['price_with_tax'] < $b['price_with_tax']) {
@@ -436,6 +441,17 @@ class Product extends ActiveRecordCommon
return $priceArray;
}

public function hasPriceWithQuantityZero($priceArray)
{
foreach($priceArray as $price) {
if($price['from_quantity'] == 0) {
return true;
}
}

return false;
}

public function getSpecificPricesFilterByPriorityMatch($specificPrices, $user, $pointSale)
{
$priorityMatchSpecificPrice = ProductPrice::getPriorityMatchOfSpecificPriceArray($specificPrices, $user, $pointSale);

+ 22
- 12
common/models/ProductPrice.php Просмотреть файл

@@ -178,23 +178,33 @@ class ProductPrice extends ActiveRecordCommon
return $percentValues;
}

public static function getPriorityMatchOfSpecificPriceArray($specificPriceArray, $user, $pointSale)
public static function hasMatchOfType($specificPriceArray, $typeMatch, $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';
if($specificPrice->$typeMatch($user, $pointSale)) {
return true;
}
}

return false;
}

public static function getPriorityMatchOfSpecificPriceArray($specificPriceArray, $user, $pointSale)
{
if(self::hasMatchOfType($specificPriceArray, 'matchUser', $user, $pointSale)) {
return 'matchUser';
}
if(self::hasMatchOfType($specificPriceArray, 'matchUserGroup', $user, $pointSale)) {
return 'matchUserGroup';
}
if(self::hasMatchOfType($specificPriceArray, 'matchPointSale', $user, $pointSale)) {
return 'matchPointSale';
}

if(self::hasMatchOfType($specificPriceArray, 'matchUserPointSale', $user, $pointSale)) {
return 'matchUserPointSale';
}

return null;
}


Загрузка…
Отмена
Сохранить