I have 2 array to get the sold products from location at different date ranges then I need to make comparison between the two arrays
$results_day_report = DB::table('transaction_sell_lines as tsl')
->select(DB::raw('b.name as business_location_name , t.location_id as location_id , tsl.product_id as product_id , p.name as product_name , tsl.variation_id as variation_id , sum(tsl.quantity) AS quantity , sum(tsl.quantity * tsl.unit_price_inc_tax) AS total'))
->leftjoin('transactions as t','t.id', '=', 'tsl.transaction_id')
->leftjoin('business_locations as b','b.id', '=', 't.location_id')
->leftjoin('products as p','p.id', '=', 'tsl.product_id')
->whereRaw("t.transaction_date LIKE '$reportdate%'")
->where('b.custom_field1', $location_type)
->where('t.type', '=' , 'sell')
->where('t.status', '=' , 'final')
->where('p.id', '!=' , 1)
->groupBy('tsl.variation_id')
->orderBy('tsl.variation_id', 'asc')
->orderBy('b.id', 'asc')
->get();
$results_day_report_year_before = DB::table('transaction_sell_lines as tsl')
->select(DB::raw('b.name as business_location_name , t.location_id as location_id , tsl.product_id as product_id , p.name as product_name , tsl.variation_id as variation_id , sum(tsl.quantity) AS quantity , sum(tsl.quantity * tsl.unit_price_inc_tax) AS total'))
->leftjoin('transactions as t','t.id', '=', 'tsl.transaction_id')
->leftjoin('business_locations as b','b.id', '=', 't.location_id')
->leftjoin('products as p','p.id', '=', 'tsl.product_id')
->whereRaw("t.transaction_date LIKE '$reportdate_year_before%'")
->where('b.custom_field1', $location_type)
->where('t.type', '=' , 'sell')
->where('t.status', '=' , 'final')
->where('p.id', '!=' , 1)
->groupBy('tsl.variation_id')
->orderBy('tsl.variation_id', 'asc')
->orderBy('b.id', 'asc')
->get();
and Now I have results to be $results_day_report =
Illuminate\Support\Collection {#4170 ▼
#items: array:23 [▼
0 => {#4172 ▼
"business_location_name": "Gaming Area"
"location_id": 19
"product_id": 526
"product_name": "سيارات تصادم (مايكل)"
"variation_id": 674
"quantity": "15.0000"
"total": "300.00000000"
}
1 => {#4178 ▼
"business_location_name": "Gaming Area"
"location_id": 19
"product_id": 528
"product_name": "قطار اطفال (مايكل)"
"variation_id": 676
"quantity": "5.0000"
"total": "50.00000000"
}
2 => {#4179 ▼
"business_location_name": "Gaming Area"
"location_id": 19
"product_id": 529
"product_name": "ارنوب (مايكل)"
"variation_id": 677
"quantity": "3.0000"
"total": "30.00000000"
}
3 => {#4173 ▼
"business_location_name": "Lavandula Gym"
"location_id": 17
"product_id": 594
"product_name": "جم رجال يومى ع.د"
"variation_id": 742
"quantity": "3.0000"
"total": "90.00000000"
}
4 => {#4171 ▼
"business_location_name": "Lavandula Gym"
"location_id": 17
"product_id": 595
"product_name": "جم رجال يومى ق.م"
"variation_id": 743
"quantity": "1.0000"
"total": "60.00000000"
}
5 => {#4177 ▼
"business_location_name": "Lavandula Gym"
"location_id": 17
"product_id": 597
"product_name": "جم رجال شهرى ع.د"
"variation_id": 745
"quantity": "1.0000"
"total": "200.00000000"
}
6 => {#4175 ▼
"business_location_name": "Lavandula Gym"
"location_id": 17
"product_id": 615
"product_name": "رسم استخراج كارنيه اول مره"
"variation_id": 763
"quantity": "1.0000"
"total": "30.00000000"
}
7 => {#4180 ▼
"business_location_name": "Tiba Rose Gym"
"location_id": 16
"product_id": 637
"product_name": "جلسة مساج مشتركين"
"variation_id": 785
"quantity": "2.0000"
"total": "400.00000000"
}
8 => {#4181 ▼
"business_location_name": "Tiba Rose Gym"
"location_id": 16
"product_id": 679
"product_name": "جيم سيدات يومى ع د"
"variation_id": 827
"quantity": "2.0000"
"total": "70.00000000"
}
9 => {#4182 ▼
"business_location_name": "Tiba Rose Gym"
"location_id": 16
"product_id": 681
"product_name": "جيم سيدات يومى مدنى"
"variation_id": 829
"quantity": "1.0000"
"total": "85.00000000"
}
10 => {#4183 ▼
"business_location_name": "Tiba Rose Gym"
"location_id": 16
"product_id": 683
"product_name": "جيم سيدات شهرى ق م"
"variation_id": 831
"quantity": "1.0000"
"total": "450.00000000"
}
11 => {#4184 ▼
"business_location_name": "Tiba Rose Gym"
"location_id": 16
"product_id": 705
"product_name": "حمام سباحة مغطى(م)"
"variation_id": 853
"quantity": "1.0000"
"total": "75.00000000"
}
12 => {#4185 ▼
"business_location_name": "Subscriptions"
"location_id": 20
"product_id": 773
"product_name": "العاب ثابته"
"variation_id": 921
"quantity": "3.0000"
"total": "30.00000000"
}
13 => {#4186 ▼
"business_location_name": "Gaming Area"
"location_id": 19
"product_id": 6140
"product_name": "الترامبولين م"
"variation_id": 6291
"quantity": "7.0000"
"total": "70.00000000"
}
14 => {#4187 ▼
"business_location_name": "Gaming Area"
"location_id": 19
"product_id": 6568
"product_name": "كاروسيل (مايكل)"
"variation_id": 6719
"quantity": "5.0000"
"total": "50.00000000"
}
15 => {#4188 ▼
"business_location_name": "Red Zone"
"location_id": 44
"product_id": 7781
"product_name": "مصارعه سومو"
"variation_id": 7932
"quantity": "10.0000"
"total": "217.39100000"
}
16 => {#4189 ▼
"business_location_name": "Red Zone"
"location_id": 44
"product_id": 7782
"product_name": "تيلي جيمز"
"variation_id": 7933
"quantity": "10.0000"
"total": "217.39100000"
}
17 => {#4190 ▼
"business_location_name": "Red Zone"
"location_id": 44
"product_id": 7783
"product_name": "فولي بيج بول"
"variation_id": 7934
"quantity": "10.0000"
"total": "347.82600000"
}
18 => {#4191 ▼
"business_location_name": "Red Zone"
"location_id": 44
"product_id": 7784
"product_name": "سوكر شوز"
"variation_id": 7935
"quantity": "10.0000"
"total": "347.82600000"
}
19 => {#4192 ▼
"business_location_name": "Red Zone"
"location_id": 44
"product_id": 7785
"product_name": "الساعة"
"variation_id": 7936
"quantity": "10.0000"
"total": "434.78300000"
}
20 => {#4193 ▼
"business_location_name": "Tiba Rose Gym"
"location_id": 16
"product_id": 9091
"product_name": "اكستريم جيم سيدات شهري مدني"
"variation_id": 9251
"quantity": "1.0000"
"total": "800.00000000"
}
21 => {#4194 ▼
"business_location_name": "Red Zone"
"location_id": 44
"product_id": 9341
"product_name": "ارشيرلي فايت"
"variation_id": 9501
"quantity": "10.0000"
"total": "434.78300000"
}
22 => {#4195 ▼
"business_location_name": "Red Zone"
"location_id": 44
"product_id": 9342
"product_name": "كومبو (2)"
"variation_id": 9502
"quantity": "10.0000"
"total": "2000.00000000"
}
]
}
and result of $results_day_report_year_before to be
Illuminate\Support\Collection {#4174 ▼
#items: array:15 [▼
0 => {#4196 ▼
"business_location_name": "Video Game"
"location_id": 18
"product_id": 513
"product_name": "موتوسيكل"
"variation_id": 661
"quantity": "1.0000"
"total": "15.00000000"
}
1 => {#4202 ▼
"business_location_name": "Video Game"
"location_id": 18
"product_id": 517
"product_name": "هزازات"
"variation_id": 665
"quantity": "1.0000"
"total": "10.00000000"
}
2 => {#4203 ▼
"business_location_name": "Video Game"
"location_id": 18
"product_id": 520
"product_name": "هدايا كبيرة"
"variation_id": 668
"quantity": "1.0000"
"total": "20.00000000"
}
3 => {#4197 ▼
"business_location_name": "Gaming Area"
"location_id": 19
"product_id": 526
"product_name": "سيارات تصادم (مايكل)"
"variation_id": 674
"quantity": "24.0000"
"total": "480.00000000"
}
4 => {#4176 ▼
"business_location_name": "Gaming Area"
"location_id": 19
"product_id": 527
"product_name": "سامبا (مايكل)"
"variation_id": 675
"quantity": "2.0000"
"total": "40.00000000"
}
5 => {#4201 ▼
"business_location_name": "Gaming Area"
"location_id": 19
"product_id": 528
"product_name": "قطار اطفال (مايكل)"
"variation_id": 676
"quantity": "4.0000"
"total": "40.00000000"
}
6 => {#4199 ▼
"business_location_name": "Lavandula Gym"
"location_id": 17
"product_id": 594
"product_name": "جم رجال يومى ع.د"
"variation_id": 742
"quantity": "1.0000"
"total": "30.00000000"
}
7 => {#4204 ▼
"business_location_name": "Lavandula Gym"
"location_id": 17
"product_id": 598
"product_name": "جم رجال شهرى ق.م"
"variation_id": 746
"quantity": "1.0000"
"total": "350.00000000"
}
8 => {#4205 ▼
"business_location_name": "Tiba Rose Gym"
"location_id": 16
"product_id": 615
"product_name": "رسم استخراج كارنيه اول مره"
"variation_id": 763
"quantity": "5.0000"
"total": "50.00000000"
}
9 => {#4206 ▼
"business_location_name": "Tiba Rose Gym"
"location_id": 16
"product_id": 688
"product_name": "جم رجال يومى ع.د"
"variation_id": 836
"quantity": "2.0000"
"total": "70.00000000"
}
10 => {#4207 ▼
"business_location_name": "Tiba Rose Gym"
"location_id": 16
"product_id": 690
"product_name": "جم رجال يومى مدنى"
"variation_id": 838
"quantity": "1.0000"
"total": "85.00000000"
}
11 => {#4208 ▼
"business_location_name": "Tiba Rose Gym"
"location_id": 16
"product_id": 691
"product_name": "جم رجال شهرى ع.د"
"variation_id": 839
"quantity": "3.0000"
"total": "750.00000000"
}
12 => {#4209 ▼
"business_location_name": "Tiba Rose Gym"
"location_id": 16
"product_id": 692
"product_name": "جم رجال شهرى ق.م"
"variation_id": 840
"quantity": "1.0000"
"total": "450.00000000"
}
13 => {#4210 ▼
"business_location_name": "Subscriptions"
"location_id": 20
"product_id": 763
"product_name": "حجزملعب س/ط س ن"
"variation_id": 911
"quantity": "4.0000"
"total": "560.00000000"
}
14 => {#4211 ▼
"business_location_name": "Subscriptions"
"location_id": 20
"product_id": 773
"product_name": "العاب ثابته"
"variation_id": 921
"quantity": "2.0000"
"total": "20.00000000"
}
]
}
then I make this foreach loop to generate one array with the comparison
foreach($results_day_report as $result_day_report)
{
array_push($queries, (object)[
'business_location_name' => $result_day_report->business_location_name,
'location_id' => $result_day_report->location_id,
'product_id' => $result_day_report->product_id,
'product_name' => $result_day_report->product_name,
'variation_id' => $result_day_report->variation_id,
'quantity_report_date_same_year' => $result_day_report->quantity,
'total_report_date_same_year' => $result_day_report->total,
'quantity_report_date_year_before' => 0,
'total_report_date_year_before' => 0,
'quantity_date_range_same_year' => 0,
'total_date_range_same_year' => 0,
'quantity_date_range_year_before' => 0,
'total_date_range_year_before' => 0,
'step_a' => '1',
]);
}
foreach($results_day_report_year_before as $result_day_report_year_before)
{
$collection = collect($queries);
if(!$collection->contains('variation_id', $result_day_report_year_before->variation_id))
{
array_push($queries, (object)[
'business_location_name' => $result_day_report_year_before->business_location_name,
'location_id' => $result_day_report_year_before->location_id,
'product_id' => $result_day_report_year_before->product_id,
'product_name' => $result_day_report_year_before->product_name,
'variation_id' => $result_day_report_year_before->variation_id,
'quantity_report_date_same_year' => 0,
'total_report_date_same_year' => 0,
'quantity_report_date_year_before' => $result_day_report_year_before->quantity,
'total_report_date_year_before' => $result_day_report_year_before->total,
'quantity_date_range_same_year' => 0,
'total_date_range_same_year' => 0,
'quantity_date_range_year_before' => 0,
'total_date_range_year_before' => 0,
'step_b' => '1',
]);
}
else
{
$data = $collection->map(function ($item, $key) use ($result_day_report_year_before) {
if($item->location_id == $result_day_report_year_before->location_id && $item->variation_id == $result_day_report_year_before->variation_id)
{
$item->quantity_report_date_year_before = $result_day_report_year_before->quantity ;
$item->total_report_date_year_before = $result_day_report_year_before->total ;
$item->step_b = 2 ;
}
elseif($item->location_id != $result_day_report_year_before->location_id && $item->variation_id == $result_day_report_year_before->variation_id)
{
array_push($queries, (object)[
'business_location_name' => $result_day_report_year_before->business_location_name,
'location_id' => $result_day_report_year_before->location_id,
'product_id' => $result_day_report_year_before->product_id,
'product_name' => $result_day_report_year_before->product_name,
'variation_id' => $result_day_report_year_before->variation_id,
'quantity_report_date_same_year' => 0,
'total_report_date_same_year' => 0,
'quantity_report_date_year_before' => $result_day_report_year_before->quantity,
'total_report_date_year_before' => $result_day_report_year_before->total,
'quantity_date_range_same_year' => 0,
'total_date_range_same_year' => 0,
'quantity_date_range_year_before' => 0,
'total_date_range_year_before' => 0,
'step_b' => '3',
]);
}
return $item;
});
}
}
Now the result of final array ($queries) will be
This result isn't correct, the correct answer must be array with 34 object not 33 object Error at step of array push which contain 'step_b' => '3' , it return null not correct object
What's the problem ?
CodePudding user response:
I find the problem on my code
the correct code is
$collection->map(function ($item, $key) use ($result_day_report_year_before ,&$queries) {
if($item->location_id == $result_day_report_year_before->location_id && $item->variation_id == $result_day_report_year_before->variation_id)
{
$item->quantity_report_date_year_before = $result_day_report_year_before->quantity ;
$item->total_report_date_year_before = $result_day_report_year_before->total ;
$item->step_b = 2 ;
}
elseif($item->location_id != $result_day_report_year_before->location_id && $item->variation_id == $result_day_report_year_before->variation_id)
{
array_push($queries, (object)[
'business_location_name' => $result_day_report_year_before->business_location_name,
'location_id' => $result_day_report_year_before->location_id,
'product_id' => $result_day_report_year_before->product_id,
'product_name' => $result_day_report_year_before->product_name,
'variation_id' => $result_day_report_year_before->variation_id,
'quantity_report_date_same_year' => 0,
'total_report_date_same_year' => 0,
'quantity_report_date_year_before' => $result_day_report_year_before->quantity,
'total_report_date_year_before' => $result_day_report_year_before->total,
'quantity_date_range_same_year' => 0,
'total_date_range_same_year' => 0,
'quantity_date_range_year_before' => 0,
'total_date_range_year_before' => 0,
'step_b' => '3',
]);
}
});
The error was in using the array in the map function
Thanks