新版订单消耗系统

CustomerInvoices.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use DB;
  5. class CustomerInvoices extends Model
  6. {
  7. protected $table = 'customer_invoices';
  8. public $timestamps = false;
  9. protected static $unguarded = true;
  10. public static $typeList = [1,3,4,5,6];
  11. public static function invoiceHistory($orderIds)
  12. {
  13. $details = CustomerInvoiceOrders::select('inv_id', 'order_id', 'invoice_amount')
  14. ->whereIn('order_id', $orderIds)->where('enable', 1)
  15. ->orderBy('id', 'asc')->get();
  16. $orders = Order::select('id', 'task_id', 'customer_name', 'billing_date', 'final_amount')
  17. ->whereIn('id', $orderIds)->where('enable', 1)
  18. ->get()->keyBy('id')->toArray();
  19. $inv_ids = $details->pluck('inv_id');
  20. $inv_ids = json_decode(json_encode($inv_ids), 1);
  21. $inv_ids = array_unique($inv_ids);
  22. $typeList = self::$typeList;
  23. if (empty($inv_ids)) {
  24. $invoices = [];
  25. } else {
  26. $sql = 'select ci.* from customer_invoices as ci left join statements as s on s.id = ci.statements_id where
  27. s.type in ('.implode(',', $typeList).') and ci.status = 4 and ci.is_invoiced=1 and ci.id in ('.implode(',', $inv_ids) . ') and ci.enable = 1
  28. and s.enable = 1';
  29. $invoices = \DB::select($sql);
  30. $invoices = json_decode(json_encode($invoices), 1);
  31. $invoices = array_column($invoices, null, 'id');
  32. }
  33. $data = array();
  34. foreach($details->toArray() as $k=>&$v){
  35. $inv_id = $v['inv_id'];
  36. if(!isset($invoices[$inv_id])){
  37. continue;
  38. }
  39. $invoice = $invoices[$inv_id];
  40. $order = $orders[$v['order_id']];
  41. if(!isset($data[$v['order_id']])) {
  42. //第一次开票
  43. $data[$v['order_id']] = [
  44. 'first_invoice_amount' => $v['invoice_amount'],
  45. 'first_invoice_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  46. 'invoice_num' => 1,
  47. 'second_invoice_amount' => 0,
  48. 'second_invoice_date' => null,
  49. 'uninvoiced_amount' => $order['final_amount'] - $v['invoice_amount'],
  50. 'invoice_amount' => $v['invoice_amount'],
  51. 'billing_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  52. ];
  53. } else {
  54. //第二次开票
  55. $data[$v['order_id']] = [
  56. 'invoice_num' => 2,
  57. 'first_invoice_amount' => $data[$v['order_id']]['first_invoice_amount'],
  58. 'first_invoice_date' => $data[$v['order_id']]['first_invoice_date'],
  59. 'second_invoice_amount' => $v['invoice_amount'],
  60. 'second_invoice_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  61. 'uninvoiced_amount' => $data[$v['order_id']]['uninvoiced_amount'] - $v['invoice_amount'],
  62. 'invoice_amount' => ($data[$v['order_id']]['first_invoice_amount'] + $v['invoice_amount']),
  63. 'billing_date' => !empty($invoice['invoiced_time']) ? substr($invoice['invoiced_time'], 0, 10) : null,
  64. ];
  65. }
  66. // $data[] = [
  67. // 'task_id' => $order['task_id'],
  68. // 'invoice_amount' => $v['invoice_amount'],
  69. // 'cdate' => $order['billing_date'],
  70. // 'created_at' => $invoice['created_at']
  71. // ];
  72. }
  73. return $data;
  74. }
  75. public static function getOrderIdsByHandler($search) {
  76. # 根据月份以及经手人姓名获取初始订单id
  77. $orderIdList = Order::query()->where('handler', current($search['handler']))
  78. ->where(function($query) use ($search) {
  79. if(isset($search['month']) && $search['month']) {
  80. if(count($search['month']) == 1) {
  81. $query->where('month', '=', date('Y-m-01', strtotime(current($search['month'])) ) );
  82. } else {
  83. $monthList = array_map(function($value){
  84. return date('Y-m-01', strtotime($value));
  85. }, $search['month']);
  86. $query->whereIn('month', $monthList);
  87. }
  88. }
  89. })->where('enable', 1)->pluck('id')->toArray();
  90. # 根据订单id获取对应的发票id
  91. $customerInvoiceIdList = CustomerInvoiceOrders::where('enable', 1)
  92. ->whereIn('order_id', $orderIdList)->pluck('inv_id')->toArray();
  93. # 根据查询到的发票id获取已经成功开票的发票id
  94. $successCustomerInvoiceIdList = CustomerInvoices::join('statements', 'statements.id', 'customer_invoices.statements_id')->whereIn('customer_invoices.id', $customerInvoiceIdList)
  95. ->where('customer_invoices.enable', 1)->where('customer_invoices.is_invoiced', 1)->where('statements.type', '!=', 2)->pluck('customer_invoices.id')->toArray();
  96. # 根据成功开票的发票id再次查询对应的订单id
  97. $successOrderIdList = CustomerInvoiceOrders::where('enable', 1)
  98. ->whereIn('inv_id', $successCustomerInvoiceIdList)->pluck('order_id')->toArray();
  99. # 对订单id进行交集运算
  100. $actualOrderIdList = array_intersect($orderIdList, $successOrderIdList);
  101. return $actualOrderIdList;
  102. }
  103. public static function getOrderIdsByMcnHandler($search) {
  104. # 根据月份以及经手人姓名获取初始订单id
  105. $orderIdList = Order::where('month', $search['month'])->where('mcn_handler', current($search['mcn_handler']))
  106. ->where('enable', 1)->pluck('id')->toArray();
  107. # 根据订单id获取对应的发票id
  108. $customerInvoiceIdList = CustomerInvoiceOrders::where('enable', 1)
  109. ->whereIn('order_id', $orderIdList)->pluck('inv_id')->toArray();
  110. # 根据查询到的发票id获取已经成功开票的发票id
  111. $successCustomerInvoiceIdList = CustomerInvoices::join('statements', 'statements.id', 'customer_invoices.statements_id')->whereIn('customer_invoices.id', $customerInvoiceIdList)
  112. ->where('customer_invoices.enable', 1)->where('customer_invoices.is_invoiced', 1)->where('statements.type', 2)->pluck('customer_invoices.id')->toArray();
  113. # 根据成功开票的发票id再次查询对应的订单id
  114. $successOrderIdList = CustomerInvoiceOrders::where('enable', 1)
  115. ->whereIn('inv_id', $successCustomerInvoiceIdList)->pluck('order_id')->toArray();
  116. # 对订单id进行交集运算
  117. $actualOrderIdList = array_intersect($orderIdList, $successOrderIdList);
  118. return $actualOrderIdList;
  119. }
  120. }