Happy New Year!
This commit is contained in:
miroman-afk
2021-12-29 13:17:03 +03:00
parent 1a7808f75b
commit 2702c4b6e9
31 changed files with 2013 additions and 3 deletions

View File

@@ -0,0 +1,90 @@
<?php
namespace App\Commands;
use App\Component\Models\Terminal;
use App\Console\Commands\HRCCommand;
use App\Console\Commands\HRCCommandInterface;
class Import extends HRCCommand implements HRCCommandInterface {
protected $signature = 'getimport';
public function command($input, $output = null) {
$HRCPortalURL = 'https://portal.hrc.by/';
$folder = 'backup';
$terminals = Terminal::where('soft', '=', 1)->where('is_active', '=', 1)->get();
$end_date = date('m/d/Y H:i:s', strtotime($input['end_date']) + 86399);
$start_date = date('m/d/Y H:i:s', strtotime($input['start_date']));
foreach ($terminals as $terminal) {
$url = $HRCPortalURL . 'api/cloud/list?api=2.0&project_code=hrc&code=' . $terminal['key'] . '&folder=' . $folder;
$search = curl_init();
curl_setopt_array($search, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
));
$search_response = curl_exec($search);
curl_close($search);
$responses = json_decode($search_response, TRUE)['files'];
foreach ($responses as $key => $response) {
if (array_key_exists('filename', $response)) {
$fulldate = date_parse_from_format('d-m-Y-H-i-s', $response['filename']);
$fulldate = mktime($fulldate['hour'], $fulldate['minute'], $fulldate['second'], $fulldate['month'], $fulldate['day'], $fulldate['year']);
if ($fulldate >= strtotime($start_date) && $fulldate <= strtotime($end_date)) {
$out[] = date('d-m-Y-H-i-s', $fulldate);
}
}
}
if (!isset($out)) {
return [
'status' => 'success',
'message' => 'shifts not found',
];
} else {
foreach ($out as $filename) {
$date_file = date('Y-m-d', $filename);
//$path = '/backup/' . $filename . '.xml';
$path = '/Резервная%20копия/' . $date_file . '/exchange/' . $date_file . '/exchange/' . $filename . '.xml';
$download_url = $HRCPortalURL . 'api/cloud/download?api=2.0&project_code=hrc&code=' . $terminal['key'] . '&path=' . $path;
$download = curl_init();
curl_setopt_array($download, array(
CURLOPT_URL => $download_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
));
$download_response = curl_exec($download);
curl_close($download);
$file = json_decode($download_response, TRUE)['content'];
$params = array('code' => $terminal['key'], 'name' => $filename . '.xml', 'folder' => 'exchange', 'content' => $file, 'project_code' => 'hrc', 'api' => '2.0');
$upload = curl_init();
curl_setopt_array($upload, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_URL => $HRCPortalURL . 'api/cloud/upload',
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
));
$upload_response = curl_exec($upload);
curl_close($upload);
}
return [
'status' => 'success',
'start_date' => strtotime($start_date),
'end_date' => strtotime($end_date),
];
}
}
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace App\Commands;
use App\Component\Models\Dishes;
use App\Component\Models\ExchangeItems;
use App\Component\Models\ExchangeOrders;
use App\Component\Models\Terminal;
use App\Console\Commands\HRCCommand;
use App\Console\Commands\HRCCommandInterface;
class TopDishesNewYear extends HRCCommand implements HRCCommandInterface {
protected $signature = 'gettopdishesnewyear';
public function command($input, $output = null) {
$terminal = Terminal::where('soft', '=', 1)->where('is_active', '=', 1)->first();
$dirname = __DIR__ . "\\..\\..\\..\\Exchange\\" . $terminal['key'] . "\\";
$filename = "newyear.json";
if (!is_dir($dirname)) {
mkdir($dirname, 0777);
}
if (!file_exists($dirname . $filename)) {
$info = ExchangeItems::where('menu_code', '>', 0)
->where('real_price', '>', 1)
->where('created_at', '>=', '2021-10-01 00:00:00')
->where('created_at', '<', '2021-12-31 23:59:59')
->get()
->unique('menu_code');
$count = ExchangeItems::where('menu_code', '>', 0)->where('created_at', '>=', '2021-10-01 00:00:00')->where('created_at', '<', '2021-12-31 23:59:59')->count();
if ($count > 0) {
foreach ($info as $key => $value) {
$out[] = $value;
}
foreach ($out as $key => $item) {
$dishInfo = Dishes::where('code', '=', $item['menu_code'])
->where('legacy_code', '=', $item['dishes_code'])
// ->where('is_history', '=', 0)
->first();
$onlineDishInfo = ExchangeItems::where('menu_code', '=', $item['menu_code'])
->where('dishes_code', '=', $item['dishes_code'])
->where('created_at', '>=', '2021-10-01 00:00:00')
->where('created_at', '<', '2021-12-31 23:59:59')
->first();
$dishName = $dishInfo['name'];
$dishCount = ExchangeItems::where('menu_code', '=', $item['menu_code'])
->where('created_at', '>=', '2021-10-01 00:00:00')
->where('created_at', '<', '2021-12-31 23:59:59')
->sum('count');
$dishSum = $onlineDishInfo['real_price'] * $dishCount;
if ($dishSum > 0) {
$dishTotalCost = round(($onlineDishInfo['special_price'] * $dishCount), 2);
$dishPercent = round((100 - ((($dishSum - $dishTotalCost) * 100) / $dishSum)), 2);
$dishProfit = $dishSum - $dishTotalCost;
} else {
$dishTotalCost = 0;
$dishPercent = 0;
$dishProfit = 0;
}
$dishes[] = array('name' => $dishName, 'count' => intval($dishCount), 'sum' => intval($dishSum), 'totalCost' => intval($dishTotalCost), 'percentProffit' => $dishPercent, 'proffit' => $dishProfit);
}
for ($i = 0; $i < count($dishes); $i++) {
$sortkey[$i] = $dishes[$i]['count'];
}
arsort($sortkey);
foreach ($sortkey as $key => $key) {
$sorted[] = $dishes[$key];
}
$sorted = array_slice($sorted, 0, 3);
//End top dishes counter
//Start top day
//SELECT COUNT(`code`) AS `orders_counter`, `shift_id` FROM `exchange_orders` WHERE `is_closed` > 0 AND `opened` > '2021-01-01 00:00:00' GROUP BY `shift_id` ORDER BY `orders_counter` desc
$shifts = ExchangeOrders::where('is_closed', '>', 0)
->where('opened', '>', '2021-01-01 00:00:00')
->get()
->unique('shift_id');
foreach ($shifts as $key => $shift) {
$shift_id = $shift['shift_id'];
$shift_order_count = ExchangeOrders::where('shift_id', '=', $shift_id)->count();
$shift_order_sum = ExchangeOrders::where('shift_id', '=', $shift_id)->sum('order_sum');
$shift_date = ExchangeOrders::where('shift_id', '=', $shift_id)->first();
$tr_m = ['Нулябрь' /* для сдвига индекса на +1*/, 'Января', 'Февраля', 'Марта', 'Апреля', 'Мая', 'Июня', 'Июля', 'Августа', 'Сентября', 'Октября', 'Ноября', 'Декабря'];
$exp_date = getdate(strtotime($shift_date['opened']));
$full_datedate = sprintf(
'%d %s %d',
$exp_date['mday'],
$tr_m[$exp_date['mon']],
$exp_date['year']
);
$shift_counter[] = array('shift_id' => $shift_id, 'count' => $shift_order_count, 'date' => $full_datedate, 'sum' => $shift_order_sum);
}
for ($i = 0; $i < count($shift_counter); $i++) {
$shift_counter_sortkey[$i] = $shift_counter[$i]['count'];
}
arsort($shift_counter_sortkey);
foreach ($shift_counter_sortkey as $key => $key) {
$shift_counter_sorted[] = $shift_counter[$key];
}
$shift_counter_sorted = array_slice($shift_counter_sorted, 0, 1);
//End top day
//Return data
$data = [
'status' => 'success',
'count' => $count,
'dishes' => $sorted,
'top_shift' => $shift_counter_sorted,
];
$handle = fopen($dirname . $filename, 'w+');
fputs($handle, json_encode($data));
fclose($handle);
return $data;
} else {
return [
'status' => 'success',
'count' => 0,
'dishes' => [],
];
}
} else {
$data = json_decode(file_get_contents($dirname . $filename), true);
return $data;
}
}
}