FinancialTz.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 static function MakeInfo($product,$create)
  34. {
  35. foreach ($product as $k=>&$v) {
  36. $create['num']=bcadd($create['num'],$v['num'],8);
  37. $v['ktCode']=$create['ktCode'];
  38. $create['out_fee']=bcadd($create['out_fee'],bcmul($v['num'],$v['subunit_price'],15),15);
  39. }
  40. $kt = self::create($create);
  41. if($kt->isEmpty()) throw new \Exception('计提单生成失败');
  42. $key=(new TzProduct)->saveAll($product);
  43. if($key->isEmpty()) throw new \Exception('计提商品信息生成失败');
  44. }
  45. public static function onAfterUpdate(Model $model): void
  46. {
  47. $change = $model->getChangedData();
  48. $origin = $model->getOrigin();
  49. if (isset($change['status']) && $change['status'] == 2 && $origin['status'] == 1) {
  50. $product = TzProduct::with(['Product'=>['ProductsCombind','ProductStock']])->where(['ktCode' => $model['ktCode'], 'type' => 1])->select();
  51. $product->each(function ($item) use ($model) {
  52. if($item['is_combind']==0){
  53. ProductStock::AddSingleStock($item->Product, $item['num']);
  54. $log[] = [
  55. "type" => 5,
  56. 'order_item_id' => $model->id,
  57. 'product_id' => $item['product_id'],
  58. 'num' => $item['num'],
  59. "fz_date" => $model->fz_date,
  60. 'unit_price' => $item['unit_price'],
  61. 'rate' => $item->Product->cat_tax,
  62. 'apply_id' => $model->checkUid,
  63. 'apply_name' => $model->checkUname,
  64. ];
  65. }else{
  66. if($item->ProductsCombind->isEmpty()) throw new \Exception('子商品不存在');
  67. $ids=ProductStock::AddCombindStock( $item['product_id'], $item['num']);
  68. foreach ($ids as $id){
  69. $log[] = [
  70. "type" => 1,
  71. 'order_item_id' => $model->id,
  72. 'product_id' => $id['product_id'],
  73. 'num' =>$id['num'],
  74. "fz_date" => $model->fz_date,
  75. 'unit_price' => $id['unit_price'],
  76. 'rate' => $id['rate'],
  77. 'apply_id' => $model->checkUid,
  78. 'apply_name' => $model->checkUname,
  79. ];
  80. }
  81. }
  82. (new ProductOnlog)->saveAll($log);
  83. });
  84. if ($change['is_checkOrder'] == 1) {
  85. $check=[
  86. "checkCode"=>makeNo("CWTZ"),
  87. "type"=>1,
  88. "code"=>$model->ktCode,
  89. "itemid"=>$model->Manager->inv_item_id,
  90. "goodNo"=>$model->Manager->goodNo,
  91. 'goodType'=>$model->Manager->goodType,
  92. "goodName"=>$model->Manager->goodName,
  93. "spec"=>$model->Manager->inv_spec,
  94. "unit"=>$model->Manager->inv_unit,
  95. "company_code"=>$model->Manager->seller_code,
  96. "company_name"=>$model->Manager->seller_name,
  97. "num"=>0, // 出库调整金额
  98. "price"=>$model->Manager->inv_price,
  99. "subprice"=>$model->Manager->inv_subprice,
  100. "check_fee"=>$model->check_fee,
  101. "check_uid"=>$model->checkUid,
  102. "fz_date"=> $model->Manager->fz_date,
  103. "check_uname"=>$model->checkUname,
  104. ];
  105. FinancialCheck::create($check);
  106. }
  107. }
  108. }
  109. }