Filing.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. namespace app\abutment\logic;
  3. use think\Exception;
  4. use think\facade\Db;
  5. class Filing
  6. {
  7. //列表
  8. public static function list(array $data = [])
  9. {
  10. $where = [['is_del', '=', '0']];
  11. if ($data['cat_id'] != '') $where[] = ['cat_id', '=', $data['cat_id']];
  12. if ($data['start_date'] != '' && $data['end_date'] != '') $where[] = ['addtime', 'between', [$data['start_date'], $data['end_date']]];
  13. if ($data['filingCode'] != '') $where[] = ['filingCode', 'like', '%' . $data['filingCode'] . '%'];
  14. if ($data['status'] != '') $where[] = ['status', '=', $data['status']];
  15. if ($data['good_name'] != '') $where[] = ['good_name', 'like', '%' . $data['good_name'] . '%'];
  16. if ($data['companyName'] != '') $where[] = ['companyName', 'like', '%' . $data['companyName'] . '%'];
  17. if ($data['companyCode'] != '') $where[] = ['companyCode', 'like', '%' . $data['companyCode'] . '%'];
  18. if ($data['orderCode'] != '') $where[] = ['orderCode', 'like', '%' . $data['orderCode'] . '%'];
  19. if ($data['supplierNo'] != '') $where[] = ['supplierNo', '=', $data['supplierNo']];
  20. $count = Db::name('filing')
  21. ->where($where)
  22. ->count('id');
  23. $list = Db::name('filing')
  24. ->field('id,filingCode,cat_id,good_name,num,good_img,price,expect_service,companyName,companyCode,status,addtime,"" catinfo,orderCode')
  25. ->where($where)
  26. ->page($data['page'], $data['size'])
  27. ->order('id', 'desc')
  28. ->withAttr('good_img', function ($val) {
  29. return explode(',', $val);
  30. })
  31. ->select()
  32. ->toArray();
  33. $all_cat = [];
  34. foreach ($list as &$value) {
  35. if (!isset($all_cat[$value['cat_id']])) {
  36. $value['catinfo'] = $all_cat[$value['cat_id']] = implode('/', array_column(made($value['cat_id']), 'name'));
  37. }
  38. }
  39. return json_show(0, '获取报备单列表成功', ['count' => $count, 'list' => $list]);
  40. }
  41. //添加
  42. public static function add(array $data = [])
  43. {
  44. if ($data['is_determine_price'] == 1) $data['cgd_charge'] = round(bcsub($data['price'], $data['expect_service'], 3), 2);//确定售价时,采购价=售价-服务费
  45. else $data['price'] = round(bcadd($data['cgd_charge'], $data['expect_service'], 3), 2);//不确定售价时,售价=采购价+服务费
  46. $customerName = Db::name('business')
  47. ->where(['companyNo' => $data['customerCode']])
  48. ->value('company', '');
  49. if ($customerName == '') return json_show(1004, '该业务公司不存在');
  50. $rs = Db::name('filing')
  51. ->strict(false)
  52. ->insert(array_merge($data, [
  53. 'customerName' => $customerName,
  54. 'filingCode' => makeNo('BM'),
  55. 'service_charge' => $data['expect_service'],//服务费与期望服务费一致
  56. 'specinfo' => json_encode($data['spec_list']),
  57. 'send_way' => 2,
  58. 'cert_fee' => 0,
  59. 'pakge_fee' => 0,
  60. 'cost_fee' => 0,
  61. 'mark_fee' => 0,
  62. 'demo_fee' => 0,
  63. 'open_fee' => 0,
  64. 'delivery_fee' => 0,
  65. 'is_gold_price' => 0,
  66. 'is_diff' => 0,
  67. 'apply_id' => request()->user['uid'],
  68. 'apply_name' => request()->user['nickname'],
  69. 'status' => 0,
  70. 'is_del' => 0,
  71. 'addtime' => date('Y-m-d H:i:s'),
  72. 'updatetime' => date('Y-m-d H:i:s'),
  73. 'gold_weight' => $data['gold_weight'] ?? 0,
  74. 'noble_metal' => $data['noble_metal'] ?? 0,
  75. 'config' => $data['config'] ?? '',
  76. 'other_config' => $data['other_config'] ?? '',
  77. 'good_img' => implode(',', $data['good_img']),
  78. 'origin_place' => implode(',', $data['origin_place']),
  79. 'delivery_place' => implode(',', $data['delivery_place']),
  80. ]));
  81. return $rs ? json_show(0, '创建报备单成功') : json_show(1004, '创建报备单失败');
  82. }
  83. //详情
  84. public static function detail(array $data = [])
  85. {
  86. $rs = Db::name('filing')
  87. ->alias('a')
  88. ->field('a.*,b.brand_name,d.unit unit_name,p.platform_name,"" cat_info')
  89. ->leftJoin('brand b', 'b.id=a.brand_id')
  90. ->leftJoin('unit d', 'd.id=a.unit_id')
  91. ->leftJoin('platform p', 'p.id=a.platform_id')
  92. ->where(['a.is_del' => 0, 'a.id' => $data['id']])
  93. ->withAttr('specinfo', function ($val) {
  94. return json_decode($val);
  95. })
  96. ->withAttr('good_img', function ($val) {
  97. return explode(',', $val);
  98. })->withAttr('origin_place', function ($val) {
  99. return explode(',', $val);
  100. })->withAttr('delivery_place', function ($val) {
  101. return explode(',', $val);
  102. })->withAttr('cat_info', function ($val, $da) {
  103. return made($da['cat_id']);
  104. })
  105. ->findOrEmpty();
  106. return json_show(0, '获取报备单详情成功', $rs);
  107. }
  108. //审核
  109. public static function status(array $data = [])
  110. {
  111. $rs = Db::name('filing')
  112. ->field('id,status')
  113. ->where(['is_del' => 0, 'id' => $data['id']])
  114. ->whereIn('status', [0, 2])
  115. ->findOrEmpty();
  116. if (empty($rs)) return json_show(1005, '该报备单不存在或不允许审核');
  117. $companyName = Db::name('customer_info')
  118. ->where(['is_del' => 0, 'companyNo' => $data['companyCode']])
  119. ->value('companyName', '');
  120. if ($companyName == '') return json_show(1005, '该客户不存在');
  121. $tmp = Db::name('platform')
  122. ->where(['is_del' => 0, 'id' => $data['platform_id']])
  123. ->field('id')
  124. ->findOrEmpty();
  125. if (empty($tmp)) return json_show(1005, '该平台不存在');
  126. $res = Db::name('filing')
  127. ->where(['is_del' => 0, 'id' => $data['id']])
  128. ->whereIn('status', [0, 2])
  129. ->update(array_merge($data, [
  130. 'companyName' => $companyName,
  131. 'updatetime' => date('Y-m-d H:i:s')
  132. ]));
  133. return $res ? json_show(0, '操作成功') : json_show(1004, '操作失败');
  134. }
  135. //取消转单
  136. public static function cancel(array $data = [])
  137. {
  138. $where = [
  139. ['is_del', '=', 0],
  140. ['id', '=', $data['id']],
  141. ['supplierNo', '=', $data['supplierNo']],
  142. ['status', 'in', [0, 1, 2, 4]]
  143. ];
  144. $rs = Db::name('filing')
  145. ->field('id,status')
  146. ->where($where)
  147. ->findOrEmpty();
  148. if (empty($rs)) return json_show(1005, '该报备单不存在或不允许取消');
  149. $res = Db::name('filing')
  150. ->where($where)
  151. ->update(['status' => 5, 'updatetime' => date('Y-m-d H:i:s')]);
  152. return $res ? json_show(0, '操作成功') : json_show(1004, '操作失败');
  153. }
  154. //转单
  155. public static function transfer(array $data = [])
  156. {
  157. $filing = Db::name('filing')
  158. ->field(true)
  159. ->where(['is_del' => 0, 'id' => $data['id'], 'supplierNo' => $data['supplierNo'], 'status' => 2])
  160. ->findOrEmpty();
  161. if (empty($filing)) return json_show(1005, '该报备单不存在或不允许转单');
  162. if (array_sum(array_column($data['addr_list'], 'receipt_quantity')) != $filing['num']) return json_show(1004, '收货地址的收货总数不符合');
  163. $supplier = Db::name('supplier')
  164. ->field('id,person,personid,name,code')
  165. ->where(['code' => $filing['supplierNo']])
  166. ->findOrEmpty();
  167. $date = date('Y-m-d H:i:s');
  168. Db::startTrans();
  169. try {
  170. $order_type = 4;//报备商品
  171. $order_source = 7;//报备转单
  172. $spuCode = makeNo('SKU');
  173. //商品表
  174. Db::name('good_zixun')
  175. ->insert([
  176. 'spuCode' => $spuCode,
  177. 'good_name' => $filing['good_name'],
  178. 'brand_id' => $filing['brand_id'],
  179. 'cat_id' => $filing['cat_id'],
  180. 'good_unit' => $filing['unit_id'],
  181. 'good_type' => 0,
  182. 'moq' => 1,
  183. 'customized' => $filing['make_day'],
  184. 'tax' => $filing['tax'],
  185. 'platform_id' => $filing['platform_id'],
  186. 'supplierNo' => $filing['supplierNo'],
  187. 'is_auth' => 0,
  188. 'craft_desc' => $filing['good_name'],
  189. 'good_remark' => '',
  190. 'good_img' => $filing['good_img'],
  191. 'good_thumb_img' => '',
  192. 'good_info_img' => '',
  193. 'status' => 1,
  194. 'createrid' => $filing['apply_id'],
  195. 'creater' => $filing['apply_name'],
  196. 'is_del' => 0,
  197. 'addtime' => $date,
  198. 'updatetime' => $date,
  199. 'specinfo' => $filing['specinfo'],
  200. 'work_day' => $filing['make_day'],
  201. 'noble_metal' => $filing['noble_metal'],
  202. 'is_gold_price' => $filing['is_gold_price'],
  203. 'good_weight' => $filing['gold_weight'],
  204. 'config' => $filing['config'],
  205. 'other_config' => $filing['other_config'],
  206. 'weight' => $filing['weight'],
  207. 'supply_area' => $filing['supply_area'],
  208. 'is_diff' => $filing['is_diff'],
  209. 'pay_way' => $filing['pay_way'],
  210. 'send_way' => $filing['send_way'],
  211. 'companyNo' => $filing['customerCode'],
  212. 'proof_type' => '',
  213. 'proof_url' => '',
  214. 'order_type' => $order_type
  215. ]);
  216. $orderCode = makeNo('QR');
  217. //订单表
  218. //sale
  219. $sale_id = Db::name('sale')->insertGetId([
  220. 'orderCode' => $orderCode,
  221. 'apply_id' => $filing['apply_id'],
  222. 'apply_name' => $filing['apply_name'],
  223. 'order_type' => $order_type,//报备商品
  224. 'order_source' => $order_source,//报备转单
  225. 'platform_id' => $filing['platform_id'],
  226. 'good_code' => $spuCode,
  227. 'skuCode' => '',
  228. 'cat_id' => $filing['cat_id'],
  229. 'good_name' => $filing['good_name'],
  230. 'good_num' => $filing['num'],
  231. 'good_type' => 1,
  232. 'origin_price' => $filing['cgd_charge'],
  233. 'sale_price' => $filing['price'],
  234. 'total_price' => round(bcmul($filing['price'], $filing['num'], 3), 2),
  235. 'post_fee' => 0,
  236. 'is_diff' => $filing['is_diff'],
  237. 'is_activity' => 0,
  238. 'activity_code' => '',
  239. 'is_stock' => 0,
  240. 'customer_code' => $filing['companyCode'],
  241. 'supplierNo' => $filing['supplierNo'],
  242. 'zxNo' => '',
  243. 'platform_order' => '',
  244. 'send_num' => 0,
  245. 'wsend_num' => $filing['num'],
  246. 'th_num' => 0,
  247. 'th_fee' => 0,
  248. 'send_status' => 1,
  249. 'send_type' => 1,//直接发货
  250. 'remark' => '',
  251. 'status' => 0,
  252. 'is_del' => 0,
  253. 'proof_id' => 0,
  254. 'other_orderNo' => '',
  255. 'workNo' => '',
  256. 'poNo' => '',
  257. 'use_order' => 0,
  258. 'good_weight' => 0,
  259. 'gold_price' => 0,
  260. 'cost_price' => $filing['cost_fee'],
  261. 'diff_weight' => 0,
  262. 'diff_fee' => 0,
  263. 'returnCode' => '',
  264. 'addtime' => $date,
  265. 'updatetime' => $date,
  266. 'cgderid' => $supplier['personid'],
  267. 'cgder' => $supplier['person'],
  268. 'good_createrid' => $filing['apply_id'],
  269. 'good_creater' => $filing['apply_name'],
  270. ]);
  271. //仓库
  272. $wsm = Db::name('warehouse_info')
  273. ->field('id,wsm_code')
  274. ->where(['supplierNo' => $filing['supplierNo'], 'companyNo' => $filing['customerCode'], 'is_del' => 0])
  275. ->findOrEmpty();
  276. if (empty($wsm)) {
  277. $wsm_code = makeNo('WSM');
  278. Db::name('warehouse_info')->insert([
  279. 'wsm_code' => $wsm_code,
  280. 'name' => $supplier['name'],
  281. 'wsm_type' => 2,
  282. 'supplierNo' => $supplier['code'],
  283. 'addr' => '',
  284. 'addrs_code' => '',
  285. 'contactor' => 0,
  286. 'contactor_name' => 0,
  287. 'mobile' => '',
  288. 'position' => '',
  289. 'companyNo' => $filing['supplierNo'],
  290. 'status' => 1,
  291. 'is_del' => 0,
  292. 'addtime' => $date,
  293. 'updatetime' => $date,
  294. ]);
  295. } else $wsm_code = $wsm['wsm_code'];
  296. //采购单
  297. $cgdCode = makeNo('CG');
  298. Db::name('purchease_order')->insert([
  299. 'cgdNo' => $cgdCode,
  300. 'bkcode' => '',
  301. 'wsm_code' => $wsm_code,
  302. 'cgder' => $supplier['person'],
  303. 'cgder_id' => $supplier['personid'],
  304. 'spuCode' => $spuCode,
  305. 'good_name' => $filing['good_name'],
  306. 'good_num' => $filing['num'],
  307. 'good_price' => $filing['cgd_charge'],
  308. 'total_fee' => round(bcmul($filing['cgd_charge'], $filing['num']), 2),
  309. 'pakge_fee' => $filing['pakge_fee'],
  310. 'cert_fee' => $filing['cert_fee'],
  311. 'open_fee' => $filing['open_fee'],
  312. 'delivery_fee' => $filing['delivery_fee'],
  313. 'mark_fee' => $filing['mark_fee'],
  314. 'demo_fee' => $filing['demo_fee'],
  315. 'diff_weight' => '0',
  316. 'diff_fee' => '0',
  317. 'gold_price' => '0',
  318. 'supplierNo' => $filing['supplierNo'],
  319. 'supplier_name' => $filing['supplierName'],
  320. 'companyNo' => $filing['customerCode'],
  321. 'send_status' => 1,
  322. 'send_num' => '0',
  323. 'wsend_num' => $filing['num'],
  324. 'remark' => '',
  325. 'status' => 0,
  326. 'lasttime' => $date,
  327. 'is_del' => 0,
  328. 'order_type' => $order_type,
  329. 'order_source' => $order_source,
  330. 'good_type' => 1,
  331. 'addtime' => $date,
  332. 'updatetime' => $date,
  333. 'good_createrid' => $filing['apply_id'],
  334. 'good_creater' => $filing['apply_name'],
  335. ]);
  336. //台账
  337. $standing_bood_data = [
  338. 'standBookNo' => makeNo('IO'),
  339. 'orderCode' => $orderCode,
  340. 'sale_id' => $sale_id,
  341. 'customer_code' => $filing['companyCode'],
  342. 'supplierNo' => $filing['supplierNo'],
  343. 'order_type' => $order_type,
  344. 'order_source' => $order_source,
  345. 'spuCode' => $spuCode,
  346. 'addtime' => $date,
  347. 'updatetime' => $date,
  348. 'cgdNo' => $cgdCode,
  349. ];
  350. $i = 0;
  351. $order_send_insert = [];
  352. foreach ($data['addr_list'] as $addr) {
  353. //地址addr
  354. $addrid = Db::name('order_addr')->insertGetId([
  355. 'orderCode' => $orderCode,
  356. 'addr' => $addr['addr'],
  357. 'addr_code' => implode(',', $addr['addr_code']),
  358. 'contactor' => $addr['contactor'],
  359. 'mobile' => $addr['mobile'],
  360. 'customer_code' => $filing['companyCode'],
  361. 'post_fee' => 0,
  362. 'receipt_quantity' => $addr['receipt_quantity'],
  363. 'is_del' => 0,
  364. 'addtime' => $date,
  365. 'updatetime' => $date,
  366. ]);
  367. //发货单
  368. $outCode = makeNo('DF');
  369. //改变编码规则,将原来的编码后两位换成序列号
  370. //str_pad字符串填充
  371. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  372. Db::name('order_out')->insert([
  373. 'orderCode' => $orderCode,
  374. 'outCode' => $outCode,
  375. 'apply_id' => request()->user['uid'],
  376. 'apply_name' => request()->user['nickname'],
  377. 'addrid' => $addrid,
  378. 'post_name' => '',
  379. 'post_code' => '',
  380. 'post_fee' => 0,
  381. 'sendtime' => $date,
  382. 'send_num' => $addr['receipt_quantity'],
  383. 'check_num' => 0,
  384. 'error_num' => 0,
  385. 'wsm_code' => '',
  386. 'order_type' => $order_type,
  387. 'status' => 0,
  388. 'addtime' => $date,
  389. 'updatetime' => $date,
  390. ]);
  391. $standing_bood_data['outCode'][] = $outCode;
  392. $order_send_insert[] = [
  393. 'cgdNo' => $cgdCode,
  394. 'outCode' => $outCode,
  395. 'send_num' => $addr['receipt_quantity'],
  396. 'bnCode' => '',
  397. 'status' => 1,
  398. 'addtime' => $date,
  399. 'updatetime' => $date,
  400. ];
  401. $i++;
  402. }
  403. //更新报备单
  404. Db::name('filing')
  405. ->where(['is_del' => 0, 'id' => $data['id'], 'supplierNo' => $data['supplierNo'], 'status' => 2])
  406. ->update(['status' => 3, 'updatetime' => $date, 'orderCode' => $orderCode, 'reason' => '']);
  407. //待办已办先不处理
  408. //关联表
  409. Db::name('order_num')
  410. ->insert([
  411. 'orderCode' => $orderCode,
  412. 'cgdNo' => $cgdCode,
  413. 'spuCode' => $spuCode,
  414. 'companyNo' => $filing['customerCode'],
  415. 'good_num' => $filing['num'],
  416. 'wsend_num' => $filing['num'],
  417. 'send_num' => 0,
  418. 'wait_num' => $filing['num'],
  419. ]);
  420. if ($order_send_insert) Db::name('order_send')->insertAll($order_send_insert);
  421. //处理台账
  422. if (isset($standing_bood_data['outCode']) && is_array($standing_bood_data['outCode'])) $standing_bood_data['outCode'] = implode(',', $standing_bood_data['outCode']);
  423. Db::name('standing_book')->insert($standing_bood_data);
  424. Db::commit();
  425. return json_show(0, '转单成功');
  426. } catch (Exception $exception) {
  427. Db::rollback();
  428. Db::name('filing')
  429. ->where(['is_del' => 0, 'id' => $data['id'], 'supplierNo' => $data['supplierNo'], 'status' => 2])
  430. ->update(['status' => 4, 'updatetime' => $date, 'reason' => $exception->getMessage()]);
  431. return json_show(1005, '转单失败,' . $exception->getMessage());
  432. }
  433. }
  434. }