人人商城

model.php 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. <?php
  2. if (!(defined('IN_IA')))
  3. {
  4. exit('Access Denied');
  5. }
  6. class CashierModel extends PluginModel
  7. {
  8. const APPLY = 'apply';
  9. const CHECKED = 'checked';
  10. const APPLY_CLEARING = 'apply_clearing';
  11. const PAY = 'pay';
  12. const PAY_CASHIER = 'pay_cashier';
  13. const PAY_CASHIER_USER = 'pay_cashier_user';
  14. static public $paytype = array(0 => '微信', 1 => '支付宝', 2 => '商城余额', 3 => '现金收款', 101 => '系统微信');
  15. public $setmeal = array('标准套餐', '豪华套餐');
  16. static public $UserSet = array();
  17. static public function perm()
  18. {
  19. $perm = array('index' => '我要收款', 'goods' => '商品收款', 'order' => '收款订单', 'statistics' => '收银统计', 'sysset' => '设置', 'sale' => '营销', 'clearing' => '提现', 'goodsmanage' => '商品管理');
  20. if (empty($_W['cashieruser']['can_withdraw']))
  21. {
  22. unset($perm['clearing']);
  23. }
  24. return $perm;
  25. }
  26. public function getUserSet($name = '', $cashierid)
  27. {
  28. global $_W;
  29. if (!(isset(static::$UserSet[$cashierid])))
  30. {
  31. $user = $this->userInfo($cashierid);
  32. $set = ((empty($user['set']) ? array() : json_decode($user['set'], true)));
  33. static::$UserSet[$cashierid] = $set;
  34. }
  35. if (empty($name))
  36. {
  37. return static::$UserSet[$cashierid];
  38. }
  39. return (isset(static::$UserSet[$cashierid][$name]) ? static::$UserSet[$cashierid][$name] : '');
  40. }
  41. public function updateUserSet($data = array(), $cashierid)
  42. {
  43. global $_W;
  44. $user = $this->userInfo($cashierid);
  45. $set = ((empty($user['set']) ? array() : json_decode($user['set'], true)));
  46. $set = json_encode(array_merge($set, $data));
  47. 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));
  48. }
  49. public function payResult($logid, $return_log = false)
  50. {
  51. global $_W;
  52. $id = intval($logid);
  53. if ($id != 0)
  54. {
  55. $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));
  56. }
  57. else
  58. {
  59. $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));
  60. }
  61. if (!(empty($log)))
  62. {
  63. $_W['cashierid'] = $log['cashierid'];
  64. $res = $this->updateOrder($log);
  65. if ($res && ($log['status'] != 1))
  66. {
  67. $log['status'] = 1;
  68. $log['paytime'] = time();
  69. }
  70. return ($return_log ? $log : $res);
  71. }
  72. return false;
  73. }
  74. public function categoryAll($status = 1)
  75. {
  76. global $_W;
  77. $status = intval($status);
  78. $condition = ' and uniacid=:uniacid and status=' . intval($status);
  79. $params = array(':uniacid' => $_W['uniacid']);
  80. $item = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_cashier_category') . ' WHERE 1 ' . $condition . ' ORDER BY displayorder desc, id DESC', $params);
  81. return $item;
  82. }
  83. public function categoryOne($id)
  84. {
  85. global $_W;
  86. $item = pdo_fetch('select * from ' . tablename('ewei_shop_cashier_category') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $id, ':uniacid' => $_W['uniacid']));
  87. return $item;
  88. }
  89. public function savaUser(array $params, $diyform = array())
  90. {
  91. global $_W;
  92. $diyform_flag = 0;
  93. $diyform_plugin = p('diyform');
  94. $f_data = array();
  95. if ($diyform_plugin && !(empty($_W['shopset']['cashier']['apply_diyform'])))
  96. {
  97. if (!(empty($item['diyformdata'])))
  98. {
  99. $diyform_flag = 1;
  100. $fields = iunserializer($item['diyformfields']);
  101. $f_data = iunserializer($item['diyformdata']);
  102. }
  103. else
  104. {
  105. $diyform_id = $_W['shopset']['cashier']['apply_diyformid'];
  106. if (!(empty($diyform_id)))
  107. {
  108. $formInfo = $diyform_plugin->getDiyformInfo($diyform_id);
  109. if (!(empty($formInfo)))
  110. {
  111. $diyform_flag = 1;
  112. $fields = $formInfo['fields'];
  113. }
  114. }
  115. }
  116. }
  117. $fdata = array();
  118. if ($diyform_flag)
  119. {
  120. $fdata = p('diyform')->getPostDatas($fields);
  121. if (is_error($fdata))
  122. {
  123. show_json(0, $fdata['message']);
  124. }
  125. if ($diyform_flag)
  126. {
  127. $params['diyformdata'] = iserializer($fdata);
  128. if (!(empty($diyform)))
  129. {
  130. $insert_data = $diyform_plugin->getInsertData($fields, $diyform);
  131. $params['diyformdata'] = $insert_data['data'];
  132. }
  133. $params['diyformfields'] = iserializer($fields);
  134. }
  135. }
  136. if (empty($params['title']))
  137. {
  138. show_json(0, '请填写收银台名称!');
  139. }
  140. if (empty($params['manageopenid']))
  141. {
  142. show_json(0, '请填写管理微信号!');
  143. }
  144. if (empty($params['name']))
  145. {
  146. show_json(0, '请填写联系人!');
  147. }
  148. if (empty($params['mobile']))
  149. {
  150. show_json(0, '请填写联系电话!');
  151. }
  152. if (empty($params['username']))
  153. {
  154. show_json(0, '请填写后台登录用户名!');
  155. }
  156. if (!(empty($params['password'])))
  157. {
  158. $params['salt'] = random(8);
  159. $params['password'] = md5(trim($params['password']) . $params['salt']);
  160. }
  161. else
  162. {
  163. unset($params['password']);
  164. }
  165. $params['storeid'] = intval($params['storeid']);
  166. $params['merchid'] = intval($params['merchid']);
  167. $params['isopen_commission'] = intval($params['isopen_commission']);
  168. $params['title'] = trim($params['title']);
  169. $params['logo'] = trim($params['logo']);
  170. $params['openid'] = trim($params['openid']);
  171. $params['manageopenid'] = trim($params['manageopenid']);
  172. $params['name'] = trim($params['name']);
  173. $params['mobile'] = trim($params['mobile']);
  174. $params['username'] = trim($params['username']);
  175. $params['withdraw'] = floatval($params['withdraw']);
  176. $params['wechat_status'] = intval($params['wechat_status']);
  177. $params['alipay_status'] = intval($params['alipay_status']);
  178. if (isset($params['deleted']))
  179. {
  180. $params['deleted'] = intval($params['deleted']);
  181. }
  182. if (!(isset($params['id'])))
  183. {
  184. $params['createtime'] = TIMESTAMP;
  185. $params['deleted'] = 0;
  186. pdo_insert('ewei_shop_cashier_user', $params);
  187. $params['id'] = pdo_insertid();
  188. }
  189. else
  190. {
  191. pdo_update('ewei_shop_cashier_user', $params, array('id' => $params['id'], 'uniacid' => $params['uniacid']));
  192. }
  193. return $params;
  194. }
  195. public function userInfo($openid)
  196. {
  197. global $_W;
  198. $id = intval($openid);
  199. $sql = 'SELECT * FROM ' . tablename('ewei_shop_cashier_user') . ' WHERE uniacid=:uniacid AND deleted=0';
  200. $params = array(':uniacid' => $_W['uniacid']);
  201. if ($id == 0)
  202. {
  203. $sql .= ' AND openid=:openid';
  204. $params[':openid'] = $openid;
  205. }
  206. else
  207. {
  208. $sql .= ' AND id=:id';
  209. $params[':id'] = $id;
  210. }
  211. $res = pdo_fetch($sql . ' LIMIT 1', $params);
  212. return $res;
  213. }
  214. public function sendMessage($params, $type, $openid = NULL)
  215. {
  216. global $_W;
  217. if (isset($params['createtime']))
  218. {
  219. $params['createtime'] = date('Y-m-d H:i:s', $params['createtime']);
  220. }
  221. if (isset($params['paytime']))
  222. {
  223. $params['paytime'] = date('Y-m-d H:i:s', $params['paytime']);
  224. }
  225. $data = m('common')->getPluginset('cashier');
  226. $notice = $data['notice'];
  227. if (empty($notice[$type]))
  228. {
  229. return false;
  230. }
  231. switch ($type)
  232. {
  233. case self: $datas = array('[联系人]' => $params['name'], '[联系电话]' => $params['mobile'], '[申请时间]' => date('Y-m-d H:i:s', $params['createtime']));
  234. break;
  235. switch ($type)
  236. {
  237. case self: $datas = array('[联系人]' => $params['name'], '[联系电话]' => $params['mobile'], '[审核状态]' => $params['status'], '[审核时间]' => $params['createtime'], '[驳回原因]' => $params['reason']);
  238. break;
  239. switch ($type)
  240. {
  241. case self: $datas = array('[联系人]' => $params['name'], '[联系电话]' => $params['mobile'], '[申请时间]' => $params['createtime'], '[申请金额]' => $params['money']);
  242. break;
  243. switch ($type)
  244. {
  245. case self: $datas = array('[联系人]' => $params['name'], '[联系电话]' => $params['mobile'], '[申请时间]' => $params['createtime'], '[打款时间]' => $params['paytime'], '[申请金额]' => $params['money'], '[打款金额]' => $params['realmoney']);
  246. break;
  247. switch ($type)
  248. {
  249. case self: $datas = array('[订单编号]' => $params['logno'], '[付款金额]' => $params['money'], '[余额抵扣]' => $params['deduction'], '[付款时间]' => $params['paytime'], '[收银台名称]' => $params['cashier_title']);
  250. break;
  251. switch ($type)
  252. {
  253. case self: $datas = array('[订单编号]' => $params['logno'], '[付款金额]' => $params['money'], '[余额抵扣]' => $params['deduction'], '[付款时间]' => $params['paytime'], '[收银台名称]' => $params['cashier_title']);
  254. break;
  255. break;
  256. $datas = ((isset($datas) ? $datas : array()));
  257. }
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. protected function sendNotice($notice, $tag, $datas)
  265. {
  266. global $_W;
  267. if (!(empty($notice[$tag])))
  268. {
  269. $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']));
  270. if (!(empty($advanced_template)))
  271. {
  272. $url = ((!(empty($advanced_template['url'])) ? $this->replaceArray($datas, $advanced_template['url']) : ''));
  273. $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']) );
  274. $data = iunserializer($advanced_template['data']);
  275. foreach ($data as $d )
  276. {
  277. $advanced_message[$d['keywords']] = array('value' => $this->replaceArray($datas, $d['value']), 'color' => $d['color']);
  278. }
  279. if (!(empty($notice['openid'])))
  280. {
  281. $notice['openid'] = ((is_array($notice['openid']) ? $notice['openid'] : explode(',', $notice['openid'])));
  282. foreach ($notice['openid'] as $openid )
  283. {
  284. if (!(empty($notice[$tag])) && !(empty($advanced_template['template_id'])))
  285. {
  286. m('message')->sendTplNotice($openid, $advanced_template['template_id'], $advanced_message, $url);
  287. }
  288. else
  289. {
  290. m('message')->sendCustomNotice($openid, $advanced_message, $url);
  291. }
  292. }
  293. }
  294. }
  295. }
  296. return false;
  297. }
  298. protected function replaceArray(array $array, $message)
  299. {
  300. foreach ($array as $key => $value )
  301. {
  302. $message = str_replace($key, $value, $message);
  303. }
  304. return $message;
  305. }
  306. public function wechayPayInfo($user)
  307. {
  308. $wechatpay = json_decode($user['wechatpay'], true);
  309. if (empty($wechatpay['appid']) || empty($wechatpay['mch_id']))
  310. {
  311. $wechat = array('appid' => $wechatpay['sub_appid'], 'mch_id' => $wechatpay['sub_mch_id'], 'apikey' => $wechatpay['apikey']);
  312. }
  313. else
  314. {
  315. $wechat = $wechatpay;
  316. }
  317. return $wechat;
  318. }
  319. public function wechatpay($params)
  320. {
  321. global $_W;
  322. $wechat = $this->wechayPayInfo($_W['cashieruser']);
  323. return m('common')->wechat_micropay_build($params, $wechat, 13);
  324. }
  325. public function wechatpay_101($params)
  326. {
  327. global $_W;
  328. $set = m('common')->getSysset(array('shop', 'pay'));
  329. if (isset($set['pay']) && ($set['pay']['weixin'] == 1))
  330. {
  331. load()->model('payment');
  332. $setting = uni_setting($_W['uniacid'], array('payment'));
  333. $account = pdo_get('account_wechats', array('uniacid' => $_W['uniacid']), array('key', 'secret'));
  334. if (is_array($setting['payment']))
  335. {
  336. $options = $setting['payment']['wechat'];
  337. $options['appid'] = $account['key'];
  338. $options['secret'] = $account['secret'];
  339. $wechat = array('appid' => $options['appid'], 'mch_id' => $options['mchid'], 'apikey' => $options['signkey']);
  340. }
  341. }
  342. else if (isset($set['pay']) && ($set['pay']['weixin_sub'] == 1))
  343. {
  344. $sec = m('common')->getSec();
  345. $sec = iunserializer($sec['sec']);
  346. $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']);
  347. }
  348. if (empty($wechat))
  349. {
  350. $sec = m('common')->getSec();
  351. $sec = iunserializer($sec['sec']);
  352. if (isset($set['pay']) && ($set['pay']['weixin_jie'] == 1))
  353. {
  354. $wechat = array('appid' => $sec['appid'], 'mch_id' => $sec['mchid'], 'apikey' => $sec['apikey']);
  355. }
  356. else if (isset($set['pay']) && ($set['pay']['weixin_jie_sub'] == 1))
  357. {
  358. $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']);
  359. }
  360. }
  361. return (isset($wechat) ? m('common')->wechat_micropay_build($params, $wechat, 13) : error(-1, '$wechat' . '参数错误'));
  362. }
  363. public function alipay($params)
  364. {
  365. global $_W;
  366. return m('common')->AliPayBarcode($params, json_decode($_W['cashieruser']['alipay'], true));
  367. }
  368. public function createOrder(array $array, $return = NULL, $can_sale = true)
  369. {
  370. global $_W;
  371. if (empty($array))
  372. {
  373. return 0;
  374. }
  375. $array['operatorid'] = ((isset($array['operatorid']) ? $array['operatorid'] : 0));
  376. $array['deduction'] = ((isset($array['deduction']) ? $array['deduction'] : 0));
  377. $realmoney = $array['money'];
  378. $sale = array();
  379. if ((0 < $realmoney) && $can_sale)
  380. {
  381. if (!(empty($array['usecoupon'])))
  382. {
  383. $usecoupon = $this->caculatecoupon($array['usecoupon'], $realmoney);
  384. $realmoney = $usecoupon['new_price'];
  385. }
  386. $sale = $this->saleCalculate($realmoney);
  387. $realmoney = $sale['money'];
  388. }
  389. if ($realmoney < $array['deduction'])
  390. {
  391. $array['deduction'] = $realmoney;
  392. }
  393. $realmoney = $realmoney - $array['deduction'];
  394. $title = $_W['cashieruser']['title'] . '消费';
  395. if (!(empty($array['title'])))
  396. {
  397. $title = $array['title'];
  398. }
  399. $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);
  400. $res = pdo_insert('ewei_shop_cashier_pay_log', $data);
  401. if (!($res))
  402. {
  403. return error(-2, '数据插入异常,请重试!');
  404. }
  405. $data['id'] = pdo_insertid();
  406. if (!(empty($usecoupon)))
  407. {
  408. pdo_update('ewei_shop_coupon_data', array('used' => 1, 'usetime' => $data['createtime'], 'ordersn' => $data['logno']), array('id' => $array['usecoupon']));
  409. }
  410. if ($return !== NULL)
  411. {
  412. return $data;
  413. }
  414. return $this->pay($data, (isset($array['auth_code']) ? $array['auth_code'] : NULL));
  415. }
  416. public function pay($data, $auth_code = NULL)
  417. {
  418. global $_W;
  419. if (($data['money'] <= 0) || ($data['paytype'] == 3))
  420. {
  421. $data['status'] = 1;
  422. $data['paytype'] = (($data['paytype'] == 3 ? $data['paytype'] : 2));
  423. $data['deduction'] -= $data['randommoney'];
  424. $data['paytime'] = time();
  425. pdo_update('ewei_shop_cashier_pay_log', $data, array('id' => $data['id']));
  426. m('member')->setCredit($data['openid'], 'credit2', -$data['deduction'], array(0, '收银台 ' . $_W['cashieruser']['title'], '收款'));
  427. $user = $this->userInfo($data['cashierid']);
  428. $this->paySuccess($data, $user);
  429. return array('res' => true, 'id' => $data['id']);
  430. }
  431. $params = array('title' => $data['title'], 'tid' => $data['logno'], 'fee' => $data['money']);
  432. $params['out_trade_no'] = $params['tid'];
  433. $params['total_amount'] = $params['fee'];
  434. $params['subject'] = $params['title'];
  435. $params['body'] = $_W['uniacid'] . ':' . 2;
  436. if ($auth_code !== NULL)
  437. {
  438. $params['auth_code'] = $auth_code;
  439. }
  440. if ($data['paytype'] == 0)
  441. {
  442. $res = $this->wechatpay($params);
  443. }
  444. else if ($data['paytype'] == 1)
  445. {
  446. $res = $this->alipay($params);
  447. }
  448. else
  449. {
  450. $res = $this->wechatpay_101($params);
  451. }
  452. return array('res' => $res, 'id' => $data['id']);
  453. }
  454. public function orderQuery($pay_log)
  455. {
  456. $array = array();
  457. if (is_array2($pay_log))
  458. {
  459. foreach ($pay_log as $value )
  460. {
  461. if ($value['status'] == '0')
  462. {
  463. $res = $this->updateOrder($value);
  464. if ($res)
  465. {
  466. $array[] = $res;
  467. }
  468. }
  469. }
  470. }
  471. else if (is_array($pay_log))
  472. {
  473. $res = $this->updateOrder($pay_log);
  474. if ($res)
  475. {
  476. $array[] = $res;
  477. }
  478. }
  479. else
  480. {
  481. $res = $this->payResult($pay_log);
  482. if ($res)
  483. {
  484. $array[] = $res;
  485. }
  486. }
  487. return $array;
  488. }
  489. public function updateOrder($log)
  490. {
  491. global $_W;
  492. if (!(empty($log['status'])))
  493. {
  494. return (int) $log['id'];
  495. }
  496. $realmoney = floatval($log['money']);
  497. $user = $this->userInfo($log['cashierid']);
  498. if ($log['paytype'] != '101')
  499. {
  500. if (empty($log['paytype']))
  501. {
  502. $wechat = $this->wechayPayInfo($user);
  503. $res = m('common')->wechat_order_query($log['logno'], 0, $wechat);
  504. }
  505. else if ($log['paytype'] == '1')
  506. {
  507. $alipay = json_decode($user['alipay'], true);
  508. $res = m('common')->AliPayQuery($log['logno'], $alipay);
  509. }
  510. }
  511. else
  512. {
  513. $set = m('common')->getSysset(array('shop', 'pay'));
  514. if (isset($set['pay']) && ($set['pay']['weixin'] == 1))
  515. {
  516. load()->model('payment');
  517. $setting = uni_setting($_W['uniacid'], array('payment'));
  518. $account = pdo_get('account_wechats', array('uniacid' => $_W['uniacid']), array('key', 'secret'));
  519. if (is_array($setting['payment']))
  520. {
  521. $options = $setting['payment']['wechat'];
  522. $options['appid'] = $account['key'];
  523. $options['secret'] = $account['secret'];
  524. if (IMS_VERSION <= 0.80000000000000004)
  525. {
  526. $options['apikey'] = $options['signkey'];
  527. }
  528. $wechat = array('appid' => $options['appid'], 'mch_id' => $options['mchid'], 'apikey' => $options['apikey']);
  529. $res = m('common')->wechat_order_query($log['logno'], 0, $wechat);
  530. }
  531. }
  532. else if (isset($set['pay']) && ($set['pay']['weixin_sub'] == 1))
  533. {
  534. $sec = m('common')->getSec();
  535. $sec = iunserializer($sec['sec']);
  536. $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']);
  537. $res = m('common')->wechat_order_query($log['logno'], 0, $wechat);
  538. }
  539. if (empty($res))
  540. {
  541. $sec = m('common')->getSec();
  542. $sec = iunserializer($sec['sec']);
  543. if (isset($set['pay']) && ($set['pay']['weixin_jie'] == 1))
  544. {
  545. $wechat = array('appid' => $sec['appid'], 'mch_id' => $sec['mchid'], 'apikey' => $sec['apikey']);
  546. $res = m('common')->wechat_order_query($log['logno'], 0, $wechat);
  547. }
  548. else if (isset($set['pay']) && ($set['pay']['weixin_jie_sub'] == 1))
  549. {
  550. $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']);
  551. $res = m('common')->wechat_order_query($log['logno'], 0, $wechat);
  552. }
  553. }
  554. }
  555. if (empty($res))
  556. {
  557. return false;
  558. }
  559. if (($res['total_fee'] == round($realmoney * 100, 2)) || ($res['total_amount'] == round($realmoney, 2)))
  560. {
  561. $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']));
  562. if (empty($log['status']))
  563. {
  564. $res['openid'] = ((isset($res['openid']) ? $res['openid'] : ''));
  565. $log['openid'] = ((empty($log['openid']) ? $res['openid'] : $log['openid']));
  566. $this->paySuccess($log, $user, $res);
  567. }
  568. return (int) $log['id'];
  569. }
  570. }
  571. public function paySuccess($log, $user)
  572. {
  573. global $_W;
  574. $coupon = 0;
  575. if (!(empty($log['openid'])))
  576. {
  577. if ($user['isopen_commission'])
  578. {
  579. $this->becomeDown($log['openid'], $user['manageopenid']);
  580. }
  581. $this->sendMessage(array('logno' => $log['logno'], 'money' => $log['money'], 'deduction' => $log['deduction'], 'paytime' => time(), 'cashier_title' => $user['title']), self::PAY_CASHIER_USER, $log['openid']);
  582. $coupon = $this->seedCoupon($log['money'] + $log['deduction'], $log['openid']);
  583. }
  584. 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']));
  585. $log['deduction'] = (double) $log['deduction'];
  586. if (!(empty($log['deduction'])))
  587. {
  588. $userinfo = m('member')->getMobileMember($log['mobile']);
  589. m('member')->setCredit($userinfo['openid'], 'credit2', -$log['deduction'], array(0, '收银台 ' . $_W['cashieruser']['title'], '收款'));
  590. }
  591. if (!(empty($log['orderid'])))
  592. {
  593. $paytype = 1;
  594. if (empty($log['paytype']) || ($log['paytype'] == '101'))
  595. {
  596. $paytype = 21;
  597. }
  598. if ($log['paytype'] == '1')
  599. {
  600. $paytype = 22;
  601. }
  602. pdo_update('ewei_shop_order', array('status' => 3, 'paytype' => $paytype, 'paytime' => time(), 'sendtime' => time(), 'finishtime' => time()), array('id' => $log['orderid'], 'uniacid' => $_W['uniacid']));
  603. $this->setStocks($log['orderid']);
  604. if (p('commission'))
  605. {
  606. p('commission')->checkOrderPay($log['orderid']);
  607. p('commission')->checkOrderFinish($log['orderid']);
  608. }
  609. }
  610. if (!(empty($log['isgoods'])))
  611. {
  612. $this->setSelfGoodsStocks($log['id']);
  613. }
  614. $operator = false;
  615. if (!(empty($log['operatorid'])))
  616. {
  617. $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']));
  618. }
  619. if ($operator)
  620. {
  621. $user['manageopenid'] = $operator['manageopenid'];
  622. }
  623. $this->sendMessage(array('logno' => $log['logno'], 'money' => $log['money'], 'deduction' => $log['deduction'], 'paytime' => time(), 'cashier_title' => $user['title']), self::PAY_CASHIER, $user['manageopenid']);
  624. $userset = json_decode($user['set'], true);
  625. if (!(empty($log['openid'])))
  626. {
  627. $present_credit1 = $this->sendCredit1($log, $userset);
  628. if (0 < $present_credit1)
  629. {
  630. pdo_update('ewei_shop_cashier_pay_log', array('present_credit1' => $present_credit1), array('id' => $log['id']));
  631. }
  632. }
  633. if ((!(empty($log['isgoods'])) || !(empty($log['orderid']))) && !(empty($userset['printer_status'])) && !(empty($userset['printer'])) && !(empty($userset['printer_template'])))
  634. {
  635. com_run('printer::sendCashierMessage', $log, $userset['printer_template'], $userset['printer'], $operator);
  636. }
  637. else if (!(empty($userset['printer_status'])) && !(empty($userset['printer'])) && !(empty($userset['printer_template_default'])))
  638. {
  639. com_run('printer::sendCashier', $log, $userset['printer_template_default'], $userset['printer'], $operator);
  640. }
  641. }
  642. public function paytype($paytype = 0, $auto_code = NULL)
  643. {
  644. global $_W;
  645. if (is_null($paytype) && ($auto_code !== NULL) && (substr($auto_code, 0, 2) == '28'))
  646. {
  647. return error(-101, '暂时不支持支付宝支付!');
  648. }
  649. if (($paytype == -1) && ($auto_code !== NULL))
  650. {
  651. $type = substr($auto_code, 0, 2);
  652. if ($type == '13')
  653. {
  654. $paytype = 0;
  655. }
  656. else if ($type == '28')
  657. {
  658. if (empty($_W['cashieruser']['alipay_status']))
  659. {
  660. return error(-101, '暂时不支持支付宝支付!');
  661. }
  662. $paytype = 1;
  663. }
  664. }
  665. if (empty($paytype) && !(empty($_W['cashieruser']['wechat_status'])))
  666. {
  667. $paytype = 0;
  668. }
  669. else if ($paytype == '1')
  670. {
  671. $paytype = 1;
  672. }
  673. else if ($paytype == '2')
  674. {
  675. $paytype = 2;
  676. }
  677. else if ($paytype == '3')
  678. {
  679. $paytype = 3;
  680. }
  681. else
  682. {
  683. $paytype = 101;
  684. }
  685. return $paytype;
  686. }
  687. public function getrandommoney($money = 0)
  688. {
  689. global $_W;
  690. $userset = $this->getUserSet('', $_W['cashierid']);
  691. if (isset($userset['randtime']) && !($this->sale_time($userset['randtime'])))
  692. {
  693. return 0;
  694. }
  695. $probability = $userset['rand'];
  696. if (empty($probability))
  697. {
  698. return 0;
  699. }
  700. if (!(empty($probability['minmoney'])) && ($money < floatval($probability['minmoney'])))
  701. {
  702. return 0;
  703. }
  704. $sum = 0;
  705. $i = 0;
  706. while ($i < count($probability['rand']))
  707. {
  708. $sum += $probability['rand'][$i];
  709. $rand_num = rand(1, 100);
  710. if ($rand_num <= $sum)
  711. {
  712. $loop = $i;
  713. break;
  714. }
  715. ++$i;
  716. }
  717. $min = (double) $probability['rand_left'][$loop] * 100;
  718. $max = (double) $probability['rand_right'][$loop] * 100;
  719. return rand($min, $max) / 100;
  720. }
  721. public function becomeDown($openid, $manageopenid)
  722. {
  723. $member = m('member')->getMember($openid);
  724. if ($member)
  725. {
  726. if (empty($member['isagent']) && empty($member['agentid']))
  727. {
  728. $store_member = m('member')->getMember($manageopenid);
  729. pdo_query('UPDATE ' . tablename('ewei_shop_member') . ' SET agentid=' . $store_member['id'] . ' WHERE `id` = :id', array(':id' => $member['id']));
  730. }
  731. }
  732. }
  733. public function getTodayOrder()
  734. {
  735. $starttime = strtotime(date('Y-m-d'));
  736. $endtime = time();
  737. return $this->getOrderMoney($starttime, $endtime);
  738. }
  739. public function getWeekOrder()
  740. {
  741. $starttime = strtotime(date('Y-m-d')) - ((date('w') - 1) * 3600 * 24);
  742. $endtime = time();
  743. return $this->getOrderMoney($starttime, $endtime);
  744. }
  745. public function getMonthOrder()
  746. {
  747. $starttime = strtotime(date('Y-m') . '-1');
  748. $endtime = time();
  749. return $this->getOrderMoney($starttime, $endtime);
  750. }
  751. public function getOrderMoney($starttime = 0, $endtime = 0)
  752. {
  753. global $_W;
  754. if (($starttime == 0) && ($endtime == 0))
  755. {
  756. $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']));
  757. }
  758. else
  759. {
  760. $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));
  761. }
  762. return $money;
  763. }
  764. public function getEnoughs($price = 0)
  765. {
  766. global $_W;
  767. $set = $this->getUserSet('', $_W['cashierid']);
  768. if (isset($set['enoughtime']) && !($this->sale_time($set['enoughtime'])))
  769. {
  770. return array('old_price' => $price, 'new_price' => $price, 'enough' => 0, 'money' => 0);
  771. }
  772. $allenoughs = array();
  773. $enoughs = $set['enoughs'];
  774. if ((0 < floatval($set['enoughmoney'])) && (0 < floatval($set['enoughdeduct'])))
  775. {
  776. $allenoughs[] = array('enough' => floatval($set['enoughmoney']), 'money' => floatval($set['enoughdeduct']));
  777. }
  778. if (is_array($enoughs))
  779. {
  780. foreach ($enoughs as $e )
  781. {
  782. if ((0 < floatval($e['enough'])) && (0 < floatval($e['give'])))
  783. {
  784. $allenoughs[] = array('enough' => floatval($e['enough']), 'money' => floatval($e['give']));
  785. }
  786. }
  787. }
  788. usort($allenoughs, 'sort_cashier');
  789. $new_price = $price;
  790. $enough = 0;
  791. $money = 0;
  792. foreach ($allenoughs as $key => $value )
  793. {
  794. if (($value['enough'] <= $price) && (0 < $value))
  795. {
  796. $new_price = $price - $value['money'];
  797. $enough = $value['enough'];
  798. $money = $value['money'];
  799. break;
  800. }
  801. }
  802. return array('old_price' => $price, 'new_price' => round($new_price, 2), 'enough' => $enough, 'money' => $money);
  803. }
  804. public function is_perm($text)
  805. {
  806. global $_W;
  807. if (isset($_W['cashieruser']['operator']))
  808. {
  809. $perm = json_decode($_W['cashieruser']['operator']['perm'], true);
  810. $routes = explode('.', $text);
  811. if (!(in_array($routes[0], $perm)))
  812. {
  813. return false;
  814. }
  815. }
  816. return true;
  817. }
  818. public function getDiscount($price = 0)
  819. {
  820. global $_W;
  821. $set = $this->getUserSet('', $_W['cashierid']);
  822. $set['discount'] = floatval($set['discount']);
  823. $price = floatval($price);
  824. if (empty($price) || empty($set['discount']) || (isset($set['discounttime']) && !($this->sale_time($set['discounttime']))))
  825. {
  826. return array('old_price' => $price, 'new_price' => $price, 'discount' => 0, 'money' => 0);
  827. }
  828. $new_price = 0;
  829. if (!(empty($set['discount'])))
  830. {
  831. $new_price = ($set['discount'] * $price) / 10;
  832. }
  833. return array('old_price' => $price, 'new_price' => round($new_price, 2), 'discount' => $set['discount'], 'money' => round($price - $new_price, 2));
  834. }
  835. public function sale_time($sale_time)
  836. {
  837. if (!(empty($sale_time)) && ($sale_time['start'] <= time()) && (time() <= $sale_time['end']))
  838. {
  839. if (empty($sale_time['start1']))
  840. {
  841. return true;
  842. }
  843. $hour = idate('H');
  844. $minute = idate('i');
  845. $return = false;
  846. foreach ($sale_time['start1'] as $key => $value )
  847. {
  848. if (($sale_time['start1'][$key] == $hour) && ($sale_time['start2'][$key] <= $minute))
  849. {
  850. if ((($sale_time['end1'][$key] == $hour) && ($minute <= $sale_time['end2'][$key])) || ($hour < $sale_time['end1'][$key]))
  851. {
  852. $return = true;
  853. }
  854. }
  855. if ($sale_time['start1'][$key] < $hour)
  856. {
  857. if ((($sale_time['end1'][$key] == $hour) && ($minute <= $sale_time['end2'][$key])) || ($hour < $sale_time['end1'][$key]))
  858. {
  859. $return = true;
  860. }
  861. }
  862. }
  863. return $return;
  864. }
  865. return false;
  866. }
  867. public function saleCalculate($money = 0, $random = true)
  868. {
  869. $randommoney = 0;
  870. if ($random)
  871. {
  872. $randommoney = $this->getrandommoney($money);
  873. }
  874. $realmoney = round($money - $randommoney, 2);
  875. $enoughs = $this->getEnoughs($realmoney);
  876. $discount = $this->getDiscount($enoughs['new_price']);
  877. return array('randommoney' => $randommoney, 'enough' => $enoughs, 'discount' => $discount, 'money' => $discount['new_price']);
  878. }
  879. public function createGoodsOrder($goods, $openid = '')
  880. {
  881. global $_W;
  882. global $_GPC;
  883. $allgoods = array();
  884. $realmoney = 0;
  885. $goodsprice = 0;
  886. foreach ($goods as $g )
  887. {
  888. if (empty($g))
  889. {
  890. continue;
  891. }
  892. $goodsid = intval($g['goodsid']);
  893. $optionid = intval($g['optionid']);
  894. $goodstotal = intval($g['total']);
  895. if ($goodstotal < 1)
  896. {
  897. $goodstotal = 1;
  898. }
  899. if (empty($goodsid))
  900. {
  901. show_json(0, '参数错误,未选择商品!');
  902. }
  903. $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';
  904. $data = pdo_fetch($sql, array(':uniacid' => $_W['uniacid'], ':id' => $goodsid));
  905. if (!(empty($data['hasoption'])))
  906. {
  907. $opdata = m('goods')->getOption($data['goodsid'], $optionid);
  908. if (empty($opdata) || empty($optionid))
  909. {
  910. show_json(0, '商品' . $data['title'] . '的规格不存在,请到购物车删除该商品重新选择规格!');
  911. }
  912. }
  913. $merchid = $data['merchid'];
  914. $merch_array[$merchid]['goods'][] = $data['goodsid'];
  915. $data['stock'] = $data['total'];
  916. $data['total'] = $goodstotal;
  917. if (!(empty($optionid)))
  918. {
  919. $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));
  920. if (!(empty($option)))
  921. {
  922. if ($option['stock'] != -1)
  923. {
  924. if (empty($option['stock']))
  925. {
  926. show_json(-1, $data['title'] . '<br/>' . $option['title'] . ' 库存不足!');
  927. }
  928. }
  929. $data['optionid'] = $optionid;
  930. $data['optiontitle'] = $option['title'];
  931. $data['marketprice'] = $option['marketprice'];
  932. if (!(empty($option['goodssn'])))
  933. {
  934. $data['goodssn'] = $option['goodssn'];
  935. }
  936. if (!(empty($option['productsn'])))
  937. {
  938. $data['productsn'] = $option['productsn'];
  939. }
  940. if (!(empty($option['weight'])))
  941. {
  942. $data['weight'] = $option['weight'];
  943. }
  944. }
  945. }
  946. $data['marketprice'] = ((isset($g['marketprice']) ? floatval($g['marketprice']) : $data['marketprice']));
  947. $gprice = $data['marketprice'] * $goodstotal;
  948. $diyprice = ((isset($g['price']) ? floatval($g['price']) : $data['marketprice']));
  949. $data['ggprice'] = $gprice;
  950. $data['realprice'] = $diyprice * $goodstotal;
  951. $realmoney += $data['realprice'];
  952. $goodsprice += $gprice;
  953. $allgoods[] = $data;
  954. }
  955. $ismerch = 0;
  956. if (0 < $_W['cashieruser']['merchid'])
  957. {
  958. $ismerch = 1;
  959. }
  960. if (0 < $ismerch)
  961. {
  962. $ordersn = m('common')->createNO('order', 'ordersn', 'ME');
  963. }
  964. else
  965. {
  966. $ordersn = m('common')->createNO('order', 'ordersn', 'SH');
  967. }
  968. $order = array();
  969. $order['ismerch'] = $ismerch;
  970. $order['parentid'] = 0;
  971. $order['uniacid'] = $_W['uniacid'];
  972. $order['openid'] = $openid;
  973. $order['ordersn'] = $ordersn;
  974. $order['price'] = $realmoney;
  975. $order['oldprice'] = $goodsprice;
  976. $order['grprice'] = $goodsprice;
  977. $order['cash'] = 0;
  978. $order['status'] = 0;
  979. $order['remark'] = trim($_GPC['remark']);
  980. $order['addressid'] = 0;
  981. $order['goodsprice'] = $goodsprice;
  982. $order['storeid'] = 0;
  983. $order['createtime'] = time();
  984. $order['paytype'] = 0;
  985. $order['merchshow'] = 0;
  986. $order['merchid'] = intval($_W['cashieruser']['merchid']);
  987. $order['isparent'] = 0;
  988. $order['transid'] = '';
  989. pdo_insert('ewei_shop_order', $order);
  990. $orderid = pdo_insertid();
  991. foreach ($allgoods as $goods )
  992. {
  993. $order_goods = array();
  994. $order_goods['merchid'] = $goods['merchid'];
  995. $order_goods['uniacid'] = $_W['uniacid'];
  996. $order_goods['orderid'] = $orderid;
  997. $order_goods['goodsid'] = $goods['goodsid'];
  998. $order_goods['price'] = $goods['marketprice'] * $goods['total'];
  999. $order_goods['total'] = $goods['total'];
  1000. $order_goods['optionid'] = $goods['optionid'];
  1001. $order_goods['createtime'] = time();
  1002. $order_goods['optionname'] = $goods['optiontitle'];
  1003. $order_goods['goodssn'] = $goods['goodssn'];
  1004. $order_goods['productsn'] = $goods['productsn'];
  1005. $order_goods['realprice'] = $goods['realprice'];
  1006. $order_goods['oldprice'] = $goods['ggprice'];
  1007. $order_goods['openid'] = $openid;
  1008. pdo_insert('ewei_shop_order_goods', $order_goods);
  1009. }
  1010. $pluginc = p('commission');
  1011. if ($pluginc)
  1012. {
  1013. $pluginc->checkOrderConfirm($orderid);
  1014. }
  1015. $order['id'] = $orderid;
  1016. return $order;
  1017. }
  1018. public function goodsCalculate($selfgoods = array(), $shopgoods = array(), $params, $return = false)
  1019. {
  1020. global $_W;
  1021. $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);
  1022. if (!(empty($selfgoods)))
  1023. {
  1024. foreach ($selfgoods as $key => $val )
  1025. {
  1026. $order['goodsprice'] += $val['price'] * $val['total'];
  1027. $order['isgoods'] = 1;
  1028. $data = array('cashierid' => $order['cashierid'], 'logid' => $order['id'], 'goodsid' => $val['goodsid'], 'price' => $val['price'] * $val['total'], 'total' => $val['total']);
  1029. $g = pdo_get('ewei_shop_cashier_goods', array('cashierid' => $data['cashierid'], 'id' => $data['goodsid']));
  1030. if (($g['total'] < $data['total']) && ($g['total'] != -1))
  1031. {
  1032. pdo_delete('ewei_shop_cashier_pay_log_goods', array('cashierid' => $data['cashierid'], 'logid' => $data['logid']));
  1033. return array('res' => error('-101', $g['title'] . ' 库存不足, 剩余库存 ' . $g['total']));
  1034. }
  1035. pdo_insert('ewei_shop_cashier_pay_log_goods', $data);
  1036. }
  1037. }
  1038. if (!(empty($shopgoods)))
  1039. {
  1040. $goods = array();
  1041. foreach ($shopgoods as $val )
  1042. {
  1043. $goods[] = array('goodsid' => $val['goodsid'], 'optionid' => $val['optionid'], 'price' => $val['price'], 'total' => $val['total'], 'marketprice' => (isset($val['marketprice']) ? $val['marketprice'] : NULL));
  1044. }
  1045. $goodsOrder = $this->createGoodsOrder($shopgoods, $params['openid']);
  1046. $order['orderid'] = $goodsOrder['id'];
  1047. $order['orderprice'] = $goodsOrder['price'];
  1048. }
  1049. if ($params['money'] == $order['orderprice'] + $order['goodsprice'])
  1050. {
  1051. pdo_update('ewei_shop_cashier_pay_log', $order, array('id' => $order['id']));
  1052. if ($return)
  1053. {
  1054. return $order;
  1055. }
  1056. return $this->pay($order, $params['auth_code']);
  1057. }
  1058. return array('res' => error('-101', '支付金额与商品金额有误差!'));
  1059. }
  1060. public function setStocks($orderid = 0)
  1061. {
  1062. global $_W;
  1063. if ($orderid == 0)
  1064. {
  1065. return false;
  1066. }
  1067. $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));
  1068. $param = array();
  1069. $param[':uniacid'] = $_W['uniacid'];
  1070. if ($order['isparent'] == 1)
  1071. {
  1072. $condition = ' og.parentorderid=:parentorderid';
  1073. $param[':parentorderid'] = $orderid;
  1074. }
  1075. else
  1076. {
  1077. $condition = ' og.orderid=:orderid';
  1078. $param[':orderid'] = $orderid;
  1079. }
  1080. $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);
  1081. foreach ($goods as $g )
  1082. {
  1083. $stocktype = -1;
  1084. if (!(empty($stocktype)))
  1085. {
  1086. if (!(empty($g['optionid'])))
  1087. {
  1088. $option = m('goods')->getOption($g['goodsid'], $g['optionid']);
  1089. if (!(empty($option)) && ($option['stock'] != -1))
  1090. {
  1091. $stock = -1;
  1092. if ($stocktype == 1)
  1093. {
  1094. $stock = $option['stock'] + $g['total'];
  1095. }
  1096. else if ($stocktype == -1)
  1097. {
  1098. $stock = $option['stock'] - $g['total'];
  1099. ($stock <= 0) && ($stock = 0);
  1100. }
  1101. if ($stock != -1)
  1102. {
  1103. pdo_update('ewei_shop_goods_option', array('stock' => $stock), array('uniacid' => $_W['uniacid'], 'goodsid' => $g['goodsid'], 'id' => $g['optionid']));
  1104. }
  1105. }
  1106. }
  1107. if (!(empty($g['goodstotal'])) && ($g['goodstotal'] != -1))
  1108. {
  1109. $totalstock = -1;
  1110. if ($stocktype == 1)
  1111. {
  1112. $totalstock = $g['goodstotal'] + $g['total'];
  1113. }
  1114. else if ($stocktype == -1)
  1115. {
  1116. $totalstock = $g['goodstotal'] - $g['total'];
  1117. ($totalstock <= 0) && ($totalstock = 0);
  1118. }
  1119. if ($totalstock != -1)
  1120. {
  1121. pdo_update('ewei_shop_goods', array('total' => $totalstock), array('uniacid' => $_W['uniacid'], 'id' => $g['goodsid']));
  1122. }
  1123. }
  1124. }
  1125. if (1 <= $order['status'])
  1126. {
  1127. if ($g['totalcnf'] != 1)
  1128. {
  1129. pdo_update('ewei_shop_goods', array('sales' => $g['sales'] - $g['total']), array('uniacid' => $_W['uniacid'], 'id' => $g['goodsid']));
  1130. }
  1131. $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']));
  1132. pdo_update('ewei_shop_goods', array('salesreal' => $salesreal), array('id' => $g['goodsid']));
  1133. }
  1134. }
  1135. }
  1136. public function setSelfGoodsStocks($logid = 0)
  1137. {
  1138. global $_W;
  1139. if ($logid == 0)
  1140. {
  1141. return false;
  1142. }
  1143. $goods = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_cashier_pay_log_goods') . ' WHERE cashierid=:cashierid AND logid=:logid', array(':cashierid' => $_W['cashierid'], ':logid' => $logid));
  1144. foreach ($goods as $g )
  1145. {
  1146. 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']));
  1147. }
  1148. }
  1149. public function querycoupons($kwd = '')
  1150. {
  1151. global $_W;
  1152. global $_GPC;
  1153. $params = array();
  1154. $params[':uniacid'] = $_W['uniacid'];
  1155. $params[':merchid'] = intval($_W['cashieruser']['merchid']);
  1156. $condition = ' and uniacid=:uniacid and merchid=:merchid and (total=-1 OR total>0)';
  1157. if (empty($_W['cashieruser']['couponid']))
  1158. {
  1159. return array();
  1160. }
  1161. $condition .= 'AND id IN (' . $_W['cashieruser']['couponid'] . ')';
  1162. if (!(empty($kwd)))
  1163. {
  1164. $condition .= ' AND `couponname` LIKE :keyword';
  1165. $params[':keyword'] = '%' . $kwd . '%';
  1166. }
  1167. $ds = pdo_fetchall('SELECT id,couponname as title , thumb FROM ' . tablename('ewei_shop_coupon') . ' WHERE 1 ' . $condition . ' order by createtime desc', $params);
  1168. $ds = set_medias($ds, 'thumb');
  1169. return $ds;
  1170. }
  1171. public function seedCoupon($price = 0, $member)
  1172. {
  1173. global $_W;
  1174. if (empty($member))
  1175. {
  1176. return false;
  1177. }
  1178. if (is_string($member))
  1179. {
  1180. $member = m('member')->getMember($member);
  1181. }
  1182. $set = $this->getUserSet('', $_W['cashierid']);
  1183. $price = floatval($price);
  1184. if (empty($price) || empty($set['coupon']) || (isset($set['coupontime']) && !($this->sale_time($set['coupontime']))))
  1185. {
  1186. return false;
  1187. }
  1188. if (empty($set['coupon']['couponid']))
  1189. {
  1190. return false;
  1191. }
  1192. if (!(empty($set['coupon']['minmoney'])) && ($price < floatval($set['coupon']['minmoney'])))
  1193. {
  1194. return false;
  1195. }
  1196. $coupon = com('coupon');
  1197. if ($coupon)
  1198. {
  1199. $data = $coupon->getCoupon($set['coupon']['couponid']);
  1200. if (($data['total'] == '-1') || (0 < $data['total']))
  1201. {
  1202. $coupon->poster($member, $set['coupon']['couponid'], 1);
  1203. return $set['coupon']['couponid'];
  1204. }
  1205. }
  1206. return false;
  1207. }
  1208. public function caculatecoupon($couponid, $totalprice, $openid = NULL)
  1209. {
  1210. global $_W;
  1211. $openid = ((is_null($openid) ? $_W['openid'] : $openid));
  1212. $uniacid = $_W['uniacid'];
  1213. $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';
  1214. $sql .= ' left join ' . tablename('ewei_shop_coupon') . ' c on d.couponid = c.id';
  1215. $sql .= ' where d.id=:id and d.uniacid=:uniacid and d.openid=:openid and d.used=0 limit 1';
  1216. $data = pdo_fetch($sql, array(':uniacid' => $uniacid, ':id' => $couponid, ':openid' => $openid));
  1217. if (empty($data))
  1218. {
  1219. return false;
  1220. }
  1221. $data['enough'] = floatval($data['enough']);
  1222. if (!(empty($data['enough'])) && ($totalprice < $data['enough']))
  1223. {
  1224. return false;
  1225. }
  1226. $deduct = (double) $data['deduct'];
  1227. $discount = (double) $data['discount'];
  1228. $backtype = (double) $data['backtype'];
  1229. $deductprice = 0;
  1230. if ((0 < $deduct) && ($backtype == 0) && (0 < $totalprice))
  1231. {
  1232. if ($totalprice < $deduct)
  1233. {
  1234. $deduct = $totalprice;
  1235. }
  1236. if ($deduct <= 0)
  1237. {
  1238. $deduct = 0;
  1239. }
  1240. $deductprice = $deduct;
  1241. }
  1242. else if ((0 < $discount) && ($backtype == 1))
  1243. {
  1244. $deductprice = round($totalprice * (1 - ($discount / 10)), 2);
  1245. if ($totalprice < $deductprice)
  1246. {
  1247. $deductprice = $totalprice;
  1248. }
  1249. if ($deductprice <= 0)
  1250. {
  1251. $deductprice = 0;
  1252. }
  1253. }
  1254. $new_price = $totalprice - $deductprice;
  1255. return array('old_price' => $totalprice, 'new_price' => round($new_price, 2), 'discount' => $discount, 'money' => $deductprice);
  1256. }
  1257. public function sendCredit1($log, $userset = NULL)
  1258. {
  1259. $credit1_double = 1;
  1260. $price = $log['money'] + $log['deduction'];
  1261. if (empty($userset['credit1']) || empty($price))
  1262. {
  1263. return 0;
  1264. }
  1265. if (!(empty($userset['credit1_double'])))
  1266. {
  1267. $credit1_double = $userset['credit1_double'];
  1268. }
  1269. $price = $price * $credit1_double;
  1270. $credit1 = com_run('sale::getCredit1', $log['openid'], $price, 37, 1, 0, $log['title'] . '收银订单号 : ' . $log['logno'] . ' 收银台消费送积分');
  1271. return $credit1;
  1272. }
  1273. }
  1274. function sort_cashier($a, $b)
  1275. {
  1276. $enough1 = floatval($a['enough']);
  1277. $enough2 = floatval($b['enough']);
  1278. if ($enough1 == $enough2)
  1279. {
  1280. return 0;
  1281. }
  1282. return ($enough1 < $enough2 ? 1 : -1);
  1283. }
  1284. ?>