Contract.php 48 KB

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