Consume.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 Consume Extends Model {
  10. use SoftDelete;
  11. // 表名,不含前缀
  12. const CONTRACT_TYPE = 'contract';
  13. const RECEIVABLES_TYPE = 'receivables';//合同
  14. const CUSTOMER_TYPE = 'customer';//回款
  15. const WORKORDER_TYPE = 'workorder';//报价单
  16. const EVENT_TYPE = 'event';//工单
  17. // 表名,不含前缀
  18. protected $name = 'qingdongams_consume';
  19. // 开启自动写入时间戳字段
  20. protected $autoWriteTimestamp = 'int';
  21. // 定义时间戳字段名
  22. protected $createTime = 'createtime';
  23. protected $updateTime = 'updatetime';
  24. protected $deleteTime = 'deletetime';
  25. // 追加属性
  26. protected $append = [
  27. 'file_text',
  28. 'relation_data',
  29. 'title_text',
  30. ];
  31. public static function getconsumetype() {
  32. return [
  33. 'jtf' => '交通费',
  34. 'clf' => '差旅费',
  35. 'call' => '电话费',
  36. 'jjf' => '交际费',
  37. 'bgkx' => '办公开销',
  38. 'cgf' => '采购费',
  39. 'ggf' => '广告宣传费',
  40. 'other' => '其他费用',
  41. ];
  42. }
  43. public static function getNumber()
  44. {
  45. return 'M' . date('ymd') . rand(100, 999);
  46. }
  47. public function getTitleTextAttr($value, $data)
  48. {
  49. $value = $value ? $value : '';
  50. if (isset($value) && $value) {
  51. return $value;
  52. } else {
  53. return '日常报销';
  54. }
  55. }
  56. public function getRelationDataAttr($value, $data)
  57. {
  58. if (!isset($data['relation_type'])) {
  59. return '';
  60. }
  61. if ($data['relation_type'] == self::WORKORDER_TYPE) {
  62. return Workorder::where(['id' => $data['relation_id']])->field('id,title,workorder_number')->find();
  63. } elseif ($data['relation_type'] == self::EVENT_TYPE) {
  64. return Event::where(['id' => $data['relation_id']])->field('id,title')->find();
  65. }
  66. return [];
  67. }
  68. //添加费用
  69. public function getCheckStatusTextAttr($value, $data)
  70. {
  71. $check_status=$data['check_status']??0;
  72. $a = [0 => __('待审核'), 1 => __('审核中'), 2 => __('审核通过'), 3 => __('审核未通过')];
  73. return $a[$check_status] ?? '';
  74. }
  75. // 图片
  76. public function getFileTextAttr($value, $data)
  77. {
  78. if(!isset($data['file_ids'])){
  79. return '';
  80. }
  81. if(is_array($value)){
  82. return $value;
  83. }
  84. $value = $value ? $value : $data['file_ids'];
  85. $files = explode(',',$value);
  86. $files_v = [];
  87. foreach ($files as $fid) {
  88. if($fid){
  89. $files_v[] = cdnurl(File::getUrl($fid), true);
  90. }
  91. }
  92. return $files_v;
  93. }
  94. //添加费用
  95. public static function createConsume($params) {
  96. foreach ($params as $name => $val) {
  97. if($params[$name] === ''){
  98. $params[$name]=NULL;
  99. }
  100. }
  101. $staff = Staff::info();
  102. $staff_id = $staff->id;
  103. if(isset($params['customer_id']) && $params['customer_id']){
  104. $staff_id = Customer::where(['id'=>$params['customer_id']])->value('owner_staff_id');
  105. }
  106. $params['staff_id'] = $staff_id;
  107. $params['check_status'] = 1;
  108. $flow = Flow::getsteplist(Flow::CONSUME_STATUS);
  109. $params['flow_id'] = $flow['flow_id'];
  110. $params['order_id'] = $flow['order_id'];
  111. $detailes = $params['detail']??[];
  112. unset($params['detail']);
  113. if ($flow['status'] == 0) {//发起人自选
  114. if (empty($params['flow_staff_ids'])) {
  115. // throw new Exception('审批人必须选择');
  116. }
  117. $params['flow_staff_ids'] = trim($params['flow_staff_ids']??'');
  118. } else {
  119. $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  120. }
  121. $Model = new self;
  122. $result = $Model->allowField(true)->save($params);
  123. if (false === $result) {
  124. // 验证失败 输出错误信息
  125. throw new Exception($Model->getError());
  126. }
  127. $lastId= $Model->getLastInsID();
  128. $detail = [];
  129. foreach ($detailes as $v) {
  130. $detail[] = [
  131. 'customer_id' => $params['customer_id']??0,
  132. 'consume_id' => $lastId,
  133. 'consume_date' => $v['consume_date'],
  134. 'consume_type' => $v['consume_type'],
  135. 'consume_money' => $v['consume_money'],
  136. 'remark' => $v['remark'] ?? '',
  137. 'file_ids' => $v['file_ids'] ?? '',
  138. 'mileage' => $v['mileage'] ?? '',
  139. 'car_number' => $v['car_number'] ?? '',
  140. 'car_type' => $v['car_type'] ?? '',
  141. 'start_mileage' => $v['start_mileage'] ?? '',
  142. 'end_mileage' => $v['end_mileage'] ?? '',
  143. 'other_files' => $v['other_files'] ?? '',
  144. ];
  145. }
  146. $detailModel = new ConsumeDetail();
  147. $detailModel->insertAll($detail);
  148. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  149. ExamineRecord::addExaminse(ExamineRecord::CONSUME_TYPE,$lastId, $staff_id);
  150. return true;
  151. }
  152. //编辑费用
  153. public static function updateConsume($params) {
  154. $params['check_status'] = 1;
  155. $flow = Flow::getsteplist(Flow::CONSUME_STATUS);
  156. $params['flow_id'] = $flow['flow_id'];
  157. $params['order_id'] = $flow['order_id'];
  158. if ($flow['status'] == 0) {//发起人自选
  159. if (empty($params['flow_staff_ids'])) {
  160. throw new Exception('审批人必须选择');
  161. }
  162. $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
  163. } else {
  164. $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  165. }
  166. $detailes = $params['detail']??[];
  167. unset($params['detail']);
  168. $Model = new self;
  169. // 调用当前模型对应的User验证器类进行数据验证
  170. $result = $Model->save($params,['id'=>$params['id']]);
  171. if (false === $result) {
  172. // 验证失败 输出错误信息
  173. throw new Exception($Model->getError());
  174. }
  175. $detail = [];
  176. foreach ($detailes as $v) {
  177. $detail[] = [
  178. 'customer_id' => $params['customer_id']?:0,
  179. 'consume_id' => $params['id'],
  180. 'consume_date' => $v['consume_date'],
  181. 'consume_type' => $v['consume_type'],
  182. 'consume_money' => $v['consume_money'],
  183. 'remark' => $v['remark'] ?? '',
  184. 'file_ids' => $v['file_ids'] ?? '',
  185. 'mileage' => $v['mileage'] ?? '',
  186. 'car_number' => $v['car_number'] ?? '',
  187. 'car_type' => $v['car_type'] ?? '',
  188. 'start_mileage' => $v['start_mileage'] ?? '',
  189. 'end_mileage' => $v['end_mileage'] ?? '',
  190. 'other_files' => $v['other_files'] ?? '',
  191. ];
  192. }
  193. $detailModel = new ConsumeDetail();
  194. $detailModel->where(['consume_id'=>$params['id']])->delete();
  195. $detailModel->insertAll($detail);
  196. if ($flow['status'] == 1) {//固定审批
  197. //发送审批通知
  198. Flow::sendStepRecord($flow,Flow::CONSUME_STATUS, $params['id']);
  199. } else {//发起人自选 依次审批
  200. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  201. if ($staff_id) {
  202. ExamineRecord::addExaminse(ExamineRecord::CONSUME_TYPE, $params['id'], $staff_id);
  203. }
  204. }
  205. return true;
  206. }
  207. //计算交通费
  208. public static function getMileageMoney($number)
  209. {
  210. return $number * 10;
  211. }
  212. public function getCreatetimeAttr($value){
  213. if(empty($value)){
  214. return $value;
  215. }
  216. return date('Y-m-d H:i:s',$value);
  217. }
  218. public function getUpdatetimeAttr($value){
  219. if(empty($value)){
  220. return $value;
  221. }
  222. return date('Y-m-d H:i:s',$value);
  223. }
  224. //销售
  225. public function staff() {
  226. return $this->hasOne(Staff::class, 'id', 'staff_id')
  227. ->field('id,img,name,mobile');
  228. }
  229. //审核人
  230. public function followStaff() {
  231. return $this->hasOne(Staff::class, 'id', 'flow_staff_ids')
  232. ->field('id,img,name,mobile');
  233. }
  234. //客户
  235. public function customer() {
  236. return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name');
  237. }
  238. //费用详情
  239. public function consumeDetail()
  240. {
  241. return $this->hasMany(ConsumeDetail::class, 'consume_id', 'id');
  242. }
  243. }