Filing.php 18 KB

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