File: D:/HostingSpaces/SBogers10/topswtwmobile.komma.pro/app/KommaApp/Shop/Products/ProductEntity.php
<?php
namespace KommaApp\Shop\Products;
use Carbon\Carbon;
use KommaApp\Shop\Entities\AbstractEntity;
class ProductEntity extends AbstractEntity
{
public $id = null;
public $internal_article_number = '';
public $pick_number = '';
public $article_number = '';
public $bypass = false;
public $filter_class = '';
public $pollen_filter = 0;
public $fine_dust_filter = false;
public $size = '';
public $unit_type = '';
public $volume = '';
public $brand_name = '';
public $supplier_name = '';
public $is_filter_set = null;
public $product_group = '';
public $images = [];
public $discounts = [];
public $large_brand_image_url = '';
public $small_brand_image_url = '';
public $price = 0;
public $name = '';
public $filter_type = '';
public $description = '';
public $composition = '';
public $composition_formatted = '';
public $model = '';
public $notes = '';
public $warning = '';
public $meta_title = '';
public $meta_description = '';
public $special_1 = '';
public $special_2 = '';
public $category_id = null;
public $route = '';
public $tax_name = '';
public $tax_rate = '';
public function setComposition($composition)
{
$this->composition = $composition;
$this->composition_formatted = $this->getFormattedComposition();
}
public function getFormattedComposition()
{
if( ! empty($this->composition))
{
return \View::make('partials.composition')->withComposition($this->composition)->render();
}
return '';
}
public function getParent()
{
if($this->parent != null) return $this->parent;
return false;
}
public function getPriceExVat($quantity = 1)
{
return round(($this->price * $quantity) / ((100 + $this->tax_rate) / 100));
}
public function getOriginalPrice($quantity)
{
return $this->price * $quantity;
}
public function getDiscountPriceExVat($quantity, $couponCode = null, $batchId = null)
{
return round($this->getDiscountPrice($quantity, $couponCode, $batchId) / ((100 + $this->tax_rate) / 100));
}
public function getDiscountPrice($quantity, $couponCode = null, $batchId = null)
{
$discountPrice = $this->price * $quantity;
$applicableDiscounts = $this->getApplicableDiscountRules($quantity, $couponCode, $batchId);
foreach($applicableDiscounts as $discount)
{
if(is_numeric($discount->discount_percentage))
{
$discountPrice = $discountPrice * (1-($discount->discount_percentage / 100));
}
// Gratis producten worden niet meegenomen in de prijs.
/*if(is_numeric($discount->discount_fraction))
{
$discountPrice -= $this->price * $discount->discount_fraction;
}*/
if(is_numeric($discount->discount_absolute))
{
$discountPrice -= $discount->discount_absolute;
}
}
return round($discountPrice);
}
public function getNumberOfFreeProducts($quantity, $couponCode = null, $batchId = null)
{
$nFreeProducts = 0;
$applicableDiscounts = $this->getApplicableDiscountRules($quantity, $couponCode, $batchId);
foreach($applicableDiscounts as $discount)
{
if(is_numeric($discount->discount_fraction))
{
//Check if the discount is cumulative
if(isset($discount->is_cumulative ) && $discount->is_cumulative == 1){
//Yes. floor() the fraction of the quantity to the min quantity and multiply by the fraction, add this to free products
$nFreeProducts += floor($quantity/$discount->quantity_min)*$discount->discount_fraction;
continue;
}
//No
$nFreeProducts += $discount->discount_fraction;
}
}
return $nFreeProducts;
}
public function getDiscountSummary($quantity, $couponCode = null, $batchId = null, $exTax = false)
{
$applicableDiscounts = $this->getApplicableDiscountRules($quantity, $couponCode, $batchId);
$output = [];
foreach($applicableDiscounts as $discount)
{
if(is_numeric($discount->discount_percentage))
{
//$output[] = $discount->discount_percentage.'%';
// Tops specifiek: QnD - Percentage als eerste laten zien
array_unshift($output,$discount->discount_percentage . '%');
}
if(is_numeric($discount->discount_fraction))
{
$amount = $discount->discount_fraction;
if($discount->is_cumulative){
$amount = floor($quantity/$discount->quantity_min);
}
$output[] = '<span class="small">' . $amount . ' '.\Lang::get('pages/products.free').'</span>';
}
if(is_numeric($discount->discount_absolute))
{
$discountAbsolute = $exTax ? $discount->discount_absolute / ((100 + $this->tax_rate) / 100) : $discount->discount_absolute;
$output[] = \Format::asPrice($discountAbsolute, ['currency' => '€']);
}
}
if(count($output) < 1) return '-';
return implode('<br/>', $output);
}
public function hasDiscount($quantity, $couponCode = null, $batchId = null)
{
return count($this->getApplicableDiscountRules($quantity, $couponCode, $batchId)) > 0;
}
public function hasNoShippingCosts($quantity, $couponCode = null, $batchId = null)
{
$discounts = $this->getApplicableDiscountRules($quantity, $couponCode, $batchId);
foreach($discounts as $discount)
{
if($discount->discount_no_shipping_costs == 1) return true;
}
return false;
}
protected function getApplicableDiscountRules($quantity, $couponCode = null, $batchId = null)
{
if($couponCode) $couponCode = sha1($couponCode);
$applicableDiscounts = [];
foreach($this->discounts as $discount)
{
if(
($discount->quantity_min == null or $discount->quantity_min <= $quantity) and
($discount->quantity_max == null or $discount->quantity_max >= $quantity) and
($discount->date_min == null or Carbon::parse($discount->date_min) <= Carbon::now()) and
($discount->date_max == null or Carbon::parse($discount->date_max) >= Carbon::now()) and
($discount->custom_coupon_code == null or $discount->custom_coupon_code == $couponCode) and
($discount->coupon_batch_id == null or $discount->coupon_batch_id == $batchId)
){
$applicableDiscounts[] = $discount;
if($discount->is_final == 1) break;
}
}
return $applicableDiscounts;
}
public function getName(){
$title_parts = [];
if (strtolower($this->brand_name) != 'toebehoren') {
$title_parts[] = $this->brand_name;
}
$title_parts[] = $this->name;
$title_parts[] = $this->filter_class;
return implode(' ', $title_parts);
}
}