FinancialTz.php 4.4 KB

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