SaleCgdPrice.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\model;
  4. use think\Exception;use think\facade\Db;use think\Model;use think\model\concern\SoftDelete;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class SaleCgdPrice extends Model
  9. {
  10. use SoftDelete;
  11. protected $createTime="addtime";
  12. protected $updateTime="updatetime";
  13. protected $deleteTime="delete_time";
  14. /** 计算销售单关联备库采购单数量获取成本单价计算
  15. * @param $orderId
  16. * @throws \think\Exception
  17. */
  18. public static function GetPrice($orderId){
  19. return ;
  20. $order = Sale::findOrEmpty($orderId);
  21. if($order->isEmpty()) throw new Exception("未找到订单数据");
  22. if($order->origin_price==0){
  23. $temp = CgdPrice::checkCgdPrice($order);
  24. }else{
  25. $temp = CgdPrice::checkCgdNum($order);
  26. }
  27. if(!empty($temp))self::insertAll($temp);
  28. }
  29. public static function RePrice($orderId,$num){
  30. return ;
  31. $order = Sale::findOrEmpty($orderId);
  32. if($order->isEmpty()) throw new Exception('未找到订单数据');
  33. if($order->is_activity==0){
  34. while ($num>0){
  35. $saleCgd=self::where(['orderCode'=>$order->orderCode])->order("id","desc")->findOrEmpty();
  36. if($saleCgd->isEmpty())throw new Exception('未找到订单成本数据');
  37. $tempNum=0;
  38. if($saleCgd->num<=$num){
  39. $tempNum=$saleCgd->num;
  40. $num -=$saleCgd->num;
  41. $saleCgd->delete();
  42. }else{
  43. $tempNum=$num;
  44. $saleCgd->num-=$num;
  45. $num=0;
  46. $saleCgd->save();
  47. }
  48. if($saleCgd->cgd_price_id!=0){
  49. $cgdinfo = CgdPrice::findOrEmpty($saleCgd->cgd_price_id);
  50. if($cgdinfo->usedNum< $tempNum)throw new Exception('订单成本数据有误');
  51. $save=[
  52. 'usedNum'=>$cgdinfo->usedNum-$tempNum,
  53. 'status'=>$cgdinfo->usedNum-$tempNum<$cgdinfo->goodNum?1:2
  54. ];
  55. $cgdinfo->save($save);
  56. }
  57. }
  58. $origin = self::where(['orderCode'=>$order->orderCode])->order('id','desc')->sum(Db::raw("goodPrice*num"));
  59. $order->origin_price = bcdiv($origin,strval($order->good_num-$num-$order->th_num),6);
  60. $order->save();
  61. }
  62. }
  63. }