multi-store/app/admin/logic/purchase_order/PurchaseOrderLogic.php

222 lines
8.3 KiB
PHP

<?php
namespace app\admin\logic\purchase_order;
use app\common\model\purchase_order\PurchaseOrder;
use app\common\logic\BaseLogic;
use app\common\model\purchase_order_info\PurchaseOrderInfo;
use app\common\model\purchase_product_offer\PurchaseProductOffer;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\model\store_product\StoreProduct;
use app\common\model\system_store\SystemStore;
use think\facade\Db;
/**
* 采购订单逻辑
* Class PurchaseOrderLogic
* @package app\admin\logic\purchase_order
*/
class PurchaseOrderLogic extends BaseLogic
{
public static function mergeOrder($params)
{
$oid_arr = [];
$store_arr = [];
foreach ($params as $k => $v) {
$oid_arr[] = $v['oid'];
$store_arr[] = $v['store_id'];
}
$store_arr = array_unique($store_arr);
Db::startTrans();
try {
$price = StoreOrder::whereIn('id', $oid_arr)->field('SUM(pay_price) as pay_price,SUM(total_price) as total_price')->find();
$data = [
'store_id' => 0,
'store_arr' => json_encode($store_arr),
'order_arr' => json_encode($oid_arr),
'order_id' => getNewOrderId('PT'),
'total' => $price['total_price'],
'actual' => $price['pay_price'],
'money' => $price['pay_price'],
'paid' => 1,
'is_mer' => 2,
'create_time' => time(),
'update_time' => time(),
];
$res = PurchaseOrder::create($data);
$info = StoreOrderCartInfo::where('oid', 'in', $oid_arr)->field('store_id, product_id,price,SUM(total_price) as total_price, SUM(cart_num) as cart_num')->group('product_id')->select();
$purchase_order_info = [];
foreach ($info as $item) {
$arr['order_id'] = $res['id'];
$arr['product_id'] = $item['product_id'];
$arr['price'] = $item['price'];
$arr['total_price'] = $item['total_price'];
$arr['need_num'] = $item['cart_num'];
$arr['unit'] = StoreProduct::where('id',$item['product_id'])->value('unit');
$purchase_order_info[] = $arr;
}
$purchaseOrderInfo = new PurchaseProductOffer();
$purchaseOrderInfo->saveAll($purchase_order_info);
StoreOrder::whereIn('id', $oid_arr)->update(['is_merge' => 1]);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 合并今日商户订单
* @param array $params
* @return bool
* @author admin
* @date 2024/08/01 16:32
*/
public static function StoreTodayOrder(): bool
{
Db::startTrans();
try {
$store_arr = StoreOrder::where(['paid' => 1, 'refund_status' => 0])->whereDay('create_time')->group('store_id')->column('store_id');
$purchaseOrderInfo = new PurchaseOrderInfo();
foreach ($store_arr as $store_id) {
$purchase_order_info = [];
$order_arr = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'store_id' => $store_id])->whereDay('create_time')->column('id');
$price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'store_id' => $store_id])->whereDay('create_time')->field('SUM(pay_price) as pay_price,SUM(total_price) as total_price')->find();
$data = [
'store_id' => $store_id,
'order_arr' => json_encode($order_arr),
'order_id' => getNewOrderId('CG'),
'total' => $price['total_price'],
'actual' => $price['pay_price'],
'money' => $price['pay_price'],
'paid' => 1,
'is_mer' => 1,
'create_time' => time(),
'update_time' => time(),
];
$res = PurchaseOrder::create($data);
$info = StoreOrderCartInfo::where('oid', 'in', $order_arr)->field('store_id, product_id,price,SUM(total_price) as total_price, SUM(cart_num) as cart_num')->group('store_id, product_id')->select();
foreach ($info as $item) {
$arr['oid'] = $res['id'];
$arr['store_id'] = $item['store_id'];
$arr['product_id'] = $item['product_id'];
$arr['price'] = $item['price'];
$arr['total_price'] = $item['total_price'];
$arr['cart_num'] = $item['cart_num'];
$arr['status'] = 1;
$purchase_order_info[] = $arr;
}
$purchaseOrderInfo->saveAll($purchase_order_info);
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 合并今日平台订单
* @param array $params
* @return bool
* @author admin
* @date 2024/08/01 16:32
*/
public static function platformTodayOrder()
{
Db::startTrans();
try {
$purchaseOrderInfo = new PurchaseOrderInfo();
$purchase_order_info = [];
$order_arr = PurchaseOrder::whereDay('create_time')->where('is_mer', 1)->column('id');
$price = PurchaseOrder::whereDay('create_time')->field('SUM(actual) as pay_price,SUM(total) as total_price')->find();
$data = [
'store_id' => 0,
'order_arr' => json_encode($order_arr),
'order_id' => getNewOrderId('PT'),
'total' => $price['total_price'],
'actual' => $price['pay_price'],
'money' => $price['pay_price'],
'paid' => 1,
'is_mer' => 2,
'create_time' => time(),
'update_time' => time(),
];
$res = PurchaseOrder::create($data);
$info = PurchaseOrderInfo::where('oid', 'in', $order_arr)->field('store_id, product_id,price,SUM(total_price) as total_price, SUM(cart_num) as cart_num')->group('store_id, product_id')->select();
foreach ($info as $item) {
$arr['oid'] = $res['id'];
$arr['store_id'] = $item['store_id'];
$arr['product_id'] = $item['product_id'];
$arr['price'] = $item['price'];
$arr['total_price'] = $item['total_price'];
$arr['cart_num'] = $item['cart_num'];
$arr['status'] = 1;
$purchase_order_info[] = $arr;
}
$purchaseOrderInfo->saveAll($purchase_order_info);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除采购订单
* @param array $params
* @return bool
* @author admin
* @date 2024/08/01 16:32
*/
public static function delete(array $params): bool
{
return PurchaseOrder::destroy($params['id']);
}
/**
* @notes 获取采购订单详情
* @param $params
* @return array
* @author admin
* @date 2024/08/01 16:32
*/
public static function detail($id): array
{
$data = PurchaseOrder::findOrEmpty($id)->toArray();
if ($data) {
if ($data['store_id']) {
$data['system_store'] = SystemStore::where('id', $data['store_id'])->value('name');
} else {
$data['system_store'] = '平台';
}
switch ($data['storage']) {
case 0:
$data['storage_name'] = '未入库';
break;
case 1:
$data['storage_name'] = '部分入库';
break;
case 2:
$data['storage_name'] = '已入库';
break;
default:
$data['storage_name'] = '未入库';
}
}
return $data;
}
}