Explorar el Código

Added Super User

Jacqueline Maldonado hace 7 años
padre
commit
5a6fd16731

+ 26 - 7
app/controllers/users_controller.rb

@@ -9,11 +9,16 @@ class UsersController < ApplicationController
   before_action :get_filters, only: [:index, :show, :edit, :new]
 
   def index
-    @users = current_user.usertype == 'A' ? User.includes(:pointsale, :warehouse).where('status > 0').order("id desc") : User.includes(:pointsale, :warehouse).where('status > 0 and pointsale_id = ?', current_user.pointsale_id).order("id desc")
+    @users = User.includes(:pointsale, :warehouse).vigentes
+    if current_user.usertype != "SS"
+      @users = @users.where.not(usertype: "SS")
+      @users = @users.where(pointsale_id: current_user.pointsale_id) if current_user.usertype != "A"
+    end
   end
 
   def new
     @user = User.new
+    set_usertypes
   end
 
   def edit; end
@@ -36,17 +41,20 @@ class UsersController < ApplicationController
   def create
     @user = User.new(user_params)
     respond_to do |format|
-      message =
-        if @user.usertype == 'A'
-          "usuario #{@user.userid} con perfil ADMINISTRADOR creado."
-        else
-          "Usuario #{@user.userid} creado y asignado al " + (@user.pointsale.present? ? "punto de venta #{@user.pointsale.name}" : "almacén #{@user.warehouse.name}")
-        end
+      message = "Usuario #{@user.userid}"
+      if @user.usertype == 'SS'
+        message += " con perfil SUPER ADMINISTRADOR creado."
+      elsif @user.usertype == "A"
+        message += " con perfil ADMINISTRADOR creado."
+      else
+        message += " creado y asignado al " + (@user.pointsale.present? ? "punto de venta #{@user.pointsale.name}" : "almacén #{@user.warehouse.name}")
+      end
       @user.audit_comment = message
       if @user.save
         format.html { redirect_to users_path, success: message }
         format.json { render :show, status: :created, location: @user }
       else
+        set_usertypes
         format.html { render :new }
         format.json { render json: @user.errors, status: :unprocessable_entity }
       end
@@ -95,6 +103,17 @@ class UsersController < ApplicationController
     end
   end
 
+  def set_usertypes
+    @options_for_select =
+      if current_user.usertype == "SS"
+        Rails.application.config.usertypes_for_super
+      elsif current_user.usertype == "A"
+        Rails.application.config.usertypes_for_admin
+      else
+        Rails.application.config.usertypes_for_manager
+      end
+  end
+
   private
 
   # Use callbacks to share common setup or constraints between actions.

+ 23 - 0
app/helpers/users_helper.rb

@@ -1,2 +1,25 @@
 module UsersHelper
+  def usertype(user)
+    case user.usertype
+    when "SS" then
+      content_tag(:span, "Super Administrador", class: "label label-danger")
+    when "A" then
+      content_tag(:span, "Administrador", class: "label label-danger", style: "font-size:100%")
+    when "G" then
+      content_tag(:span, "Gerente", class: "label label-warning", style: "font-size:85%")
+    when "C" then
+      content_tag(:span, "Caja", class: "label label-info")
+    when "S" then
+      content_tag(:span, "Almacenista", class: "label label-info")
+    end
+  end
+
+  def user_status(user)
+    case user.status
+    when "active" then
+      content_tag(:span, "", class: "fa fa-check fa-2 font-green")
+    when "inactive" then
+      content_tag(:span, "", class: "fa fa-times fa-2 font-red")
+    end
+  end
 end

+ 9 - 5
app/models/ability.rb

@@ -32,14 +32,14 @@ class Ability
 
     user ||= User.new
 
-    if user.usertype == "A"
+    if user.usertype == "A" || user.usertype == "SS"
       # Cajas registradoras
-      can :read, [CashRegister, Purchase, PaymentMethod, ProductsReturn]
+      can :read, [CashRegister, Purchase, PaymentMethod, ProductsReturn, CashOut]
       # Categorias
