Saludos de nuevo, tengo un problema con el formulario de salidas en un sistema de facturación que estoy implementando, en un problema con la actualización de los submayores me han sugerido algunas cosas que creo deben trabajar bien, pero para poderlo probar tengo primero que resolver este:
El problema es que tengo dos tablas datossal (datos de las salidas o facturas) con los campos Id, fecha, Codsalida (Tipo de salida), Codcliente (A quien se le factura), CodUEB (Unidad perteneciente a la empresa que factura), CodMon (Tipo de moneda), el Id es la llave y es del tipo entero y autoincrementable; ademas tengo otra tabla detallesfact (detalles de la facturas), con los campos Id, Id_sal, Codprod (productos a facturar), cantidad (cantidad a facturar), estas tablas estan relacionadas entre uno a varios por Id de datossal con Id_sal de detallesfact, o sea si el id de datossal es 1, entonces en Id_sal de detallesfact deben aparecer tantos 1 como productos se estén facturando, en mi formulario de salidas le agregue una función agregar productos con un botón, donde a medida que selecciono el producto y le pongo la cantidad se agrega a una tabla, luego que tengo todos los productos le doy al botón create y me debe guardar en las dos tablas los datos y los detalles de la factura, pero el botón Create no trabaja, o sea, el botón agregar agrega los productos, pero el botón crear no crea nada, o sea cuando se hace clic sobre él no hace nada y no salva nada para las dos tablas.
Aquí pongo todo lo que hice en la forma:
<?php
/* @var $this DatossalController */
/* @var $model Datossal */
/* @var $form CActiveForm */
?>
<script>
$(function(){
//Add, Save, Edit and Delete functions code
$(".btnEdit").on("click", Edit);
$(".btnDelete").on("click", Delete);
$("#btnAdd").on("click", Add);
});
function Add(){
if($('#producto option:selected').text()!=''){
var cantidad= $('#incantidad').val()==''?'0':$('#incantidad').val();
$("#tblData tbody").append(
"<tr class='even'>"+
"<td>"+ $('#producto option:selected').text()+"</td>"+
"<td>"+ cantidad +"</td>"+
"<td><img src='images/update.png' class='btnEdit' title='Actualizar' style='cursor:pointer'><img src='images/delete.png' class='btnDelete' title='Eliminar' style='cursor:pointer'/></td>"+
"</tr>");
$(".btnEdit").off("click");
$(".btnDelete").off("click");
$(".btnEdit").on("click", Edit);
$(".btnDelete").on("click", Delete);
document.getElementById('incantidad').value ='';
$('#producto option:selected').remove();
}
else{
alert('No quedan productos para adicionar');
}
};
var oldCantidad;
function Edit(){
var par = $(this).parent().parent(); //tr
var tdCantidad = par.children("td:nth-child(2)");
var tdButtons = par.children("td:nth-child(3)");
oldCantidad=tdCantidad.html();
tdCantidad.html("<input type='text' id='txtCantidad' value='"+tdCantidad.html()+"'/>");
tdButtons.html("<img src='images/save.png' class='btnSave' title='Guardar' style='cursor:pointer'/><img src='images/cancel.gif' class='btnCancel' title='Cancelar' style='cursor:pointer'/>");
$(".btnEdit").off("click");
$(".btnDelete").off("click");
$(".btnSave").on("click", Save);
$(".btnCancel").on("click", Cancel);
$(".btnEdit").on("click", Edit);
$(".btnDelete").on("click", Delete);
};
function Save(){
var par = $(this).parent().parent(); //tr
var tdCantidad = par.children("td:nth-child(2)");
var tdButtons = par.children("td:nth-child(3)");
tdCantidad.html(tdCantidad.children("input[type=text]").val());
tdButtons.html("<img src='images/update.png' class='btnEdit' title='Actualizar' style='cursor:pointer'/><img src='images/delete.png' class='btnDelete' title='Eliminar' style='cursor:pointer'/>");
$(".btnEdit").off("click");
$(".btnDelete").off("click");
$(".btnEdit").on("click", Edit);
$(".btnDelete").on("click", Delete);
};
function Cancel(){
var par = $(this).parent().parent(); //tr
var tdCantidad = par.children("td:nth-child(2)");
var tdButtons = par.children("td:nth-child(3)");
tdCantidad.html(oldCantidad);
tdButtons.html("<img src='images/update.png' class='btnEdit' title='Actualizar' style='cursor:pointer'/><img src='images/delete.png' class='btnDelete' title='Eliminar' style='cursor:pointer'/>");
$(".btnEdit").off("click");
$(".btnDelete").off("click");
$(".btnEdit").on("click", Edit);
$(".btnDelete").on("click", Delete);
};
var cont=1;
function Delete(){
var par = $(this).parent().parent(); //tr
$('#producto')
.append($("<option></option>")
.attr("value",cont)
.text(par.children("td:nth-child(1)").html()));
cont++;
par.remove();
};
$('#datossal-form').live('submit',function(event) {
var tbl = $('#tblData tbody tr').map(function() {
var row = $(this);
return { producto: row.find(':nth-child(1)').text(),
cantidad: row.find(':nth-child(2)').text()
};
}).get();
$.ajax({
url: 'index.php?r=datossal/salvarFactura',
type: 'POST',
dataType: 'text',
data: {"detalle":tbl,
"fecha": $('#Datossal_fecha').val(),
"codsalida":$('#Datossal_Codsalida').val(),
"cliente":$('#Datossal_CodCliente').val(),
"provedor":$('#Datossal_CodProvedor').val(),
"codmon":$('#Datossal_CodMon').val(),
"descuento":$('#Datossal_Descuento').val(),
"dc":$('#Datossal_DC').val(),
"modelid":$('#modelid').val()
},
success: function(data) {
window.location.href='index.php?r=datossal/admin';
}
});
return false;
});
</script>
<style>
.mystyle
{
background: white;
border-collapse: collapse;
width: 100%;
border: 1px #D0E3EF solid;
}
.mystyle th, .mystyle td
{
border: 1px white solid;
padding: 0.3em;
}
.mystyle th
{
color: white;
background: #69A8CD;
text-align: center;
}
.mystyle tr.even
{
background: #F8F8F8;
}
.mystyle tr.odd
{
background: #E5F1F4;
}
.mystyle tr.selected
{
background: #BCE774;
}
.mystyle tbody tr:hover
{
background: #ECFBD4;
}
</style>
<div class="form">
<table width="86" border="1">
<tr>
<td width="18"><?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'datossal-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
));
?></td>
<td width="18"><?php echo $form->errorSummary($model); ?></td>
<td width="28"><?php echo '<input type="hidden" id="modelid" value="'.$model->Id.'"/>'; ?></td>
</tr>
<tr>
<td width="23"><div align="right"><?php echo $form->labelEx($model,'fecha'); ?></div></td>
<td width="39"><div align="left"><span class="row">
<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$model,
'attribute'=>'fecha',
'value'=>$model->fecha,
'language' => 'es',
'htmlOptions' => array('readonly'=>"readonly"),
'options'=>array(
'dateFormat'=>'yy-mm-dd',
'minDate'=>'date-30("Y-m-d")', //fecha minima
'maxDate'=> "+20Y", //fecha maxima
),
)); ?>
<?php echo $form->error($model,'fecha'); ?></span></div></td>
<tr>
<td width="23"><div align="right"><?php echo $form->labelEx($model,'Cliente'); ?></div></td>
<td width="39"><div align="left"><span class="row"><?php echo $form->dropDownList($model,'CodCliente',
CHtml::listData(clientes::model()->findAll(array('order'=>'Descripcion')), 'CodCliente', 'Descripcion')); ?><?php echo $form->error($model,'CodCliente'); ?></span></div></td>
</tr>
<tr>
<td width="23"><div align="right"><?php echo $form->labelEx($model,'Moneda'); ?></div></td>
<td width="63"><div align="left"><?php echo $form->dropDownList($model,'CodMon',
CHtml::listData(monedas::model()->findAll(array('order'=>'NMoneda')), 'CodMon', 'NMoneda')); ?><?php echo $form->error($model,'CodMon');?></div>
</tr>
<tr>
<td width="22"><div align="right"><?php echo $form->labelEx($model,'Tipo Salida'); ?></div></td>
<td width="42"><div align="left"><?php echo $form->dropDownList($model,'Codsalida',
CHtml::listData(tipoSalida::model()->findAll(array('order'=>'Descripcion')), 'Codsalida', 'Descripcion')); ?><?php echo $form->error($model,'Codsalida'); ?></div></td>
</tr>
<tr>
<td width="21"><div align="right"><?php echo $form->labelEx($model,'Entidad o UEB'); ?></div></td>
<td width="76"><div align="left"><?php echo $form->dropDownList($model,'CodUEB',
CHtml::listData(entidades::model()->findAll(array('order'=>'NEnt')), 'CodUEB', 'NEnt')); ?><?php echo $form->error($model,'CodUEB'); ?></div></td>
</tr>
</table>
<!-- -------------------------------------------- Detalles de la Factura --------------------------------------------- -->
<fieldset>
<legend>Lista de Productos</legend>
<table>
<tr>
<td>
<label>Producto</label>
<?php
$conceptos=array();
$todos=Productos::model()->findAll();
foreach ($todos as $producto) {
$esta=false;
foreach ($model->detallesfacts as $value) {
if($producto->NProductos==$value->idProducto->NProductos){
$esta=true;
break;
}
}
if(!$esta)
$conceptos[$producto->Codprod]=$producto->NProductos;
}
echo CHtml::dropDownList('producto','', $conceptos);
?>
<td>
<td>
<label>Cantidad</label>
<input type="text" id="incantidad"/></td>
</tr>
<tr>
<td><input name="button" type="button" id="btnAdd" value="Adicionar Producto" /></td>
</tr>
</table>
<table id="tblData" class="mystyle">
<thead>
<tr>
<th>Producto</th>
<th>Cantidad</th>
<th style="width:50px"></th>
</tr>
</thead>
<tbody>
<?php
foreach ($model->detallesfacts as $value) {
echo "<tr>";
echo "<td>".$value->idProducto->NProductos."</td>";
echo "<td>".$value->cantidad."</td>";
echo "<td><img src='".Yii::app()->request->baseUrl."/images/update.png' class='btnEdit' title='Actualizar' style='cursor:pointer'><img src='".Yii::app()->request->baseUrl."/images/delete.png' class='btnDelete' title='Eliminar' style='cursor:pointer'/></td>";
echo "</tr>";
}
?>
</tbody>
</table>
</fieldset>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Crear' : 'Salvar'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Cuando creo la forma tal y como la crea Yii code generator y agrego las funciones, tabla y botón para agregar los productos o detalles de las facturas, entonces quien no trabaja es el botón Adicionar Producto y cuando arreglo la forma para poner el campo fecha con un zii.widgets.jui.CJuiDatePicker y los datos de Clientes, UEB o Entidad, Moneda y Tipo de Salida con dropDownList, entonces no trabaja el botón Crear, necesito ayuda, ya que no tengo mucha experiencia en PHP y HTML, ademas soy nuevo en yii, es apenas mi segundo proyecto y el primero no tenia tantas complicaciones.