protected function execute(InputInterface $input, OutputInterface $output)
 {
     $orderfilename = $input->getArgument('orderfilename');
     // $discountfilename = $input->getArgument('discountfilename');
     $output->writeln('Loading order: ' . $orderfilename);
     // $order = new \BusinessComponents\Order\Order();
     $l = new JsonOrderLoader($orderfilename);
     $order = $l->load();
     //$order->clearAdjustments();
     // -----------------------------
     $discount = new Discount();
     $discount->setName('10% off on 3 food items for orders from France');
     $discount->setAction('LINE-PERCENTAGE');
     $rule = new Rule();
     $rule->setScope('line');
     $rule->setVariable('category.food');
     $rule->setComparison('equals');
     $rule->setValue('true');
     $discount->addRule($rule);
     $rule = new Rule();
     $rule->setScope('line');
     $rule->setVariable('undefined');
     $rule->setComparison('not-equals');
     $rule->setValue('true');
     $discount->addRule($rule);
     $rule = new Rule();
     $rule->setScope('subject');
     $rule->setVariable('shipcountry');
     $rule->setComparison('equals');
     $rule->setValue('FRA');
     $discount->addRule($rule);
     $quantitybreak = new QuantityBreak();
     $quantitybreak->setQuantity(1);
     $quantitybreak->setActionParameter(0);
     $discount->addQuantityBreak($quantitybreak);
     $quantitybreak = new QuantityBreak();
     $quantitybreak->setQuantity(3);
     $quantitybreak->setActionParameter(10);
     $discount->addQuantityBreak($quantitybreak);
     $discounter = new Discounter();
     $discounter->generateAdjustments($order, $discount);
     // -----------------------------
     $discount = new Discount();
     $discount->setName('2.50 off on green products');
     $discount->setAction('LINE-FIXED');
     $rule = new Rule();
     $rule->setScope('line');
     $rule->setVariable('color');
     $rule->setComparison('equals');
     $rule->setValue('green');
     $discount->addRule($rule);
     $quantitybreak = new QuantityBreak();
     $quantitybreak->setQuantity(1);
     $quantitybreak->setActionParameter(2.5);
     $discount->addQuantityBreak($quantitybreak);
     $discounter = new Discounter();
     $discounter->generateAdjustments($order, $discount);
     // -----------------------------
     $this->dumpOrder($order);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $productfilename = $input->getArgument('productfilename');
     // $discountfilename = $input->getArgument('discountfilename');
     $output->writeln('Loading product: ' . $productfilename);
     // $order = new \BusinessComponents\Order\Order();
     $l = new JsonProductLoader($productfilename);
     $product = $l->load();
     //$order->clearAdjustments();
     // -----------------------------
     $discount = new Discount();
     $discount->setName('2.50 off on green smoothies');
     $discount->setAction('LINE-FIXED');
     $rule = new Rule();
     $rule->setScope('line');
     $rule->setVariable('color');
     $rule->setComparison('equals');
     $rule->setValue('green');
     $discount->addRule($rule);
     $rule = new Rule();
     $rule->setScope('line');
     $rule->setVariable('category');
     $rule->setComparison('equals');
     $rule->setValue('smoothies');
     $discount->addRule($rule);
     $quantitybreak = new QuantityBreak();
     $quantitybreak->setQuantity(1);
     $quantitybreak->setActionParameter(2.5);
     $discount->addQuantityBreak($quantitybreak);
     $discounts = array($discount);
     $discounter = new Discounter();
     $this->dumpProduct($product);
     $matches = $discounter->getMatchedDiscountsForProduct($product, $discounts);
     $output->writeLn("Matched discounts: " . count($matches));
     foreach ($matches as $matcheddiscount) {
         $output->writeLn(" * " . $discount->getName());
     }
 }
 public function matchProduct(ProductInterface $product, Discount $discount)
 {
     foreach ($discount->getRules() as $rule) {
         if (!$product->hasAttribute($rule->getVariable())) {
             $value = null;
         } else {
             $attribute = $product->getAttribute($rule->getVariable());
             $value = $attribute->getValue();
         }
         $rulevalue = $rule->getValue();
         //echo "VALUE: [$value][$rulevalue]\n";
         if ($rule->getComparison() != 'equals') {
             if ($value == $rulevalue) {
                 return false;
             }
         } else {
             if ($value != $rulevalue) {
                 return false;
             }
         }
     }
     return true;
 }