Skip to content

Commit

Permalink
Merge branch 'organize_clients'
Browse files Browse the repository at this point in the history
  • Loading branch information
jresinas committed May 22, 2017
2 parents a698039 + 73b8fc1 commit c304309
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 25 deletions.
12 changes: 11 additions & 1 deletion app/models/facturaplus_client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
class FacturaplusClient < ActiveRecord::Base

def self.get_clients(biller)
if Facturaplus::Fp::BILLER_IDS.include?(biller)
self.where(biller_id: Facturaplus::Fp::BILLER_IDS[biller]).map(&:client_name).sort
else
if Setting.plugin_redmine_facturaplus['default_clients'].present?
return Setting.plugin_redmine_facturaplus['default_clients'].split("\r\n")
else
return []
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
<% if Setting.plugin_redmine_facturaplus['bill_tracker'].present? and Setting.plugin_redmine_facturaplus['client_field'].present? and @issue.tracker_id.to_s == Setting.plugin_redmine_facturaplus['bill_tracker'] and User.current.allowed_to?(:clients_sync, @project) %>
<script>
$(document).ready(function(){
client = $("#issue_custom_field_values_<%=Setting.plugin_redmine_facturaplus['client_field']%>");
sync_link = " <a href='#' class='icon icon-reload' onclick=\'if (confirm(\"<%=l(:'facturaplus.text_confirm_sync_client_field')%>\")){ sync_client_field(client); }\'></a>";
$(client).prev().append(sync_link);
client_field = $("#issue_custom_field_values_<%=Setting.plugin_redmine_facturaplus['client_field']%>");
biller_field = $("#issue_custom_field_values_<%=Setting.plugin_redmine_facturaplus['biller_field']%>");
sync_link = " <a href='#' class='icon icon-reload' onclick=\'if (confirm(\"<%=l(:'facturaplus.text_confirm_sync_client_field')%>\")){ sync_client_field(biller_field, client_field); }\'></a>";
$(client_field).prev().append(sync_link);

$(biller_field).on('change', function(){
change_biller(biller_field, client_field);
});
change_biller(biller_field, client_field);
});
</script>

Expand Down
2 changes: 1 addition & 1 deletion app/views/settings/_facturaplus_settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
end
%>
<%= select_tag 'settings[client_field]', options %>
<%= link_to '', '#', :class => 'icon icon-reload', :onclick => "if (confirm('#{l(:'facturaplus.text_confirm_sync_client_field')}')){ sync_client_field($(#settings_client_field)); }", :title => l(:'facturaplus.label_sync_client_field') %>
<%= link_to '', '#', :class => 'icon icon-reload', :onclick => "if (confirm('#{l(:'facturaplus.text_confirm_sync_client_field')}')){ sync_client_field(null, null); }", :title => l(:'facturaplus.label_sync_client_field') %>
</p>

