231 lines
6.5 KiB
PHP
Raw Normal View History

2020-12-24 01:28:38 +03:00
<?php
2022-06-01 10:15:55 +03:00
namespace App\View\Components\Documents\Form;
2020-12-24 01:28:38 +03:00
2022-06-01 10:15:55 +03:00
use App\Abstracts\View\Component;
use App\Models\Common\Contact as Model;
2020-12-24 01:28:38 +03:00
use Illuminate\Support\Str;
2022-06-01 10:15:55 +03:00
class Contact extends Component
2020-12-24 01:28:38 +03:00
{
public $type;
public $contact;
public $placeholder;
public $contacts;
public $searchRoute;
2020-12-24 01:28:38 +03:00
public $createRoute;
2020-12-24 01:28:38 +03:00
2020-12-28 22:58:28 +03:00
/** @var string */
public $textAddContact;
/** @var string */
public $textCreateNewContact;
/** @var string */
public $textEditContact;
/** @var string */
public $textContactInfo;
/** @var string */
public $textChooseDifferentContact;
/** @var $error */
public $error;
2020-12-24 01:28:38 +03:00
/**
* Create a new component instance.
*
* @return void
*/
2020-12-28 22:58:28 +03:00
public function __construct(
$type, $contact = false, $contacts = [], $searchRoute = '', $createRoute = '', string $error = '',
$textAddContact = '', $textCreateNewContact = '', $textEditContact = '', $textContactInfo = '', $textChooseDifferentContact = ''
2022-06-01 10:15:55 +03:00
) {
2020-12-24 01:28:38 +03:00
$this->type = $type;
$this->contact = $contact;
$this->contacts = $contacts;
$this->searchRoute = $searchRoute;
$this->createRoute = $createRoute;
$this->error = ($error) ?: "form.errors.get('contact_id')" ;
2020-12-28 22:58:28 +03:00
$this->textAddContact = $this->getTextAddContact($type, $textAddContact);
$this->textCreateNewContact = $this->getTextCreateNewContact($type, $textCreateNewContact);
$this->textEditContact = $this->getTextEditContact($type, $textEditContact);
$this->textContactInfo = $this->getTextContactInfo($type, $textContactInfo);
$this->textChooseDifferentContact = $this->getTextChooseDifferentContact($type, $textChooseDifferentContact);
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()
{
if (empty($this->contacts)) {
2022-06-01 10:15:55 +03:00
$this->contacts = Model::{$this->type}()->enabled()->orderBy('name')->take(setting('default.select_limit'))->get();
if (!empty($this->contact) && (!$this->contacts->contains('id', $contact->id))) {
$this->contacts->push($this->contact);
}
2020-12-24 01:28:38 +03:00
}
if (empty($this->searchRoute)) {
2020-12-24 01:28:38 +03:00
switch ($this->type) {
case 'customer':
$this->searchRoute = route('customers.index');
2020-12-24 01:28:38 +03:00
break;
case 'vendor':
$this->searchRoute = route('vendors.index');
2020-12-24 01:28:38 +03:00
break;
}
}
if (empty($this->createRoute)) {
2020-12-24 01:28:38 +03:00
switch ($this->type) {
case 'customer':
$this->createRoute = route('modals.customers.create');
2020-12-24 01:28:38 +03:00
break;
case 'vendor':
$this->createRoute = route('modals.vendors.create');
2020-12-24 01:28:38 +03:00
break;
}
}
#todo 3rd part apps
2021-01-22 16:01:48 +03:00
$this->placeholder = trans('general.placeholder.contact_search', ['type' => trans_choice('general.' . Str::plural($this->type, 2), 1)]);
2020-12-24 01:28:38 +03:00
2022-06-01 10:15:55 +03:00
return view('components.documents.form.contact');
2020-12-24 01:28:38 +03:00
}
2020-12-28 22:58:28 +03:00
protected function getTextAddContact($type, $textAddContact)
{
if (!empty($textAddContact)) {
return $textAddContact;
}
switch ($type) {
case 'bill':
case 'expense':
case 'purchase':
$textAddContact = [
'general.form.add',
'general.vendors'
];
2020-12-28 22:58:28 +03:00
break;
default:
$textAddContact = [
'general.form.add',
'general.customers'
];
2020-12-28 22:58:28 +03:00
break;
}
return $textAddContact;
}
protected function getTextCreateNewContact($type, $textCreateNewContact)
{
2022-06-07 17:19:49 +03:00
if (!empty($textCreateNewContact) && is_array($textCreateNewContact)) {
return trans($textCreateNewContact[0], ['type' => trans_choice($textCreateNewContact[1], 1)]);
2020-12-28 22:58:28 +03:00
}
switch ($type) {
case 'bill':
case 'expense':
case 'purchase':
$textCreateNewContact = [
'general.form.add_new',
'general.vendors'
];
2020-12-28 22:58:28 +03:00
break;
default:
$textCreateNewContact = [
'general.form.add_new',
'general.customers'
];
2020-12-28 22:58:28 +03:00
break;
}
return $textCreateNewContact;
}
protected function getTextEditContact($type, $textEditContact)
{
if (!empty($textEditContact)) {
return $textEditContact;
}
switch ($type) {
case 'bill':
case 'expense':
case 'purchase':
$textEditContact = [
'general.form.contact_edit',
'general.vendors'
];
2020-12-28 22:58:28 +03:00
break;
default:
$textEditContact = [
'general.form.contact_edit',
'general.customers'
];
2020-12-28 22:58:28 +03:00
break;
}
return $textEditContact;
}
protected function getTextContactInfo($type, $textContactInfo)
{
if (!empty($textContactInfo)) {
return $textContactInfo;
}
switch ($type) {
case 'bill':
case 'expense':
case 'purchase':
$textContactInfo = 'bills.bill_from';
2020-12-28 22:58:28 +03:00
break;
default:
$textContactInfo = 'invoices.bill_to';
2020-12-28 22:58:28 +03:00
break;
}
return $textContactInfo;
}
protected function getTextChooseDifferentContact($type, $textChooseDifferentContact)
{
if (!empty($textChooseDifferentContact)) {
return $textChooseDifferentContact;
}
switch ($type) {
case 'bill':
case 'expense':
case 'purchase':
$textChooseDifferentContact = [
'general.form.choose_different',
'general.vendors'
];
2020-12-28 22:58:28 +03:00
break;
default:
$textChooseDifferentContact = [
'general.form.choose_different',
'general.customers'
];
2020-12-28 22:58:28 +03:00
break;
}
return $textChooseDifferentContact;
}
2020-12-24 01:28:38 +03:00
}