Cuando inicie este dialogo, no me di cuenta realmente del error y no supe explicar lo que necesitaba, pido mis disculpas, aquí esta como veo ahora el problema:
El problema es que me di cuenta que en la tabla submayorprod debía aparecer los productos para cada UEB o Entidades, para ello en las tablas datosent, datossal y submayorprod adicione el campo CodUEB, relacionadas con ese campo en la tabla Entidades (UEB).
Cuando le de entrada a un producto me debe actualizar la fila correspondiente a ese producto para esa UEB seleccionada, al igual que a la hora de actualizar y borrar un productos, como tengo ahora el código se lo hace al primer producto encontrado para la UEB seleccionada, o sea, si adiciono un producto de código 123.7.01.8413 para la UEB 6, me actualiza el código 123.4.99.000 para la UEB 6.
En el caso de las salidas me actualiza el código del producto seleccionado para la primera UEB localizada, o sea, si le doy salida a los productos 123.4.99.000 y 123.7.01.8413 me actualiza esos códigos en la UEB 1.
para ello envio los siguientes ficheros:
DatossentController:
public function actionCreate()
{
$model=new Datosent;
if(isset($_POST['Datosent']))
{
$model->attributes=$_POST['Datosent'];
if($model->save()){
$criteria=new CDbCriteria;
$criteria->condition='Codprod=:codigo';
$criteria->condition='CodUEB=:coden';
$criteria->params=array(':codigo'=>$model->Codprod);
$criteria->params=array(':coden'=>$model->CodUEB);
$objSubmp = Submayorprod::model()->find($criteria);
$objSubmp->SaldoAct = $objSubmp->SaldoAct + $model->Cantidad;
$objSubmp->save();
$this->redirect(array('view', 'id' => $model->Id));
}
}
$this->render('create',array(
'model'=>$model
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Datosent']))
{
$cantidad_inicial=$model->Cantidad;
$model->attributes=$_POST['Datosent'];
//$cantidadinicial = $model->Cantidad;
//SaldoAct = salcdo actua;l - cantidad incial
if($model->save()){
$criteria=new CDbCriteria;
$criteria->condition='Codprod=:codigo';
$criteria->condition='CodUEB=:coden';
$criteria->params=array(':codigo'=>$model->Codprod);
$criteria->params=array(':coden'=>$model->CodUEB);
$objSubmp = Submayorprod::model()->find($criteria);
$objSubmp->SaldoAct = $objSubmp->SaldoAct-$cantidad_inicial+$model->Cantidad;
$objSubmp->save();
$this->redirect(array('view', 'id' => $model->Id));
}
}
$this->render('update',array(
'model'=>$model
));
}
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'admin' page.
* @param integer $id the ID of the model to be deleted
*/
public function actionDelete($id)
{
$model=$this->loadModel($id);
$criteria=new CDbCriteria;
$criteria->condition='Codprod=:codigo';
$criteria->condition='CodUEB=:coden';
$criteria->params=array(':codigo'=>$model->Codprod);
$criteria->params=array(':coden'=>$model->CodUEB);
$objSubmp = Submayorprod::model()->find($criteria);
$objSubmp->SaldoAct = $objSubmp->SaldoAct-$model->Cantidad;
$objSubmp->save();
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
DatossalController:
public function actionSalvarFactura()
{
$id=$_POST['modelid'];
if($id)
$model=$this->loadModel($id);
else
$model = new Datossal;
if(isset($_POST['detalle']))
{
$model->fecha=$_POST['fecha'];
$model->Codsalida=$_POST['codsalida'];
$model->CodCliente=$_POST['cliente'];
$model->CodProvedor=$_POST['provedor'];
$model->CodMon=$_POST['codmon'];
$model->Descuento=$_POST['descuento'];
$model->DC=$_POST['dc'];
if ($model->save()) {
foreach ($model->detalleFacturas as $value) {
//Actualizar saldo de productos
$criteria=new CDbCriteria;
$criteria->condition='Codprod=:codigo';
//$criteria->condition='CodUEB=:coden'; //puesto despues
$criteria->params=array(':codigo'=>$value->Codprod);
//$criteria->params=array(':coden'=>$model->CodUEB); //puesto despues
$objSubmp = Submayorprod::model()->find($criteria);
$objSubmp->SaldoAct = $objSubmp->SaldoAct + $value->cantidad;
$objSubmp->save();
}
//Elimino todos los detalles de la Salida
Detallesfact::model()->deleteAll('Id_sal=:id', array(':id'=>$model->Id));
foreach($_POST['detalle'] as $item){
//Insertar los detalles de la salida
$producto= Productos::model()->find('NProductos=:postID', array(':postID'=>$item['producto']));
$detalle=new Detallesfact;
$detalle->Id_sal=$model->Id;
$detalle->Codprod=$producto->Codprod;
$detalle->cantidad=$item['cantidad'];
$detalle->save();
//Actualizar saldo de productos
$criteria=new CDbCriteria;
$criteria->condition='Codprod=:codigo';
//$criteria->condition='CodUEB=:coden'; //puesto despues
$criteria->params=array(':codigo'=>$detalle->Codprod);
//$criteria->params=array(':coden'=>$model->CodUEB); //puesto despues
$objSubmp = Submayorprod::model()->find($criteria);
$objSubmp->SaldoAct = $objSubmp->SaldoAct - $detalle->cantidad;
$objSubmp->save();
}
}
}
}
public function actionImprimir($id)
{
spl_autoload_unregister(array('YiiBase','autoload'));
require(Yii::app()->basePath.'/extensions/phpexcel/Classes/PHPExcel.php');
spl_autoload_register(array('YiiBase', 'autoload'));
$objPHPExcel = PHPExcel_IOFactory::load("template.xls");
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', $id);
$sql='SELECT `datossal`.`fecha`,`datossal`.`Codsalida`,`datossal`.`CodCliente`,
`datossal`.`CodProvedor`,`datossal`.`DC`,`datossal`.`CodMon`
FROM `datossal`,`productos`
ORDER BY `datossal`.`fecha`';
$command = Yii::app()->db->createCommand($sql);
$dataReader = $command->queryAll();
$indice=3;
foreach($dataReader as $row){
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('B'.$indice, $row['fecha'])
->setCellValue('C'.$indice, $row['Codsalida'])
->setCellValue('D'.$indice, $row['CodCliente'])
->setCellValue('E'.$indice, $row['CodProvedor'])
->setCellValue('F'.$indice, $row['DC'])
->setCellValue('G'.$indice, $row['CodMon']);
$indice++;
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="reporte.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}
Datosent Model
class Datosent extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Datosent the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'datosent';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('Codentrada', 'numerical', 'integerOnly'=>true),
array('Cantidad', 'numerical'),
array('Codprod', 'length', 'max'=>14),
array('CodUEB', 'length', 'max'=>12),
array('Fecha', 'safe'),
array('CodMon','numerical', 'integerOnly'=>true),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('Fecha, Codprod, Codentrada, CodMon, CodUEB,Cantidad, Id', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'codentrada' => array(self::BELONGS_TO, 'Tipoentrada', 'Codentrada'),
'codMon' => array(self::BELONGS_TO, 'Monedas', 'CodMon'),
'codprod' => array(self::BELONGS_TO, 'productos', 'Codprod'),
'codUEB' => array(self::BELONGS_TO, 'Entidades', 'CodUEB'),
'codprod' => array(self::BELONGS_TO, 'submayorprod', 'Codprod'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'Fecha' => 'Fecha',
'Codprod' => 'Codprod',
'Codentrada' => 'Codentrada',
'CodMon'=> 'CodMon',
'CodUEB'=> 'CodUEB',
'Cantidad' => 'Cantidad',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('Fecha',$this->Fecha,true);
$criteria->compare('Codprod',$this->Codprod,true);
$criteria->compare('Codentrada',$this->Codentrada);
$criteria->compare('CodMon',$this->CodMon);
$criteria->compare('CodUEB',$this->CodUEB);
$criteria->compare('Cantidad',$this->Cantidad);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
Datossal Model
class Datossal extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'datossal';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('Codsalida, CodMon', 'numerical', 'integerOnly'=>true),
array('DC', 'numerical'),
array('CodCliente', 'length', 'max'=>10),
array('CodProvedor, CodUEB', 'length', 'max'=>12),
array('Descuento', 'length', 'max'=>2),
array('Id_Fir', 'length', 'max'=>11),
array('fecha', 'safe'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('Id, fecha, Codsalida, CodCliente, CodProvedor, CodMon, Descuento, DC, CodUEB, Id_Fir', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'codsalida' => array(self::BELONGS_TO, 'Tiposalida', 'Codsalida'),
'codCliente' => array(self::BELONGS_TO, 'Clientes', 'CodCliente'),
'idFir' => array(self::BELONGS_TO, 'Firmantes', 'Id_Fir'),
'codMon' => array(self::BELONGS_TO, 'Monedas', 'CodMon'),
'codProvedor' => array(self::BELONGS_TO, 'Proveedor', 'CodProvedor'),
'codUEB' => array(self::BELONGS_TO, 'Entidades', 'CodUEB'),
'detalleFacturas' => array(self::HAS_MANY, 'Detallesfact', 'Id_sal'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'Id' => 'ID',
'fecha' => 'Fecha',
'Codsalida' => 'Codsalida',
'CodCliente' => 'Cod Cliente',
'CodProvedor' => 'Cod Provedor',
'CodMon' => 'Cod Mon',
'Descuento' => 'Descuento',
'DC' => 'Dc',
'CodUEB' => 'CodUEB',
'Id_Fir' => 'Id Fir',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('Id',$this->Id);
$criteria->compare('fecha',$this->fecha,true);
$criteria->compare('Codsalida',$this->Codsalida);
$criteria->compare('CodCliente',$this->CodCliente,true);
$criteria->compare('CodProvedor',$this->CodProvedor,true);
$criteria->compare('CodMon',$this->CodMon);
$criteria->compare('Descuento',$this->Descuento,true);
$criteria->compare('DC',$this->DC);
$criteria->compare('CodUEB',$this->CodUEB,true);
$criteria->compare('Id_Fir',$this->Id_Fir,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Datossal the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
Un saludo.
[/quote]