| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- namespace bizGhs\live\classes;
- use bizGhs\base\classes\BaseClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\product\classes\ProductClass;
- use common\components\imgUtil;
- use common\components\orderSn;
- use common\components\util;
- class LiveOrderClass extends BaseClass
- {
- public static $baseFile = '\bizGhs\live\models\LiveOrder';
- public static function updateCustom($customData, $live)
- {
- $orderSn = $live->orderSn ?? '';
- $mainId = $live->mainId ?? 0;
- $customMap = [];
- $ids = [];
- foreach ($customData as $key => $val) {
- $customId = $val['customId'] ?? 0;
- $customMap[$customId] = $val;
- $ids[] = $customId;
- }
- $currentList = CustomClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
- if (!empty($currentList)) {
- foreach ($currentList as $current) {
- if ($current->ownMainId != $mainId) {
- util::fail('不是你的客户');
- }
- }
- }
- $customList = LiveOrderCustomClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (empty($customList)) {
- return false;
- }
- $totalNum = 0;
- $totalAmount = 0;
- foreach ($customList as $k => $info) {
- $customId = $info->customId ?? 0;
- $num = isset($customMap[$customId]) && isset($customMap[$customId]['num']) ? $customMap[$customId]['num'] : 0;
- $unitPrice = isset($customMap[$customId]) && isset($customMap[$customId]['unitPrice']) ? $customMap[$customId]['unitPrice'] : 0;
- $price = bcmul($num, $unitPrice, 2);
- $info->num = $num;
- $info->unitPrice = $unitPrice;
- $info->price = $price;
- $info->save();
- $totalNum = bcadd($totalNum, $num);
- $totalAmount = bcadd($totalAmount, $price, 2);
- }
- $live->itemNum = $totalNum;
- $live->itemAmount = $totalAmount;
- $live->save();
- }
- public static function delCustom($custom, $live)
- {
- $customId = $custom->id ?? 0;
- $orderSn = $live->orderSn ?? '';
- $customList = LiveOrderCustomClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- $num = 0;
- $price = 0;
- if (!empty($customList)) {
- foreach ($customList as $it) {
- $currentCustomId = $it->customId ?? 0;
- if ($currentCustomId == $customId) {
- $num = $it->num ?? 0;
- $price = $it->price ?? 0;
- $it->delete();
- }
- }
- }
- $live->customNum = bcsub($live->customNum, 1);
- $live->itemNum = bcsub($live->itemNum, $num);
- $live->itemAmount = bcsub($live->itemAmount, $price, 2);
- $live->save();
- }
- public static function addCustom($params, $custom, $live)
- {
- if ($custom->mainId == $live->mainId) {
- util::fail('自己的店,不能添加');
- }
- $customId = $custom->id ?? 0;
- $orderSn = $live->orderSn ?? '';
- $customList = LiveOrderCustomClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($customList)) {
- foreach ($customList as $it) {
- $currentCustomId = $it->customId ?? 0;
- if ($currentCustomId == $customId) {
- util::fail('客户已经存在');
- }
- }
- }
- $customName = $custom->name ?? '';
- $seatSn = $custom->seatSn ?? 0;
- //分配货位
- if (empty($seatSn)) {
- $mainId = $live->mainId ?? 0;
- $seatRes = LiveSeatClass::getByCondition(['mainId' => $mainId, 'customId' => 0], true);
- if (empty($seatRes)) {
- util::fail('没有货位可分配哦');
- }
- $seatResId = $seatRes->id ?? 0;
- $seatRes = LiveSeatClass::getLockById($seatResId);
- $newSeatSn = $seatRes->no;
- $custom->seatSn = $newSeatSn;
- $custom->visitTime = date("Y-m-d H:i:s");
- $custom->save();
- $seatRes->customId = $custom->id;
- $seatRes->save();
- }
- $num = $params['num'] ?? 0;
- $unitPrice = $params['price'] ?? 0;
- $price = bcmul($unitPrice, $num, 2);
- $data = [
- 'mainId' => $live->mainId,
- 'orderSn' => $orderSn,
- 'customName' => $customName,
- 'customId' => $customId,
- 'num' => $num,
- 'unitPrice' => $unitPrice,
- 'unitName' => '',
- 'price' => $price,
- ];
- LiveOrderCustomClass::add($data, true);
- $live->customNum = bcadd($live->customNum, 1);
- $live->itemNum = bcadd($live->itemNum, $num);
- $live->itemAmount = bcadd($live->itemAmount, $price, 2);
- $live->save();
- }
- public static function getOrderList($where)
- {
- $data = self::getList('*', $where, 'addTime DESC');
- if (isset($data['list']) && !empty($data['list'])) {
- $ids = array_column($data['list'], 'itemId');
- $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,stock', 'id');
- foreach ($data['list'] as $key => $val) {
- $shortCover = $val['itemCover'] ?? '';
- $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_100,w_100";
- $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
- $largeCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_990,w_990";
- $data['list'][$key]['shortCover'] = $shortCover;
- $data['list'][$key]['cover'] = $cover;
- $data['list'][$key]['bigCover'] = $bigCover;
- $data['list'][$key]['largeCover'] = $largeCover;
- $itemId = $val['itemId'] ?? 0;
- $stock = isset($productList[$itemId]) && isset($productList[$itemId]['stock']) ? floatval($productList[$itemId]['stock']) : 0;
- $data['list'][$key]['stock'] = $stock;
- }
- }
- return $data;
- }
- public static function addOrder($params, $mainId)
- {
- $product = $params['product'];
- $itemId = $product->id ?? 0;
- $ptItemId = $product->itemId ?? 0;
- $itemName = $product->name ?? '';
- $itemPy = $product->py ?? '';
- $itemCover = $product->cover ?? '';
- $customList = $params['customList'];
- $staffId = $params['staffId'] ?? 0;
- $staffName = $params['staffName'] ?? '';
- $itemPrice = $params['itemPrice'] ?? 0;
- $itemRemark = $params['itemRemark'] ?? '';
- $customNum = count($customList);
- $itemNum = 0;
- $itemAmount = 0;
- foreach ($customList as $custom) {
- $num = $custom['num'] ?? 0;
- $unitPrice = $custom['price'] ?? 0;
- $itemNum = bcadd($num, $itemNum);
- $price = bcmul($unitPrice, $num, 2);
- $itemAmount = bcadd($itemAmount, $price, 2);
- }
- $orderSn = orderSn::getLiveOrderSn();
- $liveData = [
- 'mainId' => $mainId,
- 'itemId' => $itemId,
- 'ptItemId' => $ptItemId,
- 'itemName' => $itemName,
- 'itemPy' => $itemPy,
- 'itemCover' => $itemCover,
- 'customNum' => $customNum,
- 'itemNum' => $itemNum,
- 'itemAmount' => $itemAmount,
- 'orderSn' => $orderSn,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'itemPrice' => $itemPrice,
- 'itemRemark' => $itemRemark,
- ];
- self::add($liveData, true);
- $ids = array_column($customList, 'customId');
- $infoList = CustomClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
- foreach ($customList as $it) {
- $customId = $it['customId'] ?? 0;
- $num = $it['num'] ?? 0;
- $unitName = $product->bigUnit ?? '扎';
- $unitPrice = $it['price'] ?? 0;
- $price = bcmul($num, $unitPrice, 2);
- $customInfo = $infoList[$customId] ?? null;
- if (empty($customInfo)) {
- util::fail('客户信息缺失,编号673');
- }
- $customName = $customInfo->name ?? '';
- $customMainId = $customInfo->mainId ?? 0;
- if ($mainId == $customMainId) {
- util::fail($customName . ' 是自己的店');
- }
- $seatSn = $customInfo->seatSn ?? 0;
- //分配货位
- if (empty($seatSn)) {
- $seatRes = LiveSeatClass::getByCondition(['mainId' => $mainId, 'customId' => 0], true);
- if (empty($seatRes)) {
- util::fail('没有货位可分配哈');
- }
- $seatResId = $seatRes->id ?? 0;
- $seatRes = LiveSeatClass::getLockById($seatResId);
- $newSeatSn = $seatRes->no;
- $customInfo->seatSn = $newSeatSn;
- $customInfo->visitTime = date("Y-m-d H:i:s");
- $customInfo->save();
- $seatRes->customId = $customInfo->id;
- $seatRes->save();
- }
- $customData = [
- 'mainId' => $mainId,
- 'orderSn' => $orderSn,
- 'customName' => $customName,
- 'customId' => $customId,
- 'num' => $num,
- 'unitName' => $unitName,
- 'unitPrice' => $unitPrice,
- 'price' => $price,
- ];
- LiveOrderCustomClass::add($customData, true);
- }
- }
- }
|