Skip to content

Commit

Permalink
Refactor Stock Handling (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen committed May 26, 2022
1 parent 365c04e commit 95ddc3b
Show file tree
Hide file tree
Showing 34 changed files with 464 additions and 410 deletions.
18 changes: 4 additions & 14 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,17 @@ import "core-js/features/url"
import { Socket } from 'phoenix';
import 'phoenix_html';
import {LiveSocket} from 'phoenix_live_view';
import SelectContent from './select-content.hook';
import Orientation from './orientation.hook';

let csrfToken = document
.querySelector("meta[name='csrf-token']")
.getAttribute('content');
let liveSocket = new LiveSocket('/live', Socket, {
params: { _csrf_token: csrfToken },
hooks: {
SelectContent: {
mounted() {
if (!this.el.hidden) this.focus();
this.hiddenBefore = this.el.hidden;
},
updated() {
if (this.hiddenBefore && !this.el.hidden) this.focus();
this.hiddenBefore = this.el.hidden;
},
focus() {
this.el.focus();
this.el.select();
}
}
SelectContent,
Orientation,
}
});
liveSocket.connect();
24 changes: 24 additions & 0 deletions assets/js/orientation.hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function getOrientation() {
return window.innerHeight > window.innerWidth ? 'portrait' : 'landscape';
}

export default {
mounted() {
this.lastOrientation = getOrientation();

this.listener = () => {
const newOrientation = getOrientation();
if(this.lastOrientation === newOrientation) return;

this.lastOrientation = newOrientation;
this.pushEvent('orientationchange', {orientation: this.lastOrientation});
}

this.pushEvent('orientationchange', {orientation: this.lastOrientation});

window.addEventListener('resize', this.listener);
},
destroyed() {
window.removeEventListener('resize', this.listener);
}
};
14 changes: 14 additions & 0 deletions assets/js/select-content.hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
mounted() {
if (!this.el.hidden) this.focus();
this.hiddenBefore = this.el.hidden;
},
updated() {
if (this.hiddenBefore && !this.el.hidden) this.focus();
this.hiddenBefore = this.el.hidden;
},
focus() {
this.el.focus();
this.el.select();
},
};
78 changes: 0 additions & 78 deletions assets/scss/base/_table.scss
Original file line number Diff line number Diff line change
@@ -1,81 +1,3 @@
table {
margin-top: 2rem;
border-spacing: 1px;
border-collapse: collapse;
width: 100%;

tr {
&:hover {
background-color: $color-secondary-dark;
}

&.important {
background-color: $color-red;
color: $color-cream;
a,
a:visited {
color: $color-cream;
}

td {
border-color: $color-secondary;
}

&:hover {
background-color: darken($color-red, 10%);
}
}
&.warning {
background-color: $color-orange;
color: $color-black;
a {
color: $color-black;
}
td {
border-color: $color-secondary;
}

&:hover {
background-color: darken($color-orange, 10%);
}
}

td,
th {
border-bottom: 1px solid rgba($color-primary, 0.5);
padding: 0.5rem;
text-align: left;

&.integer {
text-align: right;
}
}

th {
text-transform: uppercase;
vertical-align: bottom;
line-height: 1.4;
font-size: 1.4rem;
user-select: none;

&.sort {
cursor: pointer;

&.sort_current {
&.sort_asc::after {
content: '';
color: $color-primary-light;
}
&.sort_desc::after {
content: '';
color: $color-primary-light;
}
}
}
}
}
}

