PrintingLogic.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * 打印设置逻辑类
  4. *
  5. * @author qifan
  6. * @date 2020-12-03
  7. */
  8. namespace app\admin\logic;
  9. use app\admin\controller\ApiCommon;
  10. use think\Db;
  11. class PrintingLogic
  12. {
  13. /**
  14. * 打印模板列表
  15. *
  16. * @param $page
  17. * @param $limit
  18. * @return array
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. * @throws \think\exception\DbException
  22. */
  23. public function index($page, $limit)
  24. {
  25. $result = [];
  26. $type = [1 => '商机', 2 => '合同', 3 => '回款'];
  27. $field = ['id', 'name', 'type', 'user_name', 'create_time', 'update_time'];
  28. $count = Db::name('admin_printing')->count();
  29. $data = Db::name('admin_printing')->field($field)->order('id', 'desc')->limit(($page - 1) * $limit, $limit)->select();
  30. foreach ($data AS $key => $value) {
  31. $result[] = [
  32. 'id' => $value['id'],
  33. 'name' => $value['name'],
  34. 'type' => $value['type'],
  35. 'type_name' => !empty($type[$value['type']]) ? $type[$value['type']] : '',
  36. 'create_time' => date('Y-m-d H:i:s', $value['create_time']),
  37. 'user_name' => $value['user_name'],
  38. 'update_time' => date('Y-m-d H:i:s', $value['update_time'])
  39. ];
  40. }
  41. return ['count' => $count, 'list' => $result];
  42. }
  43. /**
  44. * 创建打印模板
  45. *
  46. * @param $param
  47. * @return int|string
  48. */
  49. public function create($param)
  50. {
  51. $apiCommon = new ApiCommon();
  52. $userId = $apiCommon->userInfo['id'];
  53. $userName = Db::name('admin_user')->where('id', $userId)->value('realname');
  54. $data = [
  55. 'user_id' => $userId,
  56. 'user_name' => $userName,
  57. 'name' => $param['name'],
  58. 'type' => $param['type'],
  59. 'content' => htmlspecialchars($param['content']),
  60. 'create_time' => time(),
  61. 'update_time' => time()
  62. ];
  63. return Db::name('admin_printing')->insert($data);
  64. }
  65. /**
  66. * 获取模板详情
  67. *
  68. * @param $id
  69. * @return array
  70. */
  71. public function read($id)
  72. {
  73. $content = Db::name('admin_printing')->where('id', $id)->value('content');
  74. return ['id' => $id, 'content' => htmlspecialchars_decode($content)];
  75. }
  76. /**
  77. * 更新模板数据
  78. *
  79. * @param $param
  80. * @return int|string
  81. * @throws \think\Exception
  82. * @throws \think\exception\PDOException
  83. */
  84. public function update($param)
  85. {
  86. if (!empty($param['content'])) $param['content'] = htmlspecialchars($param['content']);
  87. return Db::name('admin_printing')->update($param);
  88. }
  89. /**
  90. * 删除模板数据
  91. *
  92. * @param $id
  93. * @return int
  94. * @throws \think\Exception
  95. * @throws \think\exception\PDOException
  96. */
  97. public function delete($id)
  98. {
  99. return Db::name('admin_printing')->where('id', $id)->delete();
  100. }
  101. /**
  102. * 复制模板数据
  103. *
  104. * @param $id
  105. * @return false|int|string
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. * @throws \think\exception\DbException
  109. */
  110. public function copy($id)
  111. {
  112. $apiCommon = new ApiCommon();
  113. $info = Db::name('admin_printing')->where('id', $id)->find();
  114. if (!empty($info['id'])) {
  115. $userId = $apiCommon->userInfo['id'];
  116. $userName = Db::name('admin_user')->where('id', $userId)->value('realname');
  117. $data = [
  118. 'user_id' => $userId,
  119. 'user_name' => $userName,
  120. 'name' => strlen($info['name']) > 25 ? $info['name'] : $info['name'] . rand(111, 999),
  121. 'type' => $info['type'],
  122. 'content' => $info['content'],
  123. 'update_time' => time(),
  124. 'create_time' => time()
  125. ];
  126. return Db::name('admin_printing')->insert($data);
  127. }
  128. return false;
  129. }
  130. /**
  131. * 获取打印模板需要的字段
  132. *
  133. * @param $type 1商机;2合同;3回款
  134. * @return array[]
  135. * @throws \think\db\exception\DataNotFoundException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. * @throws \think\exception\DbException
  138. */
  139. public function getFields($type)
  140. {
  141. $result = [];
  142. switch ($type) {
  143. case 1:
  144. $result['business'] = $this->getBusinessFields();
  145. $result['customer'] = $this->getCustomerFields();
  146. $result['product'] = $this->getProductFields();
  147. break;
  148. case 2:
  149. $result['contract'] = $this->getContractFields();
  150. $result['customer'] = $this->getCustomerFields();
  151. $result['contacts'] = $this->getContactsFields();
  152. $result['product'] = $this->getProductFields();
  153. break;
  154. case 3:
  155. $result['receivables'] = $this->getReceivablesFields();
  156. $result['contract'] = $this->getContractFields();
  157. break;
  158. default:
  159. $result['business'] = $this->getBusinessFields();
  160. $result['customer'] = $this->getCustomerFields();
  161. $result['product'] = $this->getProductFields();
  162. $result['contract'] = $this->getContractFields();
  163. $result['contacts'] = $this->getContactsFields();
  164. $result['receivables'] = $this->getReceivablesFields();
  165. }
  166. return $result;
  167. }
  168. /**
  169. * 获取商机字段
  170. *
  171. * @return array
  172. * @throws \think\db\exception\DataNotFoundException
  173. * @throws \think\db\exception\ModelNotFoundException
  174. * @throws \think\exception\DbException
  175. */
  176. private function getBusinessFields()
  177. {
  178. $result = [];
  179. $businessList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_business')->select();
  180. # 处理自定义字段
  181. foreach ($businessList AS $key => $value) {
  182. if ($value['field'] == 'customer_id') continue;
  183. $result[] = [
  184. 'name' => $value['name'],
  185. 'field' => $value['field']
  186. ];
  187. }
  188. # 处理固定字段
  189. $result[] = ['name' => '负责人', 'field' => 'owner_user_id'];
  190. $result[] = ['name' => '创建人', 'field' => 'create_user_id'];
  191. $result[] = ['name' => '创建日期', 'field' => 'create_time'];
  192. $result[] = ['name' => '更新日期', 'field' => 'update_time'];
  193. return $result;
  194. }
  195. /**
  196. * 获取客户字段
  197. *
  198. * @return array
  199. * @throws \think\db\exception\DataNotFoundException
  200. * @throws \think\db\exception\ModelNotFoundException
  201. * @throws \think\exception\DbException
  202. */
  203. private function getCustomerFields()
  204. {
  205. $result = [];
  206. $customerList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_customer')->select();
  207. # 处理自定义字段
  208. foreach ($customerList AS $key => $value) {
  209. if (in_array($value['field'], ['next_time', 'remark'])) continue;
  210. $result[] = [
  211. 'name' => $value['name'],
  212. 'field' => $value['field']
  213. ];
  214. }
  215. return $result;
  216. }
  217. /**
  218. * 获取产品字段
  219. *
  220. * @return array
  221. * @throws \think\db\exception\DataNotFoundException
  222. * @throws \think\db\exception\ModelNotFoundException
  223. * @throws \think\exception\DbException
  224. */
  225. private function getProductFields()
  226. {
  227. $result = [];
  228. $productList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_product')->select();
  229. # 处理自定义字段
  230. foreach ($productList AS $key => $value) {
  231. if ($value['field'] == 'status') continue;
  232. $result[] = [
  233. 'name' => $value['name'],
  234. 'field' => $value['field']
  235. ];
  236. }
  237. # 处理固定字段
  238. $result[] = ['name' => '售价', 'field' => 'sales_price'];
  239. $result[] = ['name' => '数量', 'field' => 'count'];
  240. $result[] = ['name' => '折扣', 'field' => 'discount'];
  241. $result[] = ['name' => '整单折扣', 'field' => 'discount_rate'];
  242. $result[] = ['name' => '合计', 'field' => 'subtotal'];
  243. return $result;
  244. }
  245. /**
  246. * 获取合同字段
  247. *
  248. * @return array
  249. * @throws \think\db\exception\DataNotFoundException
  250. * @throws \think\db\exception\ModelNotFoundException
  251. * @throws \think\exception\DbException
  252. */
  253. private function getContractFields()
  254. {
  255. $result = [];
  256. $contractList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_contract')->select();
  257. # 处理自定义字段
  258. foreach ($contractList AS $key => $value) {
  259. $result[] = [
  260. 'name' => $value['name'],
  261. 'field' => $value['field']
  262. ];
  263. }
  264. # 处理固定字段
  265. $result[] = ['name' => '负责人', 'field' => 'owner_user_id'];
  266. $result[] = ['name' => '创建人', 'field' => 'create_user_id'];
  267. $result[] = ['name' => '创建日期', 'field' => 'create_time'];
  268. $result[] = ['name' => '更新日期', 'field' => 'update_time'];
  269. $result[] = ['name' => '已收款金额', 'field' => 'received'];
  270. $result[] = ['name' => '未收款金额', 'field' => 'uncollected'];
  271. return $result;
  272. }
  273. /**
  274. * 获取联系人字段
  275. *
  276. * @return array
  277. * @throws \think\db\exception\DataNotFoundException
  278. * @throws \think\db\exception\ModelNotFoundException
  279. * @throws \think\exception\DbException
  280. */
  281. private function getContactsFields()
  282. {
  283. $result = [];
  284. $contactsList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_contacts')->select();
  285. # 处理自定义字段
  286. foreach ($contactsList AS $key => $value) {
  287. if ($value['field'] == 'next_time') continue;
  288. $result[] = [
  289. 'name' => $value['name'],
  290. 'field' => $value['field']
  291. ];
  292. }
  293. return $result;
  294. }
  295. /**
  296. * 获取回款字段
  297. *
  298. * @return array
  299. * @throws \think\db\exception\DataNotFoundException
  300. * @throws \think\db\exception\ModelNotFoundException
  301. * @throws \think\exception\DbException
  302. */
  303. private function getReceivablesFields()
  304. {
  305. $result = [];
  306. $receivablesList = Db::name('admin_field')->field(['name', 'field'])->where('types', 'crm_receivables')->select();
  307. # 处理自定义字段
  308. foreach ($receivablesList AS $key => $value) {
  309. $result[] = [
  310. 'name' => $value['name'],
  311. 'field' => $value['field']
  312. ];
  313. }
  314. # 处理固定字段
  315. $result[] = ['name' => '负责人', 'field' => 'owner_user_id'];
  316. $result[] = ['name' => '创建人', 'field' => 'create_user_id'];
  317. $result[] = ['name' => '创建日期', 'field' => 'create_time'];
  318. $result[] = ['name' => '更新日期', 'field' => 'update_time'];
  319. return $result;
  320. }
  321. }