Example #1
0
 /** @return array BillingPlan including existing, $toDelete - but existing not found in request */
 public function updatePlansFromRequest(Product $record, $values, $existing = array())
 {
     // we access "POST" directly here as there is no access to new added
     // fields from the form!
     $plans = $_POST['_plan'];
     unset($plans['TPL']);
     //we should use output of getValue to set additional fields
     //in order to value be correct
     $this->getDi()->billingPlanTable->customFields()->getAll();
     $form = new Am_Form_Admin();
     foreach ($this->getDi()->billingPlanTable->customFields()->getAll() as $f) {
         $f->addToQf2($form);
     }
     foreach ($plans as $k => $plan) {
         $form->setDataSources(array(new HTML_QuickForm2_DataSource_Array($plan)));
         $plans[$k] = array_merge($plan, $form->getValue());
     }
     foreach ($plans as $k => &$arr) {
         if (Am_Di::getInstance()->config->get('product_paysystem')) {
             $arr['paysys_id'] = implode(',', isset($arr['paysys_id']) ? $arr['paysys_id'] : array());
         }
         if ($arr['_rebill_times'] != 'x') {
             $arr['rebill_times'] = $arr['_rebill_times'];
         }
         try {
             $p = new Am_Period($arr['first_period']['c'], $arr['first_period']['u']);
             $arr['first_period'] = (string) $p;
         } catch (Am_Exception_InternalError $e) {
             unset($plans[$k]);
             continue;
         }
         try {
             $p = new Am_Period($arr['second_period']['c'], $arr['second_period']['u']);
             $arr['second_period'] = (string) $p;
         } catch (Am_Exception_InternalError $e) {
             $arr['second_period'] = '';
         }
         if (empty($arr['variable_qty'])) {
             $arr['variable_qty'] = 0;
         }
         if (empty($arr['qty'])) {
             $arr['qty'] = 1;
         }
         if ($arr['rebill_times'] == 0) {
             $arr['second_price'] = null;
             $arr['second_period'] = null;
         }
     }
     foreach ($existing as $k => $plan) {
         if (empty($plans[$plan->pk()])) {
             $plan->delete();
         } else {
             $plan->setForUpdate($plans[$plan->pk()]);
             $plan->update();
             unset($plans[$plan->pk()]);
         }
     }
     foreach ($plans as $id => $a) {
         $plan = $this->getDi()->billingPlanRecord;
         $plan->setForInsert($a);
         $plan->product_id = $record->pk();
         $plan->insert();
     }
     // temp. stub
     $record->updateQuick('default_billing_plan_id', $this->getDi()->db->selectCell("SELECT MIN(plan_id) FROM ?_billing_plan WHERE product_id=?d\n                AND (disabled IS NULL OR disabled = 0)", $record->product_id));
 }
 /** @return array BillingPlan including existing, $toDelete - but existing not found in request */
 public function updatePlansFromRequest(Product $record, $values, $existing = array())
 {
     // we access "POST" directly here as there is no access to new added
     // fields from the form!
     $plans = $_POST['_plan'];
     unset($plans['TPL']);
     foreach ($plans as $k => &$arr) {
         if ($arr['_rebill_times'] != 'x') {
             $arr['rebill_times'] = $arr['_rebill_times'];
         }
         try {
             $p = new Am_Period($arr['first_period']['c'], $arr['first_period']['u']);
             $arr['first_period'] = (string) $p;
         } catch (Am_Exception_InternalError $e) {
             unset($plans[$k]);
             continue;
         }
         try {
             $p = new Am_Period($arr['second_period']['c'], $arr['second_period']['u']);
             $arr['second_period'] = (string) $p;
         } catch (Am_Exception_InternalError $e) {
             $arr['second_period'] = '';
         }
     }
     foreach ($existing as $k => $plan) {
         if (empty($plans[$plan->pk()])) {
             $plan->delete();
         } else {
             $plan->setForUpdate($plans[$plan->pk()]);
             $plan->update();
             unset($plans[$plan->pk()]);
         }
     }
     foreach ($plans as $id => $a) {
         $plan = $this->getDi()->billingPlanRecord;
         $plan->setForInsert($a);
         $plan->product_id = $record->pk();
         $plan->insert();
     }
     // temp. stub
     $record->updateQuick('default_billing_plan_id', $this->getDi()->db->selectCell("SELECT MIN(plan_id) FROM ?_billing_plan WHERE product_id=?d\n                AND (disabled IS NULL OR disabled = 0)", $record->product_id));
 }