purchases fix price

This commit is contained in:
adeliaala 2025-05-23 00:15:31 +07:00
parent 5d63a7058d
commit a4d1ea0ec2
6 changed files with 58 additions and 22 deletions

View File

@ -14,7 +14,8 @@ class ProductBatch extends Model
'branch_id', 'branch_id',
'batch_code', 'batch_code',
'quantity', 'quantity',
'purchase_price', 'unit_price',
'price',
'expired_date', 'expired_date',
'created_by', 'created_by',
'updated_by' 'updated_by'
@ -22,7 +23,8 @@ class ProductBatch extends Model
protected $casts = [ protected $casts = [
'quantity' => 'integer', 'quantity' => 'integer',
'purchase_price' => 'decimal:2', 'unit_price' => 'decimal:2',
'price' => 'decimal:2',
'expired_date' => 'date' 'expired_date' => 'date'
]; ];
@ -54,7 +56,8 @@ class ProductBatch extends Model
// Update existing batch // Update existing batch
$batch->update([ $batch->update([
'quantity' => $batch->quantity + $data['quantity'], 'quantity' => $batch->quantity + $data['quantity'],
'purchase_price' => $data['purchase_price'], // Update price to latest 'unit_price' => $data['unit_price'], // Update price to latest
'price' => $data['price'], // Update price to latest
'updated_by' => auth()->user()->name 'updated_by' => auth()->user()->name
]); ]);
return $batch; return $batch;
@ -66,7 +69,8 @@ class ProductBatch extends Model
'branch_id' => $data['branch_id'], 'branch_id' => $data['branch_id'],
'batch_code' => $data['batch_code'] ?? null, 'batch_code' => $data['batch_code'] ?? null,
'quantity' => $data['quantity'], 'quantity' => $data['quantity'],
'purchase_price' => $data['purchase_price'], 'unit_price' => $data['unit_price'],
'price' => $data['price'],
'expired_date' => $data['expired_date'], 'expired_date' => $data['expired_date'],
'purchase_id' => $data['purchase_id'], 'purchase_id' => $data['purchase_id'],
'created_by' => auth()->user()->name, 'created_by' => auth()->user()->name,

View File

@ -16,6 +16,7 @@ class PurchaseDetail extends Model
'product_id', 'product_id',
'product_name', 'product_name',
'quantity', 'quantity',
'unit_price',
'price', 'price',
'sub_total', 'sub_total',
'created_by', 'created_by',

View File

@ -90,7 +90,8 @@ class PurchaseController extends Controller
'products' => 'required|array|min:1', 'products' => 'required|array|min:1',
'products.*.product_id' => 'required|exists:products,id', 'products.*.product_id' => 'required|exists:products,id',
'products.*.qty' => 'required|integer|min:1', 'products.*.qty' => 'required|integer|min:1',
'products.*.purchase_price' => 'required|numeric|min:0', 'products.*.unit_price' => 'required|numeric|min:0',
'products.*.price' => 'required|numeric|min:0',
'products.*.expired_date' => 'nullable|date|after:today', 'products.*.expired_date' => 'nullable|date|after:today',
'discount_percentage' => 'nullable|numeric|min:0', 'discount_percentage' => 'nullable|numeric|min:0',
'discount_amount' => 'nullable|numeric|min:0', 'discount_amount' => 'nullable|numeric|min:0',
@ -158,8 +159,8 @@ class PurchaseController extends Controller
'product_code' => Product::findOrFail($product['product_id'])->product_code, 'product_code' => Product::findOrFail($product['product_id'])->product_code,
'qty' => $product['qty'], 'qty' => $product['qty'],
// 'price' => $product['purchase_price'], // 'price' => $product['purchase_price'],
'unit_price' => $product['purchase_price'], 'unit_price' => $product['unit_price'],
'subtotal' => $product['qty'] * $product['purchase_price'], 'subtotal' => $product['qty'] * $product['unit_price'],
'product_discount_amount' => 0, 'product_discount_amount' => 0,
'product_discount_type' => 'fixed', 'product_discount_type' => 'fixed',
'product_tax_amount' => 0 'product_tax_amount' => 0
@ -170,7 +171,8 @@ class PurchaseController extends Controller
'product_id' => $product['product_id'], 'product_id' => $product['product_id'],
'branch_id' => session('active_branch'), 'branch_id' => session('active_branch'),
'qty' => $product['qty'], 'qty' => $product['qty'],
'purchase_price' => $product['purchase_price'], 'unit_price' => $product['unit_price'],
'price' => $product['price'],
'exp_date' => $product['expired_date'], 'exp_date' => $product['expired_date'],
'purchase_id' => $purchase->id, 'purchase_id' => $purchase->id,
'batch_code' => $purchase->reference_no . '-' . $product['product_id'], 'batch_code' => $purchase->reference_no . '-' . $product['product_id'],
@ -255,7 +257,7 @@ class PurchaseController extends Controller
'id' => $purchase_detail->product_id, 'id' => $purchase_detail->product_id,
'name' => $purchase_detail->product_name, 'name' => $purchase_detail->product_name,
'qty' => $purchase_detail->quantity, 'qty' => $purchase_detail->quantity,
'price' => $purchase_detail->price, 'unit_price' => $purchase_detail->unit_price,
'weight' => 1, 'weight' => 1,
'options' => [ 'options' => [
'product_discount' => $purchase_detail->product_discount_amount, 'product_discount' => $purchase_detail->product_discount_amount,
@ -315,7 +317,7 @@ class PurchaseController extends Controller
'product_name' => $cart_item->name, 'product_name' => $cart_item->name,
'product_code' => $cart_item->options->code, 'product_code' => $cart_item->options->code,
'qty' => $cart_item->qty, 'qty' => $cart_item->qty,
'price' => $cart_item->price * 100, //'price' => $cart_item->price * 100,
'unit_price' => $cart_item->price * 100, 'unit_price' => $cart_item->price * 100,
'subtotal' => $cart_item->options->subtotal * 100, 'subtotal' => $cart_item->options->subtotal * 100,
'product_discount_amount' => $cart_item->options->product_discount * 100, 'product_discount_amount' => $cart_item->options->product_discount * 100,

View File

@ -34,7 +34,7 @@ class CreatePurchase extends Component
'items' => 'required|array|min:1', 'items' => 'required|array|min:1',
'items.*.product_id' => 'required|exists:products,id', 'items.*.product_id' => 'required|exists:products,id',
'items.*.qty' => 'required|integer|min:1', 'items.*.qty' => 'required|integer|min:1',
'items.*.purchase_price' => 'required|numeric|min:0', 'items.*.price' => 'required|numeric|min:0',
'items.*.unit_price' => 'required|numeric|min:0', 'items.*.unit_price' => 'required|numeric|min:0',
'discount' => 'nullable|numeric|min:0', 'discount' => 'nullable|numeric|min:0',
'discount_percentage' => 'nullable|numeric|min:0|max:100', 'discount_percentage' => 'nullable|numeric|min:0|max:100',
@ -56,8 +56,8 @@ class CreatePurchase extends Component
'items.*.product_id.exists' => 'Selected product is invalid', 'items.*.product_id.exists' => 'Selected product is invalid',
'items.*.qty.required' => 'Quantity is required', 'items.*.qty.required' => 'Quantity is required',
'items.*.qty.min' => 'Quantity must be at least 1', 'items.*.qty.min' => 'Quantity must be at least 1',
'items.*.purchase_price.required' => 'Purchase price is required', 'items.*.price.required' => 'Purchase price is required',
'items.*.purchase_price.min' => 'Purchase price must be greater than 0', 'items.*.price.min' => 'Purchase price must be greater than 0',
'items.*.unit_price.required' => 'Unit price is required', 'items.*.unit_price.required' => 'Unit price is required',
'items.*.unit_price.min' => 'Unit price must be greater than 0', 'items.*.unit_price.min' => 'Unit price must be greater than 0',
'discount.numeric' => 'Discount must be a number', 'discount.numeric' => 'Discount must be a number',
@ -80,7 +80,7 @@ class CreatePurchase extends Component
$this->items[] = [ $this->items[] = [
'product_id' => '', 'product_id' => '',
'qty' => 1, 'qty' => 1,
'purchase_price' => 0, 'price' => 0,
'unit_price' => 0, 'unit_price' => 0,
'discount' => 0, 'discount' => 0,
'discount_type' => null, 'discount_type' => null,
@ -181,7 +181,7 @@ class CreatePurchase extends Component
'product_name' => $product->product_name, 'product_name' => $product->product_name,
// 'product_code' => $product->product_code, // 'product_code' => $product->product_code,
'qty' => $item['qty'], 'qty' => $item['qty'],
// 'price' => $item['purchase_price'], //'price' => $item['price'],
'unit_price' => $item['unit_price'], 'unit_price' => $item['unit_price'],
'subtotal' => $item['qty'] * $item['unit_price'], 'subtotal' => $item['qty'] * $item['unit_price'],
'product_discount_amount' => $item['discount'] ?? 0, 'product_discount_amount' => $item['discount'] ?? 0,
@ -197,7 +197,8 @@ class CreatePurchase extends Component
'branch_id' => session('branch_id') ?? session('active_branch') ?? 1, 'branch_id' => session('branch_id') ?? session('active_branch') ?? 1,
'batch_code' => $this->reference_no . '-' . $item['product_id'], 'batch_code' => $this->reference_no . '-' . $item['product_id'],
'qty' => $item['qty'], 'qty' => $item['qty'],
'unit_price' => $item['purchase_price'], 'unit_price' => $item['unit_price'],
'price' => $item['price'],
'exp_date' => null, 'exp_date' => null,
'purchase_id' => $purchase->id, 'purchase_id' => $purchase->id,
'created_by' => auth()->user()->name ?? 'system', 'created_by' => auth()->user()->name ?? 'system',

View File

@ -150,7 +150,7 @@
<th>Product</th> <th>Product</th>
<th>Quantity</th> <th>Quantity</th>
<th>Purchase Price</th> <th>Purchase Price</th>
<th>Unit Price</th> <th>Sell Price</th>
<th>Discount</th> <th>Discount</th>
<th>Tax</th> <th>Tax</th>
<th>Subtotal</th> <th>Subtotal</th>
@ -173,14 +173,16 @@
<input type="number" class="form-control" wire:model="items.{{ $index }}.qty" min="1"> <input type="number" class="form-control" wire:model="items.{{ $index }}.qty" min="1">
@error("items.{$index}.qty") <span class="text-danger">{{ $message }}</span> @enderror @error("items.{$index}.qty") <span class="text-danger">{{ $message }}</span> @enderror
</td> </td>
<td> {{-- saya ganti jadi price --}}
<input type="number" class="form-control" wire:model="items.{{ $index }}.purchase_price" min="0" step="0.01">
@error("items.{$index}.purchase_price") <span class="text-danger">{{ $message }}</span> @enderror
</td>
<td> <td>
<input type="number" class="form-control" wire:model="items.{{ $index }}.unit_price" min="0" step="0.01"> <input type="number" class="form-control" wire:model="items.{{ $index }}.unit_price" min="0" step="0.01">
@error("items.{$index}.unit_price") <span class="text-danger">{{ $message }}</span> @enderror @error("items.{$index}.unit_price") <span class="text-danger">{{ $message }}</span> @enderror
</td> </td>
{{-- bagian ini saya ganti ke unit_price --}}
<td>
<input type="number" class="form-control" wire:model="items.{{ $index }}.price" min="0" step="0.01">
@error("items.{$index}.price") <span class="text-danger">{{ $message }}</span> @enderror
</td>
<td> <td>
<div class="input-group"> <div class="input-group">
<input type="number" class="form-control" wire:model="items.{{ $index }}.discount" min="0" step="0.01"> <input type="number" class="form-control" wire:model="items.{{ $index }}.discount" min="0" step="0.01">
@ -207,7 +209,7 @@
</tbody> </tbody>
<tfoot> <tfoot>
<tr> <tr>
<td colspan="6" class="text-right"><strong>Subtotal:</strong></td> <td colspan="6" class="text-right"><strong>Total:</strong></td>
<td colspan="2"> <td colspan="2">
<strong>{{ number_format($total_amount, 2) }}</strong> <strong>{{ number_format($total_amount, 2) }}</strong>
</td> </td>

View File

@ -0,0 +1,26 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::table('product_batches', function (Blueprint $table) {
$table->decimal('price', 15, 2)->nullable()->after('unit_price');
});
}
public function down()
{
Schema::table('product_batches', function (Blueprint $table) {
$table->dropColumn('price');
});
}
};