Sales
This commit is contained in:
parent
339031e370
commit
9e649ea5b8
|
@ -2,13 +2,13 @@
|
||||||
|
|
||||||
namespace Modules\Sale\Http\Controllers;
|
namespace Modules\Sale\Http\Controllers;
|
||||||
|
|
||||||
use Modules\Sale\DataTables\SalesDataTable;
|
|
||||||
use Gloudemans\Shoppingcart\Facades\Cart;
|
use Gloudemans\Shoppingcart\Facades\Cart;
|
||||||
use Illuminate\Routing\Controller;
|
use Illuminate\Routing\Controller;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
use Modules\People\Entities\Customer;
|
use Modules\People\Entities\Customer;
|
||||||
use Modules\Product\Entities\Product;
|
use Modules\Product\Entities\Product;
|
||||||
|
use Modules\Sale\DataTables\SalesDataTable;
|
||||||
use Modules\Sale\Entities\Sale;
|
use Modules\Sale\Entities\Sale;
|
||||||
use Modules\Sale\Entities\SaleDetails;
|
use Modules\Sale\Entities\SaleDetails;
|
||||||
use Modules\Sale\Entities\SalePayment;
|
use Modules\Sale\Entities\SalePayment;
|
||||||
|
@ -30,65 +30,37 @@ class SaleController extends Controller
|
||||||
return view('sale::create');
|
return view('sale::create');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function allocateProductFromBatches($product_id, $required_qty)
|
|
||||||
{
|
|
||||||
$branch_id = session('branch_id');
|
|
||||||
|
|
||||||
$batches = DB::table('product_batches')
|
|
||||||
->where('product_id', $product_id)
|
|
||||||
->where('branch_id', $branch_id)
|
|
||||||
->where('quantity', '>', 0)
|
|
||||||
->orderBy('created_at') // FIFO
|
|
||||||
->get();
|
|
||||||
|
|
||||||
$allocation = [];
|
|
||||||
$remaining = $required_qty;
|
|
||||||
|
|
||||||
foreach ($batches as $batch) {
|
|
||||||
if ($remaining <= 0) break;
|
|
||||||
|
|
||||||
$used_qty = min($batch->quantity, $remaining);
|
|
||||||
$allocation[] = [
|
|
||||||
'batch_id' => $batch->id,
|
|
||||||
'quantity' => $used_qty,
|
|
||||||
'unit_price' => $batch->unit_price
|
|
||||||
];
|
|
||||||
|
|
||||||
// Kurangi jumlah di batch
|
|
||||||
DB::table('product_batches')
|
|
||||||
->where('id', $batch->id)
|
|
||||||
->decrement('quantity', $used_qty);
|
|
||||||
|
|
||||||
$remaining -= $used_qty;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($remaining > 0) {
|
|
||||||
throw new \Exception("Stok produk tidak cukup di cabang saat ini.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return $allocation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function store(StoreSaleRequest $request)
|
public function store(StoreSaleRequest $request)
|
||||||
{
|
{
|
||||||
DB::transaction(function () use ($request) {
|
DB::transaction(function () use ($request) {
|
||||||
$due_amount = $request->total_amount - $request->paid_amount;
|
$total_amount = 0;
|
||||||
$payment_status = $due_amount == $request->total_amount ? 'Unpaid' :
|
foreach (Cart::instance('sale')->content() as $item) {
|
||||||
|
$total_amount += $item->options->sub_total;
|
||||||
|
}
|
||||||
|
|
||||||
|
$paid_amount = $request->paid_amount ?? 0;
|
||||||
|
$due_amount = $total_amount - $paid_amount;
|
||||||
|
|
||||||
|
$payment_status = $due_amount == $total_amount ? 'Unpaid' :
|
||||||
($due_amount > 0 ? 'Partial' : 'Paid');
|
($due_amount > 0 ? 'Partial' : 'Paid');
|
||||||
|
|
||||||
$customer_id = $request->customer_id ?? 1;
|
$branch_id = session('branch_id');
|
||||||
|
if (!$branch_id) {
|
||||||
|
throw new \Exception("Branch belum dipilih.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$customer_id = $request->customer_id ?? 1;
|
||||||
|
|
||||||
$sale = Sale::create([
|
$sale = Sale::create([
|
||||||
'branch_id' => session('branch_id'),
|
'branch_id' => $branch_id,
|
||||||
'date' => $request->date,
|
'date' => $request->date,
|
||||||
'reference' => $request->reference,
|
'reference' => $request->reference,
|
||||||
'customer_id' => $request->customer_id,
|
'customer_id' => $customer_id,
|
||||||
'customer_name' => optional(Customer::find($request->customer_id))->customer_name,
|
'customer_name' => optional(Customer::find($customer_id))->customer_name,
|
||||||
'tax_percentage' => $request->tax_percentage,
|
'tax_percentage' => $request->tax_percentage ?? 0,
|
||||||
'discount_percentage' => $request->discount_percentage,
|
'discount_percentage' => $request->discount_percentage ?? 0,
|
||||||
'shipping_amount' => $request->shipping_amount * 100,
|
'paid_amount' => $paid_amount * 100,
|
||||||
'paid_amount' => $request->paid_amount * 100,
|
'total_amount' => $total_amount * 100,
|
||||||
'total_amount' => $request->total_amount * 100,
|
|
||||||
'due_amount' => $due_amount * 100,
|
'due_amount' => $due_amount * 100,
|
||||||
'payment_status' => $payment_status,
|
'payment_status' => $payment_status,
|
||||||
'payment_method' => $request->payment_method,
|
'payment_method' => $request->payment_method,
|
||||||
|
@ -99,17 +71,17 @@ class SaleController extends Controller
|
||||||
|
|
||||||
foreach (Cart::instance('sale')->content() as $item) {
|
foreach (Cart::instance('sale')->content() as $item) {
|
||||||
SaleDetails::create([
|
SaleDetails::create([
|
||||||
'sale_id' => $sale->id,
|
'sale_id' => $sale->id,
|
||||||
'product_id' => $item->id,
|
'product_id' => $item->id,
|
||||||
'product_name' => $item->name,
|
'product_name' => $item->name,
|
||||||
'product_code' => $item->options->code,
|
'product_code' => $item->options->code,
|
||||||
'quantity' => $item->qty,
|
'quantity' => $item->qty,
|
||||||
'price' => $item->price * 100,
|
'price' => $item->price * 100,
|
||||||
'unit_price' => $item->options->unit_price * 100,
|
'unit_price' => $item->options->unit_price * 100,
|
||||||
'sub_total' => $item->options->sub_total * 100,
|
'sub_total' => $item->options->sub_total * 100,
|
||||||
'product_discount_amount' => $item->options->product_discount * 100,
|
'product_discount_amount' => $item->options->product_discount * 100,
|
||||||
'product_discount_type' => $item->options->product_discount_type,
|
'product_discount_type' => $item->options->product_discount_type,
|
||||||
'product_tax_amount' => $item->options->product_tax * 100,
|
'product_tax_amount' => $item->options->product_tax * 100,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (in_array($request->status, ['Shipped', 'Completed'])) {
|
if (in_array($request->status, ['Shipped', 'Completed'])) {
|
||||||
|
@ -138,35 +110,28 @@ class SaleController extends Controller
|
||||||
return redirect()->route('sales.index');
|
return redirect()->route('sales.index');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show(Sale $sale)
|
|
||||||
{
|
|
||||||
abort_if(Gate::denies('show_sales'), 403);
|
|
||||||
$customer = Customer::find($sale->customer_id);
|
|
||||||
return view('sale::show', compact('sale', 'customer'));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function edit(Sale $sale)
|
public function edit(Sale $sale)
|
||||||
{
|
{
|
||||||
abort_if(Gate::denies('edit_sales'), 403);
|
abort_if(Gate::denies('edit_sales'), 403);
|
||||||
|
|
||||||
$cart = Cart::instance('sale');
|
Cart::instance('sale')->destroy();
|
||||||
$cart->destroy();
|
|
||||||
|
|
||||||
foreach ($sale->saleDetails as $detail) {
|
foreach ($sale->saleDetails as $detail) {
|
||||||
$cart->add([
|
Cart::instance('sale')->add([
|
||||||
'id' => $detail->product_id,
|
'id' => $detail->product_id,
|
||||||
'name' => $detail->product_name,
|
'name' => $detail->product_name,
|
||||||
'qty' => $detail->quantity,
|
'qty' => $detail->quantity,
|
||||||
'price' => $detail->price,
|
'price' => $detail->price / 100,
|
||||||
'weight' => 1,
|
'weight'=> 1,
|
||||||
'options' => [
|
'options' => [
|
||||||
'product_discount' => $detail->product_discount_amount,
|
'code' => $detail->product_code,
|
||||||
'product_discount_type' => $detail->product_discount_type,
|
'unit_price' => $detail->unit_price / 100,
|
||||||
'sub_total' => $detail->sub_total,
|
'sub_total' => $detail->sub_total / 100,
|
||||||
'code' => $detail->product_code,
|
'product_discount' => $detail->product_discount_amount / 100,
|
||||||
'stock' => optional(Product::find($detail->product_id))->product_quantity,
|
'product_discount_type' => $detail->product_discount_type,
|
||||||
'product_tax' => $detail->product_tax_amount,
|
'product_tax' => $detail->product_tax_amount / 100,
|
||||||
'unit_price' => $detail->unit_price
|
'stock' => optional(Product::find($detail->product_id))->product_quantity,
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
@ -196,12 +161,12 @@ class SaleController extends Controller
|
||||||
'reference' => $request->reference,
|
'reference' => $request->reference,
|
||||||
'customer_id' => $request->customer_id,
|
'customer_id' => $request->customer_id,
|
||||||
'customer_name' => optional(Customer::find($request->customer_id))->customer_name,
|
'customer_name' => optional(Customer::find($request->customer_id))->customer_name,
|
||||||
'tax_percentage' => $request->tax_percentage,
|
'tax_percentage' => $request->tax_percentage ?? 0,
|
||||||
'discount_percentage' => $request->discount_percentage,
|
'discount_percentage' => $request->discount_percentage ?? 0,
|
||||||
'shipping_amount' => $request->shipping_amount * 100,
|
'shipping_amount' => ($request->shipping_amount ?? 0) * 100,
|
||||||
'paid_amount' => $request->paid_amount * 100,
|
'paid_amount' => ($request->paid_amount ?? 0) * 100,
|
||||||
'total_amount' => $request->total_amount * 100,
|
'total_amount' => ($request->total_amount ?? 0) * 100,
|
||||||
'due_amount' => $due_amount * 100,
|
'due_amount' => ($due_amount ?? 0) * 100,
|
||||||
'payment_status' => $payment_status,
|
'payment_status' => $payment_status,
|
||||||
'payment_method' => $request->payment_method,
|
'payment_method' => $request->payment_method,
|
||||||
'note' => $request->note,
|
'note' => $request->note,
|
||||||
|
@ -211,17 +176,17 @@ class SaleController extends Controller
|
||||||
|
|
||||||
foreach (Cart::instance('sale')->content() as $item) {
|
foreach (Cart::instance('sale')->content() as $item) {
|
||||||
SaleDetails::create([
|
SaleDetails::create([
|
||||||
'sale_id' => $sale->id,
|
'sale_id' => $sale->id,
|
||||||
'product_id' => $item->id,
|
'product_id' => $item->id,
|
||||||
'product_name' => $item->name,
|
'product_name' => $item->name,
|
||||||
'product_code' => $item->options->code,
|
'product_code' => $item->options->code,
|
||||||
'quantity' => $item->qty,
|
'quantity' => $item->qty,
|
||||||
'price' => $item->price * 100,
|
'price' => $item->price * 100,
|
||||||
'unit_price' => $item->options->unit_price * 100,
|
'unit_price' => $item->options->unit_price * 100,
|
||||||
'sub_total' => $item->options->sub_total * 100,
|
'sub_total' => $item->options->sub_total * 100,
|
||||||
'product_discount_amount' => $item->options->product_discount * 100,
|
'product_discount_amount' => $item->options->product_discount * 100,
|
||||||
'product_discount_type' => $item->options->product_discount_type,
|
'product_discount_type' => $item->options->product_discount_type,
|
||||||
'product_tax_amount' => $item->options->product_tax * 100,
|
'product_tax_amount' => $item->options->product_tax * 100,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (in_array($request->status, ['Shipped', 'Completed'])) {
|
if (in_array($request->status, ['Shipped', 'Completed'])) {
|
||||||
|
@ -240,6 +205,13 @@ class SaleController extends Controller
|
||||||
return redirect()->route('sales.index');
|
return redirect()->route('sales.index');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function show(Sale $sale)
|
||||||
|
{
|
||||||
|
abort_if(Gate::denies('show_sales'), 403);
|
||||||
|
$customer = Customer::find($sale->customer_id);
|
||||||
|
return view('sale::show', compact('sale', 'customer'));
|
||||||
|
}
|
||||||
|
|
||||||
public function destroy(Sale $sale)
|
public function destroy(Sale $sale)
|
||||||
{
|
{
|
||||||
abort_if(Gate::denies('delete_sales'), 403);
|
abort_if(Gate::denies('delete_sales'), 403);
|
||||||
|
@ -249,4 +221,42 @@ class SaleController extends Controller
|
||||||
toast('Sale Deleted!', 'warning');
|
toast('Sale Deleted!', 'warning');
|
||||||
return redirect()->route('sales.index');
|
return redirect()->route('sales.index');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function allocateProductFromBatches($product_id, $required_qty)
|
||||||
|
{
|
||||||
|
$branch_id = session('branch_id');
|
||||||
|
|
||||||
|
$batches = DB::table('product_batches')
|
||||||
|
->where('product_id', $product_id)
|
||||||
|
->where('branch_id', $branch_id)
|
||||||
|
->where('qty', '>', 0)
|
||||||
|
->orderBy('created_at') // FIFO
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$allocation = [];
|
||||||
|
$remaining = $required_qty;
|
||||||
|
|
||||||
|
foreach ($batches as $batch) {
|
||||||
|
if ($remaining <= 0) break;
|
||||||
|
|
||||||
|
$used_qty = min($batch->qty, $remaining);
|
||||||
|
$allocation[] = [
|
||||||
|
'batch_id' => $batch->id,
|
||||||
|
'qty' => $used_qty,
|
||||||
|
'unit_price' => $batch->price,
|
||||||
|
];
|
||||||
|
|
||||||
|
DB::table('product_batches')
|
||||||
|
->where('id', $batch->id)
|
||||||
|
->decrement('qty', $used_qty);
|
||||||
|
|
||||||
|
$remaining -= $used_qty;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($remaining > 0) {
|
||||||
|
throw new \Exception("Stok produk tidak cukup di cabang saat ini.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $allocation;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,12 +17,12 @@ class StoreSaleRequest extends FormRequest
|
||||||
return [
|
return [
|
||||||
'customer_id' => 'required|numeric',
|
'customer_id' => 'required|numeric',
|
||||||
'reference' => 'required|string|max:255',
|
'reference' => 'required|string|max:255',
|
||||||
'tax_percentage' => 'required|integer|min:0|max:100',
|
'tax_percentage' => 'nullable|integer|min:0|max:100',
|
||||||
'discount_percentage' => 'required|integer|min:0|max:100',
|
'discount_percentage' => 'nullable|integer|min:0|max:100',
|
||||||
'shipping_amount' => 'required|numeric',
|
'shipping_amount' => 'nullable|numeric',
|
||||||
'total_amount' => 'required|numeric',
|
'total_amount' => 'nullable|numeric',
|
||||||
'paid_amount' => 'required|numeric',
|
'paid_amount' => 'required|numeric',
|
||||||
'status' => 'required|string|max:255',
|
'status' => 'nullable|string|max:255',
|
||||||
'payment_method' => 'required|string|max:255',
|
'payment_method' => 'required|string|max:255',
|
||||||
'note' => 'nullable|string|max:1000'
|
'note' => 'nullable|string|max:1000'
|
||||||
];
|
];
|
||||||
|
|
|
@ -5,7 +5,3 @@
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="discount_amount">Discount Amount</label>
|
<label for="discount_amount">Discount Amount</label>
|
||||||
<input type="number" class="form-control" id="discount_amount" name="discount_amount" min="0" value="0" readonly>
|
<input type="number" class="form-control" id="discount_amount" name="discount_amount" min="0" value="0" readonly>
|
||||||
</div>
|
|
||||||
<div class="form-group">
|
|
||||||
<label for="total_amount">Total Amount</label>
|
|
||||||
<input type="number" class="form-control" id="total_amount" name="total_amount" min="0" value="0" readonly>
|
|
|
@ -4,18 +4,17 @@ namespace App\Livewire;
|
||||||
|
|
||||||
use Gloudemans\Shoppingcart\Facades\Cart;
|
use Gloudemans\Shoppingcart\Facades\Cart;
|
||||||
use Illuminate\Support\Facades\Request;
|
use Illuminate\Support\Facades\Request;
|
||||||
|
use Illuminate\Support\Facades\Session;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
use Modules\Product\Entities\Product;
|
use Modules\Product\Entities\Product;
|
||||||
use App\Models\ProductBatch;
|
use App\Models\ProductBatch;
|
||||||
|
|
||||||
class ProductCart extends Component
|
class ProductCart extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
public $listeners = ['productSelected', 'discountModalRefresh'];
|
public $listeners = ['productSelected', 'discountModalRefresh'];
|
||||||
|
|
||||||
public $cart_instance;
|
public $cart_instance;
|
||||||
public $global_discount;
|
public $global_discount;
|
||||||
//public $global_tax;
|
|
||||||
public $shipping;
|
public $shipping;
|
||||||
public $quantity;
|
public $quantity;
|
||||||
public $check_quantity;
|
public $check_quantity;
|
||||||
|
@ -31,20 +30,15 @@ class ProductCart extends Component
|
||||||
|
|
||||||
if ($data) {
|
if ($data) {
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
|
|
||||||
$this->global_discount = $data->discount_percentage;
|
$this->global_discount = $data->discount_percentage;
|
||||||
//$this->global_tax = $data->tax_percentage;
|
|
||||||
$this->shipping = $data->shipping_amount;
|
$this->shipping = $data->shipping_amount;
|
||||||
|
|
||||||
//$this->updatedGlobalTax();
|
|
||||||
$this->updatedGlobalDiscount();
|
$this->updatedGlobalDiscount();
|
||||||
|
|
||||||
$cart_items = Cart::instance($this->cart_instance)->content();
|
$cart_items = Cart::instance($this->cart_instance)->content();
|
||||||
|
|
||||||
foreach ($cart_items as $cart_item) {
|
foreach ($cart_items as $cart_item) {
|
||||||
$this->check_quantity[$cart_item->id] = [$cart_item->options->stock];
|
$this->check_quantity[$cart_item->id] = [$cart_item->options->stock];
|
||||||
$this->quantity[$cart_item->id] = $cart_item->qty;
|
$this->quantity[$cart_item->id] = $cart_item->qty;
|
||||||
$this->unit_price[$cart_item->id] = $cart_item->unit_price;
|
$this->unit_price[$cart_item->id] = $cart_item->options->unit_price;
|
||||||
$this->discount_type[$cart_item->id] = $cart_item->options->product_discount_type;
|
$this->discount_type[$cart_item->id] = $cart_item->options->product_discount_type;
|
||||||
if ($cart_item->options->product_discount_type == 'fixed') {
|
if ($cart_item->options->product_discount_type == 'fixed') {
|
||||||
$this->item_discount[$cart_item->id] = $cart_item->options->product_discount;
|
$this->item_discount[$cart_item->id] = $cart_item->options->product_discount;
|
||||||
|
@ -54,7 +48,6 @@ class ProductCart extends Component
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->global_discount = 0;
|
$this->global_discount = 0;
|
||||||
//$this->global_tax = 0;
|
|
||||||
$this->shipping = 0.00;
|
$this->shipping = 0.00;
|
||||||
$this->check_quantity = [];
|
$this->check_quantity = [];
|
||||||
$this->quantity = [];
|
$this->quantity = [];
|
||||||
|
@ -66,7 +59,6 @@ class ProductCart extends Component
|
||||||
|
|
||||||
public function render() {
|
public function render() {
|
||||||
$cart_items = Cart::instance($this->cart_instance)->content();
|
$cart_items = Cart::instance($this->cart_instance)->content();
|
||||||
|
|
||||||
return view('livewire.product-cart', [
|
return view('livewire.product-cart', [
|
||||||
'cart_items' => $cart_items
|
'cart_items' => $cart_items
|
||||||
]);
|
]);
|
||||||
|
@ -81,70 +73,89 @@ class ProductCart extends Component
|
||||||
|
|
||||||
if ($exists->isNotEmpty()) {
|
if ($exists->isNotEmpty()) {
|
||||||
session()->flash('message', 'Product exists in the cart!');
|
session()->flash('message', 'Product exists in the cart!');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->product = $product;
|
$branch_id = session('branch_id');
|
||||||
|
if (!$branch_id) {
|
||||||
|
session()->flash('message', 'Branch belum dipilih.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$batch = ProductBatch::where('product_id', $product['id'])
|
||||||
|
->where('branch_id', $branch_id)
|
||||||
|
->where('qty', '>', 0)
|
||||||
|
->orderBy('exp_date', 'asc')
|
||||||
|
->orderBy('created_at', 'asc')
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (!$batch) {
|
||||||
|
session()->flash('message', 'Stok produk tidak tersedia.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$unit_price = $batch->unit_price;
|
||||||
|
$stock_qty = $batch->qty;
|
||||||
|
|
||||||
$cart->add([
|
$cart->add([
|
||||||
'id' => $product['id'],
|
'id' => $product['id'],
|
||||||
'name' => $product['product_name'],
|
'name' => $product['product_name'],
|
||||||
'qty' => 1,
|
'qty' => 1,
|
||||||
'price' => $this->calculate($product)['price'],
|
'price' => $unit_price,
|
||||||
'weight' => 1,
|
'weight' => 1,
|
||||||
'options' => [
|
'options' => [
|
||||||
'product_discount' => 0.00,
|
'product_discount' => 0.00,
|
||||||
'product_discount_type' => 'fixed',
|
'product_discount_type' => 'fixed',
|
||||||
'sub_total' => $this->calculate($product)['sub_total'],
|
'sub_total' => $unit_price,
|
||||||
'code' => $product['product_code'],
|
'code' => $product['product_code'],
|
||||||
'stock' => $product['product_quantity'],
|
'stock' => $stock_qty,
|
||||||
'unit' => $product['product_unit'],
|
'unit' => $product['product_unit'],
|
||||||
//'product_tax' => $this->calculate($product)['product_tax'],
|
'unit_price' => $unit_price,
|
||||||
'unit_price' => $this->calculate($product)['unit_price']
|
'batch_id' => $batch->id,
|
||||||
|
'exp_date' => $batch->exp_date,
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->check_quantity[$product['id']] = $product['product_quantity'];
|
$productId = $product['id'];
|
||||||
$this->quantity[$product['id']] = 1;
|
$this->check_quantity[$productId] = $stock_qty;
|
||||||
$this->discount_type[$product['id']] = 'fixed';
|
$this->quantity[$productId] = 1;
|
||||||
$this->item_discount[$product['id']] = 0;
|
$this->discount_type[$productId] = 'fixed';
|
||||||
|
$this->item_discount[$productId] = 0;
|
||||||
|
$this->unit_price[$productId] = $unit_price;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeItem($row_id) {
|
public function removeItem($row_id) {
|
||||||
Cart::instance($this->cart_instance)->remove($row_id);
|
Cart::instance($this->cart_instance)->remove($row_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// public function updatedGlobalTax() {
|
|
||||||
// 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((int) $this->global_discount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateQuantity($row_id, $product_id) {
|
public function updateQuantity($row_id, $product_id) {
|
||||||
if ($this->cart_instance == 'sale' || $this->cart_instance == 'purchase_return') {
|
if (
|
||||||
if ($this->check_quantity[$product_id] < $this->quantity[$product_id]) {
|
in_array($this->cart_instance, ['sale', 'purchase_return']) &&
|
||||||
session()->flash('message', 'The requested quantity is not available in stock.');
|
isset($this->check_quantity[$product_id], $this->quantity[$product_id]) &&
|
||||||
return;
|
$this->check_quantity[$product_id] < $this->quantity[$product_id]
|
||||||
}
|
) {
|
||||||
|
session()->flash('message', 'The requested quantity is not available in stock.');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Cart::instance($this->cart_instance)->update($row_id, $this->quantity[$product_id]);
|
$qty = $this->quantity[$product_id] ?? 1;
|
||||||
|
Cart::instance($this->cart_instance)->update($row_id, $qty);
|
||||||
|
|
||||||
$cart_item = Cart::instance($this->cart_instance)->get($row_id);
|
$cart_item = Cart::instance($this->cart_instance)->get($row_id);
|
||||||
|
|
||||||
Cart::instance($this->cart_instance)->update($row_id, [
|
Cart::instance($this->cart_instance)->update($row_id, [
|
||||||
'options' => [
|
'options' => [
|
||||||
'sub_total' => $cart_item->price * $cart_item->qty,
|
'sub_total' => $cart_item->price * $cart_item->qty,
|
||||||
'code' => $cart_item->options->code,
|
'code' => $cart_item->options->code ?? '',
|
||||||
'stock' => $cart_item->options->stock,
|
'stock' => $cart_item->options->stock ?? 0,
|
||||||
'unit' => $cart_item->options->unit,
|
'unit' => $cart_item->options->unit ?? '',
|
||||||
//'product_tax' => $cart_item->options->product_tax,
|
'unit_price' => $cart_item->options->unit_price ?? 0,
|
||||||
'unit_price' => $cart_item->options->unit_price,
|
'product_discount' => $cart_item->options->product_discount ?? 0,
|
||||||
'product_discount' => $cart_item->options->product_discount,
|
'product_discount_type' => $cart_item->options->product_discount_type ?? 'fixed',
|
||||||
'product_discount_type' => $cart_item->options->product_discount_type,
|
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
@ -161,21 +172,19 @@ class ProductCart extends Component
|
||||||
$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)
|
Cart::instance($this->cart_instance)->update($row_id, [
|
||||||
->update($row_id, [
|
'price' => ($cart_item->price + $cart_item->options->product_discount) - $this->item_discount[$product_id]
|
||||||
'price' => ($cart_item->price + $cart_item->options->product_discount) - $this->item_discount[$product_id]
|
]);
|
||||||
]);
|
|
||||||
|
|
||||||
$discount_amount = $this->item_discount[$product_id];
|
$discount_amount = $this->item_discount[$product_id];
|
||||||
|
|
||||||
$this->updateCartOptions($row_id, $product_id, $cart_item, $discount_amount);
|
$this->updateCartOptions($row_id, $product_id, $cart_item, $discount_amount);
|
||||||
|
|
||||||
} elseif ($this->discount_type[$product_id] == 'percentage') {
|
} elseif ($this->discount_type[$product_id] == 'percentage') {
|
||||||
$discount_amount = ($cart_item->price + $cart_item->options->product_discount) * ($this->item_discount[$product_id] / 100);
|
$discount_amount = ($cart_item->price + $cart_item->options->product_discount) * ($this->item_discount[$product_id] / 100);
|
||||||
|
|
||||||
Cart::instance($this->cart_instance)
|
Cart::instance($this->cart_instance)->update($row_id, [
|
||||||
->update($row_id, [
|
'price' => ($cart_item->price + $cart_item->options->product_discount) - $discount_amount
|
||||||
'price' => ($cart_item->price + $cart_item->options->product_discount) - $discount_amount
|
]);
|
||||||
]);
|
|
||||||
|
|
||||||
$this->updateCartOptions($row_id, $product_id, $cart_item, $discount_amount);
|
$this->updateCartOptions($row_id, $product_id, $cart_item, $discount_amount);
|
||||||
}
|
}
|
||||||
|
@ -185,7 +194,6 @@ class ProductCart extends Component
|
||||||
|
|
||||||
public function updatePrice($row_id, $product_id) {
|
public function updatePrice($row_id, $product_id) {
|
||||||
$product = Product::findOrFail($product_id);
|
$product = Product::findOrFail($product_id);
|
||||||
|
|
||||||
$cart_item = Cart::instance($this->cart_instance)->get($row_id);
|
$cart_item = Cart::instance($this->cart_instance)->get($row_id);
|
||||||
|
|
||||||
Cart::instance($this->cart_instance)->update($row_id, ['price' => $this->unit_price[$product['id']]]);
|
Cart::instance($this->cart_instance)->update($row_id, ['price' => $this->unit_price[$product['id']]]);
|
||||||
|
@ -196,7 +204,6 @@ class ProductCart extends Component
|
||||||
'code' => $cart_item->options->code,
|
'code' => $cart_item->options->code,
|
||||||
'stock' => $cart_item->options->stock,
|
'stock' => $cart_item->options->stock,
|
||||||
'unit' => $cart_item->options->unit,
|
'unit' => $cart_item->options->unit,
|
||||||
//'product_tax' => $this->calculate($product, $this->unit_price[$product['id']])['product_tax'],
|
|
||||||
'unit_price' => $this->calculate($product, $this->unit_price[$product['id']])['unit_price'],
|
'unit_price' => $this->calculate($product, $this->unit_price[$product['id']])['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,
|
'product_discount_type' => $cart_item->options->product_discount_type,
|
||||||
|
@ -205,30 +212,26 @@ class ProductCart extends Component
|
||||||
}
|
}
|
||||||
|
|
||||||
public function calculate($product, $new_price = null) {
|
public function calculate($product, $new_price = null) {
|
||||||
if ($new_price) {
|
$productId = is_array($product) ? $product['id'] : $product->id;
|
||||||
$product_price = $new_price;
|
$product_price = $new_price ?? (
|
||||||
} else {
|
($this->cart_instance === 'purchase' || $this->cart_instance === 'purchase_return')
|
||||||
$this->unit_price[$product['id']] = $product['product_price'];
|
? $product['product_cost'] ?? 0
|
||||||
if ($this->cart_instance == 'purchase' || $this->cart_instance == 'purchase_return') {
|
: $product['product_price'] ?? 0
|
||||||
$this->unit_price[$product['id']] = $product['product_cost'];
|
);
|
||||||
}
|
|
||||||
$product_price = $this->unit_price[$product['id']];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if wholesale price should be applied
|
if (
|
||||||
if (isset($product['min_quantity_for_wholesale']) &&
|
isset($product['min_quantity_for_wholesale'], $product['wholesale_discount_percentage'], $this->quantity[$productId]) &&
|
||||||
isset($product['wholesale_discount_percentage']) &&
|
$this->quantity[$productId] >= $product['min_quantity_for_wholesale']
|
||||||
isset($this->quantity[$product['id']]) &&
|
) {
|
||||||
$this->quantity[$product['id']] >= $product['min_quantity_for_wholesale']) {
|
|
||||||
$discount = $product['wholesale_discount_percentage'] / 100;
|
$discount = $product['wholesale_discount_percentage'] / 100;
|
||||||
$product_price = $product_price * (1 - $discount);
|
$product_price *= (1 - $discount);
|
||||||
}
|
}
|
||||||
|
|
||||||
$price = $product_price;
|
return [
|
||||||
$unit_price = $product_price;
|
'price' => $product_price,
|
||||||
$sub_total = $product_price;
|
'unit_price' => $product_price,
|
||||||
|
'sub_total' => $product_price,
|
||||||
return ['price' => $price, 'unit_price' => $unit_price, 'sub_total' => $sub_total];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateCartOptions($row_id, $product_id, $cart_item, $discount_amount) {
|
public function updateCartOptions($row_id, $product_id, $cart_item, $discount_amount) {
|
||||||
|
@ -237,7 +240,6 @@ class ProductCart extends Component
|
||||||
'code' => $cart_item->options->code,
|
'code' => $cart_item->options->code,
|
||||||
'stock' => $cart_item->options->stock,
|
'stock' => $cart_item->options->stock,
|
||||||
'unit' => $cart_item->options->unit,
|
'unit' => $cart_item->options->unit,
|
||||||
//'product_tax' => $cart_item->options->product_tax,
|
|
||||||
'unit_price' => $cart_item->options->unit_price,
|
'unit_price' => $cart_item->options->unit_price,
|
||||||
'product_discount' => $discount_amount,
|
'product_discount' => $discount_amount,
|
||||||
'product_discount_type' => $this->discount_type[$product_id],
|
'product_discount_type' => $this->discount_type[$product_id],
|
||||||
|
|
Loading…
Reference in New Issue