function sync_products($vid = NULL, $cid = NULL) { global $conn; connect_to_db(); $sql = "\nSELECT c.channel_id, c.channel, c.products_api, s.store_id, s.store_name, s.api_key, s.api_password, s.access_token, v.vendor\n FROM store AS s\n JOIN vendor AS v ON s.vendor_id = v.vendor_id\n JOIN channel AS c ON s.channel_id = c.channel_id\n WHERE 1\n"; if ($vid && preg_match('/^\\d+$/', $vid)) { $sql .= ' AND s.vendor_id=' . $vid; } if ($cid && preg_match('/^\\d+$/', $cid)) { $sql .= ' AND s.channel_id=' . $cid; } // actually, Vend's API can be accessed using a private access-token without expiration! $query = mysqli_query($conn, $sql); while ($row = mysqli_fetch_assoc($query)) { extract($row); $api_url = str_replace(['{STORE_NAME}', '{API_KEY}', '{API_PASSWORD}', '{ACCESS_TOKEN}'], [$store_name, $api_key, $api_password, $access_token], $products_api); $store = new stdClass(); $store->store_name = $store_name; $store->vendor = $vendor; $store->channel = $channel; $store->api_url = $api_url; $stores[$store_id] = $store; } $stores_apis = array_combine(array_keys($stores), array_map(function ($x) { return $x->api_url; }, $stores)); $api_results = fetch_products($stores_apis); $results = []; foreach ($stores as $store_id => $store) { $result = new stdClass(); $result->store_name = $store->store_name; $result->vendor = $store->vendor; $result->channel = $store->channel; if (!empty($api_results[$store_id])) { $channel_products = json_decode($api_results[$store_id]); $products = translate_channel_products($store->channel, $channel_products); save_products($store_id, $products); $result->status = 'succeeded'; } else { $result->status = 'failed'; } $results[$store_id] = $result; } return $results; }
$handle->bindParam(':tme', $tme); $handle->bindParam(':code', $code); $handle->bindParam(':create_id', $prenote->id_employee, PDO::PARAM_INT); $handle->bindParam(':update_id', $prenote->id_employee, PDO::PARAM_INT); $handle->bindParam(':id_employee', $prenote->id_employee, PDO::PARAM_INT); $handle->bindParam(':total', $prenote->total); $handle->bindParam(':narticles', $prenote->narticles); $handle->bindParam(':reference', $prenote->clientName); $handle->bindParam(':comment', $prenote->cotizationNumber); $handle->bindValue(':type_description', 'Venta'); $handle->bindParam(':workstation', $prenote->terminal); $handle->bindParam(':customer_id', $prenote->customerUUID); $handle->bindValue(':id', '0', PDO::PARAM_INT); $handle->bindParam(':store_id', $prenote->store_id, PDO::PARAM_INT); $handle->execute(); save_products($link, $prenote_uuid, $prenote->product, $prenote->id_employee, $lastUpdate); $link->commit(); $prenote->folio = $code; $saved = true; } else { $terminalCode = ord($prenote->terminal); generate_barcode(false, $prenote->folio, $terminalCode); $saved = true; } for ($i = 0; $i < $print_times; $i++) { $isPrinted = print_ticket($prenote); } if ($isPrinted == true) { unlink($prenote->folio . ".png"); unlink($prenote->folio . ".pdf"); $printed = true;