-      can :manage, [Category, Customer, BillingInformation, Expensesconcept, Pointsale, Product, Supplier, Unit, Sale, PosConfig, Purchase, SpecialPrice, ProductWaste, Seller, CashOut, Transfer, Expense, User, Warehouse, Commission, Sellerscommission]
-
+      can :manage, [Category, Customer, BillingInformation, Expensesconcept, Pointsale, Product, Supplier, Unit, Sale, PosConfig, Purchase, SpecialPrice, ProductWaste, Seller, Transfer, Expense, User, Warehouse, Commission, Sellerscommission]
+      can [:opened_cash_registers, :find_cash_outs_by_date], CashOut
       cannot [:create, :delete, :liquidate_reserve], Sale
-
+      cannot :create, ProductWaste
     elsif user.usertype == "G"
       # Cajas registradoras
       can :manage, [CashRegister, Purchase, Product, PrePurchase, Seller, Sale, Expense, ProductWaste, Transfer, OpenCashRegister, CashOut, Supplier, Customer, Credit, CreditPayment, Commission, Sellerscommission, ProductsReturn, Category]
@@ -48,6 +48,8 @@ class Ability
       # Clientes
       can :cru, [Customer, BillingInformation, Pointsale, User, Warehouse, Credit, CreditPayment, Commission, Sellerscommission]
 
+      cannot :opened_cash_registers, CashOut
+
     elsif user.usertype == "C"
       # Cajas registradoras
       can :read, [Product, Pointsale, Customer, BillingInformation, Seller, SpecialPrice, Expensesconcept, Credit, CreditPayment]
@@ -56,6 +58,8 @@ class Ability
 
       can :manage, [CashRegister, PreSale, OpenCashRegister, Sale, Customer, Credit, CreditPayment, CashOut, Expense, Transfer, ProductsReturn, ProductWaste]
 
+      cannot :opened_cash_registers, CashOut
+
     elsif user.usertype == "S"
       can :read, [CashRegister, Product, Pointsale, Customer, BillingInformation, Seller, SpecialPrice, Expensesconcept]
 

+ 2 - 0
app/models/user.rb

@@ -38,6 +38,8 @@ class User < ActiveRecord::Base
 
   validates :userid, uniqueness: { message: "El usuario ya fue utilizado, favor de especificar otro." }
 
+  scope :vigentes, -> { where.not(status: 0).order(userid: :asc) }
+
   def full_name
     "#{first_name} #{last_name}"
   end

+ 22 - 36
app/views/users/_form.html.erb

@@ -1,5 +1,5 @@
 <div id="form_wizard_1">
-	<%= form_for(@user, :html => {:class=>"form-horizontal", :id=> "submit_form"}) do |f| %>
+	<%= form_for(@user, html: { class: "form-horizontal", id: "submit_form" }) do |f| %>
 		<div class="portlet-body form">
 			<% if @user.errors.any? %>
 				<div class="alert alert-danger">
@@ -49,54 +49,45 @@
 							<h3 class="block">Proporciona la información de la cuenta de usuario</h3>
 							<div id="error_explanation"></div>
 							<div class="form-group">
-								<%= f.label :usertype, "", {:class=>"col-md-3 control-label"} do %> Tipo de usuario
-									<span class="required">*</span><% end %>
+								<%= f.label :usertype, "", { class: "col-md-3 control-label" } do %> Tipo de usuario <span class="required">*</span> <% end %>
 								<div class="col-md-4">
-									<%= f.select :usertype, (current_user.usertype=="A" ? Rails.application.config.usertypes_for_admin : Rails.application.config.usertypes_for_manager), {:prompt => "Seleccione"}, { :class => 'form-control select2' } %>
+									<%= f.select :usertype, (@options_for_select), { prompt: "Seleccione" }, { class: 'form-control select2' } %>
 								</div>
 							</div>
 							<div class="form-group hidden" id="pointsale_div">
-								<%= f.label :pointsale_id, "", {:class=>"col-md-3 control-label"} do %> Punto de venta
-									<span class="required">*</span><% end %>
+								<%= f.label :pointsale_id, "", {:class=>"col-md-3 control-label"} do %> Punto de venta <span class="required">*</span> <% end %>
 								<div class="col-md-4">
