Advertisement
Guest User

Invoices

a guest
Mar 3rd, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.88 KB | None | 0 0
  1. <?php
  2. ob_start();
  3. require_once("plantilla.php");
  4. checkLogin();
  5.  
  6. displayForm();
  7.  
  8. function displayForm() {
  9.  
  10.     mysql_connect(DB_DSN_GRID,DB_USER_GRID ,DB_PASS_GRID);
  11.     mysql_select_db(DB_DB_GRID);
  12.  
  13.     include("../lib/inc/jqgrid_dist.php");
  14.    
  15.  
  16.     // master grid
  17.     $mastergrid = new jqgrid();
  18.        
  19.    
  20.     //ID DE LA VENTA
  21.     $col = array();
  22.     $col["title"] = "Id";
  23.     $col["name"] = "id";
  24.     $col["width"] = "4";
  25.     $col["editable"] = false;
  26.     $col["editrules"] = array("edithidden"=>true);  
  27.     $cols[] = $col;
  28.    
  29.     //ID DE L'EMPRESA
  30.     $col = array();
  31.     $col["title"] = "EmpresaId";
  32.     $col["name"] = "idempresa";
  33.     $col["width"] = "4";
  34.     $col["editable"] = true;
  35.     $col["hidden"] = false;    
  36.     $col["editoptions"]=array("defaultValue"=>$_SESSION["empresaactiva"],"readonly"=>"readonly");
  37.     $cols[] = $col;
  38.    
  39.    
  40.     //ID SEGMENTO
  41.     $col = array();
  42.     $col["title"] = "SegmentoId";
  43.     $col["name"] = "idsegmento";
  44.     $col["width"] = "12";
  45.     $col["editable"] = true;
  46.     $col["edittype"]= "select";
  47.     $str = $mastergrid->get_dropdown_values("select distinct id as k, descripcion as v from segmentos where idempresa=" . $_SESSION["empresaactiva"]);
  48.     $col["editoptions"] = array("value"=>":;".$str);  
  49.     $col["formatter"] = "select";
  50.     $cols[] = $col;
  51.  
  52.     //ID CLIENTE
  53.     $col = array();
  54.     $col["title"] = "Cliente";
  55.     $col["name"] = "idcliente";
  56.     $col["width"] = "50";
  57.     $col["editable"] = true;
  58.     $col["edittype"]= "select";
  59.     $col["editoptions"] = array("size"=>60);
  60.     $str = $mastergrid->get_dropdown_values("select distinct id as k, concat(apellidos,' ',nombre) as v from clientes order by concat(apellidos,' ',nombre)");
  61.     $col["editoptions"] = array("value"=>":;".$str);  
  62.     $col["formatter"] = "select";
  63.     $cols[] = $col;
  64.    
  65.     //FECHA
  66.     $col = array();
  67.     $col["title"] = "Fecha";
  68.     $col["name"] = "fecha";
  69.     $col["width"] = "17";
  70.     $col["editable"] = true;
  71.     $col["formatter"] = "date";
  72.     $col["formatoptions"] = array("srcformat"=>'Y-m-d',"newformat"=>'d/m/Y');
  73.     $cols[] = $col;
  74.    
  75.     //CONCEPTO
  76.     $col = array();
  77.     $col["title"] = "Concepto";
  78.     $col["name"] = "concepto";
  79.     $col["width"] = "20";
  80.     $col["editoptions"] = array("size"=>150);
  81.     $col["editable"] = true;
  82.     $cols[] = $col;
  83.    
  84.     //BASE
  85.     $col = array();
  86.     $col["title"] = "Base";
  87.     $col["name"] = "base";
  88.     $col["width"] = "14";
  89.     $col["editable"] = true;
  90.     $col["formatter"] = "number";
  91.     $col["editoptions"] = array("size"=>12);           
  92.     $col["formatoptions"] = array("thousandsSeparator" =>".","decimalSeparator"=>",","decimalPlaces"=>2);
  93.     $col["align"] = "right";
  94.     $cols[] = $col;
  95.    
  96.     //cuotaiva
  97.     $col = array();
  98.     $col["title"] = "Cuota";
  99.     $col["name"] = "cuota";
  100.     $col["width"] = "14";
  101.     $col["editable"] = true;
  102.     $col["formatter"] = "number";
  103.     $col["editoptions"] = array("size"=>12);           
  104.     $col["formatoptions"] = array("thousandsSeparator" =>".","decimalSeparator"=>",","decimalPlaces"=>2);
  105.     $col["align"] = "right";
  106.     $cols[] = $col;
  107.    
  108.    
  109.     //TOTAL
  110.     $col = array();
  111.     $col["title"] = "Total";
  112.     $col["name"] = "total";
  113.     $col["width"] = "14";
  114.     $col["editable"] = true;
  115.     $col["formatter"] = "number";
  116.     $col["editoptions"] = array("size"=>12);           
  117.     $col["formatoptions"] = array("thousandsSeparator" =>".","decimalSeparator"=>",","decimalPlaces"=>2);
  118.     $col["align"] = "right";
  119.     $cols[] = $col;
  120.    
  121.    
  122.     // file upload column
  123.     $col = array();
  124.     $col["title"] = "Fichero";
  125.     $col["name"] = "fichero1";
  126.     $col["width"] = "40";
  127.     $col["editable"] = true;
  128.     $col["edittype"] = "file"; // render as file
  129.     $col["upload_dir"] = "img_ventas"; // upload here
  130.     $col["show"] = array("list"=>false,"edit"=>true,"add"=>true); // only show in add/edit dialog
  131.     $cols[] = $col;
  132.  
  133.     // get upload folder url for display in grid -- change it as per your upload path
  134.     $upload_url = explode("/",$_SERVER["REQUEST_URI"]);
  135.     array_pop($upload_url);
  136.     $upload_url = implode("/",$upload_url)."/";
  137.  
  138.     $col = array();
  139.     $col["title"] = "Image";
  140.     $col["name"] = "imagen";
  141.     $col["width"] = "200";
  142.     $col["editable"] = true;
  143.     $col["condition"] = array('$row["fichero1"] == ""', "", "<a href='$upload_url/{fichero1}' target='_blank'><img height=100 src='$upload_url/{fichero1}'></a>");
  144.     $col["show"] = array("list"=>true,"edit"=>true,"add"=>false); // only show in listing
  145.     $col["editoptions"]["dataInit"] = "function(o){jQuery(o).parent().html(o.value);}";
  146.     $cols[] = $col;
  147.  
  148.  
  149.    
  150.     //VALIDA
  151.     $col = array();
  152.     $col["title"] = "VALIDA";
  153.     $col["name"] = "valida";
  154.     $col["width"] = "6";
  155.     $col["editable"] = true;
  156.     $col["edittype"]="checkbox";
  157.     $col["editoptions"]=array("value"=>"1:0");
  158.     $col["formatter"] = "checkbox";
  159.     if ($_SESSION["usuario"]->getValue("permisos")=='admin') {
  160.         $col["editable"] = true;
  161.     }else{
  162.         $col["editable"] = false;
  163.     }
  164.     $cols[] = $col;
  165.    
  166.     //ENVIADA
  167.     $col = array();
  168.     $col["title"] = "ENVIADA";
  169.     $col["name"] = "enviada";
  170.     $col["width"] = "6";
  171.     $col["editable"] = true;
  172.     $col["edittype"]="checkbox";
  173.     $col["editoptions"]=array("value"=>"1:0");
  174.     $col["formatter"] = "checkbox";
  175.     $col["editable"] = false;
  176.     $cols[] = $col;
  177.    
  178.  
  179.    
  180.     $masteropt["caption"] = "VENTAS";
  181.     //definim l'ID del grid de detall
  182.     $masteropt["detail_grid_id"] = "list2";
  183.     $masteropt["altRows"]=true;
  184.     $masteropt["altclass"]="myAltRowClass";
  185.     $masteropt["autowidth"]=true;
  186.     $masteropt["toolbar"] = "both";
  187.     $masteropt["add_options"] = array('width'=>'650');
  188.     $masteropt["edit_options"] = array('width'=>'650');
  189.     $masteropt["add_options"] ["success_msg"] = "Registro añadido con éxito!";
  190.     $masteropt["edit_options"] ["success_msg"] = "Registro actualizado con éxto!";
  191.     $masteropt["delete_options"] ["success_msg"] = "Registro eliminado con éxito!";
  192.     $masteropt["form"]["nav"]  = true;
  193.     $masteropt["height"] = "300";
  194.     $masteropt["sortorder"] = "desc";
  195.     $masteropt["multiselect"] = false; 
  196.    
  197.     // refresh detail grid on master edit
  198.     $masteropt["edit_options"]["afterSubmit"] = "function(){ jQuery('#list2').trigger('reloadGrid', [{current:true}]); return [true,'']; jQuery('#list1').setSelection(jQuery('#list1').jqGrid('getGridParam','selrow')); }";
  199.  
  200.     $mastergrid->set_options($masteropt);
  201.    
  202.     if ($_SESSION["usuario"]->getValue("permisos")=='admin') {
  203.         $mastergrid->set_actions(array(    
  204.                             "add"=>true, // allow/disallow add
  205.                             "edit"=>true, // allow/disallow edit
  206.                             "delete"=>true, // allow/disallow delete
  207.                             "clone"=>false, // allow/disallow clone
  208.                             "rowactions"=>true, // show/hide row wise edit/del/save option
  209.                             "autofilter" => true, // show/hide autofilter for search
  210.                             "search" => "advance", // show single/multi field search condition (e.g. simple or advance)
  211.                             "showhidecolumns" => false,
  212.                             "refresh"=>false
  213.                         )
  214.                     );
  215.         $mastergrid->select_command = "SELECT v.id, v.idempresa, v.idsegmento, v.idcliente, v.fecha, v.concepto, v.base, v.cuota,
  216.                         v.total , v.valida, v.enviada, v.fichero1, c.nombre as clientenombre
  217.                         from ventas as v left join segmentos as sg on v.idsegmento=sg.id
  218.                         left join clientes as c on v.idcliente=c.id
  219.                         where v.idempresa=" . $_SESSION["empresaactiva"];
  220.    
  221.     }else{
  222.         $mastergrid->set_actions(array(    
  223.                             "add"=>true, // allow/disallow add
  224.                             "edit"=>true, // allow/disallow edit
  225.                             "delete"=>true, // allow/disallow delete
  226.                             "clone"=>false, // allow/disallow clone
  227.                             "rowactions"=>true, // show/hide row wise edit/del/save option
  228.                             "autofilter" => true, // show/hide autofilter for search
  229.                             "search" => "advance", // show single/multi field search condition (e.g. simple or advance)
  230.                             "showhidecolumns" => false,
  231.                             "refresh"=>false
  232.                             )
  233.                     );
  234.                    
  235.         $mastergrid->select_command = "SELECT v.id, v.idempresa, v.idsegmento, v.idcliente, v.fecha, v.concepto, v.base, v.cuota,
  236.                         v.total , v.valida, v.enviada, v.fichero1, c.nombre as clientenombre
  237.                         from ventas as v left join segmentos as sg on v.idsegmento=sg.id
  238.                         left join clientes as c on v.idcliente=c.id
  239.                         where v.idempresa=" . $_SESSION["empresaactiva"] . " and v.valida=0 or v.valida is null";
  240.    
  241.     }
  242.    
  243.    
  244.     // set database table for CRUD operations
  245.     $mastergrid->table = "ventas";
  246.    
  247.    
  248.     // use events if you need custom logic for upload  
  249.     $ev["on_update"] = array("update_venta", null, true);
  250.     $ev["on_delete"] = array("delete_venta", null, true);  
  251.     $mastergrid->set_events($ev);
  252.  
  253.  
  254.     // callback for update
  255.     function update_venta($data)
  256.     {
  257.         $upload_file_path = $data["params"]["fichero1"];
  258.         $file_content = file_get_contents($upload_file_path);
  259.         // your custom upload code goes here e.g. File DB insertion
  260.     }
  261.  
  262.     function delete_venta($data)
  263.     {
  264.         $rs = mysql_fetch_assoc(mysql_query("SELECT fichero1 FROM ventas WHERE id = {$data[id]}"));
  265.         unlink(realpath($rs["fichero1"]));
  266.     }
  267.  
  268.     // pass the cooked columns to grid
  269.     $mastergrid->set_columns($cols);
  270.  
  271.  
  272.     // render grid and get html/js output
  273.     $out_master = $mastergrid->render("list1");
  274.    
  275.    
  276.     //GRID DETALL  --------------------------------------------------------------
  277.     //GRID DETALL  --------------------------------------------------------------
  278.     //GRID DETALL  --------------------------------------------------------------
  279.     //GRID DETALL  --------------------------------------------------------------
  280.     $detailgrid = new jqgrid();
  281.        
  282.    
  283.     $detailopt = array();
  284.     $detailopt["height"] = ""; // autofit height of subgrid
  285.     $detailopt["caption"] = "Detalle Venta"; // caption of grid
  286.     $detailopt["toolbar"] = "both";
  287.     $detailopt["add_options"] = array('width'=>'600');
  288.     $detailopt["edit_options"] = array('width'=>'600');    
  289.     $detailopt["add_options"]["afterSubmit"] = "function(){jQuery('#list1').trigger('reloadGrid',[{current:true}]);  return [true, ''];}";
  290.     $detailopt["edit_options"]["afterSubmit"] = "function(){jQuery('#list1').trigger('reloadGrid',[{current:true}]); return [true, ''];}";
  291.     $detailopt["delete_options"]["afterSubmit"] = "function(){jQuery('#list1').trigger('reloadGrid',[{current:true}]);  return [true, ''];}";
  292.    
  293.    
  294.     $detailopt["onAfterSave"] = "function(){ var selr = jQuery('#list1').jqGrid('getGridParam','selrow'); jQuery('#list1').trigger('reloadGrid',[{jqgrid_page:1}]); setTimeout( function(){jQuery('#list1').setSelection(selr,true);},500 ); }";
  295.     $detailgrid->set_options($detailopt);
  296.  
  297.     $detailgrid->set_actions(array(    
  298.                         "add"=>true, // allow/disallow add
  299.                         "edit"=>true, // allow/disallow edit
  300.                         "delete"=>true, // allow/disallow delete
  301.                         "rowactions"=>false, // show/hide row wise edit/del/save option
  302.                         "export"=>false, // show/hide export to excel option
  303.                         "autofilter" => true, // show/hide autofilter for search
  304.                         "search" => "advance" // show single/multi field search condition (e.g. simple or advance)                     
  305.                     )
  306.                 );
  307.  
  308.     // receive id, selected row of parent grid
  309.     $id = intval($_GET["rowid"]);
  310.        
  311.     // and use in sql for filteration
  312.     $detailgrid->select_command = "SELECT * FROM detventa WHERE idventa = $id";
  313.     // this db table will be used for add,edit,delete
  314.     $detailgrid->table = "detventa";
  315.    
  316.     //ID DE LA VENTA
  317.     $col = array();
  318.     $col["title"] = "Linea";
  319.     $col["name"] = "id";
  320.     $col["width"] = "4";
  321.     $col["editable"] = false;
  322.     $col["editrules"] = array("edithidden"=>true);  
  323.     $cols_detail[] = $col;
  324.    
  325.     //ID DE LA VENTA
  326.     $col = array();
  327.     $col["title"] = "IdVenta";
  328.     $col["name"] = "idventa";
  329.     $col["width"] = "4";
  330.     $col["editable"] = false;
  331.     $col["editrules"] = array("edithidden"=>true);  
  332.     $cols_detail[] = $col;
  333.    
  334.     //ID ARTICULO
  335.     $col = array();
  336.     $col["title"] = "Articulo";
  337.     $col["name"] = "idarticulo";
  338.     $col["width"] = "50";
  339.     $col["editable"] = true;
  340.     $col["edittype"]= "select";
  341.     $col["editoptions"] = array("size"=>60);
  342.     $str = $detailgrid->get_dropdown_values("select distinct id as k,  descripcion as v from articulos order by descripcion");
  343.     $col["editoptions"] = array("value"=>":;".$str);  
  344.     $col["formatter"] = "select";  
  345.     $cols_detail[] = $col;
  346.    
  347.    
  348.     //CONCEPTO
  349.     $col = array();
  350.     $col["title"] = "Concepto";
  351.     $col["name"] = "concepto";
  352.     $col["width"] = "4";
  353.     $col["editable"] = true;
  354.     $col["editoptions"] = array("size"=>100);
  355.     //$col["editrules"] = array("edithidden"=>true);  
  356.     $cols_detail[] = $col;
  357.        
  358.     //UNIDADES
  359.     $col = array();
  360.     $col["title"] = "Uds";
  361.     $col["name"] = "uds";
  362.     $col["width"] = "9";
  363.     $col["editable"] = true;
  364.     $col["formatter"] = "number";
  365.     $col["editoptions"] = array("size"=>12);
  366.     $col["formatoptions"] = array("thousandsSeparator" =>".","decimalSeparator"=>",","decimalPlaces"=>2);
  367.     $col["align"] = "right";
  368.     $cols_detail[] = $col;
  369.        
  370.     //PRECIO
  371.     $col = array();
  372.     $col["title"] = "Precio";
  373.     $col["name"] = "precio";
  374.     $col["width"] = "9";
  375.     $col["editable"] = true;
  376.     $col["formatter"] = "number";
  377.     $col["editoptions"] = array("size"=>12);
  378.     $col["formatoptions"] = array("thousandsSeparator" =>".","decimalSeparator"=>",","decimalPlaces"=>2);
  379.     $col["align"] = "right";
  380.     $cols_detail[] = $col;
  381.        
  382.     //TIPOIVA
  383.     $col = array();
  384.     $col["title"] = "TipoIva"; // caption of column
  385.     $col["name"] = "tipoiva"; // grid column name, must be exactly same as returned column-name from sql (tablefield or field-alias)
  386.     $col["width"] = "5";
  387.     $col["editable"] = true;
  388.     $col["formatter"] = "number";
  389.     $col["editoptions"] = array("size"=>6);
  390.     $col["formatoptions"] = array("thousandsSeparator" =>".","decimalSeparator"=>",","decimalPlaces"=>2);
  391.     $col["align"] = "right";
  392.     $cols_detail[] = $col;
  393.    
  394.     $detailgrid->set_columns($cols_detail);    
  395.    
  396.         // evento, com el tercer parametre està a TRUE : 1er) funció "add_det_venta" 2on) inserta en BBDD
  397.     $e["on_insert"] = array("add_det_venta", null, true);  
  398.     $e["on_after_insert"] = array("fnActualizaVenta",null, false);
  399.     $e["on_after_update"] = array("fnActualizaVenta",null, false);
  400.     $e["on_after_delete"] = array("fnActualizaVenta",null, false); 
  401.     $detailgrid->set_events($e);
  402.    
  403.    
  404.        
  405.     function add_det_venta(&$data)
  406.     {      
  407.         $id = intval($_GET["rowid"]);
  408.         $data["params"]["idventa"] = $id;          
  409.     }
  410.    
  411.     function fnActualizaVenta(&$data)
  412.     {
  413.         $id = intval($_GET["rowid"]);
  414.         mysql_query("UPDATE ventas set base = (select sum(totbases) from (select round(sum(uds*precio),2) as totbases from detventa where idventa='{$id}' group by tipoiva) as temp) where id='{$id}'");
  415.         mysql_query("UPDATE ventas set cuota = (select sum(totcuotas) from (select round(round(sum(uds*precio),2)*tipoiva/100,2) as totcuotas from detventa where idventa='{$id}' group by tipoiva) as temp) where id='{$id}'");
  416.         mysql_query("UPDATE ventas set total = base+cuota where id='{$id}'");          
  417.     }
  418.    
  419.  
  420.     // generate grid output, with unique grid name as 'list1'
  421.     $out_detail = $detailgrid->render("list2");
  422.    
  423.    
  424.     fnAsignarEncabezado("VENTAS"); //carga encabezado  
  425.  
  426.     fnMenuPrincipal(); 
  427.    
  428.     ?>
  429.    
  430.    
  431.     <!-- display grid here -->
  432.     <?php echo $out_master?>
  433.     <br>
  434.     <?php echo $out_detail?>
  435.     <!-- display grid here -->
  436.    
  437.     <?php
  438.      
  439.     fnCerrarPagina();  //cierra el contenido, el body y el html
  440. }
  441. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement