Receivables.php 7.7 KB

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