$distribution = Distribution::findOne($idDistribution); | $distribution = Distribution::findOne($idDistribution); | ||||
$user = User::findOne($idUser); | $user = User::findOne($idUser); | ||||
$userProducer = UserProducer::searchOne([ | |||||
'id_user' => $idUser, | |||||
]); | |||||
$pointSale = PointSale::findOne($idPointSale); | $pointSale = PointSale::findOne($idPointSale); | ||||
$productsArray = Product::find() | $productsArray = Product::find() | ||||
$productOrderArray = []; | $productOrderArray = []; | ||||
foreach ($productsArray as $product) { | foreach ($productsArray as $product) { | ||||
$priceArray = []; | |||||
// specific prices | |||||
$specificPriceArray = $product->getSpecificPricesFilterByPriorityMatch( | |||||
$product->productPrice, | |||||
$user, | |||||
$pointSale | |||||
); | |||||
foreach ($specificPriceArray as $specificPrice) { | |||||
$priceArray[] = [ | |||||
'from_quantity' => $specificPrice->from_quantity ? $specificPrice->from_quantity : 0, | |||||
'price_with_tax' => $product->getPriceWithTax([ | |||||
'user' => $user, | |||||
'user_producer' => $userProducer, | |||||
'point_sale' => $pointSale, | |||||
'quantity' => $specificPrice->from_quantity | |||||
]), | |||||
]; | |||||
} | |||||
// base price | |||||
$priceArray[] = [ | |||||
'from_quantity' => 0, | |||||
'price_with_tax' => $product->getPriceWithTax(), | |||||
]; | |||||
$priceArray = $product->getPriceArray($user, $pointSale); | |||||
$productOrderArray[$product['id']] = [ | $productOrderArray[$product['id']] = [ | ||||
'quantity' => 0, | 'quantity' => 0, |
'id', | 'id', | ||||
function ($product) use ($document, $userProducer, $pointSale) { | function ($product) use ($document, $userProducer, $pointSale) { | ||||
return array_merge($product->getAttributes(), [ | return array_merge($product->getAttributes(), [ | ||||
'price_with_tax' => $product->getPriceWithTax([ | |||||
'user' => $document->user, | |||||
'user_producer' => $userProducer, | |||||
'point_sale' => $pointSale, | |||||
]), | |||||
'price' => $product->getPrice([ | |||||
'user' => $document->user, | |||||
'user_producer' => $userProducer, | |||||
'point_sale' => $pointSale, | |||||
]), | |||||
'unit_coefficient' => Product::$unitsArray[$product->unit]['coefficient'], | |||||
'prices' => $product->getPriceArray($userProducer->user, $pointSale), | |||||
'wording_unit' => $product->wording_unit, | 'wording_unit' => $product->wording_unit, | ||||
'tax_rate' => $product->taxRate->value | 'tax_rate' => $product->taxRate->value | ||||
]); | ]); |
changeProductAdd: function(event) { | changeProductAdd: function(event) { | ||||
var idProduct = event.currentTarget.value ; | var idProduct = event.currentTarget.value ; | ||||
this.productAddId = idProduct ; | this.productAddId = idProduct ; | ||||
//this.productAddPrice = parseFloat(this.productsArray[idProduct].price).toFixed(2) ; | |||||
this.productAddPrice = parseFloat(this.productsArray[idProduct].price); | |||||
this.productAddPrice = this.getBestProductPrice(idProduct, this.getStepProductAdd()); | |||||
this.productAddQuantity = this.getStepProductAdd() ; | this.productAddQuantity = this.getStepProductAdd() ; | ||||
}, | |||||
}, | |||||
getBestProductPrice: function(idProduct, theQuantity) { | |||||
var thePriceWithTax = 9999; | |||||
var pricesArray = this.productsArray[idProduct].prices; | |||||
var unitCoefficient = this.productsArray[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; | |||||
} | |||||
}, | |||||
changeQuantityProductAdd: function(quantity) { | changeQuantityProductAdd: function(quantity) { | ||||
var step = this.getStepProductAdd() ; | var step = this.getStepProductAdd() ; | ||||
quantity = quantity * step ; | quantity = quantity * step ; | ||||
if(this.productAddQuantity < 1) { | if(this.productAddQuantity < 1) { | ||||
this.productAddQuantity = step ; | this.productAddQuantity = step ; | ||||
} | } | ||||
this.productAddPrice = this.getBestProductPrice(app.productAddId, this.productAddQuantity); | |||||
}, | }, | ||||
submitProductAdd: function() { | submitProductAdd: function() { | ||||
var app = this ; | var app = this ; |
return $strUnit; | return $strUnit; | ||||
} | } | ||||
public function getPriceArray($user, $pointSale) | |||||
{ | |||||
$priceArray = []; | |||||
$userProducer = UserProducer::searchOne([ | |||||
'id_user' => $user->id, | |||||
]); | |||||
// specific prices | |||||
$specificPriceArray = $this->getSpecificPricesFilterByPriorityMatch( | |||||
$this->productPrice, | |||||
$user, | |||||
$pointSale | |||||
); | |||||
foreach ($specificPriceArray as $specificPrice) { | |||||
$priceArray[] = [ | |||||
'from_quantity' => $specificPrice->from_quantity ? $specificPrice->from_quantity : 0, | |||||
'price_with_tax' => $this->getPriceWithTax([ | |||||
'user' => $user, | |||||
'user_producer' => $userProducer, | |||||
'point_sale' => $pointSale, | |||||
'quantity' => $specificPrice->from_quantity | |||||
]), | |||||
]; | |||||
} | |||||
// base price | |||||
$priceArray[] = [ | |||||
'from_quantity' => 0, | |||||
'price_with_tax' => $this->getPriceWithTax(), | |||||
]; | |||||
return $priceArray; | |||||
} | |||||
public function getSpecificPricesFilterByPriorityMatch($specificPrices, $user, $pointSale) | public function getSpecificPricesFilterByPriorityMatch($specificPrices, $user, $pointSale) | ||||
{ | { | ||||
$priorityMatchSpecificPrice = ProductPrice::getPriorityMatchOfSpecificPriceArray($specificPrices, $user, $pointSale); | $priorityMatchSpecificPrice = ProductPrice::getPriorityMatchOfSpecificPriceArray($specificPrices, $user, $pointSale); |