コード例 #1
0
    /**
     * Pinta el pareto
     */
    public function actionPareto()
    {
        $modinventario = new Alinventario();
        $model = new ParetoForm();
        // collect user input data
        if (isset($_POST['ParetoForm'])) {
            $model->attributes = $_POST['ParetoForm'];
            $almacen = $_POST['ParetoForm']['almacen'];
            $centro = $_POST['ParetoForm']['centro'];
            $tipo = $_POST['ParetoForm']['codtipo'];
            $almacen = MiFactoria::cleanInput($almacen);
            $centro = MiFactoria::cleanInput($centro);
            if ($model->validate()) {
                ini_set("set_time_limit", 0);
                if (is_null($tipo)) {
                    $valortotal = $modinventario->getStockValAlmacen($almacen);
                } else {
                    $valortotal = $modinventario->getStockValAlmacen($almacen);
                }
                $rows = Yii::app()->db->createCommand()->select('a.id, (' . $modinventario->getcadenacampos() . ')*a.punit*b.cambio as stock,
			    a.codalm,a.codart,
			    a.codcen')->from('{{alinventario}} a ,{{tipocambio}} b , {{almacenes}} c')->where("a.codalm=c.codalm and a.codcen=c.codcen and\n\t\t  c.codmon=b.codmon1 and b.codmon2=:Vmonedadefault  and a.codalm=:vcodal and (" . $modinventario->getcadenacampos() . ") > 0 ", array(":vcodal" => $almacen, ":Vmonedadefault" => yii::app()->settings->get('general', 'general_monedadef')))->order('(' . $modinventario->getcadenacampos() . ')*a.punit*b.cambio DESC')->queryAll();
                ///borramos todos los registros anteriores primero
                $comando = Yii::app()->db->createCommand(" delete from {{pareto}} ");
                // $comando=Yii::app()->db->createCommand(" delete from {{pareto}}  where
                // hinventario in (SELECT id from ".Yii::app()->params['prefijo']."alinventario where codcen='".$centro."'  and codalm ='".$almacen."' ) ");
                $comando->execute();
                $i = 1;
                //ranking
                $acumulado = 0;
                //valor del stcok acumulado
                $porcenacumulado = 0;
                //$transaction = $this->getDbConnection()->beginTransaction();
                foreach ($rows as $valor) {
                    $acumulado = $acumulado + $valor['stock'];
                    /*var_dump($valortotal[$almacen]['stock_total']);
                    		yii::app()->end();*/
                    $porcentaje = round($valor['stock'] / $valortotal[$almacen]['stock_total'], 3);
                    $porcenacumulado = round($porcenacumulado + $porcentaje, 2);
                    $comando1 = Yii::app()->db->createCommand(" INSERT INTO {{pareto}} (hinventario,ranking, clase, acumulado,porcentaje,porcentajeac,  idsesion) values( " . $valor['id'] . " , " . $i . ",'', " . $acumulado . "," . $porcentaje . "," . $porcenacumulado . "," . Yii::app()->user->getId() . "  ) ");
                    $comando1->execute();
                    //$transaction->commit();
                    $i = $i + 1;
                }
                //luego establecer los margenes de la clase
                $rangoa = $_POST['ParetoForm']['rangoa'] / 100;
                $rangob = $_POST['ParetoForm']['rangob'] / 100;
                $rangoc = $_POST['ParetoForm']['rangoc'] / 100;
                //actualñizar todas las clases A
                $comando2 = Yii::app()->db->createCommand(" UPDATE " . Yii::app()->params['prefijo'] . "pareto SET clase='A' where porcentajeac < " . $rangoa . "  ");
                $comando2->execute();
                //actualñizar todas las clases B
                $comando2 = Yii::app()->db->createCommand(" UPDATE " . Yii::app()->params['prefijo'] . "pareto SET clase='B' where porcentajeac >=" . $rangoa . " and porcentajeac < " . ($rangoa + $rangob) . " ");
                $comando2->execute();
                //actualñizar todas las clases C
                $comando2 = Yii::app()->db->createCommand(" UPDATE " . Yii::app()->params['prefijo'] . "pareto SET clase='C' where porcentajeac >=" . ($rangoa + $rangob) . " ");
                $comando2->execute();
                //$this->render('vwlistadopareto');
                //$this->render('vwpareto',array('model'=>$model));
                //$this->redirect('vwpareto',array('model'=>$model));
                $this->redirect(array('adminpareto'));
            }
            //echo "ESTOS SON LOS RANGOS  ".$rangoa."    ".$rangob."    ".$rangoc;
            //Yii::app()->end();
        }
        // display the login form
        $this->render('pareto_form', array('model' => $model));
    }