CreateOwnerBillReport.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\OwnerService;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. class CreateOwnerBillReport extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'createOwnerBillReport';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'create owner bill report';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * 1号生成账单确认,确认金额由人工填写
  31. *
  32. * @return void
  33. */
  34. public function handle()
  35. {
  36. $year = (int)date('Y');
  37. $month = (int)date('m');
  38. if ($month == 1){
  39. $year--;
  40. $lastMonth = '12';
  41. }else $lastMonth = ($month-1) < 10 ? "0".($month-1) : ($month-1);
  42. $bills = DB::select(DB::raw("select owner_id,SUM(work_fee)+SUM(logistic_fee) as total from owner_fee_details where worked_at like ? GROUP BY owner_id"),[$year."-".$lastMonth."%"]);
  43. $chunks = array_chunk($bills,50);
  44. foreach ($chunks as $bills){
  45. $date = date('Y-m-d H:i:s');
  46. $createOwnerBillReport = [];
  47. foreach ($bills as $bill){
  48. $createOwnerBillReport[] = [
  49. "owner_id" => $bill->owner_id, //项目ID
  50. "counting_month" => $year."-".$lastMonth."-01", //结算月
  51. "initial_fee" => $bill->total, //原始账单金额
  52. "created_at" => $date,
  53. ];
  54. }
  55. DB::table("owner_bill_reports")->insert($createOwnerBillReport);
  56. }
  57. }
  58. }