CreateOwnerBillReport.php 3.6 KB

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