1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/Http/Livewire/RequiredClientInfo.php

294 lines
7.8 KiB
PHP
Raw Normal View History

2021-01-12 15:43:07 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-01-12 15:43:07 +01:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2021-01-12 15:43:07 +01:00
*/
namespace App\Http\Livewire;
use App\Libraries\MultiDB;
2021-01-12 16:30:11 +01:00
use App\Models\ClientContact;
2022-09-13 01:59:16 +02:00
use App\Models\CompanyGateway;
use App\Models\Invoice;
use App\Utils\Traits\MakesHash;
use Illuminate\Support\Facades\Cache;
2021-01-12 15:43:07 +01:00
use Illuminate\Support\Facades\Validator;
2021-01-12 16:23:39 +01:00
use Illuminate\Support\Str;
2021-01-12 15:43:07 +01:00
use Livewire\Component;
class RequiredClientInfo extends Component
{
use MakesHash;
/**
* @var bool
*/
public $show_terms = false;
/**
* @var array
*/
public $invoice;
/**
* @var array
*/
2021-01-12 15:43:07 +01:00
public $fields = [];
2021-01-12 16:30:11 +01:00
/**
* @var ClientContact
*/
2021-01-12 16:23:39 +01:00
public $contact;
/**
* @var Client
*/
2022-03-08 13:33:20 +01:00
public $client;
/**
* @var array
*/
public $countries;
2021-01-12 16:30:11 +01:00
/**
* Mappings for updating the database. Left side is mapping from gateway,
* right side is column in database.
*
* @var string[]
*/
2021-01-12 16:23:39 +01:00
private $mappings = [
'client_name' => 'name',
2021-01-12 16:30:11 +01:00
'client_website' => 'website',
'client_phone' => 'phone',
'client_address_line_1' => 'address1',
'client_address_line_2' => 'address2',
'client_city' => 'city',
'client_state' => 'state',
'client_postal_code' => 'postal_code',
'client_country_id' => 'country_id',
'client_shipping_address_line_1' => 'shipping_address1',
'client_shipping_address_line_2' => 'shipping_address2',
'client_shipping_city' => 'shipping_city',
'client_shipping_state' => 'shipping_state',
'client_shipping_postal_code' => 'shipping_postal_code',
'client_shipping_country_id' => 'shipping_country_id',
'contact_first_name' => 'first_name',
'contact_last_name' => 'last_name',
'contact_email' => 'email',
2021-11-24 04:31:05 +01:00
// 'contact_phone' => 'phone',
2021-01-12 16:23:39 +01:00
];
2022-03-08 13:09:20 +01:00
public $client_address_array = [
2022-03-08 13:33:20 +01:00
'address1',
'address2',
2022-03-08 13:09:20 +01:00
'city',
'state',
'postal_code',
'country_id',
2022-07-14 13:29:17 +02:00
'shipping_address1',
'shipping_address2',
'shipping_city',
'shipping_state',
'shipping_postal_code',
2022-07-14 02:39:55 +02:00
'shipping_country_id',
2022-03-08 13:09:20 +01:00
];
2022-03-08 13:33:20 +01:00
protected $rules = [
'client.address1' => '',
'client.address2' => '',
'client.city' => '',
'client.state' => '',
'client.postal_code' => '',
'client.country_id' => '',
2022-03-10 03:05:01 +01:00
'client.shipping_address1' => '',
'client.shipping_address2' => '',
'client.shipping_city' => '',
'client.shipping_state' => '',
'client.shipping_postal_code' => '',
'client.shipping_country_id' => '',
'contact.first_name' => '',
'contact.last_name' => '',
'contact.email' => '',
'client.name' => '',
'client.website' => '',
'client.phone' => '',
2022-03-08 13:33:20 +01:00
];
public $show_form = false;
public $company;
2022-09-13 01:59:16 +02:00
public $company_gateway_id;
public function mount()
{
MultiDB::setDb($this->company->db);
2022-03-08 13:33:20 +01:00
$this->client = $this->contact->client;
2022-03-10 03:05:01 +01:00
count($this->fields) > 0
? $this->checkFields()
: $this->show_form = false;
if($this->company->settings->show_accept_invoice_terms && request()->has('hash'))
{
$this->show_terms = true;
$hash = Cache::get(request()->input('hash'));
$this->invoice = Invoice::find($this->decodePrimaryKey($hash['invoice_id']));
}
}
2021-01-25 10:07:12 +01:00
2021-01-12 15:43:07 +01:00
public function handleSubmit(array $data): bool
{
$rules = [];
collect($this->fields)->map(function ($field) use (&$rules) {
if (! array_key_exists('filled', $field)) {
$rules[$field['name']] = array_key_exists('validation_rules', $field)
? $field['validation_rules']
: 'required';
}
2021-01-12 15:43:07 +01:00
});
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
session()->flash('validation_errors', $validator->getMessageBag()->getMessages());
return false;
}
2021-01-12 16:23:39 +01:00
if ($this->updateClientDetails($data)) {
$this->emit('passed-required-fields-check', [
'client_postal_code' => $this->contact->client->postal_code,
]);
2021-01-12 16:23:39 +01:00
2022-09-13 01:59:16 +02:00
//if stripe is enabled, we want to update the customer at this point.
2021-01-12 16:23:39 +01:00
return true;
}
// TODO: Throw an exception about not being able to update the profile.
return false;
}
private function updateClientDetails(array $data): bool
{
2022-09-13 01:59:16 +02:00
nlog($this->company->id);
2021-01-12 16:23:39 +01:00
$client = [];
$contact = [];
foreach ($data as $field => $value) {
2021-01-12 16:30:11 +01:00
if (Str::startsWith($field, 'client_')) {
2021-01-12 16:23:39 +01:00
$client[$this->mappings[$field]] = $value;
}
if (Str::startsWith($field, 'contact_')) {
$contact[$this->mappings[$field]] = $value;
}
}
$contact_update = $this->contact
->fill($contact)
->push();
$client_update = $this->contact->client
->fill($client)
->push();
if ($contact_update && $client_update) {
2022-09-13 01:59:16 +02:00
$cg = CompanyGateway::find($this->company_gateway_id);
if($cg && $cg->update_details){
$payment_gateway = $cg->driver($this->client)->init();
if(method_exists($payment_gateway, "updateCustomer"))
$payment_gateway->updateCustomer();
}
2021-01-12 16:23:39 +01:00
return true;
}
2021-01-12 15:43:07 +01:00
2021-01-12 16:23:39 +01:00
return false;
2021-01-12 15:43:07 +01:00
}
public function checkFields()
{
foreach ($this->fields as $index => $field) {
$_field = $this->mappings[$field['name']];
if (Str::startsWith($field['name'], 'client_')) {
2022-03-08 13:09:20 +01:00
if (empty($this->contact->client->{$_field}) || is_null($this->contact->client->{$_field}) || in_array($_field, $this->client_address_array)) {
$this->show_form = true;
} else {
$this->fields[$index]['filled'] = true;
}
}
if (Str::startsWith($field['name'], 'contact_')) {
2022-01-11 09:42:00 +01:00
if (empty($this->contact->{$_field}) || is_null($this->contact->{$_field})) {
$this->show_form = true;
} else {
$this->fields[$index]['filled'] = true;
}
}
}
if($this->show_terms)
$this->show_form = true;
}
public function showCopyBillingCheckbox(): bool
{
$fields = [];
collect($this->fields)->map(function ($field) use (&$fields) {
if (! array_key_exists('filled', $field)) {
$fields[] = $field['name'];
}
});
foreach ($fields as $field) {
if (Str::startsWith($field, 'client_shipping')) {
return true;
}
}
return false;
}
public function handleCopyBilling(): void
{
$this->emit('update-shipping-data', [
'client_shipping_address_line_1' => $this->contact->client->address1,
'client_shipping_address_line_2' => $this->contact->client->address2,
'client_shipping_city' => $this->contact->client->city,
'client_shipping_state' => $this->contact->client->state,
'client_shipping_postal_code' => $this->contact->client->postal_code,
'client_shipping_country_id' => $this->contact->client->country_id,
]);
}
2021-01-12 15:43:07 +01:00
public function render()
{
return render('components.livewire.required-client-info');
}
}