新版订单消耗系统

CPAStatementsDetail.php 2.5KB

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