/** * main action */ public function mainAction() { require_once 'models/ecommerce/ecommerce_promotion.php'; $Promotion = new ecommerce_promotion(); /** * Save on request */ if (is_array($_POST['promotion'])) { $_POST['promotion']['type'] = 1; // can add discount coupon only if ($Promotion->addPromotion($_POST['promotion'])) { //onxshopGoTo("/backoffice/marketing"); msg('Inserted'); } else { msg('Insert failed', 'error'); } } /** * Display Detail */ if (count($_POST['promotion']) > 0) { $this->tpl->assign('PROMOTION', $_POST['promotion']); } return true; }
/** * generateSingleVoucher */ public function generateSingleVoucher($voucher_basket_item) { if (!is_array($voucher_basket_item)) { return false; } $voucher_data = array(); $voucher_data['variety_id'] = $voucher_basket_item['product_variety_id']; $voucher_data['recipient_name'] = $voucher_basket_item['other_data']['recipient_name']; $voucher_data['recipient_email'] = $voucher_basket_item['other_data']['recipient_email']; $voucher_data['message'] = $voucher_basket_item['other_data']['message']; $voucher_data['sender_name'] = $voucher_basket_item['other_data']['sender_name']; if ($voucher_basket_item['other_data']['delivery_date']) { $voucher_data['delivery_date'] = $voucher_basket_item['other_data']['delivery_date']; } if (!$this->validateData($voucher_data)) { msg("Voucher data are not valid", 'error'); return false; } /** * create discount code */ $code_pattern_base = "GIFT-{$voucher_basket_item['id']}" . '-'; $promotion_data = array(); $promotion_data['type'] = 4; // Gift Voucher $promotion_data['code_pattern'] = $code_pattern_base . $this->randomCode(); $promotion_data['title'] = $promotion_data['code_pattern']; $promotion_data['discount_percentage_value'] = 0; $promotion_data['discount_fixed_value'] = $voucher_basket_item['total']; $promotion_data['uses_per_coupon'] = $voucher_basket_item['quantity']; $promotion_data['other_data'] = $voucher_basket_item['other_data']; $promotion_data['publish'] = 1; $promotion_data['generated_by_order_id'] = $this->GET['order_id']; require_once 'models/ecommerce/ecommerce_promotion.php'; $Promotion = new ecommerce_promotion(); $Promotion->setCacheable(false); //TODO: check if the code wasn't generated before for the same order if ($Promotion->checkCodeMatchPartially($code_pattern_base)) { msg("Code {$code_pattern_base}* was previously generated", 'error'); return false; } //preg_match("/GIFT-{$voucher_basket_item['id']}/", $all_patterns_list) if ($promotion_id = $Promotion->addPromotion($promotion_data)) { msg("Promotion code {$promotion_data['code_pattern']} generated as promotion ID {$promotion_id}", 'ok', 1); } else { msg('Promotion code generation failed', 'error'); //return false; } /** * create the voucher file */ $url = "http://{$_SERVER['SERVER_NAME']}/request/sys/html5.node/site/print.component/ecommerce/gift_voucher~voucher_code={$promotion_data['code_pattern']}~"; $gift_voucher_directory = ONXSHOP_PROJECT_DIR . "var/vouchers/"; $gift_voucher_filename = "{$promotion_data['code_pattern']}.png"; $gift_voucher_filename_fullpath = $gift_voucher_directory . $gift_voucher_filename; //check directory exits if (!is_dir($gift_voucher_directory)) { mkdir($gift_voucher_directory); } $shell_command = "wkhtmltoimage {$url} {$gift_voucher_filename_fullpath}"; if ($result = local_exec($shell_command)) { msg("File {$gift_voucher_filename_fullpath} generated by wkhtmltoimage", 'ok', 1); } /** * send email * postpone if delivery_date is set */ if (preg_match("/[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4}/", $voucher_data['delivery_date'])) { $this->postponeDelivery($promotion_id, $voucher_data['delivery_date']); } else { $this->sendEmail($promotion_data, $voucher_data, $gift_voucher_filename); } return true; }