Compare commits

...

2 Commits

Author SHA1 Message Date
6f9bdff115 update
All checks were successful
Build, Push and Deploy / build-and-push (push) Successful in 3m59s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 18s
Build, Push and Deploy / deploy-staging (push) Successful in 39s
Build, Push and Deploy / deploy-production (push) Has been skipped
2026-05-14 12:25:21 +07:00
ce2b815b07 Fix BadMethodCallException and remaining PostgreSQL issues 2026-05-14 12:24:33 +07:00
5 changed files with 49 additions and 24 deletions

View File

@@ -241,7 +241,7 @@ public function incomeExpense(Request $request)
$data['expenses'] = Sale::where('business_id', auth()->user()->business_id) $data['expenses'] = Sale::where('business_id', auth()->user()->business_id)
->whereYear('created_at', request('year') ?? date('Y')) ->whereYear('created_at', request('year') ?? date('Y'))
->where('lossProfit', '<', 0) ->where('lossProfit', '<', 0)
->selectRaw("EXTRACT(MONTH FROM created_at) as month_number, TO_CHAR(created_at, 'Month') as month, SUM(ABS(lossProfit)) as total") ->selectRaw("EXTRACT(MONTH FROM created_at) as month_number, TO_CHAR(created_at, 'Month') as month, SUM(ABS(\"lossProfit\")) as total")
->groupBy('month_number', 'month') ->groupBy('month_number', 'month')
->orderBy('month_number') ->orderBy('month_number')
->get(); ->get();
@@ -249,7 +249,7 @@ public function incomeExpense(Request $request)
$data['incomes'] = Sale::where('business_id', auth()->user()->business_id) $data['incomes'] = Sale::where('business_id', auth()->user()->business_id)
->whereYear('created_at', request('year') ?? date('Y')) ->whereYear('created_at', request('year') ?? date('Y'))
->where('lossProfit', '>=', 0) ->where('lossProfit', '>=', 0)
->selectRaw("EXTRACT(MONTH FROM created_at) as month_number, TO_CHAR(created_at, 'Month') as month, SUM(ABS(lossProfit)) as total") ->selectRaw("EXTRACT(MONTH FROM created_at) as month_number, TO_CHAR(created_at, 'Month') as month, SUM(ABS(\"lossProfit\")) as total")
->groupBy('month_number', 'month') ->groupBy('month_number', 'month')
->orderBy('month_number') ->orderBy('month_number')
->get(); ->get();

View File

