OwnerStoragePriceModelService.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. if (!$month)$month = date("Y-m");
  100. /** @var \stdClass $model */
  101. if (!$model || !$area || $model->operation) return array(0,null);
  102. if ($area < $model->minimum_area) $area = $model->minimum_area;
  103. if (!isset($GLOBALS["FEE_INFO"]))$GLOBALS["FEE_INFO"] = [
  104. "fee_description" => "",
  105. "tax_rate" => "",
  106. ];
  107. $GLOBALS["FEE_INFO"]["fee_description"] .= "起租面积:".$model->minimum_area;
  108. $days = 1;
  109. switch ($model->timeUnit->name){
  110. case "日":
  111. $days = date('t', strtotime($month."-01"));
  112. $area *= $days;
  113. break;
  114. }
  115. switch ($model->discount_type){
  116. case "按单减免":
  117. $GLOBALS["FEE_INFO"]["fee_description"] .= ",按单减免";
  118. if ($owner_id && $month){
  119. if ($model->timeUnit->name == '月'){
  120. $GLOBALS["FEE_INFO"]["fee_description"] .= ":每".$model->discount_value."单/月 减免1".$model->unit->name ?? 'm²';
  121. $report = OwnerReport::query()->select("id",DB::raw("(to_business_quantity+to_customer_quantity) AS total"))
  122. ->where("owner_id",$owner_id)
  123. ->where("counting_month","like",$month."%")->first();
  124. if ($report && $report->total>=0)$area -= floor($report->total/$model->discount_value);
  125. }else{
  126. $days = date('t', strtotime($month."-01"));
  127. $area *= $days;
  128. $logistics = app("OwnerPriceExpressService")->getBuildLogistic($owner_id);
  129. $logisticSql = "(''";
  130. foreach ($logistics as $logistic)$logisticSql.=",".$logistic;
  131. $logisticSql .= ")";
  132. $GLOBALS["FEE_INFO"]["fee_description"] .= ":每".$model->discount_value."单/".$model->timeUnit->name." 减免1".$model->unit->name ?? 'm²';
  133. for($i=1;$i<=$days;$i++){
  134. $d = $i<10 ? "0".$i : $i;
  135. $query = DB::raw("SELECT COUNT(1) c FROM orders WHERE logistic_id IN {$logisticSql} AND wms_status = ? AND wms_edittime BETWEEN ? AND ?");
  136. $count = DB::selectOne($query,['订单完成',$month."-".$d." 00:00:00",$month."-".$d." 23:59:59"]);
  137. if ($count && $count->c>=0)$area -= floor($count->c/$model->discount_value);
  138. }
  139. }
  140. }
  141. break;
  142. case "固定减免":
  143. $GLOBALS["FEE_INFO"]["fee_description"] .= ",固定减免:每".$model->timeUnit->name."减免".$model->discount_value.($model->unit->name ?? 'm²');
  144. if ($model->timeUnit->name == '月'){
  145. $area -= $model->discount_value;
  146. }else $area -= $days*$model->discount_value;
  147. break;
  148. }
  149. $index = 0;
  150. if ($model->amount_interval){
  151. $total = app("OrderService")->getOrderQuantity($owner_id,false,$month);
  152. for ($i=count($model->amount_interval)-1;$i>=0;$i--){
  153. if ($total>=$model->amount_interval[$i])$index = $i;
  154. }
  155. }
  156. $GLOBALS["FEE_INFO"]["fee_description"] .= ",单价: ".$model->price[$index]."/".($model->unit->name ?? 'm²')."/".$model->timeUnit->name;
  157. $money = $area>0 ? $area*$model->price[$index] : 0;
  158. $taxRate = app("OwnerService")->getTaxRateFee($model, $owner_id);
  159. $taxFee = $money*($taxRate/100);
  160. $GLOBALS["FEE_INFO"]["fee_description"] .= ",税费: ".$taxFee;
  161. $GLOBALS["FEE_INFO"]["tax_rate"] = $taxRate;
  162. return array($money,$taxFee);
  163. }
  164. }