Month.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\crontab\controller;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 定时器
  13. */
  14. class Month extends BaseCron {
  15. /**
  16. * 默认方法
  17. */
  18. public function index(){
  19. //生成平台月账单统计
  20. $this->_create_orderstatis();
  21. }
  22. private function _create_orderstatis(){
  23. $max_time=strtotime(date('Y-m-01 0:0:0', TIMESTAMP))-1;//上个月最后一天
  24. $os_month=Db::name('orderstatis')->order('os_month desc')->value('os_month');
  25. if($os_month){
  26. //有生成过平台账单则查看最新的账单时间到上个月期间是否有结算单
  27. $start_time= strtotime($os_month.'01 0:0:0 + 1 month');
  28. }else{
  29. //没生成过平台账单则生成最旧的结算单时间到上个月期间的平台账单
  30. $ob_createdate=Db::name('orderbill')->order('ob_startdate asc')->value('ob_startdate');
  31. if($ob_createdate){
  32. $start_time= strtotime(date('Y-m-01 0:0:0',$ob_createdate));
  33. }
  34. }
  35. if(isset($start_time)){
  36. for($i=1;$i<=100;$i++){
  37. $end_time=strtotime(date('Y-m-01 0:0:0', $start_time)." +1 month")-1;
  38. if($end_time>$max_time){
  39. break;
  40. }
  41. $orderbill_sum=Db::name('orderbill')->where('ob_enddate','between',[$start_time,$end_time])->field('SUM(ob_order_totals) AS os_order_totals,SUM(ob_shipping_totals) AS os_shipping_totals,SUM(ob_order_return_totals) AS os_order_returntotals,SUM(ob_commis_totals) AS os_commis_totals,SUM(ob_commis_return_totals) AS os_commis_returntotals,SUM(ob_store_cost_totals) AS os_store_costtotals,SUM(ob_vr_order_totals) AS os_vr_order_totals,SUM(ob_vr_commis_totals) AS os_vr_commis_totals,SUM(ob_vr_inviter_totals) AS os_vr_inviter_totals,SUM(ob_result_totals) AS os_result_totals,SUM(ob_inviter_totals) AS os_inviter_totals,SUM(ob_vr_order_return_totals) AS os_vr_order_return_totals,SUM(ob_vr_commis_return_totals) AS os_vr_commis_return_totals')->find();
  42. if($orderbill_sum){
  43. Db::name('orderstatis')->insert(array(
  44. 'os_month'=>date('Ym',$start_time),
  45. 'os_createdate'=>TIMESTAMP,
  46. 'os_order_totals'=>floatval($orderbill_sum['os_order_totals']),
  47. 'os_shipping_totals'=>floatval($orderbill_sum['os_shipping_totals']),
  48. 'os_order_returntotals'=>floatval($orderbill_sum['os_order_returntotals']),
  49. 'os_commis_totals'=>floatval($orderbill_sum['os_commis_totals']),
  50. 'os_commis_returntotals'=>floatval($orderbill_sum['os_commis_returntotals']),
  51. 'os_store_costtotals'=>floatval($orderbill_sum['os_store_costtotals']),
  52. 'os_vr_order_totals'=>floatval($orderbill_sum['os_vr_order_totals']),
  53. 'os_vr_commis_totals'=>floatval($orderbill_sum['os_vr_commis_totals']),
  54. 'os_vr_inviter_totals'=>floatval($orderbill_sum['os_vr_inviter_totals']),
  55. 'os_result_totals'=>floatval($orderbill_sum['os_result_totals']),
  56. 'os_inviter_totals'=>floatval($orderbill_sum['os_inviter_totals']),
  57. 'os_vr_order_return_totals'=>floatval($orderbill_sum['os_vr_order_return_totals']),
  58. 'os_vr_commis_return_totals'=>floatval($orderbill_sum['os_vr_commis_return_totals']),
  59. ));
  60. }else{
  61. Db::name('orderstatis')->insert(array('os_month'=>date('Ym',$start_time),'os_createdate'=>TIMESTAMP));
  62. }
  63. $start_time=$end_time+1;
  64. }
  65. if($end_time<$max_time){
  66. $this->redirect('month/index');
  67. }
  68. }
  69. }
  70. }