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