@php $from = $widget['date_from']; $to = $widget['date_to']; @endphp

Sales Backlog

{{$from}} - {{$to}}
@php $groups = \App\Models\CompanyGroup::with([ 'companies' => function ($company) use ($from, $to) { $company->with([ 'projects' => function($query) use ($from, $to) { $query->where('is_confirmed', true); $query->whereBetween('created_at', [$from, $to]); } ]); }, ])->get(); @endphp @foreach ($groups as $group) @php $companyAmountTotal = 0; $userCommissionArray = array(); $loopNumber = 0; $values = null; foreach ($group->companies as $company) { if (is_array($company->projects) || is_object($company->projects)) { $index = 0; foreach ($company->projects as $project) { $agreementSalesAmount = 0; $variationOrderSalesAmount = 0; $supplierInvoiceCostAmout = 0; // Check Supplier Invoice $supplierInvoices = \App\Models\SupplierInvoice::where('project_id', $project->id)->where('is_approved', true)->get(); if ($supplierInvoices->count() > 0) { foreach ($supplierInvoices as $supplierInvoice) { $supplierInvoiceCostAmout += $supplierInvoice->amount; } } $agreements = \App\Models\Agreement::where('company_id', $company->id)->where('project_id', $project->id) ->where('is_approved', true) ->where('approved_by', '!=', null) ->get(); $variationOrders = \App\Models\VariationOrder::where('company_id', $company->id)->where('project_id', $project->id)->get(); foreach ($agreements as $agreement) { $agreementItem = \App\Models\AgreementItem::where('agreement_id', $agreement->id)->limit(1)->orderBy('id', 'desc')->first(); if ($agreementItem) { $agreementItemLists = \App\Models\AgreementItemList::where('agreement_id', $agreement->id)->where('agreement_item_id', $agreementItem->id)->where('tier_level', 1)->get(); foreach ($agreementItemLists as $agreementItemList) { if ($supplierInvoiceCostAmout != 0) { $agreementSalesAmount += $agreementItemList->amount; } } } } foreach ($variationOrders as $variationOrder) { $variationOrderItem = \App\Models\VariationOrderItem::where('variation_order_id', $variationOrder->id)->limit(1)->orderBy('id', 'desc')->first(); if ($variationOrderItem) { $variationOrderItemLists = \App\Models\VariationOrderItemList::where('variation_order_id', $variationOrder->id)->where('variation_order_item_id', $variationOrderItem->id)->where('tier_level', 1)->get(); foreach ($variationOrderItemLists as $variationOrderItemList) { if ($supplierInvoiceCostAmout != 0) { $variationOrderSalesAmount += $variationOrderItemList->amount; } } } } $totalSales = $agreementSalesAmount + $variationOrderSalesAmount; $totalProfit = $totalSales-$supplierInvoiceCostAmout; $projectMemberCommissions = \App\Models\ProjectMemberCommission::where('project_id', $project->id)->with('user')->get(); foreach ($projectMemberCommissions as $projectMemberCommission) { $commission = $totalProfit*($projectMemberCommission->user->commission/100)*($projectMemberCommission->commission/100); $companyAmountTotal += $commission; $userCommissionArray[$index][$projectMemberCommission->user->name] = ($supplierInvoiceCostAmout != 0 ? $commission : 0); } $final = array(); foreach($userCommissionArray as $value) { $final = array_merge($final, $value); } $indej = 0; foreach($final as $key => &$value){ $value = array_sum(array_column($userCommissionArray, $key)); $values[$indej]['value'] = $value; $values[$indej]['name'] = $key; $indej++; } $index++; } } } @endphp
{{$group->name}}

${{number_format($companyAmountTotal)}}


@if (is_array($values) || is_object($values)) @foreach ($values as $key => $value) @if (!empty($value)) @endif @endforeach @endif
{{$value['name']}} ${{number_format($value['value'])}}
@php unset($value); unset($values); unset($userCommissionArray); @endphp @endforeach