Cron.php 7.3 KB

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