CreateOwnerBillReport.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\OwnerAreaReport;
  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. $areas = OwnerAreaReport::query()->with("ownerStoragePriceModel")->where("counting_month","like",$year."-".$lastMonth."%")->get();
  44. $map = [];
  45. foreach($areas as $area){
  46. if (isset($map[$area->owner_id."_".$area->counting_month])){
  47. if (!$area->ownerStoragePriceModel)continue;
  48. $map[$area->owner_id."_".$area->counting_month] += app('OwnerStoragePriceModelService')
  49. ->calculationAmount($area->ownerStoragePriceModel,$area->accounting_area,$area->owner_id,$area->counting_month);
  50. }else{
  51. if (!$area->ownerStoragePriceModel)continue;
  52. $map[$area->owner_id."_".$area->counting_month] = app('OwnerStoragePriceModelService')
  53. ->calculationAmount($area->ownerStoragePriceModel,$area->accounting_area,$area->owner_id,$area->counting_month);
  54. }
  55. }
  56. $chunks = array_chunk($bills,50);
  57. foreach ($chunks as $bills){
  58. $date = date('Y-m-d H:i:s');
  59. $createOwnerBillReport = [];
  60. foreach ($bills as $bill){
  61. $key = $bill->owner_id."_".$year."-".$lastMonth;
  62. $total = $bill->total;
  63. if (isset($map[$key]))$total += $map[$key];
  64. $createOwnerBillReport[] = [
  65. "owner_id" => $bill->owner_id, //项目ID
  66. "counting_month" => $year."-".$lastMonth."-01", //结算月
  67. "initial_fee" => $total, //原始账单金额
  68. "created_at" => $date,
  69. ];
  70. }
  71. DB::table("owner_bill_reports")->insert($createOwnerBillReport);
  72. }
  73. }
  74. }