where($where) ->count('id'); $list = Db::name('filing') ->field('id,filingCode,cat_id,good_name,num,good_img,price,expect_service,companyName,companyCode,status,addtime,"" catinfo,orderCode,cgd_charge') ->where($where) ->page($data['page'], $data['size']) ->order('id', 'desc') ->withAttr('good_img', function ($val) { return explode(',', $val); }) ->select() ->toArray(); $all_cat = []; foreach ($list as &$value) { if (!isset($all_cat[$value['cat_id']])) $all_cat[$value['cat_id']] = implode('/', array_column(made($value['cat_id']), 'name')); $value['catinfo'] = $all_cat[$value['cat_id']]; } return json_show(0, '获取报备单列表成功', ['count' => $count, 'list' => $list]); } //添加 public static function add(array $data = []) { if ($data['is_determine_price'] == 1) $data['cgd_charge'] = round(bcsub($data['price'], $data['expect_service'], 3), 2);//确定售价时,采购价=售价-服务费 else $data['price'] = round(bcadd($data['cgd_charge'], $data['expect_service'], 3), 2);//不确定售价时,售价=采购价+服务费 $customerName = Db::name('business') ->where(['companyNo' => $data['customerCode']]) ->value('company', ''); if ($customerName == '') return json_show(1004, '该业务公司不存在'); $rs = Db::name('filing') ->strict(false) ->insert(array_merge($data, [ 'customerName' => $customerName, 'filingCode' => makeNo('BM'), 'service_proportion' => $data['expect_service_proportion'],//服务费比例与期望服务费比例一致 'service_charge' => $data['expect_service'],//服务费与期望服务费一致 'specinfo' => json_encode($data['spec_list']), 'send_way' => 2, 'cert_fee' => 0, 'pakge_fee' => 0, 'cost_fee' => 0, 'mark_fee' => 0, 'demo_fee' => 0, 'open_fee' => 0, 'delivery_fee' => 0, 'is_gold_price' => 0, 'is_diff' => 0, 'apply_id' => request()->user['uid'], 'apply_name' => request()->user['nickname'], 'status' => 0, 'is_del' => 0, 'addtime' => date('Y-m-d H:i:s'), 'updatetime' => date('Y-m-d H:i:s'), 'gold_weight' => $data['gold_weight'] ?? 0, 'noble_metal' => $data['noble_metal'] ?? 0, 'config' => $data['config'] ?? '', 'other_config' => $data['other_config'] ?? '', 'good_img' => implode(',', $data['good_img']), 'origin_place' => implode(',', $data['origin_place']), 'delivery_place' => implode(',', $data['delivery_place']), ])); return $rs ? json_show(0, '创建报备单成功') : json_show(1004, '创建报备单失败'); } //详情 public static function detail(array $data = []) { $rs = Db::name('filing') ->alias('a') ->field('a.*,b.brand_name,d.unit unit_name,p.platform_name,"" cat_info') ->leftJoin('brand b', 'b.id=a.brand_id') ->leftJoin('unit d', 'd.id=a.unit_id') ->leftJoin('platform p', 'p.id=a.platform_id') ->where(['a.is_del' => 0, 'a.id' => $data['id']]) ->withAttr('specinfo', function ($val) { return json_decode($val); }) ->withAttr('good_img', function ($val) { return explode(',', $val); })->withAttr('origin_place', function ($val) { return explode(',', $val); })->withAttr('delivery_place', function ($val) { return explode(',', $val); })->withAttr('cat_info', function ($val, $da) { return made($da['cat_id']); }) ->findOrEmpty(); return json_show(0, '获取报备单详情成功', $rs); } //审核 public static function status(array $data = []) { $rs = Db::name('filing') ->field('id,status,is_determine_price,price,cgd_charge') ->where(['is_del' => 0, 'id' => $data['id']]) ->whereIn('status', [0, 2]) ->findOrEmpty(); if (empty($rs)) return json_show(1005, '该报备单不存在或不允许审核'); $companyName = Db::name('customer_info') ->where(['is_del' => 0, 'companyNo' => $data['companyCode']]) ->value('companyName', ''); if ($companyName == '') return json_show(1005, '该客户不存在'); $tmp = Db::name('platform') ->where(['is_del' => 0, 'id' => $data['platform_id']]) ->field('id') ->findOrEmpty(); if (empty($tmp)) return json_show(1005, '该平台不存在'); $update = array_merge($data, [ 'companyName' => $companyName, 'updatetime' => date('Y-m-d H:i:s'), ]); if ($rs['is_determine_price'] == 1) $update['cgd_charge'] = round(bcsub($rs['price'], $data['service_charge'], 3), 2);//确定售价时,采购价=售价-服务费 else $update['price'] = round(bcadd($rs['cgd_charge'], $data['service_charge'], 3), 2);//不确定售价时,售价=采购价+服务费 $res = Db::name('filing') ->where(['is_del' => 0, 'id' => $data['id']]) ->whereIn('status', [0, 2]) ->update($update); return $res ? json_show(0, '操作成功') : json_show(1004, '操作失败'); } //取消转单 public static function cancel(array $data = []) { $where = [ ['is_del', '=', 0], ['id', '=', $data['id']], ['supplierNo', '=', $data['supplierNo']], ['status', 'in', [0, 1, 2, 4]] ]; $rs = Db::name('filing') ->field('id,status') ->where($where) ->findOrEmpty(); if (empty($rs)) return json_show(1005, '该报备单不存在或不允许取消'); $res = Db::name('filing') ->where($where) ->update(['status' => 5, 'updatetime' => date('Y-m-d H:i:s')]); return $res ? json_show(0, '操作成功') : json_show(1004, '操作失败'); } //转单 public static function transfer(array $data = []) { $filing = Db::name('filing') ->field(true) ->where(['is_del' => 0, 'id' => $data['id'], 'supplierNo' => $data['supplierNo'], 'status' => 2]) ->findOrEmpty(); if (empty($filing)) return json_show(1005, '该报备单不存在或不允许转单'); if (array_sum(array_column($data['addr_list'], 'receipt_quantity')) != $filing['num']) return json_show(1004, '收货地址的收货总数不符合'); $supplier = Db::name('supplier') ->field('id,person,personid,name,code') ->where(['code' => $filing['supplierNo']]) ->findOrEmpty(); $date = date('Y-m-d H:i:s'); Db::startTrans(); try { $order_type = 4;//报备商品 $order_source = 7;//报备转单 $spuCode = makeNo('SKU'); //商品表 Db::name('good_zixun') ->insert([ 'spuCode' => $spuCode, 'good_name' => $filing['good_name'], 'brand_id' => $filing['brand_id'], 'cat_id' => $filing['cat_id'], 'good_unit' => $filing['unit_id'], 'good_type' => 0, 'moq' => 1, 'customized' => $filing['make_day'], 'tax' => $filing['tax'], 'platform_id' => $filing['platform_id'], 'supplierNo' => $filing['supplierNo'], 'is_auth' => 0, 'craft_desc' => $filing['good_name'], 'good_remark' => '', 'good_img' => $filing['good_img'], 'good_thumb_img' => '', 'good_info_img' => '', 'status' => 1, 'createrid' => $filing['apply_id'], 'creater' => $filing['apply_name'], 'is_del' => 0, 'addtime' => $date, 'updatetime' => $date, 'specinfo' => $filing['specinfo'], 'work_day' => $filing['make_day'], 'noble_metal' => $filing['noble_metal'], 'is_gold_price' => $filing['is_gold_price'], 'good_weight' => $filing['gold_weight'], 'config' => $filing['config'], 'other_config' => $filing['other_config'], 'weight' => $filing['weight'], 'supply_area' => $filing['supply_area'], 'is_diff' => $filing['is_diff'], 'pay_way' => $filing['pay_way'], 'send_way' => $filing['send_way'], 'companyNo' => $filing['customerCode'], 'proof_type' => '', 'proof_url' => '', 'order_type' => $order_type ]); $orderCode = makeNo('QR'); //订单表 //sale $sale_id = Db::name('sale')->insertGetId([ 'orderCode' => $orderCode, 'apply_id' => $filing['apply_id'], 'apply_name' => $filing['apply_name'], 'order_type' => $order_type,//报备商品 'order_source' => $order_source,//报备转单 'platform_id' => $filing['platform_id'], 'good_code' => $spuCode, 'skuCode' => '', 'cat_id' => $filing['cat_id'], 'good_name' => $filing['good_name'], 'good_num' => $filing['num'], 'good_type' => 1, 'origin_price' => $filing['cgd_charge'], 'sale_price' => $filing['price'], 'total_price' => round(bcmul($filing['price'], $filing['num'], 3), 2), 'post_fee' => 0, 'is_diff' => $filing['is_diff'], 'is_activity' => 0, 'activity_code' => '', 'is_stock' => 0, 'customer_code' => $filing['companyCode'], 'supplierNo' => $filing['customerCode'], 'zxNo' => '', 'platform_order' => '', 'send_num' => 0, 'wsend_num' => $filing['num'], 'th_num' => 0, 'th_fee' => 0, 'send_status' => 1, 'send_type' => 1,//直接发货 'remark' => '', 'status' => 0, 'is_del' => 0, 'proof_id' => 0, 'other_orderNo' => '', 'workNo' => '', 'poNo' => '', 'use_order' => 0, 'good_weight' => 0, 'gold_price' => 0, 'cost_price' => $filing['cost_fee'], 'diff_weight' => 0, 'diff_fee' => 0, 'returnCode' => '', 'addtime' => $date, 'updatetime' => $date, 'cgderid' => $supplier['personid'], 'cgder' => $supplier['person'], 'good_createrid' => $filing['apply_id'], 'good_creater' => $filing['apply_name'], ]); //仓库 $wsm = Db::name('warehouse_info') ->field('id,wsm_code') ->where(['supplierNo' => $filing['supplierNo'], 'companyNo' => $filing['customerCode'], 'is_del' => 0]) ->findOrEmpty(); if (empty($wsm)) { $wsm_code = makeNo('WSM'); Db::name('warehouse_info')->insert([ 'wsm_code' => $wsm_code, 'name' => $supplier['name'], 'wsm_type' => 2, 'supplierNo' => $supplier['code'], 'addr' => '', 'addrs_code' => '', 'contactor' => 0, 'contactor_name' => 0, 'mobile' => '', 'position' => '', 'companyNo' => $filing['supplierNo'], 'status' => 1, 'is_del' => 0, 'addtime' => $date, 'updatetime' => $date, ]); } else $wsm_code = $wsm['wsm_code']; //采购单 $cgdCode = makeNo('CG'); Db::name('purchease_order')->insert([ 'cgdNo' => $cgdCode, 'bkcode' => '', 'wsm_code' => $wsm_code, 'cgder' => $supplier['person'], 'cgder_id' => $supplier['personid'], 'spuCode' => $spuCode, 'good_name' => $filing['good_name'], 'good_num' => $filing['num'], 'good_price' => $filing['cgd_charge'], 'total_fee' => round(bcmul($filing['cgd_charge'], $filing['num']), 2), 'pakge_fee' => $filing['pakge_fee'], 'cert_fee' => $filing['cert_fee'], 'open_fee' => $filing['open_fee'], 'delivery_fee' => $filing['delivery_fee'], 'mark_fee' => $filing['mark_fee'], 'demo_fee' => $filing['demo_fee'], 'diff_weight' => '0', 'diff_fee' => '0', 'gold_price' => '0', 'supplierNo' => $filing['supplierNo'], 'supplier_name' => $filing['supplierName'], 'companyNo' => $filing['customerCode'], 'send_status' => 1, 'send_num' => '0', 'wsend_num' => $filing['num'], 'remark' => '', 'status' => 0, 'lasttime' => $date, 'is_del' => 0, 'order_type' => $order_type, 'order_source' => $order_source, 'good_type' => 1, 'addtime' => $date, 'updatetime' => $date, 'good_createrid' => $filing['apply_id'], 'good_creater' => $filing['apply_name'], ]); //台账 $standing_bood_data = [ 'standBookNo' => makeNo('IO'), 'orderCode' => $orderCode, 'sale_id' => $sale_id, 'customer_code' => $filing['companyCode'], 'supplierNo' => $filing['supplierNo'], 'order_type' => $order_type, 'order_source' => $order_source, 'spuCode' => $spuCode, 'addtime' => $date, 'updatetime' => $date, 'cgdNo' => $cgdCode, ]; $i = 0; $order_send_insert = []; foreach ($data['addr_list'] as $addr) { //地址addr $addrid = Db::name('order_addr')->insertGetId([ 'orderCode' => $orderCode, 'addr' => $addr['addr'], 'addr_code' => implode(',', $addr['addr_code']), 'contactor' => $addr['contactor'], 'mobile' => $addr['mobile'], 'customer_code' => $filing['companyCode'], 'post_fee' => 0, 'receipt_quantity' => $addr['receipt_quantity'], 'is_del' => 0, 'addtime' => $date, 'updatetime' => $date, ]); //发货单 $outCode = makeNo('DF'); //改变编码规则,将原来的编码后两位换成序列号 //str_pad字符串填充 $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT); Db::name('order_out')->insert([ 'orderCode' => $orderCode, 'outCode' => $outCode, 'apply_id' => request()->user['uid'], 'apply_name' => request()->user['nickname'], 'addrid' => $addrid, 'post_name' => '', 'post_code' => '', 'post_fee' => 0, 'sendtime' => $date, 'send_num' => $addr['receipt_quantity'], 'check_num' => 0, 'error_num' => 0, 'wsm_code' => '', 'order_type' => $order_type, 'status' => 0, 'addtime' => $date, 'updatetime' => $date, ]); $standing_bood_data['outCode'][] = $outCode; $order_send_insert[] = [ 'cgdNo' => $cgdCode, 'outCode' => $outCode, 'send_num' => $addr['receipt_quantity'], 'bnCode' => '', 'status' => 1, 'addtime' => $date, 'updatetime' => $date, ]; $i++; } //更新报备单 Db::name('filing') ->where(['is_del' => 0, 'id' => $data['id'], 'supplierNo' => $data['supplierNo'], 'status' => 2]) ->update(['status' => 3, 'updatetime' => $date, 'orderCode' => $orderCode, 'reason' => '']); //待办已办先不处理 //关联表 Db::name('order_num') ->insert([ 'orderCode' => $orderCode, 'cgdNo' => $cgdCode, 'spuCode' => $spuCode, 'companyNo' => $filing['customerCode'], 'good_num' => $filing['num'], 'wsend_num' => $filing['num'], 'send_num' => 0, 'wait_num' => $filing['num'], ]); if ($order_send_insert) Db::name('order_send')->insertAll($order_send_insert); //处理台账 if (isset($standing_bood_data['outCode']) && is_array($standing_bood_data['outCode'])) $standing_bood_data['outCode'] = implode(',', $standing_bood_data['outCode']); Db::name('standing_book')->insert($standing_bood_data); Db::commit(); return json_show(0, '转单成功'); } catch (Exception $exception) { Db::rollback(); Db::name('filing') ->where(['is_del' => 0, 'id' => $data['id'], 'supplierNo' => $data['supplierNo'], 'status' => 2]) ->update(['status' => 4, 'updatetime' => $date, 'reason' => $exception->getMessage()]); return json_show(1005, '转单失败,' . $exception->getMessage()); } } }