In my code, the result of total_tax_1 is 0 even tho there are any value stored in tax1_amount.
public function store(Request $request)
{
$sum_sub_total = (@count($request->warehouse_details)>0 ? array_sum(array_column($request->warehouse_details,'dpp_subtotal')) : 0);
$total_taxes = 0;
$totalTax1 = array_sum(array_column($request->warehouse_details,'tax1_amount'));
$totalTax2 = 0;
//...
if($request->is_use_tax == 1){
$tax = Tax::find($request->id_tax);
$totalTax2 = ($sum_sub_total * $tax->percentage);
}
$total_taxes = $totalTax1 + $totalTax2;
$data_wh = [
//...
'total_tax' => $total_taxes,
'total_amount' => $sub_total_after_disc + $total_taxes,
//...
'total_tax_2' => $totalTax2,
'total_tax_1' => $totalTax1,
];
$warehouse_details = [];
//create list Wh Detail
$i = 0;
foreach($request->warehouse_details as $wh_detail){
//...
$tax_per_row = 0;
if($sub_total_after_disc != 0){
$tax_per_row = (($wh_detail['subtotal'] - $disc_item) / $sub_total_after_disc) * $total_taxes;
}
$pph22Amount = 0;
$pph22Rate = 0;
//..
$pph22Amount = MyHelper::processTax($amount, $wh_detail['id_pph22'], TransType::$TO_PURCHASE_INVOICE);
//...
$data['sub_total_tax'] = $tax_per_row;
//...
$data['tax1_amount'] = $tax1_amount;
array_push($warehouse, $data);
}
//create Wh
$warehouse = Warehouse::create($data_wh);
if(!$warehouse){
DB::rollBack();
return response()->json(false);
}
//create Wh Detail
$create_wh_detail = $warehouse->warehouse_details()->createMany($warehouse_details);
if(!$warehouse_details_check){
DB::rollBack();
return response()->json(false);
}
//...
}
I want to get the value from tax1_amount and save it into the variable $totalTax1 so I can use it when creating warehouses and warehouse_details. Is there any solution other than moving the create flow? Because I have my own reasons for creating warehouses first