ManagerProduct.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. 'create_time'=>'datetime',//时间
  12. ];
  13. protected $createTime='create_time';
  14. protected $updateTime=false;
  15. public function Product(){
  16. return $this->belongsTo(FinancialProducts::class,'product_id','id')->bind(['goodName','skuCode']);
  17. }
  18. public static function AddProduct($manager_id,$product){
  19. $add=(new ManagerProduct)->saveAll(array_map(function ($iitem) use ($manager_id){
  20. $iitem['manager_id']=$manager_id;
  21. return $iitem;
  22. },$product));
  23. if($add->isEmpty()) throw new \Exception('添加失败');
  24. }
  25. public static function onAfterInsert(Model $model) : void{
  26. $manager= FinancialManager::findOrEmpty($model->manager_id);
  27. if($manager->isEmpty()) throw new \Exception('数据不存在');
  28. $product=FinancialProducts::with(['ProductsCombind'=>["Products"]])->findOrEmpty($model->product_id);
  29. if($product->isEmpty()) throw new \Exception('商品不存在');
  30. $data[]=[
  31. "type"=>$model['type'],
  32. 'order_item_id'=>$model->manager_id,
  33. 'product_id'=>$model->product_id,
  34. 'num'=>$model->num,
  35. "fz_date"=>$manager->fz_date,
  36. 'unit_price'=>$product->unit_price,
  37. 'rate'=>$manager->inv_tax,
  38. 'apply_id'=>$manager->apply_id,
  39. 'apply_name'=>$manager->apply_name,
  40. ];
  41. if($product->is_combind==1 && !$product->ProductsCombind->isEmpty()){
  42. $product->ProductsCombind->each(function ($item) use (&$data,$manager,$model){
  43. $data[]=[
  44. "type"=>$model['type'],
  45. 'order_item_id'=>$model->manager_id,
  46. 'product_id'=>$item->child_id,
  47. 'fz_date'=>$manager->fz_date,
  48. 'num'=>bcmul($item->child_num,$model->num,8),
  49. 'unit_price'=>$item->products->unit_price??0,
  50. 'rate'=>$item->products->cat_tax,
  51. 'apply_id'=>$manager->apply_id,
  52. 'apply_name'=>$manager->apply_name,
  53. ];
  54. });
  55. }
  56. (new ProductOnlog)->saveAll($data);
  57. }
  58. }