Browse Source

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

refactoring
Guillaume 2 years ago
parent
commit
555ff8518e
3 changed files with 75 additions and 52 deletions
  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 View File

Vue.set(app.orderCreate.productOrder[idProduct], 'prices', response.data[idProduct].prices); 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], 'active', response.data[idProduct].active);
Vue.set(app.orderCreate.productOrder[idProduct], 'unit_coefficient', response.data[idProduct].unit_coefficient); 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) { if (app.showModalFormOrderUpdate && app.idOrderUpdate) {
app.loadingUpdateProductOrder = false; 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;
}
},
}, },
}); });




Vue.component('order-form',{ Vue.component('order-form',{
props: ['date', 'pointsSale','meansPayment', 'users', 'products', 'order', 'producer', 'loadingUpdateProductOrder'], props: ['date', 'pointsSale','meansPayment', 'users', 'products', 'order', 'producer', 'loadingUpdateProductOrder'],
emits: ['updateProductPrice'],
data: function() { data: function() {
return { return {
errors: [], errors: [],
theQuantity = parseInt(theQuantity); 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) { productPriceChange: function(event) {

+ 24
- 8
common/models/Product.php View File

{ {
$priceArray = []; $priceArray = [];


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


// specific prices // specific prices
$specificPriceArray = $this->getSpecificPricesFilterByPriorityMatch( $specificPriceArray = $this->getSpecificPricesFilterByPriorityMatch(
]; ];
} }


// 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) { usort($priceArray, function($a, $b) {
if($a['price_with_tax'] < $b['price_with_tax']) { if($a['price_with_tax'] < $b['price_with_tax']) {
return $priceArray; 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) public function getSpecificPricesFilterByPriorityMatch($specificPrices, $user, $pointSale)
{ {
$priorityMatchSpecificPrice = ProductPrice::getPriorityMatchOfSpecificPriceArray($specificPrices, $user, $pointSale); $priorityMatchSpecificPrice = ProductPrice::getPriorityMatchOfSpecificPriceArray($specificPrices, $user, $pointSale);

+ 22
- 12
common/models/ProductPrice.php View File

return $percentValues; return $percentValues;
} }


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



Loading…
Cancel
Save