CreateOwnerBillReport.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. $sql = "SELECT owner_id,SUM(IFNULL(work_fee,0))+SUM(IFNULL(logistic_fee,0)) AS total FROM owner_fee_details WHERE worked_at LIKE ? AND ((type = '发货' AND logistic_fee IS NOT NULL AND work_fee IS NOT NULL) OR (type <> '发货' AND work_fee IS NOT NULL)) GROUP BY owner_id";
  44. $billDetails = DB::select(DB::raw($sql),[$year."-".$lastMonth."%"]);
  45. $areas = OwnerAreaReport::query()->with("ownerStoragePriceModel")->where("counting_month","like",$year."-".$lastMonth."%")->get();
  46. $map = [];
  47. foreach($areas as $area){
  48. if (isset($map[$area->owner_id."_".$area->counting_month])){
  49. if (!$area->ownerStoragePriceModel)continue;
  50. $map[$area->owner_id."_".$area->counting_month] += app('OwnerStoragePriceModelService')
  51. ->calculationAmount($area->ownerStoragePriceModel,$area->accounting_area,$area->owner_id,$area->counting_month);
  52. }else{
  53. if (!$area->ownerStoragePriceModel)continue;
  54. $map[$area->owner_id."_".$area->counting_month] = app('OwnerStoragePriceModelService')
  55. ->calculationAmount($area->ownerStoragePriceModel,$area->accounting_area,$area->owner_id,$area->counting_month);
  56. }
  57. }
  58. $chunks = array_chunk($billDetails,50);
  59. foreach ($chunks as $bills){
  60. $date = date('Y-m-d H:i:s');
  61. $createOwnerBillReport = [];
  62. foreach ($bills as $bill){
  63. $key = $bill->owner_id."_".$year."-".$lastMonth;
  64. $total = $bill->total;
  65. if (isset($map[$key]))$total += $map[$key];
  66. $createOwnerBillReport[] = [
  67. "owner_id" => $bill->owner_id, //项目ID
  68. "counting_month" => $year."-".$lastMonth."-01", //结算月
  69. "initial_fee" => $total, //原始账单金额
  70. "created_at" => $date,
  71. ];
  72. }
  73. LogService::log(__METHOD__,"客户管理-生成确认账单",json_encode($createOwnerBillReport));
  74. DB::table("owner_bill_reports")->insert($createOwnerBillReport);
  75. }
  76. }
  77. }