新版订单消耗系统

ECommerceStatementsDetail.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2021/4/15
  6. * Time: 13:56
  7. */
  8. namespace App\Models;
  9. use Illuminate\Database\Eloquent\Model;
  10. class ECommerceStatementsDetail extends Model
  11. {
  12. protected $table = 'e_commerce_statements_detail';
  13. public $timestamps = false;
  14. public static function calculateSubtotal($orderList)
  15. {
  16. $monthData = [];
  17. $total = [];
  18. foreach($orderList as $order) {
  19. $order['url'] = isset($order['url']) ? $order['url'] : null;
  20. $order['actual_consumption'] = isset($order['actual_consumption']) ? $order['actual_consumption'] : 0;
  21. $order['discount_amount'] = isset($order['discount_amount']) ? $order['discount_amount'] : 0;
  22. $order['discount_rate'] = isset($order['discount_rate']) ? $order['discount_rate'] :
  23. ($order['actual_consumption'] > 0 ? round($order['discount_amount'] / $order['actual_consumption'], 2) : 0);
  24. $total['actual_consumption'] = isset($total['actual_consumption']) ? $total['actual_consumption'] + $order['actual_consumption'] : 0;
  25. $total['discount_amount'] = isset($total['discount_amount']) ? $total['discount_amount'] + $order['discount_amount'] : 0;
  26. $key = $order['proejct_name'].'@'.$order['month'];
  27. if (isset($monthData[$key])) {
  28. if ($monthData[$key]['month'] == date('m月', strtotime($order['month']))) {
  29. $monthData[$key]['actual_consumption'] += $order['actual_consumption'];
  30. $monthData[$key]['discount_amount'] += $order['discount_amount'];
  31. }
  32. } else {
  33. $monthData[$key]['actual_consumption'] = $order['actual_consumption'];
  34. $monthData[$key]['discount_amount'] = $order['discount_amount'];
  35. $monthData[$key]['month'] = date('m月', strtotime($order['month']));
  36. }
  37. $order['month'] = date('Y年m月', strtotime($order['month']));
  38. $monthData[$key]['row'][] = $order;
  39. }
  40. return ['list' => array_values($monthData), 'total' => $total];
  41. }
  42. }