OwnerFeeTotalService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace App\Services;
  3. use App\Owner;
  4. use App\OwnerPriceOperation;
  5. use App\OwnerPriceOperationItem;
  6. use App\Traits\ServiceAppAop;
  7. use App\OwnerFeeTotal;
  8. use Illuminate\Database\Eloquent\Builder;
  9. class OwnerFeeTotalService
  10. {
  11. use ServiceAppAop;
  12. protected $modelClass = OwnerFeeTotal::class;
  13. /**
  14. * 生成统计数据
  15. * @param string|null $counting_month string 统计月份默认为上一个月
  16. */
  17. public function record(string $counting_month = null)
  18. {
  19. if (is_null($counting_month)) {
  20. $counting_month = now()->subMonth()->startOfMonth()->toDateString();
  21. }
  22. /**
  23. * 仓储。入库,出库
  24. * 1. 查到所有货主
  25. * 2. 遍历货主
  26. * 3. 获得货主下的计费模型,将计费模型里面的数据转换成“描述”
  27. * 4. 查询相关表,获得指定计费模型下的费用总和(含税费)
  28. *
  29. * 配送,加工,系统使用,杂项
  30. * 1. 查询对应表的总和即可,不需关联计费模型
  31. *
  32. * 理赔
  33. * 1. 暂不明确
  34. *
  35. * 优惠
  36. * 1. 一个字段,在结算管理-账单却认下添加一个优惠输入的字段
  37. *
  38. * 税率
  39. * 1. 计算得出:总税费/总金额
  40. */
  41. //仓储:ownerStoragePriceModels。入库,出库:ownerPriceOperations
  42. $owners = Owner::query()
  43. ->with([
  44. "ownerStoragePriceModels.unit:id,name",
  45. "ownerStoragePriceModels.timeUnit:id,name",
  46. "ownerPriceOperations" => function ($query) {
  47. /** @var Builder $query */
  48. $query->with(["items" => function ($query) {
  49. /** @var Builder $query */
  50. $query->orderByRaw("CASE strategy WHEN '起步' THEN 1 WHEN '默认' THEN 2 WHEN '特征' THEN 3 END");
  51. }]);
  52. }])
  53. ->where('deleted_at', '>=', now()->subMonth()->startOfMonth())
  54. ->orWhereNull('deleted_at')->get();
  55. foreach ($owners as $owner) {
  56. $features = app("FeatureService")->getMapArray();
  57. OwnerPriceOperation::$features = $features;
  58. OwnerPriceOperationItem::$features = $features;
  59. foreach ($owner->ownerPriceOperations as &$operation) {
  60. $operation["featureFormat"] = $operation->featureFormat;
  61. $operation["isRejected"] = $operation->type_mark === 0 ? true : false;
  62. foreach ($operation->items as &$item) {
  63. $item["featureFormat"] = $item->featureFormat;
  64. if ($item["strategy"] == "起步") $item["type"] = $item["amount"] ? 0 : 1;
  65. }
  66. }
  67. $information = [
  68. //仓储
  69. 'storageFee' => [],
  70. //入库
  71. 'storeFee' => [],
  72. //出库
  73. 'storeOutFee' => [],
  74. ];
  75. //仓储
  76. foreach ($owner->ownerStoragePriceModels as $ownerStoragePriceModel) {
  77. /**@var $areaFeeService SettlementBillsAreaFeeService */
  78. $areaFeeService = app('SettlementBillsAreaFeeService');
  79. $remark = "起租面积:{$ownerStoragePriceModel->minimum_area},{$ownerStoragePriceModel->price[0]}元/{$ownerStoragePriceModel->unit->name}/{$ownerStoragePriceModel->timeUnit->name}";
  80. $information['storageFee'][] = [
  81. 'name' => $ownerStoragePriceModel->name,
  82. 'remark' => $remark,
  83. 'id' => $ownerStoragePriceModel->id,
  84. 'fee' => $areaFeeService->getTotalFee($owner->id, $counting_month)->storage_fee ?? 0,
  85. 'tax_fee' => $areaFeeService->getTotalFee($owner->id, $counting_month)->storage_tax_fee ?? 0
  86. ];
  87. }
  88. $ownerPriceOperationsGrouped = $owner->ownerPriceOperations->groupBy('operation_type');
  89. /**@var $storeOutFeeDetailsService OwnerStoreOutFeeDetailService */
  90. $storeOutFeeDetailsService = app('OwnerStoreOutFeeDetailService');
  91. $workFeeTotalGrouped = $storeOutFeeDetailsService->getTotalFee($owner->id, $counting_month)->groupBy('owner_price_operation_id');
  92. //入库
  93. foreach ($ownerPriceOperationsGrouped['入库'] ?? [] as $ownerPriceOperationsGroupedItem) {
  94. $information['storeFee'][] = [
  95. 'name' => $ownerPriceOperationsGroupedItem->name,
  96. 'remark' => $this->buildPriceRemarks($ownerPriceOperationsGroupedItem),
  97. 'id' => $ownerPriceOperationsGroupedItem->id,
  98. 'fee' => $workFeeTotalGrouped[$ownerPriceOperationsGroupedItem->id][0]->work_fee ?? 0,
  99. 'tax_fee' => $workFeeTotalGrouped[$ownerPriceOperationsGroupedItem->id][0]->work_tax_fee ?? 0,
  100. ];
  101. }
  102. //出库
  103. foreach ($ownerPriceOperationsGrouped['出库'] ?? [] as $ownerPriceOperationsGroupedItem) {
  104. $information['storeOutFee'][] = [
  105. 'name' => $ownerPriceOperationsGroupedItem->name,
  106. 'remark' => $this->buildPriceRemarks($ownerPriceOperationsGroupedItem),
  107. 'id' => $ownerPriceOperationsGroupedItem->id,
  108. 'fee' => $workFeeTotalGrouped[$ownerPriceOperationsGroupedItem->id][0]->work_fee ?? 0,
  109. 'tax_fee' => $workFeeTotalGrouped[$ownerPriceOperationsGroupedItem->id][0]->work_tax_fee ?? 0,
  110. ];
  111. }
  112. $ownerFeeTotal = [];
  113. $ownerFeeTotal['information'] = $information;
  114. //快递费
  115. /**@var $expressFeeService OwnerLogisticFeeReportService */
  116. $expressFeeService = app('OwnerLogisticFeeReportService');
  117. $expressFeeTotal = $expressFeeService->getTotalFee($owner->id, $counting_month);
  118. $ownerFeeTotal ['logistic_fee'] = $expressFeeTotal->fee;
  119. $ownerFeeTotal ['logistic_tax_fee'] = $expressFeeTotal->tax_fee;
  120. //物流作业费
  121. }
  122. }
  123. /**
  124. * 出库入库描述
  125. * @param $ownerPriceOperation
  126. * @return array
  127. */
  128. public function buildPriceRemarks($ownerPriceOperation): array
  129. {
  130. //起步: 3 件 / 2.7000元 (满减单价: 0-19999 单(2.7元) , 20000-49999 单(2.5元) , 50000-99999 单(2元) , 100000+ 单(1.6元) )
  131. //默认续费: 1 件 / 0.5000元 (满减单价: 0-19999 单(0.5元) , 20000-49999 单(0.4元) , 50000-99999 单(0.3元) , 100000+ 单(0.2元) )
  132. $discount_counts = explode(',', $ownerPriceOperation->discount_count);
  133. $priceRemarks = [];
  134. foreach ($ownerPriceOperation->items as $operationItem) {
  135. $discount_prices = explode(',', $operationItem->discount_price);
  136. $strategy = $operationItem->strategy == '起步' ? '起步' : '默认续费';
  137. $priceRemark = "{$strategy}: {$operationItem->amount} {$operationItem->unit->name}/{$operationItem->unit_price}元";
  138. if (!empty($discount_prices)) {
  139. $priceRemark .= "(满减单价:";
  140. for ($i = 0; $i < count($discount_counts) - 1; $i++) {
  141. $next_discount_count = $discount_counts[$i + 1] ?? '+';
  142. $discount_count = $discount_counts[$i] ?? '';
  143. $discount_price = $discount_prices[$i] ?? '';
  144. $priceRemark .= "{$discount_count}-{$next_discount_count} {$operationItem->unit->name} {$discount_price}元,";
  145. }
  146. $priceRemark .= ")";
  147. }
  148. $priceRemarks[] = $priceRemark;
  149. }
  150. return $priceRemarks;
  151. }
  152. /**
  153. * 重新统计
  154. * @param $owner_id
  155. * @param $counting_month
  156. */
  157. public function restartRecord($owner_id, $counting_month)
  158. {
  159. // OwnerBillTotal::query()
  160. // ->where('owner_id', $owner_id)
  161. // ->where('counting_month', $counting_month)
  162. // ->updateOrInsert();
  163. }
  164. public function getRecord(): array
  165. {
  166. $result = [];
  167. return $result;
  168. }
  169. /**
  170. * 仓储费
  171. * @param string|null $counting_month
  172. * @param $owner_id
  173. * @return array
  174. */
  175. private function getStorageFeeForTotal(?string $counting_month, $owner_id): array
  176. {
  177. /** @var $service SettlementBillsAreaFeeService */
  178. $service = app('SettlementBillsAreaFeeService');
  179. list($areaReports, $billReport) = $service->get([
  180. 'counting_month' => $counting_month,
  181. 'owner_id' => $owner_id,
  182. 'type' => $service::TYPE,
  183. ]);
  184. $storageFee = [
  185. 'data' => [],
  186. 'fee' => $billReport->storage_fee,
  187. ];
  188. foreach ($areaReports as $areaReport) {
  189. //起租面积:1.2元/m^3/天
  190. $remark = '起租面积:'
  191. . $areaReport->ownerStoragePriceModel->minimum_area . ','
  192. . $areaReport->ownerStoragePriceModel->price . '/'
  193. . $areaReport->ownerStoragePriceModel->unit->name .
  194. '/' . $areaReport->ownerStoragePriceModel->timeUnit->name;
  195. $storageFee['data'][] = [
  196. 'name' => $areaReport->ownerStoragePriceModel->name,
  197. 'remark' => $remark,
  198. ];
  199. }
  200. return $storageFee;
  201. }
  202. }