MoneyLog.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. $data['create_month'] = date('Ym', strtotime($data['create_date']));;//创建月份
  63. $result = $monuyModel::create($data);
  64. Db::commit();
  65. }catch (ValidateException $e) {
  66. Db::rollback();
  67. return $this->error($e->getError());
  68. } catch (\Exception $e) {
  69. Db::rollback();
  70. $this->error($e->getMessage());
  71. }
  72. if ($result === false) {
  73. $this->error(__('没有新增任何数据'));
  74. }
  75. $this->success();
  76. }
  77. //账单编辑
  78. public function moneyedit(MonuyModel $monuyModel)
  79. {
  80. $data = $this->request->post();
  81. $result = false;
  82. Db::startTrans();
  83. try {
  84. validate(MoneyValidate::class)->scene('edit')->check($data);
  85. $rows = $monuyModel->where('id', $data['id'])->where('user_id', $this->userinfo['id'])->whereDay('createtime')->where('status', $monuyModel::STATUS_NORMAL)->find();
  86. if(!$rows) $this->error('数据不存在');
  87. //发货数据
  88. $result = $rows->save($data);
  89. Db::commit();
  90. }catch (ValidateException $e) {
  91. Db::rollback();
  92. return $this->error($e->getError());
  93. } catch (\Exception $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. }
  97. if ($result === false) {
  98. $this->error(__('没有更新任何数据'));
  99. }
  100. $this->success();
  101. }
  102. //删除账单
  103. public function moneydel(MonuyModel $monuyModel)
  104. {
  105. $id = $this->request->post('id/d');
  106. $result = false;
  107. Db::startTrans();
  108. try {
  109. $rows = $monuyModel->where('id', $id)->where('user_id', $this->userinfo['id'])->whereDay('createtime')->where('status', $monuyModel::STATUS_NORMAL)->find();
  110. if(!$rows) $this->error('数据不存在');
  111. //发货数据
  112. $rows->status = $monuyModel::STATUS_HIDDEN;
  113. $result = $rows->save();
  114. Db::commit();
  115. } catch (\Exception $e) {
  116. Db::rollback();
  117. $this->error($e->getMessage());
  118. }
  119. if ($result === false) {
  120. $this->error(__('没有删除任何数据'));
  121. }
  122. $this->success();
  123. }
  124. /**
  125. * @return void 全部类型图标
  126. */
  127. public function getConfig()
  128. {
  129. $type = $this->request->post('type/s', 'bank_account');
  130. if(!in_array($type, ['bank_account', 'money_in_type', 'money_out_type'])) $this->error('参数有误');
  131. $this->success('提交成功', site_config('addonsd.'.$type));
  132. }
  133. }