-									<%= f.collection_select :pointsale_id, Pointsale.activos, :id, :name, {:prompt => "Seleccione", :selected => (current_user.usertype == 'G' ? current_user.pointsale_id : @user.pointsale_id) }, {:class => "form-control select2",
-										:disabled => (true if current_user.usertype == 'G')  } %>
-									<%= f.hidden_field :pointsale_id, :value => (current_user.usertype == 'G' ? current_user.pointsale_id : @user.pointsale_id), :id => 'pointsale_id' %>
+									<%= f.collection_select :pointsale_id, Pointsale.activos, :id, :name, { prompt: "Seleccione", selected: (current_user.usertype == 'G' ? current_user.pointsale_id : @user.pointsale_id) }, { class: "form-control select2",
+										disabled: (true if current_user.usertype == 'G') } %>
+									<%= f.hidden_field :pointsale_id, value: (current_user.usertype == 'G' ? current_user.pointsale_id : @user.pointsale_id), id: 'pointsale_id' %>
 								</div>
 							</div>
-							<% if current_user.usertype == "A" %>
+							<% if current_user.usertype == "A" || current_user.usertype == "SS" %>
 								<div class="form-group hidden" id="warehouse_div">
-									<%= f.label :warehouse_id, "", {:class=>"col-md-3 control-label"} do %> Almacen
-										<span class="required">*</span><% end %>
+									<%= f.label :warehouse_id, "", { class: "col-md-3 control-label" } do %> Almacén <span class="required">*</span> <% end %>
 									<div class="col-md-4">
-										<%= f.collection_select :warehouse_id, Warehouse.activos, :id, :name, {:prompt => "Seleccione"}, {:class => "form-control select2" } %>
+										<%= f.collection_select :warehouse_id, Warehouse.activos, :id, :name, { prompt: "Seleccione" }, { class: "form-control select2" } %>
 									</div>
 								</div>
 							<% end %>
 							<div class="form-group">
-								<%= f.label :userid, "Nombre de usuario", {:class=>"col-md-3 control-label"} do %> Usuario
-									<span class="required">*</span>
-								<% end %>
+								<%= f.label :userid, "Nombre de usuario", { class: "col-md-3 control-label" } do %> Usuario <span class="required">*</span> <% end %>
 								<div class="col-md-4">
-									<%= f.text_field :userid, {:class=>"form-control", :disabled => @user.persisted?} %>
+									<%= f.text_field :userid, { class: "form-control", disabled: @user.persisted? } %>
 									<span class="help-block"> El usuario no debe de contener espacios. </span>
 								</div>
 							</div>
 							<% unless @user.persisted? %>
 								<div class="form-group">
-									<%= f.label :password, "Contraseña", {:class=>"col-md-3 control-label"} do %> Contraseña
-										<span class="required">*</span>
-									<% end %>
+									<%= f.label :password, "Contraseña", { class: "col-md-3 control-label" } do %> Contraseña <span class="required">*</span> <% end %>
 									<div class="col-md-4">
-										<%= f.password_field :password, {:class=>"form-control"} %>
+										<%= f.password_field :password, { class: "form-control"} %>
 									</div>
 								</div>
 								<div class="form-group">
-									<%= f.label :password_confirmation, "Confirmar contraseña", {:class=>"col-md-3 control-label"} do %> Confirmar Contraseña
-										<span class="required">*</span>
-									<% end %>
+									<%= f.label :password_confirmation, "Confirmar contraseña", { class: "col-md-3 control-label" } do %> Confirmar Contraseña <span class="required">*</span> <% end %>
 									<div class="col-md-4">
-										<%= f.password_field :password_confirmation, {:class=>"form-control"} %>
+										<%= f.password_field :password_confirmation, { class: "form-control" } %>
 									</div>
 								</div>
 							<% end %>
@@ -104,28 +95,23 @@
 						<div class="tab-pane" id="tab2">
 							<h3 class="block">Proporciona los datos particulares del usuario</h3>
 							<div class="form-group">
-								<%= f.label :first_name, "Nombre", {:class=>"col-md-3 control-label"} do %> Nombre
-									<span class="required">*</span>
-								<% end %>
+								<%= f.label :first_name, "Nombre", { class: "col-md-3 control-label" } do %> Nombre <span class="required">*</span> <% end %>
 								<div class="col-md-4">
-									<%= f.text_field :first_name, {:class=>"form-control"} %>
+									<%= f.text_field :first_name, { class: "form-control" } %>
 								</div>
 							</div>
 							<div class="form-group">
-								<%= f.label :last_name, "Apellidos", {:class=>"col-md-3 control-label"} do %> Apellidos
-									<span class="required">*</span>
-								<% end %>
+								<%= f.label :last_name, "Apellidos", { class: "col-md-3 control-label" } do %> Apellidos <span class="required">*</span> <% end %>
 								<div class="col-md-4">
