/**
  * This function calculates the ABC Mix of a stock line
  *
  * @author Erick Guevara Martí­nez
  * @param FamilyLine $familyLine
  * @param KeyMotherLine $keyMotherLine
  * @return RestockAbcClass
  */
 private function getABCMix(FamilyLine $familyLine, KeyMotherLine $keyMotherLine)
 {
     if ($keyMotherLine->isRescue()) {
         return $this->getABCClass()->getR();
     }
     if ($familyLine->isClass($this->getABCClass()->getA()) || $keyMotherLine->isClass($this->getABCClass()->getA())) {
         return $this->getABCClass()->getA();
     }
     if ($familyLine->isClass($this->getABCClass()->getB()) || $keyMotherLine->isClass($this->getABCClass()->getB())) {
         return $this->getABCClass()->getB();
     }
     if ($familyLine->isClass($this->getABCClass()->getC()) || $keyMotherLine->isClass($this->getABCClass()->getC())) {
         return $this->getABCClass()->getC();
     }
     if ($familyLine->isClass($this->getABCClass()->getD()) || $keyMotherLine->isClass($this->getABCClass()->getD())) {
         return $this->getABCClass()->getD();
     }
     if ($familyLine->isClass($this->getABCClass()->getE()) || $keyMotherLine->isClass($this->getABCClass()->getE())) {
         return $this->getABCClass()->getE();
     }
 }
 /**
  * This function generates and return the rotation by key mother
  * (This function generates all math of Rot x Cve Madre workset Excel Document)
  *
  * @author Erick Guevara Mart�nez
  * @param InventoryRotationCollection $inventory
  * @param ProductCollection $products
  * @return \Application\Managers\RotationsManager\Stocks\KeyMotherStock
  */
 public function generateRotationByKeyMother(InventoryRotationCollection $inventory, ProductCollection $products)
 {
     // Init a new stock
     $stockByKeyMother = new KeyMotherStock();
     // Read all inventory lines to get the family stock rotation
     $inventory->rewind();
     //$inventoryParentKey = $inventory->copy();
     $productCostsQuery = ProductCostLogQuery::create()->whereAdd(ProductCostLog::ID_PRODUCT, $products->getPrimaryKeys())->addAscendingOrderBy(ProductCostLog::ID_PRODUCT)->addDescendingOrderBy(ProductCostLog::DATE);
     $productCosts = ProductCostLogQuery::create()->addColumn("*")->removeFrom()->from($productCostsQuery, "myGroup")->addGroupBy(ProductCostLog::ID_PRODUCT)->find()->getUpdates();
     //die("<pre>".print_r($productCosts, true));
     while ($inventory->valid()) {
         $productInventory = $inventory->read();
         $product = $products->getByPK($productInventory->getItemCode());
         // Verify if exist product in our database
         if ($product instanceof Product) {
             // Verify If key mother is into stock
             if ($stockByKeyMother->existIndex($productInventory->getCMADRE())) {
                 // If key mother is into stock then get key mother line
                 $keyMotherLine = $stockByKeyMother->getByIndex($productInventory->getCMADRE());
                 // sum the properties
                 $keyMotherLine->setMonth00($keyMotherLine->getMonth00() + $productInventory->getMes00P())->setMonth01($keyMotherLine->getMonth01() + $productInventory->getMes01P())->setMonth02($keyMotherLine->getMonth02() + $productInventory->getMes02P())->setMonth03($keyMotherLine->getMonth03() + $productInventory->getMes03P())->setMonth04($keyMotherLine->getMonth04() + $productInventory->getMes04P())->setMonth05($keyMotherLine->getMonth05() + $productInventory->getMes05P())->setMonth06($keyMotherLine->getMonth06() + $productInventory->getMes06P())->setMonth07($keyMotherLine->getMonth07() + $productInventory->getMes07P())->setMonth08($keyMotherLine->getMonth08() + $productInventory->getMes08P())->setMonth09($keyMotherLine->getMonth09() + $productInventory->getMes09P())->setMonth10($keyMotherLine->getMonth10() + $productInventory->getMes10P())->setMonth11($keyMotherLine->getMonth11() + $productInventory->getMes11P())->setMonth12($keyMotherLine->getMonth12() + $productInventory->getMes12P())->setAccumSalesUnits($keyMotherLine->getAccumSalesUnits() + $productInventory->getVTAACUM())->setOnHand($keyMotherLine->getOnHand() + $productInventory->getOH())->setTotalSale($keyMotherLine->getTotalSale() + $productInventory->getTVTA())->setProductByCMADRE($keyMotherLine->getProductByCMADRE() + 1)->setLineProducts($product->isLine() ? $keyMotherLine->getLineProducts() + 1 : $keyMotherLine->getLineProducts())->setManualVIM($keyMotherLine->getManualVIM() + $product->getIndividualVim())->setDescription($product->getItemName());
                 // Check if the product is rescue then set key mother in rescue
                 if ($product->isRescue()) {
                     $keyMotherLine->setRescue(1);
                 }
             } else {
                 // If key mother isn't into stock then create it
                 $keyMotherLine = new KeyMotherLine();
                 $keyMotherLine->setKeyMother($productInventory->getCMADRE())->setMonth00($productInventory->getMes00P())->setMonth01($productInventory->getMes01P())->setMonth02($productInventory->getMes02P())->setMonth03($productInventory->getMes03P())->setMonth04($productInventory->getMes04P())->setMonth05($productInventory->getMes05P())->setMonth06($productInventory->getMes06P())->setMonth07($productInventory->getMes07P())->setMonth08($productInventory->getMes08P())->setMonth09($productInventory->getMes09P())->setMonth10($productInventory->getMes10P())->setMonth11($productInventory->getMes11P())->setMonth12($productInventory->getMes12P())->setAccumSalesUnits($productInventory->getVTAACUM())->setOnHand($productInventory->getOH())->setPrice7($productInventory->getPRC7())->setManualVIM($product->getIndividualVim())->setRescue($product->isRescue() ? 1 : 0)->setTotalSale($productInventory->getTVTA())->setProductByCMADRE(1)->setDateLastLogin($productCosts[$product->getItemCode()] ? $productCosts[$product->getItemCode()] : "--")->setLineProducts($product->isLine() ? 1 : 0)->setDescription($product->getItemName());
                 if ($product->isRescue()) {
                     $keyMotherLine->setRescue(1);
                 }
             }
             // Add the kye mother changes into stock
             $stockByKeyMother->addStockable($keyMotherLine);
         }
     }
     // return the stock by key mother
     return $stockByKeyMother->ABCClassmentBySale();
 }