|
|
@@ -0,0 +1,89 @@ |
|
|
|
<?php |
|
|
|
namespace app\admin\service; |
|
|
|
|
|
|
|
use app\admin\model\OrderHotel; |
|
|
|
use app\admin\model\OrderItem; |
|
|
|
use app\admin\model\OrderMain; |
|
|
|
use app\admin\model\Purchase; |
|
|
|
use app\admin\model\PurchasePrice; |
|
|
|
|
|
|
|
/** |
|
|
|
* Created by PhpStorm. |
|
|
|
* User: nizongfeng |
|
|
|
* Date: 2021/10/24 |
|
|
|
* Time: 15:55 |
|
|
|
*/ |
|
|
|
|
|
|
|
class OrderMainService |
|
|
|
{ |
|
|
|
/** |
|
|
|
* 设置主订单金额 |
|
|
|
* @param int $orderId |
|
|
|
* @throws |
|
|
|
*/ |
|
|
|
private static function setOrderAmount(int $orderId){ |
|
|
|
$itemModel = new OrderItem(); |
|
|
|
$hotelModel = new OrderHotel(); |
|
|
|
$itemList =$itemModel->where(["order_id"=>$orderId])->select()->toArray(); |
|
|
|
$hotelList = $hotelModel->where(["order_id"=>$orderId])->select()->toArray(); |
|
|
|
$amount = 0; |
|
|
|
$cost = 0; |
|
|
|
foreach ($itemList as $item) { |
|
|
|
$amount += $item['total_price']; |
|
|
|
$cost+= $item['total_cost']; |
|
|
|
} |
|
|
|
foreach ($hotelList as $hotel) { |
|
|
|
$amount += $hotel['total_price']; |
|
|
|
$cost += $hotel["total_cost"]; |
|
|
|
} |
|
|
|
//更新金额 |
|
|
|
OrderMain::update(["total_amount"=>$amount,"cost_amount"=>$cost])->where(["id"=>$orderId]); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 设置子订单金额 |
|
|
|
* @param int $subOrderId |
|
|
|
* @param string $prodType |
|
|
|
* @throws \think\db\exception\DataNotFoundException |
|
|
|
* @throws \think\db\exception\ModelNotFoundException |
|
|
|
* @throws \think\exception\DbException |
|
|
|
*/ |
|
|
|
private static function setSubOrderAmount( int $subOrderId,string $prodType) { |
|
|
|
$purchaseModel = new Purchase(); |
|
|
|
$purchaseList = $purchaseModel->where(["order_detail_id"=>$subOrderId,"del_flag"=>0])->select()->toArray(); |
|
|
|
$cost = 0; |
|
|
|
$amount = 0; |
|
|
|
foreach ($purchaseList as $purchase) { |
|
|
|
$cost += $purchase['total_cost']; |
|
|
|
$amount += $purchase['total_price']; |
|
|
|
} |
|
|
|
if ($prodType == "hotel") { |
|
|
|
OrderHotel::update(["total_price"=>$amount,"total_cost"=>$cost])->where(["id"=>$subOrderId]); |
|
|
|
} else { |
|
|
|
OrderItem::update(["total_price"=>$amount,"total_cost"=>$cost])->where(["id"=>$subOrderId]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 设置采购单/子订单/主订单 金额 |
|
|
|
* @param Purchase $purchase |
|
|
|
* @throws \think\db\exception\DataNotFoundException |
|
|
|
* @throws \think\db\exception\ModelNotFoundException |
|
|
|
* @throws \think\exception\DbException |
|
|
|
*/ |
|
|
|
public function setPurchase(Purchase $purchase){ |
|
|
|
$purchasePrice = new PurchasePrice(); |
|
|
|
$purchasePriceList = $purchasePrice->where(["purchase_id"=>$purchase["id"],"del_flag"=>0])->select()->toArray(); |
|
|
|
$cost = 0; |
|
|
|
$amount = 0; |
|
|
|
foreach ($purchasePriceList as $price) { |
|
|
|
$cost += $price['cost'] * $price["cnt"]; |
|
|
|
$amount += $price['price'] * $price["cnt"]; |
|
|
|
} |
|
|
|
Purchase::update(["total_price"=>$amount,["total_cost"=>$cost]])->where(["id"=>$purchase['id']]); |
|
|
|
//更新子表 |
|
|
|
static::setSubOrderAmount($purchase['order_detail_id'], $purchase['prod_type']); |
|
|
|
//更新主表金额 |
|
|
|
static::setOrderAmount($purchase['order_id']); |
|
|
|
} |
|
|
|
} |