123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284 |
- <?php
- if (!(defined('IN_IA')))
- {
- exit('Access Denied');
- }
- class CashierModel extends PluginModel
- {
- const APPLY = 'apply';
- const CHECKED = 'checked';
- const APPLY_CLEARING = 'apply_clearing';
- const PAY = 'pay';
- const PAY_CASHIER = 'pay_cashier';
- const PAY_CASHIER_USER = 'pay_cashier_user';
- static public $paytype = array(0 => '微信', 1 => '支付宝', 2 => '商城余额', 3 => '现金收款', 101 => '系统微信');
- public $setmeal = array('标准套餐', '豪华套餐');
- static public $UserSet = array();
- static public function perm()
- {
- $perm = array('index' => '我要收款', 'goods' => '商品收款', 'order' => '收款订单', 'statistics' => '收银统计', 'sysset' => '设置', 'sale' => '营销', 'clearing' => '提现', 'goodsmanage' => '商品管理');
- if (empty($_W['cashieruser']['can_withdraw']))
- {
- unset($perm['clearing']);
- }
- return $perm;
- }
- public function getUserSet($name = '', $cashierid)
- {
- global $_W;
- if (!(isset(static::$UserSet[$cashierid])))
- {
- $user = $this->userInfo($cashierid);
- $set = ((empty($user['set']) ? array() : json_decode($user['set'], true)));
- static::$UserSet[$cashierid] = $set;
- }
- if (empty($name))
- {
- return static::$UserSet[$cashierid];
- }
- return (isset(static::$UserSet[$cashierid][$name]) ? static::$UserSet[$cashierid][$name] : '');
- }
- public function updateUserSet($data = array(), $cashierid)
- {
- global $_W;
- $user = $this->userInfo($cashierid);
- $set = ((empty($user['set']) ? array() : json_decode($user['set'], true)));
- $set = json_encode(array_merge($set, $data));
- return pdo_query('UPDATE ' . tablename('ewei_shop_cashier_user') . ' SET `set`=:set WHERE `uniacid` = :uniacid AND `id` = :id', array(':uniacid' => $_W['uniacid'], ':id' => $cashierid, ':set' => $set));
- }
- public function payResult($logid, $return_log = false)
- {
- global $_W;
- $id = intval($logid);
- if ($id != 0)
- {
- $log = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_cashier_pay_log') . ' WHERE `id`=:id and `uniacid`=:uniacid limit 1', array(':uniacid' => $_W['uniacid'], ':id' => $id));
- }
- else
- {
- $log = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_cashier_pay_log') . ' WHERE `logno`=:logno and `uniacid`=:uniacid limit 1', array(':uniacid' => $_W['uniacid'], ':logno' => $logid));
- }
- if (!(empty($log)))
- {
- $_W['cashierid'] = $log['cashierid'];
- $res = $this->updateOrder($log);
- if ($res && ($log['status'] != 1))
- {
- $log['status'] = 1;
- $log['paytime'] = time();
- }
- return ($return_log ? $log : $res);
- }
- return false;
- }
- public function categoryAll($status = 1)
- {
- global $_W;
- $status = intval($status);
- $condition = ' and uniacid=:uniacid and status=' . intval($status);
- $params = array(':uniacid' => $_W['uniacid']);
- $item = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_cashier_category') . ' WHERE 1 ' . $condition . ' ORDER BY displayorder desc, id DESC', $params);
- return $item;
- }
- public function categoryOne($id)
- {
- global $_W;
- $item = pdo_fetch('select * from ' . tablename('ewei_shop_cashier_category') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $id, ':uniacid' => $_W['uniacid']));
- return $item;
- }
- public function savaUser(array $params, $diyform = array())
- {
- global $_W;
- $diyform_flag = 0;
- $diyform_plugin = p('diyform');
- $f_data = array();
- if ($diyform_plugin && !(empty($_W['shopset']['cashier']['apply_diyform'])))
- {
- if (!(empty($item['diyformdata'])))
- {
- $diyform_flag = 1;
- $fields = iunserializer($item['diyformfields']);
- $f_data = iunserializer($item['diyformdata']);
- }
- else
- {
- $diyform_id = $_W['shopset']['cashier']['apply_diyformid'];
- if (!(empty($diyform_id)))
- {
- $formInfo = $diyform_plugin->getDiyformInfo($diyform_id);
- if (!(empty($formInfo)))
- {
- $diyform_flag = 1;
- $fields = $formInfo['fields'];
- }
- }
- }
- }
- $fdata = array();
- if ($diyform_flag)
- {
- $fdata = p('diyform')->getPostDatas($fields);
- if (is_error($fdata))
- {
- show_json(0, $fdata['message']);
- }
- if ($diyform_flag)
- {
- $params['diyformdata'] = iserializer($fdata);
- if (!(empty($diyform)))
- {
- $insert_data = $diyform_plugin->getInsertData($fields, $diyform);
- $params['diyformdata'] = $insert_data['data'];
- }
- $params['diyformfields'] = iserializer($fields);
- }
- }
- if (empty($params['title']))
- {
- show_json(0, '请填写收银台名称!');
- }
- if (empty($params['manageopenid']))
- {
- show_json(0, '请填写管理微信号!');
- }
- if (empty($params['name']))
- {
- show_json(0, '请填写联系人!');
- }
- if (empty($params['mobile']))
- {
- show_json(0, '请填写联系电话!');
- }
- if (empty($params['username']))
- {
- show_json(0, '请填写后台登录用户名!');
- }
- if (!(empty($params['password'])))
- {
- $params['salt'] = random(8);
- $params['password'] = md5(trim($params['password']) . $params['salt']);
- }
- else
- {
- unset($params['password']);
- }
- $params['storeid'] = intval($params['storeid']);
- $params['merchid'] = intval($params['merchid']);
- $params['isopen_commission'] = intval($params['isopen_commission']);
- $params['title'] = trim($params['title']);
- $params['logo'] = trim($params['logo']);
- $params['openid'] = trim($params['openid']);
- $params['manageopenid'] = trim($params['manageopenid']);
- $params['name'] = trim($params['name']);
- $params['mobile'] = trim($params['mobile']);
- $params['username'] = trim($params['username']);
- $params['withdraw'] = floatval($params['withdraw']);
- $params['wechat_status'] = intval($params['wechat_status']);
- $params['alipay_status'] = intval($params['alipay_status']);
- if (isset($params['deleted']))
- {
- $params['deleted'] = intval($params['deleted']);
- }
- if (!(isset($params['id'])))
- {
- $params['createtime'] = TIMESTAMP;
- $params['deleted'] = 0;
- pdo_insert('ewei_shop_cashier_user', $params);
- $params['id'] = pdo_insertid();
- }
- else
- {
- pdo_update('ewei_shop_cashier_user', $params, array('id' => $params['id'], 'uniacid' => $params['uniacid']));
- }
- return $params;
- }
- public function userInfo($openid)
- {
- global $_W;
- $id = intval($openid);
- $sql = 'SELECT * FROM ' . tablename('ewei_shop_cashier_user') . ' WHERE uniacid=:uniacid AND deleted=0';
- $params = array(':uniacid' => $_W['uniacid']);
- if ($id == 0)
- {
- $sql .= ' AND openid=:openid';
- $params[':openid'] = $openid;
- }
- else
- {
- $sql .= ' AND id=:id';
- $params[':id'] = $id;
- }
- $res = pdo_fetch($sql . ' LIMIT 1', $params);
- return $res;
- }
- public function sendMessage($params, $type, $openid = NULL)
- {
- global $_W;
- if (isset($params['createtime']))
- {
- $params['createtime'] = date('Y-m-d H:i:s', $params['createtime']);
- }
- if (isset($params['paytime']))
- {
- $params['paytime'] = date('Y-m-d H:i:s', $params['paytime']);
- }
- $data = m('common')->getPluginset('cashier');
- $notice = $data['notice'];
- if (empty($notice[$type]))
- {
- return false;
- }
- switch ($type)
- {
- case self: $datas = array('[联系人]' => $params['name'], '[联系电话]' => $params['mobile'], '[申请时间]' => date('Y-m-d H:i:s', $params['createtime']));
- break;
- switch ($type)
- {
- case self: $datas = array('[联系人]' => $params['name'], '[联系电话]' => $params['mobile'], '[审核状态]' => $params['status'], '[审核时间]' => $params['createtime'], '[驳回原因]' => $params['reason']);
- break;
- switch ($type)
- {
- case self: $datas = array('[联系人]' => $params['name'], '[联系电话]' => $params['mobile'], '[申请时间]' => $params['createtime'], '[申请金额]' => $params['money']);
- break;
- switch ($type)
- {
- case self: $datas = array('[联系人]' => $params['name'], '[联系电话]' => $params['mobile'], '[申请时间]' => $params['createtime'], '[打款时间]' => $params['paytime'], '[申请金额]' => $params['money'], '[打款金额]' => $params['realmoney']);
- break;
- switch ($type)
- {
- case self: $datas = array('[订单编号]' => $params['logno'], '[付款金额]' => $params['money'], '[余额抵扣]' => $params['deduction'], '[付款时间]' => $params['paytime'], '[收银台名称]' => $params['cashier_title']);
- break;
- switch ($type)
- {
- case self: $datas = array('[订单编号]' => $params['logno'], '[付款金额]' => $params['money'], '[余额抵扣]' => $params['deduction'], '[付款时间]' => $params['paytime'], '[收银台名称]' => $params['cashier_title']);
- break;
- break;
- $datas = ((isset($datas) ? $datas : array()));
- }
- }
- }
- }
- }
- }
- }
- protected function sendNotice($notice, $tag, $datas)
- {
- global $_W;
- if (!(empty($notice[$tag])))
- {
- $advanced_template = pdo_fetch('select * from ' . tablename('ewei_shop_member_message_template') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $notice[$tag], ':uniacid' => $_W['uniacid']));
- if (!(empty($advanced_template)))
- {
- $url = ((!(empty($advanced_template['url'])) ? $this->replaceArray($datas, $advanced_template['url']) : ''));
- $advanced_message = array( 'first' => array('value' => $this->replaceArray($datas, $advanced_template['first']), 'color' => $advanced_template['firstcolor']), 'remark' => array('value' => $this->replaceArray($datas, $advanced_template['remark']), 'color' => $advanced_template['remarkcolor']) );
- $data = iunserializer($advanced_template['data']);
- foreach ($data as $d )
- {
- $advanced_message[$d['keywords']] = array('value' => $this->replaceArray($datas, $d['value']), 'color' => $d['color']);
- }
- if (!(empty($notice['openid'])))
- {
- $notice['openid'] = ((is_array($notice['openid']) ? $notice['openid'] : explode(',', $notice['openid'])));
- foreach ($notice['openid'] as $openid )
- {
- if (!(empty($notice[$tag])) && !(empty($advanced_template['template_id'])))
- {
- m('message')->sendTplNotice($openid, $advanced_template['template_id'], $advanced_message, $url);
- }
- else
- {
- m('message')->sendCustomNotice($openid, $advanced_message, $url);
- }
- }
- }
- }
- }
- return false;
- }
- protected function replaceArray(array $array, $message)
- {
- foreach ($array as $key => $value )
- {
- $message = str_replace($key, $value, $message);
- }
- return $message;
- }
- public function wechayPayInfo($user)
- {
- $wechatpay = json_decode($user['wechatpay'], true);
- if (empty($wechatpay['appid']) || empty($wechatpay['mch_id']))
- {
- $wechat = array('appid' => $wechatpay['sub_appid'], 'mch_id' => $wechatpay['sub_mch_id'], 'apikey' => $wechatpay['apikey']);
- }
- else
- {
- $wechat = $wechatpay;
- }
- return $wechat;
- }
- public function wechatpay($params)
- {
- global $_W;
- $wechat = $this->wechayPayInfo($_W['cashieruser']);
- return m('common')->wechat_micropay_build($params, $wechat, 13);
- }
- public function wechatpay_101($params)
- {
- global $_W;
- $set = m('common')->getSysset(array('shop', 'pay'));
- if (isset($set['pay']) && ($set['pay']['weixin'] == 1))
- {
- load()->model('payment');
- $setting = uni_setting($_W['uniacid'], array('payment'));
- $account = pdo_get('account_wechats', array('uniacid' => $_W['uniacid']), array('key', 'secret'));
- if (is_array($setting['payment']))
- {
- $options = $setting['payment']['wechat'];
- $options['appid'] = $account['key'];
- $options['secret'] = $account['secret'];
- $wechat = array('appid' => $options['appid'], 'mch_id' => $options['mchid'], 'apikey' => $options['signkey']);
- }
- }
- else if (isset($set['pay']) && ($set['pay']['weixin_sub'] == 1))
- {
- $sec = m('common')->getSec();
- $sec = iunserializer($sec['sec']);
- $wechat = array('appid' => $sec['appid_sub'], 'mch_id' => $sec['mchid_sub'], 'sub_appid' => (!(empty($sec['sub_appid_sub'])) ? $sec['sub_appid_sub'] : ''), 'sub_mch_id' => $sec['sub_mchid_sub'], 'apikey' => $sec['apikey_sub']);
- }
- if (empty($wechat))
- {
- $sec = m('common')->getSec();
- $sec = iunserializer($sec['sec']);
- if (isset($set['pay']) && ($set['pay']['weixin_jie'] == 1))
- {
- $wechat = array('appid' => $sec['appid'], 'mch_id' => $sec['mchid'], 'apikey' => $sec['apikey']);
- }
- else if (isset($set['pay']) && ($set['pay']['weixin_jie_sub'] == 1))
- {
- $wechat = array('appid' => $sec['appid_jie_sub'], 'mch_id' => $sec['mchid_jie_sub'], 'sub_appid' => (!(empty($sec['sub_appid_jie_sub'])) ? $sec['sub_appid_jie_sub'] : ''), 'sub_mch_id' => $sec['sub_mchid_jie_sub'], 'apikey' => $sec['apikey_jie_sub']);
- }
- }
- return (isset($wechat) ? m('common')->wechat_micropay_build($params, $wechat, 13) : error(-1, '$wechat' . '参数错误'));
- }
- public function alipay($params)
- {
- global $_W;
- return m('common')->AliPayBarcode($params, json_decode($_W['cashieruser']['alipay'], true));
- }
- public function createOrder(array $array, $return = NULL, $can_sale = true)
- {
- global $_W;
- if (empty($array))
- {
- return 0;
- }
- $array['operatorid'] = ((isset($array['operatorid']) ? $array['operatorid'] : 0));
- $array['deduction'] = ((isset($array['deduction']) ? $array['deduction'] : 0));
- $realmoney = $array['money'];
- $sale = array();
- if ((0 < $realmoney) && $can_sale)
- {
- if (!(empty($array['usecoupon'])))
- {
- $usecoupon = $this->caculatecoupon($array['usecoupon'], $realmoney);
- $realmoney = $usecoupon['new_price'];
- }
- $sale = $this->saleCalculate($realmoney);
- $realmoney = $sale['money'];
- }
- if ($realmoney < $array['deduction'])
- {
- $array['deduction'] = $realmoney;
- }
- $realmoney = $realmoney - $array['deduction'];
- $title = $_W['cashieruser']['title'] . '消费';
- if (!(empty($array['title'])))
- {
- $title = $array['title'];
- }
- $data = array('uniacid' => $_W['uniacid'], 'cashierid' => $_W['cashierid'], 'operatorid' => $array['operatorid'], 'paytype' => $array['paytype'], 'openid' => (isset($array['openid']) ? $array['openid'] : ''), 'logno' => 'CS' . date('YmdHis') . mt_rand(10000, 99999), 'title' => $title, 'createtime' => time(), 'money' => $realmoney, 'randommoney' => (isset($sale['randommoney']) ? $sale['randommoney'] : 0), 'enough' => (isset($sale['enough']['money']) ? $sale['enough']['money'] : 0), 'mobile' => (isset($array['mobile']) ? $array['mobile'] : 0), 'deduction' => (isset($array['deduction']) ? $array['deduction'] : 0), 'discountmoney' => (isset($sale['discount']['money']) ? $sale['discount']['money'] : 0), 'discount' => (isset($sale['discount']['discount']) ? $sale['discount']['discount'] : 0), 'couponpay' => (isset($array['couponpay']) ? $array['couponpay'] : 0), 'nosalemoney' => (isset($array['nosalemoney']) ? $array['nosalemoney'] : 0), 'coupon' => (isset($array['coupon']) ? $array['coupon'] : 0), 'usecoupon' => (isset($array['usecoupon']) ? $array['usecoupon'] : 0), 'usecouponprice' => (isset($usecoupon['money']) ? $usecoupon['money'] : 0), 'status' => 0);
- $res = pdo_insert('ewei_shop_cashier_pay_log', $data);
- if (!($res))
- {
- return error(-2, '数据插入异常,请重试!');
- }
- $data['id'] = pdo_insertid();
- if (!(empty($usecoupon)))
- {
- pdo_update('ewei_shop_coupon_data', array('used' => 1, 'usetime' => $data['createtime'], 'ordersn' => $data['logno']), array('id' => $array['usecoupon']));
- }
- if ($return !== NULL)
- {
- return $data;
- }
- return $this->pay($data, (isset($array['auth_code']) ? $array['auth_code'] : NULL));
- }
- public function pay($data, $auth_code = NULL)
- {
- global $_W;
- if (($data['money'] <= 0) || ($data['paytype'] == 3))
- {
- $data['status'] = 1;
- $data['paytype'] = (($data['paytype'] == 3 ? $data['paytype'] : 2));
- $data['deduction'] -= $data['randommoney'];
- $data['paytime'] = time();
- pdo_update('ewei_shop_cashier_pay_log', $data, array('id' => $data['id']));
- m('member')->setCredit($data['openid'], 'credit2', -$data['deduction'], array(0, '收银台 ' . $_W['cashieruser']['title'], '收款'));
- $user = $this->userInfo($data['cashierid']);
- $this->paySuccess($data, $user);
- return array('res' => true, 'id' => $data['id']);
- }
- $params = array('title' => $data['title'], 'tid' => $data['logno'], 'fee' => $data['money']);
- $params['out_trade_no'] = $params['tid'];
- $params['total_amount'] = $params['fee'];
- $params['subject'] = $params['title'];
- $params['body'] = $_W['uniacid'] . ':' . 2;
- if ($auth_code !== NULL)
- {
- $params['auth_code'] = $auth_code;
- }
- if ($data['paytype'] == 0)
- {
- $res = $this->wechatpay($params);
- }
- else if ($data['paytype'] == 1)
- {
- $res = $this->alipay($params);
- }
- else
- {
- $res = $this->wechatpay_101($params);
- }
- return array('res' => $res, 'id' => $data['id']);
- }
- public function orderQuery($pay_log)
- {
- $array = array();
- if (is_array2($pay_log))
- {
- foreach ($pay_log as $value )
- {
- if ($value['status'] == '0')
- {
- $res = $this->updateOrder($value);
- if ($res)
- {
- $array[] = $res;
- }
- }
- }
- }
- else if (is_array($pay_log))
- {
- $res = $this->updateOrder($pay_log);
- if ($res)
- {
- $array[] = $res;
- }
- }
- else
- {
- $res = $this->payResult($pay_log);
- if ($res)
- {
- $array[] = $res;
- }
- }
- return $array;
- }
- public function updateOrder($log)
- {
- global $_W;
- if (!(empty($log['status'])))
- {
- return (int) $log['id'];
- }
- $realmoney = floatval($log['money']);
- $user = $this->userInfo($log['cashierid']);
- if ($log['paytype'] != '101')
- {
- if (empty($log['paytype']))
- {
- $wechat = $this->wechayPayInfo($user);
- $res = m('common')->wechat_order_query($log['logno'], 0, $wechat);
- }
- else if ($log['paytype'] == '1')
- {
- $alipay = json_decode($user['alipay'], true);
- $res = m('common')->AliPayQuery($log['logno'], $alipay);
- }
- }
- else
- {
- $set = m('common')->getSysset(array('shop', 'pay'));
- if (isset($set['pay']) && ($set['pay']['weixin'] == 1))
- {
- load()->model('payment');
- $setting = uni_setting($_W['uniacid'], array('payment'));
- $account = pdo_get('account_wechats', array('uniacid' => $_W['uniacid']), array('key', 'secret'));
- if (is_array($setting['payment']))
- {
- $options = $setting['payment']['wechat'];
- $options['appid'] = $account['key'];
- $options['secret'] = $account['secret'];
- if (IMS_VERSION <= 0.80000000000000004)
- {
- $options['apikey'] = $options['signkey'];
- }
- $wechat = array('appid' => $options['appid'], 'mch_id' => $options['mchid'], 'apikey' => $options['apikey']);
- $res = m('common')->wechat_order_query($log['logno'], 0, $wechat);
- }
- }
- else if (isset($set['pay']) && ($set['pay']['weixin_sub'] == 1))
- {
- $sec = m('common')->getSec();
- $sec = iunserializer($sec['sec']);
- $wechat = array('appid' => $sec['appid_sub'], 'mch_id' => $sec['mchid_sub'], 'sub_appid' => (!(empty($sec['sub_appid_sub'])) ? $sec['sub_appid_sub'] : ''), 'sub_mch_id' => $sec['sub_mchid_sub'], 'apikey' => $sec['apikey_sub']);
- $res = m('common')->wechat_order_query($log['logno'], 0, $wechat);
- }
- if (empty($res))
- {
- $sec = m('common')->getSec();
- $sec = iunserializer($sec['sec']);
- if (isset($set['pay']) && ($set['pay']['weixin_jie'] == 1))
- {
- $wechat = array('appid' => $sec['appid'], 'mch_id' => $sec['mchid'], 'apikey' => $sec['apikey']);
- $res = m('common')->wechat_order_query($log['logno'], 0, $wechat);
- }
- else if (isset($set['pay']) && ($set['pay']['weixin_jie_sub'] == 1))
- {
- $wechat = array('appid' => $sec['appid_jie_sub'], 'mch_id' => $sec['mchid_jie_sub'], 'sub_appid' => (!(empty($sec['sub_appid_jie_sub'])) ? $sec['sub_appid_jie_sub'] : ''), 'sub_mch_id' => $sec['sub_mchid_jie_sub'], 'apikey' => $sec['apikey_jie_sub']);
- $res = m('common')->wechat_order_query($log['logno'], 0, $wechat);
- }
- }
- }
- if (empty($res))
- {
- return false;
- }
- if (($res['total_fee'] == round($realmoney * 100, 2)) || ($res['total_amount'] == round($realmoney, 2)))
- {
- $log = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_cashier_pay_log') . ' WHERE `id`=:id and `uniacid`=:uniacid limit 1', array(':uniacid' => $_W['uniacid'], ':id' => $log['id']));
- if (empty($log['status']))
- {
- $res['openid'] = ((isset($res['openid']) ? $res['openid'] : ''));
- $log['openid'] = ((empty($log['openid']) ? $res['openid'] : $log['openid']));
- $this->paySuccess($log, $user, $res);
- }
- return (int) $log['id'];
- }
- }
- public function paySuccess($log, $user)
- {
- global $_W;
- $coupon = 0;
- if (!(empty($log['openid'])))
- {
- if ($user['isopen_commission'])
- {
- $this->becomeDown($log['openid'], $user['manageopenid']);
- }
- $this->sendMessage(array('logno' => $log['logno'], 'money' => $log['money'], 'deduction' => $log['deduction'], 'paytime' => time(), 'cashier_title' => $user['title']), self::PAY_CASHIER_USER, $log['openid']);
- $coupon = $this->seedCoupon($log['money'] + $log['deduction'], $log['openid']);
- }
- pdo_update('ewei_shop_cashier_pay_log', array('openid' => $log['openid'], 'payopenid' => $log['openid'], 'money' => $log['money'], 'status' => 1, 'paytime' => (0 < $log['paytime'] ? $log['paytime'] : time()), 'coupon' => $coupon), array('id' => $log['id']));
- $log['deduction'] = (double) $log['deduction'];
- if (!(empty($log['deduction'])))
- {
- $userinfo = m('member')->getMobileMember($log['mobile']);
- m('member')->setCredit($userinfo['openid'], 'credit2', -$log['deduction'], array(0, '收银台 ' . $_W['cashieruser']['title'], '收款'));
- }
- if (!(empty($log['orderid'])))
- {
- $paytype = 1;
- if (empty($log['paytype']) || ($log['paytype'] == '101'))
- {
- $paytype = 21;
- }
- if ($log['paytype'] == '1')
- {
- $paytype = 22;
- }
- pdo_update('ewei_shop_order', array('status' => 3, 'paytype' => $paytype, 'paytime' => time(), 'sendtime' => time(), 'finishtime' => time()), array('id' => $log['orderid'], 'uniacid' => $_W['uniacid']));
- $this->setStocks($log['orderid']);
- if (p('commission'))
- {
- p('commission')->checkOrderPay($log['orderid']);
- p('commission')->checkOrderFinish($log['orderid']);
- }
- }
- if (!(empty($log['isgoods'])))
- {
- $this->setSelfGoodsStocks($log['id']);
- }
- $operator = false;
- if (!(empty($log['operatorid'])))
- {
- $operator = pdo_fetch('select * from ' . tablename('ewei_shop_cashier_operator') . ' WHERE id=:id AND cashierid=:cashierid limit 1', array(':id' => $log['operatorid'], ':cashierid' => $log['cashierid']));
- }
- if ($operator)
- {
- $user['manageopenid'] = $operator['manageopenid'];
- }
- $this->sendMessage(array('logno' => $log['logno'], 'money' => $log['money'], 'deduction' => $log['deduction'], 'paytime' => time(), 'cashier_title' => $user['title']), self::PAY_CASHIER, $user['manageopenid']);
- $userset = json_decode($user['set'], true);
- if (!(empty($log['openid'])))
- {
- $present_credit1 = $this->sendCredit1($log, $userset);
- if (0 < $present_credit1)
- {
- pdo_update('ewei_shop_cashier_pay_log', array('present_credit1' => $present_credit1), array('id' => $log['id']));
- }
- }
- if ((!(empty($log['isgoods'])) || !(empty($log['orderid']))) && !(empty($userset['printer_status'])) && !(empty($userset['printer'])) && !(empty($userset['printer_template'])))
- {
- com_run('printer::sendCashierMessage', $log, $userset['printer_template'], $userset['printer'], $operator);
- }
- else if (!(empty($userset['printer_status'])) && !(empty($userset['printer'])) && !(empty($userset['printer_template_default'])))
- {
- com_run('printer::sendCashier', $log, $userset['printer_template_default'], $userset['printer'], $operator);
- }
- }
- public function paytype($paytype = 0, $auto_code = NULL)
- {
- global $_W;
- if (is_null($paytype) && ($auto_code !== NULL) && (substr($auto_code, 0, 2) == '28'))
- {
- return error(-101, '暂时不支持支付宝支付!');
- }
- if (($paytype == -1) && ($auto_code !== NULL))
- {
- $type = substr($auto_code, 0, 2);
- if ($type == '13')
- {
- $paytype = 0;
- }
- else if ($type == '28')
- {
- if (empty($_W['cashieruser']['alipay_status']))
- {
- return error(-101, '暂时不支持支付宝支付!');
- }
- $paytype = 1;
- }
- }
- if (empty($paytype) && !(empty($_W['cashieruser']['wechat_status'])))
- {
- $paytype = 0;
- }
- else if ($paytype == '1')
- {
- $paytype = 1;
- }
- else if ($paytype == '2')
- {
- $paytype = 2;
- }
- else if ($paytype == '3')
- {
- $paytype = 3;
- }
- else
- {
- $paytype = 101;
- }
- return $paytype;
- }
- public function getrandommoney($money = 0)
- {
- global $_W;
- $userset = $this->getUserSet('', $_W['cashierid']);
- if (isset($userset['randtime']) && !($this->sale_time($userset['randtime'])))
- {
- return 0;
- }
- $probability = $userset['rand'];
- if (empty($probability))
- {
- return 0;
- }
- if (!(empty($probability['minmoney'])) && ($money < floatval($probability['minmoney'])))
- {
- return 0;
- }
- $sum = 0;
- $i = 0;
- while ($i < count($probability['rand']))
- {
- $sum += $probability['rand'][$i];
- $rand_num = rand(1, 100);
- if ($rand_num <= $sum)
- {
- $loop = $i;
- break;
- }
- ++$i;
- }
- $min = (double) $probability['rand_left'][$loop] * 100;
- $max = (double) $probability['rand_right'][$loop] * 100;
- return rand($min, $max) / 100;
- }
- public function becomeDown($openid, $manageopenid)
- {
- $member = m('member')->getMember($openid);
- if ($member)
- {
- if (empty($member['isagent']) && empty($member['agentid']))
- {
- $store_member = m('member')->getMember($manageopenid);
- pdo_query('UPDATE ' . tablename('ewei_shop_member') . ' SET agentid=' . $store_member['id'] . ' WHERE `id` = :id', array(':id' => $member['id']));
- }
- }
- }
- public function getTodayOrder()
- {
- $starttime = strtotime(date('Y-m-d'));
- $endtime = time();
- return $this->getOrderMoney($starttime, $endtime);
- }
- public function getWeekOrder()
- {
- $starttime = strtotime(date('Y-m-d')) - ((date('w') - 1) * 3600 * 24);
- $endtime = time();
- return $this->getOrderMoney($starttime, $endtime);
- }
- public function getMonthOrder()
- {
- $starttime = strtotime(date('Y-m') . '-1');
- $endtime = time();
- return $this->getOrderMoney($starttime, $endtime);
- }
- public function getOrderMoney($starttime = 0, $endtime = 0)
- {
- global $_W;
- if (($starttime == 0) && ($endtime == 0))
- {
- $money = (double) pdo_fetchcolumn('SELECT SUM(money+deduction) FROM ' . tablename('ewei_shop_cashier_pay_log') . ' WHERE uniacid=:uniacid AND status=1 AND cashierid=:cashierid ', array(':uniacid' => $_W['uniacid'], ':cashierid' => $_W['cashierid']));
- }
- else
- {
- $money = (double) pdo_fetchcolumn('SELECT SUM(money+deduction) FROM ' . tablename('ewei_shop_cashier_pay_log') . ' WHERE uniacid=:uniacid AND status=1 AND cashierid=:cashierid AND createtime BETWEEN :starttime AND :endtime', array(':uniacid' => $_W['uniacid'], ':cashierid' => $_W['cashierid'], ':starttime' => $starttime, ':endtime' => $endtime));
- }
- return $money;
- }
- public function getEnoughs($price = 0)
- {
- global $_W;
- $set = $this->getUserSet('', $_W['cashierid']);
- if (isset($set['enoughtime']) && !($this->sale_time($set['enoughtime'])))
- {
- return array('old_price' => $price, 'new_price' => $price, 'enough' => 0, 'money' => 0);
- }
- $allenoughs = array();
- $enoughs = $set['enoughs'];
- if ((0 < floatval($set['enoughmoney'])) && (0 < floatval($set['enoughdeduct'])))
- {
- $allenoughs[] = array('enough' => floatval($set['enoughmoney']), 'money' => floatval($set['enoughdeduct']));
- }
- if (is_array($enoughs))
- {
- foreach ($enoughs as $e )
- {
- if ((0 < floatval($e['enough'])) && (0 < floatval($e['give'])))
- {
- $allenoughs[] = array('enough' => floatval($e['enough']), 'money' => floatval($e['give']));
- }
- }
- }
- usort($allenoughs, 'sort_cashier');
- $new_price = $price;
- $enough = 0;
- $money = 0;
- foreach ($allenoughs as $key => $value )
- {
- if (($value['enough'] <= $price) && (0 < $value))
- {
- $new_price = $price - $value['money'];
- $enough = $value['enough'];
- $money = $value['money'];
- break;
- }
- }
- return array('old_price' => $price, 'new_price' => round($new_price, 2), 'enough' => $enough, 'money' => $money);
- }
- public function is_perm($text)
- {
- global $_W;
- if (isset($_W['cashieruser']['operator']))
- {
- $perm = json_decode($_W['cashieruser']['operator']['perm'], true);
- $routes = explode('.', $text);
- if (!(in_array($routes[0], $perm)))
- {
- return false;
- }
- }
- return true;
- }
- public function getDiscount($price = 0)
- {
- global $_W;
- $set = $this->getUserSet('', $_W['cashierid']);
- $set['discount'] = floatval($set['discount']);
- $price = floatval($price);
- if (empty($price) || empty($set['discount']) || (isset($set['discounttime']) && !($this->sale_time($set['discounttime']))))
- {
- return array('old_price' => $price, 'new_price' => $price, 'discount' => 0, 'money' => 0);
- }
- $new_price = 0;
- if (!(empty($set['discount'])))
- {
- $new_price = ($set['discount'] * $price) / 10;
- }
- return array('old_price' => $price, 'new_price' => round($new_price, 2), 'discount' => $set['discount'], 'money' => round($price - $new_price, 2));
- }
- public function sale_time($sale_time)
- {
- if (!(empty($sale_time)) && ($sale_time['start'] <= time()) && (time() <= $sale_time['end']))
- {
- if (empty($sale_time['start1']))
- {
- return true;
- }
- $hour = idate('H');
- $minute = idate('i');
- $return = false;
- foreach ($sale_time['start1'] as $key => $value )
- {
- if (($sale_time['start1'][$key] == $hour) && ($sale_time['start2'][$key] <= $minute))
- {
- if ((($sale_time['end1'][$key] == $hour) && ($minute <= $sale_time['end2'][$key])) || ($hour < $sale_time['end1'][$key]))
- {
- $return = true;
- }
- }
- if ($sale_time['start1'][$key] < $hour)
- {
- if ((($sale_time['end1'][$key] == $hour) && ($minute <= $sale_time['end2'][$key])) || ($hour < $sale_time['end1'][$key]))
- {
- $return = true;
- }
- }
- }
- return $return;
- }
- return false;
- }
- public function saleCalculate($money = 0, $random = true)
- {
- $randommoney = 0;
- if ($random)
- {
- $randommoney = $this->getrandommoney($money);
- }
- $realmoney = round($money - $randommoney, 2);
- $enoughs = $this->getEnoughs($realmoney);
- $discount = $this->getDiscount($enoughs['new_price']);
- return array('randommoney' => $randommoney, 'enough' => $enoughs, 'discount' => $discount, 'money' => $discount['new_price']);
- }
- public function createGoodsOrder($goods, $openid = '')
- {
- global $_W;
- global $_GPC;
- $allgoods = array();
- $realmoney = 0;
- $goodsprice = 0;
- foreach ($goods as $g )
- {
- if (empty($g))
- {
- continue;
- }
- $goodsid = intval($g['goodsid']);
- $optionid = intval($g['optionid']);
- $goodstotal = intval($g['total']);
- if ($goodstotal < 1)
- {
- $goodstotal = 1;
- }
- if (empty($goodsid))
- {
- show_json(0, '参数错误,未选择商品!');
- }
- $sql = 'SELECT id as goodsid,title,type, weight,total,issendfree,isnodiscount, thumb,marketprice,cash,isverify,verifytype,' . ' goodssn,productsn,sales,istime,timestart,timeend,hasoption,' . ' usermaxbuy,minbuy,maxbuy,unit,buylevels,buygroups,deleted,' . ' status,deduct,manydeduct,`virtual`,discounts,deduct2,ednum,edmoney,edareas,diyformtype,diyformid,diymode,' . ' dispatchtype,dispatchid,dispatchprice,merchid,merchsale,cates,' . ' isdiscount,isdiscount_time,isdiscount_discounts, virtualsend,' . ' buyagain,buyagain_islong,buyagain_condition, buyagain_sale' . ' FROM ' . tablename('ewei_shop_goods') . ' where id=:id and uniacid=:uniacid and cashier=1 limit 1';
- $data = pdo_fetch($sql, array(':uniacid' => $_W['uniacid'], ':id' => $goodsid));
- if (!(empty($data['hasoption'])))
- {
- $opdata = m('goods')->getOption($data['goodsid'], $optionid);
- if (empty($opdata) || empty($optionid))
- {
- show_json(0, '商品' . $data['title'] . '的规格不存在,请到购物车删除该商品重新选择规格!');
- }
- }
- $merchid = $data['merchid'];
- $merch_array[$merchid]['goods'][] = $data['goodsid'];
- $data['stock'] = $data['total'];
- $data['total'] = $goodstotal;
- if (!(empty($optionid)))
- {
- $option = pdo_fetch('select id,title,marketprice,goodssn,productsn,stock,`virtual`,weight from ' . tablename('ewei_shop_goods_option') . ' where id=:id and goodsid=:goodsid and uniacid=:uniacid limit 1', array(':uniacid' => $_W['uniacid'], ':goodsid' => $goodsid, ':id' => $optionid));
- if (!(empty($option)))
- {
- if ($option['stock'] != -1)
- {
- if (empty($option['stock']))
- {
- show_json(-1, $data['title'] . '<br/>' . $option['title'] . ' 库存不足!');
- }
- }
- $data['optionid'] = $optionid;
- $data['optiontitle'] = $option['title'];
- $data['marketprice'] = $option['marketprice'];
- if (!(empty($option['goodssn'])))
- {
- $data['goodssn'] = $option['goodssn'];
- }
- if (!(empty($option['productsn'])))
- {
- $data['productsn'] = $option['productsn'];
- }
- if (!(empty($option['weight'])))
- {
- $data['weight'] = $option['weight'];
- }
- }
- }
- $data['marketprice'] = ((isset($g['marketprice']) ? floatval($g['marketprice']) : $data['marketprice']));
- $gprice = $data['marketprice'] * $goodstotal;
- $diyprice = ((isset($g['price']) ? floatval($g['price']) : $data['marketprice']));
- $data['ggprice'] = $gprice;
- $data['realprice'] = $diyprice * $goodstotal;
- $realmoney += $data['realprice'];
- $goodsprice += $gprice;
- $allgoods[] = $data;
- }
- $ismerch = 0;
- if (0 < $_W['cashieruser']['merchid'])
- {
- $ismerch = 1;
- }
- if (0 < $ismerch)
- {
- $ordersn = m('common')->createNO('order', 'ordersn', 'ME');
- }
- else
- {
- $ordersn = m('common')->createNO('order', 'ordersn', 'SH');
- }
- $order = array();
- $order['ismerch'] = $ismerch;
- $order['parentid'] = 0;
- $order['uniacid'] = $_W['uniacid'];
- $order['openid'] = $openid;
- $order['ordersn'] = $ordersn;
- $order['price'] = $realmoney;
- $order['oldprice'] = $goodsprice;
- $order['grprice'] = $goodsprice;
- $order['cash'] = 0;
- $order['status'] = 0;
- $order['remark'] = trim($_GPC['remark']);
- $order['addressid'] = 0;
- $order['goodsprice'] = $goodsprice;
- $order['storeid'] = 0;
- $order['createtime'] = time();
- $order['paytype'] = 0;
- $order['merchshow'] = 0;
- $order['merchid'] = intval($_W['cashieruser']['merchid']);
- $order['isparent'] = 0;
- $order['transid'] = '';
- pdo_insert('ewei_shop_order', $order);
- $orderid = pdo_insertid();
- foreach ($allgoods as $goods )
- {
- $order_goods = array();
- $order_goods['merchid'] = $goods['merchid'];
- $order_goods['uniacid'] = $_W['uniacid'];
- $order_goods['orderid'] = $orderid;
- $order_goods['goodsid'] = $goods['goodsid'];
- $order_goods['price'] = $goods['marketprice'] * $goods['total'];
- $order_goods['total'] = $goods['total'];
- $order_goods['optionid'] = $goods['optionid'];
- $order_goods['createtime'] = time();
- $order_goods['optionname'] = $goods['optiontitle'];
- $order_goods['goodssn'] = $goods['goodssn'];
- $order_goods['productsn'] = $goods['productsn'];
- $order_goods['realprice'] = $goods['realprice'];
- $order_goods['oldprice'] = $goods['ggprice'];
- $order_goods['openid'] = $openid;
- pdo_insert('ewei_shop_order_goods', $order_goods);
- }
- $pluginc = p('commission');
- if ($pluginc)
- {
- $pluginc->checkOrderConfirm($orderid);
- }
- $order['id'] = $orderid;
- return $order;
- }
- public function goodsCalculate($selfgoods = array(), $shopgoods = array(), $params, $return = false)
- {
- global $_W;
- $order = $this->createOrder(array('paytype' => $params['paytype'], 'openid' => (isset($params['openid']) ? $params['openid'] : ''), 'money' => (double) $params['money'], 'couponpay' => (double) $params['couponpay'], 'nosalemoney' => (double) $params['nosalemoney'], 'operatorid' => (int) $params['operatorid'], 'deduction' => (double) $params['deduction'], 'mobile' => (int) $params['mobile'], 'title' => $params['title']), 1, false);
- if (!(empty($selfgoods)))
- {
- foreach ($selfgoods as $key => $val )
- {
- $order['goodsprice'] += $val['price'] * $val['total'];
- $order['isgoods'] = 1;
- $data = array('cashierid' => $order['cashierid'], 'logid' => $order['id'], 'goodsid' => $val['goodsid'], 'price' => $val['price'] * $val['total'], 'total' => $val['total']);
- $g = pdo_get('ewei_shop_cashier_goods', array('cashierid' => $data['cashierid'], 'id' => $data['goodsid']));
- if (($g['total'] < $data['total']) && ($g['total'] != -1))
- {
- pdo_delete('ewei_shop_cashier_pay_log_goods', array('cashierid' => $data['cashierid'], 'logid' => $data['logid']));
- return array('res' => error('-101', $g['title'] . ' 库存不足, 剩余库存 ' . $g['total']));
- }
- pdo_insert('ewei_shop_cashier_pay_log_goods', $data);
- }
- }
- if (!(empty($shopgoods)))
- {
- $goods = array();
- foreach ($shopgoods as $val )
- {
- $goods[] = array('goodsid' => $val['goodsid'], 'optionid' => $val['optionid'], 'price' => $val['price'], 'total' => $val['total'], 'marketprice' => (isset($val['marketprice']) ? $val['marketprice'] : NULL));
- }
- $goodsOrder = $this->createGoodsOrder($shopgoods, $params['openid']);
- $order['orderid'] = $goodsOrder['id'];
- $order['orderprice'] = $goodsOrder['price'];
- }
- if ($params['money'] == $order['orderprice'] + $order['goodsprice'])
- {
- pdo_update('ewei_shop_cashier_pay_log', $order, array('id' => $order['id']));
- if ($return)
- {
- return $order;
- }
- return $this->pay($order, $params['auth_code']);
- }
- return array('res' => error('-101', '支付金额与商品金额有误差!'));
- }
- public function setStocks($orderid = 0)
- {
- global $_W;
- if ($orderid == 0)
- {
- return false;
- }
- $order = pdo_fetch('select id,ordersn,price,openid,dispatchtype,addressid,carrier,status,isparent from ' . tablename('ewei_shop_order') . ' where id=:id limit 1', array(':id' => $orderid));
- $param = array();
- $param[':uniacid'] = $_W['uniacid'];
- if ($order['isparent'] == 1)
- {
- $condition = ' og.parentorderid=:parentorderid';
- $param[':parentorderid'] = $orderid;
- }
- else
- {
- $condition = ' og.orderid=:orderid';
- $param[':orderid'] = $orderid;
- }
- $goods = pdo_fetchall('select og.goodsid,og.total,g.totalcnf,og.realprice,g.credit,og.optionid,g.total as goodstotal,og.optionid,g.sales,g.salesreal from ' . tablename('ewei_shop_order_goods') . ' og ' . ' left join ' . tablename('ewei_shop_goods') . ' g on g.id=og.goodsid ' . ' where ' . $condition . ' and og.uniacid=:uniacid ', $param);
- foreach ($goods as $g )
- {
- $stocktype = -1;
- if (!(empty($stocktype)))
- {
- if (!(empty($g['optionid'])))
- {
- $option = m('goods')->getOption($g['goodsid'], $g['optionid']);
- if (!(empty($option)) && ($option['stock'] != -1))
- {
- $stock = -1;
- if ($stocktype == 1)
- {
- $stock = $option['stock'] + $g['total'];
- }
- else if ($stocktype == -1)
- {
- $stock = $option['stock'] - $g['total'];
- ($stock <= 0) && ($stock = 0);
- }
- if ($stock != -1)
- {
- pdo_update('ewei_shop_goods_option', array('stock' => $stock), array('uniacid' => $_W['uniacid'], 'goodsid' => $g['goodsid'], 'id' => $g['optionid']));
- }
- }
- }
- if (!(empty($g['goodstotal'])) && ($g['goodstotal'] != -1))
- {
- $totalstock = -1;
- if ($stocktype == 1)
- {
- $totalstock = $g['goodstotal'] + $g['total'];
- }
- else if ($stocktype == -1)
- {
- $totalstock = $g['goodstotal'] - $g['total'];
- ($totalstock <= 0) && ($totalstock = 0);
- }
- if ($totalstock != -1)
- {
- pdo_update('ewei_shop_goods', array('total' => $totalstock), array('uniacid' => $_W['uniacid'], 'id' => $g['goodsid']));
- }
- }
- }
- if (1 <= $order['status'])
- {
- if ($g['totalcnf'] != 1)
- {
- pdo_update('ewei_shop_goods', array('sales' => $g['sales'] - $g['total']), array('uniacid' => $_W['uniacid'], 'id' => $g['goodsid']));
- }
- $salesreal = pdo_fetchcolumn('select ifnull(sum(total),0) from ' . tablename('ewei_shop_order_goods') . ' og ' . ' left join ' . tablename('ewei_shop_order') . ' o on o.id = og.orderid ' . ' where og.goodsid=:goodsid and o.status>=1 and o.uniacid=:uniacid limit 1', array(':goodsid' => $g['goodsid'], ':uniacid' => $_W['uniacid']));
- pdo_update('ewei_shop_goods', array('salesreal' => $salesreal), array('id' => $g['goodsid']));
- }
- }
- }
- public function setSelfGoodsStocks($logid = 0)
- {
- global $_W;
- if ($logid == 0)
- {
- return false;
- }
- $goods = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_cashier_pay_log_goods') . ' WHERE cashierid=:cashierid AND logid=:logid', array(':cashierid' => $_W['cashierid'], ':logid' => $logid));
- foreach ($goods as $g )
- {
- pdo_query('UPDATE ' . tablename('ewei_shop_cashier_goods') . ' SET total=total-' . $g['total'] . ' WHERE cashierid=:cashierid AND id=:id AND total<>-1', array(':cashierid' => $_W['cashierid'], ':id' => $g['goodsid']));
- }
- }
- public function querycoupons($kwd = '')
- {
- global $_W;
- global $_GPC;
- $params = array();
- $params[':uniacid'] = $_W['uniacid'];
- $params[':merchid'] = intval($_W['cashieruser']['merchid']);
- $condition = ' and uniacid=:uniacid and merchid=:merchid and (total=-1 OR total>0)';
- if (empty($_W['cashieruser']['couponid']))
- {
- return array();
- }
- $condition .= 'AND id IN (' . $_W['cashieruser']['couponid'] . ')';
- if (!(empty($kwd)))
- {
- $condition .= ' AND `couponname` LIKE :keyword';
- $params[':keyword'] = '%' . $kwd . '%';
- }
- $ds = pdo_fetchall('SELECT id,couponname as title , thumb FROM ' . tablename('ewei_shop_coupon') . ' WHERE 1 ' . $condition . ' order by createtime desc', $params);
- $ds = set_medias($ds, 'thumb');
- return $ds;
- }
- public function seedCoupon($price = 0, $member)
- {
- global $_W;
- if (empty($member))
- {
- return false;
- }
- if (is_string($member))
- {
- $member = m('member')->getMember($member);
- }
- $set = $this->getUserSet('', $_W['cashierid']);
- $price = floatval($price);
- if (empty($price) || empty($set['coupon']) || (isset($set['coupontime']) && !($this->sale_time($set['coupontime']))))
- {
- return false;
- }
- if (empty($set['coupon']['couponid']))
- {
- return false;
- }
- if (!(empty($set['coupon']['minmoney'])) && ($price < floatval($set['coupon']['minmoney'])))
- {
- return false;
- }
- $coupon = com('coupon');
- if ($coupon)
- {
- $data = $coupon->getCoupon($set['coupon']['couponid']);
- if (($data['total'] == '-1') || (0 < $data['total']))
- {
- $coupon->poster($member, $set['coupon']['couponid'], 1);
- return $set['coupon']['couponid'];
- }
- }
- return false;
- }
- public function caculatecoupon($couponid, $totalprice, $openid = NULL)
- {
- global $_W;
- $openid = ((is_null($openid) ? $_W['openid'] : $openid));
- $uniacid = $_W['uniacid'];
- $sql = 'SELECT d.id,d.couponid,c.enough,c.backtype,c.deduct,c.discount,c.backmoney,c.backcredit,c.backredpack,c.merchid,c.limitgoodtype,c.limitgoodcatetype,c.limitgoodids,c.limitgoodcateids,c.limitdiscounttype FROM ' . tablename('ewei_shop_coupon_data') . ' d';
- $sql .= ' left join ' . tablename('ewei_shop_coupon') . ' c on d.couponid = c.id';
- $sql .= ' where d.id=:id and d.uniacid=:uniacid and d.openid=:openid and d.used=0 limit 1';
- $data = pdo_fetch($sql, array(':uniacid' => $uniacid, ':id' => $couponid, ':openid' => $openid));
- if (empty($data))
- {
- return false;
- }
- $data['enough'] = floatval($data['enough']);
- if (!(empty($data['enough'])) && ($totalprice < $data['enough']))
- {
- return false;
- }
- $deduct = (double) $data['deduct'];
- $discount = (double) $data['discount'];
- $backtype = (double) $data['backtype'];
- $deductprice = 0;
- if ((0 < $deduct) && ($backtype == 0) && (0 < $totalprice))
- {
- if ($totalprice < $deduct)
- {
- $deduct = $totalprice;
- }
- if ($deduct <= 0)
- {
- $deduct = 0;
- }
- $deductprice = $deduct;
- }
- else if ((0 < $discount) && ($backtype == 1))
- {
- $deductprice = round($totalprice * (1 - ($discount / 10)), 2);
- if ($totalprice < $deductprice)
- {
- $deductprice = $totalprice;
- }
- if ($deductprice <= 0)
- {
- $deductprice = 0;
- }
- }
- $new_price = $totalprice - $deductprice;
- return array('old_price' => $totalprice, 'new_price' => round($new_price, 2), 'discount' => $discount, 'money' => $deductprice);
- }
- public function sendCredit1($log, $userset = NULL)
- {
- $credit1_double = 1;
- $price = $log['money'] + $log['deduction'];
- if (empty($userset['credit1']) || empty($price))
- {
- return 0;
- }
- if (!(empty($userset['credit1_double'])))
- {
- $credit1_double = $userset['credit1_double'];
- }
- $price = $price * $credit1_double;
- $credit1 = com_run('sale::getCredit1', $log['openid'], $price, 37, 1, 0, $log['title'] . '收银订单号 : ' . $log['logno'] . ' 收银台消费送积分');
- return $credit1;
- }
- }
- function sort_cashier($a, $b)
- {
- $enough1 = floatval($a['enough']);
- $enough2 = floatval($b['enough']);
- if ($enough1 == $enough2)
- {
- return 0;
- }
- return ($enough1 < $enough2 ? 1 : -1);
- }
- ?>
|