-									<%= f.text_field :last_name, {:class=>"form-control"} %>
+									<%= f.text_field :last_name, { class: "form-control" } %>
 								</div>
 							</div>
 							<div class="form-group">
-								<%= f.label :email, "Correo", {:class=>"col-md-3 control-label"} do %> Correo electrónico <span class="required">*</span>
-								<% end %>
+								<%= f.label :email, "Correo", { class: "col-md-3 control-label" } do %> Correo electrónico <span class="required">*</span> <% end %>
 								<div class="col-md-4">
 									<div class="input-icon">
 										<i class="fa fa-envelope"></i>
-										<%= f.text_field :email, {:class=>"form-control", :placeholder=>"ejemplo@correo.com" } %>
+										<%= f.text_field :email, { class: "form-control", placeholder: "ejemplo@correo.com" } %>
 									</div>
 								</div>
 							</div>

+ 11 - 28
app/views/users/index.html.erb

@@ -44,7 +44,7 @@
                   </div>
                   <div class="actions">
                     <% if can? :create, User %>
-                      <%= link_to new_user_path, {:class=>"btn bold green pull-right filtros"} do %> Nuevo Usuario <i class="fa fa-plus"></i>
+                      <%= link_to new_user_path, { class: "btn bold green pull-right filtros" } do %> Nuevo Usuario <i class="fa fa-plus"></i>
                       <% end %>
                     <% end %>
                   </div>
@@ -68,42 +68,25 @@
                     <tbody>
                       <% @users.each_with_index do |user, key| %>
                         <tr>
-                          <td> <%= key +1 %> </td>
-                          <td> <%= user.userid  %> </td>
-                          <td> <%= user.first_name + ' ' + user.last_name %> </td>
-                          <td>
-                            <% case user.usertype %>
-                            <% when "A" %>
-                              <span class="text-danger">Administrador</span>
-                            <% when "G" %>
-                              <span class="text-warning">Gerente</span>
-                            <% when "C" %>
-                              <span class="text-info">Caja</span>
-                            <% when "S" %>
-                              <span class="text-info">Almacenista</span>
-                            <% end %>
-                          </td>
-                          <td> <%= user.pointsale.name if user.pointsale.present?%> </td>
-                          <td> <%= user.warehouse.name if user.warehouse.present?%> </td>
-                          <td class="text-center">
-                            <% if user.active? %>
-                              <span class="label label-sm label-success"> <i class="fa fa-check"></i> Activo </span>
-                            <% elsif user.inactive? %>
-                              <span class="label label-sm label-danger"> <i class="fa fa-times"></i> Inactivo </span>
-                            <% end %>
-                          </td>
+                          <td> <%= key + 1 %> </td>
+                          <td> <%= user.userid %> </td>
+                          <td> <%= user.full_name %> </td>
+                          <td> <%= usertype(user) %></td>
+                          <td> <%= user.pointsale.name if user.pointsale.present? %> </td>
+                          <td> <%= user.warehouse.name if user.warehouse.present? %> </td>
+                          <td class="text-center"><%= user_status(user) %></td>
                           <td class="text-center">
                             <% if can? :update, User %>
-                              <%= link_to edit_user_path(user), {:class=>"btn btn-icon-only btn-primary filtros", :title=>"Editar cliente"} do %>
+                              <%= link_to edit_user_path(user), { class: "btn btn-icon-only btn-primary filtros", :title=>"Editar usuario" } do %>
                                 <i class="fa fa-edit"></i>
                               <% end %>
                             <% end %>
                             <% if user.active? %>
-                              <%= link_to user_update_status_path(user), :class=>"btn btn-icon-only default", :title=>"Desactivar usuario", data: { confirm: '¿Esta seguro de desactivar al usuario?', method: 'post'}  do %>
+                              <%= link_to user_update_status_path(user), class: "btn btn-icon-only default", title: "Desactivar usuario", data: { confirm: '¿Está seguro de desactivar al usuario?', method: 'post' } do %>
                                 <i class="fa fa-toggle-off"></i>
                               <% end %>
                             <% elsif user.inactive? %>
