260 lines
9.9 KiB
PHP
260 lines
9.9 KiB
PHP
<?php
|
||
/**
|
||
* ThinkShop商城系统 - 团队十年电商经验汇集巨献!
|
||
* =========================================================
|
||
* Copy right 2019-2029 成都云之牛科技有限公司, 保留所有权利。
|
||
* ----------------------------------------------
|
||
* 官方网址: https://www.cdcloudshop.com
|
||
|
||
* =========================================================
|
||
*/
|
||
|
||
namespace addon\cashier\model;
|
||
|
||
use app\model\BaseModel;
|
||
use app\model\order\OrderRefund;
|
||
|
||
/**
|
||
* @author Administrator
|
||
*/
|
||
class Cashier extends BaseModel
|
||
{
|
||
private $path = 'addon/cashier/source';
|
||
|
||
/**
|
||
* 查询班次内数据
|
||
* @param $site_id
|
||
* @param $store_id
|
||
*/
|
||
public function getShiftsData($site_id, $store_id){
|
||
$last_info = model('change_shifts_record')->getFirstData([ ['site_id', '=', $site_id], ['store_id', '=', $store_id] ], 'end_time', 'end_time desc');
|
||
$start_time = empty($last_info) ? 0 : $last_info['end_time'];
|
||
$end_time = time();
|
||
|
||
$order_condition = [
|
||
['site_id', '=', $site_id],
|
||
['store_id', '=', $store_id],
|
||
['order_scene', '=', 'cashier'],
|
||
['order_status', '=', 10],
|
||
['finish_time', 'between', [$start_time, $end_time] ]
|
||
];
|
||
$order_condition[5] = ['cashier_order_type', '=', 'goods'];
|
||
$billing_stat = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
|
||
|
||
$order_condition[5] = ['cashier_order_type', '=', 'card'];
|
||
$card_stat = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
|
||
|
||
$order_condition[5] = ['cashier_order_type', '=', 'recharge'];
|
||
$recharge_stat = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
|
||
|
||
$order_condition[5] = ['pay_type', '=', 'cash'];
|
||
$cash = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
|
||
|
||
$order_condition[5] = ['pay_type', '=', 'alipay'];
|
||
$alipay = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
|
||
|
||
$order_condition[5] = ['pay_type', '=', 'wechatpay'];
|
||
$wechatpay = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
|
||
|
||
$order_condition[5] = ['pay_type', '=', 'own_wechatpay'];
|
||
$own_wechatpay = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
|
||
|
||
$order_condition[5] = ['pay_type', '=', 'own_alipay'];
|
||
$own_alipay = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
|
||
|
||
$order_condition[5] = ['pay_type', '=', 'own_pos'];
|
||
$own_pos = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
|
||
|
||
$refund_condition = [
|
||
['o.site_id', '=', $site_id],
|
||
['o.store_id', '=', $store_id],
|
||
['o.order_scene', '=', 'cashier'],
|
||
['og.refund_status', '=', OrderRefund::REFUND_COMPLETE ],
|
||
['og.refund_time', 'between', [$start_time, $end_time] ]
|
||
];
|
||
$refund_stat = model('order_goods')->getInfo($refund_condition, 'count(og.order_goods_id) as num, ifnull(sum(refund_pay_money), 0) as money', 'og', [
|
||
['order o', 'o.order_id = og.order_id', 'inner']
|
||
]);
|
||
|
||
$data = [
|
||
'start_time' => $start_time,
|
||
'end_time' => $end_time,
|
||
'billing_count' => $billing_stat['num'],
|
||
'billing_money' => $billing_stat['pay_money'],
|
||
'buycard_count' => $card_stat['num'],
|
||
'buycard_money' => $card_stat['pay_money'],
|
||
'recharge_count' => $recharge_stat['num'],
|
||
'recharge_money' => $recharge_stat['pay_money'],
|
||
'refund_count' => $refund_stat['num'],
|
||
'refund_money' => $refund_stat['money'],
|
||
'cash_count' => $cash['num'],
|
||
'cash' => $cash['pay_money'],
|
||
'alipay_count' => $alipay['num'],
|
||
'alipay' => $alipay['pay_money'],
|
||
'wechatpay_count' => $wechatpay['num'],
|
||
'wechatpay' => $wechatpay['pay_money'],
|
||
'own_wechatpay_count' => $own_wechatpay['num'],
|
||
'own_wechatpay' => $own_wechatpay['pay_money'],
|
||
'own_alipay_count' => $own_alipay['num'],
|
||
'own_alipay' => $own_alipay['pay_money'],
|
||
'own_pos_count' => $own_pos['num'],
|
||
'own_pos' => $own_pos['pay_money'],
|
||
];
|
||
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 交接班
|
||
* @param $user_info
|
||
* @param $site_id
|
||
* @param $store_id
|
||
*/
|
||
public function changeShifts($user_info, $site_id, $store_id) {
|
||
$data = $this->getShiftsData($site_id, $store_id);
|
||
$data = array_merge($data, [
|
||
'site_id' => $site_id,
|
||
'store_id' => $store_id,
|
||
'uid' => $user_info['uid']
|
||
]);
|
||
$id = model('change_shifts_record')->add($data);
|
||
if ($id) {
|
||
return $this->success($id);
|
||
} else {
|
||
return $this->error('', '交接班数据添加失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 查询交班记录
|
||
* @param array $condition
|
||
* @param bool $field
|
||
* @param string $order
|
||
* @param int $page
|
||
* @param int $list_rows
|
||
* @param string $alias
|
||
* @param array $join
|
||
* @return array
|
||
*/
|
||
public function getchangeShiftsPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = []){
|
||
$data = model('change_shifts_record')->pageList($condition, $field, $order, $page, $list_rows, $alias, $join);
|
||
return $this->success($data);
|
||
}
|
||
|
||
/**
|
||
* 刷新收银端
|
||
* @return array
|
||
*/
|
||
public function refreshCashier()
|
||
{
|
||
try {
|
||
|
||
$path = $this->path . '/default';
|
||
$cashier_path = 'cashregister'; // 收银端生成目录
|
||
$config_path = 'cashregister/static/js'; // 收银模板文件目录
|
||
if (!is_dir($path) || count(scandir($path)) <= 3) {
|
||
return $this->error('', '未找到源码包,请检查目录文件');
|
||
}
|
||
|
||
if (is_dir($cashier_path)) {
|
||
// 先将之前的文件删除
|
||
if (count(scandir($cashier_path)) > 1) deleteDir($cashier_path);
|
||
} else {
|
||
// 创建收银目录
|
||
mkdir($cashier_path, intval('0777', 8), true);
|
||
}
|
||
|
||
// 将原代码包拷贝到收银目录下
|
||
recurseCopy($path, $cashier_path);
|
||
$this->copyFile($config_path);
|
||
file_put_contents($cashier_path . '/refresh.log', time());
|
||
return $this->success();
|
||
} catch (\Exception $e) {
|
||
return $this->error('', $e->getMessage() . $e->getLine());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 替换配置信息,API请求域名地址、图片、地图密钥等
|
||
* @param $source_path
|
||
* @param string $domain
|
||
*/
|
||
private function copyFile($source_path, $domain = __ROOT__)
|
||
{
|
||
$files = scandir($source_path);
|
||
foreach ($files as $path) {
|
||
if ($path != '.' && $path != '..') {
|
||
$temp_path = $source_path . '/' . $path;
|
||
if (file_exists($temp_path)) {
|
||
if (preg_match("/(index.)(\w{8})(.js)$/", $temp_path)) {
|
||
$content = file_get_contents($temp_path);
|
||
$content = $this->paramReplace($content, $domain);
|
||
file_put_contents($temp_path, $content);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 参数替换
|
||
* @param $string
|
||
* @param string $domain
|
||
* @return string|string[]|null
|
||
*/
|
||
private function paramReplace($string, $domain = __ROOT__)
|
||
{
|
||
$patterns = [
|
||
'/\{\{\$baseUrl\}\}/',
|
||
'/\{\{\$imgDomain\}\}/',
|
||
];
|
||
$replacements = [
|
||
$domain,
|
||
$domain
|
||
];
|
||
$string = preg_replace($patterns, $replacements, $string);
|
||
return $string;
|
||
}
|
||
|
||
/**
|
||
* 下载收银端uniapp源码
|
||
* @return array
|
||
*/
|
||
public function downloadOs(){
|
||
try {
|
||
$source_file_path = $this->path . '/os';
|
||
if (!is_dir($source_file_path) || count(scandir($source_file_path)) <= 3) {
|
||
return $this->error('', '未找到源码包,请检查目录文件');
|
||
}
|
||
$file_arr = getFileMap($source_file_path);
|
||
|
||
if (!empty($file_arr)) {
|
||
$zipname = 'cashier_os_' . date('YmdHi') . '.zip';
|
||
$zip = new \ZipArchive();
|
||
$res = $zip->open($zipname, \ZipArchive::CREATE);
|
||
if ($res === TRUE) {
|
||
foreach ($file_arr as $file_path => $file_name) {
|
||
if (is_dir($file_path)) {
|
||
$file_path = str_replace($source_file_path . '/', '', $file_path);
|
||
$zip->addEmptyDir($file_path);
|
||
} else {
|
||
$zip_path = str_replace($source_file_path . '/', '', $file_path);
|
||
$zip->addFile($file_path, $zip_path);
|
||
}
|
||
}
|
||
$zip->close();
|
||
|
||
header("Content-Type: application/zip");
|
||
header("Content-Transfer-Encoding: Binary");
|
||
header("Content-Length: " . filesize($zipname));
|
||
header("Content-Disposition: attachment; filename=\"" . basename($zipname) . "\"");
|
||
readfile($zipname);
|
||
@unlink($zipname);
|
||
}
|
||
}
|
||
return $this->success();
|
||
} catch (\Exception $e) {
|
||
return $this->error('', $e->getMessage() . $e->getLine());
|
||
}
|
||
}
|
||
} |