Joywork/parse_nb_quantity.php
2026-05-22 21:21:54 +03:00

86 lines
2.8 KiB
PHP

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
// parse price 2018
if (!isset($_GET['year']) || !in_array($_GET['year'], [2018, 2019]))
exit();
$year = $_GET['year'];
$file = fopen($year.'_quantity.csv', 'r');
while (!feof($file)) {
$arr = fgetcsv($file, 1024, ';');
$j = count($arr);
if ($j>1) {
$block_name = trim($arr[2]);
$room_type_name = trim($arr[3]);
$data = [
$year.'-01-01' => $arr[4],
$year.'-02-01' => $arr[5],
$year.'-03-01' => $arr[6],
$year.'-04-01' => $arr[7],
$year.'-05-01' => $arr[8],
$year.'-06-01' => $arr[9],
$year.'-07-01' => $arr[10],
$year.'-08-01' => $arr[11],
$year.'-09-01' => $arr[12],
$year.'-10-01' => $arr[13],
$year.'-11-01' => $arr[14],
$year.'-12-01' => $arr[15],
];
$sql = "SELECT * FROM blocks WHERE name='".$block_name."' LIMIT 1";
$res = mysql_query($sql);
while ($obj = mysql_fetch_assoc($res)) {
$block_id = $obj['id'];
}
if (!$block_id) continue;
$sql = "SELECT * FROM room_types WHERE name='".$room_type_name."' LIMIT 1";
$res = mysql_query($sql);
while ($obj = mysql_fetch_assoc($res)) {
$room_type_id = $obj['id'];
}
if (!$room_type_id) continue;
foreach ($data as $month => $item) {
if ($item > 0) {
$data_sales[$block_id][$room_type_id][$month][] = $item;
}
}
}
}
fclose($file);
foreach ($data_sales as $block_id => $block_data) {
foreach ($block_data as $room_type_id => $dates) {
foreach ($dates as $date => $quantities) {
$quantity = 0;
foreach ($quantities as $quantity_item) {
$quantity += $quantity_item;
}
$sql = "SELECT id, quantity FROM nb_sales WHERE block_id='".$block_id."' AND room_type_id='".$room_type_id."' AND period='".$date."' LIMIT 1";
if($rez = mysql_query($sql)) {
if(empty($obj = mysql_fetch_assoc($rez))) {
$sql = "INSERT INTO nb_sales (block_id, room_type_id, quantity, period) VALUES ('" . $block_id . "', '" . $room_type_id . "', '" . $quantity . "', '" . $date . "')";
$err = "insert";
} else {
$sql = "UPDATE nb_sales SET quantity='" . $quantity . "' WHERE id='" . $obj['id']."'";
$err = "update";
}
if(!mysql_query($sql))
echo "ERROR: Could not able to execute $sql. " . mysqli_error();
else
echo "Records ".$err." successfully.<br>";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error();
}
}
}
}