File: /home/armgroup/libs/app/Http/Livewire/Admin/ShippingPrice/Create.php
<?php
namespace App\Http\Livewire\Admin\ShippingPrice;
use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Http\Livewire\General;
use App\Models\ShippingPrice;
use Livewire\Component;
class Create extends Component
{
use ConvertEmptyStringsToNull, General;
public $shippingPrices = [];
public $deleteIds = [];
public function mount()
{
$this->shippingPrices = ShippingPrice::query()
->select('id', 'from', 'up_to', 'my_province_courier', 'my_province_express', 'neighbor_province_courier', 'neighbor_province_express', 'not_neighbor_province_courier', 'not_neighbor_province_express')
->get()
->toArray();
}
public function render()
{
return view('livewire.admin.shipping-price.create');
}
protected function rules()
{
return [
'shippingPrices.*.from' => ['required', 'integer', 'min:0', 'max:100000000', 'distinct'],
'shippingPrices.*.up_to' => ['nullable', 'integer', 'min:1', 'max:100000000', 'distinct', 'gt:shippingPrices.*.from', 'distinct'],
'shippingPrices.*.my_province_courier' => ['nullable', 'integer', 'min:0', 'max:100000000'],
'shippingPrices.*.my_province_express' => ['nullable', 'integer', 'min:0', 'max:100000000'],
'shippingPrices.*.neighbor_province_courier' => ['nullable', 'integer', 'min:0', 'max:100000000'],
'shippingPrices.*.neighbor_province_express' => ['nullable', 'integer', 'min:0', 'max:100000000'],
'shippingPrices.*.not_neighbor_province_courier' => ['nullable', 'integer', 'min:0', 'max:100000000'],
'shippingPrices.*.not_neighbor_province_express' => ['nullable', 'integer', 'min:0', 'max:100000000'],
];
}
protected $validationAttributes = [
'shippingPrices.*.from' => 'از وزن',
'shippingPrices.*.up_to' => 'تا وزن',
'shippingPrices.*.my_province_courier' => 'پیک درون استانی',
'shippingPrices.*.my_province_express' => 'پیشتاز درون استانی',
'shippingPrices.*.neighbor_province_courier' => 'پیک استان همجوار',
'shippingPrices.*.neighbor_province_express' => 'پیشتاز استان همجوار',
'shippingPrices.*.not_neighbor_province_courier' => 'پیک استان غیرهمجوار',
'shippingPrices.*.not_neighbor_province_express' => 'پیشتاز استان غیرهمجوار',
];
public function addItem()
{
array_push($this->shippingPrices, [
'id' => null,
'from' => null,
'up_to' => null,
'my_province_courier' => null,
'my_province_express' => null,
'neighbor_province_courier' => null,
'neighbor_province_express' => null,
'not_neighbor_province_courier' => null,
'not_neighbor_province_express' => null,
]);
}
public function removeItem($index)
{
if ($this->shippingPrices[$index]['id']) {
$this->deleteIds[] = $this->shippingPrices[$index]['id'];
}
unset($this->shippingPrices[$index]);
}
public function store()
{
$this->validate();
if (! count($this->shippingPrices)) {
ShippingPrice::delete();
} else {
if (count($this->deleteIds)) {
ShippingPrice::whereIn('id', $this->deleteIds)->delete();
}
ShippingPrice::upsert(
$this->shippingPrices,
['id'],
['from', 'up_to', 'my_province_courier', 'my_province_express', 'neighbor_province_courier', 'neighbor_province_express', 'not_neighbor_province_courier', 'not_neighbor_province_express']
);
}
saveActivity('هزینههای ارسال مقداردهی شد');
$this->doneMessage();
return redirect()->route('admin.shipping-prices.index');
}
}