feat: 完成進貨單自動拋轉應付帳款流程與AP介面優化
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m8s
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m8s
1. 新增 AccountPayable (應付帳款) 模組,包含 Migration、Model、Service 與 Controller 2. 修改 GoodsReceipt (進貨單) 流程,在確認進貨時自動產生對應的應付帳款單 (AP-YYYYMMDD-XX) 3. 實作應付帳款詳細頁面 (Show.tsx),包含發票登記與標記付款功能 4. 修正應付帳款 Show 頁面的排版,將發票資訊套用標準的綠色背景區塊,並調整按鈕位置 5. 更新相關的 Service Provider 與 Routes
This commit is contained in:
124
app/Modules/Finance/Controllers/AccountPayableController.php
Normal file
124
app/Modules/Finance/Controllers/AccountPayableController.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Finance\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Modules\Finance\Models\AccountPayable;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class AccountPayableController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = AccountPayable::with(['vendor', 'creator']);
|
||||
|
||||
// 關鍵字搜尋 (單號、供應商名稱)
|
||||
if ($request->filled('search')) {
|
||||
$search = $request->search;
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('document_number', 'like', "%{$search}%")
|
||||
->orWhereHas('vendor', function ($q) use ($search) {
|
||||
$q->where('name', 'like', "%{$search}%");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 狀態過濾
|
||||
if ($request->filled('status') && $request->status !== 'all') {
|
||||
$query->where('status', $request->status);
|
||||
}
|
||||
|
||||
// 供應商過濾
|
||||
if ($request->filled('vendor_id') && $request->vendor_id !== 'all') {
|
||||
$query->where('vendor_id', $request->vendor_id);
|
||||
}
|
||||
|
||||
// 日期區間過濾
|
||||
if ($request->filled('date_start')) {
|
||||
$query->where('due_date', '>=', $request->date_start);
|
||||
}
|
||||
if ($request->filled('date_end')) {
|
||||
$query->where('due_date', '<=', $request->date_end);
|
||||
}
|
||||
|
||||
$perPage = $request->input('per_page', 10);
|
||||
$payables = $query->latest()->paginate($perPage)->withQueryString();
|
||||
|
||||
$vendors = \App\Modules\Procurement\Models\Vendor::select('id', 'name')->get();
|
||||
|
||||
return Inertia::render('AccountPayable/Index', [
|
||||
'payables' => $payables,
|
||||
'filters' => $request->all(['search', 'status', 'vendor_id', 'date_start', 'date_end', 'per_page']),
|
||||
'vendors' => $vendors,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(AccountPayable $accountPayable)
|
||||
{
|
||||
$accountPayable->load(['vendor', 'creator']);
|
||||
|
||||
// 嘗試加載來源單據資訊 (目前支援 goods_receipt)
|
||||
$sourceDocumentCode = null;
|
||||
if ($accountPayable->source_document_type === 'goods_receipt') {
|
||||
$receipt = \App\Modules\Inventory\Models\GoodsReceipt::find($accountPayable->source_document_id);
|
||||
if ($receipt) {
|
||||
$sourceDocumentCode = $receipt->code;
|
||||
}
|
||||
}
|
||||
|
||||
return Inertia::render('AccountPayable/Show', [
|
||||
// 將 model 轉換成 array 加入額外資訊
|
||||
'payable' => array_merge($accountPayable->toArray(), ['source_document_code' => $sourceDocumentCode]),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新發票資訊
|
||||
*/
|
||||
public function updateInvoice(Request $request, AccountPayable $accountPayable)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'invoice_number' => 'nullable|string|max:50',
|
||||
'invoice_date' => 'nullable|date',
|
||||
]);
|
||||
|
||||
$accountPayable->update([
|
||||
'invoice_number' => $validated['invoice_number'],
|
||||
'invoice_date' => $validated['invoice_date'],
|
||||
]);
|
||||
|
||||
return back()->with('success', '發票資訊已更新');
|
||||
}
|
||||
|
||||
/**
|
||||
* 標記已付款
|
||||
*/
|
||||
public function pay(Request $request, AccountPayable $accountPayable)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'payment_method' => 'required|string|max:50',
|
||||
'paid_at' => 'required|date',
|
||||
'payment_note' => 'nullable|string|max:255',
|
||||
]);
|
||||
|
||||
if ($accountPayable->status === AccountPayable::STATUS_PAID) {
|
||||
return back()->with('error', '該帳款已經標記為已付款');
|
||||
}
|
||||
|
||||
$accountPayable->update([
|
||||
'status' => AccountPayable::STATUS_PAID,
|
||||
'payment_method' => $validated['payment_method'],
|
||||
'paid_at' => $validated['paid_at'],
|
||||
'payment_note' => $validated['payment_note'],
|
||||
]);
|
||||
|
||||
return back()->with('success', '帳款已成功標記為已付款');
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,9 @@ class FinanceServiceProvider extends ServiceProvider
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
\Illuminate\Support\Facades\Event::listen(
|
||||
\App\Modules\Inventory\Events\GoodsReceiptApprovedEvent::class,
|
||||
\App\Modules\Finance\Listeners\CreateAccountPayableFromGoodsReceipt::class
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Finance\Listeners;
|
||||
|
||||
use App\Modules\Inventory\Events\GoodsReceiptApprovedEvent;
|
||||
use App\Modules\Finance\Services\AccountPayableService;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class CreateAccountPayableFromGoodsReceipt
|
||||
{
|
||||
protected AccountPayableService $accountPayableService;
|
||||
|
||||
/**
|
||||
* Create the event listener.
|
||||
*/
|
||||
public function __construct(AccountPayableService $accountPayableService)
|
||||
{
|
||||
$this->accountPayableService = $accountPayableService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*/
|
||||
public function handle(GoodsReceiptApprovedEvent $event): void
|
||||
{
|
||||
try {
|
||||
// 目前使用系統預設 User ID 或 0 作為自動生成的建立者,若能從 event 取得更好
|
||||
$userId = auth()->id() ?? 1; // 假設 1 為系統管理員或預設使用者
|
||||
|
||||
$this->accountPayableService->createFromGoodsReceipt($event->goodsReceiptId, $userId);
|
||||
|
||||
Log::info("已成功為進貨單 ID: {$event->goodsReceiptId} 建立應付帳款");
|
||||
} catch (\Exception $e) {
|
||||
Log::error("建立應付帳款失敗 (進貨單 ID: {$event->goodsReceiptId}): " . $e->getMessage());
|
||||
// 根據需求決定是否拋出 exception 或只記錄 log
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
61
app/Modules/Finance/Models/AccountPayable.php
Normal file
61
app/Modules/Finance/Models/AccountPayable.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Finance\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class AccountPayable extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public const STATUS_PENDING = 'pending';
|
||||
public const STATUS_PARTIALLY_PAID = 'partially_paid';
|
||||
public const STATUS_PAID = 'paid';
|
||||
public const STATUS_CANCELLED = 'cancelled';
|
||||
|
||||
protected $fillable = [
|
||||
'vendor_id',
|
||||
'source_document_type',
|
||||
'source_document_id',
|
||||
'document_number',
|
||||
'total_amount',
|
||||
'tax_amount',
|
||||
'status',
|
||||
'due_date',
|
||||
'invoice_number',
|
||||
'invoice_date',
|
||||
'paid_at',
|
||||
'payment_method',
|
||||
'payment_note',
|
||||
'remarks',
|
||||
'created_by',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'total_amount' => 'decimal:2',
|
||||
'tax_amount' => 'decimal:2',
|
||||
'due_date' => 'date',
|
||||
'invoice_date' => 'date',
|
||||
'paid_at' => 'datetime',
|
||||
];
|
||||
|
||||
/**
|
||||
* 關聯:供應商
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function vendor(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\App\Modules\Procurement\Models\Vendor::class, 'vendor_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 關聯:建立者
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function creator(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\App\Modules\Core\Models\User::class, 'created_by');
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,17 @@ use Illuminate\Support\Facades\Route;
|
||||
use App\Modules\Finance\Controllers\UtilityFeeController;
|
||||
use App\Modules\Finance\Controllers\AccountingReportController;
|
||||
|
||||
use App\Modules\Finance\Controllers\AccountPayableController;
|
||||
|
||||
Route::middleware('auth')->group(function () {
|
||||
// 應付帳款
|
||||
Route::group(['prefix' => 'finance'], function () {
|
||||
Route::get('/account-payables', [AccountPayableController::class, 'index'])->name('account-payables.index');
|
||||
Route::get('/account-payables/{accountPayable}', [AccountPayableController::class, 'show'])->name('account-payables.show');
|
||||
Route::post('/account-payables/{accountPayable}/invoice', [AccountPayableController::class, 'updateInvoice'])->name('account-payables.invoice');
|
||||
Route::post('/account-payables/{accountPayable}/pay', [AccountPayableController::class, 'pay'])->name('account-payables.pay');
|
||||
});
|
||||
|
||||
// 公共事業費管理
|
||||
Route::middleware('permission:utility_fees.view')->group(function () {
|
||||
Route::get('/utility-fees', [UtilityFeeController::class, 'index'])->name('utility-fees.index');
|
||||
|
||||
85
app/Modules/Finance/Services/AccountPayableService.php
Normal file
85
app/Modules/Finance/Services/AccountPayableService.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Finance\Services;
|
||||
|
||||
use App\Modules\Finance\Models\AccountPayable;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Modules\Inventory\Contracts\GoodsReceiptServiceInterface;
|
||||
|
||||
class AccountPayableService
|
||||
{
|
||||
protected GoodsReceiptServiceInterface $goodsReceiptService;
|
||||
|
||||
public function __construct(GoodsReceiptServiceInterface $goodsReceiptService)
|
||||
{
|
||||
$this->goodsReceiptService = $goodsReceiptService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根據進貨單建立應付帳款
|
||||
*
|
||||
* @param int $goodsReceiptId
|
||||
* @param int $userId 執行操作的使用者 ID
|
||||
* @return AccountPayable
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function createFromGoodsReceipt(int $goodsReceiptId, int $userId): AccountPayable
|
||||
{
|
||||
// 透過 Contract 取得 Inventory 模組的資料,避免直接依賴 Model
|
||||
$receiptData = $this->goodsReceiptService->getGoodsReceiptData($goodsReceiptId);
|
||||
|
||||
if (!$receiptData) {
|
||||
throw new \Exception("找不到對應的進貨單資料 (ID: {$goodsReceiptId})");
|
||||
}
|
||||
|
||||
// 檢查是否已經建立過(密等性)
|
||||
$existingAp = AccountPayable::where('source_document_type', 'goods_receipt')
|
||||
->where('source_document_id', $goodsReceiptId)
|
||||
->first();
|
||||
|
||||
if ($existingAp) {
|
||||
return $existingAp;
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($receiptData, $userId) {
|
||||
$ap = AccountPayable::create([
|
||||
'vendor_id' => $receiptData['vendor_id'],
|
||||
'source_document_type' => 'goods_receipt',
|
||||
'source_document_id' => $receiptData['id'],
|
||||
'document_number' => $this->generateApNumber(),
|
||||
'total_amount' => collect($receiptData['items'] ?? [])->sum('total_amount'),
|
||||
'tax_amount' => 0, // 假設後續會實作稅額計算,目前預設為 0
|
||||
'status' => AccountPayable::STATUS_PENDING,
|
||||
// 設定應付日期,預設為進貨後 30 天 (可依據供應商設定調整)
|
||||
'due_date' => now()->addDays(30)->toDateString(),
|
||||
'created_by' => $userId,
|
||||
'remarks' => "由進貨單 {$receiptData['code']} 自動生成",
|
||||
]);
|
||||
|
||||
return $ap;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 產生應付帳款單號
|
||||
*/
|
||||
protected function generateApNumber(): string
|
||||
{
|
||||
$prefix = 'AP-' . date('Ymd') . '-';
|
||||
$lastPrefix = "{$prefix}%";
|
||||
|
||||
$latest = AccountPayable::where('document_number', 'like', $lastPrefix)
|
||||
->orderBy('document_number', 'desc')
|
||||
->first();
|
||||
|
||||
if (!$latest) {
|
||||
return $prefix . '01';
|
||||
}
|
||||
|
||||
$parts = explode('-', $latest->document_number);
|
||||
$lastNumber = intval(end($parts));
|
||||
$newNumber = str_pad((string)($lastNumber + 1), 2, '0', STR_PAD_LEFT);
|
||||
|
||||
return $prefix . $newNumber;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user