| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- namespace App\Services;
- use App\Owner;
- use App\OwnerPriceOperation;
- use App\OwnerPriceOperationItem;
- use App\Traits\ServiceAppAop;
- use App\OwnerFeeTotal;
- use Illuminate\Database\Eloquent\Builder;
- class OwnerFeeTotalService
- {
- use ServiceAppAop;
- protected $modelClass = OwnerFeeTotal::class;
- /**
- * 生成统计数据
- * @param string|null $counting_month string 统计月份默认为上一个月
- */
- public function record(string $counting_month = null)
- {
- if (is_null($counting_month)) {
- $counting_month = now()->subMonth()->startOfMonth()->toDateString();
- }
- /**
- * 仓储。入库,出库
- * 1. 查到所有货主
- * 2. 遍历货主
- * 3. 获得货主下的计费模型,将计费模型里面的数据转换成“描述”
- * 4. 查询相关表,获得指定计费模型下的费用总和(含税费)
- *
- * 配送,加工,系统使用,杂项
- * 1. 查询对应表的总和即可,不需关联计费模型
- *
- * 理赔
- * 1. 暂不明确
- *
- * 优惠
- * 1. 一个字段,在结算管理-账单却认下添加一个优惠输入的字段
- *
- * 税率
- * 1. 计算得出:总税费/总金额
- */
- //仓储:ownerStoragePriceModels。入库,出库:ownerPriceOperations
- $owners = Owner::query()
- ->with([
- "ownerStoragePriceModels.unit:id,name",
- "ownerStoragePriceModels.timeUnit:id,name",
- "ownerPriceOperations" => function ($query) {
- /** @var Builder $query */
- $query->with(["items" => function ($query) {
- /** @var Builder $query */
- $query->orderByRaw("CASE strategy WHEN '起步' THEN 1 WHEN '默认' THEN 2 WHEN '特征' THEN 3 END");
- }]);
- }])
- ->where('deleted_at', '>=', now()->subMonth()->startOfMonth())
- ->orWhereNull('deleted_at')->get();
- foreach ($owners as $owner) {
- $features = app("FeatureService")->getMapArray();
- OwnerPriceOperation::$features = $features;
- OwnerPriceOperationItem::$features = $features;
- foreach ($owner->ownerPriceOperations as &$operation) {
- $operation["featureFormat"] = $operation->featureFormat;
- $operation["isRejected"] = $operation->type_mark === 0 ? true : false;
- foreach ($operation->items as &$item) {
- $item["featureFormat"] = $item->featureFormat;
- if ($item["strategy"] == "起步") $item["type"] = $item["amount"] ? 0 : 1;
- }
- }
- $information = [
- //仓储
- 'storageFee' => [],
- //入库
- 'storeFee' => [],
- //出库
- 'storeOutFee' => [],
- ];
- //仓储
- foreach ($owner->ownerStoragePriceModels as $ownerStoragePriceModel) {
- /**@var $areaFeeService SettlementBillsAreaFeeService */
- $areaFeeService = app('SettlementBillsAreaFeeService');
- $remark = "起租面积:{$ownerStoragePriceModel->minimum_area},{$ownerStoragePriceModel->price[0]}元/{$ownerStoragePriceModel->unit->name}/{$ownerStoragePriceModel->timeUnit->name}";
- $information['storageFee'][] = [
- 'name' => $ownerStoragePriceModel->name,
- 'remark' => $remark,
- 'id' => $ownerStoragePriceModel->id,
- 'fee' => $areaFeeService->getTotalFee($owner->id, $counting_month)->storage_fee ?? 0,
- 'tax_fee' => $areaFeeService->getTotalFee($owner->id, $counting_month)->storage_tax_fee ?? 0
- ];
- }
- $ownerPriceOperationsGrouped = $owner->ownerPriceOperations->groupBy('operation_type');
- /**@var $storeOutFeeDetailsService OwnerStoreOutFeeDetailService */
- $storeOutFeeDetailsService = app('OwnerStoreOutFeeDetailService');
- $workFeeTotalGrouped = $storeOutFeeDetailsService->getTotalFee($owner->id, $counting_month)->groupBy('owner_price_operation_id');
- //入库
- foreach ($ownerPriceOperationsGrouped['入库'] ?? [] as $ownerPriceOperationsGroupedItem) {
- $information['storeFee'][] = [
- 'name' => $ownerPriceOperationsGroupedItem->name,
- 'remark' => $this->buildPriceRemarks($ownerPriceOperationsGroupedItem),
- 'id' => $ownerPriceOperationsGroupedItem->id,
- 'fee' => $workFeeTotalGrouped[$ownerPriceOperationsGroupedItem->id][0]->work_fee ?? 0,
- 'tax_fee' => $workFeeTotalGrouped[$ownerPriceOperationsGroupedItem->id][0]->work_tax_fee ?? 0,
- ];
- }
- //出库
- foreach ($ownerPriceOperationsGrouped['出库'] ?? [] as $ownerPriceOperationsGroupedItem) {
- $information['storeOutFee'][] = [
- 'name' => $ownerPriceOperationsGroupedItem->name,
- 'remark' => $this->buildPriceRemarks($ownerPriceOperationsGroupedItem),
- 'id' => $ownerPriceOperationsGroupedItem->id,
- 'fee' => $workFeeTotalGrouped[$ownerPriceOperationsGroupedItem->id][0]->work_fee ?? 0,
- 'tax_fee' => $workFeeTotalGrouped[$ownerPriceOperationsGroupedItem->id][0]->work_tax_fee ?? 0,
- ];
- }
- $ownerFeeTotal = [];
- $ownerFeeTotal['information'] = $information;
- //快递费
- /**@var $expressFeeService OwnerLogisticFeeReportService */
- $expressFeeService = app('OwnerLogisticFeeReportService');
- $expressFeeTotal = $expressFeeService->getTotalFee($owner->id, $counting_month);
- $ownerFeeTotal ['logistic_fee'] = $expressFeeTotal->fee;
- $ownerFeeTotal ['logistic_tax_fee'] = $expressFeeTotal->tax_fee;
- //物流作业费
- }
- }
- /**
- * 出库入库描述
- * @param $ownerPriceOperation
- * @return array
- */
- public function buildPriceRemarks($ownerPriceOperation): array
- {
- //起步: 3 件 / 2.7000元 (满减单价: 0-19999 单(2.7元) , 20000-49999 单(2.5元) , 50000-99999 单(2元) , 100000+ 单(1.6元) )
- //默认续费: 1 件 / 0.5000元 (满减单价: 0-19999 单(0.5元) , 20000-49999 单(0.4元) , 50000-99999 单(0.3元) , 100000+ 单(0.2元) )
- $discount_counts = explode(',', $ownerPriceOperation->discount_count);
- $priceRemarks = [];
- foreach ($ownerPriceOperation->items as $operationItem) {
- $discount_prices = explode(',', $operationItem->discount_price);
- $strategy = $operationItem->strategy == '起步' ? '起步' : '默认续费';
- $priceRemark = "{$strategy}: {$operationItem->amount} {$operationItem->unit->name}/{$operationItem->unit_price}元";
- if (!empty($discount_prices)) {
- $priceRemark .= "(满减单价:";
- for ($i = 0; $i < count($discount_counts) - 1; $i++) {
- $next_discount_count = $discount_counts[$i + 1] ?? '+';
- $discount_count = $discount_counts[$i] ?? '';
- $discount_price = $discount_prices[$i] ?? '';
- $priceRemark .= "{$discount_count}-{$next_discount_count} {$operationItem->unit->name} {$discount_price}元,";
- }
- $priceRemark .= ")";
- }
- $priceRemarks[] = $priceRemark;
- }
- return $priceRemarks;
- }
- /**
- * 重新统计
- * @param $owner_id
- * @param $counting_month
- */
- public function restartRecord($owner_id, $counting_month)
- {
- // OwnerBillTotal::query()
- // ->where('owner_id', $owner_id)
- // ->where('counting_month', $counting_month)
- // ->updateOrInsert();
- }
- public function getRecord(): array
- {
- $result = [];
- return $result;
- }
- /**
- * 仓储费
- * @param string|null $counting_month
- * @param $owner_id
- * @return array
- */
- private function getStorageFeeForTotal(?string $counting_month, $owner_id): array
- {
- /** @var $service SettlementBillsAreaFeeService */
- $service = app('SettlementBillsAreaFeeService');
- list($areaReports, $billReport) = $service->get([
- 'counting_month' => $counting_month,
- 'owner_id' => $owner_id,
- 'type' => $service::TYPE,
- ]);
- $storageFee = [
- 'data' => [],
- 'fee' => $billReport->storage_fee,
- ];
- foreach ($areaReports as $areaReport) {
- //起租面积:1.2元/m^3/天
- $remark = '起租面积:'
- . $areaReport->ownerStoragePriceModel->minimum_area . ','
- . $areaReport->ownerStoragePriceModel->price . '/'
- . $areaReport->ownerStoragePriceModel->unit->name .
- '/' . $areaReport->ownerStoragePriceModel->timeUnit->name;
- $storageFee['data'][] = [
- 'name' => $areaReport->ownerStoragePriceModel->name,
- 'remark' => $remark,
- ];
- }
- return $storageFee;
- }
- }
|