FinancialTz.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\cxinv\model;
  3. use think\Model;
  4. use think\model\concern\SoftDelete;
  5. class FinancialTz extends Base
  6. {
  7. use SoftDelete;
  8. protected $schema = [
  9. 'id' =>'bigint',//
  10. 'ktCode' =>'varchar',//计提编号
  11. 'manager_id' =>'bigint',//出库id
  12. 'status' =>'tinyint',//状态 1 待审核 2 已审核 3 已驳回 4 已取消
  13. 'num' =>'decimal',//数量
  14. 'out_fee' =>'decimal',//出口金额
  15. 'check_fee' =>'decimal',//调整金额
  16. 'is_checkOrder' =>'tinyint',//是否调整单
  17. 'fz_date' =>'varchar',//
  18. 'apply_id' =>'int',//创建人
  19. 'apply_name' =>'varchar',
  20. 'create_time' =>'datetime',//
  21. 'update_time' =>'datetime',//
  22. 'delete_time' =>'datetime',//
  23. ];
  24. protected $createTime = 'create_time';
  25. protected $updateTime = 'update_time';
  26. protected $deleteTime = 'delete_time';
  27. public function Manager(){
  28. return $this->hasOne(FinancialManager::class,'id','manager_id');
  29. }
  30. public function ProductTz(){
  31. return $this->hasMany(TzProduct::class,'ktCode','ktCode');
  32. }
  33. public function FinancialCheck()
  34. {
  35. return $this->hasOne(FinancialCheck::class,'code','ktCode');
  36. }
  37. public static function MakeInfo($product,$create)
  38. {
  39. foreach ($product as $k=>&$v) {
  40. $create['num']=bcadd($create['num'],$v['num'],8);
  41. $v['ktCode']=$create['ktCode'];
  42. $create['out_fee']=bcadd($create['out_fee'],bcmul($v['num'],$v['subunit_price'],15),15);
  43. }
  44. $kt = self::create($create);
  45. if($kt->isEmpty()) throw new \Exception('计提单生成失败');
  46. $key=(new TzProduct)->saveAll($product);
  47. if($key->isEmpty()) throw new \Exception('计提商品信息生成失败');
  48. }
  49. public static function onAfterUpdate(Model $model): void
  50. {
  51. $change = $model->getChangedData();
  52. $origin = $model->getOrigin();
  53. if (isset($change['status']) && $change['status'] == 2 && $origin['status'] == 1) {
  54. $product = TzProduct::with(['Product'=>['ProductsCombind','ProductStock']])->where(['ktCode' => $model['ktCode'], 'type' => 1])->select();
  55. $product->each(function ($item) use ($model) {
  56. if($item['is_combind']==0){
  57. ProductStock::AddSingleStock($item->Product, $item['num']);
  58. $log[] = [
  59. "type" => 5,
  60. 'order_item_id' => $model->id,
  61. 'product_id' => $item['product_id'],
  62. 'num' => $item['num'],
  63. "fz_date" => $model->fz_date,
  64. 'unit_price' => $item['unit_price'],
  65. 'rate' => $item->Product->cat_tax,
  66. 'apply_id' => $model->checkUid,
  67. 'apply_name' => $model->checkUname,
  68. ];
  69. }else{
  70. if($item->ProductsCombind->isEmpty()) throw new \Exception('子商品不存在');
  71. $ids=ProductStock::AddCombindStock( $item['product_id'], $item['num']);
  72. foreach ($ids as $id){
  73. $log[] = [
  74. "type" => 1,
  75. 'order_item_id' => $model->id,
  76. 'product_id' => $id['product_id'],
  77. 'num' =>$id['num'],
  78. "fz_date" => $model->fz_date,
  79. 'unit_price' => $id['unit_price'],
  80. 'rate' => $id['rate'],
  81. 'apply_id' => $model->checkUid,
  82. 'apply_name' => $model->checkUname,
  83. ];
  84. }
  85. }
  86. (new ProductOnlog)->saveAll($log);
  87. });
  88. if ($change['is_checkOrder'] == 1) {
  89. $check=[
  90. "checkCode"=>makeNo("CWTZ"),
  91. "type"=>1,
  92. "code"=>$model->ktCode,
  93. "itemid"=>$model->Manager->inv_item_id,
  94. "goodNo"=>$model->Manager->goodNo,
  95. 'goodType'=>$model->Manager->goodType,
  96. "goodName"=>$model->Manager->goodName,
  97. "spec"=>$model->Manager->inv_spec,
  98. "unit"=>$model->Manager->inv_unit,
  99. "company_code"=>$model->Manager->seller_code,
  100. "company_name"=>$model->Manager->seller_name,
  101. "num"=>0, // 出库调整金额
  102. "price"=>$model->Manager->inv_price,
  103. "subprice"=>$model->Manager->inv_subprice,
  104. "check_fee"=>$model->check_fee,
  105. "check_uid"=>$model->checkUid,
  106. "fz_date"=> $model->Manager->fz_date,
  107. "check_uname"=>$model->checkUname,
  108. ];
  109. FinancialCheck::create($check);
  110. }
  111. }
  112. }
  113. }