Contract.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Account;
  4. use addons\qingdongams\model\Contract as ContractModel;
  5. use addons\qingdongams\model\ContractFile;
  6. use addons\qingdongams\model\ContractOther;
  7. use addons\qingdongams\model\ContractProduct;
  8. use addons\qingdongams\model\ExamineRecord;
  9. use addons\qingdongams\model\Flow;
  10. use addons\qingdongams\model\FormField;
  11. use addons\qingdongams\model\OperationLog;
  12. use addons\qingdongams\model\Ratio;
  13. use addons\qingdongams\model\Receivables;
  14. use addons\qingdongams\model\Message;
  15. use addons\qingdongams\model\Staff;
  16. use addons\qingdongams\model\CustomerProduct;
  17. use PhpOffice\PhpWord\TemplateProcessor;
  18. use think\Db;
  19. use think\Exception;
  20. /**
  21. * 合同接口
  22. */
  23. class Contract extends StaffApi
  24. {
  25. protected $noNeedLogin = [];
  26. protected $noNeedRight = [];
  27. //创建合同
  28. public function addContract()
  29. {
  30. $params = $this->request->post();
  31. // 表单验证
  32. if (($result = $this->qingdongamsValidate($params, get_class(), 'create')) !== true) {
  33. $this->error($result);
  34. }
  35. $result = FormField::checkFields(FormField::CONTRACT_TYPE,$params);
  36. if ($result !== true) {
  37. $this->error($result);
  38. }
  39. Db::startTrans();
  40. try {
  41. $result = ContractModel::createContract($params);
  42. $customerModel = new \addons\qingdongams\model\Customer();
  43. $customerInfo = $customerModel->where(['id' => $params['customer_id']])->find();
  44. if (!in_array($customerInfo['follow'], ['准备付款', '已经购买'])) {
  45. $customerModel->where(['id' => $params['customer_id']])->update(['follow' => '准备购买']);
  46. }
  47. Db::commit();
  48. } catch (Exception $e) {
  49. Db::rollback();
  50. $this->error($e->getMessage());
  51. }
  52. if ($result) {
  53. $this->success('添加合同成功');
  54. }
  55. }
  56. //获取select合同列表
  57. public function getSelectList()
  58. {
  59. $limit = input("limit/d", 10);
  60. $customer_id = input('customer_id');
  61. $where = [];
  62. $name = input('name');
  63. if ($name) {
  64. $where['num'] = ['like', "%{$name}%"];
  65. }
  66. if ($customer_id) {
  67. $where['customer_id'] = $customer_id;
  68. }
  69. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  70. $records = ContractModel::where($where)->with(['ownerStaff'])->field('id,name,num,order_date,money,owner_staff_id')->paginate($limit);
  71. $this->success('请求成功', $records);
  72. }
  73. //获取合同编号
  74. public function getContractNumber()
  75. {
  76. $this->success('请求成功', ['number' => getItemNumber('contract')]);
  77. }
  78. //获取合同列表
  79. public function getList()
  80. {
  81. $limit = input("limit/d", 10);
  82. $customer_id = input('customer_id');
  83. $name = input('name');
  84. $is_invoice = input('is_invoice');//1是 2否 是否开发票
  85. $params = $this->request->post();
  86. $where= FormField::updateWhereField(FormField::CONTRACT_TYPE,$params);
  87. $times='';
  88. if (isset($params['createtime']) && $params['createtime']) {//跟进状态
  89. $createtime = $params['createtime'];
  90. $times = setTimes($createtime, 'date');
  91. $where['order_date'] = ['between', $times];
  92. }
  93. if ($name) {
  94. $where['num'] = ['like', "%{$name}%"];
  95. }
  96. if ($is_invoice) {
  97. if ($is_invoice == 1) {
  98. $where['invoice_file_ids'] = ['neq', ''];
  99. } else {
  100. $where['invoice_file_ids'] = ['eq', ''];
  101. }
  102. }
  103. if (isset($params['staff_id']) && $params['staff_id']) {//下级员工筛选
  104. $where['owner_staff_id'] = $params['staff_id'];
  105. } else {
  106. if (isset($params['type']) && $params['type']) {//客户分类
  107. if ($params['type'] == 1) {//我的客户
  108. $where['owner_staff_id'] = $this->auth->id;
  109. } elseif ($params['type'] == 2) {//下属负责的客户
  110. $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  111. }else{
  112. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  113. }
  114. } else {
  115. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  116. }
  117. }
  118. //审核状态 1审核中、2审核通过、3审核未通过
  119. if (isset($params['check_status']) && $params['check_status']) {
  120. //0待审核、1审核中、2审核通过、3审核未通过、4撤销
  121. if ($params['check_status'] == 1) {
  122. $where['check_status'] = ['in', [0, 1]];
  123. } elseif ($params['check_status'] == 2) {
  124. $where['check_status'] = 2;
  125. } elseif ($params['check_status'] == 3) {
  126. $where['check_status'] = 3;
  127. } elseif ($params['check_status'] == 4) {
  128. $where['check_status'] = 4;
  129. } elseif ($params['check_status'] == 9) {
  130. $where['check_status'] = 9;
  131. }
  132. }
  133. if ($customer_id) {
  134. $where['customer_id'] = $customer_id;
  135. }
  136. if (isset($params['id_list'])) {//日志 查询id列表
  137. $where = [];
  138. $where['id'] = ['in', explode(',', $params['id_list'])];
  139. }
  140. $records = ContractModel::where($where)->with([
  141. 'customer',
  142. 'contacts',
  143. 'ownerStaff',
  144. 'orderStaff',
  145. 'receivables'
  146. ])->order('id desc')->paginate($limit)->toArray();
  147. $data = $records['data'];
  148. $contractId = '';
  149. foreach ($data as $k => $v) {
  150. if (empty($v['receivables'])) {
  151. $v['receivables'] = [
  152. 'repayment_money' => 0,
  153. 'be_money' => $v['money'],
  154. 'ratio' => 0
  155. ];
  156. } else {
  157. $be_money = $v['money'] - $v['receivables']['repayment_money'];
  158. $be_ratio = 0;
  159. if($v['money'] > 0){
  160. $be_ratio = round($v['receivables']['repayment_money'] / $v['money'] * 100, 2);
  161. }
  162. $v['receivables'] = [
  163. 'repayment_money' => $v['receivables']['repayment_money'],
  164. 'be_money' => ($be_money > 0) ? $be_money : 0,
  165. 'ratio' => $be_ratio
  166. ];
  167. }
  168. $data[$k] = $v;
  169. $contractId = $v['id'].','.$contractId;
  170. }
  171. $whereT['contract_id']=array('in',$contractId);
  172. $noMoney = ContractModel::where($where)->sum('money');
  173. $moneyinfo['repayment_money'] = Receivables::where($whereT)->where(array('check_status'=>2))->sum('money'); //已回款
  174. $moneyinfo['be_money'] = sprintf("%.2f",$noMoney)-sprintf("%.2f",$moneyinfo['repayment_money']);//未回款
  175. $moneyinfo['allmoney'] = $noMoney;//合同总金额
  176. $this->success('请求成功', [
  177. 'moneyinfo' => $moneyinfo,
  178. 'total' => $records['total'],
  179. 'per_page' => $records['per_page'],
  180. 'current_page' => $records['current_page'],
  181. 'last_page' => $records['last_page'],
  182. 'data' => $data
  183. ]);
  184. }
  185. //获取客户相关合同列表
  186. public function getCustomerContacts()
  187. {
  188. $limit = input("limit/d", 10);
  189. $customer_id = input('customer_id');
  190. $contacts_id = input('contacts_id');
  191. $where = [];
  192. if ($customer_id) {
  193. $where['customer_id'] = $customer_id;
  194. }
  195. if ($contacts_id) {
  196. $where['contacts_id'] = $contacts_id;
  197. }
  198. $records = ContractModel::where($where)->with(['receivables'])
  199. ->field('id,name,money,num,check_status')->order('id desc')->select();
  200. $records = collection($records)->toArray();
  201. foreach ($records as $k => $v) {
  202. if (empty($v['receivables'])) {
  203. $v['receivables'] = [
  204. 'repayment_money' => 0,
  205. 'be_money' => $v['money'],
  206. 'ratio' => 0
  207. ];
  208. } else {
  209. $be_money = $v['money'] - $v['receivables']['repayment_money'];
  210. $be_ratio = 0;
  211. if($v['money']>0){
  212. $be_ratio = round($v['receivables']['repayment_money'] / $v['money'] * 100, 2);
  213. }
  214. $v['receivables'] = [
  215. 'repayment_money' => $v['receivables']['repayment_money'],
  216. 'be_money' => ($be_money > 0) ? $be_money : 0,
  217. 'ratio' => $be_ratio
  218. ];
  219. }
  220. $records[$k] = $v;
  221. }
  222. $this->success('请求成功', $records);
  223. }
  224. //获取合同详情
  225. public function getDetail()
  226. {
  227. $id = input('id');
  228. $contract = ContractModel::where(['id' => $id])->with([
  229. 'customer',
  230. 'contacts',
  231. 'business',
  232. 'ownerStaff',
  233. 'orderStaff',
  234. 'product',
  235. 'receivables'
  236. ])->find();
  237. if (empty($contract)) {
  238. $this->error('合同不存在');
  239. }
  240. $contract = $contract->toArray();
  241. $contract = ContractOther::getOther($contract);
  242. $receivablesMoney = Receivables::where(['contract_id' => $contract['id'], 'check_status' => 2])->sum('money');
  243. //回款金额
  244. $contract['receivables_money'] = $receivablesMoney;
  245. if ($contract['check_status'] == 0 || $contract['check_status'] == 1) {
  246. $contract['is_examine'] = ExamineRecord::isExaminse(ExamineRecord::CONTRACT_TYPE, $id);
  247. } else {
  248. $contract['is_examine'] = 0;
  249. }
  250. $contract['ratios'] = json_decode($contract['ratios'],true);
  251. if($contract['ratios']){
  252. foreach ($contract['ratios'] as $k => $v) {
  253. $v['staff'] = Staff::where(['id' => $v['staff_id']])->field('id,name,img,post')->find()->toArray();
  254. $v['money'] = round($contract['money'] * ($v['ratio'] / 100),2);
  255. $contract['ratios'][$k] = $v;
  256. }
  257. }
  258. if (empty($contract['receivables'])) {
  259. $contract['receivables'] = [
  260. 'repayment_money' => 0,
  261. 'be_money' => $contract['money'],
  262. 'ratio' => 0
  263. ];
  264. } else {
  265. $be_money = $contract['money'] - $contract['receivables']['repayment_money'];
  266. $be_ratio = 0;
  267. if($contract['money']>0){
  268. $be_ratio = round($contract['receivables']['repayment_money'] / $contract['money'] * 100, 2);
  269. }
  270. $contract['receivables'] = [
  271. 'repayment_money' => $contract['receivables']['repayment_money'],
  272. 'be_money' => ($be_money > 0) ? $be_money : 0,
  273. 'ratio' => $be_ratio
  274. ];
  275. }
  276. $contract['is_operation'] = 0;
  277. if ($contract['owner_staff_id'] == $this->auth->id) {
  278. //是否可以操作
  279. $contract['is_operation'] = 1;
  280. }
  281. //标记通知已读
  282. Message::setRead(Message::CONTRACT_TYPE, $id, $this->auth->id);
  283. $this->success('请求成功', $contract);
  284. }
  285. //获取修改后的合同详情
  286. public function getContractDetail()
  287. {
  288. $id = input('id');
  289. $contract = ContractModel::where(['id' => $id])->with([
  290. 'customer', 'contacts', 'ownerStaff', 'orderStaff', 'product', 'receivables'
  291. ])->find();
  292. if (empty($contract)) {
  293. $this->error('合同不存在');
  294. }
  295. $contract = $contract->toArray();
  296. $contract = ContractOther::getOther($contract);
  297. $receivablesMoney = Receivables::where(['contract_id' => $contract['id'], 'check_status' => 2])->sum('money');
  298. //回款金额
  299. $contract['receivables_money'] = $receivablesMoney;
  300. if ($contract['check_status'] == 0 || $contract['check_status'] == 1) {
  301. $contract['is_examine'] = ExamineRecord::isExaminse(ExamineRecord::CONTRACT_TYPE, $id);
  302. $examine = ExamineRecord::where([
  303. 'relation_type' => ExamineRecord::CONTRACT_TYPE,
  304. 'relation_id' => $id,
  305. 'status' => 0,
  306. ])->find();;
  307. $contract['examine'] = json_decode($examine['edit_field']);
  308. } else {
  309. $contract['is_examine'] = 0;
  310. }
  311. if (empty($contract['receivables'])) {
  312. $contract['receivables'] = [
  313. 'repayment_money' => 0,
  314. 'be_money' => $contract['money'],
  315. 'ratio' => 0
  316. ];
  317. } else {
  318. $be_money = $contract['money'] - $contract['receivables']['repayment_money'];
  319. $be_ratio = 0;
  320. if($contract['money'] > 0){
  321. $be_ratio = round($contract['receivables']['repayment_money'] / $contract['money'] * 100, 2);
  322. }
  323. $contract['receivables'] = [
  324. 'repayment_money' => $contract['receivables']['repayment_money'],
  325. 'be_money' => ($be_money > 0) ? $be_money : 0,
  326. 'ratio' => $be_ratio
  327. ];
  328. }
  329. $contract['is_operation'] = 0;
  330. if ($contract['owner_staff_id'] == $this->auth->id) {
  331. //是否可以操作
  332. $contract['is_operation'] = 1;
  333. }
  334. //标记通知已读
  335. Message::setRead(Message::CONTRACT_TYPE, $id, $this->auth->id);
  336. $customerProducts = CustomerProduct::where(['contract_id' => $contract['id']])->with(['product'])->select();
  337. $contract['customer_product'] = $customerProducts;
  338. $this->success('请求成功', $contract);
  339. }
  340. //修改合同
  341. public function editReviewedContract()
  342. {
  343. $id = input('id');
  344. $params = $this->request->post();
  345. $row = ContractModel::where(['id' => $id, 'check_status' => 2])->find();
  346. if (empty($row)) {
  347. $this->error('合同信息不存在');
  348. }
  349. // 表单验证
  350. if (($result = $this->qingdongamsValidate($params, get_class(), 'create')) !== true) {
  351. $this->error($result);
  352. }
  353. Db::startTrans();
  354. try {
  355. $result = ContractModel::updateReviewedContract($params);
  356. Db::commit();
  357. } catch (Exception $e) {
  358. Db::rollback();
  359. $this->error($e->getMessage());
  360. }
  361. $this->success('提交修改信息成功');
  362. }
  363. //获取附件列表
  364. public function getFilesList()
  365. {
  366. $id = input('contract_id');
  367. $files = ContractFile::where(['contract_id' => $id])->field('file_id')->with(['file'])->select();
  368. $this->success('请求成功', $files);
  369. }
  370. //添加附件
  371. public function addFiles()
  372. {
  373. $contract_id = input('contract_id');
  374. $files = input('files');
  375. $files = ContractFile::addFiles($files, $contract_id);
  376. if($files){
  377. $this->success('添加成功');
  378. }
  379. $this->error('添加失败');
  380. }
  381. //删除附件
  382. public function deleteFiles()
  383. {
  384. $id = input('id');
  385. $files = ContractFile::where(['file_id' => $id])->delete();
  386. if($files){
  387. $this->success('删除成功');
  388. }
  389. $this->error('删除失败');
  390. }
  391. //撤回审核
  392. public function cancel()
  393. {
  394. $id = input('id');
  395. $contract = ContractModel::where(['id' => $id, 'check_status' => ['in', [0, 1]]])->find();
  396. if (empty($contract)) {
  397. $this->error('合同信息不存在');
  398. }
  399. if($contract['owner_staff_id'] != $this->auth->id){
  400. $this->error('只有合同负责人可以撤回审核');
  401. }
  402. $record = ExamineRecord::where([
  403. 'relation_type' => ExamineRecord::CONTRACT_TYPE,
  404. 'relation_id' => $id,
  405. 'status' => 0
  406. ])->find();
  407. Db::startTrans();
  408. try {
  409. if ($message = Message::where([
  410. 'relation_type' => 'examine',
  411. 'relation_id' => $record['id'],
  412. 'from_staff_id' => $this->auth->id
  413. ])->find()) {
  414. Message::where(['id' => $message['id']])->update(['status' => 1, 'read_time' => time()]);
  415. }
  416. ContractModel::where(['id' => $id])->update(['check_status' => 4, 'flow_staff_ids' => '']);
  417. ExamineRecord::where([
  418. 'relation_type' => ExamineRecord::CONTRACT_TYPE,
  419. 'relation_id' => $id,
  420. 'status' => 0
  421. ])->update(['status' => 3]);
  422. OperationLog::createLog(OperationLog::CONTRACT_TYPE, $id, '撤回审批');
  423. Db::commit();
  424. } catch (Exception $e) {
  425. Db::rollback();;
  426. $this->error($e->getMessage());
  427. }
  428. $this->success('撤回成功');
  429. }
  430. //修改合同
  431. public function editContract()
  432. {
  433. $id = input('id');
  434. $params = $this->request->post();
  435. $row = ContractModel::where(['id' => $id, 'check_status' => ['in', [3, 4]]])->find();
  436. if (empty($row)) {
  437. $this->error('合同信息不存在');
  438. }
  439. // 表单验证
  440. if (($result = $this->qingdongamsValidate($params, get_class(), 'create')) !== true) {
  441. $this->error($result);
  442. }
  443. $result = FormField::checkFields(FormField::CONTRACT_TYPE,$params,$id);
  444. if ($result !== true) {
  445. $this->error($result);
  446. }
  447. Db::startTrans();
  448. try {
  449. $result = ContractModel::updateContract($params);
  450. Db::commit();
  451. } catch (Exception $e) {
  452. Db::rollback();
  453. $this->error($e->getMessage());
  454. }
  455. $this->success('修改合同信息成功');
  456. }
  457. //修改合同乙方信息
  458. public function editContractPartyB()
  459. {
  460. $id = input('id');
  461. $params = $this->request->post();
  462. $row = ContractModel::where(['id' => $id])->find();
  463. if (empty($row)) {
  464. $this->error('合同信息不存在');
  465. }
  466. Db::startTrans();
  467. try {
  468. $result = ContractModel::updateContractPartyB($params);
  469. Db::commit();
  470. } catch (Exception $e) {
  471. Db::rollback();
  472. $this->error($e->getMessage());
  473. }
  474. $this->success('修改合同信息成功');
  475. }
  476. //修改合同地址
  477. public function editContractAddress()
  478. {
  479. $id = input('id');
  480. $params = $this->request->post();
  481. $row = ContractModel::where(['id' => $id])->find();
  482. if (empty($row)) {
  483. $this->error('合同信息不存在');
  484. }
  485. Db::startTrans();
  486. try {
  487. $result = ContractModel::updateContractAddress($params);
  488. Db::commit();
  489. } catch (Exception $e) {
  490. Db::rollback();
  491. $this->error($e->getMessage());
  492. }
  493. $this->success('修改合同信息成功');
  494. }
  495. //下载合同
  496. public function downloadContract()
  497. {
  498. $id = input('id');
  499. $type = input('type', 'word');
  500. $contract = ContractModel::where(['id' => $id])
  501. ->with(['customer', 'contacts', 'orderStaff'])->find();
  502. if (empty($contract)) {
  503. $this->error('不能为空');
  504. }
  505. $contract = $contract->toArray();
  506. $contractProduct = ContractProduct::where(['contract_id' => $id])->with(['product'])->select();
  507. $tmp = new TemplateProcessor('assets/addons/qingdongams/phpword/contract.docx');
  508. \PhpOffice\PhpWord\Settings::setCompatibility(true);
  509. \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
  510. $tmp->setValue('number', $contract['num']);//合同编号
  511. $tmp->setValue('name', $contract['customer']['name']);//乙方
  512. $tmp->setValue('signing_time', date('Y年m月d日', strtotime($contract['order_date'])));//签订时间
  513. $tmp->setValue('signed_at', $contract['signed_at']);//签订地点
  514. $rows = count($contractProduct);//总行数
  515. $tmp->cloneRow('one', $rows);//复制行
  516. for ($i = 0; $i < $rows; $i++) {
  517. $tmp->setValue("one#" . ($i + 1), $contractProduct[$i]['name'] ?? '');//名称
  518. $tmp->setValue("two#" . ($i + 1), $contractProduct[$i]['num'] ?? '');//型号
  519. $tmp->setValue("three#" . ($i + 1), $contractProduct[$i]['number'] ?? '');//数量
  520. $tmp->setValue("four#" . ($i + 1),
  521. $contractProduct[$i]['price'] * $contractProduct[$i]['number']);//总价格
  522. }
  523. $tmp->setValue("address", $contract['customer']['address']);//金额
  524. $tmp->setValue("discount_rate", $contract['discount_rate']);//金额
  525. $tmp->setValue("price", $contract['money']);//金额
  526. $tmp->setValue("price_big", convertAmountToCn($contract['money']));//汉字金额
  527. $tmp->setValue("remarks", $contract['remarks']);//备注
  528. $tmp->setValue("payment", $contract['payment_text'] . $contract['tax_text']);//支付方式
  529. $tmp->setValue("be_overdue", $contract['be_overdue']);//到期条款
  530. $tmp->setValue("delivery_time", $contract['delivery_time']);//
  531. $tmp->setValue("account", $contract['collection_account']);//
  532. $tmp->setValue("account_name", $contract['collection_name']);//
  533. $tmp->setValue("bank_name", $contract['collection_deposit']);//
  534. $tmp->setValue("effective", $contract['effective']);//
  535. $tmp->setValue("guarantee", $contract['guarantee']);//
  536. // $tmp->setValue("address", $contract['customer']['address']);//
  537. $tmp->setValue("legal", $contract['legal']);//
  538. $tmp->setValue("entrust", $contract['entrust']);//
  539. $tmp->setValue("handler", $contract['handler']);//
  540. $tmp->setValue("mobile", $contract['mobile']);//
  541. $tmp->setValue("fax", $contract['fax']);//
  542. $tmp->setValue("deposit", $contract['deposit']);//
  543. $tmp->setValue("y_account", $contract['account']);//
  544. $tmp->setValue("order_name", $contract['order_staff']['name']);//
  545. $tmp->setValue("order_mobile", $contract['order_staff']['mobile']);//
  546. //end 配置
  547. $filename = $contract['customer']['name'] . $contract['num'] .'-'.date('His'). '.docx';
  548. $fileurl = './uploads/' . date('Ymd') . '/';
  549. if (!file_exists($fileurl)) {
  550. mkdir($fileurl);
  551. }
  552. $tmp->saveAs($fileurl . $filename);//另存为
  553. $model = new \addons\qingdongams\model\File();
  554. $data = [
  555. 'types' => 'file',
  556. 'name' => $filename,
  557. 'save_name' => $fileurl . $filename,
  558. 'size' => filesize($fileurl . $filename),
  559. 'file_path' => trim($fileurl, '.') . $filename,
  560. ];
  561. $model->save($data);
  562. $lastid = $model->getLastInsID();
  563. $file = cdnurl($model::getUrl($lastid), true);
  564. if ($type == 'pdf') {
  565. $docfile = $_SERVER['DOCUMENT_ROOT'] . trim($data['save_name'], '.'); // word文件
  566. $pdfdir = $_SERVER['DOCUMENT_ROOT'] . trim($fileurl, '.'); // pdf文件
  567. $cmd = "export HOME=/tmp && libreoffice --headless -convert-to pdf {$docfile} -outdir {$pdfdir}";
  568. @exec($cmd);
  569. $pathinfo = pathinfo($file);
  570. $file = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '.pdf';
  571. $filename = $pathinfo['filename'] . '.pdf';
  572. }
  573. OperationLog::createLog(OperationLog::CONTRACT_TYPE, $id, '下载合同');
  574. $this->success('请求成功', ['file' => $file, 'id' => $lastid, 'filename' => $filename]);
  575. }
  576. //获取账户列表
  577. public function getAccountList()
  578. {
  579. $accounts = Account::where(['status'=>1])->select();
  580. $this->success('请求成功', $accounts);
  581. }
  582. //合同生效
  583. public function startContract()
  584. {
  585. $id = input('contract_id');
  586. $row = ContractModel::get($id);
  587. if (empty($row)) {
  588. $this->error('合同信息不存在');
  589. }
  590. $model = new ContractModel();
  591. $contract = $model->get($id);
  592. $files = $model->where(['id' => $id])->update(['is_start' => 1]);
  593. $product = ContractProduct::where(['contract_id' => $row['id']])->select();
  594. $customerInfo = \addons\qingdongams\model\Customer::get($row['customer_id']);
  595. //自动发货审批人
  596. $addWorkorder = [
  597. 'workorder_number' => \addons\qingdongams\model\Workorder::getWorkorderNumber(),
  598. 'workorder_type' => '产品发货',
  599. 'title' => '产品发货',
  600. 'customer_id' => $customerInfo['id'],
  601. 'contacts_id' => $row['contacts_id'],
  602. 'address' => $customerInfo['address'],
  603. 'address_detail' => $customerInfo['address_detail'],
  604. 'lng' => $customerInfo['lng'],
  605. 'lat' => $customerInfo['lat'],
  606. 'priority' => '一般',
  607. 'create_staff_id' => $row['create_staff_id'],
  608. 'owner_staff_id' => $row['owner_staff_id'],
  609. 'product' => $product,
  610. 'delivery_address' => $contract['delivery_address'],
  611. 'delivery_address_detail' => $contract['delivery_address_detail'],
  612. 'consignee' => $contract['consignee'],
  613. 'consignee_mobile' => $contract['consignee_mobile'],
  614. ];
  615. \addons\qingdongams\model\Workorder::createWorkorder($addWorkorder);
  616. OperationLog::createLog(OperationLog::CONTRACT_TYPE, $id, '客户已确认');
  617. $this->success('操作成功', $files);
  618. }
  619. //终止合同
  620. public function revokeContract()
  621. {
  622. $id = input('contract_id');
  623. $model = new ContractModel();
  624. $row = $model->get($id);
  625. if (empty($row)) {
  626. $this->error('信息不存在');
  627. }
  628. if($row['owner_staff_id'] != $this->auth->id){
  629. $this->error('只有合同负责人可以终止合同');
  630. }
  631. $files = $model->where(['id' => $id])->update(['contract_status' => 2, 'check_status' => 9]);
  632. Message::addMessage(Message::CONTRACT_TYPE, $id, $row['owner_staff_id'], $this->auth->id, "合同《{$row['num']}》被终止,请您及时查看!");
  633. OperationLog::createLog(OperationLog::CONTRACT_TYPE, $id, '终止合同');
  634. $this->success('操作成功', $files);
  635. }
  636. //回执合同
  637. public function receiptContract()
  638. {
  639. $params = $this->request->post();
  640. if (empty($params['id'])) {
  641. $this->error('id不能为空');
  642. }
  643. if (empty($params['receipt_file_ids'])) {
  644. $this->error('回执单不能为空');
  645. }
  646. if (empty($params['receipt_date'])) {
  647. $this->error('回执时间不能为空');
  648. }
  649. Db::startTrans();
  650. try {
  651. $result = ContractModel::receiptContract($params);
  652. Db::commit();
  653. } catch (Exception $e) {
  654. Db::rollback();
  655. $this->error($e->getMessage());
  656. }
  657. $this->success('回执成功');
  658. }
  659. //发票物流
  660. public function invoiceContract()
  661. {
  662. $params = $this->request->post();
  663. if (empty($params['id'])) {
  664. $this->error('id不能为空');
  665. }
  666. if (empty($params['invoice_file_ids'])) {
  667. $this->error('发票附件不能为空');
  668. }
  669. if (empty($params['invoice_date'])) {
  670. $this->error('时间不能为空');
  671. }
  672. if (empty($params['invoice_logistics_number'])) {
  673. $this->error('单号不能为空');
  674. }
  675. Db::startTrans();
  676. try {
  677. $result = ContractModel::invoiceContract($params);
  678. Db::commit();
  679. } catch (Exception $e) {
  680. Db::rollback();
  681. $this->error($e->getMessage());
  682. }
  683. $this->success('回执成功');
  684. }
  685. //获取支付类型文字
  686. public function getPayTypeText()
  687. {
  688. $money = input('money');
  689. $type = input('type');
  690. if ($type == '五五') {
  691. $money = $money / 2;
  692. $cnMoney = convertAmountToCn($money);
  693. $content = "合同生效后,发货前,乙方须在当天内预付合同总价定金50%¥{$money}(人民币大写:{$cnMoney}),安装调试合格后,乙方须在当天内付清合同总价尾款50%¥{$money}(人民币大写:{$cnMoney})";
  694. } elseif ($type == '三六一') {
  695. $money1 = ($money / 10) * 3;
  696. $cnMoney1 = convertAmountToCn($money1);
  697. $money2 = ($money / 10) * 6;
  698. $cnMoney2 = convertAmountToCn($money2);
  699. $money3 = ($money / 10) * 1;
  700. $cnMoney3 = convertAmountToCn($money3);
  701. $content = "合同生效后,乙方须在当天内预付合同总价定金30%¥{$money1}(人民币大写:{$cnMoney1}),发货前,乙方须在当天内预付合同总价货款60%¥{$money2}(人民币大写:{$cnMoney2}),设备安装调试后,乙方须在当天内付清合同总价尾款10%¥{$money3}(人民币大写:{$cnMoney3})。";
  702. } elseif ($type == '三七') {
  703. $money1 = ($money / 10) * 3;
  704. $cnMoney1 = convertAmountToCn($money1);
  705. $money2 = ($money / 10) * 7;
  706. $cnMoney2 = convertAmountToCn($money2);
  707. $content = "合同生效后,发货前,乙方须在当天内预付合同总价定金30%¥{$money1}(人民币大写:{$cnMoney1}),安装调试合格后,乙方须在当天内付清合同总价尾款70%¥{$money2}(人民币大写:{$cnMoney2})。";
  708. } else {
  709. $content = '';
  710. }
  711. $this->success('请求成功', $content);
  712. }
  713. /**
  714. * 获取业绩分割比例
  715. */
  716. public function getRatio()
  717. {
  718. $ratios = Ratio::where(['status' => 1])->field('name,ratio')->select();
  719. $ratios = collection($ratios)->toArray();
  720. foreach ($ratios as $k => $v) {
  721. $v['ratio'] = json_decode($v['ratio'], true);
  722. $ratios[$k] = $v;
  723. }
  724. $this->success('请求成功', $ratios);
  725. }
  726. }