123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <?php
- namespace app\common\service;
- use app\common\model\BaseGoods;
- use app\common\model\BaseGoodsItem;
- use app\common\model\BaseGoodsParam;
- use app\common\model\Depot;
- use app\common\model\DepotGoods;
- use app\common\model\DepotGoodsItem;
- use app\common\model\DepotOrder;
- use app\common\model\DepotOrderItem;
- use app\common\model\GoodsParam;
- use app\common\model\OfflineOrder;
- use app\common\model\OfflineOrderRefund;
- use app\common\model\StoreGoods;
- use app\common\model\StoreGoodsItem;
- use app\common\model\SupplierOrder;
- use app\common\model\SupplierOrderRefund;
- use app\common\model\SupplierRelevancyOrder;
- use library\tools\Data;
- use think\Db;
- use think\Exception;
- /**
- * 供货管理
- * Class PurchaseLogic
- */
- class PurchaseLogic
- {
- public static $ret = ['code'=>200,'msg'=>'数据保存成功!'];
- // 采购订单审核通过后管理供货商订单
- public static function relevancyOrder($order_id)
- {
- $order_info = SupplierOrder::where('id',$order_id)->find()->toArray();
- $relevancy = [
- 'order_id'=>$order_id,
- 'supplier_id'=>$order_info['supplier_id'],
- 'order_money'=>$order_info['order_money'],
- ];
- Data::save('SupplierRelevancyOrder',$relevancy,'order_id',['order_id'=>$order_id]);
- return true;
- }
- // 仓库商品上线售卖
- public static function deoptGoodsOnline($deopt_goods)
- {
- // 仓库商品详情
- $ret = ['code'=>200,'msg'=>''];
- $deopt_info = DepotGoods::with('itemList')->where('id',$deopt_goods)->find()->toArray();
- if($deopt_info['online_id']) return $ret;
- $no_field = ['id','create_at'];
- // 商品基础信息
- $base_goods = BaseGoods::where('id',$deopt_info['goods_id'])->find()->toArray();
- $base_goods['depot_goods'] = $deopt_info['id'];
- $base_goods['base_id'] = $base_goods['id'];
- $base_goods['depot_id'] = $deopt_info['depot_id'];
- foreach ($no_field as $field){
- unset($base_goods[$field]);
- }
- $base_param = BaseGoodsParam::where('goods_id',$deopt_goods)->value('goods_param');
- Db::startTrans();
- try {
- $line_goods = StoreGoods::create($base_goods,true);// 添加到线上商城
- if($base_param) GoodsParam::create(['goods_id'=>$line_goods->id,'goods_param'=>$base_param]);
- DepotGoods::where('id',$deopt_goods)->update(['online_id'=>$line_goods->id]);
- array_walk($deopt_info['item_list'],function (&$v,$k)use($deopt_info,$line_goods){
- $base_item = BaseGoodsItem::where('id',$v['spec_id'])->find()->toArray();
- $store_item = [
- 'goods_id' => $line_goods->id,
- 'goods_spec' => $v['goods_spec'],
- 'spec_title' => $base_item['spec_title'],
- 'goods_no' => $base_item['goods_no'],
- 'cover' => $base_item['cover'],
- 'stock' => $v['stock'],
- 'base_stock' => $v['base_stock'],
- 'original_price' => $base_item['original_price'],
- 'sell_price' => $base_item['sell_price'],
- 'virtual' => $base_item['virtual'],
- 'weight' => $base_item['weight'],
- 'depot_item' => $v['id'],
- ];
- StoreGoodsItem::create($store_item,true);
- });
- Db::commit();
- }catch (\Exception $e){
- $ret['code'] = 201;
- $ret['msg'] = $e->getMessage();
- Db::rollback();
- }
- return $ret;
- }
- // 供货订单入库
- public static function supplierOrderDepot($order_id,$depot_id,$admin_user){
- $supplier_order = SupplierOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- $check_depot = DepotGoods::where(['goods_id'=>$supplier_order['goods_id'],'depot_id'=>$depot_id])->find();
- $ret = ['code'=>200,'msg'=>'入库成功'];
- if($supplier_order['depot_user']) return ['code'=>201,'msg'=>'商品已入库'];
- if(!$check_depot) return ['code'=>201,'msg'=>'商品没有添加到该仓库'];
- //var_dump($supplier_order,$check_depot);die();
- // 商品入库
- Db::startTrans();
- try {
- list($depot_info,$stock_info) = [[],[]];
- foreach ($supplier_order['order_item'] as $iv) {
- if($iv['num'] == 0) continue;
- //仓库规格添加【仓库】
- DepotGoodsItem::where(['depot_id'=>$depot_id,'goods_id'=>$check_depot->id,'spec_id'=>$iv['spec_id']])->update(['stock'=>Db::raw('stock +'.$iv['num']),'base_stock'=>Db::raw('base_stock +'.$iv['num'])]);
- //规格库存添加【上商城商品】
- if($check_depot->online_id){
- $depot_item = DepotGoodsItem::where(['goods_id'=>$check_depot->id,'depot_id'=>$depot_id,'spec_id'=>$iv['spec_id']])->value('id');
- StoreGoodsItem::where(['goods_id'=>$check_depot->online_id,'depot_item'=>$depot_item])->update(['stock'=>Db::raw('stock +'.$iv['num']),'base_stock'=>Db::raw('base_stock +'.$iv['num'])]);
- // 商品库存【商城】
- array_push($stock_info, [
- 'goods_id' => $check_depot->online_id,
- 'goods_spec' => $iv['goods_spec'],
- 'number_stock' => $iv['num'],
- ]);
- }
- // 仓库入库记录
- array_push($depot_info, [
- 'depot_id' => $depot_id,
- 'order_id' => $order_id,
- 'goods_id' => $supplier_order['goods_id'],
- 'spec_id' => $iv['spec_id'],
- 'goods_spec' => $iv['goods_spec'],
- 'num' => $iv['num'],
- 'remark' => '商品入库',
- 'type' => 1,
- ]);
- }
- // 线上商城总库存【不一定用,先更新】
- if($check_depot->online_id) {
- $total_num = array_sum(array_column($supplier_order['order_item'],'num'));
- StoreGoods::where(['id'=>$check_depot->online_id])->update(['stock'=>Db::raw('stock +'.$total_num),'base_stock'=>Db::raw('base_stock +'.$total_num)]);
- }
- // 修改入库订单
- SupplierOrder::where('id',$order_id)->update(['depot_user'=>$admin_user,'depot_status'=>1,'depot_id'=>$depot_id]);
- // 库存记录
- if(!empty($stock_info)) Db::name('GoodsStock')->insertAll($stock_info);
- if(!empty($depot_info)) Db::name('DepotGoodsInfo')->insertAll($depot_info);
- Db::commit();
- }catch (\Exception $e) {
- $ret['code'] = 202;
- $ret['msg'] = $e->getMessage();
- Db::rollback();
- }
- return $ret;
- }
- // 供货订单申请退款
- public static function supplierOrderApplyRefund($order_id,$reason)
- {
- $ret = ['code'=>200,'msg'=>'ok'];
- $order_info = SupplierOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- if(in_array($order_info['refund_status'],[1,2])) return ['code'=>201,'msg'=>'订单已申请退款'];
- Db::startTrans();
- try {
- $refund_info = [
- 'order_id' => $order_id,
- 'reason' => $reason,
- 'order_num' => $order_info['order_num'],
- 'supplier_id' => $order_info['supplier_id'],
- 'order_money' => $order_info['order_money'],
- 'goods_id' => $order_info['goods_id'],
- 'sh_status' => 0,
- ];
- Data::save('SupplierOrderRefund',$refund_info,'order_id',['order_id'=>$order_id]);// 退款记录
- SupplierOrder::where('id',$order_id)->update(['refund_status'=>1]);//关联订单
- SupplierRelevancyOrder::where(['order_id'=>$order_id])->update(['refund_status'=>1]);// 供货订单
- Db::commit();
- }catch (\Exception $e){
- $ret['code'] = 202;
- $ret['msg'] = $e->getMessage();
- Db::rollback();
- }
- return $ret;
- }
- // 供货订单退款申请审核
- public static function auditSupplierRefund($order_id,$sh_status,$remark)
- {
- $order_info = SupplierOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- if(in_array($order_info['refund_status'],[2])) return ['code'=>201,'msg'=>'订单已退款,请勿重新审核'];
- Db::startTrans();
- try {
- SupplierOrder::where('id',$order_id)->update(['refund_status'=>$sh_status + 1]);// 订单退款状态
- SupplierRelevancyOrder::where('order_id',$order_id)->update(['refund_status'=>$sh_status + 1]);//关联订单退款状态
- SupplierOrderRefund::where('order_id',$order_id)->update(['sh_status'=>$sh_status,'remark'=>$remark,'sh_time'=>date('Y-m-d H:i:s')]);// 退款记录
- Db::commit();
- }catch (\Exception $e){
- self::$ret['code'] = 201;
- self::$ret['msg'] = $e->getMessage();
- Db::rollback();
- }
- return self::$ret;
- }
- // 供货订单关联商品出库【已入库订单需要出库记录】
- public static function depotRefund($order_id)
- {
- $order_info = SupplierOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- if(in_array($order_info['refund_status'],[4])) return ['code'=>201,'msg'=>'已出库,请勿重新出库'];
- Db::startTrans();
- try {
- list($depot_info,$stock_info) = [[],[]];
- $check_depot = DepotGoods::where(['goods_id'=>$order_info['goods_id'],'depot_id'=>$order_info['depot_id']])->find();
- SupplierOrder::where('id',$order_id)->update(['refund_status'=>4]);// 订单退款状态
- SupplierOrderRefund::where('order_id',$order_id)->update(['sh_status'=>3]);// 退款记录
- foreach ($order_info['order_item'] as $iv) {
- if($iv['num'] == 0) continue;
- //仓库规格扣减
- DepotGoodsItem::where(['depot_id'=>$order_info['depot_id'],'goods_id'=>$check_depot->id,'spec_id'=>$iv['spec_id']])->update(['stock'=>Db::raw('stock -'.$iv['num']),'base_stock'=>Db::raw('base_stock -'.$iv['num'])]);
- //规格库存添加【上商城商品】
- if($check_depot->online_id){
- $depot_item = DepotGoodsItem::where(['goods_id'=>$check_depot->id,'depot_id'=>$order_info['depot_id'],'spec_id'=>$iv['spec_id']])->value('id');
- StoreGoodsItem::where(['goods_id'=>$check_depot->online_id,'depot_item'=>$depot_item])->update(['stock'=>Db::raw('stock +'.$iv['num']),'base_stock'=>Db::raw('base_stock +'.$iv['num'])]);
- // 商品库存【商城】
- array_push($stock_info, [
- 'goods_id' => $check_depot->online_id,
- 'goods_spec' => $iv['goods_spec'],
- 'number_stock' => -$iv['num'],
- ]);
- }
- // 仓库入库记录
- array_push($depot_info, [
- 'depot_id' => $order_info['depot_id'],
- 'order_id' => $order_id,
- 'goods_id' => $order_info['goods_id'],
- 'spec_id' => $iv['spec_id'],
- 'goods_spec' => $iv['goods_spec'],
- 'num' => -$iv['num'],
- 'remark' => '供货订单退款,商品入库',
- 'type' => 2,
- ]);
- }
- // 线上商城总库存【不一定用,先更新】
- if($check_depot->online_id) {
- $total_num = array_sum(array_column($order_info['order_item'],'num'));
- StoreGoods::where(['id'=>$check_depot->online_id])->update(['stock'=>Db::raw('stock +'.$total_num),'base_stock'=>Db::raw('base_stock +'.$total_num)]);
- }
- if(!empty($stock_info)) Db::name('GoodsStock')->insertAll($stock_info);
- if(!empty($depot_info)) Db::name('DepotGoodsInfo')->insertAll($depot_info);
- Db::commit();
- }catch (\Exception $e){
- self::$ret['code'] = 201;
- self::$ret['msg'] = $e->getMessage();
- Db::rollback();
- }
- return self::$ret;
- }
- // 仓库出库【线上商城订单】
- public static function depotOrderOutStock($order_id,$out_num,$remark)
- {
- $depot_order = DepotOrder::with('itemList')->where('id',$order_id)->find()->toArray();
- if($depot_order['out_num']) return ['code'=>201,'msg'=>'已出库'];
- Db::startTrans();
- try {
- list($depot_info) = [[]];
- DepotOrder::where(['id'=>$order_id])->update(['out_num'=>$out_num,'remark'=>$remark]);
- DepotOrderItem::where(['depot_order'=>$order_id])->update(['out_num'=>$out_num]);
- foreach ($depot_order['item_list'] as $item) {
- DepotGoodsItem::where('id',$item['spec_id'])->update(['stock'=>Db::raw('stock -'.$item['num'])]);
- $base_goods = BaseGoods::where('id',$item['goods_id'])->value('goods_id');
- $base_item = BaseGoodsItem::where('id',$item['spec_id'])->value('spec_id');
- array_push($depot_info, [
- 'depot_id' => $depot_order['depot_id'],
- 'order_id' => $depot_order['order_id'],
- 'goods_id' => $base_goods,
- 'spec_id' => $base_item,
- 'goods_spec' => $item['goods_spec'],
- 'num' => $item['num'],
- 'remark' => '商城订单出库',
- 'type' => 2,
- ]);
- }
- if(!empty($depot_info)) Db::name('DepotGoodsInfo')->insertAll($depot_info);
- Db::commit();
- }catch (\Exception $e){
- self::$ret['code'] = 201;
- self::$ret['msg'] = $e->getMessage();
- Db::rollback();
- }
- return self::$ret;
- }
- // 线下订单退款申请
- public static function offlineOrderRefund($order_id,$reason)
- {
- $ret = ['code'=>200,'msg'=>'ok'];
- $order_info = OfflineOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- if(in_array($order_info['refund_status'],[1,2])) return ['code'=>201,'msg'=>'订单已申请退款'];
- Db::startTrans();
- try {
- $refund_info = [
- 'order_id' => $order_id,
- 'reason' => $reason,
- 'order_num' => $order_info['order_num'],
- 'order_money' => $order_info['order_money'],
- 'goods_id' => $order_info['goods_id'],
- 'sh_status' => 0,
- ];
- Data::save('OfflineOrderRefund',$refund_info,'order_id',['order_id'=>$order_id]);// 退款记录
- OfflineOrder::where('id',$order_id)->update(['refund_status'=>1]);//关联订单
- Db::commit();
- }catch (\Exception $e){
- $ret['code'] = 202;
- $ret['msg'] = $e->getMessage();
- Db::rollback();
- }
- return $ret;
- }
- // 线下订单退款申请审核
- public static function auditOfflineRefund($order_id,$sh_status,$remark)
- {
- $order_info = OfflineOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
- if(in_array($order_info['refund_status'],[2])) return ['code'=>201,'msg'=>'订单已退款,请勿重新审核'];
- Db::startTrans();
- try {
- OfflineOrder::where('id',$order_id)->update(['refund_status'=>$sh_status + 1]);// 订单退款状态
- OfflineOrderRefund::where('order_id',$order_id)->update(['sh_status'=>$sh_status,'remark'=>$remark,'sh_time'=>date('Y-m-d H:i:s')]);// 退款记录
- Db::commit();
- }catch (\Exception $e){
- self::$ret['code'] = 201;
- self::$ret['msg'] = $e->getMessage();
- Db::rollback();
- }
- return self::$ret;
- }
- }
|