FinancialTz.php 5.0 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',//状态
  13. 'num' =>'decimal',//数量
  14. 'out_fee' =>'decimal',//出口金额
  15. 'check_fee' =>'decimal',//调整金额
  16. 'is_checkOrder' =>'tinyint',//是否调整单
  17. 'apply_id' =>'int',//创建人
  18. 'apply_name' =>'varchar',
  19. 'create_time' =>'datetime',//
  20. 'update_time' =>'datetime',//
  21. 'delete_time' =>'datetime',//
  22. ];
  23. protected $createTime = 'create_time';
  24. protected $updateTime = 'update_time';
  25. protected $deleteTime = 'delete_time';
  26. public function Manager(){
  27. return $this->hasOne(FinancialManager::class,'id','manager_id');
  28. }
  29. public function ProductTz(){
  30. return $this->hasMany(TzProduct::class,'ktCode','ktCode');
  31. }
  32. public static function MakeInfo($product,$create)
  33. {
  34. foreach ($product as $k=>&$v) {
  35. $create['num']=bcadd($create['num'],$v['num'],8);
  36. $v['ktCode']=$create['ktCode'];
  37. $create['out_fee']=bcadd($create['out_fee'],bcmul($v['num'],$v['subunit_price'],15),15);
  38. }
  39. $kt = self::create($create);
  40. if($kt->isEmpty()) throw new \Exception('计提单生成失败');
  41. $key=(new TzProduct)->saveAll($product);
  42. if($key->isEmpty()) throw new \Exception('计提商品信息生成失败');
  43. }
  44. public static function onAfterUpdate(Model $model): void
  45. {
  46. $change = $model->getChangedData();
  47. $origin = $model->getOrigin();
  48. if (isset($change['status']) && $change['status'] == 2 && $origin['status'] == 1) {
  49. $product = TzProduct::with(['Product'=>['ProductsCombind']])->where(['ktCode' => $model['ktCode'], 'type' => 1])->select();
  50. $product->each(function ($item) use ($model) {
  51. if($item['is_combind']==0){
  52. ProductStock::where('product_id', $item['product_id'])->inc('residue_stock', $item['num'])->save();
  53. $log[] = [
  54. "type" => 1,
  55. 'order_item_id' => $model->manager_id,
  56. 'product_id' => $item['product_id'],
  57. 'num' => $item['num'],
  58. "fz_date" => $model->Manager->fz_date,
  59. 'unit_price' => $item['unit_price'],
  60. 'rate' => $item->Product->cat_tax,
  61. 'apply_id' => $model->checkUid,
  62. 'apply_name' => $model->checkUname,
  63. ];
  64. }else{
  65. if($item->ProductsCombind->isEmpty()) throw new \Exception('子商品不存在');
  66. $item->ProductsCombind->each(function ($child) use (&$log, $model, $item) {
  67. $childInfo= $child->products;
  68. ProductStock::where('product_id', $child['child_id'])->inc('residue_stock', bcmul($child['child_num'], $item['num'], 8))->save();
  69. $log[] = [
  70. "type" => 1,
  71. 'order_item_id' => $model->manager_id,
  72. 'product_id' => $child['child_id'],
  73. 'num' => bcmul($child['child_num'], $item['num'], 8),
  74. "fz_date" => $model->Manager->fz_date,
  75. 'unit_price' => $childInfo->unit_price,
  76. 'rate' => $childInfo->cat_tax,
  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. }