CreateOwnerBillReport.php 4.3 KB

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