StatSaleClass.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace bizGhs\stat\classes;
  3. use biz\stat\classes\StatCgClass;
  4. use biz\stat\classes\StatStockInClass;
  5. use biz\stat\classes\StatStockOutClass;
  6. use bizGhs\expend\classes\ExpendClass;
  7. use bizGhs\staff\classes\SalaryClass;
  8. use bizHd\order\classes\OrderClass as HdOrderClass;
  9. use bizGhs\order\classes\OrderClass;
  10. use common\components\dateUtil;
  11. use common\components\dict;
  12. use Yii;
  13. use bizGhs\base\classes\BaseClass;
  14. class StatSaleClass extends BaseClass
  15. {
  16. public static $baseFile = '\bizGhs\stat\models\StatSale';
  17. public static function profile($mainId)
  18. {
  19. //收入:批发开单、零售开单(含门店、美团和微信客服)、出库
  20. //支出:采购、调拨入库、批发退款、零售退款、店租、水电、伙食
  21. $get = Yii::$app->request->get();
  22. $where = [];
  23. $where['mainId'] = $mainId;
  24. $searchTime = 'today';
  25. if (!empty($searchTime)) {
  26. $startTime = $get['startTime'] ?? '';
  27. $endTime = $get['endTime'] ?? '';
  28. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  29. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  30. }
  31. //出库
  32. $stockOut = StatStockOutClass::getSum($where);
  33. //采购入库
  34. $cg = StatCgClass::getSum($where);
  35. //调拨入库
  36. $stockIn = StatStockInClass::getSum($where);
  37. $totalOrderNum = 0;
  38. $todayDate = date('Y-m-d');
  39. $todayStartTime = $todayDate . ' 00:00:00';
  40. $todayEndTime = $todayDate . ' 23:59:59';
  41. //批发开单
  42. $pfIncome = 0;
  43. $pfSendCost = 0;
  44. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  45. $where['payTime'] = ['between', [$todayStartTime, $todayEndTime]];
  46. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  47. if (!empty($ghsOrderList)) {
  48. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  49. $actPrice = $ghsOrder['actPrice'] ?? 0;
  50. $sendCost = $ghsOrder['sendCost'] ?? 0;
  51. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  52. $pfSendCost = bcadd($pfSendCost, $sendCost, 2);
  53. $totalOrderNum++;
  54. }
  55. }
  56. //零售开单
  57. $lsIncome = 0;
  58. $lsSendCost = 0;
  59. $lsServiceFee = 0;
  60. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  61. $hdWhere['payTime'] = ['between', [$todayStartTime, $todayEndTime]];
  62. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  63. foreach ($hdOrderList as $hdKey => $hdOrder) {
  64. $actPrice = $hdOrder['actPrice'] ?? 0;
  65. $serviceFee = $hdOrder['serviceFee'] ?? 0;
  66. $sendCost = $hdOrder['sendCost'] ?? 0;
  67. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  68. $lsSendCost = bcadd($lsSendCost, $sendCost, 2);
  69. $lsServiceFee = bcadd($lsServiceFee, $serviceFee, 2);
  70. $totalOrderNum++;
  71. }
  72. $condition = ['mainId' => $mainId, 'addTime' => ['between', [$todayStartTime, $todayEndTime]],];
  73. $salary = 0;
  74. $salaryList = SalaryClass::getAllByCondition($condition, null, '*', null, true);
  75. if (!empty($salaryList)) {
  76. foreach ($salaryList as $item) {
  77. $salary = bcadd($salary, $item->amount, 2);
  78. }
  79. }
  80. $expendList = ExpendClass::getAllByCondition($condition, null, '*', null, true);
  81. $expendTypeMap = dict::getDict('expendTypeMap');
  82. $expendAmount = [];
  83. if (!empty($expendList)) {
  84. foreach ($expendList as $item) {
  85. $type = $item->type ?? 0;
  86. $amount = $item->amount ?? 0;
  87. $name = $expendTypeMap[$type] ?? '暂无';
  88. if (isset($expendAmount[$type]['amount']) == false) {
  89. $expendAmount[$type]['amount'] = 0;
  90. }
  91. $expendAmount[$type]['name'] = $name;
  92. $expendAmount[$type]['amount'] = floatval(bcadd($expendAmount[$type]['amount'], $amount, 2));
  93. }
  94. }
  95. $income = [
  96. ['name' => '批发销售', 'amount' => floatval($pfIncome)],
  97. ['name' => '零售销售', 'amount' => floatval($lsIncome)],
  98. ['name' => '调拨出库', 'amount' => floatval($stockOut)],
  99. ];
  100. $expend = [
  101. ['name' => '批发运费', 'amount' => floatval($pfSendCost)],
  102. ['name' => '零售运费', 'amount' => floatval($lsSendCost)],
  103. ['name' => '美团手续费', 'amount' => floatval($lsServiceFee)],
  104. ['name' => '采购', 'amount' => floatval($cg)],
  105. ['name' => '调拨入库', 'amount' => floatval($stockIn)],
  106. ['name' => '员工工资', 'amount' => floatval($salary)],
  107. ];
  108. if (!empty($expendAmount)) {
  109. foreach ($expendAmount as $it) {
  110. $expend[] = $it;
  111. }
  112. }
  113. $balance = 0;
  114. foreach ($income as $it) {
  115. $amount = $it['amount'] ?? 0;
  116. $balance = bcadd($amount, $balance, 2);
  117. }
  118. foreach ($expend as $it) {
  119. $amount = $it['amount'] ?? 0;
  120. $balance = bcsub($balance, $amount, 2);
  121. }
  122. return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum];
  123. }
  124. }