.table-scroll-container {
width: 100%;
overflow-x: auto;
Expand Down
1 change: 1 addition & 0 deletions assets/scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
@import 'base/select';
@import 'base/utils';
@import 'pages/location';
@import 'pages/overview';
106 changes: 106 additions & 0 deletions assets/scss/pages/overview.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
.overview-table {
margin-top: 2rem;
border-spacing: 1px;
border-collapse: collapse;
min-width: 100%;
overflow: hidden;
position: relative;
box-sizing: border-box;

tr {
&:nth-child(2n) {
background-color: rgba($color-secondary-dark, .4);
}

td,
th {
vertical-align: middle;
padding: 5px;

&:nth-child(2n) {
background-color: rgba($color-secondary-dark, .4);
}

&.important {
background-color: $color-red;
color: $color-cream;
a,
a:visited {
color: $color-cream;
}

td {
border-color: $color-secondary;
}

&:hover {
background-color: darken($color-red, 10%);
}
}
&.warning {
background-color: $color-orange;
color: $color-black;
a {
color: $color-black;
}
td {
border-color: $color-secondary;
}

&:hover {
background-color: darken($color-orange, 10%);
}
}
}

&:hover {
background-color: $color-secondary-dark;
}

td {
text-align: center;

&.item-group-spacer {
font-weight: bold;

.landscape & {
border-left: 1px solid $color-secondary-dark;
}

.portrait & {
border-top: 1px solid $color-secondary-dark;
}
}
}
th {
text-align: left;
font-weight: normal;

position: sticky;
top: 0;
left: 0;

&:first-child {
min-width: 180px;
}

&.item-group {
font-weight: bold;

.landscape & {
border-left: 1px solid $color-secondary-dark;
}

.portrait & {
border-top: 1px solid $color-secondary-dark;
}
}
}

&:first-child th {
text-align: left;
writing-mode: vertical-rl;
transform: rotate(-180deg);
}
}
}
2 changes: 0 additions & 2 deletions lib/athena.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ defmodule Athena do
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
@timestamps_opts type: :utc_datetime_usec

@type association(type) :: Ecto.Association.NotLoaded.t() | type
end
end

Expand Down
109 changes: 11 additions & 98 deletions lib/athena/inventory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -743,107 +743,20 @@ defmodule Athena.Inventory do

@spec stock_query :: Ecto.Queryable.t()
def stock_query do
supply_query =
from(m in Movement,
select: %{
item_id: m.item_id,
amount: sum(m.amount),
location_id: m.destination_location_id
},
where: is_nil(m.source_location_id),
group_by: m.item_id,
group_by: m.destination_location_id
)

consumption_query =
from(m in Movement,
select: %{
item_id: m.item_id,
amount: sum(m.amount),
location_id: m.source_location_id
},
where: is_nil(m.destination_location_id),
group_by: m.item_id,
group_by: m.source_location_id
)

movement_out_query =
from(m in Movement,
select: %{
item_id: m.item_id,
amount: sum(m.amount),
location_id: m.source_location_id
},
where: not is_nil(m.destination_location_id),
group_by: m.item_id,
group_by: m.source_location_id
)

movement_in_query =
from(m in Movement,
select: %{
item_id: m.item_id,
amount: sum(m.amount),
location_id: m.destination_location_id
},
where: not is_nil(m.source_location_id),
group_by: m.item_id,
group_by: m.destination_location_id
)

from(e in Event,
join: l in assoc(e, :locations),
from(
event in Event,
join: stock_entry in assoc(event, :stock_entries),
as: :stock_entry,
join: l in assoc(stock_entry, :location),
as: :location,
join: ig in assoc(e, :item_groups),
join: ig in assoc(stock_entry, :item_group),
as: :item_group,
join: i in assoc(ig, :items),
join: i in assoc(stock_entry, :item),
as: :item,
left_join: m_supply in subquery(supply_query),
on: m_supply.item_id == i.id and m_supply.location_id == l.id,
left_join: m_consumption in subquery(consumption_query),
on: m_consumption.item_id == i.id and m_consumption.location_id == l.id,
left_join: m_out in subquery(movement_out_query),
on: m_out.item_id == i.id and m_out.location_id == l.id,
left_join: m_in in subquery(movement_in_query),
on: m_in.item_id == i.id and m_in.location_id == l.id,
having:
count(m_supply.item_id) != 0 or count(m_consumption.item_id) != 0 or
count(m_in.item_id) != 0 or count(m_out.item_id) != 0,
group_by: l.id,
group_by: ig.id,
group_by: i.id,
select: %{
location: l,
item_group: ig,
item: i,
supply: fragment("COALESCE(?, 0)", max(m_supply.amount)),
consumption: fragment("COALESCE(?, 0)", max(m_consumption.amount)),
movement_out: fragment("COALESCE(?, 0)", max(m_out.amount)),
movement_in: fragment("COALESCE(?, 0)", max(m_in.amount)),
stock:
fragment("COALESCE(?, 0)", max(m_supply.amount)) -
fragment("COALESCE(?, 0)", max(m_consumption.amount)) +
fragment("COALESCE(?, 0)", max(m_in.amount)) -
fragment("COALESCE(?, 0)", max(m_out.amount)),
percentage:
fragment(
"""
CASE
WHEN ? = ? THEN ?
ELSE ?
END
""",
fragment("COALESCE(?, 0)", max(m_in.amount)) +
fragment("COALESCE(?, 0)", max(m_supply.amount)),
0,
0.0,
1 /
(fragment("COALESCE(?, 0)", max(m_in.amount)) +
fragment("COALESCE(?, 0)", max(m_supply.amount))) *
(fragment("COALESCE(?, 0)", max(m_out.amount)) +
fragment("COALESCE(?, 0)", max(m_consumption.amount)))
)
},
where:
stock_entry.supply > 0 or stock_entry.consumption > 0 or stock_entry.movement_in > 0 or
stock_entry.movement_out > 0,
select: stock_entry,
order_by: l.name,
order_by: ig.name,
order_by: i.name
Expand Down
Loading

0 comments on commit 95ddc3b

Please sign in to comment.