$result = DB::table('orders')
->select(
'orders.id',
'orders.company_id',
DB::raw('SUM(orders.amount) as order_total_amount'),
DB::raw('COALESCE(SUM(packing_lists.amount), 0) as packing_list_total_amount'),
DB::raw('COALESCE(SUM(order_expense_detail_targets.actual),0) as expense_cut_total_amount'),
'companies.name as company_name'
)
->join('order_expenses', function ($join) use ($expenseCut) {
$join->on('orders.id', '=', 'order_expenses.order_id')
->where('order_expenses.expense_id', '=', $expenseCut->id);
})
->join('order_expense_details', function ($join) use ($filter_supplier_company_id) {
$join = $join->on('order_expenses.id', '=', 'order_expense_details.order_expense_id');
if ($filter_supplier_company_id) {
$join = $join->whereIn('order_expense_details.supplier_company_id', $filter_supplier_company_id);
}
})
->join('order_expense_detail_targets', 'order_expense_details.id', '=', 'order_expense_detail_targets.order_expense_detail_id')
->join('companies', 'companies.id', '=', 'orders.company_id')
->leftJoin('packing_lists', function ($join) use ($filter_supplier_company_id) {
$join = $join->on('orders.id', '=', 'packing_lists.orderable_id')
->where('packing_lists.orderable_type', '=', Order::class);
if ($filter_supplier_company_id) {
$join = $join->whereIn('packing_lists.supplier_company_id', $filter_supplier_company_id);
}
})
->groupBy('orders.company_id');
Bu sorgu içerisinde ki DB::raw('SUM(orders.amount) as order_total_amount') sum kısmı düzgün sonuç vermiyor halbuki
SQL sorgusunda düzgün sonuçlar veriyor
SELECT `company_id`,SUM(amount) FROM `orders` GROUP BY company_id;
mesela Order amount için sum yaptığımda misalen bir şirket için 27 çıkıyor ama sql sorgusunda 12 çıkıyor tutarsız çalışıyor