ShopPurchase.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. $this->assign('row',$row);
  92. $this->fetch('edit');
  93. }elseif ($this->request->isPost()){
  94. $merchant=DataMerchants::getByAdmin(AdminService::getUserId());
  95. if(!$merchant){
  96. $this->error('您不是商家无法操作');
  97. }
  98. $data=$request->post();
  99. $item=$data['item']??[];
  100. foreach ($item as $i){
  101. $make=$validate->check($i,[
  102. 'name'=>['require',],
  103. 'spec'=>['require',],
  104. 'number'=>['require','integer'],
  105. 'standard'=>['require',],
  106. 'price'=>['require','float'],
  107. ]);
  108. if(!$make){
  109. $this->error('请填写正确的商品信息');
  110. }
  111. }
  112. // print_r($data);
  113. // exit();
  114. app()->db->startTrans();
  115. unset($data['item']);
  116. $data['admin_id']=AdminService::getUserId();
  117. $data['longitude']=$merchant['longitude']??null;
  118. $data['latitude']=$merchant['latitude']??null;
  119. $data['amount']=min(array_column($item,'price'));
  120. $data['type'] =1;
  121. if(!$data['offer_id']){
  122. unset($data['offer_id']);
  123. }
  124. $order=Model::update($data);
  125. // $order->items()->saveAll($item);
  126. app()->db->commit();
  127. $item_ids=[];
  128. foreach ($item as &$v){
  129. $item_data = [
  130. 'purchase_id'=>$data['id'],
  131. 'name'=>$v['name'],
  132. 'spec'=>$v['spec'],
  133. 'number'=>$v['number'],
  134. 'standard'=>$v['standard'],
  135. 'price'=>$v['price'],
  136. ];
  137. if(!empty($v['id'])){
  138. $itemModel=$order->items()->where('id',$v['id'])->find();
  139. if(!$itemModel){
  140. $this->error('商品不存在');
  141. }
  142. $itemModel->save($item_data);
  143. }else{
  144. $itemModel=$order->items()->save($item_data);
  145. }
  146. $item_ids[]=$itemModel['id'];
  147. }
  148. $order = $order->items()->whereNotIn('id',$item_ids)->select()->each(function ($d){$d->delete();});
  149. ShopPurchaseOffer::where('order_id',$data['id'])
  150. ->whereNotIn('item_id',$item_ids)
  151. ->select()->each(function ($m){$m->delete();});
  152. $this->success('保存成功');
  153. }
  154. }
  155. /**
  156. * 取消订单
  157. * @auth true
  158. * @menu true
  159. */
  160. public function cancellation(Request $request,Model $purchase){
  161. $purchase_=$purchase->where('id',$request['id'])->findOrFail();
  162. $purchase_['status']=0;
  163. $purchase_->save();
  164. $this->success('取消成功');
  165. }
  166. /**
  167. * 匹配订单
  168. * @auth true
  169. * @menu true
  170. */
  171. public function match(Request $request,Model $purchase){
  172. $admin=AdminService::getUserId();
  173. $tab = 1;
  174. $id = $request['id'];
  175. $item_id = $request['item_id'];
  176. Db::startTrans();
  177. // $admin['id'] = '10010';
  178. $model= \app\data\model\ShopPurchase::getItem($tab,$admin)->lock(true)
  179. ->where(function ($query) use ($tab,$item_id){
  180. // if ($tab==1){
  181. // $query->where('purchase_id',$id);
  182. // }else{
  183. $query->where('id',$item_id);
  184. // }
  185. })
  186. ->findOrEmpty();
  187. $model_id = $model['id'];
  188. //dump($model->toArray());die;
  189. if($model['is_matched']){
  190. Db::rollback();
  191. $this->error('该订单已匹配');
  192. }
  193. // $hasMatch=ShopPurchase::getItem($data['tab'])->where('offer_id',$data['offer_id'])->value('id');
  194. $hasMatch=\app\data\model\ShopPurchase::getItem($tab)->where('id',$id)->value('offer_id');
  195. if($hasMatch){
  196. Db::rollback();
  197. $this->error('该接单已被其他订单匹配');
  198. }
  199. $offer=\app\data\model\ShopPurchase::getOffer($tab)->lock(true)
  200. // ->where('user_id',$data['offer_id'])
  201. ->when($tab,function ($querys) use ($tab,$model_id){
  202. switch ($tab){
  203. case 1:
  204. $querys->where('item_id',$model_id);
  205. break;
  206. case 2:
  207. $querys->where('production_id',$model_id);
  208. break;
  209. case 3:
  210. $querys->where('corrdination_id',$model_id);
  211. break;
  212. case 4:
  213. $querys->where('ocea_id',$model_id);
  214. break;
  215. }
  216. })
  217. ->findOrEmpty();
  218. if($tab==1){
  219. if ($offer->item_id != $model['id']) {
  220. Db::rollback();
  221. $this->error('该接单不属于此订单');
  222. }
  223. }else {
  224. if ($offer->order_id != $model['id']) {
  225. Db::rollback();
  226. $this->error('该接单不属于此订单');
  227. }
  228. }
  229. $model['offer_id']=$request['offer_id'];
  230. if($tab==1){
  231. $main=$model->main;
  232. $main['status']=3;
  233. $main->save();
  234. }else{
  235. $model['status']=3;
  236. }
  237. // print_r($model);
  238. $model->save();
  239. Db::commit();
  240. $url = setword($tab+1,$request['item_id']);
  241. // print_r($url);
  242. // exit();
  243. \app\data\model\ShopPurchase::getItem($tab)->where('id',$model['id'])->update(['down_word'=>$url]);
  244. $this->success('匹配成功');
  245. }
  246. /**
  247. * 查看报价历史
  248. * @auth true
  249. * @menu true
  250. */
  251. public function offer_history(Request $request,Offer $offer){
  252. $offer=$offer->where('id',$request['id'])->findOrFail();
  253. return $offer->amounts()->select();
  254. }
  255. /**
  256. * 发布
  257. * @auth true
  258. * @menu true
  259. */
  260. public function add(Request $request,\think\Validate $validate){
  261. $this->title='发布订单';
  262. $this->assign('vo',null);
  263. if($request->isGet()) {
  264. $this->fetch();
  265. }else{
  266. $merchant=DataMerchants::getByAdmin(AdminService::getUserId());
  267. if(!$merchant){
  268. $this->error('您不是商家无法发布');
  269. }
  270. $data=$request->post();
  271. $item=$data['item']??[];
  272. foreach ($item as $i){
  273. $make=$validate->check($i,[
  274. 'name'=>['require',],
  275. 'spec'=>['require',],
  276. 'number'=>['require','integer'],
  277. 'standard'=>['require',],
  278. 'price'=>['require','float'],
  279. ]);
  280. if(!$make){
  281. $this->error('请填写正确的商品信息');
  282. }
  283. }
  284. app()->db->startTrans();
  285. unset($data['item']);
  286. $data['admin_id']=AdminService::getUserId();
  287. $data['longitude']=$merchant['longitude']??null;
  288. $data['latitude']=$merchant['latitude']??null;
  289. $data['amount']=min(array_column($item,'price'));
  290. $data['type'] =1;
  291. $order=Model::create($data);
  292. $order->items()->saveAll($item);
  293. app()->db->commit();
  294. $this->success('保存成功');
  295. }
  296. }
  297. }