Cron.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. $where['pay_type'] = 1;
  38. $this->up($where, $v, 'shop', $num);
  39. }
  40. }
  41. # 处理门店零售对账单
  42. public function shop_sell_api(){}
  43. public function shop_sell()
  44. {
  45. $num = Dever::input('num', 1);
  46. $where['status'] = 2;
  47. $shop = Dever::db('shop/info')->select();
  48. foreach ($shop as $k => $v) {
  49. $where['type'] = 1;
  50. $where['type_id'] = $v['id'];
  51. $where['pay_type'] = '2,3';
  52. $this->up($where, $v, 'shop', $num);
  53. }
  54. }
  55. public function store_api(){}
  56. public function store()
  57. {
  58. $num = Dever::input('num', 1);
  59. $where['status'] = $w['status'] = 2;
  60. $store = Dever::db('store/info')->select();
  61. foreach ($store as $k => $v) {
  62. $where['type'] = 2;
  63. $where['type_id'] = $v['id'];
  64. $this->up($where, $v, 'store', $num, 1);
  65. }
  66. }
  67. public function factory_api(){}
  68. public function factory()
  69. {
  70. $num = Dever::input('num', 1);
  71. $where['status'] = 2;
  72. $store = Dever::db('factory/info')->select();
  73. foreach ($store as $k => $v) {
  74. $where['source_type'] = 3;
  75. $where['source_id'] = $v['id'];
  76. $this->up($where, $v, 'factory', $num);
  77. }
  78. }
  79. private function up($where, $info, $type, $num, $k = 1)
  80. {
  81. $state = $this->check($info);
  82. if (!$state) {
  83. return;
  84. }
  85. if ($info['stat_type'] == 1) {
  86. $method = 'month';
  87. } elseif ($info['stat_type'] == 2) {
  88. $method = 'week';
  89. } else {
  90. $method = 'day';
  91. }
  92. list($start, $end) = Dever::$method($num);
  93. $where['start'] = $start;
  94. $where['end'] = $end;
  95. $data = array();
  96. $data[$type . '_id'] = $info['id'];
  97. $data['type'] = $info['stat_type'];
  98. $data['day'] = $start;
  99. $data['t'] = $k;
  100. $find = Dever::db('cash/' . $type)->find($data);
  101. if ($type == 'factory') {
  102. $msg_type = 4;
  103. $cash = Dever::db('cash/order')->getPCash($where);
  104. $data['cash'] = $cash['total'] ? $cash['total'] : 0;
  105. } else {
  106. $msg_type = 3;
  107. $cash = Dever::db('cash/order')->getCash($where);
  108. $data['cash'] = $cash['total'] ? $cash['total'] : 0;
  109. }
  110. $num = Dever::db('cash/order')->getNum($where);
  111. $data['num'] = $num['total'] ? $num['total'] : 0;
  112. $data['start'] = $start;
  113. $data['end'] = $end;
  114. if (!$find) {
  115. $id = Dever::db('cash/' . $type)->insert($data);
  116. } else {
  117. $id = $data['where_id'] = $find['id'];
  118. Dever::db('cash/' . $type)->update($data);
  119. }
  120. if ($id > 0 && $type == 'store') {
  121. $where['source_type'] = $where['type'];
  122. $where['source_id'] = $where['type_id'];
  123. unset($where['type']);
  124. unset($where['type_id']);
  125. $cash = Dever::db('cash/order')->getCash($where);
  126. $up['cash'] = $cash['total'] ? $cash['total'] : 0;
  127. $num = Dever::db('cash/order')->getNum($where);
  128. $up['num'] = $num['total'] ? $num['total'] : 0;
  129. $data['cash'] += $up['cash'];
  130. $data['num'] += $up['num'];
  131. $data['where_id'] = $id;
  132. Dever::db('cash/' . $type)->update($data);
  133. }
  134. if (($type == 'factory' || $type == 'store') && Dever::project('message') && $id) {
  135. $msg_param['type'] = 2;//消息头类型2是对账单消息
  136. $msg_param['id'] = $id;
  137. $msg_param = Dever::json_encode($msg_param);
  138. $msg = '您的'.Dever::load('cash/lib/set')->statDate($data['type'], $data['day']).'对账单已经生成,请您及时确认,确认之后方可结算';
  139. Dever::load('message/lib/data')->push(-1, $info['id'], '对账通知', $msg, 6, $msg_type, false, $msg_param);
  140. }
  141. return $id;
  142. }
  143. }