Contract.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 Contract Extends Model {
  10. use SoftDelete;
  11. // 表名,不含前缀
  12. protected $name = 'qingdongams_contract';
  13. // 开启自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = 'updatetime';
  18. protected $deleteTime = 'deletetime';
  19. //创建合同
  20. public static function createContract($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($params[$name] === ''){
  33. $params[$name]=NULL;
  34. }
  35. }
  36. }
  37. $product = [];
  38. if (isset($params['product']) && $params['product']) {
  39. $product = $params['product'];
  40. unset($params['product']);
  41. foreach ($product as $tkey => $t) {
  42. $parts=$t['parts']??[];
  43. $new=[];
  44. if($parts){
  45. foreach ($parts as $v){
  46. if(!isset($v['part_id'])){
  47. continue;
  48. }
  49. $new[]=['part_id'=>$v['part_id'],'number'=>$v['number']];
  50. }
  51. }
  52. $product[$tkey]['parts'] = json_encode($new);
  53. }
  54. }
  55. if (isset($params['ratio_id']) && $params['ratio_id'] == 0) {
  56. $params['ratios'] = '';
  57. }
  58. if (isset($params['ratios']) && $params['ratios']) {
  59. foreach ($params['ratios'] as $v) {
  60. if (empty($v['staff_id'])) {
  61. throw new Exception('业绩归属人必须全部选择');
  62. }
  63. }
  64. $params['ratios'] = json_encode($params['ratios']);
  65. }
  66. $customer=Customer::where(['id'=>$params['customer_id']])->find();
  67. if(empty($customer)){
  68. throw new Exception('客户不存在');
  69. }
  70. $params['owner_staff_id'] = $customer->owner_staff_id;
  71. if (isset($params['plan'])) {
  72. $plan = $params['plan'];
  73. unset($params['plan']);
  74. }
  75. $staff = Staff::info();
  76. if (!empty($staff)) {
  77. $params['create_staff_id'] = $staff->id;
  78. }
  79. $flow = Flow::getsteplist(Flow::CONTRACT_STATUS);
  80. $params['flow_id'] = $flow['flow_id'];
  81. $params['order_id'] = $flow['order_id'];
  82. if ($flow['status'] == 0) {//发起人自选
  83. if (empty($params['flow_staff_ids'])) {
  84. throw new Exception('审批人必须选择');
  85. }
  86. $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
  87. } else {
  88. $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  89. }
  90. $Model = new self;
  91. $result = $Model->allowField(true)->save($params);
  92. if (false === $result) {
  93. // 验证失败 输出错误信息
  94. throw new Exception($Model->getError());
  95. }
  96. $lastId = $Model->id;
  97. $otherModel = new ContractOther();
  98. if ($otherModel->save(['id' => $lastId, 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)]) === false) {
  99. // 验证失败 输出错误信息
  100. throw new Exception($otherModel->getError());
  101. }
  102. $addRatios = [];
  103. if (isset($params['ratios']) && $params['ratios']) {
  104. $ratios=json_decode($params['ratios'],true);
  105. foreach ($ratios as $v) {
  106. $addRatios[] = [
  107. 'contract_id' => $lastId,
  108. 'ratio' => $v['ratio'],
  109. 'staff_id' => $v['staff_id'],
  110. 'ratio_money' => $params['money'] * ($v['ratio'] / 100)
  111. ];
  112. }
  113. }
  114. //业绩分成 默认自己全部
  115. if (empty($addRatios)) {
  116. $addRatios[] = [
  117. 'contract_id' => $lastId,
  118. 'ratio' => 100,
  119. 'staff_id' => isset($params['owner_staff_id']) ? $params['owner_staff_id'] : $staff->id ,
  120. 'ratio_money' => $params['money']
  121. ];
  122. }
  123. if ($addRatios) {
  124. $ratioModel = new ContractRatio();
  125. $ratioModel->insertAll($addRatios);
  126. }
  127. if (isset($plan)) {
  128. $addPlan = [];
  129. foreach ($plan as $pl) {
  130. $addPlan[] = [
  131. 'contract_id' => $lastId,
  132. 'customer_id' => $params['customer_id'],
  133. 'money' => $pl['money'],
  134. 'return_date' => $pl['time'],
  135. 'num' => $pl['name'],
  136. 'create_staff_id' => $params['owner_staff_id'],
  137. 'owner_staff_id' => $params['owner_staff_id'],
  138. 'createtime' => time(),
  139. 'updatetime' => time(),
  140. ];
  141. }
  142. $planModel = new ReceivablesPlan();
  143. $planModel->insertAll($addPlan);
  144. }
  145. $addProduct = [];
  146. foreach ($product as $v) {
  147. if (isset($v['id'])){
  148. unset($v['id']);
  149. }
  150. $v['contract_id'] = $lastId;
  151. $addProduct[] = $v;
  152. }
  153. if ($addProduct) {
  154. $productModel = new ContractProduct();
  155. $productModel->allowField(true)->saveAll($addProduct);
  156. }
  157. if ($flow['status'] == 1) {//固定审批
  158. //发送审批通知
  159. Flow::sendStepRecord($flow,Flow::CONTRACT_STATUS, $lastId);
  160. } else {//发起人自选 依次审批
  161. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  162. if ($staff_id) {
  163. ExamineRecord::addExaminse(ExamineRecord::CONTRACT_TYPE, $lastId, $staff_id);
  164. }
  165. }
  166. OperationLog::createLog(OperationLog::CONTRACT_TYPE, $lastId, '创建合同');
  167. return true;
  168. }
  169. //修改合同
  170. public static function updateContract($params) {
  171. $row=self::get($params['id']);
  172. //自定义字段
  173. $other = [];
  174. foreach ($params as $name => $val) {
  175. if (strstr($name, 'other_') !== false) {
  176. if(is_array($val)){
  177. $other[$name] = implode(',',$val);
  178. }else{
  179. $other[$name] = $val;
  180. }
  181. unset($params[$name]);
  182. }else{
  183. if($params[$name] === ''){
  184. $params[$name]=NULL;
  185. }
  186. }
  187. }
  188. $product = [];
  189. if (isset($params['product'])) {
  190. $product = $params['product'];
  191. unset($params['product']);
  192. foreach ($product as $tkey => $t) {
  193. $parts=$t['parts']??[];
  194. $new=[];
  195. if($parts){
  196. foreach ($parts as $v){
  197. if(!isset($v['part_id'])){
  198. continue;
  199. }
  200. $new[]=['part_id'=>$v['part_id'],'number'=>$v['number']];
  201. }
  202. }
  203. $product[$tkey]['parts'] = json_encode($new);
  204. }
  205. }
  206. if (isset($params['plan'])) {
  207. $plan = $params['plan'];
  208. unset($params['plan']);
  209. }
  210. if (isset($params['ratio_id']) && $params['ratio_id'] == 0) {
  211. $params['ratios'] = '';
  212. }
  213. if (isset($params['ratios']) && $params['ratios']) {
  214. foreach ($params['ratios'] as $v) {
  215. if (empty($v['staff_id'])) {
  216. throw new Exception('业绩归属人必须全部选择');
  217. }
  218. }
  219. $params['ratios'] = json_encode($params['ratios']);
  220. }
  221. $params['owner_staff_id'] = $row->owner_staff_id;
  222. $flow = Flow::getsteplist(Flow::CONTRACT_STATUS);
  223. $params['flow_id'] = $flow['flow_id'];
  224. $params['order_id'] = $flow['order_id'];
  225. if ($flow['status'] == 0) {//发起人自选
  226. if (empty($params['flow_staff_ids'])) {
  227. throw new Exception('审批人必须选择');
  228. }
  229. $params['flow_staff_ids'] = trim($params['flow_staff_ids']);
  230. } else {
  231. $params['flow_staff_ids'] = trim($flow['flow_staff_ids']);
  232. }
  233. $Model = new self;
  234. $params['check_status'] = 1;
  235. // 调用当前模型对应的User验证器类进行数据验证
  236. $result = $Model->allowField(true)->save($params, ['id' => $params['id']]);
  237. if (false === $result) {
  238. // 验证失败 输出错误信息
  239. throw new Exception($Model->getError());
  240. }
  241. $otherModel = new ContractOther();
  242. if ($otherModel->save(['otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)], ['id' => $params['id']]) === false) {
  243. // 验证失败 输出错误信息
  244. throw new Exception($otherModel->getError());
  245. }
  246. $addRatios = [];
  247. if (isset($params['ratios']) && $params['ratios']) {
  248. $ratios=json_decode($params['ratios'],true);
  249. foreach ($ratios as $v) {
  250. $addRatios[] = [
  251. 'contract_id' => $params['id'],
  252. 'ratio' => $v['ratio'],
  253. 'staff_id' => $v['staff_id'],
  254. 'ratio_money' => $params['money'] * ($v['ratio'] / 100)
  255. ];
  256. }
  257. }
  258. //业绩分成 默认自己全部
  259. $staff = Staff::info();
  260. if (empty($addRatios)) {
  261. $addRatios[] = [
  262. 'contract_id' => $params['id'],
  263. 'ratio' => 100,
  264. 'staff_id' => isset($params['owner_staff_id']) ? $params['owner_staff_id'] : $staff->id,
  265. 'ratio_money' => $params['money']
  266. ];
  267. }
  268. if (isset($plan)) {
  269. $addPlan = [];
  270. foreach ($plan as $pl) {
  271. $addPlan[] = [
  272. 'contract_id' => $params['id'],
  273. 'customer_id' => $params['customer_id'],
  274. 'money' => $pl['money'],
  275. 'return_date' => $pl['time'],
  276. 'num' => $pl['name'],
  277. 'create_staff_id' => $params['owner_staff_id'],
  278. 'owner_staff_id' => $params['owner_staff_id'],
  279. 'createtime' => time(),
  280. 'updatetime' => time(),
  281. ];
  282. }
  283. $planModel = new ReceivablesPlan();
  284. $planModel->where(['contract_id' => $params['id']])->delete();
  285. $planModel->insertAll($addPlan);
  286. }
  287. $ratioModel = new ContractRatio();
  288. $ratioModel->where(['contract_id' => $params['id']])->delete();
  289. if ($addRatios) {
  290. $ratioModel->insertAll($addRatios);
  291. }
  292. $addProduct = [];
  293. if($product){
  294. foreach ($product as $v) {
  295. if (isset($v['id'])){
  296. unset($v['id']);
  297. }
  298. $v['contract_id'] = $params['id'];
  299. $addProduct[] = $v;
  300. }
  301. }
  302. if ($addProduct) {
  303. $productModel = new ContractProduct();
  304. $productModel->where(['contract_id' => $params['id']])->delete();
  305. $productModel = new ContractProduct();
  306. $productModel->allowField(true)->saveAll($addProduct);
  307. }
  308. if ($flow['status'] == 1) {//固定审批
  309. //发送审批通知
  310. Flow::sendStepRecord($flow,Flow::CONTRACT_STATUS, $params['id']);
  311. } else {//发起人自选 依次审批
  312. $staff_id = explode(',', $params['flow_staff_ids'])[0];
  313. if ($staff_id) {
  314. ExamineRecord::addExaminse(ExamineRecord::CONTRACT_TYPE, $params['id'], $staff_id);
  315. }
  316. }
  317. return true;
  318. }
  319. public static function updateContractAddress($params)
  320. {
  321. $row = self::get($params['id']);
  322. $Model = new self;
  323. // 调用当前模型对应的User验证器类进行数据验证
  324. $result = $Model->allowField(true)->save($params, ['id' => $params['id']]);
  325. if (false === $result) {
  326. // 验证失败 输出错误信息
  327. throw new Exception($Model->getError());
  328. }
  329. $staff = Staff::info();
  330. $edit_reason=$params['edit_reason'] ?? '';
  331. OperationLog::createLog(OperationLog::CONTRACT_TYPE, $params['id'], '修改合同地址,原因:' . $edit_reason);
  332. return true;
  333. }
  334. //修改合同乙方信息
  335. public static function updateContractPartyB($params)
  336. {
  337. $row = self::get($params['id']);
  338. $Model = new self;
  339. // 调用当前模型对应的User验证器类进行数据验证
  340. $result = $Model->allowField(true)->save($params, ['id' => $params['id']]);
  341. if (false === $result) {
  342. // 验证失败 输出错误信息
  343. throw new Exception($Model->getError());
  344. }
  345. $edit_reason=$params['edit_reason'] ?? '';
  346. OperationLog::createLog(OperationLog::CONTRACT_TYPE, $params['id'], '修改合同乙方信息,原因:' . $edit_reason);
  347. return true;
  348. }
  349. //回执附件id
  350. public function getReceiptFileIdsAttr($value)
  351. {
  352. if (empty($value)) {
  353. return [];
  354. }
  355. $files = explode(',', $value);
  356. $result = [];
  357. foreach ($files as $fid) {
  358. if ($fid) {
  359. $result[] = cdnurl(File::getUrl($fid), true);
  360. }
  361. }
  362. return $result;
  363. }
  364. //添加发票物流
  365. public function getInvoiceFileIdsAttr($value)
  366. {
  367. if (empty($value)) {
  368. return [];
  369. }
  370. $files = explode(',', $value);
  371. $result = [];
  372. foreach ($files as $fid) {
  373. if ($fid) {
  374. $result[] = cdnurl(File::getUrl($fid), true);
  375. }
  376. }
  377. return $result;
  378. }
  379. //获取回款图片
  380. public function getCollectionImgAttr($value)
  381. {
  382. $files = explode(',', $value);
  383. $result = [];
  384. foreach ($files as $fid) {
  385. if ($fid) {
  386. $result[] = ['id' => $fid, 'url' => cdnurl(File::getUrl($fid), true)];
  387. }
  388. }
  389. return $result;
  390. }
  391. //获取附件
  392. public function getFilesAttr($value)
  393. {
  394. $files = explode(',', $value);
  395. $result = [];
  396. foreach ($files as $fid) {
  397. if ($fid) {
  398. $result[] = ['id' => $fid, 'url' => cdnurl(File::getUrl($fid), true)];
  399. }
  400. }
  401. return $result;
  402. }
  403. //添加合同回执单
  404. public static function receiptContract($params)
  405. {
  406. $result = self::where(['id' => $params['id']])->update([
  407. 'receipt_file_ids' => $params['receipt_file_ids'],//回访
  408. 'receipt_date' => $params['receipt_date'],//回访
  409. ]);
  410. if (false === $result) {
  411. // 验证失败 输出错误信息
  412. throw new Exception('修改信息错误');
  413. }
  414. OperationLog::createLog(OperationLog::CONTRACT_TYPE, $params['id'], '添加合同回执单');
  415. return true;
  416. }
  417. //添加合同发票物流
  418. public static function invoiceContract($params)
  419. {
  420. $result = self::where(['id' => $params['id']])->update([
  421. 'invoice_file_ids' => $params['invoice_file_ids'],//
  422. 'invoice_date' => $params['invoice_date'],//
  423. 'invoice_logistics_number' => $params['invoice_logistics_number'],//
  424. ]);
  425. if (false === $result) {
  426. // 验证失败 输出错误信息
  427. throw new Exception('修改信息错误');
  428. }
  429. OperationLog::createLog(OperationLog::CONTRACT_TYPE, $params['id'], '添加合同发票物流');
  430. return true;
  431. }
  432. //获取更新时间
  433. public function getUpdatetimeAttr($value) {
  434. return date('Y-m-d H:i:s', $value);
  435. }
  436. //获取回款计划
  437. public function getPlanAttr($value)
  438. {
  439. return json_decode($value, true);
  440. }
  441. //负责人
  442. public function ownerStaff() {
  443. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img');
  444. }
  445. //获取合同相关信息
  446. public function contractOther() {
  447. return $this->belongsTo(ContractOther::class,'id','id');
  448. }
  449. //客户
  450. public function customer() {
  451. return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name,follow,address');
  452. }
  453. //商机
  454. public function business() {
  455. return $this->hasOne(Business::class, 'id', 'business_id')->field('id,name');
  456. }
  457. //联系人
  458. public function contacts() {
  459. return $this->hasOne(Contacts::class, 'id', 'contacts_id')->field('id,name');
  460. }
  461. //员工
  462. public function staff() {
  463. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name,img,department_id,post');
  464. }
  465. //签约人
  466. public function orderStaff() {
  467. return $this->hasOne(Staff::class, 'id', 'order_staff_id')->field('id,name,mobile');
  468. }
  469. //产品
  470. public function product(){
  471. return $this->hasMany(ContractProduct::class,'contract_id','id')->with('product');
  472. }
  473. //回款统计
  474. public function receivables() {
  475. return $this->hasOne(Receivables::class, 'contract_id', 'id')->where(['check_status' => 2])->group('contract_id')->field('contract_id,sum(money) as repayment_money');
  476. }
  477. //发票
  478. public function invoice() {
  479. return $this->hasOne(Invoice::class, 'contract_id', 'id')->where(['check_status' => 2])->group('contract_id')->field('contract_id,sum(money) as invoice_money');
  480. }
  481. //导入
  482. public static function importContract($data) {
  483. $addContacts = [];
  484. $addOther = [];
  485. $addratio = [];
  486. foreach ($data as $params) {
  487. //自定义字段
  488. $other = [];
  489. foreach ($params as $name => $val) {
  490. if (strstr($name, 'other_') !== false) {
  491. if(is_array($val)){
  492. $other[$name] = implode(',',$val);
  493. }else{
  494. $other[$name] = $val;
  495. }
  496. unset($params[$name]);
  497. }else{
  498. if($params[$name] === ''){
  499. $params[$name]=NULL;
  500. }
  501. }
  502. }
  503. $params['createtime'] = time();
  504. $params['updatetime'] = time();
  505. $params['check_status'] = 2;
  506. $other['id'] = $params['id'];
  507. $addOther[] = ['id' => $params['id'], 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)];
  508. $addContacts[] = $params;
  509. $addratio[] = array(
  510. 'contract_id'=>$params['id'],
  511. 'ratio'=>100,
  512. 'staff_id'=>isset($params['owner_staff_id'])?$params['owner_staff_id']:0,
  513. 'ratio_money'=>isset($params['money'])?$params['money']:0,
  514. );
  515. }
  516. $model = new self;
  517. // 调用当前模型对应的User验证器类进行数据验证
  518. $result = $model->allowField(true)->insertAll($addContacts);
  519. $otherModel = new ContractOther();
  520. $otherModel->allowField(true)->insertAll($addOther);
  521. $modelRatio = new ContractRatio();
  522. $modelRatio->allowField(true)->insertAll($addratio);
  523. return true;
  524. }
  525. //获取合同编号
  526. public static function getNum()
  527. {
  528. return 'C' . date('Ymd') . rand(10000,99999);
  529. }
  530. }