@@ -34,8 +34,9 @@ public function getDashboardData()
public function yearlySubscriptions() public function yearlySubscriptions()
{ {
$subscriptions = PlanSubscribe::whereYear('created_at', request('year') ?? date('Y')) $subscriptions = PlanSubscribe::whereYear('created_at', request('year') ?? date('Y'))
->selectRaw("TO_CHAR(created_at, 'Month') as month, SUM(price) as total_amount") ->selectRaw("TO_CHAR(created_at, 'FMMonth') as month, SUM(price) as total_amount")
->groupBy('month') ->groupBy(DB::raw("TO_CHAR(created_at, 'FMMonth')"))
->orderBy(DB::raw("MIN(created_at)"))
->get(); ->get();
return response()->json($subscriptions); return response()->json($subscriptions);

View File

@@ -118,7 +118,7 @@ public function dashboard()
$sales_data = Sale::selectRaw("TO_CHAR(created_at, '$dateFormatSQL') as date, SUM(\"totalAmount\") as amount") $sales_data = Sale::selectRaw("TO_CHAR(created_at, '$dateFormatSQL') as date, SUM(\"totalAmount\") as amount")
->where('business_id', $business_id) ->where('business_id', $business_id)
->whereBetween('created_at', [$start, $end]) ->whereBetween('created_at', [$start, $end])
->groupBy('date') ->groupBy(DB::raw("TO_CHAR(created_at, '$dateFormatSQL')"))
->orderBy('date') ->orderBy('date')
->get() ->get()
->map(function ($item) { ->map(function ($item) {
@@ -131,7 +131,7 @@ public function dashboard()
$purchase_data = Purchase::selectRaw("TO_CHAR(created_at, '$dateFormatSQL') as date, SUM(\"totalAmount\") as amount") $purchase_data = Purchase::selectRaw("TO_CHAR(created_at, '$dateFormatSQL') as date, SUM(\"totalAmount\") as amount")
->where('business_id', $business_id) ->where('business_id', $business_id)
->whereBetween('created_at', [$start, $end]) ->whereBetween('created_at', [$start, $end])
->groupBy('date') ->groupBy(DB::raw("TO_CHAR(created_at, '$dateFormatSQL')"))
->orderBy('date') ->orderBy('date')
->get() ->get()
->map(function ($item) { ->map(function ($item) {

View File

@@ -84,6 +84,17 @@ public function dueCollect()
{ {
return $this->hasOne(DueCollect::class); return $this->hasOne(DueCollect::class);
} }
public function dueForBranch($branchId = null)
{
$query = $this->type === 'Supplier' ? $this->purchases_dues() : $this->sales_dues();
if ($branchId) {
$query->where('branch_id', $branchId);
}
return $query->sum('dueAmount');
}
/** /**
* The attributes that should be cast to native types. * The attributes that should be cast to native types.
* *

View File

@@ -1,24 +1,37 @@
# Walkthrough - Fixing PostgreSQL Compatibility # Fix: BadMethodCallException & PostgreSQL Compatibility Audit
I have resolved the SQL errors occurring in the Loss Profit History module when running on PostgreSQL. I have resolved the `BadMethodCallException` reported in the `AcnooSaleController` and performed a final "automated" audit to fix remaining PostgreSQL compatibility issues.
## Changes Made ## Changes Made
### Business Module Controllers ### 1. Fixed Missing `dueForBranch` Method
- **[AcnooLossProfitHistoryController.php](file:///media/itc43/Data/OTHER_PROJECT/kulakpos/kulakpos_15Maret2026_to%20GTEA/public_html/Modules/Business/App/Http/Controllers/AcnooLossProfitHistoryController.php)**: - **File**: [Party.php](file:///media/itc43/Data/OTHER_PROJECT/kulakpos/kulakpos_15Maret2026_to%20GTEA/public_html/app/Models/Party.php)
- Quoted `saleDate`, `lossProfit`, `incomeDate`, `expenseDate`, and `date` columns in all `DB::raw` expressions. - **Fix**: Implemented the `dueForBranch($branchId = null)` method in the `Party` model.
- Changed double quotes (`"`) to single quotes (`'`) for string literals (`'positive'`, `'negative'`) in `CASE` statements. - **Logic**:
- **[AcnooLossProfitHistoryReportController.php](file:///media/itc43/Data/OTHER_PROJECT/kulakpos/kulakpos_15Maret2026_to%20GTEA/public_html/Modules/Business/App/Http/Controllers/AcnooLossProfitHistoryReportController.php)**: - For **Suppliers**: Sums `dueAmount` from the `purchases` table for the specified branch.
- Applied identical fixes for PostgreSQL compatibility. - For **Customers**: Sums `dueAmount` from the `sales` table for the specified branch.
- **[AcnooProductLossProfitReportController.php](file:///media/itc43/Data/OTHER_PROJECT/kulakpos/kulakpos_15Maret2026_to%20GTEA/public_html/Modules/Business/App/Http/Controllers/AcnooProductLossProfitReportController.php)**: - This resolves the crash on the Sale and Purchase creation screens.
- Quoted `lossProfit` in `CASE` statements.
### Export Classes ### 2. Automated PostgreSQL Audit & Fixes
- **[ExportLossProfitHistory.php](file:///media/itc43/Data/OTHER_PROJECT/kulakpos/kulakpos_15Maret2026_to%20GTEA/public_html/Modules/Business/App/Exports/ExportLossProfitHistory.php)**: I ran an audit script to find remaining MySQL-specific syntax or unquoted mixed-case identifiers.
- Fixed column quoting and string literals for Excel/CSV exports.
- **[ExportProductLossProfit.php](file:///media/itc43/Data/OTHER_PROJECT/kulakpos/kulakpos_15Maret2026_to%20GTEA/public_html/Modules/Business/App/Exports/ExportProductLossProfit.php)**:
- Fixed column quoting in `CASE` statements for product loss/profit exports.
## Verification Results - **[AcnooBranchController.php](file:///media/itc43/Data/OTHER_PROJECT/kulakpos/kulakpos_15Maret2026_to%20GTEA/public_html/Modules/MultiBranchAddon/App/Http/Controllers/AcnooBranchController.php)**: Quoted `"lossProfit"` in `incomeExpense` reports.
- The code changes directly address the `SQLSTATE[42703]` (Undefined column) error by ensuring PostgreSQL recognizes the mixed-case column names. - **[Admin/DashboardController.php](file:///media/itc43/Data/OTHER_PROJECT/kulakpos/kulakpos_15Maret2026_to%20GTEA/public_html/app/Http/Controllers/Admin/DashboardController.php)**:
- String literal fixes prevent PostgreSQL from interpreting `'positive'` and `'negative'` as column names. - Updated `TO_CHAR` format to `FMMonth` to avoid padded spaces.
- Fixed `groupBy` to use the full expression instead of an alias for better PostgreSQL compatibility.
- **[StatisticsController.php](file:///media/itc43/Data/OTHER_PROJECT/kulakpos/kulakpos_15Maret2026_to%20GTEA/public_html/app/Http/Controllers/Api/StatisticsController.php)**:
- Updated `groupBy` logic to use the `TO_CHAR` expression directly, preventing "must appear in the GROUP BY clause" errors.
## Verification
### Automated Audit
- The audit script was used to identify these locations.
- You can find the script here: [audit_postgres.py](file:///media/itc43/Data/OTHER_PROJECT/kulakpos/kulakpos_15Maret2026_to%20GTEA/public_html/scratch/audit_postgres.py).
### Manual Verification Required
1. **Sale Creation**: Open the "Create Sale" screen to verify that customer due amounts are now displayed correctly without error.
2. **Purchase Creation**: Open the "Create Purchase" screen to verify supplier due amounts.
3. **Reports**: Check the Branch Income/Expense report and Admin Dashboard to ensure charts are rendering correctly.
> [!NOTE]
> I have committed these changes locally. Please run `git push` from your terminal to upload the fixes to your repository.