123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace app\cxinv\model;
- use think\facade\Log;
- use think\Model;use think\model\concern\SoftDelete;
- class InvoiceOrder extends Base{
- use SoftDelete;
- protected $createTime='createTime';
- protected $updateTime='updateTime';
- protected $autoWriteTimestamp=true;
- protected $deleteTime='delete_time';
- protected $append=["merge_code","short_name","cat_code","cat_name","tax","inv_good_name"];
- public function ItemInfo(){
- return $this->hasOne('InvoiceItem','itemId','id');
- }
- public function getMergeCodeAttr($value,$data){
- return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("merge_code","");
- }
- public function getShortNameAttr($value,$data){
- return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("short_name","");
- }
- public function getCatCodeAttr($value,$data){
- return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("cat_code","");
- }
- public function getCatNameAttr($value,$data){
- return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("cat_name","");
- }
- public function getTaxAttr($value,$data){
- return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("tax","");
- }
- public function getInvGoodNameAttr($value,$data){
- return OrderCategory::where(['code'=>$data['code'],'spuCode'=>$data['spuCode']])->value("inv_good_name","");
- }
- public static function getNumAttr($val){
- return strval(floatval($val));
- }
- public static function setDiffInfoAttr($val){
- return json_encode($val,JSON_UNESCAPED_UNICODE);
- }
- public static function getDiffInfoAttr($val){
- return json_decode($val,true);
- }
- public static function getOrderInfo($code,$orderCode,$type){
- $payNo = PayInvoice::where(['hpNo'=>$code])->value("payNo","");
- $info = PayInfo::alias('a')
- ->join('cgd_info b','a.cgdNo=b.sequenceNo','left')
- ->join('order_category c','b.sequenceNo=c.code and b.goodNo=c.spuCode and c.order_type=2','left')
- ->where([['a.status','=',1],['a.is_del','=',0],['a.payNo',"=",$payNo ],['a.cgdNo',"=",$orderCode]])
- ->field('b.sequenceNo,b.goodNo,b.goodName,(b.goodNum-thNum) as goodNum,b.goodPrice,b.totalPrice,b.ainv_fee,b.winv_fee,b.apay_fee,b.wpay_fee,
- c.merge_code,c.cat_code,c.cat_name,c.short_name,c.tax,c.inv_good_name')
- ->findOrEmpty();
- return $info;
- }
- //回票流程结束未认证 状态3 退票认证结束 状态4
- public static function onAfterWrite( Model $model){
- $id= $model->id;
- $change = $model->getChangedData();
- Log::info('InvoiceOrder跟前数据:'.json_encode($model->id,JSON_UNESCAPED_UNICODE));
- $info = InvoiceOrder::where(['id'=>$id])->findOrEmpty();
- Log::info('InvoiceOrder跟后数据:'.json_encode($info->toArray(),JSON_UNESCAPED_UNICODE));
- if($info->isEmpty())return ;
- if(empty($change)&&in_array($model->status,[1,2])) self::orderIn($info->code,bcsub($info->total_amount,$info->balance_amount,2));
- if(!empty($change)&&isset($change['status'])){
- if($change['status']==3) self::orderOut($info->code,bcsub($info->total_amount,$info->balance_amount,2));
- if($change['status']==4) self::orderCancel($info->code,bcsub($info->total_amount,$info->balance_amount,2));
- }
- Log::info('InvoiceOrder跟后数据:'.InvoiceOrder::getLastsql());
- }
- public static function onBeforeDelete($model){
- $id=$model->id;
- Log::info('InvoiceOrder数据:onBeforeDelete:'.json_encode($model->id,JSON_UNESCAPED_UNICODE));
- $info = InvoiceOrder::where(['id'=>$id])->findOrEmpty();
- Log::info('InvoiceOrder数据:onBeforeDelete'.json_encode($info->toArray(),JSON_UNESCAPED_UNICODE));
- if($info->isEmpty())return ;
- if(in_array($info->status,[1,2])) self::orderOut($info->code,bcsub($info->total_amount,$info->balance_amount,2));
- Log::info('InvoiceOrder数据:onBeforeDelete:'.InvoiceOrder::getLastsql());
- }
- //订单关联初始化金额
- public static function orderIn($code,$fee){
- $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
- Log::info('InvoiceOrder跟后数据orderIn:cgd:'. $cgdinfo->winv_fee."----".$fee);
- if(!$cgdinfo->isEmpty() && $cgdinfo->winv_fee>=strval($fee)){
- $cgdinfo->winv_fee = bcsub($cgdinfo->winv_fee,strval($fee),2);
- $cgdinfo->inv_status=2;
- $cgdinfo->save();
- }else throw new \Exception("关联金额{$fee}大于采购单{$code}未开票金额{$cgdinfo->winv_fee}");
- $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
- if(!$payinfo->isEmpty()){
- $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
- Log::info('InvoiceOrder跟后数据orderIn:pay' . $pay->winv_fee . '----' . $fee);
- if(!$pay->isEmpty() && $pay->winv_fee>=strval($fee)){
- $pay->winv_fee=bcsub($pay->winv_fee,strval($fee),2);
- $pay->inv_fee = bcadd($pay->inv_fee,strval($fee),2);
- $pay->inv_status=2;
- $pay->save();
- }else throw new \Exception("关联金额{$fee}大于对账单{$payinfo->payNo}未开票金额{$pay->winv_fee}");
- }
- }
- //流程未结束驳回中断
- public static function orderOut($code,$fee){
- $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
- if(!$cgdinfo->isEmpty()){
- $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,strval($fee),2);
- $cgdinfo->save();
- }
- $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
- if(!$payinfo->isEmpty()){
- $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
- if(!$pay->isEmpty() && $pay->inv_fee>=strval($fee)){
- $pay->winv_fee=bcadd($pay->winv_fee,strval($fee),2);
- $pay->inv_fee = bcsub($pay->inv_fee,strval($fee),2);
- $pay->inv_status=$pay->ainv_fee==0 && $pay->inv_fee==0?1:2;
- $pay->save();
- }else throw new \Exception("关联金额{$fee}大于对账单{$payinfo->payNo}开票中金额{$pay->winv_fee}");
- }
- }
- public static function orderFinish($code,$fee){
- $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
- if(!$cgdinfo->isEmpty()){
- $cgdinfo->ainv_fee = bcadd($cgdinfo->ainv_fee,strval($fee),2);
- $cgdinfo->inv_status = $cgdinfo->ainv_fee==$cgdinfo->totalPrice-$cgdinfo->inv_tag_fe&& $cgdinfo->winv_fee==0 ?3:2;
- $cgdinfo->save();
- }
- $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
- if(!$payinfo->isEmpty()){
- $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
- if(!$pay->isEmpty() && $pay->inv_fee>=strval($fee)){
- $pay->ainv_fee=bcadd($pay->ainv_fee,strval($fee),2);
- $pay->inv_fee = bcsub($pay->inv_fee,strval($fee),2);
- $pay->inv_status = $pay->winv_fee==0 && $pay->inv_fee==0?3:2;
- $pay->save();
- }else throw new \Exception("关联金额{$fee}大于对账单{$payinfo->payNo}开票中金额{$pay->winv_fee}");
- }
- }
- public static function orderCancel($code,$fee){
- $cgdinfo = CgdInfo::where(['sequenceNo'=>$code])->findOrEmpty();
- if(!$cgdinfo->isEmpty()){
- $cgdinfo->winv_fee = bcadd($cgdinfo->winv_fee,strval($fee),2);
- $cgdinfo->ainv_fee = bcsub($cgdinfo->ainv_fee,strval($fee),2);
- $cgdinfo->inv_status = $cgdinfo->ainv_fee==0 && $cgdinfo->winv_fee==$cgdinfo->totalPrice-$cgdinfo->inv_tag_fee?1:2;
- $cgdinfo->save();
- }
- $payinfo = PayInfo::where(['cgdNo'=>$code,'status'=>1,'is_del'=>0])->findOrEmpty();
- if(!$payinfo->isEmpty()){
- $pay= Pay::where(['payNo'=>$payinfo->payNo,'status'=>2])->findOrEmpty();
- if(!$pay->isEmpty()){
- $pay->winv_fee=bcadd($pay->winv_fee,strval($fee),2);
- $pay->ainv_fee = bcsub($pay->ainv_fee,strval($fee),2);
- $pay->inv_status = $pay->ainv_fee==0 && $pay->inv_fee==0?1:2;
- $pay->save();
- }
- }
- }
- public static function CheckOrder($code,&$data){
- $InvoiceOrder = InvoiceOrder::with(['ItemInfo'])->where(['code'=>$code,'status'=>[1,2]])->select();
- if(!$InvoiceOrder->isEmpty()){
- $hpinfo=[];
- foreach ($InvoiceOrder as $item){
- if(isset($hpinfo[$item->ItemInfo->invoiceCode]))continue;
- $hpinfo[$item->ItemInfo->invoiceCode] = $item->ItemInfo->invoiceCode;
- $hp = PayInvoice::with(['invoice'])->where(['hpNo'=>$item->ItemInfo->invoiceCode])->findOrEmpty();
- $data[]=['type'=>'发票池关联采购单','username'=> $hp['apply_name'],'orderCode'=>$hp['hpNo'],'companyName'=>$hp['companyName']];
- }
- }
- }
- }
|