Quote.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 Quote extends Model
  10. {
  11. use SoftDelete;
  12. protected $name = 'qingdongams_quote';
  13. // 开启自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = 'updatetime';
  18. protected $deleteTime = 'deletetime';
  19. // 追加属性
  20. protected $append = [
  21. 'show_staff_data',
  22. ];
  23. public static function createQuote($params)
  24. {
  25. //自定义字段
  26. $other = [];
  27. foreach ($params as $name => $val) {
  28. if (strstr($name, 'other_') !== false) {
  29. $other[$name] = $val;
  30. unset($params[$name]);
  31. }
  32. }
  33. $is_check = $params['is_check'] ?? 0;
  34. unset($params['is_check']);
  35. if ($is_check == 1) {
  36. $params['check_status'] = 0;
  37. $params['next_staff_id'] = explode(',', $params['flow_staff_ids'])[0];//第一位即下一位审核人
  38. } else {
  39. $params['check_status'] = 2;
  40. }
  41. $product = [];
  42. if ($params['product']) {
  43. $product = $params['product'];
  44. unset($params['product']);
  45. }
  46. $params['product_type'] = json_encode($params['product_type'] ?? []);
  47. $params['clause'] = htmlentities($params['clause'] ?? '');
  48. $staff = Staff::info();
  49. if (!empty($staff)) {
  50. $params['create_staff_id'] = $staff->id;
  51. $params['owner_staff_id'] = $staff->id;
  52. }
  53. if ($is_check == 1) {
  54. $flow = Flow::getsteplist(Flow::CONTRACT_STATUS);
  55. $params['flow_id'] = $flow['flow_id'];
  56. $params['order_id'] = $flow['order_id'];
  57. if ($flow['status'] == 0) {//发起人自选
  58. if (empty($params['flow_staff_ids'])) {
  59. throw new Exception('审批人必须选择');
  60. }
  61. $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
  62. } else {
  63. $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  64. }
  65. }
  66. $customer = new self;
  67. // 调用当前模型对应的User验证器类进行数据验证
  68. $result = $customer->allowField(true)->save($params);
  69. $lastId = $customer->getLastInsID();
  70. if (false === $result) {
  71. // 验证失败 输出错误信息
  72. throw new Exception($customer->getError());
  73. }
  74. $otherModel = new QuoteOther();
  75. if ($otherModel->save(['id' => $lastId, 'other' => json_encode($other, JSON_UNESCAPED_UNICODE)]) === false) {
  76. // 验证失败 输出错误信息
  77. throw new Exception($otherModel->getError());
  78. }
  79. $addProduct = [];
  80. foreach ($product as $v) {
  81. $addProduct[] = [
  82. 'quote_id' => $lastId,
  83. 'config' => json_encode($v['config'] ?? []),
  84. 'config_desc' => $v['config_desc'] ?? '',
  85. 'number' => $v['number'] ?? '',
  86. 'price' => $v['price'] ?? '',
  87. 'product_id' => $v['product_id'] ?? '',
  88. 'remarks' => $v['remarks'] ?? '',
  89. ];
  90. }
  91. if ($addProduct) {
  92. $productModel = new QuoteProduct();
  93. $productModel->allowField(true)->insertAll($addProduct);
  94. }
  95. if (isset($params['flow_staff_ids']) && $params['flow_staff_ids'] && $is_check == 1) {
  96. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  97. ExamineRecord::addExaminse(ExamineRecord::QUOTE_TYPE, $lastId, $staff_id);
  98. }
  99. StaffSignIn::quickSignIn($params['customer_id'],
  100. "新增报价单:单号:".$params['number'],
  101. ['relation_process'=>'备注']);
  102. OperationLog::createLog(OperationLog::QUOTE_TYPE, $lastId, '创建报价单');
  103. return $lastId;
  104. }
  105. //创建人
  106. /**
  107. *修改报价单
  108. */
  109. public static function updateQuote($params)
  110. {
  111. //自定义字段
  112. $other = [];
  113. foreach ($params as $name => $val) {
  114. if (strstr($name, 'other_') !== false) {
  115. $other[$name] = $val;
  116. unset($params[$name]);
  117. }
  118. }
  119. $product = [];
  120. if ($params['product']) {
  121. $product = $params['product'];
  122. unset($params['product']);
  123. }
  124. $is_check = $params['is_check'] ?? 0;
  125. unset($params['is_check']);
  126. if ($is_check == 1 && isset($params['flow_staff_ids'])) {
  127. $params['check_status'] = 0;
  128. $params['next_staff_id'] = explode(',', $params['flow_staff_ids'])[0];//第一位即下一位审核人
  129. } else {
  130. $params['check_status'] = 2;
  131. }
  132. $params['product_type'] = json_encode($params['product_type'] ?? []);
  133. $params['clause'] = htmlentities($params['clause'] ?? '');
  134. $customer = new self;
  135. // 调用当前模型对应的User验证器类进行数据验证
  136. $result = $customer->isUpdate(true)->allowField(true)->save($params, ['id' => $params['id']]);
  137. if (false === $result) {
  138. // 验证失败 输出错误信息
  139. throw new Exception($customer->getError());
  140. }
  141. $otherModel = new QuoteOther();
  142. if ($otherModel->save(['other' => json_encode($other, JSON_UNESCAPED_UNICODE)], ['id' => $params['id']]) === false) {
  143. // 验证失败 输出错误信息
  144. throw new Exception($otherModel->getError());
  145. }
  146. $addProduct = [];
  147. foreach ($product as $v) {
  148. if(isset($v['id']))unset($v['id']);
  149. $v['quote_id'] = $params['id'];
  150. $v['config'] = json_encode($v['config'] ?? []);
  151. $addProduct[] = $v;
  152. }
  153. $productModel = new QuoteProduct();
  154. $productModel->where(['quote_id' => $params['id']])->delete();
  155. if ($addProduct) {
  156. $productModel->allowField(true)->saveAll($addProduct);
  157. }
  158. //撤回审批
  159. ExamineRecord::cancelExaminse(ExamineRecord::QUOTE_TYPE, $params['id']);
  160. if (isset($params['flow_staff_ids']) && $params['flow_staff_ids'] && $is_check == 1) {
  161. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  162. ExamineRecord::addExaminse(ExamineRecord::QUOTE_TYPE, $params['id'], $staff_id);
  163. }
  164. $edit_reason= $params['edit_reason'] ?? '';
  165. OperationLog::createLog(OperationLog::QUOTE_TYPE, $params['id'], '修改报价单<br>备注:' . $edit_reason);
  166. return true;
  167. }
  168. //负责人
  169. public static function transferQuote($id, $staff_id, $desc = '')
  170. {
  171. $show_staff_id = JointFollow::transfer(JointFollow::QUOTE_TYPE, $id, $staff_id);
  172. $result = self::where(['id' => $id])->update(['owner_staff_id' => $staff_id, 'show_staff_id' => $show_staff_id]);
  173. if (empty($result)) {
  174. throw new Exception('修改负责人失败,请重新尝试');
  175. }
  176. $row = self::where(['id' => $id])->with(['customer', 'ownerStaff'])->find();
  177. $newStaff = Staff::get($staff_id);
  178. $staff = Staff::info();
  179. $log = $staff->name . " 将报价单【{$row['customer']['name']}】" . '由原负责人:' . $row['owner_staff']['name'] . '变更为: ' . $newStaff['name'] . ';原因:' . $desc;
  180. OperationLog::createCustomerLog($id, $log);
  181. Message::addMessage(Message::CUSTOMER_TYPE, $id, $staff_id, $staff->id, $log, 4);
  182. Message::addMessage(Message::CUSTOMER_TYPE, $id, $row['owner_staff_id'], $staff->id, $log, 4);
  183. return true;
  184. }
  185. public function getShowStaffDataAttr($value, $data)
  186. {
  187. if (!isset($data['show_staff_id'])) {
  188. return '';
  189. }
  190. $ids = explode(',', $data['show_staff_id']);
  191. return Staff::where(['id' => ['in', $ids]])->field('id,name,img,post,mobile')->select();
  192. }
  193. public function getCreatetimeAttr($value)
  194. {
  195. if ($value) {
  196. return date('Y-m-d H:i', $value);
  197. }
  198. return $value;
  199. }
  200. public function getUpdatetimeAttr($value)
  201. {
  202. if ($value) {
  203. return date('Y-m-d H:i', $value);
  204. }
  205. return $value;
  206. }
  207. //客户
  208. public function createStaff()
  209. {
  210. return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name,img');
  211. }
  212. //联系人
  213. public function ownerStaff()
  214. {
  215. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img');
  216. }
  217. //创建报价单
  218. public function orderStaff()
  219. {
  220. return $this->hasOne(Staff::class, 'id', 'order_staff_id')->field('id,name,img,mobile');
  221. }
  222. public function customer()
  223. {
  224. return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name,follow,address');
  225. }
  226. //获取联系人信息
  227. public function contacts()
  228. {
  229. return $this->hasOne(Contacts::class, 'id', 'contacts_id')->field('id,name,mobile,telephone,mobilecode,region');
  230. }
  231. public function product()
  232. {
  233. return $this->hasMany(QuoteProduct::class, 'quote_id', 'id')->with('product');
  234. }
  235. }