/**
  * This function generates and return the rotation by family
  * (This function generate the math of Claculo ABC x Sfam in new_rotation workset Excel Document)
  *
  * @author Erick Guevara Mart�nez
  * @param InventoryRotationCollection $inventory
  * @param ProductCollection $products
  */
 public function generateRotationByFamily(InventoryRotationCollection $inventory, ProductCollection $products)
 {
     // Init a new stock
     $stockByFamily = new FamilyStock();
     // Read all inventory lines to get the family stock rotation
     $inventory->rewind();
     while ($inventory->valid()) {
         $productInventory = $inventory->read();
         $product = $products->getByPK($productInventory->getItemCode());
         // If the product exist in own catalog then constinue
         if ($product instanceof Product && $product->isLine()) {
             // If the family product exist in the stock then get the family line and increment the total sale
             if ($stockByFamily->existIndex(substr($product->getItemCode(), 0, 3))) {
                 $familyLine = $stockByFamily->getByIndex(substr($product->getItemCode(), 0, 3));
                 $familyLine->setTotalSale($familyLine->getTotalSale() + $productInventory->getTVTA());
                 // If the family product not exist in the stock, then create it
             } else {
                 $familyLine = new FamilyLine();
                 $familyLine->setIdFamily(substr($product->getItemCode(), 0, 3))->setTotalSale($productInventory->getTVTA());
             }
             // Add the family line into stock
             $stockByFamily->addStockable($familyLine);
         }
     }
     // Create a ABC classification by sale and return the stock
     return $stockByFamily->ABCClassmentBySale();
 }
 /**
  *
  * @param InventoryRotationCollection $inventory
  * @param KeyMotherStock $stockByKeyMother
  * @param FamilyStock $stockByFamily
  * @param unknown_type $products
  * @param FreightFareCollection $freightFares
  */
 public function generateMinMaxPurchase(InventoryRotationCollection $inventory, KeyMotherStock $stockByKeyMother, FamilyStock $stockByFamily, ProductCollection $products, FreightFareCollection $freightFares)
 {
     // Generates a new min/max purchase list
     $productMinMaxPurchaseList = new ProductMinMaxPurchaseList();
     // Process each products in the inventory
     $inventory->rewind();
     while ($inventory->valid()) {
         // Get the inventory line and product
         $inventoryLine = $inventory->read();
         $product = $products->getByPK($inventoryLine->getItemCode());
         // If the product exist in own catalog and your status is restockable then continue
         if ($product instanceof Product && ($product->isLine() || $product->isHold() && $product->hasRestockRequiredInformation())) {
             // Get the product default freight fare
             $freightFare = $freightFares->getByPkOrElse($product->getDefaultFreightFare(), new FreightFare());
             // Get the correcponging family rotation line
             $familyLine = $stockByFamily->getByIndex(substr($product->getItemCode(), 0, 3));
             // Get the correcesponging key mother rotation line
             $keyMotherLine = $stockByKeyMother->getByIndex($product->getKeyMother());
             // Get ABC Mix
             $abcMix = $this->getABCMix($familyLine, $keyMotherLine);
             // Get the percent min max inventory by abc
             // The min max percent inventory represent the extra inventory to keep (safely stock)
             $percentMinMax = $this->getMinMaxPercentPurchase()->getByPK($abcMix->getIdRestockAbcClass());
             // Generate the min max purchase line for each allowed product
             $productMinMaxPurchase = new ProductMinMaxPurchaseLine();
             // Set the basic data to generate all calcs of VIM excel worksheet ("recompra" document)
             $productMinMaxPurchase->setIdProduct($product->getItemCode())->setABCMix($abcMix->getIdRestockAbcClass())->setManualRestock($product->needManualRestock() ? 1 : 0)->setManualVIM($product->getIndividualVim())->setPercentMax($percentMinMax->getMaxPurchasePercent())->setPercentMin($percentMinMax->getMinPurchasePercent())->setPrice7($inventoryLine->getPRC7())->setReleaseTime(ceil(($product->getLeadTime() + $freightFare->getTt()) / 7));
             // Append the line into list
             $productMinMaxPurchaseList->appendLine($productMinMaxPurchase);
         }
     }
     // Return min max purchase list
     return $productMinMaxPurchaseList;
 }