OwnerAreaReportService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerAreaReport;
  4. use App\OwnerBillReport;
  5. use App\OwnerPriceSystem;
  6. use App\Services\common\QueryService;
  7. use Carbon\Carbon;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\DB;
  11. use App\Traits\ServiceAppAop;
  12. class OwnerAreaReportService
  13. {
  14. use ServiceAppAop;
  15. protected $modelClass=OwnerAreaReport::class;
  16. /**
  17. * @param Builder $builder
  18. * @param array $params
  19. * @return Builder
  20. */
  21. private function query(Builder $builder, array $params)
  22. {
  23. $columnQueryRules = [
  24. 'counting_month_start' => ['alias' => 'counting_month', 'startDate' => '-01'],
  25. 'counting_month_end' => ['alias' => 'counting_month', 'endDate' => '-31'],
  26. 'owner_id' => ['multi' => ','],
  27. ];
  28. if ($params["customer_id"] ?? false){
  29. $builder->whereHas('owner',function ($query)use(&$params){
  30. /** @var Builder $query*/
  31. $query->where("customer_id",$params["customer_id"]);
  32. unset($params["customer_id"]);
  33. });
  34. }
  35. return app(QueryService::class)->query($params, $builder, $columnQueryRules);
  36. }
  37. public function paginate(array $params, array $withs = null)
  38. {
  39. $areas = OwnerAreaReport::query()->whereIn("owner_id",app("OwnerService")->getQuery())->orderByDesc('id');
  40. if ($withs)$areas->with($withs);
  41. return $this->query($areas,$params)->paginate($params["paginate"] ?? 50);
  42. }
  43. /**
  44. * 面积变更会去对比账单更新账单 考虑到后续账单变更有多种渠道此处加排他锁
  45. *
  46. * @param array $params
  47. * @param array $values
  48. * @return bool|string
  49. */
  50. public function update(array $params, array $values)
  51. {
  52. DB::beginTransaction();
  53. $area = $this->query(OwnerAreaReport::query(),$params)->with("ownerStoragePriceModel")->lockForUpdate()->first();
  54. try{
  55. if ($values["accounting_area"] ?? null && $area->accounting_area != $values["accounting_area"]){
  56. $report = OwnerBillReport::query()->where("owner_id",$area->owner_id)
  57. ->where("counting_month",'like',$area->counting_month."%")->first();
  58. if ($report){
  59. if (!$area->ownerStoragePriceModel)return false;
  60. list($storeFee,$taxFee) = app("OwnerStoragePriceModelService")->calculationAmount($area->ownerStoragePriceModel, $values["accounting_area"], $area->owner_id, $area->counting_month);
  61. $up = ["storage_fee"=>$storeFee,"storage_tax_fee"=>$taxFee];
  62. if ($report->confirm_fee !== null || $report->confirmed == '是'){
  63. $initial = $report->work_fee + $report->logistic_fee + $storeFee;
  64. $up["difference"] = $initial - $report->confirm_fee;
  65. }
  66. $report->update($up);
  67. }
  68. }
  69. $area->update($values);
  70. DB::commit();
  71. return true;
  72. }catch (\Exception $e){
  73. DB::rollBack();
  74. return $e->getMessage();
  75. }
  76. }
  77. public function get(array $params, array $withs = null)
  78. {
  79. $query = OwnerAreaReport::query()->orderByDesc('id');
  80. if ($withs)$query->with($withs);
  81. return $this->query($query,$params)->get();
  82. }
  83. //锁定面积
  84. public function lockArea($id = null, $ownerId = null, $countingMonth = null)
  85. {
  86. if (!$id && !$ownerId && !$countingMonth)return false;
  87. $query = OwnerAreaReport::query()->where("status","编辑中");
  88. if ($id)$query->where("id",$id);
  89. if ($ownerId)$query->where("owner_id",$ownerId);
  90. if ($countingMonth)$query->where("counting_month","like",$countingMonth."%");
  91. return $query->update(["status"=>"已完成"]);
  92. }
  93. //根据货主生成盘点面积记录
  94. public function notExistToInsert($owners)
  95. {
  96. if (!$owners)return;
  97. $reports = OwnerAreaReport::query()
  98. ->where("counting_month",">=",date("Y-m")."-01")
  99. ->whereIn("owner_id",array_column(is_array($owners) ? $owners : $owners->toArray(),"id"))->get();
  100. $sign = [];
  101. foreach ($reports as $report)$sign[$report->owner_id."_".$report->owner_storage_price_model_id] = true;
  102. $month = date('Y-m-d');
  103. $date = date('Y-m-d H:i:s');
  104. $createOwnerAreaReport = [];
  105. foreach ($owners as $owner){
  106. if (!$owner->ownerStoragePriceModels)continue;
  107. foreach ($owner->ownerStoragePriceModels as $model){
  108. $key = $owner->id."_".$model->id;
  109. if (!isset($sign[$key])) $createOwnerAreaReport[] = [
  110. "owner_id" => $owner->id,
  111. "counting_month" => $month,
  112. "user_owner_group_id" => $owner->user_owner_group_id,
  113. "created_at" => $date,
  114. "owner_storage_price_model_id" => $model->id,
  115. ];
  116. }
  117. }
  118. if ($createOwnerAreaReport){
  119. DB::table("owner_area_reports")->insert($createOwnerAreaReport);
  120. LogService::log(__METHOD__,"项目管理-生成盘点记录",json_encode($createOwnerAreaReport));
  121. }
  122. }
  123. /**
  124. * 系统费用
  125. *
  126. * @param OwnerPriceSystem|\stdClass $system
  127. * @param string $month
  128. *
  129. * @return array
  130. */
  131. public function systemFee(OwnerPriceSystem $system,string $month):array
  132. {
  133. if (!$system->timeUnit)$money = $system->usage_fee;
  134. else{
  135. switch ($system->timeUnit->name){
  136. case "日":
  137. $money = $system->usage_fee*(Carbon::parse($month)->lastOfMonth()->format("d"));
  138. break;
  139. case "单":
  140. $money = $system->usage_fee * (app("OrderService")->getOrderQuantity($system->owner_id,false,$month));
  141. break;
  142. case "年":
  143. $money = $system->usage_fee/12;
  144. break;
  145. default:
  146. $money = $system->usage_fee;
  147. }
  148. }
  149. $taxFee = app("OwnerService")->getTaxRateFee($system, $system->owner_id, $money);
  150. return array($money,$taxFee);
  151. }
  152. }