<script>
Expand Down
43 changes: 34 additions & 9 deletions assets/javascripts/sync.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
function sync_client_field(field){
cf = $('#settings_client_field').val();
function sync_client_field(biller_field, client_field){
client_custom_field = $('#settings_client_field').val();
biller = $(biller_field).val();
$.ajax({
url: '/sync_client_field',
data: {custom_field: cf},
data: {client_custom_field: client_custom_field, biller: biller},
success: function(result_json){
$('#content .flash').remove();
result = JSON.parse(result_json);
if (result['type'] == 'success'){
$('#content').prepend("<div class='flash notice' id='flash_notice'>"+result['message']+"</div>");
$('option:not(:first)', field).remove();
options = []
$.each(result['data'], function(key, value){
options.push($("<option></option>").attr('value', value).text(value));
})
$(field).append(options)
if (client_custom_field == undefined){
update_clients(result['data'], client_field);
}
} else {
$('#content').prepend("<div class='flash error' id='flash_error'>"+result['message']+"</div>");
}
}
});
}

function change_biller(biller_field, client_field){
biller = $(biller_field).val();
$.ajax({
url: '/get_clients',
data: {biller: biller},
success: function(result_json){
result = JSON.parse(result_json);
update_clients(result, client_field);
}
});
}


function update_clients(data, client_field){
selected = $('option:selected', client_field).text();
$('option:not(:first)', client_field).remove();
options = []
$.each(data, function(key, value){
if (value == selected){
options.push($("<option></option>").attr('value', value).text(value).prop("selected",true));
} else {
options.push($("<option></option>").attr('value', value).text(value));
}
})
$(client_field).append(options)
}
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
match '/settings/show_list_options' => 'settings#show_list_options', :via => [:get, :post]
match '/settings/show_tracker_statuses' => 'settings#show_tracker_statuses', :via => [:get, :post]

match '/sync_client_field' => 'custom_fields#sync_client_field', :via => [:get, :post]
match '/sync_client_field' => 'custom_fields#sync_client_field', :via => [:get, :post]
match '/get_clients' => 'custom_fields#get_clients', :via => [:get, :post]
18 changes: 15 additions & 3 deletions lib/facturaplus/custom_fields_controller_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,31 @@ module ClassMethods

module InstanceMethods
def sync_client_field
if Setting.plugin_redmine_facturaplus['client_field'].present? or params[:custom_field].present?
client_field_id = params[:custom_field] || Setting.plugin_redmine_facturaplus['client_field']
if Setting.plugin_redmine_facturaplus['client_field'].present? or params[:client_custom_field].present?
client_field_id = params[:client_custom_field] || Setting.plugin_redmine_facturaplus['client_field']
client_field = CustomField.find(client_field_id)

res = Facturaplus::Fp.get_clients(client_field_id)

if res[:result]
client_field.possible_values = (Setting.plugin_redmine_facturaplus['default_clients'].split("\r\n") + res[:options])
render :text => {:type => 'success', :message => l(:'facturaplus.text_sync_success'), :data => client_field.possible_values}.to_json and return if client_field.save
if params[:biller].present?
render :text => {:type => 'success', :message => l(:'facturaplus.text_sync_success'), :data => FacturaplusClient.get_clients(params[:biller])}.to_json and return if client_field.save
else
render :text => {:type => 'success', :message => l(:'facturaplus.text_sync_success'), :data => Setting.plugin_redmine_facturaplus['default_clients'].split("\r\n")}.to_json and return if client_field.save
end
end
end
render :text => {:type => 'error', :message => l(:'facturaplus.text_sync_fail')}.to_json
end

def get_clients
if params[:biller].present?
render :text => FacturaplusClient.get_clients(params[:biller]).to_json and return
else
render :text => Setting.plugin_redmine_facturaplus['default_clients'].split("\r\n").to_json and return
end
end
end
end
end
Expand Down
11 changes: 5 additions & 6 deletions lib/facturaplus/fp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
require "uri"

module Facturaplus
BILLER_IDS = {"Emergya S.C.A." => 31, "Emergya Ingeniería S.L." => 32}
SERVICE_IDS = {"Desarrollo" => "01", "Consultoría" => "02", "Licencias" => "03", "Mantenimiento" => "04", "BPO" => "05", "Subcontratación" => "06", "Otros" =>"07", "Alquiler" => "99", "No clasificado" => "08"}

class Fp
def self.requirements?
BILLER_IDS = {"Emergya S.C.A." => 31, "Emergya Ingeniería S.L." => 32}
SERVICE_IDS = {"Desarrollo" => "01", "Consultoría" => "02", "Licencias" => "03", "Mantenimiento" => "04", "BPO" => "05", "Subcontratación" => "06", "Otros" =>"07", "Alquiler" => "99", "No clasificado" => "08"}

Setting.plugin_redmine_facturaplus['bill_tracker'].present? and
Setting.plugin_redmine_facturaplus['biller_field'].present? and
Setting.plugin_redmine_facturaplus['billers'].present? and
Expand Down Expand Up @@ -140,7 +139,7 @@ def self.get_client_name(issue)

def self.get_biller_id(issue)
begin
Facturaplus::BILLER_IDS[get_biller_name(issue)]
BILLER_IDS[get_biller_name(issue)]
rescue
nil
end
Expand Down Expand Up @@ -204,7 +203,7 @@ def self.get_service_name(issue)

def self.get_service_id(issue)
begin
Facturaplus::SERVICE_IDS[get_service_name(issue)]
SERVICE_IDS[get_service_name(issue)]
rescue
nil
end
Expand Down
2 changes: 1 addition & 1 deletion lib/facturaplus/issue_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def facturaplus_bill_save

if tracker_id.to_s == Setting.plugin_redmine_facturaplus['bill_tracker'] and !Setting.plugin_redmine_facturaplus['billed_statuses'].include?(status_id.to_s) and Setting.plugin_redmine_facturaplus['billers'].include?(biller_field.value)
# Es una factura emitada por una empresa con FacturaPlus y está en estado NO facturado
biller_id = begin Facturaplus::BILLER_IDS[biller_field.value] rescue nil end
biller_id = begin Facturaplus::Fp::BILLER_IDS[biller_field.value] rescue nil end
client_id = begin FacturaplusClient.find_by(client_name: client_field.value, biller_id: biller_id).client_id rescue nil end
amount = begin amount_field.value.to_f rescue nil end
currency = begin currency_field.value rescue nil end
Expand Down

0 comments on commit c304309

Please sign in to comment.