refactor: rename hpp field to cogs across models, database, and controllers

This commit is contained in:
Yoga Pangestu 2026-04-24 19:57:31 +07:00
parent 22f214dc64
commit 706937f479
8 changed files with 18 additions and 18 deletions

View File

@ -50,7 +50,7 @@ public function store(OrderRequest $request): RedirectResponse
try {
DB::transaction(function () use ($validated) {
$totalItemsPrice = collect($validated['items'])->sum('total');
$hpp = collect($validated['items'])->sum(function ($item) {
$cogs = collect($validated['items'])->sum(function ($item) {
$product = Product::find($item['product_id']);
$purchasePrice = $product->prices()->where('price_type', PriceType::PURCHASE)->first()?->price ?? 0;
@ -61,7 +61,7 @@ public function store(OrderRequest $request): RedirectResponse
'user_id' => auth()->id(),
'invoice_number' => $validated['invoice_number'] ?? 'INV-'.now()->format('YmdHis').'-'.strtoupper(fake()->bothify('??##')),
'customer_name' => $validated['customer_name'],
'hpp' => $hpp,
'cogs' => $cogs,
'discount' => $validated['discount'],
'payment' => $validated['payment'],
'total' => $totalItemsPrice - $validated['discount'],
@ -124,7 +124,7 @@ public function update(OrderRequest $request, Order $order): RedirectResponse
try {
DB::transaction(function () use ($validated, $order) {
$totalItemsPrice = collect($validated['items'])->sum(fn ($item) => $item['qty'] * $item['price']);
$hpp = collect($validated['items'])->sum(function ($item) {
$cogs = collect($validated['items'])->sum(function ($item) {
$product = Product::find($item['product_id']);
$purchasePrice = $product->prices()->where('price_type', PriceType::PURCHASE)->first()?->price ?? 0;
@ -140,7 +140,7 @@ public function update(OrderRequest $request, Order $order): RedirectResponse
$order->update([
'customer_name' => $validated['customer_name'],
'hpp' => $hpp,
'cogs' => $cogs,
'discount' => $validated['discount'],
'payment' => $validated['payment'],
'total' => $totalItemsPrice - $validated['discount'],

View File

@ -40,7 +40,7 @@ public function __invoke(Request $request)
'total_revenue' => $this->getStats(fn () => $applyFilter(Order::query())->sum('total')),
'total_expenses' => $this->getStats(fn () => $applyFilter(Expense::query())->sum('amount')),
'total_payrolls' => $this->getStats(fn () => $applyFilter(Payroll::query(), 'period_month')->sum('total_salary')),
'cogs' => $this->getStats(fn () => $applyFilter(Order::query())->sum('hpp')),
'cogs' => $this->getStats(fn () => $applyFilter(Order::query())->sum('cogs')),
'total_discount' => $this->getStats(fn () => $applyFilter(Order::query())->sum('discount')),
'total_purchases' => $this->getStats(fn () => $applyFilter(Purchase::query())->sum('total')),
'products_sold' => $this->getStats(fn () => $applyFilter(OrderItem::query())->sum('qty')),
@ -160,7 +160,7 @@ public function __invoke(Request $request)
->select(
DB::raw('MONTH(created_at) as month'),
DB::raw('SUM(total) as revenue'),
DB::raw('SUM(hpp) as cogs'),
DB::raw('SUM(cogs) as cogs'),
DB::raw('COUNT(*) as count')
)
->groupBy(DB::raw('MONTH(created_at)'))

View File

@ -24,7 +24,7 @@ public function __invoke()
'total_sales' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->count()),
'total_revenue' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->sum('total')),
'total_expenses' => $this->getStats($today, $yesterday, fn ($date) => Expense::whereDate('created_at', $date)->sum('amount')),
'cogs' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->sum('hpp')),
'cogs' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->sum('cogs')),
'total_discount' => $this->getStats($today, $yesterday, fn ($date) => Order::whereDate('created_at', $date)->sum('discount')),
'total_purchases' => $this->getStats($today, $yesterday, fn ($date) => Purchase::whereDate('created_at', $date)->sum('total')),
'products_sold' => $this->getStats($today, $yesterday, fn ($date) => OrderItem::whereHas('order', fn ($q) => $q->whereDate('created_at', $date))->sum('qty')),

View File

@ -26,7 +26,7 @@ class Order extends Model
protected function casts(): array
{
return [
'hpp' => 'integer',
'cogs' => 'integer',
'discount' => 'integer',
'payment' => 'integer',
'total' => 'integer',
@ -39,7 +39,7 @@ protected function casts(): array
protected function hppFormatted(): Attribute
{
return Attribute::make(
get: fn () => 'Rp '.number_format($this->hpp, 0, ',', '.'),
get: fn () => 'Rp '.number_format($this->cogs, 0, ',', '.'),
);
}
@ -77,7 +77,7 @@ public function items(): HasMany
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['invoice_number', 'customer_name', 'hpp', 'discount', 'payment', 'total', 'payment_method', 'order_status', 'order_channel'])
->logOnly(['invoice_number', 'customer_name', 'cogs', 'discount', 'payment', 'total', 'payment_method', 'order_status', 'order_channel'])
->logOnlyDirty()
->useLogName('Pesanan');
}
@ -95,7 +95,7 @@ public function tapActivity(Activity $activity, string $eventName)
$attributeMap = [
'invoice_number' => 'Nomor Invoice',
'customer_name' => 'Nama Pelanggan',
'hpp' => 'Modal',
'cogs' => 'Modal',
'discount' => 'Diskon',
'payment' => 'Bayar',
'total' => 'Total',

View File

@ -29,7 +29,7 @@ public function definition(): array
'user_id' => User::inRandomOrder()->first()?->id ?? User::factory(),
'invoice_number' => 'INV-'.$date->format('YmdHis').'-'.strtoupper($this->faker->bothify('??##')),
'customer_name' => $this->faker->name(),
'hpp' => 0,
'cogs' => 0,
'discount' => 0,
'payment' => 0,
'total' => 0,

View File

@ -19,7 +19,7 @@ public function up(): void
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('invoice_number', 30);
$table->string('customer_name', 100);
$table->unsignedInteger('hpp');
$table->unsignedInteger('cogs');
$table->unsignedInteger('discount');
$table->unsignedInteger('payment');
$table->unsignedInteger('total');

View File

@ -51,9 +51,9 @@ private function createOrderWithItems(Order $order, $users, $products): void
$price = rand(100000, 300000);
$itemTotal = $qty * $price;
// Assuming hpp is 70% of price for simulation
$hpp = intval($price * 0.7);
$totalOrderHpp += ($hpp * $qty);
// Assuming cogs is 70% of price for simulation
$cogs = intval($price * 0.7);
$totalOrderHpp += ($cogs * $qty);
OrderItem::factory()->create([
'order_id' => $order->id,
@ -73,7 +73,7 @@ private function createOrderWithItems(Order $order, $users, $products): void
$finalTotal = max(0, $totalOrderPrice - $discount);
$order->update([
'hpp' => $totalOrderHpp,
'cogs' => $totalOrderHpp,
'discount' => $discount,
'total' => $finalTotal,
'payment' => $finalTotal, // Assume fully paid

View File

@ -4,7 +4,7 @@ export interface Order {
id: number;
invoice_number: string;
customer_name: string;
hpp: number;
cogs: number;
discount: number;
payment: number;
total: number;