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