public function cancel_grn()
 {
     $flash = Flash::Instance();
     $errors = array();
     if (!$this->CheckParams('gr_number')) {
         sendBack();
     }
     $poreceivedline = DataObjectFactory::Factory('POReceivedLine');
     $cc = new ConstraintChain();
     $cc->add(new Constraint('gr_number', '=', $this->_data['gr_number']));
     $poreceivedlines = $poreceivedline->getAll($cc);
     $db = DB::Instance();
     $db->StartTrans();
     foreach ($poreceivedlines as $poreceivedline_id => $value) {
         $poreceivedline = DataObjectFactory::Factory('POReceivedLine');
         $poreceivedline->load($poreceivedline_id);
         // Update the order line to reverse the GRN
         $porderline = DataObjectFactory::Factory('POrderLine');
         $porderline->load($poreceivedline->orderline_id);
         if (!is_null($porderline->stitem_id)) {
             $stitem = DataObjectFactory::Factory('STItem');
             $stitem->load($porderline->stitem_id);
             $qty_decimals = $stitem->qty_decimals;
         } else {
             $qty_decimals = maxdp($porderline->os_qty, $porderline->del_qty, $poreceivedline->received);
         }
         // get the received quantity for this orderline, excluding lines in the GRN
         $received_qty = $poreceivedline->getReceivedQty($porderline->id, $poreceivedline->gr_number);
         $porderline->os_qty = BCSUB($porderline->revised_qty, $received_qty, $qty_decimals);
         $porderline->del_qty = $received_qty;
         if ($porderline->del_qty > 0) {
             $porderline->status = $porderline->partReceivedStatus();
         } else {
             $porderline->status = $porderline->awaitingDeliveryStatus();
         }
         // Load the PO Header and save the orderline
         // to ensure header status is updated if required
         $porder = DataObjectFactory::Factory('POrder');
         $porder->load($porderline->order_id);
         if (!$porderline->save($porder)) {
             $errors[] = 'Error updating PO line ' . $db->ErrorMsg();
         }
         // Finally update the po received line
         $poreceivedline->status = $poreceivedline->cancelStatus();
         if (!$poreceivedline->save()) {
             $errors[] = 'Error updating GRN ' . $db->ErrorMsg();
         }
     }
     if (count($errors) === 0) {
         // Now get the transactions for the GRN
         // Note each GRN line has two transactions linked by transfer_id
         $sttransaction = DataObjectFactory::Factory('STTransaction');
         $sttransaction->identifierField = 'transfer_id';
         $cc = new ConstraintChain();
         $cc->add(new Constraint('process_name', '=', 'GR'));
         $cc->add(new Constraint('process_id', '=', $this->_data['gr_number']));
         $sttransactions = $sttransaction->getAll($cc);
         // Need to link new transfer ids to existing transactions transfer_ids
         $transfer_ids = array();
         $transferrule = DataObjectFactory::Factory('WHTransferrule');
         foreach ($sttransactions as $sttransaction_id => $value) {
             if (!isset($transfer_ids[$value])) {
                 $transfer_ids[$value] = $transferrule->getTransferId()->transfer_id;
             }
         }
         // Reverse each of the transaction pairs associated with the GRN lines
         foreach ($sttransactions as $sttransaction_id => $value) {
             $sttransaction = DataObjectFactory::Factory('STTransaction');
             $sttransaction->load($sttransaction_id);
             // create new transaction by setting new id value
             $test = $sttransaction->autoHandle($sttransaction->idField);
             if ($test !== false) {
                 $sttransaction->{$sttransaction->idField} = $test;
             } else {
                 $errors[] = 'Error getting identifier for new item';
             }
             // Reverse the quantity and save
             $sttransaction->transfer_id = $transfer_ids[$sttransaction->transfer_id];
             $sttransaction->created = $sttransaction->autoHandle('created');
             $sttransaction->createdby = EGS_USERNAME;
             $sttransaction->qty = $sttransaction->qty * -1;
             $sttransaction->save($errors);
         }
     }
     // Check for errors
     if (count($errors) > 0) {
         $flash->addErrors($errors);
         $flash->addError('Error cancelling GRN ' . $this->_data['gr_number']);
         $db->FailTrans();
     } else {
         $flash->addMessage('GRN ' . $this->_data['gr_number'] . ' Cancelled');
     }
     $db->CompleteTrans();
     sendTo($this->name, 'index', $this->_modules);
 }
Beispiel #2
0
/**
 * Will return an array of all the values between $min and $max, with separation $step
 * e.g. getRange(0,1,0.2) will return [0.0,0.2,0.4,0.6,0.8,1.0]
 * - will maintain the precision of the most precise argument
 * @see maxdp()
 */
function getRange($min, $max, $step, $keys = FALSE, $value_prefix = '', $value_suffix = '', $signed = FALSE, $ignore_zero = FALSE)
{
    $values = array();
    $dp = maxdp($min, $max, $step);
    for ($i = $min; $i <= $max; $i += $step) {
        if ($ignore_zero && $i == 0) {
            continue;
        }
        $value = sprintf('%01.' . $dp . 'f', $i);
        if ($signed && floatval($value) > 0) {
            $value = '+' . $value;
        }
        if ($keys) {
            $values[$value] = $value_prefix . $value . $value_suffix;
        } else {
            $values[] = $value;
        }
    }
    return $values;
}