實作產品與庫存匯入邏輯 (ProductImport, InventoryImport) 並更新相關 Service 與 Controller
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 56s
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 56s
This commit is contained in:
@@ -110,4 +110,72 @@ class ProductService implements ProductServiceInterface
|
||||
{
|
||||
return Product::whereIn('code', $codes)->get();
|
||||
}
|
||||
|
||||
public function createProduct(array $data)
|
||||
{
|
||||
if (empty($data['code'])) {
|
||||
$data['code'] = $this->generateRandomCode();
|
||||
}
|
||||
|
||||
if (empty($data['barcode'])) {
|
||||
$data['barcode'] = $this->generateRandomBarcode();
|
||||
}
|
||||
|
||||
return Product::create($data);
|
||||
}
|
||||
|
||||
public function updateProduct(Product $product, array $data)
|
||||
{
|
||||
if (empty($data['code'])) {
|
||||
$data['code'] = $this->generateRandomCode();
|
||||
}
|
||||
|
||||
if (empty($data['barcode'])) {
|
||||
$data['barcode'] = $this->generateRandomBarcode();
|
||||
}
|
||||
|
||||
$product->update($data);
|
||||
return $product;
|
||||
}
|
||||
|
||||
public function generateRandomCode()
|
||||
{
|
||||
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
$code = '';
|
||||
|
||||
do {
|
||||
$code = '';
|
||||
for ($i = 0; $i < 8; $i++) {
|
||||
$code .= $characters[rand(0, strlen($characters) - 1)];
|
||||
}
|
||||
} while (Product::where('code', $code)->exists());
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
public function generateRandomBarcode()
|
||||
{
|
||||
$barcode = '';
|
||||
|
||||
do {
|
||||
$barcode = '';
|
||||
for ($i = 0; $i < 13; $i++) {
|
||||
$barcode .= rand(0, 9);
|
||||
}
|
||||
} while (Product::where('barcode', $barcode)->exists());
|
||||
|
||||
return $barcode;
|
||||
}
|
||||
|
||||
public function findByBarcodeOrCode(?string $barcode, ?string $code)
|
||||
{
|
||||
$product = null;
|
||||
if (!empty($barcode)) {
|
||||
$product = Product::where('barcode', $barcode)->first();
|
||||
}
|
||||
if (!$product && !empty($code)) {
|
||||
$product = Product::where('code', $code)->first();
|
||||
}
|
||||
return $product;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user