<?php
/*
* Copyright Bekanntmacher
*
* @author Simon Wohler <s.wohler@bekanntmacher.ch>
* @license commercial
*/
namespace Bekanntmacher\BoxmakerBundle\Module;
use Bekanntmacher\BoxmakerBundle\Classes\Config;
use Bekanntmacher\BoxmakerBundle\Classes\UserInput;
use Bekanntmacher\BoxmakerBundle\Model\FarbeModel;
use Bekanntmacher\BoxmakerBundle\Model\TypeModel;
use Bekanntmacher\BoxmakerBundle\Model\ConfigModel;
use Bekanntmacher\BoxmakerBundle\Model\WelleModel;
use Bekanntmacher\BoxmakerBundle\Calculation\PriceCalculator;
use Bekanntmacher\BoxmakerBundle\Calculation\Margin;
/**
* Boxmaker Typelist
*/
class TypeDebug extends TypeUtil
{
/**
* @var string Template
*/
protected $strTemplate = 'mod_boxmaker_typedebug';
protected $objType = null;
protected $objConfig = null;
protected $objUserInput = null;
/**
* Display a wildcard in the back end
* @return string
*/
public function generate()
{
// Display a wildcard in the back end
if (TL_MODE === 'BE')
{
$template = new \BackendTemplate('be_wildcard');
$template->wildcard = '### BOXMAKER TYPE DETAIL ###';
$template->title = $this->name;
$template->id = $this->id;
$template->link = $this->name;
$template->href = 'contao/main.php?do=themes&table=tl_module&act=edit&id=' . $this->id;
}
return parent::generate();
}
/**
* Generate the module
*/
protected function compile()
{
// Type
$objTypeModel = TypeModel::findByPk((int) \Input::get('type'), array('eager' => true));
if ($objTypeModel === null)
{
global $objPage;
$objHandler = new $GLOBALS['TL_PTY']['error_404']();
$objHandler->generate($objPage->id);
}
// Type and type_config
$arrType = $this->compileTypeData($objTypeModel);
$this->objType = new Type($arrType);
$this->objType->addTypeConfig($objTypeModel->getRelated('type_config_set')->row());
$this->Template->type = $this->objType->getAll();
// Boxmaker Konfiguration
$this->objConfig = new Config(ConfigModel::findByPk($this->boxmaker_config)->row());
// User Input
$this->objUserInput = new UserInput($this->getUserInput());
$this->objUserInput->set('farbe_title', FarbeModel::findByPk($this->objUserInput->farbe)->selecttitle);
$this->Template->userInput = $this->objUserInput->getAll();
// Welle
$objWelle = WelleModel::findByPk(\Input::get('welle'));
$this->Template->welle = ($objWelle !== null) ? $objWelle->title : WelleModel::findByPk($this->objType->type_standardwelle)->title;
// Farbe
$objFarbe = FarbeModel::findByPk(\Input::get('wellenfarbe'));
$this->Template->farbe = ($objFarbe !== null) ? $objFarbe->title : FarbeModel::findByPk($this->objType->type_standardfarbe)->title;
// Druck
$this->Template->druck = $this->getDruckLabel();
// Optionen
$this->Template->optionen = array();
// Preise
$this->Template->prices = $this->addDebugPrices();
// Debug
// $this->Template->debug = print_r($this->addDebugPrices(),true);
}
private function getDruckLabel()
{
$druckverfahren = str_replace(\Input::get('druckfarben'),'',\Input::get('druckverfahren'));
$strDruck = ($druckverfahren == '') ? 'unbedruckt' : ucfirst($druckverfahren);
$strDruck .= ($druckverfahren != '') ? ', ' . str_replace('_','', \Input::get('druckfarben')) : '';
$strDruck .= ($druckverfahren != '') ? ', ' .\Input::get('seite') : '';
return $strDruck;
}
private function addDebugPrices()
{
// Marge
$objMargin = new Margin($this->objType, $this->objUserInput);
$grundmarge_pro_stk = $objMargin->getBaseMarginStk();
// Preise berechnen
$arrPricesTotal = array();
$objCalc = new PriceCalculator($this->objType, $this->objConfig, $this->objUserInput);
$arrPrices = $objCalc->getAll();
$material = $objCalc->getMaterialPrice($objCalc->getCheapestProductionMethod(), \Input::get('welle'), \Input::get('wellenfarbe'));
$produktion = $objCalc->getProduktionPrice(); // stanzen + kleben
$drucken = $objCalc->getDruckPrice(str_replace('_','', \Input::get('druckfarben')),\Input::get('druckverfahren'));
if(\Input::get('seite') == '2s')
{
$drucken = $drucken * 2;
}
// optionen
$options = 0;
$arrO = $this->generateOptionsTable();
$arrOptions = array();
foreach ($this->objType->type_options as $id)
{
if((int)(\Input::get('o_'.$id) > 0))
{
$options = $options + $arrO['array'][$id]['price'];
$arrOptions[] = $arrO[$id] = array(
'title' => $arrO['array'][$id]['title'],
'price' => $arrO['array'][$id]['price']
);
}
}
$this->Template->options = $arrOptions;
$arrPricesTotal['lieferant']['total'] = $this->getAmount($material + $produktion);
$arrPricesTotal['lieferant']['material'] = $this->getAmount($material);
$arrPricesTotal['lieferant']['produktion'] = $this->getAmount($produktion);
$arrPricesTotal['lieferant']['stanzen'] = $this->getAmount($objCalc->getStanzenPrice());
$arrPricesTotal['lieferant']['kleben'] = $this->getAmount($objCalc->getKlebenPrice());
$arrPricesTotal['lieferant']['drucken'] = $this->getAmount($drucken);
// Totalpreise
$ep = $material + $produktion;
$drucken = 0;
$p = 0;
if(\Input::get('druckverfahren') != 'blank')
{
$drucken = $objCalc->getDruckPrice(str_replace('_', '', \Input::get('druckfarben')), \Input::get('druckverfahren'));
}
if(\Input::get('druckverfahren') != '')
{
$ep_stk = $ep;
$p = $ep + $grundmarge_pro_stk + $objMargin->getDynamicMarginStk($ep);
$m = $grundmarge_pro_stk + $objMargin->getDynamicMarginStk($ep);
if(\Input::get('seite') == '1s')
{
$ep_stk = $ep + $drucken;
$p = $ep + $drucken + $grundmarge_pro_stk + $objMargin->getDynamicMarginStk($ep + $drucken);
$m = $grundmarge_pro_stk + $objMargin->getDynamicMarginStk($ep + $drucken);
}
elseif(\Input::get('seite') == '2s')
{
$ep_stk = $ep + (2 * $drucken);
$p = $ep + (2 * $drucken) + $grundmarge_pro_stk + $objMargin->getDynamicMarginStk($ep + (2 * $drucken));
$m = $grundmarge_pro_stk + $objMargin->getDynamicMarginStk($ep + (2 * $drucken));
}
}
//$arrPricesTotal['debug3'] = print_r($this->objType->getAll(),true);
//$arrPricesTotal['debug2'] = $material;
//$arrPricesTotal['debug'] = print_r($objCalc->getAll(),true);
$arrPricesTotal['ep_stk'] = $this->getAmount($ep_stk);
$arrPricesTotal['ep_total'] = $this->getAmount($this->getAmount($ep_stk) * \Input::get('stk'));
$arrPricesTotal['total']['stk'] = $this->getAmount($p);
$arrPricesTotal['total']['total'] = $this->getAmount($this->getAmount($p) * \Input::get('stk'));
$arrPricesTotal['total']['total_options'] = $this->getAmount( $options + $arrPricesTotal['total']['total']);
// Spezifikationen
$this->Template->method = $objCalc->getCheapestProductionMethod();
$this->Template->grundmarge = $this->getAmount($grundmarge_pro_stk * \Input::get('stk'));
$this->Template->margeInProzent = $objMargin->getMarginPercent();
$this->Template->margeTotal = number_format(round($m * \Input::get('stk'),2),2);
// Produktion
foreach($GLOBALS['BOXMAKER']['productionMethods'] as $method)
{
$material = $arrPrices['material'][$method][\Input::get('welle')]['farbe_' . \Input::get('wellenfarbe')];
$arrPricesTotal['prduktion'][$method] = array
(
'total' => $this->getAmount($material + $objCalc->getStanzenPrice($method) + $objCalc->getKlebenPrice($method)),
'material' => $this->getAmount($material),
'produktion' => $this->getAmount($objCalc->getProduktionPrice($method)),
'stanzen' => $this->getAmount($objCalc->getStanzenPrice($method)),
'kleben' => $this->getAmount($objCalc->getKlebenPrice($method))
);
}
// Druck
foreach($GLOBALS['BOXMAKER']['printingMethods'] as $method)
{
if ($method == 'blank')
{
continue;
}
$arr = explode('_', $method);
$arrPricesTotal['druck'][ucfirst($method)] = $this->getAmount($objCalc->getDruckPrice($arr[1], $method));
}
return $arrPricesTotal;
}
}