akaunting/app/View/Components/SelectItemButton.php

82 lines
1.8 KiB
PHP
Raw Normal View History

2020-12-24 01:28:38 +03:00
<?php
namespace App\View\Components;
use Illuminate\View\Component;
use App\Models\Common\Item;
class SelectItemButton extends Component
{
/** @var string */
public $type;
/** @var bool */
public $isSale;
/** @var bool */
public $isPurchase;
2021-07-27 17:56:35 +03:00
/** @var int */
public $searchCharLimit;
2020-12-24 01:28:38 +03:00
/**
* Create a new component instance.
*
* @return void
*/
2021-07-27 17:56:35 +03:00
public function __construct(string $type = 'sale', bool $isSale = false, bool $isPurchase = false, int $searchCharLimit = 3)
2020-12-24 01:28:38 +03:00
{
$this->type = $type;
$this->isSale = $isSale;
$this->isPurchase = $isPurchase;
2021-07-27 17:56:35 +03:00
$this->searchCharLimit = $searchCharLimit;
2020-12-24 01:28:38 +03:00
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
$items = Item::enabled()->orderBy('name')->take(setting('default.select_limit'))->get();
$price_type = $this->getPriceType($this->type, $this->isSale, $this->isPurchase);
2020-12-24 01:28:38 +03:00
foreach ($items as $item) {
2020-12-28 17:17:42 +03:00
$price = $item->{$price_type . '_price'};
2020-12-24 01:28:38 +03:00
$item->price = $price;
}
$price = $price_type . '_price';
2020-12-24 01:28:38 +03:00
return view('components.select-item-button', compact('items', 'price'));
}
2020-12-28 17:17:42 +03:00
protected function getPriceType($type, $is_sale, $is_purchase)
{
if (!empty($is_sale)) {
return 'sale';
}
if (!empty($is_purchase)) {
2020-12-28 17:17:42 +03:00
return 'purchase';
}
switch ($type) {
case 'bill':
case 'expense':
case 'purchase':
$type = 'purchase';
break;
case 'sale':
case 'income':
case 'invoice':
default:
$type = 'sale';
}
return $type;
}
2020-12-24 01:28:38 +03:00
}