123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- <?php
- namespace app\admin\controller;
- use app\data\model\DataMerchants;
- use app\data\model\ShopGoodsItem;
- use app\data\model\ShopPurchase as Model;
- use app\data\model\ShopPurchaseOffer;
- use app\data\model\ShopPurchaseOffer as Offer;
- use app\data\model\DataUser;
- use Carbon\Carbon;
- use think\admin\Controller;
- use app\data\model\SystemUser;
- use think\admin\service\AdminService;
- use think\db\Query;
- use think\db\Where;
- use think\facade\Db;
- use think\facade\Validate;
- use think\Request;
- /**
- * 采购订单管理
- * Class Xw
- * @package app\admin\controller\ShipYard
- * http://www.zjnk.com/fileserver/News/04b7854f-bb0a-458b-b359-ce948acfea09.JPG
- */
- class ShopPurchase extends Controller
- {
- /**
- * 采购订单列表
- * @auth true
- * @menu true
- */
- public function index(Request $request){
- if(AdminService::getUserId()==10000){
- $admin_id = [];
- }else{
- $admin_id['admin_id']=AdminService::getUserId();
- }
- $this->title='采购订单管理';
- $this->assign('status',Model::getStatus());
- $this->assign('admin_id',$admin_id);
- $result = Model::mQuery()
- ->equal('status')
- ->with(['merchant'])
- ->when($request['name'],function (Query $query)use ($request){
- $query->hasWhere('merchant',function (Query $query)use ($request){
- $query->whereLike('name',"%{$request['name']}%");
- });
- })->where($admin_id)
- ->where('status','in','1,2,3')
- // ->field("sum(amount) as kkk")
- ->dateBetween('create_time')
- ->order('create_time','desc');
- $this->amount = $result->db()->sum('amount');
- $result->page();
- }
- protected function _index_page_filter(&$data)
- {
- // dump($data);die;
- }
- /**
- * 详情
- * @auth true
- * @menu true
- */
- public function detail(Request $request,Model $purchase){
- DataUser::$copyRealName=true;
- $row = $purchase
- ->with(['merchant','items','items.offers','items.offers.user'])
- ->where('id',$request['id'])->findOrFail();
- if (!$row['merchant']){
- $array = ['company'=>'平台发布'];
- $row['merchant'] = $array;
- }
- $this->assign('row',$row);
- $this->fetch('form');
- }
- /**
- * 编辑
- * @auth true
- * @menu true
- */
- public function edit(Request $request,Model $purchase,\think\Validate $validate){
- if ($this->request->isGet()) {
- DataUser::$copyRealName=true;
- $row = $purchase
- ->with(['merchant','items','items.offers','items.offers.user'])
- ->where('id',$request['id'])->findOrFail();
- if (!$row['merchant']){
- $array = ['company'=>'平台发布'];
- $row['merchant'] = $array;
- }
- if($row['images'] == '' || $row['images'] == null){
- $row['images'] = [];
- }
- $this->assign('row',$row);
- $this->fetch('edit');
- }elseif ($this->request->isPost()){
- $merchant=DataMerchants::getByAdmin(AdminService::getUserId());
- if(!$merchant){
- $this->error('您不是商家无法操作');
- }
- $data=$request->post();
- $item=$data['item']??[];
- foreach ($item as $i){
- $make=$validate->check($i,[
- 'name'=>['require',],
- 'spec'=>['require',],
- 'number'=>['require','integer'],
- 'standard'=>['require',],
- 'price'=>['require','float'],
- ]);
- if(!$make){
- $this->error('请填写正确的商品信息');
- }
- }
- // print_r($data);
- // exit();
- app()->db->startTrans();
- unset($data['item']);
- $data['admin_id']=AdminService::getUserId();
- $data['longitude']=$merchant['longitude']??null;
- $data['latitude']=$merchant['latitude']??null;
- $data['amount']=min(array_column($item,'price'));
- $data['type'] =1;
- if(isset($data['offer_id']) && !$data['offer_id']){
- unset($data['offer_id']);
- }
- if(!isset($data['images'])){
- $data['images'] = null;
- }
- $order=Model::update($data);
- // $order->items()->saveAll($item);
- app()->db->commit();
- $item_ids=[];
- foreach ($item as &$v){
- $item_data = [
- 'purchase_id'=>$data['id'],
- 'name'=>$v['name'],
- 'spec'=>$v['spec'],
- 'number'=>$v['number'],
- 'standard'=>$v['standard'],
- 'price'=>$v['price'],
- ];
- if(!empty($v['id'])){
- $itemModel=$order->items()->where('id',$v['id'])->find();
- if(!$itemModel){
- $this->error('商品不存在');
- }
- $itemModel->save($item_data);
- }else{
- $itemModel=$order->items()->save($item_data);
- }
- $item_ids[]=$itemModel['id'];
- }
- $order = $order->items()->whereNotIn('id',$item_ids)->select()->each(function ($d){$d->delete();});
- ShopPurchaseOffer::where('order_id',$data['id'])
- ->whereNotIn('item_id',$item_ids)
- ->select()->each(function ($m){$m->delete();});
- $this->success('保存成功');
- }
- }
- /**
- * 取消订单
- * @auth true
- * @menu true
- */
- public function cancellation(Request $request,Model $purchase){
- $purchase_=$purchase->where('id',$request['id'])->findOrFail();
- $purchase_['status']=0;
- $purchase_->save();
- $this->success('取消成功');
- }
- /**
- * 匹配订单
- * @auth true
- * @menu true
- */
- public function match(Request $request,Model $purchase){
- $admin=AdminService::getUserId();
- $tab = 1;
- $id = $request['id'];
- $item_id = $request['item_id'];
- Db::startTrans();
- // $admin['id'] = '10010';
- $model= \app\data\model\ShopPurchase::getItem($tab,$admin)->lock(true)
- ->where(function ($query) use ($tab,$item_id){
- // if ($tab==1){
- // $query->where('purchase_id',$id);
- // }else{
- $query->where('id',$item_id);
- // }
- })
- ->findOrEmpty();
- $model_id = $model['id'];
- //dump($model->toArray());die;
- if($model['is_matched']){
- Db::rollback();
- $this->error('该订单已匹配');
- }
- // $hasMatch=ShopPurchase::getItem($data['tab'])->where('offer_id',$data['offer_id'])->value('id');
- $hasMatch=\app\data\model\ShopPurchase::getItem($tab)->where('id',$id)->value('offer_id');
- if($hasMatch){
- Db::rollback();
- $this->error('该接单已被其他订单匹配');
- }
- $offer=\app\data\model\ShopPurchase::getOffer($tab)->lock(true)
- // ->where('user_id',$data['offer_id'])
- ->when($tab,function ($querys) use ($tab,$model_id){
- switch ($tab){
- case 1:
- $querys->where('item_id',$model_id);
- break;
- case 2:
- $querys->where('production_id',$model_id);
- break;
- case 3:
- $querys->where('corrdination_id',$model_id);
- break;
- case 4:
- $querys->where('ocea_id',$model_id);
- break;
- }
- })
- ->findOrEmpty();
- if($tab==1){
- if ($offer->item_id != $model['id']) {
- Db::rollback();
- $this->error('该接单不属于此订单');
- }
- }else {
- if ($offer->order_id != $model['id']) {
- Db::rollback();
- $this->error('该接单不属于此订单');
- }
- }
- $model['offer_id']=$request['offer_id'];
- if($tab==1){
- $main=$model->main;
- $main['status']=3;
- $main->save();
- }else{
- $model['status']=3;
- }
- // print_r($model);
- $model->save();
- Db::commit();
- $url = setword($tab+1,$request['item_id']);
- // print_r($url);
- // exit();
- \app\data\model\ShopPurchase::getItem($tab)->where('id',$model['id'])->update(['down_word'=>$url]);
- $this->success('匹配成功');
- }
- /**
- * 查看报价历史
- * @auth true
- * @menu true
- */
- public function offer_history(Request $request,Offer $offer){
- $offer=$offer->where('id',$request['id'])->findOrFail();
- return $offer->amounts()->select();
- }
- /**
- * 发布
- * @auth true
- * @menu true
- */
- public function add(Request $request,\think\Validate $validate){
- $this->title='发布订单';
- $this->assign('vo',null);
- if($request->isGet()) {
- $this->fetch();
- }else{
- $merchant=DataMerchants::getByAdmin(AdminService::getUserId());
- if(!$merchant){
- $this->error('您不是商家无法发布');
- }
- $data=$request->post();
- $item=$data['item']??[];
- foreach ($item as $i){
- $make=$validate->check($i,[
- 'name'=>['require',],
- 'spec'=>['require',],
- 'number'=>['require','integer'],
- 'standard'=>['require',],
- 'price'=>['require','float'],
- ]);
- if(!$make){
- $this->error('请填写正确的商品信息');
- }
- }
- app()->db->startTrans();
- unset($data['item']);
- $data['admin_id']=AdminService::getUserId();
- $data['longitude']=$merchant['longitude']??null;
- $data['latitude']=$merchant['latitude']??null;
- $data['amount']=min(array_column($item,'price'));
- $data['type'] =1;
- $order=Model::create($data);
- $order->items()->saveAll($item);
- app()->db->commit();
- $this->success('保存成功');
- }
- }
- }
|