123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * 发票表
  4. *
  5. * @author qifan
  6. * @data 2020-12-07
  7. */
  8. namespace app\crm\model;
  9. use app\admin\model\Common;
  10. class Invoice extends Common
  11. {
  12. protected $name = 'crm_invoice';
  13. protected $pk = 'invoice_id';
  14. protected $dateFormat = "Y-m-d H:i:s";
  15. /**
  16. * 关联用户模型
  17. *
  18. * @return \think\model\relation\HasOne
  19. */
  20. public function toCustomer()
  21. {
  22. return $this->hasOne('Customer', 'customer_id', 'customer_id')->bind([
  23. 'customer_name' => 'name'
  24. ]);
  25. }
  26. /**
  27. * 关联合同模型
  28. *
  29. * @return \think\model\relation\HasOne
  30. */
  31. public function toContract()
  32. {
  33. return $this->hasOne('Contract', 'contract_id', 'contract_id')->bind([
  34. 'contract_number' => 'num',
  35. ]);
  36. }
  37. /**
  38. * 关联用户模型
  39. *
  40. * @return \think\model\relation\HasOne
  41. */
  42. public function toAdminUser()
  43. {
  44. return $this->hasOne('AdminUser', 'id', 'owner_user_id')->bind([
  45. 'owner_user_name' => 'realname'
  46. ]);
  47. }
  48. /**
  49. * @param string $id
  50. * @param int $userId
  51. * @param string $model
  52. * @author: alvin guogaobo
  53. * @version: 11.1.0
  54. * Date: 2021/8/30 14:11
  55. */
  56. public function getDataById($id = '', $userId = 0, $model='')
  57. {
  58. $map['invoice_id'] = $id;
  59. $dataInfo = $this->where($map)->find();
  60. if (!$dataInfo) {
  61. $this->error = '暂无此数据';
  62. return false;
  63. }
  64. if(empty($model) && $model!='update'){
  65. $grantData = getFieldGrantData($userId);
  66. foreach ($grantData['crm_invoice'] as $key => $value) {
  67. foreach ($value as $ke => $va) {
  68. if($va['maskType']!=0){
  69. $fieldGrant[$ke]['maskType'] = $va['maskType'];
  70. $fieldGrant[$ke]['form_type'] = $va['form_type'];
  71. $fieldGrant[$ke]['field'] = $va['field'];
  72. }
  73. }
  74. }
  75. foreach ($fieldGrant AS $key => $val){
  76. //掩码相关类型字段
  77. if ($val['maskType']!=0 && $val['form_type'] == 'mobile') {
  78. $pattern = "/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i";
  79. $rs = preg_replace($pattern, "$1****$2", $dataInfo[$val['field']]);
  80. $dataInfo[$val['field']] = !empty($dataInfo[$val['field']]) ? (string)$rs : null;
  81. } elseif ($val['maskType']!=0 && $val['form_type'] == 'email') {
  82. $email_array = explode("@", $dataInfo[$val['field']]);
  83. $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($dataInfo[$val['field']], 0, 2); //邮箱前缀
  84. $str = preg_replace('/([\d\w+_-]{0,100})@/', "***@", $dataInfo[$val['field']], -1, $count);
  85. $rs = $prevfix . $str;
  86. $dataInfo[$val['field']] = !empty($dataInfo[$val['field']]) ?$rs: null;
  87. } elseif ($val['maskType']!=0 && in_array($val['form_type'],['position','floatnumber'])) {
  88. $dataInfo[$val['field']] = !empty($dataInfo[$val['field']]) ? (string)substr_replace($dataInfo[$val['field']], '*****',0,strlen($dataInfo[$val['field']])) : null;
  89. }
  90. }
  91. }
  92. // $userModel = new \app\admin\model\User();
  93. // $dataInfo['create_user_id_info'] = isset($dataInfo['create_user_id']) ? $userModel->getUserById($dataInfo['create_user_id']) : [];
  94. // $dataInfo['owner_user_id_info'] = isset($dataInfo['owner_user_id']) ? $userModel->getUserById($dataInfo['owner_user_id']) : [];
  95. // $dataInfo['create_user_name'] = !empty($dataInfo['create_user_id_info']['realname']) ? $dataInfo['create_user_id_info']['realname'] : '';
  96. // $dataInfo['owner_user_name'] = !empty($dataInfo['owner_user_id_info']['realname']) ? $dataInfo['owner_user_id_info']['realname'] : '';
  97. // $dataInfo['customer_id_info'] = $dataInfo['customer_id'] ? db('crm_customer')->where(['customer_id' => $dataInfo['customer_id']])->field('customer_id,name')->find() : [];
  98. // $dataInfo['contract_id_info'] = $dataInfo['contract_id'] ? db('crm_contract')->where(['contract_id' => $dataInfo['contract_id']])->field('contract_id,name,money')->find() : [];
  99. // $dataInfo['receivables_id'] = $id;
  100. // $userModel = new \app\admin\model\User();
  101. // $dataInfo['create_user_info'] = $userModel->getUserById($dataInfo['create_user_id']);
  102. // $dataInfo['plan_id'] = $id;
  103. // # 处理时间格式
  104. // $fieldModel = new \app\admin\model\Field();
  105. // $datetimeField = $fieldModel->getFieldByFormType('crm_receivables', 'datetime'); //日期时间类型
  106. // foreach ($datetimeField as $key => $val) {
  107. // $dataInfo[$val] = !empty($dataInfo[$val]) ? date('Y-m-d H:i:s', $dataInfo[$val]) : null;
  108. // }
  109. // $dataInfo['create_time'] = !empty($dataInfo['create_time']) ? date('Y-m-d H:i:s', $dataInfo['create_time']) : null;
  110. // $dataInfo['update_time'] = !empty($dataInfo['update_time']) ? date('Y-m-d H:i:s', $dataInfo['update_time']) : null;
  111. // // 字段授权
  112. // if (!empty($userId)) {
  113. // $grantData = getFieldGrantData($userId);
  114. // $userLevel = isSuperAdministrators($userId);
  115. // foreach ($dataInfo as $key => $value) {
  116. // if (!$userLevel && !empty($grantData['crm_receivables'])) {
  117. // $status = getFieldGrantStatus($key, $grantData['crm_receivables']);
  118. //
  119. // # 查看权限
  120. // if ($status['read'] == 0) unset($dataInfo[$key]);
  121. // }
  122. // }
  123. // if (!$userLevel && !empty($grantData['crm_receivables'])) {
  124. // # 客户名称
  125. // $customerStatus = getFieldGrantStatus('customer_id', $grantData['crm_receivables']);
  126. // if ($customerStatus['read'] == 0) {
  127. // $dataInfo['customer_name'] = '';
  128. // $dataInfo['customer_id_info'] = [];
  129. // }
  130. // # 合同金额
  131. // $contractMoneyStatus = getFieldGrantStatus('contract_money', $grantData['crm_receivables']);
  132. // if ($contractMoneyStatus['read'] == 0) $dataInfo['contract_id_info']['money'] = '';
  133. // # 合同名称
  134. // $contractMoneyStatus = getFieldGrantStatus('contract_money', $grantData['crm_receivables']);
  135. // if ($contractMoneyStatus['read'] == 0) $dataInfo['contract_id_info']['money'] = '';
  136. // }
  137. // }
  138. return $dataInfo;
  139. }
  140. }