ManagerProduct.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 onAfterInsert(Model $model) : void{
  29. $manager= FinancialManager::findOrEmpty($model->manager_id);
  30. if($manager->isEmpty()) throw new \Exception('数据不存在');
  31. $product=FinancialProducts::with(['ProductsCombind'=>["Products"]])->findOrEmpty($model->product_id);
  32. if($product->isEmpty()) throw new \Exception('商品不存在');
  33. $data[]=[
  34. "type"=>$model['type'],
  35. 'order_item_id'=>$model->manager_id,
  36. 'product_id'=>$model->product_id,
  37. 'num'=>$model->num,
  38. "fz_date"=>$manager->fz_date,
  39. 'unit_price'=>$product->unit_price,
  40. 'rate'=>$manager->inv_tax,
  41. 'apply_id'=>$manager->apply_id,
  42. 'apply_name'=>$manager->apply_name,
  43. ];
  44. if($product->is_combind==1 && !$product->ProductsCombind->isEmpty()){
  45. $product->ProductsCombind->each(function ($item) use (&$data,$manager,$model){
  46. $data[]=[
  47. "type"=>$model['type'],
  48. 'order_item_id'=>$model->manager_id,
  49. 'product_id'=>$item->child_id,
  50. 'fz_date'=>$manager->fz_date,
  51. 'num'=>bcmul($item->child_num,$model->num,8),
  52. 'unit_price'=>$item->products->unit_price??0,
  53. 'rate'=>$item->products->cat_tax,
  54. 'apply_id'=>$manager->apply_id,
  55. 'apply_name'=>$manager->apply_name,
  56. ];
  57. });
  58. }
  59. (new ProductOnlog)->saveAll($data);
  60. }
  61. }