ShopPurchase.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace app\admin\controller;
  3. use app\data\model\DataMerchants;
  4. use app\data\model\ShopGoodsItem;
  5. use app\data\model\ShopPurchase as Model;
  6. use app\data\model\ShopPurchaseOffer;
  7. use app\data\model\ShopPurchaseOffer as Offer;
  8. use app\data\model\DataUser;
  9. use Carbon\Carbon;
  10. use think\admin\Controller;
  11. use app\data\model\SystemUser;
  12. use think\admin\service\AdminService;
  13. use think\db\Query;
  14. use think\db\Where;
  15. use think\facade\Db;
  16. use think\facade\Validate;
  17. use think\Request;
  18. /**
  19. * 采购订单管理
  20. * Class Xw
  21. * @package app\admin\controller\ShipYard
  22. * http://www.zjnk.com/fileserver/News/04b7854f-bb0a-458b-b359-ce948acfea09.JPG
  23. */
  24. class ShopPurchase extends Controller
  25. {
  26. /**
  27. * 采购订单列表
  28. * @auth true
  29. * @menu true
  30. */
  31. public function index(Request $request){
  32. if(AdminService::getUserId()==10000){
  33. $admin_id = [];
  34. }else{
  35. $admin_id['admin_id']=AdminService::getUserId();
  36. }
  37. $this->title='采购订单管理';
  38. $this->assign('status',Model::getStatus());
  39. $this->assign('admin_id',$admin_id);
  40. $result = Model::mQuery()
  41. ->equal('status')
  42. ->with(['merchant'])
  43. ->when($request['name'],function (Query $query)use ($request){
  44. $query->hasWhere('merchant',function (Query $query)use ($request){
  45. $query->whereLike('name',"%{$request['name']}%");
  46. });
  47. })->where($admin_id)
  48. ->where('status','in','1,2,3')
  49. // ->field("sum(amount) as kkk")
  50. ->dateBetween('create_time')
  51. ->order('create_time','desc');
  52. $this->amount = $result->db()->sum('amount');
  53. $result->page();
  54. }
  55. protected function _index_page_filter(&$data)
  56. {
  57. // dump($data);die;
  58. }
  59. /**
  60. * 详情
  61. * @auth true
  62. * @menu true
  63. */
  64. public function detail(Request $request,Model $purchase){
  65. DataUser::$copyRealName=true;
  66. $row = $purchase
  67. ->with(['merchant','items','items.offers','items.offers.user'])
  68. ->where('id',$request['id'])->findOrFail();
  69. if (!$row['merchant']){
  70. $array = ['company'=>'平台发布'];
  71. $row['merchant'] = $array;
  72. }
  73. $this->assign('row',$row);
  74. $this->fetch('form');
  75. }
  76. /**
  77. * 编辑
  78. * @auth true
  79. * @menu true
  80. */
  81. public function edit(Request $request,Model $purchase,\think\Validate $validate){
  82. if ($this->request->isGet()) {
  83. DataUser::$copyRealName=true;
  84. $row = $purchase
  85. ->with(['merchant','items','items.offers','items.offers.user'])
  86. ->where('id',$request['id'])->findOrFail();
  87. if (!$row['merchant']){
  88. $array = ['company'=>'平台发布'];
  89. $row['merchant'] = $array;
  90. }
  91. if($row['images'] == '' || $row['images'] == null){
  92. $row['images'] = [];
  93. }
  94. $this->assign('row',$row);
  95. $this->fetch('edit');
  96. }elseif ($this->request->isPost()){
  97. $merchant=DataMerchants::getByAdmin(AdminService::getUserId());
  98. if(!$merchant){
  99. $this->error('您不是商家无法操作');
  100. }
  101. $data=$request->post();
  102. $item=$data['item']??[];
  103. foreach ($item as $i){
  104. $make=$validate->check($i,[
  105. 'name'=>['require',],
  106. 'spec'=>['require',],
  107. 'number'=>['require','integer'],
  108. 'standard'=>['require',],
  109. 'price'=>['require','float'],
  110. ]);
  111. if(!$make){
  112. $this->error('请填写正确的商品信息');
  113. }
  114. }
  115. // print_r($data);
  116. // exit();
  117. app()->db->startTrans();
  118. unset($data['item']);
  119. $data['admin_id']=AdminService::getUserId();
  120. $data['longitude']=$merchant['longitude']??null;
  121. $data['latitude']=$merchant['latitude']??null;
  122. $data['amount']=min(array_column($item,'price'));
  123. $data['type'] =1;
  124. if(isset($data['offer_id']) && !$data['offer_id']){
  125. unset($data['offer_id']);
  126. }
  127. if(!isset($data['images'])){
  128. $data['images'] = null;
  129. }
  130. $order=Model::update($data);
  131. // $order->items()->saveAll($item);
  132. app()->db->commit();
  133. $item_ids=[];
  134. foreach ($item as &$v){
  135. $item_data = [
  136. 'purchase_id'=>$data['id'],
  137. 'name'=>$v['name'],
  138. 'spec'=>$v['spec'],
  139. 'number'=>$v['number'],
  140. 'standard'=>$v['standard'],
  141. 'price'=>$v['price'],
  142. ];
  143. if(!empty($v['id'])){
  144. $itemModel=$order->items()->where('id',$v['id'])->find();
  145. if(!$itemModel){
  146. $this->error('商品不存在');
  147. }
  148. $itemModel->save($item_data);
  149. }else{
  150. $itemModel=$order->items()->save($item_data);
  151. }
  152. $item_ids[]=$itemModel['id'];
  153. }
  154. $order = $order->items()->whereNotIn('id',$item_ids)->select()->each(function ($d){$d->delete();});
  155. ShopPurchaseOffer::where('order_id',$data['id'])
  156. ->whereNotIn('item_id',$item_ids)
  157. ->select()->each(function ($m){$m->delete();});
  158. $this->success('保存成功');
  159. }
  160. }
  161. /**
  162. * 取消订单
  163. * @auth true
  164. * @menu true
  165. */
  166. public function cancellation(Request $request,Model $purchase){
  167. $purchase_=$purchase->where('id',$request['id'])->findOrFail();
  168. $purchase_['status']=0;
  169. $purchase_->save();
  170. $this->success('取消成功');
  171. }
  172. /**
  173. * 匹配订单
  174. * @auth true
  175. * @menu true
  176. */
  177. public function match(Request $request,Model $purchase){
  178. $admin=AdminService::getUserId();
  179. $tab = 1;
  180. $id = $request['id'];
  181. $item_id = $request['item_id'];
  182. Db::startTrans();
  183. // $admin['id'] = '10010';
  184. $model= \app\data\model\ShopPurchase::getItem($tab,$admin)->lock(true)
  185. ->where(function ($query) use ($tab,$item_id){
  186. // if ($tab==1){
  187. // $query->where('purchase_id',$id);
  188. // }else{
  189. $query->where('id',$item_id);
  190. // }
  191. })
  192. ->findOrEmpty();
  193. $model_id = $model['id'];
  194. //dump($model->toArray());die;
  195. if($model['is_matched']){
  196. Db::rollback();
  197. $this->error('该订单已匹配');
  198. }
  199. // $hasMatch=ShopPurchase::getItem($data['tab'])->where('offer_id',$data['offer_id'])->value('id');
  200. $hasMatch=\app\data\model\ShopPurchase::getItem($tab)->where('id',$id)->value('offer_id');
  201. if($hasMatch){
  202. Db::rollback();
  203. $this->error('该接单已被其他订单匹配');
  204. }
  205. $offer=\app\data\model\ShopPurchase::getOffer($tab)->lock(true)
  206. // ->where('user_id',$data['offer_id'])
  207. ->when($tab,function ($querys) use ($tab,$model_id){
  208. switch ($tab){
  209. case 1:
  210. $querys->where('item_id',$model_id);
  211. break;
  212. case 2:
  213. $querys->where('production_id',$model_id);
  214. break;
  215. case 3:
  216. $querys->where('corrdination_id',$model_id);
  217. break;
  218. case 4:
  219. $querys->where('ocea_id',$model_id);
  220. break;
  221. }
  222. })
  223. ->findOrEmpty();
  224. if($tab==1){
  225. if ($offer->item_id != $model['id']) {
  226. Db::rollback();
  227. $this->error('该接单不属于此订单');
  228. }
  229. }else {
  230. if ($offer->order_id != $model['id']) {
  231. Db::rollback();
  232. $this->error('该接单不属于此订单');
  233. }
  234. }
  235. $model['offer_id']=$request['offer_id'];
  236. if($tab==1){
  237. $main=$model->main;
  238. $main['status']=3;
  239. $main->save();
  240. }else{
  241. $model['status']=3;
  242. }
  243. // print_r($model);
  244. $model->save();
  245. Db::commit();
  246. $url = setword($tab+1,$request['item_id']);
  247. // print_r($url);
  248. // exit();
  249. \app\data\model\ShopPurchase::getItem($tab)->where('id',$model['id'])->update(['down_word'=>$url]);
  250. $this->success('匹配成功');
  251. }
  252. /**
  253. * 查看报价历史
  254. * @auth true
  255. * @menu true
  256. */
  257. public function offer_history(Request $request,Offer $offer){
  258. $offer=$offer->where('id',$request['id'])->findOrFail();
  259. return $offer->amounts()->select();
  260. }
  261. /**
  262. * 发布
  263. * @auth true
  264. * @menu true
  265. */
  266. public function add(Request $request,\think\Validate $validate){
  267. $this->title='发布订单';
  268. $this->assign('vo',null);
  269. if($request->isGet()) {
  270. $this->fetch();
  271. }else{
  272. $merchant=DataMerchants::getByAdmin(AdminService::getUserId());
  273. if(!$merchant){
  274. $this->error('您不是商家无法发布');
  275. }
  276. $data=$request->post();
  277. $item=$data['item']??[];
  278. foreach ($item as $i){
  279. $make=$validate->check($i,[
  280. 'name'=>['require',],
  281. 'spec'=>['require',],
  282. 'number'=>['require','integer'],
  283. 'standard'=>['require',],
  284. 'price'=>['require','float'],
  285. ]);
  286. if(!$make){
  287. $this->error('请填写正确的商品信息');
  288. }
  289. }
  290. app()->db->startTrans();
  291. unset($data['item']);
  292. $data['admin_id']=AdminService::getUserId();
  293. $data['longitude']=$merchant['longitude']??null;
  294. $data['latitude']=$merchant['latitude']??null;
  295. $data['amount']=min(array_column($item,'price'));
  296. $data['type'] =1;
  297. $order=Model::create($data);
  298. $order->items()->saveAll($item);
  299. app()->db->commit();
  300. $this->success('保存成功');
  301. }
  302. }
  303. }