OwnerStoragePriceModelService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Services;
  3. use App\Order;
  4. use App\Owner;
  5. use App\OwnerReport;
  6. use App\OwnerStoragePriceModel;
  7. use App\Services\common\QueryService;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Support\Facades\DB;
  10. use App\Traits\ServiceAppAop;
  11. class OwnerStoragePriceModelService
  12. {
  13. use ServiceAppAop;
  14. protected $modelClass=OwnerStoragePriceModel::class;
  15. public function getSelection(array $columns = ["counting_type","using_type"], array $withs = [])
  16. {
  17. return OwnerStoragePriceModel::query()->select($columns)->with($withs)->get();
  18. }
  19. public function paginate($id = null,array $withs = [])
  20. {
  21. $query = OwnerStoragePriceModel::query()->orderByDesc('id')->with($withs);
  22. if ($id)$query->where("id",$id);
  23. return $query->paginate($params["paginate"] ?? 50);
  24. }
  25. public function create(array $params)
  26. {
  27. $params["operation"] = "C";
  28. return OwnerStoragePriceModel::query()->create($params);
  29. }
  30. /**
  31. * 拷贝目标数据
  32. *
  33. * @param object|int $model
  34. * @param array $values
  35. * @param array|null $owners
  36. *
  37. * @return object|null
  38. */
  39. public function copy($model, $values = [], $owners = null)
  40. {
  41. if (is_integer($model))$model = OwnerStoragePriceModel::query()->find($model);
  42. if (!$model)return null;
  43. $values["operation"] = "U";
  44. $values["target_id"] = $model->id;
  45. foreach ($model->getFillable() as $column){
  46. if (!array_key_exists($column,$values))$values[$column] = $model[$column];
  47. }
  48. if ($owners===null){
  49. $query = DB::raw("SELECT * FROM owner_storage_price_model_owner WHERE owner_storage_price_model_id = {$model->id}");
  50. $owners = array_column(DB::select($query),"owner_id");
  51. }
  52. /** @var OwnerStoragePriceModel $model */
  53. $model = OwnerStoragePriceModel::query()->create($values);
  54. $model->owners()->sync($owners);
  55. return $model;
  56. }
  57. /**
  58. * 审核或恢复目标集
  59. *
  60. * @param bool $isAudit
  61. * @param integer|null|array $ownerId
  62. * @param integer|null|array $ids
  63. */
  64. public function auditOrRecover($isAudit = true, $ownerId = null, $ids = null)
  65. {
  66. if (!$ownerId && !$ids)return;
  67. $result = app(QueryService::class)->priceModelAuditOrRecoverQuery($isAudit,OwnerStoragePriceModel::query(),$ownerId,$ids);
  68. if ($result["delete"])$this->destroy($result["delete"]);
  69. if ($result["update"])OwnerStoragePriceModel::query()->whereIn("id",$result["update"])->update(["operation"=>null,"target_id"=>null]);
  70. }
  71. public function update(array $params, array $values)
  72. {
  73. $query = OwnerStoragePriceModel::query();
  74. foreach ($params as $column => $value){
  75. $query->where($column,$value);
  76. }
  77. return $query->update($values);
  78. }
  79. public function find($id, $withs = [])
  80. {
  81. return OwnerStoragePriceModel::query()->with($withs)->find($id);
  82. }
  83. public function destroy($id)
  84. {
  85. if (is_array($id)){
  86. $query = "IN (";
  87. for ($i=0;$i<count($id)-1;$i++)$query .= "?,";
  88. $query .= "?)";
  89. }else{
  90. $query = "= ?";
  91. $id = [$id];
  92. }
  93. DB::delete(DB::raw("DELETE FROM owner_storage_price_model_owner WHERE owner_storage_price_model_id ".$query),$id);
  94. return OwnerStoragePriceModel::destroy($id);
  95. }
  96. //暂时不考虑单位换算问题:后期可能存在多单位换算,此处仅视为单位为 m²,m³
  97. public function calculationAmount(OwnerStoragePriceModel $model, $area, $owner_id = null, $month = null):array
  98. {
  99. /** @var \stdClass $model */
  100. if (!$model || !$area || $model->operation) return array(0,null);
  101. if ($area < $model->minimum_area) $area = $model->minimum_area;
  102. $GLOBALS["FEE_INFO"]["fee_description"] .= "起租面积:".$model->minimum_area;
  103. switch ($model->discount_type){
  104. case "按单减免":
  105. $GLOBALS["FEE_INFO"]["fee_description"] .= ",按单减免";
  106. if ($owner_id && $month){
  107. if ($model->timeUnit->name == '月'){
  108. $GLOBALS["FEE_INFO"]["fee_description"] .= ":每".$model->discount_value."单/月 减免1".$model->unit->name ?? 'm²';
  109. $report = OwnerReport::query()->select("id",DB::raw("(to_business_quantity+to_customer_quantity) AS total"))
  110. ->where("owner_id",$owner_id)
  111. ->where("counting_month","like",$month."%")->first();
  112. if ($report && $report->total>=0)$area -= floor($report->total/$model->discount_value);
  113. }else{
  114. $days = date('t', strtotime($month."-01"));
  115. $area *= $days;
  116. $logistics = app("OwnerPriceExpressService")->getBuildLogistic($owner_id);
  117. $logisticSql = "(''";
  118. foreach ($logistics as $logistic)$logisticSql.=",".$logistic;
  119. $logisticSql .= ")";
  120. $GLOBALS["FEE_INFO"]["fee_description"] .= ":每".$model->discount_value."单/".$model->timeUnit->name." 减免1".$model->unit->name ?? 'm²';
  121. for($i=1;$i<=$days;$i++){
  122. $d = $i<10 ? "0".$i : $i;
  123. $query = DB::raw("SELECT COUNT(1) c FROM orders WHERE logistic_id IN {$logisticSql} AND wms_status = ? AND wms_edittime BETWEEN ? AND ?");
  124. $count = DB::selectOne($query,['订单完成',$month."-".$d." 00:00:00",$month."-".$d." 23:59:59"]);
  125. if ($count && $count->c>=0)$area -= floor($count->c/$model->discount_value);
  126. }
  127. }
  128. }
  129. break;
  130. case "固定减免":
  131. $GLOBALS["FEE_INFO"]["fee_description"] .= ",固定减免:每".$model->timeUnit->name."减免".$model->discount_value.($model->unit->name ?? 'm²');
  132. if ($model->timeUnit->name == '月'){
  133. $area -= $model->discount_value;
  134. }else{
  135. $days = date('t', strtotime($month."-01"));
  136. $area *= $days;
  137. $area -= $days*$model->discount_value;
  138. }
  139. break;
  140. }
  141. $index = 0;
  142. if ($model->amount_interval){
  143. $total = app("OrderService")->getOrderQuantity($owner_id);
  144. for ($i=count($model->amount_interval)-1;$i>=0;$i--){
  145. if ($total>=$model->amount_interval[$i])$index = $i;
  146. }
  147. }
  148. $GLOBALS["FEE_INFO"]["fee_description"] .= ",单价: ".$model->price[$index]."/".($model->unit->name ?? 'm²')."/".$model->timeUnit->name;
  149. $money = $area>0 ? $area*$model->price[$index] : 0;
  150. $taxFee = app("OwnerService")->getTaxRateFee($model, $owner_id, $money);
  151. $GLOBALS["FEE_INFO"]["fee_description"] .= ",税费: ".$taxFee;
  152. return array($money,$taxFee);
  153. }
  154. }