Updated: ProductCart Component (Livewire)
This commit is contained in:
parent
ef77acd2f0
commit
5c24e3be49
|
@ -22,7 +22,7 @@ class ProductCreateRequest extends FormRequest
|
||||||
'product_cost' => ['required', 'numeric', 'max:2147483647'],
|
'product_cost' => ['required', 'numeric', 'max:2147483647'],
|
||||||
'product_price' => ['required', 'numeric', 'max:2147483647'],
|
'product_price' => ['required', 'numeric', 'max:2147483647'],
|
||||||
'product_stock_alert' => ['required', 'integer', 'min:0'],
|
'product_stock_alert' => ['required', 'integer', 'min:0'],
|
||||||
'product_order_tax' => ['nullable', 'integer', 'min:1'],
|
'product_order_tax' => ['nullable', 'integer', 'min:0', 'max:100'],
|
||||||
'product_tax_type' => ['nullable', 'integer'],
|
'product_tax_type' => ['nullable', 'integer'],
|
||||||
'product_note' => ['nullable', 'string', 'max:1000'],
|
'product_note' => ['nullable', 'string', 'max:1000'],
|
||||||
'category_id' => ['required', 'integer']
|
'category_id' => ['required', 'integer']
|
||||||
|
|
|
@ -23,7 +23,7 @@ class ProductUpdateRequest extends FormRequest
|
||||||
'product_cost' => ['required', 'numeric', 'max:2147483647'],
|
'product_cost' => ['required', 'numeric', 'max:2147483647'],
|
||||||
'product_price' => ['required', 'numeric', 'max:2147483647'],
|
'product_price' => ['required', 'numeric', 'max:2147483647'],
|
||||||
'product_stock_alert' => ['required', 'integer', 'min:0'],
|
'product_stock_alert' => ['required', 'integer', 'min:0'],
|
||||||
'product_order_tax' => ['nullable', 'integer', 'min:1'],
|
'product_order_tax' => ['nullable', 'integer', 'min:0', 'max:100'],
|
||||||
'product_tax_type' => ['nullable', 'integer'],
|
'product_tax_type' => ['nullable', 'integer'],
|
||||||
'product_note' => ['nullable', 'string', 'max:1000'],
|
'product_note' => ['nullable', 'string', 'max:1000'],
|
||||||
'category_id' => ['required', 'integer']
|
'category_id' => ['required', 'integer']
|
||||||
|
|
|
@ -95,7 +95,7 @@
|
||||||
<div class="col-md-6">
|
<div class="col-md-6">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="product_stock_alert">Alert Quantity <span class="text-danger">*</span></label>
|
<label for="product_stock_alert">Alert Quantity <span class="text-danger">*</span></label>
|
||||||
<input type="number" class="form-control" name="product_stock_alert" required value="{{ old('product_stock_alert') }}" min="0">
|
<input type="number" class="form-control" name="product_stock_alert" required value="{{ old('product_stock_alert') }}" min="0" max="100">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -104,14 +104,14 @@
|
||||||
<div class="col-md-6">
|
<div class="col-md-6">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="product_order_tax">Tax (%)</label>
|
<label for="product_order_tax">Tax (%)</label>
|
||||||
<input type="number" class="form-control" name="product_order_tax" value="{{ $product->product_order_tax }}" min="1">
|
<input type="number" class="form-control" name="product_order_tax" value="{{ $product->product_order_tax }}" min="0" max="100">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-6">
|
<div class="col-md-6">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="product_tax_type">Tax type</label>
|
<label for="product_tax_type">Tax type</label>
|
||||||
<select class="form-control" name="product_tax_type" id="product_tax_type">
|
<select class="form-control" name="product_tax_type" id="product_tax_type">
|
||||||
<option value="" selected disabled>Select Tax Type</option>
|
<option value="" selected>None</option>
|
||||||
<option {{ $product->product_tax_type == 1 ? 'selected' : '' }} value="1">Exclusive</option>
|
<option {{ $product->product_tax_type == 1 ? 'selected' : '' }} value="1">Exclusive</option>
|
||||||
<option {{ $product->product_tax_type == 2 ? 'selected' : '' }} value="2">Inclusive</option>
|
<option {{ $product->product_tax_type == 2 ? 'selected' : '' }} value="2">Inclusive</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
|
@ -76,6 +76,7 @@ class SaleController extends Controller
|
||||||
'unit_price' => $cart_item->options->unit_price * 100,
|
'unit_price' => $cart_item->options->unit_price * 100,
|
||||||
'sub_total' => $cart_item->options->sub_total * 100,
|
'sub_total' => $cart_item->options->sub_total * 100,
|
||||||
'product_discount_amount' => $cart_item->options->product_discount * 100,
|
'product_discount_amount' => $cart_item->options->product_discount * 100,
|
||||||
|
'product_discount_type' => $cart_item->options->product_discount_type,
|
||||||
'product_tax_amount' => $cart_item->options->product_tax * 100,
|
'product_tax_amount' => $cart_item->options->product_tax * 100,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -121,6 +122,7 @@ class SaleController extends Controller
|
||||||
'weight' => 1,
|
'weight' => 1,
|
||||||
'options' => [
|
'options' => [
|
||||||
'product_discount' => $sale_detail->product_discount_amount,
|
'product_discount' => $sale_detail->product_discount_amount,
|
||||||
|
'product_discount_type' => $sale_detail->product_discount_type,
|
||||||
'sub_total' => $sale_detail->sub_total,
|
'sub_total' => $sale_detail->sub_total,
|
||||||
'code' => $sale_detail->product_code,
|
'code' => $sale_detail->product_code,
|
||||||
'stock' => Product::findOrFail($sale_detail->product_id)->product_quantity,
|
'stock' => Product::findOrFail($sale_detail->product_id)->product_quantity,
|
||||||
|
@ -185,6 +187,7 @@ class SaleController extends Controller
|
||||||
'unit_price' => $cart_item->options->unit_price * 100,
|
'unit_price' => $cart_item->options->unit_price * 100,
|
||||||
'sub_total' => $cart_item->options->sub_total * 100,
|
'sub_total' => $cart_item->options->sub_total * 100,
|
||||||
'product_discount_amount' => $cart_item->options->product_discount * 100,
|
'product_discount_amount' => $cart_item->options->product_discount * 100,
|
||||||
|
'product_discount_type' => $cart_item->options->product_discount_type,
|
||||||
'product_tax_amount' => $cart_item->options->product_tax * 100,
|
'product_tax_amount' => $cart_item->options->product_tax * 100,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -207,5 +210,11 @@ class SaleController extends Controller
|
||||||
|
|
||||||
public function destroy(Sale $sale) {
|
public function destroy(Sale $sale) {
|
||||||
abort_if(Gate::denies('delete_sales'), 403);
|
abort_if(Gate::denies('delete_sales'), 403);
|
||||||
|
|
||||||
|
$sale->delete();
|
||||||
|
|
||||||
|
toast('Sale Deleted!', 'warning');
|
||||||
|
|
||||||
|
return redirect()->route('sales.index');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<livewire:product-cart :cartInstance="'sale'" :sale="$sale"/>
|
<livewire:product-cart :cartInstance="'sale'" :data="$sale"/>
|
||||||
|
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<div class="col-lg-4">
|
<div class="col-lg-4">
|
||||||
|
|
|
@ -1,24 +1,30 @@
|
||||||
@can('edit_sales')
|
<div class="btn-group dropleft">
|
||||||
<a href="{{ route('sales.edit', $data->id) }}" class="btn btn-info btn-sm">
|
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
|
||||||
<i class="bi bi-pencil"></i>
|
<i class="bi bi-three-dots-vertical"></i>
|
||||||
|
</button>
|
||||||
|
<div class="dropdown-menu">
|
||||||
|
@can('edit_sales')
|
||||||
|
<a href="{{ route('sales.edit', $data->id) }}" class="dropdown-item">
|
||||||
|
<i class="bi bi-pencil mr-2" style="line-height: 1;"></i> Edit
|
||||||
</a>
|
</a>
|
||||||
@endcan
|
@endcan
|
||||||
@can('show_sales')
|
@can('show_sales')
|
||||||
<a href="{{ route('sales.show', $data->id) }}" class="btn btn-primary btn-sm">
|
<a href="{{ route('sales.show', $data->id) }}" class="dropdown-item">
|
||||||
<i class="bi bi-eye"></i>
|
<i class="bi bi-eye mr-2" style="line-height: 1;"></i> Details
|
||||||
</a>
|
</a>
|
||||||
@endcan
|
@endcan
|
||||||
@can('delete_sales')
|
@can('delete_sales')
|
||||||
<button id="delete" class="btn btn-danger btn-sm" onclick="
|
<button id="delete" class="dropdown-item" onclick="
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
if (confirm('Are you sure? It will delete the dNPata permanently!')) {
|
if (confirm('Are you sure? It will delete the dNPata permanently!')) {
|
||||||
document.getElementById('destroy{{ $data->id }}').submit()
|
document.getElementById('destroy{{ $data->id }}').submit()
|
||||||
}
|
}">
|
||||||
">
|
<i class="bi bi-trash mr-2" style="line-height: 1;"></i> Delete
|
||||||
<i class="bi bi-trash"></i>
|
|
||||||
<form id="destroy{{ $data->id }}" class="d-none" action="{{ route('sales.destroy', $data->id) }}" method="POST">
|
<form id="destroy{{ $data->id }}" class="d-none" action="{{ route('sales.destroy', $data->id) }}" method="POST">
|
||||||
@csrf
|
@csrf
|
||||||
@method('delete')
|
@method('delete')
|
||||||
</form>
|
</form>
|
||||||
</button>
|
</button>
|
||||||
@endcan
|
@endcan
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
|
@ -52,6 +52,12 @@ class PermissionsTableSeeder extends Seeder
|
||||||
'show_suppliers',
|
'show_suppliers',
|
||||||
'edit_suppliers',
|
'edit_suppliers',
|
||||||
'delete_suppliers',
|
'delete_suppliers',
|
||||||
|
//Sales
|
||||||
|
'access_sales',
|
||||||
|
'create_sales',
|
||||||
|
'show_sales',
|
||||||
|
'edit_sales',
|
||||||
|
'delete_sales',
|
||||||
//Currencies
|
//Currencies
|
||||||
'access_currencies',
|
'access_currencies',
|
||||||
'create_currencies',
|
'create_currencies',
|
||||||
|
|
|
@ -9,7 +9,7 @@ use Livewire\Component;
|
||||||
class ProductCart extends Component
|
class ProductCart extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
public $listeners = ['productSelected'];
|
public $listeners = ['productSelected', 'discountModalRefresh'];
|
||||||
|
|
||||||
public $cart_instance;
|
public $cart_instance;
|
||||||
public $global_discount;
|
public $global_discount;
|
||||||
|
@ -19,28 +19,32 @@ class ProductCart extends Component
|
||||||
public $check_quantity;
|
public $check_quantity;
|
||||||
public $discount_type;
|
public $discount_type;
|
||||||
public $item_discount;
|
public $item_discount;
|
||||||
public $sale;
|
public $data;
|
||||||
|
|
||||||
public function mount($cartInstance, $sale = null) {
|
public function mount($cartInstance, $data = null) {
|
||||||
$this->cart_instance = $cartInstance;
|
$this->cart_instance = $cartInstance;
|
||||||
|
|
||||||
if ($sale) {
|
if ($data) {
|
||||||
$this->sale = $sale;
|
$this->data = $data;
|
||||||
|
|
||||||
$this->global_discount = $sale->discount_percentage;
|
$this->global_discount = $data->discount_percentage;
|
||||||
$this->global_tax = $sale->tax_percentage;
|
$this->global_tax = $data->tax_percentage;
|
||||||
$this->shipping = $sale->shipping_amount;
|
$this->shipping = $data->shipping_amount;
|
||||||
|
|
||||||
$this->updatedGlobalTax();
|
$this->updatedGlobalTax();
|
||||||
$this->updatedGlobalDiscount();
|
$this->updatedGlobalDiscount();
|
||||||
|
|
||||||
$sale_details = $this->sale->saleDetails;
|
$cart_items = Cart::instance($this->cart_instance)->content();
|
||||||
|
|
||||||
foreach ($sale_details as $sale_detail) {
|
foreach ($cart_items as $cart_item) {
|
||||||
$this->check_quantity[$sale_detail->product_id] = [$sale_detail->product->product_quantity];
|
$this->check_quantity[$cart_item->id] = [$cart_item->options->stock];
|
||||||
$this->quantity[$sale_detail->product_id] = $sale_detail->quantity;
|
$this->quantity[$cart_item->id] = $cart_item->qty;
|
||||||
$this->discount_type[$sale_detail->product_id] = 'fixed';
|
$this->discount_type[$cart_item->id] = $cart_item->options->product_discount_type;
|
||||||
$this->item_discount[$sale_detail->product_id] = $sale_detail->product_discount_amount;
|
if ($cart_item->options->product_discount_type == 'fixed') {
|
||||||
|
$this->item_discount[$cart_item->id] = $cart_item->options->product_discount;
|
||||||
|
} elseif($cart_item->options->product_discount_type == 'percentage') {
|
||||||
|
$this->item_discount[$cart_item->id] = 100 * ($cart_item->options->product_discount / $cart_item->price);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->global_discount = 0;
|
$this->global_discount = 0;
|
||||||
|
@ -81,6 +85,7 @@ class ProductCart extends Component
|
||||||
'weight' => 1,
|
'weight' => 1,
|
||||||
'options' => [
|
'options' => [
|
||||||
'product_discount' => 0.00,
|
'product_discount' => 0.00,
|
||||||
|
'product_discount_type' => 'fixed',
|
||||||
'sub_total' => $this->calculate($product)['sub_total'],
|
'sub_total' => $this->calculate($product)['sub_total'],
|
||||||
'code' => $product['product_code'],
|
'code' => $product['product_code'],
|
||||||
'stock' => $product['product_quantity'],
|
'stock' => $product['product_quantity'],
|
||||||
|
@ -100,11 +105,11 @@ class ProductCart extends Component
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updatedGlobalTax() {
|
public function updatedGlobalTax() {
|
||||||
Cart::instance($this->cart_instance)->setGlobalTax((integer)$this->global_tax);
|
Cart::instance($this->cart_instance)->setGlobalTax((integer) $this->global_tax);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updatedGlobalDiscount() {
|
public function updatedGlobalDiscount() {
|
||||||
Cart::instance($this->cart_instance)->setGlobalDiscount((integer)$this->global_discount);
|
Cart::instance($this->cart_instance)->setGlobalDiscount((integer) $this->global_discount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateQuantity($row_id, $product_id) {
|
public function updateQuantity($row_id, $product_id) {
|
||||||
|
@ -125,25 +130,44 @@ class ProductCart extends Component
|
||||||
'product_tax' => $cart_item->options->product_tax,
|
'product_tax' => $cart_item->options->product_tax,
|
||||||
'unit_price' => $cart_item->options->unit_price,
|
'unit_price' => $cart_item->options->unit_price,
|
||||||
'product_discount' => $cart_item->options->product_discount,
|
'product_discount' => $cart_item->options->product_discount,
|
||||||
|
'product_discount_type' => $cart_item->options->product_discount_type,
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function updatedDiscountType($value, $name) {
|
||||||
|
$this->item_discount[$name] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function discountModalRefresh($product_id, $row_id) {
|
||||||
|
$this->updateQuantity($row_id, $product_id);
|
||||||
|
}
|
||||||
|
|
||||||
public function setProductDiscount($row_id, $product_id) {
|
public function setProductDiscount($row_id, $product_id) {
|
||||||
$cart_item = Cart::instance($this->cart_instance)->get($row_id);
|
$cart_item = Cart::instance($this->cart_instance)->get($row_id);
|
||||||
|
|
||||||
if ($this->discount_type[$product_id] == 'fixed') {
|
if ($this->discount_type[$product_id] == 'fixed') {
|
||||||
Cart::instance($this->cart_instance)->update($row_id, ['price' => ($cart_item->price + $cart_item->options->product_discount) - $this->item_discount[$product_id]]);
|
Cart::instance($this->cart_instance)
|
||||||
|
->update($row_id, [
|
||||||
|
'price' => ($cart_item->price + $cart_item->options->product_discount) - $this->item_discount[$product_id]
|
||||||
|
]);
|
||||||
|
|
||||||
Cart::instance($this->cart_instance)->update($row_id, ['options' => [
|
$discount_amount = $this->item_discount[$product_id];
|
||||||
'sub_total' => $cart_item->price * $cart_item->qty,
|
|
||||||
'code' => $cart_item->options->code,
|
$this->updateCartOptions($row_id, $product_id, $cart_item, $discount_amount);
|
||||||
'stock' => $cart_item->options->stock,
|
|
||||||
'product_tax' => $cart_item->options->product_tax,
|
|
||||||
'unit_price' => $cart_item->options->unit_price,
|
|
||||||
'product_discount' => $this->item_discount[$product_id],
|
|
||||||
]]);
|
|
||||||
}
|
}
|
||||||
|
elseif ($this->discount_type[$product_id] == 'percentage') {
|
||||||
|
$discount_amount = $cart_item->price * ($this->item_discount[$product_id] / 100);
|
||||||
|
|
||||||
|
Cart::instance($this->cart_instance)
|
||||||
|
->update($row_id, [
|
||||||
|
'price' => ($cart_item->price + $cart_item->options->product_discount) - (($cart_item->price * $this->item_discount[$product_id] / 100))
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->updateCartOptions($row_id, $product_id, $cart_item, $discount_amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
session()->flash('discount_message' . $product_id, 'Discount added to the product!');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function calculate($product) {
|
public function calculate($product) {
|
||||||
|
@ -173,4 +197,16 @@ class ProductCart extends Component
|
||||||
|
|
||||||
return ['price' => $price, 'unit_price' => $unit_price, 'product_tax' => $product_tax, 'sub_total' => $sub_total];
|
return ['price' => $price, 'unit_price' => $unit_price, 'product_tax' => $product_tax, 'sub_total' => $sub_total];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function updateCartOptions($row_id, $product_id, $cart_item, $discount_amount) {
|
||||||
|
Cart::instance($this->cart_instance)->update($row_id, ['options' => [
|
||||||
|
'sub_total' => $cart_item->price * $cart_item->qty,
|
||||||
|
'code' => $cart_item->options->code,
|
||||||
|
'stock' => $cart_item->options->stock,
|
||||||
|
'product_tax' => $cart_item->options->product_tax,
|
||||||
|
'unit_price' => $cart_item->options->unit_price,
|
||||||
|
'product_discount' => $discount_amount,
|
||||||
|
'product_discount_type' => $this->discount_type[$product_id],
|
||||||
|
]]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<!-- Button trigger Discount Modal -->
|
<!-- Button trigger Discount Modal -->
|
||||||
<span role="button" class="badge badge-warning pointer-event" data-toggle="modal" data-target="#discountModal{{ $cart_item->id }}">
|
<span wire:click="$emitSelf('discountModalRefresh', '{{ $cart_item->id }}', '{{ $cart_item->rowId }}')" role="button" class="badge badge-warning pointer-event" data-toggle="modal" data-target="#discountModal{{ $cart_item->id }}">
|
||||||
<i class="bi bi-pencil-square text-white"></i>
|
<i class="bi bi-pencil-square text-white"></i>
|
||||||
</span>
|
</span>
|
||||||
<!-- Discount Modal -->
|
<!-- Discount Modal -->
|
||||||
|
@ -20,15 +20,31 @@
|
||||||
</div>
|
</div>
|
||||||
<form wire:submit.prevent="setProductDiscount('{{ $cart_item->rowId }}', '{{ $cart_item->id }}')" method="POST">
|
<form wire:submit.prevent="setProductDiscount('{{ $cart_item->rowId }}', '{{ $cart_item->id }}')" method="POST">
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
|
@if (session()->has('discount_message' . $cart_item->id))
|
||||||
|
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
||||||
|
<div class="alert-body">
|
||||||
|
<span>{{ session('discount_message' . $cart_item->id) }}</span>
|
||||||
|
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
|
||||||
|
<span aria-hidden="true">×</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>Discount Type <span class="text-danger">*</span></label>
|
<label>Discount Type <span class="text-danger">*</span></label>
|
||||||
<select wire:model.defer="discount_type.{{ $cart_item->id }}" class="form-control" required>
|
<select wire:model="discount_type.{{ $cart_item->id }}" class="form-control" required>
|
||||||
<option {{ $discount_type[$cart_item->id] == 'fixed' ? 'selected' : '' }} value="fixed">Fixed</option>
|
<option value="fixed">Fixed</option>
|
||||||
|
<option value="percentage">Percentage</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
@if($discount_type[$cart_item->id] == 'percentage')
|
||||||
|
<label>Discount(%) <span class="text-danger">*</span></label>
|
||||||
|
<input wire:model.defer="item_discount.{{ $cart_item->id }}" type="number" class="form-control" value="{{ $item_discount[$cart_item->id] }}" min="0" max="100">
|
||||||
|
@elseif($discount_type[$cart_item->id] == 'fixed')
|
||||||
<label>Discount <span class="text-danger">*</span></label>
|
<label>Discount <span class="text-danger">*</span></label>
|
||||||
<input wire:model.defer="item_discount.{{ $cart_item->id }}" type="number" class="form-control" value="{{ $item_discount[$cart_item->id] }}" step="0.01">
|
<input wire:model.defer="item_discount.{{ $cart_item->id }}" type="number" class="form-control" value="{{ $item_discount[$cart_item->id] }}" step="0.01">
|
||||||
|
@endif
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
|
|
Loading…
Reference in New Issue