TagLog.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace app\cxinv\model;
  3. /**
  4. * Class TagLog
  5. * @package app\cxinv\model
  6. */class TagLog extends Base
  7. {
  8. //设置字段信息
  9. protected $schema = [
  10. 'id' =>'bigint',//
  11. 'code' =>'varchar',//单号
  12. 'tag_id' =>'int',//标签id
  13. 'creater' =>'varchar',//创建人
  14. 'createrid' =>'int',//创建人id
  15. 'tag_fee' =>'decimal',//标签金额
  16. 'tag_img' =>'varchar',//标签图片
  17. 'status' =>'tinyint',//0/1 状态 有效无效
  18. 'addtime' =>'datetime',//
  19. 'updatetime' =>'datetime',//
  20. 'tag_remark' =>'varchar',//备注
  21. ];
  22. protected $createTime = 'addtime';
  23. protected $updateTime = 'updatetime';
  24. public function TagInfo(){
  25. return $this->belongsTo(OrderTag::class,'tag_id','id')->bind(['type','tag_name']);
  26. }
  27. public function OrderInfo(){
  28. return $this->belongsTo(QrdInfo::class,'code','sequenceNo')
  29. ->bind(['companyNo','companyName','customerNo','customerName',]);
  30. }
  31. public function PayInfo(){
  32. return $this->belongsTo(Pay::class,'code','payNo')
  33. ->bind(['companyNo','companyName','supplierNo','supplierName']);
  34. }
  35. public function CgdInfo(){
  36. return $this->belongsTo(CgdInfo::class,'code','sequenceNo')
  37. ->bind(['companyNo','companyName','supplierNo','supplierName']);
  38. }
  39. /** 添加标签
  40. * @param $type 标签类型 1对账单付款2对账单回票3销售回款4销售开票5采购单付款 6采购单回票
  41. * @param $code 单编号
  42. * @param $total_fee 标签金额
  43. * @return \think\response\Json|void
  44. * @throws \think\db\exception\DbException
  45. */
  46. public static function CheckOrderInfo($code,$total_fee,$type){
  47. switch($type){
  48. case 1:
  49. case 2:
  50. self::payTag($type,$code,$total_fee);
  51. break;
  52. case 3:
  53. case 4:
  54. self::qrdTag($type,$code,$total_fee);
  55. break;
  56. case 5:
  57. case 6:
  58. self::cgdTag($type,$code,$total_fee);
  59. break;
  60. default:
  61. throw new \Exception('标签类型错误');
  62. }
  63. }
  64. /** 解除标签
  65. * @param $type
  66. * @param $code
  67. * @return int|\think\response\Json|void
  68. * @throws \think\db\exception\DbException
  69. */
  70. public static function ReturnTag($type,$code){
  71. $fee=0;
  72. switch($type){
  73. case 1:
  74. case 2:
  75. $fee=self::payReTag($type,$code);
  76. break;
  77. case 3:
  78. case 4:
  79. $fee=self::qrdReTag($type,$code);
  80. break;
  81. case 5:
  82. case 6:
  83. $fee=self::cgdReTag($type,$code);
  84. break;
  85. default:
  86. throw new \Exception('标签类型错误');
  87. }
  88. return $fee;
  89. }
  90. /**# 对账单打标签判断 9/25后对账单不允许打标签 采购单非通用类型对账单
  91. * @param $tagId 标签类型 1 付款2回票
  92. * @param $code 对账单编号
  93. * @param $total_fee 标签金额
  94. * @return \think\response\Json|void
  95. * @throws \think\db\exception\DbException
  96. */
  97. private static function payTag($type,$code,$total_fee){
  98. $pay=Pay::where(['payNo'=>$code,'is_del'=>0])->findOrEmpty();
  99. if($pay->isEmpty())throw new \Exception('未找到对账单数据');
  100. if($pay->is_comon==0 && $pay->pay_type==1 && strtotime($pay->addtime)>=strtotime(env("tag.endtime"))) throw new \Exception('非通用采购对账单不可加标签');
  101. if($type==1){
  102. if($pay->wpay_fee<$total_fee)throw new \Exception('对账单未付金额不足');
  103. $pay->wpay_fee=$pay->wpay_fee-$total_fee;
  104. $pay->pay_status = $pay->wpay_fee==0 &&$pay->pay_fee==0 ? 3:($pay->apay_fee==0?1:2);
  105. $pay->pay_tag_fee+=$total_fee;
  106. $pay->pay_tag= 1;
  107. }
  108. if($type==2){
  109. if($pay->winv_fee<$total_fee)throw new \Exception('对账单未开票金额不足');
  110. $pay->winv_fee=$pay->winv_fee-$total_fee;
  111. $pay->inv_status = $pay->winv_fee==0 &&$pay->inv_fee==0 ? 3:($pay->ainv_fee==0?1:2);
  112. $pay->inv_tag_fee+=$total_fee;
  113. $pay->inv_tag =1;
  114. }
  115. $resulr= $pay->save();
  116. if($resulr==false) throw new \Exception('对账单更新失败');
  117. }
  118. /**解除标签
  119. * @param $tagId 标签类型 1 付款2回票
  120. * @param $code 对账单编号
  121. * @param $total_fee 标签金额
  122. * @return \think\response\Json|void
  123. * @throws \think\db\exception\DbException
  124. */
  125. private static function payReTag($type,$code){
  126. $pay=Pay::where(['payNo'=>$code,'is_del'=>0])->findOrEmpty();
  127. if($pay->isEmpty())throw new \Exception('未找到对账单数据');
  128. $fee=0;
  129. if($type==1){
  130. if($pay->pay_tag_fee<0)throw new \Exception('对账单付款标签金额不足');
  131. $fee=$pay->pay_tag_fee;
  132. $pay->wpay_fee=$pay->wpay_fee+$pay->pay_tag_fee;
  133. $pay->pay_tag_fee=0;
  134. $pay->pay_status = $pay->wpay_fee==0 && $pay->pay_fee==0?3:($pay->apay_fee==0?1:2);
  135. $pay->pay_tag=0;
  136. }
  137. if($type==2){
  138. if($pay->inv_tag_fee<0)throw new \Exception('对账单回票标签金额不足');
  139. $fee=$pay->inv_tag_fee;
  140. $pay->winv_fee=$pay->winv_fee+$pay->inv_tag_fee;
  141. $pay->inv_tag_fee=0;
  142. $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0?3:($pay->ainv_fee==0?1:2);
  143. $pay->inv_tag=0;
  144. }
  145. $resulr= $pay->save();
  146. if($resulr==false)throw new \Exception('对账单更新失败');
  147. return $fee;
  148. }
  149. /**
  150. * @param $tagId 标签类型 3 回款4开票
  151. * @param $code 销售单编号
  152. * @param $total_fee 标签金额
  153. * @return \think\response\Json|void
  154. * @throws \think\db\exception\DbException
  155. */
  156. private static function qrdTag($type,$code,$total_fee){
  157. $qrd=QrdInfo::where(['sequenceNo'=>$code,'is_del'=>0])->findOrEmpty();
  158. if($qrd->isEmpty())throw new \Exception('未找到销售单数据');
  159. if($type==3){
  160. if($qrd->wpay_fee<$total_fee){
  161. throw new \Exception('销售单未付金额不足');
  162. }
  163. $qrd->wpay_fee=$qrd->wpay_fee-$total_fee;
  164. $qrd->pay_status = $qrd->wpay_fee==0 &&$qrd->pay_fee==0 ? 3:($qrd->apay_fee==0?1:2);
  165. $qrd->pay_tag_fee+=$total_fee;
  166. $qrd->pay_tag= 1;
  167. }
  168. if($type==4){
  169. if($qrd->winv_fee<$total_fee)throw new \Exception('对账单未开票金额不足');
  170. $qrd->winv_fee=$qrd->winv_fee-$total_fee;
  171. $qrd->inv_status = $qrd->winv_fee==0 &&$qrd->inv_fee==0 ? 3:($qrd->ainv_fee==0?1:2);
  172. $qrd->inv_tag_fee+=$total_fee;
  173. $qrd->inv_tag =1;
  174. }
  175. $resulr=$qrd->save();
  176. if($resulr==false) throw new \Exception('标签添加失败');
  177. }
  178. /** 解除标签销售单
  179. * @param $tagId 标签类型 3 回款4开票
  180. * @param $code 销售单编号
  181. * @param $total_fee 标签金额
  182. * @return \think\response\Json|void
  183. * @throws \think\db\exception\DbException
  184. */
  185. private static function qrdReTag($tagId,$code){
  186. $qrd=QrdInfo::where(['sequenceNo'=>$code,'is_del'=>0])->findOrEmpty();
  187. if($qrd->isEmpty())throw new \Exception('未找到销售单数据');
  188. $fee =0 ;
  189. if($tagId==3){
  190. if($qrd->pay_tag_fee<0)throw new \Exception('销售单回款标签金额不足');
  191. $fee =$qrd->pay_tag_fee ;
  192. $qrd->wpay_fee=$qrd->wpay_fee+$qrd->pay_tag_fee;
  193. $qrd->pay_tag_fee=0;
  194. $qrd->pay_status = $qrd->wpay_fee==0 && $qrd->pay_fee==0?3:($qrd->apay_fee==0?1:2);
  195. $qrd->pay_tag=0;
  196. }
  197. if($tagId==4){
  198. if($qrd->inv_tag_fee<0)throw new \Exception('销售单开票标签金额不足');
  199. $fee =$qrd->inv_tag_fee ;
  200. $qrd->winv_fee=$qrd->winv_fee+$qrd->inv_tag_fee;
  201. $qrd->inv_tag_fee=0;
  202. $qrd->inv_status = $qrd->winv_fee==0 && $qrd->inv_fee==0?3:($qrd->ainv_fee==0?1:2);
  203. $qrd->inv_tag=0;
  204. }
  205. $resulr= $qrd->save();
  206. if($resulr==false)throw new \Exception('销售单更新失败');
  207. return $fee;
  208. }
  209. /**
  210. * @param $tagId 标签类型 3 回款4开票
  211. * @param $code 销售单编号
  212. * @param $total_fee 标签金额
  213. * @return \think\response\Json|void
  214. * @throws \think\db\exception\DbException
  215. */
  216. private static function cgdTag($type,$code,$total_fee){
  217. $cgd=CgdInfo::where(['sequenceNo'=>$code,'is_del'=>0])->findOrEmpty();
  218. if($cgd->isEmpty())throw new \Exception('未找到采购单数据');
  219. if($cgd->is_comon==1) throw new \Exception('该采购单为通用采购单,不允许添加标签');
  220. $payInfo = PayInfo::where(["cgdNo"=>$code,"status"=>1,"is_del"=>0])->findOrEmpty();
  221. if(!$payInfo->isEmpty()){
  222. $pay= Pay::where(["payNo"=>$payInfo->payNo,"is_del"=>0,"status"=>[0,1,2]])->findOrEmpty();
  223. }
  224. if($type==5){
  225. if($cgd->wpay_fee<$total_fee)throw new \Exception('采购单未付金额不足');
  226. $cgd->wpay_fee=$cgd->wpay_fee-$total_fee;
  227. $cgd->pay_status = $cgd->wpay_fee==0 ? 3:($cgd->apay_fee==0?1:2);
  228. $cgd->pay_tag_fee+=$total_fee;
  229. $cgd->pay_tag= 1;
  230. if(!$payInfo->isEmpty()){
  231. $payInfo->wpay_fee=$payInfo->wpay_fee-$total_fee;
  232. if(!$pay->isEmpty()){
  233. if ($pay->wpay_fee<$total_fee) throw new \Exception('对账单未付金额不足');
  234. $pay->wpay_fee=$pay->wpay_fee-$total_fee;
  235. $pay->pay_status = $pay->wpay_fee==0 &&$pay->pay_fee==0 ? 3:($pay->apay_fee==0?1:2);
  236. $payInfo->save();
  237. $pay->save();
  238. }
  239. }
  240. }
  241. if($type==6){
  242. if($cgd->winv_fee<$total_fee)throw new \Exception('对账单未开票金额不足');
  243. $cgd->winv_fee=$cgd->winv_fee-$total_fee;
  244. $cgd->inv_status = $cgd->winv_fee==0? 3:($cgd->ainv_fee==0?1:2);
  245. $cgd->inv_tag_fee+=$total_fee;
  246. $cgd->inv_tag =1;
  247. if(!$payInfo->isEmpty()){
  248. $payInfo->winv_fee=$payInfo->winv_fee-$total_fee;
  249. if(!$pay->isEmpty()){
  250. if ($pay->winv_fee<$total_fee) throw new \Exception('对账单未开票金额不足');
  251. $pay->winv_fee=$pay->winv_fee-$total_fee;
  252. $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0? 3:($pay->ainv_fee==0?1:2);
  253. $payInfo->save();
  254. $pay->save();
  255. }
  256. }
  257. }
  258. $resulr= $cgd->save();
  259. if($resulr==false) throw new \Exception('标签添加失败');
  260. }
  261. /** 解除标签销售单
  262. * @param $tagId 标签类型 3 回款4开票
  263. * @param $code 销售单编号
  264. * @param $total_fee 标签金额
  265. * @return \think\response\Json|void
  266. * @throws \think\db\exception\DbException
  267. */
  268. private static function cgdReTag($type,$code){
  269. $cgd=CgdInfo::where(['sequenceNo'=>$code,'is_del'=>0])->findOrEmpty();
  270. if($cgd->isEmpty())throw new \Exception('未找到采购单数据');
  271. $payInfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
  272. if(!$payInfo->isEmpty()){
  273. $pay= Pay::where(['payNo'=>$payInfo->payNo,'is_del'=>0,'status'=>[0,1,2]])->findOrEmpty();
  274. }
  275. $fee =0 ;
  276. if($type==5){
  277. if($cgd->pay_tag_fee<0)throw new \Exception('采购单回款标签金额不足');
  278. $fee =$cgd->pay_tag_fee ;
  279. $cgd->wpay_fee=$cgd->wpay_fee+$cgd->pay_tag_fee;
  280. $cgd->pay_tag_fee=0;
  281. $cgd->pay_status = $cgd->wpay_fee==0?3:($cgd->apay_fee==0?1:2);
  282. $cgd->pay_tag=0;
  283. if(!$payInfo->isEmpty()){
  284. $payInfo->wpay_fee=$payInfo->wpay_fee+$fee;
  285. if(!$pay->isEmpty()){
  286. $pay->wpay_fee=$pay->wpay_fee+$fee;
  287. $pay->pay_status = $pay->wpay_fee==0 &&$pay->pay_fee==0 ? 3:($pay->apay_fee==0?1:2);
  288. $payInfo->save();
  289. $pay->save();
  290. }
  291. }
  292. }
  293. if($type==6){
  294. if($cgd->inv_tag_fee<0)throw new \Exception('采购单开票标签金额不足');
  295. $fee =$cgd->inv_tag_fee ;
  296. $cgd->winv_fee=$cgd->winv_fee+$cgd->inv_tag_fee;
  297. $cgd->inv_tag_fee=0;
  298. $cgd->inv_status = $cgd->winv_fee==0?3:($cgd->ainv_fee==0?1:2);
  299. $cgd->inv_tag=0;
  300. if(!$payInfo->isEmpty()){
  301. $payInfo->winv_fee=$payInfo->winv_fee+$fee;
  302. if(!$pay->isEmpty()){
  303. $pay->winv_fee=$pay->winv_fee+$fee;
  304. $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0? 3:($pay->ainv_fee==0?1:2);
  305. $payInfo->save();
  306. $pay->save();
  307. }
  308. }
  309. }
  310. $resulr= $cgd->save();
  311. if($resulr==false)throw new \Exception('采购单更新失败');
  312. return $fee;
  313. }
  314. }