InvoiceItem.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\InvoiceGood;
  4. use app\admin\model\InvoiceOrder;
  5. use app\admin\model\OrderCategory;
  6. use app\admin\model\PayInfo;
  7. use think\App;
  8. use think\facade\Validate;
  9. class InvoiceItem extends Base{
  10. public function __construct(App $app) {
  11. # $this->novalidate = ["*"];
  12. parent::__construct($app);
  13. }
  14. //查询
  15. public function cgdListByPayNo(){
  16. $param = $this->request->param(["payNo"=>"","merge_code"=>"","inv_good_name"=>"",'good_name'=>''],"post","trim");
  17. $where=[["a.status","=",1],["a.is_del","=",0]];
  18. if($param["payNo"]!=="") $where[]=["a.payNo","=",$param["payNo"]];
  19. if($param["merge_code"]!=="") $where[]=["c.merge_code","like","%{$param['merge_code']}%"];
  20. if($param["inv_good_name"]!=="") $where[]=["c.inv_good_name","like","%{$param["inv_good_name"]}%"];
  21. if($param["good_name"]!=="") $where[]=["b.goodName","like","%{$param["good_name"]}%"];
  22. $list = PayInfo::alias('a')
  23. ->join('cgd_info b','a.cgdNo=b.sequenceNo',"left")
  24. ->join('order_category c','b.sequenceNo=c.code and b.goodNo=c.spuCode and c.order_type=2',"left")
  25. ->where($where)
  26. ->field("b.sequenceNo,b.goodNo,b.goodName,(b.goodNum-thNum) as goodNum,b.goodPrice,b.totalPrice,
  27. c.merge_code,c.cat_code,c.cat_name,c.short_name,c.tax,c.inv_good_name")
  28. ->select();
  29. $list->each(function (&$item){
  30. $item['balance_amount']= bcsub($item['totalPrice'],InvoiceOrder::getInvoiceOrderTotalFee($item['sequenceNo']),2);
  31. });
  32. $this->success('获取成功',$list);
  33. }
  34. //查询
  35. public function qrdListByInvNo(){
  36. $param = $this->request->param(["invNo"=>"","merge_code"=>"","inv_good_name"=>""],"post","trim");
  37. $where=[["a.goodNum",">",0],["a.is_del","=",0]];
  38. if($param["invNo"]!=="") $where[]=["a.invNo","=",$param["invNo"]];
  39. if($param["merge_code"]!=="") $where[]=["c.merge_code","=",$param["merge_code"]];
  40. if($param["inv_good_name"]!=="") $where[]=["c.inv_good_name","like","%{$param["inv_good_name"]}%"];
  41. $list= InvoiceGood::alias('a')
  42. ->join('qrd_info b','a.orderCode=b.sequenceNo','left')
  43. ->join('order_category c','b.sequenceNo=c.code and b.goodNo=c.spuCode and c.order_type=1','left')
  44. ->where($where)
  45. ->field("b.sequenceNo,b.goodNo,b.goodName,a.goodNum,a.goodPrice,a.totalPrice,
  46. c.merge_code,c.cat_code,c.cat_name,c.short_name,c.tax,c.inv_good_name")
  47. ->select();
  48. $list->each(function (&$item){
  49. $item['balance_amount']= bcsub($item['totalPrice'],InvoiceOrder::getInvoiceOrderTotalFee($item['sequenceNo']),2);
  50. });
  51. $this->success('获取成功',$list);
  52. }
  53. //添加发票明细对应关系
  54. public function create(){
  55. $param = $this->request->param(["itemId"=>"","orderArr"=>[]],"post","trim");
  56. $valid=Validate::rule([
  57. 'itemId|发票明细id' => 'require|number|gt:0',
  58. 'orderArr|订单信息' => 'require|array',
  59. ]);
  60. if(!$valid->check($param))$this->error($valid->getError());
  61. $valids=Validate::rule([
  62. 'code|订单号' => 'require|max:255',
  63. 'spuCode|商品编码' => 'require|max:255',
  64. 'good_name|商品名称' => 'require|max:255',
  65. 'num|数量' => 'require|float|gt:0',
  66. 'good_price|商品价格' => 'require|float|gt:0',
  67. 'total_amount|总金额' => 'require|float|gt:0',
  68. 'remark|备注' => 'max:255',
  69. ]);
  70. $item = \app\admin\model\InvoiceItem::where(['id'=>$param["itemId"]])->findOrEmpty();
  71. if($item->isEmpty())$this->error("发票明细不存在");
  72. $save=[];
  73. $amount = $item['balance_amount'];
  74. foreach ($param["orderArr"] as $k=>$v){
  75. if(!$valids->check($v))$this->error($valids->getError());
  76. $taxInfo = OrderCategory::where(['code'=>$v['code'],'spuCode'=>$v['spuCode']])->findOrEmpty();
  77. if($taxInfo->isEmpty())$this->error("商品编码{$v['spuCode']}税目信息不存在");
  78. $balance="0";
  79. if($amount<$v['total_amount']){
  80. $balance = bcsub($v['total_amount'],$amount,2);
  81. $amount="0";
  82. }else{
  83. $balance = "0";
  84. $amount = bcsub($amount,$v['total_amount'],2);
  85. }
  86. $diff=[];
  87. $tax_diff=1;
  88. $cat_diff=1;
  89. if(str_replace("%","",$item['tax'])!=$taxInfo['tax']){
  90. $diff["tax"] = [
  91. "inv_tax"=>str_replace('%','',$item['tax']),
  92. "order_tax"=>$taxInfo['tax'],
  93. ];
  94. $tax_diff=2;
  95. }
  96. if($item['cat_code']!=$taxInfo['merge_code']){
  97. $diff["cat_code"] = [
  98. "inv_cat_code"=>$item['cat_code'],
  99. "order_cat_code"=>$taxInfo['merge_code'],
  100. ];
  101. $cat_diff=2;
  102. }
  103. $save[]=[
  104. "itemId"=>$param["itemId"],
  105. "code"=>$v["code"],
  106. "spuCode"=>$v["spuCode"],
  107. "good_name"=>$v['good_name'],
  108. "num"=>$v["num"],
  109. "good_price"=>$v["good_price"],
  110. "total_amount"=>$v["total_amount"],
  111. "tax_diff"=> $tax_diff,
  112. "cat_diff"=>$cat_diff,
  113. "balance_amount"=>$balance,
  114. "diff_info"=>$diff,
  115. "status"=>$balance>0?2:1,
  116. "remark"=>$v["remark"],
  117. "apply_id"=>$this->uid,
  118. "apply_name"=>$this->uname
  119. ];
  120. }
  121. $item->startTrans();
  122. try{
  123. $res=(new \app\admin\model\InvoiceOrder)->saveAll($save);
  124. if($res){
  125. $item->balance_amount = $amount;
  126. $item->status = $amount>0?2:1; //已关联数据 $amont=0 2 还有余额则为1
  127. $item->save();
  128. }else throw new \Exception("添加失败");
  129. $item->commit();
  130. }catch (\Exception $e){
  131. $item->rollback();
  132. $this->error($e->getMessage());
  133. }
  134. $this->success('添加成功');
  135. }
  136. //删除发票明细对应关系
  137. public function delete(){
  138. $param = $this->request->param(["id"=>""],"post","trim");
  139. $valid=Validate::rule([
  140. 'id|发票明细对应id' => 'require|number|gt:0',
  141. ]);
  142. if(!$valid->check($param))$this->error($valid->getError());
  143. $info = \app\admin\model\InvoiceOrder::where(['id'=>$param["id"]])->findOrEmpty();
  144. if($info->isEmpty())$this->error("发票明细对应关系不存在");
  145. $item = \app\admin\model\InvoiceItem::where(['id'=>$info['itemId']])->findOrEmpty();
  146. if($item->isEmpty())$this->error("发票明细不存在");
  147. InvoiceOrder::startTrans();
  148. try{
  149. $res = $info->delete();
  150. if($res){
  151. $count = \app\admin\model\InvoiceOrder::where(['itemId'=>$info['itemId']])->count();
  152. $item->balance_amount = bcadd($item['balance_amount'],bcsub($info['total_amount'],$info['balance_amount'],2),2);
  153. $item->status = ($count==0) ? 0 : ($item->balance_amount==$item->total_amount?0:($item->balance_amount>0?2:1));
  154. $item->save();
  155. }else throw new \Exception("删除失败");
  156. InvoiceOrder::commit();
  157. }catch (\Exception $e){
  158. InvoiceOrder::rollback();
  159. $this->error($e->getMessage());
  160. }
  161. $this->success('删除成功');
  162. }
  163. public function addRemark(){
  164. $param = $this->request->param(['id'=>''],'post','trim');
  165. $valid=Validate::rule([
  166. 'id|发票明细对应id' => 'require|number|gt:0',
  167. 'remark|备注' => 'require|max:255',
  168. ]);
  169. if(!$valid->check($param))$this->error($valid->getError());
  170. $info = \app\admin\model\InvoiceOrder::where(['id'=>$param['id']])->findOrEmpty();
  171. if($info->isEmpty())$this->error('发票明细对应关系不存在');
  172. $item = \app\admin\model\InvoiceItem::where(['id'=>$info['itemId']])->findOrEmpty();
  173. if($item->isEmpty())$this->error('发票明细不存在');
  174. $info->startTrans();
  175. try{
  176. $info->remark = $param['remark'] ?? '';
  177. $res = $info->save();
  178. if($res==false)throw new \Exception('备注添加失败');
  179. $info->commit();
  180. }catch (\Exception $e){
  181. $info->rollback();
  182. }
  183. $this->success('备注添加成功');
  184. }
  185. public function getListByCode(){
  186. $param=$this->request->param(["code"=>"","status"=>"","size"=>100]);
  187. $where=[];
  188. if($param["code"]!=="") $where[]=["invoiceCode","=",$param["code"]];
  189. if($param["status"]!=="") $where[]=["status","=",$param["status"]];
  190. $list= \app\admin\model\InvoiceItem::with(["OrderInfo"])
  191. ->where($where)
  192. ->limit($param["size"])
  193. ->select();
  194. $this->success('获取成功',$list);
  195. }
  196. public function importOrder(){
  197. $param=$this->request->param([ "order_type"=>"","list"=>[]],"post","trim");
  198. $valid = Validate::rule([
  199. 'order_type|订单类型' => 'require|in:1,2',
  200. 'list|发票明细' => 'require|array',
  201. ]);
  202. if(!$valid->check($param))$this->error($valid->getError());
  203. $listValid = Validate::rule([
  204. "itemId|发票明细ID"=>'require|number|gt:0',
  205. 'code|订单编码' => 'require|max:20',
  206. 'num|数量' => 'float|egt:0',
  207. 'good_price|商品价格' => 'float|egt:0',
  208. 'total_amount|总金额' => 'require|float|gt:0',
  209. 'remark|备注' =>'max:255',
  210. ]);
  211. $itemArr= \app\admin\model\InvoiceItem::whereIn('id',array_column($param['list'],"itemId"))->column("id,invoiceCode,order_type,balance_amount,cat_code,tax","id");
  212. $orderInfo=[];
  213. $orderItem=[];
  214. foreach($param["list"] as $v){
  215. if(!$listValid->check($v))$this->error($listValid->getError());
  216. if(!isset($itemArr[$v['itemId']]))$this->error("发票明细不存在");
  217. if(!isset($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v["code"]])){
  218. $orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]= \app\admin\model\InvoiceOrder::getOrderInfo( $itemArr[$v['itemId']]['invoiceCode'],$v['code'],$param['order_type']);
  219. }
  220. if($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]->isEmpty())$this->error("发票{$itemArr[$v['itemId']]['invoiceCode']}关联订单{$v['code']}信息不存在");
  221. if($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['balance_amount']<$v['total_amount'])$this->error("{$v["code"]}订单金额不足");
  222. if($itemArr[$v['itemId']]['balance_amount']<=0)$this->error("发票明细ID {$v["itemId"]} 可关联金额不足");
  223. if($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['merge_code']=="")$this->error("{$v["code"]}订单税目信息不存在");
  224. if($itemArr[$v['itemId']]['balance_amount']<$v['total_amount']){
  225. $balance = bcsub($v['total_amount'],$itemArr[$v['itemId']]['balance_amount'],2);
  226. $itemArr[$v['itemId']]['balance_amount']='0';
  227. }else{
  228. $balance = '0';
  229. $itemArr[$v['itemId']]['balance_amount'] = bcsub($itemArr[$v['itemId']]['balance_amount'],$v['total_amount'],2);
  230. }
  231. $orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['balance_amount'] =
  232. bcsub($orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['balance_amount'],bcsub($v['total_amount'],$balance,2),2);
  233. $tax = $orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['tax'];
  234. $merge_code = $orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['merge_code'];
  235. $diff=[];
  236. $tax_diff=1;
  237. $cat_diff=1;
  238. if(str_replace('%','',$itemArr[$v['itemId']]['tax'])!=$tax ){
  239. $diff['tax'] = [
  240. 'inv_tax'=>str_replace('%','',$itemArr[$v['itemId']]['tax']),
  241. 'order_tax'=>$tax ,
  242. ];
  243. $tax_diff=2;
  244. }
  245. if($itemArr[$v['itemId']]['cat_code']!=$merge_code){
  246. $diff['cat_code'] = [
  247. 'inv_cat_code'=>$itemArr[$v['itemId']]['cat_code'],
  248. 'order_cat_code'=>$merge_code,
  249. ];
  250. $cat_diff=2;
  251. }
  252. $itemArr[$v['itemId']]['status'] = ($itemArr[$v['itemId']]['balance_amount']>0?2:1);
  253. $orderItem[]=[
  254. 'itemId'=>$v['itemId'],
  255. 'code'=>$v['code'],
  256. 'spuCode'=>$orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['goodNo'],
  257. 'good_name'=>$orderInfo[$itemArr[$v['itemId']]['invoiceCode'].$v['code']]['goodName'],
  258. 'num'=>$v['num']??0,
  259. 'good_price'=>$v['good_price']??0,
  260. 'total_amount'=>$v['total_amount'],
  261. 'tax_diff'=> $tax_diff,
  262. 'cat_diff'=>$cat_diff,
  263. 'balance_amount'=>$balance,
  264. 'diff_info'=>$diff,
  265. 'status'=>$balance>0?2:1,
  266. 'remark'=>$v['remark'],
  267. 'apply_id'=>$this->uid,
  268. 'apply_name'=>$this->uname
  269. ];
  270. }
  271. $orderItemModel = new \app\admin\model\InvoiceItem();
  272. $orderItemModel->startTrans();
  273. try{
  274. $num=$orderItemModel->saveAll(array_values($itemArr));
  275. if(count($num)>0){
  276. (new InvoiceOrder())->saveAll($orderItem);
  277. }else throw new \Exception("导入失败");
  278. }catch (\Exception $e){
  279. $orderItemModel->rollback();
  280. $this->error($e->getMessage());
  281. }
  282. $orderItemModel->commit();
  283. $this->success("导入成功");
  284. }
  285. public function exportItem(){
  286. $param=$this->request->param([ "order_type"=>"","list"=>[]],"post","trim");
  287. }
  288. }