<?php
include $_SERVER['DOCUMENT_ROOT'] . '/classes/autoload.php';

// var_dump($_POST);

$note = str_replace('"', '', $_POST['note']);
$note = str_replace("'", '', $note);

$db->query("credit_notes", "UPDATE credit_notes SET `invoice_id` = '{$_POST['invoice_id']}', `client_banking_details` = '$note' WHERE `record_id` = '{$_POST['record_id']}'");

$db->query("credit_notes_list", "DELETE FROM credit_notes_list WHERE credit_note_id = '{$_POST['record_id']}'");
$stokc_items_arr = $_POST['descriptions'];
$index = 0;
foreach ($stokc_items_arr as $item_name) {
    $db->query("credit_notes_list", "INSERT INTO credit_notes_list (`credit_note_id`,`name`,`amount`) VALUES ('{$_POST['record_id']}','$item_name','{$_POST['amounts'][$index]}')");
    $index++;
}
if ($_POST['invoice_id'] == 1) {
    header("location:../invoices/edit_invoices.php?record_id={$_POST['invoice_id']}");

} else {
    header("location:home.php");
}