<?php

include '../ReglasNegocio/bodega.php';
include '../ReglasNegocio/obra.php';
include '../ReglasNegocio/area.php';
include '../ReglasNegocio/merma.php';
session_start();
$bodega = new bodega();
$obra = new obra();
$area = new area();
$arr = $_SESSION["lista"];
$boton = "verifica()";
$boton2 = "verifica2()";
$merma = new merma();
$folio = $merma->GetMayor() + 1;
?>
<script LANGUAGE="JavaScript">

function verifica()
{
	if (document.miform.CodigoMM.value.length==0)
   {
		alert("Debe ingresar un Codigo de Articulo");
		document.miform.CodigoMM.focus();
      	return 0;
	}
	document.miform.submit();
}
function verifica2()
{
	document.miform.submitMerma.value="Ingresar";
            }
        }
        if ($a == 0) {
            $arr[] = array($rowcontiene["ID_MATERIAL"], $rowm["NOMBRE_MATERIAL"], $rowm["UNIDADMEDIDA_MATERIAL"]);
        }
        $_SESSION["lista1"] = $arr;
        $_SESSION["lista"] = $arr;
    }
    header("Location: paso.php?c=d");
} else {
    $arr = $_SESSION["lista1"];
    $folio = $_SESSION["folio"];
    $idobra = $_POST["obras"];
    if ($_POST["obras"] == "Ninguna") {
        $idobra = 0;
    }
    $contiene = new contiene();
    $stock = new stock();
    $fecha = time();
    $fechaactual = date("Y-m-d h:m:s", $fecha);
    $merma = new merma();
    for ($i = 0; $i < count($arr); $i++) {
        $n = "cantidad" . $i;
        $merma->Add($folio, $_POST["areas"], $_POST["Bodegas"], $arr[$i][0], $idobra, $_POST["obs"], $_POST["{$n}"], $fechaactual);
        $contiene->Update($_POST["{$n}"], $arr[$i][0], $idobra);
        if ($idobra == 0) {
            //descontar de stock si es que se saca de bodega general osea el id de obra es 0
            $stock->UpdateRC($arr[$i][0], $_POST["{$n}"]);
        }
    }
}