Created
August 27, 2015 14:54
-
-
Save pacoguevara/80857cbdc87846355604 to your computer and use it in GitHub Desktop.
Tipo cambio files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var TipoCambio; | |
TipoCambio = (function () { | |
'use strict'; | |
var getAll, update, add, deleteTipoCambio, responseSuccess; | |
var url = '/api/v2/tipo_cambio'; | |
getAll = function (data, success, error) { | |
$.ajax({ | |
url: url, | |
type: 'GET', | |
data: data, | |
success: success, | |
error: error | |
}); | |
}; | |
update = function (id, data, success, error){ | |
$.ajax({ | |
url: url+'/'+id+'/update', | |
type: 'POST', | |
data: data, | |
success: success, | |
error: error | |
}); | |
}; | |
add = function (data, success, error){ | |
$.ajax({ | |
url: url+'/new', | |
type: 'GET', | |
data: data, | |
success: success, | |
error: error | |
}); | |
} | |
deleteTipoCambio = function(id, data, success, error){ | |
$.ajax({ | |
url: url+'/'+id, | |
type: 'DELETE', | |
data: data, | |
success: success, | |
error: error | |
}); | |
} | |
responseSuccess = function(response) { | |
if (response.success === 'yes') { | |
console.log(response) | |
alert("Tipo de cambio guardado con exito"); | |
location.reload(); | |
} else { | |
alert("No se puede agregar más de un tipo de cambio por día"); | |
} | |
}; | |
return { | |
getAll: getAll, | |
update: update, | |
add: add, | |
deleteTipoCambio: deleteTipoCambio, | |
responseSuccess: responseSuccess | |
}; | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def self.in_day_created day | |
tipo_cambio = TipoCambio | |
.where("extract(month from created_at) = #{day.month}") | |
.where("extract(day from created_at) = #{day.day}") | |
.where("extract(year from created_at) = #{day.year}") | |
end | |
def self.get_all name, day | |
per_page = 20 | |
tipo_cambio = TipoCambio.order("created_at desc").page(1).per_page(per_page) | |
if !name.blank? | |
tipo_cambio = tipo_cambio.where(:nombre => name) | |
end | |
if !day.blank? | |
new_date = DateTime.strptime(day, '%Y-%m-%d') | |
tipo_cambio = tipo_cambio.in_day_created(new_date) | |
end | |
return tipo_cambio | |
end | |
def self.update id, precio, nombre, date | |
new_date = DateTime.strptime(date, '%Y-%m-%d') | |
tc_today = self.in_day_created(new_date).where(:nombre => nombre) | |
if tc_today.blank? | |
tipo_cambio = TipoCambio.find(id) | |
tipo_cambio.nombre = nombre | |
tipo_cambio.precio = precio | |
tipo_cambio.created_at = new_date | |
tipo_cambio.save | |
success = "yes" | |
else | |
success = "not" | |
end | |
return success | |
end | |
def self.add_new precio, nombre, date | |
new_date = DateTime.strptime(date, '%Y-%m-%d') | |
tc_today = self.in_day_created(new_date).where(:nombre => nombre) | |
if tc_today.blank? | |
tipo_cambio = TipoCambio.create(:nombre => nombre, :precio => precio, :created_at => new_date) | |
success = "yes" | |
else | |
success = "not" | |
end | |
return success | |
end | |
def self.delete_tipo_cambio id | |
TipoCambio.find(id).delete | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /controller/api/v2/ | |
module Api | |
module V2 | |
class TipoCambioController < ApplicationController | |
#before_filter :restrict_access | |
respond_to :json | |
def index | |
tipo_cambio = TipoCambio.get_all(params[:nombre], params[:created_at]) | |
hash = {"data" => tipo_cambio} | |
return respond_with hash | |
end | |
def update2 | |
id = params[:id] | |
precio = params[:precio] | |
nombre = params[:nombre] | |
date = params[:date] | |
success = TipoCambio.update(id, precio, nombre, date) | |
render json: {success: success} | |
end | |
def new | |
precio = params[:precio] | |
nombre = params[:nombre] | |
date = params[:date] | |
success = TipoCambio.add_new(precio, nombre, date) | |
hash = {"success" => success} | |
respond_with hash | |
end | |
def destroy | |
respond_with TipoCambio.delete_tipo_cambio(params[:id]) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var TipoCambioIndex; | |
TipoCambioIndex = (function(){ | |
'use strict'; | |
var init, bind, loadTable, handleEditButton, handleSaveEditButton, | |
handleSaveNewButton, handleDeleteButton; | |
init = function () { | |
bind(); | |
loadTable(); | |
}; | |
bind = function () { | |
handleEditButton(); | |
handleSaveEditButton(); | |
handleSaveNewButton(); | |
handleDeleteButton(); | |
}; | |
loadTable = function () { | |
$('#tipo_de_cambio_table').cltables({ | |
className: 'table-bordered', | |
cols: { | |
nombre: { | |
filter: true, | |
label: 'Nombre', | |
format: 'string' | |
}, | |
precio: { | |
filter: false, | |
format: 'number' | |
}, | |
created_at: { | |
filter: true, | |
label: "AAAA-MM-DD", | |
format: 'string' | |
}, | |
id: { | |
filter: false, | |
custumFormat:true, | |
format: function (column) { | |
var final_format = [], idx = 0; | |
final_format[idx++] = "<button class='btn btn-info edit_tipo_cambio' id='"+column.id+"'"; | |
final_format[idx++] = "precio = '"+column.precio+"' nombre='"+column.nombre+"'"; | |
final_format[idx++] = "created_at = '"+column.created_at+"'"; | |
final_format[idx++] = "data-toggle='modal' data-target='#tipo_cambio_modal'>Editar</button>"; | |
final_format[idx++] = "<button class='btn btn-danger tipo_cambio_delete' tc-id='"+column.id+"'>Eliminar</button>" | |
return final_format.join(""); | |
} | |
} | |
} | |
}); | |
}; | |
handleEditButton = function () { | |
$(document).on('click', '.edit_tipo_cambio', function(event) { | |
var id, nombre, precio, created_at, date; | |
nombre = $(this).attr('nombre'); | |
precio = $(this).attr('precio'); | |
id = $(this).attr('id'); | |
created_at = $(this).attr('created_at'); | |
date = new Date(created_at) | |
var day = ("0" + date.getDate()).slice(-2); | |
var month = ("0" + (date.getMonth() + 1)).slice(-2); | |
var today = date.getFullYear()+"-"+(month)+"-"+(day) ; | |
$('#tipo_cambio_precio').val(precio); | |
$('#tipo_cambio_nombre').val(nombre); | |
$('#tipo_cambio_id').val(id); | |
$('#tipo_cambio_created_at').val(today) | |
}); | |
} | |
handleSaveEditButton = function () { | |
$(document).on('click', '.save_tipo_de_cambio', function(event) { | |
var id, nombre, precio, data, success, error, date; | |
precio = $('#tipo_cambio_precio').val(); | |
nombre = $('#tipo_cambio_nombre').val(); | |
id = $('#tipo_cambio_id').val(); | |
date = $('#tipo_cambio_created_at').val(); | |
data = {precio: precio, nombre: nombre, authenticity_token: window._token, date: date} | |
success = function (data) { | |
TipoCambio.responseSuccess(data) | |
} | |
error = function () { alert("Ocurrió un error") } | |
TipoCambio.update(id, data, success, error); | |
}); | |
} | |
handleSaveNewButton = function () { | |
$(document).on('click', '.save_new_tipo_de_cambio', function(event) { | |
var nombre, precio, data, success, error, date; | |
precio = $('#new_tipo_cambio_precio').val(); | |
nombre = $('#new_tipo_cambio_nombre').val(); | |
date = $('#new_tipo_cambio_created_at').val(); | |
data = {precio: precio, nombre: nombre, authenticity_token: window._token, date: date} | |
success = function (data) { TipoCambio.responseSuccess(data) } | |
error = function() { alert("Ocurrio un error"); } | |
TipoCambio.add(data, success, error); | |
}); | |
} | |
handleDeleteButton = function () { | |
$(document).on('click', '.tipo_cambio_delete', function(event) { | |
var id, r, data, success, error; | |
r = confirm("Confirma que desea eliminar este elemento?"); | |
if (r === true) { | |
success = function(){ | |
alert("Tipo de cambio eliminado con exito"); | |
location.reload() | |
} | |
error = function(){ alert("Ocurrio un error"); } | |
data = { authenticity_token: window._token }; | |
id = $(this).attr('tc-id'); | |
TipoCambio.deleteTipoCambio(id, data, success, error); | |
} | |
}); | |
} | |
return { | |
init: init | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment