


<?php Class AtolWebDriver { protected $addr="127.0.0.1",$port="16732"; public $timeout = 30; // public $operator; function __construct($addr=false,$port=false) { if ($addr!==false) $this->addr=$addr; if ($port!==false) $this->port=$port; } public function CallAPI($method, $data,$_url="/requests") { $url = "http://".$this->addr.":".$this->port.$_url; $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl,CURLOPT_TIMEOUT, $this->timeout); $headers = ['Content-Type: application/json']; curl_setopt($curl,CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); $resp = curl_exec($curl); $data = json_decode($resp,1); $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); $res= [$data,$code,$resp]; print_r($res); return $res; } // public function get_res($uuid) { $ready = false; $cnt=0; $res_url = '/requests/'.$uuid; while (!$ready && ++$cnt<60) { usleep(500000); // , list($res,$code,$resp) = $this->CallAPI('GET',[],$res_url); $ready = ($res['results'][0]['status'] == 'ready'); if ($ready) return $res; } return false; // } // public function add_req($uuid,$req) { return $this->CallAPI('POST', ['uuid'=>$uuid,'request'=>$req]); } // id public function gen_uuid() { return exec('uuidgen -r'); } // public function atol_task($type,$req=[]) { $req['type'] = $type; $uuid = $this->gen_uuid(); $req = $this->add_req($uuid,$req); if ($req[1]!='201') return false; // $res = $this->get_res($uuid); // if ($res===false || !isset($res['results'][0])) return false; return $res['results'][0]; } /* */ // public function get_shift_status() { $res = $this->atol_task('getShiftStatus'); if ($res===false) return false; //closed / opened / expired return $res['result']['shiftStatus']['state']; } // public function open_shift() { $status = $this->get_shift_status(); //e , if ($status=="expired") $this->close_shift(); if ($status=="opened") return " "; $res = $this->atol_task('openShift',['operator'=>$this->operator]); } // public function close_shift() { $status = $this->get_shift_status(); if ($status=="closed") return " "; $res = $this->atol_task('closeShift',['operator'=>$this->operator]); } public function items_prepare($items) { $res_items = []; $summ = 0; while ($item = array_shift($items)) { $res_item = $item; if (!isset($item['type'])) $res_item['type']="position"; if (isset($item['price']) && isset($item['quantity'])) { $res_item['amount'] = $item['price']*$item['quantity']; $res_item['tax'] = ['type'=>'none']; $summ+=$res_item['amount']; } $res_items[] = $res_item; } return [$res_items,$summ]; } // sell, sellReturn public function fiskal($type_op="sell",$items,$pay_type="cash") { $data = []; $data['operator'] = $this->operator; $data['payments'] = []; list($data['items'],$summ) = $this->items_prepare($items); //+++ $data['payments'][] = ['type'=>$pay_type,'sum'=>$summ]; $res = $this->atol_task($type_op,$data); } } // ip web- , $atol = new AtolWebDriver('192.168.100.10'); // $atol->operator = ['name'=>'.']; // $items = []; $items[] = ['name'=>' ','price'=>0.7,'quantity'=>1]; $items[] = ['name'=>' ','price'=>0.4,'quantity'=>1]; // $atol->open_shift(); // $atol->fiskal("sell",$items); sleep(10); // , // , .. $atol->fiskal("sellReturn",$items); // sleep(20); // , $atol->close_shift(); Source: https://habr.com/ru/post/457684/
All Articles