Filing.php 19 KB

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