Receivables.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace addons\qingdong\model;
  3. use think\Exception;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. /**
  7. *回款表
  8. */
  9. class Receivables Extends Model {
  10. use SoftDelete;
  11. // 表名,不含前缀
  12. protected $name = 'qingdong_receivables';
  13. // 开启自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = 'updatetime';
  18. protected $deleteTime = 'deletetime';
  19. //创建回款
  20. public static function createReceivables($params) {
  21. //自定义字段
  22. $other = [];
  23. foreach ($params as $name => $val) {
  24. if (strstr($name,'other_') !== false) {
  25. if(is_array($val)){
  26. $other[$name] = implode(',',$val);
  27. }else{
  28. $other[$name] = $val;
  29. }
  30. unset($params[$name]);
  31. }else{
  32. if(empty($params[$name])){
  33. $params[$name]=NULL;
  34. }
  35. }
  36. }
  37. $staff = Staff::info();
  38. $params['create_staff_id'] = $staff->id;
  39. $params['owner_staff_id'] = isset($params['owner_staff_id']) ? $params['owner_staff_id'] :$staff->id;
  40. $params['check_status'] = 1;
  41. $flow = Flow::getsteplist(Flow::RECEIVABLES_STATUS);
  42. $params['flow_id'] = $flow['flow_id'];
  43. $params['order_id'] = $flow['order_id'];
  44. if ($flow['status'] == 0) {//发起人自选
  45. if (empty($params['flow_staff_ids'])) {
  46. throw new Exception('审批人必须选择');
  47. }
  48. $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
  49. } else {
  50. $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  51. }
  52. $params['check_status'] =2;
  53. $params['check_staff_ids'] =$params['owner_staff_id'];
  54. $Model = new self;
  55. $result = $Model->allowField(true)->save($params);
  56. if (false === $result) {
  57. // 验证失败 输出错误信息
  58. throw new Exception($Model->getError());
  59. }
  60. $lastId=$Model->getLastInsID();
  61. $otherModel = new ReceivablesOther();
  62. if ($otherModel->save(['id' => $lastId, 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)]) === false) {
  63. // 验证失败 输出错误信息
  64. throw new Exception($otherModel->getError());
  65. }
  66. if (isset($params['plan_id']) && $params['plan_id']) {//修改回款计划为已完成
  67. ReceivablesPlan::where(['id' => $params['plan_id']])->update(['status' => 1]);
  68. }
  69. if ($flow['status'] == 1) {//固定审批
  70. //发送审批通知
  71. Flow::sendStepRecord($flow,Flow::RECEIVABLES_STATUS, $lastId);
  72. } else {//发起人自选 依次审批
  73. // $staff_id = explode(',', $params['flow_staff_ids'])[0];
  74. $staff_id = $staff->id;
  75. if ($staff_id) {
  76. ExamineRecord::addExaminse(ExamineRecord::RECEIVABLES_TYPE, $lastId, $staff_id);
  77. }
  78. }
  79. Record::quickCreateRecord(Record::CUSTOMER_TYPE, $params['customer_id'], '创建回款:' . $params['number']);
  80. return true;
  81. }
  82. /**
  83. *修改回款信息
  84. */
  85. public static function updateReceivables($params) {
  86. //自定义字段
  87. $other = [];
  88. foreach ($params as $name => $val) {
  89. if (strstr($name,'other_') !== false) {
  90. if(is_array($val)){
  91. $other[$name] = implode(',',$val);
  92. }else{
  93. $other[$name] = $val;
  94. }
  95. unset($params[$name]);
  96. }else{
  97. if(empty($params[$name])){
  98. $params[$name]=NULL;
  99. }
  100. }
  101. }
  102. $flow = Flow::getsteplist(Flow::RECEIVABLES_STATUS);
  103. $params['flow_id'] = $flow['flow_id'];
  104. $params['order_id'] = $flow['order_id'];
  105. if ($flow['status'] == 0) {//发起人自选
  106. if (empty($params['flow_staff_ids'])) {
  107. throw new Exception('审批人必须选择');
  108. }
  109. $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
  110. } else {
  111. $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  112. }
  113. $Model = new self;
  114. $params['check_status'] = 1;
  115. // 调用当前模型对应的User验证器类进行数据验证
  116. $result = $Model->save($params, ['id' => $params['id']]);
  117. if (false === $result) {
  118. // 验证失败 输出错误信息
  119. throw new Exception($Model->getError());
  120. }
  121. $otherModel = new ReceivablesOther();
  122. if ($otherModel->save(['otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)],['id' => $params['id']]) === false) {
  123. // 验证失败 输出错误信息
  124. throw new Exception($otherModel->getError());
  125. }
  126. if ($flow['status'] == 1) {//固定审批
  127. //发送审批通知
  128. Flow::sendStepRecord($flow,Flow::RECEIVABLES_STATUS, $params['id']);
  129. } else {//发起人自选 依次审批
  130. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  131. if ($staff_id) {
  132. ExamineRecord::addExaminse(ExamineRecord::RECEIVABLES_TYPE, $params['id'], $staff_id);
  133. }
  134. }
  135. return true;
  136. }
  137. //客户
  138. public function customer() {
  139. return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name,subname');
  140. }
  141. //合同
  142. public function contract() {
  143. return $this->hasOne(Contract::class, 'id', 'contract_id')->field('id,name,num,money');
  144. }
  145. //获取回款相关信息
  146. public function receivablesOther() {
  147. return $this->belongsTo(ReceivablesOther::class,'id','id');
  148. }
  149. //导入回款
  150. public static function importReceivables($data) {
  151. $addReceivables = [];
  152. $addOther = [];
  153. foreach ($data as $params) {
  154. //自定义字段
  155. $other = [];
  156. foreach ($params as $name => $val) {
  157. if (strstr($name, 'other_') !== false) {
  158. if(is_array($val)){
  159. $other[$name] = implode(',',$val);
  160. }else{
  161. $other[$name] = $val;
  162. }
  163. unset($params[$name]);
  164. }else{
  165. if(empty($params[$name])){
  166. $params[$name]=NULL;
  167. }
  168. }
  169. }
  170. $params['createtime'] = time();
  171. $params['updatetime'] = time();
  172. $params['check_status'] = 2;
  173. $other['id'] = $params['id'];
  174. $addOther[] = ['id' => $params['id'], 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)];
  175. $addReceivables[] = $params;
  176. }
  177. $model = new self;
  178. // 调用当前模型对应的User验证器类进行数据验证
  179. $result = $model->allowField(true)->insertAll($addReceivables);
  180. $otherModel = new ReceivablesOther();
  181. $otherModel->allowField(true)->insertAll($addOther);
  182. return true;
  183. }
  184. //
  185. public function ownerStaff() {
  186. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img')->removeOption('soft_delete');
  187. }
  188. //
  189. public function createStaff() {
  190. return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name,img');
  191. }
  192. //
  193. public function staff() {
  194. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img,department_id,post');
  195. }
  196. //
  197. public function getCreatetimeAttr($value) {
  198. return date('Y-m-d H:i', $value);
  199. }
  200. //
  201. public function getUpdatetimeAttr($value) {
  202. return date('Y-m-d H:i', $value);
  203. }
  204. //回款期数
  205. public function plan() {
  206. return $this->hasOne(ReceivablesPlan::class, 'id', 'plan_id')->field('id,num,money');
  207. }
  208. public static function getNum()
  209. {
  210. return 'R' . date('Ymd') . rand(10000,99999);
  211. }
  212. }