Merge branch 'develop' into ai-sci-6731-fix-edit-title-for-stock-consumption-modal

This commit is contained in:
aignatov-bio 2022-04-15 14:43:51 +02:00 committed by GitHub
commit 31f4b35186
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 244 additions and 6 deletions

View file

@ -4,8 +4,8 @@ var RepositoryStockValues = (function() {
const UNIT_SELECTOR = '#repository-stock-value-units';
function formatDecimalValue(value, decimals) {
let regexp = decimals === 0 ? /[^0-9]/g : /[^0-9.]/g;
return value.replace(regexp, '').match(new RegExp(`^\\d*(\\.\\d{0,${decimals}})?`))[0];
let regexp = decimals === 0 ? /[^-0-9]/g : /[^-0-9.]/g;
return value.replace(regexp, '').match(new RegExp(`^-?\\d*(\\.\\d{0,${decimals}})?`))[0];
}
function updateChangeAmount($element) {
@ -36,6 +36,7 @@ var RepositoryStockValues = (function() {
$('#change_amount').val(inputAmount);
$('#repository_stock_value_amount').val(newAmount);
$('.stock-final-container').toggleClass('negative', newAmount < 0);
$('.stock-final-container .value').text(
formatDecimalValue(String(newAmount), $('#stock-input-amount').data('decimals'))
);

View file

@ -32,6 +32,12 @@
@include font-small;
}
&.negative {
.value {
color: $brand-danger;
}
}
&.error {
.value {
color: $brand-danger;

View file

@ -1,4 +1,6 @@
class MyModuleRepositoryRow < ApplicationRecord
include ActionView::Helpers::NumberHelper
attr_accessor :last_modified_by
attr_accessor :comment
@ -33,6 +35,16 @@ class MyModuleRepositoryRow < ApplicationRecord
end
end
def formated_stock_consumption
if stock_consumption
number_with_precision(
stock_consumption,
precision: (repository_row.repository.repository_stock_column.metadata['decimals'].to_i || 0),
strip_insignificant_zeros: true
)
end
end
private
def nulify_stock_consumption

View file

@ -17,7 +17,7 @@
<div class="sci-input-container" data-error-text="<%= t('repository_stock_values.manage_modal.amount_error') %>">
<%= f.label :stock_consumption, t('my_modules.repository.stock_modal.amount') %>
<%= f.text_field :stock_consumption,
value: @module_repository_row.stock_consumption,
value: @module_repository_row.formated_stock_consumption,
tabindex: 1,
placeholder: t('my_modules.repository.stock_modal.consumed') ,
class: 'sci-input-field',
@ -36,7 +36,7 @@
<div class="stock-update-view">
<div class="stock-initial-container">
<span class="subtitle"><%= t('repository_stock_values.manage_modal.current_stock') %></span>
<span class="value"><%= @stock_value.amount %></span>
<span class="value"><%= @stock_value.formatted_value %></span>
<span class="units"><%= @stock_value.repository_stock_unit_item&.data %></span>
</div>
<div class="stock-arrow">

View file

@ -114,7 +114,7 @@
<div class="row">
<div class="col-sm-12">
<div class="stock-update-view">
<div class="stock-initial-container">
<div class="stock-initial-container <%= 'negative' if repository_stock_value.amount.negative? %>">
<span class="subtitle"><%= t('repository_stock_values.manage_modal.current_stock') %></span>
<span class="value"><%= repository_stock_value.formatted_value %></span>
<span class="units"><%= repository_stock_value.repository_stock_unit_item&.data %></span>
@ -122,7 +122,7 @@
<div class="stock-arrow">
<i class="fas fa-arrow-right"></i>
</div>
<div class="stock-final-container">
<div class="stock-final-container <%= 'negative' if repository_stock_value.amount.negative? %> ">
<span class="subtitle"><%= t('repository_stock_values.manage_modal.new_stock') %></span>
<span class="value"><%= repository_stock_value.formatted_value %></span>
<span class="units"><%= repository_stock_value.repository_stock_unit_item&.data %></span>

View file

@ -0,0 +1,60 @@
# frozen_string_literal: true
require 'rails_helper'
describe RepositoryStockValuesController, type: :controller do
before :all do
ApplicationSettings.instance.update(
values: ApplicationSettings.instance.values.merge({"stock_management_enabled" => true})
)
end
login_user
let!(:user) { controller.current_user }
let!(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, :admin, team: team, user: user }
let!(:repository) { create :repository, team: team, created_by: user }
let(:repository_column) do
create :repository_column, :stock_type, created_by: user, repository: repository
end
let!(:repository_row) do
create :repository_row, repository: repository,
created_by: user,
last_modified_by: user
end
let!(:repository_stock_unit_item) {create :repository_stock_unit_item, created_by: user,
last_modified_by: user,
repository_column: repository_column}
describe 'create' do
let(:params) do {
repository_stock_value: {
amount: 10,
unit_item_id: repository_stock_unit_item.id,
comment: 'test',
low_stock_threshold: ''
},
operator: 'set',
change_amount: 0,
repository_id: repository.id,
id: repository_row.id
}
end
let(:action) { post :create_or_update, params: params, format: :json }
let(:action1) { post :create_or_update, params: params, format: :json }
it 'Create stock value' do
expect { action }.to change(RepositoryLedgerRecord, :count).by(1)
end
it 'Ledger immutability' do
action
expect { action1 }
.to (change(RepositoryLedgerRecord, :count).by(1)
.and(change(RepositoryStockValue, :count).by(0)))
end
end
end

View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
FactoryBot.define do
factory :repository_ledger_record do
repository_stock_value
association :reference, factory: :repository_row
user
end
end

View file

@ -5,6 +5,16 @@ require 'rails_helper'
describe MyModuleRepositoryRow, type: :model do
let(:my_module_repository_row) { build :mm_repository_row }
let(:user) { create :user }
let(:repository_stock_value) { create :repository_stock_value }
let(:repository_row) { create :repository_row, repository_stock_value: repository_stock_value }
let(:my_module_repository_row_stock) { create :mm_repository_row,
repository_row: repository_row,
assigned_by: user,
last_modified_by: user
}
it 'is valid' do
expect(my_module_repository_row).to be_valid
end
@ -16,6 +26,8 @@ describe MyModuleRepositoryRow, type: :model do
describe 'Database table' do
it { should have_db_column :id }
it { should have_db_column :repository_row_id }
it { should have_db_column :repository_stock_unit_item_id }
it { should have_db_column :stock_consumption }
it { should have_db_column :my_module_id }
it { should have_db_column :assigned_by_id }
it { should have_db_column :created_at }
@ -24,7 +36,16 @@ describe MyModuleRepositoryRow, type: :model do
describe 'Relations' do
it { should belong_to(:my_module) }
it { should belong_to(:repository_stock_unit_item).optional }
it { should belong_to(:assigned_by).class_name('User').optional }
it { should belong_to(:repository_row) }
end
describe 'Repository ledger record' do
it 'Ledger creation on changed consumption' do
my_module_repository_row_stock.stock_consumption = 10.0
expect { my_module_repository_row_stock.save! }
.to change(RepositoryLedgerRecord, :count).by(1)
end
end
end

View file

@ -10,6 +10,7 @@ describe RepositoryCell, type: :model do
let(:repository_cell_a) { build :repository_cell, :asset_value }
let(:repository_cell_s) { build :repository_cell, :status_value }
let(:repository_cell_d_r) { build :repository_cell, :date_time_range_value }
let(:repository_cell_s_v) { build :repository_cell, :stock_value }
context 'when do not have value' do
it 'is not valid' do
@ -41,6 +42,10 @@ describe RepositoryCell, type: :model do
it 'is valid for date time range value' do
expect(repository_cell_d_r).to be_valid
end
it 'is valid for stock value' do
expect(repository_cell_s_v).to be_valid
end
end
it 'should be of class RepositoryCell' do

View file

@ -0,0 +1,35 @@
# frozen_string_literal: true
require 'rails_helper'
describe RepositoryLedgerRecord, type: :model do
let(:repository_ledger_record) { build :repository_ledger_record }
let(:repository_stock_value_new) { build :repository_stock_value }
it 'is valid' do
expect(repository_ledger_record).to be_valid
end
it 'should be of class RepositoryLedgerRecord' do
expect(subject.class).to eq RepositoryLedgerRecord
end
describe 'Database table' do
it { should have_db_column :id }
it { should have_db_column :repository_stock_value_id }
it { should have_db_column :reference_id }
it { should have_db_column :reference_type }
it { should have_db_column :amount }
it { should have_db_column :balance }
it { should have_db_column :user_id }
it { should have_db_column :comment }
it { should have_db_column :created_at }
it { should have_db_column :updated_at }
end
describe 'Associations' do
it { should belong_to(:repository_stock_value).optional }
it { should belong_to(:reference) }
it { should belong_to(:user) }
end
end

View file

@ -0,0 +1,32 @@
# frozen_string_literal: true
require 'rails_helper'
describe RepositoryStockUnitItem, type: :model do
let(:repository_stock_unit_item) { build :repository_stock_unit_item }
it 'is valid' do
expect(repository_stock_unit_item).to be_valid
end
it 'should be of class RepositoryStockUnitItem' do
expect(subject.class).to eq RepositoryStockUnitItem
end
describe 'Database table' do
it { should have_db_column :id }
it { should have_db_column :data }
it { should have_db_column :repository_column_id }
it { should have_db_column :created_by_id }
it { should have_db_column :last_modified_by_id }
it { should have_db_column :created_at }
it { should have_db_column :updated_at }
end
describe 'Associations' do
it { should belong_to(:repository_column) }
it { should belong_to(:created_by) }
it { should belong_to(:last_modified_by) }
end
end

View file

@ -0,0 +1,56 @@
# frozen_string_literal: true
require 'rails_helper'
describe RepositoryStockValue, type: :model do
let(:repository_stock_value) { build :repository_stock_value }
let(:repository) { build :repository }
let(:user) { build :user }
it 'is valid' do
expect(repository_stock_value).to be_valid
end
it 'should be of class RepositoryStockValue' do
expect(subject.class).to eq RepositoryStockValue
end
describe 'Database table' do
it { should have_db_column :id }
it { should have_db_column :amount }
it { should have_db_column :repository_stock_unit_item_id }
it { should have_db_column :type }
it { should have_db_column :last_modified_by_id }
it { should have_db_column :created_by_id }
it { should have_db_column :created_at }
it { should have_db_column :updated_at }
it { should have_db_column :low_stock_threshold }
end
describe 'Associations' do
it { is_expected.to belong_to(:repository_stock_unit_item).optional }
it { is_expected.to belong_to(:created_by).optional }
it { is_expected.to belong_to(:last_modified_by).optional }
end
describe 'Saving stock value' do
let(:repository_stock_value1) { build :repository_stock_value }
it 'Save stock value' do
expect { repository_stock_value.save }.to change(RepositoryStockValue, :count).by(1)
end
it 'Updating stock value' do
repository_stock_value.save
expect { repository_stock_value.update_data!({amount: 10, low_stock_threshold:''}, user) }
.to change(RepositoryStockValue, :count).by(0)
end
it 'Updating stock value with ledger' do
repository_stock_value.save
expect { repository_stock_value.update_stock_with_ledger!(10, repository, "") }
.to (change(RepositoryLedgerRecord, :count).by(1))
end
end
end