CreateOwnerBillReport.php 5.2 KB

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