Created
October 5, 2019 15:12
-
-
Save gjj/8b6422a58533d453b472019008820797 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Content-Type: text/plain"); | |
//echo "sales_id,customer_id,product_id,product_type,unit_cost,quantity,unit_cost\r\n"; | |
echo "PONumber,SupplierID,ProductID,Quantity,OrderDate,ReceivedDate,ReceivedQuantity\r\n"; | |
$start = "1 January 2018"; | |
$end = "31 December 2018"; | |
$dateStart = strtotime($start); | |
$dateEnd = strtotime($end); | |
$days = round(($dateEnd - $dateStart) / (60 * 60 * 24)); | |
// 2017: CNY, Qingming, Lunar 7th Month, Mid Autumn | |
$counter = 1; | |
$holidays = [ | |
'6 February 2019', | |
'5 April 2019', | |
'15 August 2019' | |
]; | |
$products = [ | |
['P0001', 'P0002', 'P0003', 'P0004', 'P0005'], | |
['P0006', 'P0007'], | |
['P0008', 'P0009', 'P0010'], | |
['P0011'], | |
['P0012', 'P0013', 'P0014'], | |
['P0015', 'P0016'], | |
['P0017', 'P0018'], | |
['P0019', 'P0020'], | |
['P0021', 'P0022', 'P0023'] | |
]; | |
$productsAll = call_user_func_array('array_merge', $products); | |
for ($i = 0; $i <= $days; $i++) { | |
// each day. | |
$currentDate = strtotime($start . " +{$i} days"); | |
$format = date("Ym", $currentDate); | |
$inRangeOfHoliday = false; | |
foreach ($holidays as $holiday) { | |
$oneMonthBefore = strtotime($holiday . " -1 month"); | |
$holiday = strtotime($holiday); | |
if ($oneMonthBefore <= $currentDate and $currentDate <= $holiday) { | |
$inRangeOfHoliday = true; | |
} | |
} | |
if ($inRangeOfHoliday) { | |
$orders = mt_rand(14, 22); // No. of orders in a day. | |
} | |
else { | |
$orders = mt_rand(8, 14); // No. of orders in a day. | |
} | |
if (date('j', $currentDate) === '1') { | |
$counter = 1; | |
} | |
$supplierIds = randomGen(1, 40, $orders); | |
for ($j = 1; $j <= $orders; $j++) { | |
// each order. $inRangeOfHoliday | |
$receivedDate = strtotime(date("Y-m-d", $currentDate) . " +" . mt_rand(1, 2) . " days"); | |
$noOfProducts = mt_rand(1, 5); | |
$supplierId = $supplierIds[0]; | |
$supplierIds = array_slice($supplierIds, 1); | |
$productIds = randomGenProducts($noOfProducts, $inRangeOfHoliday); //$productsAll | |
for ($k = 1; $k <= $noOfProducts; $k++) { | |
// each product | |
$data = "PO" . $format . sprintf('%03d', $counter) . ","; | |
$data .= "SUP" . sprintf('%08d', $supplierId) . ","; | |
$productId = $productsAll[$productIds[0]]; | |
$data .= $productId . ","; | |
$productIds = array_slice($productIds, 1); | |
if (in_array($productId, ['P0001', 'P0002', 'P0003', 'P0004', 'P0005'])) { | |
$prob = mt_rand(1, 100); | |
if ($prob < 75) { | |
$quantity = mt_rand(1, 5); | |
} | |
else { | |
$quantity = mt_rand(6, 10); | |
} | |
} | |
else if (in_array($productId, ['P0006', 'P0007'])) { | |
$prob = mt_rand(1, 100); | |
if ($prob < 75) { | |
$quantity = mt_rand(1, 5); | |
} | |
else { | |
$quantity = mt_rand(6, 10); | |
} | |
} | |
else if (in_array($productId, ['P0008', 'P0009', 'P0010'])) { | |
$prob = mt_rand(1, 100); | |
if ($prob < 75) { | |
$quantity = mt_rand(ceil(10/5), floor(40/5))*5; | |
} | |
else { | |
$quantity = mt_rand(ceil(40/5), floor(80/5))*5; | |
} | |
} | |
else if (in_array($productId, ['P0011'])) { | |
$prob = mt_rand(1, 100); | |
if ($prob < 80) { | |
$quantity = mt_rand(ceil(10/5), floor(40/5))*5; | |
} | |
else { | |
$quantity = mt_rand(ceil(40/5), floor(80/5))*5; | |
} | |
} | |
else if (in_array($productId, ['P0012', 'P0013', 'P0014'])) { | |
if ($inRangeOfHoliday) { | |
$quantity = mt_rand(1, 2); | |
} | |
} | |
else if (in_array($productId, ['P0015', 'P0016'])) { | |
$prob = mt_rand(1, 100); | |
if ($prob < 75) { | |
$quantity = mt_rand(2, 20)/2; | |
} | |
else { | |
$quantity = mt_rand(21, 40)/2; | |
} | |
} | |
else if (in_array($productId, ['P0017', 'P0018'])) { | |
$prob = mt_rand(1, 100); | |
if ($prob < 75) { | |
$quantity = mt_rand(2, 10)/2; | |
} | |
else { | |
$quantity = mt_rand(11, 20)/2; | |
} | |
} | |
else if (in_array($productId, ['P0019', 'P0020'])) { | |
$prob = mt_rand(1, 100); | |
if ($prob < 75) { | |
$quantity = mt_rand(2, 20)/2; | |
} | |
else { | |
$quantity = mt_rand(21, 40)/2; | |
} | |
} | |
else if (in_array($productId, ['P0021', 'P0022', 'P0023'])) { | |
$prob = mt_rand(1, 100); | |
if ($prob < 75) { | |
$quantity = mt_rand(2, 10)/2; | |
} | |
else { | |
$quantity = mt_rand(11, 20)/2; | |
} | |
} | |
$data .= $quantity . ","; | |
$data .= date("Y-m-d", $currentDate) . ","; | |
$data .= date("Y-m-d", $receivedDate) .","; | |
$data .= $quantity; | |
echo $data . "\r\n"; | |
} | |
$counter++; | |
} | |
} | |
function randomGen($min, $max, $quantity) { | |
$numbers = range($min, $max); | |
shuffle($numbers); | |
return array_slice($numbers, 0, $quantity); | |
} | |
function randomGenProducts($quantity, $isHoliday) { | |
if ($isHoliday) { | |
$numbers = range(0, 21); | |
shuffle($numbers); | |
return array_slice($numbers, 0, $quantity); | |
} | |
else { | |
$numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 15, 16, 17, 18, 19, 20, 21]; | |
$prob = mt_rand(1, 100); | |
if ($prob >= 95) { | |
array_push($numbers, 11, 12, 13); | |
} | |
shuffle($numbers); | |
return array_slice($numbers, 0, $quantity); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment