Business.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\Business as BusinessModel;
  4. use addons\qingdong\model\BusinessOther;
  5. use addons\qingdong\model\FormField;
  6. use addons\qingdong\model\Staff;
  7. use addons\qingdong\model\Message;
  8. use addons\qingdong\model\Contract;
  9. use addons\qingdong\model\BusinessStatus;
  10. use addons\qingdong\model\File;
  11. use think\Db;
  12. use think\Exception;
  13. /**
  14. * 商机接口
  15. */
  16. class Business extends StaffApi
  17. {
  18. protected $noNeedLogin = [];
  19. protected $noNeedRight = [];
  20. //创建商机
  21. public function addBusiness()
  22. {
  23. $params = $this->request->post();
  24. $result = FormField::checkFields(FormField::BUSINESS_TYPE, $params);
  25. if ($result !== true) {
  26. $this->error($result);
  27. }
  28. Db::startTrans();
  29. try {
  30. $result = BusinessModel::createBusiness($params);
  31. Db::commit();
  32. } catch (Exception $e) {
  33. Db::rollback();
  34. $this->error($e->getMessage());
  35. }
  36. if ($result) {
  37. $this->success('添加商机成功');
  38. }
  39. }
  40. //获取商机列表
  41. public function getList()
  42. {
  43. $limit = input("limit/d", 10);
  44. $customer_id = input('customer_id');
  45. $params = $this->request->post();
  46. $where= FormField::updateWhereField(FormField::BUSINESS_TYPE,$params);
  47. if (isset($params['createtime']) && $params['createtime']) {//
  48. $createtime = $params['createtime'];
  49. $createtime = explode(',', $createtime);
  50. $where['createtime'] = ['between', [strtotime($createtime[0]), strtotime($createtime[1]) + 86400 - 1]];
  51. }
  52. if (isset($params['staff_id']) && $params['staff_id']) {//下级员工筛选
  53. $where['owner_staff_id'] = $params['staff_id'];
  54. } else {
  55. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  56. if (isset($params['type']) && $params['type']) {//客户分类
  57. if ($params['type'] == 1) {//我的创建
  58. $where['owner_staff_id'] = $this->auth->id;
  59. } elseif ($params['type'] == 2) {//下属创建
  60. $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  61. }
  62. }
  63. }
  64. $wheres =[];
  65. if (isset($params['contract_status']) && $params['contract_status']) {
  66. if($params['contract_status'] == 1){
  67. $wheres['contract_status'] = 0;
  68. }
  69. if($params['contract_status'] == 2){
  70. $wheres['contract_status'] = 1;
  71. }
  72. }
  73. if ($customer_id) {
  74. $where['customer_id'] = $customer_id;
  75. }
  76. $records = BusinessModel::where($where)->where($wheres)->with([
  77. 'customer',
  78. 'ownerStaff',
  79. ])->order('id desc')->paginate($limit)->toArray();
  80. $data=[];
  81. foreach($records['data'] as $k=>$v){
  82. $types = BusinessStatus::where(array('business_id'=>$v['id']))->order('id desc')->value('type');
  83. if($types){
  84. $v['type'] = (int)$types;
  85. }else{
  86. $v['type'] = 0;
  87. }
  88. $data[$k] = $v;
  89. }
  90. $allMoney = BusinessModel::where($where)->where($wheres)->sum('money');
  91. $moneyinfo['repayment_money'] = BusinessModel::where($where)->where(array('contract_status'=>1))->sum('money'); //成交金额
  92. $moneyinfo['no_money'] = sprintf("%.2f",$allMoney)-sprintf("%.2f",$moneyinfo['repayment_money']);//未成交金额
  93. $moneyinfo['allmoney'] = $allMoney;//合同总金额
  94. $this->success('请求成功', ['moneyinfo' => $moneyinfo, 'total' => $records['total'], 'per_page' => $records['per_page'], 'current_page' => $records['current_page'], 'last_page' => $records['last_page'], 'data' => $data]);
  95. }
  96. //获取商机详情
  97. public function getDetail()
  98. {
  99. $id = input('id');
  100. $contract = BusinessModel::where(['id' => $id])->with([
  101. 'customer',
  102. 'ownerStaff',
  103. 'product',
  104. ])->find();
  105. if (empty($contract)) {
  106. $this->error('商机不存在');
  107. }
  108. $contract = $contract->toArray();
  109. $contract = BusinessOther::getOther($contract);
  110. $type = BusinessStatus::where(array('business_id'=>$contract['id']))->order('id desc')->value('type');
  111. $contract['type'] = $type ? (int)$type : 0;
  112. //产品删除不显示
  113. if(isset($contract['product']) && $contract['product']){
  114. foreach($contract['product'] as $k=>$v){
  115. if(!$v['name'] && !$v['num']){
  116. unset($contract['product'][$k]);
  117. }
  118. }
  119. }
  120. //标记通知已读
  121. Message::setRead(Message::BUSINESS_TYPE, $id, $this->auth->id);
  122. $this->success('请求成功', $contract);
  123. }
  124. //修改商机
  125. public function editBusiness()
  126. {
  127. $id = input('id');
  128. $params = $this->request->post();
  129. $row = BusinessModel::where(['id' => $id])->find();
  130. if (empty($row)) {
  131. $this->error('商机信息不存在');
  132. }
  133. $result = FormField::checkFields(FormField::BUSINESS_TYPE, $params,$id);
  134. if ($result !== true) {
  135. $this->error($result);
  136. }
  137. Db::startTrans();
  138. try {
  139. $result = BusinessModel::updateBusiness($params);
  140. Db::commit();
  141. } catch (Exception $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. }
  145. $this->success('修改商机成功');
  146. }
  147. /**
  148. * 转移商机
  149. */
  150. public function batch_change()
  151. {
  152. $ids = input('id');
  153. $staff_id = input('owner_staff_id');
  154. if(!$ids || !$staff_id){
  155. $this->error('参数不正确');
  156. }
  157. $ids = BusinessModel::where([
  158. 'id' => $ids
  159. ])->column('id');
  160. if (empty($ids)) {
  161. $this->error('商机不存在');
  162. }
  163. $result = BusinessModel::batchTransfer($ids, $staff_id);
  164. if(!$result){
  165. $this->error('操作失败');
  166. }
  167. $this->success('操作成功');
  168. }
  169. //删除商机
  170. public function delete()
  171. {
  172. $ids = input('id');
  173. if(!$ids){
  174. $this->error('参数不正确');
  175. }
  176. $data = BusinessModel::where([
  177. 'id' => $ids
  178. ])->column('id');
  179. if (empty($data)) {
  180. $this->error('商机不存在');
  181. }
  182. $result = BusinessModel::where(array('id'=>$ids))->update(array('updatetime'=>time(),'deletetime'=>time()));
  183. if(!$result){
  184. $this->error('删除失败');
  185. }
  186. $this->success('删除成功');
  187. }
  188. //关联商机列表
  189. public function business_list(){
  190. $ids = input('customer_id');
  191. if(!$ids){
  192. $this->error('参数不正确');
  193. }
  194. $data= BusinessModel::where(['customer_id'=>$ids,'owner_staff_id'=>['in',Staff::getMyStaffIds()]])->field('id,name')->select();
  195. $this->success('请求成功',$data);
  196. }
  197. //商机合同
  198. public function contract(){
  199. $limit = input("limit/d", 10);
  200. $ids = input('id');
  201. if(!$ids){
  202. $this->error('参数不正确');
  203. }
  204. $contract = Contract::where(array('business_id'=>$ids))->with([
  205. 'customer',
  206. 'contacts',
  207. 'ownerStaff',
  208. 'orderStaff',
  209. 'receivables'
  210. ])->order('id desc')->paginate($limit)->toArray();
  211. $data = isset($contract['data'])?$contract['data']:[];
  212. if($data){
  213. foreach ($data as $k => $v) {
  214. if (empty($v['receivables'])) {
  215. $v['receivables'] = [
  216. 'repayment_money' => 0,
  217. 'be_money' => $v['money'],
  218. 'ratio' => 0
  219. ];
  220. } else {
  221. $be_money = $v['money'] - $v['receivables']['repayment_money'];
  222. $be_money = ($be_money > 0) ? $be_money : 0;
  223. $v['receivables'] = [
  224. 'repayment_money' => $v['receivables']['repayment_money'],
  225. 'be_money' =>$be_money,
  226. 'ratio' => round($v['receivables']['repayment_money'] / $v['money'] * 100, 2)
  227. ];
  228. }
  229. $data[$k] = $v;
  230. }
  231. }
  232. $this->success('请求成功', ['total' => $contract['total'], 'per_page' => $contract['per_page'], 'current_page' => $contract['current_page'], 'last_page' => $contract['last_page'], 'data' => $data]);
  233. }
  234. //商机状态
  235. public function business_status(){
  236. $id = input('id');
  237. $type = input('type',0);
  238. $remark = input('remark');
  239. $file = input('file');
  240. if(!$id){
  241. $this->error('参数不正确');
  242. }
  243. $data = array(
  244. 'id'=>$id,
  245. 'type'=>$type,
  246. 'remark'=>$remark,
  247. 'file'=>$file,
  248. );
  249. $business =BusinessModel::batchStatus($data);
  250. if(!$business){
  251. $this->error('操作失败');
  252. }
  253. $this->success('操作成功');
  254. }
  255. //商机历史
  256. public function business_history(){
  257. $id = input('id');
  258. if(!$id){
  259. $this->error('参数不正确');
  260. }
  261. $business = BusinessStatus::where(array('business_id'=>$id))->order('id desc')->select();
  262. foreach($business as $k=>$v){
  263. $business[$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
  264. if($v['file']){
  265. $business[$k]['file'] = File::where(array('id'=>['in',$v['file']]))->field('id,types,name,save_name,size,file_path')->select();
  266. }
  267. }
  268. $this->success('请求成功',$business);
  269. }
  270. //查询商机列表
  271. public function get_select_list(){
  272. $limit = input("limit/d", 10);
  273. $params = $this->request->post();
  274. $where = [];
  275. if(isset($params['name']) && $params['name']){
  276. $where['name'] = array('like','%'.$params['name'].'%');
  277. }
  278. $data= BusinessModel::where(['owner_staff_id'=>['in',Staff::getMyStaffIds()]])->where($where)->field('id,name')->order('id desc')->paginate($limit)->toArray();;
  279. $this->success('请求成功',$data);
  280. }
  281. }