Cron.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace Cash\Lib;
  3. use Dever;
  4. class Cron
  5. {
  6. private function check($info)
  7. {
  8. $type = Dever::input('type');
  9. if ($type && $type == 1) {
  10. return true;
  11. }
  12. if (!$info['stat_day']) {
  13. $info['stat_day'] = 1;
  14. }
  15. if ($info['stat_type'] == 1) {
  16. $day = intval(date('d'));
  17. if ($day != $info['stat_day']) {
  18. return false;
  19. }
  20. } elseif ($info['stat_type'] == 2) {
  21. $day = intval(date('w'));
  22. if (!$day || $day == 0) {
  23. $day = 7;
  24. }
  25. if ($day != $info['stat_day']) {
  26. return false;
  27. }
  28. }
  29. return true;
  30. }
  31. # 处理门店采购对账单
  32. public function shop_api(){}
  33. public function shop()
  34. {
  35. $num = Dever::input('num', 1);
  36. $where['status'] = 2;
  37. $shop = Dever::db('shop/info')->select();
  38. foreach ($shop as $k => $v) {
  39. $where['type'] = 1;
  40. $where['type_id'] = $v['id'];
  41. $where['pay_type'] = 1;
  42. $this->up($where, $v, 'shop', $num);
  43. }
  44. return 'ok';
  45. }
  46. # 处理门店零售对账单
  47. public function shop_sell_api(){}
  48. public function shop_sell()
  49. {
  50. $num = Dever::input('num', 1);
  51. $where['status'] = 2;
  52. $shop = Dever::db('shop/info')->select();
  53. foreach ($shop as $k => $v) {
  54. $where['type'] = 1;
  55. $where['type_id'] = $v['id'];
  56. $where['pay_type'] = '2,3';
  57. $this->up($where, $v, 'shop_sell', $num);
  58. }
  59. return 'ok';
  60. }
  61. public function store_api(){}
  62. public function store()
  63. {
  64. $num = Dever::input('num', 1);
  65. $where['status'] = $w['status'] = 2;
  66. $store = Dever::db('store/info')->select();
  67. foreach ($store as $k => $v) {
  68. $where['type'] = 2;
  69. $where['type_id'] = $v['id'];
  70. $this->up($where, $v, 'store', $num, 1);
  71. }
  72. }
  73. public function factory_api(){}
  74. public function factory()
  75. {
  76. $num = Dever::input('num', 1);
  77. $where['status'] = 2;
  78. $store = Dever::db('factory/info')->select();
  79. foreach ($store as $k => $v) {
  80. $where['source_type'] = 3;
  81. $where['source_id'] = $v['id'];
  82. $this->up($where, $v, 'factory', $num);
  83. }
  84. }
  85. private function up($where, $info, $type, $num, $k = 1)
  86. {
  87. $state = $this->check($info);
  88. if (!$state) {
  89. return;
  90. }
  91. if ($info['stat_type'] == 1) {
  92. $method = 'month';
  93. } elseif ($info['stat_type'] == 2) {
  94. $method = 'week';
  95. } else {
  96. $method = 'day';
  97. }
  98. $table = 'cash/' . $type;
  99. if ($type == 'shop_sell') {
  100. $type = 'shop';
  101. }
  102. list($start, $end) = Dever::$method($num);
  103. $where['start'] = $start;
  104. $where['end'] = $end;
  105. $data = array();
  106. $data[$type . '_id'] = $info['id'];
  107. $data['type'] = $info['stat_type'];
  108. $data['day'] = $start;
  109. $data['t'] = $k;
  110. $find = Dever::db($table)->find($data);
  111. if ($type == 'factory') {
  112. $msg_type = 4;
  113. $cash = Dever::db('cash/order')->getPCash($where);
  114. $data['cash'] = $cash['total'] ? $cash['total'] : 0;
  115. } else {
  116. $msg_type = 3;
  117. $cash = Dever::db('cash/order')->getCash($where);
  118. $data['cash'] = $cash['total'] ? $cash['total'] : 0;
  119. }
  120. $num = Dever::db('cash/order')->getNum($where);
  121. $data['num'] = $num['total'] ? $num['total'] : 0;
  122. $data['start'] = $start;
  123. $data['end'] = $end;
  124. if (!$find) {
  125. $id = Dever::db($table)->insert($data);
  126. } else {
  127. $id = $data['where_id'] = $find['id'];
  128. Dever::db($table)->update($data);
  129. }
  130. if ($id > 0 && $type == 'store') {
  131. $where['source_type'] = $where['type'];
  132. $where['source_id'] = $where['type_id'];
  133. unset($where['type']);
  134. unset($where['type_id']);
  135. $cash = Dever::db('cash/order')->getCash($where);
  136. $up['cash'] = $cash['total'] ? $cash['total'] : 0;
  137. $num = Dever::db('cash/order')->getNum($where);
  138. $up['num'] = $num['total'] ? $num['total'] : 0;
  139. $data['cash'] += $up['cash'];
  140. $data['num'] += $up['num'];
  141. $data['where_id'] = $id;
  142. Dever::db($table)->update($data);
  143. }
  144. if (($type == 'factory' || $type == 'store') && Dever::project('message') && $id) {
  145. $msg_param['type'] = 2;//消息头类型2是对账单消息
  146. $msg_param['id'] = $id;
  147. $msg_param = Dever::json_encode($msg_param);
  148. $msg = '您的'.Dever::load('cash/lib/set')->statDate($data['type'], $data['day']).'对账单已经生成,请您及时确认,确认之后方可结算';
  149. Dever::load('message/lib/data')->push(-1, $info['id'], '对账通知', $msg, 6, $msg_type, false, $msg_param);
  150. }
  151. return $id;
  152. }
  153. # 修正礼品卡结算单金额,之前是九折,现在不是了
  154. public function up_cash_api()
  155. {
  156. $order = Dever::db('shop/sell_order')->select();
  157. if ($order) {
  158. foreach ($order as $k => $v) {
  159. if ($v['card_code_cash'] > 0) {
  160. # 礼品卡订单
  161. $info = Dever::db('cash/order')->select(array('pay_type' => 2, 'source_order_num' => $v['order_num']));
  162. $order_data = $v;
  163. $order_data['type'] = 1;
  164. $order_data['type_id'] = $order_data['shop_id'];
  165. $order_data['source_type'] = 4;
  166. $order_data['source_id'] = -1;
  167. $order_data['price'] = $v['card_code_cash'];
  168. $order_data['p_price'] = $v['card_code_cash'];
  169. if (!$info) {
  170. if ($v['status'] == 5 || $v['status'] == 6) {
  171. # 完成
  172. Dever::load('cash/lib/order')->up($order_data, 3, 2, false, 2, $v['fdate']);
  173. } elseif ($v['status'] == 8) {
  174. # 退款
  175. # 获取退款信息
  176. Dever::load('cash/lib/order')->up($order_data, 2, 2, false, 2, $v['operdate']);
  177. } elseif ($v['status'] == 7) {
  178. # 取消
  179. Dever::load('cash/lib/order')->up($order_data, 3, 3, false, 2, $v['operdate']);
  180. } else {
  181. # 处理中
  182. Dever::load('cash/lib/order')->up($order_data, 3, 1, false, 2, $v['cdate']);
  183. }
  184. } elseif ($info) {
  185. if ($v['status'] == 7 && $info['status'] == 2) {
  186. Dever::db('cash/order')->update(array('where_id' => $info['id'], 'status' => 1));
  187. }
  188. }
  189. }
  190. }
  191. }
  192. return 'ok';
  193. }
  194. }