src/bekanntmacher/boxmaker-bundle/src/Calculation/PriceCalculator.php line 166

Open in your IDE?
  1. <?php
  2. /**
  3.  * Boxmaker extension for Contao Open Source CMS
  4.  *
  5.  * Copyright (C) 2013-2014 bekanntmacher
  6.  *
  7.  * @author     Yanick Witschi <yanick.witschi@terminal42.ch>
  8.  * @license    commercial
  9.  */
  10. namespace Bekanntmacher\BoxmakerBundle\Calculation;
  11. class PriceCalculator
  12. {
  13.     private $objType null;
  14.     private $objConfig null;
  15.     private $objUserInput null;
  16.     private $arrPrices = array();
  17.     private $floatM2piece 0;
  18.     public $cheapestProductionMethod;
  19.     public $cheapestPrintingMethod = array();
  20.     /**
  21.      * Initialize a new PriceCalculator object
  22.      */
  23.     public function __construct($objType$objConfig$objUserInput)
  24.     {
  25.         $this->objType $objType;
  26.         $this->objConfig $objConfig;
  27.         $this->objUserInput $objUserInput;
  28.         // Fläche pro Stück
  29.         $surface = new Surface(
  30.             $this->objType->formula_laenge,
  31.             $this->objType->formula_breite,
  32.             $this->objUserInput->getAll()
  33.         );
  34.         $this->floatM2piece $this->arrPrices['floatM2piece'] = $surface->getM2PerPiece();
  35.         $this->calculatePrices();
  36.     }
  37.     public function __get($key)
  38.     {
  39.         return $this->get($key);
  40.     }
  41.     public function get($key)
  42.     {
  43.         if(key_exists($key$this->arrPrices) !== false)
  44.         {
  45.             return $this->arrPrices[$key];
  46.         }
  47.         return false;
  48.     }
  49.     public function getAll()
  50.     {
  51.         return $this->arrPrices;
  52.     }
  53.     public function getCheapestProductionMethod()
  54.     {
  55.         $lowestEp null;
  56.         $ep 0;
  57.         foreach($GLOBALS['BOXMAKER']['productionMethods'] as $method)
  58.         {
  59.             $ep  $this->getMaterialPrice($method$this->objUserInput->welle$this->objUserInput->farbe);
  60.             $ep += $this->getStanzenPrice($method);
  61.             $ep += $this->getKlebenPrice($method);
  62.             if ($lowestEp === null || $ep $lowestEp)
  63.             {
  64.                 $lowestEp $ep;
  65.                 $this->cheapestProductionMethod $method;
  66.             }
  67.         }
  68.         return $this->cheapestProductionMethod;
  69.     }
  70.     public function getCheapestPrintingMethod()
  71.     {
  72.         $arrLowest = array ( '_1f' => null'_4f' => null );
  73.         $arrF = array('_1f','_4f');
  74.         foreach ($arrF as $strF)
  75.         {
  76.             foreach($GLOBALS['BOXMAKER']['printingMethods'] as $method)
  77.             {
  78.                 if($method == 'blank' || strpos($method$strF) === false || ($method == 'offsetdruck_1f')) {
  79.                     continue;
  80.                 }
  81.                 $ep $this->getDruckPrice($strF$method);
  82.                // echo $method . ' ' . $ep ."\r\n";
  83.                 if (($arrLowest[$strF] === null || $ep $arrLowest[$strF]) && $ep != 0)
  84.                 {
  85.                     $arrLowest[$strF] = $ep;
  86.                     $this->cheapestPrintingMethod[$strF] = $method;
  87.                 }
  88.             }
  89.             $this->cheapestPrintingMethod[$strF];
  90.         }
  91.         return $this->cheapestPrintingMethod;
  92.     }
  93.     public function getMaterialPrice($method$welle$farbe)
  94.     {
  95.         return $this->arrPrices['material'][$method][$welle]['farbe_' $farbe];
  96.     }
  97.     public function getProduktionPrice($method=null)
  98.     {
  99.         if($method == null)
  100.         {
  101.             return $this->arrPrices['allProduktionPrices'][$this->cheapestProductionMethod];
  102.         }
  103.         else
  104.         {
  105.             return $this->arrPrices['allProduktionPrices'][$method];
  106.         }
  107.     }
  108.     public function getStanzenPrice($method=null)
  109.     {
  110.         if($method === null)
  111.         {
  112.             $method $this->cheapestProductionMethod;
  113.         }
  114.         return $this->arrPrices['stanzen'][$method]['stanzen_per_stk'];
  115.     }
  116.     public function getStanzmesserPrice($method=null)
  117.     {
  118.         if($method === null)
  119.         {
  120.             $method $this->cheapestProductionMethod;
  121.         }
  122.         return $this->arrPrices['stanzen'][$method]['stanzmesser'];
  123.     }
  124.     public function getKlebenPrice($method=null)
  125.     {
  126.         if($method === null)
  127.         {
  128.             $method $this->cheapestProductionMethod;
  129.         }
  130.         return $this->arrPrices['kleben'][$method]['kleben_per_stk'];
  131.     }
  132.     public function getDruckPrice($color$printing_method)
  133.     {
  134.         $color str_replace('_',''$color);
  135.         return $this->arrPrices['druck'][$color][$printing_method];
  136.     }
  137.     public function getAllDruckPrices()
  138.     {
  139.         return $this->arrPrices['druck'];
  140.     }
  141.     private function calculatePrices()
  142.     {
  143.         // Materialkosten berechnen
  144.         $this->calculateMaterialkostenPerStueck();
  145.         // Produktionskosten
  146.         $this->calculateProduktionskostenPerStueck();
  147.         // Günstigste Produktionsmethode
  148.         $this->getCheapestProductionMethod();
  149.         // Druckkosten
  150.         foreach ($GLOBALS['BOXMAKER']['printingMethods'] as $method) {
  151.             if ($this->objUserInput->stk $this->objType->{$method '_min'} && $this->getCheapestProductionMethod() !== 'ap') {
  152.                 continue;
  153.             }
  154.             $einrichten_per_stk $this->objType->{$method '_einrichten'} / $this->objUserInput->stk;
  155.             if ($method == 'digitaldruck_1f' || $method == 'digitaldruck_4f' || $method == 'digitaldruckeco_4f') {
  156.                 $druck_per_stk = ($this->objType->{$method '_perm2'} * $this->floatM2piece);
  157.                 $this->arrPrices['druck']['1f'][$method] = $einrichten_per_stk $druck_per_stk;
  158.                 $this->arrPrices['druck']['4f'][$method] = $einrichten_per_stk $druck_per_stk;
  159.             } elseif ($method == 'siebdruck_1f') {
  160.                 $druck_per_stk $this->objType->{$method '_klischee'} / $this->objUserInput->stk;
  161.                 $druck_per_stk += $this->objType->{$method '_perstueck'};
  162.                 $druck_per_stk += ($this->objType->{$method '_perm2'} * $this->floatM2piece);
  163.                 $this->arrPrices['druck']['1f'][$method] = $einrichten_per_stk $druck_per_stk;
  164.                 $this->arrPrices['druck']['4f'][$method] = null;
  165.             } elseif ($method == 'offsetdruck_1f') {
  166.                 $einrichten_per_stk $this->objType->{'offsetdruck_4f_einrichten'} / $this->objUserInput->stk;
  167.                 $druck_per_stk $this->objType->{'offsetdruck_4f_klischee'} / $this->objUserInput->stk;
  168.                 $druck_per_stk += $this->objType->{'offsetdruck_4f_perstueck'};
  169.                 $druck_per_stk += ($this->objType->{'offsetdruck_4f_perm2'} * $this->floatM2piece);
  170.                 $this->arrPrices['druck']['1f'][$method] = $einrichten_per_stk $druck_per_stk;
  171.                 $this->arrPrices['druck']['4f'][$method] = $einrichten_per_stk $druck_per_stk;
  172.             } elseif ($method == 'offsetdruck_4f') {
  173.                 $druck_per_stk $this->objType->{$method '_klischee'} / $this->objUserInput->stk;
  174.                 $druck_per_stk += $this->objType->{$method '_perstueck'};
  175.                 $druck_per_stk += ($this->objType->{$method '_perm2'} * $this->floatM2piece);
  176.                 $this->arrPrices['druck']['1f'][$method] = $einrichten_per_stk $druck_per_stk;
  177.                 $this->arrPrices['druck']['4f'][$method] = $einrichten_per_stk $druck_per_stk;
  178.             } elseif ($method == 'flexodruck_1f') {
  179.                 $druck_per_stk $this->objType->{$method '_klischee'} / $this->objUserInput->stk;
  180.                 $druck_per_stk += ($this->objType->{$method '_klischee_perm2'} * $this->floatM2piece) / $this->objUserInput->stk;
  181.                 $druck_per_stk += $this->objType->{$method '_perstueck'};
  182.                 $druck_per_stk += ($this->objType->{$method '_perm2'} * $this->floatM2piece);
  183.                 $this->arrPrices['druck']['1f'][$method] = $einrichten_per_stk $druck_per_stk;
  184.                 $this->arrPrices['druck']['4f'][$method] = null;
  185.             } elseif ($method == 'blank') {
  186.                 $this->arrPrices['druck']['1f'][$method] = 0;
  187.                 $this->arrPrices['druck']['4f'][$method] = 0;
  188.             }
  189.         }
  190.         $this->getCheapestPrintingMethod();
  191.         return $this->arrPrices;
  192.     }
  193.     private function calculateMaterialkostenPerStueck()
  194.     {
  195.         foreach ($GLOBALS['BOXMAKER']['productionMethods'] as $method)
  196.         {
  197.             $arrMethodePricing $this->objType->{$method '_pricing'};
  198.             foreach ($arrMethodePricing as $arrWellePricing)
  199.             {
  200.                 $idWelle array_shift($arrWellePricing);
  201.                 foreach ($arrWellePricing as $farbe => $value)
  202.                 {
  203.                     // welle in der Farbe nicht erhältlich
  204.                     if($value == 0)
  205.                     continue;
  206.                     $this->arrPrices['material'][$method][$idWelle][$farbe] = $value $this->floatM2piece;
  207.                 }
  208.             }
  209.         }
  210.     }
  211.     private function calculateProduktionskostenPerStueck()
  212.     {
  213.         $lowestProductionCosts null;
  214.         foreach($GLOBALS['BOXMAKER']['productionMethods'] as $method)
  215.         {
  216.             $produktion $this->calculateStanzkostenPerStk($method) + $this->calculateKlebekostenPerStk($method);
  217.             $this->arrPrices['allProduktionPrices'][$method] = $produktion;
  218.         }
  219.     }
  220.     private function calculateStanzkostenPerStk($method)
  221.     {
  222.         foreach($GLOBALS['BOXMAKER']['productionMethods'] as $m)
  223.         {
  224.             $einrichten_per_stk $this->objType->{$m '_stanzen_einrichten'} / $this->objUserInput->stk;
  225.             if ($m === 'p') {
  226.                 $stanzen_per_stk $this->objType->p_stanzen_perm2 $this->floatM2piece;
  227.                 $stanzmesser_per_stk 0;
  228.             }
  229.             else // Handtiegel / Autoplatine
  230.             {
  231.                 $stanzen_per_stk  $this->getFormulaResult($this->objType->{$m '_stanzen_perstueck'}, $this->objUserInput->getAll());
  232.                 $stanzmesser =  $this->objType->{$m '_stanzen_stanzmesser'};
  233.                 $stanzmesser += ($this->objType->{$m '_stanzen_stanzmesser_perm2'} * $this->floatM2piece);
  234.                 $stanzmesser_per_stk $stanzmesser $this->objUserInput->stk;
  235.             }
  236.             // falls unterhalb der minimalen Stückzahl für dieses Verfahren
  237.             if($m == 'ap' && $this->objUserInput->stk $this->objType->ap_minimal_stk)
  238.             {
  239.                 $einrichten_per_stk $einrichten_per_stk 100000000000;
  240.             }
  241.             $this->arrPrices['stanzen'][$m]['stanzen_per_stk'] = $einrichten_per_stk $stanzen_per_stk $stanzmesser_per_stk;
  242.             $this->arrPrices['stanzen'][$m]['stanzmesser'] = $stanzmesser_per_stk $this->objUserInput->stk;
  243.         }
  244.         return $this->arrPrices['stanzen'][$method]['stanzen_per_stk'];
  245.     }
  246.     private function calculateKlebekostenPerStk($method)
  247.     {
  248.         $einrichten_per_stk $this->objType->{$method '_kleben_einrichten'} / $this->objUserInput->stk;
  249.         $kleben_per_stk $this->objType->{$method '_kleben_perstueck'};
  250.         $kleben_per_stk += $this->objType->{$method '_kleben_permm'} * $this->objUserInput->hoehe;
  251.         $kleben_total_per_stk $einrichten_per_stk $kleben_per_stk;
  252.         $this->arrPrices['kleben'][$method]['kleben_per_stk'] = $kleben_total_per_stk;
  253.         return $kleben_total_per_stk;
  254.     }
  255.     private function getFormulaResult($code$arrData)
  256.     {
  257.         $code 'return ' . \StringUtil::parseSimpleTokens($code$arrData) . ';';
  258.         return (float) eval($code);
  259.     }
  260. }