PrintingLogic.php 12KB

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