ManagerProduct.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\cxinv\model;
  3. use think\Model;
  4. class ManagerProduct extends Base{
  5. protected $schema=[
  6. 'id'=>'int',//主键,自动递增
  7. 'manager_id'=>'int',//导入数据记录id
  8. 'product_id'=>'int',//关联商品id
  9. 'type'=>'int', // 类型 1 增加 2减少
  10. 'num'=>'decimal',//关联数量
  11. 'status'=>'int',//状态 1 正常 0 删除
  12. 'apply_id'=>'int',//申请id
  13. 'apply_name'=>'string',//申请人
  14. 'create_time'=>'datetime',//时间
  15. ];
  16. protected $createTime='create_time';
  17. protected $updateTime=false;
  18. public function Product(){
  19. return $this->belongsTo(FinancialProducts::class,'product_id','id')->bind(['goodName','skuCode']);
  20. }
  21. public static function AddProduct($manager_id,$product){
  22. $add=(new ManagerProduct)->saveAll(array_map(function ($iitem) use ($manager_id){
  23. $iitem['manager_id']=$manager_id;
  24. return $iitem;
  25. },$product));
  26. if($add->isEmpty()) throw new \Exception('添加失败');
  27. }
  28. public static function onAfterWrite(Model $model) : void{
  29. $original=$model->getOrigin();
  30. $changed=$model->getChangedData();
  31. if(($model->status==1 || $changed['status']==1) && ($original['status']==0|| isset($original['status']))){
  32. $manager= FinancialManager::findOrEmpty($model->manager_id);
  33. if($manager->isEmpty()) throw new \Exception('数据不存在');
  34. $product=FinancialProducts::with(['ProductsCombind'=>["Products"]])->findOrEmpty($model->product_id);
  35. if($product->isEmpty()) throw new \Exception('商品不存在');
  36. $data[]=[
  37. "type"=>$model['type'],
  38. 'order_item_id'=>$model->manager_id,
  39. 'product_id'=>$model->product_id,
  40. 'num'=>$model->num,
  41. "fz_date"=>$manager->fz_date,
  42. 'unit_price'=>$product->unit_price,
  43. 'rate'=>$manager->inv_tax,
  44. 'apply_id'=>$manager->apply_id,
  45. 'apply_name'=>$manager->apply_name,
  46. ];
  47. if($product->is_combind==1 && !$product->ProductsCombind->isEmpty()){
  48. $product->ProductsCombind->each(function ($item) use (&$data,$manager,$model){
  49. $data[]=[
  50. "type"=>$model['type'],
  51. 'order_item_id'=>$model->manager_id,
  52. 'product_id'=>$item->child_id,
  53. 'fz_date'=>$manager->fz_date,
  54. 'num'=>bcmul($item->child_num,$model->num,8),
  55. 'unit_price'=>$item->products->unit_price??0,
  56. 'rate'=>$item->products->cat_tax,
  57. 'apply_id'=>$manager->apply_id,
  58. 'apply_name'=>$manager->apply_name,
  59. ];
  60. });
  61. }
  62. (new ProductOnlog)->saveAll($data);
  63. }
  64. }
  65. }