Skip to content

Commit

Permalink
Merge pull request #8 from praserx/v1.4.0
Browse files Browse the repository at this point in the history
v1.4.0: credit, cash, mail template
  • Loading branch information
praserx authored May 12, 2024
2 parents 02de810 + 23e7e5e commit e081daf
Show file tree
Hide file tree
Showing 19 changed files with 1,621 additions and 852 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v1.4.0
- Pay bill by credit (use credit first)
- Added cash arg to period initialization
- Mail template update

# v1.3.1
- Unpaid template fix

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GOBUILD=CGO_ENABLED=1 CGO_CFLAGS="-g -O2 -Wno-return-local-addr" $(GOCMD) build
GOTESTS=CGO_ENABLED=1 CGO_CFLAGS="-g -O2 -Wno-return-local-addr" $(GOCMD) test
GOCLEAN=$(GOCMD) clean

VERSION=1.3.1
VERSION=1.4.0

DIRECTORY_BIN=bin
DIRECTORY_TMP=./resources/templates
Expand Down
32 changes: 16 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ toolchain go1.21.1

require (
github.com/dundee/qrpay v0.0.4
github.com/jedib0t/go-pretty/v6 v6.4.8
github.com/praserx/mailgo v1.0.1
github.com/urfave/cli/v2 v2.25.7
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
github.com/jedib0t/go-pretty/v6 v6.5.4
github.com/praserx/mailgo v1.2.0
github.com/urfave/cli/v2 v2.27.1
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
gopkg.in/ini.v1 v1.67.0
gorm.io/driver/sqlite v1.5.4
gorm.io/gorm v1.25.4
gorm.io/driver/sqlite v1.5.5
gorm.io/gorm v1.25.7
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead // indirect
github.com/emersion/go-smtp v0.18.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 // indirect
github.com/emersion/go-smtp v0.20.2 // indirect
github.com/jbub/banking v0.8.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/nicksnyder/go-i18n/v2 v2.2.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/nicksnyder/go-i18n/v2 v2.4.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
32 changes: 32 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -11,30 +13,46 @@ github.com/dundee/qrpay v0.0.4/go.mod h1:b0H5eLIdRrwafJ9zDT1nXjW2xcA7IlECCB5APEF
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead h1:fI1Jck0vUrXT8bnphprS1EoVRe2Q5CKCX8iDlpqjQ/Y=
github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 h1:hH4PQfOndHDlpzYfLAAfl63E8Le6F2+EL/cdhlkyRJY=
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-smtp v0.18.1 h1:4DFV0jxKhq0Gqt/Br3BRHyKZy5TStk6NIMHAx6GE/LA=
github.com/emersion/go-smtp v0.18.1/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
github.com/emersion/go-smtp v0.20.2 h1:peX42Qnh5Q0q3vrAnRy43R/JwTnnv75AebxbkTL7Ia4=
github.com/emersion/go-smtp v0.20.2/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
github.com/jbub/banking v0.6.0/go.mod h1:19/mEIFSyT8JLWERPDWJe/PH0MSA9kO5h1mN065IXMA=
github.com/jbub/banking v0.8.0 h1:79kXJj1X2E9dWdWuFNkk2Pw7c6uYPFQS8ev0l+zMFxk=
github.com/jbub/banking v0.8.0/go.mod h1:ctv/bD2EGRR5PobFrJSXZ/FZXCFtUbmVv6v2qf/b/88=
github.com/jedib0t/go-pretty/v6 v6.4.8 h1:HiNzyMSEpsBaduKhmK+CwcpulEeBrTmxutz4oX/oWkg=
github.com/jedib0t/go-pretty/v6 v6.4.8/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
github.com/jedib0t/go-pretty/v6 v6.5.4 h1:gOGo0613MoqUcf0xCj+h/V3sHDaZasfv152G6/5l91s=
github.com/jedib0t/go-pretty/v6 v6.5.4/go.mod h1:5LQIxa52oJ/DlDSLv0HEkWOFMDGoWkJb9ss5KqPpJBg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/nicksnyder/go-i18n/v2 v2.2.1 h1:aOzRCdwsJuoExfZhoiXHy4bjruwCMdt5otbYojM/PaA=
github.com/nicksnyder/go-i18n/v2 v2.2.1/go.mod h1:fF2++lPHlo+/kPaj3nB0uxtPwzlPm+BlgwGX7MkeGj0=
github.com/nicksnyder/go-i18n/v2 v2.4.0 h1:3IcvPOAvnCKwNm0TB0dLDTuawWEj+ax/RERNC+diLMM=
github.com/nicksnyder/go-i18n/v2 v2.4.0/go.mod h1:nxYSZE9M0bf3Y70gPQjN9ha7XNHX7gMc814+6wVyEI4=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/praserx/mailgo v1.0.1 h1:ZHDpWjRa0kF4Q8ZMcIWpbLFDKBnGRyJb2sDxxasl6yg=
github.com/praserx/mailgo v1.0.1/go.mod h1:QgW4P/5QY4opIu1Sh3Pw5mBXYko7LYhIc7/yvfq4MYQ=
github.com/praserx/mailgo v1.2.0 h1:AN1ObJbaq3B3mHqeay2KlmW/134iKyq2PIMpK8v+SUI=
github.com/praserx/mailgo v1.2.0/go.mod h1:ypYcwK5Z2CNnTECro0EDW8riqHzbyEzLyNLnTRE0xQg=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
Expand All @@ -48,13 +66,19 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e h1:+SOyEddqYF09QP7vr7CgJ1eti3pY9Fn3LHO1M1r/0sI=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
Expand All @@ -69,6 +93,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -77,6 +103,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand All @@ -92,5 +120,9 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.4 h1:IqXwXi8M/ZlPzH/947tn5uik3aYQslP9BVveoax0nV0=
gorm.io/driver/sqlite v1.5.4/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4=
gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E=
gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE=
gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw=
gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
48 changes: 43 additions & 5 deletions pkg/cmd/gobarista/commands/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ var BillingPeriodsNew = cli.Command{
Name: "new",
Aliases: []string{"n"},
Usage: "Add new billing period",
ArgsUsage: "[from(2006-01-02) to(2006-01-02) issued(2006-01-02) total_amount amount_per_package]",
ArgsUsage: "[from(2006-01-02) to(2006-01-02) issued(2006-01-02) total_amount amount_per_package cash]",
Action: func(ctx *cli.Context) (err error) {
if err = helpers.SetupDatabase(ctx); err != nil {
return err
}

if ctx.NArg() != 5 {
return fmt.Errorf("error: too few arguments: requires (5), get (%d)", ctx.NArg())
if ctx.NArg() != 6 {
return fmt.Errorf("error: too few arguments: requires (6), get (%d)", ctx.NArg())
}

DateFrom, err := time.Parse("2006-01-02", ctx.Args().Get(0))
Expand Down Expand Up @@ -129,6 +129,11 @@ var BillingPeriodsNew = cli.Command{
return fmt.Errorf("error: cannot parse float: %v", err)
}

Cash, err := strconv.ParseFloat(ctx.Args().Get(5), 32)
if err != nil {
return fmt.Errorf("error: cannot parse float: %v", err)
}

totalMonths := 1
if int(DateTo.Sub(DateFrom).Hours()/24/30) > 0 {
totalMonths = int(DateTo.Sub(DateFrom).Hours() / 24 / 30)
Expand All @@ -141,6 +146,7 @@ var BillingPeriodsNew = cli.Command{
TotalMonths: totalMonths,
TotalAmount: float32(TotalAmount),
AmountPerPackage: float32(AmountPerPackage),
Cash: float32(Cash),
}

id, err := database.InsertPeriod(period)
Expand Down Expand Up @@ -199,7 +205,39 @@ var BillingPeriodsClose = cli.Command{

for _, bill := range bills {
amount := float32(bill.Quantity) * unitPrice
err = database.UpdateBillOnPeriodClose(bill.ID, amount)
payment := amount

user, err := database.SelectUserByID(bill.UserID)
if errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("error: user for giver bill is not found: user_id=%d", bill.UserID)
} else if err != nil {
return fmt.Errorf("error: cannot get user: user_id=%d: %v", bill.UserID, err)
}

if user.Credit != 0 {
var ballance float32
if float32(user.Credit) <= amount {
payment = amount - float32(user.Credit)
ballance = amount - payment
} else {
payment = 0
ballance = amount
}

err = database.WithdrawMoney(bill.UserID, ballance)
if err != nil {
logger.Error(fmt.Sprintf("error: cannot withdraw credit for user: bill_id=%d user_id=%d ballance=%.2f: %v", bill.ID, bill.UserID, ballance, err.Error()))
} else {
logger.Info(fmt.Sprintf("credit has been reduced for user: bill_id=%d user_id=%d reduction=%.2f", bill.ID, bill.UserID, ballance))
}

_, err = database.InsertTransaction(models.Transaction{Type: models.WITHDRAW, Amount: ballance, UserID: bill.UserID})
if err != nil {
logger.Error(fmt.Sprintf("error: cannot insert withdraw transaction for user: bill_id=%d user_id=%d ballance=%.2f: %v", bill.ID, bill.UserID, ballance, err.Error()))
}
}

err = database.UpdateBillOnPeriodClose(bill.ID, amount, payment)
if err != nil {
logger.Error(fmt.Sprintf("error: cannot update bill with bill_id=%d: %v", bill.ID, err.Error()))
} else {
Expand Down Expand Up @@ -247,7 +285,7 @@ var BillingPeriodsSummary = cli.Command{
fmt.Printf("Unit price: %.2f\n", period.UnitPrice)
fmt.Printf("Total quantity: %d\n", period.TotalQuantity)
fmt.Printf("Total amount: %.2f\n", period.TotalAmount)
fmt.Printf("Total amount (wc): %.2f (total - cash)\n", period.TotalAmount)
fmt.Printf("Total amount (wc): %.2f (total - cash)\n", period.TotalAmount-period.Cash)
fmt.Printf("Total months: %d\n", period.TotalMonths)
fmt.Printf("Cash: %.2f\n", period.Cash)
fmt.Printf("Closed: %t\n", period.Closed)
Expand Down
43 changes: 41 additions & 2 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database

import (
"fmt"
"math"

"gorm.io/driver/sqlite"
"gorm.io/gorm"
Expand Down Expand Up @@ -65,6 +66,7 @@ func RunAutoMigration() error {
return gdb.AutoMigrate(
&models.Schema{},
&models.User{},
&models.Transaction{},
&models.Period{},
&models.Bill{},
)
Expand Down Expand Up @@ -125,6 +127,22 @@ func UpdateUserName(uid uint, firstname, lastname string) error {
Updates(models.User{Firstname: firstname, Lastname: lastname}).Error
}

func DepositMoney(uid uint, amount float32) error {
var user models.User
gdb.First(&user, uid)
return gdb.
Model(&user).
Update("Credit", user.Credit+int(amount)).Error
}

func WithdrawMoney(uid uint, amount float32) error {
var user models.User
gdb.First(&user, uid)
return gdb.
Model(&user).
Update("Credit", user.Credit-int(math.Abs(float64(amount)))).Error
}

func SelectAllPeriods() ([]models.Period, error) {
var periods []models.Period
result := gdb.
Expand Down Expand Up @@ -201,12 +219,12 @@ func InsertBill(bill models.Bill) (int, error) {
return int(obj.ID), result.Error
}

func UpdateBillOnPeriodClose(bid uint, amount float32) error {
func UpdateBillOnPeriodClose(bid uint, amount, payment float32) error {
var bill models.Bill
gdb.First(&bill, bid)
return gdb.
Model(&bill).
Update("Amount", amount).Error
Updates(models.Bill{Amount: amount, Payment: payment}).Error
}

func UpdateBillOnIssued(bid uint) error {
Expand All @@ -232,3 +250,24 @@ func UpdateBillOnPaymentConfirmation(bid uint) error {
Model(&bill).
Update("PaymentConfirmation", true).Error
}

func SelectAllTransactions() ([]models.Transaction, error) {
var transactions []models.Transaction
result := gdb.
Find(&transactions)
return transactions, result.Error
}

func SelectAllTransactionsForUser(uid uint) ([]models.Transaction, error) {
var transactions []models.Transaction
result := gdb.
Where("user_id = ?", uid).
Find(&transactions)
return transactions, result.Error
}

func InsertTransaction(transaction models.Transaction) (int, error) {
obj := transaction
result := gdb.Create(&obj)
return int(obj.ID), result.Error
}
Loading

0 comments on commit e081daf

Please sign in to comment.