Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d4cad7cb74 | |||
| ee08291fc1 | |||
| 23779ad6ad | |||
| f23c4ffd1c | |||
| 861a1640d4 | |||
| 28a8810ffb | |||
| 3ed7d33b84 |
@@ -9,6 +9,7 @@ import (
|
||||
type Client struct {
|
||||
token string
|
||||
device string
|
||||
isDM bool
|
||||
resty *resty.Client
|
||||
apiBaseURL string
|
||||
fiscalEndpoint string
|
||||
@@ -17,6 +18,7 @@ type Client struct {
|
||||
func NewClient(token string) *Client {
|
||||
return &Client{
|
||||
token: token,
|
||||
isDM: false,
|
||||
resty: resty.New(),
|
||||
apiBaseURL: "https://kasa.vchasno.ua/api/v3",
|
||||
fiscalEndpoint: "/fiscal/execute",
|
||||
@@ -38,8 +40,9 @@ func NewDMClient(token, dmURL, device string) *Client {
|
||||
return &Client{
|
||||
token: token,
|
||||
device: device,
|
||||
isDM: true,
|
||||
resty: restyClient,
|
||||
apiBaseURL: dmURL,
|
||||
fiscalEndpoint: "/dm/fiscal",
|
||||
fiscalEndpoint: "/dm/execute",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,22 +5,41 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
|
||||
type APIError struct {
|
||||
Code int
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *APIError) Error() string {
|
||||
return fmt.Sprintf("vchasno api error %d: %s", e.Code, e.Message)
|
||||
}
|
||||
|
||||
func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, response interface{}) error {
|
||||
request.Device = c.device
|
||||
|
||||
if c.isDM {
|
||||
request.Ver = 6
|
||||
request.Type = 1
|
||||
}
|
||||
|
||||
reqJson, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal request: %w", err)
|
||||
}
|
||||
|
||||
url := c.apiBaseURL + c.fiscalEndpoint
|
||||
log.Printf("[VCHASNO] POST %s", url)
|
||||
log.Printf("[VCHASNO] Request: %s", string(reqJson))
|
||||
|
||||
resp, err := c.resty.R().
|
||||
SetContext(ctx).
|
||||
SetHeader("Authorization", c.token).
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetBody(reqJson).
|
||||
Post(c.apiBaseURL + c.fiscalEndpoint)
|
||||
Post(url)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("request failed: %w", err)
|
||||
@@ -31,6 +50,8 @@ func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, resp
|
||||
return fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("[VCHASNO] Response (status %d): %s", resp.StatusCode(), string(body))
|
||||
|
||||
if resp.StatusCode() != 200 {
|
||||
return fmt.Errorf("api error (status %d): %s", resp.StatusCode(), string(body))
|
||||
}
|
||||
@@ -39,6 +60,16 @@ func (c *Client) executeRequest(ctx context.Context, request FiscalRequest, resp
|
||||
return fmt.Errorf("failed to unmarshal response: %w, body: %s", err, string(body))
|
||||
}
|
||||
|
||||
var baseResp BaseResponse
|
||||
if err := json.Unmarshal(body, &baseResp); err == nil {
|
||||
if baseResp.HasError() {
|
||||
return &APIError{
|
||||
Code: baseResp.Res,
|
||||
Message: baseResp.Errortxt,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -46,7 +77,6 @@ func (c *Client) OpenShift(ctx context.Context, cashier string) (*SellResponse,
|
||||
request := FiscalRequest{
|
||||
Fiscal: Fiscal{
|
||||
Task: TaskOpenShift,
|
||||
Cashier: cashier,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -62,7 +92,6 @@ func (c *Client) CloseShift(ctx context.Context, cashier string) (*ZReportRespon
|
||||
request := FiscalRequest{
|
||||
Fiscal: Fiscal{
|
||||
Task: TaskZReport,
|
||||
Cashier: cashier,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package api
|
||||
|
||||
type FiscalRequest struct {
|
||||
Source string `json:"source"`
|
||||
Ver int `json:"ver,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
Device string `json:"device,omitempty"`
|
||||
Tag string `json:"tag,omitempty"`
|
||||
Type int `json:"type,omitempty"`
|
||||
Userinfo Userinfo `json:"userinfo,omitempty"`
|
||||
Fiscal Fiscal `json:"fiscal"`
|
||||
}
|
||||
@@ -14,7 +17,7 @@ type Userinfo struct {
|
||||
|
||||
type Fiscal struct {
|
||||
Task int `json:"task"`
|
||||
Cashier string `json:"cashier"`
|
||||
Cashier string `json:"cashier,omitempty"`
|
||||
Receipt *Receipt `json:"receipt,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
193
api/responses.go
193
api/responses.go
@@ -4,17 +4,30 @@ type BaseResponse struct {
|
||||
Task int `json:"task"`
|
||||
Type int `json:"type"`
|
||||
Ver int `json:"ver"`
|
||||
RespVer int `json:"resp_ver"`
|
||||
Source string `json:"source"`
|
||||
Device string `json:"device"`
|
||||
Tag string `json:"tag"`
|
||||
TaskStatus int `json:"task_status"`
|
||||
Dt string `json:"dt"`
|
||||
Res int `json:"res"`
|
||||
ResAction int `json:"res_action"`
|
||||
Errortxt string `json:"errortxt"`
|
||||
Warnings []string `json:"warnings"`
|
||||
AqErrortxt string `json:"aq_errortxt"`
|
||||
ErrorExtra interface{} `json:"error_extra"`
|
||||
}
|
||||
|
||||
func (r *BaseResponse) HasError() bool {
|
||||
return r.Res != 0
|
||||
}
|
||||
|
||||
func (r *BaseResponse) Error() string {
|
||||
if r.Errortxt != "" {
|
||||
return r.Errortxt
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type SellResponse struct {
|
||||
BaseResponse
|
||||
Info SellInfo `json:"info"`
|
||||
@@ -25,15 +38,144 @@ type SellInfo struct {
|
||||
Fisid string `json:"fisid"`
|
||||
Dataid int `json:"dataid"`
|
||||
Doccode string `json:"doccode"`
|
||||
Docno interface{} `json:"docno"`
|
||||
Dt string `json:"dt"`
|
||||
Cashier string `json:"cashier"`
|
||||
Dtype int `json:"dtype"`
|
||||
Isprint int `json:"isprint"`
|
||||
Ispay int `json:"ispay"`
|
||||
Isoffline bool `json:"isoffline"`
|
||||
Safe float64 `json:"safe"`
|
||||
SafeStartShift float64 `json:"safe_start_shift"`
|
||||
ShiftLink int `json:"shift_link"`
|
||||
Docno int `json:"docno"`
|
||||
ShiftPrevLink int `json:"shift_prev_link"`
|
||||
ShiftID string `json:"shift_id"`
|
||||
OpenShiftDt string `json:"open_shift_dt"`
|
||||
Cancelid string `json:"cancelid,omitempty"`
|
||||
VacantOffNums int `json:"vacant_off_nums"`
|
||||
Devinfo string `json:"devinfo"`
|
||||
DfsLocalNumber string `json:"dfs_local_number"`
|
||||
Userdata1 string `json:"userdata1"`
|
||||
Userdata2 string `json:"userdata2"`
|
||||
Userdata3 string `json:"userdata3"`
|
||||
QR string `json:"qr"`
|
||||
QR1 string `json:"qr1"`
|
||||
Billing *Billing `json:"billing,omitempty"`
|
||||
Printheader *Printheader `json:"printheader,omitempty"`
|
||||
Printinfo *Printinfo `json:"printinfo,omitempty"`
|
||||
}
|
||||
|
||||
type Billing struct {
|
||||
PaidDateTo string `json:"paid_date_to"`
|
||||
EnoughToRenewSubscription int `json:"enough_to_renew_subscription"`
|
||||
}
|
||||
|
||||
type Printheader struct {
|
||||
Name string `json:"name"`
|
||||
Shopname string `json:"shopname"`
|
||||
Shoptype string `json:"shoptype"`
|
||||
Shopad string `json:"shopad"`
|
||||
VatCode string `json:"vat_code"`
|
||||
FisCode string `json:"fis_code"`
|
||||
Dt string `json:"dt"`
|
||||
OpenShiftDt string `json:"open_shift_dt"`
|
||||
IsOffline bool `json:"isOffline"`
|
||||
Fisid string `json:"fisid"`
|
||||
Manuf string `json:"manuf"`
|
||||
Cashier string `json:"cashier"`
|
||||
Task int `json:"task"`
|
||||
DfsLocalNumber string `json:"dfs_local_number"`
|
||||
}
|
||||
|
||||
type Printinfo struct {
|
||||
Name string `json:"name"`
|
||||
Shopname string `json:"shopname"`
|
||||
Shoptype string `json:"shoptype"`
|
||||
Shopad string `json:"shopad"`
|
||||
VatCode string `json:"vat_code"`
|
||||
FisCode string `json:"fis_code"`
|
||||
DfsLocalNumber string `json:"dfs_local_number"`
|
||||
Fisn string `json:"fisn"`
|
||||
Dt string `json:"dt"`
|
||||
OpenShiftDt string `json:"open_shift_dt"`
|
||||
QR string `json:"qr"`
|
||||
QR1 string `json:"qr1"`
|
||||
IsOffline bool `json:"isOffline"`
|
||||
Mac string `json:"mac"`
|
||||
Fisid string `json:"fisid"`
|
||||
Manuf string `json:"manuf"`
|
||||
Cashier string `json:"cashier"`
|
||||
Task int `json:"task"`
|
||||
Subtask int `json:"subtask"`
|
||||
FcId string `json:"fcId"`
|
||||
Fisdoctype string `json:"fisdoctype"`
|
||||
CommentDown string `json:"comment_down"`
|
||||
CommentUp string `json:"comment_up"`
|
||||
Safe float64 `json:"safe"`
|
||||
SafeStartShift float64 `json:"safe_start_shift"`
|
||||
Docno string `json:"docno"`
|
||||
Userdata1 string `json:"userdata1"`
|
||||
Userdata2 string `json:"userdata2"`
|
||||
Userdata3 string `json:"userdata3"`
|
||||
Crc32 int `json:"crc32"`
|
||||
SumReceipt float64 `json:"sum_receipt"`
|
||||
SumTopay float64 `json:"sum_topay"`
|
||||
Round float64 `json:"round"`
|
||||
Goods []PrintGood `json:"goods"`
|
||||
Pays []PrintPay `json:"pays"`
|
||||
Taxes []PrintTax `json:"taxes"`
|
||||
}
|
||||
|
||||
type PrintGood struct {
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
Code1 string `json:"code1"`
|
||||
Code2 string `json:"code2"`
|
||||
CodeA string `json:"code_a"`
|
||||
CodeAa []string `json:"code_aa"`
|
||||
Cnt int `json:"cnt"`
|
||||
Price float64 `json:"price"`
|
||||
Cost float64 `json:"cost"`
|
||||
CostAfterDisc float64 `json:"cost_after_disc"`
|
||||
Disc float64 `json:"disc"`
|
||||
DiscType int `json:"disc_type"`
|
||||
Taxgrp int `json:"taxgrp"`
|
||||
Taxlit string `json:"taxlit"`
|
||||
Comment string `json:"comment"`
|
||||
Commission float64 `json:"commission"`
|
||||
}
|
||||
|
||||
type PrintPay struct {
|
||||
PayClass int `json:"pay_class"`
|
||||
IsTerminal bool `json:"is_terminal"`
|
||||
Type int `json:"type"`
|
||||
Typen string `json:"typen"`
|
||||
Sum float64 `json:"sum"`
|
||||
SumOrig float64 `json:"sum_orig"`
|
||||
Commission float64 `json:"commission"`
|
||||
Currency string `json:"currency"`
|
||||
OperType string `json:"oper_type"`
|
||||
ShowAdditionalInfo bool `json:"show_additional_info"`
|
||||
Info string `json:"info"`
|
||||
Comment string `json:"comment"`
|
||||
ReceivedSum float64 `json:"received_sum"`
|
||||
Change float64 `json:"change"`
|
||||
}
|
||||
|
||||
type PrintTax struct {
|
||||
GrCode int `json:"gr_code"`
|
||||
BaseSum float64 `json:"base_sum"`
|
||||
TaxName string `json:"tax_name"`
|
||||
TaxFname string `json:"tax_fname"`
|
||||
TaxLit string `json:"tax_lit"`
|
||||
TaxPercent float64 `json:"tax_percent"`
|
||||
BaseTaxSum float64 `json:"base_tax_sum"`
|
||||
TaxSum float64 `json:"tax_sum"`
|
||||
ExName string `json:"ex_name"`
|
||||
ExPercent float64 `json:"ex_percent"`
|
||||
BaseExSum float64 `json:"base_ex_sum"`
|
||||
ExSum float64 `json:"ex_sum"`
|
||||
ActivationDt string `json:"activation_dt"`
|
||||
}
|
||||
|
||||
type ZReportResponse struct {
|
||||
@@ -46,14 +188,24 @@ type ZReportInfo struct {
|
||||
Fisid string `json:"fisid"`
|
||||
Dataid int `json:"dataid"`
|
||||
Doccode string `json:"doccode"`
|
||||
Docno interface{} `json:"docno"`
|
||||
Dt string `json:"dt"`
|
||||
OpenShiftDt string `json:"open_shift_dt"`
|
||||
Cashier string `json:"cashier"`
|
||||
Dtype int `json:"dtype"`
|
||||
Isprint int `json:"isprint"`
|
||||
Ispay int `json:"ispay"`
|
||||
Isoffline bool `json:"isoffline"`
|
||||
Safe float64 `json:"safe"`
|
||||
SafeStartShift float64 `json:"safe_start_shift"`
|
||||
ShiftLink int `json:"shift_link"`
|
||||
Docno int `json:"docno"`
|
||||
ShiftPrevLink int `json:"shift_prev_link"`
|
||||
VacantOffNums int `json:"vacant_off_nums"`
|
||||
Devinfo string `json:"devinfo"`
|
||||
DfsLocalNumber string `json:"dfs_local_number"`
|
||||
Userdata1 string `json:"userdata1"`
|
||||
Userdata2 string `json:"userdata2"`
|
||||
Userdata3 string `json:"userdata3"`
|
||||
Receipt ZReportReceipt `json:"receipt"`
|
||||
Summary ZReportSummary `json:"summary"`
|
||||
Taxes []ZReportTax `json:"taxes"`
|
||||
@@ -61,12 +213,27 @@ type ZReportInfo struct {
|
||||
Money []ZReportMoney `json:"money"`
|
||||
Cash []ZReportMoney `json:"cash"`
|
||||
MoneyTransfer []interface{} `json:"money_transfer"`
|
||||
Income []ZReportMoney `json:"income"`
|
||||
Lastcheck *Lastcheck `json:"lastcheck,omitempty"`
|
||||
Reports []interface{} `json:"reports"`
|
||||
Receipts []interface{} `json:"receipts"`
|
||||
Billing *Billing `json:"billing,omitempty"`
|
||||
Printheader *Printheader `json:"printheader,omitempty"`
|
||||
}
|
||||
|
||||
type Lastcheck struct {
|
||||
Packnum int `json:"packnum"`
|
||||
Docnum int `json:"docnum"`
|
||||
Fisnum string `json:"fisnum"`
|
||||
Packtype int `json:"packtype"`
|
||||
}
|
||||
|
||||
type ZReportReceipt struct {
|
||||
CountP int `json:"count_p"`
|
||||
CountM int `json:"count_m"`
|
||||
Count14 int `json:"count_14"`
|
||||
Count15 int `json:"count_15"`
|
||||
Count16 int `json:"count_16"`
|
||||
CountTransfer int `json:"count_transfer"`
|
||||
LastDocnoP int `json:"last_docno_p"`
|
||||
LastDocnoM int `json:"last_docno_m"`
|
||||
@@ -79,12 +246,25 @@ type ZReportSummary struct {
|
||||
TaxexM float64 `json:"taxex_m"`
|
||||
DiscP float64 `json:"disc_p"`
|
||||
DiscM float64 `json:"disc_m"`
|
||||
DiscPSale float64 `json:"disc_p_sale"`
|
||||
DiscPRef float64 `json:"disc_p_ref"`
|
||||
DiscMSale float64 `json:"disc_m_sale"`
|
||||
DiscMRef float64 `json:"disc_m_ref"`
|
||||
Disc0P float64 `json:"disc0_p"`
|
||||
Disc0M float64 `json:"disc0_m"`
|
||||
CalcP float64 `json:"calc_p"`
|
||||
CalcM float64 `json:"calc_m"`
|
||||
IncomeP float64 `json:"income_p"`
|
||||
}
|
||||
|
||||
type ZReportTax struct {
|
||||
GrCode int `json:"gr_code"`
|
||||
BaseSumP float64 `json:"base_sum_p"`
|
||||
BaseSumM float64 `json:"base_sum_m"`
|
||||
BaseSumLinkP float64 `json:"base_sum_link_p"`
|
||||
BaseSumLinkM float64 `json:"base_sum_link_m"`
|
||||
TaxSumLinkP float64 `json:"tax_sum_link_p"`
|
||||
TaxSumLinkM float64 `json:"tax_sum_link_m"`
|
||||
BaseTaxSumP float64 `json:"base_tax_sum_p"`
|
||||
BaseTaxSumM float64 `json:"base_tax_sum_m"`
|
||||
BaseExSumP float64 `json:"base_ex_sum_p"`
|
||||
@@ -99,6 +279,10 @@ type ZReportTax struct {
|
||||
ExPercent float64 `json:"ex_percent"`
|
||||
ExSumP float64 `json:"ex_sum_p"`
|
||||
ExSumM float64 `json:"ex_sum_m"`
|
||||
ActivationDt string `json:"activation_dt"`
|
||||
TaxNotIncl int `json:"tax_not_incl"`
|
||||
TaxAlg int `json:"tax_alg"`
|
||||
TaxLink int `json:"tax_link"`
|
||||
}
|
||||
|
||||
type ZReportPay struct {
|
||||
@@ -110,6 +294,9 @@ type ZReportPay struct {
|
||||
RoundPd float64 `json:"round_pd"`
|
||||
RoundMu float64 `json:"round_mu"`
|
||||
RoundMd float64 `json:"round_md"`
|
||||
CommissionP float64 `json:"commission_p"`
|
||||
CommissionM float64 `json:"commission_m"`
|
||||
SumTaxM float64 `json:"sum_tax_m"`
|
||||
}
|
||||
|
||||
type ZReportMoney struct {
|
||||
|
||||
Reference in New Issue
Block a user