-                              <%= link_to user_update_status_path(user), :class=>"btn btn-icon-only green-jungle", :title=>"Activar usuario", data: { confirm: '¿Esta seguro de activar al usuario?', method: 'post'}  do %>
+                              <%= link_to user_update_status_path(user), class: "btn btn-icon-only green-jungle", title: "Activar usuario", data: { confirm: '¿Está seguro de activar al usuario?', method: 'post' } do %>
                                 <i class="fa fa-toggle-on"></i>
                               <% end %>
                             <% end %>

+ 8 - 0
config/application.rb

@@ -24,6 +24,14 @@ module Pos
     # Do not swallow errors in after_commit/after_rollback callbacks.
     config.action_view.embed_authenticity_token_in_remote_forms = true
     config.active_record.raise_in_transactional_callbacks = true
+    config.usertypes_for_super = {
+      "Super Administrador" => "SS",
+      "Administrador" => "A",
+      "Almacenista" => "S",
+      "Caja" => "C",
+      "Gerente" => "G"
+    }
+
     config.usertypes_for_admin = {
       "Administrador" => "A",
       "Almacenista" => "S",

+ 1 - 4
config/navigation.rb

@@ -41,7 +41,7 @@ SimpleNavigation.register_renderer bootstrap_breadcrumbs: SimpleNavigationn::Ren
 SimpleNavigation::Configuration.run do |navigation|
   navigation.renderer = SimpleNavigationRenderers::Bootstrap3
   navigation.items do |primary|
-    if current_user.usertype == 'A'
+    if current_user.usertype == 'A' || current_user.usertype == "SS"
       # clientes
       primary.item :customers, { icon: "fa fa-fw fa-smile-o", text: "Clientes" }, customers_path, class: 'menu-dropdown classic-menu-dropdown' do |sub_nav|
         sub_nav.dom_attributes = { class: 'dropdown-menu pull-left' }
@@ -83,7 +83,6 @@ SimpleNavigation::Configuration.run do |navigation|
         sub_nav.item :list_cash_registers, 'Lista de cajas', cash_registers_path
         sub_nav.item :list_cash_outs, 'Lista de cortes de caja', cash_outs_path
         sub_nav.item :divider_before_sellers, '#', divider: true
-        sub_nav.item :new_seller, 'Nuevo vendedor', new_seller_path
         sub_nav.item :list_sellers, 'Lista de vendedores', sellers_path
         sub_nav.item :divider_after_sellers, '#', divider: true
         sub_nav.item :new_transfer, 'Nuevo traspaso', new_transfer_path
@@ -171,12 +170,10 @@ SimpleNavigation::Configuration.run do |navigation|
       # punto de venta, caja registradora y vendedores
       primary.item :pointsales, { icon: "fa fa-fw fa-cart-plus", text: "Puntos de venta" }, pointsales_path, class: 'menu-dropdown classic-menu-dropdown' do |sub_nav|
         sub_nav.dom_attributes = { class: 'dropdown-menu pull-left' }
-        sub_nav.item :new_product, 'Nueva Caja', new_cash_register_path
         sub_nav.item :list_products, 'Lista de cajas ', cash_registers_path
         sub_nav.item :divider_after_list_products, '#', divider: true
         sub_nav.item :list_cash_outs, 'Lista de cortes de caja', cash_outs_path
         sub_nav.item :divider_after_list_cash_outs, '#', divider: true
-        sub_nav.item :new_seller, 'Nuevo vendedor', new_seller_path
         sub_nav.item :list_sellers, 'Lista de vendedores', sellers_path
         sub_nav.item :divider_after_list_sellers, '#', divider: true
         sub_nav.item :new_transfer, 'Nuevo traspaso', new_transfer_path

+ 1 - 1
db/seeds.rb

@@ -4,7 +4,7 @@
 # Examples:
 #
 	# USUARIOS DEFAULT
-	User.create(userid:'adminpos', first_name: 'Administrador', last_name: ' POS ', email: 'info@sml.mx', usertype: "A" , password: 'smlpos2016', password_confirmation: 'smlpos2016').save(:validate => false)
+	User.create(userid:'adminpos', first_name: 'Administrador', last_name: ' POS ', email: 'info@sml.mx', usertype: "SS" , password: 'smlpos2016', password_confirmation: 'smlpos2016').save(:validate => false)
 
   	## UNIDADES DE MEDIDA
 	unidades = Unit.create([{unit:"Kilo",status:1},{unit:"Gramo",status:1},{unit:"Pieza",status:1},{unit:"Litro",status:1}])