12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\model;
- use think\Exception;use think\facade\Db;use think\Model;use think\model\concern\SoftDelete;
- /**
- * @mixin \think\Model
- */
- class SaleCgdPrice extends Model
- {
- use SoftDelete;
- protected $createTime="addtime";
- protected $updateTime="updatetime";
- protected $deleteTime="delete_time";
-
- /** 计算销售单关联备库采购单数量获取成本单价计算
- * @param $orderId
- * @throws \think\Exception
- */
- public static function GetPrice($orderId){
- return ;
- $order = Sale::findOrEmpty($orderId);
- if($order->isEmpty()) throw new Exception("未找到订单数据");
- if($order->origin_price==0){
- $temp = CgdPrice::checkCgdPrice($order);
-
- }else{
- $temp = CgdPrice::checkCgdNum($order);
- }
- if(!empty($temp))self::insertAll($temp);
- }
-
- public static function RePrice($orderId,$num){
- return ;
- $order = Sale::findOrEmpty($orderId);
- if($order->isEmpty()) throw new Exception('未找到订单数据');
- if($order->is_activity==0){
- while ($num>0){
- $saleCgd=self::where(['orderCode'=>$order->orderCode])->order("id","desc")->findOrEmpty();
- if($saleCgd->isEmpty())throw new Exception('未找到订单成本数据');
- $tempNum=0;
- if($saleCgd->num<=$num){
- $tempNum=$saleCgd->num;
- $num -=$saleCgd->num;
- $saleCgd->delete();
- }else{
- $tempNum=$num;
- $saleCgd->num-=$num;
- $num=0;
- $saleCgd->save();
- }
- if($saleCgd->cgd_price_id!=0){
- $cgdinfo = CgdPrice::findOrEmpty($saleCgd->cgd_price_id);
- if($cgdinfo->usedNum< $tempNum)throw new Exception('订单成本数据有误');
- $save=[
- 'usedNum'=>$cgdinfo->usedNum-$tempNum,
- 'status'=>$cgdinfo->usedNum-$tempNum<$cgdinfo->goodNum?1:2
- ];
- $cgdinfo->save($save);
- }
- }
- $origin = self::where(['orderCode'=>$order->orderCode])->order('id','desc')->sum(Db::raw("goodPrice*num"));
- $order->origin_price = bcdiv($origin,strval($order->good_num-$num-$order->th_num),6);
- $order->save();
- }
-
- }
- }
|