新版订单消耗系统

McnStatementsDetail.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2021/1/26
  6. * Time: 15:16
  7. */
  8. namespace App\Models;
  9. use Illuminate\Database\Eloquent\Model;
  10. class McnStatementsDetail extends Model
  11. {
  12. public $timestamps = false;
  13. protected $table = 'mcn_statements_detail';
  14. protected static $unguarded = true;
  15. public static function calculateSubtotal($orderList)
  16. {
  17. $monthData = [];
  18. $total = [];
  19. foreach($orderList as $order) {
  20. $order['final_amount'] = isset($order['final_amount']) ? $order['final_amount'] : 0;
  21. $order['return_point_amount'] = isset($order['return_point_amount']) ? $order['return_point_amount'] : 0;
  22. $order['percentage_of_service_charge'] = isset($order['percentage_of_service_charge']) ? $order['percentage_of_service_charge'] :
  23. ($order['final_amount'] > 0 ? round($order['return_point_amount'] / $order['final_amount'], 2) : 0);
  24. $total['final_amount'] = isset($total['final_amount']) ? $total['final_amount'] + $order['final_amount'] : $order['final_amount'];
  25. $total['return_point_amount'] = isset($total['return_point_amount']) ? $total['return_point_amount'] + $order['return_point_amount'] : $order['return_point_amount'];
  26. // 统计每月小计
  27. $key = $order['sort_month'];
  28. // $key = $order['project_name_second'].'@'.$order['month'];
  29. if(isset($monthData[$key])) {
  30. if ($monthData[$key]['sort_month_format'] == $order['sort_month']) {
  31. $monthData[$key]['final_amount'] += $order['final_amount'];
  32. $monthData[$key]['return_point_amount'] += $order['return_point_amount'];
  33. }
  34. } else {
  35. $monthData[$key]['final_amount'] = $order['final_amount'];
  36. $monthData[$key]['return_point_amount'] = $order['return_point_amount'];
  37. $monthData[$key]['sort_month'] = date('Y年m月', strtotime($order['sort_month']));
  38. $monthData[$key]['sort_month_format'] = $order['sort_month'];
  39. }
  40. // 将当月数据写入
  41. $order['sort_month'] = date('Y年m月', strtotime($order['sort_month']));
  42. $order['month'] = date('Y年m月', strtotime($order['month']));
  43. $order['order_original_month'] = !empty($order['order_original_month']) ? date('Y年m月', strtotime($order['order_original_month'])) : '';
  44. $monthData[$key]['row'][] = $order;
  45. }
  46. ksort($monthData);
  47. return ['list' => array_values($monthData), 'total' => $total];
  48. }
  49. }