CreateOwnerBillReport.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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" =>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"] = $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]) = $this->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. /**
  97. * 系统费用
  98. *
  99. * @param OwnerPriceSystem|\stdClass $system
  100. * @param string $month
  101. *
  102. * @return array
  103. */
  104. private function systemFee(OwnerPriceSystem $system,$month)
  105. {
  106. if (!$system->timeUnit)$money = $system->usage_fee;
  107. else{
  108. switch ($system->timeUnit->name){
  109. case "日":
  110. $money = $system->usage_fee*(Carbon::parse($month)->lastOfMonth()->format("d"));
  111. break;
  112. case "单":
  113. $money = $system->usage_fee * (app("OrderService")->getOrderQuantity($system->owner_id));
  114. break;
  115. case "年":
  116. $money = $system->usage_fee/12;
  117. break;
  118. default:
  119. $money = $system->usage_fee;
  120. }
  121. }
  122. $taxFee = app("OwnerService")->getTaxRateFee($system, $system->owner_id, $money);
  123. return array($money,$taxFee);
  124. }
  125. }