CreateOwnerBillReport.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\OwnerAreaReport;
  4. use App\OwnerPriceSystem;
  5. use App\Services\LogService;
  6. use Carbon\Carbon;
  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"])->where("counting_month","like",$year."-".$lastMonth."%")->get();
  48. $map = [];
  49. $mapTax = [];
  50. foreach($areas as $area){
  51. $key = $area->owner_id."_".$area->counting_month;
  52. if (isset($map[$key])){
  53. if (!$area->ownerStoragePriceModel)continue;
  54. list($money,$taxFee) = app('OwnerStoragePriceModelService')
  55. ->calculationAmount($area->ownerStoragePriceModel,$area->accounting_area,$area->owner_id,$area->counting_month);
  56. $map[$key] += $money;
  57. $mapTax[$key] += $taxFee;
  58. }else{
  59. if (!$area->ownerStoragePriceModel)continue;
  60. list($map[$key],$mapTax[$key]) = app('OwnerStoragePriceModelService')
  61. ->calculationAmount($area->ownerStoragePriceModel,$area->accounting_area,$area->owner_id,$area->counting_month);
  62. }
  63. }
  64. foreach (OwnerPriceSystem::query()->with(["timeUnit","taxRate"])->select("owner_id","usage_fee")->whereNull("operation")->orWhere("operation","")->get() as $system){
  65. list($systemFee[$system->owner_id],$systemTaxFee[$system->owner_id]) = $this->systemFee($system,$year."-".$lastMonth);
  66. }
  67. $chunks = array_chunk($billDetails,50);
  68. foreach ($chunks as $bills){
  69. $date = date('Y-m-d H:i:s');
  70. $createOwnerBillReport = [];
  71. foreach ($bills as $bill){
  72. $key = $bill->owner_id."_".$year."-".$lastMonth;
  73. $createOwnerBillReport[] = [
  74. "owner_id" => $bill->owner_id, //项目ID
  75. "counting_month" => $year."-".$lastMonth."-01", //结算月
  76. "work_fee" => $bill->work_fee,
  77. "logistic_fee" => $bill->logistic_fee,
  78. "storage_fee" => $map[$key] ?? 0,
  79. "storage_tax_fee" => $mapTax[$key] ?? 0,
  80. "other_fee" => $systemFee[$bill->owner_id] ?? null,
  81. "other_tax_fee" => $systemTaxFee[$bill->owner_id] ?? null,
  82. "created_at" => $date,
  83. ];
  84. }
  85. LogService::log(__METHOD__,"项目管理-生成确认账单",json_encode($createOwnerBillReport));
  86. DB::table("owner_bill_reports")->insert($createOwnerBillReport);
  87. }
  88. }
  89. /**
  90. * 系统费用
  91. *
  92. * @param OwnerPriceSystem|\stdClass $system
  93. * @param string $month
  94. *
  95. * @return array
  96. */
  97. private function systemFee(OwnerPriceSystem $system,$month)
  98. {
  99. if (!$system->timeUnit)$money = $system->usage_fee;
  100. else{
  101. switch ($system->timeUnit->name){
  102. case "日":
  103. $money = $system->usage_fee*(Carbon::parse($month)->lastOfMonth()->format("d"));
  104. break;
  105. case "单":
  106. $money = $system->usage_fee * (app("OrderService")->getOrderQuantity($system->owner_id));
  107. break;
  108. case "年":
  109. $money = $system->usage_fee/12;
  110. break;
  111. default:
  112. $money = $system->usage_fee;
  113. }
  114. }
  115. if ($system->taxRate)$taxFee = $money * ($system->taxRate->value/100);
  116. else{
  117. $system->load("owner.taxRate");
  118. if ($system->owner && $system->owner->taxRate)$taxFee = $money * ($system->owner->taxRate->value/100);
  119. else $taxFee = null;
  120. }
  121. return array($money,$taxFee);
  122. }
  123. }