370 lines
16 KiB
PHP
370 lines
16 KiB
PHP
<?php
|
||
|
||
namespace App\Component\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Support\Facades\Log;
|
||
|
||
class ExchangeOrders extends Model
|
||
{
|
||
protected $table = 'exchange_orders';
|
||
|
||
public static function getReturnedItems($order_id, $shift_id)
|
||
{
|
||
$action = ExchangeActions::where('order_code', $order_id)
|
||
->where('shift_id', $shift_id)
|
||
->where('action_type', 17)
|
||
->orderBy('time', 'desc')
|
||
->first();
|
||
$returned_order_id = intval($action['value']);
|
||
$start_order_items = ExchangeActions::where('order_code', $returned_order_id)
|
||
->where('shift_id', $shift_id)
|
||
->where('action_type', 2)
|
||
->get();
|
||
$start_order_info = $cancel_order_info = [];
|
||
foreach ($start_order_items as $start_order_item) {
|
||
$item = ExchangeItems::where('order_code', $returned_order_id)
|
||
->where('shift_id', $shift_id)
|
||
->where('menu_code', intval($start_order_item['more']))
|
||
->first();
|
||
$start_order_info[] = array(
|
||
'code' => intval($start_order_item['more']),
|
||
'count' => Base::tofloat($start_order_item['value']),
|
||
'sale_price' => $item['sale_price'],
|
||
'special_price' => $item['special_price']
|
||
);
|
||
}
|
||
$canceled_order_items_count = ExchangeActions::where('order_code', $returned_order_id)
|
||
->where('shift_id', $shift_id)
|
||
->where('action_type', 22)
|
||
->count();
|
||
if ($canceled_order_items_count > 0) {
|
||
$canceled_order_items = ExchangeActions::selectRaw('SUM(value * 1) AS count, order_position')
|
||
->where('shift_id', $shift_id)
|
||
->where('order_code', $returned_order_id)
|
||
->where('action_type', 22)
|
||
->groupBy('order_position')
|
||
->get();
|
||
|
||
foreach ($canceled_order_items as $canceled_order_item) {
|
||
$item = ExchangeItems::where('order_code', $returned_order_id)
|
||
->where('shift_id', $shift_id)
|
||
->where('menu_code', $canceled_order_item['order_position'])
|
||
->first();
|
||
$cancel_order_info[] = array(
|
||
'code' => $canceled_order_item['order_position'],
|
||
'count' => $canceled_order_item['count'],
|
||
'sale_price' => $item['sale_price'],
|
||
'special_price' => $item['special_price']
|
||
);
|
||
}
|
||
foreach ($start_order_info as $start_order) {
|
||
foreach ($cancel_order_info as $cancel_order) {
|
||
if ($start_order['code'] == $cancel_order['code']) {
|
||
$start_order['count'] = $start_order['count'] - $cancel_order['count'];
|
||
}
|
||
}
|
||
$item = ExchangeItems::where('order_code', $returned_order_id)
|
||
->where('shift_id', $shift_id)
|
||
->where('menu_code', $start_order['code'])
|
||
->first();
|
||
$end_order[] = array(
|
||
'code' => $start_order['code'],
|
||
'count' => $start_order['count'],
|
||
'sale_price' => $item['sale_price'],
|
||
'special_price' => $item['special_price']
|
||
);
|
||
}
|
||
} else {
|
||
$end_order = $start_order_info;
|
||
}
|
||
|
||
return $end_order;
|
||
}
|
||
|
||
/**
|
||
* Get order status
|
||
*/
|
||
public static function getOrderStatus($order_id, $shift_id)
|
||
{
|
||
$order_status = '';
|
||
$order = self::where('code', $order_id)
|
||
->where('shift_id', $shift_id)
|
||
->first();
|
||
$order_online = 0;
|
||
$count_online_open = ExchangeActions::where('order_code', $order['code'])
|
||
->where('shift_id', $shift_id)
|
||
->where('action_type', 45)
|
||
->where('more', '<>', '0')
|
||
->count();
|
||
$count_online_close = ExchangeActions::where('order_code', $order['code'])
|
||
->where('shift_id', $shift_id)
|
||
->where('action_type', 6)
|
||
->count();
|
||
if ($count_online_open > 0 && $count_online_close > 0) {
|
||
$order_online = $order['order_sum'];
|
||
}
|
||
$order_double = 0;
|
||
$count_double_is_close = self::where('code', $order['code'])
|
||
->where('shift_id', $shift_id)
|
||
->where('is_closed', 1)
|
||
->where('is_deleted', 0)
|
||
->where('is_returned', 0)
|
||
->count();
|
||
$count_double_close = ExchangeActions::where('order_code', $order['code'])
|
||
->where('shift_id', $shift_id)
|
||
->where('action_type', 6)
|
||
->count();
|
||
|
||
if ($count_double_is_close > 0 && $count_double_close < 1) {
|
||
$order_double = $order['order_sum'];
|
||
}
|
||
if ($order['is_returned'] > 0) {
|
||
$action = ExchangeActions::where('order_code', $order_id)
|
||
->where('shift_id', $shift_id)
|
||
->where('action_type', 17)
|
||
->orderBy('time', 'desc')
|
||
->first();
|
||
$returned_order_id = intval($action['value']);
|
||
$canceled_order_items_count = ExchangeActions::where('order_code', $returned_order_id)
|
||
->where('shift_id', $shift_id)
|
||
->where('action_type', 22)
|
||
->count();
|
||
if ($canceled_order_items_count > 0) {
|
||
$order_status = 'Возвращен частично';
|
||
} else {
|
||
$order_status = 'Возвращен';
|
||
}
|
||
} elseif ($order['is_deleted'] > 0 && $order['check_number'] < 1) {
|
||
$order_status = 'Удален';
|
||
} elseif ($order['is_deleted'] > 0 && $order['check_number'] > 0) {
|
||
$order_status = 'Аннулирован';
|
||
} elseif ($order['is_waited'] > 0 && $order['is_closed'] < 1 && $order['place_name'] != "ДОСТАВКА") {
|
||
$order_status = 'В ожидании';
|
||
} elseif ($order_online > 0) {
|
||
$order_status = 'Оплачен онлайн';
|
||
} elseif ($order_double > 0) {
|
||
$order_status = 'Оплачен(*)';
|
||
} elseif ($order['place_name'] == "ДОСТАВКА") {
|
||
$order_status = 'Доставка';
|
||
} else {
|
||
$order_status = 'Оплачен';
|
||
}
|
||
|
||
return $order_status;
|
||
}
|
||
|
||
/**
|
||
* Get returned order id
|
||
*/
|
||
public static function getReturnedOrderId($order_id, $shift_id)
|
||
{
|
||
$order = self::where('code', $order_id)
|
||
->where('shift_id', $shift_id)
|
||
->first();
|
||
if ($order['is_returned'] > 0) {
|
||
$action = ExchangeActions::where('order_code', $order_id)
|
||
->where('action_type', 17)
|
||
->first();
|
||
$returned_order_id = intval($action['value']);
|
||
}
|
||
return $returned_order_id;
|
||
}
|
||
|
||
/**
|
||
* Get order info
|
||
*/
|
||
public static function getInfo($order_id, $shift_id)
|
||
{
|
||
$order = self::where('code', $order_id)
|
||
->where('shift_id', $shift_id)
|
||
->first();
|
||
Log::debug($order_id . ' ' . $shift_id);
|
||
$open_time = $order['opened'];
|
||
if ($order['closed'] == '0000-00-00 00:00:00') {
|
||
$close_time = false;
|
||
} else {
|
||
$close_time = $order['closed'];
|
||
}
|
||
|
||
$who_open = Staff::getName($order['who_open']);
|
||
if ($order['who_close'] == 0) {
|
||
$who_close = false;
|
||
} else {
|
||
$who_close = Staff::getName($order['who_close']);
|
||
}
|
||
$order_client_count = $order['clients_count'];
|
||
$items = ExchangeItems::where('order_code', $order_id)
|
||
->where('shift_id', $shift_id)
|
||
->where('parent_id', 0)
|
||
->get();
|
||
$deleted_items = ExchangeDeleted::where('order_code', $order_id)
|
||
->where('shift_id', $shift_id)
|
||
->get();
|
||
$order_status = self::getOrderStatus($order_id, $shift_id);
|
||
$returned_items = [];
|
||
$returned_method = false;
|
||
$returned_sum = $returned_count = 0;
|
||
$deleted_sum = $deleted_count = 0;
|
||
|
||
if ($order_status == "Возвращен" || $order_status == "Возвращен частично") {
|
||
$returned_items = self::getReturnedItems($order['code'], $shift_id);
|
||
foreach ($returned_items as $returned_item) {
|
||
$returned_sum += round(abs($returned_item['sale_price']) * $returned_item['count'], 2);
|
||
$returned_count += $returned_item['count'];
|
||
}
|
||
$returned_id = self::getReturnedOrderId($order_id, $shift_id);
|
||
$returned_at = self::where('code', $returned_id)
|
||
->where('shift_id', $shift_id)
|
||
->first();
|
||
$returned_method = array(
|
||
'cash' => abs($returned_at['cash']),
|
||
'credit' => abs($returned_at['credit']),
|
||
);
|
||
}
|
||
$order_cash = $order['cash'];
|
||
$order_credit = $order['credit'];
|
||
$order_clearing = $order['clearing'];
|
||
$order_presale = $order['presale'];
|
||
$order_self = $order['self'];
|
||
$order_online = 0;
|
||
$count_online_open = ExchangeActions::where('order_code', $order['code'])
|
||
->where('shift_id', $shift_id)
|
||
->where('action_type', 45)
|
||
->where('more', '<>', '0')
|
||
->count();
|
||
$count_online_close = ExchangeActions::where('order_code', $order['code'])
|
||
->where('shift_id', $shift_id)
|
||
->where('action_type', 6)
|
||
->count();
|
||
if ($count_online_open > 0 && $count_online_close > 0) {
|
||
$order_online = $order['order_sum'];
|
||
}
|
||
$order_info = [];
|
||
$amount = $full_price = 0;
|
||
foreach ($items as $key => $item) {
|
||
$realPrice = $item['real_price'] * $item['count'] * $item['cof'];
|
||
$salePrice = $item['sale_price'] * $item['count'];
|
||
if ($realPrice > 0) {
|
||
$discount = $salePrice / $realPrice;
|
||
$discount = (1 - $discount) * 100;
|
||
} else {
|
||
$discount = 0;
|
||
}
|
||
$order_info[] = array(
|
||
'id' => $key + 1,
|
||
'code' => $item['menu_code'],
|
||
'name' => Dishes::getName($item['menu_code']),
|
||
'count' => floatval($item['count']),
|
||
'returned_count' => 0,
|
||
'returned_sum' => 0,
|
||
'deleted_count' => 0,
|
||
'deleted_sum' => 0,
|
||
'price' => round($item['real_price'], 2),
|
||
'sale_price' => round($item['sale_price'], 2),
|
||
'special_price' => round($item['special_price'], 2),
|
||
'cof' => round($item['cof'], 2),
|
||
'unit' => Units::getName($item['units_id']),
|
||
'amount' => round($item['sale_price'] * $item['count'], 2),
|
||
'discount' => round($discount),
|
||
'total_count' => floatval($item['count']),
|
||
'profit' => floatval($item['count']) * round($item['sale_price'], 2) - floatval($item['count']) * round($item['special_price'], 2)
|
||
);
|
||
|
||
foreach ($deleted_items as $deleted_item) {
|
||
if ($item['menu_code'] == $deleted_item['menu_code']) {
|
||
$order_info[$key]['deleted_count'] = $deleted_item['count'];
|
||
$order_info[$key]['total_count'] = $order_info[$key]['total_count'] - $deleted_item['count'];
|
||
$order_info[$key]['deleted_sum'] = abs($deleted_item['sale_price']) * $deleted_item['count'];
|
||
$order_info[$key]['profit'] = $order_info[$key]['total_count'] * round($item['sale_price'], 2) - $order_info[$key]['total_count'] * round($item['special_price'], 2);
|
||
$deleted_count += $deleted_item['count'];
|
||
$deleted_sum += abs($deleted_item['sale_price']) * $deleted_item['count'];
|
||
}
|
||
}
|
||
|
||
$amount += round($item['sale_price'] * $item['count'], 2);
|
||
$full_price += round($realPrice, 2);
|
||
if ($order_status == "Возвращен" || $order_status == "Возвращен частично") {
|
||
foreach ($returned_items as $returned_item) {
|
||
if ($item['menu_code'] == $returned_item['code']) {
|
||
$order_info[$key]['returned_count'] = $returned_item['count'];
|
||
$order_info[$key]['total_count'] = $order_info[$key]['total_count'] - $returned_item['count'];
|
||
$order_info[$key]['profit'] = $order_info[$key]['total_count'] * round($item['sale_price'], 2) - $order_info[$key]['total_count'] * round($item['special_price'], 2);
|
||
$order_info[$key]['returned_sum'] = abs($returned_item['sale_price']) * $returned_item['count'];
|
||
$order_info[$key]['amount'] = round($item['sale_price'] * $item['count'], 2) - (abs($returned_item['sale_price']) * $returned_item['count']);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
$key = count($order_info);
|
||
$items_code = [];
|
||
foreach ($order_info as $order_info_item) {
|
||
$items_code[] = $order_info_item['code'];
|
||
}
|
||
foreach ($deleted_items as $deleted_item) {
|
||
if (in_array($deleted_item['menu_code'], $items_code)) {
|
||
break;
|
||
} else {
|
||
$key++;
|
||
$realPrice = $deleted_item['real_price'] * $deleted_item['count'] * $deleted_item['cof'];
|
||
$salePrice = $deleted_item['sale_price'] * $deleted_item['count'];
|
||
if ($realPrice > 0) {
|
||
$discount = $salePrice / $realPrice;
|
||
$discount = (1 - $discount) * 100;
|
||
} else {
|
||
$discount = 0;
|
||
}
|
||
$order_info[] = array(
|
||
'id' => $key,
|
||
'code' => $deleted_item['menu_code'],
|
||
'name' => Dishes::getName($deleted_item['menu_code']),
|
||
'count' => 0,
|
||
'returned_count' => 0,
|
||
'returned_sum' => 0,
|
||
'deleted_count' => floatval($deleted_item['count']),
|
||
'deleted_sum' => abs($deleted_item['sale_price']) * $deleted_item['count'],
|
||
'price' => round($deleted_item['real_price'], 2),
|
||
'sale_price' => round($deleted_item['sale_price'], 2),
|
||
'special_price' => Dishes::getSpecialPrice($deleted_item['menu_code']),
|
||
'cof' => 1,
|
||
'unit' => Units::getName($deleted_item['units_id']),
|
||
'amount' => 0,
|
||
'discount' => round($discount),
|
||
'total_count' => 0,
|
||
'profit' => 0
|
||
);
|
||
$deleted_count += floatval($deleted_item['count']);
|
||
$deleted_sum += abs($deleted_item['sale_price']) * $deleted_item['count'];
|
||
}
|
||
}
|
||
$order = array(
|
||
'order_id' => $order_id,
|
||
'place' => Base::data_decode($order['place_name']),
|
||
'table' => $order['table_name'],
|
||
'opened' => $open_time,
|
||
'closed' => $close_time,
|
||
'who_open' => $who_open,
|
||
'who_close' => $who_close,
|
||
'order_status' => $order_status,
|
||
'client_count' => $order_client_count,
|
||
'amount' => $amount,
|
||
'full_price' => $full_price,
|
||
'cash' => $order_cash,
|
||
'credit' => $order_credit,
|
||
'clearing' => $order_clearing,
|
||
'self' => $order_self,
|
||
'online' => $order_online,
|
||
'presale' => $order_presale,
|
||
'items' => $order_info,
|
||
'returned_items' => $returned_items,
|
||
'returned_count' => $returned_count,
|
||
'returned_sum' => $returned_sum,
|
||
'returned_method' => $returned_method,
|
||
'deleted_count' => $deleted_count,
|
||
'deleted_sum' => $deleted_sum
|
||
);
|
||
return $order;
|
||
}
|
||
} |