src/bekanntmacher/boxmaker-bundle/src/Module/Checkout.php line 54

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright Bekanntmacher
  4.  *
  5.  * @author Simon Wohler <s.wohler@bekanntmacher.ch>
  6.  * @license    commercial
  7.  */
  8. namespace Bekanntmacher\BoxmakerBundle\Module;
  9. use Bekanntmacher\BoxmakerBundle\Calculation\OptionCalculator;
  10. use Bekanntmacher\BoxmakerBundle\Classes\Config;
  11. use Bekanntmacher\BoxmakerBundle\Classes\UserInput;
  12. use Bekanntmacher\BoxmakerBundle\Model\FarbeModel;
  13. use Bekanntmacher\BoxmakerBundle\Model\TypeModel;
  14. use Bekanntmacher\BoxmakerBundle\Model\ConfigModel;
  15. use Bekanntmacher\BoxmakerBundle\Model\WelleModel;
  16. use Bekanntmacher\BoxmakerBundle\Calculation\PriceCalculator;
  17. use Bekanntmacher\BoxmakerBundle\Calculation\Margin;
  18. use Bekanntmacher\BoxmakerBundle\Classes\FormHelper;
  19. use Bekanntmacher\BoxmakerBundle\Model\Order;
  20. use Bekanntmacher\BoxmakerBundle\Model\Offer;
  21. use Contao\File;
  22. use Contao\FilesModel;
  23. use Contao\Frontend;
  24. use Haste\Form\Form;
  25. use Haste\Util\Format;
  26. use Bekanntmacher\BoxmakerBundle\Classes\PdfGenerator;
  27. use Contao\Email;
  28. /**
  29.  * Boxmaker Typelist
  30.  */
  31. class Checkout extends TypeUtil
  32. {
  33.     /**
  34.      * @var string Template
  35.      */
  36.     protected $strTemplate 'mod_boxmaker_checkout';
  37.     protected $objType null;
  38.     protected $objConfig null;
  39.     protected $objUserInput null;
  40.     /**
  41.      * Display a wildcard in the back end
  42.      * @return string
  43.      */
  44.     public function generate()
  45.     {
  46.         // Display a wildcard in the back end
  47.         if (TL_MODE === 'BE')
  48.         {
  49.             $template = new \BackendTemplate('be_wildcard');
  50.             $template->wildcard '### BOXMAKER TYPE DETAIL ###';
  51.             $template->title $this->name;
  52.             $template->id $this->id;
  53.             $template->link $this->name;
  54.             $template->href 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' $this->id;
  55.         }
  56.         return parent::generate();
  57.     }
  58.     /**
  59.      * Generate the module
  60.      */
  61.     protected function compile()
  62.     {
  63.         // Config
  64.         $this->objConfig = new Config(ConfigModel::findByPk($this->boxmaker_config)->row());
  65.         // Instantiate session
  66.         $objSession = \System::getContainer()->get('session');
  67.         $arrCart = array();
  68.         if($objSession->has('mybox_cart') && $objSession->get('mybox_cart') !== null)
  69.         {
  70.             $arrCart $objSession->get('mybox_cart');
  71.         }
  72.         // Form offer
  73.         $objFormOffer = new Form('boxmaker_offer''POST', function($objHaste) {
  74.             return \Input::post('FORM_SUBMIT') === $objHaste->getFormId();
  75.         });
  76.         $arrFieldsOffer FormHelper::getOfferFormFields();
  77.         $objFormOffer->addFormFields($arrFieldsOffer);
  78.         $objFormOffer->addSubmitFormField('submit'$GLOBALS['TL_LANG']['MSC']['boxmaker_typelist_labelOffer']);
  79.         if ($objFormOffer->validate() && count($arrCart) > 0) {
  80.             // Store Offer
  81.             $offer = new Offer();
  82.             $offer->cart $arrCart;
  83.             $offer->save();
  84.             $intOfferid str_pad($offer->id4"0"STR_PAD_LEFT);
  85.             // New PDF
  86.             $objPdf = new PdfGenerator($this->objConfig);
  87.             // Add stylesheet
  88.             $strStylesheet file_get_contents(\FilesModel::findByUuid($this->objConfig->pdf_css)->path);
  89.             $objPdf->setCss($strStylesheet);
  90.             // Write HTML
  91.             $strHTML $this->getPdfHtml($objFormOffer->fetchAll(), $arrCart$intOfferid);
  92.             $objPdf->writeHTML($strHTML);
  93.             // Send offer via swift mailer (because nc doesn't support attach file as string)
  94.             $objEmail = new Email();
  95.             $objEmail->subject = \StringUtil::parseSimpleTokens($this->objConfig->offer_subject, array('nr' => $intOfferid));
  96.             // Connvert to plain text
  97.             $strText strip_tags($this->objConfig->offer_text);
  98.             $strText = \StringUtil::decodeEntities($strText);
  99.             $strText str_replace(array('[&]''[lt]''[gt]'), array('&''<''>'), $strText);
  100.             $objEmail->text $strText;
  101.             $objEmail->html $this->objConfig->offer_text;
  102.             $objEmail->attachFileFromString($objPdf->getPDFasString(), $GLOBALS['TL_LANG']['MSC']['boxmaker_offer_filename'],'application/pdf');
  103.             $objEmail->from $this->objConfig->offer_from;
  104.             
  105.             $objEmail->sendTo($this->objConfig->offer_bcc);
  106.             $objEmail->sendTo($objFormOffer->fetch('offer_email'));
  107.             $objSession->remove('mybox_cart');
  108.             Frontend::jumpToOrReload($this->boxmaker_jumpToOfferConfirmation);
  109.         }
  110.         $objFormOfferContainer = new \stdClass();
  111.         $objFormOffer->addToObject($objFormOfferContainer);
  112.         $this->Template->form_offer $objFormOfferContainer;
  113.         // Form Order
  114.         $objFormOrder = new Form('boxmaker_order''POST', function($objHaste) {
  115.             return \Input::post('FORM_SUBMIT') === $objHaste->getFormId();
  116.         });
  117.         $arrFieldsOrder FormHelper::getOrderFormFields();
  118.         $objFormOrder->addFormFields($arrFieldsOrder);
  119.         $objFormOrder->addSubmitFormField('submit'$GLOBALS['TL_LANG']['MSC']['boxmaker_typelist_labelOrder']);
  120.         // Check if there are validators
  121.         foreach ($arrFieldsOrder as $name => $dca) {
  122.             if ($dca['eval']['boxmakerValidator']) {
  123.                 $objFormOrder->addValidator($name$dca['eval']['boxmakerValidator']);
  124.             }
  125.         }
  126.         if ($objFormOrder->validate() && count($arrCart) > 0) {
  127.             // Send admin notification (pro cart position one message)
  128.             foreach ($arrCart as $pos => $arrSessionType)
  129.             {
  130.                 if($arrSessionType['muster'] != 1)
  131.                 {
  132.                     $notification = \NotificationCenter\Model\Notification::findByPk($this->objConfig->boxmaker_admin_notification);
  133.                 }
  134.                 else {
  135.                     $notification = \NotificationCenter\Model\Notification::findByPk($this->objConfig->boxmaker_admin_muster_notification);
  136.                 }
  137.                 $tokens $this->prepareTokens($objFormOrder->fetchAll(), $arrSessionType);
  138.                 // Store Order
  139.                 $order = new Order();
  140.                 $order->setDataFromOrderForm($tokens$this->typeModel);
  141.                 $order->save();
  142.                 $tokens['order_id'] = $order->id;
  143.                 if (null !== $notification) {
  144.                     $notification->send($tokens);
  145.                     echo 'send';
  146.                 }
  147.             }
  148.             $objSession->remove('mybox_cart');
  149.             Frontend::jumpToOrReload($this->boxmaker_jumpToConfirmation);
  150.         }
  151.         $objFormOrderContainer = new \stdClass();
  152.         $objFormOrder->addToObject($objFormOrderContainer);
  153.         $this->Template->form $objFormOrderContainer;
  154.         // Weiterleitungsseite
  155.         $objJumpToStart = \PageModel::findByPk($this->boxmaker_jumpToStart);
  156.         if($objJumpToStart !== null) {
  157.             $this->Template->boxmaker_jumpToStart $objJumpToStart->getFrontendUrl();
  158.         }
  159.     }
  160.     private function prepareTokens($formData$arrCartPos)
  161.     {
  162.         $tokens = array();
  163.         // Format form data
  164.         $tokens array_merge($tokens$this->addAdressTokens($formData$tokens));
  165.         // User Input
  166.         $this->objUserInput = new UserInput($arrCartPos);
  167.         $this->objUserInput->set('farbe_title'FarbeModel::findByPk($this->objUserInput->farbe)->selecttitle);
  168.         $objType TypeModel::findByPk((int) $arrCartPos['type'], array('eager' => true));
  169.         $objT = new Type($this->compileTypeData($objType));
  170.         $objT->addTypeConfig($objType->getRelated('type_config_set')->row());
  171.         // Title
  172.         if($arrCartPos['muster'] != 1)
  173.         {
  174.             $tokens['type_title'] = $objT->title;
  175.         } else {
  176.             $tokens['type_title'] = ($arrCartPos['druckverfahren'] == 'blank') ? $objT->title ' Muster unbedruckt' $objT->title ' Muster bedruckt';
  177.         }
  178.         // Auftrag
  179.         $tokens['stk'] = $this->objUserInput->stk;
  180.         $tokens['laenge'] = $this->objUserInput->laenge;
  181.         $tokens['breite'] = $this->objUserInput->breite;
  182.         $tokens['hoehe'] = $this->objUserInput->hoehe;
  183.         $tokens['welle'] = WelleModel::findByPk($this->objUserInput->welle)->title;
  184.         $tokens['farbe'] = FarbeModel::findByPk($this->objUserInput->farbe)->title;
  185.         $tokens['order_print'] = $this->getDruckLabel();
  186.         // Preis berechnen
  187.         $objCalc = new PriceCalculator($objT$this->objConfig$this->objUserInput);
  188.         $objMargin = new Margin($objT$this->objUserInput);
  189.         $objOcalc = new OptionCalculator($objT$this->objUserInput);
  190.         // EP's
  191.         // ep aterial
  192.         $objCalc->getCheapestProductionMethod();
  193.         $this->objUserInput->welle;
  194.         $this->objUserInput->farbe;
  195.         $material       $objCalc->getMaterialPrice($arrCartPos['methode_selected'], $this->objUserInput->welle$this->objUserInput->farbe);
  196.         // ep produktion ( = stanzen und kleben )
  197.         $produktion $objCalc->getProduktionPrice($arrCartPos['methode_selected']);
  198.         $tokens['methode_selected'] = $GLOBALS['BOXMAKER']['productionMethodsLabel'][$arrCartPos['methode_selected']];
  199.         $key $arrCartPos['methode_selected'] . '_delivery_time';
  200.         $tokens['delivery_time'] = $objT->$key;
  201.         // drucken
  202.         $drucken       $objCalc->getDruckPrice($this->objUserInput->druckfarben$this->objUserInput->druckverfahren);
  203.         // eco 2s ersetzen
  204.         $arrDP $objCalc->getAllDruckPrices();
  205.         if($this->objUserInput->seite == '2s')
  206.         {
  207.             if($this->objUserInput->druckverfahren != 'digitaldruckeco_4f')
  208.             {
  209.                 $drucken $drucken 2;
  210.             } else {
  211.                 $lowest 0;
  212.                 foreach ($objCalc->getAllDruckPrices()['4f'] as $key => $value)
  213.                 {
  214.                     if(strpos($key,'_4f') !== false) {
  215.                         if($key != 'digitaldruckeco_4f' && ($lowest == || $value $lowest)) {
  216.                             $lowest $value;
  217.                         }
  218.                     }
  219.                 }
  220.                 $drucken =  $lowest 2;
  221.             }
  222.         }
  223.         // Druckbild weiss hinterlegt
  224.         $tokens['druck_hg'] = '-';
  225.         $tokens['druck_hg_price'] = '0';
  226.         if($arrCartPos['druck_hg'] == 1)
  227.         {
  228.             $objO = new \stdClass();
  229.             $objO->surcharge_stk $this->objConfig->hg_print_surcharge_stk;
  230.             $objO->surcharge_m2 $this->objConfig->hg_print_surcharge_m2;
  231.             $objO->surcharge_total $this->objConfig->hg_print_surcharge_total;
  232.             $weiss_hinterlegt $objOcalc->calculateOptionPriceStk($objO);
  233.             $tokens['druck_hg'] = 'ja';
  234.             $tokens['druck_hg_price'] = $this->getAmount($weiss_hinterlegttruetruefalse);
  235.         }
  236.         // EP's
  237.         if($arrCartPos['muster'] != 1)
  238.         {
  239.             $tokens['ep_material'] = $this->getAmount($material);
  240.             $tokens['ep_produktion'] = $this->getAmount($produktion);
  241.             $tokens['ep_stanzen'] = $this->getAmount($objCalc->getStanzenPrice());
  242.             $tokens['ep_stanzmesser'] = $this->getAmount($objCalc->getStanzmesserPrice());
  243.             $tokens['ep_kleben'] = $this->getAmount($objCalc->getKlebenPrice());
  244.             $tokens['ep_drucken'] = $this->getAmount($drucken);
  245.             $tokens['ep_total_stk'] = $this->getAmount($material $produktion $drucken $tokens['druck_hg_price']);
  246.             $tokens['ep_gesamt'] = $this->getAmount(($material $produktion $drucken $weiss_hinterlegt ) * $this->objUserInput->stk);
  247.         }
  248.         // VP's Normal
  249.         $tokens['vp_total_stk'] = $this->getAmount
  250.         (
  251.             $material
  252.             $produktion
  253.             $drucken
  254.             $objMargin->getBaseMarginStk()
  255.             + $objMargin->getDynamicMarginStk($material $produktion $drucken)
  256.             + $weiss_hinterlegt
  257.         );
  258.         $tokens['vp_gesamt'] = $this->getAmount
  259.         (
  260.             $this->getAmount(
  261.                 $material
  262.                 $produktion
  263.                 $drucken
  264.                 $objMargin->getBaseMarginStk()
  265.                 + $objMargin->getDynamicMarginStk($material $produktion $drucken)
  266.                 + $weiss_hinterlegt
  267.                 false
  268.             )
  269.             * $this->objUserInput->stk
  270.         );
  271.         // VP Muster
  272.         if($arrCartPos['muster'] == 1)
  273.         {
  274.             if($arrCartPos['druckverfahren'] == 'blank')
  275.             {
  276.                 $tokens['vp_total'] = $tokens['vp_total_stk'] = $this->getAmount($this->objConfig->boxmaker_price_sample_blankfalse);
  277.             } else {
  278.                 $tokens['vp_total'] = $tokens['vp_total_stk'] = $this->getAmount($this->objConfig->boxmaker_price_sample_printedfalse);
  279.             }
  280.             $tokens['vp_options'] = 0;
  281.             $tokens['vp_gesamt'] = $tokens['vp_total'];
  282.         }
  283.         // günstigste Prod Methode
  284.         $tokens['method'] = $GLOBALS['BOXMAKER']['productionMethodsLabel'][$objCalc->getCheapestProductionMethod()];
  285.         // ep verfahren
  286.         $arrPrices $objCalc->getAll();
  287.         if($arrCartPos['muster'] != 1)
  288.         {
  289.             foreach ($GLOBALS['BOXMAKER']['productionMethods'] as $method)
  290.             {
  291.                 $m $objCalc->getMaterialPrice($method$this->objUserInput->welle$this->objUserInput->farbe);
  292.                 $p $arrPrices['allProduktionPrices'][$method];
  293.                 $s $arrPrices['stanzen'][$method]['stanzen_per_stk'];
  294.                 $sm $arrPrices['stanzen'][$method]['stanzmesser'];
  295.                 $k $arrPrices['kleben'][$method]['kleben_per_stk'];
  296.                 $tot $m $s $k;
  297.                 $tokens['produktion_' $method '_material'] = ($m == '') ? 'nicht lieferbar' $this->getAmount($m);
  298.                 $tokens['produktion_' $method '_produktion'] = $this->getAmount($p);
  299.                 $tokens['produktion_' $method '_stanzen'] = $this->getAmount($s);
  300.                 $tokens['produktion_' $method '_stanzmesser'] = $this->getAmount($sm);
  301.                 $tokens['produktion_' $method '_kleben'] = $this->getAmount($k);
  302.                 $tokens['produktion_' $method '_total'] = $this->getAmount($tot);
  303.             }
  304.             // Druck
  305.             $tokens['druck_Digitaldruck_1f'] = $this->getAmount($arrPrices['druck']['1f']['digitaldruck_1f']);
  306.             $tokens['druck_Digitaldruck_4f'] = $this->getAmount($arrPrices['druck']['1f']['digitaldruck_4f']);
  307.             $tokens['druck_Digitaldruckeco_4f'] = $this->getAmount($arrPrices['druck']['1f']['digitaldruckeco_4f']);
  308.             $tokens['druck_Siebdruck_1f'] = $this->getAmount($arrPrices['druck']['1f']['siebdruck_1f']);
  309.             $tokens['druck_Flexodruck_1f'] = $this->getAmount($arrPrices['druck']['1f']['flexodruck_1f']);
  310.             $tokens['druck_Offsetdruck_1f'] = $this->getAmount($arrPrices['druck']['1f']['offsetdruck_1f']);
  311.             $tokens['druck_Offsetdruck_4f'] = $this->getAmount($arrPrices['druck']['1f']['offsetdruck_4f']);
  312.             // Marge
  313.             $tokens['marge_prozent'] = $objMargin->getMarginPercent();
  314.         }
  315.         // infos
  316.         $tokens['mwst_satz'] = $this->objConfig->boxmaker_mwst;
  317.         $tokens['boxmaker_currency'] = $this->objConfig->boxmaker_currency;
  318.         // debug...
  319.         /*
  320.         $tokens['userInput'] = $this->objUserInput->getAll();
  321.         $tokens['type'] = $objT->getAll();
  322.         $tokens['arrPrices'] = $objCalc->getAll();
  323.         */
  324.         return $tokens;
  325.     }
  326.     protected function addAdressTokens($formData)
  327.     {
  328.         $buffer = array();
  329.         $fields FormHelper::getOrderFormFields();
  330.         foreach ($fields as $name => $dca) {
  331.             $buffer[$name] = Format::dcaValueFromArray($dca$formData[$name]);
  332.         }
  333.         return $buffer;
  334.     }
  335.     public static function getOrderFormFields()
  336.     {
  337.         $arrFields = static::createAddressFields('billing');
  338.         $arrFields['enable_shipping_address'] = array(
  339.             'label'         => array('''Abweichende Lieferadresse'),
  340.             'inputType'     => 'checkbox'
  341.         );
  342.         $validator = new MandatoryOn();
  343.         $validator->addMatch('enable_shipping_address'true);
  344.         $arrFields array_merge($arrFields, static::createAddressFields('shipping'$validator));
  345.         return $arrFields;
  346.     }
  347.     // PDF offer
  348.     private function getPdfHtml($arrFormData$arrCart$idOffer) {
  349.         $objTemplate = new \FrontendTemplate('boxmaker_pdf_offer');
  350.         // General
  351.         $objTemplate->boxmaker_currency $this->objConfig->boxmaker_currency;
  352.         $objTemplate->offerid $idOffer;
  353.         $objTemplate->datum date('d.m.Y'time());
  354.         $objTemplate->address $this->generateAddress($arrFormData);
  355.         // Generate cart items
  356.         $objCartUtil = new CartUtil($this->objConfig);
  357.         $cart_items = array();
  358.         foreach ($arrCart as $pos => $arrType)
  359.         {
  360.             $cart_items[] = $objCartUtil->renderType($pos$arrType'cart_item_pdf_offer' true);
  361.         }
  362.         $objTemplate->cart_items $cart_items;
  363.         $objTemplate->show_total = (count($arrCart) == 1) ? true false;
  364.         $objTemplate->boxmaker_currency $this->objConfig->boxmaker_currency;
  365.         $objTemplate->zwischensumme $objCartUtil->getAmount($objCartUtil->zwischensummefalse);
  366.         // VEK
  367.         $vek $objCartUtil->zwischensumme * ($this->objConfig->boxmaker_vek/100);
  368.         $objTemplate->vek $objCartUtil->getAmount($vek);
  369.         $objTemplate->vek_prozent $this->objConfig->boxmaker_vek ' % ' $GLOBALS['TL_LANG']['MSC']['boxmaker_vek_label'];
  370.         $mwst = ($objCartUtil->zwischensumme $vek) * ($this->objConfig->boxmaker_mwst/100);
  371.         $objTemplate->mwst $objCartUtil->getAmount($mwstfalse);
  372.         $objTemplate->mwst_satz $this->objConfig->boxmaker_mwst '%';
  373.         $objTemplate->total $objCartUtil->getAmount($objCartUtil->zwischensumme $vek $mwstfalse);
  374.         $this->objConfig->boxmaker_currency;
  375.         $objTemplate->shipping = ($this->objConfig->boxmaker_currency == 'EUR') ? $GLOBALS['TL_LANG']['MSC']['boxmaker_shipping_costs_de'] : $GLOBALS['TL_LANG']['MSC']['boxmaker_shipping_costs_ch'] ;
  376.         $objTemplate->conditions $GLOBALS['TL_LANG']['MSC']['boxmaker_offer_conditions'];
  377.         $objTemplate->message_plus $objCartUtil->getMessagePlus();
  378.         return  $objTemplate->parse();
  379.     }
  380.     public function generateAddress($arrFormData)
  381.     {
  382.         $a = array();
  383.         $a[] = $arrFormData['offer_company'];
  384.         $a[] = $arrFormData['offer_name'];
  385.         $a[] = $arrFormData['offer_strasse'];
  386.         $a[] = $arrFormData['offer_ort'];
  387.         return array_filter($a);
  388.     }
  389. }