<?php
session_start();
include "db_connect.php";

$sr_recno = $_POST['sr_recno']; // ok
$arcodigo = $_POST['arcodigo']; //
$qtd_atual = $_POST['qtd_atual']; // ok
$qtd_anterior = $_POST['qtd_anterior'];
$vlr_atual = $_POST['vlr_atual']; // ok
$vlr_anterior = $_POST['vlr_anterior'];
$locacao = $_POST['arlcacao']; // ok
$arcodvde = $_SESSION['codigo'];
$arnomvde = $_SESSION['nome'];
$mac = $_SESSION['mac'];

//$conn->autocommit(false);

try {
    $query = "SELECT CAST( MID( arqtdest, ( artabatu - 1) * 9 + 1 , 9 ) * 1 AS DECIMAL( 9,3 ) ) AS arqtdest, CAST( MID( arvrvend, ( artabatu - 1) * 9 + 1 , 9 ) * 1 AS DECIMAL( 9,3 ) ) AS arvrvend FROM estoque WHERE sr_recno = $sr_recno FOR UPDATE";

    $res = mysqli_query($conn, $query);
    if (mysqli_num_rows($res) > 0) {
        while ($row = $res->fetch_object()) {
            $query = "UPDATE estoque SET ";
            if (($row->arqtdest == $qtd_anterior) && ($qtd_atual != "")) {
                $query .= "data_ajuste = current_date, qtd_ajuste = $qtd_atual, arqtd_estok = $qtd_atual, arqtdest = IF(arqtdest IS NULL,
                CONCAT(LEFT(RPAD('', 108, '0'), (artabatu - 1) * 9), LPAD(CAST($qtd_atual AS DECIMAL(9, 3)), 9, '0'), MID(RPAD('', 108, '0'), (artabatu * 9) + 1)),
                CONCAT(LEFT(RPAD(REPLACE(arqtdest, ' ', '0'), 108, '0'), (artabatu - 1) * 9), LPAD(CAST($qtd_atual AS DECIMAL(9, 3)), 9, '0'), MID(RPAD(REPLACE(arqtdest, ' ', '0'), 108, '0'), (artabatu * 9) + 1))), ";
            }
            if (($row->arvrvend == $vlr_anterior) && ($vlr_atual != "")) {
                $query .= "arvrvend = IF(arvrvend IS NULL, CONCAT(LEFT(RPAD('', 192, '0'), (artabatu - 1) * 16), LPAD(CAST($vlr_atual AS DECIMAL(16, 2)), 16, '0'), MID(RPAD('', 192, '0'), (artabatu * 16) + 1)), CONCAT(LEFT(RPAD(REPLACE(arvrvend, ' ', '0'), 192, '0'), (artabatu - 1) * 16), LPAD(CAST($vlr_atual AS DECIMAL(16, 2)), 16, '0'), MID(RPAD(REPLACE(arvrvend, ' ', '0'), 192, '0'), (artabatu * 16) + 1))), 
                arvr_venda = $vlr_atual";
            }
        
                    
                $query .= "arlcacao = '$locacao' WHERE sr_recno = $sr_recno";
                $stmt = mysqli_prepare($conn, $query);
                mysqli_stmt_execute($stmt);
//                $conn->commit();
                if (mysqli_affected_rows($conn) > 0) {

                    $query = "INSERT INTO estoque_ajuste (arcodigo, saldo_anterior, saldo_atual, usuario, mac_mobile, sr_deleted) VALUE 
                    ('$arcodigo', $qtd_anterior, $qtd_atual, '$arcodvde', '$mac', ' ')";

                    $stmt = mysqli_prepare($conn, $query);
                    mysqli_stmt_execute($stmt);
//                    $conn->commit();
                    $error = mysqli_error($conn);
                    $_SESSION['dberror'] = $error;
                    $_SESSION['error'] = false;
                    $_SESSION['msg'] = "Produto $arcodigo alterado com sucesso!";
//                    $conn->autocommit(true);
                    header('Location: produtos.php');
                    exit();
                } else {
                    $error = mysqli_error($conn);
//                    print_r($error);
                    $_SESSION['error'] = true;
                    $_SESSION['msg'] = "Impossível alterar o produto $arcodigo!<br>$error";
//                    $conn->autocommit(true);
                    header('Location: produtos.php');
                    exit();
                }
            } else {
                $_SESSION['error'] = true;
                $_SESSION['msg'] = "Produto com estoque anterior alterado";
//                $conn->autocommit(true);
                header('Location: produtos.php');
                exit();
            }
        }
    }
 
} catch (Exception $e) {
//    $conn->rollback();
    $_SESSION['error'] =  true;
    $_SESSION['msg'] = $e;
    header('Location: produtos.php');
    //exit();
//    print_r($e);
}
