Filing.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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'] . ' 00:00:00', $data['end_date'] . ' 23:59:59']];
  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_proportion' => $data['expect_service_proportion'],//服务费比例与期望服务费比例一致
  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,is_determine_price,price,cgd_charge')
  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. $update = array_merge($data, [
  127. 'companyName' => $companyName,
  128. 'updatetime' => date('Y-m-d H:i:s'),
  129. ]);
  130. if ($rs['is_determine_price'] == 1) $update['cgd_charge'] = round(bcsub($rs['price'], $data['service_charge'], 3), 2);//确定售价时,采购价=售价-服务费
  131. else $update['price'] = round(bcadd($rs['cgd_charge'], $data['service_charge'], 3), 2);//不确定售价时,售价=采购价+服务费
  132. $res = Db::name('filing')
  133. ->where(['is_del' => 0, 'id' => $data['id']])
  134. ->whereIn('status', [0, 2])
  135. ->update($update);
  136. return $res ? json_show(0, '操作成功') : json_show(1004, '操作失败');
  137. }
  138. //取消转单
  139. public static function cancel(array $data = [])
  140. {
  141. $where = [
  142. ['is_del', '=', 0],
  143. ['id', '=', $data['id']],
  144. ['supplierNo', '=', $data['supplierNo']],
  145. ['status', 'in', [0, 1, 2, 4]]
  146. ];
  147. $rs = Db::name('filing')
  148. ->field('id,status')
  149. ->where($where)
  150. ->findOrEmpty();
  151. if (empty($rs)) return json_show(1005, '该报备单不存在或不允许取消');
  152. $res = Db::name('filing')
  153. ->where($where)
  154. ->update(['status' => 5, 'updatetime' => date('Y-m-d H:i:s')]);
  155. return $res ? json_show(0, '操作成功') : json_show(1004, '操作失败');
  156. }
  157. //转单
  158. public static function transfer(array $data = [])
  159. {
  160. $filing = Db::name('filing')
  161. ->field(true)
  162. ->where(['is_del' => 0, 'id' => $data['id'], 'supplierNo' => $data['supplierNo'], 'status' => 2])
  163. ->findOrEmpty();
  164. if (empty($filing)) return json_show(1005, '该报备单不存在或不允许转单');
  165. if (array_sum(array_column($data['addr_list'], 'receipt_quantity')) != $filing['num']) return json_show(1004, '收货地址的收货总数不符合');
  166. $supplier = Db::name('supplier')
  167. ->field('id,person,personid,name,code')
  168. ->where(['code' => $filing['supplierNo']])
  169. ->findOrEmpty();
  170. $date = date('Y-m-d H:i:s');
  171. Db::startTrans();
  172. try {
  173. $order_type = 4;//报备商品
  174. $order_source = 7;//报备转单
  175. $spuCode = makeNo('SKU');
  176. //商品表
  177. Db::name('good_zixun')
  178. ->insert([
  179. 'spuCode' => $spuCode,
  180. 'good_name' => $filing['good_name'],
  181. 'brand_id' => $filing['brand_id'],
  182. 'cat_id' => $filing['cat_id'],
  183. 'good_unit' => $filing['unit_id'],
  184. 'good_type' => 0,
  185. 'moq' => 1,
  186. 'customized' => $filing['make_day'],
  187. 'tax' => $filing['tax'],
  188. 'platform_id' => $filing['platform_id'],
  189. 'supplierNo' => $filing['supplierNo'],
  190. 'is_auth' => 0,
  191. 'craft_desc' => $filing['good_name'],
  192. 'good_remark' => '',
  193. 'good_img' => $filing['good_img'],
  194. 'good_thumb_img' => '',
  195. 'good_info_img' => '',
  196. 'status' => 1,
  197. 'createrid' => $filing['apply_id'],
  198. 'creater' => $filing['apply_name'],
  199. 'is_del' => 0,
  200. 'addtime' => $date,
  201. 'updatetime' => $date,
  202. 'specinfo' => $filing['specinfo'],
  203. 'work_day' => $filing['make_day'],
  204. 'noble_metal' => $filing['noble_metal'],
  205. 'is_gold_price' => $filing['is_gold_price'],
  206. 'good_weight' => $filing['gold_weight'],
  207. 'config' => $filing['config'],
  208. 'other_config' => $filing['other_config'],
  209. 'weight' => $filing['weight'],
  210. 'supply_area' => $filing['supply_area'],
  211. 'is_diff' => $filing['is_diff'],
  212. 'pay_way' => $filing['pay_way'],
  213. 'send_way' => $filing['send_way'],
  214. 'companyNo' => $filing['customerCode'],
  215. 'proof_type' => '',
  216. 'proof_url' => '',
  217. 'order_type' => $order_type
  218. ]);
  219. $orderCode = makeNo('QR');
  220. //订单表
  221. //sale
  222. $sale_id = Db::name('sale')->insertGetId([
  223. 'orderCode' => $orderCode,
  224. 'apply_id' => $filing['apply_id'],
  225. 'apply_name' => $filing['apply_name'],
  226. 'order_type' => $order_type,//报备商品
  227. 'order_source' => $order_source,//报备转单
  228. 'platform_id' => $filing['platform_id'],
  229. 'good_code' => $spuCode,
  230. 'skuCode' => '',
  231. 'cat_id' => $filing['cat_id'],
  232. 'good_name' => $filing['good_name'],
  233. 'good_num' => $filing['num'],
  234. 'good_type' => 1,
  235. 'origin_price' => $filing['cgd_charge'],
  236. 'sale_price' => $filing['price'],
  237. 'total_price' => round(bcmul($filing['price'], $filing['num'], 3), 2),
  238. 'post_fee' => 0,
  239. 'is_diff' => $filing['is_diff'],
  240. 'is_activity' => 0,
  241. 'activity_code' => '',
  242. 'is_stock' => 0,
  243. 'customer_code' => $filing['companyCode'],
  244. 'supplierNo' => $filing['customerCode'],
  245. 'zxNo' => '',
  246. 'platform_order' => '',
  247. 'send_num' => 0,
  248. 'wsend_num' => $filing['num'],
  249. 'th_num' => 0,
  250. 'th_fee' => 0,
  251. 'send_status' => 1,
  252. 'send_type' => 1,//直接发货
  253. 'remark' => '',
  254. 'status' => 0,
  255. 'is_del' => 0,
  256. 'proof_id' => 0,
  257. 'other_orderNo' => '',
  258. 'workNo' => '',
  259. 'poNo' => '',
  260. 'use_order' => 0,
  261. 'good_weight' => 0,
  262. 'gold_price' => 0,
  263. 'cost_price' => $filing['cost_fee'],
  264. 'diff_weight' => 0,
  265. 'diff_fee' => 0,
  266. 'returnCode' => '',
  267. 'addtime' => $date,
  268. 'updatetime' => $date,
  269. 'cgderid' => $supplier['personid'],
  270. 'cgder' => $supplier['person'],
  271. 'good_createrid' => $filing['apply_id'],
  272. 'good_creater' => $filing['apply_name'],
  273. ]);
  274. //仓库
  275. $wsm = Db::name('warehouse_info')
  276. ->field('id,wsm_code')
  277. ->where(['supplierNo' => $filing['supplierNo'], 'companyNo' => $filing['customerCode'], 'is_del' => 0])
  278. ->findOrEmpty();
  279. if (empty($wsm)) {
  280. $wsm_code = makeNo('WSM');
  281. Db::name('warehouse_info')->insert([
  282. 'wsm_code' => $wsm_code,
  283. 'name' => $supplier['name'],
  284. 'wsm_type' => 2,
  285. 'supplierNo' => $supplier['code'],
  286. 'addr' => '',
  287. 'addrs_code' => '',
  288. 'contactor' => 0,
  289. 'contactor_name' => 0,
  290. 'mobile' => '',
  291. 'position' => '',
  292. 'companyNo' => $filing['supplierNo'],
  293. 'status' => 1,
  294. 'is_del' => 0,
  295. 'addtime' => $date,
  296. 'updatetime' => $date,
  297. ]);
  298. } else $wsm_code = $wsm['wsm_code'];
  299. //采购单
  300. $cgdCode = makeNo('CG');
  301. Db::name('purchease_order')->insert([
  302. 'cgdNo' => $cgdCode,
  303. 'bkcode' => '',
  304. 'wsm_code' => $wsm_code,
  305. 'cgder' => $supplier['person'],
  306. 'cgder_id' => $supplier['personid'],
  307. 'spuCode' => $spuCode,
  308. 'good_name' => $filing['good_name'],
  309. 'good_num' => $filing['num'],
  310. 'good_price' => $filing['cgd_charge'],
  311. 'total_fee' => round(bcmul($filing['cgd_charge'], $filing['num']), 2),
  312. 'pakge_fee' => $filing['pakge_fee'],
  313. 'cert_fee' => $filing['cert_fee'],
  314. 'open_fee' => $filing['open_fee'],
  315. 'delivery_fee' => $filing['delivery_fee'],
  316. 'mark_fee' => $filing['mark_fee'],
  317. 'demo_fee' => $filing['demo_fee'],
  318. 'diff_weight' => '0',
  319. 'diff_fee' => '0',
  320. 'gold_price' => '0',
  321. 'supplierNo' => $filing['supplierNo'],
  322. 'supplier_name' => $filing['supplierName'],
  323. 'companyNo' => $filing['customerCode'],
  324. 'send_status' => 1,
  325. 'send_num' => '0',
  326. 'wsend_num' => $filing['num'],
  327. 'remark' => '',
  328. 'status' => 0,
  329. 'lasttime' => $date,
  330. 'is_del' => 0,
  331. 'order_type' => $order_type,
  332. 'order_source' => $order_source,
  333. 'good_type' => 1,
  334. 'addtime' => $date,
  335. 'updatetime' => $date,
  336. 'good_createrid' => $filing['apply_id'],
  337. 'good_creater' => $filing['apply_name'],
  338. ]);
  339. //台账
  340. $standing_bood_data = [
  341. 'standBookNo' => makeNo('IO'),
  342. 'orderCode' => $orderCode,
  343. 'sale_id' => $sale_id,
  344. 'customer_code' => $filing['companyCode'],
  345. 'supplierNo' => $filing['supplierNo'],
  346. 'order_type' => $order_type,
  347. 'order_source' => $order_source,
  348. 'spuCode' => $spuCode,
  349. 'addtime' => $date,
  350. 'updatetime' => $date,
  351. 'cgdNo' => $cgdCode,
  352. ];
  353. $i = 0;
  354. $order_send_insert = [];
  355. foreach ($data['addr_list'] as $addr) {
  356. //地址addr
  357. $addrid = Db::name('order_addr')->insertGetId([
  358. 'orderCode' => $orderCode,
  359. 'addr' => $addr['addr'],
  360. 'addr_code' => implode(',', $addr['addr_code']),
  361. 'contactor' => $addr['contactor'],
  362. 'mobile' => $addr['mobile'],
  363. 'customer_code' => $filing['companyCode'],
  364. 'post_fee' => 0,
  365. 'receipt_quantity' => $addr['receipt_quantity'],
  366. 'is_del' => 0,
  367. 'addtime' => $date,
  368. 'updatetime' => $date,
  369. ]);
  370. //发货单
  371. $outCode = makeNo('DF');
  372. //改变编码规则,将原来的编码后两位换成序列号
  373. //str_pad字符串填充
  374. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  375. Db::name('order_out')->insert([
  376. 'orderCode' => $orderCode,
  377. 'outCode' => $outCode,
  378. 'apply_id' => request()->user['uid'],
  379. 'apply_name' => request()->user['nickname'],
  380. 'addrid' => $addrid,
  381. 'post_name' => '',
  382. 'post_code' => '',
  383. 'post_fee' => 0,
  384. 'sendtime' => $date,
  385. 'send_num' => $addr['receipt_quantity'],
  386. 'check_num' => 0,
  387. 'error_num' => 0,
  388. 'wsm_code' => '',
  389. 'order_type' => $order_type,
  390. 'status' => 0,
  391. 'addtime' => $date,
  392. 'updatetime' => $date,
  393. ]);
  394. $standing_bood_data['outCode'][] = $outCode;
  395. $order_send_insert[] = [
  396. 'cgdNo' => $cgdCode,
  397. 'outCode' => $outCode,
  398. 'send_num' => $addr['receipt_quantity'],
  399. 'bnCode' => '',
  400. 'status' => 1,
  401. 'addtime' => $date,
  402. 'updatetime' => $date,
  403. ];
  404. $i++;
  405. }
  406. //更新报备单
  407. Db::name('filing')
  408. ->where(['is_del' => 0, 'id' => $data['id'], 'supplierNo' => $data['supplierNo'], 'status' => 2])
  409. ->update(['status' => 3, 'updatetime' => $date, 'orderCode' => $orderCode, 'reason' => '']);
  410. //待办已办先不处理
  411. //关联表
  412. Db::name('order_num')
  413. ->insert([
  414. 'orderCode' => $orderCode,
  415. 'cgdNo' => $cgdCode,
  416. 'spuCode' => $spuCode,
  417. 'companyNo' => $filing['customerCode'],
  418. 'good_num' => $filing['num'],
  419. 'wsend_num' => $filing['num'],
  420. 'send_num' => 0,
  421. 'wait_num' => $filing['num'],
  422. ]);
  423. if ($order_send_insert) Db::name('order_send')->insertAll($order_send_insert);
  424. //处理台账
  425. if (isset($standing_bood_data['outCode']) && is_array($standing_bood_data['outCode'])) $standing_bood_data['outCode'] = implode(',', $standing_bood_data['outCode']);
  426. Db::name('standing_book')->insert($standing_bood_data);
  427. Db::commit();
  428. return json_show(0, '转单成功');
  429. } catch (Exception $exception) {
  430. Db::rollback();
  431. Db::name('filing')
  432. ->where(['is_del' => 0, 'id' => $data['id'], 'supplierNo' => $data['supplierNo'], 'status' => 2])
  433. ->update(['status' => 4, 'updatetime' => $date, 'reason' => $exception->getMessage()]);
  434. return json_show(1005, '转单失败,' . $exception->getMessage());
  435. }
  436. }
  437. }