WorkorderLogistics.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use app\common\model\Workorder as WorkorderModel;
  4. use think\Exception;
  5. use think\Model;
  6. use traits\model\SoftDelete;
  7. /**
  8. *添加物流信息
  9. */
  10. class WorkorderLogistics extends Model
  11. {
  12. use SoftDelete;
  13. protected $name = 'qingdongams_workorder_logistics';
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. protected $deleteTime = 'deletetime';
  20. public function getCreatetimeAttr($value)
  21. {
  22. return date('Y-m-d H:i', $value);
  23. }
  24. public function getContentAttr($value)
  25. {
  26. return json_decode($value, true);
  27. }
  28. //物流状态
  29. public function getCurrentStatusAttr($value)
  30. {
  31. return json_decode($value,true);
  32. }
  33. //
  34. public function getFileidsAttr($value) {
  35. $files = explode(',', $value);
  36. $result = [];
  37. foreach ($files as $fid) {
  38. if ($fid) {
  39. $result[]=['url'=> cdnurl(File::getUrl($fid), true),'id'=>$fid] ;
  40. }
  41. }
  42. return $result;
  43. }
  44. //创建人
  45. public function createStaff()
  46. {
  47. return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name');
  48. }
  49. //添加发货记录
  50. public static function createLogistics($params) {
  51. $staff = Staff::info();
  52. $params['create_staff_id'] = $staff->id;
  53. $model = new self;
  54. // 调用当前模型对应的User验证器类进行数据验证
  55. $result = $model->allowField(true)->save($params);
  56. $lastId = $model->getLastInsID();
  57. if($params['type'] == '送货上门' && $params['money']>0){
  58. $mileage_consume = [
  59. 'title' => '送货上门-物流费用',
  60. 'number' => Consume::getNumber(),
  61. 'customer_id' => $params['customer_id'],
  62. 'relation_id' => $params['workorder_id'],
  63. 'relation_type' => Consume::WORKORDER_TYPE,
  64. 'submit_date' => date('Y-m-d'),
  65. 'remark'=>'',
  66. 'money'=>$params['money']
  67. ];
  68. $mileage_consume['detail'][] = [
  69. 'consume_date' => date('Y-m-d'),
  70. 'consume_type' => '配送费',
  71. 'car_type' => $params['company'],
  72. 'consume_money' => $params['money'],
  73. 'file_ids' => $params['file_ids']
  74. ];
  75. Consume::createConsume($mileage_consume);
  76. }
  77. $product_ids=explode(',',$params['product_ids']);
  78. CustomerProduct::where(['id'=>['in',$product_ids]])
  79. ->update(['status'=>1]);
  80. if (false === $result) {
  81. // 验证失败 输出错误信息
  82. throw new Exception($model->getError());
  83. }
  84. return true;
  85. }
  86. //修改发货记录
  87. public static function updateLogistics($params) {
  88. $staff = Staff::info();
  89. $model = new self;
  90. $row=$model->get($params['id']);
  91. if($row['type'] != '送货上门' && $params['type'] == '送货上门' && $params['money']>0){
  92. $mileage_consume = [
  93. 'title' => '送货上门-物流费用',
  94. 'number' => Consume::getNumber(),
  95. 'customer_id' => $params['customer_id'],
  96. 'relation_id' => $params['workorder_id'],
  97. 'relation_type' => Consume::WORKORDER_TYPE,
  98. 'submit_date' => date('Y-m-d'),
  99. 'remark'=>'',
  100. 'money'=>$params['money']
  101. ];
  102. $mileage_consume['detail'][] = [
  103. 'consume_date' => date('Y-m-d'),
  104. 'consume_type' => '配送费',
  105. 'car_type' => $params['company'],
  106. 'consume_money' => $params['money'],
  107. 'file_ids' => $params['file_ids']
  108. ];
  109. Consume::createConsume($mileage_consume);
  110. }
  111. // 调用当前模型对应的User验证器类进行数据验证
  112. $result = $model->allowField(true)->save($params,['id'=>$params['id']]);
  113. $lastId = $model->getLastInsID();
  114. if (false === $result) {
  115. // 验证失败 输出错误信息
  116. throw new Exception($model->getError());
  117. }
  118. return true;
  119. }
  120. }