MoneyLog.php 5.3 KB

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