Cashier.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\cashier\model;
  11. use app\model\BaseModel;
  12. use app\model\order\OrderRefund;
  13. use app\model\system\Config;
  14. /**
  15. * @author Administrator
  16. */
  17. class Cashier extends BaseModel
  18. {
  19. private $path = 'addon/cashier/source';
  20. /**
  21. * 查询班次内数据
  22. * @param $site_id
  23. * @param $store_id
  24. */
  25. public function getShiftsData($site_id, $store_id)
  26. {
  27. $last_info = model('change_shifts_record')->getFirstData([ [ 'site_id', '=', $site_id ], [ 'store_id', '=', $store_id ] ], 'end_time', 'end_time desc');
  28. $start_time = empty($last_info) ? 0 : $last_info[ 'end_time' ];
  29. $end_time = time();
  30. $order_condition = [
  31. [ 'site_id', '=', $site_id ],
  32. [ 'store_id', '=', $store_id ],
  33. [ 'order_scene', '=', 'cashier' ],
  34. [ 'order_status', '=', 10 ],
  35. [ 'finish_time', 'between', [ $start_time, $end_time ] ]
  36. ];
  37. $order_condition[ 5 ] = [ 'cashier_order_type', '=', 'goods' ];
  38. $billing_stat = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
  39. $order_condition[ 5 ] = [ 'cashier_order_type', '=', 'card' ];
  40. $card_stat = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
  41. $order_condition[ 5 ] = [ 'cashier_order_type', '=', 'recharge' ];
  42. $recharge_stat = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
  43. $order_condition[ 5 ] = [ 'pay_type', '=', 'cash' ];
  44. $cash = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
  45. $order_condition[ 5 ] = [ 'pay_type', '=', 'alipay' ];
  46. $alipay = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
  47. $order_condition[ 5 ] = [ 'pay_type', '=', 'wechatpay' ];
  48. $wechatpay = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
  49. $order_condition[ 5 ] = [ 'pay_type', '=', 'own_wechatpay' ];
  50. $own_wechatpay = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
  51. $order_condition[ 5 ] = [ 'pay_type', '=', 'own_alipay' ];
  52. $own_alipay = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
  53. $order_condition[ 5 ] = [ 'pay_type', '=', 'own_pos' ];
  54. $own_pos = model('order')->getInfo($order_condition, 'count(order_id) as num, ifnull(sum(pay_money), 0) as pay_money');
  55. $refund_condition = [
  56. [ 'o.site_id', '=', $site_id ],
  57. [ 'o.store_id', '=', $store_id ],
  58. [ 'o.order_scene', '=', 'cashier' ],
  59. [ 'og.refund_status', '=', OrderRefund::REFUND_COMPLETE ],
  60. [ 'og.refund_time', 'between', [ $start_time, $end_time ] ]
  61. ];
  62. $refund_stat = model('order_goods')->getInfo($refund_condition, 'count(og.order_goods_id) as num, ifnull(sum(refund_pay_money), 0) as money', 'og', [
  63. [ 'order o', 'o.order_id = og.order_id', 'inner' ]
  64. ]);
  65. $data = [
  66. 'start_time' => $start_time,
  67. 'end_time' => $end_time,
  68. 'billing_count' => $billing_stat[ 'num' ],
  69. 'billing_money' => $billing_stat[ 'pay_money' ],
  70. 'buycard_count' => $card_stat[ 'num' ],
  71. 'buycard_money' => $card_stat[ 'pay_money' ],
  72. 'recharge_count' => $recharge_stat[ 'num' ],
  73. 'recharge_money' => $recharge_stat[ 'pay_money' ],
  74. 'refund_count' => $refund_stat[ 'num' ],
  75. 'refund_money' => $refund_stat[ 'money' ],
  76. 'cash_count' => $cash[ 'num' ],
  77. 'cash' => $cash[ 'pay_money' ],
  78. 'alipay_count' => $alipay[ 'num' ],
  79. 'alipay' => $alipay[ 'pay_money' ],
  80. 'wechatpay_count' => $wechatpay[ 'num' ],
  81. 'wechatpay' => $wechatpay[ 'pay_money' ],
  82. 'own_wechatpay_count' => $own_wechatpay[ 'num' ],
  83. 'own_wechatpay' => $own_wechatpay[ 'pay_money' ],
  84. 'own_alipay_count' => $own_alipay[ 'num' ],
  85. 'own_alipay' => $own_alipay[ 'pay_money' ],
  86. 'own_pos_count' => $own_pos[ 'num' ],
  87. 'own_pos' => $own_pos[ 'pay_money' ],
  88. ];
  89. return $data;
  90. }
  91. /**
  92. * 交接班
  93. * @param $user_info
  94. * @param $site_id
  95. * @param $store_id
  96. */
  97. public function changeShifts($user_info, $site_id, $store_id)
  98. {
  99. $data = $this->getShiftsData($site_id, $store_id);
  100. $data = array_merge($data, [
  101. 'site_id' => $site_id,
  102. 'store_id' => $store_id,
  103. 'uid' => $user_info[ 'uid' ]
  104. ]);
  105. $id = model('change_shifts_record')->add($data);
  106. if ($id) {
  107. return $this->success($id);
  108. } else {
  109. return $this->error('', '交接班数据添加失败');
  110. }
  111. }
  112. /**
  113. * 查询交班记录
  114. * @param array $condition
  115. * @param bool $field
  116. * @param string $order
  117. * @param int $page
  118. * @param int $list_rows
  119. * @param string $alias
  120. * @param array $join
  121. * @return array
  122. */
  123. public function getchangeShiftsPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [])
  124. {
  125. $data = model('change_shifts_record')->pageList($condition, $field, $order, $page, $list_rows, $alias, $join);
  126. return $this->success($data);
  127. }
  128. /**
  129. * 查询交班信息记录
  130. * @param array $condition
  131. * @param bool $field
  132. * @return array
  133. */
  134. public function getChangeShiftsRecordInfo($condition = [], $field = true, $alias = 'a', $join = null)
  135. {
  136. $data = model('change_shifts_record')->getInfo($condition, $field, $alias, $join);
  137. return $this->success($data);
  138. }
  139. /**
  140. * 刷新收银端
  141. * @return array
  142. */
  143. public function refreshCashier()
  144. {
  145. try {
  146. $path = $this->path . '/default';
  147. $cashier_path = 'cashregister'; // 收银端生成目录
  148. $config_path = 'cashregister/static/js'; // 收银模板文件目录
  149. if (!is_dir($path) || count(scandir($path)) <= 3) {
  150. return $this->error('', '未找到源码包,请检查目录文件');
  151. }
  152. if (is_dir($cashier_path)) {
  153. // 先将之前的文件删除
  154. if (count(scandir($cashier_path)) > 1) deleteDir($cashier_path);
  155. } else {
  156. // 创建收银目录
  157. mkdir($cashier_path, intval('0777', 8), true);
  158. }
  159. // 将原代码包拷贝到收银目录下
  160. recurseCopy($path, $cashier_path);
  161. $this->copyFile($config_path);
  162. file_put_contents($cashier_path . '/refresh.log', time());
  163. return $this->success();
  164. } catch (\Exception $e) {
  165. return $this->error('', $e->getMessage() . $e->getLine());
  166. }
  167. }
  168. /**
  169. * 替换配置信息,API请求域名地址、图片、地图密钥等
  170. * @param $source_path
  171. * @param string $domain
  172. */
  173. private function copyFile($source_path, $domain = __ROOT__)
  174. {
  175. $files = scandir($source_path);
  176. foreach ($files as $path) {
  177. if ($path != '.' && $path != '..') {
  178. $temp_path = $source_path . '/' . $path;
  179. if (file_exists($temp_path)) {
  180. if (preg_match("/(index.)(\w{8})(.js)$/", $temp_path)) {
  181. $content = file_get_contents($temp_path);
  182. $content = $this->paramReplace($content, $domain);
  183. file_put_contents($temp_path, $content);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * 参数替换
  191. * @param $string
  192. * @param string $domain
  193. * @return string|string[]|null
  194. */
  195. private function paramReplace($string, $domain = __ROOT__)
  196. {
  197. $patterns = [
  198. '/\{\{\$baseUrl\}\}/',
  199. '/\{\{\$imgDomain\}\}/',
  200. ];
  201. $replacements = [
  202. $domain,
  203. $domain
  204. ];
  205. $string = preg_replace($patterns, $replacements, $string);
  206. return $string;
  207. }
  208. /**
  209. * 下载收银端uniapp源码
  210. * @return array
  211. */
  212. public function downloadOs()
  213. {
  214. try {
  215. $source_file_path = $this->path . '/os';
  216. if (!is_dir($source_file_path) || count(scandir($source_file_path)) <= 3) {
  217. return $this->error('', '未找到源码包,请检查目录文件');
  218. }
  219. $file_arr = getFileMap($source_file_path);
  220. if (!empty($file_arr)) {
  221. $zipname = 'cashier_os_' . date('YmdHi') . '.zip';
  222. $zip = new \ZipArchive();
  223. $res = $zip->open($zipname, \ZipArchive::CREATE);
  224. if ($res === TRUE) {
  225. foreach ($file_arr as $file_path => $file_name) {
  226. if (is_dir($file_path)) {
  227. $file_path = str_replace($source_file_path . '/', '', $file_path);
  228. $zip->addEmptyDir($file_path);
  229. } else {
  230. $zip_path = str_replace($source_file_path . '/', '', $file_path);
  231. $zip->addFile($file_path, $zip_path);
  232. }
  233. }
  234. $zip->close();
  235. header("Content-Type: application/zip");
  236. header("Content-Transfer-Encoding: Binary");
  237. header("Content-Length: " . filesize($zipname));
  238. header("Content-Disposition: attachment; filename=\"" . basename($zipname) . "\"");
  239. readfile($zipname);
  240. @unlink($zipname);
  241. }
  242. }
  243. return $this->success();
  244. } catch (\Exception $e) {
  245. return $this->error('', $e->getMessage() . $e->getLine());
  246. }
  247. }
  248. /**
  249. * 获取收银台收款设置
  250. * @param int $site_id
  251. * @param int $store_id
  252. * @return array
  253. */
  254. public function getCashierCollectMoneyConfig(int $site_id, int $store_id)
  255. {
  256. $config = ( new Config() )->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'CASHIER_COLLECT_MONEY_CONFIG_' . $store_id ] ])[ 'data' ][ 'value' ];
  257. if (empty($config)) {
  258. $config = [
  259. 'reduction' => 1,
  260. 'point' => 1,
  261. 'balance' => 1,
  262. 'balance_safe' => 0,
  263. 'sms_verify' => 0,
  264. 'pay_type' => [ 'third', 'cash', 'own_wechatpay', 'own_alipay', 'own_pos' ]
  265. ];
  266. }
  267. return $this->success($config);
  268. }
  269. /**
  270. * 收银台收款设置
  271. * @param int $site_id
  272. * @param int $store_id
  273. * @return array
  274. */
  275. public function setCashierCollectMoneyConfig(int $site_id, int $store_id, array $config)
  276. {
  277. $res = ( new Config() )->setConfig($config, '收银端收款设置', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'CASHIER_COLLECT_MONEY_CONFIG_' . $store_id ] ]);
  278. return $res;
  279. }
  280. }