Fill.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\bbc\controller;
  4. use app\admin\model\GoodNake;use app\admin\model\GoodPlatform;use app\admin\model\GoodZixun;use app\admin\model\OrderAddr;use app\admin\model\OrderOut;use app\admin\model\OrderOutChild;use app\admin\model\Sale;use app\bbc\model\BbcFill;
  5. use app\model\Headquarters;use app\model\Supplier;use app\report\model\ConsultBids;use think\App;
  6. use think\Exception;use think\facade\Validate;use think\Request;
  7. class Fill extends Base
  8. {
  9. protected $model=null;
  10. public function __construct(App $app) {
  11. parent::__construct($app);
  12. $this->model=new BbcFill();
  13. }
  14. /**
  15. * 显示资源列表
  16. *
  17. * @return \think\Response
  18. */
  19. public function list()
  20. {
  21. $param=$this->request->param(["cat_id"=>"","start"=>"",'end'=>'',"fillCode"=>"","status"=>"","good_name"=>"",
  22. "spuCode"=>"","companyNo"=>"","customerCode"=>"","is_compliance"=>"","page"=>1,"size"=>15],"post","trim");
  23. $where=[];
  24. $param['companyNo']==""??$where[]=["companyNo","like","%{$param['companyNo']}%"];
  25. $param['customerCode']==""??$where[]=["customerCode","like","%{$param['customerCode']}%"];
  26. $param['fillCode']==""??$where[]=["fillCode","like","%{$param['fillCode']}%"];
  27. $param['good_name']==""??$where[]=["good_name","like","%{$param['good_name']}%"];
  28. $param['spuCode']==""??$where[]=["spuCode","like","%{$param['spuCode']}%"];
  29. $param['is_compliance']===""??$where[]=["is_compliance","=",$param['is_compliance']];
  30. $param['cat_id']===""??$where[]=["cat_id","=",$param['cat_id']];
  31. $param['status']===""??$where[]=["status","=",$param['status']];
  32. $list=$this->model->with(["unit","cat","brand"])
  33. ->where($where)
  34. ->order("id desc")
  35. ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
  36. $this->success("获取成功",["list"=>$list->items(),"count"=>$list->items()]);
  37. }
  38. /**
  39. * 显示创建资源表单页.
  40. *
  41. * @return \think\Response
  42. */
  43. public function create()
  44. {
  45. $param = $this->request->param([
  46. "companyNo"=>"",
  47. "customerCode"=>"",
  48. "platform_id"=>"",
  49. "good_num"=>"",
  50. "good_price"=>"",
  51. // "spuCode"=>"",
  52. "skuCode"=>"",
  53. "manager"=>"",
  54. "managerid"=>"",
  55. "fill_remark"=>""
  56. ],"post","trim");
  57. $valid=Validate::rule([
  58. "companyNo|业务公司"=>"require|max255",
  59. "customerCode|客户公司"=>'require|max255',
  60. "platform_id|平台"=>'require|number',
  61. "good_num|商品数量"=>'require|number|gt:0',
  62. "good_price|商品单价"=>'require|float',
  63. "skuCode|商品编号"=>'require|max255',
  64. // "spuCode|商品上线编号"=>'require|max255',
  65. 'manager|业务经理'=>'require',
  66. 'managerid|业务经理id'=>'require',
  67. 'fill_remark|订单备注'=>'require'
  68. ]);
  69. if($valid->check($param)==false) $this->error($valid->getError());
  70. $head=new Headquarters();
  71. $company = $head->where(["code"=>[$param['companyNo'],$param['customerCode']]])->column("name","code");
  72. if(empty($company))$this->error("未找到相关业务公司/客户名称信息");
  73. $goodinfo = (new GoodPlatform())->alias("a")->leftJoin("good b","a.spuCode=b.spuCode")
  74. ->where("skuCode",$param['skuCode'])
  75. ->field("skuCode,b.spuCode,platform_code platform_id,is_compliance,compliance_remark,plat_code,good_name,
  76. cat_id,exam_status,brand_id,good_unit unit_id,supplierNo,supplierName,if(is_stock=1,1,2) fill_type")
  77. ->findOrEmpty();
  78. if($goodinfo->isEmpty()) $this->error("商品信息未找到");
  79. if($goodinfo->exam_status!=3) $this->error("商品未上线");
  80. $origin_price=0;
  81. if($goodinfo->fill_type==2){
  82. $origin =(new GoodNake())->where([['spuCode', '=', $goodinfo->spuCode], ['min_num', '<=', $param['good_num']],
  83. ['is_del', '=', 0]])->order('min_num desc')->findOrEmpty();
  84. if ($origin->isEmpty()) $this->error('未找到商品相关阶梯成本价格');
  85. $origin_price = $origin->nake_total;
  86. }
  87. $data=[
  88. "fillCode"=>makeNo("BF"),
  89. "total_price"=>bcmul($param['good_num'],$param['good_price'],4),
  90. "origin_price"=>$origin_price,
  91. "apply_id"=>$this->uid,
  92. "apply_name"=>$this->uname,
  93. "status"=>2,
  94. "companyName"=>$company[$param['companyNo']]??"",
  95. "customerName"=>$company[$param['customerCode']]??"",
  96. ];
  97. $create= array_merge($data,$param,$goodinfo->toArray());
  98. BbcFill::create($create);
  99. $this->success("业务报备信息创建成功");
  100. }
  101. /**
  102. * 是否合规审核
  103. *
  104. * @param \think\Request $request
  105. * @return \think\Response
  106. */
  107. public function compliance()
  108. {
  109. $param=$this->request->param(["id"=>"","is_compliance"=>"","plat_code"=>"","compliance_remark"=>""],"post","trim");
  110. $valid =Validate::rule([
  111. "id|主键id"=>"require|number|gt:0",
  112. "is_compliance|是否合规"=>"require|number|in:1,2",
  113. "compliance_remark|合规备注"=>"requireIf:is_compliance,2",
  114. "plat_code|BBC平台商品编号"=>"requireIf:is_compliance,1"
  115. ]);
  116. if($valid->check($param)==false) $this->error($valid->getError());
  117. $info= $this->model->findOrEmpty($param['id']);
  118. if($info->isEmpty())$this->error("未找到报备单数据");
  119. $info->is_compliance = $param['is_compliance'];
  120. $info->compliance_remark = $param['compliance_remark'];
  121. $info->plat_code = $param['plat_code'];
  122. // $info->status=2;
  123. $up =$info->save();
  124. $up?$this->success("报备单更新成功"):$this->error("报备单更新失败");
  125. }
  126. /**
  127. 报备转单
  128. */
  129. public function transfer()
  130. {
  131. $param = $this->request->only([
  132. 'id'=>"",
  133. 'addr_list'=>[],
  134. 'customerCode'=>'',
  135. "poCode"=>""
  136. ], 'post',"trim");
  137. $val = Validate::rule([
  138. 'id|报备的主键id' => 'require|number|gt:0',
  139. 'customerCode|客户编号'=>'require|max:255',
  140. 'addr_list|收货地址' => 'require|array|max:100',
  141. 'poCode|平台订单号' => 'require|max:255',
  142. ]);
  143. if ($val->check($param)==false)$this->error( $val->getError());
  144. $val2 = Validate::rule([
  145. 'receipt_quantity|收货数量' => 'require|number|gt:0|max:999999999',
  146. 'contactor|联系人' => 'require|max:255',
  147. 'mobile|联系电话' => 'require|mobile',
  148. 'addr_code|收货省市区编码' => 'array|length:3',
  149. 'addr|详细地址' => 'require|max:255',
  150. ]);
  151. $info = $this->model->findOrEmpty($param['id']);
  152. if($info->isEmpty()) $this->error("未找到报备单数据");
  153. if($info->status==3 ||$info->status==4 )$this->error('报备单数据'.BbcFill::$status_cn[$info->status]);
  154. $head=new Headquarters();
  155. $company = $head->where(['code'=>$param['customerCode']])->findOrEmpty();
  156. if($company->isEmpty())$this->error('未找到相关客户名称信息');
  157. if($info->fill_type!=3) $good =(new \app\admin\model\Good())->where(["spuCode",$info->spuCode])->findOrEmpty();
  158. else $good=(new GoodZixun())->where(['spuCode',$info->spuCode])->findOrEmpty();
  159. if($good->isEmpty())$this->error('未找到商品信息');
  160. $supplier = (new Supplier())->where(["code"=>$info->supplierNo])->findOrEmpty();
  161. if($supplier->isEmpty())$this->error('未找到供应商信息');
  162. $orderCode= makeNo("QR");
  163. $orderArr=[];
  164. $transNum=0;
  165. foreach ($param["addr_list"] as $item){
  166. if ($val2->check($item)==false)$this->error( $val2->getError());
  167. $temp=['orderCode' => $orderCode,
  168. 'addr' => $item['addr'],
  169. 'addr_code' =>empty( $item['addr_code'])?'': implode(',', $item['addr_code']),
  170. 'contactor' => $item['contactor'],
  171. 'mobile' => $item['mobile'],
  172. 'customer_code' => $param['customerCode'],
  173. 'post_fee' => 0,
  174. 'receipt_quantity' => $item['receipt_quantity'],
  175. 'is_del' => 0,
  176. 'addtime' => date("Y-m-d H:i:s"),
  177. 'updatetime' => date('Y-m-d H:i:s')
  178. ];
  179. $transNum+=$item['receipt_quantity'];
  180. $orderArr[]=$temp;
  181. }
  182. if($info->fill_type==1 && $good->usable_stock< $transNum) $this->error('商品可用库存不足');
  183. // if($info->fill_type==2){
  184. // $origin =(new GoodNake())->where([['spuCode', '=', $good->spuCode], ['min_num', '<=', $info->good_num],
  185. // ['is_del', '=', 0]])->order('min_num desc')->findOrEmpty();
  186. // if ($origin->isEmpty()) $this->error('未找到商品相关阶梯成本价格');
  187. // $origin_price = $origin->nake_total;
  188. // }
  189. // if($info->fill_type==3){
  190. // $origin =(new \app\admin\model\ConsultBids())->where([['spuCode', '=', $good->spuCode] ,['is_del', '=',
  191. // 0]])->findOrEmpty();
  192. // if ($origin->isEmpty()) $this->error('未找到商品相关成本价格');
  193. // $origin_price = $origin->total_fee;
  194. // }
  195. $orderInfo=[
  196. 'orderCode' => $orderCode,
  197. 'apply_id' => $this->uid,
  198. 'apply_name' => $this->uname,
  199. 'order_type' => $info->fill_type,//报备商品
  200. 'order_source' => 11,//业务报备转单
  201. 'platform_id' => $info->platform_id,
  202. 'good_code' => $info->spuCode,
  203. 'skuCode' => $info->skuCode,
  204. 'cat_id' => $info->cat_id,
  205. 'good_name' => $info->good_name,
  206. 'good_num' => $transNum,
  207. 'good_type' => 1,
  208. 'origin_price' => $info->origin_price,
  209. 'sale_price' => round($info->good_price,2),
  210. 'total_price' => round(bcmul($info->good_price, strval($transNum), 4), 2),
  211. 'post_fee' => 0,
  212. 'is_diff' => $good->is_diff,
  213. 'is_activity' => 0,
  214. 'activity_code' => '',
  215. 'is_stock' => 0,
  216. 'customer_code' => $param['customerCode'],
  217. 'customerName' => $company['name'],
  218. 'supplierNo' => $info->companyNo,
  219. 'supplierName' => $info->companyName,
  220. 'supNo' =>$info->supplierNo,
  221. 'supName' => $info->supplierName,
  222. 'zxNo' => '',
  223. 'platform_order' =>$param['poCode'],
  224. 'send_num' => 0,
  225. 'wsend_num' =>$transNum,
  226. 'th_num' => 0,
  227. 'th_fee' => 0,
  228. 'send_status' => 1,
  229. 'send_type' => 1,//直接发货
  230. 'remark' => '',
  231. 'status' => 0,
  232. 'is_del' => 0,
  233. 'proof_id' => 0,
  234. 'other_orderNo' => $info->filingCode,
  235. 'workNo' => '',
  236. 'poNo' => '',
  237. 'use_order' => 0,
  238. 'good_weight' => 0,
  239. 'gold_price' => 0,
  240. 'cost_price' => $good->cost_fee,
  241. 'diff_weight' => 0,
  242. 'diff_fee' => 0,
  243. 'returnCode' => '',
  244. 'addtime' => date("Y-m-d H:i:s"),
  245. 'updatetime' =>date('Y-m-d H:i:s'),
  246. 'cgderid' => $supplier->personid,
  247. 'cgder' => $supplier->person,
  248. 'good_createrid' => $good['createrid'],
  249. 'good_creater' =>$good['createrid'],
  250. 'manager'=>$info->manager,
  251. 'managerid'=>$info->managerid
  252. ];
  253. $this->model->startTrans();
  254. try{
  255. $order=Sale::create($orderInfo);
  256. if($order->id==false) throw new \Exception("订单创建失败");
  257. $Addr=(new OrderAddr())->saveAll($orderArr);
  258. if($info->fill_type==1){
  259. $up = (new \app\admin\model\Good())->where(["spuCode"=>$info->spuCode])->lock(true)->findOrEmpty();
  260. if($up->isEmpty())throw new \Exception('未找到商品信息');
  261. if($up->usable_stock< $transNum)throw new \Exception('商品可用库存不足');
  262. $up->usable_stock= $up->usable_stock-$transNum;
  263. $up->updatetime=date('Y-m-d H:i:s');
  264. $goodUp=$up->save();
  265. if($goodUp==false)throw new \Exception('商品库存修改失败');
  266. foreach ($Addr as $item){
  267. $outCode= make("DF");
  268. $out=[
  269. 'orderCode' => $orderCode,
  270. 'outCode' => $outCode,
  271. 'apply_id' => $this->uid,
  272. 'apply_name' => $this->uname,
  273. 'addrid' => $item->id,
  274. 'post_name' => '',
  275. 'post_code' => '',
  276. 'post_fee' => 0,
  277. 'sendtime' => date('Y-m-d H:i:s'),
  278. 'send_num' => $item->receipt_quantity,
  279. 'check_num' => 0,
  280. 'error_num' => 0,
  281. 'wsm_code' => '',
  282. 'order_type' =>1,
  283. 'send_status' => 1,
  284. 'status' => 1,
  285. 'addtime' => date('Y-m-d H:i:s'),
  286. 'updatetime' => date('Y-m-d H:i:s')
  287. ];
  288. $outinfo=OrderOut::create($out);
  289. if($outinfo->isEmpty())throw new \Exception('发货单创建失败');
  290. OrderOutChild::makeChild($outCode);
  291. $event=[ 'order_type' => 'CKD',
  292. 'order_code' =>$outCode,//咨询单详情编号
  293. 'order_id' => $outinfo->id,
  294. 'order_status' => 2,
  295. 'action_process' =>1,
  296. 'action_status' => 1,
  297. 'action_uid' => $this->uid,
  298. 'action_name' => $this->uname,
  299. ];
  300. event('ProcessOrder',$event);
  301. }
  302. }
  303. $this->model->commit();
  304. if($info->fill_type!==1){
  305. $event=[ 'order_type' => 'XSQRD',
  306. 'order_code' =>$orderCode,//咨询单详情编号
  307. 'order_id' => $order->id,
  308. 'order_status' => 2,
  309. 'action_process' =>0,
  310. 'action_status' => 0,
  311. 'action_uid' => $this->uid,
  312. 'action_name' => $this->uname,
  313. ];
  314. event('ProcessOrder',$event);
  315. event("CgdCreate",$orderCode);
  316. }
  317. }catch (\Exception $exception){
  318. $this->error($exception->getMessage());
  319. $this->model->rollback();
  320. }
  321. $this->success("转单成功",["orderCode"=>$orderCode]);
  322. }
  323. /**
  324. * 显示编辑资源表单页.
  325. *
  326. * @param int $id
  327. * @return \think\Response
  328. */
  329. public function edit($id)
  330. {
  331. //
  332. }
  333. /**
  334. * 保存更新的资源
  335. *
  336. * @param \think\Request $request
  337. * @param int $id
  338. * @return \think\Response
  339. */
  340. public function update(Request $request, $id)
  341. {
  342. //
  343. }
  344. /**
  345. * 删除指定资源
  346. *
  347. * @param int $id
  348. * @return \think\Response
  349. */
  350. public function delete($id)
  351. {
  352. //
  353. }
  354. }