-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
143 lines (135 loc) · 4.59 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
*
* API de pruebas
*
* @category API
* @package Prueba para Jorge emilio Morales
* @version 1.0
* @author Hernan Castro <[email protected]>
* @copyright Copyright (c) 2020, Hernan Alberto Castro Paniagua <[email protected]>
*
*/
class api
{
private $mysqli;
private $SERVER_DB;
private $USER;
private $PASS;
private $DATABASE;
public function __construct()
{
$this->SERVER_DB = 'localhost';
$this->USER = 'root';
$this->PASS = '';
$this->DATABASE = 'test_001';
}
private function set_conn()
{
$this->mysqli = new mysqli(
$this->SERVER_DB,
$this->USER,
$this->PASS,
$this->DATABASE
);
if ($this->mysqli->connect_error) {
die('Error : (' . $this->mysqli->connect_errno . ') ' . $this->mysqli->connect_error);
}
}
private function exec_query($query, $retID = false)
{
try {
if (FALSE) :
echo "\n" . $query . "\n";
endif;
$this->set_conn();
mysqli_set_charset($this->mysqli, 'utf8');
$results = $this->mysqli->query($query);
if ($this->mysqli->errno > 0) :
return array(
'msg' => $this->mysqli->error,
'estado' => $this->mysqli->errno,
'origin' => "mysqli"
);
endif;
$res_id = $this->mysqli->insert_id;
$this->mysqli->close();
unset($this->mysqli);
if ($retID) :
return $res_id;
else :
if ($results->num_rows <= 1) :
return $results->fetch_assoc();
else :
$arrRet = array();
while ($row = $results->fetch_assoc()) :
$arrRet[] = $row;
endwhile;
return $arrRet;
endif;
endif;
} catch (Exception $e) {
echo "Error in api->exec_query: ";
var_dump($e);
throw $e;
}
}
public function get_products_rows()
{
$table_prods = $this->exec_query('SELECT * FROM productos;');
$table_html = '';
foreach ($table_prods as $prod) :
$table_html .=
'<tr data-row_product="' . $prod['idRecepDocumentosDeta'] . '">' .
' <th data-nat="' . $prod['naturalezaProducto'] . '" scope="row">' . (($prod['naturalezaProducto'] == '0' ? 'Bien' : 'Servicio')) . '</th>' .
' <td class="code">' . $prod['CodigoComercial'] . '</td>' .
' <td class="uni">' . $prod['UnidadMedida'] . '</td>' .
' <td class="uni_com">' . $prod['UnidadMedidaComercial'] . '</td>' .
' <td class="detail">' . $prod['Detalle'] . '</td>' .
' <td class="amount">' . $prod['PrecioUnitario'] . '</td>' .
' <td class="imp">' . $prod['ImpuestoTarifa'] . '</td>' .
'</tr>';
endforeach;
return $table_html;
}
public function get_products_units()
{
$units = $this->exec_query('SELECT UnidadMedida FROM productos GROUP BY UnidadMedida;');
$options_html = '';
foreach ($units as $unit) :
$options_html .=
'<option value="' . $unit['UnidadMedida'] . '">' . $unit['UnidadMedida'] . '</option>';
endforeach;
return $options_html;
}
public function update_product($id, $nat, $uni, $det, $amo)
{
try {
$sql = "UPDATE productos SET naturalezaProducto='" . $nat . "', UnidadMedida='" . $uni . "', Detalle='" . $det . "', PrecioUnitario=" . $amo . " WHERE idRecepDocumentosDeta = " . $id . ";";
$this->exec_query($sql, true);
$ret = array(
'status' => 1
);
return json_encode($ret, JSON_UNESCAPED_UNICODE);
} catch (Exception $e) {
$ret = array(
'status' => 0
);
return json_encode($ret, JSON_UNESCAPED_UNICODE);
}
}
}
if (isset($_POST['action'])) :
$api = new api();
switch ($_POST['action']):
case 'save_prod':
header('Content-Type: application/json');
$id = $_POST['id'];
$nat = $_POST['nat'];
$uni = $_POST['uni'];
$det = $_POST['det'];
$amo = $_POST['amo'];
echo $api->update_product($id, $nat, $uni, $det, $amo);
break;
endswitch;
endif;