Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 843 | Revision 856 | Zur aktuellen Revision | Blame | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

<?php

    namespace App\Http\Controllers;

    use App\Models\Item as ItemAlias;
    use App\Http\Controllers\ShippingController;

    class BasketController extends Controller
    {
        public array $items         = array();
        public array $paymentInfo   = array();
        public array $shipmentCosts = array();

        public array $paymentMethods = array();

        //
        private function getPaymentInfo(): void
        {
            $buffer = array(
                "itemAmt"      => array(
                    0 => 0.00,
                    1 => 0.00,
                ),
                "itemAmtNetto" => array(
                    0 => 0.00,
                    1 => 0.00,
                ),
                "itemTax"      => array(
                    0 => 0.00,
                    1 => 0.00,
                ),
                "shipAmt"      => 0.00,
                "shipTax"      => array(
                    0 => 0.00,
                    1 => 0.00,
                ),
                "Amt"          => 0.00,
                "Tax"          => 0.00,
                "Total"        => 0.00,
            );
            if ( $_SESSION["SHOP"]["BASKET"]->items )
            {
                $Land = 47;
                if ( ( isset( $_SESSION['SHOP']['Lieferadresse'] ) && $_SESSION['SHOP']['Lieferadresse'] == "true" ) || ( isset( $_GET["liefer"] ) && $_GET["liefer"] == 1 ) || isset( $_SESSION['SHOP']['buy']['Persdata']['liefer_Land'] ) )
                {
                    $Land = $_SESSION['SHOP']['buy']['Persdata']['liefer_Land'];
                }
                elseif ( isset( $_SESSION['SHOP']['buy']['Persdata']['Land'] ) )
                {
                    $Land = $_SESSION['SHOP']['buy']['Persdata']['Land'];
                }
                if ( !isset( $GLOBALS["steuern"][$Land] ) )
                {
                    $Land = 47;
                }
                $taxArr = $GLOBALS["steuern"][$Land];
                foreach ( $_SESSION["SHOP"]["BASKET"]->items as $bItem )
                {
                    if ( !$bItem->code )
                    {
                        $item = ItemAlias::find( $bItem->id );
                        if ( !isset( $bItem->tax ) )
                        {
                            $bItem->tax = $item->tax1;
                        }
                        $tax = $taxArr[$bItem->tax];
                        $bPreis = $bItem->menge * $bItem->price;
                        $buffer["itemAmt"][$bItem->tax] += $bPreis;
                    }
                    else
                    {
                        $tax = 0;
                        $bPreis = $bItem->price;
                        $buffer["discount"] = $bPreis * -1;
                    }
                    if ( isset( $GLOBALS["INI"]["netto_preise"] ) && $GLOBALS["INI"]["netto_preise"] )
                    {
                        $buffer["itemTax"][$bItem->tax] += round( $bPreis * $tax / 100, 2 );
                    }
                    else
                    {
                        $buffer["itemTax"][$bItem->tax] += round( $bItem->price * $tax / ( 100 + $tax ), 2 ) * $bItem->menge;
                    }
                    $buffer["Amt"] += $bPreis;
                }
                if ( !( isset( $GLOBALS["INI"]["netto_preise"] ) && $GLOBALS["INI"]["netto_preise"] == 1 ) )
                {
                    foreach ( $buffer["itemTax"] as $tax => $amt )
                    {
                        $buffer["itemAmtNetto"][$tax] = $buffer["itemAmt"][$tax] - round( $amt, 2 );
                    }
                }
                if ( isset( $_SESSION["SHOP"]["VERSAND"] ) && $_SESSION["SHOP"]["VERSAND"] )
                {
                    $t = 1;
                    $bItem = $_SESSION["SHOP"]["VERSAND"];
                    $buffer["shipAmt"] = $bItem["preis"];
                    $buffer["Amt"] += $bItem["preis"];
                    if ( $GLOBALS["INI"]["netto_preise"] )
                    {
                        $netto = array_sum( $buffer["itemAmt"] );
                        $t = $buffer["itemAmt"][0] / $netto;
                    }
                    if ( !isset( $GLOBALS["steuern"][$Land] ) )
                    {
                        $Land = 47;
                    }
                    $taxArr = $GLOBALS["steuern"][$Land];
                    if ( $t == 1 )
                    {
                        $tax = $taxArr[0];
                        if ( $GLOBALS["INI"]["netto_preise"] )
                        {
                            $buffer["shipTax"][0] += $bItem->price * $tax / 100;
                        }
                        else
                        {
                            $buffer["shipTax"][0] += $bItem->price * $tax / ( 100 + $tax );
                        }
                    }
                    elseif ( $t == 0 )
                    {
                        $tax = $taxArr[1];
                        if ( $GLOBALS["INI"]["netto_preise"] )
                        {
                            $buffer["shipTax"][1] += $bItem->price * $tax / 100;
                        }
                        else
                        {
                            $buffer["shipTax"][1] += $bItem->price * $tax / ( 100 + $tax );
                        }
                    }
                    else
                    {
                        $tax = $taxArr[0];
                        if ( isset( $GLOBALS["INI"]["netto_preise"] ) && $GLOBALS["INI"]["netto_preise"] )
                        {
                            $buffer["shipTax"][0] += ( $bItem->price * $tax / 100 ) * $t;
                        }
                        else
                        {
                            $buffer["shipTax"][0] += ( $bItem->price * $tax / ( 100 + $tax ) ) * $t;
                        }
                        $tax = $taxArr[1];
                        if ( isset( $GLOBALS["INI"]["netto_preise"] ) && $GLOBALS["INI"]["netto_preise"] )
                        {
                            $buffer["shipTax"][1] += ( $bItem->price * $tax / 100 ) * ( 1 - $t );
                        }
                        else
                        {
                            $buffer["shipTax"][0] += ( $bItem->price * $tax / ( 100 + $tax ) ) * ( 1 - $t );
                        }
                    }
                }
                foreach ( $taxArr as $ind => $tax )
                {
                    $buffer["TaxRates"][$ind] = $tax;
                }
                $buffer["Tax"] = round( array_sum( $buffer["itemTax"] ) + array_sum( $buffer["shipTax"] ), 2 );
                if ( isset( $GLOBALS["INI"]["netto_preise"] ) && $GLOBALS["INI"]["netto_preise"] )
                {
                    $buffer["Total"] = round( $buffer["Amt"] + $buffer["Tax"], 2 );
                }
                else
                {
                    $buffer["Total"] = $buffer["Amt"];
                }
            }
            $this->paymentInfo = $buffer;
        }

        public function addItem()
        {
            $preis = 0;
            if ( request( "preis" ) )
            {
                $preis = request( "preis" );
            }
            $bItem = new BasketItemController( \request( "Menge" ), $preis, \request( "item" ) );
            $bItem->addinfo = "";
            // addons aus konfigurator
            $addon = \request( "addon" );
            if ( is_array( $addon ) )
            {
                foreach ( $addon as $itemID )
                {
                    if ( $expl = explode( "#", $itemID ) )
                    {
                        if ( $expl[0] != 0 )
                        {
                            $itemID = $expl[0];
                        }
                        $preis = 0;
                        if ( $expl[1] != 0 )
                        {
                            $preis = $expl[1];
                        }
                    }
                    if ( is_numeric( $itemID ) )
                    {
                        $aItem = new BasketItemController( 1, $preis, $itemID );
                        $bItem->addinfo .= "<br>" . $aItem->name;
                        $bItem->price += $preis;
                    }
                }
            }
            $key = sha1( $bItem->name . $bItem->addinfo );
            if ( isset( $_SESSION["SHOP"]["BASKET"]->items[$key] ) )
            {
                $bItem->menge += $_SESSION["SHOP"]["BASKET"]->items[$key]->menge;
            }
            $_SESSION["SHOP"]["BASKET"]->items[$key] = $bItem;
            $this->getPaymentInfo();
            $this->getShipmentCosts();
        }

        public function editItem()
        {
            $key = request( "id" );
            $menge = request( "menge" );
            $_SESSION["SHOP"]["BASKET"]->items[$key]->menge = $menge;
            $this->getPaymentInfo();
            $this->getShipmentCosts();
        }

        public function delItem()
        {
            $key = request( "id" );
            unset( $_SESSION["SHOP"]["BASKET"]->items[$key] );
            $this->getPaymentInfo();
            $this->getShipmentCosts();
        }

        private function getShipmentCosts()
        {
            $sc = new ShippingController();
            $buffer = $sc->getShippingOptions();
            $_SESSION["SHOP"]["BASKET"]->shipmentCosts = $buffer;
        }

        public function getBasketGewicht(): int
        {
            $gewicht = 0.00;
            foreach ( $_SESSION["SHOP"]["BASKET"]->items as $item )
            {
                $gewicht += $item->menge * $item->weight;
            }
            return $gewicht;
        }
    }