/
var
/
www
/
myprog.com.ua
/
telegram
/
models
/
/var/www/myprog.com.ua/telegram/models
mkdir
upload
Name
Size
Mode
Actions
class.api_model.php
16177
0644
edit
dl
rm
Edit:
/var/www/myprog.com.ua/telegram/models/class.api_model.php
(16177B)
<?php /** * @author Sergey Kozinets * @copyright Doncode 2017 * @package CashControl API */ class api_model extends model { public function __construct(){ parent::__construct(); } public function add_user($chat_id,$first_name,$last_name,$last_command){ try{ $data['system_id'] = 0; $data['system_login'] = ''; $data['system_auth'] = 'no'; $data['t_chat_id'] = $chat_id; $data['t_user_first_name'] = $first_name; $data['t_user_last_name'] = $last_name; $data['last_command'] = $last_command; $this->database->insert('users_telegram',$data); $user_id = $this->database->lastInsertId(); $data['id'] = $user_id; bootstrap::log("CREATE USER ID ".$user_id); return $data; }catch(PDOException $e){ bootstrap::log($e->getMessage()); return null; } } public function get_user($chat_id){ try{ $sth = $this->database->query("SELECT ut.* FROM users_telegram ut WHERE ut.t_chat_id={$chat_id} LIMIT 1 "); $data = $sth->fetchAll(PDO::FETCH_ASSOC); if(isset($data[0])){ bootstrap::log("GET CHAT ID ".$chat_id); return $data[0]; } return null; }catch(PDOException $e){ bootstrap::log($e->getMessage()); return null; } } public function set_last_command($chat_id,$last_command){ try{ $data['last_command'] = $last_command; $this->database->update('users_telegram',$data,"t_chat_id=".$chat_id); bootstrap::log("SET LAST COMMAND ".$last_command); return true; }catch(PDOException $e){ bootstrap::log($e->getMessage()); return null; } } public function get_last_command($chat_id){ try{ $sth = $this->database->query("SELECT ut.last_command FROM users_telegram ut WHERE ut.t_chat_id={$chat_id} LIMIT 1 "); $data = $sth->fetchAll(PDO::FETCH_ASSOC); if(isset($data[0])){ bootstrap::log("GET LAST COMMAND ".$data[0]['last_command']); return $data[0]['last_command']; } return null; }catch(PDOException $e){ bootstrap::log($e->getMessage()); return null; } } public function select_system($alias){ // Выбор системы по ALIAS / DOMAIN try{ $sth = $this->database->query("SELECT s.* FROM systems s WHERE s.alias='{$alias}' LIMIT 1 "); $data = $sth->fetchAll(PDO::FETCH_ASSOC); if(isset($data[0])){ bootstrap::log("SELECT SYSTEM ".$alias); return $data[0]; } return null; }catch(PDOException $e){ bootstrap::log($e->getMessage()); return null; } } public function get_system($id){ // Выбор системы по ID try{ $sth = $this->database->query("SELECT s.* FROM systems s WHERE s.id={$id} LIMIT 1 "); $data = $sth->fetchAll(PDO::FETCH_ASSOC); if(isset($data[0])){ bootstrap::log("GET SYSTEM ID ".$id); return $data[0]; } return null; }catch(PDOException $e){ bootstrap::log($e->getMessage()); return null; } } public function set_system($chat_id,$system_id){ try{ $data['system_id'] = $system_id; $this->database->update('users_telegram',$data,"t_chat_id=".$chat_id); bootstrap::log("SET SYSTEM ID ".$system_id); return true; }catch(PDOException $e){ bootstrap::log($e->getMessage()); return null; } } public function login(){ $login = isset($_POST['login'])?$_POST['login']:0; $pass = isset($_POST['pass'])?$_POST['pass']:0; try{ $sth = $this->database->prepare("SELECT id FROM users WHERE login='".$login."' AND password='".hash::create('sha256',$pass,HASH_STATIC)."' LIMIT 1 "); $sth->execute(); $data = $sth->fetchAll(PDO::FETCH_ASSOC); if(!isset($data[0]))return null; $client_id = $data[0]['id']; $client_key = md5(md5($login).$pass); $this->database->query("DELETE FROM api_keys WHERE client_id = '".$client_id."'"); $insertkeyid = $this->database->insert(DB_PREFIX.'api_keys',array( 'client_id' =>$client_id, 'client_key' =>$client_key, 'activated' =>'1' )); return $client_key; }catch(PDOException $e){ return $e->getMessage(); } } public function get_bills($client_id,$su=false){ try{ if($su){ $superuser = ' '; }else{ $superuser = ($client_id>0)?' AND ub.user_id='.$client_id.' ':' '; } $sth=$this->database->prepare("SELECT b.*, cu.name as currency, co.name as company, u.login as owner_login FROM user_bills ub LEFT JOIN bills b ON ub.bill_id=b.id INNER JOIN currency cu ON cu.id=b.currency_id LEFT JOIN company co ON co.id=b.company_id LEFT JOIN users u ON u.id=b.owner_user_id WHERE b.deleted='0' ".$superuser." GROUP BY ub.bill_id ORDER BY b.company_id,b.currency_id"); $sth->execute(); // $data = $sth->fetchAll(); $data = $sth->fetchAll(PDO::FETCH_ASSOC); if(!isset($data[0]))return null; return $data; }catch(PDOException $e){ return $e->getMessage(); } } public function get_orders($client_id,$su=false){ try{ if($su){ $superuser = ' '; $superinner = ' '; }else{ $superuser = ($client_id>0)?' AND ub.user_id='.$client_id.' ':' '; $superinner = ' INNER JOIN user_bills ub ON ub.bill_id=b.id '; } $deleted = (isset($_POST['deleted'])&&$_POST['deleted']==1)?' ':" o.deleted='0' AND "; $not_fixed = (isset($_POST['not_fixed'])&&$_POST['not_fixed']==1)?" o.reduction_id='0' AND ":' '; $company = (isset($_POST['company_id'])&&$_POST['company_id']>0)?' AND b.company_id='.$_POST['company_id'].' ':' '; $bill = (isset($_POST['bill_id'])&&$_POST['bill_id']>0)?' AND o.bill_id='.$_POST['bill_id'].' ':' '; $currency = (isset($_POST['currency_id'])&&$_POST['currency_id']>0)?' AND b.currency_id='.$_POST['currency_id'].' ':' '; if(isset($_POST['start_date'])){ $start = date("Y-m-d H:i:s",strtotime($_POST['start_date'])); }else{ $start = date("Y-m-d 00:00:00"); } if(isset($_POST['end_date'])){ $end = date("Y-m-d H:i:s",strtotime($_POST['end_date'])); }else{ $end = date("Y-m-d 23:59:59"); } //$where_date = ($start==$end)?'':''; $where_date = (isset($_POST['not_fixed'])&&$_POST['not_fixed']==1)?' ': " AND o.order_date>='".substr($start,0,10).' 00:00:00'."' AND o.order_date<='".substr($end,0,10).' 23:59:59'."' "; $where=(isset($_POST['contractor_id'])&&$_POST['contractor_id']>0)?' AND o.contractor_id='.$_POST['contractor_id']:''; $where.=(isset($_POST['order_type_id'])&&$_POST['order_type_id']>0)?' AND (o.order_type_id='.$_POST['order_type_id'].' OR ot.owner_id='.$_POST['order_type_id'].') ':''; $where.=$company.$bill.$currency.$superuser; $sql="SELECT o.*, cu.name as currency, t.accept, t.transfer_from, t.transfer_to, b.name as bill_name, ot.owner_id FROM orders o INNER JOIN bills b ON o.bill_id=b.id INNER JOIN currency cu ON cu.id=b.currency_id INNER JOIN order_types ot ON ot.id=o.order_type_id OR ot.owner_id=o.order_type_id LEFT JOIN transactions t ON t.order_from = o.id OR t.order_to = o.id ".$superinner." WHERE ".$deleted." ".$not_fixed." b.deleted='0' ".$where_date." ".$where." GROUP BY o.id ORDER BY o.order_date, o.id DESC"; // echo $sql;exit; $sth=$this->database->prepare($sql); $sth->execute(); // $data = $sth->fetchAll(); $data = $sth->fetchAll(PDO::FETCH_ASSOC); //var_export($data);exit; if(!isset($data[0]))return null; return $data; }catch(PDOException $e){ return $e->getMessage(); } } /////// public function getCompanyBills(){ $company_id = isset($_POST['company_id'])?$_POST['company_id']:'0'; $sth=$this->database->prepare("SELECT b.*, cu.name as currency, co.name as company, u.login as owner_login FROM bills b INNER JOIN currency cu ON cu.id=b.currency_id LEFT JOIN company co ON co.id=b.company_id LEFT JOIN users u ON u.id=b.owner_user_id WHERE b.deleted='0' AND b.company_id='".$company_id."' ORDER BY b.currency_id"); $sth->execute(); $data = $sth->fetchAll(); return $data; } public function getContractors($client_id){ $login_id = $client_id;//isset($_POST['login_id'])?$_POST['login_id']:'0'; $bill_id = isset($_POST['bill_id'])?$_POST['bill_id']:'0'; $company_id = isset($_POST['company_id'])?$_POST['company_id']:'0'; if($login_id>0){ if($bill_id>0||$company_id>0){ $sql = "SELECT c.* FROM contractor c INNER JOIN company_contractor cc ON cc.contractor_id = c.id AND cc.company_id = ".$company_id." WHERE c.deleted='0'"; }else{ $sql = "SELECT c.* FROM contractor c INNER JOIN user_bills ub ON ub.user_id = ".$login_id." INNER JOIN bills b ON b.id = ub.bill_id INNER JOIN company_contractor cc ON cc.contractor_id = c.id AND cc.company_id = b.company_id WHERE c.deleted='0' GROUP BY c.id"; } }else{ $sql = "SELECT * FROM contractor WHERE deleted='0'"; } $sth=$this->database->prepare($sql); $sth->execute(); return $sth->fetchAll(); } public function getOrderTypes($incoming=0,$client_id=0,$ajax=false){ $login_id = $client_id;//isset($_POST['login_id'])?$_POST['login_id']:'0'; $bill_id = isset($_POST['bill_id'])?$_POST['bill_id']:'0'; $company_id = isset($_POST['company_id'])?$_POST['company_id']:'0'; if($login_id>0){ if($bill_id>0||$company_id>0){ $sql = "SELECT ot.* FROM order_types ot INNER JOIN company_order_types cot ON cot.order_type_id = ot.id AND cot.company_id = ".$company_id." WHERE ot.deleted='0' AND ot.incoming='".$incoming."'"; }else{ $sql = "SELECT ot.* FROM order_types ot INNER JOIN user_bills ub ON ub.user_id = ".$login_id." INNER JOIN bills b ON b.id = ub.bill_id INNER JOIN company_order_types cot ON cot.order_type_id = ot.id AND cot.company_id = b.company_id WHERE ot.deleted='0' AND ot.incoming='".$incoming."' GROUP BY ot.id"; //echo $sql;exit; } }else{ $sql = "SELECT * FROM order_types WHERE deleted='0' AND incoming='".$incoming."'"; } $sth=$this->database->prepare($sql); $sth->execute(); return $sth->fetchAll(); } public function order_create($client_id=0,$ajax=false){ $login = 'API'; $create_user_id = $client_id; $bill_id = isset($_POST['bill_id'])?intval($_POST['bill_id']):0; $order_date = isset($_POST['order_date'])?date("Y-m-d H:i:s",strtotime($_POST['order_date'].' 00:00:00')):date("Y-m-d H:i:s"); $id = isset($_POST['id'])?$_POST['id']:0; $incoming = isset($_POST['incoming'])?str_replace(',','.',trim($_POST['incoming'])):0; $consumption = isset($_POST['consumption'])?str_replace(',','.',trim($_POST['consumption'])):0; $contractor_id = isset($_POST['contractor_id'])?intval($_POST['contractor_id']):0; $order_type_id = isset($_POST['order_type_id'])?intval($_POST['order_type_id']):0; $comment = isset($_POST['comment'])?$_POST['comment']:''; if (date("Y",strtotime($order_date))<2015){ $error = 100; $error_msg = "Дата введена не правильно"; return array($error,$error_msg,0); } if ($bill_id==0){ $error = 101; $error_msg = "Счет не выбран"; return array($error,$error_msg,0); } if ($incoming>0&&$consumption>0){ $error = 102; $error_msg = "Приход и расход не могут быть заполнены одновременно"; return array($error,$error_msg,0); } if($incoming>0){ $incoming_type = 1; }else{ $incoming_type = 0; } if ($incoming_type==1&&$order_type_id==0){ $error = 103; $error_msg = "Выберите тип ордера"; return array($error,$error_msg,0); } if ($contractor_id==0){ $error = 104; $error_msg = "Выберите контрагента"; return array($error,$error_msg,0); } if($incoming_type==1){ if (!filter_var($incoming, FILTER_VALIDATE_FLOAT)){ $error = 105; $error_msg = "Сумма прихода указана не верно"; return array($error,$error_msg,0); } if ($incoming<=0){ $error = 106; $error_msg = "Сумма должна быть больше нуля"; return array($error,$error_msg,0); } }else{ if (!filter_var($consumption, FILTER_VALIDATE_FLOAT)){ $error = 107; $error_msg = "Сумма расхода указана не верно"; return array($error,$error_msg,0); } if ($consumption<=0){ $error = 108; $error_msg = "Сумма должна быть больше нуля"; return array($error,$error_msg,0); } } if($incoming_type==1){ $consumption = 0; $order_text = 'приходный'; } if($incoming_type==0){ $incoming = 0; $order_text = 'расходный'; } if($id==0){ $insert_id = $this->database->insert('orders',array( 'bill_id' =>$bill_id, 'create_user_id' =>$create_user_id, 'order_date' =>$order_date, 'contractor_id' =>$contractor_id, 'incoming' =>$incoming, 'consumption' =>$consumption, 'order_type_id' =>$order_type_id, 'reduction_id' =>0, 'comment' =>$comment )); if($insert_id>0){ $this->database->query("UPDATE bills SET balance=balance+'".($incoming-$consumption)."' WHERE deleted='0' AND id=".$bill_id); $this->database->log('Пользователь '.$login.' создал '.$order_text.' ордер '.$insert_id,get_class($this).'->'.__FUNCTION__); $error = 0; $error_msg = "Сумма должна быть больше нуля"; return array($error,$error_msg,$insert_id); //ajax::success(true); }else{ $error = 109; $error_msg = "Ошибка создания ордера"; return array($error,$error_msg,0); } }else{ $sth = $this->database->query("SELECT * FROM orders WHERE id=".$id); $order_before = $sth->fetchAll(); $insert_id = $this->database->update('orders',array( 'bill_id' =>$bill_id, 'edit_user_id' =>$create_user_id, 'order_date' =>$order_date, 'contractor_id' =>$contractor_id, 'incoming' =>$incoming, 'consumption' =>$consumption, 'order_type_id' =>$order_type_id, 'comment' =>$comment ),"id=".$id." AND reduction_id='0'"); if($insert_id>0){ $this->database->query("UPDATE bills SET balance=balance+'".(($incoming-$consumption)-($order_before[0]['incoming']-$order_before[0]['consumption']))."' WHERE deleted='0' AND id=".$order_before[0]['bill_id']); $this->database->log('Пользователь '.$login.' изменил '.$order_text.' ордер '.$insert_id,get_class($this).'->'.__FUNCTION__); $error = 0; $error_msg = "Сумма должна быть больше нуля"; return array($error,$error_msg,$insert_id); //ajax::success(true); }else{ $error = 110; $error_msg = "Ошибка редактирования ордера"; return array($error,$error_msg,0); } } } }
Save
cmd:
run