Esempio n. 1
0
 public function generate($numtransactions, $type = 'sale')
 {
     // get dependant record
     $this->getRecords();
     // set cur time
     $curprocessdt = time() * 1000;
     if (date('D', $curprocessdt) > 16) {
         $curprocessdt = strtotime(date("Y-m-d", $curprocessdt / 1000) . " 17:00:00") * 1000;
     }
     $initprocessdt = $curprocessdt;
     for ($i = 0; $i < $numtransactions; $i++) {
         // contruct JSON test data
         $saleobj = new stdClass();
         $saleobj->processdt = $curprocessdt;
         // pick a random device if  pos sale
         if ($type == 'sale') {
             $device = $this->devices[rand(0, sizeof($this->devices) - 1)];
             $saleobj->devid = $device['id'];
             $saleobj->locid = $device['locationid'];
         }
         $saleobj->ref = $curprocessdt . "-" . ($type == 'sale' ? $device['id'] : 0) . "-" . rand(1000, 9999);
         // pick a random user
         $saleobj->userid = $this->users[rand(0, sizeof($this->users) - 1)]['id'];
         // add misc data
         $saleobj->custid = "";
         $saleobj->custemail = "";
         $saleobj->notes = "";
         $saleobj->discount = 0;
         $saleobj->discountval = 0;
         // add random items
         $numitems = rand(1, 100) > 75 ? rand(1, 100) > 95 ? rand(7, 10) : rand(4, 6) : rand(1, 3);
         $totalitemqty = 0;
         $total = 0.0;
         $totaltax = 0.0;
         $taxes = [];
         $items = [];
         // loop through num items time
         for ($inum = 0; $inum < $numitems; $inum++) {
             $item = $this->items[rand(0, sizeof($this->items) - 1)];
             // If price is 0 or "" pick a random price
             if ($item['price'] == "" || $item['price'] == 0) {
                 $item['price'] = rand(1, 100);
             }
             // select random qty and get item total
             $randqty = rand(1, 100);
             $qty = $randqty > 80 ? $randqty > 95 ? 3 : 2 : 1;
             $totalitemqty += $qty;
             $itemtotal = round($item['price'] * $qty, 2);
             // work out tax and add totals
             $itemtax = WposAdminUtilities::calculateTax($item['taxid'], isset($saleobj->locid) ? $saleobj->locid : 0, $itemtotal);
             if (!$itemtax->inclusive) {
                 $itemtotal += $itemtax->total;
             }
             $total += $itemtotal;
             $totaltax += $itemtax->total;
             foreach ($itemtax->values as $key => $value) {
                 if (isset($taxes[$key])) {
                     $taxes[$key] += $value;
                 } else {
                     $taxes[$key] = $value;
                 }
             }
             $itemObj = new stdClass();
             $itemObj->ref = $inum + 1;
             $itemObj->sitemid = $item['id'];
             $itemObj->qty = $qty;
             $itemObj->name = $item['name'];
             $itemObj->desc = $item['description'];
             $itemObj->unit = $item['price'];
             $itemObj->taxid = $item['taxid'];
             $itemObj->tax = $itemtax;
             $itemObj->price = $itemtotal;
             $items[] = $itemObj;
         }
         $saleobj->items = $items;
         $subtotal = $total - $totaltax;
         // if method cash round the total & add rounding amount, no cash payments for invoices
         if ($type == 'sale') {
             $paymethod = $this->paymentMethods[rand(0, sizeof($this->paymentMethods) - 1)];
         } else {
             $paymethod = $this->paymentMethods[rand(0, sizeof($this->paymentMethods) - 2)];
         }
         if ($type == 'sale' && $paymethod == "cash") {
             // round to nearest five cents
             $temptotal = $total;
             $total = round($total / 0.05) * 0.05;
             $saleobj->rounding = number_format($total - $temptotal, 2, '.', '');
             //if (floatval($saleobj->rounding)!=0)
             //echo($temptotal." ".$total."<br/>");
         } else {
             $saleobj->rounding = 0.0;
         }
         // add payment to the sale
         if ($type == 'sale') {
             // leave a few invoices unpaid.
             $payment = new stdClass();
             $payment->method = $paymethod;
             $payment->amount = number_format($total, 2, '.', '');
             if ($paymethod == "cash") {
                 $tender = round($total) % 5 === 0 ? round($total) : round(($total + 5 / 2) / 5) * 5;
                 $payment->tender = number_format($tender, 2, '.', '');
                 $payment->change = number_format($tender - $total, 2, '.', '');
             }
             $saleobj->payments = [$payment];
         } else {
             if ($type == 'invoice') {
                 if ($i < 2 || $i == 60) {
                     $saleobj->payments = [];
                 } else {
                     $payment = new stdClass();
                     $payment->method = $paymethod == 'cash' ? 'eftpos' : $paymethod;
                     $payment->amount = number_format($total, 2, '.', '');
                     $saleobj->payments = [$payment];
                 }
             }
         }
         // add totals and tax
         $saleobj->numitems = $totalitemqty;
         $saleobj->taxdata = $taxes;
         $saleobj->tax = number_format($totaltax, 2, '.', '');
         $saleobj->subtotal = number_format($subtotal, 2, '.', '');
         $saleobj->total = number_format($total, 2, '.', '');
         // randomly add a void/refund to the sale
         if ($type == 'sale' && rand(1, 30) == 1) {
             $voidobj = new stdClass();
             // pick another random device
             $device = $this->devices[rand(0, sizeof($this->devices) - 1)];
             $voidobj->deviceid = $device['id'];
             $voidobj->locationid = $device['locationid'];
             // pick another random user
             $voidobj->userid = $this->users[rand(0, sizeof($this->users) - 1)]['id'];
             // set sometime in the future but do not set before the initial date (now).
             $voidobj->processdt = $curprocessdt + rand(30, 60 * 24) > $initprocessdt ? $initprocessdt : $curprocessdt + rand(30, 60 * 24);
             if (rand(1, 2) == 1) {
                 // add reason
                 $voidobj->reason = "Faulty Item";
                 // refund, add additional data
                 $voidobj->method = $this->paymentMethods[rand(0, sizeof($this->paymentMethods) - 1)];
                 // pick item to return
                 $retitem = $items[rand(0, sizeof($items) - 1)];
                 $itemdata = new stdClass();
                 $itemdata->numreturned = 1;
                 $itemdata->ref = $retitem->ref;
                 $voidobj->items = [$itemdata];
                 $voidobj->amount = $retitem->unit;
                 // put in array before adding to saleobj
                 $saleobj->refunddata = [$voidobj];
             } else {
                 // add reason
                 $voidobj->reason = "Mistake";
                 // void
                 $saleobj->voiddata = $voidobj;
             }
         }
         // process the sale
         if ($type == 'sale') {
             $this->wposSales = new WposPosSale($saleobj);
             $this->wposSales->setNoBroadcast();
             $result = $this->wposSales->insertTransaction(["errorCode" => "OK", "error" => "OK", "data" => ""]);
             //echo("Sale created: ".json_encode($result)."<br/>");
         } else {
             // add invoice only fields
             $saleobj->duedt = $curprocessdt + 1209600000;
             $saleobj->custid = rand(1, 2);
             $saleobj->channel = "manual";
             $this->wposSales = new WposInvoices($saleobj, null, true);
             $result = $this->wposSales->createInvoice(["errorCode" => "OK", "error" => "OK", "data" => ""]);
             //echo("Invoice created: ".json_encode($result));
         }
         // decrement by a random time between 2-40 minutes
         if ($type == 'sale') {
             $curprocessdt = $curprocessdt - rand(2, 40) * 60 * 1000;
         } else {
             $curprocessdt = $curprocessdt - rand(40, 280) * 60 * 1000;
         }
         // if it's before shop open time, decrement to the last days closing time.
         $hour = date("H", $curprocessdt / 1000);
         if ($hour < 9) {
             $curprocessdt = strtotime(date("Y-m-d", $curprocessdt / 1000 - 86400) . " 17:00:00") * 1000;
         }
     }
     return;
 }