Cron.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Cash\Lib;
  3. use Dever;
  4. class Cron
  5. {
  6. private function check($info)
  7. {
  8. if (!$info['stat_day']) {
  9. $info['stat_day'] = 1;
  10. }
  11. if ($info['stat_type'] == 1) {
  12. $day = intval(date('d'));
  13. if ($day != $info['stat_day']) {
  14. return false;
  15. }
  16. } elseif ($info['stat_type'] == 2) {
  17. $day = intval(date('w'));
  18. if (!$day || $day == 0) {
  19. $day = 7;
  20. }
  21. if ($day != $info['stat_day']) {
  22. return false;
  23. }
  24. }
  25. return true;
  26. }
  27. # 处理门店对账单
  28. public function shop_api(){}
  29. public function shop()
  30. {
  31. $num = Dever::input('num', 1);
  32. $where['status'] = 2;
  33. $shop = Dever::db('shop/info')->select();
  34. foreach ($shop as $k => $v) {
  35. $where['type'] = 1;
  36. $where['type_id'] = $v['id'];
  37. $this->up($where, $v, 'shop', $num);
  38. }
  39. }
  40. public function store_api(){}
  41. public function store()
  42. {
  43. $num = Dever::input('num', 1);
  44. $where['status'] = $w['status'] = 2;
  45. $store = Dever::db('store/info')->select();
  46. foreach ($store as $k => $v) {
  47. $where['type'] = 2;
  48. $where['type_id'] = $v['id'];
  49. $this->up($where, $v, 'store', $num, 1);
  50. }
  51. }
  52. public function factory_api(){}
  53. public function factory()
  54. {
  55. $num = Dever::input('num', 1);
  56. $where['status'] = 2;
  57. $store = Dever::db('factory/info')->select();
  58. foreach ($store as $k => $v) {
  59. $where['source_type'] = 3;
  60. $where['source_id'] = $v['id'];
  61. $this->up($where, $v, 'factory', $num);
  62. }
  63. }
  64. private function up($where, $info, $type, $num, $k = 1)
  65. {
  66. $state = $this->check($info);
  67. if (!$state) {
  68. return;
  69. }
  70. if ($info['stat_type'] == 1) {
  71. $method = 'month';
  72. } elseif ($info['stat_type'] == 2) {
  73. $method = 'week';
  74. } else {
  75. $method = 'day';
  76. }
  77. list($start, $end) = Dever::$method($num);
  78. $where['start'] = $start;
  79. $where['end'] = $end;
  80. $data = array();
  81. $data[$type . '_id'] = $info['id'];
  82. $data['type'] = $info['stat_type'];
  83. $data['day'] = $start;
  84. $data['t'] = $k;
  85. $find = Dever::db('cash/' . $type)->find($data);
  86. if ($type == 'factory') {
  87. $msg_type = 4;
  88. $cash = Dever::db('cash/order')->getPCash($where);
  89. $data['cash'] = $cash['total'] ? $cash['total'] : 0;
  90. } else {
  91. $msg_type = 3;
  92. $cash = Dever::db('cash/order')->getCash($where);
  93. $data['cash'] = $cash['total'] ? $cash['total'] : 0;
  94. }
  95. $num = Dever::db('cash/order')->getNum($where);
  96. $data['num'] = $num['total'] ? $num['total'] : 0;
  97. $data['start'] = $start;
  98. $data['end'] = $end;
  99. if (!$find) {
  100. $id = Dever::db('cash/' . $type)->insert($data);
  101. } else {
  102. $id = $data['where_id'] = $find['id'];
  103. Dever::db('cash/' . $type)->update($data);
  104. }
  105. if ($id > 0 && $type == 'store') {
  106. $where['source_type'] = $where['type'];
  107. $where['source_id'] = $where['type_id'];
  108. unset($where['type']);
  109. unset($where['type_id']);
  110. $cash = Dever::db('cash/order')->getCash($where);
  111. $up['cash'] = $cash['total'] ? $cash['total'] : 0;
  112. $num = Dever::db('cash/order')->getNum($where);
  113. $up['num'] = $num['total'] ? $num['total'] : 0;
  114. $data['cash'] += $up['cash'];
  115. $data['num'] += $up['num'];
  116. $data['where_id'] = $id;
  117. Dever::db('cash/' . $type)->update($data);
  118. }
  119. if (($type == 'factory' || $type == 'store') && Dever::project('message') && $id) {
  120. $msg_param['type'] = 2;//消息头类型2是对账单消息
  121. $msg_param['id'] = $id;
  122. $msg_param = Dever::json_encode($msg_param);
  123. $msg = '您的'.Dever::load('cash/lib/set')->statDate($data['type'], $data['day']).'对账单已经生成,请您及时确认,确认之后方可结算';
  124. Dever::load('message/lib/data')->push(-1, $info['id'], '对账通知', $msg, 6, $msg_type, false, $msg_param);
  125. }
  126. return $id;
  127. }
  128. }