MoneyLog.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * @return void 全部类型图标
  69. */
  70. public function getConfig()
  71. {
  72. $type = $this->request->post('type/s', 'bank_account');
  73. if(!in_array($type, ['bank_account', 'money_in_type', 'money_out_type'])) $this->error('参数有误');
  74. $this->success('提交成功', site_config('addonsd.'.$type));
  75. }
  76. }