FinancialProducts.php 16 KB

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