MoneyLog.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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', date('Y-m'));
  17. $result['count'] = $monuyModel::getCountMonthBalance($this->userinfo['id'], $time);
  18. $result['list'] = $monuyModel->where('user_id', $this->userinfo['id'])
  19. ->where('status', MonuyModel::STATUS_NORMAL)
  20. ->whereMonth('create_date', $time)
  21. ->order('id desc')
  22. ->paginate(10);
  23. $this->success('ok', $result);
  24. }
  25. //记账统计
  26. public function getCount(MonuyModel $monuyModel)
  27. {
  28. $type = $this->request->post('type/d, 1');
  29. $time = $this->request->post('time/s', date('Y-m'));
  30. //月统计
  31. if($type == 1){
  32. $result = $monuyModel::getCountMonthBalance($this->userinfo['id'], $time);
  33. //日均支出
  34. $day = getDaysOfMonth($time);
  35. $result['day_expenditure'] = ($result['expenditure'] > 0)? bcdiv($result['expenditure'], $day, 2): 0;
  36. }else{
  37. $result = $monuyModel::getCountYearBalance($this->userinfo['id'], $time);
  38. //日均支出
  39. $day = getDaysOfYear($time);
  40. $result['day_expenditure'] = ($result['expenditure'] > 0)? bcdiv($result['expenditure'], $day, 2): 0;
  41. }
  42. $this->success('ok', $result);
  43. }
  44. //添加记账
  45. public function money(MonuyModel $monuyModel)
  46. {
  47. $data = $this->request->post();
  48. $result = false;
  49. Db::startTrans();
  50. try {
  51. validate(MoneyValidate::class)->scene('add')->check($data);
  52. //发货数据
  53. $data['user_id'] = $this->userinfo['id'];
  54. $result = $monuyModel::create($data);
  55. Db::commit();
  56. }catch (ValidateException $e) {
  57. Db::rollback();
  58. return $this->error($e->getError());
  59. } catch (\Exception $e) {
  60. Db::rollback();
  61. $this->error($e->getMessage());
  62. }
  63. if ($result === false) {
  64. $this->error(__('没有新增任何数据'));
  65. }
  66. $this->success();
  67. }
  68. //账单编辑
  69. public function moneyedit(MonuyModel $monuyModel)
  70. {
  71. $data = $this->request->post();
  72. $result = false;
  73. Db::startTrans();
  74. try {
  75. validate(MoneyValidate::class)->scene('edit')->check($data);
  76. $rows = $monuyModel->where('id', $data['id'])->where('user_id', $this->userinfo['id'])->where('status', $monuyModel::STATUS_NORMAL)->find();
  77. if(!$rows) $this->error('数据不存在');
  78. //发货数据
  79. $result = $rows->save($data);
  80. Db::commit();
  81. }catch (ValidateException $e) {
  82. Db::rollback();
  83. return $this->error($e->getError());
  84. } catch (\Exception $e) {
  85. Db::rollback();
  86. $this->error($e->getMessage());
  87. }
  88. if ($result === false) {
  89. $this->error(__('没有更新任何数据'));
  90. }
  91. $this->success();
  92. }
  93. //删除账单
  94. public function moneydel(MonuyModel $monuyModel)
  95. {
  96. $id = $this->request->post('id/d');
  97. $result = false;
  98. Db::startTrans();
  99. try {
  100. $rows = $monuyModel->where('id', $id)->where('user_id', $this->userinfo['id'])->where('status', $monuyModel::STATUS_NORMAL)->find();
  101. if(!$rows) $this->error('数据不存在');
  102. //发货数据
  103. $rows->status = $monuyModel::STATUS_HIDDEN;
  104. $result = $rows->save();
  105. Db::commit();
  106. } catch (\Exception $e) {
  107. Db::rollback();
  108. $this->error($e->getMessage());
  109. }
  110. if ($result === false) {
  111. $this->error(__('没有删除任何数据'));
  112. }
  113. $this->success();
  114. }
  115. /**
  116. * @return void 全部类型图标
  117. */
  118. public function getConfig()
  119. {
  120. $type = $this->request->post('type/s', 'bank_account');
  121. if(!in_array($type, ['bank_account', 'money_in_type', 'money_out_type'])) $this->error('参数有误');
  122. $this->success('提交成功', site_config('addonsd.'.$type));
  123. }
  124. }