$grn_no = trim(filter_input(INPUT_POST, 'grn_no', FILTER_SANITIZE_STRING));
$doc_type = trim(filter_input(INPUT_POST, 'doc_type', FILTER_SANITIZE_STRING));
$req_slip = trim(filter_input(INPUT_POST, 'req_slip', FILTER_SANITIZE_STRING));
$req_to = trim(filter_input(INPUT_POST, 'req_to', FILTER_SANITIZE_STRING));
$iss_for = trim(filter_input(INPUT_POST, 'iss_for', FILTER_SANITIZE_STRING));
$po_amd_no_pr = filter_input(INPUT_POST, 'po_amd_no_pr', FILTER_VALIDATE_INT);

if ($doc_type === "PO") {

$stmt = $conn->prepare("WITH RECURSIVE parts_tree AS (
SELECT
pm.part_code,
pm.mn_code,
0 AS level,
pm.part_code AS root_parent
FROM part_mst pm
WHERE pm.mn_code IS NULL
AND pm.part_code IN (
SELECT item_cd FROM pend_grn_for_iss
WHERE grn_no = ? AND unit_cd = ?
)

UNION ALL
SELECT
pm.part_code,
pm.mn_code,
pt.level + 1,
pt.root_parent
FROM part_mst pm
INNER JOIN parts_tree pt ON pm.mn_code = pt.part_code
)

SELECT
pm.mn_tp,
ml.iss_uom,
pm.mn_code,
pm.part_group,
pm.part_sub_group,
pm.description,
pt.root_parent AS parent_code,
pgi.item_cd,
pgi.grn_no,
pgi.po_no,
pgi.po_amd_no,
pgi.batch_no,
pgi.batch_dt,
pgi.proc_cd,
pgi.proc_seq,
pgi.qty,
pgi.unit_cd,
pt.level AS sort_order,
(select ifnull(sum(cs.qty),0) from lot_stock cs where pgi.item_cd = cs.item_cd AND pgi.proc_cd = cs.proc_cd AND pgi.batch_no=cs.lot_no and pgi.batch_dt=cs.lot_dt and cs.loc_cd=?) as stock,
(SELECT pi.part_code FROM proc_input pi WHERE pgi.item_cd = pi.part_input_code LIMIT 1) as product_code
FROM parts_tree pt
INNER JOIN pend_grn_for_iss pgi ON pgi.item_cd = pt.part_code
INNER JOIN part_mst pm ON pm.part_code = pgi.item_cd
INNER JOIN master_list ml on ml.code = pgi.item_cd
WHERE pgi.grn_no = ?
AND pgi.unit_cd = ?
AND pgi.po_no = ?
AND pgi.po_amd_no = ?
ORDER BY pt.root_parent, pgi.item_cd, pt.level;");

$stmt->bind_param("sississ", $grn_no, $unit_cd, $req_to, $grn_no, $unit_cd, $req_slip, $po_amd_no_pr);
$stmt->execute();
$res = $stmt->get_result();

// when insert data in php generate bmr no like if part_grp ort and item type main the its main also child has bmr1,else if grp not ortho and main not generate bmr,generate its child bmr like bmr2,if same item have 2 lot no like a item not ortho ,b1,b2 is btatch now henerate bmr2.1,bmr2.2,after that the next item is 3 in php
$stmt->close();
if ($res->num_rows > 0) {
$json['status'] = 'success';
$mn_cd = '';
$itemArr = [];
while ($row = $res->fetch_assoc()) {

if ($row['part_group'] == 'ORTH') {
if (empty($row['mn_code'])) {
$mn_cd = $row['item_cd'];
}

$json['items'][] = array(
"grn_head_no" => $row['grn_no'],
"item_cd" => $row['item_cd'],
"description" => $row['description'],
"part_group" => $row['part_group'],
"mn_code" => $mn_cd,
"mn_tp" => $row['mn_tp'],
"qty" => $row['qty'],
"proc_cd" => $row['proc_cd'],
"proc_name" => $master->get_process_master($row['proc_cd'], $conn)['name'] ?? 'Finish',
"part_sub_group" => $row['part_sub_group'],
"proc_seq" => $row['proc_seq'],
"uom" => $row['iss_uom'],
"batch_no" => $row['batch_no'],
"batch_dt" => $row['batch_dt'] ?? '',
"stock" => $row['stock'],
"product_code" => $row['product_code'],
);
}
}



}



echo json_encode($json);
}

*DO NOT CHANGE THE QUERY*
this is my code i am trying to do is if the product group is ortho then it will populate as it is otherwise it do as given bellow

note there are main then child of the main items then main then child of the main....

step 1 :- Find the minimum value of that main's child item
STEP 2 :- then get main qty will remains same as before but child qty will be change according to main if its more than minimum then minimum qty will be there 

STEP 3 :- Item should not repeat for that main item even if it has multiple batch

Step 4 :- Find the no off of the child in proc_input table then divide it with the minum qty