FinancialProducts.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace app\cxinv\controller;
  3. use app\cxinv\model\ProductCheck;
  4. use app\cxinv\model\ProductsCombind;
  5. use think\App;
  6. class FinancialProducts extends Base{
  7. public function __construct(App $app) {
  8. $this->noLogin=["*"];
  9. parent::__construct($app);
  10. $this->model=new \app\cxinv\model\FinancialProducts();
  11. }
  12. public function List(){
  13. $param = $this->request->param(["skuCode"=>"","good_type"=>"","buyer_code"=>"","buyer_name"=>"","start"=>"",
  14. "end"=>"","is_combind"=>"","basic_status"=>"","page"=>1,"size"=>15],"post","trim");
  15. $where=[];
  16. if($param['skuCode']!="") $where[]= ["skuCode","=","%{$param['skuCode']}%"];
  17. if($param['good_type']!="") $where[]= ["good_type","=",$param['good_type']];
  18. if($param['buyer_code']!="") $where[]= ["buyer_code","=",$param['buyer_code']];
  19. if($param['buyer_name']!="") $where[]= ["buyer_name","like","%{$param['buyer_name']}%"];
  20. if($param['start']!="") $where[]= ["create_time",">=",startTime($param['start'])];
  21. if($param['end']!="") $where[]= ["create_time","<=",endTime($param['end'])];
  22. if($param['is_combind']!="") $where[]= ["is_combind","=",$param['is_combind']];
  23. if($param['basic_status']!=="") $where[]= ["basic_status","=",$param['basic_status']];
  24. $list = $this->model
  25. ->with(["catInfo","ProductsCombind"=>['products'],"ProductStock"])
  26. ->where($where)
  27. ->order("id desc")
  28. ->paginate(['page'=>$param['page'],'list_rows'=>$param['size']]);
  29. return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  30. }
  31. public function create(){
  32. $param= $this->request->param(["skuCode"=>"","goodName"=>"","good_type"=>"","buyer_code"=>"","buyer_name"=>"",
  33. "seller_code"=>"","seller_name"=>"","good_source"=>"","inv_good_name"=>"","is_combind"=>"","spec"=>"","good_code"=>"",
  34. "unit"=>"","unit_price"=>"","subunit_price"=>"","unit_weight"=>"","cat_code"=>"","cat_tax"=>"","inv_type"=>"",
  35. "basic_status"=>1,'spectral' =>'',"childArr"=>[]],"post","trim");
  36. $valid = $this->validate($param,[
  37. 'skuCode|商品编号'=>'require|max:255',
  38. 'goodName|商品名称'=>'require|max:255',
  39. 'good_type|商品类型'=>'require|in:1,2,3',
  40. 'buyer_code|买方公司纳税识别号'=>'require|max:255',
  41. 'buyer_name|买方公司名称'=>'require|max:255',
  42. 'seller_code|卖方公司纳税识别号'=>'require|max:255',
  43. 'seller_name|卖方公司名称'=>'require|max:255',
  44. 'good_source|商品来源'=>'require|in:1,2',
  45. 'inv_good_name|发票商品名称'=>'require|max:255',
  46. 'is_combind|是否组合商品'=>'require|in:0,1',
  47. 'spec|规格'=>'max:255',
  48. 'good_code|商品代码'=>'max:255',
  49. 'unit|单位'=>'require|max:255',
  50. 'unit_price|成本税前单价'=>'require|float',
  51. 'subunit_price|成本税后单价'=>'require|float',
  52. 'unit_weight|重量'=>'float',
  53. "spectral|分光"=>'max:255',
  54. 'cat_code|进项类目'=>'require|max:255',
  55. 'cat_tax|进项税率'=>'require|max:255',
  56. 'inv_type|发票类型'=>'require|max:255',
  57. 'basic_status|进项成本状态'=>'require|in:1,2',
  58. 'childArr|商品子商品'=>'requireIf:is_combind,1|array'
  59. ]);
  60. if($valid!==true) return error($valid);
  61. $param['apply_id']=$this->uid;
  62. $param['apply_name']=$this->uname;
  63. if($param['is_combind']==1){
  64. foreach ($param['childArr'] as $key=>$item){
  65. $iteminfo = $this->model->where('id',$item['child_id'])->findOrEmpty();
  66. if($iteminfo->isEmpty()) return error("子商品{$iteminfo->skuCode}不存在");
  67. if($item['child_num']<=0) return error("子商品{$iteminfo->skuCode}数量必须大于0");
  68. if($iteminfo->basic_status==2) return error("子商品{$iteminfo->skuCode}成本状态为预估成本");
  69. }
  70. }
  71. $where=$param;
  72. unset($where['childArr']);
  73. $isT = $this->model->where($where)->findOrEmpty();
  74. if(!$isT->isEmpty()) return error("商品编号{$param['skuCode']}已存在");
  75. $this->model->startTrans();
  76. try{
  77. $res = $this->model->create($param);
  78. if($res->isEmpty()) throw new \Exception('添加失败');
  79. if($param['is_combind']==1){
  80. $parentid = $res->id;
  81. $childRes = (new ProductsCombind)->saveAll(array_map(function ($item) use ($parentid ){
  82. $item['parent_id']=$parentid;
  83. return $item;
  84. },$param['childArr']));
  85. if($childRes->isEmpty()) throw new \Exception('添加失败');
  86. }
  87. $this->model->commit();
  88. }catch (\Exception $e){
  89. $this->model->rollback();
  90. return error($e->getMessage());
  91. }
  92. return success('添加成功');
  93. }
  94. public function update(){
  95. $param= $this->request->param(["id"=>"","skuCode"=>"","goodName"=>"","good_type"=>"","buyer_code"=>"","buyer_name"=>"",
  96. "seller_code"=>"","seller_name"=>"","good_source"=>"","inv_good_name"=>"","is_combind"=>"","spec"=>"","good_code"=>"",
  97. "unit"=>"","unit_price"=>"","subunit_price"=>"","unit_weight"=>"","cat_code"=>"","cat_tax"=>"","inv_type"=>"",
  98. "basic_status"=>"","childArr"=>[]],"post","trim");
  99. $valid = $this->validate($param,[
  100. 'id|商品ID'=>'require|integer',
  101. 'skuCode|商品编号'=>'require|max:255',
  102. 'goodName|商品名称'=>'require|max:255',
  103. 'good_type|商品类型'=>'require|in:1,2,3',
  104. 'buyer_code|买方公司纳税识别号'=>'require|max:255',
  105. 'buyer_name|买方公司名称'=>'require|max:255',
  106. 'seller_code|卖方公司纳税识别号'=>'require|max:255',
  107. 'seller_name|卖方公司名称'=>'require|max:255',
  108. 'good_source|商品来源'=>'require|in:1,2',
  109. 'inv_good_name|发票商品名称'=>'require|max:255',
  110. 'is_combind|是否组合商品'=>'require|in:0,1',
  111. 'spec|规格'=>'max:255',
  112. 'good_code|商品代码'=>'max:255',
  113. 'unit|单位'=>'require|max:255',
  114. 'unit_price|成本税前单价'=>'require|float',
  115. 'subunit_price|成本税后单价'=>'require|float',
  116. 'unit_weight|重量'=>'float',
  117. "spectral|分光"=>'max:255',
  118. 'cat_code|进项类目'=>'require|max:255',
  119. 'cat_tax|进项税率'=>'require|max:255',
  120. 'inv_type|发票类型'=>'require|max:255',
  121. 'basic_status|进项成本状态'=>'require|in:1,2',
  122. 'childArr|商品子商品'=>'requireIf:is_combind,1|array'
  123. ]);
  124. if($valid!==true) return error($valid);
  125. $info = $this->model->with(['ProductsCombind'])->findOrEmpty($param['id']);
  126. if($info->isEmpty()) return error('数据不存在');
  127. if($info->is_combind==1|| $param['is_combind']==1){
  128. $childIds =array_column($info->ProductsCombind->toArray(),"id") ;
  129. $paramChilds = array_column($param['childArr'],'id');
  130. $delIds = array_diff($childIds,$paramChilds);
  131. $add=[];
  132. foreach ($param['childArr'] as $key=>$item){
  133. $iteminfo = $this->model->where('id',$item['child_id'])->findOrEmpty();
  134. if($iteminfo->isEmpty()) return error("子商品{$iteminfo->skuCode}不存在");
  135. if($item['child_num']<=0) return error("子商品{$iteminfo->skuCode}数量必须大于0");
  136. if($iteminfo->basic_status==2) return error("子商品{$iteminfo->skuCode}成本状态为预估成本");
  137. $item['id']= $item['id']??null;
  138. $item['parent_id']=$param['id'];
  139. $add[]=$item;
  140. }
  141. }
  142. $where=$param;
  143. unset($where['childArr']);
  144. unset($where['id']);
  145. $isT = $this->model->where($where)->where("id","<>",$param['id'])->findOrEmpty();
  146. if(!$isT->isEmpty()) return error("商品编号{$param['skuCode']}已存在");
  147. $this->model->startTrans();
  148. try{
  149. $res = $info->save($param);
  150. if(!$res) throw new \Exception('更新失败');
  151. if($param['is_combind']==1){
  152. if(!empty($delIds)) ProductsCombind::where(["parent_id"=>$param['id'],"id"=>$delIds])->delete();
  153. if(!empty( $add)){
  154. $childRes = (new ProductsCombind)->saveAll( $add);
  155. if($childRes->isEmpty()) throw new \Exception('更新失败');
  156. }
  157. }
  158. $this->model->commit();
  159. }catch (\Exception $e){
  160. $this->model->rollback();
  161. return error($e->getMessage());
  162. }
  163. return success('更新成功');
  164. }
  165. public function info(){
  166. $id= $this->request->param("id","0","int");
  167. $info = $this->model->with(["catInfo","ProductsCombind"=>['products'],'ProductStock'])->findOrEmpty($id);
  168. if($info->isEmpty()) return error('数据不存在');
  169. return success('获取成功',$info);
  170. }
  171. public function delete(){
  172. $id= $this->request->param("id","0","int");
  173. $info = $this->model->findOrEmpty($id);
  174. if($info->isEmpty()) return error('数据不存在');
  175. $res = $info->delete();
  176. if($info->is_combind==1){
  177. ProductsCombind::where("parent_id",$id)->delete();
  178. }
  179. return $res?success('删除成功'):error('删除失败');
  180. }
  181. public function status(){
  182. $param= $this->request->param(["id"=>"","status"=>""],"post","trim");
  183. $valid = $this->validate($param,[
  184. 'id|商品ID'=>'require|integer',
  185. 'status|状态'=>'require|in:0,1'
  186. ]);
  187. if($valid!==true) return error($valid);
  188. $info = $this->model->findOrEmpty($param['id']);
  189. if($info->isEmpty()) return error('数据不存在');
  190. $res = $info->save(['status'=>$param['status']]);
  191. return $res?success('修改成功'):error('修改失败');
  192. }
  193. public function getGoods(){
  194. $param= $this->request->param(["skuCode"=>"","good_type"=>"","buyer_code"=>"","seller_code"=>"","start"=>"",
  195. "end"=>"","is_combind"=>"","basic_status"=>"","limit"=>100],"post","trim");
  196. $valid = $this->validate($param,[
  197. 'skuCode|商品编号'=>'max:255',
  198. 'good_type|商品类型'=>'in:1,2,3',
  199. 'buyer_code|买方公司纳税识别号'=>'max:255',
  200. 'seller_code|卖方公司纳税识别号'=>'max:255',
  201. 'start|开始时间'=>'date',
  202. 'end|结束时间'=>'date',
  203. 'is_combind|是否组合商品'=>'in:0,1',
  204. 'basic_status|进项成本状态'=>'in:1,2',
  205. ]);
  206. if($valid!==true) return error($valid);
  207. $where=[];
  208. if($param['basic_status']!=='') $where[]= ['basic_status','=',$param['basic_status']];
  209. if(!empty($param['skuCode'])) $where[]=['skuCode','like','%'.$param['skuCode'].'%'];
  210. if(!empty($param['good_type'])) $where[]=['good_type','=',$param['good_type']];
  211. if(!empty($param['buyer_code'])) $where[]=['buyer_code','like','%'.$param['buyer_code'].'%'];
  212. if(!empty($param['seller_code'])) $where[]=['seller_code','like','%'.$param['seller_code'].'%'];
  213. if(!empty($param['start']) && !empty($param['end'])) $where[]=['create_time','between',[$param['start'],$param['end']]];
  214. $list = $this->model->with(['catInfo','ProductStock','ProductsCombind'=>['products']])->where($where)->order('id desc')->limit($param['limit'])->select();
  215. return success('获取成功',$list);
  216. }
  217. public function combindGood(){
  218. $param= $this->request->param(["parent_id"=>"","childArr"=>[]],"post","trim");
  219. $valid = $this->validate($param,[
  220. 'parent_id|商品ID'=>'require|integer',
  221. 'childArr|子商品'=>'require|array'
  222. ]);
  223. if($valid!==true) return error($valid);
  224. $parent = $this->model->findOrEmpty($param['pid']);
  225. if($parent->isEmpty()) return error('数据不存在');
  226. $combind= new ProductsCombind();
  227. $ist = $combind->where(['parent_id'=>$param['parent_id']])->column("child_id");
  228. $del=array_diff($ist, array_column($param['childArr'],'child_id'));
  229. $add=[];
  230. foreach ($param['childArr'] as $item){
  231. $vali = $this->validate($item,[
  232. 'child_id|子商品ID'=>'require|integer',
  233. 'child_num|子商品数量'=>'require|integer'
  234. ]);
  235. if($vali!==true) return error($vali);
  236. $add[]=[
  237. "id"=>$ist[$item['child_id']]??null,
  238. "parent_id"=>$param['parent_id'],
  239. "child_num"=>$item['child_num'],
  240. "child_id"=>$item['child_id']
  241. ];
  242. }
  243. $this->model->startTrans();
  244. try{
  245. if(!empty($del))$combind->where(["parent_id"=>$param['parent_id'],"child_id"=>$del])->delete();
  246. if(!empty($add))$combind->saveAll($add);
  247. $this->model->commit();
  248. }catch (\Exception $e){
  249. $this->model->rollback();
  250. return error($e->getMessage());
  251. }
  252. return success('修改成功');
  253. }
  254. public function CheckCreate(){
  255. $param= $this->request->param(["product_id"=>"",'check_num'=>"","check_type"=>0,"fz_date"=>""],"post","trim");
  256. $valid = $this->validate($param,[
  257. 'product_id|商品ID'=>'require|integer',
  258. 'check_num|盘点数量'=>'require|float',
  259. 'check_type|盘点类型'=>'require|in:0,1,2',
  260. 'fz_date|调整单封账日期'=>'max:255'
  261. ]);
  262. if($valid!==true) return error($valid);
  263. $product = $this->model->with(['catInfo','ProductStock'])->findOrEmpty($param['product_id']);
  264. if($product->isEmpty()) return error('商品数据不存在');
  265. if($product->is_combind==1) return error('组合商品不允许盘点');
  266. $diff_num = bcsub($param['check_num'],$product->residue_stock??"0",8);
  267. if($diff_num==0) return error('盘点数量与库存数量一致,无需盘点');
  268. $is_diff = $diff_num>0?1:0;
  269. $data=[
  270. 'checkCode'=>makeNo("CWPD"),
  271. 'product_id'=>$param['product_id'],
  272. 'check_num'=>$param['check_num'],
  273. 'diff_num'=>$diff_num,
  274. 'stock_num'=>$product->residue_stock??"0",
  275. 'unit_price'=>$product->unit_price,
  276. 'diff_fee'=>bcmul($diff_num,$product->unit_price,8),
  277. 'check_type'=>$param['check_type'],
  278. 'fz_date'=>$param['fz_date'],
  279. 'is_diff'=>$is_diff,
  280. "apply_id"=> $this->uid,
  281. "apply_name"=> $this->uname
  282. ];
  283. $this->model->startTrans();
  284. try{
  285. $use=ProductCheck::create($data);
  286. if($use->isEmpty()) throw new \think\Exception("新增盘点单失败");
  287. }catch (\Exception $e){
  288. $this->model->rollback();
  289. return error($e->getTrace());
  290. }
  291. $this->model->commit();
  292. return success('盘点单创建成功');
  293. }
  294. public function CheckList(){
  295. $param= $this->request->param(["checkCode"=>"","check_type"=>"","start"=>"","end"=>"","skuCode"=>"","buyer_code"=>"","page"=>1,"size"=>20]
  296. ,"post","trim");
  297. $where=[];
  298. if($param['checkCode']!=="") $where[]=['checkCode','like','%'.$param['checkCode'].'%'];
  299. if($param['check_type']!=="") $where[]=['check_type','=',$param['check_type']];
  300. if(!empty($param['start']) && !empty($param['end'])) $where[]=['product_check.create_time','between',[startTime($param['start']),endTime($param['end'])]];
  301. if($param['skuCode']!=="") $where[]=['product.skuCode','like','%'.$param['skuCode'].'%'];
  302. if($param['buyer_code']!=="") $where[]=['product.buyer_code','=',$param['buyer_code']];
  303. $list = ProductCheck::with(['product'=>['ProductStock']])->withJoin(['product'],'LEFT')
  304. ->where($where)
  305. ->order('id desc')
  306. ->paginate(['page'=>$param['page'],'list_rows'=>$param['size']]);
  307. return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  308. }
  309. }