PospalForm.php 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\pospal;
  8. use app\models\Option;
  9. use app\constants\OptionSetting;
  10. use app\models\AccountLog;
  11. use app\models\User;
  12. use app\models\SaasUser;
  13. use app\models\Cat;
  14. use app\models\Goods;
  15. use app\models\Attr;
  16. use app\models\AttrGroup;
  17. use app\modules\admin\models\GoodsForm;
  18. use app\models\GoodsCat;
  19. use app\models\Order;
  20. use app\models\OrderDetail;
  21. use app\models\OrderPospal;
  22. use app\models\OrderPospalDetail;
  23. use app\models\PospalCat;
  24. use app\models\PospalGoods;
  25. use app\models\PospalUser;
  26. use app\models\PospalOrder;
  27. use app\models\Store;
  28. use app\utils\pospal\Api;
  29. use app\models\common\CommonOrder;
  30. use app\modules\client\models\v1\BindForm;
  31. use app\modules\client\models\v1\ShareMoneyForm;
  32. use app\modules\client\models\OrderComplete;
  33. use app\models\UserShareLog;
  34. use app\models\Share;
  35. use app\models\UserShareMoney;
  36. use app\models\UserBindLog;
  37. use app\models\Level;
  38. class PospalForm extends Model
  39. {
  40. public $hostInfo;
  41. public $store_id;
  42. public $sync_stock_uids = [];
  43. public static function isopen($store_id = 0) {
  44. $appkey = Api::appkey($store_id);
  45. return empty($appkey) ? 0 : 1;
  46. }
  47. public function pGoodsList($param = []) {
  48. if($param['barcode']){
  49. $list = Api::queryProductByBarcode($this->store_id, $param['barcode']);
  50. return $list;
  51. }
  52. $list = Api::queryProductPages($this->store_id, $param);
  53. return $list;
  54. }
  55. public function pUserList($param = []) {
  56. $list = Api::queryCustomerPages($this->store_id, $param);
  57. return $list;
  58. }
  59. public function getSyncCache($key) {
  60. return Option::get($key, $this->store_id, 'store')['value'];
  61. }
  62. public function setSyncCache($key, $val) {
  63. return Option::set($key, $val, $this->store_id, 'store');
  64. }
  65. public function orderNotify($order){
  66. if(!self::isopen($this->store_id)){
  67. return;
  68. }
  69. return $this->syncOrder2Yinbao($order->id);
  70. }
  71. public function afterGoodsSave($goods, $changedAttributes){
  72. if(!self::isopen($this->store_id)){
  73. return;
  74. }
  75. return $this->syncGoods2Yinbao($goods->id, $changedAttributes);
  76. }
  77. public function afterUserSave($user){
  78. if(!self::isopen($this->store_id)){
  79. return;
  80. }
  81. return $this->syncUser2Yinbao($user->id);
  82. }
  83. public function needSyncStockUidsYb() {
  84. $orderIdMax = intval($this->getSyncCache(OptionSetting::POSPAL_SYNC_STOCK_YB_ORDERID_MAX));
  85. $query = PospalGoods::find()->alias('pg');
  86. $query->leftJoin(['od' => OrderPospalDetail::tableName()], 'pg.goods_id = od.goods_id');
  87. $query->leftJoin(['o' => OrderPospal::tableName()], 'o.id = od.order_id');
  88. $query->andWhere(['>', 'o.id', $orderIdMax]);
  89. $res = $query->select('o.id orderid, pg.goods_id, pg.productUid')->groupBy('goods_id')->orderBy('orderid ASC')->asArray()->all();
  90. return $res;
  91. }
  92. public function needSyncStockUids() {
  93. $orderIdMax = intval($this->getSyncCache(OptionSetting::POSPAL_SYNC_STOCK_ORDERID_MAX));
  94. $query = PospalGoods::find()->alias('pg');
  95. $query->leftJoin(['od' => OrderDetail::tableName()], 'pg.goods_id = od.goods_id');
  96. $query->leftJoin(['o' => Order::tableName()], 'o.id = od.order_id');
  97. $query->andWhere(['>', 'o.id', $orderIdMax]);
  98. $res = $query->select('o.id orderid, pg.goods_id, pg.productUid')->groupBy('goods_id')->orderBy('orderid ASC')->asArray()->all();
  99. return $res;
  100. }
  101. //从银豹同步订单/减商品库存
  102. public function syncOrders($param = [], $okCount = 0){
  103. if(!self::isopen($this->store_id)){
  104. return;
  105. }
  106. set_time_limit(0);
  107. $i = 0;
  108. $time = time()-$i*86400;
  109. empty($param['startTime']) && $param['startTime'] = date('Y-m-d H:i:s', $time - 3600);
  110. empty($param['endTime']) && $param['endTime'] = date('Y-m-d H:i:s', $time);
  111. $orders = Api::queryTicketPages($this->store_id, $param);
  112. // $orders = json_decode('{"status":"success","messages":[],"data":{"postBackParameter":{"parameterType":"LAST_RESULT_MAX_ID","parameterValue":"11047888"},"result":[{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":1770095525384431672,"sn":"202305290833048640002","datetime":"2023-05-29 08:33:04","totalAmount":35.00,"totalProfit":6.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 08:33:08","externalOrderNo":"4200057841202305291614222138","remark":"","items":[{"id":24715270,"name":"华润五丰寒地东北大米 5kg/袋","buyPrice":29.00000000,"sellPrice":35.00,"customerPrice":35.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":35.00,"totalProfit":6.00000000,"isCustomerDiscount":1,"productUid":227844485398636082,"productBarcode":"6956056600012","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":35.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0002"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":1235988339197949796,"sn":"202305290854475270003","datetime":"2023-05-29 08:54:47","totalAmount":2.00,"totalProfit":1.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 08:54:51","externalOrderNo":"4200001873202305298027075630","remark":"","items":[{"id":24723579,"name":"农夫山泉天然水550ml/瓶","buyPrice":1.00000000,"sellPrice":2.00,"customerPrice":2.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":2.00,"totalProfit":1.00000000,"isCustomerDiscount":0,"productUid":126346716668394093,"productBarcode":"6921168509256","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":2.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0003"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":1503365188614434406,"sn":"202305290907231940004","datetime":"2023-05-29 09:07:23","totalAmount":27.00,"totalProfit":2.80,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 09:07:26","externalOrderNo":"4200001855202305296498929001","remark":"","items":[{"id":24728442,"name":"无码商品","buyPrice":15.00000000,"sellPrice":15.00,"customerPrice":15.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":15.00,"totalProfit":0E-8,"isCustomerDiscount":0,"productUid":1685322427191116108,"isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]},{"id":24728443,"name":"统一阿萨姆原味奶茶500ml/瓶","buyPrice":3.20000000,"sellPrice":4.00,"customerPrice":4.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":4.00,"totalProfit":0.80000000,"isCustomerDiscount":1,"productUid":526311942833963253,"productBarcode":"6925303730574","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]},{"id":24728444,"name":"达利园注心巧克力派260g/包","buyPrice":6.00000000,"sellPrice":8.00,"customerPrice":8.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":8.00,"totalProfit":2.00000000,"isCustomerDiscount":1,"productUid":784071213180411994,"productBarcode":"6911988007209","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":27.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0004"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":23779605014613706,"sn":"202305290909260730005","datetime":"2023-05-29 09:09:26","totalAmount":8.00,"totalProfit":2.20,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 09:09:29","externalOrderNo":"4200001865202305295299253992","remark":"","items":[{"id":24729320,"name":"康师傅冰红茶柠檬味1L/瓶","buyPrice":2.90000000,"sellPrice":4.00,"customerPrice":4.00,"quantity":2.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":8.00,"totalProfit":2.20000000,"isCustomerDiscount":1,"productUid":548419923932191655,"productBarcode":"6922456896362","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":8.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0005"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":86127327725930988,"sn":"202305290920596140006","datetime":"2023-05-29 09:20:59","totalAmount":5.00,"totalProfit":1.40,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 09:21:02","externalOrderNo":"4200001866202305296887452416","remark":"","items":[{"id":24733982,"name":"农夫山泉茶兀蜜桃乌龙茶500ml/瓶","buyPrice":3.60000000,"sellPrice":5.00,"customerPrice":5.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":5.00,"totalProfit":1.40000000,"isCustomerDiscount":1,"productUid":1122283104413246111,"productBarcode":"6921168593569","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":5.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0006"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":661507702745965305,"sn":"202305290925457620007","datetime":"2023-05-29 09:25:55","totalAmount":8.00,"totalProfit":1.50,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 09:25:55","remark":"","items":[{"id":24735980,"name":"特仑苏有机纯牛奶250ml/瓶","buyPrice":6.50000000,"sellPrice":8.00,"customerPrice":8.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":8.00,"totalProfit":1.50000000,"isCustomerDiscount":1,"productUid":265605305856195537,"productBarcode":"6923644285036","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_1","amount":8.00}]},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":118636800949823123,"sn":"202305290933121500008","datetime":"2023-05-29 09:33:12","totalAmount":5.00,"totalProfit":0.50,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 09:33:15","externalOrderNo":"4200059248202305296965753331","remark":"","items":[{"id":24738744,"name":"安慕希风味酸奶205ml(盒)","buyPrice":4.50000000,"sellPrice":5.00,"customerPrice":5.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":5.00,"totalProfit":0.50000000,"isCustomerDiscount":1,"productUid":939881303174811439,"productBarcode":"6907992512570","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":5.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0008"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":1347406214677782228,"sn":"202305291006275310009","datetime":"2023-05-29 10:06:28","totalAmount":10.50,"totalProfit":3.30,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 10:06:29","remark":"","items":[{"id":24751584,"name":"晶心低钠盐400g/袋","buyPrice":2.40000000,"sellPrice":3.50,"customerPrice":3.50,"quantity":3.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":10.50,"totalProfit":3.30000000,"isCustomerDiscount":1,"productUid":814556814232884208,"productBarcode":"6922311001504","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_1","amount":10.50}]},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":214911432762,"sn":"202305291059040850010","datetime":"2023-05-29 10:59:04","totalAmount":4.60,"totalProfit":0.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 10:59:08","externalOrderNo":"4200067681202305298808566251","remark":"","items":[{"id":24773449,"name":"无码商品","buyPrice":4.60000000,"sellPrice":4.60,"customerPrice":4.60,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":4.60,"totalProfit":0E-8,"isCustomerDiscount":0,"productUid":1685329142464934416,"isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":4.60}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0010"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":2492415667839217670,"sn":"202305291107522870011","datetime":"2023-05-29 11:07:53","totalAmount":11.00,"totalProfit":0.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 11:07:53","remark":"","items":[{"id":24777613,"name":"无码商品","buyPrice":11.00000000,"sellPrice":11.00,"customerPrice":11.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":11.00,"totalProfit":0E-8,"isCustomerDiscount":0,"productUid":1685329653099465764,"isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_1","amount":11.00}]},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":167123812307132400,"sn":"202305291108338330012","datetime":"2023-05-29 11:08:42","totalAmount":49.00,"totalProfit":13.40,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 11:08:42","remark":"","items":[{"id":24778094,"name":"统一阿萨姆奶茶1.5L/瓶","buyPrice":8.60000000,"sellPrice":12.00,"customerPrice":12.00,"quantity":2.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":24.00,"totalProfit":6.80000000,"isCustomerDiscount":1,"productUid":1031419696635823237,"productBarcode":"6925303739454","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]},{"id":24778095,"name":"美年达橙味饮料汽水 2L","buyPrice":5.30000000,"sellPrice":7.00,"customerPrice":7.00,"quantity":2.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":14.00,"totalProfit":3.40000000,"isCustomerDiscount":1,"productUid":146195606367304196,"productBarcode":"6915766000826","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]},{"id":24778096,"name":"雪碧清爽柠檬味饮料2L","buyPrice":5.30000000,"sellPrice":7.00,"customerPrice":7.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":7.00,"totalProfit":1.70000000,"isCustomerDiscount":1,"productUid":1106265916467625467,"productBarcode":"6954767433073","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]},{"id":24778097,"name":"伊利火炬香草口味冰淇淋80克/支","buyPrice":2.50000000,"sellPrice":4.00,"customerPrice":4.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":4.00,"totalProfit":1.50000000,"isCustomerDiscount":1,"productUid":1121871764110816648,"productBarcode":"6907992822709","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_1","amount":49.00}]},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":669363428927196978,"sn":"202305291110548000013","datetime":"2023-05-29 11:10:54","totalAmount":4.00,"totalProfit":0.80,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 11:10:58","externalOrderNo":"4200067674202305294482565680","remark":"","items":[{"id":24779393,"name":"美汁源果粒奶优菠萝味水果牛奶饮料450g/瓶","buyPrice":3.20000000,"sellPrice":4.00,"customerPrice":4.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":4.00,"totalProfit":0.80000000,"isCustomerDiscount":0,"productUid":517058333993442949,"productBarcode":"6956416203402","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":4.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0013"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":249493661972895410,"sn":"202305291114101040014","datetime":"2023-05-29 11:14:11","totalAmount":4.00,"totalProfit":1.60,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 11:14:11","remark":"","items":[{"id":24780908,"name":"晶心中盐加碘食用盐400g/袋","buyPrice":1.20000000,"sellPrice":2.00,"customerPrice":2.00,"quantity":2.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":4.00,"totalProfit":1.60000000,"isCustomerDiscount":1,"productUid":41831880508985532,"productBarcode":"6922311001306","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_1","amount":4.00}]},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":544496958183373393,"uid":409963322694929892,"sn":"202305291118071410015","datetime":"2023-05-29 11:18:07","totalAmount":4.00,"totalProfit":0.40,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 11:18:09","externalOrderNo":"4200001866202305293576207525","remark":"","items":[{"id":24782901,"name":"雀巢咖啡原味1+2速溶15g/支","buyPrice":1.20000000,"sellPrice":2.00,"customerPrice":2.00,"quantity":3.000,"discount":66.66667,"customerDiscount":100.00,"totalAmount":4.00,"totalProfit":0.40000000,"isCustomerDiscount":1,"productUid":815546838447584211,"productBarcode":"6917878044729","isWeighing":0,"ticketitemattributes":[],"discountDetails":[{"productUid":815546838447584211,"discountType":6,"discountRate":66.66667,"discountAmount":0.66667,"discountRuleUid":0,"discountTotalAmount":2.00,"datetime":"2023-05-29 11:18:07"}],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":4.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0015"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":199055072119788256,"sn":"202305291128280100016","datetime":"2023-05-29 11:28:28","totalAmount":2.00,"totalProfit":1.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 11:28:31","externalOrderNo":"4200067684202305295693565371","remark":"","items":[{"id":24788274,"name":"怡宝纯净水555ml","buyPrice":1.00000000,"sellPrice":2.00,"customerPrice":2.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":2.00,"totalProfit":1.00000000,"isCustomerDiscount":1,"productUid":839630792014576874,"productBarcode":"6901285991219","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":2.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0016"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":686679351800155692,"sn":"202305291129330580017","datetime":"2023-05-29 11:29:33","totalAmount":6.00,"totalProfit":1.50,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 11:29:35","externalOrderNo":"4200059249202305296525946795","remark":"","items":[{"id":24788828,"name":"国药集团积醉解风味饮料240ml/罐","buyPrice":4.50000000,"sellPrice":6.00,"customerPrice":6.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":6.00,"totalProfit":1.50000000,"isCustomerDiscount":1,"productUid":815609676422751865,"productBarcode":"6971772961452","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":6.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0017"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":35696205555915267,"sn":"202305291138008170018","datetime":"2023-05-29 11:38:00","totalAmount":100.00,"totalProfit":0.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 11:38:04","externalOrderNo":"4200001862202305292893712081","remark":"","items":[{"id":24794054,"name":"无码商品","buyPrice":100.00000000,"sellPrice":100.00,"customerPrice":100.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":100.00,"totalProfit":0E-8,"isCustomerDiscount":0,"productUid":1685331477808719665,"isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":100.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0018"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":384535591230405433,"sn":"202305291220300480019","datetime":"2023-05-29 12:20:30","totalAmount":4.00,"totalProfit":1.10,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 12:20:35","externalOrderNo":"4200057837202305294713723168","remark":"","items":[{"id":24821913,"name":"百事可乐太汽系列白桃乌龙味500","buyPrice":2.90000000,"sellPrice":4.00,"customerPrice":4.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":4.00,"totalProfit":1.10000000,"isCustomerDiscount":1,"productUid":46114672084537658,"productBarcode":"6915766005081","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":4.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0019"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":391467556600875334,"sn":"202305291300270930020","datetime":"2023-05-29 13:00:32","totalAmount":149.97,"totalProfit":51.19,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 13:00:34","remark":"","items":[{"id":24839156,"name":"怡宝纯净水555ml","buyPrice":1.00000000,"sellPrice":2.00,"customerPrice":2.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":2.00,"totalProfit":1.00000000,"isCustomerDiscount":1,"productUid":839630792014576874,"productBarcode":"6901285991219","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]},{"id":24839157,"name":"云南蓝莓葡萄A级","buyPrice":18.00000000,"sellPrice":49.60,"customerPrice":49.60,"quantity":0.920,"discount":100.00000,"customerDiscount":100.00,"totalAmount":45.63,"totalProfit":29.07072581,"isCustomerDiscount":1,"productUid":230287254216269643,"productBarcode":"2211026","isWeighing":1,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]},{"id":24839158,"name":"老挝进口香蕉A级","buyPrice":7.00000000,"sellPrice":11.80,"customerPrice":9.80,"quantity":2.317,"discount":100.00000,"customerDiscount":100.00,"totalAmount":27.34,"totalProfit":11.12135593,"isCustomerDiscount":0,"productUid":352248709527617620,"productBarcode":"2211080","isWeighing":1,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]},{"id":24839159,"name":"伊利安慕希原味酸奶230ml*10/提","buyPrice":65.00000000,"sellPrice":75.00,"customerPrice":75.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":75.00,"totalProfit":10.00000000,"isCustomerDiscount":1,"productUid":292314413556524364,"productBarcode":"6907992514178","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_1","amount":149.97}]},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":446674388176021810,"sn":"202305291327172450021","datetime":"2023-05-29 13:27:17","totalAmount":1.00,"totalProfit":0.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 13:27:21","externalOrderNo":"4200001868202305290516952884","remark":"","items":[{"id":24847850,"name":"无码商品","buyPrice":1.00000000,"sellPrice":1.00,"customerPrice":1.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":1.00,"totalProfit":0E-8,"isCustomerDiscount":0,"productUid":1685338036123002492,"isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":1.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0021"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":401797095890325341,"sn":"202305291354211290022","datetime":"2023-05-29 13:54:21","totalAmount":28.00,"totalProfit":14.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 13:54:25","externalOrderNo":"4200001859202305290586934257","remark":"","items":[{"id":24856152,"name":"怡宝纯净水555ml","buyPrice":1.00000000,"sellPrice":2.00,"customerPrice":2.00,"quantity":14.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":28.00,"totalProfit":14.00000000,"isCustomerDiscount":1,"productUid":839630792014576874,"productBarcode":"6901285991219","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":28.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0022"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":1634335269744141504,"sn":"202305291355062500023","datetime":"2023-05-29 13:55:11","totalAmount":12.35,"totalProfit":4.56,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 13:55:12","remark":"","items":[{"id":24856403,"name":"静宁片红苹果90#A级","buyPrice":13.00000000,"sellPrice":24.00,"customerPrice":1.00,"quantity":0.369,"discount":100.00000,"customerDiscount":100.00,"totalAmount":8.85,"totalProfit":4.05625000,"isCustomerDiscount":0,"productUid":629521688225667493,"productBarcode":"2211038","isWeighing":1,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]},{"id":24856404,"name":"伊利纯牛奶250ml","buyPrice":3.00000000,"sellPrice":3.50,"customerPrice":3.50,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":3.50,"totalProfit":0.50000000,"isCustomerDiscount":1,"productUid":235408933014276878,"productBarcode":"6907992100272","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_1","amount":12.35}]},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":997131943933226865,"sn":"202305291355337900024","datetime":"2023-05-29 13:55:33","totalAmount":4.00,"totalProfit":1.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 13:55:37","externalOrderNo":"4200059246202305293811197983","remark":"","items":[{"id":24856506,"name":"康师傅水蜜桃1L/瓶","buyPrice":3.00000000,"sellPrice":4.00,"customerPrice":4.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":4.00,"totalProfit":1.00000000,"isCustomerDiscount":1,"productUid":108606589723706730,"productBarcode":"6922456889906","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":4.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0024"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":168164870064613378,"sn":"202305291444510380025","datetime":"2023-05-29 14:44:51","totalAmount":19.00,"totalProfit":2.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 14:44:56","externalOrderNo":"4200001878202305296454372915","remark":"","items":[{"id":24871854,"name":"无码商品","buyPrice":15.00000000,"sellPrice":15.00,"customerPrice":15.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":15.00,"totalProfit":0E-8,"isCustomerDiscount":0,"productUid":1685342677047973674,"isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]},{"id":24871855,"name":"农夫山泉天然水550ml/瓶","buyPrice":1.00000000,"sellPrice":2.00,"customerPrice":2.00,"quantity":2.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":4.00,"totalProfit":2.00000000,"isCustomerDiscount":0,"productUid":126346716668394093,"productBarcode":"6921168509256","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":19.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0025"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":1379115746226265030,"sn":"202305291535463260026","datetime":"2023-05-29 15:35:46","totalAmount":9.00,"totalProfit":3.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 15:35:55","externalOrderNo":"4200066272202305298853790021","remark":"","items":[{"id":24888503,"name":"口水娃元气零食片片香香辣味78g/袋","buyPrice":3.00000000,"sellPrice":4.50,"customerPrice":4.50,"quantity":2.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":9.00,"totalProfit":3.00000000,"isCustomerDiscount":1,"productUid":951887611489126191,"productBarcode":"6947245887291","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":9.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0026"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":635955904204826352,"sn":"202305291547394980027","datetime":"2023-05-29 15:47:54","totalAmount":4.00,"totalProfit":1.50,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 15:47:55","remark":"","items":[{"id":24893484,"name":"怡宝纯净水1.555L","buyPrice":2.50000000,"sellPrice":4.00,"customerPrice":4.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":4.00,"totalProfit":1.50000000,"isCustomerDiscount":1,"productUid":398464377287734739,"productBarcode":"6901285991271","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_1","amount":4.00}]},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":3389519309558740868,"sn":"202305291614514370028","datetime":"2023-05-29 16:14:51","totalAmount":6.00,"totalProfit":1.50,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 16:15:03","externalOrderNo":"4200066272202305293530505599","remark":"","items":[{"id":24905151,"name":"国药集团积醉解风味饮料240ml/罐","buyPrice":4.50000000,"sellPrice":6.00,"customerPrice":6.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":6.00,"totalProfit":1.50000000,"isCustomerDiscount":1,"productUid":815609676422751865,"productBarcode":"6971772961452","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":6.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0028"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":1598792652086715612,"sn":"202305291624581700029","datetime":"2023-05-29 16:24:58","totalAmount":30.00,"totalProfit":3.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 16:25:03","externalOrderNo":"4200067679202305292865482795","remark":"","items":[{"id":24909874,"name":"无码商品","buyPrice":24.00000000,"sellPrice":24.00,"customerPrice":24.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":24.00,"totalProfit":0E-8,"isCustomerDiscount":0,"productUid":1685348664415586690,"isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]},{"id":24909875,"name":"贝卓BAZON尼龙扎带100根(2.5)3*150/袋","buyPrice":3.00000000,"sellPrice":6.00,"customerPrice":6.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":6.00,"totalProfit":3.00000000,"isCustomerDiscount":1,"productUid":300688425076327003,"productBarcode":"6941334135195","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":30.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0029"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":308448173958667421,"sn":"202305291626453050030","datetime":"2023-05-29 16:26:47","totalAmount":3.00,"totalProfit":0.80,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 16:26:49","remark":"","items":[{"id":24910750,"name":"可乐可乐经典美味500ML/瓶","buyPrice":2.20000000,"sellPrice":3.00,"customerPrice":3.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":3.00,"totalProfit":0.80000000,"isCustomerDiscount":0,"productUid":147458061563215496,"productBarcode":"6954767412573","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_1","amount":3.00}]},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":2650702680380045278,"sn":"202305291643189500031","datetime":"2023-05-29 16:43:19","totalAmount":3.50,"totalProfit":0.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 16:43:22","externalOrderNo":"4200001858202305293200459411","remark":"","items":[{"id":24919187,"name":"无码商品","buyPrice":3.50000000,"sellPrice":3.50,"customerPrice":3.50,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":3.50,"totalProfit":0E-8,"isCustomerDiscount":0,"productUid":1685349794555529180,"isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":3.50}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0031"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":4484746223952809016,"sn":"202305291654044170032","datetime":"2023-05-29 16:54:04","totalAmount":17.00,"totalProfit":5.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 16:54:08","externalOrderNo":"4200067678202305294044848839","remark":"","items":[{"id":24924812,"name":"百草味甄选清甜白桃干果脯45g/袋","buyPrice":5.20000000,"sellPrice":7.00,"customerPrice":7.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":7.00,"totalProfit":1.80000000,"isCustomerDiscount":1,"productUid":409651947091407190,"productBarcode":"6941410725333","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]},{"id":24924813,"name":"零嘴上零食虎皮凤爪鲜辣味70克/袋","buyPrice":6.80000000,"sellPrice":10.00,"customerPrice":10.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":10.00,"totalProfit":3.20000000,"isCustomerDiscount":1,"productUid":794874512156681613,"productBarcode":"4897115501098","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":17.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0032"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":296197634688158735,"sn":"202305291656477840033","datetime":"2023-05-29 16:56:47","totalAmount":3.50,"totalProfit":1.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 16:56:51","externalOrderNo":"4200001859202305293837948897","remark":"","items":[{"id":24926248,"name":"真果粒草莓味250g(盒)","buyPrice":2.50000000,"sellPrice":3.50,"customerPrice":3.50,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":3.50,"totalProfit":1.00000000,"isCustomerDiscount":1,"productUid":407736867120106061,"productBarcode":"6923644268503","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":3.50}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0033"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":1562478375889523038,"sn":"202305291659156400034","datetime":"2023-05-29 16:59:15","totalAmount":3.00,"totalProfit":0.50,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 16:59:19","externalOrderNo":"4200001867202305293212171237","remark":"","items":[{"id":24927811,"name":"景田纯净水1.5L","buyPrice":2.50000000,"sellPrice":3.00,"customerPrice":3.50,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":3.00,"totalProfit":0.50000000,"isCustomerDiscount":1,"productUid":1681447118029731714,"productBarcode":"6944649700126","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":3.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0034"}},{"cashierUid":832101480292933201,"cashier":{"jobNumber":"1001","name":"刘本之","uid":832101480292933201},"customerUid":0,"uid":90728990137448333,"sn":"202305291713143400035","datetime":"2023-05-29 17:13:14","totalAmount":3.00,"totalProfit":1.00,"discount":100.00000,"rounding":0.00,"ticketType":"SELL","invalid":0,"sysUpdateTime":"2023-05-29 17:13:18","externalOrderNo":"4200001878202305294321556096","remark":"","items":[{"id":24936168,"name":"宜简白桃味苏打水饮品360ml/瓶","buyPrice":2.00000000,"sellPrice":3.00,"customerPrice":3.00,"quantity":1.000,"discount":100.00000,"customerDiscount":100.00,"totalAmount":3.00,"totalProfit":1.00000000,"isCustomerDiscount":1,"productUid":867168497559034854,"productBarcode":"6950549800354","isWeighing":0,"ticketitemattributes":[],"discountDetails":[],"saleGuiderList":[]}],"payments":[{"code":"payCode_-2900","amount":3.00}],"ticketOnTable":{"tableUid":0,"tableAreaUid":0,"tableCardNo":"0035"}}],"pageSize":100}}', true);
  113. if ($orders['status'] == 'success') {
  114. $data = $orders['data']['result'];
  115. foreach ($data as $order) {
  116. if($order['ticketType'] == 'SELL_RETURN'){
  117. continue;
  118. }
  119. $syncOrder = $this->syncOrder($order);
  120. if($syncOrder['code'] == 0){
  121. $okCount++;
  122. }else{
  123. \Yii::error([__METHOD__, $param, $okCount, $syncOrder]);
  124. // return $syncOrder;
  125. }
  126. }
  127. if(count($data) == $orders['data']['pageSize']){
  128. empty($orders['data']['postBackParameter']) || $param['postBackParameter'] = $orders['data']['postBackParameter'];
  129. return $this->syncOrders($param, $okCount);
  130. }
  131. $uids = [];
  132. foreach($this->needSyncStockUids() as $item){
  133. $productUid = $item['productUid'];
  134. $syncGoodsStock = $this->syncGoodsStock($productUid);
  135. $orderIdMax = $item['orderid'];
  136. $this->setSyncCache(OptionSetting::POSPAL_SYNC_STOCK_ORDERID_MAX, $orderIdMax);
  137. if($syncGoodsStock['code'] != 0){
  138. \Yii::error([__METHOD__, $param, $productUid, $syncGoodsStock]);
  139. return [
  140. 'code' => 1,
  141. 'msg' => '同步失败,syncGoodsStock错误' . $syncGoodsStock['msg'],
  142. ];
  143. }
  144. $uids[$productUid] = 1;
  145. }
  146. foreach($this->needSyncStockUidsYb() as $item){
  147. $productUid = $item['productUid'];
  148. if(isset($uids[$productUid])){
  149. continue;
  150. }
  151. $syncGoodsStock = $this->syncGoodsStock($productUid);
  152. $orderIdMax = $item['orderid'];
  153. $this->setSyncCache(OptionSetting::POSPAL_SYNC_STOCK_YB_ORDERID_MAX, $orderIdMax);
  154. if($syncGoodsStock['code'] != 0){
  155. \Yii::error([__METHOD__, $param, $productUid, $syncGoodsStock]);
  156. return [
  157. 'code' => 1,
  158. 'msg' => '同步失败,syncGoodsStock错误' . $syncGoodsStock['msg'],
  159. ];
  160. }
  161. }
  162. return [
  163. 'code' => 0,
  164. 'msg' => '同步成功,数量:' . $okCount,
  165. ];
  166. }
  167. \Yii::error([__METHOD__, $param, $orders]);
  168. return [
  169. 'code' => 1,
  170. 'msg' => '银豹订单数据获取失败,同步数量:' . $okCount,
  171. ];
  172. }
  173. public function syncOrder($order){
  174. \Yii::error([__METHOD__, 'syncOrder', $order['sn']]);
  175. if($this->hasOrder($order['sn'])){
  176. return [
  177. 'code' => 0,
  178. 'msg' => '此前已同步成功',
  179. ];
  180. }
  181. $t = \Yii::$app->db->beginTransaction();
  182. try{
  183. $customerUid = $order['customerUid'];
  184. if($customerUid){
  185. $syncUser = $this->syncUser($customerUid);
  186. if($syncUser['code'] != 0){
  187. $t->rollBack();
  188. return [
  189. 'code' => 1,
  190. 'msg' => '同步失败,syncUser错误' . $syncUser['msg'],
  191. ];
  192. }
  193. }
  194. $syncUids = [];
  195. $items = $order['items'];
  196. foreach ($items as $item) {
  197. $productUid = $item['productUid'];
  198. if(!$this->hasGoods($productUid)){
  199. $syncUids[] = $productUid;
  200. }
  201. $this->sync_stock_uids[$productUid] = $productUid;
  202. $syncGoods = $this->syncGoods($productUid);
  203. if($syncGoods['code'] != 0){
  204. $t->rollBack();
  205. return [
  206. 'code' => 1,
  207. 'msg' => '同步失败,syncGoods错误' . $syncGoods['msg'],
  208. ];
  209. }
  210. }
  211. $add = $this->addOrder($order, $syncUids);
  212. if(!$add){
  213. $t->rollBack();
  214. return [
  215. 'code' => 1,
  216. 'msg' => '同步失败,addOrder错误',
  217. ];
  218. }
  219. $savePospalOrder = $this->savePospalOrder(0, $order, $add->id);
  220. if(!$savePospalOrder){
  221. $t->rollBack();
  222. return [
  223. 'code' => 1,
  224. 'msg' => '同步失败,savePospalOrder错误',
  225. ];
  226. }
  227. $t->commit();
  228. \Yii::error([__METHOD__, '$savePospalOrder', $savePospalOrder, $order['sn']]);
  229. } catch (\Exception $e) {
  230. $t->rollBack();
  231. \Yii::error([__METHOD__, $e]);
  232. return [
  233. 'code' => 1,
  234. 'msg' => '同步失败,' . $e->getMessage(),
  235. ];
  236. }
  237. return [
  238. 'code' => 0,
  239. 'msg' => '同步成功',
  240. ];
  241. }
  242. public function syncUser($uid, $phone = ''){
  243. if($phone){
  244. if($this->hasUserPhone($phone)){
  245. return [
  246. 'code' => 0,
  247. 'msg' => '此前已同步成功',
  248. ];
  249. }
  250. }else{
  251. if(empty($uid)){
  252. return [
  253. 'code' => 0,
  254. 'msg' => 'uid为空',
  255. ];
  256. }
  257. if($this->hasUser($uid)){
  258. return [
  259. 'code' => 0,
  260. 'msg' => '此前已同步成功',
  261. ];
  262. }
  263. }
  264. $t = \Yii::$app->db->beginTransaction();
  265. try{
  266. if($phone){
  267. $api = Api::queryUserBytel($this->store_id, $phone);
  268. }else{
  269. $api = Api::queryUserByUid($this->store_id, $uid);
  270. }
  271. if ($api['status'] != 'success') {
  272. \Yii::error([__METHOD__, $uid, $phone, $api]);
  273. $t->rollBack();
  274. return [
  275. 'code' => 1,
  276. 'msg' => '同步失败,queryUserByUid错误' . json_encode($api['messages']),
  277. ];
  278. }
  279. $user = $api['data'];
  280. $add = $this->addUser($user);
  281. if(!$add){
  282. \Yii::error([__METHOD__, $uid, $user, $add]);
  283. $t->rollBack();
  284. return [
  285. 'code' => 1,
  286. 'msg' => '同步失败,addUser错误',
  287. ];
  288. }
  289. $savePospalUser = $this->savePospalUser($uid, $user, $add->id);
  290. if(!$savePospalUser){
  291. \Yii::error([__METHOD__, $uid, $user, $add, $savePospalUser]);
  292. $t->rollBack();
  293. return [
  294. 'code' => 1,
  295. 'msg' => '同步失败,savePospalUser错误',
  296. ];
  297. }
  298. $t->commit();
  299. return [
  300. 'code' => 0,
  301. 'msg' => '同步成功',
  302. ];
  303. } catch (\Exception $e) {
  304. $t->rollBack();
  305. \Yii::error([__METHOD__, $e]);
  306. return [
  307. 'code' => 1,
  308. 'msg' => '同步失败,' . $e->getMessage(),
  309. ];
  310. }
  311. }
  312. public function syncCat($param = [], $okCount = 0){
  313. $t = \Yii::$app->db->beginTransaction();
  314. try{
  315. $cats = Api::queryProductCategoryPages($this->store_id, $param);
  316. if ($cats['status'] == 'success') {
  317. $data = $cats['data']['result'];
  318. $catname = [];
  319. foreach ($data as $item) {
  320. if(isset($catname[$item['name']])){
  321. \Yii::error([__METHOD__, $param, $item, $catname[$item['name']]]);
  322. $t->rollBack();
  323. return [
  324. 'code' => 1,
  325. 'msg' => '同步失败,银豹系统分类名称有重复' . $item['name'],
  326. ];
  327. }
  328. $catname[$item['name']] = $item['name'];
  329. $okCount++;
  330. $pcat = $this->hasCat($item['uid']);
  331. if($pcat){
  332. continue;
  333. }
  334. $cats = Cat::findAll(['store_id' => $this->store_id, 'name' => $item['name']]);
  335. $cid = 0;
  336. foreach ($cats as $cat) {
  337. if(!$pcat = PospalCat::findOne(['store_id' => $this->store_id, 'cat_id' => $cat->id])){
  338. $cid = $cat->id;
  339. }
  340. }
  341. if(!$cid){
  342. $catModel = new Cat();
  343. $catModel->store_id = $this->store_id;
  344. $catModel->name = $item['name'];
  345. $add = $catModel->save();
  346. $cid = $add->id;
  347. }
  348. $savePospalCat = $this->savePospalCat($item['uid'], $item, $cid);
  349. if(!$savePospalCat){
  350. \Yii::error([__METHOD__, $param, $okCount, $savePospalCat]);
  351. $t->rollBack();
  352. return [
  353. 'code' => 1,
  354. 'msg' => '同步失败,savePospalCat错误',
  355. ];
  356. }
  357. }
  358. if(count($data) < $cats['data']['pageSize']){
  359. empty($cats['data']['postBackParameter']) || $param['postBackParameter'] = $cats['data']['postBackParameter'];
  360. $t->commit();
  361. return $this->syncCat($param, $okCount);
  362. }
  363. $allPospalCat = PospalCat::findAll(['store_id' => $this->store_id]);
  364. foreach ($allPospalCat as $item) {
  365. if($item->parentUid){
  366. $pcat = $this->hasCat($item->parentUid);
  367. $cat = Cat::findOne($item->cat_id);
  368. $cat->parent_id = $pcat->cat_id;
  369. $save = $cat->save();
  370. if(!$save){
  371. \Yii::error([__METHOD__, $param, $item, $cat]);
  372. $t->rollBack();
  373. return [
  374. 'code' => 1,
  375. 'msg' => '同步失败,$cat->save()错误',
  376. ];
  377. }
  378. }
  379. }
  380. $t->commit();
  381. return [
  382. 'code' => 0,
  383. 'msg' => '同步成功,数量:' . $okCount,
  384. ];
  385. }
  386. \Yii::error([__METHOD__, $param, $cats]);
  387. $t->rollBack();
  388. return [
  389. 'code' => 1,
  390. 'msg' => '银豹分类数据获取失败,同步数量:' . $okCount,
  391. ];
  392. } catch (\Exception $e) {
  393. $t->rollBack();
  394. \Yii::error([__METHOD__, $e]);
  395. return [
  396. 'code' => 1,
  397. 'msg' => '同步失败,' . $e->getMessage(),
  398. ];
  399. }
  400. }
  401. public function syncGoodsStock($uid, $baseOn = 0){
  402. if(!$pgoods = $this->hasGoods($uid)){
  403. $syncGoods = $this->syncGoods($uid);
  404. if($syncGoods['code'] != 0){
  405. \Yii::error([__METHOD__, $uid, $syncGoods]);
  406. return [
  407. 'code' => 1,
  408. 'msg' => '同步失败' . $syncGoods['msg'],
  409. ];
  410. }
  411. $pgoods = $this->hasGoods($uid);
  412. }
  413. $pProduct = Api::queryProductByUid($this->store_id, $uid);
  414. if ($pProduct['status'] != 'success') {
  415. \Yii::error([__METHOD__, $uid, $pProduct]);
  416. return [
  417. 'code' => 1,
  418. 'msg' => '同步失败,queryProductByUid接口调用失败',
  419. ];
  420. }
  421. if (!$pProduct['data']) {
  422. \Yii::error([__METHOD__, $uid, $pProduct]);
  423. return [
  424. 'code' => 1,
  425. 'msg' => '同步失败,没有找到银豹商品',
  426. ];
  427. }
  428. $pstock = $pProduct['data']['stock'];
  429. $goods = Goods::findOne($pgoods['goods_id']);
  430. $gattrid = $this->getGoodsAttrIdByPuid($uid);
  431. if(!$gattrid){
  432. \Yii::error([__METHOD__, $uid, $goods, $gattrid]);
  433. return [
  434. 'code' => 1,
  435. 'msg' => '同步失败,没有找到规格' . $uid,
  436. ];
  437. }
  438. $attr = json_decode($goods->attr, true);
  439. $attr_id_list = [];
  440. $gstock = intval($attr[$gattrid - 1]['num']);
  441. foreach ($attr[$gattrid - 1]['attr_list'] as $item) {
  442. array_push($attr_id_list, $item['attr_id']);
  443. }
  444. if($gstock != $pstock){
  445. if($baseOn == 0){
  446. $api = Api::updateProductInfo($this->store_id, [
  447. 'productInfo' => [
  448. 'uid' => $uid,
  449. 'stock' => $gstock,
  450. ]
  451. ]);
  452. if ($api['status'] != 'success') {
  453. \Yii::error([__METHOD__, $uid, $gstock, $api]);
  454. return [
  455. 'code' => 1,
  456. 'msg' => '同步失败,接口调用失败updateProductInfo',
  457. ];
  458. }
  459. }
  460. if($baseOn == 1){
  461. $add = $goods->numAdd($attr_id_list, $pstock - $gstock);
  462. if (!$add) {
  463. \Yii::error([__METHOD__, $uid, $attr_id_list, $pstock, $gstock, $goods]);
  464. return [
  465. 'code' => 1,
  466. 'msg' => '同步失败,numAdd错误',
  467. ];
  468. }
  469. }
  470. }
  471. return [
  472. 'code' => 0,
  473. 'msg' => '同步成功',
  474. ];
  475. }
  476. public function syncGoodsStockByGoodsId($id, $baseOn = 0) {
  477. $pgoods = PospalGoods::findAll(['store_id' => $this->store_id, 'goods_id' => $id]);
  478. if(!$pgoods){
  479. $syncGoods2Yinbao = $this->syncGoods2Yinbao($id);
  480. if($syncGoods2Yinbao['code'] != 0){
  481. \Yii::error([__METHOD__, $id, $syncGoods2Yinbao]);
  482. return [
  483. 'code' => 1,
  484. 'msg' => '同步失败,syncGoods2Yinbao错误',
  485. ];
  486. }
  487. }
  488. $pgoods = PospalGoods::findAll(['store_id' => $this->store_id, 'goods_id' => $id]);
  489. foreach ($pgoods as $item) {
  490. $syncGoodsStock = $this->syncGoodsStock($item->productUid, $baseOn);
  491. if($syncGoodsStock['code'] != 0){
  492. \Yii::error([__METHOD__, $id, $item]);
  493. return [
  494. 'code' => 1,
  495. 'msg' => '同步失败,syncGoodsStock错误',
  496. ];
  497. }
  498. }
  499. return [
  500. 'code' => 0,
  501. 'msg' => '同步成功',
  502. ];
  503. }
  504. public function syncGoods($uid, $barcode = 0){
  505. if($barcode){
  506. if($this->hasGoodsBarcode($barcode)){
  507. return [
  508. 'code' => 0,
  509. 'msg' => '此前已同步成功',
  510. ];
  511. }
  512. }else{
  513. if($this->hasGoods($uid)){
  514. return [
  515. 'code' => 0,
  516. 'msg' => '此前已同步成功',
  517. ];
  518. }
  519. }
  520. $t = \Yii::$app->db->beginTransaction();
  521. try{
  522. if($barcode){
  523. $api = Api::queryProductByBarcode($this->store_id, $barcode);
  524. }else{
  525. $api = Api::queryProductByUid($this->store_id, $uid);
  526. }
  527. if ($api['status'] != 'success') {
  528. \Yii::error([__METHOD__, $uid, $barcode, $api]);
  529. $t->rollBack();
  530. return [
  531. 'code' => 1,
  532. 'msg' => '同步失败,queryProductByUid错误' . json_encode($api['messages']),
  533. ];
  534. }
  535. if(empty($api['data'])){
  536. $t->rollBack();
  537. return [
  538. 'code' => 0,
  539. 'msg' => '未知商品',
  540. ];
  541. }
  542. $goods = $api['data'];
  543. $add = $this->addGoods($goods);
  544. if(!$add){
  545. \Yii::error([__METHOD__, $uid, $barcode, $goods]);
  546. $t->rollBack();
  547. return [
  548. 'code' => 1,
  549. 'msg' => '同步失败,addGoods错误',
  550. ];
  551. }
  552. $savePospalGoods = $this->savePospalGoods($uid, $goods, $add->id);
  553. if(!$savePospalGoods){
  554. \Yii::error([__METHOD__, $uid, $barcode, $goods, $savePospalGoods]);
  555. $t->rollBack();
  556. return [
  557. 'code' => 1,
  558. 'msg' => '同步失败,savePospalGoods错误',
  559. ];
  560. }
  561. $t->commit();
  562. return [
  563. 'code' => 0,
  564. 'msg' => '同步成功',
  565. ];
  566. } catch (\Exception $e) {
  567. $t->rollBack();
  568. \Yii::error([__METHOD__, $e]);
  569. return [
  570. 'code' => 1,
  571. 'msg' => '同步失败,' . $e->getMessage(),
  572. ];
  573. }
  574. }
  575. public function hasOrder($sn){
  576. $res = PospalOrder::findOne(['store_id' => $this->store_id, 'sn' => $sn]);
  577. return $res;
  578. }
  579. public function hasUser($uid){
  580. $res = PospalUser::findOne(['store_id' => $this->store_id, 'customerUid' => $uid]);
  581. return $res;
  582. }
  583. public function hasUserPhone($phone){
  584. $res = PospalUser::findOne(['store_id' => $this->store_id, 'phone' => $phone]);
  585. return $res;
  586. }
  587. public function hasCat($uid){
  588. $res = PospalCat::findOne(['store_id' => $this->store_id, 'uid' => $uid]);
  589. if(!$res){
  590. $syncCat = $this->syncCat();
  591. if($syncCat){
  592. $res = PospalCat::findOne(['store_id' => $this->store_id, 'uid' => $uid]);
  593. return $res;
  594. }
  595. }
  596. return $res;
  597. }
  598. public function hasGoods($uid){
  599. $res = PospalGoods::findOne(['store_id' => $this->store_id, 'productUid' => $uid]);
  600. return $res;
  601. }
  602. public function hasGoodsBarcode($barcode){
  603. $res = PospalGoods::findOne(['store_id' => $this->store_id, 'barcode' => $barcode]);
  604. return $res;
  605. }
  606. public function savePospalOrder($uid, $data, $id){
  607. $model = new PospalOrder();
  608. $model->store_id = $this->store_id;
  609. empty($data['uid']) || $model->uid = $data['uid'];
  610. $model->sn = $data['sn'];
  611. empty($data['webOrderNo']) || $model->webOrderNo = $data['webOrderNo'];
  612. $model->json = json_encode($data);
  613. $model->order_id = $id;
  614. $res = $model->save();
  615. if(!$res){
  616. \Yii::error([__METHOD__, $data, $model]);
  617. }
  618. return $res;
  619. }
  620. public function savePospalUser($uid, $data, $id){
  621. $model = new PospalUser();
  622. $model->store_id = $this->store_id;
  623. $model->customerUid = $data['customerUid'];
  624. $model->number = $data['number'];
  625. $model->phone = $data['phone'];
  626. $model->json = json_encode($data);
  627. $model->user_id = $id;
  628. $res = $model->save();
  629. if(!$res){
  630. \Yii::error([__METHOD__, $data, $model]);
  631. }
  632. return $res;
  633. }
  634. public function savePospalCat($uid, $data, $id){
  635. $model = new PospalCat();
  636. $model->store_id = $this->store_id;
  637. $model->uid = $data['uid'];
  638. $model->parentUid = $data['parentUid'];
  639. $model->name = $data['name'];
  640. $model->cat_id = $id;
  641. $res = $model->save();
  642. if(!$res){
  643. \Yii::error([__METHOD__, $data, $model]);
  644. }
  645. return $res;
  646. }
  647. public function savePospalGoods($uid, $data, $id){
  648. $model = new PospalGoods();
  649. $model->store_id = $this->store_id;
  650. $model->productUid = $data['uid'];
  651. $model->barcode = $data['barcode'];
  652. $model->spu = $data['attribute5'];
  653. $model->json = json_encode($data);
  654. $model->goods_id = $id;
  655. $res = $model->save();
  656. if(!$res){
  657. \Yii::error([__METHOD__, $data, $model]);
  658. }
  659. return $res;
  660. }
  661. public function addOrder($data, $syncUids = []){
  662. $userid = 0;
  663. if($data['customerUid']){
  664. $puser = $this->hasUser($data['customerUid']);
  665. if(!$puser){
  666. \Yii::error([__METHOD__, $data, $puser]);
  667. return false;
  668. }
  669. $userid = $puser['user_id'];
  670. }
  671. $totalAmount = $data['totalAmount'];
  672. $integral = ['forehead' => 0, 'forehead_integral' => 0];
  673. if($data['pointUsage']){
  674. $integral = ['forehead' => $data['pointUsage']['money'], 'forehead_integral' => $data['pointUsage']['point']];
  675. }
  676. $pay_type = 6;
  677. if($data['payments']){
  678. foreach($data['payments'] as $pay){
  679. //储值卡
  680. if($pay['code'] == 'payCode_2'){
  681. $pay_type = 3;
  682. break;
  683. }
  684. }
  685. }
  686. $model = new OrderPospal();
  687. $model->store_id = $this->store_id;
  688. $model->user_id = $userid;
  689. $model->order_no = 'YB'.$data['sn'];
  690. $model->total_price = $totalAmount;
  691. $model->pay_price = $totalAmount;
  692. $model->integral_price = 0;
  693. $model->integral = json_encode($integral);
  694. $model->created_at = time();
  695. $model->is_offline = 1;
  696. $model->version = cyy_version();
  697. $model->discount = $data['discount'] / 10;
  698. $model->remark = $data['remark'];
  699. $model->pay_type = $pay_type;
  700. $model->trade_status = Order::ORDER_FLOW_CONFIRM;
  701. $model->is_pay = 1;
  702. $model->pay_time = strtotime($data['datetime']);
  703. $model->confirm_time = strtotime($data['datetime']);
  704. $model->send_time = strtotime($data['datetime']);
  705. $model->created_at = strtotime($data['datetime']);
  706. $model->first_price = 0;
  707. $model->second_price = 0;
  708. $model->third_price = 0;
  709. $res = $model->save();
  710. if(!$res){
  711. \Yii::error([__METHOD__, $data, $model]);
  712. return false;
  713. }
  714. //orderdetail减库存
  715. $goods_profit = 0;
  716. foreach($data['items'] as $item){
  717. $uid = $item['productUid'];
  718. $pGoods = $this->hasGoods($uid);
  719. if(!$pGoods){
  720. continue;
  721. }
  722. $goods = Goods::findOne($pGoods->goods_id);
  723. $gattrid = $this->getGoodsAttrIdByPuid($uid);
  724. if(!$gattrid){
  725. continue;
  726. }
  727. $attr_id_list = [];
  728. $attrs = [];
  729. $attr = json_decode($goods->attr, true);
  730. foreach ($attr[$gattrid - 1]['attr_list'] as $attritem) {
  731. $attrs[] = $attritem['attr_name'];
  732. array_push($attr_id_list, $attritem['attr_id']);
  733. }
  734. $attr_list = Attr::find()->alias('a')
  735. ->select('ag.id AS attr_group_id,ag.attr_group_name,a.id AS attr_id,a.attr_name')
  736. ->leftJoin(['ag' => AttrGroup::tableName()], 'a.attr_group_id=ag.id')
  737. ->where(['a.id' => $attr_id_list, 'ag.store_id' => $this->store_id,])
  738. ->asArray()->all();
  739. $order_detail = new OrderPospalDetail();
  740. $order_detail->order_id = $model->id;
  741. $order_detail->goods_id = $pGoods->goods_id;
  742. $order_detail->num = $item['quantity'] <= 0 ? 0 : intval($item['quantity']);
  743. $order_detail->total_price = $item['totalAmount'];
  744. $order_detail->goods_name = $goods->name . ' ' . implode('/', $attrs);
  745. $order_detail->goods_info = $goods ? json_encode($goods->toArray()) : json_encode([]);
  746. $order_detail->pic = $goods->cover_pic;
  747. $order_detail->attr = json_encode($attr_list, JSON_UNESCAPED_UNICODE);
  748. $order_detail->delivery_type = OrderDetail::GOODS_DELIVERY_SHOP;
  749. // 计算单个商品可分红金额
  750. $profit = $goods->rate_type == 0 ? floatval($order_detail->total_price * $goods->rate / 100) : $goods->rate;
  751. $goods_profit += $profit;
  752. $order_detail->profit = $profit;
  753. $odSave = $order_detail->save();
  754. if($odSave){
  755. if(!in_array($uid, $syncUids)){
  756. $numSub = $goods->numSub($attr_id_list, $item['quantity']);
  757. if(!$numSub){
  758. \Yii::error([__METHOD__, $numSub, $item, $order_detail]);
  759. }
  760. }
  761. }else{
  762. \Yii::error([__METHOD__, $item, $order_detail]);
  763. }
  764. }
  765. // goods_profit 计算之后存入订单表中
  766. $model->profit = $goods_profit;
  767. $model->save();
  768. if(!$userid){
  769. return $model;
  770. }
  771. //减余额积分
  772. if($data['payments']){
  773. foreach($data['payments'] as $pay){
  774. //储值卡
  775. if($pay['code'] == 'payCode_2'){
  776. $amount = $pay['amount'];
  777. AccountLog::saveLog($userid, $amount, AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_EXPEND, 0, $model->id, '银豹消费,订单号:'.$data['sn']);
  778. }
  779. }
  780. }
  781. if($data['pointUsage']){
  782. if($data['pointUsage']['point'] > 0){
  783. $amount = $data['pointUsage']['point'];
  784. AccountLog::saveLog($userid, $amount, AccountLog::TYPE_INTEGRAL, AccountLog::LOG_TYPE_EXPEND, 0, $model->id, '银豹消费,订单号:'.$data['sn']);
  785. }
  786. }
  787. // 绑定上下级
  788. $user = User::findOne($userid);
  789. if ($user && $user->parent_user_id) {
  790. $this->saveParentId($user);
  791. $this->saveParentId($user, 2);
  792. }
  793. $this->setReturnData($model);
  794. // $form = new OrderComplete();
  795. // $form->order_id = $order->id;
  796. // $form->order_type = 0;
  797. // $form->store_id = get_store_id();
  798. // $form->notify();
  799. // 消费满指定金额自动成为分销商
  800. $this->autoBecomeShare($userid, $this->store_id, 'STORE');
  801. // 购买指定或任意商品自动成为分销商
  802. $this->autoBuyGood($userid, $this->store_id, $model->id);
  803. $model->is_sale = 1;
  804. $model->save();
  805. $this->share_money($model);
  806. return $model;
  807. }
  808. /**
  809. * 分销订单佣金结算
  810. * @param Order $order
  811. */
  812. private function sendOrderProfit($order, $expire_day = 0) {
  813. if (!$order->share_order_profit) {
  814. \Yii::warning('订单佣金可分发值为0');
  815. return;
  816. }
  817. $user_share = UserShareLog::find()->where(['user_id' => $order->user_id])->orderBy('created_at desc')->limit(1)->asArray()->one();
  818. \Yii::warning($user_share);
  819. if (empty($user_share)) {
  820. \Yii::warning('订单佣金未找到分享关系');
  821. return;
  822. }
  823. if ($expire_day > 0) {
  824. if ($order->pay_time - $user_share['created_at'] > $expire_day * 24 * 3600) {
  825. \Yii::warning('订单佣金分享关系已过期');
  826. return;
  827. }
  828. }
  829. $user = User::findOne($user_share['parent_id']);
  830. if (!$user) {
  831. \Yii::warning('订单佣金未找到分享关系用户');
  832. return;
  833. }
  834. if ($order->share_send_type == 2) {
  835. AccountLog::saveLog($user_share['parent_id'], $order->share_order_profit, AccountLog::TYPE_INTEGRAL,
  836. AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_ORDER,
  837. $order->id, "商城订单佣金赠送积分, 订单号:{$order->order_no}");
  838. } else if ($order->share_send_type == 3) {
  839. AccountLog::saveLog($user_share['parent_id'], $order->share_order_profit, AccountLog::TYPE_BALANCE,
  840. AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_ORDER,
  841. $order->id, "商城订单佣金赠送余额, 订单号:{$order->order_no}");
  842. } else {
  843. $user->total_price += doubleval($order->share_order_profit);
  844. $user->price += doubleval($order->share_order_profit);
  845. $user->save();
  846. $order->share_price += doubleval($order->share_order_profit);
  847. $order->save();
  848. UserShareMoney::set($order->share_order_profit, $user->id, $order->id, 0, 5, $order->store_id, 0);
  849. }
  850. }
  851. // 分销订单佣金结算
  852. private function share_money($order)
  853. {
  854. if (!$order) {
  855. return;
  856. }
  857. if ($order->is_price != 0) {
  858. return;
  859. }
  860. //排除商盟订单
  861. if($order->user_id == 0) {
  862. return;
  863. }
  864. $setting = Option::get('share_money_setting', $this->store_id);
  865. $setting = $setting ? json_decode($setting['value'], true) : [];
  866. if (!$setting) {
  867. \Yii::warning('订单佣金发送:未开启分销设置1');
  868. return;
  869. }
  870. // if (!is_array($setting['commission_type']) || empty($setting['commission_type'])) {
  871. // \Yii::warning('订单佣金发送:未开启分销设置2');
  872. // return;
  873. // }
  874. // $commission_type = $setting['commission_type'];
  875. // // 订单佣金 2
  876. // if (in_array(2, $commission_type)) {
  877. $this->sendOrderProfit($order, $setting['expire_day']);
  878. // } else {
  879. // \Yii::warning('订单佣金发送:未开启订单佣金分销');
  880. // }
  881. // // 会员佣金
  882. // if (!in_array(1, $commission_type)) {
  883. // \Yii::warning('订单佣金发送:未开启会员佣金分销');
  884. // return;
  885. // }
  886. $share_setting = Option::get('share_basic_setting', $this->store_id);
  887. $share_setting = $share_setting ? json_decode($share_setting['value'], true) : [];
  888. // 分销商自购返利
  889. $order->share_price = 0;
  890. $user = User::findOne(['id' => $order->user_id]);
  891. if ($order->rebate > 0) {
  892. if ($order->share_send_type == 2) {
  893. AccountLog::saveLog($order->user_id, $order->rebate, AccountLog::TYPE_INTEGRAL,
  894. AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_ORDER,
  895. $order->id, "商城订单自购返利赠送积分, 订单号:{$order->order_no}");
  896. } else if ($order->share_send_type == 3) {
  897. AccountLog::saveLog($order->user_id, $order->rebate, AccountLog::TYPE_BALANCE,
  898. AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_ORDER,
  899. $order->id, "商城订单自购返利赠送余额, 订单号:{$order->order_no}");
  900. } else {
  901. $user->total_price += doubleval($order->rebate);
  902. $user->price += doubleval($order->rebate);
  903. $user->save();
  904. $order->share_price += doubleval($order->rebate);
  905. UserShareMoney::set($order->rebate, $user->id, $order->id, 0, 4, $order->store_id, 0);
  906. }
  907. $order->is_price = 1;
  908. }
  909. // 一级佣金发放
  910. if ($share_setting['level']['value'] >= 1) {
  911. $user_1 = User::findOne($order->parent_id);
  912. if (!$user_1) {
  913. $order->save();
  914. return;
  915. }
  916. if ($order->share_send_type == 2) {
  917. AccountLog::saveLog($user_1->id, $order->first_price, AccountLog::TYPE_INTEGRAL,
  918. AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_ORDER,
  919. $order->id, "商城订单分销赠送积分, 订单号:{$order->order_no}");
  920. } else if ($order->share_send_type == 3) {
  921. AccountLog::saveLog($user_1->id, $order->first_price, AccountLog::TYPE_BALANCE,
  922. AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_ORDER,
  923. $order->id, "商城订单分销赠送余额, 订单号:{$order->order_no}");
  924. } else {
  925. $user_1->total_price += $order->first_price;
  926. $user_1->price += $order->first_price;
  927. $user_1->save();
  928. UserShareMoney::set($order->first_price, $user_1->id, $order->id, 0, 1, $order->store_id, 0);
  929. $order->share_price += doubleval($order->first_price);
  930. }
  931. $order->is_price = 1;
  932. }
  933. // 二级佣金发放
  934. if ($share_setting['level']['value'] >= 2) {
  935. $user_2 = User::findOne($order->parent_id_1);
  936. if ($user_2) {
  937. // if (!$user_2) {
  938. // if ($user_1->parent_id != 0 && $order->parent_id_1 == 0) {
  939. // $res = $this->money($user_1->parent_id, $order->second_price);
  940. // UserShareMoney::set($order->second_price, $user_1->parent_id, $order->id, 0, 2, $this->store_id, 0);
  941. //
  942. // if ($res['parent_id'] != 0 && $share_setting['level']['value'] == 3) {
  943. // $res = $this->money($res['parent_id'], $order->third_price);
  944. // UserShareMoney::set($order->third_price, $res['parent_id'], $order->id, 0, 3, $this->store_id, 0);
  945. // }
  946. // }
  947. // $order->save();
  948. // return;
  949. // }
  950. if ($order->share_send_type == 2) {
  951. AccountLog::saveLog($user_2->id, $order->second_price, AccountLog::TYPE_INTEGRAL,
  952. AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_ORDER,
  953. $order->id, "商城订单分销赠送积分, 订单号:{$order->order_no}");
  954. } else if ($order->share_send_type == 3) {
  955. AccountLog::saveLog($user_2->id, $order->second_price, AccountLog::TYPE_BALANCE,
  956. AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_ORDER,
  957. $order->id, "商城订单分销赠送余额, 订单号:{$order->order_no}");
  958. } else {
  959. $user_2->total_price += $order->second_price;
  960. $user_2->price += $order->second_price;
  961. $user_2->save();
  962. UserShareMoney::set($order->second_price, $user_2->id, $order->id, 0, 2, $order->store_id, 0);
  963. $order->share_price += doubleval($order->second_price);
  964. }
  965. }
  966. }
  967. // 三级佣金发放
  968. if ($share_setting['level']['value'] >= 3 && $order->rebate == 0) {
  969. $user_3 = User::findOne($order->parent_id_2);
  970. if ($user_3) {
  971. // if (!$user_3) {
  972. // if ($user_2->parent_id != 0 && $order->parent_id_2 == 0) {
  973. // $this->money($user_2->parent_id, $order->third_price);
  974. // UserShareMoney::set($order->third_price, $user_2->parent_id, $order->id, 0, 3, $this->store_id, 0);
  975. // }
  976. // $order->save();
  977. // return;
  978. // }
  979. if ($order->share_send_type == 2) {
  980. AccountLog::saveLog($user_3->id, $order->third_price, AccountLog::TYPE_INTEGRAL,
  981. AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_ORDER,
  982. $order->id, "商城订单分销赠送积分, 订单号:{$order->order_no}");
  983. } else if ($order->share_send_type == 3) {
  984. AccountLog::saveLog($user_3->id, $order->third_price, AccountLog::TYPE_BALANCE,
  985. AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_ORDER,
  986. $order->id, "商城订单分销赠送余额, 订单号({$order->order_no})");
  987. } else {
  988. $user_3->total_price += $order->third_price;
  989. $user_3->price += $order->third_price;
  990. $user_3->save();
  991. UserShareMoney::set($order->third_price, $user_3->id, $order->id, 0, 3, $order->store_id, 0);
  992. $order->share_price += doubleval($order->third_price);
  993. }
  994. }
  995. }
  996. //临时关系佣金发放
  997. if (!empty($order->limit_id) && !empty($order->limit_price)) {
  998. \Yii::error("临时绑定关系发放佣金", '临时绑定关系发放佣金');
  999. //临时绑定关系发放佣金
  1000. $bind_log = UserBindLog::findOne($order->limit_id);
  1001. $user = User::findOne($bind_log->parent_id);
  1002. $user->total_price += $order->limit_price;
  1003. $user->price += $order->limit_price;
  1004. if (!$user->save()) {
  1005. \Yii::error("用户佣金保存失败");
  1006. }
  1007. UserShareMoney::set($order->limit_price, $user->id, $order->id, 0, 2, $order->store_id, 0);
  1008. $order->is_send_limit = 1;
  1009. $order->share_price += doubleval($order->limit_price);
  1010. $res = $order->save();
  1011. if (!$res) {
  1012. \Yii::error("订单保存失败");
  1013. };
  1014. } else {
  1015. \Yii::error("订单中临时关系佣金为空");
  1016. }
  1017. $order->save();
  1018. return;
  1019. }
  1020. /**
  1021. * 消费满指定金额自动成为分销商
  1022. * @param $user_id integer 用户id
  1023. */
  1024. private function autoBecomeShare($user_id, $store_id, $type = null)
  1025. {
  1026. $setting = Option::get('share_basic_setting', $store_id);
  1027. $setting = $setting ? json_decode($setting['value'], true) : [];
  1028. $auto_share_val = floatval($setting['auto_share_val']['value']);
  1029. if ($auto_share_val == 0) {
  1030. return;
  1031. }
  1032. $share = Share::findOne(['user_id' => $user_id, 'is_delete' => 0, 'store_id' => $store_id]);
  1033. if ($share && $share->status == 1) {
  1034. return;
  1035. }
  1036. $consumption_sum = Order::find()->where(['user_id' => $user_id, 'is_delete' => 0, 'is_pay' => 1])->sum('pay_price');
  1037. $consumption_sum += OrderPospal::find()->where(['user_id' => $user_id, 'is_delete' => 0, 'is_pay' => 1])->sum('pay_price');
  1038. $consumption_sum = floatval(($consumption_sum ? $consumption_sum : 0));
  1039. if ($consumption_sum < $auto_share_val) {
  1040. return;
  1041. }
  1042. if (!$share || $share->status == 2) {
  1043. $share = new Share();
  1044. $share->user_id = $user_id;
  1045. $share->mobile = '';
  1046. $share->name = '';
  1047. $share->is_delete = 0;
  1048. $share->store_id = $store_id;
  1049. }
  1050. $share->status = 1;
  1051. $share->created_at = time();
  1052. $share->save();
  1053. $user = User::findOne($user_id);
  1054. $user->time = time();
  1055. $user->is_distributor = 1;
  1056. $user->save();
  1057. }
  1058. /**
  1059. * 购买指定商品成为分销商
  1060. */
  1061. public function autoBuyGood($user_id, $store_id, $order_id, $type = 0)
  1062. {
  1063. $setting = Option::get('share_basic_setting', $store_id);
  1064. $setting = $setting ? json_decode($setting['value'], true) : [];
  1065. //如果开启无需审核才能开始走下面逻辑
  1066. if (intval($setting['share_condition']['value']) !== 2) {
  1067. return;
  1068. }
  1069. // 购买商城成分销商关闭状态不执行
  1070. if ($setting['share_goods_status']['value'] == 0) {
  1071. return;
  1072. }
  1073. $share = Share::findOne(['user_id' => $user_id, 'is_delete' => 0, 'store_id' => $store_id]);
  1074. if ($share && $share->status == 1) {
  1075. return;
  1076. }
  1077. $goodIds = OrderPospalDetail::find()->where(['order_id' => $order_id])->select('goods_id')->all();
  1078. $sign = false;
  1079. // 购买任意商品
  1080. if ($setting['share_goods_status']['value'] == 1) {
  1081. $sign = true;
  1082. }
  1083. // 购买指定商品自动成为分销商
  1084. if ($setting['share_goods_status']['value'] == 2) {
  1085. foreach ($goodIds as $item) {
  1086. $goods_ids = \explode(',', $setting['share_goods_id']['value']);
  1087. if (in_array($item->goods_id, $goods_ids)) {
  1088. $sign = true;
  1089. break;
  1090. }
  1091. }
  1092. }
  1093. if ($sign) {
  1094. if (!$share || $share->status == 2) {
  1095. $share = new Share();
  1096. $share->user_id = $user_id;
  1097. $share->mobile = '';
  1098. $share->name = '';
  1099. $share->is_delete = 0;
  1100. $share->store_id = $store_id;
  1101. }
  1102. $share->status = 1;
  1103. $share->created_at = time();
  1104. $share->save();
  1105. $user = User::findOne($user_id);
  1106. $user->time = time();
  1107. $user->is_distributor = 1;
  1108. $user->save();
  1109. }
  1110. }
  1111. /**
  1112. * 设置佣金
  1113. * @param Order $order
  1114. * @return mixed
  1115. */
  1116. private function setReturnData($order)
  1117. {
  1118. $form = new ShareMoneyForm();
  1119. $form->store_id = $order->store_id;
  1120. $form->yinbao = 1;
  1121. $form->order = $order;
  1122. $form->order_type = 0;
  1123. return $form->setData();
  1124. }
  1125. /**
  1126. * 分销 保存上级的ID(用于先成为上下级,再成为分销商)
  1127. * @param $user
  1128. * @return static
  1129. */
  1130. public function saveParentId($user, $condition = 1)
  1131. {
  1132. $parentId = $user->parent_user_id;
  1133. if (!$parentId) {
  1134. return;
  1135. }
  1136. // 父级用户不存在
  1137. $parentUser = User::findOne($parentId);
  1138. if (!$parentUser) {
  1139. return;
  1140. }
  1141. if ($user) {
  1142. $form = new BindForm();
  1143. $form->store_id = $this->store_id;
  1144. $form->user_id = $user->id;
  1145. $form->parent_id = $parentId;
  1146. $form->old_parent_id = $parentId;
  1147. $form->condition = $condition;
  1148. $form->save();
  1149. // $user->parent_user_id = $parentId;
  1150. // $user->save();
  1151. }
  1152. return $user;
  1153. }
  1154. public function addUser($data, $moneyAdd = 0){
  1155. // 创建平台会员saas_user
  1156. $saas_user = SaasUser::find()->where(['mobile' => $data['phone'], 'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
  1157. if (!$saas_user) {
  1158. $saas_user = new SaasUser();
  1159. $saas_user->access_token = '';
  1160. $saas_user->name = $data['name'];
  1161. $saas_user->mobile = $data['phone'];
  1162. $saas_user->bytedance_open_id = '';
  1163. $saas_user->platform_open_id = '';
  1164. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/yinbao.png';
  1165. $saas_user->store_id = $this->store_id;
  1166. $saas_user->save();
  1167. }
  1168. $user = User::findOne(['store_id' => $this->store_id, 'binding' => $data['phone']]);
  1169. if($user){
  1170. if($moneyAdd){
  1171. if($data['balance']){
  1172. AccountLog::saveLog($user->id, $data['balance'], AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_INCOME, 0, 0, '银豹同步数据,会员号:'.$data['number'].'银豹会员唯一标识'.$data['customerUid']);
  1173. }
  1174. if($data['point']){
  1175. AccountLog::saveLog($user->id, $data['point'], AccountLog::TYPE_INTEGRAL, AccountLog::LOG_TYPE_INCOME, 0, 0, '银豹同步数据,会员号:'.$data['number'].'银豹会员唯一标识'.$data['customerUid']);
  1176. }
  1177. }
  1178. return $user;
  1179. }
  1180. $model = new User(['yinbaoSync' => 1]);
  1181. $model->store_id = $this->store_id;
  1182. $model->is_delete = User::USER_NOT_DELETE;
  1183. $model->type = User::USER_TYPE_NORMAL;
  1184. $model->binding = $data['phone'];
  1185. $model->nickname = $data['name'];
  1186. $model->avatar_url = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/yinbao.png';
  1187. $model->username = $data['name'];
  1188. $model->password = '';
  1189. $model->access_token = '0';
  1190. $model->level = 0;
  1191. $model->integral = 0;
  1192. $model->money = 0;
  1193. $model->created_at = strtotime($data['createdDate']);
  1194. $res = $model->save();
  1195. if(!$res){
  1196. debug_log([__METHOD__, $model->getErrors(), $data], __CLASS__ . '.log');
  1197. \Yii::error([__METHOD__, $model->getErrors(), $data, $model]);
  1198. }
  1199. if($data['balance']){
  1200. AccountLog::saveLog($model->id, $data['balance'], AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_INCOME, 0, 0, '银豹同步数据,会员号:'.$data['number'].'银豹会员唯一标识'.$data['customerUid']);
  1201. }
  1202. if($data['point']){
  1203. AccountLog::saveLog($model->id, $data['point'], AccountLog::TYPE_INTEGRAL, AccountLog::LOG_TYPE_INCOME, 0, 0, '银豹同步数据,会员号:'.$data['number'].'银豹会员唯一标识'.$data['customerUid']);
  1204. }
  1205. return $res ? $model : 0;
  1206. }
  1207. public function addGoods($data){
  1208. // $pcat = $this->hasCat($data['categoryUid']);
  1209. // if(!$pcat){
  1210. // \Yii::error([__METHOD__, $data, $pcat]);
  1211. // return false;
  1212. // }
  1213. // $catid = [$pcat['cat_id']];
  1214. $catid = 0;
  1215. $use_attr = 0;
  1216. $hasSpu = false;
  1217. if($data['attribute6'] && $data['attribute5']){
  1218. $use_attr = 1;
  1219. //有规格 且 已经同步过其他规格
  1220. $hasSpu = PospalGoods::findOne(['store_id' => $this->store_id, 'spu' => $data['attribute5'], ['not', ['productUid' => $data['productUid']]]]);
  1221. }
  1222. if($hasSpu){
  1223. //setattr
  1224. $goodsId = $hasSpu->goods_id;
  1225. $goods = Goods::findOne($goodsId);
  1226. $model = new GoodsForm(['yinbaoSync' => 1]);
  1227. $model->store_id = $this->store_id;
  1228. $model->use_attr = 1;
  1229. $attr = json_decode($goods->attr, true);
  1230. $attr[] = [
  1231. 'attr_list' => [[
  1232. 'attr_group_name' => '规格',
  1233. 'attr_name' => $data['attribute6'],
  1234. ]],
  1235. 'num' => intval($data['stock']),
  1236. 'price' => $data['sellPrice'],
  1237. 'no' => $data['barcode'],
  1238. ];
  1239. $model->attr = $attr;
  1240. $goods->yinbaoSync = 1;
  1241. $model->setAttr($goods);
  1242. return $goods;
  1243. }else{
  1244. $model = new GoodsForm(['yinbaoSync' => 1]);
  1245. $model->store_id = $this->store_id;
  1246. $model->name = $data['name'];
  1247. $model->price = $data['sellPrice'];
  1248. $model->cost_price = $data['buyPrice'];
  1249. $model->goods_num = $data['stock'] <= 0 ? 0 : intval($data['stock']);
  1250. $model->goods_no = $data['barcode'];
  1251. $model->use_attr = $use_attr;
  1252. $attr = [];
  1253. if($use_attr){
  1254. $attr[] = [
  1255. 'attr_list' => [[
  1256. 'attr_group_name' => '规格',
  1257. 'attr_name' => $data['attribute6'],
  1258. ]],
  1259. 'num' => intval($data['stock']),
  1260. 'price' => $data['sellPrice'],
  1261. 'no' => $data['barcode'],
  1262. ];
  1263. $model->attr = $attr;
  1264. }
  1265. $model->cat_id = $catid;
  1266. $model->service = '';
  1267. $model->content = '';
  1268. $model->product_type = 0;
  1269. $model->form = ['is_form' => 0];
  1270. $model->cover_pic = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/yinbao.png';
  1271. $res = $model->save();
  1272. if($res['code'] != 0){
  1273. \Yii::error([__METHOD__, $res, $data, $model]);
  1274. return false;
  1275. }
  1276. return $res['goods'];
  1277. }
  1278. }
  1279. public function getGoodsAttrIdByPuid($uid) {
  1280. $pGoods = $this->hasGoods($uid);
  1281. $pGoodsJson = json_decode($pGoods->json, true);
  1282. if(!$pGoodsJson['attribute5']){
  1283. return 1;
  1284. }
  1285. $goods = Goods::findOne($pGoods->goods_id);
  1286. $attrs = json_decode($goods->attr, true);
  1287. $attrItems = [];
  1288. foreach ($attrs as $i => $attr) {
  1289. if($pGoods->barcode == $attr['no']){
  1290. return $i+1;
  1291. }
  1292. $attrItem = [];
  1293. foreach($attr['attr_list'] as $att){
  1294. $attrItem[] = $att['attr_name'];
  1295. }
  1296. sort($attrItem);
  1297. $attrItems[] = implode('/', $attrItem);
  1298. }
  1299. $pItemJson = json_decode($pGoods->json, true);
  1300. $pAttr = $pItemJson['attribute6'];
  1301. foreach ($attrItems as $i => $attrItem) {
  1302. if($pAttr == $attrItem){
  1303. return $i+1;
  1304. }
  1305. }
  1306. return false;
  1307. }
  1308. public function syncBalancePointByUserId($userId, $pay, $point){
  1309. $pUser = PospalUser::findOne(['store_id' => $this->store_id, 'user_id' => $userId]);
  1310. if($pUser){
  1311. $tel = $pUser->phone;
  1312. $pUserTel = PospalUser::findAll(['store_id' => $this->store_id, 'phone' => $tel]);
  1313. if(count($pUserTel) > 1){
  1314. $users = [];
  1315. $api = Api::queryUserBytel($this->store_id, $tel);
  1316. if ($api['status'] == 'success' && count((array)$api['data'])) {
  1317. foreach((array)$api['data'] as $_uitem){
  1318. $item = [
  1319. 'customerUid' => $_uitem['customerUid'],
  1320. 'balance' => $pay,
  1321. 'point' => $point,
  1322. ];
  1323. if($pay > 0){
  1324. if($_uitem['balance'] < $pay){
  1325. $item['balance'] = $_uitem['balance'];
  1326. }
  1327. }
  1328. $pay -= $item['balance'];
  1329. if($point > 0){
  1330. if($_uitem['point'] < $point){
  1331. $item['point'] = $_uitem['point'];
  1332. }
  1333. }
  1334. $point -= $item['point'];
  1335. if($item['balance'] > 0 || $item['point'] > 0){
  1336. $users[] = $item;
  1337. }
  1338. }
  1339. }else{
  1340. debug_log([__METHOD__, $this->store_id, $tel, $api], __CLASS__ . '.log');
  1341. \Yii::error([__METHOD__, $this->store_id, $tel, $api]);
  1342. }
  1343. }else{
  1344. $users = [
  1345. [
  1346. 'customerUid' => $pUser->customerUid,
  1347. 'balance' => $pay,
  1348. 'point' => $point,
  1349. ]
  1350. ];
  1351. }
  1352. foreach($users as $userItem){
  1353. //同步过用户更新余额积分
  1354. $syncBalancePoint = $this->syncBalancePoint($userItem['customerUid'], $userItem['balance'], $userItem['point']);
  1355. if ($syncBalancePoint['code'] != 0) {
  1356. debug_log([__METHOD__, $userId, $pay, $point, $userItem, $syncBalancePoint], __CLASS__ . '.log');
  1357. \Yii::error([__METHOD__, $userId, $pay, $point, $userItem, $syncBalancePoint]);
  1358. return [
  1359. 'code' => 1,
  1360. 'msg' => '同步失败,syncBalancePoint错误' . $syncBalancePoint['msg'],
  1361. ];
  1362. }
  1363. }
  1364. }else{
  1365. $syncUser2Yinbao = $this->syncUser2Yinbao($userId);
  1366. if(!$syncUser2Yinbao){
  1367. \Yii::error([__METHOD__, $userId, $syncUser2Yinbao]);
  1368. return [
  1369. 'code' => 1,
  1370. 'msg' => '同步失败,syncUser2Yinbao错误'.$userId . $syncUser2Yinbao['msg'],
  1371. ];
  1372. }
  1373. }
  1374. }
  1375. public function syncBalancePoint($uid, $pay, $point){
  1376. if($pay != 0 || $point != 0){
  1377. $api = Api::updateBalancePointByIncrement($this->store_id, [
  1378. 'customerUid' => $uid,
  1379. 'balanceIncrement' => $pay != 0 ? $pay : null,
  1380. 'pointIncrement' => $point != 0 ? $point : null,
  1381. 'dataChangeTime' => date('Y-m-d H:i:s'),
  1382. ]);
  1383. if ($api['status'] != 'success') {
  1384. \Yii::error([__METHOD__, $uid, $pay, $point, $api]);
  1385. return [
  1386. 'code' => 1,
  1387. 'msg' => '同步失败,Api::updateBalancePointByIncrement错误' . json_encode($api['messages']),
  1388. ];
  1389. }
  1390. }
  1391. return [
  1392. 'code' => 0,
  1393. 'msg' => '同步成功,' . $pay . ',' . $point,
  1394. ];
  1395. }
  1396. public function syncOrder2Yinbao($id) {
  1397. $pOrder = PospalOrder::findOne(['store_id' => $this->store_id, 'order_id' => $id]);
  1398. if($pOrder){
  1399. return [
  1400. 'code' => 0,
  1401. 'msg' => '此前已同步成功',
  1402. ];
  1403. }
  1404. $order = Order::findOne($id);
  1405. if(!$order){
  1406. \Yii::error([__METHOD__, $id]);
  1407. return [
  1408. 'code' => 1,
  1409. 'msg' => '同步失败,Order::findOne错误'.$id,
  1410. ];
  1411. }
  1412. $t = \Yii::$app->db->beginTransaction();
  1413. try{
  1414. $userId = $order->user_id;
  1415. $point = 0;
  1416. if($order->integral){
  1417. $integral = json_decode($order->integral, true);
  1418. $point = -$integral['forehead_integral'];
  1419. }
  1420. //余额支付
  1421. $pay = 0;
  1422. if($order->pay_type == 3){
  1423. $pay = -$order->pay_price;
  1424. }
  1425. $syncBalancePointByUserId = $this->syncBalancePointByUserId($userId, $pay, $point);
  1426. if($syncBalancePointByUserId['code'] != 0){
  1427. $t->rollBack();
  1428. \Yii::error([__METHOD__, $userId, $pay, $point, $syncBalancePointByUserId]);
  1429. return [
  1430. 'code' => 1,
  1431. 'msg' => '同步失败,syncBalancePointByUserId错误'.$id . $syncBalancePointByUserId['msg'],
  1432. ];
  1433. }
  1434. $orderDetail = OrderDetail::findAll(['order_id' => $id]);
  1435. foreach ($orderDetail as $detail) {
  1436. $goodsId = $detail->goods_id;
  1437. $syncGoods2Yinbao = $this->syncGoods2Yinbao($goodsId);
  1438. if(!$syncGoods2Yinbao){
  1439. $t->rollBack();
  1440. \Yii::error([__METHOD__, $goodsId, $syncGoods2Yinbao]);
  1441. return [
  1442. 'code' => 1,
  1443. 'msg' => '同步失败,syncGoods2Yinbao错误'.$userId . $syncGoods2Yinbao['msg'],
  1444. ];
  1445. }
  1446. }
  1447. $t->commit();
  1448. return [
  1449. 'code' => 0,
  1450. 'msg' => '同步成功',
  1451. ];
  1452. } catch (\Exception $e) {
  1453. $t->rollBack();
  1454. \Yii::error([__METHOD__, $e]);
  1455. return [
  1456. 'code' => 1,
  1457. 'msg' => '同步失败,' . $e->getMessage(),
  1458. ];
  1459. }
  1460. }
  1461. public function syncGoods2Yinbao($id, $changedAttributes = []) {
  1462. $pGoods = PospalGoods::findOne(['store_id' => $this->store_id, 'goods_id' => $id]);
  1463. $goods = Goods::findOne($id);
  1464. if($pGoods){
  1465. if(isset($changedAttributes['goods_num']) && ($goods->goods_num != $changedAttributes['goods_num'])){
  1466. $syncGoodsStockByGoodsId = $this->syncGoodsStockByGoodsId($id);
  1467. if($syncGoodsStockByGoodsId['code'] != 0){
  1468. \Yii::error([__METHOD__, $id, $pGoods]);
  1469. return [
  1470. 'code' => 1,
  1471. 'msg' => '同步失败,syncGoodsStockByGoodsId错误',
  1472. ];
  1473. }
  1474. }
  1475. return [
  1476. 'code' => 0,
  1477. 'msg' => '此前已同步成功',
  1478. ];
  1479. }
  1480. $t = \Yii::$app->db->beginTransaction();
  1481. try{
  1482. $goods = Goods::findOne($id);
  1483. if(!$goods){
  1484. \Yii::error([__METHOD__, $id, $goods]);
  1485. $t->rollBack();
  1486. return [
  1487. 'code' => 1,
  1488. 'msg' => '同步失败,Goods::findOne错误'.$id,
  1489. ];
  1490. }
  1491. $pcat_id = null;
  1492. $cat = GoodsCat::findOne(['store_id' => $this->store_id, 'goods_id' => $id, 'is_delete' => 0]);
  1493. $pcat = PospalCat::findOne(['store_id' => $this->store_id, 'cat_id' => $cat->id]);
  1494. if($pcat){
  1495. $pcat_id = $pcat['uid'];
  1496. }
  1497. $attr = json_decode($goods->attr, true);
  1498. foreach ($attr as $attrItem) {
  1499. $attrids = [];
  1500. $attrs = [];
  1501. foreach ($attrItem['attr_list'] as $attr_list) {
  1502. $attrids[] = $attr_list['attr_id'];
  1503. $attrs[] = $attr_list['attr_name'];
  1504. }
  1505. $spu = null;
  1506. if($goods->use_attr){
  1507. $spu = $goods->store_id . 'cyy' . $goods->id;
  1508. }
  1509. $barcode = $goods->store_id . 'cyy' . $goods->id . 'c' . implode('c', $attrids);
  1510. $barcode = $attrItem['no'] ? $attrItem['no'] : $barcode;
  1511. $api = Api::addProductInfo($this->store_id, [
  1512. 'productInfo' => [
  1513. 'categoryUid' => $pcat_id,
  1514. 'name' => $goods->name,
  1515. 'barcode' => $barcode,
  1516. 'attribute5' => $spu,
  1517. 'attribute6' => implode('/', $attrs),
  1518. 'buyPrice' => $goods->cost_price ? $goods->cost_price : $attrItem['price'],
  1519. 'sellPrice' => $attrItem['price'],
  1520. 'stock' => $attrItem['num'],
  1521. 'maxStock' => 99999999,
  1522. 'minStock' => 0,
  1523. 'noStock' => 0,
  1524. ]
  1525. ]);
  1526. if ($api['status'] != 'success') {
  1527. \Yii::error([__METHOD__, $goods, $api]);
  1528. $t->rollBack();
  1529. return [
  1530. 'code' => 1,
  1531. 'msg' => '同步失败,Api::addProductInfo错误' . json_encode($api['messages']),
  1532. ];
  1533. }
  1534. $uid = $api['data']['uid'];
  1535. $api = Api::queryProductByUid($this->store_id, $uid);
  1536. if ($api['status'] != 'success') {
  1537. \Yii::error([__METHOD__, $uid, $api]);
  1538. $t->rollBack();
  1539. return [
  1540. 'code' => 1,
  1541. 'msg' => '同步失败,queryProductByUid错误' . json_encode($api['messages']),
  1542. ];
  1543. }
  1544. $savePospalGoods = $this->savePospalGoods($uid, $api['data'], $id);
  1545. if(!$savePospalGoods){
  1546. \Yii::error([__METHOD__, $uid, $api['data'], $id, $savePospalGoods]);
  1547. $t->rollBack();
  1548. return [
  1549. 'code' => 1,
  1550. 'msg' => '同步失败,savePospalGoods错误',
  1551. ];
  1552. }
  1553. }
  1554. $t->commit();
  1555. return [
  1556. 'code' => 0,
  1557. 'msg' => '同步成功',
  1558. ];
  1559. } catch (\Exception $e) {
  1560. $t->rollBack();
  1561. \Yii::error([__METHOD__, $e]);
  1562. return [
  1563. 'code' => 1,
  1564. 'msg' => '同步失败,' . $e->getMessage(),
  1565. ];
  1566. }
  1567. }
  1568. public function syncUser2Yinbao($id) {
  1569. $pUser = PospalUser::findOne(['store_id' => $this->store_id, 'user_id' => $id]);
  1570. if($pUser){
  1571. return [
  1572. 'code' => 0,
  1573. 'msg' => '此前已同步成功',
  1574. ];
  1575. }
  1576. $user = User::findOne($id);
  1577. if(!$user){
  1578. \Yii::error([__METHOD__, $id]);
  1579. return [
  1580. 'code' => 1,
  1581. 'msg' => '同步失败,User::findOne错误'.$id,
  1582. ];
  1583. }
  1584. $api = Api::queryUserBytel($this->store_id, $user->binding);
  1585. if ($api['status'] == 'success' && count((array)$api['data'])) {
  1586. \Yii::error([__METHOD__, $user->binding, $api]);
  1587. return [
  1588. 'code' => 1,
  1589. 'msg' => '同步失败,银豹会员已存在相同的手机号' . $user->binding,
  1590. ];
  1591. }
  1592. $t = \Yii::$app->db->beginTransaction();
  1593. try{
  1594. $discount = null;
  1595. $level = Level::findOne(['level' => $user->level]);
  1596. if($level){
  1597. $discount = $level->discount * 10;
  1598. }
  1599. $api = Api::addUser($this->store_id, [
  1600. 'customerInfo' => [
  1601. 'number' => $user->id . 'cyy' . $user->binding,
  1602. 'discount' => $discount,
  1603. 'name' => $user->nickname,
  1604. 'phone' => $user->binding,
  1605. 'point' => $user->integral,
  1606. 'balance' => $user->money,
  1607. 'point' => $user->integral,
  1608. ],
  1609. 'extInfo' => [
  1610. 'totalPoint' => $user->total_integral,
  1611. 'nickName' => $user->nickname,
  1612. ]
  1613. ]);
  1614. if ($api['status'] != 'success') {
  1615. \Yii::error([__METHOD__, $user, $api]);
  1616. $t->rollBack();
  1617. return [
  1618. 'code' => 1,
  1619. 'msg' => '同步失败,Api::addUser错误' . json_encode($api['messages']),
  1620. ];
  1621. }
  1622. $uid = $api['data']['customerUid'];
  1623. $api = Api::queryUserByUid($this->store_id, $uid);
  1624. if ($api['status'] != 'success') {
  1625. \Yii::error([__METHOD__, $uid, $api]);
  1626. $t->rollBack();
  1627. return [
  1628. 'code' => 1,
  1629. 'msg' => '同步失败,queryProductByUid错误' . json_encode($api['messages']),
  1630. ];
  1631. }
  1632. $savePospalUser = $this->savePospalUser($uid, $api['data'], $id);
  1633. if(!$savePospalUser){
  1634. \Yii::error([__METHOD__, $uid, $api['data'], $id, $savePospalUser]);
  1635. $t->rollBack();
  1636. return [
  1637. 'code' => 1,
  1638. 'msg' => '同步失败,savePospalUser错误',
  1639. ];
  1640. }
  1641. $t->commit();
  1642. return [
  1643. 'code' => 0,
  1644. 'msg' => '同步成功',
  1645. ];
  1646. } catch (\Exception $e) {
  1647. $t->rollBack();
  1648. \Yii::error([__METHOD__, $e]);
  1649. return [
  1650. 'code' => 1,
  1651. 'msg' => '同步失败,' . $e->getMessage(),
  1652. ];
  1653. }
  1654. }
  1655. //id544 导入银豹会员
  1656. public function importUser() {
  1657. if(!self::isopen($this->store_id)){
  1658. return [
  1659. 'code' => 1,
  1660. 'msg' => '没有银豹配置信息',
  1661. ];
  1662. }
  1663. set_time_limit(0);
  1664. $filename = $_FILES['excel']['name'];
  1665. $tmpname = $_FILES['excel']['tmp_name'];
  1666. $path = \Yii::$app->basePath . '/web/temp/';
  1667. if(!is_dir($path)){
  1668. mkdir($path);
  1669. }
  1670. $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  1671. if (($ext != 'xlsx') && ($ext != 'xls')) {
  1672. \Yii::error([__METHOD__, $filename, $ext]);
  1673. return [
  1674. 'code' => 1,
  1675. 'msg' => '请上传excel文件'
  1676. ];
  1677. }
  1678. $file = time() . $this->store_id . '.' . $ext;
  1679. $uploadfile = $path . $file;
  1680. move_uploaded_file($tmpname, $uploadfile);
  1681. $rows = \Spatie\SimpleExcel\SimpleExcelReader::create($uploadfile)
  1682. ->getRows();
  1683. $arr = [];
  1684. $rows->each(function (array $item) use (&$arr) {
  1685. try{
  1686. if (isset($item['会员号(必填)'])) {
  1687. //首先校验是否重复手机号
  1688. // $is_exit_binding_user = User::findOne(['binding' => $item['电话(必填)'], 'store_id' => $this->store_id, 'is_delete' => 0]);
  1689. // if(!$is_exit_binding_user){
  1690. $res = $this->addUser([
  1691. 'number' => $item['会员号(必填)'],
  1692. 'phone' => $item['电话(必填)'],
  1693. 'name' => $item['姓名(必填)'],
  1694. 'level' => 0,
  1695. 'point' => $item['积分(必填)'],
  1696. 'balance' => $item['余额(必填)'],
  1697. ], 1);
  1698. if($res){
  1699. $arr[] = $res;
  1700. }
  1701. // }
  1702. }
  1703. } catch (\Exception $e){
  1704. \Yii::error([__METHOD__, $item, $e]);
  1705. debug_log($e->getMessage());
  1706. }
  1707. });
  1708. $count = count($arr);
  1709. @unlink($uploadfile);
  1710. return [
  1711. 'code' => $count ? 0 : 1,
  1712. 'msg' => "共导入{$count}条数据"
  1713. ];
  1714. }
  1715. //id544 导入银豹商品
  1716. public function importProduct() {
  1717. if(!self::isopen($this->store_id)){
  1718. return [
  1719. 'code' => 1,
  1720. 'msg' => '没有银豹配置信息',
  1721. ];
  1722. }
  1723. set_time_limit(0);
  1724. $filename = $_FILES['excel']['name'];
  1725. $tmpname = $_FILES['excel']['tmp_name'];
  1726. $path = \Yii::$app->basePath . '/web/temp/';
  1727. if(!is_dir($path)){
  1728. mkdir($path);
  1729. }
  1730. $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  1731. if (($ext != 'xlsx') && ($ext != 'xls')) {
  1732. \Yii::error([__METHOD__, $filename, $ext]);
  1733. return [
  1734. 'code' => 1,
  1735. 'msg' => '请上传excel文件'
  1736. ];
  1737. }
  1738. $file = time() . $this->store_id . '.' . $ext;
  1739. $uploadfile = $path . $file;
  1740. move_uploaded_file($tmpname, $uploadfile);
  1741. $rows = \Spatie\SimpleExcel\SimpleExcelReader::create($uploadfile)
  1742. ->getRows();
  1743. $rowCount = $rows->count();
  1744. if($rowCount > 200){
  1745. return [
  1746. 'code' => 1,
  1747. 'msg' => '每次最大上传200条数据',
  1748. ];
  1749. }
  1750. $rows = \Spatie\SimpleExcel\SimpleExcelReader::create($uploadfile)
  1751. ->getRows();
  1752. $arr = [];
  1753. $arrErr = [];
  1754. $rows->each(function (array $item) use (&$arr, &$arrErr) {
  1755. try{
  1756. if (isset($item['条码'])) {
  1757. $pGoods = $this->hasGoodsBarcode($item['条码']);
  1758. $goods = null;
  1759. if($pGoods){
  1760. $goods = Goods::findOne(['id' => $pGoods->goods_id]);
  1761. }
  1762. if(!$pGoods || !$goods){
  1763. $res = $this->syncGoods(0, $item['条码']);
  1764. if($res['code'] == 0){
  1765. $arr[] = $res;
  1766. }else{
  1767. $arrErr[] = $res['msg'];
  1768. }
  1769. }
  1770. //删除的商品修改状态
  1771. if($goods && $goods->is_delete == 1){
  1772. $goods->is_delete = 0;
  1773. $goods->save();
  1774. $arr[] = $goods;
  1775. }
  1776. }
  1777. } catch (\Exception $e){
  1778. \Yii::error([__METHOD__, $item, $e]);
  1779. debug_log($e->getMessage());
  1780. }
  1781. });
  1782. $count = count($arr);
  1783. @unlink($uploadfile);
  1784. $msg = '';
  1785. if(count($arrErr)){
  1786. $msg = ',错误:' . implode(',', $arrErr);
  1787. }
  1788. return [
  1789. 'code' => $count ? 0 : 1,
  1790. 'msg' => "共导入{$count}条数据" . $msg,
  1791. ];
  1792. }
  1793. //从银豹同步商品图片
  1794. public function importProductImgQueue($pageNo = 0, $okCount = 0, $hostInfo = null){
  1795. $queue = queue_push(new \app\jobs\pospal\ImportProductImgJob([
  1796. 'store_id' => $this->store_id,
  1797. 'hostInfo' => $hostInfo ? $hostInfo : \Yii::$app->request->hostInfo,
  1798. 'pageNo' => $pageNo,
  1799. 'okCount' => $okCount,
  1800. ]), 10);
  1801. return [
  1802. 'code' => 0,
  1803. 'msg' => '操作成功,后台任务处理中,请在商品列表查看图片',
  1804. ];
  1805. }
  1806. //从银豹同步商品图片
  1807. public function importProductImgPage($param = [], $okCount = 0){
  1808. if(!self::isopen($this->store_id)){
  1809. return;
  1810. }
  1811. set_time_limit(0);
  1812. $imgs = Api::queryProductImgPages($this->store_id, $param);
  1813. if ($imgs['status'] == 'success') {
  1814. $upload = new \app\models\common\Upload();
  1815. $upload->store_id = $this->store_id;
  1816. $upload->upload_config = \app\models\UploadConfig::getConf($this->store_id);
  1817. $data = $imgs['data']['result'];
  1818. foreach ($data as $img) {
  1819. $syncImg = $this->syncProductImg($upload, $img);
  1820. if($syncImg['code'] == 0){
  1821. $okCount++;
  1822. }else{
  1823. \Yii::error([__METHOD__, $param, $okCount, $syncImg]);
  1824. }
  1825. }
  1826. if(count($data) == $imgs['data']['pageSize']){
  1827. empty($imgs['data']['postBackParameter']) || $param['postBackParameter'] = $imgs['data']['postBackParameter'];
  1828. // return $this->importProductImg($param, $okCount);
  1829. }
  1830. return [
  1831. 'code' => 0,
  1832. 'msg' => '同步成功,数量:' . $okCount,
  1833. ];
  1834. }
  1835. \Yii::error([__METHOD__, $param, $imgs]);
  1836. return [
  1837. 'code' => 1,
  1838. 'msg' => '银豹商品图片数据获取失败,同步数量:' . $okCount,
  1839. ];
  1840. }
  1841. //从银豹同步商品图片
  1842. public function importProductImg($pageNo = 0, $okCount = 0){
  1843. if(!self::isopen($this->store_id)){
  1844. return;
  1845. }
  1846. set_time_limit(0);
  1847. $upload = new \app\models\common\Upload();
  1848. $upload->store_id = $this->store_id;
  1849. $upload->upload_config = \app\models\UploadConfig::getConf($this->store_id);
  1850. $pageSize = 100;
  1851. $query = PospalGoods::find()->where(['store_id' => $this->store_id]);
  1852. $list = $query->offset(($pageNo - 1) * $pageSize)->limit($pageSize)->orderBy('id DESC')->asArray()->all();
  1853. foreach($list as $item){
  1854. $imgs = Api::queryProductImagesByBarcode($this->store_id, $item['barcode']);
  1855. if ($img = $imgs['data'][0]) {
  1856. $syncImg = $this->syncProductImg($upload, $item, $img);
  1857. if($syncImg['code'] == 0){
  1858. $okCount++;
  1859. }else{
  1860. \Yii::error([__METHOD__, $param, $item['barcode'], $syncImg]);
  1861. }
  1862. }
  1863. }
  1864. if(count($list) == $pageSize){
  1865. $this->importProductImgQueue($pageNo + 1, $okCount, $this->hostInfo);
  1866. }
  1867. \Yii::error([__METHOD__, $pageNo, $okCount]);
  1868. return [
  1869. 'code' => 0,
  1870. 'msg' => '同步成功,数量:' . $okCount,
  1871. ];
  1872. }
  1873. public function syncProductImg($upload, $item, $img) {
  1874. $productUid = $item['productUid'];
  1875. $imageUrl = $img['imageUrl'];
  1876. if(empty($productUid) || empty($imageUrl)){
  1877. return [
  1878. 'code' => 1,
  1879. 'msg' => '没有数据',
  1880. ];
  1881. }
  1882. if(!$pgoods = $this->hasGoods($productUid)){
  1883. \Yii::error([__METHOD__, $productUid, '商品没有同步']);die;
  1884. return [
  1885. 'code' => 1,
  1886. 'msg' => '商品没有同步',
  1887. ];
  1888. }
  1889. $goodsId = $pgoods['goods_id'];
  1890. if(empty($goodsId)){
  1891. return [
  1892. 'code' => 1,
  1893. 'msg' => '商品没有数据',
  1894. ];
  1895. }
  1896. $uploadRes = $upload->downloadImg($imageUrl);
  1897. if($uploadRes['code'] != 0){
  1898. return $uploadRes;
  1899. }
  1900. $url = $uploadRes['url'];
  1901. if (strpos($url, 'http') !== 0){
  1902. $url = $this->hostInfo . $url;
  1903. }
  1904. Goods::updateAll(['cover_pic' => $url], ['id' => $goodsId]);
  1905. $GoodsPic = \app\models\GoodsPic::findOne(['goods_id' => $goodsId]);
  1906. if(empty($GoodsPic)){
  1907. $goodsPic = new \app\models\GoodsPic();
  1908. $goodsPic->goods_id = $goodsId;
  1909. $goodsPic->pic_url = $url;
  1910. $goodsPic->save();
  1911. }
  1912. return [
  1913. 'code' => 0,
  1914. 'msg' => '同步成功',
  1915. ];
  1916. }
  1917. public function userList($params) {
  1918. $query = PospalUser::find()->where(['store_id' => $this->store_id, 'is_delete' => 0]);
  1919. if(!empty($params['name'])){
  1920. $query->andWhere(['like', 'json', $params['name']]);
  1921. }
  1922. if(!empty($params['phone'])){
  1923. $query->andWhere(['phone' => $params['phone']]);
  1924. }
  1925. if(!empty($params['number'])){
  1926. $query->andWhere(['number' => $params['number']]);
  1927. }
  1928. if(!empty($params['customerUid'])){
  1929. $query->andWhere(['customerUid' => $params['customerUid']]);
  1930. }
  1931. if(!empty($params['user_id'])){
  1932. $query->andWhere(['user_id' => $params['user_id']]);
  1933. }
  1934. $data = pagination_make($query);
  1935. foreach ($data['list'] as &$item) {
  1936. $userid = $item['user_id'];
  1937. $user = User::findOne($userid);
  1938. $item['userInfo'] = $user;
  1939. }
  1940. return [
  1941. 'code' => 0,
  1942. 'msg' => 'ok',
  1943. 'data' => $data,
  1944. ];
  1945. }
  1946. public function productList($params) {
  1947. $query = PospalGoods::find()->where(['store_id' => $this->store_id, 'is_delete' => 0]);
  1948. if(!empty($params['name'])){
  1949. $query->andWhere(['like', 'json', $params['name']]);
  1950. }
  1951. if(!empty($params['spu'])){
  1952. $query->andWhere(['spu' => $params['spu']]);
  1953. }
  1954. if(!empty($params['barcode'])){
  1955. $query->andWhere(['barcode' => $params['barcode']]);
  1956. }
  1957. if(!empty($params['productUid'])){
  1958. $query->andWhere(['productUid' => $params['productUid']]);
  1959. }
  1960. if(!empty($params['goods_id'])){
  1961. $query->andWhere(['goods_id' => $params['goods_id']]);
  1962. }
  1963. $data = pagination_make($query);
  1964. foreach ($data['list'] as &$item) {
  1965. $goodsid = $item['goods_id'];
  1966. $goods = Goods::findOne($goodsid);
  1967. $item['goodsInfo'] = $goods;
  1968. }
  1969. return [
  1970. 'code' => 0,
  1971. 'msg' => 'ok',
  1972. 'data' => $data,
  1973. ];
  1974. }
  1975. public function timeslimit() {
  1976. $queryAccessTimes = Api::queryAccessTimes($this->store_id);
  1977. $queryDailyAccessTimesLog = Api::queryDailyAccessTimesLog($this->store_id, [
  1978. "beginDate" => date('Y-m-d', time() - 86400*3),
  1979. "endDate" => date('Y-m-d'),
  1980. ]);
  1981. return [
  1982. 'code' => 0,
  1983. 'msg' => 'ok',
  1984. 'queryAccessTimes' => $queryAccessTimes,
  1985. 'queryDailyAccessTimesLog' => $queryDailyAccessTimesLog,
  1986. ];
  1987. }
  1988. }