MoneyLog.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\api\controller;
  3. use DateTime;
  4. use app\common\model\MoneyLog as MonuyModel;
  5. use app\common\model\StockLog;
  6. use think\exception\ValidateException;
  7. use app\api\validate\Money as MoneyValidate;
  8. use app\common\model\StockDetail;
  9. use think\facade\Db;
  10. //记账记录表
  11. class MoneyLog extends Base
  12. {
  13. //记账记录
  14. public function moneylog(MonuyModel $monuyModel)
  15. {
  16. $time = $this->request->post('time/s', '');
  17. $count = $monuyModel::getCountMonthBalance($time);
  18. $list = $monuyModel->with('users')->where('status', MonuyModel::STATUS_NORMAL);
  19. if(!empty($time)) $list = $list->whereMonth('create_date', $time);
  20. $list = $list->order('create_date desc,id desc')->paginate(10);
  21. $this->success('ok', compact('list', 'count'));
  22. }
  23. //记账统计每天
  24. public function getCountDay(MonuyModel $monuyModel)
  25. {
  26. $time = $this->request->post('time/s', '');
  27. $result = $monuyModel::getCountDayBalance($time);
  28. $this->success('ok', $result);
  29. }
  30. //记账统计
  31. public function getCount(MonuyModel $monuyModel)
  32. {
  33. $type = $this->request->post('type/d, 1');
  34. $time = $this->request->post('time/s', date('Y-m'));
  35. //月统计
  36. if($type == 1){
  37. $result = $monuyModel::getCountMonthBalance($time);
  38. //日均支出
  39. $day = getDaysOfMonth($time);
  40. $result['day_expenditure'] = ($result['expenditure'] > 0)? bcdiv($result['expenditure'], $day, 2): 0;
  41. }else{
  42. $result = $monuyModel::getCountYearBalance($time);
  43. //日均支出
  44. $day = getDaysOfYear($time);
  45. $result['day_expenditure'] = ($result['expenditure'] > 0)? bcdiv($result['expenditure'], $day, 2): 0;
  46. }
  47. $this->success('ok', $result);
  48. }
  49. //添加记账
  50. public function money(MonuyModel $monuyModel)
  51. {
  52. $data = $this->request->post();
  53. $result = false;
  54. Db::startTrans();
  55. try {
  56. validate(MoneyValidate::class)->scene('add')->check($data);
  57. //发货数据
  58. $data['user_id'] = $this->userinfo['id'];
  59. $data['create_month'] = date('Ym', strtotime($data['create_date']));;//创建月份
  60. $result = $monuyModel::create($data);
  61. Db::commit();
  62. }catch (ValidateException $e) {
  63. Db::rollback();
  64. return $this->error($e->getError());
  65. } catch (\Exception $e) {
  66. Db::rollback();
  67. $this->error($e->getMessage());
  68. }
  69. if ($result === false) {
  70. $this->error(__('没有新增任何数据'));
  71. }
  72. $this->success();
  73. }
  74. //账单编辑
  75. public function moneyedit(MonuyModel $monuyModel)
  76. {
  77. $data = $this->request->post();
  78. $result = false;
  79. Db::startTrans();
  80. try {
  81. validate(MoneyValidate::class)->scene('edit')->check($data);
  82. $rows = $monuyModel->where('id', $data['id'])->where('user_id', $this->userinfo['id'])->where('status', $monuyModel::STATUS_NORMAL)->find();
  83. if(!$rows) throw new \Exception('订单不存在');
  84. $dateString = date('Ymd', strtotime($rows->createtime . ' +2 days'));
  85. // 将日期字符串转换为时间戳
  86. if(date('Ymd') > $dateString) throw new \Exception('已超出修改时间限制');
  87. //发货数据
  88. $data['create_month'] = date('Ym', strtotime($data['create_date']));//创建月份
  89. $result = $rows->save($data);
  90. Db::commit();
  91. }catch (ValidateException $e) {
  92. Db::rollback();
  93. return $this->error($e->getError());
  94. } catch (\Exception $e) {
  95. Db::rollback();
  96. $this->error($e->getMessage());
  97. }
  98. if ($result === false) {
  99. $this->error(__('没有更新任何数据'));
  100. }
  101. $this->success();
  102. }
  103. //删除账单
  104. public function moneydel(MonuyModel $monuyModel)
  105. {
  106. $id = $this->request->post('id/d');
  107. $result = false;
  108. Db::startTrans();
  109. try {
  110. $rows = $monuyModel->where('id', $id)->where('user_id', $this->userinfo['id'])->whereDay('createtime')->where('status', $monuyModel::STATUS_NORMAL)->find();
  111. if(!$rows) $this->error('数据不存在');
  112. //发货数据
  113. $rows->status = $monuyModel::STATUS_HIDDEN;
  114. $result = $rows->save();
  115. Db::commit();
  116. } catch (\Exception $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. }
  120. if ($result === false) {
  121. $this->error(__('没有删除任何数据'));
  122. }
  123. $this->success();
  124. }
  125. //支收总览
  126. public function getCountAll(MonuyModel $monuyModel)
  127. {
  128. $type = $this->request->post('type/d', 0); //1月 2年 3自定义
  129. $time = $this->request->post('time/s', '');
  130. $uids = $this->request->post('user_ids/a', []);
  131. //支收
  132. $count = $monuyModel::getCountAllBalance($type, $time, $uids);
  133. //记账账户余额
  134. $account = $monuyModel::getCountAllUserBalance($type, $time, $uids);
  135. //月统计
  136. $month = $monuyModel::getCountAllMonthBalance($type, $time, $uids);
  137. $this->success('ok', compact('count', 'account', 'month'));
  138. }
  139. /**
  140. * @return void 全部类型图标
  141. */
  142. public function getConfig()
  143. {
  144. $type = $this->request->post('type/s', 'bank_account');
  145. if(!in_array($type, ['bank_account', 'money_in_type', 'money_out_type'])) $this->error('参数有误');
  146. $this->success('提交成功', site_config('addonsd.'.$type));
  147. }
  148. }