complete structure revamp

This commit is contained in:
2025-10-07 14:46:31 +03:00
parent 0d800e014e
commit 1081f2cca6
11 changed files with 1009 additions and 377 deletions

17
api/client.go Normal file
View File

@@ -0,0 +1,17 @@
package api
import "resty.dev/v3"
type Client struct {
token string
resty *resty.Client
apiBaseURL string
}
func NewClient(token string) *Client {
return &Client{
token: token,
resty: resty.New(),
apiBaseURL: "https://kasa.vchasno.ua/api/v3",
}
}

16
api/constants.go Normal file
View File

@@ -0,0 +1,16 @@
package api
const (
TaskOpenShift = 0
TaskSell = 1
TaskZReport = 11
)
const (
PayTypeCash = 0
PayTypeCard = 2
)
const (
PaySystemParkingPos = "parking_pos"
)

103
api/fiscal.go Normal file
View File

@@ -0,0 +1,103 @@
package api
import (
"context"
"encoding/json"
"fmt"
"io"
)
func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, response interface{}) error {
resp, err := c.resty.R().
SetContext(ctx).
SetHeader("Authorization", c.token).
SetBody(request).
Post(c.apiBaseURL + "/fiscal/execute")
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
if resp.IsError() {
return fmt.Errorf("api error: %v", resp.Error())
}
if resp.StatusCode() != 200 {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode())
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response: %w", err)
}
if err := json.Unmarshal(body, response); err != nil {
return fmt.Errorf("failed to unmarshal response: %w", err)
}
return nil
}
func (c *Client) OpenShift(ctx context.Context, cashier string) (*SellResponse, error) {
request := FiscalRequest{
Fiscal: Fiscal{
Task: TaskOpenShift,
Cashier: cashier,
},
}
var response SellResponse
if err := c.executeRequest(ctx, request, &response); err != nil {
return nil, err
}
return &response, nil
}
func (c *Client) CloseShift(ctx context.Context, cashier string) (*ZReportResponse, error) {
request := FiscalRequest{
Fiscal: Fiscal{
Task: TaskZReport,
Cashier: cashier,
},
}
var response ZReportResponse
if err := c.executeRequest(ctx, request, &response); err != nil {
return nil, err
}
return &response, nil
}
type SellParams struct {
Cashier string
Source string
Rows []ReceiptRow
Pays []ReceiptPay
Userinfo *Userinfo
}
func (c *Client) Sell(ctx context.Context, params SellParams) (*SellResponse, error) {
receipt := NewReceipt(params.Rows, params.Pays)
request := FiscalRequest{
Source: params.Source,
Fiscal: Fiscal{
Task: TaskSell,
Cashier: params.Cashier,
Receipt: &receipt,
},
}
if params.Userinfo != nil {
request.Userinfo = *params.Userinfo
}
var response SellResponse
if err := c.executeRequest(ctx, request, &response); err != nil {
return nil, err
}
return &response, nil
}

49
api/helpers.go Normal file
View File

@@ -0,0 +1,49 @@
package api
func NewReceiptRow(name string, cnt int, price float64, taxgrp string) ReceiptRow {
return ReceiptRow{
Name: name,
Cnt: cnt,
Price: price,
Taxgrp: taxgrp,
}
}
func NewReceiptPayCash(sum float64, comment string) ReceiptPay {
return ReceiptPay{
Type: PayTypeCash,
Sum: sum,
Comment: comment,
}
}
func NewReceiptPayCard(sum float64, cardmask, bankID, rrnCode, authCode string) ReceiptPay {
return ReceiptPay{
Type: PayTypeCard,
Sum: sum,
Paysys: PaySystemParkingPos,
Cardmask: cardmask,
BankID: bankID,
Rrn: rrnCode,
AuthCode: authCode,
}
}
func CalculateReceiptSum(rows []ReceiptRow) float64 {
sum := 0.0
for _, row := range rows {
sum += (row.Price - row.Disc) * float64(row.Cnt)
}
return sum
}
func NewReceipt(rows []ReceiptRow, pays []ReceiptPay) Receipt {
return Receipt{
Sum: CalculateReceiptSum(rows),
Round: 0.00,
Disc: 0,
DiscType: 0,
Rows: rows,
Pays: pays,
}
}

View File

@@ -1,152 +0,0 @@
package api
import (
"context"
"encoding/json"
"fmt"
"io"
"resty.dev/v3"
)
func NewKasaInstance(token string) *Kasa {
return &Kasa{
token: token,
resty: resty.New(),
}
}
type Kasa struct {
token string
resty *resty.Client
}
func createReceiptRow(name string, cnt int, price float64, comment string, disc float64, taxgrp string) ReceiptRow {
return ReceiptRow{
Name: name,
Cnt: cnt,
Price: price,
Disc: disc,
Taxgrp: taxgrp,
Comment: comment,
}
}
func createReceiptPayCash(PayType int, sum float64, comment string) ReceiptPay {
return ReceiptPay{
Type: PayType,
Sum: sum,
Comment: comment,
}
}
func createReceiptPayCard(PayType int, sum float64, comment string, cardmask string, bankID string, rrnCode string, authCode string) ReceiptPay {
return ReceiptPay{
Type: PayType,
Sum: sum,
Comment: comment,
Paysys: "parking_pos",
Cardmask: cardmask,
BankID: bankID,
Rrn: rrnCode,
AuthCode: authCode,
}
}
func getReceiptSum(rows []ReceiptRow, taxgrp string) float64 {
sum := 0.0
for _, row := range rows {
sum += (row.Price - row.Disc) * float64(row.Cnt)
}
return sum
}
func createReceipt(PayType int, sum float64, comment string, cardmask string, bankID string, name string, cnt int, price float64, disc float64, taxgrp string, rrnCode string, authCode string) Receipt {
rows := []ReceiptRow{createReceiptRow(name, cnt, price, comment, disc, taxgrp)}
sum = getReceiptSum(rows, taxgrp)
r := Receipt{
Sum: sum,
Round: 0.00,
Disc: 0,
DiscType: 0,
Rows: rows,
Pays: []ReceiptPay{createReceiptPayCard(PayType, sum, comment, cardmask, bankID, rrnCode, authCode)},
}
fmt.Println("Receipt sum:", r.Sum)
return r
}
// "fiscal": {
// "task": 1,
// "cashier": "Парковка",
// "receipt": {
// "sum": 40.00,
// "round": 0.00,
// "comment_up": "Квитанція за паркування",
// "comment_down": "Дякуємо за користування паркінгом!",
// "disc": 0.00,
// "disc_type": 0,
// "rows": [
// {
// "code": "PARK-3H",
// "pop": "Оплата за послуги паркування",
// "name": "Парковка, 3 години",
// "cnt": 1,
// "price": 40.00,
// "disc": 0.00,
// "taxgrp": "4",
// "comment": "Тариф: 3 години"
// }
// ],
// "pays": [
// {
// "type": 0,
// "sum": 40.00,
// "change": 0.00,
// "comment": "Оплата готівкою"
// }
// ]
// }
// }
func createFiscal(source string, cashier string, PayType int, sum float64, comment string, cardmask string, bankID string, name string, cnt int, price float64, disc float64, taxgrp string, rrnCode string, authCode string) FiskalCheck {
return FiskalCheck{
Source: source,
Fiscal: Fiscal{
Task: 1,
Cashier: cashier,
Receipt: createReceipt(PayType, sum, comment, cardmask, bankID, name, cnt, price, disc, taxgrp, rrnCode, authCode),
},
}
}
func (k *Kasa) NewSell(ctx context.Context, PayType int, sum float64, comment string, cardmask string, bankID string, name string, cnt int, price float64, disc float64, taxgrp string, rrnCode string, authCode string) (*KasaResponse, error) {
fiscal := createFiscal("kasa", "test", PayType, sum, comment, cardmask, bankID, name, cnt, price, disc, taxgrp, rrnCode, authCode)
// create a POST request to the kasa api https://kasa.vchasno.ua/api/v3/fiscal/execute with resty
request, err := k.resty.R().SetBody(fiscal).SetHeader("Authorization", k.token).Post("https://kasa.vchasno.ua/api/v3/fiscal/execute")
if err != nil {
return nil, fmt.Errorf("failed to create a POST request to the kasa api: %w", err)
}
if request.IsError() {
return nil, fmt.Errorf("failed to create a POST request to the kasa api: %v", request.Error())
}
if request.StatusCode() != 200 {
return nil, fmt.Errorf("failed to create a POST request to the kasa api: %v", request.Error())
}
body, err := io.ReadAll(request.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
fmt.Println(string(body))
var response KasaResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
return &response, nil
}

61
api/requests.go Normal file
View File

@@ -0,0 +1,61 @@
package api
type FiscalRequest struct {
Source string `json:"source"`
Userinfo Userinfo `json:"userinfo,omitempty"`
Fiscal Fiscal `json:"fiscal"`
}
type Userinfo struct {
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
}
type Fiscal struct {
Task int `json:"task"`
Cashier string `json:"cashier"`
Receipt *Receipt `json:"receipt,omitempty"`
}
type Receipt struct {
Sum float64 `json:"sum"`
Round float64 `json:"round"`
CommentUp string `json:"comment_up,omitempty"`
CommentDown string `json:"comment_down,omitempty"`
Disc float64 `json:"disc"`
DiscType int `json:"disc_type"`
Rows []ReceiptRow `json:"rows"`
Pays []ReceiptPay `json:"pays"`
}
type ReceiptRow struct {
Code string `json:"code,omitempty"`
Pop string `json:"pop,omitempty"`
Code1 string `json:"code1,omitempty"`
Code2 string `json:"code2,omitempty"`
CodeAa []string `json:"code_aa,omitempty"`
Name string `json:"name"`
Cnt int `json:"cnt"`
Price float64 `json:"price"`
Disc float64 `json:"disc"`
Taxgrp string `json:"taxgrp"`
Comment string `json:"comment,omitempty"`
CodeA string `json:"code_a,omitempty"`
}
type ReceiptPay struct {
Type int `json:"type"`
Sum float64 `json:"sum"`
Change float64 `json:"change,omitempty"`
Comment string `json:"comment,omitempty"`
Commission float64 `json:"commission,omitempty"`
Paysys string `json:"paysys,omitempty"`
Rrn string `json:"rrn,omitempty"`
OperType string `json:"oper_type,omitempty"`
Cardmask string `json:"cardmask,omitempty"`
TermID string `json:"term_id,omitempty"`
BankName string `json:"bank_name,omitempty"`
BankID string `json:"bank_id,omitempty"`
AuthCode string `json:"auth_code,omitempty"`
ShowAdditionalInfo bool `json:"show_additional_info,omitempty"`
}

124
api/responses.go Normal file
View File

@@ -0,0 +1,124 @@
package api
type BaseResponse struct {
Task int `json:"task"`
Type int `json:"type"`
Ver int `json:"ver"`
Source string `json:"source"`
Device string `json:"device"`
Tag string `json:"tag"`
Dt string `json:"dt"`
Res int `json:"res"`
ResAction int `json:"res_action"`
Errortxt string `json:"errortxt"`
Warnings []string `json:"warnings"`
ErrorExtra interface{} `json:"error_extra"`
}
type SellResponse struct {
BaseResponse
Info SellInfo `json:"info"`
}
type SellInfo struct {
Task int `json:"task"`
Fisid string `json:"fisid"`
Dataid int `json:"dataid"`
Doccode string `json:"doccode"`
Dt string `json:"dt"`
Cashier string `json:"cashier"`
Dtype int `json:"dtype"`
Isprint int `json:"isprint"`
Isoffline bool `json:"isoffline"`
Safe float64 `json:"safe"`
ShiftLink int `json:"shift_link"`
Docno int `json:"docno"`
Cancelid string `json:"cancelid,omitempty"`
}
type ZReportResponse struct {
BaseResponse
Info ZReportInfo `json:"info"`
}
type ZReportInfo struct {
Task int `json:"task"`
Fisid string `json:"fisid"`
Dataid int `json:"dataid"`
Doccode string `json:"doccode"`
Dt string `json:"dt"`
Cashier string `json:"cashier"`
Dtype int `json:"dtype"`
Isprint int `json:"isprint"`
Isoffline bool `json:"isoffline"`
Safe float64 `json:"safe"`
ShiftLink int `json:"shift_link"`
Docno int `json:"docno"`
Receipt ZReportReceipt `json:"receipt"`
Summary ZReportSummary `json:"summary"`
Taxes []ZReportTax `json:"taxes"`
Pays []ZReportPay `json:"pays"`
Money []ZReportMoney `json:"money"`
Cash []ZReportMoney `json:"cash"`
MoneyTransfer []interface{} `json:"money_transfer"`
}
type ZReportReceipt struct {
CountP int `json:"count_p"`
CountM int `json:"count_m"`
Count14 int `json:"count_14"`
CountTransfer int `json:"count_transfer"`
LastDocnoP int `json:"last_docno_p"`
LastDocnoM int `json:"last_docno_m"`
}
type ZReportSummary struct {
BaseP float64 `json:"base_p"`
BaseM float64 `json:"base_m"`
TaxexP float64 `json:"taxex_p"`
TaxexM float64 `json:"taxex_m"`
DiscP float64 `json:"disc_p"`
DiscM float64 `json:"disc_m"`
}
type ZReportTax struct {
GrCode int `json:"gr_code"`
BaseSumP float64 `json:"base_sum_p"`
BaseSumM float64 `json:"base_sum_m"`
BaseTaxSumP float64 `json:"base_tax_sum_p"`
BaseTaxSumM float64 `json:"base_tax_sum_m"`
BaseExSumP float64 `json:"base_ex_sum_p"`
BaseExSumM float64 `json:"base_ex_sum_m"`
TaxName string `json:"tax_name"`
TaxFname string `json:"tax_fname"`
TaxLit string `json:"tax_lit"`
TaxPercent float64 `json:"tax_percent"`
TaxSumP float64 `json:"tax_sum_p"`
TaxSumM float64 `json:"tax_sum_m"`
ExName string `json:"ex_name"`
ExPercent float64 `json:"ex_percent"`
ExSumP float64 `json:"ex_sum_p"`
ExSumM float64 `json:"ex_sum_m"`
}
type ZReportPay struct {
Type int `json:"type"`
Name string `json:"name"`
SumP float64 `json:"sum_p"`
SumM float64 `json:"sum_m"`
RoundPu float64 `json:"round_pu"`
RoundPd float64 `json:"round_pd"`
RoundMu float64 `json:"round_mu"`
RoundMd float64 `json:"round_md"`
}
type ZReportMoney struct {
Type int `json:"type"`
Name string `json:"name"`
SumP float64 `json:"sum_p"`
SumM float64 `json:"sum_m"`
RoundPu float64 `json:"round_pu"`
RoundPd float64 `json:"round_pd"`
RoundMu float64 `json:"round_mu"`
RoundMd float64 `json:"round_md"`
}

View File

@@ -1,93 +0,0 @@
package api
type FiskalCheck struct {
Source string `json:"source"`
Userinfo Userinfo `json:"userinfo"`
Fiscal Fiscal `json:"fiscal"`
}
type Userinfo struct {
Email string `json:"email"`
Phone string `json:"phone"`
}
type Fiscal struct {
Task int `json:"task"`
Cashier string `json:"cashier"`
Receipt Receipt `json:"receipt"`
}
type Receipt struct {
Sum float64 `json:"sum"`
Round float64 `json:"round"`
CommentUp string `json:"comment_up"`
CommentDown string `json:"comment_down"`
Disc float64 `json:"disc"`
DiscType int `json:"disc_type"`
Rows []ReceiptRow `json:"rows"`
Pays []ReceiptPay `json:"pays"`
}
type ReceiptRow struct {
Code string `json:"code"`
Pop string `json:"pop,omitempty"`
Code1 string `json:"code1"`
Code2 string `json:"code2"`
CodeAa []string `json:"code_aa,omitempty"`
Name string `json:"name"`
Cnt int `json:"cnt"`
Price float64 `json:"price"`
Disc float64 `json:"disc"`
Taxgrp string `json:"taxgrp"`
Comment string `json:"comment"`
CodeA string `json:"code_a,omitempty"`
}
type ReceiptPay struct {
Type int `json:"type"`
Sum float64 `json:"sum"`
Change float64 `json:"change,omitempty"`
Comment string `json:"comment"`
Commission float64 `json:"commission,omitempty"`
Paysys string `json:"paysys,omitempty"`
Rrn string `json:"rrn,omitempty"`
OperType string `json:"oper_type,omitempty"`
Cardmask string `json:"cardmask,omitempty"`
TermID string `json:"term_id,omitempty"`
BankName string `json:"bank_name,omitempty"`
BankID string `json:"bank_id,omitempty"`
AuthCode string `json:"auth_code,omitempty"`
ShowAdditionalInfo bool `json:"show_additional_info,omitempty"`
}
type KasaResponse struct {
Task int `json:"task"`
Type int `json:"type"`
Ver int `json:"ver"`
Source string `json:"source"`
Device string `json:"device"`
Tag string `json:"tag"`
Dt string `json:"dt"`
Res int `json:"res"`
ResAction int `json:"res_action"`
Errortxt string `json:"errortxt"`
Warnings []string `json:"warnings"`
Info Info `json:"info"`
ErrorExtra interface{} `json:"error_extra"`
}
type Info struct {
Task int `json:"task"`
Fisid string `json:"fisid"`
Dataid int `json:"dataid"`
Doccode string `json:"doccode"`
Dt string `json:"dt"`
Cashier string `json:"cashier"`
Dtype int `json:"dtype"`
Isprint int `json:"isprint"`
Isoffline bool `json:"isoffline"`
Safe float64 `json:"safe"`
ShiftLink int `json:"shift_link"`
Docno int `json:"docno"`
Cancelid string `json:"cancelid"`
}