CreateOwnerBillReport.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\OwnerAreaReport;
  4. use App\Services\LogService;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. class CreateOwnerBillReport extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'createOwnerBillReport';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'create owner bill report';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * 1号生成账单确认,确认金额由人工填写 原始金额默认为:即时账单月记录金额+仓储计费与面积计算金额
  32. *
  33. * @return void
  34. */
  35. public function handle()
  36. {
  37. $year = (int)date('Y');
  38. $month = (int)date('m');
  39. if ($month == 1){
  40. $year--;
  41. $lastMonth = '12';
  42. }else $lastMonth = ($month-1) < 10 ? "0".($month-1) : ($month-1);
  43. $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."%"]);
  44. $areas = OwnerAreaReport::query()->with("ownerStoragePriceModel")->where("counting_month","like",$year."-".$lastMonth."%")->get();
  45. $map = [];
  46. foreach($areas as $area){
  47. if (isset($map[$area->owner_id."_".$area->counting_month])){
  48. if (!$area->ownerStoragePriceModel)continue;
  49. $map[$area->owner_id."_".$area->counting_month] += app('OwnerStoragePriceModelService')
  50. ->calculationAmount($area->ownerStoragePriceModel,$area->accounting_area,$area->owner_id,$area->counting_month);
  51. }else{
  52. if (!$area->ownerStoragePriceModel)continue;
  53. $map[$area->owner_id."_".$area->counting_month] = app('OwnerStoragePriceModelService')
  54. ->calculationAmount($area->ownerStoragePriceModel,$area->accounting_area,$area->owner_id,$area->counting_month);
  55. }
  56. }
  57. $chunks = array_chunk($bills,50);
  58. foreach ($chunks as $bills){
  59. $date = date('Y-m-d H:i:s');
  60. $createOwnerBillReport = [];
  61. foreach ($bills as $bill){
  62. $key = $bill->owner_id."_".$year."-".$lastMonth;
  63. $total = $bill->total;
  64. if (isset($map[$key]))$total += $map[$key];
  65. $createOwnerBillReport[] = [
  66. "owner_id" => $bill->owner_id, //项目ID
  67. "counting_month" => $year."-".$lastMonth."-01", //结算月
  68. "initial_fee" => $total, //原始账单金额
  69. "created_at" => $date,
  70. ];
  71. }
  72. LogService::log(__METHOD__,"客户管理-生成确认账单",json_encode($createOwnerBillReport));
  73. DB::table("owner_bill_reports")->insert($createOwnerBillReport);
  74. }
  75. }
  76. }