| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <?php
- /**
- * Niushop商城系统 - 团队十年电商经验汇集巨献!
- * =========================================================
- * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
- * ----------------------------------------------
- * 官方网址: https://www.niushop.com
- * =========================================================
- */
- namespace addon\cashier\model;
- use app\model\BaseModel;
- use app\model\order\OrderRefund;
- use app\model\system\Config;
- /**
- * @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);
- }
- /**
- * 查询交班信息记录
- * @param array $condition
- * @param bool $field
- * @return array
- */
- public function getChangeShiftsRecordInfo($condition = [], $field = true, $alias = 'a', $join = null)
- {
- $data = model('change_shifts_record')->getInfo($condition, $field, $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());
- }
- }
- /**
- * 获取收银台收款设置
- * @param int $site_id
- * @param int $store_id
- * @return array
- */
- public function getCashierCollectMoneyConfig(int $site_id, int $store_id)
- {
- $config = ( new Config() )->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'CASHIER_COLLECT_MONEY_CONFIG_' . $store_id ] ])[ 'data' ][ 'value' ];
- if (empty($config)) {
- $config = [
- 'reduction' => 1,
- 'point' => 1,
- 'balance' => 1,
- 'balance_safe' => 0,
- 'sms_verify' => 0,
- 'pay_type' => [ 'third', 'cash', 'own_wechatpay', 'own_alipay', 'own_pos' ]
- ];
- }
- return $this->success($config);
- }
- /**
- * 收银台收款设置
- * @param int $site_id
- * @param int $store_id
- * @return array
- */
- public function setCashierCollectMoneyConfig(int $site_id, int $store_id, array $config)
- {
- $res = ( new Config() )->setConfig($config, '收银端收款设置', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'CASHIER_COLLECT_MONEY_CONFIG_' . $store_id ] ]);
- return $res;
- }
- }
|