| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace App\Services;
- use App\Order;
- use App\Owner;
- use App\OwnerReport;
- use App\OwnerStoragePriceModel;
- use App\Services\common\QueryService;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Facades\DB;
- use App\Traits\ServiceAppAop;
- class OwnerStoragePriceModelService
- {
- use ServiceAppAop;
- protected $modelClass=OwnerStoragePriceModel::class;
- public function getSelection(array $columns = ["counting_type","using_type"], array $withs = [])
- {
- return OwnerStoragePriceModel::query()->select($columns)->with($withs)->get();
- }
- public function paginate($id = null,array $withs = [])
- {
- $query = OwnerStoragePriceModel::query()->orderByDesc('id')->with($withs);
- if ($id)$query->where("id",$id);
- return $query->paginate($params["paginate"] ?? 50);
- }
- public function create(array $params)
- {
- $params["operation"] = "C";
- return OwnerStoragePriceModel::query()->create($params);
- }
- /**
- * 拷贝目标数据
- *
- * @param object|int $model
- * @param array $values
- * @param array|null $owners
- *
- * @return object|null
- */
- public function copy($model, $values = [], $owners = null)
- {
- if (is_integer($model))$model = OwnerStoragePriceModel::query()->find($model);
- if (!$model)return null;
- $values["operation"] = "U";
- $values["target_id"] = $model->id;
- foreach ($model->getFillable() as $column){
- if (!array_key_exists($column,$values))$values[$column] = $model[$column];
- }
- if ($owners===null){
- $query = DB::raw("SELECT * FROM owner_storage_price_model_owner WHERE owner_storage_price_model_id = {$model->id}");
- $owners = array_column(DB::select($query),"owner_id");
- }
- /** @var OwnerStoragePriceModel $model */
- $model = OwnerStoragePriceModel::query()->create($values);
- $model->owners()->sync($owners);
- return $model;
- }
- /**
- * 审核或恢复目标集
- *
- * @param bool $isAudit
- * @param integer|null|array $ownerId
- * @param integer|null|array $ids
- */
- public function auditOrRecover($isAudit = true, $ownerId = null, $ids = null)
- {
- if (!$ownerId && !$ids)return;
- $result = app(QueryService::class)->priceModelAuditOrRecoverQuery($isAudit,OwnerStoragePriceModel::query(),$ownerId,$ids);
- if ($result["delete"])$this->destroy($result["delete"]);
- if ($result["update"])OwnerStoragePriceModel::query()->whereIn("id",$result["update"])->update(["operation"=>null,"target_id"=>null]);
- }
- public function update(array $params, array $values)
- {
- $query = OwnerStoragePriceModel::query();
- foreach ($params as $column => $value){
- $query->where($column,$value);
- }
- return $query->update($values);
- }
- public function find($id, $withs = [])
- {
- return OwnerStoragePriceModel::query()->with($withs)->find($id);
- }
- public function destroy($id)
- {
- if (is_array($id)){
- $query = "IN (";
- for ($i=0;$i<count($id)-1;$i++)$query .= "?,";
- $query .= "?)";
- }else{
- $query = "= ?";
- $id = [$id];
- }
- DB::delete(DB::raw("DELETE FROM owner_storage_price_model_owner WHERE owner_storage_price_model_id ".$query),$id);
- return OwnerStoragePriceModel::destroy($id);
- }
- //暂时不考虑单位换算问题:后期可能存在多单位换算,此处仅视为单位为 m²,m³
- public function calculationAmount(OwnerStoragePriceModel $model, $area, $owner_id = null, $month = null):array
- {
- if (!$month)$month = date("Y-m");
- /** @var \stdClass $model */
- if (!$model || !$area || $model->operation) return array(0,null);
- if ($area < $model->minimum_area) $area = $model->minimum_area;
- if (!isset($GLOBALS["FEE_INFO"]))$GLOBALS["FEE_INFO"] = [
- "fee_description" => "",
- "tax_rate" => "",
- ];
- $GLOBALS["FEE_INFO"]["fee_description"] .= "起租面积:".$model->minimum_area;
- $days = 1;
- switch ($model->timeUnit->name){
- case "日":
- $days = date('t', strtotime($month."-01"));
- $area *= $days;
- break;
- }
- switch ($model->discount_type){
- case "按单减免":
- $GLOBALS["FEE_INFO"]["fee_description"] .= ",按单减免";
- if ($owner_id && $month){
- if ($model->timeUnit->name == '月'){
- $GLOBALS["FEE_INFO"]["fee_description"] .= ":每".$model->discount_value."单/月 减免1".$model->unit->name ?? 'm²';
- $report = OwnerReport::query()->select("id",DB::raw("(to_business_quantity+to_customer_quantity) AS total"))
- ->where("owner_id",$owner_id)
- ->where("counting_month","like",$month."%")->first();
- if ($report && $report->total>=0)$area -= floor($report->total/$model->discount_value);
- }else{
- $days = date('t', strtotime($month."-01"));
- $area *= $days;
- $logistics = app("OwnerPriceExpressService")->getBuildLogistic($owner_id);
- $logisticSql = "(''";
- foreach ($logistics as $logistic)$logisticSql.=",".$logistic;
- $logisticSql .= ")";
- $GLOBALS["FEE_INFO"]["fee_description"] .= ":每".$model->discount_value."单/".$model->timeUnit->name." 减免1".$model->unit->name ?? 'm²';
- for($i=1;$i<=$days;$i++){
- $d = $i<10 ? "0".$i : $i;
- $query = DB::raw("SELECT COUNT(1) c FROM orders WHERE logistic_id IN {$logisticSql} AND wms_status = ? AND wms_edittime BETWEEN ? AND ?");
- $count = DB::selectOne($query,['订单完成',$month."-".$d." 00:00:00",$month."-".$d." 23:59:59"]);
- if ($count && $count->c>=0)$area -= floor($count->c/$model->discount_value);
- }
- }
- }
- break;
- case "固定减免":
- $GLOBALS["FEE_INFO"]["fee_description"] .= ",固定减免:每".$model->timeUnit->name."减免".$model->discount_value.($model->unit->name ?? 'm²');
- if ($model->timeUnit->name == '月'){
- $area -= $model->discount_value;
- }else $area -= $days*$model->discount_value;
- break;
- }
- $index = 0;
- if ($model->amount_interval){
- $total = app("OrderService")->getOrderQuantity($owner_id,false,$month);
- for ($i=count($model->amount_interval)-1;$i>=0;$i--){
- if ($total>=$model->amount_interval[$i])$index = $i;
- }
- }
- $GLOBALS["FEE_INFO"]["fee_description"] .= ",单价: ".$model->price[$index]."/".($model->unit->name ?? 'm²')."/".$model->timeUnit->name;
- $money = $area>0 ? $area*$model->price[$index] : 0;
- $taxRate = app("OwnerService")->getTaxRateFee($model, $owner_id);
- $taxFee = $money*($taxRate/100);
- $GLOBALS["FEE_INFO"]["fee_description"] .= ",税费: ".$taxFee;
- $GLOBALS["FEE_INFO"]["tax_rate"] = $taxRate;
- return array($money,$taxFee);
- }
- }
|