Contract.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. <?php
  2. namespace app\admin\controller\qingdong\customer;
  3. use addons\qingdong\model\BusinessProduct;
  4. use addons\qingdong\model\ContactsOther;
  5. use addons\qingdong\model\ContractProduct;
  6. use addons\qingdong\model\CustomerOther;
  7. use addons\qingdong\model\ExamineRecord;
  8. use addons\qingdong\model\File;
  9. use addons\qingdong\model\FormField;
  10. use addons\qingdong\model\ProductPart;
  11. use addons\qingdong\model\Product;
  12. use addons\qingdong\model\Ratio;
  13. use addons\qingdong\model\Receivables;
  14. use addons\qingdong\model\Staff;
  15. use addons\qingdong\model\WordTemplate;
  16. use app\admin\controller\qingdong\Base;
  17. use addons\qingdong\model\ContractOther;
  18. use addons\qingdong\model\Form;
  19. use addons\qingdong\model\Contacts;
  20. use addons\qingdong\model\Customer;
  21. use addons\qingdong\model\OperationLog;
  22. use addons\qingdong\model\Record;
  23. use addons\qingdong\model\ContractFile;
  24. use addons\qingdong\model\Flow;
  25. use addons\qingdong\model\Business;
  26. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  27. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  28. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  29. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  30. use PhpOffice\PhpSpreadsheet\RichText\RichText;
  31. use PhpOffice\PhpWord\Settings;
  32. use PhpOffice\PhpWord\TemplateProcessor;
  33. use think\Db;
  34. use think\Exception;
  35. /**
  36. * 合同管理
  37. * 操作文档:https://doc.fastadmin.net/qingdong
  38. * 软件介绍:https://www.fastadmin.net/store/qingdong.html
  39. * 售后微信:qingdong_crm
  40. */
  41. class Contract extends Base {
  42. protected $relationSearch = true;
  43. /**
  44. * @var \addons\qingdong\model\Contract
  45. */
  46. protected $model = null;
  47. public function _initialize() {
  48. parent::_initialize();
  49. $this->model = new \addons\qingdong\model\Contract;
  50. }
  51. /**
  52. * 查看
  53. */
  54. public function index() {
  55. //设置过滤方法
  56. $this->request->filter(['strip_tags', 'trim']);
  57. if ($this->request->isAjax()) {
  58. if ($this->request->request('keyField')) {
  59. return $this->selectpage();
  60. }
  61. //0:全部 1:我负责的 2:下属负责的 3:今日待跟进 4:今日已跟进 5:从未跟进的
  62. $type = input('type',0);
  63. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  64. switch($type){
  65. case 1:
  66. $staff = Staff::info();
  67. $wheres['owner_staff_id'] = $staff->id;
  68. break;
  69. case 2:
  70. $wheres['owner_staff_id'] = array('in',Staff::getLowerStaffId());
  71. break;
  72. case 3:
  73. $start = date('Y-m-d 00:00:00');
  74. $end = date('Y-m-d 23:59:59');
  75. $record = collection(Record::where(array('relation_type'=>3,'next_time'=>array(array('egt',$start),array('elt',$end))))->field("id,relation_id")->select())->toArray();
  76. $relationId = [];
  77. foreach($record as $k=>$v){
  78. $whereRe['id'] = array('gt',$v['id']);
  79. $whereRe['relation_id'] = $v['relation_id'];
  80. $recordData = Record::where($whereRe)->count();
  81. if($recordData == 0){
  82. $relationId[] = $v['relation_id'];
  83. }
  84. }
  85. $wheres['id'] = array('in',$relationId);
  86. $staff = Staff::info();
  87. $wheres['owner_staff_id'] = $staff->id;
  88. break;
  89. case 4:
  90. $start = date('Y-m-d 00:00:00');
  91. $end = date('Y-m-d 23:59:59');
  92. $record = collection(Record::where(array('relation_type'=>3,'next_time'=>array(array('egt',$start),array('elt',$end))))->field("id,relation_id")->select())->toArray();
  93. $relationId = [];
  94. foreach($record as $k=>$v){
  95. $whereRe['id'] = array('gt',$v['id']);
  96. $whereRe['relation_id'] = $v['relation_id'];
  97. $recordData = Record::where($whereRe)->count();
  98. if($recordData >=1){
  99. $relationId[] = $v['relation_id'];
  100. }
  101. }
  102. $wheres['id'] = array('in',$relationId);
  103. $staff = Staff::info();
  104. $wheres['owner_staff_id'] = $staff->id;
  105. break;
  106. case 5:
  107. $record = collection(Record::where(array('relation_type'=>3,'next_time'=>array('neq','')))->column('relation_id'))->toArray();
  108. $wheres['id'] = array('not in',$record);
  109. $staff = Staff::info();
  110. $wheres['owner_staff_id'] = $staff->id;
  111. break;
  112. default:
  113. $wheres['owner_staff_id'] = array('in',Staff::getMyStaffIds());
  114. break;
  115. }
  116. $staff = Staff::info();
  117. $ids=[];
  118. $group_id=input('group_id');
  119. $staff_id=input('staff_id');
  120. if ($group_id) {//角色组
  121. $ids = Staff::getGroupStaffIds($group_id);
  122. }
  123. if ($staff_id) {
  124. $ids = $staff_id;
  125. }
  126. if ($group_id || $staff_id) {
  127. $wheres['owner_staff_id'] = ['in', $ids];
  128. }
  129. $staff = Staff::info();
  130. $staffid = $staff->id;
  131. $s = "FIND_IN_SET(".$staffid.",order_staff_id)";
  132. $list = $this->model->where($where)
  133. ->where($s)
  134. ->with([
  135. 'customer',
  136. 'orderStaff',
  137. 'contacts'
  138. ])->order($sort, $order)->paginate($limit);
  139. $allmoney = $this->model->where($wheres)->where(array('check_status'=>2))->sum('money');
  140. $repayment_money = Receivables::where($wheres)->where(array('check_status'=>2))->sum('money'); //已回款
  141. $no_money = $allmoney - $repayment_money;
  142. if($no_money <= 0){
  143. $no_money = 0;
  144. }
  145. $result = array("total" => $list->total(), "rows" => $list->items(),'allmoney'=>$allmoney,'repayment_money'=>$repayment_money,'no_money'=>$no_money,
  146. 'role' => $staff->role);
  147. return json($result);
  148. }
  149. $field = FormField::getFields(FormField::CONTRACT_TYPE);
  150. $this->assignconfig('fields', $field);
  151. return $this->view->fetch();
  152. }
  153. /**
  154. * 添加
  155. */
  156. public function add()
  157. {
  158. if ($this->request->isPost()) {
  159. $params = $this->request->post("row/a");
  160. if ($params) {
  161. $params = $this->preExcludeFields($params);
  162. $nowsaff = Staff::info();
  163. $params['order_staff_id'] = $nowsaff->id;
  164. $params['flow_staff_ids'] = '1';
  165. // 表单验证
  166. if (($result = $this->qingdongValidate($params, 'Contract', 'create')) !== true) {
  167. $this->error($result);
  168. }
  169. $result = FormField::checkFields(FormField::CONTRACT_TYPE, $params);
  170. if ($result !== true) {
  171. $this->error($result);
  172. }
  173. $result = false;
  174. Db::startTrans();
  175. try {
  176. $params = Form::updateFormParams(Form::CONTRACT_TYPE, $params);
  177. // if($params['product']){
  178. // $params['product']=json_decode($params['product'],true);
  179. // }
  180. $result = $this->model::createContract($params);
  181. Db::commit();
  182. } catch (Exception $e) {
  183. Db::rollback();
  184. $this->error($e->getMessage());
  185. }
  186. if ($result !== false) {
  187. $this->success();
  188. } else {
  189. $this->error(__('No rows were inserted'));
  190. }
  191. }
  192. $this->error(__('Parameter %s can not be empty', ''));
  193. }
  194. $flow= Flow::getsteplist(Flow::CONTRACT_STATUS);
  195. if (empty($flow)) {
  196. $this->error('无可用审批流,请联系管理员','qingdong/work/flow');
  197. }
  198. $customer_id = input('customer_id');
  199. if($customer_id){
  200. $business = Business::where(array('customer_id'=>$customer_id))->select();
  201. }else{
  202. $business = Business::getList();
  203. }
  204. $this->assign('flow',$flow);
  205. $this->assign('staff', Staff::getStaff());
  206. $this->assign('customers', Customer::getList());
  207. $this->assign('business', $business);
  208. $this->assign('customer_id', $customer_id);
  209. $this->assign('business_id', input('business_id'));
  210. $this->assign('staffs', Staff::allList());
  211. $this->assign('form_data', Form::getDataValue('contract'));
  212. $this->assign('ratio',Ratio::where(['status'=>1])->select());
  213. $this->assign('contractNum', 'C'.date('Ymd').rand(10000,99999));
  214. return $this->view->fetch();
  215. }
  216. /**
  217. * 修改合同
  218. */
  219. public function edit($ids = null) {
  220. $map['id'] = $ids;
  221. if ($this->request->isAjax()) {
  222. $params = $this->request->post('row/a');
  223. $params = $this->preExcludeFields($params);
  224. $row = $this->model::where(['id' => $ids])->find();
  225. // if (empty($row)) {
  226. // $this->error('合同审核中或已审核通过无法修改');
  227. // }
  228. $params['flow_staff_ids'] = '1';
  229. // 表单验证
  230. if (($result = $this->qingdongValidate($params, 'Contract', 'create')) !== true) {
  231. $this->error($result);
  232. }
  233. $result = FormField::checkFields(FormField::CONTRACT_TYPE, $params,$ids);
  234. if ($result !== true) {
  235. $this->error($result);
  236. }
  237. Db::startTrans();
  238. try {
  239. $params = Form::updateFormParams(Form::CONTRACT_TYPE, $params);
  240. $params['id']=$ids;
  241. $params['owner_staff_id'] = $row['owner_staff_id'];
  242. // if($params['product']){
  243. // $params['product']=json_decode($params['product'],true);
  244. // }
  245. $result = $this->model::updateContract($params);
  246. Db::commit();
  247. } catch (Exception $e) {
  248. Db::rollback();
  249. $this->error($e->getMessage());
  250. }
  251. $this->success('修改成功');
  252. }
  253. $row = $this->model->where($map)->with(['customer','contacts','business'])->find();
  254. if(empty($row)){
  255. $this->error('信息不存在');
  256. }
  257. $row = $row->toArray();
  258. $row = ContractOther::getOther($row);
  259. $product=ContractProduct::where(['contract_id'=>$ids])->with(['productinfo'])->select();
  260. foreach ($product as $k=>$v){
  261. if($v['productinfo']){
  262. $v['product_name']=$v['productinfo']['name'];
  263. $v['cost_price']=$v['productinfo']['cost_price'];
  264. $v['type']=$v['productinfo']['type'];
  265. if($v['productinfo']['goods']){
  266. $v['goods_name']=$v['productinfo']['goods']['name'];
  267. }
  268. }
  269. $product[$k]=$v;
  270. }
  271. $row['product']=json_encode($product);
  272. $row['ratios']=json_decode($row['ratios'],true);
  273. $flow= Flow::getsteplist(Flow::CONTRACT_STATUS);
  274. if (empty($flow)) {
  275. $this->error('无可用审批流,请联系管理员','qingdong/work/flow');
  276. }
  277. $this->assign('flow',$flow);
  278. $this->view->assign("row", $row);
  279. $this->assign('customers', Customer::getList());
  280. $this->assign('business', Business::getList());
  281. $this->assign('staffs', Staff::getList());
  282. $this->assign('staff', Staff::getStaff());
  283. $this->assign('ratio',Ratio::where(['status'=>1])->select());
  284. $this->assign('form_data', Form::getDataValue('contract',$row));
  285. $this->assign('contacts', Contacts::where(['customer_id'=>$row['customer_id']])->field('id,name')->select());
  286. return $this->view->fetch();
  287. }
  288. /**
  289. * 获取附件记录
  290. */
  291. public function get_file($ids = null) {
  292. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  293. $list = ContractFile::where(['contract_id' => $ids])->with(['file'])->field('file_id')->paginate($limit);
  294. $result = array("total" => $list->total(), "rows" => $list->items());
  295. return json($result);
  296. }
  297. /**
  298. * 合同详情
  299. */
  300. public function detail($ids = null) {
  301. $row = $this->model->where(['id' => $ids])->with([
  302. 'customer',
  303. 'contacts',
  304. 'orderStaff',
  305. 'ownerStaff',
  306. 'business'
  307. ])->find();
  308. if (empty($row)) {
  309. $this->error(__('No Results were found'));
  310. }
  311. $row=$row->toArray();
  312. $row=ContractOther::getOther($row);
  313. $row['ratios'] = json_decode($row['ratios'], true);
  314. if ($row['ratios']) {
  315. foreach ($row['ratios'] as $k => $v) {
  316. $v['staff'] = Staff::where(['id' => $v['staff_id']])->field('id,name,img,post')->find()->toArray();
  317. $v['money'] = $row['money'] * ($v['ratio'] / 100);
  318. $row['ratios'][$k] = $v;
  319. }
  320. }
  321. $staff = Staff::info();
  322. $row['repayment_money'] = Receivables::where(['contract_id'=>$ids])->sum('money'); //已回款
  323. $row['no_money'] = $row['money'] -$row['repayment_money'];
  324. if($row['no_money'] <= 0){
  325. $row['no_money'] = 0;
  326. }
  327. $filedcontect = Form::getDataValue('contract',$row);
  328. if($staff->role != 1 && $staff->role != 2){
  329. foreach ($filedcontect as $k=>$v){
  330. unset($filedcontect[2]);
  331. }
  332. $filedcontect = array_values($filedcontect);
  333. }
  334. //重新排序
  335. $this->assign('examine_record',ExamineRecord::getList(ExamineRecord::CONTRACT_TYPE,$ids));
  336. $this->assign('flow',Flow::getstepdetail(Flow::CONTRACT_STATUS, $ids));
  337. //跟进记录
  338. $this->assign('records', Record::getList(Record::CONTRACT_TYPE, $ids));
  339. //操作记录
  340. $log=OperationLog::getList(OperationLog::CONTRACT_TYPE, $ids);
  341. $this->assign('operation_log',$log);
  342. $this->assign('row', $row);
  343. $this->assign('role', $staff->role);
  344. $this->assign('ids', $ids);
  345. $this->assign('form_data',$filedcontect);
  346. $this->assignconfig("idinfo", ['id' => $ids]);
  347. return $this->view->fetch();
  348. }
  349. /**
  350. * 获取联系人select
  351. */
  352. public function get_contacts_list(){
  353. $pageSize = input('pageSize');
  354. $pageNumber = input('pageNumber');
  355. $where = [];
  356. if ($keyValue = $this->request->request("keyValue")) {
  357. $where['id'] = ['in',$keyValue];
  358. }
  359. $ids=input('ids');
  360. $name = input('name');
  361. if(!empty($name)){
  362. $where['name'] = ['like','%'.$name.'%'];
  363. }
  364. $staff = Contacts::where($where)->where(['customer_id'=>$ids])
  365. ->field('id,name')->order('id desc')->paginate($pageSize, false, ['page' => $pageNumber]);
  366. return json(['list' => $staff->items(), 'total' => $staff->total()]);
  367. }
  368. /**
  369. * 获取产品明细
  370. */
  371. public function get_contract_product(){
  372. $contract_id=input('contract_id',0,'intval');
  373. if(empty($contract_id)){
  374. $this->error('参数不能为空');
  375. }
  376. $products=ContractProduct::where(['contract_id'=>$contract_id])
  377. ->with(['productinfo'])->select();
  378. foreach($products as $k=>$v){
  379. if(!$v['productinfo']['id']){
  380. unset($products[$k]);
  381. }
  382. }
  383. return json(['rows' =>$products, 'total' => count($products)]);
  384. }
  385. /**
  386. * 获取回款计划
  387. */
  388. public function get_receivables_plan($ids = null) {
  389. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  390. $list = \addons\qingdong\model\ReceivablesPlan::where([
  391. 'contract_id' => $ids
  392. ])->with(['customer'])->paginate($limit);
  393. $result = array("total" => $list->total(), "rows" => $list->items());
  394. return json($result);
  395. }
  396. /**
  397. * 获取回款记录
  398. */
  399. public function get_receivables($ids = null) {
  400. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  401. $list = \addons\qingdong\model\Receivables::where([
  402. 'contract_id' => $ids
  403. ])->with(['customer','ownerStaff'])->paginate($limit);
  404. $result = array("total" => $list->total(), "rows" => $list->items());
  405. return json($result);
  406. }
  407. /**
  408. * 获取商机关联产品
  409. */
  410. public function get_business_product(){
  411. $business_id=input('business_id');
  412. if(empty($business_id)){
  413. $this->error('参数不能为空');
  414. }
  415. $business=BusinessProduct::where(['business_id'=>$business_id])->with(['productinfo'])->select();
  416. $this->success('请求成功','',$business);
  417. }
  418. /**
  419. * 获取产品
  420. */
  421. public function get_product()
  422. {
  423. $where = [];
  424. if ($keyValue = $this->request->request("keyValue")) {
  425. $where['id'] = $keyValue;
  426. }
  427. $name = input('name');
  428. if(!empty($name)){
  429. $where['name'] = ['like','%'.$name.'%'];
  430. }
  431. $countrys = Product::where($where)->field('id,name')->order('id desc')->select();
  432. return json(['list' => $countrys, 'total' => count($countrys)]);
  433. }
  434. /**
  435. * 获取产品配置
  436. */
  437. public function get_product_part()
  438. {
  439. $where = [];
  440. if ($keyValue = $this->request->request("keyValue")) {
  441. $where['id'] = $keyValue;
  442. }
  443. $name = input('name');
  444. if(!empty($name)){
  445. $where['name'] = ['like','%'.$name.'%'];
  446. }
  447. $countrys = ProductPart::where($where)->field('id,name')->order('id desc')->select();
  448. return json(['list' => $countrys, 'total' => count($countrys)]);
  449. }
  450. /**
  451. * 获取产品价格
  452. */
  453. public function get_product_price()
  454. {
  455. $id = input('id', 0);
  456. $price = Product::where(['id' => $id])->value('price') ?? 0;
  457. $this->success('请求成功', '', ['price' => $price]);
  458. }
  459. /**
  460. * 获取审批人列表
  461. */
  462. public function getstaff(){
  463. $pageSize = input('pageSize');
  464. $pageNumber = input('pageNumber');
  465. $where = [];
  466. if ($keyValue = $this->request->request("keyValue")) {
  467. $where['id'] = ['in',$keyValue];
  468. }
  469. $name = input('name');
  470. if(!empty($name)){
  471. $where['name'] = ['like','%'.$name.'%'];
  472. }
  473. $staff = Staff::where($where)->where(['id'=>['neq',$this->_staff->id]])->field('id,name')->order('id desc')->paginate($pageSize, false, ['page' => $pageNumber]);
  474. return json(['list' => $staff->items(), 'total' => $staff->total()]);
  475. }
  476. /**
  477. * 删除
  478. */
  479. public function del($ids = "") {
  480. if (!$this->request->isPost()) {
  481. $this->error(__("Invalid parameters"));
  482. }
  483. $map['id'] = array('in', $ids);
  484. $result = $this->model->destroy($map);
  485. if (!$result) {
  486. $this->error('删除失败');
  487. }
  488. $this->success('删除成功');
  489. }
  490. /**
  491. * 导入合同
  492. * @return string|void
  493. */
  494. public function import()
  495. {
  496. set_time_limit(0);
  497. if ($this->request->isPost()) {
  498. $file = $this->request->request('file');
  499. if (!$file) {
  500. $this->error(__('Parameter %s can not be empty', 'file'));
  501. }
  502. $filePath = ROOT_PATH . 'public' . $file;
  503. if (!is_file($filePath)) {
  504. $this->error(__('No results were found'));
  505. }
  506. //实例化reader
  507. $ext = pathinfo($filePath, PATHINFO_EXTENSION);
  508. if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
  509. $this->error(__('Unknown data format'));
  510. }
  511. if ($ext === 'csv') {
  512. $file = fopen($filePath, 'r');
  513. $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
  514. $fp = fopen($filePath, "w");
  515. $n = 0;
  516. while ($line = fgets($file)) {
  517. $line = rtrim($line, "\n\r\0");
  518. $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
  519. if ($encoding != 'utf-8') {
  520. $line = mb_convert_encoding($line, 'utf-8', $encoding);
  521. }
  522. if ($n == 0 || preg_match('/^".*"$/', $line)) {
  523. fwrite($fp, $line . "\n");
  524. } else {
  525. fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
  526. }
  527. $n++;
  528. }
  529. fclose($file) || fclose($fp);
  530. $reader = new Csv();
  531. } elseif ($ext === 'xls') {
  532. $reader = new Xls();
  533. } else {
  534. $reader = new Xlsx();
  535. }
  536. if (!$PHPExcel = $reader->load($filePath)) {
  537. $this->error(__('Unknown data format'));
  538. }
  539. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  540. $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  541. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  542. $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
  543. //开始读取数据
  544. $fields = [];
  545. for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
  546. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  547. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  548. $fields[$currentRow][] = $val;
  549. if ($val instanceof RichText) {//富文本转换字符串
  550. $val = $val->__toString();
  551. }
  552. $values[] = is_null($val) ? NULL : trim($val);
  553. }
  554. }
  555. if (!isset($fields[1])) {
  556. $this->error('导入文件第一行没有数据');
  557. }
  558. $lastid = $this->model->withTrashed()->order('id desc')->value('id');
  559. $lastid = $lastid + 5;//防止重复
  560. $contractRow = [];
  561. $errorInfo = [];
  562. $fieldnames = FormField::where(['types' => FormField::CONTRACT_TYPE])->column('field', 'name');
  563. if(!$fieldnames){
  564. $this->error('请在系统管理->字段管理中保存合同管理表单生成合同导入字段');
  565. }
  566. $customerNames=Customer::where([])->column('id,owner_staff_id','name');
  567. $contacts = Contacts::where([])->field('id,name,customer_id')->select();
  568. $contacts = collection($contacts)->toArray();
  569. $contactsNames = [];
  570. foreach ($contacts as $v) {
  571. $contactsNames[$v['customer_id']][$v['name']] = $v;
  572. }
  573. $businessNames=Business::where([])->column('id','name');
  574. $staffNames=Staff::where([])->column('id','name');
  575. $fn = [];
  576. $nums=$this->model->where([])->column('num');
  577. for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
  578. $values = [];
  579. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  580. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  581. $values[] = is_null($val) ? '' : $val;
  582. }
  583. foreach ($values as $l) {
  584. $fn[] = $fieldnames[$l] ?? '';
  585. }
  586. }
  587. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  588. $values = [];
  589. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  590. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  591. if ($val instanceof RichText) {//富文本转换字符串
  592. $val = $val->__toString();
  593. }
  594. $values[] = is_null($val) ? NULL : trim($val);
  595. }
  596. $lastid++;
  597. $addContract = ['id' => $lastid];
  598. $customer=isset($customerNames[$values[0]])?$customerNames[$values[0]]:0;
  599. if(empty($customer)){
  600. $errorInfo[] = "第{$currentRow}行,客户名称不存在;";
  601. continue;
  602. }
  603. $addContract['customer_id']=$customer['id'];
  604. $addContract['owner_staff_id']=$customer['owner_staff_id'];
  605. $addContract['create_staff_id']=$customer['owner_staff_id'];
  606. $contacts = isset($contactsNames[$addContract['customer_id']])?(isset($contactsNames[$addContract['customer_id']][$values[1]]) ? $contactsNames[$addContract['customer_id']][$values[1]] : []):[];
  607. if (!empty($values[1]) && empty($contacts)) {
  608. $errorInfo[] = "第{$currentRow}行,客户签约人名称不存在;";
  609. continue;
  610. }
  611. $addContract['contacts_id']=$contacts['id']??0;
  612. $business=isset($businessNames[$values[2]])?$businessNames[$values[2]]:[];
  613. if (!empty($values[2]) && empty($business)) {
  614. $errorInfo[] = "第{$currentRow}行,商机名称不存在;";
  615. continue;
  616. }
  617. $addContract['business_id']=$business['id']??0;
  618. $orderstaff=isset($staffNames[$values[3]])?$staffNames[$values[3]]:[];
  619. if (!empty($values[3]) && empty($orderstaff)) {
  620. $errorInfo[] = "第{$currentRow}行,商机名称不存在;";
  621. continue;
  622. }
  623. $addContract['order_staff_id']=$orderstaff['id']??0;
  624. foreach ($values as $kv => $value) {
  625. if (!isset($fn[$kv]) || empty($fn[$kv])) {
  626. continue;
  627. }
  628. $addContract[$fn[$kv]] = $value;
  629. }
  630. if (empty($addContract['num'])) {
  631. $errorInfo[] = "第{$currentRow}行,合同编号不能为空;";
  632. continue;
  633. }
  634. if (in_array($addContract['num'], $nums)) {
  635. $errorInfo[] = "第{$currentRow}行,合同编号`{$addContract['num']}`已存在;";
  636. continue;
  637. }
  638. if (empty($addContract['name'])) {
  639. $errorInfo[] = "第{$currentRow}行,合同名称不能为空;";
  640. continue;
  641. }
  642. $contractRow[] = $addContract;
  643. }
  644. if (!empty($errorInfo)) {
  645. $this->error(implode(',', $errorInfo));
  646. }
  647. Db::startTrans();
  648. try {
  649. $this->model::importContract($contractRow);
  650. Db::commit();
  651. } catch (Exception $e) {
  652. Db::rollback();
  653. $this->error($e->getMessage());
  654. }
  655. $this->success('导入成功');
  656. }
  657. $this->assign('staffs', Staff::getList());
  658. return $this->view->fetch();
  659. }
  660. /**
  661. * 模板
  662. */
  663. public function template()
  664. {
  665. $title = [
  666. '客户名称',
  667. '客户签约人',
  668. '商机名称',
  669. '公司签约人',
  670. ];
  671. $contractData = Form::getDataValue(Form::CONTRACT_TYPE);
  672. foreach ($contractData as $val) {
  673. $title[] = $val['config']['label'];
  674. }
  675. $file = export_excel($title, [], '合同');
  676. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  677. header('Content-Disposition: attachment;filename=' . $file['fileName']);
  678. header('Cache-Control: max-age=0');
  679. $obj = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  680. // 以下内容是excel文件的信息描述信息
  681. $obj->getProperties()->setTitle('导出文件'); //设置标题
  682. $obj->setActiveSheetIndex(0);
  683. $obj->getActiveSheet()->setTitle('导出文件');
  684. /* 循环读取每个单元格的数据 */
  685. $a = 'A';
  686. $currentSheet = $obj->getActiveSheet();
  687. foreach ($title as $key => $value) {
  688. //读取工作表1
  689. // 设置第一行加粗
  690. $obj->getActiveSheet()->getStyle($a . '1')->getFont()->setBold(true);
  691. //这里是设置单元格的内容
  692. $currentSheet->getCell($a . '1')->setValue($value);
  693. $a++;
  694. }
  695. $PHPWriter = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($obj);
  696. $PHPWriter->save('php://output');
  697. }
  698. /**
  699. * 导出信息
  700. */
  701. public function export()
  702. {
  703. $this->request->filter(['strip_tags', 'trim']);
  704. $ids = input('ids');
  705. $isall = input('isall');
  706. $wheres = array();
  707. //导出其中几条
  708. if (isset($ids)) {
  709. $wheres['id'] = array('in', $ids);
  710. }
  711. //导出全部
  712. if (isset($isall)) {
  713. if ($isall == 3) {
  714. unset($wheres['id']);
  715. }
  716. }
  717. //0:全部 1:我负责的 2:下属负责的 3:今日待跟进 4:今日已跟进 5:从未跟进的
  718. $type = input('type',0);
  719. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  720. switch($type){
  721. case 1:
  722. $staff = Staff::info();
  723. $wheres['owner_staff_id'] = $staff->id;
  724. break;
  725. case 2:
  726. $wheres['owner_staff_id'] = array('in',Staff::getLowerStaffId());
  727. break;
  728. case 3:
  729. $start = date('Y-m-d 00:00:00');
  730. $end = date('Y-m-d 23:59:59');
  731. $record = collection(Record::where(array('relation_type'=>3,'next_time'=>array(array('egt',$start),array('elt',$end))))->field("id,relation_id")->select())->toArray();
  732. $relationId = [];
  733. foreach($record as $k=>$v){
  734. $whereRe['id'] = array('gt',$v['id']);
  735. $whereRe['relation_id'] = $v['relation_id'];
  736. $recordData = Record::where($whereRe)->count();
  737. if($recordData == 0){
  738. $relationId[] = $v['relation_id'];
  739. }
  740. }
  741. $wheres['id'] = array('in',$relationId);
  742. $staff = Staff::info();
  743. $wheres['owner_staff_id'] = $staff->id;
  744. break;
  745. case 4:
  746. $start = date('Y-m-d 00:00:00');
  747. $end = date('Y-m-d 23:59:59');
  748. $record = collection(Record::where(array('relation_type'=>3,'next_time'=>array(array('egt',$start),array('elt',$end))))->field("id,relation_id")->select())->toArray();
  749. $relationId = [];
  750. foreach($record as $k=>$v){
  751. $whereRe['id'] = array('gt',$v['id']);
  752. $whereRe['relation_id'] = $v['relation_id'];
  753. $recordData = Record::where($whereRe)->count();
  754. if($recordData >=1){
  755. $relationId[] = $v['relation_id'];
  756. }
  757. }
  758. $wheres['id'] = array('in',$relationId);
  759. $staff = Staff::info();
  760. $wheres['owner_staff_id'] = $staff->id;
  761. break;
  762. case 5:
  763. $record = collection(Record::where(array('relation_type'=>3,'next_time'=>array('neq','')))->column('relation_id'))->toArray();
  764. $wheres['id'] = array('not in',$record);
  765. $staff = Staff::info();
  766. $wheres['owner_staff_id'] = $staff->id;
  767. break;
  768. default:
  769. $wheres['owner_staff_id'] = array('in',Staff::getMyStaffIds());
  770. break;
  771. }
  772. $list = $this->model->with([
  773. 'customer',
  774. 'orderStaff',
  775. 'contacts',
  776. 'ownerStaff','contractOther',
  777. ])->where($where)->where($wheres)->order($sort, $order)->select();
  778. $list = collection($list)->toArray();
  779. if (!$list) {
  780. $this->error('无导出数据');
  781. }
  782. $title = [
  783. '序号',
  784. '客户名称',
  785. '客户签约人',
  786. '公司签约人',
  787. '归属人',
  788. '创建时间',
  789. ];
  790. $dataValue = Form::getDataValue(Form::CONTRACT_TYPE);
  791. foreach ($dataValue as $val) {
  792. $title[] = $val['config']['label'];
  793. }
  794. foreach ($list as $k => $v) {
  795. if($v['contract_other']){//其他客户
  796. $other=$v['contract_other']['otherdata'];
  797. $other=json_decode($other,true);
  798. $v = array_merge($v, $other);
  799. }
  800. $field = array(
  801. $v['id'],
  802. $v['customer']['name'],
  803. $v['contacts']['name']??'',
  804. $v['order_staff']['name']??'',
  805. $v['owner_staff']['name']??'',
  806. $v['createtime'],
  807. );
  808. foreach ($dataValue as $val) {
  809. if ($val['component'] == 'uploadImage' || $val['component'] == 'uploadFile') {
  810. $field[] = $v[$val['id'] . '_str'] ?? '';
  811. } else {
  812. $field[] = ($v[$val['id']] ?? '');
  813. }
  814. }
  815. $data[] = $field;
  816. }
  817. $file = export_excel($title, $data, '合同');
  818. if ($file['filePath']) {
  819. $this->success('导出成功', '', $file);
  820. }
  821. $this->error('导出失败');
  822. }
  823. /**
  824. * 导入产品明细
  825. * @return string|void
  826. */
  827. public function import_product()
  828. {
  829. set_time_limit(0);
  830. if ($this->request->isPost()) {
  831. $file = $this->request->request('file');
  832. if (!$file) {
  833. $this->error(__('Parameter %s can not be empty', 'file'));
  834. }
  835. $filePath = ROOT_PATH . 'public' . $file;
  836. if (!is_file($filePath)) {
  837. $this->error(__('No results were found'));
  838. }
  839. //实例化reader
  840. $ext = pathinfo($filePath, PATHINFO_EXTENSION);
  841. if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
  842. $this->error(__('Unknown data format'));
  843. }
  844. if ($ext === 'csv') {
  845. $file = fopen($filePath, 'r');
  846. $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
  847. $fp = fopen($filePath, "w");
  848. $n = 0;
  849. while ($line = fgets($file)) {
  850. $line = rtrim($line, "\n\r\0");
  851. $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
  852. if ($encoding != 'utf-8') {
  853. $line = mb_convert_encoding($line, 'utf-8', $encoding);
  854. }
  855. if ($n == 0 || preg_match('/^".*"$/', $line)) {
  856. fwrite($fp, $line . "\n");
  857. } else {
  858. fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
  859. }
  860. $n++;
  861. }
  862. fclose($file) || fclose($fp);
  863. $reader = new Csv();
  864. } elseif ($ext === 'xls') {
  865. $reader = new Xls();
  866. } else {
  867. $reader = new Xlsx();
  868. }
  869. if (!$PHPExcel = $reader->load($filePath)) {
  870. $this->error(__('Unknown data format'));
  871. }
  872. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  873. $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  874. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  875. $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
  876. //开始读取数据
  877. $fields = [];
  878. for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
  879. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  880. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  881. $fields[$currentRow][] = $val;
  882. if ($val instanceof RichText) {//富文本转换字符串
  883. $val = $val->__toString();
  884. }
  885. $values[] = is_null($val) ? '' : trim($val);
  886. }
  887. }
  888. if (!isset($fields[1])) {
  889. $this->error('导入文件第一行没有数据');
  890. }
  891. $lastid = ContractProduct::order('id desc')->value('id');
  892. $lastid = $lastid + 5;//防止重复
  893. $contractProductRow = [];
  894. $errorInfo = [];
  895. $contractNum=$this->model::where([])->column('id,name','num');
  896. $productName=Product::where([])->column('id,price','name');
  897. $fn = [];
  898. for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
  899. $values = [];
  900. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  901. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  902. $values[] = is_null($val) ? '' : $val;
  903. }
  904. }
  905. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  906. $values = [];
  907. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  908. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  909. if ($val instanceof RichText) {//富文本转换字符串
  910. $val = $val->__toString();
  911. }
  912. $values[] = is_null($val) ? NULL : trim($val);
  913. }
  914. $lastid++;
  915. $addContractProduct = ['id' => $lastid];
  916. $contract = isset($contractNum[$values[0]]) ? $contractNum[$values[0]] : 0;
  917. if (empty($contract)) {
  918. $errorInfo[] = "第{$currentRow}行,合同编号不存在;";
  919. continue;
  920. }
  921. $product = isset($productName[$values[1]]) ? $productName[$values[1]] : 0;
  922. if (empty($product)) {
  923. $errorInfo[] = "第{$currentRow}行,产品名称不存在;";
  924. continue;
  925. }
  926. if (empty($values[2])) {
  927. $errorInfo[] = "第{$currentRow}行,产品数量不能为空;";
  928. continue;
  929. }
  930. if (empty($values[3])) {
  931. $errorInfo[] = "第{$currentRow}行,产品售价不能为空;";
  932. continue;
  933. }
  934. $addContractProduct['contract_id'] = $contract['id'];
  935. $addContractProduct['product_id'] = $product['id'];
  936. $addContractProduct['number'] = $values[2];
  937. $addContractProduct['price'] = $values[3];
  938. $contractProductRow[]=$addContractProduct;
  939. }
  940. if (!empty($errorInfo)) {
  941. $this->error(implode(',', $errorInfo));
  942. }
  943. Db::startTrans();
  944. try {
  945. ContractProduct::importProduct($contractProductRow);
  946. Db::commit();
  947. } catch (Exception $e) {
  948. Db::rollback();
  949. $this->error($e->getMessage());
  950. }
  951. $this->success('导入成功');
  952. }
  953. $this->assign('staffs', Staff::getList());
  954. return $this->view->fetch();
  955. }
  956. /**
  957. * 模板
  958. */
  959. public function template_product()
  960. {
  961. $title = [
  962. '合同编号',
  963. '商品名称',
  964. '数量',
  965. '售价',
  966. ];
  967. $file = export_excel($title, [], '产品明细');
  968. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  969. header('Content-Disposition: attachment;filename=' . $file['fileName']);
  970. header('Cache-Control: max-age=0');
  971. $obj = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  972. // 以下内容是excel文件的信息描述信息
  973. $obj->getProperties()->setTitle('导出文件'); //设置标题
  974. $obj->setActiveSheetIndex(0);
  975. $obj->getActiveSheet()->setTitle('导出文件');
  976. /* 循环读取每个单元格的数据 */
  977. $a = 'A';
  978. $currentSheet = $obj->getActiveSheet();
  979. foreach ($title as $key => $value) {
  980. //读取工作表1
  981. // 设置第一行加粗
  982. $obj->getActiveSheet()->getStyle($a . '1')->getFont()->setBold(true);
  983. //这里是设置单元格的内容
  984. $currentSheet->getCell($a . '1')->setValue($value);
  985. $a++;
  986. }
  987. $PHPWriter = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($obj);
  988. $PHPWriter->save('php://output');
  989. }
  990. /**
  991. * 导出信息
  992. */
  993. public function export_product()
  994. {
  995. $ids = input('ids');
  996. $type = input('type');
  997. $wheres = array();
  998. //导出其中几条
  999. if (isset($ids)) {
  1000. $wheres['contract_id'] = array('in', $ids);
  1001. }
  1002. //导出全部
  1003. if (isset($type)) {
  1004. if ($type == 3) {
  1005. unset($wheres['contract_id']);
  1006. }
  1007. }
  1008. $list=ContractProduct::where($wheres)->with(['contract','productinfo'])->select();
  1009. $list = collection($list)->toArray();
  1010. if (!$list) {
  1011. $this->error('无导出数据');
  1012. }
  1013. $title = [
  1014. '序号',
  1015. '合同编号',
  1016. '合同名称',
  1017. '商品名称',
  1018. '售价',
  1019. '批发价',
  1020. '产品',
  1021. '规格',
  1022. '数量',
  1023. ];
  1024. $data=[];
  1025. foreach ($list as $k => $v) {
  1026. $field = array(
  1027. $v['id'],
  1028. $v['contract']['num'],
  1029. $v['contract']['name'],
  1030. $v['productinfo']['name']??'',
  1031. $v['price']??'',
  1032. $v['productinfo']['cost_price']??'',
  1033. $v['productinfo']['goods']['name']??'',
  1034. $v['productinfo']['unit']??'',
  1035. $v['number']??'',
  1036. );
  1037. $data[] = $field;
  1038. }
  1039. $file = export_excel($title, $data, '产品明细');
  1040. if ($file['filePath']) {
  1041. $this->success('导出成功', '', $file);
  1042. }
  1043. $this->error('导出失败');
  1044. }
  1045. /**
  1046. * 导出word
  1047. */
  1048. public function words($ids = null)
  1049. {
  1050. if ($this->request->isPost()) {
  1051. $file = input('file', '');
  1052. $is_template=input('is_template',0);
  1053. $template_name=input('template_name','');
  1054. if (empty($file)) {
  1055. $this->error('导出模板');
  1056. }
  1057. if($is_template == 1){
  1058. if(empty($template_name)){
  1059. $this->error('模板名称不能为空');
  1060. }
  1061. $create=[
  1062. 'type'=>'contract',
  1063. 'name'=>$template_name,
  1064. 'url'=>$file
  1065. ];
  1066. $wordTemplateModel = new WordTemplate();
  1067. $wordTemplateModel->save($create);
  1068. }
  1069. $tmp = new TemplateProcessor('.' . $file);
  1070. Settings::setCompatibility(true);
  1071. Settings::setOutputEscapingEnabled(true);
  1072. $row = $this->model->where(['id'=>$ids])->with(['customer','contacts','business','orderStaff'])->find();
  1073. $row = $row->toArray();
  1074. $row = ContractOther::getOther($row);
  1075. $form = Form::getDataValue(Form::CONTRACT_TYPE);
  1076. $imgNames=[];
  1077. foreach($form as $k=>$v){
  1078. if($v['component'] == 'uploadImage'){
  1079. $imgNames[]=$v['id'];
  1080. }
  1081. }
  1082. foreach ($row as $key => $item) {
  1083. if(in_array($key,$imgNames) && $item ){
  1084. if(is_array($item)){
  1085. foreach ($item as $it){
  1086. $tmp->setImageValue($key,ltrim(parse_url($it['file_path'])['path'],'/'));
  1087. }
  1088. }else{
  1089. $fileinfo = File::where(['id'=>['in',$item]])->field('id,name,file_path,save_name')->select();
  1090. foreach ($fileinfo as $it){
  1091. $tmp->setImageValue($key,ltrim(parse_url($it['file_path'])['path'],'/'));
  1092. }
  1093. }
  1094. }else{
  1095. if(is_array($item)){
  1096. $tmp->setValue($key , '');//
  1097. continue;
  1098. }
  1099. $tmp->setValue($key , $item);//
  1100. }
  1101. }
  1102. $contractProduct=ContractProduct::where(['contract_id'=>$ids])
  1103. ->with(['productinfo'])->select();
  1104. $contractProduct=collection($contractProduct)->toArray();
  1105. $c = count($contractProduct);//总行数
  1106. // $tmp->cloneRow('product_name', $c);//复制行
  1107. // for ($i = 0; $i < $c; $i++) {
  1108. // $p=$contractProduct[$i];
  1109. // $tmp->setValue("product_name#" . ($i + 1), $p['productinfo']['name'] ?? '');//名称
  1110. // $tmp->setValue("product_unit#" . ($i + 1), $p['productinfo']['unit'] ?? '');//单位
  1111. // $tmp->setValue("product_num#" . ($i + 1), $p['productinfo']['num'] ?? '');//编码
  1112. // $tmp->setValue("product_type#" . ($i + 1), $p['productinfo']['type'] ?? '');//规格
  1113. // $tmp->setValue("product_number#" . ($i + 1), $p['number']);//数量
  1114. // $tmp->setValue("product_price#" . ($i + 1), $p['price']);//总价格
  1115. // $tmp->setValue("product_total#" . ($i + 1), $p['price'] * $p['number']);//总价格
  1116. // }
  1117. $tmp->setValue('money_big', convertAmountToCn($row['money']));
  1118. $tmp->setValue('customer_name', $row['customer']['name'] ?? '');
  1119. $tmp->setValue('contacts_name', $row['contacts']['name'] ?? '');
  1120. $tmp->setValue('business_name', $row['business']['name'] ?? '');
  1121. $tmp->setValue('order_staff_name', $row['order_staff']['name'] ?? '');
  1122. //end 配置
  1123. $filename = $row['name'];
  1124. $filepath = $filename . '.docx';
  1125. $fileurl = './uploads/' . date('Ymd') . '/';
  1126. if (!file_exists($fileurl)) {
  1127. mkdir($fileurl);
  1128. }
  1129. $tmp->saveAs($fileurl . $filepath);//另存为
  1130. $model = new File();
  1131. $data = [
  1132. 'types' => 'file',
  1133. 'name' => $filepath,
  1134. 'save_name' => $fileurl . $filepath,
  1135. 'size' => filesize($fileurl . $filepath),
  1136. 'file_path' => trim($fileurl, '.') . $filepath,
  1137. ];
  1138. $model->save($data);
  1139. $lastid = $model->id;
  1140. $file = cdnurl($model::getUrl($lastid), true);
  1141. $this->success('成功', '', ['file' => $file, 'id' => $lastid, 'filename' => $filepath]);
  1142. }
  1143. $templates=WordTemplate::where(['type'=>'contract'])->select();
  1144. $contract = Form::getDataValue(Form::CONTRACT_TYPE);
  1145. $columns = array();
  1146. $columns[] = ['key' => 'customer_name', 'name' => '客户名称'];
  1147. $columns[] = ['key' => 'contacts_name', 'name' => '联系人名称'];
  1148. $columns[] = ['key' => 'business_name', 'name' => '商机名称'];
  1149. $columns[] = ['key' => 'order_staff_name', 'name' => '公司签约人名称'];
  1150. $columns[] = ['key' => 'money_big', 'name' => '合同金额人民币大写'];
  1151. foreach ($contract as $k => $v) {
  1152. $columns[] = array(
  1153. 'key' => $v['id'],
  1154. 'name' => $v['config']['label'],
  1155. );
  1156. }
  1157. $columns[] = ['key' => 'product_name', 'name' => '产品名称'];
  1158. $columns[] = ['key' => 'product_unit', 'name' => '产品单位'];
  1159. $columns[] = ['key' => 'product_num', 'name' => '产品编码'];
  1160. $columns[] = ['key' => 'product_type', 'name' => '产品规格'];
  1161. $columns[] = ['key' => 'product_number', 'name' => '产品数量'];
  1162. $columns[] = ['key' => 'product_price', 'name' => '产品单价'];
  1163. $columns[] = ['key' => 'product_total', 'name' => '产品合计'];
  1164. $this->assign('column', $columns);
  1165. $this->assign('templates', $templates);
  1166. $this->assign('ids', $ids);
  1167. return $this->view->fetch();
  1168. }
  1169. }