Пример #1
0
 /**
   Add a transaction record directly to dtransactions on the backend
   @param $connection [SQLManager] database connection
   @param $trans_no [integer] transaction number (dtransactions.trans_no)
   @param $params [array] of column_name => value
 
   If emp_no and register_no values are not specified, the defaults
   are the configuration settings FANNIE_EMP_NO and FANNIE_REGISTER_NO.
 
   The following columns are always calculated by addItem() and values
   set in $params will be ignored:
   - datetime (always current)
   - trans_id (assigned based on existing records)
   Additionally, the following values are looked up if $params['card_no']
   is specified:
   - memType
   - staff
 */
 public static function addItem(SQLManager $connection, $trans_no, $params)
 {
     $config = FannieConfig::factory();
     $model = new DTransactionsModel($connection);
     $model->whichDB($config->get('TRANS_DB'));
     $model->trans_no($trans_no);
     $model->emp_no($config->get('EMP_NO'));
     if (isset($params['emp_no'])) {
         $model->emp_no($params['emp_no']);
     }
     $model->register_no($config->get('REGISTER_NO'));
     if (isset($params['register_no'])) {
         $model->register_no($params['register_no']);
     }
     $current_records = $model->find('trans_id', true);
     if (count($current_records) == 0) {
         $model->trans_id(1);
     } else {
         $last = $current_records[0];
         $model->trans_id($last->trans_id() + 1);
     }
     if (isset($params['card_no'])) {
         $account = \COREPOS\Fannie\API\member\MemberREST::get($params['card_no']);
         if ($account) {
             $model->memType($account['customerTypeID']);
             $model->staff($account['customers'][0]['staff']);
         }
     }
     $defaults = self::$DEFAULTS;
     $skip = array('datetime', 'emp_no', 'register_no', 'trans_no', 'trans_id');
     foreach ($defaults as $name => $value) {
         if (in_array($name, $skip)) {
             continue;
         }
         if (isset($params[$name])) {
             $model->{$name}($params[$name]);
         } else {
             $model->{$name}($value);
         }
     }
     $model->datetime(date('Y-m-d H:i:s'));
     if ($model->save()) {
         return true;
     } else {
         return false;
     }
 }