multi-store/app/admin/logic/warehouse_product/WarehouseProductLogic.php

180 lines
6.0 KiB
PHP

<?php
namespace app\admin\logic\warehouse_product;
use app\common\model\warehouse_product\WarehouseProduct;
use app\common\logic\BaseLogic;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_product\StoreProduct;
use app\common\model\warehouse_product_storege\WarehouseProductStorege;
use support\Log;
use think\facade\Db;
/**
* 商品仓储信息逻辑
* Class WarehouseProductLogic
* @package app\admin\logic\warehouse_product
*/
class WarehouseProductLogic extends BaseLogic
{
/**
* @notes 添加商品仓储信息
* @param array $params
* @return bool
* @author admin
* @date 2024/07/31 16:55
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
$data = [
'warehouse_id' => $params['warehouse_id'],
'store_id' => $params['store_id']??0,
'product_id' => $params['product_id'],
'financial_pm' => $params['financial_pm'],
'batch' => 0,
'nums' => $params['nums'],
'price' => $params['price'] ?? '',
'purchase' => $params['purchase'] ?? '',
'cost' => $params['cost'] ?? '',
'total_price' => $params['total_price'] ?? '',
'admin_id' => $params['admin_id'],
'code' => $params['code'] ?? '',
'status' => $params['status'] ?? 0,
'mark' => $params['mark'] ?? '',
];
if (isset($params['manufacture']) && $params['manufacture'] != '') {
$data['manufacture'] = strtotime($params['manufacture']);
}
if (isset($params['expiration_date']) && $params['expiration_date'] != '') {
$data['expiration_date'] = strtotime($params['expiration_date']);
}
$res = WarehouseProduct::create($data);
self::enter($res['id'], $params['financial_pm']);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
Log::error($e->getMessage().',file:'.$e->getFile().',line:'.$e->getLine());
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 编辑商品仓储信息
* @param array $params
* @return bool
* @author admin
* @date 2024/07/31 16:55
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
$data = [
'warehouse_id' => $params['warehouse_id'],
'product_id' => $params['product_id'],
'financial_pm' => $params['financial_pm'],
'nums' => $params['nums'],
'price' => $params['price'],
'admin_id' => $params['admin_id'],
'total_price' => $params['total_price'],
'code' => $params['code'],
'purchase' => $params['purchase'] ?? '',
'cost' => $params['cost'] ?? '',
];
if (isset($params['manufacture']) && $params['manufacture'] != '') {
$data['manufacture'] = strtotime($params['manufacture']);
}
if (isset($params['expiration_date']) && $params['expiration_date'] != '') {
$data['expiration_date'] = strtotime($params['expiration_date']);
}
$res = WarehouseProduct::where('id', $params['id'])->update($data);
Db::commit();
return $res;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 确认商品仓储信息
* @param array $params
* @author admin
* @date 2024/07/31 16:55
*/
public static function enter($id, $financial_pm = 0)
{
$find = WarehouseProduct::where('id', $id)->find();
$find->status = 1;
$find->batch = WarehouseProduct::where(['product_id' => $find['product_id'], 'warehouse_id' => $find['warehouse_id'], 'financial_pm' => $financial_pm])->count();
$find->save();
$storege = WarehouseProductStorege::where('warehouse_id', $find['warehouse_id'])->where('product_id', $find['product_id'])->find();
if ($financial_pm == 0) {
StoreProduct::where('id', $find['product_id'])->dec('stock', $find['nums'])->update();
} else {
StoreProduct::where('id', $find['product_id'])->inc('stock', $find['nums'])->update(['purchase' => $find['purchase'], 'cost' => $find['cost'], 'price' => $find['price']]);
StoreBranchProduct::where('product_id', $find['product_id'])->update(['purchase' => $find['purchase'], 'cost' => $find['cost'], 'price' => $find['price']]);
}
if ($storege) {
if ($financial_pm == 0) {
$storege->nums = bcsub($storege->nums, $find['nums']);
} else {
$storege->nums = bcadd($storege->nums, $find['nums']);
}
$storege->save();
} else {
WarehouseProductStorege::create([
'warehouse_id' => $find['warehouse_id'],
'product_id' => $find['product_id'],
'nums' => $find['nums'],
]);
}
}
/**
* @notes 删除商品仓储信息
* @param array $params
* @return bool
* @author admin
* @date 2024/07/31 16:55
*/
public static function delete(array $params): bool
{
return WarehouseProduct::destroy($params['id']);
}
/**
* @notes 获取商品仓储信息详情
* @param $params
* @return array
* @author admin
* @date 2024/07/31 16:55
*/
public static function detail($params): array
{
$data = WarehouseProduct::findOrEmpty($params['id'])->toArray();
if ($data) {
$data['manufacture'] = date('Y-m-d', $data['manufacture']);
$data['expiration_date'] = date('Y-m-d', $data['expiration_date']);
}
return $data;
}
}