FinancialProducts.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace app\cxinv\controller;
  3. use app\cxinv\model\ProductsCombind;
  4. use think\App;
  5. class FinancialProducts extends Base{
  6. public function __construct(App $app) {
  7. parent::__construct($app);
  8. $this->model=new \app\cxinv\model\FinancialProducts();
  9. }
  10. public function List(){
  11. $param = $this->request->param(["skuCode"=>"","good_type"=>"","buyer_code"=>"","buyer_name"=>"","start"=>"",
  12. "end"=>"","page"=>1,"size"=>15],"post","trim");
  13. $where=[];
  14. if($param['skuCode']!="") $where[]= ["skuCode","=","%{$param['skuCode']}%"];
  15. if($param['good_type']!="") $where[]= ["good_type","=",$param['good_type']];
  16. if($param['buyer_code']!="") $where[]= ["buyer_code","=",$param['buyer_code']];
  17. if($param['buyer_name']!="") $where[]= ["buyer_name","like","%{$param['buyer_name']}%"];
  18. if($param['start']!="") $where[]= ["create_time",">=",startTime($param['start'])];
  19. if($param['end']!="") $where[]= ["create_time","<=",endTime($param['end'])];
  20. $list = $this->model
  21. ->where($where)
  22. ->order("id desc")
  23. ->paginate(['page'=>$param['page'],'list_rows'=>$param['size']]);
  24. return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  25. }
  26. public function create(){
  27. $param= $this->request->param(["skuCode"=>"","goodName"=>"","good_type"=>"","buyer_code"=>"","buyer_name"=>"",
  28. "seller_code"=>"","seller_name"=>"","good_source"=>"","inv_good_name"=>"","is_combind"=>"","spec"=>"","good_code"=>"",
  29. "unit"=>"","unit_price"=>"","subunit_price"=>"","unit_weight"=>"","cat_code"=>"","cat_tax"=>"","inv_type"=>"",
  30. "basic_status"=>1,"childArr"=>[]],"post","trim");
  31. $valid = $this->validate($param,[
  32. 'skuCode|商品编号'=>'require|max:255',
  33. 'goodName|商品名称'=>'require|max:255',
  34. 'good_type|商品类型'=>'require|in:1,2,3',
  35. 'buyer_code|买方公司纳税识别号'=>'require|max:255',
  36. 'buyer_name|买方公司名称'=>'require|max:255',
  37. 'seller_code|卖方公司纳税识别号'=>'require|max:255',
  38. 'seller_name|卖方公司名称'=>'require|max:255',
  39. 'good_source|商品来源'=>'require|in:1,2',
  40. 'inv_good_name|发票商品名称'=>'require|max:255',
  41. 'is_combind|是否组合商品'=>'require|in:0,1',
  42. 'spec|规格'=>'max:255',
  43. 'good_code|商品代码'=>'max:255',
  44. 'unit|单位'=>'require|max:255',
  45. 'unit_price|成本税前单价'=>'require|float',
  46. 'subunit_price|成本税后单价'=>'require|float',
  47. 'unit_weight|重量'=>'float',
  48. "spectral|分光"=>'max:255',
  49. 'cat_code|进项类目'=>'require|max:255',
  50. 'cat_tax|进项税率'=>'require|max:255',
  51. 'inv_type|发票类型'=>'require|max:255',
  52. 'basic_status|进项成本状态'=>'require|in:1,2',
  53. 'childArr|商品子商品'=>'requireIf:is_combind:1|array'
  54. ]);
  55. if($valid!==true) return error($valid);
  56. $param['apply_id']=$this->uid;
  57. $param['apply_name']=$this->uname;
  58. $this->model->startTrans();
  59. try{
  60. $res = $this->model->create($param);
  61. if($res->isEmpty()) throw new \Exception('添加失败');
  62. if($param['is_combind']==1){
  63. $parentid = $res->id;
  64. $childRes = (new ProductsCombind)->saveAll(array_map(function ($item) use ($parentid ){
  65. $item['parent_id']=$parentid;
  66. return $item;
  67. },$param['childArr']));
  68. if($childRes->isEmpty()) throw new \Exception('添加失败');
  69. }
  70. $this->model->commit();
  71. }catch (\Exception $e){
  72. $this->model->rollback();
  73. return error($e->getMessage());
  74. }
  75. return success('添加成功');
  76. }
  77. public function update(){
  78. $param= $this->request->param(["id"=>"","skuCode"=>"","goodName"=>"","good_type"=>"","buyer_code"=>"","buyer_name"=>"",
  79. "seller_code"=>"","seller_name"=>"","good_source"=>"","inv_good_name"=>"","is_combind"=>"","spec"=>"","good_code"=>"",
  80. "unit"=>"","unit_price"=>"","subunit_price"=>"","unit_weight"=>"","cat_ code"=>"","cat_tax"=>"","inv_type"=>"",
  81. "basic_status"=>"","childArr"=>[]],"post","trim");
  82. $valid = $this->validate($param,[
  83. 'id|商品ID'=>'require|integer',
  84. 'skuCode|商品编号'=>'require|max:255',
  85. 'goodName|商品名称'=>'require|max:255',
  86. 'good_type|商品类型'=>'require|in:1,2,3',
  87. 'buyer_code|买方公司纳税识别号'=>'require|max:255',
  88. 'buyer_name|买方公司名称'=>'require|max:255',
  89. 'seller_code|卖方公司纳税识别号'=>'require|max:255',
  90. 'seller_name|卖方公司名称'=>'require|max:255',
  91. 'good_source|商品来源'=>'require|in:1,2',
  92. 'inv_good_name|发票商品名称'=>'require|max:255',
  93. 'is_combind|是否组合商品'=>'require|in:0,1',
  94. 'spec|规格'=>'max:255',
  95. 'good_code|商品代码'=>'max:255',
  96. 'unit|单位'=>'require|max:255',
  97. 'unit_price|成本税前单价'=>'require|float',
  98. 'subunit_price|成本税后单价'=>'require|float',
  99. 'unit_weight|重量'=>'float',
  100. "spectral|分光"=>'max:255',
  101. 'cat_code|进项类目'=>'require|max:255',
  102. 'cat_tax|进项税率'=>'require|max:255',
  103. 'inv_type|发票类型'=>'require|max:255',
  104. 'basic_status|进项成本状态'=>'require|in:1,2',
  105. 'childArr|商品子商品'=>'requireIf:is_combind:1|array'
  106. ]);
  107. if($valid!==true) return error($valid);
  108. $info = $this->model->with(['ProductsCombind'])->findOrEmpty($param['id']);
  109. if($info->isEmpty()) return error('数据不存在');
  110. if($info->is_combind==1|| $param['is_combind']==1){
  111. $childIds =array_column($info->ProductsCombind->toArray(),"id") ;
  112. $paramChilds = array_column($param['childArr'],'id');
  113. $delIds = array_diff($childIds,$paramChilds);
  114. $add = array_map(function ($item)use ($param){
  115. $item['id']= $item['id']??null;
  116. $item['parent_id']=$param['id'];
  117. return $item;
  118. },$param['childArr']);
  119. }
  120. $this->model->startTrans();
  121. try{
  122. $res = $info->save($param);
  123. if(!$res) throw new \Exception('更新失败');
  124. if($param['is_combind']==1){
  125. if(!empty($delIds)) ProductsCombind::where(["parent_id"=>$param['id'],"id"=>$delIds])->delete();
  126. if(!empty( $add)){
  127. $childRes = (new ProductsCombind)->saveAll( $add);
  128. if($childRes->isEmpty()) throw new \Exception('更新失败');
  129. }
  130. }
  131. $this->model->commit();
  132. }catch (\Exception $e){
  133. $this->model->rollback();
  134. return error($e->getMessage());
  135. }
  136. return success('更新成功');
  137. }
  138. public function info(){
  139. $id= $this->request->param("id","0","int");
  140. $info = $this->model->with(["ProductsCombind"=>['products']])->findOrEmpty($id);
  141. if($info->isEmpty()) return error('数据不存在');
  142. return success('获取成功',$info);
  143. }
  144. public function delete(){
  145. $id= $this->request->param("id","0","int");
  146. $info = $this->model->findOrEmpty($id);
  147. if($info->isEmpty()) return error('数据不存在');
  148. $res = $info->delete();
  149. if($info->is_combind==1){
  150. ProductsCombind::where("parent_id",$id)->delete();
  151. }
  152. return $res?success('删除成功'):error('删除失败');
  153. }
  154. public function status(){
  155. $param= $this->request->param(["id"=>"","status"=>""],"post","trim");
  156. $valid = $this->validate($param,[
  157. 'id|商品ID'=>'require|integer',
  158. 'status|状态'=>'require|in:0,1'
  159. ]);
  160. if($valid!==true) return error($valid);
  161. $info = $this->model->findOrEmpty($param['id']);
  162. if($info->isEmpty()) return error('数据不存在');
  163. $res = $info->save(['status'=>$param['status']]);
  164. return $res?success('修改成功'):error('修改失败');
  165. }
  166. public function getGoods(){
  167. $param= $this->request->param(["skuCode"=>"","good_type"=>"","buyer_code"=>"","seller_code"=>"","start"=>"",
  168. "end"=>"","limit"=>100],"post","trim");
  169. $valid = $this->validate($param,[
  170. 'skuCode|商品编号'=>'max:255',
  171. 'good_type|商品类型'=>'in:1,2,3',
  172. 'buyer_code|买方公司纳税识别号'=>'max:255',
  173. 'seller_code|卖方公司纳税识别号'=>'max:255',
  174. 'start|开始时间'=>'date',
  175. 'end|结束时间'=>'date'
  176. ]);
  177. if($valid!==true) return error($valid);
  178. $where=[];
  179. if(!empty($param['skuCode'])) $where[]=['skuCode','like','%'.$param['skuCode'].'%'];
  180. if(!empty($param['good_type'])) $where[]=['good_type','=',$param['good_type']];
  181. if(!empty($param['buyer_code'])) $where[]=['buyer_code','like','%'.$param['buyer_code'].'%'];
  182. if(!empty($param['seller_code'])) $where[]=['seller_code','like','%'.$param['seller_code'].'%'];
  183. if(!empty($param['start']) && !empty($param['end'])) $where[]=['create_time','between',[$param['start'],$param['end']]];
  184. $list = $this->model->where($where)->order('id desc')->limit($param['limit'])->select();
  185. return success('获取成功',$list);
  186. }
  187. public function combindGood(){
  188. $param= $this->request->param(["parent_id"=>"","childArr"=>[]],"post","trim");
  189. $valid = $this->validate($param,[
  190. 'parent_id|商品ID'=>'require|integer',
  191. 'childArr|子商品'=>'require|array'
  192. ]);
  193. if($valid!==true) return error($valid);
  194. $parent = $this->model->findOrEmpty($param['pid']);
  195. if($parent->isEmpty()) return error('数据不存在');
  196. $combind= new ProductsCombind();
  197. $ist = $combind->where(['parent_id'=>$param['parent_id']])->column("child_id");
  198. $del=array_diff($ist, array_column($param['childArr'],'child_id'));
  199. $add=[];
  200. foreach ($param['childArr'] as $item){
  201. $vali = $this->validate($item,[
  202. 'child_id|子商品ID'=>'require|integer',
  203. 'child_num|子商品数量'=>'require|integer'
  204. ]);
  205. if($vali!==true) return error($vali);
  206. $add[]=[
  207. "id"=>$ist[$item['child_id']]??null,
  208. "parent_id"=>$param['parent_id'],
  209. "child_num"=>$item['child_num'],
  210. "child_id"=>$item['child_id']
  211. ];
  212. }
  213. $this->model->startTrans();
  214. try{
  215. if(!empty($del))$combind->where(["parent_id"=>$param['parent_id'],"child_id"=>$del])->delete();
  216. if(!empty($add))$combind->saveAll($add);
  217. $this->model->commit();
  218. }catch (\Exception $e){
  219. $this->model->rollback();
  220. return error($e->getMessage());
  221. }
  222. return success('修改成功');
  223. }
  224. }