From eb55b01b0d4e4ef236c24c0e1a84f959d9163970 Mon Sep 17 00:00:00 2001 From: Kevin Adametz Date: Wed, 1 Jul 2020 16:13:38 +0200 Subject: [PATCH] Shipping Tax Card --- .idea/workspace.xml | 24 +++--- app/Http/Controllers/ShippingController.php | 10 +-- app/Models/Shipping.php | 3 + app/Models/ShippingPrice.php | 18 ++--- app/Services/Yard.php | 77 +++++++++---------- config/cart.php | 2 +- ...06_004849_create_shipping_prices_table.php | 2 +- ...3724_create_shopping_order_items_table.php | 1 + resources/lang/de/email.php | 1 + resources/views/admin/shipping/edit.blade.php | 12 +-- resources/views/emails/checkout.blade.php | 8 +- .../views/emails/checkout_status.blade.php | 8 +- resources/views/web/templates/card.blade.php | 1 - 13 files changed, 86 insertions(+), 81 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 68d84af..dfead78 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,17 +3,18 @@ - - - - - - + + + + + + + + + - - @@ -541,12 +543,12 @@ - + - + @@ -565,7 +567,7 @@ file://$PROJECT_DIR$/resources/views/emails/checkout.blade.php - 360 + 362 diff --git a/app/Http/Controllers/ShippingController.php b/app/Http/Controllers/ShippingController.php index 83af29a..1f6733e 100755 --- a/app/Http/Controllers/ShippingController.php +++ b/app/Http/Controllers/ShippingController.php @@ -59,8 +59,8 @@ class ShippingController extends Controller $shipping = false; $data = Request::all(); - if($data['action'] == 'shipping'){ - if ($data['id'] == "new") { + if($data['action'] === 'shipping'){ + if ($data['id'] === "new") { $shipping = new Shipping(); $rules = array('name' => 'required'); } else { @@ -79,7 +79,7 @@ class ShippingController extends Controller $shipping->save(); } - if($data['action'] == 'price'){ + if($data['action'] === 'price'){ $shipping = Shipping::findOrFail($data['shipping_id']); $rules = array('price' => 'required'); $ret = ['value' => $shipping]; @@ -87,7 +87,7 @@ class ShippingController extends Controller if ($validator->fails()) { return view('admin.shipping.edit', $ret)->withErrors($validator); } - if ($data['id'] == "new") { + if ($data['id'] === "new") { $price = ShippingPrice::create($data); } else { $price = ShippingPrice::findOrFail($data['id']); @@ -99,7 +99,7 @@ class ShippingController extends Controller } } - if($data['action'] == 'country'){ + if($data['action'] === 'country'){ $shipping = Shipping::findOrFail($data['shipping_id']); foreach($data['country_ids'] as $country_id){ if(ShippingCountry::where('country_id', $country_id)->count() == 0){ diff --git a/app/Models/Shipping.php b/app/Models/Shipping.php index 20c0427..405ee9c 100644 --- a/app/Models/Shipping.php +++ b/app/Models/Shipping.php @@ -73,4 +73,7 @@ class Shipping extends Model public function prices(){ return $this->hasMany('App\Models\ShippingPrice', 'shipping_id', 'id'); } + public function shipping_prices(){ + return $this->hasMany('App\Models\ShippingPrice', 'shipping_id', 'id'); + } } diff --git a/app/Models/ShippingPrice.php b/app/Models/ShippingPrice.php index cc8ed07..54effe7 100644 --- a/app/Models/ShippingPrice.php +++ b/app/Models/ShippingPrice.php @@ -10,7 +10,7 @@ use Illuminate\Database\Eloquent\Model; * @property int $id * @property int $shipping_id * @property float|null $price - * @property float|null $tax + * @property float|null $tax_rate * @property float|null $factor * @property float|null $total_from * @property float|null $total_to @@ -40,7 +40,7 @@ class ShippingPrice extends Model protected $table = 'shipping_prices'; protected $fillable = [ - 'shipping_id', 'price', 'tax', 'factor', 'total_from', 'total_to', 'weight_from', 'weight_to', + 'shipping_id', 'price', 'tax_rate', 'factor', 'total_from', 'total_to', 'weight_from', 'weight_to', ]; public function shipping() @@ -75,9 +75,9 @@ class ShippingPrice extends Model { $value = $this->_format_number($value); if ($value == "") { - $this->attributes['tax'] = null; + $this->attributes['tax_rate'] = null; } else { - $this->attributes['tax'] = floatval(str_replace(',', '.', $value)); + $this->attributes['tax_rate'] = floatval(str_replace(',', '.', $value)); } } @@ -115,15 +115,15 @@ class ShippingPrice extends Model return number_format($this->attributes['price'], 2, ',', '.'); } - public function getFormattedTax() + public function getFormattedTaxRate() { - if ($this->attributes['tax'] === NULL) { - return $this->attributes['tax']; + if ($this->attributes['tax_rate'] === NULL) { + return $this->attributes['tax_rate']; } if (\App::getLocale() == "en") { - return number_format($this->attributes['tax'], 2, '.', ','); + return number_format($this->attributes['tax_rate'], 2, '.', ','); } - return number_format($this->attributes['tax'], 2, ',', '.'); + return number_format($this->attributes['tax_rate'], 2, ',', '.'); } public function getFormattedFactor() diff --git a/app/Services/Yard.php b/app/Services/Yard.php index a65fdf5..1b3dcc2 100644 --- a/app/Services/Yard.php +++ b/app/Services/Yard.php @@ -11,7 +11,8 @@ use Illuminate\Support\Collection; class Yard extends Cart { - private $shipping = 0; + private $shipping_price = 0; + private $shipping_tax = 0; private $shipping_country_id = 0; //default de private $ysession; private $yinstance; @@ -21,7 +22,11 @@ class Yard extends Cart $this->ysession = $session; $this->yinstance = sprintf('%s.%s', 'cart', 'shipping_extras'); if($this->getShippingExtra('shipping_price')){ - $this->shipping = floatval($this->getShippingExtra('shipping_price')); + $this->shipping_price = (float) ($this->getShippingExtra('shipping_price')); + } + + if($this->getShippingExtra('shipping_tax')){ + $this->shipping_tax = (float) ($this->getShippingExtra('shipping_tax')); } if($this->getShippingExtra('shipping_country_id')){ @@ -32,20 +37,18 @@ class Yard extends Cart if($this->shipping_country_id == 0){ $shippingCountry = ShippingCountry::first(); - $this->shipping_country_id = $shippingCountry->id; - + if($shippingCountry){ + $this->shipping_country_id = $shippingCountry->id; + } } - if($this->shipping == 0){ + if($this->shipping_price == 0){ self::instance('shopping')->setShippingCountryWithPrice($this->shipping_country_id); } - } public static function getTaxRate() { - return config('cart.tax'); - } public function putShippingExtra($key, $value){ @@ -53,7 +56,6 @@ class Yard extends Cart $content = $this->getYContent(); $content->put($key, $value); $this->ysession->put($this->yinstance, $content); - } public function getShippingExtra($key){ @@ -113,24 +115,26 @@ class Yard extends Cart $shippingCountry = ShippingCountry::find($this->shipping_country_id); $shipping = $shippingCountry->shipping; - if(intval($this->weight()) == 0){ - $price = $shipping->prices->first(); - $price->price = 0; + if($this->weight() == 0){ + $shipping_price = $shipping->shipping_prices->first(); + $shipping_price->price = 0; }else{ //first by price - $price = $this->shippingPriceByTotal($shipping->prices, floatval($this->total(2, '.', ','))); + $shipping_price = $this->shippingPriceByTotal($shipping->shipping_prices, $this->total(2, '.', '')); //sec by weight - if(!$price){ - $price = $this->shippingPriceByWeight($shipping->prices, intval($this->weight())); + if(!$shipping_price){ + $shipping_price = $this->shippingPriceByWeight($shipping->shipping_prices, $this->weight()); } //default - if(!$price){ - $price = $shipping->prices->first(); + if(!$shipping_price){ + $shipping_price = $shipping->shipping_prices->first(); } } - if($price){ - $this->shipping = floatval($price->price); - $this->putShippingExtra('shipping_price', $this->shipping); + if($shipping_price){ + $this->shipping_price = $shipping_price->price; + $this->shipping_tax = $shipping_price->tax_rate; + $this->putShippingExtra('shipping_price', $this->shipping_price); + $this->putShippingExtra('shipping_tax', $this->shipping_tax); } } @@ -155,9 +159,6 @@ class Yard extends Cart return false; } - - - /** * @param null $decimals * @param null $decimalPoint @@ -166,26 +167,24 @@ class Yard extends Cart */ public function shipping($decimals = null, $decimalPoint = null, $thousandSeperator = null) { - return $this->numberFormat($this->shipping, $decimals, $decimalPoint, $thousandSeperator); + return $this->numberFormat($this->shipping_price, $decimals, $decimalPoint, $thousandSeperator); } - // - private function shippingTax($taxRate = -19){ - return ($this->shipping * ($taxRate / 100)); + private function shippingTax($decimals = null, $decimalPoint = null, $thousandSeperator = null){ + $shippingTax = $this->shipping_price / (100 + $this->shipping_tax) * $this->shipping_tax; + return $this->numberFormat($shippingTax, $decimals, $decimalPoint, $thousandSeperator); } - private function shippingSub($taxRate){ - return ($this->shipping + $this->shippingTax($taxRate)); + private function subShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null){ + $subShipping = $this->shipping_price - $this->shippingTax($decimals, $decimalPoint, $thousandSeperator); + return $this->numberFormat($subShipping, $decimals, $decimalPoint, $thousandSeperator); } public function subtotalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null) { - $subtotal = $this->subtotal(2, '.', '') + $this->shipping; - //$taxRate =config('cart.tax'); - /*$total = (float) ($this->total(2, '.', '')) + $this->shipping; - $totalTax = $total/ (100 + $taxRate) * $taxRate;*/ + $subtotal = $this->subtotal(2, '.', '') + $this->subShipping(2, '.', ''); return $this->numberFormat($subtotal, $decimals, $decimalPoint, $thousandSeperator); } @@ -193,21 +192,17 @@ class Yard extends Cart public function taxWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null) { $totalTax = $this->tax(2, '.', ''); - /* $taxRate =config('cart.tax'); - $total = (float) ($this->total(2, '.', '')) + $this->shipping; - $totalTax = $total/ (100 + $taxRate) * $taxRate; */ - return $this->numberFormat($totalTax, $decimals, $decimalPoint, $thousandSeperator); + $shippingTax = $this->shippingTax(2, '.', ''); + return $this->numberFormat(($totalTax+$shippingTax), $decimals, $decimalPoint, $thousandSeperator); } public function totalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null) { - $total = (float) ($this->total(2, '.', '')) + $this->shipping; + $total = (float) ($this->total(2, '.', '')) + $this->shipping_price; return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator); } - - /** * Get the total price of the items in the cart. * @@ -220,7 +215,7 @@ class Yard extends Cart { $content = $this->getContent(); $total = $content->reduce(function ($total, CartItem $cartItem) { - return $total + ($cartItem->options->weight ? intval($cartItem->options->weight*$cartItem->qty) : 0); + return $total + ($cartItem->options->weight ? ($cartItem->options->weight*$cartItem->qty) : 0); }, 0); return $total; diff --git a/config/cart.php b/config/cart.php index df3cbc0..5e766e5 100644 --- a/config/cart.php +++ b/config/cart.php @@ -12,7 +12,7 @@ return [ | */ - 'tax' => 19, + 'tax' => 16, /* |-------------------------------------------------------------------------- diff --git a/database/migrations/2019_01_06_004849_create_shipping_prices_table.php b/database/migrations/2019_01_06_004849_create_shipping_prices_table.php index f02e75a..ed7944e 100644 --- a/database/migrations/2019_01_06_004849_create_shipping_prices_table.php +++ b/database/migrations/2019_01_06_004849_create_shipping_prices_table.php @@ -18,7 +18,7 @@ class CreateShippingPricesTable extends Migration $table->unsignedInteger('shipping_id'); $table->decimal('price', 8, 2)->nullable(); - $table->decimal('tax', 5, 2)->nullable(); + $table->decimal('tax_rate', 5, 2)->nullable(); $table->decimal('factor', 5, 2)->nullable(); $table->decimal('total_from', 8, 2)->nullable(); diff --git a/database/migrations/2019_02_23_163724_create_shopping_order_items_table.php b/database/migrations/2019_02_23_163724_create_shopping_order_items_table.php index 70c8d94..a30406a 100644 --- a/database/migrations/2019_02_23_163724_create_shopping_order_items_table.php +++ b/database/migrations/2019_02_23_163724_create_shopping_order_items_table.php @@ -21,6 +21,7 @@ class CreateShoppingOrderItemsTable extends Migration $table->unsignedInteger('product_id'); $table->unsignedInteger('qty'); $table->decimal('price', 8, 2)->nullable(); + $table->decimal('tax_rate', 5, 2)->nullable(); $table->string('slug')->nullable(); $table->timestamps(); diff --git a/resources/lang/de/email.php b/resources/lang/de/email.php index 17b72be..857314d 100644 --- a/resources/lang/de/email.php +++ b/resources/lang/de/email.php @@ -48,6 +48,7 @@ 'checkout_mail_shipping' => 'Verpackungs- u. Versandkosten', 'checkout_mail_status_info' => 'Statusinfo:', 'checkout_mail_total' => 'Gesamtpreis', + 'checkout_mail_tax_info' => 'Preis inkl. MwSt', 'checkout_mail_pay_info' => 'Zahlungsinfo:', 'checkout_mail_pay_success' => 'Zahlung ist bestÃĪtigt!', 'checkout_mail_pay_with' => 'Zahlung mit:', diff --git a/resources/views/admin/shipping/edit.blade.php b/resources/views/admin/shipping/edit.blade.php index a8e663e..f6a3dd6 100755 --- a/resources/views/admin/shipping/edit.blade.php +++ b/resources/views/admin/shipping/edit.blade.php @@ -83,7 +83,7 @@