OLDataProvider.m 395 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. #import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. #import "UIProgressView+AFNetworking.h"
  15. #import "AFHTTPSessionManager.h"
  16. @interface OLDataProvider ()
  17. @end
  18. @implementation OLDataProvider
  19. +(NSString*) get_offline_soid:(sqlite3*)db
  20. {
  21. NSString* soid=nil;
  22. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  23. [formatter setDateFormat:@"yyMMdd"];
  24. NSString* date = [formatter stringFromDate:[NSDate date]];
  25. for(int i=1;i<999;i++)
  26. {
  27. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  28. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  29. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  30. if(count==0)
  31. return soid;
  32. }
  33. return nil;
  34. }
  35. +(NSArray*) enumOfflineOrder
  36. {
  37. NSMutableArray* ret = [[NSMutableArray alloc] init];
  38. sqlite3 *db = [iSalesDB get_db];
  39. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  40. sqlite3_stmt * statement;
  41. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  42. if ( dbresult== SQLITE_OK)
  43. {
  44. while (sqlite3_step(statement) == SQLITE_ROW)
  45. {
  46. char *so_id = (char*)sqlite3_column_text(statement, 0);
  47. NSString* nsso_id=nil;
  48. if(so_id!=nil)
  49. {
  50. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  51. }
  52. [ret addObject:nsso_id];
  53. }
  54. sqlite3_finalize(statement);
  55. }
  56. [iSalesDB close_db:db];
  57. return ret;
  58. }
  59. +(void) uploadFile:(NSString*) file
  60. {
  61. NSData* data = [NSData dataWithContentsOfFile: file];
  62. UIApplication * app = [UIApplication sharedApplication];
  63. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  64. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  65. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  66. if(appDelegate.user!=nil)
  67. [params setValue:appDelegate.user forKey:@"user"];
  68. // if(appDelegate.contact_id!=nil)
  69. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  70. if(appDelegate.password!=nil)
  71. [params setValue:appDelegate.password forKey:@"password"];
  72. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  73. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  74. } error:nil];
  75. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  76. NSProgress *progress = nil;
  77. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  78. if (error) {
  79. NSString* err_msg = [error localizedDescription];
  80. NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  81. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  82. NSLog(@"data string: %@",str);
  83. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  84. } else {
  85. NSLog(@"response ");
  86. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  87. // 再将NSData转为字符串
  88. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  89. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  90. NSLog(@"data string: %@",jsonStr);
  91. NSDictionary* json = responseObject;
  92. if([[json valueForKey:@"result"] intValue]==2)
  93. {
  94. // NSString* img_url_down = json[@"img_url_aname"];
  95. // NSString* img_url_up = json[@"img_url"];
  96. }
  97. else
  98. {
  99. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  100. }
  101. }
  102. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  103. }];
  104. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  105. //
  106. [uploadTask resume];
  107. }
  108. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  109. {
  110. NSString* serial= [[NSUUID UUID] UUIDString];
  111. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  112. NSString *cachefolder = [paths objectAtIndex:0];
  113. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  114. upfolder = [upfolder stringByAppendingPathComponent:serial];
  115. NSFileManager* fileManager = [NSFileManager defaultManager];
  116. BOOL bdir=YES;
  117. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  118. {
  119. NSError *error = nil;
  120. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  121. if(!bsuccess)
  122. DebugLog(@"Create UPLOAD folder failed");
  123. }
  124. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  125. ret[@"contact"]=[self prepareContact:serial];
  126. ret[@"wishlist"]=[self prepareWishlist:serial];
  127. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  128. NSString* str= [RAUtils dict2string:ret];
  129. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  130. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  131. NSError *error=nil;
  132. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  133. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  134. ZipArchive* zip = [[ZipArchive alloc] init];
  135. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  136. for(int i=0;i<arr_files.count;i++)
  137. {
  138. NSString* file=arr_files[i];
  139. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  140. }
  141. if( ![zip CloseZipFile2] )
  142. {
  143. zippath = @"";
  144. }
  145. ret[@"file"]=zippath;
  146. return ret;
  147. }
  148. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  149. {
  150. if(filename.length==0)
  151. return false;
  152. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  153. bool ret=false;
  154. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  155. NSString *cachefolder = [paths objectAtIndex:0];
  156. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  157. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  158. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  159. upfolder = [upfolder stringByAppendingPathComponent:serial];
  160. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  161. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  162. NSFileManager* fileManager = [NSFileManager defaultManager];
  163. BOOL bdir=NO;
  164. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  165. {
  166. NSError *error = nil;
  167. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  168. {
  169. ret=false;
  170. }
  171. else
  172. {
  173. ret=true;
  174. }
  175. }
  176. return ret;
  177. }
  178. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  179. {
  180. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  181. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  182. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  183. NSString* where=@" 1=1";
  184. if (ver!=nil) {
  185. where=@"is_dirty=1";
  186. }
  187. sqlite3 *db = [iSalesDB get_db];
  188. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  189. sqlite3_stmt * statement;
  190. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  191. int count=0;
  192. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  193. if ( dbresult== SQLITE_OK)
  194. {
  195. while (sqlite3_step(statement) == SQLITE_ROW)
  196. {
  197. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  198. int _id = sqlite3_column_int(statement, 0);
  199. int product_id = sqlite3_column_int(statement, 1);
  200. int item_id = sqlite3_column_int(statement, 2);
  201. int qty = sqlite3_column_int(statement, 3);
  202. char *create_time = (char*)sqlite3_column_text(statement, 4);
  203. NSString* nscreate_time=nil;
  204. if(create_time!=nil)
  205. {
  206. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  207. }
  208. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  209. NSString* nsmodify_time=nil;
  210. if(modify_time!=nil)
  211. {
  212. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  213. }
  214. int is_delete = sqlite3_column_int(statement, 6);
  215. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  216. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  217. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  218. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  219. itemjson[@"create_time"]=nscreate_time;
  220. itemjson[@"modify_time"]=nsmodify_time;
  221. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  222. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  223. count++;
  224. }
  225. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  226. sqlite3_finalize(statement);
  227. }
  228. ret[@"count"]=[NSNumber numberWithInt:count ];
  229. [iSalesDB close_db:db];
  230. return ret;
  231. }
  232. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  233. {
  234. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  235. UIApplication * app = [UIApplication sharedApplication];
  236. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  237. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",orderCode ];
  238. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  239. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  240. DebugLog(@"offline_login sql:%@",sqlQuery);
  241. sqlite3_stmt * statement;
  242. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  243. int count=0;
  244. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  245. if ( dbresult== SQLITE_OK)
  246. {
  247. while (sqlite3_step(statement) == SQLITE_ROW)
  248. {
  249. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  250. int product_id = sqlite3_column_int(statement, 0);
  251. char *str_price = (char*)sqlite3_column_text(statement, 1);
  252. int item_id = sqlite3_column_int(statement, 7);
  253. NSString* Price=nil;
  254. if(str_price==nil)
  255. {
  256. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  257. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  258. if(price==nil)
  259. Price=@"No Price.";
  260. else
  261. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  262. }
  263. else
  264. {
  265. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  266. }
  267. double discount = sqlite3_column_double(statement, 2);
  268. int item_count = sqlite3_column_int(statement, 3);
  269. char *line_note = (char*)sqlite3_column_text(statement, 4);
  270. NSString *nsline_note=nil;
  271. if(line_note!=nil)
  272. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  273. char *name = (char*)sqlite3_column_text(statement, 5);
  274. NSString *nsname=nil;
  275. if(name!=nil)
  276. nsname= [[NSString alloc]initWithUTF8String:name];
  277. char *description = (char*)sqlite3_column_text(statement, 6);
  278. NSString *nsdescription=nil;
  279. if(description!=nil)
  280. nsdescription= [[NSString alloc]initWithUTF8String:description];
  281. // int stockUom = sqlite3_column_int(statement, 8);
  282. // int _id = sqlite3_column_int(statement, 9);
  283. //
  284. //
  285. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  286. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  287. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  288. // int carton=[bsubtotaljson[@"carton"] intValue];
  289. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  290. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  291. itemjson[@"model"]=nsname;
  292. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  293. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  294. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  295. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  296. // itemjson[@"check"]=@"true";
  297. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  298. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  299. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  300. itemjson[@"unit_price"]=Price;
  301. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  302. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  303. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  304. itemjson[@"note"]=nsline_note;
  305. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  306. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  307. count++;
  308. }
  309. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  310. sqlite3_finalize(statement);
  311. }
  312. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  313. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  314. return ret;
  315. }
  316. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  317. {
  318. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  319. sqlite3 *db = [iSalesDB get_db];
  320. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  321. // for(int i=0;i<arr_soid.count;i++)
  322. sqlite3_stmt * statement;
  323. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  324. //int count=0;
  325. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  326. if ( dbresult== SQLITE_OK)
  327. {
  328. int count=0;
  329. while (sqlite3_step(statement) == SQLITE_ROW)
  330. {
  331. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  332. int _id = sqlite3_column_int(statement, 0);
  333. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  334. NSString* nssync_data=nil;
  335. if(sync_data!=nil)
  336. {
  337. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  338. }
  339. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  340. NSString* nsimg_1=nil;
  341. if(img_1!=nil)
  342. {
  343. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  344. [self copy_upImg:serial file:nsimg_1];
  345. }
  346. char *so_no = (char*)sqlite3_column_text(statement, 3);
  347. NSString* nsso_no=nil;
  348. if(so_no!=nil)
  349. {
  350. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  351. }
  352. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  353. itemjson[@"order_type"]=@"submit order";
  354. else
  355. itemjson[@"order_type"]=@"archive order";
  356. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  357. itemjson[@"json_data"]= nssync_data;
  358. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  359. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  360. count++;
  361. }
  362. ret[@"count"]=[NSNumber numberWithInt:count ];
  363. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  364. }
  365. sqlite3_finalize(statement);
  366. [iSalesDB close_db:db];
  367. return ret;
  368. }
  369. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  370. {
  371. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  372. sqlite3 *db = [iSalesDB get_db];
  373. // UIApplication * app = [UIApplication sharedApplication];
  374. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  375. NSString *sqlQuery =@"select _id,img_0,img_1,img_2,sync_data from offline_contact where is_active=1 and not sync_data is null";
  376. sqlite3_stmt * statement;
  377. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  378. int count=0;
  379. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  380. if ( dbresult== SQLITE_OK)
  381. {
  382. while (sqlite3_step(statement) == SQLITE_ROW)
  383. {
  384. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  385. int _id = sqlite3_column_int(statement, 0);
  386. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  387. NSString* nsimg_0=nil;
  388. if(img_0!=nil)
  389. {
  390. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  391. [self copy_upImg:serial file:nsimg_0];
  392. }
  393. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  394. NSString* nsimg_1=nil;
  395. if(img_1!=nil)
  396. {
  397. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  398. [self copy_upImg:serial file:nsimg_1];
  399. }
  400. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  401. NSString* nsimg_2=nil;
  402. if(img_2!=nil)
  403. {
  404. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  405. [self copy_upImg:serial file:nsimg_2];
  406. }
  407. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  408. NSString* nssync_data=nil;
  409. if(sync_data!=nil)
  410. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  411. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  412. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  413. itemjson[@"json_data"]= nssync_data;
  414. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  415. count++;
  416. }
  417. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  418. sqlite3_finalize(statement);
  419. }
  420. ret[@"count"]=[NSNumber numberWithInt:count ];
  421. [iSalesDB close_db:db];
  422. return ret;
  423. }
  424. +(bool) check_offlinedata
  425. {
  426. UIApplication * app = [UIApplication sharedApplication];
  427. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  428. sqlite3 *db = [iSalesDB get_db];
  429. NSString * where=@"1=1";
  430. // if(appDelegate.user!=nil)
  431. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  432. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  433. [iSalesDB close_db:db];
  434. if(count==0)
  435. {
  436. return false;
  437. }
  438. return true;
  439. //
  440. //[iSalesDB close_db:db];
  441. }
  442. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  443. {
  444. UIApplication * app = [UIApplication sharedApplication];
  445. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  446. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  447. sqlite3 *db = [iSalesDB get_db];
  448. NSString* collectId=params[@"collectId"];
  449. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  450. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  451. [iSalesDB execSql:sqlQuery db:db];
  452. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  453. [iSalesDB close_db:db];
  454. appDelegate.wish_count =count;
  455. [appDelegate update_count_mark];
  456. ret[@"result"]= [NSNumber numberWithInt:2];
  457. return ret;
  458. }
  459. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  460. //{
  461. //
  462. // UIApplication * app = [UIApplication sharedApplication];
  463. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  464. //
  465. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  466. // sqlite3 *db = [iSalesDB get_db];
  467. // NSString* product_id=params[@"product_id"];
  468. //
  469. //
  470. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  471. //
  472. //
  473. // for(int i=0;i<arr.count;i++)
  474. // {
  475. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  476. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  477. // if(count==0)
  478. // {
  479. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  480. // [iSalesDB execSql:sqlQuery db:db];
  481. // }
  482. // }
  483. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  484. // [iSalesDB close_db:db];
  485. //
  486. // appDelegate.wish_count =count;
  487. //
  488. // [appDelegate update_count_mark];
  489. // ret[@"result"]= [NSNumber numberWithInt:2];
  490. // return ret;
  491. // //
  492. // //return ret;
  493. //}
  494. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  495. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  496. DebugLog(@"time interval: %lf",interval);
  497. }
  498. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  499. {
  500. UIApplication * app = [UIApplication sharedApplication];
  501. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  502. int sort = [[params objectForKey:@"sort"] intValue];
  503. NSString *sort_str = @"";
  504. switch (sort) {
  505. case 0:{
  506. sort_str = @"order by w.modify_time desc";
  507. }
  508. break;
  509. case 1:{
  510. sort_str = @"order by w.modify_time asc";
  511. }
  512. break;
  513. case 2:{
  514. sort_str = @"order by m.name asc";
  515. }
  516. break;
  517. case 3:{
  518. sort_str = @"order by m.name desc";
  519. }
  520. break;
  521. case 4:{
  522. sort_str = @"order by m.description asc";
  523. }
  524. break;
  525. default:
  526. break;
  527. }
  528. NSString* user = appDelegate.user;
  529. sqlite3 *db = [iSalesDB get_db];
  530. // order by w.create_time
  531. NSString* sqlQuery = [NSString stringWithFormat:@"select w.product_id,m.name||'\n'||m.description,w._id,w.qty from wishlist as w left join product as m on w.product_id=m.product_id where w.is_delete is NULL or w.is_delete != 1 %@ ",sort_str];
  532. // NSString* sqlQuery = [NSString stringWithFormat:@"select m.product_id,(m.name||'\n'||m.description) as des,w._id from product as m,wishlist as w where m.product_id = w.product_id %@",sort_str];
  533. sqlite3_stmt * statement;
  534. NSDate *date1 = [NSDate date];
  535. NSDate *date2 = nil;
  536. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  537. int count=0;
  538. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  539. {
  540. while (sqlite3_step(statement) == SQLITE_ROW)
  541. {
  542. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  543. int product_id = sqlite3_column_double(statement, 0);
  544. char *description = (char*)sqlite3_column_text(statement, 1);
  545. if(description==nil)
  546. description= "";
  547. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  548. int item_id = sqlite3_column_double(statement, 2);
  549. NSDate *date_image = [NSDate date];
  550. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  551. printf("image : ");
  552. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  553. // char *url = (char*)sqlite3_column_text(statement, 3);
  554. // if(url==nil)
  555. // url="";
  556. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  557. int qty = sqlite3_column_int(statement, 3);
  558. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  559. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  560. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  561. item[@"description"]= nsdescription;
  562. item[@"img"]= nsurl;
  563. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  564. count++;
  565. }
  566. printf("total time:");
  567. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  568. ret[@"count"]= [NSNumber numberWithInt:count];
  569. ret[@"total_count"]= [NSNumber numberWithInt:count];
  570. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  571. ret[@"result"]= [NSNumber numberWithInt:2];
  572. appDelegate.wish_count =count;
  573. [appDelegate update_count_mark];
  574. sqlite3_finalize(statement);
  575. }
  576. [iSalesDB close_db:db];
  577. return ret;
  578. }
  579. +(NSDictionary*) offline_notimpl
  580. {
  581. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  582. ret[@"result"]=@"8";
  583. ret[@"err_msg"]=@"offline mode does not support this function.";
  584. return ret;
  585. }
  586. +(NSDictionary*) offline_home
  587. {
  588. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  589. NSString *cachefolder = [paths objectAtIndex:0];
  590. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  591. NSData* json =nil;
  592. json=[NSData dataWithContentsOfFile:img_cache];
  593. NSError *error=nil;
  594. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  595. return menu;
  596. }
  597. +(NSDictionary*) offline_category_menu
  598. {
  599. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  600. NSString *cachefolder = [paths objectAtIndex:0];
  601. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  602. NSData* json =nil;
  603. json=[NSData dataWithContentsOfFile:img_cache];
  604. NSError *error=nil;
  605. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  606. return menu;
  607. }
  608. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  609. {
  610. NSString* offline_command=params[@"offline_Command"];
  611. NSDictionary* ret=nil;
  612. if([offline_command isEqualToString:@"model_NIYMAL"])
  613. {
  614. NSString* category = params[@"category"];
  615. ret = [self refresh_model_NIYMAL:category];
  616. }
  617. return ret;
  618. }
  619. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  620. {
  621. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  622. [ret setValue:@"2" forKey:@"result"];
  623. [ret setValue:@"Regular Mode" forKey:@"mode"];
  624. sqlite3* db= [iSalesDB get_db];
  625. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  626. [iSalesDB close_db:db];
  627. [ret setObject:detail1_section forKey:@"detail_1"];
  628. return ret;
  629. }
  630. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  631. {
  632. UIApplication * app = [UIApplication sharedApplication];
  633. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  634. // NSArray* arr1 = [self get_user_all_price_type];
  635. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  636. // NSSet *set1 = [NSSet setWithArray:arr1];
  637. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  638. // if(appDelegate.contact_id==nil)
  639. // set2=[set1 mutableCopy];
  640. // else
  641. // [set2 intersectsSet:set1];
  642. // NSArray *retarr = [set2 allObjects];
  643. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  644. // sqlite3 *db = [iSalesDB get_db];
  645. NSString* sqlQuery = nil;
  646. if(appDelegate.contact_id==nil)
  647. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d order by a.order_by;",item_id];
  648. else
  649. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d and b.price_name in(%@) order by a.order_by;",item_id,whereprice];
  650. sqlite3_stmt * statement;
  651. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  652. int count=0;
  653. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  654. {
  655. while (sqlite3_step(statement) == SQLITE_ROW)
  656. {
  657. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  658. char *name = (char*)sqlite3_column_text(statement, 0);
  659. if(name==nil)
  660. name="";
  661. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  662. // double price = sqlite3_column_double(statement, 1);
  663. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  664. // if(isnull==nil)
  665. // item[nsname]= @"No Price";
  666. // else
  667. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  668. char *price = (char*)sqlite3_column_text(statement, 1);
  669. if(price!=nil)
  670. {
  671. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  672. nsprice=[AESCrypt fastdecrypt:nsprice];
  673. if(nsprice.length>0)
  674. {
  675. double dp= [nsprice doubleValue];
  676. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  677. }
  678. }
  679. else
  680. {
  681. item[nsname]= @"No Price";
  682. }
  683. // item[nsname]= nsprice;
  684. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  685. count++;
  686. }
  687. ret[@"count"]= [NSNumber numberWithInt:count];
  688. sqlite3_finalize(statement);
  689. }
  690. // [iSalesDB close_db:db];
  691. return ret;
  692. }
  693. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  694. //{
  695. // NSArray* arr1 = [self get_user_all_price_type:db];
  696. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  697. //
  698. // // NSSet *set1 = [NSSet setWithArray:arr1];
  699. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  700. // // [set2 intersectsSet:set1];
  701. // //
  702. // //
  703. // // NSArray *retarr = [set2 allObjects];
  704. //
  705. // NSString* whereprice=nil;
  706. // if(contact_id==nil)
  707. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  708. // else
  709. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  710. //
  711. //
  712. // // sqlite3 *db = [iSalesDB get_db];
  713. //
  714. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  715. // sqlite3_stmt * statement;
  716. //
  717. //
  718. // NSNumber* ret = nil;
  719. // double dprice=DBL_MAX;
  720. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  721. // {
  722. //
  723. //
  724. // while (sqlite3_step(statement) == SQLITE_ROW)
  725. // {
  726. //
  727. // // double val = sqlite3_column_double(statement, 0);
  728. // char *price = (char*)sqlite3_column_text(statement, 0);
  729. // if(price!=nil)
  730. // {
  731. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  732. // nsprice=[AESCrypt fastdecrypt:nsprice];
  733. // if(nsprice.length>0)
  734. // {
  735. // double dp= [nsprice doubleValue];
  736. // if(dp<dprice)
  737. // dprice=dp;
  738. // }
  739. // }
  740. // }
  741. //
  742. //
  743. //
  744. //
  745. // sqlite3_finalize(statement);
  746. //
  747. //
  748. //
  749. //
  750. // }
  751. //
  752. // // [iSalesDB close_db:db];
  753. //
  754. // if(dprice==DBL_MAX)
  755. // ret= nil;
  756. // else
  757. // ret= [NSNumber numberWithDouble:dprice];
  758. // return ret;
  759. //}
  760. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  761. {
  762. NSArray* arr1 = [self get_user_all_price_type:db];
  763. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  764. NSString* whereprice=nil;
  765. if(contact_id==nil)
  766. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  767. else
  768. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  769. // sqlite3 *db = [iSalesDB get_db];
  770. NSString *productIdCondition = @"1 = 1";
  771. if (product_id) {
  772. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  773. }
  774. NSString *itemIdCondition = @"";
  775. if (item_id) {
  776. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  777. }
  778. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  779. sqlite3_stmt * statement;
  780. NSNumber* ret = nil;
  781. double dprice=DBL_MAX;
  782. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  783. {
  784. while (sqlite3_step(statement) == SQLITE_ROW)
  785. {
  786. // double val = sqlite3_column_double(statement, 0);
  787. char *price = (char*)sqlite3_column_text(statement, 0);
  788. if(price!=nil)
  789. {
  790. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  791. nsprice=[AESCrypt fastdecrypt:nsprice];
  792. if(nsprice.length>0)
  793. {
  794. double dp= [nsprice doubleValue];
  795. if(dp<dprice)
  796. dprice=dp;
  797. }
  798. }
  799. }
  800. sqlite3_finalize(statement);
  801. }
  802. // [iSalesDB close_db:db];
  803. if(dprice==DBL_MAX)
  804. ret= nil;
  805. else
  806. ret= [NSNumber numberWithDouble:dprice];
  807. return ret;
  808. }
  809. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  810. {
  811. NSArray* ret=nil;
  812. // sqlite3 *db = [iSalesDB get_db];
  813. // no customer assigned , use login user contact_id
  814. UIApplication * app = [UIApplication sharedApplication];
  815. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  816. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  817. sqlite3_stmt * statement;
  818. // int count=0;
  819. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  820. {
  821. if (sqlite3_step(statement) == SQLITE_ROW)
  822. {
  823. char *val = (char*)sqlite3_column_text(statement, 0);
  824. if(val==nil)
  825. val="";
  826. NSString* price = [[NSString alloc]initWithUTF8String:val];
  827. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  828. }
  829. sqlite3_finalize(statement);
  830. }
  831. // [iSalesDB close_db:db];
  832. return ret;
  833. }
  834. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  835. {
  836. // sqlite3 *db = [iSalesDB get_db];
  837. if(contact_id==nil)
  838. {
  839. // no customer assigned , use login user contact_id
  840. UIApplication * app = [UIApplication sharedApplication];
  841. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  842. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  843. sqlite3_stmt * statement;
  844. // int count=0;
  845. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  846. {
  847. if (sqlite3_step(statement) == SQLITE_ROW)
  848. {
  849. char *val = (char*)sqlite3_column_text(statement, 0);
  850. if(val==nil)
  851. val="";
  852. contact_id = [[NSString alloc]initWithUTF8String:val];
  853. }
  854. sqlite3_finalize(statement);
  855. }
  856. if(contact_id.length<=0)
  857. {
  858. // [iSalesDB close_db:db];
  859. return nil;
  860. }
  861. }
  862. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  863. sqlite3_stmt * statement;
  864. NSArray* ret=nil;
  865. // int count=0;
  866. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  867. {
  868. if (sqlite3_step(statement) == SQLITE_ROW)
  869. {
  870. char *price_type = (char*)sqlite3_column_text(statement, 0);
  871. if(price_type==nil)
  872. price_type="";
  873. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  874. if(nsprice_type.length>0)
  875. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  876. else
  877. ret=nil;
  878. }
  879. sqlite3_finalize(statement);
  880. }
  881. // [iSalesDB close_db:db];
  882. return ret;
  883. }
  884. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  885. {
  886. NSString* ret= nil;
  887. // sqlite3 *db = [iSalesDB get_db];
  888. NSString *sqlQuery = nil;
  889. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  890. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  891. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  892. if(product_id==nil && model_name)
  893. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where product_id in (select product_id from product where name='%@') order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  894. else if (product_id)
  895. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where i.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  896. sqlite3_stmt * statement;
  897. // int count=0;
  898. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  899. {
  900. if (sqlite3_step(statement) == SQLITE_ROW)
  901. {
  902. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  903. if(imgurl==nil)
  904. imgurl="";
  905. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  906. ret=nsimgurl;
  907. }
  908. sqlite3_finalize(statement);
  909. }
  910. else
  911. {
  912. [ret setValue:@"8" forKey:@"result"];
  913. }
  914. // [iSalesDB close_db:db];
  915. DebugLog(@"data string: %@",ret );
  916. return ret;
  917. }
  918. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  919. {
  920. NSString* ret= nil;
  921. sqlite3 *db = [iSalesDB get_db];
  922. NSString *sqlQuery = nil;
  923. if(product_id==nil)
  924. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  925. else
  926. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  927. sqlite3_stmt * statement;
  928. // int count=0;
  929. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  930. {
  931. if (sqlite3_step(statement) == SQLITE_ROW)
  932. {
  933. char *default_category = (char*)sqlite3_column_text(statement, 0);
  934. if(default_category==nil)
  935. default_category="";
  936. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  937. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  938. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  939. ret=nsdefault_category;
  940. }
  941. sqlite3_finalize(statement);
  942. }
  943. else
  944. {
  945. [ret setValue:@"8" forKey:@"result"];
  946. }
  947. [iSalesDB close_db:db];
  948. DebugLog(@"data string: %@",ret );
  949. return ret;
  950. }
  951. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  952. {
  953. NSString* model_name = [params valueForKey:@"product_name"];
  954. NSString* product_id = [params valueForKey:@"product_id"];
  955. NSString* category = [params valueForKey:@"category"];
  956. if(category==nil)
  957. category = [self model_default_category:product_id model_name:model_name];
  958. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  959. sqlite3 *db = [iSalesDB get_db];
  960. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  961. NSString *sqlQuery = nil;
  962. if(product_id==nil)
  963. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id from product where name='%@';",model_name]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where name='%@';
  964. else
  965. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id from product where product_id=%@;",product_id]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where product_id=%@;
  966. sqlite3_stmt * statement;
  967. [ret setValue:@"2" forKey:@"result"];
  968. [ret setValue:@"3" forKey:@"detail_section_count"];
  969. // int count=0;
  970. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  971. {
  972. if (sqlite3_step(statement) == SQLITE_ROW)
  973. {
  974. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  975. char *name = (char*)sqlite3_column_text(statement, 0);
  976. if(name==nil)
  977. name="";
  978. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  979. char *description = (char*)sqlite3_column_text(statement, 1);
  980. if(description==nil)
  981. description="";
  982. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  983. int product_id = sqlite3_column_int(statement, 2);
  984. char *color = (char*)sqlite3_column_text(statement, 3);
  985. if(color==nil)
  986. color="";
  987. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  988. //
  989. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  990. // if(legcolor==nil)
  991. // legcolor="";
  992. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  993. //
  994. //
  995. int availability = sqlite3_column_int(statement, 5);
  996. //
  997. int incoming_stock = sqlite3_column_int(statement, 6);
  998. char *demension = (char*)sqlite3_column_text(statement, 7);
  999. if(demension==nil)
  1000. demension="";
  1001. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  1002. // ,,,,,,,,,
  1003. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  1004. if(seat_height==nil)
  1005. seat_height="";
  1006. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  1007. char *material = (char*)sqlite3_column_text(statement, 9);
  1008. if(material==nil)
  1009. material="";
  1010. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  1011. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  1012. if(box_dim==nil)
  1013. box_dim="";
  1014. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  1015. char *volume = (char*)sqlite3_column_text(statement, 11);
  1016. if(volume==nil)
  1017. volume="";
  1018. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  1019. double weight = sqlite3_column_double(statement, 12);
  1020. char *model_set = (char*)sqlite3_column_text(statement, 13);
  1021. if(model_set==nil)
  1022. model_set="";
  1023. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  1024. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  1025. if(load_ability==nil)
  1026. load_ability="";
  1027. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  1028. char *default_category = (char*)sqlite3_column_text(statement, 15);
  1029. if(default_category==nil)
  1030. default_category="";
  1031. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1032. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  1033. if(fabric_content==nil)
  1034. fabric_content="";
  1035. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  1036. char *assembling = (char*)sqlite3_column_text(statement, 17);
  1037. if(assembling==nil)
  1038. assembling="";
  1039. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  1040. char *made_in = (char*)sqlite3_column_text(statement, 18);
  1041. if(made_in==nil)
  1042. made_in="";
  1043. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  1044. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  1045. if(special_remarks==nil)
  1046. special_remarks="";
  1047. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  1048. int stockUcom = sqlite3_column_double(statement, 20);
  1049. char *product_group = (char*)sqlite3_column_text(statement, 21);
  1050. if(product_group==nil)
  1051. product_group="";
  1052. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  1053. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  1054. // if(fashion_selector==nil)
  1055. // fashion_selector="";
  1056. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  1057. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  1058. if(selector_field==nil)
  1059. selector_field="";
  1060. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  1061. char *property_field = (char*)sqlite3_column_text(statement, 23);
  1062. if(property_field==nil)
  1063. property_field="";
  1064. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  1065. char *packaging = (char*)sqlite3_column_text(statement, 24);
  1066. if(packaging==nil)
  1067. packaging="";
  1068. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  1069. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1070. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  1071. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  1072. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  1073. [img_section setValue:model_s_img forKey:@"model_s_img"];
  1074. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  1075. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  1076. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  1077. NSString* Availability=nil;
  1078. if(availability>0)
  1079. Availability=[NSString stringWithFormat:@"%d",availability];
  1080. else
  1081. Availability = @"In Production";
  1082. [img_section setValue:Availability forKey:@"Availability"];
  1083. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  1084. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1085. char *eta = (char*)sqlite3_column_text(statement, 25);
  1086. if(eta==nil)
  1087. eta="";
  1088. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  1089. [img_section setValue:nseta forKey:@"ETA"];
  1090. int item_id = sqlite3_column_int(statement, 26);
  1091. NSString* Price=nil;
  1092. if(appDelegate.bLogin==false)
  1093. Price=@"Must Sign in.";
  1094. else
  1095. {
  1096. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1097. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1098. if(price==nil)
  1099. Price=@"No Price.";
  1100. else
  1101. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1102. }
  1103. [img_section setValue:Price forKey:@"price"];
  1104. [img_section setValue:nsname forKey:@"model_name"];
  1105. [img_section setValue:nsdescription forKey:@"model_descrition"];
  1106. [ret setObject:img_section forKey:@"img_section"];
  1107. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  1108. int detail0_item_count=0;
  1109. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1110. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1111. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1112. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1113. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1114. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1115. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1116. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1117. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1118. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1119. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1120. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1121. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1122. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  1123. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  1124. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  1125. {
  1126. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  1127. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1128. }
  1129. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  1130. [detail0_section setValue:@"kv" forKey:@"type"];
  1131. [detail0_section setValue:@"Product Information" forKey:@"title"];
  1132. [ret setObject:detail0_section forKey:@"detail_0"];
  1133. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1134. // [detail1_section setValue:@"detail" forKey:@"target"];
  1135. // [detail1_section setValue:@"popup" forKey:@"action"];
  1136. // [detail1_section setValue:@"content" forKey:@"type"];
  1137. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  1138. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1139. // [detail1_section setValue:@"true" forKey:@"single_row"];
  1140. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  1141. [ret setObject:detail1_section forKey:@"detail_1"];
  1142. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  1143. [detail2_section setValue:@"detail" forKey:@"target"];
  1144. [detail2_section setValue:@"popup" forKey:@"action"];
  1145. [detail2_section setValue:@"content" forKey:@"type"];
  1146. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  1147. [detail2_section setValue:@"true" forKey:@"single_row"];
  1148. [detail2_section setValue:@"local" forKey:@"data"];
  1149. [ret setObject:detail2_section forKey:@"detail_2"];
  1150. }
  1151. sqlite3_finalize(statement);
  1152. }
  1153. else
  1154. {
  1155. [ret setValue:@"8" forKey:@"result"];
  1156. }
  1157. NSLog(@"count:%d",count);
  1158. [iSalesDB close_db:db];
  1159. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1160. return ret;
  1161. }
  1162. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  1163. NSString* orderCode = [params valueForKey:@"orderCode"];
  1164. NSString* keyword = [params valueForKey:@"keyword"];
  1165. keyword=keyword.lowercaseString;
  1166. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  1167. int limit = [[params valueForKey:@"limit"] intValue];
  1168. int offset = [[params valueForKey:@"offset"] intValue];
  1169. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1170. NSString *limit_str = @"";
  1171. if (limited) {
  1172. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  1173. }
  1174. sqlite3 *db = [iSalesDB get_db];
  1175. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  1176. UIApplication * app = [UIApplication sharedApplication];
  1177. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1178. NSString *sqlQuery = nil;
  1179. if(exactMatch )
  1180. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name %@;",appDelegate.order_code, keyword,limit_str]; // select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' order by m.name limit %d offset %d ;
  1181. else
  1182. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, keyword,keyword,limit_str];// select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or
  1183. DebugLog(@"offline_search sql:%@",sqlQuery);
  1184. sqlite3_stmt * statement;
  1185. [ret setValue:@"2" forKey:@"result"];
  1186. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  1187. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  1188. // int count=0;
  1189. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1190. {
  1191. int i=0;
  1192. while (sqlite3_step(statement) == SQLITE_ROW)
  1193. {
  1194. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1195. // char *name = (char*)sqlite3_column_text(statement, 1);
  1196. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1197. char *name = (char*)sqlite3_column_text(statement, 0);
  1198. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1199. char *description = (char*)sqlite3_column_text(statement, 1);
  1200. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1201. int product_id = sqlite3_column_int(statement, 2);
  1202. // char *url = (char*)sqlite3_column_text(statement, 3);
  1203. // if(url==nil)
  1204. // url="";
  1205. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1206. int wid = sqlite3_column_int(statement, 3);
  1207. int closeout = sqlite3_column_int(statement, 4);
  1208. int cid = sqlite3_column_int(statement, 5);
  1209. int wisdelete = sqlite3_column_int(statement, 6);
  1210. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  1211. if(wid !=0 && wisdelete != 1)
  1212. [item setValue:@"true" forKey:@"wish_exists"];
  1213. else
  1214. [item setValue:@"false" forKey:@"wish_exists"];
  1215. if(closeout==0)
  1216. [item setValue:@"false" forKey:@"is_closeout"];
  1217. else
  1218. [item setValue:@"true" forKey:@"is_closeout"];
  1219. if(cid==0)
  1220. [item setValue:@"false" forKey:@"cart_exists"];
  1221. else
  1222. [item setValue:@"true" forKey:@"cart_exists"];
  1223. [item addEntriesFromDictionary:imgjson];
  1224. // [item setValue:nsurl forKey:@"img"];
  1225. [item setValue:nsname forKey:@"fash_name"];
  1226. [item setValue:nsdescription forKey:@"description"];
  1227. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1228. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1229. i++;
  1230. }
  1231. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1232. [ret setObject:items forKey:@"items"];
  1233. sqlite3_finalize(statement);
  1234. }
  1235. NSLog(@"count:%d",count);
  1236. [iSalesDB close_db:db];
  1237. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1238. return ret;
  1239. }
  1240. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  1241. {
  1242. return [self search:params limited:YES];
  1243. }
  1244. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  1245. {
  1246. UIApplication * app = [UIApplication sharedApplication];
  1247. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1248. appDelegate.disable_trigger=true;
  1249. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1250. appDelegate.disable_trigger=false;
  1251. //
  1252. // NSString* user = [params valueForKey:@"user"];
  1253. //
  1254. // NSString* password = [params valueForKey:@"password"];
  1255. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1256. ret[@"result"]=[NSNumber numberWithInt:2 ];
  1257. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  1258. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  1259. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  1260. [appDelegate update_count_mark];
  1261. appDelegate.can_show_price =false;
  1262. appDelegate.can_see_price =false;
  1263. appDelegate.can_create_portfolio =false;
  1264. appDelegate.can_create_order =false;
  1265. appDelegate.can_cancel_order =false;
  1266. appDelegate.can_set_cart_price =false;
  1267. appDelegate.can_delete_order =false;
  1268. appDelegate.can_submit_order =false;
  1269. appDelegate.can_set_tearsheet_price =false;
  1270. appDelegate.can_update_contact_info = false;
  1271. appDelegate.save_order_logout = false;
  1272. appDelegate.submit_order_logout = false;
  1273. appDelegate.alert_sold_in_quantities = false;
  1274. appDelegate.ipad_perm =nil ;
  1275. appDelegate.user_type = USER_ROLE_UNKNOWN;
  1276. appDelegate.OrderFilter= nil;
  1277. [appDelegate SetSo:nil];
  1278. [appDelegate set_main_button_panel];
  1279. // sqlite3 *db = [iSalesDB get_db];
  1280. //
  1281. //
  1282. //
  1283. //
  1284. //
  1285. // NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  1286. //
  1287. //
  1288. //
  1289. //
  1290. // DebugLog(@"offline_login sql:%@",sqlQuery);
  1291. // sqlite3_stmt * statement;
  1292. //
  1293. //
  1294. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1295. //
  1296. //
  1297. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1298. // {
  1299. //
  1300. //
  1301. // if (sqlite3_step(statement) == SQLITE_ROW)
  1302. // {
  1303. //
  1304. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1305. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1306. //
  1307. //
  1308. //
  1309. // int can_show_price = sqlite3_column_int(statement, 0);
  1310. // int can_see_price = sqlite3_column_int(statement, 1);
  1311. //
  1312. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1313. // if(contact_id==nil)
  1314. // contact_id="";
  1315. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1316. //
  1317. // int user_type = sqlite3_column_int(statement, 3);
  1318. //
  1319. // int can_cancel_order = sqlite3_column_int(statement, 4);
  1320. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  1321. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  1322. // int can_delete_order = sqlite3_column_int(statement, 7);
  1323. // int can_submit_order = sqlite3_column_int(statement, 8);
  1324. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1325. // int can_create_order = sqlite3_column_int(statement, 10);
  1326. //
  1327. //
  1328. // char *mode = (char*)sqlite3_column_text(statement, 11);
  1329. // if(mode==nil)
  1330. // mode="";
  1331. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1332. //
  1333. //
  1334. // char *username = (char*)sqlite3_column_text(statement, 12);
  1335. // if(username==nil)
  1336. // username="";
  1337. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1338. //
  1339. //
  1340. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1341. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1342. // [header setValue:nscontact_id forKey:@"contact_id"];
  1343. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1344. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1345. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1346. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1347. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1348. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1349. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1350. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1351. //
  1352. // [header setValue:nsusername forKey:@"username"];
  1353. //
  1354. //
  1355. // [ret setObject:header forKey:@"header"];
  1356. // [ret setValue:nsmode forKey:@"mode"];
  1357. //
  1358. //
  1359. // }
  1360. //
  1361. //
  1362. //
  1363. // sqlite3_finalize(statement);
  1364. // }
  1365. //
  1366. //
  1367. //
  1368. // [iSalesDB close_db:db];
  1369. //
  1370. //
  1371. //
  1372. //
  1373. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1374. return ret;
  1375. }
  1376. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  1377. {
  1378. UIApplication * app = [UIApplication sharedApplication];
  1379. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1380. appDelegate.disable_trigger=true;
  1381. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1382. appDelegate.disable_trigger=false;
  1383. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1384. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  1385. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  1386. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  1387. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  1388. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  1389. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  1390. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  1391. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  1392. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  1393. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  1394. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  1395. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  1396. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  1397. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  1398. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  1399. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  1400. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  1401. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  1402. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  1403. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  1404. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  1405. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1406. [arr_name addObject:customer_first_name];
  1407. [arr_name addObject:customer_last_name];
  1408. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1409. // default ship from
  1410. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,decrypt(phone) from offline_contact where contact_id = 'NPD';";
  1411. __block NSString *cid = @"";
  1412. __block NSString *name = @"";
  1413. __block NSString *ext = @"";
  1414. __block NSString *contact = @"";
  1415. __block NSString *email = @"";
  1416. __block NSString *fax = @"";
  1417. __block NSString *phone = @"";
  1418. sqlite3 *db = [iSalesDB get_db];
  1419. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  1420. cid = [self textAtColumn:0 statement:statment];
  1421. name = [self textAtColumn:1 statement:statment];
  1422. ext = [self textAtColumn:2 statement:statment];
  1423. contact = [self textAtColumn:3 statement:statment];
  1424. email = [self textAtColumn:4 statement:statment];
  1425. fax = [self textAtColumn:5 statement:statment];
  1426. phone = [self textAtColumn:6 statement:statment];
  1427. }];
  1428. NSString* so_id = [self get_offline_soid:db];
  1429. if(so_id==nil)
  1430. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  1431. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email];
  1432. int result =[iSalesDB execSql:sql_neworder db:db];
  1433. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  1434. //insert into offline_order(so_id,status,contact_id,sales_rep,create_by,customer_contact) select 'OFFLINE'||hex(randomblob(16)),-1,contact_id,sales_rep,company_name from offline_contact where contact_id='99FURO0002'
  1435. //soId
  1436. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  1437. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  1438. sqlite3_stmt * statement;
  1439. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1440. {
  1441. if (sqlite3_step(statement) == SQLITE_ROW)
  1442. {
  1443. // char *name = (char*)sqlite3_column_text(statement, 1);
  1444. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1445. //ret = sqlite3_column_int(statement, 0);
  1446. char *soId = (char*)sqlite3_column_text(statement, 0);
  1447. if(soId==nil)
  1448. soId="";
  1449. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  1450. [ret setValue:nssoId forKey:@"soId"];
  1451. [ret setValue:nssoId forKey:@"orderCode"];
  1452. }
  1453. sqlite3_finalize(statement);
  1454. }
  1455. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  1456. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1457. [iSalesDB close_db:db];
  1458. return [RAUtils dict2data:ret];
  1459. }
  1460. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  1461. {
  1462. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1463. NSString* orderCode = [params valueForKey:@"orderCode"];
  1464. UIApplication * app = [UIApplication sharedApplication];
  1465. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1466. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  1467. {
  1468. appDelegate.disable_trigger=true;
  1469. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1470. appDelegate.disable_trigger=false;
  1471. }
  1472. sqlite3 *db = [iSalesDB get_db];
  1473. int cart_count=[self query_ordercartcount:orderCode db:db];
  1474. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1475. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1476. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1477. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1478. [iSalesDB close_db:db];
  1479. return [RAUtils dict2data:ret];
  1480. }
  1481. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  1482. {
  1483. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1484. sqlite3 *db = [iSalesDB get_db];
  1485. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  1486. int count =0;
  1487. if(params[@"count"]!=nil)
  1488. {
  1489. count = [params[@"count"] intValue];
  1490. }
  1491. NSString* product_id=params[@"product_id"];
  1492. NSString* orderCode=params[@"orderCode"];
  1493. NSString *qty = params[@"qty"];
  1494. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  1495. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  1496. for(int i=0;i<arr_id.count;i++)
  1497. {
  1498. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  1499. int item_qty= count;
  1500. if (qty) {
  1501. item_qty = [qty_arr[i] integerValue];
  1502. }
  1503. if(item_qty==0)
  1504. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  1505. NSString* sql=nil;
  1506. sqlite3_stmt *stmt;
  1507. if(_id<0)
  1508. {
  1509. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  1510. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  1511. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1512. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  1513. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  1514. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1515. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  1516. }
  1517. else
  1518. {
  1519. if (qty) {
  1520. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  1521. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1522. sqlite3_bind_int(stmt, 1, _id);
  1523. } else {
  1524. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  1525. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1526. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1527. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  1528. }
  1529. }
  1530. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  1531. [iSalesDB execSql:@"ROLLBACK" db:db];
  1532. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  1533. [iSalesDB close_db:db];
  1534. DebugLog(@"add to cart error");
  1535. return [RAUtils dict2data:ret];
  1536. }
  1537. }
  1538. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1539. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1540. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1541. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1542. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1543. [iSalesDB execSql:@"END TRANSACTION" db:db];
  1544. [iSalesDB close_db:db];
  1545. return [RAUtils dict2data:ret];
  1546. }
  1547. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  1548. {
  1549. UIApplication * app = [UIApplication sharedApplication];
  1550. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1551. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1552. NSString *sqlQuery = [NSString stringWithFormat:@"select b.item_id,b.qty,c.name,c.description,b.unit_price,b.use_unitprice from (select item_id,qty,decrypt(unit_price) as unit_price,use_unitprice from bundle where product_item_id=%d) b left join catalog c on b.item_id=c.item_id;",item_id];
  1553. sqlite3_stmt * statement;
  1554. int count=0;
  1555. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1556. {
  1557. while (sqlite3_step(statement) == SQLITE_ROW)
  1558. {
  1559. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  1560. int bitem_id = sqlite3_column_int(statement, 0);
  1561. int bitem_qty = sqlite3_column_int(statement, 1);
  1562. char *name = (char*)sqlite3_column_text(statement, 2);
  1563. if(name==nil)
  1564. name="";
  1565. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  1566. char *description = (char*)sqlite3_column_text(statement, 3);
  1567. if(description==nil)
  1568. description="";
  1569. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  1570. double unit_price = sqlite3_column_double(statement, 4);
  1571. int use_unitprice = sqlite3_column_int(statement, 5);
  1572. if(use_unitprice!=1)
  1573. {
  1574. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  1575. }
  1576. itemjson[@"model"]=nsname;
  1577. itemjson[@"description"]=nsdescription;
  1578. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  1579. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  1580. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  1581. if(compute)
  1582. {
  1583. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  1584. }
  1585. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1586. count++;
  1587. }
  1588. sqlite3_finalize(statement);
  1589. }
  1590. ret[@"count"]=@(count);
  1591. if(count==0)
  1592. return nil;
  1593. else
  1594. return ret;
  1595. }
  1596. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  1597. {
  1598. //compute: add part to subtotal;
  1599. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  1600. dict_item[@(item_id)]=@"1";
  1601. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1602. double cuft=0;
  1603. double weight=0;
  1604. int carton=0;
  1605. NSString *sqlQuery = [NSString stringWithFormat:@"select ulength ,uwidth ,uheight ,uweight ,mlength ,mwidth ,mheight ,mweigth ,ilength ,iwidth ,iheight , iweight ,pcs ,mpack ,ipack,ucbf,icbf,mcbf from catalog where item_id=%d;",item_id];
  1606. sqlite3_stmt * statement;
  1607. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1608. {
  1609. if (sqlite3_step(statement) == SQLITE_ROW)
  1610. {
  1611. double ulength = sqlite3_column_double(statement, 0);
  1612. double uwidth = sqlite3_column_double(statement, 1);
  1613. double uheight = sqlite3_column_double(statement, 2);
  1614. double uweight = sqlite3_column_double(statement, 3);
  1615. double mlength = sqlite3_column_double(statement, 4);
  1616. double mwidth = sqlite3_column_double(statement, 5);
  1617. double mheight = sqlite3_column_double(statement, 6);
  1618. double mweight = sqlite3_column_double(statement, 7);
  1619. double ilength = sqlite3_column_double(statement, 8);
  1620. double iwidth = sqlite3_column_double(statement, 9);
  1621. double iheight = sqlite3_column_double(statement, 10);
  1622. double iweight = sqlite3_column_double(statement, 11);
  1623. int pcs = sqlite3_column_int(statement,12);
  1624. int mpack = sqlite3_column_int(statement, 13);
  1625. int ipack = sqlite3_column_int(statement, 14);
  1626. double ucbf = sqlite3_column_double(statement, 15);
  1627. double icbf = sqlite3_column_double(statement, 16);
  1628. double mcbf = sqlite3_column_double(statement, 17);
  1629. if(ipack==0)
  1630. {
  1631. carton= count/mpack ;
  1632. weight = mweight*carton;
  1633. cuft= carton*(mlength*mwidth*mheight);
  1634. int remain=count%mpack;
  1635. if(remain==0)
  1636. {
  1637. //do nothing;
  1638. }
  1639. else
  1640. {
  1641. carton++;
  1642. weight += uweight*remain;
  1643. cuft += (ulength*uwidth*uheight)*remain;
  1644. }
  1645. }
  1646. else
  1647. {
  1648. carton = count/(mpack*ipack);
  1649. weight = mweight*carton;
  1650. cuft= carton*(mlength*mwidth*mheight);
  1651. int remain=count%(mpack*ipack);
  1652. if(remain==0)
  1653. {
  1654. // do nothing;
  1655. }
  1656. else
  1657. {
  1658. carton++;
  1659. int icarton =remain/ipack;
  1660. int iremain=remain%ipack;
  1661. weight += iweight*icarton;
  1662. cuft += (ilength*iwidth*iheight)*icarton;
  1663. if(iremain==0)
  1664. {
  1665. //do nothing;
  1666. }
  1667. else
  1668. {
  1669. weight += uweight*iremain;
  1670. cuft += (ulength*uwidth*uheight)*iremain;
  1671. }
  1672. }
  1673. }
  1674. #ifdef BUILD_NPD
  1675. cuft=ucbf*count;
  1676. weight= uweight*count;
  1677. #endif
  1678. }
  1679. sqlite3_finalize(statement);
  1680. }
  1681. if(compute)
  1682. {
  1683. NSArray * arr_count=nil;
  1684. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  1685. for(int i=0;i<arr_bundle.count;i++)
  1686. {
  1687. dict_item[arr_bundle[i]]=@"1";
  1688. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  1689. cuft+=[bundlejson[@"cuft"] doubleValue];
  1690. weight+=[bundlejson[@"weight"] doubleValue];
  1691. carton+=[bundlejson[@"carton"] intValue];
  1692. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  1693. }
  1694. }
  1695. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  1696. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  1697. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  1698. ret[@"items"]=dict_item;
  1699. return ret;
  1700. }
  1701. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  1702. {
  1703. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  1704. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  1705. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1706. DebugLog(@"offline_login sql:%@",sqlQuery);
  1707. sqlite3_stmt * statement;
  1708. int cart_count=0;
  1709. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1710. if ( dbresult== SQLITE_OK)
  1711. {
  1712. while (sqlite3_step(statement) == SQLITE_ROW)
  1713. {
  1714. int item_id = sqlite3_column_int(statement, 0);
  1715. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  1716. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  1717. }
  1718. sqlite3_finalize(statement);
  1719. }
  1720. return cart_count;
  1721. }
  1722. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  1723. {
  1724. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1725. sqlite3 *db = [iSalesDB get_db];
  1726. UIApplication * app = [UIApplication sharedApplication];
  1727. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1728. NSString* orderCode=params[@"orderCode"];
  1729. int sort = [[params objectForKey:@"sort"] intValue];
  1730. NSString *sort_str = @"";
  1731. switch (sort) {
  1732. case 0:{
  1733. sort_str = @"order by c.modify_time desc";
  1734. }
  1735. break;
  1736. case 1:{
  1737. sort_str = @"order by c.modify_time asc";
  1738. }
  1739. break;
  1740. case 2:{
  1741. sort_str = @"order by m.name asc";
  1742. }
  1743. break;
  1744. case 3:{
  1745. sort_str = @"order by m.name desc";
  1746. }
  1747. break;
  1748. case 4:{
  1749. sort_str = @"order by m.description asc";
  1750. }
  1751. break;
  1752. default:
  1753. break;
  1754. }
  1755. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id %@",orderCode,sort_str ];
  1756. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  1757. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1758. DebugLog(@"offline_login sql:%@",sqlQuery);
  1759. sqlite3_stmt * statement;
  1760. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1761. NSDate *date1 = [NSDate date];
  1762. int count=0;
  1763. int cart_count=0;
  1764. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1765. if ( dbresult== SQLITE_OK)
  1766. {
  1767. while (sqlite3_step(statement) == SQLITE_ROW)
  1768. {
  1769. NSDate *row_date = [NSDate date];
  1770. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1771. int product_id = sqlite3_column_int(statement, 0);
  1772. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1773. int item_id = sqlite3_column_int(statement, 7);
  1774. NSString* Price=nil;
  1775. if(str_price==nil)
  1776. {
  1777. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1778. // NSDate *price_date = [NSDate date];
  1779. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1780. // DebugLog(@"price time interval");
  1781. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  1782. if(price==nil)
  1783. Price=@"No Price.";
  1784. else
  1785. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1786. }
  1787. else
  1788. {
  1789. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1790. }
  1791. double discount = sqlite3_column_double(statement, 2);
  1792. int item_count = sqlite3_column_int(statement, 3);
  1793. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1794. NSString *nsline_note=nil;
  1795. if(line_note!=nil)
  1796. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1797. char *name = (char*)sqlite3_column_text(statement, 5);
  1798. NSString *nsname=nil;
  1799. if(name!=nil)
  1800. nsname= [[NSString alloc]initWithUTF8String:name];
  1801. char *description = (char*)sqlite3_column_text(statement, 6);
  1802. NSString *nsdescription=nil;
  1803. if(description!=nil)
  1804. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1805. int stockUom = sqlite3_column_int(statement, 8);
  1806. int _id = sqlite3_column_int(statement, 9);
  1807. // NSDate *subtotal_date = [NSDate date];
  1808. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1809. // DebugLog(@"subtotal_date time interval");
  1810. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  1811. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1812. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1813. int carton=[bsubtotaljson[@"carton"] intValue];
  1814. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  1815. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1816. // NSDate *img_date = [NSDate date];
  1817. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1818. // DebugLog(@"img_date time interval");
  1819. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  1820. itemjson[@"model"]=nsname;
  1821. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1822. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1823. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1824. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1825. itemjson[@"check"]=@"true";
  1826. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1827. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1828. itemjson[@"unit_price"]=Price;
  1829. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1830. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1831. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1832. itemjson[@"note"]=nsline_note;
  1833. // NSDate *date2 = [NSDate date];
  1834. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  1835. // DebugLog(@"model_bundle time interval");
  1836. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  1837. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1838. count++;
  1839. // DebugLog(@"row time interval");
  1840. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  1841. }
  1842. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1843. sqlite3_finalize(statement);
  1844. }
  1845. DebugLog(@"request cart total time interval");
  1846. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1847. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1848. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1849. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1850. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1851. ret[@"count"]=[NSNumber numberWithInt:count ];
  1852. ret[@"mode"]=@"Regular Mode";
  1853. [iSalesDB close_db:db];
  1854. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  1855. DebugLog(@"general notes :%@",general_note);
  1856. ret[@"general_note"]= general_note;
  1857. return [RAUtils dict2data:ret];
  1858. }
  1859. +(NSData*) offline_login :(NSMutableDictionary *) params
  1860. {
  1861. NSString* user = [params valueForKey:@"user"];
  1862. NSString* password = [params valueForKey:@"password"];
  1863. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1864. sqlite3 *db = [iSalesDB get_db];
  1865. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  1866. DebugLog(@"offline_login sql:%@",sqlQuery);
  1867. sqlite3_stmt * statement;
  1868. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1869. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1870. {
  1871. if (sqlite3_step(statement) == SQLITE_ROW)
  1872. {
  1873. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1874. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1875. int can_show_price = sqlite3_column_int(statement, 0);
  1876. int can_see_price = sqlite3_column_int(statement, 1);
  1877. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1878. if(contact_id==nil)
  1879. contact_id="";
  1880. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1881. int user_type = sqlite3_column_int(statement, 3);
  1882. int can_cancel_order = sqlite3_column_int(statement, 4);
  1883. int can_set_cart_price = sqlite3_column_int(statement, 5);
  1884. int can_create_portfolio = sqlite3_column_int(statement, 6);
  1885. int can_delete_order = sqlite3_column_int(statement, 7);
  1886. int can_submit_order = sqlite3_column_int(statement, 8);
  1887. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1888. int can_create_order = sqlite3_column_int(statement, 10);
  1889. char *mode = (char*)sqlite3_column_text(statement, 11);
  1890. if(mode==nil)
  1891. mode="";
  1892. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1893. char *username = (char*)sqlite3_column_text(statement, 12);
  1894. if(username==nil)
  1895. username="";
  1896. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1897. int can_update_contact_info = sqlite3_column_int(statement, 13);
  1898. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1899. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1900. [header setValue:nscontact_id forKey:@"contact_id"];
  1901. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1902. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1903. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1904. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1905. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1906. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1907. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1908. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1909. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  1910. [header setValue:nsusername forKey:@"username"];
  1911. NSError* error=nil;
  1912. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  1913. [header setValue:statusFilter forKey:@"statusFilter"];
  1914. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1915. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  1916. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  1917. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  1918. [ret setObject:header forKey:@"header"];
  1919. [ret setValue:nsmode forKey:@"mode"];
  1920. }
  1921. sqlite3_finalize(statement);
  1922. }
  1923. [iSalesDB close_db:db];
  1924. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1925. return [RAUtils dict2data:ret];
  1926. }
  1927. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  1928. {
  1929. NSString* contactId = [params valueForKey:@"contactId"];
  1930. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1931. sqlite3 *db = [iSalesDB get_db];
  1932. NSString *sqlQuery = nil;
  1933. {
  1934. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email,img_0,img_1,img_2,price_type,notes,sales_rep from offline_contact where contact_id='%@'",contactId];
  1935. }
  1936. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  1937. sqlite3_stmt * statement;
  1938. [ret setValue:@"2" forKey:@"result"];
  1939. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1940. {
  1941. //int i = 0;
  1942. if (sqlite3_step(statement) == SQLITE_ROW)
  1943. {
  1944. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1945. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1946. // int editable = sqlite3_column_int(statement, 0);
  1947. char *company_name = (char*)sqlite3_column_text(statement, 1);
  1948. NSString *nscompany_name =nil;
  1949. if(company_name==nil)
  1950. nscompany_name=@"";
  1951. else
  1952. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  1953. char *country = (char*)sqlite3_column_text(statement, 2);
  1954. if(country==nil)
  1955. country="";
  1956. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  1957. // char *addr = (char*)sqlite3_column_text(statement, 3);
  1958. // if(addr==nil)
  1959. // addr="";
  1960. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  1961. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  1962. if(zipcode==nil)
  1963. zipcode="";
  1964. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  1965. char *state = (char*)sqlite3_column_text(statement, 5);
  1966. if(state==nil)
  1967. state="";
  1968. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  1969. char *city = (char*)sqlite3_column_text(statement, 6);
  1970. if(city==nil)
  1971. city="";
  1972. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  1973. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  1974. // NSString *nscontact_name = nil;
  1975. // if(contact_name==nil)
  1976. // nscontact_name=@"";
  1977. // else
  1978. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  1979. char *phone = (char*)sqlite3_column_text(statement, 8);
  1980. NSString *nsphone = nil;
  1981. if(phone==nil)
  1982. nsphone=@"";
  1983. else
  1984. nsphone= [[NSString alloc]initWithUTF8String:phone];
  1985. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  1986. if(contact_id==nil)
  1987. contact_id="";
  1988. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1989. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1990. if(addr_1==nil)
  1991. addr_1="";
  1992. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1993. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1994. if(addr_2==nil)
  1995. addr_2="";
  1996. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1997. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1998. if(addr_3==nil)
  1999. addr_3="";
  2000. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  2001. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  2002. if(addr_4==nil)
  2003. addr_4="";
  2004. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  2005. char *first_name = (char*)sqlite3_column_text(statement, 14);
  2006. if(first_name==nil)
  2007. first_name="";
  2008. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  2009. char *last_name = (char*)sqlite3_column_text(statement, 15);
  2010. if(last_name==nil)
  2011. last_name="";
  2012. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  2013. char *fax = (char*)sqlite3_column_text(statement, 16);
  2014. NSString *nsfax = nil;
  2015. if(fax==nil)
  2016. nsfax=@"";
  2017. else
  2018. nsfax= [[NSString alloc]initWithUTF8String:fax];
  2019. char *email = (char*)sqlite3_column_text(statement, 17);
  2020. NSString *nsemail = nil;
  2021. if(email==nil)
  2022. nsemail=@"";
  2023. else
  2024. nsemail= [[NSString alloc]initWithUTF8String:email];
  2025. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  2026. NSString *nsimg_0 = nil;
  2027. if(img_0==nil)
  2028. nsimg_0=@"";
  2029. else
  2030. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2031. [self copy_bcardImg:nsimg_0];
  2032. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  2033. NSString *nsimg_1 = nil;
  2034. if(img_1==nil)
  2035. nsimg_1=@"";
  2036. else
  2037. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2038. [self copy_bcardImg:nsimg_1];
  2039. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  2040. NSString *nsimg_2 = nil;
  2041. if(img_2==nil)
  2042. nsimg_2=@"";
  2043. else
  2044. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2045. [self copy_bcardImg:nsimg_2];
  2046. char *price_type = (char*)sqlite3_column_text(statement, 21);
  2047. NSString *nsprice_type = nil;
  2048. if(price_type==nil)
  2049. nsprice_type=@"";
  2050. else
  2051. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  2052. char *notes = (char*)sqlite3_column_text(statement, 22);
  2053. NSString *nsnotes = nil;
  2054. if(notes==nil)
  2055. nsnotes=@"";
  2056. else
  2057. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  2058. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  2059. NSString *nssalesrep = nil;
  2060. if(salesrep==nil)
  2061. nssalesrep=@"";
  2062. else
  2063. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  2064. {
  2065. // decrypt
  2066. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2067. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2068. nsphone=[AESCrypt fastdecrypt:nsphone];
  2069. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2070. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2071. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2072. }
  2073. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2074. [arr_name addObject:nsfirst_name];
  2075. [arr_name addObject:nslast_name];
  2076. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2077. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  2078. [arr_ext addObject:nsaddr_1];
  2079. [arr_ext addObject:nsaddr_2];
  2080. [arr_ext addObject:nsaddr_3];
  2081. [arr_ext addObject:nsaddr_4];
  2082. [arr_ext addObject:@"\r\n"];
  2083. [arr_ext addObject:nscity];
  2084. [arr_ext addObject:nsstate];
  2085. [arr_ext addObject:nszipcode];
  2086. [arr_ext addObject:nscountry];
  2087. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  2088. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  2089. [item setValue:nsimg_2 forKey:@"business_card_2"];
  2090. [item setValue:nsimg_0 forKey:@"business_card_0"];
  2091. [item setValue:nscountry forKey:@"customer_country"];
  2092. [item setValue:nsphone forKey:@"customer_phone"];
  2093. [item setValue:nsimg_1 forKey:@"business_card_1"];
  2094. [item setValue:nscompany_name forKey:@"customer_name"];
  2095. [item setValue:nsprice_type forKey:@"customer_price_type"];
  2096. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  2097. [item setValue:nsext forKey:@"customer_contact_ext"];
  2098. [item setValue:nszipcode forKey:@"customer_zipcode"];
  2099. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  2100. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  2101. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  2102. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  2103. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  2104. [item setValue:nslast_name forKey:@"customer_last_name"];
  2105. [item setValue:nscity forKey:@"customer_city"];
  2106. [item setValue:nsstate forKey:@"customer_state"];
  2107. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  2108. [item setValue:contactId forKey:@"customer_cid"];
  2109. [item setValue:nscontact_name forKey:@"customer_contact"];
  2110. [item setValue:nsfax forKey:@"customer_fax"];
  2111. [item setValue:nsemail forKey:@"customer_email"];
  2112. [ret setObject:item forKey:@"customerInfo"];
  2113. // i++;
  2114. }
  2115. UIApplication * app = [UIApplication sharedApplication];
  2116. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2117. [ret setValue:appDelegate.mode forKey:@"mode"];
  2118. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  2119. sqlite3_finalize(statement);
  2120. }
  2121. [iSalesDB close_db:db];
  2122. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2123. return ret;
  2124. }
  2125. + (bool) copy_bcardImg:(NSString*) filename
  2126. {
  2127. if(filename.length==0)
  2128. return false;
  2129. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2130. bool ret=false;
  2131. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2132. NSString *cachefolder = [paths objectAtIndex:0];
  2133. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2134. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2135. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2136. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2137. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  2138. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2139. //
  2140. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2141. NSFileManager* fileManager = [NSFileManager defaultManager];
  2142. BOOL bdir=NO;
  2143. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2144. {
  2145. NSError *error = nil;
  2146. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2147. {
  2148. ret=false;
  2149. }
  2150. else
  2151. {
  2152. ret=true;
  2153. }
  2154. // NSError *error = nil;
  2155. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2156. //
  2157. // if(!bsuccess)
  2158. // {
  2159. // DebugLog(@"Create offline_createimg folder failed");
  2160. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2161. // return [RAUtils dict2data:ret];
  2162. // }
  2163. // if(bsuccess)
  2164. // {
  2165. // sqlite3 *db = [self get_db];
  2166. //
  2167. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2168. // [iSalesDB close_db:db];
  2169. // }
  2170. }
  2171. return ret;
  2172. //
  2173. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2174. // if(bsuccess)
  2175. // {
  2176. // NSError *error = nil;
  2177. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2178. // {
  2179. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2180. // }
  2181. // else
  2182. // {
  2183. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2184. //
  2185. // ret[@"img_url_aname"]=filename;
  2186. // ret[@"img_url"]=savedImagePath;
  2187. // }
  2188. // }
  2189. }
  2190. +(NSData *) offline_saveBusinesscard:(NSData *) image
  2191. {
  2192. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2193. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2194. NSString *cachefolder = [paths objectAtIndex:0];
  2195. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2196. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2197. NSFileManager* fileManager = [NSFileManager defaultManager];
  2198. BOOL bdir=YES;
  2199. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  2200. {
  2201. NSError *error = nil;
  2202. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2203. if(!bsuccess)
  2204. {
  2205. DebugLog(@"Create offline_createimg folder failed");
  2206. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2207. return [RAUtils dict2data:ret];
  2208. }
  2209. // if(bsuccess)
  2210. // {
  2211. // sqlite3 *db = [self get_db];
  2212. //
  2213. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2214. // [iSalesDB close_db:db];
  2215. // }
  2216. }
  2217. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  2218. //JEPG格式
  2219. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  2220. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  2221. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2222. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2223. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2224. if(bsuccess)
  2225. {
  2226. NSError *error = nil;
  2227. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2228. {
  2229. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2230. }
  2231. else
  2232. {
  2233. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2234. ret[@"img_url_aname"]=filename;
  2235. ret[@"img_url"]=filename;
  2236. }
  2237. }
  2238. else
  2239. {
  2240. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2241. }
  2242. return [RAUtils dict2data:ret];
  2243. }
  2244. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  2245. {
  2246. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  2247. category = [category substringToIndex:3];
  2248. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2249. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  2250. params[@"category"]= category;
  2251. ret[@"params"]= params;
  2252. [ret setValue:@"detail" forKey:@"target"];
  2253. [ret setValue:@"popup" forKey:@"action"];
  2254. [ret setValue:@"content" forKey:@"type"];
  2255. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  2256. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2257. [ret setValue:@"true" forKey:@"single_row"];
  2258. [ret setValue:@"true" forKey:@"partial_refresh"];
  2259. // sqlite3 *db = [iSalesDB get_db];
  2260. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2261. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.product_id,i.url,random() as aa from product m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;",category,OFFLINE_IMG_TYPE_CATEGORY]; // select m.name,m.product_id,i.url,random() as aa from model m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;
  2262. sqlite3_stmt * statement;
  2263. int count = 0;
  2264. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  2265. // int count=0;
  2266. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2267. {
  2268. int i=0;
  2269. while (sqlite3_step(statement) == SQLITE_ROW)
  2270. {
  2271. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2272. // char *name = (char*)sqlite3_column_text(statement, 1);
  2273. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2274. char *name = (char*)sqlite3_column_text(statement, 0);
  2275. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2276. int product_id = sqlite3_column_int(statement, 1);
  2277. char *url = (char*)sqlite3_column_text(statement, 2);
  2278. if(url==nil)
  2279. url="";
  2280. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2281. [item setValue:nsurl forKey:@"picture_path"];
  2282. [item setValue:nsname forKey:@"fash_name"];
  2283. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2284. [item setValue:category forKey:@"category"];
  2285. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2286. i++;
  2287. }
  2288. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2289. sqlite3_finalize(statement);
  2290. }
  2291. NSLog(@"count:%d",count);
  2292. // [iSalesDB close_db:db];
  2293. return ret;
  2294. }
  2295. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  2296. {
  2297. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2298. [ret setValue:key forKey:@"key"];
  2299. [ret setValue:value forKey:@"val"];
  2300. return ret;
  2301. }
  2302. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  2303. {
  2304. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2305. [ret setValue:@"0" forKey:@"img_count"];
  2306. // sqlite3 *db = [iSalesDB get_db];
  2307. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from product where product_id=%d ;",field,product_id]; // select %@,property_display from model where product_id=%d ;
  2308. sqlite3_stmt * statement;
  2309. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2310. {
  2311. int i=0;
  2312. if (sqlite3_step(statement) == SQLITE_ROW)
  2313. {
  2314. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2315. // char *name = (char*)sqlite3_column_text(statement, 1);
  2316. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2317. char *value = (char*)sqlite3_column_text(statement, 0);
  2318. if(value==nil)
  2319. value="";
  2320. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2321. char *key = (char*)sqlite3_column_text(statement, 1);
  2322. if(key==nil)
  2323. key="";
  2324. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  2325. [item setValue:nsvalue forKey:@"val"];
  2326. [item setValue:nskey forKey:@"key"];
  2327. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2328. [ret setValue:@"1" forKey:@"count"];
  2329. i++;
  2330. }
  2331. sqlite3_finalize(statement);
  2332. }
  2333. // [iSalesDB close_db:db];
  2334. return ret;
  2335. }
  2336. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  2337. {
  2338. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2339. [ret setValue:@"0" forKey:@"count"];
  2340. // sqlite3 *db = [iSalesDB get_db];
  2341. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from product where product_group='%@' order by name;",field,product_group]; // select %@,selector_display,product_id,category from model where product_group='%@' order by name;
  2342. sqlite3_stmt * statement;
  2343. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2344. {
  2345. int i=0;
  2346. while (sqlite3_step(statement) == SQLITE_ROW)
  2347. {
  2348. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2349. // char *name = (char*)sqlite3_column_text(statement, 1);
  2350. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2351. char *value = (char*)sqlite3_column_text(statement, 0);
  2352. if(value==nil)
  2353. value="";
  2354. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2355. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  2356. if(selector_display==nil)
  2357. selector_display="";
  2358. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  2359. int product_id = sqlite3_column_int(statement, 2);
  2360. char *category = (char*)sqlite3_column_text(statement, 3);
  2361. if(category==nil)
  2362. category="";
  2363. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  2364. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  2365. [item setValue:nsvalue forKey:@"title"];
  2366. [item setValue:url forKey:@"pic_url"];
  2367. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  2368. [params setValue:@"2" forKey:@"count"];
  2369. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  2370. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  2371. [param0 setValue:@"product_id" forKey:@"name"];
  2372. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  2373. [param1 setValue:nscategory forKey:@"val"];
  2374. [param1 setValue:@"category" forKey:@"name"];
  2375. [params setObject:param0 forKey:@"param_0"];
  2376. [params setObject:param1 forKey:@"param_1"];
  2377. [item setObject:params forKey:@"params"];
  2378. [ret setValue:nsselector_display forKey:@"name"];
  2379. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2380. i++;
  2381. }
  2382. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2383. [ret setValue:@"switch" forKey:@"action"];
  2384. sqlite3_finalize(statement);
  2385. }
  2386. // [iSalesDB close_db:db];
  2387. return ret;
  2388. }
  2389. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  2390. {
  2391. // model 在 category search 显示的图片。
  2392. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  2393. // sqlite3 *db = [iSalesDB get_db];
  2394. NSString *sqlQuery = [NSString stringWithFormat:@"select url,type from model_image where product_id=%d and type<2 order by default_img desc , _id asc limit 2;",product_id];
  2395. sqlite3_stmt * statement;
  2396. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2397. {
  2398. while (sqlite3_step(statement) == SQLITE_ROW)
  2399. {
  2400. char *url = (char*)sqlite3_column_text(statement, 0);
  2401. if(url==nil)
  2402. url="";
  2403. int type = sqlite3_column_int(statement, 1);
  2404. if(type==0)
  2405. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  2406. else
  2407. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  2408. }
  2409. sqlite3_finalize(statement);
  2410. }
  2411. // [iSalesDB close_db:db];
  2412. return ret;
  2413. }
  2414. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  2415. {
  2416. int item_id=-1;
  2417. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  2418. sqlite3_stmt * statement;
  2419. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2420. {
  2421. if (sqlite3_step(statement) == SQLITE_ROW)
  2422. {
  2423. item_id = sqlite3_column_int(statement, 0);
  2424. }
  2425. sqlite3_finalize(statement);
  2426. }
  2427. return item_id;
  2428. }
  2429. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  2430. {
  2431. // NSString* ret = @"";
  2432. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  2433. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  2434. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  2435. sqlite3_stmt * statement;
  2436. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2437. {
  2438. while (sqlite3_step(statement) == SQLITE_ROW)
  2439. {
  2440. int bitem_id = sqlite3_column_int(statement, 0);
  2441. int bitem_qty = sqlite3_column_int(statement, 1);
  2442. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  2443. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  2444. }
  2445. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  2446. sqlite3_finalize(statement);
  2447. }
  2448. // if(ret==nil)
  2449. // ret=@"";
  2450. *count=arr_count;
  2451. return arr_bundle;
  2452. }
  2453. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  2454. {
  2455. // get default sold qty, return -1 if model not found;
  2456. int ret = -1;
  2457. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  2458. sqlite3_stmt * statement;
  2459. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2460. {
  2461. if (sqlite3_step(statement) == SQLITE_ROW)
  2462. {
  2463. ret = sqlite3_column_int(statement, 0);
  2464. }
  2465. sqlite3_finalize(statement);
  2466. }
  2467. return ret;
  2468. }
  2469. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  2470. {
  2471. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2472. [ret setValue:@"0" forKey:@"img_count"];
  2473. // sqlite3 *db = [iSalesDB get_db];
  2474. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  2475. sqlite3_stmt * statement;
  2476. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2477. {
  2478. int i=0;
  2479. while (sqlite3_step(statement) == SQLITE_ROW)
  2480. {
  2481. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2482. // char *name = (char*)sqlite3_column_text(statement, 1);
  2483. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2484. char *url = (char*)sqlite3_column_text(statement, 0);
  2485. if(url==nil)
  2486. url="";
  2487. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2488. [item setValue:nsurl forKey:@"s"];
  2489. [item setValue:nsurl forKey:@"l"];
  2490. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  2491. i++;
  2492. }
  2493. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  2494. sqlite3_finalize(statement);
  2495. }
  2496. // [iSalesDB close_db:db];
  2497. return ret;
  2498. }
  2499. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  2500. UIApplication * app = [UIApplication sharedApplication];
  2501. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2502. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2503. sqlite3 *db = [iSalesDB get_db];
  2504. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2505. NSString* product_id=params[@"product_id"];
  2506. NSString *item_count_str = params[@"item_count"];
  2507. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2508. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  2509. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2510. // NSString *sql = @"";
  2511. for(int i=0;i<arr.count;i++)
  2512. {
  2513. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2514. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2515. __block int cart_count = 0;
  2516. if (!item_count_str) {
  2517. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  2518. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2519. NSString *model_set = [self textAtColumn:0 statement:stmt];
  2520. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  2521. cart_count = [[model_set_components lastObject] integerValue];
  2522. }];
  2523. }
  2524. if(count==0)
  2525. {
  2526. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2527. sqlite3_stmt *stmt;
  2528. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  2529. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  2530. if (item_count_arr) {
  2531. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  2532. } else {
  2533. sqlite3_bind_int(stmt,2,cart_count);
  2534. }
  2535. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2536. [iSalesDB execSql:@"ROLLBACK" db:db];
  2537. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2538. [iSalesDB close_db:db];
  2539. DebugLog(@"add to wishlist error");
  2540. return ret;
  2541. }
  2542. } else {
  2543. int qty = 0;
  2544. if (item_count_arr) {
  2545. qty = [item_count_arr[i] integerValue];
  2546. } else {
  2547. qty = cart_count;
  2548. }
  2549. sqlQuery = [NSString stringWithFormat:@"update wishlist set qty = 1 where product_id = %@ and (is_delete is null or is_delete != 1);update wishlist set qty = %d,is_delete = 0 where product_id = %@ and is_delete = 1;",arr[i],qty,arr[i]];
  2550. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  2551. [iSalesDB execSql:@"ROLLBACK" db:db];
  2552. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  2553. [iSalesDB close_db:db];
  2554. DebugLog(@"add to wishlist error");
  2555. return ret;
  2556. }
  2557. }
  2558. }
  2559. // [iSalesDB execSql:sql db:db];
  2560. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2561. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2562. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2563. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  2564. [iSalesDB close_db:db];
  2565. appDelegate.wish_count =count;
  2566. [appDelegate update_count_mark];
  2567. ret[@"result"]= [NSNumber numberWithInt:2];
  2568. return ret;
  2569. }
  2570. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  2571. // 0 category
  2572. // 1 search
  2573. // 2 itemsearch
  2574. NSData *ret = nil;
  2575. NSDictionary *items = nil;
  2576. switch (from) {
  2577. case 0:{
  2578. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  2579. }
  2580. break;
  2581. case 1:{
  2582. items = [[self search:params limited:NO] objectForKey:@"items"];
  2583. }
  2584. break;
  2585. case 2:{
  2586. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  2587. }
  2588. break;
  2589. default:
  2590. break;
  2591. }
  2592. if (!items) {
  2593. return ret;
  2594. }
  2595. int count = [[items objectForKey:@"count"] intValue];
  2596. NSMutableString *product_id_str = [@"" mutableCopy];
  2597. for (int i = 0; i < count; i++) {
  2598. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  2599. NSDictionary *item = [items objectForKey:key];
  2600. NSString *product_id = [item objectForKey:@"product_id"];
  2601. if (i == 0) {
  2602. [product_id_str appendString:product_id];
  2603. } else {
  2604. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  2605. }
  2606. }
  2607. NSString *add_to = [self valueInParams:params key:@"addTo"];
  2608. if ([add_to isEqualToString:@"cart"]) {
  2609. NSString *order_code = [params objectForKey:@"orderCode"];
  2610. if (order_code.length) {
  2611. NSDictionary *newParams = @{
  2612. @"product_id" : product_id_str,
  2613. @"orderCode" : order_code
  2614. };
  2615. ret = [self offline_add2cart:newParams.mutableCopy];
  2616. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2617. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2618. ret = [RAUtils dict2data:retDic];
  2619. }
  2620. } else if([add_to isEqualToString:@"wishlist"]) {
  2621. NSDictionary *newParams = @{
  2622. @"product_id" : product_id_str
  2623. };
  2624. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  2625. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2626. ret = [RAUtils dict2data:retDic];
  2627. } else if([add_to isEqualToString:@"portfolio"]) {
  2628. }
  2629. return ret;
  2630. }
  2631. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  2632. {
  2633. return [self addAll:params from:0];
  2634. }
  2635. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  2636. {
  2637. return [self addAll:params from:1];
  2638. }
  2639. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  2640. {
  2641. return [self addAll:params from:2];
  2642. }
  2643. #pragma mark - Jack
  2644. #warning 做SQL操作时转义!!
  2645. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  2646. // "val_227" : {
  2647. // "check" : 1,
  2648. // "value" : "US United States",
  2649. // "value_id" : "228"
  2650. // },
  2651. if (!countryCode) {
  2652. countryCode = @"US";
  2653. }
  2654. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  2655. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  2656. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  2657. int code_id = sqlite3_column_int(stmt, 3); // id
  2658. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2659. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  2660. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  2661. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2662. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  2663. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2664. }
  2665. long n = *count;
  2666. *count = n + 1;
  2667. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  2668. [container setValue:countryDic forKey:key];
  2669. }] mutableCopy];
  2670. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2671. return ret;
  2672. }
  2673. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  2674. countryCode = [self translateSingleQuote:countryCode];
  2675. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  2676. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2677. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  2678. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  2679. if (name == NULL) {
  2680. name = "";
  2681. }
  2682. if (code == NULL) {
  2683. code = "";
  2684. }
  2685. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2686. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2687. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  2688. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2689. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  2690. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2691. }
  2692. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  2693. [container setValue:stateDic forKey:key];
  2694. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  2695. DebugLog(@"query all state error: %@",err_msg);
  2696. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2697. // [stateDic setValue:@"Other" forKey:@"value"];
  2698. // [stateDic setValue:@"" forKey:@"value_id"];
  2699. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2700. //
  2701. // if (state_code && [@"" isEqualToString:state_code]) {
  2702. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2703. // }
  2704. //
  2705. // NSString *key = [NSString stringWithFormat:@"val_0"];
  2706. // [container setValue:stateDic forKey:key];
  2707. }] mutableCopy];
  2708. // failure 可以不用了,一样的
  2709. if (ret.allKeys.count == 0) {
  2710. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2711. [stateDic setValue:@"Other" forKey:@"value"];
  2712. [stateDic setValue:@"" forKey:@"value_id"];
  2713. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2714. if (state_code && [@"" isEqualToString:state_code]) {
  2715. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2716. }
  2717. NSString *key = [NSString stringWithFormat:@"val_0"];
  2718. [ret setValue:stateDic forKey:key];
  2719. }
  2720. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2721. return ret;
  2722. }
  2723. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  2724. codeId = [self translateSingleQuote:codeId];
  2725. NSString *sql = [NSString stringWithFormat:@"select name,code from offline_state where country_code = (select code as country_code FROM offline_country where countrycode_id = %@);",codeId];
  2726. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2727. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  2728. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  2729. if (name == NULL) {
  2730. name = "";
  2731. }
  2732. if (code == NULL) {
  2733. code = "";
  2734. }
  2735. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2736. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2737. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  2738. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2739. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  2740. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2741. }
  2742. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  2743. [container setValue:stateDic forKey:key];
  2744. }] mutableCopy];
  2745. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2746. return ret;
  2747. }
  2748. + (NSDictionary *)offline_getPrice {
  2749. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  2750. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2751. char *name = (char *) sqlite3_column_text(stmt, 1);
  2752. int type = sqlite3_column_int(stmt, 2);
  2753. int orderBy = sqlite3_column_int(stmt, 3);
  2754. if (name == NULL) {
  2755. name = "";
  2756. }
  2757. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  2758. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2759. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  2760. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  2761. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2762. if (orderBy == 0) {
  2763. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2764. }
  2765. [container setValue:priceDic forKey:key];
  2766. }] mutableCopy];
  2767. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2768. return ret;
  2769. }
  2770. + (NSDictionary *)offline_getSalesRep {
  2771. // 首先从offline_login表中取出sales_code
  2772. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2773. NSString *user = app.user;
  2774. user = [self translateSingleQuote:user];
  2775. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  2776. __block NSString *user_code = @"";
  2777. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2778. char *code = (char *)sqlite3_column_text(stmt, 0);
  2779. if (code == NULL) {
  2780. code = "";
  2781. }
  2782. user_code = [NSString stringWithUTF8String:code];
  2783. }];
  2784. // 再取所有salesRep
  2785. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  2786. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2787. // 1 name 2 code 3 salesrep_id
  2788. char *name = (char *)sqlite3_column_text(stmt, 1);
  2789. char *code = (char *)sqlite3_column_text(stmt, 2);
  2790. int salesrep_id = sqlite3_column_int(stmt, 3);
  2791. if (name == NULL) {
  2792. name = "";
  2793. }
  2794. if (code == NULL) {
  2795. code = "";
  2796. }
  2797. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  2798. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  2799. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  2800. // 比较code 相等则check
  2801. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  2802. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2803. }
  2804. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  2805. }] mutableCopy];
  2806. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2807. return ret;
  2808. }
  2809. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  2810. zipcode = [self translateSingleQuote:zipcode];
  2811. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  2812. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2813. char *country = (char *)sqlite3_column_text(stmt, 0);
  2814. char *state = (char *)sqlite3_column_text(stmt, 1);
  2815. char *city = (char *)sqlite3_column_text(stmt, 2);
  2816. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  2817. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  2818. if (country == NULL) {
  2819. country = "";
  2820. }
  2821. if (state == NULL) {
  2822. state = "";
  2823. }
  2824. if (city == NULL) {
  2825. city = "";
  2826. }
  2827. if (country_code == NULL) {
  2828. country_code = "";
  2829. }
  2830. if (state_code == NULL) {
  2831. state_code = "";
  2832. }
  2833. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  2834. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  2835. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  2836. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  2837. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  2838. }] mutableCopy];
  2839. return ret;
  2840. }
  2841. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  2842. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  2843. [item setValue:value forKey:valueKey];
  2844. [dic setValue:item forKey:itemKey];
  2845. }
  2846. + (NSString *)countryCodeByid:(NSString *)code_id {
  2847. NSString *ret = nil;
  2848. code_id = [self translateSingleQuote:code_id];
  2849. sqlite3 *db = [iSalesDB get_db];
  2850. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  2851. sqlite3_stmt * statement;
  2852. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2853. while (sqlite3_step(statement) == SQLITE_ROW) {
  2854. char *code = (char *)sqlite3_column_text(statement, 0);
  2855. if (code == NULL) {
  2856. code = "";
  2857. }
  2858. ret = [NSString stringWithUTF8String:code];
  2859. }
  2860. sqlite3_finalize(statement);
  2861. }
  2862. [iSalesDB close_db:db];
  2863. return ret;
  2864. }
  2865. + (NSString *)countryCodeIdByCode:(NSString *)code {
  2866. NSString *ret = nil;
  2867. code = [self translateSingleQuote:code];
  2868. sqlite3 *db = [iSalesDB get_db];
  2869. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  2870. sqlite3_stmt * statement;
  2871. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2872. while (sqlite3_step(statement) == SQLITE_ROW) {
  2873. char *_id = (char *)sqlite3_column_text(statement, 0);
  2874. if (_id == NULL) {
  2875. _id = "";
  2876. }
  2877. ret = [NSString stringWithFormat:@"%s",_id];
  2878. }
  2879. sqlite3_finalize(statement);
  2880. }
  2881. [iSalesDB close_db:db];
  2882. return ret;
  2883. }
  2884. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  2885. NSString *name = nil;
  2886. codeId = [self translateSingleQuote:codeId];
  2887. sqlite3 *db = [iSalesDB get_db];
  2888. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  2889. sqlite3_stmt * statement;
  2890. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2891. while (sqlite3_step(statement) == SQLITE_ROW) {
  2892. char *value = (char *)sqlite3_column_text(statement, 0);
  2893. if (value == NULL) {
  2894. value = "";
  2895. }
  2896. name = [NSString stringWithUTF8String:value];
  2897. }
  2898. sqlite3_finalize(statement);
  2899. }
  2900. [iSalesDB close_db:db];
  2901. return name;
  2902. }
  2903. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  2904. NSString *ret = nil;
  2905. sqlite3 *db = [iSalesDB get_db];
  2906. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  2907. sqlite3_stmt * statement;
  2908. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2909. while (sqlite3_step(statement) == SQLITE_ROW) {
  2910. char *name = (char *)sqlite3_column_text(statement, 0);
  2911. if (name == NULL) {
  2912. name = "";
  2913. }
  2914. ret = [NSString stringWithUTF8String:name];
  2915. }
  2916. sqlite3_finalize(statement);
  2917. }
  2918. [iSalesDB close_db:db];
  2919. return ret;
  2920. }
  2921. + (NSString *)salesRepCodeById:(NSString *)_id {
  2922. NSString *ret = nil;
  2923. _id = [self translateSingleQuote:_id];
  2924. sqlite3 *db = [iSalesDB get_db];
  2925. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  2926. sqlite3_stmt * statement;
  2927. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2928. while (sqlite3_step(statement) == SQLITE_ROW) {
  2929. char *rep = (char *)sqlite3_column_text(statement, 0);
  2930. if (rep == NULL) {
  2931. rep = "";
  2932. }
  2933. ret = [NSString stringWithUTF8String:rep];
  2934. }
  2935. sqlite3_finalize(statement);
  2936. }
  2937. [iSalesDB close_db:db];
  2938. return ret;
  2939. }
  2940. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  2941. char *tx = (char *)sqlite3_column_text(stmt, col);
  2942. if (tx == NULL) {
  2943. tx = "";
  2944. }
  2945. NSString *text = [NSString stringWithFormat:@"%s",tx];
  2946. if (!text) {
  2947. text = @"";
  2948. }
  2949. // 将字符全部为' '的字符串干掉
  2950. int spaceCount = 0;
  2951. for (int i = 0; i < text.length; i++) {
  2952. if ([text characterAtIndex:i] == ' ') {
  2953. spaceCount++;
  2954. }
  2955. }
  2956. if (spaceCount == text.length) {
  2957. text = @"";
  2958. }
  2959. return text;
  2960. }
  2961. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  2962. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  2963. NSData *data = [NSData dataWithContentsOfFile:path];
  2964. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2965. return ret;
  2966. }
  2967. + (NSString *)textFileName:(NSString *)name {
  2968. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  2969. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  2970. if (!text) {
  2971. text = @"";
  2972. }
  2973. return text;
  2974. }
  2975. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  2976. return [[dic objectForKey:key] mutableCopy];
  2977. }
  2978. + (id)translateSingleQuote:(NSString *)string {
  2979. if ([string isKindOfClass:[NSString class]])
  2980. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2981. return string;
  2982. }
  2983. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  2984. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  2985. }
  2986. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  2987. NSString* ret= nil;
  2988. NSString *sqlQuery = nil;
  2989. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  2990. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ order by i.default_img desc, i._id asc limit 1;",item_id];
  2991. sqlite3_stmt * statement;
  2992. // int count=0;
  2993. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2994. {
  2995. if (sqlite3_step(statement) == SQLITE_ROW)
  2996. {
  2997. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2998. if(imgurl==nil)
  2999. imgurl="";
  3000. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3001. ret=nsimgurl;
  3002. }
  3003. sqlite3_finalize(statement);
  3004. }
  3005. else
  3006. {
  3007. [ret setValue:@"8" forKey:@"result"];
  3008. }
  3009. // [iSalesDB close_db:db];
  3010. // DebugLog(@"data string: %@",ret );
  3011. return ret;
  3012. }
  3013. #pragma mark contact list
  3014. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  3015. {
  3016. // contactType = "Sales_Order_Customer";
  3017. // limit = 25;
  3018. // offset = 0;
  3019. // password = 123456;
  3020. // "price_name" = 16;
  3021. // user = EvanK;
  3022. sqlite3 *db = [iSalesDB get_db];
  3023. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  3024. if (contactType) {
  3025. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  3026. } else {
  3027. contactType = @"1 = 1";
  3028. }
  3029. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  3030. DebugLog(@"offline contact list keyword: %@",keyword);
  3031. // advanced search
  3032. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  3033. if (contact_name) {
  3034. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3035. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  3036. } else {
  3037. contact_name = @"";
  3038. }
  3039. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  3040. if (customer_phone) {
  3041. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3042. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  3043. } else {
  3044. customer_phone = @"";
  3045. }
  3046. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  3047. if (customer_fax) {
  3048. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3049. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  3050. } else {
  3051. customer_fax = @"";
  3052. }
  3053. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  3054. if (customer_zipcode) {
  3055. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3056. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  3057. } else {
  3058. customer_zipcode = @"";
  3059. }
  3060. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  3061. if (customer_sales_rep) {
  3062. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3063. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  3064. } else {
  3065. customer_sales_rep = @"";
  3066. }
  3067. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  3068. if (customer_state) {
  3069. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3070. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  3071. } else {
  3072. customer_state = @"";
  3073. }
  3074. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  3075. if (customer_name) {
  3076. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3077. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  3078. } else {
  3079. customer_name = @"";
  3080. }
  3081. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  3082. if (customer_country) {
  3083. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3084. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  3085. } else {
  3086. customer_country = @"";
  3087. }
  3088. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  3089. if (customer_cid) {
  3090. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3091. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  3092. } else {
  3093. customer_cid = @"";
  3094. }
  3095. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  3096. if (customer_city) {
  3097. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3098. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  3099. } else {
  3100. customer_city = @"";
  3101. }
  3102. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  3103. if (customer_address) {
  3104. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3105. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  3106. } else {
  3107. customer_address = @"";
  3108. }
  3109. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  3110. if (customer_email) {
  3111. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3112. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  3113. } else {
  3114. customer_email = @"";
  3115. }
  3116. NSString *price_name = [params valueForKey:@"price_name"];
  3117. if (price_name) {
  3118. if ([price_name containsString:@","]) {
  3119. // 首先从 price表中查处name
  3120. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  3121. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  3122. for (int i = 1;i < pArray.count;i++) {
  3123. NSString *p = pArray[i];
  3124. [mutablePStr appendFormat:@" or type = %@ ",p];
  3125. }
  3126. [mutablePStr appendString:@";"];
  3127. __block NSMutableArray *price_name_array = [NSMutableArray array];
  3128. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3129. char *name = (char *)sqlite3_column_text(stmt, 0);
  3130. if (!name)
  3131. name = "";
  3132. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  3133. }];
  3134. // 再根据name 拼sql
  3135. NSMutableString *mutable_price_name = [NSMutableString string];
  3136. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  3137. for (int i = 1; i < price_name_array.count; i++) {
  3138. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  3139. }
  3140. [mutable_price_name appendString:@")"];
  3141. price_name = mutable_price_name;
  3142. } else {
  3143. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  3144. if ([price_name isEqualToString:@""]) {
  3145. price_name = @"";
  3146. } else {
  3147. __block NSString *price;
  3148. price_name = [self translateSingleQuote:price_name];
  3149. [iSalesDB jk_query:[NSString stringWithFormat:@"SELECT name FROM price where type = %@ ;",price_name] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3150. char *p = (char *)sqlite3_column_text(stmt, 0);
  3151. if (p == NULL) {
  3152. p = "";
  3153. }
  3154. price = [NSString stringWithUTF8String:p];
  3155. }];
  3156. if ([price isEqualToString:@""]) {
  3157. price_name = @"";
  3158. } else {
  3159. price = [self translateSingleQuote:price];
  3160. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  3161. }
  3162. }
  3163. }
  3164. } else {
  3165. price_name = @"";
  3166. }
  3167. int limit = [[params valueForKey:@"limit"] intValue];
  3168. int offset = [[params valueForKey:@"offset"] intValue];
  3169. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3170. NSString *where = [NSString stringWithFormat:@" %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ and is_active=1 ",contactType,contact_name,customer_phone,customer_fax,customer_zipcode,customer_sales_rep,customer_state,customer_name,customer_country,customer_cid,customer_city,customer_address,customer_email,price_name];
  3171. NSString *sql = [NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ order by decrypt(company_name) limit %d offset %d",where,limit,offset];
  3172. // int result= [iSalesDB AddExFunction:db];
  3173. int count =0;
  3174. NSString *sqlQuery = nil;
  3175. if(keyword.length==0)
  3176. {
  3177. // sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@=1 order by decrypt(company_name) limit %d offset %d",contactType , limit, offset];
  3178. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  3179. sqlQuery = sql;
  3180. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  3181. }
  3182. else
  3183. {
  3184. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  3185. keyword = keyword.lowercaseString;
  3186. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3187. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ and (lower(contact_id ) like'%%%@%%' or lower(decrypt(company_name)) like '%%%@%%' or lower(decrypt(phone)) like '%%%@%%' or lower(decrypt(addr)) like '%%%@%%') and is_active=1 order by decrypt(company_name) limit %d offset %d",contactType , keyword,keyword,keyword,keyword,limit, offset];
  3188. count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@=1 and (lower(contact_id ) like'%@' or lower(company_name) like'%@') and is_active=1",contactType,keyword,keyword]];
  3189. }
  3190. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3191. sqlite3_stmt * statement;
  3192. [ret setValue:@"2" forKey:@"result"];
  3193. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  3194. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3195. {
  3196. int i = 0;
  3197. while (sqlite3_step(statement) == SQLITE_ROW)
  3198. {
  3199. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3200. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3201. int editable = sqlite3_column_int(statement, 0);
  3202. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3203. NSString *nscompany_name =nil;
  3204. if(company_name==nil)
  3205. nscompany_name=@"";
  3206. else
  3207. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  3208. char *country = (char*)sqlite3_column_text(statement, 2);
  3209. if(country==nil)
  3210. country="";
  3211. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3212. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3213. // if(addr==nil)
  3214. // addr="";
  3215. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3216. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3217. if(zipcode==nil)
  3218. zipcode="";
  3219. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3220. char *state = (char*)sqlite3_column_text(statement, 5);
  3221. if(state==nil)
  3222. state="";
  3223. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3224. char *city = (char*)sqlite3_column_text(statement, 6);
  3225. if(city==nil)
  3226. city="";
  3227. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3228. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3229. // NSString *nscontact_name = nil;
  3230. // if(contact_name==nil)
  3231. // nscontact_name=@"";
  3232. // else
  3233. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3234. char *phone = (char*)sqlite3_column_text(statement, 8);
  3235. NSString *nsphone = nil;
  3236. if(phone==nil)
  3237. nsphone=@"";
  3238. else
  3239. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3240. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3241. if(contact_id==nil)
  3242. contact_id="";
  3243. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3244. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3245. if(addr_1==nil)
  3246. addr_1="";
  3247. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3248. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3249. if(addr_2==nil)
  3250. addr_2="";
  3251. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3252. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3253. if(addr_3==nil)
  3254. addr_3="";
  3255. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3256. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3257. if(addr_4==nil)
  3258. addr_4="";
  3259. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3260. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3261. if(first_name==nil)
  3262. first_name="";
  3263. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3264. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3265. if(last_name==nil)
  3266. last_name="";
  3267. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3268. char *fax = (char*)sqlite3_column_text(statement, 16);
  3269. NSString *nsfax = nil;
  3270. if(fax==nil)
  3271. nsfax=@"";
  3272. else
  3273. {
  3274. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3275. if(nsfax.length>0)
  3276. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  3277. }
  3278. char *email = (char*)sqlite3_column_text(statement, 17);
  3279. NSString *nsemail = nil;
  3280. if(email==nil)
  3281. nsemail=@"";
  3282. else
  3283. {
  3284. nsemail= [[NSString alloc]initWithUTF8String:email];
  3285. if(nsemail.length>0)
  3286. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  3287. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  3288. }
  3289. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3290. [arr_name addObject:nsfirst_name];
  3291. [arr_name addObject:nslast_name];
  3292. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3293. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  3294. {
  3295. // decrypt
  3296. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3297. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3298. nsphone=[AESCrypt fastdecrypt:nsphone];
  3299. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3300. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3301. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3302. }
  3303. [arr_addr addObject:nscompany_name];
  3304. [arr_addr addObject:nscontact_name];
  3305. [arr_addr addObject:@"<br>"];
  3306. [arr_addr addObject:nsaddr_1];
  3307. [arr_addr addObject:nsaddr_2];
  3308. [arr_addr addObject:nsaddr_3];
  3309. [arr_addr addObject:nsaddr_4];
  3310. //[arr_addr addObject:nsaddr];
  3311. [arr_addr addObject:nszipcode];
  3312. [arr_addr addObject:nscity];
  3313. [arr_addr addObject:nsstate];
  3314. [arr_addr addObject:nscountry];
  3315. [arr_addr addObject:@"<br>"];
  3316. [arr_addr addObject:nsphone];
  3317. [arr_addr addObject:nsfax];
  3318. [arr_addr addObject:nsemail];
  3319. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  3320. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  3321. [item setValue:name forKey:@"name"];
  3322. [item setValue:nscontact_id forKey:@"contact_id"];
  3323. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  3324. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3325. i++;
  3326. }
  3327. UIApplication * app = [UIApplication sharedApplication];
  3328. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3329. [ret setValue:appDelegate.mode forKey:@"mode"];
  3330. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  3331. sqlite3_finalize(statement);
  3332. }
  3333. [iSalesDB close_db:db];
  3334. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3335. return ret;
  3336. }
  3337. #pragma mark contact Advanced search
  3338. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  3339. {
  3340. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  3341. return [RAUtils dict2data:contactAdvanceDic];
  3342. }
  3343. #pragma mark create new contact
  3344. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  3345. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  3346. NSData *data = [NSData dataWithContentsOfFile:path];
  3347. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3348. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3349. NSString *countryCode = nil;
  3350. NSString *countryCode_id = nil;
  3351. NSString *stateCode = nil;
  3352. NSString *city = nil;
  3353. NSString *zipCode = nil;
  3354. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3355. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  3356. NSString *code_id = params[@"country"];
  3357. countryCode_id = code_id;
  3358. countryCode = [self countryCodeByid:code_id];
  3359. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  3360. NSString *zip_code = params[@"zipcode"];
  3361. // 剔除全部为空格
  3362. int spaceCount = 0;
  3363. for (int i = 0; i < zip_code.length; i++) {
  3364. if ([zip_code characterAtIndex:i] == ' ') {
  3365. spaceCount++;
  3366. }
  3367. }
  3368. if (spaceCount == zip_code.length) {
  3369. zip_code = @"";
  3370. }
  3371. zipCode = zip_code;
  3372. if (zipCode.length > 0) {
  3373. countryCode_id = params[@"country"];
  3374. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3375. countryCode = [dic valueForKey:@"country_code"];
  3376. if (!countryCode) {
  3377. countryCode = @"US";
  3378. }
  3379. stateCode = [dic valueForKey:@"state_code"];
  3380. city = [dic valueForKey:@"city"];
  3381. // zip code
  3382. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  3383. [zipDic setValue:zipCode forKey:@"value"];
  3384. [section_0 setValue:zipDic forKey:@"item_10"];
  3385. }
  3386. }
  3387. } else {
  3388. // default: US United States
  3389. countryCode = @"US";
  3390. countryCode_id = @"228";
  3391. }
  3392. // country
  3393. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3394. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  3395. // state
  3396. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3397. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  3398. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  3399. // city
  3400. if (city) {
  3401. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  3402. [cityDic setValue:city forKey:@"value"];
  3403. [section_0 setValue:cityDic forKey:@"item_12"];
  3404. }
  3405. // price type
  3406. NSDictionary *priceDic = [self offline_getPrice];
  3407. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  3408. // Sales Rep
  3409. NSDictionary *repDic = [self offline_getSalesRep];
  3410. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3411. [ret setValue:section_0 forKey:@"section_0"];
  3412. return [RAUtils dict2data:ret];
  3413. }
  3414. #pragma mark save
  3415. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  3416. NSString *addr = nil;
  3417. NSString *contact_name = nil;
  3418. NSString *companyName = [params objectForKey:@"company"];
  3419. if (companyName) {
  3420. companyName = [AESCrypt fastencrypt:companyName];
  3421. } else {
  3422. companyName = @"";
  3423. }
  3424. DebugLog(@"company");
  3425. companyName = [self translateSingleQuote:companyName];
  3426. NSString *addr1 = [params objectForKey:@"address"];
  3427. NSString *addr2 = [params objectForKey:@"address2"];
  3428. NSString *addr3 = [params objectForKey:@"address_3"];
  3429. NSString *addr4 = [params objectForKey:@"address_4"];
  3430. if (!addr2) {
  3431. addr2 = @"";
  3432. }
  3433. if (!addr3) {
  3434. addr3 = @"";
  3435. }
  3436. if (!addr4) {
  3437. addr4 = @"";
  3438. }
  3439. if (!addr1) {
  3440. addr1 = @"";
  3441. }
  3442. DebugLog(@"addr");
  3443. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  3444. addr = [AESCrypt fastencrypt:addr];
  3445. addr = [self translateSingleQuote:addr];
  3446. if (addr1 && ![addr1 isEqualToString:@""]) {
  3447. addr1 = [AESCrypt fastencrypt:addr1];
  3448. }
  3449. addr1 = [self translateSingleQuote:addr1];
  3450. addr2 = [self translateSingleQuote:addr2];
  3451. addr3 = [self translateSingleQuote:addr3];
  3452. addr4 = [self translateSingleQuote:addr4];
  3453. NSString *country = [params objectForKey:@"country"];
  3454. if (country) {
  3455. country = [self countryNameByCountryCodeId:country];
  3456. } else {
  3457. country = @"";
  3458. }
  3459. DebugLog(@"country");
  3460. country = [self translateSingleQuote:country];
  3461. NSString *state = [params objectForKey:@"state"];
  3462. if (!state) {
  3463. state = @"";
  3464. }
  3465. DebugLog(@"state");
  3466. state = [self translateSingleQuote:state];
  3467. NSString *city = [params objectForKey:@"city"];
  3468. if (!city) {
  3469. city = @"";
  3470. }
  3471. city = [self translateSingleQuote:city];
  3472. NSString *zipcode = [params objectForKey:@"zipcode"];
  3473. if (!zipcode) {
  3474. zipcode = @"";
  3475. }
  3476. DebugLog(@"zip");
  3477. zipcode = [self translateSingleQuote:zipcode];
  3478. NSString *fistName = [params objectForKey:@"firstname"];
  3479. if (!fistName) {
  3480. fistName = @"";
  3481. }
  3482. NSString *lastName = [params objectForKey:@"lastname"];
  3483. if (!lastName) {
  3484. lastName = @"";
  3485. }
  3486. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  3487. DebugLog(@"contact_name");
  3488. contact_name = [self translateSingleQuote:contact_name];
  3489. fistName = [self translateSingleQuote:fistName];
  3490. lastName = [self translateSingleQuote:lastName];
  3491. NSString *phone = [params objectForKey:@"phone"];
  3492. if (phone) {
  3493. phone = [AESCrypt fastencrypt:phone];
  3494. } else {
  3495. phone = @"";
  3496. }
  3497. DebugLog(@"PHONE");
  3498. phone = [self translateSingleQuote:phone];
  3499. NSString *fax = [params objectForKey:@"fax"];
  3500. if (!fax) {
  3501. fax = @"";
  3502. }
  3503. DebugLog(@"FAX");
  3504. fax = [self translateSingleQuote:fax];
  3505. NSString *email = [params objectForKey:@"email"];
  3506. if (!email) {
  3507. email = @"";
  3508. }
  3509. DebugLog(@"EMAIL:%@",email);
  3510. email = [self translateSingleQuote:email];
  3511. NSString *notes = [params objectForKey:@"contact_notes"];
  3512. if (!notes) {
  3513. notes = @"";
  3514. }
  3515. DebugLog(@"NOTE:%@",notes);
  3516. notes = [self translateSingleQuote:notes];
  3517. NSString *price = [params objectForKey:@"price_name"];
  3518. if (price) {
  3519. price = [self priceNameByPriceId:price];
  3520. } else {
  3521. price = @"";
  3522. }
  3523. DebugLog(@"PRICE");
  3524. price = [self translateSingleQuote:price];
  3525. NSString *salesRep = [params objectForKey:@"sales_rep"];
  3526. if (salesRep) {
  3527. salesRep = [self salesRepCodeById:salesRep];
  3528. } else {
  3529. salesRep = @"";
  3530. }
  3531. salesRep = [self translateSingleQuote:salesRep];
  3532. NSString *img = [params objectForKey:@"business_card"];
  3533. NSArray *array = [img componentsSeparatedByString:@","];
  3534. NSString *img_0 = array[0];
  3535. if (!img_0) {
  3536. img_0 = @"";
  3537. }
  3538. img_0 = [self translateSingleQuote:img_0];
  3539. NSString *img_1 = array[1];
  3540. if (!img_1) {
  3541. img_1 = @"";
  3542. }
  3543. img_1 = [self translateSingleQuote:img_1];
  3544. NSString *img_2 = array[2];
  3545. if (!img_2) {
  3546. img_2 = @"";
  3547. }
  3548. img_2 = [self translateSingleQuote:img_2];
  3549. NSString *contact_id = [NSUUID UUID].UUIDString;
  3550. // 判断更新时是否为customer
  3551. if (update) {
  3552. contact_id = [params objectForKey:@"contact_id"];
  3553. if (!contact_id) {
  3554. contact_id = @"";
  3555. }
  3556. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  3557. __block int customer = 0;
  3558. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3559. customer = sqlite3_column_int(stmt, 0);
  3560. }];
  3561. isCustomer = customer ? YES : NO;
  3562. }
  3563. NSMutableDictionary *sync_dic = [params mutableCopy];
  3564. if (isCustomer) {
  3565. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  3566. } else {
  3567. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  3568. }
  3569. NSString *sync_data = nil;
  3570. NSString *sql = nil;
  3571. if (update){
  3572. contact_id = [params objectForKey:@"contact_id"];
  3573. if (!contact_id) {
  3574. contact_id = @"";
  3575. }
  3576. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3577. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3578. sync_data = [RAUtils dict2string:sync_dic];
  3579. sync_data = [self translateSingleQuote:sync_data];
  3580. sql = [NSString stringWithFormat:@"update offline_contact set company_name = '%@',addr_1 = '%@',addr_2 = '%@',addr_3 = '%@',addr_4 = '%@',country = '%@',state = '%@',city = '%@',zipcode = '%@',first_name = '%@',last_name = '%@',phone = '%@',fax = '%@',email = '%@',notes = '%@',price_type = '%@',sales_rep = '%@',img_0 = '%@',img_1 = '%@',img_2 = '%@',sync_data = '%@',contact_name = '%@',addr = '%@' where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,sync_data,contact_name,addr,contact_id];
  3581. } else {
  3582. contact_id = [self translateSingleQuote:contact_id];
  3583. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3584. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3585. sync_data = [RAUtils dict2string:sync_dic];
  3586. sync_data = [self translateSingleQuote:sync_data];
  3587. sql = [NSString stringWithFormat:@"insert into offline_contact (is_active,company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To,sync_data,contact_name,addr) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,0,1,'%@','%@','%@')",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id,isCustomer ? 1 : 0,isCustomer ? 1 : 0,isCustomer ? 1 : 0,sync_data,contact_name,addr];
  3588. }
  3589. int result = [iSalesDB execSql:sql];
  3590. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  3591. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  3592. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  3593. }
  3594. #pragma mark save new contact
  3595. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  3596. {
  3597. return [self offline_saveContact:params update:NO isCustomer:YES];
  3598. }
  3599. #pragma mark edit contact
  3600. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  3601. {
  3602. // {
  3603. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  3604. // password = 123456;
  3605. // user = EvanK;
  3606. // }
  3607. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  3608. NSData *data = [NSData dataWithContentsOfFile:path];
  3609. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3610. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3611. NSString *countryCode = nil;
  3612. NSString *countryCode_id = nil;
  3613. NSString *stateCode = nil;
  3614. /*------contact infor------*/
  3615. __block NSString *country = nil;
  3616. __block NSString *company_name = nil;
  3617. __block NSString *contact_id = params[@"contact_id"];
  3618. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  3619. __block NSString *zipcode = nil;
  3620. __block NSString *state = nil; // state_code
  3621. __block NSString *city = nil; //
  3622. __block NSString *firt_name,*last_name;
  3623. __block NSString *phone,*fax,*email;
  3624. __block NSString *notes,*price_type,*sales_rep;
  3625. __block NSString *img_0,*img_1,*img_2;
  3626. contact_id = [self translateSingleQuote:contact_id];
  3627. NSString *sql = [NSString stringWithFormat:@"select country,company_name,addr_1,addr_2,addr_3,addr_4,zipcode,state,city,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2 from offline_contact where contact_id = '%@';",contact_id];
  3628. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3629. country = [self textAtColumn:0 statement:stmt]; // country name
  3630. company_name = [self textAtColumn:1 statement:stmt];
  3631. addr_1 = [self textAtColumn:2 statement:stmt];
  3632. addr_2 = [self textAtColumn:3 statement:stmt];
  3633. addr_3 = [self textAtColumn:4 statement:stmt];
  3634. addr_4 = [self textAtColumn:5 statement:stmt];
  3635. zipcode = [self textAtColumn:6 statement:stmt];
  3636. state = [self textAtColumn:7 statement:stmt]; // state code
  3637. city = [self textAtColumn:8 statement:stmt];
  3638. firt_name = [self textAtColumn:9 statement:stmt];
  3639. last_name = [self textAtColumn:10 statement:stmt];
  3640. phone = [self textAtColumn:11 statement:stmt];
  3641. fax = [self textAtColumn:12 statement:stmt];
  3642. email = [self textAtColumn:13 statement:stmt];
  3643. notes = [self textAtColumn:14 statement:stmt];
  3644. price_type = [self textAtColumn:15 statement:stmt]; // name
  3645. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  3646. img_0 = [self textAtColumn:17 statement:stmt];
  3647. img_1 = [self textAtColumn:18 statement:stmt];
  3648. img_2 = [self textAtColumn:19 statement:stmt];
  3649. }];
  3650. // decrypt
  3651. if (company_name) {
  3652. company_name = [AESCrypt fastdecrypt:company_name];
  3653. }
  3654. if (addr_1) {
  3655. addr_1 = [AESCrypt fastdecrypt:addr_1];
  3656. }
  3657. if (phone) {
  3658. phone = [AESCrypt fastdecrypt:phone];
  3659. }
  3660. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  3661. countryCode = [iSalesDB jk_queryText:countrySql];
  3662. stateCode = state;
  3663. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3664. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  3665. NSString *code_id = params[@"country"];
  3666. countryCode_id = code_id;
  3667. countryCode = [self countryCodeByid:code_id];
  3668. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  3669. NSString *zip_code = params[@"zipcode"];
  3670. // 剔除全部为空格
  3671. int spaceCount = 0;
  3672. for (int i = 0; i < zip_code.length; i++) {
  3673. if ([zip_code characterAtIndex:i] == ' ') {
  3674. spaceCount++;
  3675. }
  3676. }
  3677. if (spaceCount == zip_code.length) {
  3678. zip_code = @"";
  3679. }
  3680. if (zipcode.length > 0) {
  3681. zipcode = zip_code;
  3682. countryCode_id = params[@"country"];
  3683. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3684. countryCode = [dic valueForKey:@"country_code"];
  3685. stateCode = [dic valueForKey:@"state_code"];
  3686. city = [dic valueForKey:@"city"];
  3687. // zip code
  3688. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  3689. [zipDic setValue:zipcode forKey:@"value"];
  3690. [section_0 setValue:zipDic forKey:@"item_8"];
  3691. }
  3692. }
  3693. }
  3694. // 0 Country
  3695. // 1 Company Name
  3696. // 2 Contact ID
  3697. // 3 Picture
  3698. // 4 Address 1
  3699. // 5 Address 2
  3700. // 6 Address 3
  3701. // 7 Address 4
  3702. // 8 Zip Code
  3703. // 9 State/Province
  3704. // 10 City
  3705. // 11 Contact First Name
  3706. // 12 Contact Last Name
  3707. // 13 Phone
  3708. // 14 Fax
  3709. // 15 Email
  3710. // 16 Contact Notes
  3711. // 17 Price Type
  3712. // 18 Sales Rep
  3713. // country
  3714. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3715. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  3716. // company
  3717. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  3718. // contact_id
  3719. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  3720. // picture
  3721. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  3722. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  3723. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  3724. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  3725. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  3726. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  3727. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  3728. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  3729. // addr 1 2 3 4
  3730. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  3731. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  3732. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  3733. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  3734. // zip code
  3735. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  3736. // state
  3737. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3738. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  3739. // city
  3740. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  3741. // first last
  3742. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  3743. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  3744. // phone fax email
  3745. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  3746. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  3747. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  3748. // notes
  3749. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  3750. // price
  3751. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  3752. for (NSString *key in priceDic.allKeys) {
  3753. if ([key containsString:@"val_"]) {
  3754. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  3755. if ([dic[@"value"] isEqualToString:price_type]) {
  3756. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3757. [priceDic setValue:dic forKey:key];
  3758. }
  3759. }
  3760. }
  3761. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3762. // Sales Rep
  3763. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  3764. for (NSString *key in repDic.allKeys) {
  3765. if ([key containsString:@"val_"]) {
  3766. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  3767. NSString *value = dic[@"value"];
  3768. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  3769. if (code && [code isEqualToString:sales_rep]) {
  3770. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3771. [repDic setValue:dic forKey:key];
  3772. }
  3773. }
  3774. }
  3775. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  3776. [ret setValue:section_0 forKey:@"section_0"];
  3777. return [RAUtils dict2data:ret];
  3778. }
  3779. #pragma mark save contact
  3780. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  3781. {
  3782. return [self offline_saveContact:params update:YES isCustomer:YES];
  3783. }
  3784. #pragma mark category
  3785. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  3786. if (ck) {
  3787. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  3788. for (NSString *key in res.allKeys) {
  3789. if (![key isEqualToString:@"count"]) {
  3790. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  3791. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  3792. if ([val[@"value"] isEqualToString:ck]) {
  3793. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3794. }
  3795. [res setValue:val forKey:key];
  3796. }
  3797. }
  3798. [dic setValue:res forKey:valueKey];
  3799. }
  3800. }
  3801. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  3802. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3803. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3804. NSString* category = [params valueForKey:@"category"];
  3805. if (!category || [category isEqualToString:@""]) {
  3806. category = @"%";
  3807. }
  3808. category = [self translateSingleQuote:category];
  3809. int limit = [[params valueForKey:@"limit"] intValue];
  3810. int offset = [[params valueForKey:@"offset"] intValue];
  3811. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3812. NSString *limit_str = @"";
  3813. if (limited) {
  3814. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3815. }
  3816. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  3817. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  3818. sqlite3 *db = [iSalesDB get_db];
  3819. // [iSalesDB AddExFunction:db];
  3820. int count;
  3821. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  3822. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, category,limit_str];
  3823. double price_min = 0;
  3824. double price_max = 0;
  3825. if ([params.allKeys containsObject:@"alert"]) {
  3826. // alert
  3827. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  3828. NSString *alert = params[@"alert"];
  3829. if ([alert isEqualToString:@"Display All"]) {
  3830. alert = [NSString stringWithFormat:@""];
  3831. } else {
  3832. alert = [self translateSingleQuote:alert];
  3833. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  3834. }
  3835. // available
  3836. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  3837. NSString *available = params[@"available"];
  3838. NSString *available_condition;
  3839. if ([available isEqualToString:@"Display All"]) {
  3840. available_condition = @"";
  3841. } else if ([available isEqualToString:@"Available Now"]) {
  3842. available_condition = @"and availability > 0";
  3843. } else {
  3844. available_condition = @"and availability == 0";
  3845. }
  3846. // best seller
  3847. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  3848. NSString *best_seller = @"";
  3849. NSString *order_best_seller = @"m.name asc";
  3850. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  3851. best_seller = @"and best_seller > 0";
  3852. order_best_seller = @"m.best_seller desc,m.name asc";
  3853. }
  3854. // price
  3855. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  3856. NSString *price = params[@"price"];
  3857. price_min = 0;
  3858. price_max = MAXFLOAT;
  3859. if (appDelegate.user) {
  3860. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  3861. NSMutableString *priceName = [NSMutableString string];
  3862. for (int i = 0; i < priceTypeArray.count; i++) {
  3863. NSString *pricetype = priceTypeArray[i];
  3864. pricetype = [self translateSingleQuote:pricetype];
  3865. if (i == 0) {
  3866. [priceName appendFormat:@"'%@'",pricetype];
  3867. } else {
  3868. [priceName appendFormat:@",'%@'",pricetype];
  3869. }
  3870. }
  3871. if ([price isEqualToString:@"Display All"]) {
  3872. price = [NSString stringWithFormat:@""];
  3873. } else if([price containsString:@"+"]){
  3874. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  3875. price_min = [price doubleValue];
  3876. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  3877. } else {
  3878. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  3879. price_min = [[priceArray objectAtIndex:0] doubleValue];
  3880. price_max = [[priceArray objectAtIndex:1] doubleValue];
  3881. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  3882. }
  3883. } else {
  3884. price = @"";
  3885. }
  3886. // sold_by_qty : Sold in quantities of %@
  3887. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  3888. NSString *qty = params[@"sold_by_qty"];
  3889. if ([qty isEqualToString:@"Display All"]) {
  3890. qty = @"";
  3891. } else {
  3892. qty = [self translateSingleQuote:qty];
  3893. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  3894. }
  3895. // cate
  3896. category = [self translateSingleQuote:category];
  3897. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  3898. // where bestseller > 0 order by bestseller desc
  3899. // sql query: alert availability(int) best_seller(int) price qty
  3900. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;",cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  3901. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  3902. }
  3903. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  3904. if (!appDelegate.user) {
  3905. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  3906. }
  3907. [ret setValue:filter forKey:@"filter"];
  3908. DebugLog(@"offline_category sql:%@",sqlQuery);
  3909. sqlite3_stmt * statement;
  3910. [ret setValue:@"2" forKey:@"result"];
  3911. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3912. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3913. // int count=0;
  3914. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3915. {
  3916. int i=0;
  3917. while (sqlite3_step(statement) == SQLITE_ROW)
  3918. {
  3919. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3920. char *name = (char*)sqlite3_column_text(statement, 0);
  3921. if(name==nil)
  3922. name="";
  3923. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3924. char *description = (char*)sqlite3_column_text(statement, 1);
  3925. if(description==nil)
  3926. description="";
  3927. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3928. int product_id = sqlite3_column_int(statement, 2);
  3929. int wid = sqlite3_column_int(statement, 3);
  3930. int closeout = sqlite3_column_int(statement, 4);
  3931. int cid = sqlite3_column_int(statement, 5);
  3932. int wisdelete = sqlite3_column_int(statement, 6);
  3933. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3934. if(wid !=0 && wisdelete != 1)
  3935. [item setValue:@"true" forKey:@"wish_exists"];
  3936. else
  3937. [item setValue:@"false" forKey:@"wish_exists"];
  3938. if(closeout==0)
  3939. [item setValue:@"false" forKey:@"is_closeout"];
  3940. else
  3941. [item setValue:@"true" forKey:@"is_closeout"];
  3942. if(cid==0)
  3943. [item setValue:@"false" forKey:@"cart_exists"];
  3944. else
  3945. [item setValue:@"true" forKey:@"cart_exists"];
  3946. [item addEntriesFromDictionary:imgjson];
  3947. // [item setValue:nsurl forKey:@"img"];
  3948. [item setValue:nsname forKey:@"name"];
  3949. [item setValue:nsdescription forKey:@"description"];
  3950. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3951. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3952. i++;
  3953. }
  3954. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3955. [ret setObject:items forKey:@"items"];
  3956. sqlite3_finalize(statement);
  3957. } else {
  3958. DebugLog(@"nothing...");
  3959. }
  3960. DebugLog(@"count:%d",count);
  3961. [iSalesDB close_db:db];
  3962. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3963. return ret;
  3964. }
  3965. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  3966. {
  3967. return [self categoryList:params limited:YES];
  3968. }
  3969. # pragma mark item search
  3970. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  3971. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3972. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  3973. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  3974. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  3975. // category
  3976. NSDictionary *category_menu = [self offline_category_menu];
  3977. [filter setValue:category_menu forKey:@"category"];
  3978. NSString* where= nil;
  3979. NSString* orderby= @"m.name";
  3980. if (!filterSearch) {
  3981. int covertype = [[params valueForKey:@"covertype"] intValue];
  3982. switch (covertype) {
  3983. case 0:
  3984. {
  3985. where=@"m.category like'%%#005#%%'";
  3986. break;
  3987. }
  3988. case 1:
  3989. {
  3990. where=@"m.alert like '%QS%'";
  3991. break;
  3992. }
  3993. case 2:
  3994. {
  3995. where=@"m.availability>0";
  3996. break;
  3997. }
  3998. case 3:
  3999. {
  4000. where=@"m.best_seller>0";
  4001. orderby=@"m.best_seller desc,m.name asc";
  4002. break;
  4003. }
  4004. default:
  4005. where=@"1=1";
  4006. break;
  4007. }
  4008. }
  4009. int limit = [[params valueForKey:@"limit"] intValue];
  4010. int offset = [[params valueForKey:@"offset"] intValue];
  4011. NSString *limit_str = @"";
  4012. if (limited) {
  4013. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  4014. }
  4015. sqlite3 *db = [iSalesDB get_db];
  4016. // [iSalesDB AddExFunction:db];
  4017. int count;
  4018. NSString *sqlQuery = nil;
  4019. where = [where stringByAppendingString:@" and m.is_active = 1"];
  4020. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where %@ order by %@ %@;",appDelegate.order_code, where,orderby,limit_str];
  4021. double price_min = 0;
  4022. double price_max = 0;
  4023. if (filterSearch) {
  4024. // alert
  4025. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4026. NSString *alert = params[@"alert"];
  4027. if ([alert isEqualToString:@"Display All"]) {
  4028. alert = [NSString stringWithFormat:@""];
  4029. } else {
  4030. alert = [self translateSingleQuote:alert];
  4031. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4032. }
  4033. // available
  4034. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4035. NSString *available = params[@"available"];
  4036. NSString *available_condition;
  4037. if ([available isEqualToString:@"Display All"]) {
  4038. available_condition = @"";
  4039. } else if ([available isEqualToString:@"Available Now"]) {
  4040. available_condition = @"and availability > 0";
  4041. } else {
  4042. available_condition = @"and availability == 0";
  4043. }
  4044. // best seller
  4045. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4046. NSString *best_seller = @"";
  4047. NSString *order_best_seller = @"m.name asc";
  4048. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4049. best_seller = @"and best_seller > 0";
  4050. order_best_seller = @"m.best_seller desc,m.name asc";
  4051. }
  4052. // price
  4053. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4054. NSString *price = params[@"price"];
  4055. price_min = 0;
  4056. price_max = MAXFLOAT;
  4057. if (appDelegate.user) {
  4058. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4059. NSMutableString *priceName = [NSMutableString string];
  4060. for (int i = 0; i < priceTypeArray.count; i++) {
  4061. NSString *pricetype = priceTypeArray[i];
  4062. pricetype = [self translateSingleQuote:pricetype];
  4063. if (i == 0) {
  4064. [priceName appendFormat:@"'%@'",pricetype];
  4065. } else {
  4066. [priceName appendFormat:@",'%@'",pricetype];
  4067. }
  4068. }
  4069. if ([price isEqualToString:@"Display All"]) {
  4070. price = [NSString stringWithFormat:@""];
  4071. } else if([price containsString:@"+"]){
  4072. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4073. price_min = [price doubleValue];
  4074. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  4075. } else {
  4076. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4077. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4078. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4079. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  4080. }
  4081. } else {
  4082. price = @"";
  4083. }
  4084. // sold_by_qty : Sold in quantities of %@
  4085. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4086. NSString *qty = params[@"sold_by_qty"];
  4087. if ([qty isEqualToString:@"Display All"]) {
  4088. qty = @"";
  4089. } else {
  4090. qty = [self translateSingleQuote:qty];
  4091. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4092. }
  4093. // category
  4094. NSString *category_id = params[@"ctgId"];
  4095. NSMutableArray *cate_id_array = nil;
  4096. NSMutableString *cateWhere = [NSMutableString string];
  4097. if ([category_id isEqualToString:@""] || !category_id) {
  4098. [cateWhere appendString:@"1 = 1"];
  4099. } else {
  4100. if ([category_id containsString:@","]) {
  4101. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  4102. } else {
  4103. cate_id_array = [@[category_id] mutableCopy];
  4104. }
  4105. /*-----------*/
  4106. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  4107. for (int i = 0; i < cate_id_array.count; i++) {
  4108. for (NSString *key0 in cateDic.allKeys) {
  4109. if ([key0 containsString:@"category_"]) {
  4110. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  4111. for (NSString *key1 in category0.allKeys) {
  4112. if ([key1 containsString:@"category_"]) {
  4113. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  4114. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4115. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  4116. cate_id_array[i] = [category1 objectForKey:@"id"];
  4117. if (i == 0) {
  4118. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  4119. } else {
  4120. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  4121. }
  4122. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4123. [category0 setValue:category1 forKey:key1];
  4124. [cateDic setValue:category0 forKey:key0];
  4125. }
  4126. }
  4127. }
  4128. }
  4129. }
  4130. }
  4131. [filter setValue:cateDic forKey:@"category"];
  4132. }
  4133. // where bestseller > 0 order by bestseller desc
  4134. // sql query: alert availability(int) best_seller(int) price qty
  4135. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;", cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  4136. // count
  4137. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4138. }
  4139. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  4140. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4141. if (!appDelegate.user) {
  4142. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4143. }
  4144. [ret setValue:filter forKey:@"filter"];
  4145. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  4146. sqlite3_stmt * statement;
  4147. [ret setValue:@"2" forKey:@"result"];
  4148. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4149. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4150. // int count=0;
  4151. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4152. {
  4153. int i=0;
  4154. while (sqlite3_step(statement) == SQLITE_ROW)
  4155. {
  4156. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4157. char *name = (char*)sqlite3_column_text(statement, 0);
  4158. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4159. char *description = (char*)sqlite3_column_text(statement, 1);
  4160. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4161. int product_id = sqlite3_column_int(statement, 2);
  4162. int wid = sqlite3_column_int(statement, 3);
  4163. int closeout = sqlite3_column_int(statement, 4);
  4164. int cid = sqlite3_column_int(statement, 5);
  4165. int wisdelete = sqlite3_column_int(statement, 6);
  4166. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4167. if(wid !=0 && wisdelete != 1)
  4168. [item setValue:@"true" forKey:@"wish_exists"];
  4169. else
  4170. [item setValue:@"false" forKey:@"wish_exists"];
  4171. if(closeout==0)
  4172. [item setValue:@"false" forKey:@"is_closeout"];
  4173. else
  4174. [item setValue:@"true" forKey:@"is_closeout"];
  4175. if(cid==0)
  4176. [item setValue:@"false" forKey:@"cart_exists"];
  4177. else
  4178. [item setValue:@"true" forKey:@"cart_exists"];
  4179. [item addEntriesFromDictionary:imgjson];
  4180. // [item setValue:nsurl forKey:@"img"];
  4181. [item setValue:nsname forKey:@"fash_name"];
  4182. [item setValue:nsdescription forKey:@"description"];
  4183. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4184. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4185. i++;
  4186. }
  4187. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4188. [ret setObject:items forKey:@"items"];
  4189. sqlite3_finalize(statement);
  4190. }
  4191. [iSalesDB close_db:db];
  4192. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4193. return ret;
  4194. }
  4195. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  4196. {
  4197. return [self itemsearch:params limited:YES];
  4198. }
  4199. #pragma mark order detail
  4200. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  4201. if (str1.length == 0) {
  4202. str1 = @"&nbsp";
  4203. }
  4204. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  4205. return str;
  4206. }
  4207. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  4208. {
  4209. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  4210. [fromformatter setDateFormat:from];
  4211. NSDate *date = [fromformatter dateFromString:datetime];
  4212. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  4213. [toformatter setDateFormat:to];
  4214. NSString * ret = [toformatter stringFromDate:date];
  4215. return ret;
  4216. }
  4217. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  4218. // 把毫秒去掉
  4219. if ([dateTime containsString:@"."]) {
  4220. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  4221. }
  4222. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  4223. formatter.dateFormat = formate;
  4224. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  4225. NSDate *date = [formatter dateFromString:dateTime];
  4226. formatter.dateFormat = newFormate;
  4227. return [formatter stringFromDate:date];
  4228. }
  4229. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  4230. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  4231. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  4232. }
  4233. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  4234. {
  4235. DebugLog(@"offline oderdetail params: %@",params);
  4236. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4237. int orderId = [params[@"orderId"] intValue];
  4238. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  4239. // decrypt card number and card security code
  4240. // NSString* sql=[NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,o.TotalCuft,o.TotalWeight,o.TotalCarton,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),o.credit_card_expiration,o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus from (select _id,so_id,poNumber,create_time,status,TotalCuft,TotalWeight,TotalCarton,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId ];
  4241. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  4242. sqlite3 *db = [iSalesDB get_db];
  4243. sqlite3_stmt * statement;
  4244. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  4245. NSString *nssoid = @"";
  4246. NSString* orderinfo = @"";
  4247. NSString *moreInfo = @"";
  4248. double handlingFee = 0;
  4249. double payments_and_credist = 0;
  4250. double totalPrice = 0;
  4251. double shippingFee = 0;
  4252. double lift_gate = 0;
  4253. NSString *customer_contact = @"";
  4254. NSString *customer_email = @"";
  4255. NSString *customer_fax = @"";
  4256. NSString *customer_phone = @"";
  4257. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  4258. {
  4259. if (sqlite3_step(statement) == SQLITE_ROW)
  4260. {
  4261. int order_id = sqlite3_column_int(statement, 0);
  4262. NSString *sign_url = [self textAtColumn:51 statement:statement];
  4263. ret[@"sign_url"] = sign_url;
  4264. customer_contact = [self textAtColumn:52 statement:statement];
  4265. customer_email = [self textAtColumn:53 statement:statement];
  4266. customer_phone = [self textAtColumn:54 statement:statement];
  4267. customer_fax = [self textAtColumn:55 statement:statement];
  4268. int offline_edit=sqlite3_column_int(statement, 56);
  4269. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  4270. char *soid = (char*)sqlite3_column_text(statement, 1);
  4271. if(soid==nil)
  4272. soid= "";
  4273. nssoid= [[NSString alloc]initWithUTF8String:soid];
  4274. // so#
  4275. ret[@"so#"] = nssoid;
  4276. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  4277. if(poNumber==nil)
  4278. poNumber= "";
  4279. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  4280. char *create_time = (char*)sqlite3_column_text(statement, 3);
  4281. if(create_time==nil)
  4282. create_time= "";
  4283. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  4284. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  4285. int status = sqlite3_column_int(statement, 4);
  4286. int erpStatus = sqlite3_column_int(statement, 49);
  4287. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  4288. // status
  4289. if (status > 1 && status != 3) {
  4290. status = erpStatus;
  4291. } else if (status == 3) {
  4292. status = 15;
  4293. }
  4294. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  4295. ret[@"order_status"] = nsstatus;
  4296. char *company_name = (char*)sqlite3_column_text(statement, 5);
  4297. if(company_name==nil)
  4298. company_name= "";
  4299. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  4300. // company name
  4301. ret[@"company_name"] = nscompany_name;
  4302. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  4303. if(customer_contact==nil)
  4304. customer_contact= "";
  4305. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  4306. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  4307. if(addr_1==nil)
  4308. addr_1="";
  4309. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4310. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  4311. if(addr_2==nil)
  4312. addr_2="";
  4313. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4314. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  4315. if(addr_3==nil)
  4316. addr_3="";
  4317. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4318. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  4319. if(addr_4==nil)
  4320. addr_4="";
  4321. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4322. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4323. [arr_addr addObject:nsaddr_1];
  4324. [arr_addr addObject:nsaddr_2];
  4325. [arr_addr addObject:nsaddr_3];
  4326. [arr_addr addObject:nsaddr_4];
  4327. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  4328. char *logist = (char*)sqlite3_column_text(statement, 11);
  4329. if(logist==nil)
  4330. logist= "";
  4331. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  4332. if (status == -11 || status == 10 || status == 11) {
  4333. nslogist = [self textAtColumn:59 statement:statement];
  4334. };
  4335. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  4336. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  4337. shipping = @"Shipping To Be Quoted";
  4338. } else {
  4339. shippingFee = sqlite3_column_double(statement, 12);
  4340. }
  4341. // Shipping
  4342. ret[@"Shipping"] = shipping;
  4343. int have_lift_gate = sqlite3_column_int(statement, 17);
  4344. lift_gate = sqlite3_column_double(statement, 13);
  4345. // Liftgate Fee(No loading dock)
  4346. if (!have_lift_gate) {
  4347. lift_gate = 0;
  4348. }
  4349. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  4350. if (sqlite3_column_int(statement, 57)) {
  4351. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  4352. }
  4353. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  4354. if(general_notes==nil)
  4355. general_notes= "";
  4356. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  4357. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  4358. if(internal_notes==nil)
  4359. internal_notes= "";
  4360. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  4361. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  4362. if(payment_type==nil)
  4363. payment_type= "";
  4364. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  4365. // order info
  4366. orderinfo = [self textFileName:@"order_info.html"];
  4367. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  4368. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  4369. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  4370. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  4371. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  4372. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  4373. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  4374. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  4375. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  4376. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  4377. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  4378. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  4379. NSString *payment = nil;
  4380. if([nspayment_type isEqualToString:@"Credit Card"])
  4381. {
  4382. payment = [self textFileName:@"creditcardpayment.html"];
  4383. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  4384. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  4385. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  4386. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  4387. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  4388. NSString *card_type = [self textAtColumn:42 statement:statement];
  4389. if (card_type.length > 0) { // 显示星号
  4390. card_type = @"****";
  4391. }
  4392. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  4393. if (card_number.length > 0 && card_number.length > 4) {
  4394. for (int i = 0; i < card_number.length - 4; i++) {
  4395. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  4396. }
  4397. } else {
  4398. card_number = @"";
  4399. }
  4400. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  4401. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  4402. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  4403. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  4404. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  4405. card_expiration = @"****";
  4406. }
  4407. NSString *card_city = [self textAtColumn:46 statement:statement];
  4408. NSString *card_state = [self textAtColumn:47 statement:statement];
  4409. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  4410. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  4411. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  4412. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  4413. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  4414. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  4415. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  4416. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  4417. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  4418. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  4419. }
  4420. else
  4421. {
  4422. payment=[self textFileName:@"normalpayment.html"];
  4423. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  4424. }
  4425. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  4426. ret[@"result"]= [NSNumber numberWithInt:2];
  4427. // more info
  4428. moreInfo = [self textFileName:@"more_info.html"];
  4429. /*****ship to******/
  4430. // ShipToCompany_or_&nbsp
  4431. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  4432. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  4433. NSString *shipToName = [self textAtColumn:19 statement:statement];
  4434. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  4435. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  4436. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  4437. /*****ship from******/
  4438. // ShipFromCompany_or_&nbsp
  4439. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  4440. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  4441. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  4442. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  4443. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  4444. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  4445. /*****freight to******/
  4446. // FreightBillToCompany_or_&nbsp
  4447. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  4448. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  4449. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  4450. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  4451. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  4452. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  4453. /*****merchandise to******/
  4454. // MerchandiseBillToCompany_or_&nbsp
  4455. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  4456. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  4457. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  4458. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  4459. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  4460. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  4461. /*****return to******/
  4462. // ReturnToCompany_or_&nbsp
  4463. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  4464. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  4465. NSString *returnToName = [self textAtColumn:31 statement:statement];
  4466. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  4467. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  4468. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  4469. //
  4470. // DebugLog(@"more info : %@",moreInfo);
  4471. // handling fee
  4472. handlingFee = sqlite3_column_double(statement, 33);
  4473. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  4474. if (sqlite3_column_int(statement, 58)) {
  4475. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  4476. }
  4477. //
  4478. // customer info
  4479. customerID = [self textAtColumn:36 statement:statement];
  4480. // mode
  4481. ret[@"mode"] = appDelegate.mode;
  4482. // model_count
  4483. ret[@"model_count"] = @(0);
  4484. }
  4485. sqlite3_finalize(statement);
  4486. }
  4487. [iSalesDB close_db:db];
  4488. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  4489. // "customer_email" = "Shui Hu";
  4490. // "customer_fax" = "";
  4491. // "customer_first_name" = F;
  4492. // "customer_last_name" = L;
  4493. // "customer_name" = ",da He Xiang Dong Liu A";
  4494. // "customer_phone" = "Hey Xuan Feng";
  4495. contactInfo[@"customer_phone"] = customer_phone;
  4496. contactInfo[@"customer_fax"] = customer_fax;
  4497. contactInfo[@"customer_email"] = customer_email;
  4498. NSString *first_name = @"";
  4499. NSString *last_name = @"";
  4500. if ([customer_contact isEqualToString:@""]) {
  4501. } else if ([customer_contact containsString:@" "]) {
  4502. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  4503. first_name = [customer_contact substringToIndex:first_space_index];
  4504. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  4505. }
  4506. contactInfo[@"customer_first_name"] = first_name;
  4507. contactInfo[@"customer_last_name"] = last_name;
  4508. ret[@"customerInfo"] = contactInfo;
  4509. // models
  4510. if (nssoid) {
  4511. __block double TotalCuft = 0;
  4512. __block double TotalWeight = 0;
  4513. __block int TotalCarton = 0;
  4514. __block double allItemPrice = 0;
  4515. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  4516. sqlite3 *db1 = [iSalesDB get_db];
  4517. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4518. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4519. int product_id = sqlite3_column_int(stmt, 0);
  4520. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4521. int item_id = sqlite3_column_int(stmt, 7);
  4522. NSString* Price=nil;
  4523. if(str_price==nil)
  4524. {
  4525. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4526. if(price==nil)
  4527. Price=@"No Price.";
  4528. else
  4529. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4530. }
  4531. else
  4532. {
  4533. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4534. }
  4535. double discount = sqlite3_column_double(stmt, 2);
  4536. int item_count = sqlite3_column_int(stmt, 3);
  4537. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  4538. NSString *nsline_note=nil;
  4539. if(line_note!=nil)
  4540. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4541. // char *name = (char*)sqlite3_column_text(stmt, 5);
  4542. // NSString *nsname = nil;
  4543. // if(name!=nil)
  4544. // nsname= [[NSString alloc]initWithUTF8String:name];
  4545. NSString *nsname = [self textAtColumn:5 statement:stmt];
  4546. // char *description = (char*)sqlite3_column_text(stmt, 6);
  4547. // NSString *nsdescription=nil;
  4548. // if(description!=nil)
  4549. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4550. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  4551. // int stockUom = sqlite3_column_int(stmt, 8);
  4552. // int _id = sqlite3_column_int(stmt, 9);
  4553. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  4554. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4555. double weight=[bsubtotaljson[@"weight"] doubleValue];
  4556. int carton=[bsubtotaljson[@"carton"] intValue];
  4557. TotalCuft += cuft;
  4558. TotalWeight += weight;
  4559. TotalCarton += carton;
  4560. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4561. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  4562. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  4563. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4564. itemjson[@"note"]=nsline_note;
  4565. itemjson[@"origin_price"] = Price;
  4566. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4567. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4568. itemjson[@"order_item_status"] = @""; // 暂时不处理
  4569. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4570. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4571. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  4572. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4573. if(itemjson[@"combine"] != nil)
  4574. {
  4575. // int citem=0;
  4576. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4577. for(int bc=0;bc<bcount;bc++)
  4578. {
  4579. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4580. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4581. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4582. subTotal += uprice * modulus * item_count;
  4583. }
  4584. }
  4585. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4586. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  4587. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  4588. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  4589. allItemPrice += subTotal;
  4590. }];
  4591. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  4592. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  4593. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  4594. // payments/Credits
  4595. // payments_and_credist = sqlite3_column_double(statement, 34);
  4596. payments_and_credist = allItemPrice;
  4597. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4598. // // total
  4599. // totalPrice = sqlite3_column_double(statement, 35);
  4600. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4601. } else {
  4602. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  4603. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  4604. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  4605. // payments/Credits
  4606. payments_and_credist = 0;
  4607. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4608. }
  4609. // total
  4610. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4611. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4612. ret[@"order_info"]= orderinfo;
  4613. ret[@"more_order_info"] = moreInfo;
  4614. return [RAUtils dict2data:ret];
  4615. }
  4616. #pragma mark order list
  4617. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  4618. NSString *nsstatus = @"";
  4619. switch (status) {
  4620. case 0:
  4621. {
  4622. nsstatus=@"Temp Order";
  4623. break;
  4624. }
  4625. case 1:
  4626. {
  4627. nsstatus=@"Saved Order";
  4628. break;
  4629. }
  4630. case 2: {
  4631. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  4632. break;
  4633. }
  4634. case 3:
  4635. case 15:
  4636. {
  4637. nsstatus=@"Cancelled";
  4638. break;
  4639. }
  4640. case 10:
  4641. {
  4642. nsstatus=@"Quote Submitted";
  4643. break;
  4644. }
  4645. case 11:
  4646. {
  4647. nsstatus=@"Sales Order Submitted";
  4648. break;
  4649. }
  4650. case 12:
  4651. {
  4652. nsstatus=@"Processing";
  4653. break;
  4654. }
  4655. case 13:
  4656. {
  4657. nsstatus=@"Shipped";
  4658. break;
  4659. }
  4660. case 14:
  4661. {
  4662. nsstatus=@"Closed";
  4663. break;
  4664. }
  4665. case -11:
  4666. {
  4667. nsstatus = @"Ready For Submit";
  4668. break;
  4669. }
  4670. default:
  4671. break;
  4672. }
  4673. return nsstatus;
  4674. }
  4675. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  4676. double total = 0;
  4677. __block double payments_and_credist = 0;
  4678. __block double allItemPrice = 0;
  4679. // sqlite3 *db1 = [iSalesDB get_db];
  4680. if (so_id) {
  4681. // NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",so_id];
  4682. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id from (select product_id,decrypt(str_price) as price,discount,item_count,item_id from offline_cart where so_no='%@');",so_id];
  4683. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4684. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4685. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4686. int product_id = sqlite3_column_int(stmt, 0);
  4687. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4688. int discount = sqlite3_column_int(stmt, 2);
  4689. int item_count = sqlite3_column_int(stmt, 3);
  4690. // int item_id = sqlite3_column_int(stmt, 7);
  4691. int item_id = sqlite3_column_int(stmt, 4);
  4692. NSString* Price=nil;
  4693. if(str_price==nil)
  4694. {
  4695. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4696. if(price==nil)
  4697. Price=@"No Price.";
  4698. else
  4699. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4700. }
  4701. else
  4702. {
  4703. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4704. }
  4705. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4706. itemjson[@"The unit price"]=Price;
  4707. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4708. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4709. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4710. if(itemjson[@"combine"] != nil)
  4711. {
  4712. // int citem=0;
  4713. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4714. for(int bc=0;bc<bcount;bc++)
  4715. {
  4716. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4717. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4718. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4719. subTotal += uprice * modulus * item_count;
  4720. }
  4721. }
  4722. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4723. allItemPrice += subTotal;
  4724. }];
  4725. payments_and_credist = allItemPrice;
  4726. } else {
  4727. // payments/Credits
  4728. payments_and_credist = 0;
  4729. }
  4730. // lift_gate handlingFee shippingFee
  4731. __block double lift_gate = 0;
  4732. __block double handlingFee = 0;
  4733. __block double shippingFee = 0;
  4734. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  4735. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4736. int have_lift_gate = sqlite3_column_int(stmt, 0);
  4737. if (have_lift_gate) {
  4738. lift_gate = sqlite3_column_double(stmt, 1);
  4739. }
  4740. handlingFee = sqlite3_column_double(stmt, 2);
  4741. shippingFee = sqlite3_column_double(stmt, 3);
  4742. }];
  4743. // total
  4744. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4745. if (close) {
  4746. [iSalesDB close_db:db1];
  4747. }
  4748. return total;
  4749. }
  4750. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  4751. {
  4752. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4753. int limit = [[params valueForKey:@"limit"] intValue];
  4754. int offset = [[params valueForKey:@"offset"] intValue];
  4755. NSString* keyword = [params valueForKey:@"keyWord"];
  4756. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  4757. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  4758. NSString* where=@"1 = 1";
  4759. if(keyword.length>0)
  4760. where=[where stringByAppendingString:[NSString stringWithFormat:@" and (lower(o.so_id) like '%%%@%%' or lower(o.sales_rep) like '%%%@%%' or lower(decrypt(c.company_name)) like '%%%@%%' or lower(o.create_by) like '%%%@%%')",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString]];
  4761. if (orderStatus.length > 0) {
  4762. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  4763. if (order_status_array.count == 1) {
  4764. int status_value = [[order_status_array firstObject] integerValue];
  4765. if (status_value <= 1 || status_value == 3) {
  4766. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  4767. } else {
  4768. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  4769. }
  4770. } else if (order_status_array.count > 1) {
  4771. for (int i = 0; i < order_status_array.count;i++) {
  4772. NSString *status = order_status_array[i];
  4773. NSString *condition = @" or";
  4774. if (i == 0) {
  4775. condition = @" and (";
  4776. }
  4777. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  4778. int status_value = [status integerValue];
  4779. if (status_value <= 1 || status_value == 3) {
  4780. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  4781. } else {
  4782. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  4783. }
  4784. }
  4785. where = [where stringByAppendingString:@" )"];
  4786. }
  4787. }
  4788. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price,o.customer_contact,o.erpOrderStatus,o.order_id is null from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.modify_time desc,o.create_time desc limit %d offset %d" ,where, limit, offset];
  4789. // DebugLog(@"order list sql: %@",sqlQuery);
  4790. sqlite3 *db = [iSalesDB get_db];
  4791. sqlite3_stmt * statement;
  4792. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4793. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4794. {
  4795. int count=0;
  4796. while (sqlite3_step(statement) == SQLITE_ROW)
  4797. {
  4798. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  4799. int order_id = sqlite3_column_double(statement, 0);
  4800. char *soid = (char*)sqlite3_column_text(statement, 1);
  4801. if(soid==nil)
  4802. soid= "";
  4803. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  4804. int status = sqlite3_column_double(statement, 2);
  4805. int erpStatus = sqlite3_column_double(statement, 9);
  4806. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  4807. if(sales_rep==nil)
  4808. sales_rep= "";
  4809. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  4810. char *create_by = (char*)sqlite3_column_text(statement, 4);
  4811. if(create_by==nil)
  4812. create_by= "";
  4813. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  4814. char *company_name = (char*)sqlite3_column_text(statement, 5);
  4815. if(company_name==nil)
  4816. company_name= "";
  4817. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  4818. char *create_time = (char*)sqlite3_column_text(statement, 6);
  4819. if(create_time==nil)
  4820. create_time= "";
  4821. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  4822. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  4823. // double total_price = sqlite3_column_double(statement, 7);
  4824. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  4825. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  4826. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  4827. int offline_edit = sqlite3_column_int(statement, 10);
  4828. // ": "JH",
  4829. // "": "$8307.00",
  4830. // "": "MOB1608050001",
  4831. // "": "ArpithaT",
  4832. // "": "1st Stage Property Transformations",
  4833. // "": "JANICE SUTTON",
  4834. // "": 2255,
  4835. // "": "08/02/2016 09:49:18",
  4836. // "": 1,
  4837. // "": "Saved Order"
  4838. // "": "1470384050483",
  4839. // "model_count": "6 / 28"
  4840. item[@"sales_rep"]= nssales_rep;
  4841. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  4842. item[@"so#"]= nssoid;
  4843. item[@"create_by"]= nscreate_by;
  4844. item[@"customer_name"]= nscompany_name;
  4845. item[@"customer_contact"] = customer_contact;
  4846. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  4847. item[@"purchase_time"]= nscreate_time;
  4848. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  4849. item[@"order_status"]= nsstatus;
  4850. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  4851. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  4852. // item[@"model_count"]
  4853. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  4854. count++;
  4855. }
  4856. ret[@"count"]= [NSNumber numberWithInt:count];
  4857. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  4858. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  4859. ret[@"result"]= [NSNumber numberWithInt:2];
  4860. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  4861. ret[@"time_zone"] = @"PST";
  4862. sqlite3_finalize(statement);
  4863. }
  4864. int total_count = [iSalesDB get_recordcount:db table:@"offline_order o left join offline_contact c on o.customer_cid=c.contact_id" where:where];
  4865. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  4866. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4867. [iSalesDB close_db:db];
  4868. return [RAUtils dict2data:ret];
  4869. }
  4870. #pragma mark update gnotes
  4871. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  4872. {
  4873. DebugLog(@"params: %@",params);
  4874. // comments = Meyoyoyoyoyoyoy;
  4875. // orderCode = MOB1608110001;
  4876. // password = 123456;
  4877. // user = EvanK;
  4878. NSString *sql = [NSString stringWithFormat:@"update offline_order set general_notes = '%@' where so_id = '%@';",[self valueInParams:params key:@"comments" translateSingleQuote:YES],[self valueInParams:params key:@"orderCode" translateSingleQuote:YES]];
  4879. int ret = [iSalesDB execSql:sql];
  4880. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4881. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4882. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4883. // [dic setValue:@"160409" forKey:@"min_ver"];
  4884. return [RAUtils dict2data:dic];
  4885. }
  4886. #pragma mark move to wishlist
  4887. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  4888. {
  4889. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  4890. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4891. _id = [NSString stringWithFormat:@"(%@)",_id];
  4892. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  4893. sqlite3 *db = [iSalesDB get_db];
  4894. __block NSString *product_id = @"";
  4895. __block NSString *item_count_str = @"";
  4896. // __block NSString *item_id = nil;
  4897. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4898. // product_id = [self textAtColumn:0 statement:stmt];
  4899. int item_count = sqlite3_column_int(stmt, 1);
  4900. // item_id = [self textAtColumn:2 statement:stmt];
  4901. NSString *p_id = [self textAtColumn:0 statement:stmt];
  4902. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  4903. if (p_id.length) {
  4904. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  4905. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  4906. }
  4907. }];
  4908. [iSalesDB close_db:db];
  4909. // 去除第一个,
  4910. product_id = [product_id substringFromIndex:1];
  4911. item_count_str = [item_count_str substringFromIndex:1];
  4912. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4913. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  4914. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  4915. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  4916. sqlite3 *db1 = [iSalesDB get_db];
  4917. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4918. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4919. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  4920. // 删除
  4921. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  4922. int ret = [iSalesDB execSql:deleteSql db:db1];
  4923. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4924. [iSalesDB close_db:db1];
  4925. return [RAUtils dict2data:dic];
  4926. }
  4927. #pragma mark cart delete
  4928. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  4929. {
  4930. // cartItemId = 548;
  4931. // orderCode = MOB1608110001;
  4932. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  4933. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4934. _id = [NSString stringWithFormat:@"(%@)",_id];
  4935. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  4936. int ret = [iSalesDB execSql:sql];
  4937. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4938. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4939. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4940. // [dic setValue:@"160409" forKey:@"min_ver"];
  4941. return [RAUtils dict2data:dic];
  4942. }
  4943. #pragma mark set price
  4944. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  4945. {
  4946. DebugLog(@"cart set price params: %@",params);
  4947. // "cartitem_id" = 1;
  4948. // discount = "0.000000";
  4949. // "item_note" = "";
  4950. // price = "269.000000";
  4951. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  4952. NSString *notes = [self valueInParams:params key:@"item_note"];
  4953. NSString *discount = [self valueInParams:params key:@"discount"];
  4954. NSString *price = [self valueInParams:params key:@"price"];
  4955. // bool badd_price_changed=false;
  4956. // sqlite3* db=[iSalesDB get_db];
  4957. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4958. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  4959. // NSRange range;
  4960. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  4961. //
  4962. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  4963. // badd_price_changed=true;
  4964. // [iSalesDB close_db:db];
  4965. //
  4966. // if(badd_price_changed)
  4967. // {
  4968. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  4969. // }
  4970. //
  4971. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  4972. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  4973. int ret = [iSalesDB execSql:sql];
  4974. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4975. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4976. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4977. // [dic setValue:@"160409" forKey:@"min_ver"];
  4978. return [RAUtils dict2data:dic];
  4979. }
  4980. #pragma mark set line notes
  4981. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  4982. {
  4983. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  4984. NSString *notes = [self valueInParams:params key:@"notes"];
  4985. notes = [self translateSingleQuote:notes];
  4986. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  4987. int ret = [iSalesDB execSql:sql];
  4988. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4989. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4990. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4991. // [dic setValue:@"160409" forKey:@"min_ver"];
  4992. return [RAUtils dict2data:dic];
  4993. }
  4994. #pragma mark set qty
  4995. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  4996. {
  4997. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  4998. int item_count = [params[@"inputInt"] integerValue];
  4999. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  5000. int ret = [iSalesDB execSql:sql];
  5001. __block int item_id = 0;
  5002. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5003. item_id = sqlite3_column_int(stmt, 0);
  5004. }];
  5005. sqlite3 *db = [iSalesDB get_db];
  5006. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5007. [iSalesDB close_db:db];
  5008. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5009. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5010. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5011. // [dic setValue:@"160409" forKey:@"min_ver"];
  5012. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  5013. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  5014. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  5015. return [RAUtils dict2data:dic];
  5016. }
  5017. #pragma mark place order
  5018. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  5019. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5020. if (aname) {
  5021. [dic setValue:aname forKey:@"aname"];
  5022. }
  5023. if (control) {
  5024. [dic setValue:control forKey:@"control"];
  5025. }
  5026. if (keyboard) {
  5027. [dic setValue:keyboard forKey:@"keyboard"];
  5028. }
  5029. if (name) {
  5030. [dic setValue:name forKey:@"name"];
  5031. }
  5032. if (value) {
  5033. [dic setValue:value forKey:@"value"];
  5034. }
  5035. return dic;
  5036. }
  5037. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  5038. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5039. orderCode = [self translateSingleQuote:orderCode];
  5040. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  5041. NSString *customerSql = [NSString stringWithFormat:@"select c.contact_id,c.img_0,c.img_1,c.img_2,decrypt(c.company_name),decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,c.first_name,c.last_name,o.customer_email,o.customer_phone,o.customer_fax,c.zipcode,c.city,c.state,c.country,o.customer_contact from offline_contact as c join offline_order as o on c.contact_id = o.customer_cid where o.so_id = '%@';",orderCode];
  5042. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5043. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  5044. NSString *img0 = [self textAtColumn:1 statement:stmt];
  5045. NSString *img1 = [self textAtColumn:2 statement:stmt];
  5046. NSString *img2 = [self textAtColumn:3 statement:stmt];
  5047. NSString *company_name = [self textAtColumn:4 statement:stmt];
  5048. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  5049. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  5050. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  5051. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  5052. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  5053. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  5054. NSString *email = [self textAtColumn:11 statement:stmt];
  5055. NSString *phone = [self textAtColumn:12 statement:stmt];
  5056. NSString *fax = [self textAtColumn:13 statement:stmt];
  5057. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  5058. NSString *city = [self textAtColumn:15 statement:stmt];
  5059. NSString *state = [self textAtColumn:16 statement:stmt];
  5060. NSString *country = [self textAtColumn:17 statement:stmt];
  5061. NSString *name = [self textAtColumn:18 statement:stmt];
  5062. // contact id
  5063. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  5064. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  5065. [contact_id_dic setValue:@"text" forKey:@"control"];
  5066. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  5067. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  5068. [contact_id_dic setValue:@"true" forKey:@"required"];
  5069. [contact_id_dic setValue:contact_id forKey:@"value"];
  5070. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  5071. // business card
  5072. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  5073. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  5074. [business_card_dic setValue:@"img" forKey:@"control"];
  5075. [business_card_dic setValue:@"1" forKey:@"disable"];
  5076. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  5077. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  5078. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  5079. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  5080. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  5081. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  5082. [business_card_dic setValue:@"business_card" forKey:@"name"];
  5083. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  5084. // fax
  5085. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5086. [customer_dic setValue:fax_dic forKey:@"item_10"];
  5087. // zipcode
  5088. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  5089. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  5090. // city
  5091. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  5092. [customer_dic setValue:city_dic forKey:@"item_12"];
  5093. // state
  5094. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  5095. [customer_dic setValue:state_dic forKey:@"item_13"];
  5096. // country
  5097. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  5098. [customer_dic setValue:country_dic forKey:@"item_14"];
  5099. // company name
  5100. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  5101. [customer_dic setValue:company_dic forKey:@"item_2"];
  5102. // addr_1
  5103. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  5104. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  5105. // addr_2
  5106. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  5107. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  5108. // addr_3
  5109. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  5110. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  5111. // addr_4
  5112. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  5113. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  5114. // Contact
  5115. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  5116. [customer_dic setValue:contact_dic forKey:@"item_7"];
  5117. // email
  5118. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5119. [customer_dic setValue:email_dic forKey:@"item_8"];
  5120. // phone
  5121. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5122. [customer_dic setValue:phone_dic forKey:@"item_9"];
  5123. // title
  5124. [customer_dic setValue:@"Customer" forKey:@"title"];
  5125. // count
  5126. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  5127. }];
  5128. // setting
  5129. NSDictionary *setting = params[@"setting"];
  5130. NSNumber *hide = setting[@"CustomerHide"];
  5131. [customer_dic setValue:hide forKey:@"hide"];
  5132. return customer_dic;
  5133. }
  5134. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5135. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5136. // setting
  5137. NSDictionary *setting = params[@"setting"];
  5138. NSNumber *hide = setting[@"ShipToHide"];
  5139. [dic setValue:hide forKey:@"hide"];
  5140. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5141. orderCode = [self translateSingleQuote:orderCode];
  5142. NSString *sql = [NSString stringWithFormat:@"select receive_cid,receive_name,receive_ext,receive_contact,receive_email,receive_fax,receive_phone from offline_order where so_id = '%@';",orderCode];
  5143. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5144. NSString *cid = [self textAtColumn:0 statement:stmt];
  5145. NSString *name = [self textAtColumn:1 statement:stmt];
  5146. NSString *ext = [self textAtColumn:2 statement:stmt];
  5147. NSString *contact = [self textAtColumn:3 statement:stmt];
  5148. NSString *email = [self textAtColumn:4 statement:stmt];
  5149. NSString *fax = [self textAtColumn:5 statement:stmt];
  5150. NSString *phone = [self textAtColumn:6 statement:stmt];
  5151. // count
  5152. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5153. // title
  5154. [dic setValue:@"Ship To" forKey:@"title"];
  5155. // choose
  5156. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5157. [choose_dic setValue:@"choose" forKey:@"aname"];
  5158. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5159. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5160. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5161. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5162. @"receive_contact" : @"customer_contact",
  5163. @"receive_email" : @"customer_email",
  5164. @"receive_ext" : @"customer_contact_ext",
  5165. @"receive_fax" : @"customer_fax",
  5166. @"receive_name" : @"customer_name",
  5167. @"receive_phone" : @"customer_phone"},
  5168. @"refresh" : [NSNumber numberWithInteger:1],
  5169. @"type" : @"pull"};
  5170. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  5171. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  5172. @"name" : @"Add new address",
  5173. @"refresh" : [NSNumber numberWithInteger:1],
  5174. @"value" : @"new_addr"
  5175. };
  5176. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  5177. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  5178. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5179. @"receive_contact" : @"customer_contact",
  5180. @"receive_email" : @"customer_email",
  5181. @"receive_ext" : @"customer_contact_ext",
  5182. @"receive_fax" : @"customer_fax",
  5183. @"receive_name" : @"customer_name",
  5184. @"receive_phone" : @"customer_phone"},
  5185. @"name" : @"select_ship_to",
  5186. @"refresh" : [NSNumber numberWithInteger:1],
  5187. @"value" : @"Sales_Order_Ship_To"};
  5188. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  5189. [dic setValue:choose_dic forKey:@"item_0"];
  5190. // contact id
  5191. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5192. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5193. [contact_id_dic setValue:@"true" forKey:@"required"];
  5194. [dic setValue:contact_id_dic forKey:@"item_1"];
  5195. // company name
  5196. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5197. [dic setValue:company_name_dic forKey:@"item_2"];
  5198. // address
  5199. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5200. NSNumber *required = setting[@"ShippingToAddressRequire"];
  5201. if ([required integerValue]) {
  5202. [ext_dic setValue:@"true" forKey:@"required"];
  5203. }
  5204. [dic setValue:ext_dic forKey:@"item_3"];
  5205. // contact
  5206. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5207. [dic setValue:contact_dic forKey:@"item_4"];
  5208. // phone
  5209. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5210. [dic setValue:phone_dic forKey:@"item_5"];
  5211. // fax
  5212. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5213. [dic setValue:fax_dic forKey:@"item_6"];
  5214. // email
  5215. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5216. [dic setValue:email_dic forKey:@"item_7"];
  5217. }];
  5218. return dic;
  5219. }
  5220. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  5221. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5222. // setting
  5223. NSDictionary *setting = params[@"setting"];
  5224. NSNumber *hide = setting[@"ShipFromHide"];
  5225. [dic setValue:hide forKey:@"hide"];
  5226. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5227. orderCode = [self translateSingleQuote:orderCode];
  5228. NSString *sql = [NSString stringWithFormat:@"select sender_cid,sender_name,sender_ext,sender_contact,sender_email,sender_fax,sender_phone from offline_order where so_id = '%@';",orderCode];
  5229. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,phone from offline_contact where contact_id = 'NPD';";
  5230. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5231. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  5232. __block NSString *name = [self textAtColumn:1 statement:stmt];
  5233. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  5234. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  5235. __block NSString *email = [self textAtColumn:4 statement:stmt];
  5236. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  5237. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  5238. if (!cid.length) {
  5239. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  5240. cid = [self textAtColumn:0 statement:statment];
  5241. name = [self textAtColumn:1 statement:statment];
  5242. ext = [self textAtColumn:2 statement:statment];
  5243. contact = [self textAtColumn:3 statement:statment];
  5244. email = [self textAtColumn:4 statement:statment];
  5245. fax = [self textAtColumn:5 statement:statment];
  5246. phone = [self textAtColumn:6 statement:statment];
  5247. }];
  5248. }
  5249. // count
  5250. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5251. // title
  5252. [dic setValue:@"Ship From" forKey:@"title"];
  5253. // hide
  5254. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5255. // choose
  5256. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5257. [choose_dic setValue:@"choose" forKey:@"aname"];
  5258. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5259. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5260. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  5261. @"key_map" : @{@"sender_cid" : @"customer_cid",
  5262. @"sender_contact" : @"customer_contact",
  5263. @"sender_email" : @"customer_email",
  5264. @"sender_ext" : @"customer_contact_ext",
  5265. @"sender_fax" : @"customer_fax",
  5266. @"sender_name" : @"customer_name",
  5267. @"sender_phone" : @"customer_phone"},
  5268. @"name" : @"select_cid",
  5269. @"refresh" : [NSNumber numberWithInteger:0],
  5270. @"value" : @"Sales_Order_Ship_From"};
  5271. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  5272. [dic setValue:choose_dic forKey:@"item_0"];
  5273. // contact id
  5274. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5275. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5276. [contact_id_dic setValue:@"true" forKey:@"required"];
  5277. [dic setValue:contact_id_dic forKey:@"item_1"];
  5278. // company name
  5279. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5280. [dic setValue:company_name_dic forKey:@"item_2"];
  5281. // address
  5282. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5283. [dic setValue:ext_dic forKey:@"item_3"];
  5284. // contact
  5285. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5286. [dic setValue:contact_dic forKey:@"item_4"];
  5287. // phone
  5288. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5289. [dic setValue:phone_dic forKey:@"item_5"];
  5290. // fax
  5291. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5292. [dic setValue:fax_dic forKey:@"item_6"];
  5293. // email
  5294. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5295. [dic setValue:email_dic forKey:@"item_7"];
  5296. }];
  5297. return dic;
  5298. }
  5299. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  5300. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5301. // setting
  5302. NSDictionary *setting = params[@"setting"];
  5303. NSNumber *hide = setting[@"FreightBillToHide"];
  5304. [dic setValue:hide forKey:@"hide"];
  5305. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5306. orderCode = [self translateSingleQuote:orderCode];
  5307. NSString *sql = [NSString stringWithFormat:@"select shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_email,shipping_billto_fax,shipping_billto_phone from offline_order where so_id = '%@';",orderCode];
  5308. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5309. NSString *cid = [self textAtColumn:0 statement:stmt];
  5310. NSString *name = [self textAtColumn:1 statement:stmt];
  5311. NSString *ext = [self textAtColumn:2 statement:stmt];
  5312. NSString *contact = [self textAtColumn:3 statement:stmt];
  5313. NSString *email = [self textAtColumn:4 statement:stmt];
  5314. NSString *fax = [self textAtColumn:5 statement:stmt];
  5315. NSString *phone = [self textAtColumn:6 statement:stmt];
  5316. // count
  5317. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5318. // title
  5319. [dic setValue:@"Freight Bill To" forKey:@"title"];
  5320. // hide
  5321. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5322. // choose
  5323. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5324. [choose_dic setValue:@"choose" forKey:@"aname"];
  5325. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5326. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  5327. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5328. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  5329. @"shipping_billto_contact" : @"receive_contact",
  5330. @"shipping_billto_email" : @"receive_email",
  5331. @"shipping_billto_ext" : @"receive_ext",
  5332. @"shipping_billto_fax" : @"receive_fax",
  5333. @"shipping_billto_name" : @"receive_name",
  5334. @"shipping_billto_phone" : @"receive_phone"},
  5335. @"type" : @"pull"};
  5336. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5337. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5338. @"type" : @"pull",
  5339. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  5340. @"shipping_billto_contact" : @"customer_contact",
  5341. @"shipping_billto_email" : @"customer_email",
  5342. @"shipping_billto_ext" : @"customer_contact_ext",
  5343. @"shipping_billto_fax" : @"customer_fax",
  5344. @"shipping_billto_name" : @"customer_name",
  5345. @"shipping_billto_phone" : @"customer_phone"}
  5346. };
  5347. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5348. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5349. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  5350. @"shipping_billto_contact" : @"sender_contact",
  5351. @"shipping_billto_email" : @"sender_email",
  5352. @"shipping_billto_ext" : @"sender_ext",
  5353. @"shipping_billto_fax" : @"sender_fax",
  5354. @"shipping_billto_name" : @"sender_name",
  5355. @"shipping_billto_phone" : @"sender_phone"},
  5356. @"type" : @"pull"
  5357. };
  5358. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  5359. NSDictionary *select_freight_bill_to_dic = @{
  5360. @"aname" : @"Select freight bill to",
  5361. @"name" : @"select_cid",
  5362. @"refresh" : [NSNumber numberWithInteger:0],
  5363. @"value" : @"Sales_Order_Freight_Bill_To",
  5364. @"key_map" : @{
  5365. @"shipping_billto_cid" : @"customer_cid",
  5366. @"shipping_billto_contact" : @"customer_contact",
  5367. @"shipping_billto_email" : @"customer_email",
  5368. @"shipping_billto_ext" : @"customer_contact_ext",
  5369. @"shipping_billto_fax" : @"customer_fax",
  5370. @"shipping_billto_name" : @"customer_name",
  5371. @"shipping_billto_phone" : @"customer_phone"
  5372. }
  5373. };
  5374. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  5375. [dic setValue:choose_dic forKey:@"item_0"];
  5376. // contact id
  5377. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5378. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5379. [contact_id_dic setValue:@"true" forKey:@"required"];
  5380. [dic setValue:contact_id_dic forKey:@"item_1"];
  5381. // company name
  5382. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5383. [dic setValue:company_name_dic forKey:@"item_2"];
  5384. // address
  5385. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5386. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  5387. if ([ext_required integerValue]) {
  5388. [ext_dic setValue:@"true" forKey:@"required"];
  5389. }
  5390. [dic setValue:ext_dic forKey:@"item_3"];
  5391. // contact
  5392. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5393. [dic setValue:contact_dic forKey:@"item_4"];
  5394. // phone
  5395. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5396. [dic setValue:phone_dic forKey:@"item_5"];
  5397. // fax
  5398. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5399. [dic setValue:fax_dic forKey:@"item_6"];
  5400. // email
  5401. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5402. [dic setValue:email_dic forKey:@"item_7"];
  5403. }];
  5404. return dic;
  5405. }
  5406. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5407. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5408. // setting
  5409. NSDictionary *setting = params[@"setting"];
  5410. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  5411. [dic setValue:hide forKey:@"hide"];
  5412. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5413. orderCode = [self translateSingleQuote:orderCode];
  5414. NSString *sql = [NSString stringWithFormat:@"select billing_cid,billing_name,billing_ext,billing_contact,billing_email,billing_fax,billing_phone from offline_order where so_id = '%@';",orderCode];
  5415. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5416. NSString *cid = [self textAtColumn:0 statement:stmt];
  5417. NSString *name = [self textAtColumn:1 statement:stmt];
  5418. NSString *ext = [self textAtColumn:2 statement:stmt];
  5419. NSString *contact = [self textAtColumn:3 statement:stmt];
  5420. NSString *email = [self textAtColumn:4 statement:stmt];
  5421. NSString *fax = [self textAtColumn:5 statement:stmt];
  5422. NSString *phone = [self textAtColumn:6 statement:stmt];
  5423. // count
  5424. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5425. // title
  5426. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  5427. // hide
  5428. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5429. // choose
  5430. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5431. [choose_dic setValue:@"choose" forKey:@"aname"];
  5432. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5433. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5434. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5435. @"key_map" : @{@"billing_cid" : @"receive_cid",
  5436. @"billing_contact" : @"receive_contact",
  5437. @"billing_email" : @"receive_email",
  5438. @"billing_ext" : @"receive_ext",
  5439. @"billing_fax" : @"receive_fax",
  5440. @"billing_name" : @"receive_name",
  5441. @"billing_phone" : @"receive_phone"},
  5442. @"type" : @"pull"};
  5443. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5444. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5445. @"type" : @"pull",
  5446. @"key_map" : @{@"billing_cid" : @"customer_cid",
  5447. @"billing_contact" : @"customer_contact",
  5448. @"billing_email" : @"customer_email",
  5449. @"billing_ext" : @"customer_contact_ext",
  5450. @"billing_fax" : @"customer_fax",
  5451. @"billing_name" : @"customer_name",
  5452. @"billing_phone" : @"customer_phone"}
  5453. };
  5454. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5455. NSDictionary *select_bill_to_dic = @{
  5456. @"aname" : @"Select bill to",
  5457. @"name" : @"select_cid",
  5458. @"refresh" : [NSNumber numberWithInteger:0],
  5459. @"value" : @"Sales_Order_Merchandise_Bill_To",
  5460. @"key_map" : @{
  5461. @"billing_cid" : @"customer_cid",
  5462. @"billing_contact" : @"customer_contact",
  5463. @"billing_email" : @"customer_email",
  5464. @"billing_ext" : @"customer_contact_ext",
  5465. @"billing_fax" : @"customer_fax",
  5466. @"billing_name" : @"customer_name",
  5467. @"billing_phone" : @"customer_phone"
  5468. }
  5469. };
  5470. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  5471. [dic setValue:choose_dic forKey:@"item_0"];
  5472. // contact id
  5473. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5474. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5475. [contact_id_dic setValue:@"true" forKey:@"required"];
  5476. [dic setValue:contact_id_dic forKey:@"item_1"];
  5477. // company name
  5478. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5479. [dic setValue:company_name_dic forKey:@"item_2"];
  5480. // address
  5481. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5482. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  5483. if ([ext_required integerValue]) {
  5484. [ext_dic setValue:@"true" forKey:@"required"];
  5485. }
  5486. [dic setValue:ext_dic forKey:@"item_3"];
  5487. // contact
  5488. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5489. [dic setValue:contact_dic forKey:@"item_4"];
  5490. // phone
  5491. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5492. [dic setValue:phone_dic forKey:@"item_5"];
  5493. // fax
  5494. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5495. [dic setValue:fax_dic forKey:@"item_6"];
  5496. // email
  5497. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5498. [dic setValue:email_dic forKey:@"item_7"];
  5499. }];
  5500. return dic;
  5501. }
  5502. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5503. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5504. // setting
  5505. NSDictionary *setting = params[@"setting"];
  5506. NSNumber *hide = setting[@"ReturnToHide"];
  5507. [dic setValue:hide forKey:@"hide"];
  5508. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5509. orderCode = [self translateSingleQuote:orderCode];
  5510. NSString *sql = [NSString stringWithFormat:@"select returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_email,returnto_fax,returnto_phone from offline_order where so_id = '%@';",orderCode];
  5511. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5512. NSString *cid = [self textAtColumn:0 statement:stmt];
  5513. NSString *name = [self textAtColumn:1 statement:stmt];
  5514. NSString *ext = [self textAtColumn:2 statement:stmt];
  5515. NSString *contact = [self textAtColumn:3 statement:stmt];
  5516. NSString *email = [self textAtColumn:4 statement:stmt];
  5517. NSString *fax = [self textAtColumn:5 statement:stmt];
  5518. NSString *phone = [self textAtColumn:6 statement:stmt];
  5519. // count
  5520. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5521. // title
  5522. [dic setValue:@"Return To" forKey:@"title"];
  5523. // hide
  5524. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5525. // choose
  5526. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5527. [choose_dic setValue:@"choose" forKey:@"aname"];
  5528. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5529. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  5530. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5531. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  5532. @"returnto_contact" : @"sender_contact",
  5533. @"returnto_email" : @"sender_email",
  5534. @"returnto_ext" : @"sender_ext",
  5535. @"returnto_fax" : @"sender_fax",
  5536. @"returnto_name" : @"sender_name",
  5537. @"returnto_phone" : @"sender_phone"},
  5538. @"type" : @"pull"};
  5539. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  5540. NSDictionary *select_return_to_dic = @{
  5541. @"aname" : @"Select return to",
  5542. @"name" : @"select_cid",
  5543. @"refresh" : [NSNumber numberWithInteger:0],
  5544. @"value" : @"Contact_Return_To",
  5545. @"key_map" : @{
  5546. @"returnto_cid" : @"customer_cid",
  5547. @"returnto_contact" : @"customer_contact",
  5548. @"returnto_email" : @"customer_email",
  5549. @"returnto_ext" : @"customer_contact_ext",
  5550. @"returnto_fax" : @"customer_fax",
  5551. @"returnto_name" : @"customer_name",
  5552. @"returnto_phone" : @"customer_phone"
  5553. }
  5554. };
  5555. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  5556. [dic setValue:choose_dic forKey:@"item_0"];
  5557. // contact id
  5558. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5559. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5560. [contact_id_dic setValue:@"true" forKey:@"required"];
  5561. [dic setValue:contact_id_dic forKey:@"item_1"];
  5562. // company name
  5563. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5564. [dic setValue:company_name_dic forKey:@"item_2"];
  5565. // address
  5566. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5567. [dic setValue:ext_dic forKey:@"item_3"];
  5568. // contact
  5569. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5570. [dic setValue:contact_dic forKey:@"item_4"];
  5571. // phone
  5572. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5573. [dic setValue:phone_dic forKey:@"item_5"];
  5574. // fax
  5575. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5576. [dic setValue:fax_dic forKey:@"item_6"];
  5577. // email
  5578. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5579. [dic setValue:email_dic forKey:@"item_7"];
  5580. }];
  5581. return dic;
  5582. }
  5583. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  5584. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5585. orderCode = [self translateSingleQuote:orderCode];
  5586. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5587. __block double TotalCuft = 0;
  5588. __block double TotalWeight = 0;
  5589. __block int TotalCarton = 0;
  5590. __block double payments = 0;
  5591. // setting
  5592. NSDictionary *setting = params[@"setting"];
  5593. NSNumber *hide = setting[@"ModelInformationHide"];
  5594. [dic setValue:hide forKey:@"hide"];
  5595. NSString *sql = [NSString stringWithFormat:@"select c.item_id,c.item_count,m.stockUom,c.price,c.discount,m.name,m.description,c.line_note,m.availability from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id;",orderCode];
  5596. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5597. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5598. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  5599. // item id
  5600. int item_id = sqlite3_column_int(stmt, 0);
  5601. // count
  5602. int item_count = sqlite3_column_int(stmt, 1);
  5603. // stockUom
  5604. int stockUom = sqlite3_column_int(stmt, 2);
  5605. // unit price
  5606. NSString *str_price = [self textAtColumn:3 statement:stmt];
  5607. NSString* Price=nil;
  5608. if([str_price isEqualToString:@""])
  5609. {
  5610. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  5611. if(price==nil)
  5612. Price=@"No Price.";
  5613. else
  5614. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5615. }
  5616. else
  5617. {
  5618. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  5619. }
  5620. // discount
  5621. double discount = sqlite3_column_double(stmt, 4);
  5622. // name
  5623. NSString *name = [self textAtColumn:5 statement:stmt];
  5624. // description
  5625. NSString *description = [self textAtColumn:6 statement:stmt];
  5626. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  5627. // line note
  5628. NSString *line_note = [self textAtColumn:7 statement:stmt];
  5629. int avaulability = sqlite3_column_int(stmt, 8);
  5630. // img
  5631. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  5632. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  5633. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5634. if(combine != nil)
  5635. {
  5636. // int citem=0;
  5637. int bcount=[[combine valueForKey:@"count"] intValue];
  5638. for(int bc=0;bc<bcount;bc++)
  5639. {
  5640. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5641. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5642. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5643. subTotal += uprice * modulus * item_count;
  5644. }
  5645. }
  5646. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  5647. [model_dic setValue:@"model" forKey:@"control"];
  5648. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  5649. [model_dic setValue:description forKey:@"description"];
  5650. [model_dic setValue:line_note forKey:@"note"];
  5651. [model_dic setValue:img forKey:@"img_url"];
  5652. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  5653. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  5654. [model_dic setValue:Price forKey:@"unit_price"];
  5655. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  5656. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  5657. if (combine) {
  5658. [model_dic setValue:combine forKey:@"combine"];
  5659. }
  5660. // well,what under the row is the info for total
  5661. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5662. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5663. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5664. int carton=[bsubtotaljson[@"carton"] intValue];
  5665. TotalCuft += cuft;
  5666. TotalWeight += weight;
  5667. TotalCarton += carton;
  5668. payments += subTotal;
  5669. //---------------------------
  5670. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  5671. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  5672. }];
  5673. [dic setValue:@"Model Information" forKey:@"title"];
  5674. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  5675. return dic;
  5676. }
  5677. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  5678. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5679. [dic setValue:@"Remarks Content" forKey:@"title"];
  5680. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  5681. // setting
  5682. NSDictionary *setting = params[@"setting"];
  5683. NSNumber *hide = setting[@"RemarksContentHide"];
  5684. [dic setValue:hide forKey:@"hide"];
  5685. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5686. orderCode = [self translateSingleQuote:orderCode];
  5687. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  5688. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5689. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  5690. int mustCall = sqlite3_column_int(stmt, 1);
  5691. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  5692. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  5693. NSDictionary *po_dic = @{
  5694. @"aname" : @"PO#",
  5695. @"control" : @"edit",
  5696. @"keyboard" : @"text",
  5697. @"name" : @"poNumber",
  5698. @"value" : poNumber
  5699. };
  5700. NSDictionary *must_call_dic = @{
  5701. @"aname" : @"MUST CALL BEFORE DELIVERY",
  5702. @"control" : @"switch",
  5703. @"name" : @"must_call",
  5704. @"value" : mustCall ? @"true" : @"false"
  5705. };
  5706. NSDictionary *general_notes_dic = @{
  5707. @"aname" : @"General notes",
  5708. @"control" : @"text_view",
  5709. @"keyboard" : @"text",
  5710. @"name" : @"comments",
  5711. @"value" : generalNotes
  5712. };
  5713. NSDictionary *internal_notes_dic = @{
  5714. @"aname" : @"Internal notes",
  5715. @"control" : @"text_view",
  5716. @"keyboard" : @"text",
  5717. @"name" : @"internal_notes",
  5718. @"value" : internalNotes
  5719. };
  5720. [dic setValue:po_dic forKey:@"item_0"];
  5721. [dic setValue:must_call_dic forKey:@"item_1"];
  5722. [dic setValue:general_notes_dic forKey:@"item_2"];
  5723. [dic setValue:internal_notes_dic forKey:@"item_3"];
  5724. }];
  5725. return dic;
  5726. }
  5727. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  5728. // params
  5729. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5730. orderCode = [self translateSingleQuote:orderCode];
  5731. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5732. // setting
  5733. NSDictionary *setting = params[@"setting"];
  5734. NSNumber *hide = setting[@"OrderTotalHide"];
  5735. [dic setValue:hide forKey:@"hide"];
  5736. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  5737. __block double lift_gate_value = 0;
  5738. __block double handling_fee = 0;
  5739. __block double shipping = 0;
  5740. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5741. int lift_gate = sqlite3_column_int(stmt, 0);
  5742. lift_gate_value = sqlite3_column_double(stmt, 1);
  5743. shipping = sqlite3_column_double(stmt, 2);
  5744. handling_fee = sqlite3_column_double(stmt, 3);
  5745. if (!lift_gate) {
  5746. lift_gate_value = 0;
  5747. }
  5748. }];
  5749. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  5750. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  5751. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  5752. double payments = [[total valueForKey:@"payments"] doubleValue];
  5753. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  5754. double totalPrice = payments;
  5755. [dic setValue:@"Order Total" forKey:@"title"];
  5756. NSDictionary *payments_dic = @{
  5757. @"align" : @"right",
  5758. @"aname" : @"Payments/Credits",
  5759. @"control" : @"text",
  5760. @"name" : @"paymentsAndCredits",
  5761. @"type" : @"price",
  5762. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  5763. };
  5764. [dic setValue:payments_dic forKey:@"item_0"];
  5765. NSDictionary *handling_fee_dic = @{
  5766. @"align" : @"right",
  5767. @"aname" : @"Handling Fee",
  5768. @"control" : @"text",
  5769. @"name" : @"handling_fee_value",
  5770. @"required" : @"true",
  5771. @"type" : @"price",
  5772. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  5773. };
  5774. NSDictionary *shipping_dic = @{
  5775. @"align" : @"right",
  5776. @"aname" : @"Shipping*",
  5777. @"control" : @"text",
  5778. @"name" : @"shipping",
  5779. @"required" : @"true",
  5780. @"type" : @"price",
  5781. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  5782. };
  5783. NSDictionary *lift_gate_dic = @{
  5784. @"align" : @"right",
  5785. @"aname" : @"Liftgate Fee(No Loading Dock)",
  5786. @"control" : @"text",
  5787. @"name" : @"lift_gate_value",
  5788. @"required" : @"true",
  5789. @"type" : @"price",
  5790. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  5791. };
  5792. int item_count = 1;
  5793. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  5794. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  5795. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5796. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5797. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  5798. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5799. } else {
  5800. }
  5801. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5802. NSDictionary *total_price_dic = @{
  5803. @"align" : @"right",
  5804. @"aname" : @"Total",
  5805. @"control" : @"text",
  5806. @"name" : @"totalPrice",
  5807. @"type" : @"price",
  5808. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  5809. };
  5810. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5811. NSDictionary *total_cuft_dic = @{
  5812. @"align" : @"right",
  5813. @"aname" : @"Total Cuft",
  5814. @"control" : @"text",
  5815. @"name" : @"",
  5816. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  5817. };
  5818. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5819. NSDictionary *total_weight_dic = @{
  5820. @"align" : @"right",
  5821. @"aname" : @"Total Weight",
  5822. @"control" : @"text",
  5823. @"name" : @"",
  5824. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  5825. };
  5826. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5827. NSDictionary *total_carton_dic = @{
  5828. @"align" : @"right",
  5829. @"aname" : @"Total Carton",
  5830. @"control" : @"text",
  5831. @"name" : @"",
  5832. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  5833. };
  5834. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5835. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  5836. return dic;
  5837. }
  5838. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  5839. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5840. orderCode = [self translateSingleQuote:orderCode];
  5841. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5842. // setting
  5843. NSDictionary *setting = params[@"setting"];
  5844. NSNumber *hide = setting[@"SignatureHide"];
  5845. [dic setValue:hide forKey:@"hide"];
  5846. [dic setValue:@"Signature" forKey:@"title"];
  5847. __block NSString *pic_path = @"";
  5848. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  5849. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5850. pic_path = [self textAtColumn:0 statement:stmt];
  5851. }];
  5852. NSDictionary *pic_dic = @{
  5853. @"aname" : @"Signature",
  5854. @"avalue" :pic_path,
  5855. @"control" : @"signature",
  5856. @"name" : @"sign_picpath",
  5857. @"value" : pic_path
  5858. };
  5859. [dic setValue:pic_dic forKey:@"item_0"];
  5860. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5861. return dic;
  5862. }
  5863. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  5864. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5865. // setting
  5866. NSDictionary *setting = params[@"setting"];
  5867. NSNumber *hide = setting[@"ShippingMethodHide"];
  5868. [dic setValue:hide forKey:@"hide"];
  5869. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5870. orderCode = [self translateSingleQuote:orderCode];
  5871. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  5872. __block NSString *logist = @"";
  5873. __block NSString *lift_gate = @"";
  5874. __block int lift_gate_integer = 0;
  5875. __block NSString *logistic_note = @"";
  5876. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5877. logist = [self textAtColumn:0 statement:stmt];
  5878. lift_gate_integer = sqlite3_column_int(stmt, 1);
  5879. logistic_note = [self textAtColumn:2 statement:stmt];
  5880. }];
  5881. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5882. [dic setValue:@"Shipping Method" forKey:@"title"];
  5883. // val_0
  5884. int PERSONAL_PICK_UP_check = 0;
  5885. int USE_MY_CARRIER_check = 0;
  5886. NSString *logistic_note_text = @"";
  5887. int will_call_check = 0;
  5888. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  5889. will_call_check = 1;
  5890. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  5891. PERSONAL_PICK_UP_check = 1;
  5892. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  5893. USE_MY_CARRIER_check = 1;
  5894. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  5895. logistic_note = [logistic_note_array firstObject];
  5896. if (logistic_note_array.count > 1) {
  5897. logistic_note_text = [logistic_note_array lastObject];
  5898. }
  5899. }
  5900. }
  5901. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  5902. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  5903. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  5904. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  5905. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  5906. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5907. NSDictionary *val0_subItem_item0 = @{
  5908. @"aname" : @"Option",
  5909. @"cadedate" : @{
  5910. @"count" : [NSNumber numberWithInteger:2],
  5911. @"val_0" : @{
  5912. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  5913. @"refresh" : [NSNumber numberWithInteger:0],
  5914. @"value" : @"PERSONAL PICK UP",
  5915. @"value_id" : @"PERSONAL PICK UP"
  5916. },
  5917. @"val_1" : @{
  5918. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  5919. @"refresh" : [NSNumber numberWithInteger:0],
  5920. @"sub_item" : @{
  5921. @"count" : [NSNumber numberWithInteger:1],
  5922. @"item_0" : @{
  5923. @"aname" : @"BOL",
  5924. @"control" : @"edit",
  5925. @"keyboard" : @"text",
  5926. @"name" : @"logist_note_text",
  5927. @"refresh" : [NSNumber numberWithInteger:0],
  5928. @"required" : @"false",
  5929. @"value" : logistic_note_text
  5930. }
  5931. },
  5932. @"value" : @"USE MY CARRIER",
  5933. @"value_id" : @"USE MY CARRIER"
  5934. }
  5935. },
  5936. @"control" : @"enum",
  5937. @"name" : @"logistic_note",
  5938. @"required" : @"true",
  5939. @"single_select" : @"true"
  5940. };
  5941. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  5942. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  5943. // val_1
  5944. lift_gate = [self valueInParams:params key:@"lift_gate"];
  5945. if([lift_gate isEqualToString:@""]) {
  5946. if (lift_gate_integer == 1) {
  5947. lift_gate = @"true";
  5948. } else {
  5949. lift_gate = @"false";
  5950. }
  5951. }
  5952. int common_carrier_check = 0;
  5953. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  5954. common_carrier_check = 1;
  5955. [params setValue:lift_gate forKey:@"lift_gate"];
  5956. }
  5957. NSDictionary *val_1 = @{
  5958. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  5959. @"sub_item" : @{
  5960. @"count" : [NSNumber numberWithInteger:1],
  5961. @"item_0" : @{
  5962. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  5963. @"control" : @"switch",
  5964. @"name" : @"lift_gate",
  5965. @"refresh" : [NSNumber numberWithInteger:1],
  5966. @"required" : @"true",
  5967. @"value" : lift_gate
  5968. }
  5969. },
  5970. @"value" : @"COMMON CARRIER",
  5971. @"value_id" : @"COMMON CARRIER"
  5972. };
  5973. // cadedate
  5974. NSDictionary *cadedate = @{
  5975. @"count" : [NSNumber numberWithInteger:2],
  5976. @"val_0" : val_0,
  5977. @"val_1" : val_1
  5978. };
  5979. // item_0
  5980. NSMutableDictionary *item_0 = @{
  5981. @"aname" : @"Shipping",
  5982. @"cadedate" : cadedate,
  5983. @"control" : @"enum",
  5984. @"name" : @"logist",
  5985. @"refresh" : [NSNumber numberWithInteger:1],
  5986. @"single_select" : @"true",
  5987. }.mutableCopy;
  5988. NSNumber *required = setting[@"ShippingMethodRequire"];
  5989. if ([required integerValue]) {
  5990. [item_0 setValue:@"true" forKey:@"required"];
  5991. }
  5992. [dic setValue:item_0 forKey:@"item_0"];
  5993. return dic;
  5994. }
  5995. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  5996. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5997. orderCode = [self translateSingleQuote:orderCode];
  5998. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  5999. __block int submit_as_integer = 0;
  6000. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6001. submit_as_integer = sqlite3_column_int(stmt, 0);
  6002. }];
  6003. int check11 = 0;
  6004. int check10 = 0;
  6005. if (submit_as_integer == 11) {
  6006. check11 = 1;
  6007. }
  6008. if (submit_as_integer == 10) {
  6009. check10 = 1;
  6010. }
  6011. // section_0
  6012. NSMutableDictionary *dic = @{
  6013. @"count" : @(1),
  6014. @"item_0" : @{
  6015. @"aname" : @"Submit Order As",
  6016. @"cadedate" : @{
  6017. @"count" : @(2),
  6018. @"val_0" : @{
  6019. @"check" : [NSNumber numberWithInteger:check11],
  6020. @"value" : @"Sales Order",
  6021. @"value_id" : @(11)
  6022. },
  6023. @"val_1" : @{
  6024. @"check" : [NSNumber numberWithInteger:check10],
  6025. @"value" : @"Quote",
  6026. @"value_id" : @(10)
  6027. }
  6028. },
  6029. @"control" : @"enum",
  6030. @"name" : @"erpOrderStatus",
  6031. @"required" : @"true",
  6032. @"single_select" : @"true"
  6033. },
  6034. @"title" : @"Order Type"
  6035. }.mutableCopy;
  6036. // setting
  6037. NSDictionary *setting = params[@"setting"];
  6038. NSNumber *hide = setting[@"OrderTypeHide"];
  6039. [dic setValue:hide forKey:@"hide"];
  6040. return dic;
  6041. }
  6042. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  6043. countryCode = [self translateSingleQuote:countryCode];
  6044. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  6045. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6046. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  6047. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  6048. if (name == NULL) {
  6049. name = "";
  6050. }
  6051. if (code == NULL) {
  6052. code = "";
  6053. }
  6054. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6055. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  6056. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  6057. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6058. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  6059. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6060. }
  6061. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  6062. [container setValue:stateDic forKey:key];
  6063. }] mutableCopy];
  6064. // failure 可以不用了,一样的
  6065. if (ret.allKeys.count == 0) {
  6066. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6067. [stateDic setValue:@"Other" forKey:@"value"];
  6068. [stateDic setValue:@"" forKey:@"value_id"];
  6069. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6070. if (state_code && [@"" isEqualToString:state_code]) {
  6071. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6072. }
  6073. NSString *key = [NSString stringWithFormat:@"val_0"];
  6074. [ret setValue:stateDic forKey:key];
  6075. }
  6076. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  6077. return ret;
  6078. }
  6079. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6080. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6081. orderCode = [self translateSingleQuote:orderCode];
  6082. NSString *sql = [NSString stringWithFormat:@"select paymentType,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,decrypt(credit_card_number),decrypt(credit_card_security_code),decrypt(credit_card_expiration_month),decrypt(credit_card_expiration_year),credit_card_city,credit_card_state from offline_order where so_id = '%@';",orderCode];
  6083. __block NSString *payType = @"";
  6084. __block NSString *firstName = @"";
  6085. __block NSString *lastName = @"";
  6086. __block NSString *addr1 = @"";
  6087. __block NSString *addr2 = @"";
  6088. __block NSString *zipcode = @"";
  6089. __block NSString *cardType = @"";
  6090. __block NSString *cardNumber = @"";
  6091. __block NSString *securityCode = @"";
  6092. __block NSString *month = @"";
  6093. __block NSString *year = @"";
  6094. __block NSString *city = @"";
  6095. __block NSString *state = @"";
  6096. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6097. payType = [self textAtColumn:0 statement:stmt];
  6098. firstName = [self textAtColumn:1 statement:stmt];
  6099. lastName = [self textAtColumn:2 statement:stmt];
  6100. addr1 = [self textAtColumn:3 statement:stmt];
  6101. addr2 = [self textAtColumn:4 statement:stmt];
  6102. zipcode = [self textAtColumn:5 statement:stmt];
  6103. cardType = [self textAtColumn:6 statement:stmt];
  6104. cardNumber = [self textAtColumn:7 statement:stmt];
  6105. securityCode = [self textAtColumn:8 statement:stmt];
  6106. month = [self textAtColumn:9 statement:stmt];
  6107. year = [self textAtColumn:10 statement:stmt];
  6108. city = [self textAtColumn:11 statement:stmt];
  6109. state = [self textAtColumn:12 statement:stmt];
  6110. }];
  6111. NSString *required = @"true";
  6112. // setting
  6113. NSDictionary *setting = params[@"setting"];
  6114. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  6115. if ([requiredNumber integerValue]) {
  6116. required = @"true";
  6117. } else {
  6118. required = @"false";
  6119. }
  6120. // "section_2"
  6121. NSMutableDictionary *dic = @{
  6122. @"count" : @(1),
  6123. @"item_0" : @{
  6124. @"aname" : @"Payment",
  6125. @"required" : required,
  6126. @"cadedate" : @{
  6127. @"count" : @(6),
  6128. @"val_0" : @{
  6129. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  6130. @"value" : @"Check",
  6131. @"value_id" : @"Check"
  6132. },
  6133. @"val_1" : @{
  6134. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  6135. @"value" : @"Cash",
  6136. @"value_id" : @"Cash"
  6137. },
  6138. @"val_2" : @{
  6139. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  6140. @"value" : @"NET 30",
  6141. @"value_id" : @"NET 30"
  6142. },
  6143. @"val_3" : @{
  6144. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  6145. @"value" : @"Wire Transfer",
  6146. @"value_id" : @"Wire Transfer"
  6147. },
  6148. @"val_4" : @{
  6149. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  6150. @"sub_item" : @{
  6151. @"count" : @(3),
  6152. @"item_0" : @{
  6153. @"aname" : @"choose",
  6154. @"control" : @"multi_action",
  6155. @"count" : @(1),
  6156. @"item_0" : @{
  6157. @"aname" : @"Same as customer",
  6158. @"key_map" : @{
  6159. @"credit_card_address1" : @"customer_address1",
  6160. @"credit_card_address2" : @"customer_address2",
  6161. @"credit_card_city" : @"customer_city",
  6162. @"credit_card_first_name" : @"customer_first_name",
  6163. @"credit_card_last_name" : @"customer_last_name",
  6164. @"credit_card_state" : @"customer_state",
  6165. @"credit_card_zipcode" : @"customer_zipcode"
  6166. },
  6167. @"type" : @"pull"
  6168. }
  6169. },
  6170. @"item_1" : @{
  6171. @"aname" : @"",
  6172. @"color" : @"red",
  6173. @"control" : @"text",
  6174. @"name" : @"",
  6175. @"value" : @"USA Credit cards only"
  6176. },
  6177. @"item_2" : @{
  6178. @"aname" : @"Fill",
  6179. @"cadedate" : @{
  6180. @"count" : @(2),
  6181. @"val_0" : @{
  6182. @"check" : @(1),
  6183. @"sub_item" : @{
  6184. @"count" : @(11),
  6185. @"item_0" : @{
  6186. @"aname" : @"Type",
  6187. @"cadedate" : @{
  6188. @"count" : @(2),
  6189. @"val_0" : @{
  6190. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  6191. @"value" : @"VISA",
  6192. @"value_id" : @(0)
  6193. },
  6194. @"val_1" : @{
  6195. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  6196. @"value" : @"MASTER CARD",
  6197. @"value_id" : @(1)
  6198. }
  6199. },
  6200. @"control" : @"enum",
  6201. @"name" : @"credit_card_type",
  6202. @"required" : @"true",
  6203. @"single_select" : @"true"
  6204. },
  6205. @"item_1" : @{
  6206. @"aname" : @"Number",
  6207. @"control" : @"edit",
  6208. @"keyboard" : @"int",
  6209. @"length" : @"16",
  6210. @"name" : @"credit_card_number",
  6211. @"required" : @"true",
  6212. @"value" : cardNumber
  6213. },
  6214. @"item_10" : @{
  6215. @"aname" : @"State",
  6216. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  6217. @"control" : @"enum",
  6218. @"enum" : @"true",
  6219. @"name" : @"credit_card_state",
  6220. @"required" : @"true",
  6221. @"single_select" : @"true"
  6222. },
  6223. @"item_2" : @{
  6224. @"aname" : @"Expiration Date",
  6225. @"control" : @"monthpicker",
  6226. @"name" : @"credit_card_expiration",
  6227. @"required" : @"true",
  6228. @"type" : @"date",
  6229. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  6230. },
  6231. @"item_3" : @{
  6232. @"aname" : @"Security Code",
  6233. @"control" : @"edit",
  6234. @"keyboard" : @"int",
  6235. @"length" : @"3",
  6236. @"name" : @"credit_card_security_code",
  6237. @"required" : @"true",
  6238. @"value" : securityCode
  6239. },
  6240. @"item_4" : @{
  6241. @"aname" : @"First Name",
  6242. @"control" : @"edit",
  6243. @"keyboard" : @"text",
  6244. @"name" : @"credit_card_first_name",
  6245. @"required" : @"true",
  6246. @"value" : firstName
  6247. },
  6248. @"item_5" : @{
  6249. @"aname" : @"Last Name",
  6250. @"control" : @"edit",
  6251. @"keyboard" : @"text",
  6252. @"name" : @"credit_card_last_name",
  6253. @"required" : @"true",
  6254. @"value" : lastName
  6255. },
  6256. @"item_6" : @{
  6257. @"aname" : @"Address 1",
  6258. @"control" : @"edit",
  6259. @"keyboard" : @"text",
  6260. @"name" : @"credit_card_address1",
  6261. @"required" : @"true",
  6262. @"value" : addr1
  6263. },
  6264. @"item_7" : @{
  6265. @"aname" : @"Address 2",
  6266. @"control" : @"edit",
  6267. @"keyboard" : @"text",
  6268. @"name" : @"credit_card_address2",
  6269. @"value" : addr2
  6270. },
  6271. @"item_8" : @{
  6272. @"aname" : @"zip code",
  6273. @"control" : @"edit",
  6274. @"keyboard" : @"text",
  6275. @"name" : @"credit_card_zipcode",
  6276. @"required" : @"true",
  6277. @"value" : zipcode
  6278. },
  6279. @"item_9" : @{
  6280. @"aname" : @"City",
  6281. @"control" : @"edit",
  6282. @"keyboard" : @"text",
  6283. @"name" : @"credit_card_city",
  6284. @"required" : @"true",
  6285. @"value" : city
  6286. }
  6287. },
  6288. @"value" : @"Fill Now",
  6289. @"value_id" : @""
  6290. },
  6291. @"val_1" : @{
  6292. @"check" : @(0),
  6293. @"value" : @"Fill Later",
  6294. @"value_id" : @""
  6295. }
  6296. },
  6297. @"control" : @"enum",
  6298. @"name" : @"",
  6299. @"single_select" : @"true"
  6300. }
  6301. },
  6302. @"value" : @"Credit Card",
  6303. @"value_id" : @"Credit Card"
  6304. },
  6305. @"val_5" : @{
  6306. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  6307. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  6308. @"value_id" : @"FOLLOW EXISTING"
  6309. }
  6310. },
  6311. @"control" : @"enum",
  6312. @"name" : @"paymentType",
  6313. @"single_select" : @"true"
  6314. },
  6315. @"title" : @"Payment Information"
  6316. }.mutableCopy;
  6317. NSNumber *hide = setting[@"PaymentInformationHide"];
  6318. [dic setValue:hide forKey:@"hide"];
  6319. return dic;
  6320. }
  6321. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  6322. {
  6323. sqlite3 *db = [iSalesDB get_db];
  6324. // UISetting
  6325. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  6326. NSString *cachefolder = [paths objectAtIndex:0];
  6327. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  6328. NSData* json =nil;
  6329. json=[NSData dataWithContentsOfFile:img_cache];
  6330. NSError *error=nil;
  6331. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  6332. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  6333. [params setObject:setting forKey:@"setting"];
  6334. int section_count = 0;
  6335. // params
  6336. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6337. orderCode = [self translateSingleQuote:orderCode];
  6338. // 0 Order Type 1 Shipping Method 2 Payment Information
  6339. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  6340. // 0 Order Type
  6341. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  6342. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  6343. [ret setValue:order_type_dic forKey:key0];
  6344. // 1 Shipping Method
  6345. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  6346. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  6347. [ret setValue:shipping_method_dic forKey:key1];
  6348. // 2 Payment Information
  6349. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  6350. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  6351. [ret setValue:payment_info_dic forKey:key2];
  6352. // 3 Customer
  6353. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  6354. NSDictionary *customer_dic = [self customerDic:params db:db];
  6355. [ret setValue:customer_dic forKey:key3];
  6356. // 4 Ship To
  6357. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  6358. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  6359. [ret setValue:ship_to_dic forKey:key4];
  6360. // 5 Ship From
  6361. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  6362. if (![shipFromDisable integerValue]) {
  6363. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  6364. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  6365. [ret setValue:ship_from_dic forKey:key5];
  6366. }
  6367. // 6 Freight Bill To
  6368. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  6369. if (![freightBillToDisable integerValue]) {
  6370. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  6371. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  6372. [ret setValue:freight_bill_to forKey:key6];
  6373. }
  6374. // 7 Merchandise Bill To
  6375. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  6376. if (![merchandiseBillToDisable integerValue]) {
  6377. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  6378. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  6379. [ret setValue:merchandise_bill_to_dic forKey:key7];
  6380. }
  6381. // 8 Return To
  6382. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  6383. if (![returnToDisable integerValue]) {
  6384. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  6385. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  6386. [ret setValue:return_to_dic forKey:key8];
  6387. }
  6388. // 9 Model Information
  6389. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  6390. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  6391. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  6392. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  6393. [ret setValue:model_info_dic forKey:key9];
  6394. // 10 Remarks Content
  6395. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  6396. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  6397. [ret setValue:remarks_content_dic forKey:key10];
  6398. // 11 Order Total
  6399. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  6400. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  6401. [ret setValue:order_total_dic forKey:key11];
  6402. // 12 Signature
  6403. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  6404. NSDictionary *sign_dic = [self signatureDic:params db:db];
  6405. [ret setValue:sign_dic forKey:key12];
  6406. [ret setValue:@(section_count) forKey:@"section_count"];
  6407. [iSalesDB close_db:db];
  6408. return [RAUtils dict2data:ret];
  6409. // return nil;
  6410. }
  6411. #pragma mark addr editor
  6412. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  6413. NSMutableDictionary *new_item = [item mutableCopy];
  6414. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  6415. return new_item;
  6416. }
  6417. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  6418. {
  6419. // "is_subaction" = true;
  6420. // orderCode = MOB1608240002;
  6421. // password = 123456;
  6422. // "subaction_tag" = 1;
  6423. // user = EvanK;
  6424. // {
  6425. // "is_subaction" = true;
  6426. // orderCode = MOB1608240002;
  6427. // password = 123456;
  6428. // "refresh_trigger" = zipcode;
  6429. // "subaction_tag" = 1;
  6430. // user = EvanK;
  6431. // }
  6432. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  6433. [ret removeObjectForKey:@"up_params"];
  6434. [ret setObject:@"New Address" forKey:@"title"];
  6435. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  6436. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  6437. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  6438. NSMutableDictionary *country_dic = [section_0[@"item_5"] mutableCopy];
  6439. [country_dic removeObjectForKey:@"refresh"];
  6440. [country_dic removeObjectForKey:@"restore"];
  6441. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  6442. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  6443. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_6"] add:YES] forKey:@"item_2"]; // Address 1*
  6444. [new_section_0 setObject:section_0[@"item_7"] forKey:@"item_3"]; // Address 2
  6445. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_4"]; // Address 3
  6446. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_5"]; // Address 4
  6447. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_10"] add:YES];
  6448. [zip_code_dic removeObjectForKey:@"refresh"];
  6449. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  6450. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_11"] add:YES] forKey:@"item_7"]; // State/Province*
  6451. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_8"]; // City*
  6452. [new_section_0 setObject:section_0[@"item_13"] forKey:@"item_9"]; // Contact First Name
  6453. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_10"]; // Contact Last Name
  6454. [new_section_0 setObject:section_0[@"item_4"] forKey:@"item_11"]; // Phone*
  6455. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_12"]; // Fax
  6456. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_3"] add:NO] forKey:@"item_13"]; // Email
  6457. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  6458. // country
  6459. NSString *countryCode = @"US";
  6460. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6461. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  6462. // state
  6463. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  6464. NSDictionary *tmpDic = @{
  6465. @"value" : @"Other",
  6466. @"value_id" : @"",
  6467. @"check" : [NSNumber numberWithInteger:0]
  6468. };
  6469. for (int i = 0; i < allState.allKeys.count; i++) {
  6470. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  6471. NSDictionary *tmp = allState[key];
  6472. [allState setValue:tmpDic forKey:key];
  6473. tmpDic = tmp;
  6474. }
  6475. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  6476. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  6477. [ret setValue:new_section_0 forKey:@"section_0"];
  6478. return [RAUtils dict2data:ret];
  6479. }
  6480. #pragma mark save addr
  6481. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  6482. {
  6483. // NSString *companyName = [self valueInParams:params key:@"company"];
  6484. // NSString *addr1 = [self valueInParams:params key:@"address"];
  6485. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  6486. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  6487. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  6488. // NSString *countryId = [self valueInParams:params key:@"country"];
  6489. // NSString *stateCode = [self valueInParams:params key:@"state"];
  6490. // NSString *city = [self valueInParams:params key:@"city"];
  6491. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  6492. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  6493. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  6494. // NSString *phone = [self valueInParams:params key:@"phone"];
  6495. // NSString *fax = [self valueInParams:params key:@"fax"];
  6496. // NSString *email = [self valueInParams:params key:@"email"];
  6497. return [self offline_saveContact:params update:NO isCustomer:NO];
  6498. }
  6499. #pragma mark cancel order
  6500. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  6501. {
  6502. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6503. NSString *order_id = [self valueInParams:params key:@"orderId"];
  6504. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  6505. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  6506. int ret = [iSalesDB execSql:sql];
  6507. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6508. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6509. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6510. // [dic setValue:@"160409" forKey:@"min_ver"];
  6511. return [RAUtils dict2data:dic];
  6512. }
  6513. #pragma mark sign order
  6514. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  6515. {
  6516. //参考 offline_saveBusinesscard
  6517. NSLog(@"sign order params: %@",params);
  6518. // orderCode = MOB1608240002;
  6519. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  6520. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6521. orderCode = [self translateSingleQuote:orderCode];
  6522. NSString *picPath = [self valueInParams:params key:@"picpath"];
  6523. picPath = [self translateSingleQuote:picPath];
  6524. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  6525. int ret = [iSalesDB execSql:sql];
  6526. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6527. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6528. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6529. // [dic setValue:@"160409" forKey:@"min_ver"];
  6530. return [RAUtils dict2data:dic];
  6531. }
  6532. #pragma mark save order
  6533. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  6534. NSString *ret = [self valueInParams:params key:key];
  6535. if (translate) {
  6536. ret = [self translateSingleQuote:ret];
  6537. }
  6538. return ret;
  6539. }
  6540. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  6541. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  6542. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  6543. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  6544. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  6545. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  6546. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  6547. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  6548. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  6549. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  6550. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  6551. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  6552. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  6553. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  6554. if (![total_price isEqualToString:@""]) {
  6555. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  6556. } else {
  6557. total_price = @"";
  6558. }
  6559. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  6560. if ([paymentsAndCredits isEqualToString:@""]) {
  6561. paymentsAndCredits = @"";
  6562. } else {
  6563. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  6564. }
  6565. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  6566. if ([handling_fee_value isEqualToString:@""]) {
  6567. handling_fee_value = @"";
  6568. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  6569. handling_fee_value = @"";
  6570. } else {
  6571. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  6572. }
  6573. NSString *handling_fee_placeholder = handling_fee_value;
  6574. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  6575. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  6576. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  6577. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  6578. NSString *lift_gate_placeholder = @"";
  6579. if ([lift_gate_value isEqualToString:@""]) {
  6580. lift_gate_placeholder = @"";
  6581. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  6582. lift_gate_placeholder = @"";
  6583. } else {
  6584. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  6585. }
  6586. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  6587. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  6588. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  6589. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  6590. logistic_note = [logistic_note isEqualToString:@"USE MY CARRIER"] ? [NSString stringWithFormat:@"logistic_note = '%@\n\t%@',",logistic_note,logistic_note_text] : [NSString stringWithFormat:@"logistic_note = '%@',",logistic_note];
  6591. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  6592. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  6593. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  6594. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  6595. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  6596. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  6597. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  6598. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  6599. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  6600. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  6601. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  6602. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  6603. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  6604. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  6605. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  6606. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  6607. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  6608. BOOL number_nil = [credit_card_number isEqualToString:@""];
  6609. if (!number_nil) {
  6610. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  6611. }
  6612. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  6613. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  6614. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  6615. if (!security_code_nil) {
  6616. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  6617. }
  6618. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  6619. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  6620. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  6621. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  6622. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  6623. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  6624. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  6625. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  6626. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  6627. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  6628. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  6629. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  6630. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  6631. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  6632. NSString *contact_id = customer_cid;
  6633. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  6634. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  6635. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  6636. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  6637. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  6638. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  6639. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  6640. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  6641. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  6642. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  6643. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  6644. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  6645. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  6646. //
  6647. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  6648. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  6649. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  6650. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  6651. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  6652. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  6653. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  6654. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  6655. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  6656. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  6657. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  6658. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  6659. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  6660. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  6661. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  6662. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  6663. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  6664. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  6665. NSString *contactSql = [NSString stringWithFormat:@"update offline_contact set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ where contact_id = '%@'",contact_contact,contact_email,contact_phone,contact_fax,customer_name,customer_address1,customer_address2,customer_address3,customer_address4,customer_country,customer_state,customer_city,customer_zipcode,contact_id];
  6666. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  6667. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  6668. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  6669. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  6670. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  6671. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  6672. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  6673. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  6674. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  6675. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  6676. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  6677. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  6678. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  6679. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  6680. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  6681. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  6682. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  6683. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  6684. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  6685. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  6686. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  6687. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  6688. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  6689. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  6690. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  6691. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  6692. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  6693. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  6694. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  6695. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  6696. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  6697. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  6698. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  6699. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  6700. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  6701. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  6702. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  6703. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  6704. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  6705. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  6706. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  6707. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  6708. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  6709. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  6710. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  6711. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  6712. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  6713. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  6714. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  6715. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  6716. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  6717. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  6718. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  6719. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  6720. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  6721. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  6722. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  6723. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  6724. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  6725. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  6726. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  6727. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  6728. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  6729. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  6730. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  6731. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  6732. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  6733. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  6734. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  6735. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  6736. NSString *order_status = @"status = 1,";
  6737. if (submit) {
  6738. order_status = @"status = -11,";
  6739. }
  6740. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  6741. NSString *sync_sql = @"";
  6742. if (submit) {
  6743. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  6744. [param setValue:sales_rep forKey:@"sales_rep"];
  6745. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  6746. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  6747. }
  6748. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ where so_id = '%@';",order_status,general_notes,internal_notes,poNumber,must_call,sign_picpath,total_price,paymentsAndCredits,handling_fee_placeholder,lift_gate,lift_gate_placeholder,logist,logistic_note,erpOrderStatus,paymentType,credit_card_address1,credit_card_address2,credit_card_city,credit_card_first_name,credit_card_last_name,credit_card_number,credit_card_security_code,credit_card_state,credit_card_type,credit_card_zipcode,credit_card_expiration_year,credit_card_expiration_month,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_email,receive_fax,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,so_id,sync_sql,so_id];
  6749. DebugLog(@"save order contactSql: %@",contactSql);
  6750. DebugLog(@"save order orderSql: %@",orderSql);
  6751. // int contact_ret = [iSalesDB execSql:contactSql];
  6752. int order_ret = [iSalesDB execSql:orderSql];
  6753. int ret = order_ret;
  6754. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6755. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6756. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6757. // [dic setValue:@"160409" forKey:@"min_ver"];
  6758. [dic setObject:so_id forKey:@"so#"];
  6759. return [RAUtils dict2data:dic];
  6760. }
  6761. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  6762. {
  6763. // id foo = nil;
  6764. // NSMutableArray *a = @[].mutableCopy;
  6765. // [a addObject:foo];
  6766. return [self saveorder:param submit:NO];
  6767. }
  6768. #pragma mark add to cart by name
  6769. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  6770. {
  6771. NSString *orderCode = [params objectForKey:@"orderCode"];
  6772. NSString *product_name = [params objectForKey:@"product_name"];
  6773. product_name = [self translateSingleQuote:product_name];
  6774. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  6775. sqlite3 *db = [iSalesDB get_db];
  6776. __block NSMutableString *product_id_string = [NSMutableString string];
  6777. for (int i = 0; i < product_name_array.count; i++) {
  6778. NSString *name = [product_name_array objectAtIndex:i];
  6779. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  6780. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6781. int product_id = sqlite3_column_int(stmt, 0);
  6782. if (i == product_name_array.count - 1) {
  6783. [product_id_string appendFormat:@"%d",product_id];
  6784. } else {
  6785. [product_id_string appendFormat:@"%d,",product_id];
  6786. }
  6787. }];
  6788. }
  6789. NSDictionary *newParams = @{
  6790. @"product_id" : product_id_string,
  6791. @"orderCode" : orderCode
  6792. };
  6793. [iSalesDB close_db:db];
  6794. return [self offline_add2cart:[newParams mutableCopy]];
  6795. }
  6796. #pragma mark reset order
  6797. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  6798. {
  6799. UIApplication * app = [UIApplication sharedApplication];
  6800. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  6801. appDelegate.disable_trigger=true;
  6802. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  6803. appDelegate.disable_trigger=false;
  6804. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6805. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  6806. return [RAUtils dict2data:dic];
  6807. }
  6808. #pragma mark submit order
  6809. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  6810. {
  6811. return [self saveorder:params submit:YES];
  6812. }
  6813. #pragma mark copy order
  6814. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  6815. {
  6816. NSMutableDictionary *ret = @{}.mutableCopy;
  6817. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  6818. sqlite3 *db = [iSalesDB get_db];
  6819. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6820. // 首先查看联系人是否active
  6821. NSString *customer_sql = [NSString stringWithFormat:@"select is_active from offline_contact where contact_id in (select customer_cid from offline_order where so_id = '%@');",order_code];
  6822. __block int customer_is_active = 1;
  6823. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6824. customer_is_active = sqlite3_column_int(stmt, 0);
  6825. }];
  6826. if (!customer_is_active) {
  6827. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  6828. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6829. [iSalesDB close_db:db];
  6830. return [RAUtils dict2data:ret];
  6831. }
  6832. // new order
  6833. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  6834. NSString *new_order_code = [self get_offline_soid:db];
  6835. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  6836. user = [self translateSingleQuote:user];
  6837. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,total_price,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext) select '%@',1,submit_as,internal_notes,sales_rep,'%@',total_price,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  6838. __block int result = 1;
  6839. result = [iSalesDB execSql:insert_order_sql db:db];
  6840. if (!result) {
  6841. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  6842. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6843. [iSalesDB close_db:db];
  6844. return [RAUtils dict2data:ret];
  6845. }
  6846. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  6847. // __block NSString *product_id = @"";
  6848. __weak typeof(self) weakSelf = self;
  6849. NSString *product_id_sql = [NSString stringWithFormat:@"select product_id,is_active from product WHERE product_id in (select product_id from offline_cart where so_no = '%@');",order_code];
  6850. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6851. int is_active = sqlite3_column_int(stmt, 1);
  6852. if (is_active) {
  6853. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  6854. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  6855. NSString *insert_cart_sql = [NSString stringWithFormat:@"insert into offline_cart (so_no,orderitem_id,product_id,item_id,item_count,bundle_item,type) select '%@',orderitem_id,product_id,item_id,item_count,bundle_item,type from offline_cart where product_id = %@ and so_no = '%@'",new_order_code,product_id,order_code];
  6856. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  6857. }
  6858. }];
  6859. // product_id = [product_id substringFromIndex:1];
  6860. //
  6861. // [self offline_add2cart:@{}.mutableCopy];
  6862. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6863. [iSalesDB close_db:db];
  6864. if (result) {
  6865. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  6866. } else {
  6867. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  6868. }
  6869. [ret setObject:new_order_code forKey:@"so_id"];
  6870. return [RAUtils dict2data:ret];
  6871. }
  6872. #pragma mark move wish list to cart
  6873. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  6874. NSString *collectId = params[@"collectId"];
  6875. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  6876. __block NSString *product_id = @"";
  6877. __block NSString *qty = @"";
  6878. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6879. int productId = sqlite3_column_int(stmt, 0);
  6880. int item_qty = sqlite3_column_int(stmt, 1);
  6881. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  6882. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  6883. }];
  6884. product_id = [product_id substringFromIndex:1];
  6885. qty = [qty substringFromIndex:1];
  6886. NSDictionary *newParams = @{
  6887. @"product_id" : product_id,
  6888. @"orderCode" : params[@"orderCode"],
  6889. @"qty" : qty
  6890. };
  6891. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  6892. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  6893. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  6894. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  6895. //
  6896. // int ret = [iSalesDB execSql:deleteSQL];
  6897. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  6898. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  6899. }
  6900. return [RAUtils dict2data:retDic];
  6901. }
  6902. @end