Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
cicer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
meskio
cicer
Commits
48e37b1d
Unverified
Commit
48e37b1d
authored
4 years ago
by
meskio
Browse files
Options
Downloads
Patches
Plain Diff
Calculate product price counting with existing stock and price
* Closes:
#20
parent
802af4f8
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
api/db/inventary.go
+18
-1
18 additions, 1 deletion
api/db/inventary.go
api/inventary_test.go
+2
-1
2 additions, 1 deletion
api/inventary_test.go
with
20 additions
and
2 deletions
api/db/inventary.go
+
18
−
1
View file @
48e37b1d
...
...
@@ -52,7 +52,11 @@ func (d *DB) AddInventary(num int, inventary *Inventary) error {
query
=
query
.
Update
(
"stock"
,
gorm
.
Expr
(
"stock + ?"
,
*
product
.
StockUpdate
))
}
if
product
.
Price
!=
nil
{
query
=
query
.
Update
(
"price"
,
*
product
.
Price
)
price
,
err
:=
d
.
inventaryNewPrice
(
product
)
if
err
!=
nil
{
return
err
}
query
=
query
.
Update
(
"price"
,
price
)
}
err
:=
query
.
Error
if
err
!=
nil
{
...
...
@@ -64,6 +68,19 @@ func (d *DB) AddInventary(num int, inventary *Inventary) error {
})
}
func
(
d
*
DB
)
inventaryNewPrice
(
product
InventaryProduct
)
(
int
,
error
)
{
if
product
.
StockUpdate
==
nil
||
*
product
.
StockUpdate
==
0
{
return
*
product
.
Price
,
nil
}
existingProduct
,
err
:=
d
.
GetProduct
(
product
.
ProductCode
)
if
err
!=
nil
{
return
0
,
err
}
price
:=
((
existingProduct
.
Price
*
existingProduct
.
Stock
)
+
(
*
product
.
Price
*
*
product
.
StockUpdate
))
/
(
existingProduct
.
Stock
+
*
product
.
StockUpdate
)
return
price
,
nil
}
func
(
d
*
DB
)
GetInventary
(
id
int
)
(
inventary
Inventary
,
err
error
)
{
err
=
d
.
db
.
Preload
(
"Products.Product"
)
.
Preload
(
clause
.
Associations
)
.
...
...
This diff is collapsed.
Click to expand it.
api/inventary_test.go
+
2
−
1
View file @
48e37b1d
...
...
@@ -154,7 +154,8 @@ func TestInventaryUpdateBoth(t *testing.T) {
if
product
.
Stock
!=
testProduct
.
Stock
+*
inventary
.
Products
[
0
]
.
StockUpdate
{
t
.
Error
(
"Wrong stock:"
,
product
.
Stock
)
}
if
product
.
Price
!=
*
inventary
.
Products
[
0
]
.
Price
{
newPrice
:=
((
price
*
updateStock
)
+
(
testProduct
.
Price
*
testProduct
.
Stock
))
/
(
updateStock
+
testProduct
.
Stock
)
if
product
.
Price
!=
newPrice
{
t
.
Error
(
"Wrong price:"
,
product
.
Price
)
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment