Contract.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\Contract as ContractModel;
  4. use addons\qingdong\model\ContractFile;
  5. use addons\qingdong\model\ContractOther;
  6. use addons\qingdong\model\ExamineRecord;
  7. use addons\qingdong\model\FormField;
  8. use addons\qingdong\model\Ratio;
  9. use addons\qingdong\model\Receivables;
  10. use addons\qingdong\model\Message;
  11. use addons\qingdong\model\Staff;
  12. use think\Db;
  13. use think\Exception;
  14. /**
  15. * 合同接口
  16. */
  17. class Contract extends StaffApi
  18. {
  19. protected $noNeedLogin = [];
  20. protected $noNeedRight = [];
  21. //创建合同
  22. public function addContract()
  23. {
  24. $params = $this->request->post();
  25. // 表单验证
  26. if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
  27. $this->error($result);
  28. }
  29. $result = FormField::checkFields(FormField::CONTRACT_TYPE, $params);
  30. if ($result !== true) {
  31. $this->error($result);
  32. }
  33. if (isset($params['num'])) {
  34. if (empty($params['num'])) {
  35. $params['num'] = ContractModel::getNum();
  36. }
  37. $contract = ContractModel::where(['num' => $params['num']])->find();
  38. if ($contract) {
  39. $this->error('合同编号已存在');
  40. }
  41. } else {
  42. $params['num'] = ContractModel::getNum();
  43. }
  44. Db::startTrans();
  45. try {
  46. $result = ContractModel::createContract($params);
  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. $customer_id = input('customer_id');
  60. $name = input('name','');
  61. $where = [];
  62. if ($customer_id) {
  63. $where['customer_id'] = $customer_id;
  64. }
  65. if ($name) {
  66. $where['name'] = ["like","%$name%"];
  67. }
  68. $records = ContractModel::where($where)->where(['check_status'=>2])->with(['ownerStaff'])->field('id,name,num,order_date,money')->order('id desc')->select();
  69. $this->success('请求成功', $records);
  70. }
  71. //获取合同列表
  72. public function getList()
  73. {
  74. $limit = input("limit/d", 10);
  75. $customer_id = input('customer_id');
  76. $contacts_id = input('contacts_id');
  77. $status = input('status',0);
  78. $params = $this->request->post();
  79. $whereT =[];
  80. $where= FormField::updateWhereField(FormField::CONTRACT_TYPE,$params);
  81. if (isset($params['createtime']) && $params['createtime']) {//跟进状态
  82. $createtime = $params['createtime'];
  83. $createtime = explode(',', $createtime);
  84. $where['order_date'] = ['between', [date('Y-m-d 00:00:00',strtotime($createtime[0])), date('Y-m-d 23:59:59',strtotime($createtime[1]) + 86400 - 1)]];
  85. }
  86. if (isset($params['staff_id']) && $params['staff_id']) {//下级员工筛选
  87. $where['owner_staff_id'] = $params['staff_id'];
  88. } else {
  89. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  90. if (isset($params['type']) && $params['type']) {//客户分类
  91. if ($params['type'] == 1) {//我的客户
  92. $where['owner_staff_id'] = $this->auth->id;
  93. } elseif ($params['type'] == 2) {//下属负责的客户
  94. $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  95. }
  96. }
  97. }
  98. if ($status) {
  99. if($status == 1){//待审核
  100. $where['check_status']=['in',[0,1]];
  101. }elseif($status == 2){//待回款
  102. $where['check_status']=2;
  103. $where['contract_status']=0;
  104. }elseif($status == 3){//已回款
  105. $where['check_status']=2;
  106. $where['contract_status']=1;
  107. }
  108. }
  109. if ($customer_id) {
  110. $where['customer_id'] = $customer_id;
  111. }
  112. if ($contacts_id) {
  113. $where['contacts_id'] = $contacts_id;
  114. }
  115. $records = ContractModel::where($where)->with([
  116. 'customer',
  117. 'contacts',
  118. 'ownerStaff',
  119. 'orderStaff',
  120. 'receivables'
  121. ])->order('id desc')->paginate($limit)->toArray();
  122. $data = $records['data'];
  123. foreach ($data as $k => $v) {
  124. if (empty($v['receivables'])) {
  125. $v['receivables'] = [
  126. 'repayment_money' => 0,
  127. 'be_money' => $v['money'],
  128. 'ratio' => 0
  129. ];
  130. } else {
  131. $be_money = $v['money'] - $v['receivables']['repayment_money'];
  132. $be_money = ($be_money > 0) ? $be_money : 0;
  133. $ratio_m = 0;
  134. if($v['money'] >0){
  135. $ratio_m = round($v['receivables']['repayment_money'] / $v['money'] * 100, 2);
  136. }
  137. $v['receivables'] = [
  138. 'repayment_money' => $v['receivables']['repayment_money'],
  139. 'be_money' =>$be_money,
  140. 'ratio' => $ratio_m
  141. ];
  142. }
  143. $data[$k] = $v;
  144. }
  145. $whereT['owner_staff_id']=$where['owner_staff_id'];
  146. $noMoney = ContractModel::where($whereT)->where(array('check_status'=>2))->sum('money');
  147. $moneyinfo['repayment_money'] = Receivables::where($whereT)->where(array('check_status'=>2))->sum('money'); //已回款
  148. $moneyinfo['be_money'] = sprintf("%.2f",$noMoney)-sprintf("%.2f",$moneyinfo['repayment_money']);//未回款
  149. $moneyinfo['allmoney'] = $noMoney;//合同总金额
  150. $this->success('请求成功', ['moneyinfo' => $moneyinfo, 'total' => $records['total'], 'per_page' => $records['per_page'], 'current_page' => $records['current_page'], 'last_page' => $records['last_page'], 'data' => $data]);
  151. }
  152. //获取客户相关合同列表
  153. public function getCustomerContacts()
  154. {
  155. $customer_id = input('customer_id');
  156. $contacts_id = input('contacts_id');
  157. $where = [];
  158. if ($customer_id) {
  159. $where['customer_id'] = $customer_id;
  160. }
  161. if ($contacts_id) {
  162. $where['contacts_id'] = $contacts_id;
  163. }
  164. $records = ContractModel::where($where)->with(['receivables'])->field('id,name,money,num,check_status,order_date')->order('id desc')->select();
  165. $records = collection($records)->toArray();
  166. foreach ($records as $k => $v) {
  167. if (empty($v['receivables'])) {
  168. $v['receivables'] = [
  169. 'repayment_money' => 0,
  170. 'be_money' => $v['money'],
  171. 'ratio' => 0
  172. ];
  173. } else {
  174. $be_money = $v['money'] - $v['receivables']['repayment_money'];
  175. $v['receivables'] = [
  176. 'repayment_money' => $v['receivables']['repayment_money'],
  177. 'be_money' => ($be_money > 0) ? $be_money : 0,
  178. 'ratio' => round($v['receivables']['repayment_money'] / $v['money'] * 100, 2)
  179. ];
  180. }
  181. $records[$k] = $v;
  182. }
  183. $this->success('请求成功', $records);
  184. }
  185. //获取合同详情
  186. public function getDetail()
  187. {
  188. $id = input('id');
  189. $contract = ContractModel::where(['id' => $id])->with([
  190. 'customer',
  191. 'contacts',
  192. 'ownerStaff',
  193. 'orderStaff',
  194. 'product',
  195. 'receivables',
  196. 'business'
  197. ])->find();
  198. if (empty($contract)) {
  199. //标记通知已读
  200. Message::setRead(Message::CONTRACT_TYPE, $id);
  201. $this->error('合同不存在');
  202. }
  203. $contract = $contract->toArray();
  204. $contract = ContractOther::getOther($contract);
  205. $receivablesMoney = Receivables::where(['contract_id' => $contract['id'], 'check_status' => 2])->sum('money');
  206. //回款金额
  207. $contract['receivables_money'] = $receivablesMoney;
  208. $contract['ratios'] = json_decode($contract['ratios'],true);
  209. if($contract['ratios']){
  210. foreach ($contract['ratios'] as $k => $v) {
  211. $v['staff'] = Staff::where(['id' => $v['staff_id']])->field('id,name,img,post')->find()->toArray();
  212. $v['money'] = $contract['money'] * ($v['ratio'] / 100);
  213. $contract['ratios'][$k] = $v;
  214. }
  215. }
  216. if (empty($contract['receivables'])) {
  217. $contract['receivables'] = [
  218. 'repayment_money' => 0,
  219. 'be_money' => $contract['money'],
  220. 'ratio' => 0
  221. ];
  222. } else {
  223. $be_money = $contract['money'] - $contract['receivables']['repayment_money'];
  224. $be_ratio = 0;
  225. if($contract['money'] > 0){
  226. $be_ratio =round($contract['receivables']['repayment_money'] / $contract['money'] * 100, 2);
  227. }
  228. $contract['receivables'] = [
  229. 'repayment_money' => $contract['receivables']['repayment_money'],
  230. 'be_money' => ($be_money > 0) ? $be_money : 0,
  231. 'ratio' => $be_ratio
  232. ];
  233. }
  234. //产品删除不显示
  235. if(isset($contract['product']) && $contract['product']){
  236. foreach($contract['product'] as $k=>$v){
  237. if(!$v['name'] && !$v['num']){
  238. unset($contract['product'][$k]);
  239. }
  240. }
  241. }
  242. //标记通知已读
  243. Message::setRead(Message::CONTRACT_TYPE, $id, $this->auth->id);
  244. $this->success('请求成功', $contract);
  245. }
  246. //获取附件列表
  247. public function getFilesList()
  248. {
  249. $id = input('contract_id');
  250. $files = ContractFile::where(['contract_id' => $id])->field('file_id')->with(['file'])->select();
  251. $this->success('请求成功', $files);
  252. }
  253. //撤回审核
  254. public function cancel()
  255. {
  256. $id = input('id');
  257. $customer = ContractModel::where(['id' => $id, 'check_status' => ['in', [0, 1]]])->find();
  258. if (empty($customer)) {
  259. $this->error('合同信息不存在');
  260. }
  261. $record = ExamineRecord::where([
  262. 'relation_type' => ExamineRecord::CONTRACT_TYPE,
  263. 'relation_id' => $id,
  264. 'status' => 0
  265. ])->find();
  266. Db::startTrans();
  267. try {
  268. if ($message = Message::where(['relation_type' => 'examine', 'relation_id' => $record['id'], 'from_staff_id' => $this->auth->id])->find()) {
  269. Message::where(['id' => $message['id']])->update(['status' => 1, 'read_time' => time()]);
  270. }
  271. ContractModel::where(['id' => $id])->update(['check_status' => 4]);
  272. ExamineRecord::where([
  273. 'relation_type' => ExamineRecord::CONTRACT_TYPE,
  274. 'relation_id' => $id,
  275. 'status' => 0
  276. ])->update(['status' => 3]);
  277. Db::commit();
  278. } catch (Exception $e) {
  279. Db::rollback();;
  280. $this->error($e->getMessage());
  281. }
  282. $this->success('撤回成功');
  283. }
  284. //修改合同
  285. public function editContract()
  286. {
  287. $id = input('id');
  288. $params = $this->request->post();
  289. $row = ContractModel::where(['id' => $id, 'check_status' => ['in', [3, 4]]])->find();
  290. if (empty($row)) {
  291. $this->error('合同信息不存在');
  292. }
  293. // 表单验证
  294. if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
  295. $this->error($result);
  296. }
  297. $result = FormField::checkFields(FormField::CONTRACT_TYPE, $params,$id);
  298. if ($result !== true) {
  299. $this->error($result);
  300. }
  301. Db::startTrans();
  302. try {
  303. $params['owner_staff_id'] = $row['owner_staff_id'];
  304. $result = ContractModel::updateContract($params);
  305. Db::commit();
  306. } catch (Exception $e) {
  307. Db::rollback();
  308. $this->error($e->getMessage());
  309. }
  310. $this->success('修改合同信息成功');
  311. }
  312. //获取合同编号
  313. public function getContractNumber()
  314. {
  315. $this->success('请求成功', ['number' => ContractModel::getNum()]);
  316. }
  317. /**
  318. * 获取业绩分割比例
  319. */
  320. public function getRatio()
  321. {
  322. $ratios = Ratio::where(['status' => 1])->field('name,ratio')->select();
  323. $ratios = collection($ratios)->toArray();
  324. foreach ($ratios as $k => $v) {
  325. $v['ratio'] = json_decode($v['ratio'], true);
  326. $ratios[$k] = $v;
  327. }
  328. $this->success('请求成功', $ratios);
  329. }
  330. }