You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.5KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Solver\Product;
  3. use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
  4. use Lc\CaracoleBundle\Model\Product\ProductInterface;
  5. class ProductSolver
  6. {
  7. // isProductAvailable
  8. public function isAvailable(ProductInterface $product, $quantityOrder = 0, $checkCart = false, $orderShop = null)
  9. {
  10. if ($product->getStatus() != 1 || $product->getProductFamily()->getStatus() != 1 || !$this->isProductSaleStatusOn($product)) {
  11. return false;
  12. }
  13. // @TODO : orderShop à définir où est appelé isProductAvailable
  14. if ($checkCart && !$orderShop) {
  15. throw new \Exception("Attention jeune padawan : définir le orderShop à l'endroit où est appelé isProductAvailable");
  16. }
  17. $productFamily = $product->getProductFamily();
  18. $quantityAsked = $quantityOrder;
  19. if ($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  20. if (!$quantityOrder) {
  21. $quantityAsked = $orderShop->getQuantityOrderByProduct($product, true);
  22. } else {
  23. $quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
  24. }
  25. if ($checkCart) {
  26. $quantityAsked += $orderShop->getQuantityOrderByProduct($product, true);
  27. }
  28. }
  29. if (($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
  30. || $productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
  31. if (!$quantityOrder) {
  32. $quantityAsked = $orderShop->getQuantityOrderByProduct($product);
  33. }
  34. if ($checkCart) {
  35. $quantityAsked += $orderShop->getQuantityOrderByProduct($product);
  36. }
  37. }
  38. if ($product->getAvailableQuantityInherited() >= $quantityAsked
  39. || $productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
  40. return true;
  41. } else {
  42. return false;
  43. }
  44. }
  45. public function isOneProductAvailableAddCart(array $products): bool
  46. {
  47. foreach ($products as $product) {
  48. if ($product->isAvailable(1, true)) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. public function isProductSaleStatusOn(ProductInterface $product)
  55. {
  56. if ($product->getProductFamily()->getSaleStatus() != 1) {
  57. return false;
  58. }
  59. $allCategoriesSalesOff = true;
  60. $unavailableSpecificDay = false;
  61. foreach ($product->getProductFamily()->getProductCategories() as $category) {
  62. if ($category->getParent()) {
  63. if ($category->getSaleStatus() && $category->getParent()->getSaleStatus()) {
  64. $allCategoriesSalesOff = false;
  65. }
  66. } else {
  67. if ($category->getSaleStatus()) {
  68. $allCategoriesSalesOff = false;
  69. }
  70. }
  71. // specific day
  72. // @TODO : spécifique pdl ?
  73. $displaySpecificDay = $category->getDisplaySpecificDay();
  74. if ($displaySpecificDay && $displaySpecificDay != date('N')) {
  75. $unavailableSpecificDay = true;
  76. }
  77. }
  78. if ($allCategoriesSalesOff) {
  79. return false;
  80. }
  81. if ($unavailableSpecificDay) {
  82. return false;
  83. }
  84. return true;
  85. }
  86. }