'active' => $product->productDistribution[0]->active | 'active' => $product->productDistribution[0]->active | ||||
&& (!$pointSale || $productModule->isAvailableOnPointSale($product, $pointSale)), | && (!$pointSale || $productModule->isAvailableOnPointSale($product, $pointSale)), | ||||
'invoice_price' => $invoicePrice, | 'invoice_price' => $invoicePrice, | ||||
//'quantity_remaining' => $orderModule->getResolver()->getProductQuantityRemaining($product, $distribution, $order, $quantityForm), | |||||
]; | ]; | ||||
} | } | ||||
// construction de $orderOverride | // construction de $orderOverride | ||||
$orderOverride = $order; | |||||
if(!$orderOverride) { | |||||
$orderOverride = new Order(); | |||||
} | |||||
$productOrderModule = $this->getProductOrderModule(); | |||||
$productOrderObjectArray = []; | |||||
foreach($productOrderArray as $idProduct => $productOrder) { | |||||
if($productOrder['quantity']) { | |||||
$product = $productModule->getRepository()->findOneProductById($idProduct); | |||||
$productOrderObject = $productOrderModule->getBuilder()->instanciateProductOrder( | |||||
$orderOverride, | |||||
$product, | |||||
$productOrder['quantity'], | |||||
0 | |||||
); | |||||
$productOrderObjectArray[] = $productOrderObject; | |||||
} | |||||
} | |||||
$orderOverride->populateRelation('productOrder', $productOrderObjectArray); | |||||
$orderOverride = $orderModule->getBuilder()->instanciateOrderFromProductOrdersArray($productOrderArray, $order); | |||||
foreach($productOrderArray as $idProduct => $productOrder) { | foreach($productOrderArray as $idProduct => $productOrder) { | ||||
$product = $productModule->getRepository()->findOneProductById($idProduct); | $product = $productModule->getRepository()->findOneProductById($idProduct); | ||||
$productOrderArray[$idProduct]['quantity_remaining'] = $orderModule->getResolver()->getProductQuantityRemaining($product, $distribution, $orderOverride); | $productOrderArray[$idProduct]['quantity_remaining'] = $orderModule->getResolver()->getProductQuantityRemaining($product, $distribution, $orderOverride); |
use domain\Producer\Producer\Producer; | use domain\Producer\Producer\Producer; | ||||
use domain\Producer\Producer\ProducerRepository; | use domain\Producer\Producer\ProducerRepository; | ||||
use domain\Producer\Producer\ProducerSolver; | use domain\Producer\Producer\ProducerSolver; | ||||
use domain\Product\Product\ProductRepository; | |||||
use domain\Product\Product\ProductSolver; | use domain\Product\Product\ProductSolver; | ||||
use domain\Subscription\Subscription\Subscription; | use domain\Subscription\Subscription\Subscription; | ||||
use domain\Subscription\Subscription\SubscriptionBuilder; | use domain\Subscription\Subscription\SubscriptionBuilder; | ||||
protected DocumentSolver $documentSolver; | protected DocumentSolver $documentSolver; | ||||
protected ProducerSolver $producerSolver; | protected ProducerSolver $producerSolver; | ||||
protected OrderStatusRepository $orderStatusRepository; | protected OrderStatusRepository $orderStatusRepository; | ||||
protected ProductRepository $productRepository; | |||||
public function loadDependencies(): void | public function loadDependencies(): void | ||||
{ | { | ||||
$this->documentSolver = $this->loadService(DocumentSolver::class); | $this->documentSolver = $this->loadService(DocumentSolver::class); | ||||
$this->producerSolver = $this->loadService(ProducerSolver::class); | $this->producerSolver = $this->loadService(ProducerSolver::class); | ||||
$this->orderStatusRepository = $this->loadService(OrderStatusRepository::class); | $this->orderStatusRepository = $this->loadService(OrderStatusRepository::class); | ||||
$this->productRepository = $this->loadService(ProductRepository::class); | |||||
} | } | ||||
public function instanciateOrder(Distribution $distribution): Order | public function instanciateOrder(Distribution $distribution): Order | ||||
return $order; | return $order; | ||||
} | } | ||||
public function instanciateOrderFromProductOrdersArray(array $productOrdersArray, Order $order = null): Order | |||||
{ | |||||
if(!$order) { | |||||
$order = new Order(); | |||||
$this->initOrderStatus($order); | |||||
} | |||||
$productOrderObjectArray = []; | |||||
foreach($productOrdersArray as $idProduct => $productOrder) { | |||||
if($productOrder['quantity']) { | |||||
$product = $this->productRepository->findOneProductById($idProduct); | |||||
$productOrderObject = $this->productOrderBuilder->instanciateProductOrder( | |||||
$order, | |||||
$product, | |||||
$productOrder['quantity'], | |||||
0 | |||||
); | |||||
$productOrderObjectArray[] = $productOrderObject; | |||||
} | |||||
} | |||||
$order->populateRelation('productOrder', $productOrderObjectArray); | |||||
return $order; | |||||
} | |||||
public function addOrUpdateOrderFromArray(Order $orderOverride, array $ordersArray): array | |||||
{ | |||||
$override = false; | |||||
foreach($ordersArray as $key => $order) { | |||||
if($order->id == $orderOverride->id) { | |||||
$ordersArray[$key] = $orderOverride; | |||||
$override = true; | |||||
} | |||||
} | |||||
if(!$override) { | |||||
$ordersArray[] = $orderOverride; | |||||
} | |||||
return $ordersArray; | |||||
} | |||||
public function initDateUpdate(Order $order) | public function initDateUpdate(Order $order) | ||||
{ | { | ||||
$order->date_update = date('Y-m-d H:i:s');; | $order->date_update = date('Y-m-d H:i:s');; |
protected OrderRepository $orderRepository; | protected OrderRepository $orderRepository; | ||||
protected ProductDistributionRepository $productDistributionRepository; | protected ProductDistributionRepository $productDistributionRepository; | ||||
protected FeatureChecker $featureChecker; | protected FeatureChecker $featureChecker; | ||||
protected OrderBuilder $orderBuilder; | |||||
protected array $ordersArrayCachedByDistribution = []; | protected array $ordersArrayCachedByDistribution = []; | ||||
$this->orderRepository = $this->loadService(OrderRepository::class); | $this->orderRepository = $this->loadService(OrderRepository::class); | ||||
$this->productDistributionRepository = $this->loadService(ProductDistributionRepository::class); | $this->productDistributionRepository = $this->loadService(ProductDistributionRepository::class); | ||||
$this->featureChecker = $this->loadService(FeatureChecker::class); | $this->featureChecker = $this->loadService(FeatureChecker::class); | ||||
$this->orderBuilder = $this->loadService(OrderBuilder::class); | |||||
} | } | ||||
public function getProductQuantityByDistribution( | public function getProductQuantityByDistribution( | ||||
$this->ordersArrayCachedByDistribution[$distribution->id] = $this->orderRepository->findOrdersByDistribution($distribution); | $this->ordersArrayCachedByDistribution[$distribution->id] = $this->orderRepository->findOrdersByDistribution($distribution); | ||||
} | } | ||||
$ordersArray = $this->ordersArrayCachedByDistribution[$distribution->id]; | $ordersArray = $this->ordersArrayCachedByDistribution[$distribution->id]; | ||||
// @TODO : sortir cette logique de la méthode (refactoring) | |||||
if($orderOverride) { | if($orderOverride) { | ||||
$override = false; | |||||
foreach($ordersArray as $key => $order) { | |||||
if($order->id == $orderOverride->id) { | |||||
$ordersArray[$key] = $orderOverride; | |||||
$override = true; | |||||
} | |||||
} | |||||
if(!$override) { | |||||
$ordersArray[] = $orderOverride; | |||||
} | |||||
$ordersArray = $this->orderBuilder->addOrUpdateOrderFromArray($orderOverride, $ordersArray); | |||||
} | } | ||||
$productQuantity = $this->orderSolver->getProductQuantity($product, $ordersArray); | $productQuantity = $this->orderSolver->getProductQuantity($product, $ordersArray); | ||||
if($inNumberOfPieces) { | if($inNumberOfPieces) { | ||||
$productQuantity = $this->productSolver->getNumberOfPieces($productQuantity, $product); | $productQuantity = $this->productSolver->getNumberOfPieces($productQuantity, $product); | ||||
} | } |
return 0; | return 0; | ||||
} | } | ||||
public function actionAjaxInfos(string $date = '', int $pointSaleId = 0) | |||||
public function actionAjaxInfos(string $date = '', int $pointSaleId = 0, string $productsJson = null) | |||||
{ | { | ||||
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; | ||||
$productsArray = json_decode($productsJson, true); | |||||
$user = GlobalParam::getCurrentUser(); | $user = GlobalParam::getCurrentUser(); | ||||
$producer = $this->getProducerCurrent(); | $producer = $this->getProducerCurrent(); | ||||
$pointSale = $this->getPointSaleModule()->findOnePointSaleById($pointSaleId); | $pointSale = $this->getPointSaleModule()->findOnePointSaleById($pointSaleId); | ||||
$order = $this->getOrderUser($date, $pointSale); | $order = $this->getOrderUser($date, $pointSale); | ||||
return $this->buildJsonAjaxInfos($date, $producer, $pointSale, $user, $order); | |||||
return $this->buildJsonAjaxInfos($date, $producer, $pointSale, $user, $order, $productsArray); | |||||
} | } | ||||
public function buildJsonAjaxInfos( | public function buildJsonAjaxInfos( | ||||
Producer $producer, | Producer $producer, | ||||
PointSale $pointSale = null, | PointSale $pointSale = null, | ||||
User $user = null, | User $user = null, | ||||
Order $order = null | |||||
Order $order = null, | |||||
array $productsArray = null | |||||
) | ) | ||||
{ | { | ||||
$orderModule = $this->getOrderModule(); | $orderModule = $this->getOrderModule(); | ||||
$json['distribution'] = $distribution; | $json['distribution'] = $distribution; | ||||
$json['points_sale'] = $this->ajaxInfosPointsSale($producer, $distribution); | $json['points_sale'] = $this->ajaxInfosPointsSale($producer, $distribution); | ||||
$json['categories'] = $this->ajaxInfosProductCategories($producer); | $json['categories'] = $this->ajaxInfosProductCategories($producer); | ||||
$json['products'] = $this->ajaxInfosProducts($producer, $distribution, $pointSale, $user, $order); | |||||
$json['products'] = $this->ajaxInfosProducts($producer, $distribution, $pointSale, $user, $order, $productsArray); | |||||
if ($order) { | if ($order) { | ||||
$json['order'] = array_merge($order->getAttributes(), [ | $json['order'] = array_merge($order->getAttributes(), [ | ||||
Distribution $distribution, | Distribution $distribution, | ||||
PointSale $pointSale = null, | PointSale $pointSale = null, | ||||
User $user = null, | User $user = null, | ||||
Order $order = null | |||||
Order $order = null, | |||||
array $productsFormArray = null | |||||
) | ) | ||||
{ | { | ||||
$unitModule = $this->getUnitModule(); | $unitModule = $this->getUnitModule(); | ||||
$product['photo'] = Image::getThumbnailSmall($product['photo']); | $product['photo'] = Image::getThumbnailSmall($product['photo']); | ||||
} | } | ||||
//$product['quantity_max'] = (isset($product['productDistribution']) && isset($product['productDistribution'][0])) ? $product['productDistribution'][0]['quantity_max'] : null; | |||||
$product['quantity_max'] = $orderModule->getResolver()->getProductQuantityMax($productObject, $distribution); | |||||
$quantityOrder = $orderModule->getSolver()->getProductQuantity($productObject, $ordersArray); | |||||
$quantityOrder = $orderModule->getResolver()->getProductQuantityByDistribution($productObject, $distribution); | |||||
$product['quantity_ordered'] = $quantityOrder; | $product['quantity_ordered'] = $quantityOrder; | ||||
$product['quantity_remaining'] = $product['quantity_max'] - $quantityOrder; | |||||
$product['quantity_max'] = $orderModule->getResolver()->getProductQuantityMax($productObject, $distribution); | |||||
$product['quantity_remaining'] = $orderModule->getResolver()->getProductQuantityRemaining($productObject, $distribution); | |||||
$product['wording_unit'] = $unitModule->getSolver()->strUnit($product['unit'], UnitDefinition::WORDING_UNIT, true); | $product['wording_unit'] = $unitModule->getSolver()->strUnit($product['unit'], UnitDefinition::WORDING_UNIT, true); | ||||
$product['wording_unit_ref'] = $unitModule->getSolver()->strUnit($product['unit'], UnitDefinition::WORDING_SHORT, true); | $product['wording_unit_ref'] = $unitModule->getSolver()->strUnit($product['unit'], UnitDefinition::WORDING_SHORT, true); | ||||
if ($order) { | if ($order) { | ||||
$quantityOrderUser = $orderModule->getSolver()->getProductQuantity($productObject, [$order], true); | |||||
$orderOverride = $order; | |||||
if(count($productsFormArray)) { | |||||
$productOrdersArray = []; | |||||
foreach($productsFormArray as $idProduct => $quantityProduct) { | |||||
if($idProduct) { | |||||
$productObject1 = $productModule->getRepository()->findOneProductById($idProduct); | |||||
$productOrdersArray[$idProduct] = [ | |||||
'quantity' => $quantityProduct / $productModule->getSolver()->getUnitCoefficient($productObject1) | |||||
]; | |||||
} | |||||
} | |||||
$orderOverride = $orderModule->getBuilder()->instanciateOrderFromProductOrdersArray($productOrdersArray, $orderOverride); | |||||
} | |||||
$quantityOrderUser = $orderModule->getSolver()->getProductQuantity($productObject, [$orderOverride], true); | |||||
$product['quantity_ordered'] = $quantityOrder; | $product['quantity_ordered'] = $quantityOrder; | ||||
$product['quantity_remaining'] = $product['quantity_max'] - $quantityOrder + $quantityOrderUser; | |||||
$product['quantity_remaining'] = $orderModule->getResolver()->getProductQuantityRemaining($productObject, $distribution, $orderOverride); | |||||
$product['quantity_form'] = $quantityOrderUser * $coefficient_unit; | $product['quantity_form'] = $quantityOrderUser * $coefficient_unit; | ||||
foreach ($order->productOrder as $productOrder) { | foreach ($order->productOrder as $productOrder) { | ||||
if ($productOrder->id_product == $product['id']) { | if ($productOrder->id_product == $product['id']) { |
<span v-if="product.weight">({{ product.weight }} g)</span> | <span v-if="product.weight">({{ product.weight }} g)</span> | ||||
</span> | </span> | ||||
<div> | <div> | ||||
<span v-if="product.quantity_max > 0 && ((product.quantity_form / product.coefficient_unit == product.quantity_remaining) || ((product.quantity_remaining * product.coefficient_unit) - product.quantity_form) < product.step)" | |||||
<span v-if="product.quantity_max > 0 && (product.quantity_remaining == 0 || product.quantity_remaining * product.coefficient_unit < product.step)" | |||||
class="badge bg-danger">Épuisé</span> | class="badge bg-danger">Épuisé</span> | ||||
</div> | </div> | ||||
<div class="description" v-if="product.description.length"> | <div class="description" v-if="product.description.length"> | ||||
<button class="btn btn-secondary btn-moins" | <button class="btn btn-secondary btn-moins" | ||||
type="button" | type="button" | ||||
@click="productQuantityClick(product, product.unit == 'piece' ? -1 : -parseFloat(product.step))" | @click="productQuantityClick(product, product.unit == 'piece' ? -1 : -parseFloat(product.step))" | ||||
:disabled="product.quantity_form == 0"> | |||||
:disabled="product.quantity_form == 0 || loadingProducts"> | |||||
<i class="bi bi-dash-lg"></i></button> | <i class="bi bi-dash-lg"></i></button> | ||||
</span> | </span> | ||||
<input type="text" v-model="product.quantity_form" | <input type="text" v-model="product.quantity_form" | ||||
<button class="btn btn-secondary btn-plus" | <button class="btn btn-secondary btn-plus" | ||||
type="button" | type="button" | ||||
@click="productQuantityClick(product, product.unit == 'piece' ? 1 : parseFloat(product.step))" | @click="productQuantityClick(product, product.unit == 'piece' ? 1 : parseFloat(product.step))" | ||||
:disabled="(product.quantity_form / product.coefficient_unit) == product.quantity_remaining && product.quantity_max > 0"> | |||||
:disabled="product.quantity_remaining == 0 || loadingProducts"> | |||||
<i class="bi bi-plus-lg"></i></button> | <i class="bi bi-plus-lg"></i></button> | ||||
</span> | </span> | ||||
</div> | </div> |
return Object.assign({ | return Object.assign({ | ||||
order: null, | order: null, | ||||
loading: false, | loading: false, | ||||
loadingProducts: false, | |||||
loadingInit: true, | loadingInit: true, | ||||
step: null, | step: null, | ||||
producer: null, | producer: null, | ||||
} | } | ||||
} | } | ||||
}, | }, | ||||
init: function(type, oldStep, step) { | |||||
init: function(type, loadingProducts) { | |||||
var app = this ; | var app = this ; | ||||
this.loading = true ; | |||||
if(loadingProducts) { | |||||
this.loadingProducts = true ; | |||||
} | |||||
else { | |||||
this.loading = true ; | |||||
} | |||||
if(app.isChangeState('date', 'date', 'point-sale')) { | if(app.isChangeState('date', 'date', 'point-sale')) { | ||||
app.pointSaleActive = null ; | app.pointSaleActive = null ; | ||||
axios.get("ajax-infos",{params: { | axios.get("ajax-infos",{params: { | ||||
date : this.getDate(), | date : this.getDate(), | ||||
pointSaleId: this.pointSaleActiveId ? this.pointSaleActiveId : (this.pointSaleActive ? this.pointSaleActive.id : 0) | |||||
pointSaleId: this.pointSaleActiveId ? this.pointSaleActiveId : (this.pointSaleActive ? this.pointSaleActive.id : 0), | |||||
productsJson: this.getProductsArray() | |||||
}}) | }}) | ||||
.then(function(response) { | .then(function(response) { | ||||
app.calendar.attrs = []; | app.calendar.attrs = []; | ||||
}, 500); | }, 500); | ||||
app.loading = false ; | app.loading = false ; | ||||
app.loadingProducts = false ; | |||||
app.loadingInit = false ; | app.loadingInit = false ; | ||||
}); | }); | ||||
}, | }, | ||||
} | } | ||||
}, | }, | ||||
changeStep: function(step) { | changeStep: function(step) { | ||||
this.errors = [] ; | this.errors = [] ; | ||||
var oldStep = this.step ; | var oldStep = this.step ; | ||||
this.nextStep() ; | this.nextStep() ; | ||||
}, | }, | ||||
productQuantityClick: function(product, quantity) { | productQuantityClick: function(product, quantity) { | ||||
if( this.products[product.index].quantity_form + quantity >= 0 && | |||||
(this.products[product.index].quantity_form + quantity <= (this.products[product.index].quantity_remaining * this.products[product.index].coefficient_unit) || | |||||
!this.products[product.index].quantity_max) | |||||
if(this.products[product.index].quantity_form + quantity >= 0 | |||||
&& (quantity <= (this.products[product.index].quantity_remaining * this.products[product.index].coefficient_unit) | |||||
|| this.products[product.index].quantity_remaining == null) | |||||
) { | ) { | ||||
var theQuantity = parseFloat(this.products[product.index].quantity_form) + parseFloat(quantity); | var theQuantity = parseFloat(this.products[product.index].quantity_form) + parseFloat(quantity); | ||||
this.products[product.index].quantity_form = parseFloat(theQuantity.toFixed(2)) ; | this.products[product.index].quantity_form = parseFloat(theQuantity.toFixed(2)) ; | ||||
this.init('first', true); | |||||
} | } | ||||
}, | }, | ||||
oneProductOrdered: function() { | oneProductOrdered: function() { | ||||
} | } | ||||
// products | // products | ||||
var productsArray = {} ; | |||||
for(var key in this.products) { | |||||
if( this.products[key].quantity_form != null && | |||||
this.products[key].quantity_form > 0) { | |||||
productsArray[this.products[key].id] = this.products[key].quantity_form ; | |||||
} | |||||
} | |||||
var productsArray = this.getProductsArray(); | |||||
app.disableConfirmButton = true ; | app.disableConfirmButton = true ; | ||||
axios.post('ajax-process', { | axios.post('ajax-process', { | ||||
} | } | ||||
}); | }); | ||||
}, | }, | ||||
getProductsArray: function() { | |||||
var productsArray = {} ; | |||||
for(var key in this.products) { | |||||
if( this.products[key].quantity_form != null && | |||||
this.products[key].quantity_form > 0) { | |||||
productsArray[this.products[key].id] = this.products[key].quantity_form ; | |||||
} | |||||
} | |||||
return productsArray; | |||||
}, | |||||
checkProducts: function() { | checkProducts: function() { | ||||
if(!this.oneProductOrdered()) { | if(!this.oneProductOrdered()) { | ||||
this.errors.push('Veuillez sélectionner au moins un produit.') ; | this.errors.push('Veuillez sélectionner au moins un produit.') ; |